From 08356fb50a70bc44e598ff49ab61bd149060a668 Mon Sep 17 00:00:00 2001 From: Björn Stenberg Date: Thu, 31 Oct 2002 19:05:25 +0000 Subject: More graceful handling when running out of space. git-svn-id: svn://svn.rockbox.org/rockbox/trunk@2793 a1c6a512-1295-4272-9138-f99709370657 --- firmware/common/file.c | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) (limited to 'firmware/common/file.c') diff --git a/firmware/common/file.c b/firmware/common/file.c index 0cde938a70..3e730c2880 100644 --- a/firmware/common/file.c +++ b/firmware/common/file.c @@ -197,13 +197,9 @@ static int readwrite(int fd, void* buf, int count, bool write) LDEBUGF( "readwrite(%d,%x,%d,%s)\n", fd,buf,count,write?"write":"read"); - /* attempt to access past EOF? */ - if (count > openfiles[fd].size - openfiles[fd].fileoffset) { - if ( write ) - openfiles[fd].size = openfiles[fd].fileoffset + count; - else - count = openfiles[fd].size - openfiles[fd].fileoffset; - } + /* attempt to read past EOF? */ + if (!write && count > openfiles[fd].size - openfiles[fd].fileoffset) + count = openfiles[fd].size - openfiles[fd].fileoffset; /* any head bytes? */ if ( openfiles[fd].cacheoffset != -1 ) { @@ -253,8 +249,12 @@ static int readwrite(int fd, void* buf, int count, bool write) } else { if ( rc > 0 ) { - nread += sectors * SECTOR_SIZE; + nread += rc * SECTOR_SIZE; count -= sectors * SECTOR_SIZE; + + /* if eof, skip tail bytes */ + if ( rc < sectors ) + count = 0; } else { /* eof */ @@ -285,6 +285,12 @@ static int readwrite(int fd, void* buf, int count, bool write) } openfiles[fd].fileoffset += nread; + LDEBUGF("fileoffset: %d\n", openfiles[fd].fileoffset); + + /* adjust file size to length written */ + if ( write && openfiles[fd].fileoffset > openfiles[fd].size ) + openfiles[fd].size = openfiles[fd].fileoffset; + return nread; } -- cgit v1.2.3