Avoid mixing cluster numbers and sector numbers. Makes code more readable.

Obtained from:	NetBSD
MFC after:	2 weeks
This commit is contained in:
Xin LI 2019-09-15 19:41:54 +00:00
parent 7887a5a140
commit eb1c42c1f0
3 changed files with 19 additions and 11 deletions

View File

@ -259,12 +259,18 @@ readboot(int dosfs, struct bootblock *boot)
return FSFATAL; return FSFATAL;
} }
boot->ClusterOffset = (boot->bpbRootDirEnts * 32 + boot->FirstCluster = (boot->bpbRootDirEnts * 32 +
boot->bpbBytesPerSec - 1) / boot->bpbBytesPerSec + boot->bpbBytesPerSec - 1) / boot->bpbBytesPerSec +
boot->bpbResSectors + boot->bpbFATs * boot->FATsecs - boot->bpbResSectors + boot->bpbFATs * boot->FATsecs;
CLUST_FIRST * boot->bpbSecPerClust;
boot->NumClusters = (boot->NumSectors - boot->ClusterOffset) / if (boot->FirstCluster + boot->bpbSecPerClust > boot->NumSectors) {
boot->bpbSecPerClust; pfatal("Cluster offset too large (%u clusters)\n",
boot->FirstCluster);
return FSFATAL;
}
boot->NumClusters = (boot->NumSectors - boot->FirstCluster) / boot->bpbSecPerClust +
CLUST_FIRST;
if (boot->flags & FAT32) if (boot->flags & FAT32)
boot->ClustMask = CLUST32_MASK; boot->ClustMask = CLUST32_MASK;

View File

@ -317,7 +317,8 @@ delete(int f, struct bootblock *boot, struct fatEntry *fat, cl_t startcl,
break; break;
e = delbuf + endoff; e = delbuf + endoff;
} }
off = startcl * boot->bpbSecPerClust + boot->ClusterOffset; off = (startcl - CLUST_FIRST) * boot->bpbSecPerClust + boot->FirstCluster;
off *= boot->bpbBytesPerSec; off *= boot->bpbBytesPerSec;
if (lseek(f, off, SEEK_SET) != off) { if (lseek(f, off, SEEK_SET) != off) {
perr("Unable to lseek to %" PRId64, off); perr("Unable to lseek to %" PRId64, off);
@ -457,7 +458,7 @@ check_subdirectory(int f, struct bootblock *boot, struct dosDirEntry *dir)
off = boot->bpbResSectors + boot->bpbFATs * off = boot->bpbResSectors + boot->bpbFATs *
boot->FATsecs; boot->FATsecs;
} else { } else {
off = cl * boot->bpbSecPerClust + boot->ClusterOffset; off = (cl - CLUST_FIRST) * boot->bpbSecPerClust + boot->FirstCluster;
} }
/* /*
@ -538,7 +539,7 @@ readDosDirSection(int f, struct bootblock *boot, struct fatEntry *fat,
boot->FATsecs; boot->FATsecs;
} else { } else {
last = boot->bpbSecPerClust * boot->bpbBytesPerSec; last = boot->bpbSecPerClust * boot->bpbBytesPerSec;
off = cl * boot->bpbSecPerClust + boot->ClusterOffset; off = (cl - CLUST_FIRST) * boot->bpbSecPerClust + boot->FirstCluster;
} }
off *= boot->bpbBytesPerSec; off *= boot->bpbBytesPerSec;
@ -1069,8 +1070,9 @@ reconnect(int dosfs, struct bootblock *boot, struct fatEntry *fat, cl_t head)
lfcl = (lostDir->head < boot->NumClusters) ? lostDir->head : 0; lfcl = (lostDir->head < boot->NumClusters) ? lostDir->head : 0;
return FSERROR; return FSERROR;
} }
lfoff = lfcl * boot->ClusterSize lfoff = (lfcl - CLUST_FIRST) * boot->ClusterSize
+ boot->ClusterOffset * boot->bpbBytesPerSec; + boot->FirstCluster * boot->bpbBytesPerSec;
if (lseek(dosfs, lfoff, SEEK_SET) != lfoff if (lseek(dosfs, lfoff, SEEK_SET) != lfoff
|| (size_t)read(dosfs, lfbuf, boot->ClusterSize) != boot->ClusterSize) { || (size_t)read(dosfs, lfbuf, boot->ClusterSize) != boot->ClusterSize) {
perr("could not read LOST.DIR"); perr("could not read LOST.DIR");

View File

@ -74,7 +74,7 @@ struct bootblock {
u_int32_t NumSectors; /* how many sectors are there */ u_int32_t NumSectors; /* how many sectors are there */
u_int32_t FATsecs; /* how many sectors are in FAT */ u_int32_t FATsecs; /* how many sectors are in FAT */
u_int32_t NumFatEntries; /* how many entries really are there */ u_int32_t NumFatEntries; /* how many entries really are there */
u_int ClusterOffset; /* at what sector would sector 0 start */ u_int FirstCluster; /* at what sector is Cluster CLUST_FIRST */
u_int ClusterSize; /* Cluster size in bytes */ u_int ClusterSize; /* Cluster size in bytes */
/* Now some statistics: */ /* Now some statistics: */