Module Name:    src
Committed By:   mlelstv
Date:           Fri Jan  2 06:21:28 UTC 2015

Modified Files:
        src/sbin/fsck_msdos: boot.c dir.c dosfs.h

Log Message:
Avoid mixing cluster numbers and sector numbers. Makes code more readable.


To generate a diff of this commit:
cvs rdiff -u -r1.18 -r1.19 src/sbin/fsck_msdos/boot.c
cvs rdiff -u -r1.26 -r1.27 src/sbin/fsck_msdos/dir.c
cvs rdiff -u -r1.7 -r1.8 src/sbin/fsck_msdos/dosfs.h

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sbin/fsck_msdos/boot.c
diff -u src/sbin/fsck_msdos/boot.c:1.18 src/sbin/fsck_msdos/boot.c:1.19
--- src/sbin/fsck_msdos/boot.c:1.18	Tue Nov  4 03:05:43 2014
+++ src/sbin/fsck_msdos/boot.c	Fri Jan  2 06:21:28 2015
@@ -27,7 +27,7 @@
 
 #include <sys/cdefs.h>
 #ifndef lint
-__RCSID("$NetBSD: boot.c,v 1.18 2014/11/04 03:05:43 msaitoh Exp $");
+__RCSID("$NetBSD: boot.c,v 1.19 2015/01/02 06:21:28 mlelstv Exp $");
 #endif /* not lint */
 
 #include <stdlib.h>
@@ -185,11 +185,10 @@ readboot(int dosfs, struct bootblock *bo
 		return FSFATAL;
 	}
 
-	boot->ClusterOffset = (int)(boot->RootDirEnts * 32 + boot->BytesPerSec - 1)
+	boot->FirstCluster = (boot->RootDirEnts * 32 + boot->BytesPerSec - 1)
 	    / boot->BytesPerSec
 	    + boot->ResSectors
-	    + boot->FATs * boot->FATsecs
-	    - CLUST_FIRST * boot->SecPerClust;
+	    + boot->FATs * boot->FATsecs;
 
 	if (boot->BytesPerSec % DOSBOOTBLOCKSIZE != 0) {
 		pfatal("Invalid sector size: %u", boot->BytesPerSec);
@@ -204,14 +203,16 @@ readboot(int dosfs, struct bootblock *bo
 		boot->NumSectors = boot->Sectors;
 	} else
 		boot->NumSectors = boot->HugeSectors;
-	boot->NumClusters = (boot->NumSectors - boot->ClusterOffset) / boot->SecPerClust;
 
-	if (boot->ClusterOffset > (intmax_t)boot->NumSectors) {
-		pfatal("Cluster offset too large (%d sectors)\n",
-		    boot->ClusterOffset);
+	if (boot->FirstCluster + boot->SecPerClust > boot->NumSectors) {
+		pfatal("Cluster offset too large (%u clusters)\n",
+		    boot->FirstCluster);
 		return FSFATAL;
 	}
 
+	boot->NumClusters = (boot->NumSectors - boot->FirstCluster) / boot->SecPerClust
+			    + CLUST_FIRST;
+
 	if (boot->flags&FAT32)
 		boot->ClustMask = CLUST32_MASK;
 	else if (boot->NumClusters < (CLUST_RSRVD&CLUST12_MASK))

Index: src/sbin/fsck_msdos/dir.c
diff -u src/sbin/fsck_msdos/dir.c:1.26 src/sbin/fsck_msdos/dir.c:1.27
--- src/sbin/fsck_msdos/dir.c:1.26	Mon Jul  7 17:45:42 2014
+++ src/sbin/fsck_msdos/dir.c	Fri Jan  2 06:21:28 2015
@@ -1,4 +1,4 @@
-/*	$NetBSD: dir.c,v 1.26 2014/07/07 17:45:42 christos Exp $	*/
+/*	$NetBSD: dir.c,v 1.27 2015/01/02 06:21:28 mlelstv Exp $	*/
 
 /*
  * Copyright (C) 1995, 1996, 1997 Wolfgang Solfrank
@@ -30,7 +30,7 @@
 
 #include <sys/cdefs.h>
 #ifndef lint
-__RCSID("$NetBSD: dir.c,v 1.26 2014/07/07 17:45:42 christos Exp $");
+__RCSID("$NetBSD: dir.c,v 1.27 2015/01/02 06:21:28 mlelstv Exp $");
 #endif /* not lint */
 
 #include <stdio.h>
@@ -326,7 +326,7 @@ delete(int f, struct bootblock *boot, st
 				break;
 			e = delbuf + endoff;
 		}
-		off = startcl * boot->SecPerClust + boot->ClusterOffset;
+		off = (startcl - CLUST_FIRST) * boot->SecPerClust + boot->FirstCluster;
 		off *= boot->BytesPerSec;
 		if (lseek(f, off, SEEK_SET) != off
 		    || read(f, delbuf, clsz) != clsz) {
@@ -491,7 +491,7 @@ readDosDirSection(int f, struct bootbloc
 			off = boot->ResSectors + boot->FATs * boot->FATsecs;
 		} else {
 			last = boot->SecPerClust * boot->BytesPerSec;
-			off = cl * boot->SecPerClust + boot->ClusterOffset;
+			off = (cl - CLUST_FIRST) * boot->SecPerClust + boot->FirstCluster;
 		}
 
 		off *= boot->BytesPerSec;
@@ -967,8 +967,8 @@ reconnect(int dosfs, struct bootblock *b
 			pwarn("No space in %s\n", LOSTDIR);
 			return FSERROR;
 		}
-		lfoff = lfcl * boot->ClusterSize
-		    + boot->ClusterOffset * boot->BytesPerSec;
+		lfoff = (lfcl - CLUST_FIRST) * boot->ClusterSize
+		    + boot->FirstCluster * boot->BytesPerSec;
 		if (lseek(dosfs, lfoff, SEEK_SET) != lfoff
 		    || (size_t)read(dosfs, lfbuf, boot->ClusterSize) != boot->ClusterSize) {
 			perr("could not read LOST.DIR");

Index: src/sbin/fsck_msdos/dosfs.h
diff -u src/sbin/fsck_msdos/dosfs.h:1.7 src/sbin/fsck_msdos/dosfs.h:1.8
--- src/sbin/fsck_msdos/dosfs.h:1.7	Mon Nov  3 18:55:04 2014
+++ src/sbin/fsck_msdos/dosfs.h	Fri Jan  2 06:21:28 2015
@@ -1,4 +1,4 @@
-/*	$NetBSD: dosfs.h,v 1.7 2014/11/03 18:55:04 jakllsch Exp $	*/
+/*	$NetBSD: dosfs.h,v 1.8 2015/01/02 06:21:28 mlelstv Exp $	*/
 
 /*
  * Copyright (C) 1995, 1996, 1997 Wolfgang Solfrank
@@ -70,7 +70,7 @@ struct bootblock {
 	u_int32_t NumSectors;		/* how many sectors are there */
 	u_int32_t FATsecs;		/* how many sectors are in FAT */
 	u_int32_t NumFatEntries;	/* how many entries really are there */
-	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 */
 
 	/* Now some statistics: */

Reply via email to