From 11a09e632ceb5513aad54aa6b2c6031bb28b4533 Mon Sep 17 00:00:00 2001 From: Björn Stenberg Date: Mon, 11 Nov 2002 10:21:51 +0000 Subject: close() now does not truncate the file. git-svn-id: svn://svn.rockbox.org/rockbox/trunk@2821 a1c6a512-1295-4272-9138-f99709370657 --- firmware/common/file.c | 42 ++++++++++++++++++++++++++++++++++++++---- 1 file changed, 38 insertions(+), 4 deletions(-) (limited to 'firmware/common') diff --git a/firmware/common/file.c b/firmware/common/file.c index 1dd71d98ae..b2c2644725 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 ); } -- cgit v1.2.3