From 68640edf90c84c3d4494f0852f6236e1929a4603 Mon Sep 17 00:00:00 2001 From: Björn Stenberg Date: Mon, 11 Nov 2002 14:40:18 +0000 Subject: Added ftruncate(). git-svn-id: svn://svn.rockbox.org/rockbox/trunk@2827 a1c6a512-1295-4272-9138-f99709370657 --- firmware/common/file.c | 27 ++++++++++++++++++++++++++- firmware/common/file.h | 1 + firmware/drivers/fat.c | 9 ++++++--- 3 files changed, 33 insertions(+), 4 deletions(-) (limited to 'firmware') diff --git a/firmware/common/file.c b/firmware/common/file.c index b86938007b..cf8c46c5ab 100644 --- a/firmware/common/file.c +++ b/firmware/common/file.c @@ -90,7 +90,7 @@ int open(const char* pathname, int flags) case O_WRONLY: openfiles[fd].write = true; break; - + default: DEBUGF("Only O_RDONLY and O_WRONLY is supported\n"); errno = EROFS; @@ -214,6 +214,31 @@ int remove(const char* name) return rc; } +int ftruncate(int fd, unsigned int size) +{ + int rc, sector; + + sector = size / SECTOR_SIZE; + if (size % SECTOR_SIZE) + sector++; + + rc = fat_seek(&(openfiles[fd].fatfile), sector); + if (rc < 0) { + errno = EIO; + return -1; + } + + rc = fat_truncate(&(openfiles[fd].fatfile)); + if (rc < 0) { + errno = EIO; + return -2; + } + + openfiles[fd].size = size; + + return 0; +} + static int readwrite(int fd, void* buf, int count, bool write) { int sectors; diff --git a/firmware/common/file.h b/firmware/common/file.h index 7a7c63d665..59ff717d1d 100644 --- a/firmware/common/file.h +++ b/firmware/common/file.h @@ -56,6 +56,7 @@ extern int creat(const char *pathname, int mode); extern int write(int fd, void* buf, int count); extern int remove(const char* pathname); extern int rename(const char* oldname, const char* newname); +extern int ftruncate(int fd, unsigned int size); #else #ifdef WIN32 diff --git a/firmware/drivers/fat.c b/firmware/drivers/fat.c index 74bdbd3491..ad1a7a0c82 100644 --- a/firmware/drivers/fat.c +++ b/firmware/drivers/fat.c @@ -1002,12 +1002,15 @@ int fat_truncate(struct fat_file *file) { /* truncate trailing clusters */ int next; - int last = get_next_cluster(file->lastcluster); - while ( last && last != FAT_EOF_MARK ) { + int last = file->lastcluster; + + LDEBUGF("fat_truncate(%x, %x)\n", file->firstcluster, last); + + for ( last = get_next_cluster(last); last; last = next ) { next = get_next_cluster(last); update_fat_entry(last,0); - last = next; } + update_fat_entry(file->lastcluster,FAT_EOF_MARK); return 0; } -- cgit v1.2.3