diff options
Diffstat (limited to 'firmware/common/file.c')
-rw-r--r-- | firmware/common/file.c | 57 |
1 files changed, 53 insertions, 4 deletions
diff --git a/firmware/common/file.c b/firmware/common/file.c index 01a0ebf890..1d78dcb456 100644 --- a/firmware/common/file.c +++ b/firmware/common/file.c | |||
@@ -125,7 +125,7 @@ int open(const char* pathname, int flags) | |||
125 | break; | 125 | break; |
126 | } | 126 | } |
127 | } | 127 | } |
128 | closedir(dir); | 128 | |
129 | if ( !entry ) { | 129 | if ( !entry ) { |
130 | LDEBUGF("Didn't find file %s\n",name); | 130 | LDEBUGF("Didn't find file %s\n",name); |
131 | if ( file->write && (flags & O_CREAT) ) { | 131 | if ( file->write && (flags & O_CREAT) ) { |
@@ -135,6 +135,7 @@ int open(const char* pathname, int flags) | |||
135 | DEBUGF("Couldn't create %s in %s\n",name,pathname); | 135 | DEBUGF("Couldn't create %s in %s\n",name,pathname); |
136 | errno = EIO; | 136 | errno = EIO; |
137 | file->busy = false; | 137 | file->busy = false; |
138 | closedir(dir); | ||
138 | return -5; | 139 | return -5; |
139 | } | 140 | } |
140 | file->size = 0; | 141 | file->size = 0; |
@@ -143,9 +144,11 @@ int open(const char* pathname, int flags) | |||
143 | DEBUGF("Couldn't find %s in %s\n",name,pathname); | 144 | DEBUGF("Couldn't find %s in %s\n",name,pathname); |
144 | errno = ENOENT; | 145 | errno = ENOENT; |
145 | file->busy = false; | 146 | file->busy = false; |
147 | closedir(dir); | ||
146 | return -6; | 148 | return -6; |
147 | } | 149 | } |
148 | } | 150 | } |
151 | closedir(dir); | ||
149 | 152 | ||
150 | file->cacheoffset = -1; | 153 | file->cacheoffset = -1; |
151 | file->fileoffset = 0; | 154 | file->fileoffset = 0; |
@@ -205,23 +208,69 @@ int remove(const char* name) | |||
205 | file = &openfiles[fd]; | 208 | file = &openfiles[fd]; |
206 | rc = fat_truncate(&(file->fatfile)); | 209 | rc = fat_truncate(&(file->fatfile)); |
207 | if ( rc < 0 ) { | 210 | if ( rc < 0 ) { |
208 | DEBUGF("Failed truncating file\n"); | 211 | DEBUGF("Failed truncating file: %d\n", rc); |
209 | errno = EIO; | 212 | errno = EIO; |
210 | return -1; | 213 | return -1; |
211 | } | 214 | } |
212 | 215 | ||
213 | rc = fat_remove(&(file->fatfile)); | 216 | rc = fat_remove(&(file->fatfile)); |
214 | if ( rc < 0 ) { | 217 | if ( rc < 0 ) { |
215 | DEBUGF("Failed removing file\n"); | 218 | DEBUGF("Failed removing file: %d\n", rc); |
216 | errno = EIO; | 219 | errno = EIO; |
217 | return -2; | 220 | return -2; |
218 | } | 221 | } |
219 | 222 | ||
220 | file->size = 0; | 223 | file->size = 0; |
221 | 224 | ||
225 | rc = close(fd); | ||
226 | if (rc<0) | ||
227 | return -3; | ||
228 | |||
229 | return 0; | ||
230 | } | ||
231 | |||
232 | int rename(const char* path, const char* newpath) | ||
233 | { | ||
234 | int rc, fd; | ||
235 | char* nameptr; | ||
236 | struct filedesc* file; | ||
237 | |||
238 | /* verify new path does not already exist */ | ||
239 | fd = open(newpath, O_RDONLY); | ||
240 | if ( fd >= 0 ) { | ||
241 | errno = EBUSY; | ||
242 | return fd; | ||
243 | } | ||
222 | close(fd); | 244 | close(fd); |
223 | 245 | ||
224 | return rc; | 246 | fd = open(path, O_RDONLY); |
247 | if ( fd < 0 ) { | ||
248 | errno = EIO; | ||
249 | return fd; | ||
250 | } | ||
251 | |||
252 | /* strip path */ | ||
253 | nameptr = strrchr(newpath,'/'); | ||
254 | if (nameptr) | ||
255 | nameptr++; | ||
256 | else | ||
257 | nameptr = (char*)newpath; | ||
258 | |||
259 | file = &openfiles[fd]; | ||
260 | rc = fat_rename(&file->fatfile, nameptr, file->size); | ||
261 | if ( rc < 0 ) { | ||
262 | DEBUGF("Failed renaming file: %d\n", rc); | ||
263 | errno = EIO; | ||
264 | return -1; | ||
265 | } | ||
266 | |||
267 | rc = close(fd); | ||
268 | if (rc<0) { | ||
269 | errno = EIO; | ||
270 | return -2; | ||
271 | } | ||
272 | |||
273 | return 0; | ||
225 | } | 274 | } |
226 | 275 | ||
227 | int ftruncate(int fd, unsigned int size) | 276 | int ftruncate(int fd, unsigned int size) |