Skip site navigation (1)Skip section navigation (2)
Date:      Sun, 15 Sep 2019 19:41:54 +0000 (UTC)
From:      Xin LI <delphij@FreeBSD.org>
To:        src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org
Subject:   svn commit: r352364 - head/sbin/fsck_msdosfs
Message-ID:  <201909151941.x8FJfsVf019366@repo.freebsd.org>

next in thread | raw e-mail | index | archive | help
Author: delphij
Date: Sun Sep 15 19:41:54 2019
New Revision: 352364
URL: https://svnweb.freebsd.org/changeset/base/352364

Log:
  Avoid mixing cluster numbers and sector numbers. Makes code more readable.
  
  Obtained from:	NetBSD
  MFC after:	2 weeks

Modified:
  head/sbin/fsck_msdosfs/boot.c
  head/sbin/fsck_msdosfs/dir.c
  head/sbin/fsck_msdosfs/dosfs.h

Modified: head/sbin/fsck_msdosfs/boot.c
==============================================================================
--- head/sbin/fsck_msdosfs/boot.c	Sun Sep 15 19:38:15 2019	(r352363)
+++ head/sbin/fsck_msdosfs/boot.c	Sun Sep 15 19:41:54 2019	(r352364)
@@ -259,12 +259,18 @@ readboot(int dosfs, struct bootblock *boot)
 		return FSFATAL;
 	}
 
-	boot->ClusterOffset = (boot->bpbRootDirEnts * 32 +
+	boot->FirstCluster = (boot->bpbRootDirEnts * 32 +
 	    boot->bpbBytesPerSec - 1) / boot->bpbBytesPerSec +
-	    boot->bpbResSectors + boot->bpbFATs * boot->FATsecs -
-	    CLUST_FIRST * boot->bpbSecPerClust;
-	boot->NumClusters = (boot->NumSectors - boot->ClusterOffset) /
-	    boot->bpbSecPerClust;
+	    boot->bpbResSectors + boot->bpbFATs * boot->FATsecs;
+
+	if (boot->FirstCluster + boot->bpbSecPerClust > boot->NumSectors) {
+		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)
 		boot->ClustMask = CLUST32_MASK;

Modified: head/sbin/fsck_msdosfs/dir.c
==============================================================================
--- head/sbin/fsck_msdosfs/dir.c	Sun Sep 15 19:38:15 2019	(r352363)
+++ head/sbin/fsck_msdosfs/dir.c	Sun Sep 15 19:41:54 2019	(r352364)
@@ -317,7 +317,8 @@ delete(int f, struct bootblock *boot, struct fatEntry 
 				break;
 			e = delbuf + endoff;
 		}
-		off = startcl * boot->bpbSecPerClust + boot->ClusterOffset;
+		off = (startcl - CLUST_FIRST) * boot->bpbSecPerClust + boot->FirstCluster;
+
 		off *= boot->bpbBytesPerSec;
 		if (lseek(f, off, SEEK_SET) != off) {
 			perr("Unable to lseek to %" PRId64, off);
@@ -457,7 +458,7 @@ check_subdirectory(int f, struct bootblock *boot, stru
 		off = boot->bpbResSectors + boot->bpbFATs *
 			boot->FATsecs;
 	} 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, struc
 			    boot->FATsecs;
 		} else {
 			last = boot->bpbSecPerClust * boot->bpbBytesPerSec;
-			off = cl * boot->bpbSecPerClust + boot->ClusterOffset;
+			off = (cl - CLUST_FIRST) * boot->bpbSecPerClust + boot->FirstCluster;
 		}
 
 		off *= boot->bpbBytesPerSec;
@@ -1069,8 +1070,9 @@ reconnect(int dosfs, struct bootblock *boot, struct fa
 			lfcl = (lostDir->head < boot->NumClusters) ? lostDir->head : 0;
 			return FSERROR;
 		}
-		lfoff = lfcl * boot->ClusterSize
-		    + boot->ClusterOffset * boot->bpbBytesPerSec;
+		lfoff = (lfcl - CLUST_FIRST) * boot->ClusterSize
+		    + boot->FirstCluster * boot->bpbBytesPerSec;
+
 		if (lseek(dosfs, lfoff, SEEK_SET) != lfoff
 		    || (size_t)read(dosfs, lfbuf, boot->ClusterSize) != boot->ClusterSize) {
 			perr("could not read LOST.DIR");

Modified: head/sbin/fsck_msdosfs/dosfs.h
==============================================================================
--- head/sbin/fsck_msdosfs/dosfs.h	Sun Sep 15 19:38:15 2019	(r352363)
+++ head/sbin/fsck_msdosfs/dosfs.h	Sun Sep 15 19:41:54 2019	(r352364)
@@ -74,7 +74,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 */
-	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 */
 
 	/* Now some statistics: */



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?201909151941.x8FJfsVf019366>