summaryrefslogtreecommitdiff
path: root/firmware/common/file.c
diff options
context:
space:
mode:
Diffstat (limited to 'firmware/common/file.c')
-rw-r--r--firmware/common/file.c21
1 files changed, 21 insertions, 0 deletions
diff --git a/firmware/common/file.c b/firmware/common/file.c
index ea2471ae92..9cab001e8d 100644
--- a/firmware/common/file.c
+++ b/firmware/common/file.c
@@ -278,14 +278,22 @@ int fsync(int fd)
278 if ( file->dirty ) { 278 if ( file->dirty ) {
279 rc = flush_cache(fd); 279 rc = flush_cache(fd);
280 if (rc < 0) 280 if (rc < 0)
281 {
282 /* when failing, try to close the file anyway */
283 fat_closewrite(&(file->fatfile), file->size, file->attr);
281 return rc * 10 - 3; 284 return rc * 10 - 3;
285 }
282 } 286 }
283 287
284 /* truncate? */ 288 /* truncate? */
285 if (file->trunc) { 289 if (file->trunc) {
286 rc = ftruncate(fd, file->size); 290 rc = ftruncate(fd, file->size);
287 if (rc < 0) 291 if (rc < 0)
292 {
293 /* when failing, try to close the file anyway */
294 fat_closewrite(&(file->fatfile), file->size, file->attr);
288 return rc * 10 - 4; 295 return rc * 10 - 4;
296 }
289 } 297 }
290 298
291 /* tie up all loose ends */ 299 /* tie up all loose ends */
@@ -475,6 +483,10 @@ static int readwrite(int fd, void* buf, long count, bool write)
475 struct filedesc* file = &openfiles[fd]; 483 struct filedesc* file = &openfiles[fd];
476 int rc; 484 int rc;
477 485
486 if (fd < 0 || fd > MAX_OPEN_FILES-1) {
487 errno = EINVAL;
488 return -1;
489 }
478 if ( !file->busy ) { 490 if ( !file->busy ) {
479 errno = EBADF; 491 errno = EBADF;
480 return -1; 492 return -1;
@@ -643,6 +655,10 @@ off_t lseek(int fd, off_t offset, int whence)
643 655
644 LDEBUGF("lseek(%d,%ld,%d)\n",fd,offset,whence); 656 LDEBUGF("lseek(%d,%ld,%d)\n",fd,offset,whence);
645 657
658 if (fd < 0 || fd > MAX_OPEN_FILES-1) {
659 errno = EINVAL;
660 return -1;
661 }
646 if ( !file->busy ) { 662 if ( !file->busy ) {
647 errno = EBADF; 663 errno = EBADF;
648 return -1; 664 return -1;
@@ -716,6 +732,10 @@ off_t filesize(int fd)
716{ 732{
717 struct filedesc* file = &openfiles[fd]; 733 struct filedesc* file = &openfiles[fd];
718 734
735 if (fd < 0 || fd > MAX_OPEN_FILES-1) {
736 errno = EINVAL;
737 return -1;
738 }
719 if ( !file->busy ) { 739 if ( !file->busy ) {
720 errno = EBADF; 740 errno = EBADF;
721 return -1; 741 return -1;
@@ -743,3 +763,4 @@ int release_files(int volume)
743 return closed; /* return how many we did */ 763 return closed; /* return how many we did */
744} 764}
745#endif /* #ifdef HAVE_HOTSWAP */ 765#endif /* #ifdef HAVE_HOTSWAP */
766