freemyipod r935 - Code Review

Jump to: navigation, search
Repository:freemyipod
Revision:r934‎ | r935 | r936 >
Date:11:38, 6 June 2014
Author:theseven
Status:new
Tags:
Comment:
emCORE: Fix rename() crashes and some mess surrounding open() return values, make rename() support renaming directories
Modified paths:
  • /emcore/trunk/file.c (modified) (history)

Diff [purge]

Index: emcore/trunk/file.c
@@ -60,7 +60,7 @@
6161 return open(pathname, O_WRONLY|O_CREAT|O_TRUNC, 0666);
6262 }
6363
64 -static int open_internal(const char* pathname, int flags, bool use_cache)
 64+static int open_internal(const char* pathname, int flags, bool ignoredir)
6565 {
6666 DIR* dir;
6767 struct dirent* entry;
@@ -75,7 +75,7 @@
7676 DEBUGF("'%s' is not an absolute path.", pathname);
7777 DEBUGF("Only absolute pathnames supported at the moment");
7878 errno = EINVAL;
79 - return -1;
 79+ return 0;
8080 }
8181
8282 file = (struct filedesc*)memalign(0x10, sizeof(struct filedesc));
@@ -82,7 +82,7 @@
8383 if (!file)
8484 {
8585 errno = EMFILE;
86 - return -2;
 86+ return 0;
8787 }
8888 reownalloc(file, KERNEL_OWNER(KERNEL_OWNER_FILE_HANDLE));
8989 memset(file, 0, sizeof(struct filedesc));
@@ -112,7 +112,7 @@
113113 DEBUGF("Failed opening dir");
114114 errno = EIO;
115115 free(file);
116 - return -4;
 116+ return 0;
117117 }
118118
119119 if(name[0] == 0) {
@@ -120,7 +120,7 @@
121121 errno = EINVAL;
122122 free(file);
123123 closedir(dir);
124 - return -5;
 124+ return 0;
125125 }
126126
127127 /* scan dir for name */
@@ -137,34 +137,34 @@
138138 }
139139
140140 if ( !entry ) {
141 - DEBUGF("Didn't find file %s",name);
142141 if ( file->write && (flags & O_CREAT) ) {
143142 rc = fat_create_file(name,
144143 &(file->fatfile),
145144 &(dir->fatdir));
146145 if (rc < 0) {
147 - DEBUGF("Couldn't create %s in %s",name,pathnamecopy);
 146+ DEBUGF("Couldn't create %s", pathnamecopy);
148147 errno = EIO;
149148 free(file);
150149 closedir(dir);
151 - return rc * 10 - 6;
 150+ return 0;
152151 }
 152+ DEBUGF("File %s doesn't exist, creating it", pathnamecopy);
153153 file->size = 0;
154154 file->attr = 0;
155155 }
156156 else {
157 - DEBUGF("Couldn't find %s in %s",name,pathnamecopy);
 157+ DEBUGF("Couldn't find %s", pathnamecopy);
158158 errno = ENOENT;
159159 free(file);
160160 closedir(dir);
161 - return -7;
 161+ return 0;
162162 }
163163 } else {
164 - if(file->attr & FAT_ATTR_DIRECTORY) {
 164+ if (file->attr & FAT_ATTR_DIRECTORY && !ignoredir) {
165165 errno = EISDIR;
166166 free(file);
167167 closedir(dir);
168 - return -8;
 168+ return 0;
169169 }
170170 }
171171 closedir(dir);
@@ -177,7 +177,8 @@
178178 if (rc < 0 )
179179 {
180180 free(file);
181 - return rc * 10 - 9;
 181+ errno = EIO;
 182+ return 0;
182183 }
183184 }
184185
@@ -191,8 +192,7 @@
192193
193194 int file_open(const char* pathname, int flags)
194195 {
195 - /* By default, use the dircache if available. */
196 - return open_internal(pathname, flags, true);
 196+ return open_internal(pathname, flags, false);
197197 }
198198
199199 void reown_file(int fd, struct scheduler_thread* owner)
@@ -299,10 +299,8 @@
300300 int remove(const char* name)
301301 {
302302 int rc;
303 - /* Can't use dircache now, because we need to access the fat structures. */
304303 int fd = open_internal(name, O_WRONLY, false);
305 - if ( fd < 0 )
306 - return fd * 10 - 1;
 304+ if (!fd) return - 1;
307305
308306 struct filedesc* file = (struct filedesc*)fd;
309307 rc = fat_remove(&(file->fatfile));
@@ -332,18 +330,17 @@
333331
334332 /* verify new path does not already exist */
335333 /* If it is a directory, errno == EISDIR if the name exists */
336 - fd = open(newpath, O_RDONLY);
337 - if ( fd >= 0 || errno == EISDIR) {
 334+ fd = open_internal(newpath, O_RDONLY, true);
 335+ if (fd) {
338336 close(fd);
339337 errno = EBUSY;
340338 return -1;
341339 }
342 - close(fd);
343340
344 - fd = open_internal(path, O_RDONLY, false);
345 - if ( fd < 0 ) {
 341+ fd = open_internal(path, O_RDONLY, true);
 342+ if (!fd) {
346343 errno = EIO;
347 - return fd * 10 - 2;
 344+ return -2;
348345 }
349346
350347 /* extract new file name */