summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBjörn Stenberg <bjorn@haxx.se>2002-10-31 20:41:36 +0000
committerBjörn Stenberg <bjorn@haxx.se>2002-10-31 20:41:36 +0000
commit675d258575575afd2439362f44c2a73882a9890e (patch)
tree3bd29aed0a5189bc555632bfcd18b8789c6c0c63
parentf9b5fdcd4d0a8095d6bc5bb91473de56ab6381c6 (diff)
Fixed lseek().
git-svn-id: svn://svn.rockbox.org/rockbox/trunk@2796 a1c6a512-1295-4272-9138-f99709370657
-rw-r--r--firmware/common/file.c14
-rw-r--r--firmware/drivers/fat.c62
2 files changed, 31 insertions, 45 deletions
diff --git a/firmware/common/file.c b/firmware/common/file.c
index 3e730c2880..ae5560407b 100644
--- a/firmware/common/file.c
+++ b/firmware/common/file.c
@@ -363,13 +363,15 @@ int lseek(int fd, int offset, int whence)
return -4;
}
}
- rc = fat_readwrite(&(openfiles[fd].fatfile), 1,
- &(openfiles[fd].cache),false);
- if ( rc < 0 ) {
- errno = EIO;
- return -5;
+ if ( sectoroffset ) {
+ rc = fat_readwrite(&(openfiles[fd].fatfile), 1,
+ &(openfiles[fd].cache),false);
+ if ( rc < 0 ) {
+ errno = EIO;
+ return -5;
+ }
+ openfiles[fd].cacheoffset = sectoroffset;
}
- openfiles[fd].cacheoffset = sectoroffset;
}
else
if ( openfiles[fd].cacheoffset != -1 )
diff --git a/firmware/drivers/fat.c b/firmware/drivers/fat.c
index 884311c1f5..6f6873b65e 100644
--- a/firmware/drivers/fat.c
+++ b/firmware/drivers/fat.c
@@ -1102,7 +1102,7 @@ static bool transfer( int start, int count, char* buf, bool write )
{
int err;
- LDEBUGF("transfer(s=%x, c=%x)\n",start, count);
+ LDEBUGF("transfer(s=%x, c=%x, %s)\n",start, count, write?"write":"read");
if (write)
err = ata_write_sectors(start, count, buf);
else
@@ -1126,9 +1126,9 @@ int fat_readwrite( struct fat_file *file, int sectorcount,
int first=0, last=0;
int i;
- LDEBUGF( "fat_write(file:%x,count:%d,buf:%x)\n",
- cluster,sectorcount,buf);
- LDEBUGF( "fat_write: c=%x s=%x n=%d\n", cluster,sector,numsec);
+ LDEBUGF( "fat_readwrite(file:%x,count:0x%x,buf:%x,%s)\n",
+ cluster,sectorcount,buf,write?"write":"read");
+ LDEBUGF( "fat_readwrite: sec=%x numsec=%d\n", sector,numsec);
if ( sector == -1 )
return 0;
@@ -1176,15 +1176,11 @@ int fat_readwrite( struct fat_file *file, int sectorcount,
first = sector;
}
else {
- /* last sector requested */
- if (i >= sectorcount-1) {
+ if (i == sectorcount-1) { /* last sector requested */
int count = sector - first + 1;
if (!transfer( first + fat_bpb.startsector,
count, buf, write ))
return -2;
-
- ((char*)buf) += count * SECTOR_SIZE;
- first = sector;
}
}
@@ -1204,43 +1200,31 @@ int fat_readwrite( struct fat_file *file, int sectorcount,
int fat_seek(struct fat_file *file, int seeksector )
{
+ int clusternum, sectornum, sector;
int cluster = file->firstcluster;
- int sector = cluster2sec(cluster);
- int numsec = 0;
int i;
- if ( seeksector ) {
- for (i=0; i<seeksector; i++) {
- numsec++;
- if ( numsec >= fat_bpb.bpb_secperclus ) {
- cluster = get_next_cluster(cluster);
- if (!cluster)
- {
- /* end of file */
- if (i == (seeksector-1))
- {
- /* seeksector is last sector in file */
- sector = -1;
- break;
- }
- else
- /* attempting to seek beyond end of file */
- return -1;
- }
-
- sector = cluster2sec(cluster);
- if (sector<0)
- return -2;
- numsec=0;
- }
- else
- sector++;
+ seeksector--;
+ clusternum = seeksector / fat_bpb.bpb_secperclus;
+ sectornum = seeksector % fat_bpb.bpb_secperclus;
+
+ for (i=0; i<clusternum; i++) {
+ cluster = get_next_cluster(cluster);
+ if (!cluster) {
+ sector = -1;
+ break;
}
}
-
+
+ if ( sector > -1 )
+ sector = cluster2sec(cluster) + sectornum;
+
+ LDEBUGF("fat_seek(%x) == %x, %x, %x\n",
+ seeksector, cluster, sector, sectornum);
+
file->lastcluster = cluster;
file->lastsector = sector;
- file->sectornum = numsec;
+ file->sectornum = sectornum + 1;
return 0;
}