From f7f51d6a5ec7bfaf8e47f2fca412ecdfb34508d9 Mon Sep 17 00:00:00 2001 From: Michael Chisholm Date: Wed, 2 Aug 2006 10:07:09 +0000 Subject: [PATCH] Fixed _FAT_seek_r --- source/fatfile.c | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/source/fatfile.c b/source/fatfile.c index ced8ea9..f1e63a1 100644 --- a/source/fatfile.c +++ b/source/fatfile.c @@ -33,6 +33,9 @@ 2006-07-17 - Chishm * Made all path inputs const char* * Added _FAT_rename_r + + 2006-08-02 - Chishm + * Fixed _FAT_seek_r */ @@ -731,40 +734,37 @@ int _FAT_seek_r (struct _reent *r, int fd, int pos, int dir) { return -1; } - if (position <= file->filesize) { + // Only change the read/write position if it is within the bounds of the current filesize + if (file->filesize > position) { + // Calculate the sector and byte of the current position, // and store them file->rwPosition.sector = (position % partition->bytesPerCluster) / BYTES_PER_READ; file->rwPosition.byte = position % BYTES_PER_READ; - + // Calculate where the correct cluster is - if (position > file->currentPosition) { - clusCount = (position - file->currentPosition - + (file->rwPosition.sector * partition->bytesPerSector) - + file->rwPosition.byte) / partition->bytesPerCluster; // Fixed thanks to AgentQ + if (position >= file->currentPosition) { + clusCount = (position / partition->bytesPerCluster) - (file->currentPosition / partition->bytesPerCluster); cluster = file->rwPosition.cluster; } else { clusCount = position / partition->bytesPerCluster; cluster = file->startCluster; } - - // Follow cluster list until desired one is found - if (clusCount > 0) { - // Only look at next cluster if need to + + nextCluster = _FAT_fat_nextCluster (partition, cluster); + while ((clusCount > 0) && (nextCluster != CLUSTER_FREE) && (nextCluster != CLUSTER_EOF)) { + clusCount--; + cluster = nextCluster; nextCluster = _FAT_fat_nextCluster (partition, cluster); - while ((clusCount--) && (nextCluster != CLUSTER_FREE) && (nextCluster != CLUSTER_EOF)) { - cluster = nextCluster; - nextCluster = _FAT_fat_nextCluster (partition, cluster); - } - } else { - nextCluster = cluster; } + // Check if ran out of clusters, and the file is being written to if ((clusCount > 0) && (file->write || file->append)) { // Set flag to allocate a new cluster file->rwPosition.sector = partition->sectorsPerCluster; file->rwPosition.byte = 0; } + file->rwPosition.cluster = cluster; }