summaryrefslogtreecommitdiff
path: root/firmware/common/file.c
diff options
context:
space:
mode:
authorBjörn Stenberg <bjorn@haxx.se>2002-10-31 19:05:25 +0000
committerBjörn Stenberg <bjorn@haxx.se>2002-10-31 19:05:25 +0000
commit08356fb50a70bc44e598ff49ab61bd149060a668 (patch)
tree00a9d8d7f42a5102643b228f58ff2c85932478fb /firmware/common/file.c
parent0d79fa127dfc1f5650d3701a174983de8f9e5e4d (diff)
More graceful handling when running out of space.
git-svn-id: svn://svn.rockbox.org/rockbox/trunk@2793 a1c6a512-1295-4272-9138-f99709370657
Diffstat (limited to 'firmware/common/file.c')
-rw-r--r--firmware/common/file.c22
1 files changed, 14 insertions, 8 deletions
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;
}