1
0
Fork 0
forked from len0rd/rockbox

close() now does not truncate the file.

git-svn-id: svn://svn.rockbox.org/rockbox/trunk@2821 a1c6a512-1295-4272-9138-f99709370657
This commit is contained in:
Björn Stenberg 2002-11-11 10:21:51 +00:00
parent 7912a6d39d
commit 11a09e632c
3 changed files with 57 additions and 24 deletions

View file

@ -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 );
}