Fixed _FAT_seek_r

This commit is contained in:
Michael Chisholm 2006-08-02 10:07:09 +00:00
parent 05645cd8c2
commit f7f51d6a5e

View File

@ -33,6 +33,9 @@
2006-07-17 - Chishm 2006-07-17 - Chishm
* Made all path inputs const char* * Made all path inputs const char*
* Added _FAT_rename_r * 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; 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, // Calculate the sector and byte of the current position,
// and store them // and store them
file->rwPosition.sector = (position % partition->bytesPerCluster) / BYTES_PER_READ; file->rwPosition.sector = (position % partition->bytesPerCluster) / BYTES_PER_READ;
file->rwPosition.byte = position % BYTES_PER_READ; file->rwPosition.byte = position % BYTES_PER_READ;
// Calculate where the correct cluster is // Calculate where the correct cluster is
if (position > file->currentPosition) { if (position >= file->currentPosition) {
clusCount = (position - file->currentPosition clusCount = (position / partition->bytesPerCluster) - (file->currentPosition / partition->bytesPerCluster);
+ (file->rwPosition.sector * partition->bytesPerSector)
+ file->rwPosition.byte) / partition->bytesPerCluster; // Fixed thanks to AgentQ
cluster = file->rwPosition.cluster; cluster = file->rwPosition.cluster;
} else { } else {
clusCount = position / partition->bytesPerCluster; clusCount = position / partition->bytesPerCluster;
cluster = file->startCluster; 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); nextCluster = _FAT_fat_nextCluster (partition, cluster);
while ((clusCount--) && (nextCluster != CLUSTER_FREE) && (nextCluster != CLUSTER_EOF)) { while ((clusCount > 0) && (nextCluster != CLUSTER_FREE) && (nextCluster != CLUSTER_EOF)) {
clusCount--;
cluster = nextCluster; cluster = nextCluster;
nextCluster = _FAT_fat_nextCluster (partition, cluster); nextCluster = _FAT_fat_nextCluster (partition, cluster);
} }
} else {
nextCluster = cluster;
}
// Check if ran out of clusters, and the file is being written to // Check if ran out of clusters, and the file is being written to
if ((clusCount > 0) && (file->write || file->append)) { if ((clusCount > 0) && (file->write || file->append)) {
// Set flag to allocate a new cluster // Set flag to allocate a new cluster
file->rwPosition.sector = partition->sectorsPerCluster; file->rwPosition.sector = partition->sectorsPerCluster;
file->rwPosition.byte = 0; file->rwPosition.byte = 0;
} }
file->rwPosition.cluster = cluster; file->rwPosition.cluster = cluster;
} }