forked from len0rd/rockbox
More compact & straight-forward headbytes handling.
git-svn-id: svn://svn.rockbox.org/rockbox/trunk@8255 a1c6a512-1295-4272-9138-f99709370657
This commit is contained in:
parent
69f1870324
commit
a601fb8d19
1 changed files with 12 additions and 35 deletions
|
|
@ -24,6 +24,7 @@
|
||||||
#include "dir.h"
|
#include "dir.h"
|
||||||
#include "debug.h"
|
#include "debug.h"
|
||||||
#include "dircache.h"
|
#include "dircache.h"
|
||||||
|
#include "system.h"
|
||||||
|
|
||||||
/*
|
/*
|
||||||
These functions provide a roughly POSIX-compatible file IO API.
|
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? */
|
/* any head bytes? */
|
||||||
if ( file->cacheoffset != -1 ) {
|
if ( file->cacheoffset != -1 ) {
|
||||||
int headbytes;
|
|
||||||
int offs = file->cacheoffset;
|
int offs = file->cacheoffset;
|
||||||
if ( count <= SECTOR_SIZE - file->cacheoffset ) {
|
int headbytes = MIN(count, SECTOR_SIZE - offs);
|
||||||
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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (write) {
|
if (write) {
|
||||||
memcpy( file->cache + offs, buf, headbytes );
|
memcpy( file->cache + offs, buf, headbytes );
|
||||||
if (offs+headbytes == SECTOR_SIZE) {
|
file->dirty = true;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
memcpy( buf, file->cache + offs, headbytes );
|
||||||
|
}
|
||||||
|
|
||||||
|
if (offs + headbytes == SECTOR_SIZE) {
|
||||||
|
if (file->dirty) {
|
||||||
int rc = flush_cache(fd);
|
int rc = flush_cache(fd);
|
||||||
if ( rc < 0 ) {
|
if ( rc < 0 ) {
|
||||||
errno = EIO;
|
errno = EIO;
|
||||||
return rc * 10 - 2;
|
return rc * 10 - 2;
|
||||||
}
|
}
|
||||||
file->cacheoffset = -1;
|
|
||||||
}
|
}
|
||||||
else
|
file->cacheoffset = -1;
|
||||||
file->dirty = true;
|
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
memcpy( buf, file->cache + offs, headbytes );
|
file->cacheoffset += headbytes;
|
||||||
}
|
}
|
||||||
|
|
||||||
nread = headbytes;
|
nread = headbytes;
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue