summaryrefslogtreecommitdiff
path: root/firmware/common
diff options
context:
space:
mode:
authorBjörn Stenberg <bjorn@haxx.se>2002-11-11 10:21:51 +0000
committerBjörn Stenberg <bjorn@haxx.se>2002-11-11 10:21:51 +0000
commit11a09e632ceb5513aad54aa6b2c6031bb28b4533 (patch)
treee0f1a0d93d40dbeab2c0e20fa240880e5ad67d0c /firmware/common
parent7912a6d39d73a73022feee41ba5fce8ee3ae6019 (diff)
downloadrockbox-11a09e632ceb5513aad54aa6b2c6031bb28b4533.zip
rockbox-11a09e632ceb5513aad54aa6b2c6031bb28b4533.tar.gz
rockbox-11a09e632ceb5513aad54aa6b2c6031bb28b4533.tar.bz2
rockbox-11a09e632ceb5513aad54aa6b2c6031bb28b4533.tar.xz
close() now does not truncate the file.
git-svn-id: svn://svn.rockbox.org/rockbox/trunk@2821 a1c6a512-1295-4272-9138-f99709370657
Diffstat (limited to 'firmware/common')
-rw-r--r--firmware/common/file.c42
1 files changed, 38 insertions, 4 deletions
diff --git a/firmware/common/file.c b/firmware/common/file.c
index 1dd71d9..b2c2644 100644
--- a/firmware/common/file.c
+++ b/firmware/common/file.c
@@ -38,7 +38,7 @@
struct filedesc {
unsigned char cache[SECTOR_SIZE];
int cacheoffset;
- int fileoffset;
+ unsigned int fileoffset;
int size;
struct fat_file fatfile;
bool busy;
@@ -178,7 +178,7 @@ int close(int fd)
}
/* tie up all loose ends */
- fat_closewrite(&(openfiles[fd].fatfile), openfiles[fd].fileoffset);
+ fat_closewrite(&(openfiles[fd].fatfile), openfiles[fd].size);
}
openfiles[fd].busy = false;
return rc;
@@ -191,7 +191,21 @@ int remove(const char* name)
if ( fd < 0 )
return fd;
+ rc = fat_truncate(&(openfiles[fd].fatfile));
+ if ( rc < 0 ) {
+ DEBUGF("Failed truncating file\n");
+ errno = EIO;
+ return -1;
+ }
+
rc = fat_remove(&(openfiles[fd].fatfile));
+ if ( rc < 0 ) {
+ DEBUGF("Failed removing file\n");
+ errno = EIO;
+ return -2;
+ }
+
+ openfiles[fd].size = 0;
close(fd);
@@ -259,7 +273,7 @@ static int readwrite(int fd, void* buf, int count, bool write)
if ( rc < 0 ) {
DEBUGF("Failed read/writing %d sectors\n",sectors);
errno = EIO;
- return -2;
+ return -3;
}
else {
if ( rc > 0 ) {
@@ -278,10 +292,30 @@ static int readwrite(int fd, void* buf, int count, bool write)
openfiles[fd].cacheoffset = -1;
}
}
+ openfiles[fd].fileoffset += nread;
+ nread = 0;
/* any tail bytes? */
if ( count ) {
if (write) {
+ /* sector is only partially filled. copy-back from disk */
+ int rc;
+ LDEBUGF("Copy-back tail cache\n");
+ rc = fat_readwrite(&(openfiles[fd].fatfile), 1,
+ openfiles[fd].cache, false );
+ if ( rc < 0 ) {
+ DEBUGF("Failed reading\n");
+ errno = EIO;
+ return -4;
+ }
+ /* seek back one sector to put file position right */
+ rc = fat_seek(&(openfiles[fd].fatfile),
+ openfiles[fd].fileoffset / SECTOR_SIZE);
+ if ( rc < 0 ) {
+ DEBUGF("fat_seek() failed\n");
+ errno = EIO;
+ return -5;
+ }
memcpy( openfiles[fd].cache, buf + nread, count );
}
else {
@@ -289,7 +323,7 @@ static int readwrite(int fd, void* buf, int count, bool write)
&(openfiles[fd].cache),false) < 1 ) {
DEBUGF("Failed caching sector\n");
errno = EIO;
- return -1;
+ return -6;
}
memcpy( buf + nread, openfiles[fd].cache, count );
}