diff options
Diffstat (limited to 'firmware')
-rw-r--r-- | firmware/common/file.c | 88 |
1 files changed, 49 insertions, 39 deletions
diff --git a/firmware/common/file.c b/firmware/common/file.c index 6efcd38f16..516618b460 100644 --- a/firmware/common/file.c +++ b/firmware/common/file.c | |||
@@ -65,6 +65,7 @@ int open(const char* pathname, int flags) | |||
65 | int fd; | 65 | int fd; |
66 | char* name; | 66 | char* name; |
67 | struct filedesc* file = NULL; | 67 | struct filedesc* file = NULL; |
68 | int rc; | ||
68 | 69 | ||
69 | LDEBUGF("open(\"%s\",%d)\n",pathname,flags); | 70 | LDEBUGF("open(\"%s\",%d)\n",pathname,flags); |
70 | 71 | ||
@@ -131,14 +132,15 @@ int open(const char* pathname, int flags) | |||
131 | if ( !entry ) { | 132 | if ( !entry ) { |
132 | LDEBUGF("Didn't find file %s\n",name); | 133 | LDEBUGF("Didn't find file %s\n",name); |
133 | if ( file->write && (flags & O_CREAT) ) { | 134 | if ( file->write && (flags & O_CREAT) ) { |
134 | if (fat_create_file(name, | 135 | rc = fat_create_file(name, |
135 | &(file->fatfile), | 136 | &(file->fatfile), |
136 | &(dir->fatdir)) < 0) { | 137 | &(dir->fatdir)); |
138 | if (rc < 0) { | ||
137 | DEBUGF("Couldn't create %s in %s\n",name,pathname); | 139 | DEBUGF("Couldn't create %s in %s\n",name,pathname); |
138 | errno = EIO; | 140 | errno = EIO; |
139 | file->busy = false; | 141 | file->busy = false; |
140 | closedir(dir); | 142 | closedir(dir); |
141 | return -5; | 143 | return rc * 10 - 5; |
142 | } | 144 | } |
143 | file->size = 0; | 145 | file->size = 0; |
144 | file->attr = 0; | 146 | file->attr = 0; |
@@ -157,8 +159,9 @@ int open(const char* pathname, int flags) | |||
157 | file->fileoffset = 0; | 159 | file->fileoffset = 0; |
158 | 160 | ||
159 | if (file->write && (flags & O_APPEND)) { | 161 | if (file->write && (flags & O_APPEND)) { |
160 | if (lseek(fd,0,SEEK_END) < 0 ) | 162 | rc = lseek(fd,0,SEEK_END); |
161 | return -7; | 163 | if (rc < 0 ) |
164 | return rc * 10 - 7; | ||
162 | } | 165 | } |
163 | 166 | ||
164 | return fd; | 167 | return fd; |
@@ -169,7 +172,7 @@ int close(int fd) | |||
169 | struct filedesc* file = &openfiles[fd]; | 172 | struct filedesc* file = &openfiles[fd]; |
170 | int rc = 0; | 173 | int rc = 0; |
171 | 174 | ||
172 | LDEBUGF("close(%d)\n",fd); | 175 | LDEBUGF("close(%d)\n", fd); |
173 | 176 | ||
174 | if (fd < 0 || fd > MAX_OPEN_FILES-1) { | 177 | if (fd < 0 || fd > MAX_OPEN_FILES-1) { |
175 | errno = EINVAL; | 178 | errno = EINVAL; |
@@ -182,22 +185,25 @@ int close(int fd) | |||
182 | if (file->write) { | 185 | if (file->write) { |
183 | /* flush sector cache */ | 186 | /* flush sector cache */ |
184 | if ( file->dirty ) { | 187 | if ( file->dirty ) { |
185 | if (flush_cache(fd) < 0) | 188 | rc = flush_cache(fd); |
186 | return -2; | 189 | if (rc < 0) |
190 | return rc * 10 - 3; | ||
187 | } | 191 | } |
188 | 192 | ||
189 | /* truncate? */ | 193 | /* truncate? */ |
190 | if (file->trunc) { | 194 | if (file->trunc) { |
191 | if (ftruncate(fd, file->fileoffset) < 0) | 195 | rc = ftruncate(fd, file->fileoffset); |
192 | return -1; | 196 | if (rc < 0) |
197 | return rc * 10 - 4; | ||
193 | } | 198 | } |
194 | 199 | ||
195 | /* tie up all loose ends */ | 200 | /* tie up all loose ends */ |
196 | if (fat_closewrite(&(file->fatfile), file->size, file->attr) < 0) | 201 | rc = fat_closewrite(&(file->fatfile), file->size, file->attr); |
197 | return -3; | 202 | if (rc < 0) |
203 | return rc * 10 - 5; | ||
198 | } | 204 | } |
199 | file->busy = false; | 205 | file->busy = false; |
200 | return rc; | 206 | return 0; |
201 | } | 207 | } |
202 | 208 | ||
203 | int remove(const char* name) | 209 | int remove(const char* name) |
@@ -206,28 +212,28 @@ int remove(const char* name) | |||
206 | struct filedesc* file; | 212 | struct filedesc* file; |
207 | int fd = open(name, O_WRONLY); | 213 | int fd = open(name, O_WRONLY); |
208 | if ( fd < 0 ) | 214 | if ( fd < 0 ) |
209 | return fd; | 215 | return fd * 10 - 1; |
210 | 216 | ||
211 | file = &openfiles[fd]; | 217 | file = &openfiles[fd]; |
212 | rc = fat_truncate(&(file->fatfile)); | 218 | rc = fat_truncate(&(file->fatfile)); |
213 | if ( rc < 0 ) { | 219 | if ( rc < 0 ) { |
214 | DEBUGF("Failed truncating file: %d\n", rc); | 220 | DEBUGF("Failed truncating file: %d\n", rc); |
215 | errno = EIO; | 221 | errno = EIO; |
216 | return -1; | 222 | return rc * 10 - 2; |
217 | } | 223 | } |
218 | 224 | ||
219 | rc = fat_remove(&(file->fatfile)); | 225 | rc = fat_remove(&(file->fatfile)); |
220 | if ( rc < 0 ) { | 226 | if ( rc < 0 ) { |
221 | DEBUGF("Failed removing file: %d\n", rc); | 227 | DEBUGF("Failed removing file: %d\n", rc); |
222 | errno = EIO; | 228 | errno = EIO; |
223 | return -2; | 229 | return rc * 10 - 3; |
224 | } | 230 | } |
225 | 231 | ||
226 | file->size = 0; | 232 | file->size = 0; |
227 | 233 | ||
228 | rc = close(fd); | 234 | rc = close(fd); |
229 | if (rc<0) | 235 | if (rc<0) |
230 | return -3; | 236 | return rc * 10 - 4; |
231 | 237 | ||
232 | return 0; | 238 | return 0; |
233 | } | 239 | } |
@@ -241,15 +247,16 @@ int rename(const char* path, const char* newpath) | |||
241 | /* verify new path does not already exist */ | 247 | /* verify new path does not already exist */ |
242 | fd = open(newpath, O_RDONLY); | 248 | fd = open(newpath, O_RDONLY); |
243 | if ( fd >= 0 ) { | 249 | if ( fd >= 0 ) { |
250 | close(fd); | ||
244 | errno = EBUSY; | 251 | errno = EBUSY; |
245 | return fd; | 252 | return -1; |
246 | } | 253 | } |
247 | close(fd); | 254 | close(fd); |
248 | 255 | ||
249 | fd = open(path, O_RDONLY); | 256 | fd = open(path, O_RDONLY); |
250 | if ( fd < 0 ) { | 257 | if ( fd < 0 ) { |
251 | errno = EIO; | 258 | errno = EIO; |
252 | return fd; | 259 | return fd * 10 - 2; |
253 | } | 260 | } |
254 | 261 | ||
255 | /* strip path */ | 262 | /* strip path */ |
@@ -264,13 +271,13 @@ int rename(const char* path, const char* newpath) | |||
264 | if ( rc < 0 ) { | 271 | if ( rc < 0 ) { |
265 | DEBUGF("Failed renaming file: %d\n", rc); | 272 | DEBUGF("Failed renaming file: %d\n", rc); |
266 | errno = EIO; | 273 | errno = EIO; |
267 | return -1; | 274 | return rc * 10 - 3; |
268 | } | 275 | } |
269 | 276 | ||
270 | rc = close(fd); | 277 | rc = close(fd); |
271 | if (rc<0) { | 278 | if (rc<0) { |
272 | errno = EIO; | 279 | errno = EIO; |
273 | return -2; | 280 | return rc * 10 - 4; |
274 | } | 281 | } |
275 | 282 | ||
276 | return 0; | 283 | return 0; |
@@ -288,13 +295,13 @@ int ftruncate(int fd, unsigned int size) | |||
288 | rc = fat_seek(&(file->fatfile), sector); | 295 | rc = fat_seek(&(file->fatfile), sector); |
289 | if (rc < 0) { | 296 | if (rc < 0) { |
290 | errno = EIO; | 297 | errno = EIO; |
291 | return -1; | 298 | return rc * 10 - 1; |
292 | } | 299 | } |
293 | 300 | ||
294 | rc = fat_truncate(&(file->fatfile)); | 301 | rc = fat_truncate(&(file->fatfile)); |
295 | if (rc < 0) { | 302 | if (rc < 0) { |
296 | errno = EIO; | 303 | errno = EIO; |
297 | return -2; | 304 | return rc * 10 - 2; |
298 | } | 305 | } |
299 | 306 | ||
300 | file->size = size; | 307 | file->size = size; |
@@ -313,12 +320,12 @@ static int flush_cache(int fd) | |||
313 | /* seek back one sector to get file position right */ | 320 | /* seek back one sector to get file position right */ |
314 | rc = fat_seek(&(file->fatfile), sector); | 321 | rc = fat_seek(&(file->fatfile), sector); |
315 | if ( rc < 0 ) | 322 | if ( rc < 0 ) |
316 | return -1; | 323 | return rc * 10 - 1; |
317 | 324 | ||
318 | rc = fat_readwrite(&(file->fatfile), 1, | 325 | rc = fat_readwrite(&(file->fatfile), 1, |
319 | file->cache, true ); | 326 | file->cache, true ); |
320 | if ( rc < 0 ) | 327 | if ( rc < 0 ) |
321 | return -2; | 328 | return rc * 10 - 2; |
322 | 329 | ||
323 | file->dirty = false; | 330 | file->dirty = false; |
324 | 331 | ||
@@ -330,6 +337,7 @@ static int readwrite(int fd, void* buf, int count, bool write) | |||
330 | int sectors; | 337 | int sectors; |
331 | int nread=0; | 338 | int nread=0; |
332 | struct filedesc* file = &openfiles[fd]; | 339 | struct filedesc* file = &openfiles[fd]; |
340 | int rc; | ||
333 | 341 | ||
334 | if ( !file->busy ) { | 342 | if ( !file->busy ) { |
335 | errno = EBADF; | 343 | errno = EBADF; |
@@ -364,7 +372,7 @@ static int readwrite(int fd, void* buf, int count, bool write) | |||
364 | int rc = flush_cache(fd); | 372 | int rc = flush_cache(fd); |
365 | if ( rc < 0 ) { | 373 | if ( rc < 0 ) { |
366 | errno = EIO; | 374 | errno = EIO; |
367 | return -2; | 375 | return rc * 10 - 2; |
368 | } | 376 | } |
369 | file->cacheoffset = -1; | 377 | file->cacheoffset = -1; |
370 | } | 378 | } |
@@ -381,8 +389,9 @@ static int readwrite(int fd, void* buf, int count, bool write) | |||
381 | 389 | ||
382 | /* if buffer has been modified, write it back to disk */ | 390 | /* if buffer has been modified, write it back to disk */ |
383 | if (count && file->dirty) { | 391 | if (count && file->dirty) { |
384 | if (flush_cache(fd) < 0) | 392 | rc = flush_cache(fd); |
385 | return -3; | 393 | if (rc < 0) |
394 | return rc * 10 - 3; | ||
386 | } | 395 | } |
387 | 396 | ||
388 | /* read whole sectors right into the supplied buffer */ | 397 | /* read whole sectors right into the supplied buffer */ |
@@ -393,7 +402,7 @@ static int readwrite(int fd, void* buf, int count, bool write) | |||
393 | if ( rc < 0 ) { | 402 | if ( rc < 0 ) { |
394 | DEBUGF("Failed read/writing %d sectors\n",sectors); | 403 | DEBUGF("Failed read/writing %d sectors\n",sectors); |
395 | errno = EIO; | 404 | errno = EIO; |
396 | return -5; | 405 | return rc * 10 - 4; |
397 | } | 406 | } |
398 | else { | 407 | else { |
399 | if ( rc > 0 ) { | 408 | if ( rc > 0 ) { |
@@ -425,7 +434,7 @@ static int readwrite(int fd, void* buf, int count, bool write) | |||
425 | if ( rc < 0 ) { | 434 | if ( rc < 0 ) { |
426 | DEBUGF("Failed writing\n"); | 435 | DEBUGF("Failed writing\n"); |
427 | errno = EIO; | 436 | errno = EIO; |
428 | return -6; | 437 | return rc * 10 - 5; |
429 | } | 438 | } |
430 | /* seek back one sector to put file position right */ | 439 | /* seek back one sector to put file position right */ |
431 | rc = fat_seek(&(file->fatfile), | 440 | rc = fat_seek(&(file->fatfile), |
@@ -434,18 +443,18 @@ static int readwrite(int fd, void* buf, int count, bool write) | |||
434 | if ( rc < 0 ) { | 443 | if ( rc < 0 ) { |
435 | DEBUGF("fat_seek() failed\n"); | 444 | DEBUGF("fat_seek() failed\n"); |
436 | errno = EIO; | 445 | errno = EIO; |
437 | return -7; | 446 | return rc * 10 - 6; |
438 | } | 447 | } |
439 | } | 448 | } |
440 | memcpy( file->cache, buf + nread, count ); | 449 | memcpy( file->cache, buf + nread, count ); |
441 | file->dirty = true; | 450 | file->dirty = true; |
442 | } | 451 | } |
443 | else { | 452 | else { |
444 | if ( fat_readwrite(&(file->fatfile), 1, | 453 | rc = fat_readwrite(&(file->fatfile), 1, &(file->cache),false); |
445 | &(file->cache),false) < 1 ) { | 454 | if (rc < 1 ) { |
446 | DEBUGF("Failed caching sector\n"); | 455 | DEBUGF("Failed caching sector\n"); |
447 | errno = EIO; | 456 | errno = EIO; |
448 | return -8; | 457 | return rc * 10 - 7; |
449 | } | 458 | } |
450 | memcpy( buf + nread, file->cache, count ); | 459 | memcpy( buf + nread, file->cache, count ); |
451 | } | 460 | } |
@@ -527,14 +536,15 @@ int lseek(int fd, int offset, int whence) | |||
527 | 536 | ||
528 | if ( newsector != oldsector ) { | 537 | if ( newsector != oldsector ) { |
529 | if (file->dirty) { | 538 | if (file->dirty) { |
530 | if (flush_cache(fd) < 0) | 539 | rc = flush_cache(fd); |
531 | return -5; | 540 | if (rc < 0) |
541 | return rc * 10 - 5; | ||
532 | } | 542 | } |
533 | 543 | ||
534 | rc = fat_seek(&(file->fatfile), newsector); | 544 | rc = fat_seek(&(file->fatfile), newsector); |
535 | if ( rc < 0 ) { | 545 | if ( rc < 0 ) { |
536 | errno = EIO; | 546 | errno = EIO; |
537 | return -4; | 547 | return rc * 10 - 4; |
538 | } | 548 | } |
539 | } | 549 | } |
540 | if ( sectoroffset ) { | 550 | if ( sectoroffset ) { |
@@ -542,7 +552,7 @@ int lseek(int fd, int offset, int whence) | |||
542 | &(file->cache),false); | 552 | &(file->cache),false); |
543 | if ( rc < 0 ) { | 553 | if ( rc < 0 ) { |
544 | errno = EIO; | 554 | errno = EIO; |
545 | return -6; | 555 | return rc * 10 - 6; |
546 | } | 556 | } |
547 | file->cacheoffset = sectoroffset; | 557 | file->cacheoffset = sectoroffset; |
548 | } | 558 | } |