From a601fb8d1922ddd8e7fbb39f8ae2c6137b3a12a5 Mon Sep 17 00:00:00 2001 From: Jens Arnold Date: Sat, 17 Dec 2005 12:17:11 +0000 Subject: More compact & straight-forward headbytes handling. git-svn-id: svn://svn.rockbox.org/rockbox/trunk@8255 a1c6a512-1295-4272-9138-f99709370657 --- firmware/common/file.c | 45 +++++++++++---------------------------------- 1 file changed, 11 insertions(+), 34 deletions(-) diff --git a/firmware/common/file.c b/firmware/common/file.c index 7f36ecf..90fab6d 100644 --- a/firmware/common/file.c +++ b/firmware/common/file.c @@ -24,6 +24,7 @@ #include "dir.h" #include "debug.h" #include "dircache.h" +#include "system.h" /* These functions provide a roughly POSIX-compatible file IO API. @@ -439,53 +440,29 @@ static int readwrite(int fd, void* buf, long count, bool write) /* any head bytes? */ if ( file->cacheoffset != -1 ) { - int headbytes; int offs = file->cacheoffset; - if ( count <= SECTOR_SIZE - file->cacheoffset ) { - headbytes = count; - file->cacheoffset += count; - if ( file->cacheoffset >= SECTOR_SIZE ) - { - /* Flush the cache first if it's dirty. */ - if (file->dirty) - { - rc = flush_cache(fd); - if ( rc < 0 ) { - errno = EIO; - return rc * 10 - 9; - } - } - file->cacheoffset = -1; - } + int headbytes = MIN(count, SECTOR_SIZE - offs); + + if (write) { + memcpy( file->cache + offs, buf, headbytes ); + file->dirty = true; } else { - headbytes = SECTOR_SIZE - file->cacheoffset; - if (file->dirty) - { - rc = flush_cache(fd); - if ( rc < 0 ) { - errno = EIO; - return rc * 10 - 9; - } - } - file->cacheoffset = -1; + memcpy( buf, file->cache + offs, headbytes ); } - if (write) { - memcpy( file->cache + offs, buf, headbytes ); - if (offs+headbytes == SECTOR_SIZE) { + if (offs + headbytes == SECTOR_SIZE) { + if (file->dirty) { int rc = flush_cache(fd); if ( rc < 0 ) { errno = EIO; return rc * 10 - 2; } - file->cacheoffset = -1; } - else - file->dirty = true; + file->cacheoffset = -1; } else { - memcpy( buf, file->cache + offs, headbytes ); + file->cacheoffset += headbytes; } nread = headbytes; -- cgit v1.1