]>
git.saurik.com Git - apple/hfs.git/blob - fsck_hfs/fsck_hfs.c
2 * Copyright (c) 1999-2000, 2002-2008 Apple Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
23 #include <sys/types.h>
25 #include <sys/param.h>
26 #include <sys/ucred.h>
27 #include <sys/mount.h>
28 #include <sys/ioctl.h>
30 #include <sys/sysctl.h>
33 #include <hfs/hfs_mount.h>
44 #include <TargetConditionals.h>
47 #include "fsck_msgnums.h"
48 #include "fsck_hfs_msgnums.h"
50 #include "fsck_debug.h"
51 #include "dfalib/CheckHFS.h"
54 * These definitions are duplicated from xnu's hfs_readwrite.c, and could live
55 * in a shared header file if desired. On the other hand, the freeze and thaw
56 * commands are not really supposed to be public.
59 #define F_FREEZE_FS 53 /* "freeze" all fs operations */
60 #define F_THAW_FS 54 /* "thaw" all fs operations */
63 /* Global Variables for front end */
64 const char *cdevname
; /* name of device being checked */
66 char lflag
; /* live fsck */
67 char nflag
; /* assume a no response */
68 char yflag
; /* assume a yes response */
69 char preen
; /* just fix normal inconsistencies */
70 char force
; /* force fsck even if clean (preen only) */
71 char quick
; /* quick check returns clean, dirty, or failure */
72 char debug
; /* output debugging info */
73 char disable_journal
; /* If debug, and set, do not simulate journal replay */
74 char scanflag
; /* Scan entire disk for bad blocks */
75 #if !TARGET_OS_EMBEDDED
81 char hotroot
; /* checking root device */
82 char hotmount
; /* checking read-only mounted device */
83 char guiControl
; /* this app should output info for gui control */
84 char xmlControl
; /* Output XML (plist) messages -- implies guiControl as well */
85 char rebuildBTree
; /* Rebuild requested btree files */
86 int rebuildOptions
; /* Options to indicate which btree should be rebuilt */
87 char modeSetting
; /* set the mode when creating "lost+found" directory */
88 char errorOnExit
= 0; /* Exit on first error */
89 int upgrading
; /* upgrading format */
90 int lostAndFoundMode
= 0; /* octal mode used when creating "lost+found" directory */
91 uint64_t reqCacheSize
; /* Cache size requested by the caller (may be specified by the user via -c) */
93 int fsmodified
; /* 1 => write done to file system */
94 int fsreadfd
; /* file descriptor for reading file system */
95 int fswritefd
; /* file descriptor for writing file system */
99 * Variables used to map physical block numbers to file paths
101 enum { BLOCK_LIST_INCREMENT
= 512 };
102 int gBlkListEntries
= 0;
103 u_int64_t
*gBlockList
= NULL
;
104 int gFoundBlockEntries
= 0;
105 struct found_blocks
*gFoundBlocksList
= NULL
;
106 long gBlockSize
= 512;
107 static void ScanDisk(int);
108 static int getblocklist(const char *filepath
);
111 static int checkfilesys
__P((char * filesys
));
112 static int setup
__P(( char *dev
, int *canWritePtr
));
113 static void usage
__P((void));
114 static void getWriteAccess
__P(( char *dev
, int *canWritePtr
));
115 extern char *unrawname
__P((char *name
));
128 if ((progname
= strrchr(*argv
, '/')))
133 while ((ch
= getopt(argc
, argv
, "b:B:c:D:e:Edfglm:npqrR:SuyxJ")) != EOF
) {
136 gBlockSize
= atoi(optarg
);
137 if ((gBlockSize
< 512) || (gBlockSize
& (gBlockSize
-1))) {
138 (void) fprintf(stderr
, "%s invalid block size %d\n",
139 progname
, gBlockSize
);
147 getblocklist(optarg
);
150 /* Cache size to use in fsck_hfs */
151 reqCacheSize
= strtoull(optarg
, &lastChar
, 0);
153 switch (tolower(*lastChar
)) {
155 reqCacheSize
*= 1024ULL;
158 reqCacheSize
*= 1024ULL;
161 reqCacheSize
*= 1024ULL;
178 /* Input value should be in hex example: -D 0x5 */
179 cur_debug_level
= strtoul(optarg
, NULL
, 0);
180 if (cur_debug_level
== 0) {
181 (void) fplog (stderr
, "%s: invalid debug development argument. Assuming zero\n", progname
);
187 if (strcasecmp(optarg
, "embedded") == 0)
189 else if (strcasecmp(optarg
, "desktop") == 0)
195 /* Exit on first error, after logging it */
220 lostAndFoundMode
= strtol( optarg
, NULL
, 8 );
221 if ( lostAndFoundMode
== 0 )
223 (void) fplog(stderr
, "%s: invalid mode argument \n", progname
);
242 // rebuild catalog btree
244 rebuildOptions
|= REBUILD_CATALOG
;
253 // rebuild attribute btree
255 rebuildOptions
|= REBUILD_ATTRIBUTE
;
259 // rebuild catalog btree
261 rebuildOptions
|= REBUILD_CATALOG
;
265 // rebuild extents overflow btree
267 rebuildOptions
|= REBUILD_EXTENTS
;
271 fprintf(stderr
, "%s: unknown btree rebuild code `%c' (%#x)\n", progname
, *cp
, *cp
);
294 if (debug
== 0 && disable_journal
!= 0)
297 if (gBlkListEntries
!= 0 && gBlockSize
== 0)
301 debug
= 0; /* debugging is for command line only */
303 if (signal(SIGINT
, SIG_IGN
) != SIG_IGN
)
304 (void)signal(SIGINT
, catch);
307 (void) fplog(stderr
, "%s: missing special-device\n", progname
);
313 ret
|= checkfilesys(blockcheck(*argv
++));
318 int fs_fd
=-1; // fd to the root-dir of the fs we're checking (only w/lfag == 1)
324 fcntl(fs_fd
, F_THAW_FS
, NULL
);
331 mountpoint(const char *cdev
)
334 struct statfs
*fsinfo
;
339 unraw
= strdup(cdev
);
345 result
= getmntinfo(&fsinfo
, MNT_NOWAIT
);
347 for (i
= 0; i
< result
; i
++) {
348 if (strcmp(unraw
, fsinfo
[i
].f_mntfromname
) == 0) {
349 retval
= strdup(fsinfo
[i
].f_mntonname
);
362 checkfilesys(char * filesys
)
366 int chkLev
, repLev
, logLev
;
368 char *mntonname
= NULL
;
369 fsck_ctx_t context
= NULL
;
373 hotmount
= hotroot
; // hotroot will be 1 or 0 by this time
376 // initialize the printing/logging without actually printing anything
377 // DO NOT DELETE THIS or else you can deadlock during a live fsck
378 // when something is printed and we try to create the log file.
382 context
= fsckCreate();
384 mntonname
= mountpoint(cdevname
);
388 mntonname
= strdup("/");
395 * Ensure that, if we're doing a live verify, that we're not trying
396 * to do input or output to the same device. This would cause a deadlock.
399 if (stat(cdevname
, &fs_stat
) != -1 &&
400 (((fs_stat
.st_mode
& S_IFMT
) == S_IFCHR
) ||
401 ((fs_stat
.st_mode
& S_IFMT
) == S_IFBLK
))) {
404 if (fstat(fileno(stdin
), &io_stat
) != -1 &&
405 (fs_stat
.st_rdev
== io_stat
.st_dev
)) {
406 plog("ERROR: input redirected from target volume for live verify.\n");
409 if (fstat(fileno(stdout
), &io_stat
) != -1 &&
410 (fs_stat
.st_rdev
== io_stat
.st_dev
)) {
411 plog("ERROR: output redirected to target volume for live verify.\n");
414 if (fstat(fileno(stderr
), &io_stat
) != -1 &&
415 (fs_stat
.st_rdev
== io_stat
.st_dev
)) {
416 plog("ERROR: error output redirected to target volume for live verify.\n");
424 * If the device is mounted somewhere, then we need to make sure that it's
425 * a read-only device, or that a live-verify has been requested.
427 if (mntonname
!= NULL
) {
428 struct statfs stfs_buf
;
430 if (statfs(mntonname
, &stfs_buf
) == 0) {
432 // Need to try to freeze it
433 fs_fd
= open(mntonname
, O_RDONLY
);
435 plog("ERROR: could not open %s to freeze the volume.\n", mntonname
);
440 if (fcntl(fs_fd
, F_FREEZE_FS
, NULL
) != 0) {
442 plog("ERROR: could not freeze volume (%s)\n", strerror(errno
));
445 } else if (stfs_buf
.f_flags
& MNT_RDONLY
) {
448 /* MNT_RDONLY is not set and this is not a live verification */
449 plog("ERROR: volume %s is mounted with write access. Re-run with (-l) to freeze volume.\n", mntonname
);
458 if (setup( filesys
, &canWrite
) == 0) {
460 pfatal("CAN'T CHECK FILE SYSTEM.");
462 goto ExitThisRoutine
;
466 if (hotroot
&& !guiControl
)
467 plog("** Root file system\n");
470 /* start with defaults for dfa back-end */
471 chkLev
= kAlwaysCheck
;
472 repLev
= kMajorRepairs
;
473 logLev
= kVerboseLog
;
476 repLev
= kMajorRepairs
;
479 chkLev
= kNeverCheck
;
480 repLev
= kNeverRepair
;
483 chkLev
= kForceCheck
;
486 repLev
= kMinorRepairs
;
487 chkLev
= force
? kAlwaysCheck
: kDirtyCheck
;
494 repLev
= kNeverRepair
;
496 if ( rebuildBTree
) {
497 chkLev
= kPartialCheck
;
498 repLev
= kForceRepairs
; // this will force rebuild of B-Tree file
501 fsckSetVerbosity(context
, logLev
);
502 /* All of fsck_hfs' output should go thorugh logstring */
503 fsckSetOutput(context
, NULL
);
504 /* Setup writer that will output to standard out */
505 fsckSetWriter(context
, &outstring
);
506 /* Setup logger that will write to log file */
507 fsckSetLogger(context
, &logstring
);
510 fsckSetOutputStyle(context
, fsckOutputXML
);
512 fsckSetOutputStyle(context
, fsckOutputGUI
);
514 fsckSetOutputStyle(context
, fsckOutputTraditional
);
517 if (errorOnExit
&& nflag
) {
518 chkLev
= kMajorCheck
;
522 * go check HFS volume...
525 if (rebuildOptions
&& canWrite
== 0) {
526 plog("BTree rebuild requested but writing disabled\n");
528 goto ExitThisRoutine
;
531 if (gBlockList
!= NULL
&& scanflag
!= 0) {
532 plog("Cannot scan for bad blocks and ask for listed blocks to file mapping\n");
534 goto ExitThisRoutine
;
537 plog("Scanning entire disk for bad blocks\n");
541 result
= CheckHFS( filesys
, fsreadfd
, fswritefd
, chkLev
, repLev
, context
,
542 lostAndFoundMode
, canWrite
, &fsmodified
,
543 lflag
, rebuildOptions
);
545 plog("\tCheckHFS returned %d, fsmodified = %d\n", result
, fsmodified
);
551 pwarn("QUICKCHECK ONLY; FILESYSTEM CLEAN\n");
553 goto ExitThisRoutine
;
554 } else if (result
== R_Dirty
) {
555 pwarn("QUICKCHECK ONLY; FILESYSTEM DIRTY\n");
557 goto ExitThisRoutine
;
558 } else if (result
== R_BadSig
) {
559 pwarn("QUICKCHECK ONLY; NO HFS SIGNATURE FOUND\n");
561 goto ExitThisRoutine
;
564 goto ExitThisRoutine
;
568 struct statfs stfs_buf
;
571 * Check to see if root is mounted read-write.
573 if (statfs(mntonname
, &stfs_buf
) == 0)
574 flags
= stfs_buf
.f_flags
;
577 ckfini(flags
& MNT_RDONLY
);
580 /* XXX free any allocated memory here */
582 if (hotmount
&& fsmodified
) {
583 struct hfs_mount_args args
;
585 * We modified the root. Do a mount update on
586 * it, unless it is read-write, so we can continue.
589 fsckPrint(context
, fsckVolumeModified
);
590 if (flags
& MNT_RDONLY
) {
591 bzero(&args
, sizeof(args
));
592 flags
|= MNT_UPDATE
| MNT_RELOAD
;
594 fprintf(stderr
, "doing update / reload mount for %s now\n", mntonname
);
595 if (mount("hfs", mntonname
, flags
, &args
) == 0) {
598 goto ExitThisRoutine
;
601 fprintf(stderr
, "update/reload mount for %s failed: %s\n", mntonname
, strerror(errno
));
605 plog("\n***** REBOOT NOW *****\n");
607 result
= FIXEDROOTEXIT
;
608 goto ExitThisRoutine
;
611 if (result
!= 0 && result
!= MAJOREXIT
)
617 fcntl(fs_fd
, F_THAW_FS
, NULL
);
626 fsckDestroy(context
);
633 * Setup for I/O to device
634 * Return 1 if successful, 0 if unsuccessful.
635 * canWrite - 1 if we can safely write to the raw device or 0 if not.
638 setup( char *dev
, int *canWritePtr
)
642 uint32_t cacheBlockSize
;
643 uint32_t cacheTotalBlocks
;
649 if (stat(dev
, &statb
) < 0) {
650 plog("Can't stat %s: %s\n", dev
, strerror(errno
));
653 if ((statb
.st_mode
& S_IFMT
) != S_IFCHR
) {
654 pfatal("%s is not a character device", dev
);
655 if (reply("CONTINUE") == 0)
658 /* Always attempt to replay the journal */
659 if (!nflag
&& !quick
) {
660 // We know we have a character device by now.
661 if (strncmp(dev
, "/dev/rdisk", 10) == 0) {
662 char block_device
[MAXPATHLEN
+1];
664 snprintf(block_device
, sizeof(block_device
), "/dev/%s", dev
+ 6);
665 rv
= journal_replay(block_device
);
667 plog("journal_replay(%s) returned %d\n", block_device
, rv
);
670 /* attempt to get write access to the block device and if not check if volume is */
671 /* mounted read-only. */
672 if (nflag
== 0 && quick
== 0) {
673 getWriteAccess( dev
, canWritePtr
);
676 if (nflag
|| quick
|| (fswritefd
= open(dev
, O_RDWR
| (hotmount
? 0 : O_EXLOCK
))) < 0) {
679 pfatal("** %s (NO WRITE ACCESS)\n", dev
);
683 if (preen
== 0 && !guiControl
) {
684 if (nflag
|| quick
|| fswritefd
== -1) {
685 plog("** %s (NO WRITE)\n", dev
);
687 plog("** %s\n", dev
);
691 if (fswritefd
== -1) {
692 if ((fsreadfd
= open(dev
, O_RDONLY
)) < 0) {
693 plog("Can't open %s: %s\n", dev
, strerror(errno
));
697 fsreadfd
= dup(fswritefd
);
699 plog("Can't dup fd for reading on %s: %s\n", dev
, strerror(errno
));
706 /* Get device block size to initialize cache */
707 if (ioctl(fsreadfd
, DKIOCGETBLOCKSIZE
, &devBlockSize
) < 0) {
708 pfatal ("Can't get device block size\n");
713 * Calculate the cache block size and total blocks.
715 * If a quick check was requested, we'll only be checking to see if
716 * the volume was cleanly unmounted or journalled, so we won't need
717 * a lot of cache. Since lots of quick checks can be run in parallel
718 * when a new disk with several partitions comes on line, let's avoid
719 * the memory usage when we don't need it.
721 if (reqCacheSize
== 0 && quick
== 0) {
723 * Auto-pick the cache size. The cache code will deal with minimum
724 * maximum values, so we just need to find out the size of memory, and
725 * how much of it we'll use.
727 * If we're looking at the root device, and it's not a live verify (lflag),
728 * then we will use half of physical memory; otherwise, we'll use an eigth.
732 size_t dsize
= sizeof(memSize
);
735 rv
= sysctlbyname("hw.memsize", &memSize
, &dsize
, NULL
, 0);
737 (void)fplog(stderr
, "sysctlbyname failed, not auto-setting cache size\n");
739 int d
= (hotroot
&& !lflag
) ? 2 : 8;
741 dsize
= sizeof(safeMode
);
742 rv
= sysctlbyname("kern.safeboot", &safeMode
, &dsize
, NULL
, 0);
743 if (rv
!= -1 && safeMode
!= 0 && hotroot
&& !lflag
) {
744 #define kMaxSafeModeMem ((size_t)2 * 1024 * 1024 * 1024) /* 2Gbytes, means cache will max out at 1gbyte */
746 (void)fplog(stderr
, "Safe mode and single-user, setting memsize to a maximum of 2gbytes\n");
748 memSize
= (memSize
< kMaxSafeModeMem
) ? memSize
: kMaxSafeModeMem
;
750 reqCacheSize
= memSize
/ d
;
754 CalculateCacheSizes(reqCacheSize
, &cacheBlockSize
, &cacheTotalBlocks
, debug
);
756 preTouchMem
= (hotroot
!= 0) && (lflag
!= 0);
757 /* Initialize the cache */
758 if (CacheInit (&fscache
, fsreadfd
, fswritefd
, devBlockSize
,
759 cacheBlockSize
, cacheTotalBlocks
, CacheHashSize
, preTouchMem
) != EOK
) {
760 pfatal("Can't initialize disk cache\n");
768 // This routine will attempt to open the block device with write access for the target
769 // volume in order to block others from mounting the volume with write access while we
770 // check / repair it. If we cannot get write access then we check to see if the volume
771 // has been mounted read-only. If it is read-only then we should be OK to write to
772 // the raw device. Note that this does not protect use from someone upgrading the mount
773 // from read-only to read-write.
775 static void getWriteAccess( char *dev
, int *canWritePtr
)
781 struct statfs
* myBufPtr
;
783 int blockDevice_fd
= -1;
786 myNamePtr
= malloc( strlen(dev
) + 2 );
787 if ( myNamePtr
== NULL
)
790 strcpy( (char *)myNamePtr
, dev
);
791 if ( (myCharPtr
= strrchr( (char *)myNamePtr
, '/' )) != 0 ) {
792 if ( myCharPtr
[1] == 'r' ) {
793 memmove(&myCharPtr
[1], &myCharPtr
[2], strlen(&myCharPtr
[2]) + 1);
794 blockDevice_fd
= open( (char *)myNamePtr
, O_WRONLY
| (hotmount
? 0 : O_EXLOCK
) );
798 if ( blockDevice_fd
> 0 ) {
799 // we got write access to the block device so we can safely write to raw device
801 goto ExitThisRoutine
;
804 // get count of mounts then get the info for each
805 myMountsCount
= getfsstat( NULL
, 0, MNT_NOWAIT
);
806 if ( myMountsCount
< 0 )
807 goto ExitThisRoutine
;
809 myPtr
= (void *) malloc( sizeof(struct statfs
) * myMountsCount
);
811 goto ExitThisRoutine
;
812 myMountsCount
= getfsstat( myPtr
,
813 (int)(sizeof(struct statfs
) * myMountsCount
),
815 if ( myMountsCount
< 0 )
816 goto ExitThisRoutine
;
818 myBufPtr
= (struct statfs
*) myPtr
;
819 for ( i
= 0; i
< myMountsCount
; i
++ )
821 if ( strcmp( myBufPtr
->f_mntfromname
, myNamePtr
) == 0 ) {
822 if ( myBufPtr
->f_flags
& MNT_RDONLY
)
824 goto ExitThisRoutine
;
828 *canWritePtr
= 1; // single user will get us here, f_mntfromname is not /dev/diskXXXX
834 if ( myNamePtr
!= NULL
)
837 if (blockDevice_fd
!= -1) {
838 close(blockDevice_fd
);
843 } /* getWriteAccess */
849 (void) fplog(stderr
, "usage: %s [-b [size] B [path] c [size] e [mode] ESdfglx m [mode] npqruy] special-device\n", progname
);
850 (void) fplog(stderr
, " b size = size of physical blocks (in bytes) for -B option\n");
851 (void) fplog(stderr
, " B path = file containing physical block numbers to map to paths\n");
852 (void) fplog(stderr
, " c size = cache size (ex. 512m, 1g)\n");
853 (void) fplog(stderr
, " e mode = emulate 'embedded' or 'desktop'\n");
854 (void) fplog(stderr
, " E = exit on first major error\n");
855 (void) fplog(stderr
, " d = output debugging info\n");
856 (void) fplog(stderr
, " f = force fsck even if clean (preen only) \n");
857 (void) fplog(stderr
, " g = GUI output mode\n");
858 (void) fplog(stderr
, " x = XML output mode\n");
859 (void) fplog(stderr
, " l = live fsck (lock down and test-only)\n");
860 (void) fplog(stderr
, " m arg = octal mode used when creating lost+found directory \n");
861 (void) fplog(stderr
, " n = assume a no response \n");
862 (void) fplog(stderr
, " p = just fix normal inconsistencies \n");
863 (void) fplog(stderr
, " q = quick check returns clean, dirty, or failure \n");
864 (void) fplog(stderr
, " r = rebuild catalog btree \n");
865 (void) fplog(stderr
, " S = Scan disk for bad blocks\n");
866 (void) fplog(stderr
, " u = usage \n");
867 (void) fplog(stderr
, " y = assume a yes response \n");
874 AddBlockToList(long long block
)
877 if ((gBlkListEntries
% BLOCK_LIST_INCREMENT
) == 0) {
880 // gBlkListEntries += BLOCK_LIST_INCREMENT;
881 tmp
= realloc(gBlockList
, (gBlkListEntries
+ BLOCK_LIST_INCREMENT
) * sizeof(u_int64_t
));
883 pfatal("Can't allocate memory for block list (%llu entries).\n", gBlkListEntries
);
885 gBlockList
= (u_int64_t
*)tmp
;
887 gBlockList
[gBlkListEntries
++] = block
;
891 static int printStatus
;
901 uint32_t devBlockSize
= 512;
902 uint64_t devBlockTotal
;
904 uint8_t *buffer
= NULL
;
905 size_t bufSize
= 1024 * 1024;
908 void (*oldhandler
)(int);
909 uint32_t numErrors
= 0;
910 uint32_t maxErrors
= 40; // Something more variable?
912 oldhandler
= signal(SIGINFO
, &siginfo
);
917 fprintf(stderr, "Scanning offset %lld of %lld (%d%%)\n", \
918 curPos, diskSize, (int)((curPos * 100) / diskSize)); \
920 fprintf(stderr, "Scanning offset %lld\n", curPos); \
925 if (ioctl(fd
, DKIOCGETBLOCKSIZE
, &devBlockSize
) == -1) {
929 if (ioctl(fd
, DKIOCGETBLOCKCOUNT
, &devBlockTotal
) == -1) {
932 diskSize
= devBlockTotal
* devBlockSize
;
934 while (buffer
== NULL
&& bufSize
>= devBlockSize
) {
935 buffer
= malloc(bufSize
);
936 if (buffer
== NULL
) {
940 if (buffer
== NULL
) {
941 pfatal("Cannot allocate buffer for disk scan.\n");
949 while ((nread
= pread(fd
, buffer
, bufSize
, curPos
)) == bufSize
) {
957 /* We're done with the disk */
962 /* Try reading devBlockSize blocks */
964 for (total
= 0; total
< bufSize
; total
+= devBlockSize
) {
965 nread
= pread(fd
, buffer
, devBlockSize
, curPos
+ total
);
969 fprintf(stderr
, "Bad block at offset %lld\n", curPos
+ total
);
970 AddBlockToList((curPos
+ total
) / gBlockSize
);
971 if (++numErrors
> maxErrors
) {
973 fprintf(stderr
, "Got %u errors, maxing out so stopping scan\n", numErrors
);
978 pfatal("Got a non I/O error reading disk at offset %llu: %s\n",
979 curPos
+ total
, strerror(errno
));
980 // Hey, pfatal wasn't fatal!
981 // But that seems to work out for us for some reason.
985 /* End of disk, somehow. */
988 if (nread
!= devBlockSize
) {
989 pwarn("During disk scan, did not get block size (%zd) read, got %zd instead. Skipping rest of this block.\n", (size_t)devBlockSize
, nread
);
995 } else if (errno
== EINTR
) {
998 pfatal("Got a non I/O error reading disk at offset %llu: %s\n", curPos
, strerror(errno
));
1002 if (nread
< bufSize
) {
1003 if ((nread
% devBlockSize
) == 0) {
1006 curPos
= curPos
+ (((nread
% devBlockSize
) + 1) * devBlockSize
);
1014 signal(SIGINFO
, oldhandler
);
1020 getblocklist(const char *filepath
)
1024 size_t blockListCount
; /* Number of elements allocated to gBlockList array */
1026 blockListCount
= BLOCK_LIST_INCREMENT
;
1027 gBlockList
= (u_int64_t
*) malloc(blockListCount
* sizeof(u_int64_t
));
1028 if (gBlockList
== NULL
)
1029 pfatal("Can't allocate memory for block list.\n");
1031 // printf("getblocklist: processing blocklist %s...\n", filepath);
1033 if ((file
= fopen(filepath
, "r")) == NULL
)
1034 pfatal("Can't open %s\n", filepath
);
1036 while (fscanf(file
, "%lli", &block
) > 0) {
1037 AddBlockToList(block
);
1040 (void) fclose(file
);
1042 printf("%d blocks to match:\n", gBlkListEntries
);