2 * Copyright (c) 1999-2001 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
23 Copyright (c) 1987-99 Apple Computer, Inc.
27 Contains code to implement hfs utility used by the WorkSpace to mount HFS.
30 5-Jan-1999 Don Brady Write hfs.label names in UTF-8.
31 10-Dec-1998 Pat Dirks Changed to try built-in hfs filesystem first.
32 3-Sep-1998 Don Brady Disable the daylight savings time stuff.
33 28-Aug-1998 chw Fixed parse args and verify args to indicate that the
34 flags (fixed or removable) are required in the probe case.
35 22-Jun-1998 Pat Dirks Changed HFSToUFSStr table to switch ":" and "/".
36 13-Jan-1998 jwc first cut (derived from old NextStep macfs.util code and cdrom.util code).
40 /* ************************************** I N C L U D E S ***************************************** */
42 #include <sys/types.h>
45 #include <sys/sysctl.h>
46 #include <sys/resource.h>
47 #include <sys/vmmeter.h>
48 #include <sys/mount.h>
52 #include <sys/loadable_fs.h>
53 #include <hfs/hfs_format.h>
64 #include <CoreFoundation/CFString.h>
66 #define READ_DEFAULT_ENCODING 1
68 #ifndef FSUR_MOUNT_HIDDEN
69 #define FSUR_MOUNT_HIDDEN (-9)
73 #define FSUC_ADOPT 'a'
77 #define FSUC_DISOWN 'd'
81 #define FSUC_GETUUID 'k'
85 #define FSUC_SETUUID 's'
89 #define FSUC_MKJNL 'J'
93 #define FSUC_UNJNL 'U'
97 /* **************************************** L O C A L S ******************************************* */
99 #define HFS_BLOCK_SIZE 512
101 char gHFS_FS_NAME
[] = "hfs";
102 char gHFS_FS_NAME_NAME
[] = "HFS";
104 char gNewlineString
[] = "\n";
106 char gMountCommand
[] = "/sbin/mount";
108 char gUnmountCommand
[] = "/sbin/umount";
110 char gReadOnlyOption
[] = "-r";
111 char gReadWriteOption
[] = "-w";
113 char gSuidOption
[] = "suid";
114 char gNoSuidOption
[] = "nosuid";
116 char gDevOption
[] = "dev";
117 char gNoDevOption
[] = "nodev";
119 char gUsePermissionsOption
[] = "perm";
120 char gIgnorePermissionsOption
[] = "noperm";
122 boolean_t gIsEjectable
= 0;
124 int gJournalSize
= 0;
126 #define AUTO_ADOPT_FIXED 1
127 #define AUTO_ENTER_FIXED 0
130 #define VOLUMEUUIDVALUESIZE 2
131 typedef union VolumeUUID
{
132 unsigned long value
[VOLUMEUUIDVALUESIZE
];
139 #define VOLUMEUUIDLENGTH 16
140 typedef char VolumeUUIDString
[VOLUMEUUIDLENGTH
+1];
142 #define VOLUME_RECORDED 0x80000000
143 #define VOLUME_USEPERMISSIONS 0x00000001
144 #define VOLUME_VALIDSTATUSBITS ( VOLUME_USEPERMISSIONS )
146 typedef void *VolumeStatusDBHandle
;
148 void GenerateVolumeUUID(VolumeUUID
*newVolumeID
);
149 void ConvertVolumeUUIDStringToUUID(const char *UUIDString
, VolumeUUID
*volumeID
);
150 void ConvertVolumeUUIDToString(VolumeUUID
*volumeID
, char *UUIDString
);
151 int OpenVolumeStatusDB(VolumeStatusDBHandle
*DBHandlePtr
);
152 int GetVolumeStatusDBEntry(VolumeStatusDBHandle DBHandle
, VolumeUUID
*volumeID
, unsigned long *VolumeStatus
);
153 int SetVolumeStatusDBEntry(VolumeStatusDBHandle DBHandle
, VolumeUUID
*volumeID
, unsigned long VolumeStatus
);
154 int DeleteVolumeStatusDBEntry(VolumeStatusDBHandle DBHandle
, VolumeUUID
*volumeID
);
155 int CloseVolumeStatusDB(VolumeStatusDBHandle DBHandle
);
157 /* ************************************ P R O T O T Y P E S *************************************** */
158 static void DoDisplayUsage( const char * argv
[] );
159 static void DoFileSystemFile( char * theFileNameSuffixPtr
, char * theContentsPtr
);
160 static int DoMount( char * theDeviceNamePtr
, const char * theMountPointPtr
, boolean_t isLocked
, boolean_t isSetuid
, boolean_t isDev
);
161 static int DoProbe( char * theDeviceNamePtr
);
162 static int DoUnmount( const char * theMountPointPtr
);
163 static int DoGetUUIDKey( const char * theDeviceNamePtr
);
164 static int DoChangeUUIDKey( const char * theDeviceNamePtr
);
165 static int DoAdopt( const char * theDeviceNamePtr
);
166 static int DoDisown( const char * theDeviceNamePtr
);
168 extern int DoMakeJournaled( const char * volNamePtr
, int journalSize
); // XXXdbg
169 extern int DoUnJournal( const char * volNamePtr
); // XXXdbg
171 static int ParseArgs( int argc
, const char * argv
[], const char ** actionPtr
, const char ** mountPointPtr
, boolean_t
* isEjectablePtr
, boolean_t
* isLockedPtr
, boolean_t
* isSetuidPtr
, boolean_t
* isDevPtr
);
173 static int GetVolumeUUID(const char *deviceNamePtr
, VolumeUUID
*volumeUUIDPtr
, boolean_t generate
);
174 static int SetVolumeUUID(const char *deviceNamePtr
, VolumeUUID
*volumeUUIDPtr
);
175 static int GetEmbeddedHFSPlusVol(HFSMasterDirectoryBlock
* hfsMasterDirectoryBlockPtr
, off_t
* startOffsetPtr
);
176 static int GetNameFromHFSPlusVolumeStartingAt(int fd
, off_t hfsPlusVolumeOffset
, char * name_o
);
177 static int GetBTreeNodeInfo(int fd
, off_t hfsPlusVolumeOffset
, u_int32_t blockSize
,
178 u_int32_t extentCount
, const HFSPlusExtentDescriptor
*extentList
,
179 u_int32_t
*nodeSize
, u_int32_t
*firstLeafNode
);
180 static int GetCatalogOverflowExtents(int fd
, off_t hfsPlusVolumeOffset
, HFSPlusVolumeHeader
*volHdrPtr
,
181 HFSPlusExtentDescriptor
**catalogExtents
, u_int32_t
*catalogExtCount
);
182 static int LogicalToPhysical(off_t logicalOffset
, ssize_t length
, u_int32_t blockSize
,
183 u_int32_t extentCount
, const HFSPlusExtentDescriptor
*extentList
,
184 off_t
*physicalOffset
, ssize_t
*availableBytes
);
185 static int ReadFile(int fd
, void *buffer
, off_t offset
, ssize_t length
,
186 off_t volOffset
, u_int32_t blockSize
,
187 u_int32_t extentCount
, const HFSPlusExtentDescriptor
*extentList
);
188 static ssize_t
readAt( int fd
, void * buf
, off_t offset
, ssize_t length
);
189 static ssize_t
writeAt( int fd
, void * buf
, off_t offset
, ssize_t length
);
193 * The fuction CFStringGetSystemEncoding does not work correctly in
194 * our context (autodiskmount deamon). We include a local copy here
195 * so that we can derive the default encoding. Radar 2516316.
197 #if READ_DEFAULT_ENCODING
198 #define __kCFUserEncodingFileName ("/.CFUserTextEncoding")
200 static unsigned int __CFStringGetDefaultEncodingForHFSUtil() {
201 struct passwd
*passwdp
;
203 if ((passwdp
= getpwuid(0))) { // root account
204 char buffer
[MAXPATHLEN
+ 1];
207 strcpy(buffer
, passwdp
->pw_dir
);
208 strcat(buffer
, __kCFUserEncodingFileName
);
210 if ((fd
= open(buffer
, O_RDONLY
, 0)) > 0) {
213 readSize
= read(fd
, buffer
, MAXPATHLEN
);
214 buffer
[(readSize
< 0 ? 0 : readSize
)] = '\0';
216 return strtol(buffer
, NULL
, 0);
219 return 0; // Fallback to smRoman
223 /* ******************************************** main ************************************************
225 This our main entry point to this utility. We get called by the WorkSpace. See ParseArgs
226 for detail info on input arguments.
228 argc - the number of arguments in argv.
229 argv - array of arguments.
231 returns FSUR_IO_SUCCESS if OK else one of the other FSUR_xyz errors in loadable_fs.h.
232 *************************************************************************************************** */
234 int main (int argc
, const char *argv
[])
236 const char * actionPtr
= NULL
;
237 char rawDeviceName
[MAXPATHLEN
];
238 char blockDeviceName
[MAXPATHLEN
];
239 const char * mountPointPtr
= NULL
;
240 int result
= FSUR_IO_SUCCESS
;
241 boolean_t isLocked
= 0; /* reasonable assumptions */
242 boolean_t isSetuid
= 0; /* reasonable assumptions */
243 boolean_t isDev
= 0; /* reasonable assumptions */
245 /* Verify our arguments */
246 if ( (result
= ParseArgs( argc
, argv
, & actionPtr
, & mountPointPtr
, & gIsEjectable
, & isLocked
, &isSetuid
, &isDev
)) != 0 ) {
251 -- Build our device name (full path), should end up with something like:
255 sprintf(rawDeviceName
, "/dev/r%s", argv
[2]);
256 sprintf(blockDeviceName
, "/dev/%s", argv
[2]);
258 /* call the appropriate routine to handle the given action argument after becoming root */
260 switch( * actionPtr
) {
262 result
= DoProbe(rawDeviceName
);
266 case FSUC_MOUNT_FORCE
:
267 result
= DoMount(blockDeviceName
, mountPointPtr
, isLocked
, isSetuid
, isDev
);
271 result
= DoUnmount( mountPointPtr
);
274 result
= DoGetUUIDKey( blockDeviceName
);
278 result
= DoChangeUUIDKey( blockDeviceName
);
281 result
= DoAdopt( blockDeviceName
);
285 result
= DoDisown( blockDeviceName
);
290 result
= DoMakeJournaled( argv
[3], gJournalSize
);
292 result
= DoMakeJournaled( argv
[2], gJournalSize
);
297 result
= DoUnJournal( argv
[2] );
301 /* should never get here since ParseArgs should handle this situation */
302 DoDisplayUsage( argv
);
311 return result
; /*...and make main fit the ANSI spec. */
315 /* ***************************** DoMount ********************************
317 This routine will fire off a system command to mount the given device at the given mountpoint.
318 autodiskmount will make sure the mountpoint exists and will remove it at Unmount time.
320 deviceNamePtr - pointer to the device name (full path, like /dev/disk0s2).
321 mountPointPtr - pointer to the mount point.
324 returns FSUR_IO_SUCCESS everything is cool else one of several other FSUR_xyz error codes.
325 *********************************************************************** */
327 DoMount(char *deviceNamePtr
, const char *mountPointPtr
, boolean_t isLocked
, boolean_t isSetuid
, boolean_t isDev
)
333 char *permissionsOption
;
334 int result
= FSUR_IO_FAIL
;
336 char encodeopt
[16] = "";
337 CFStringEncoding encoding
;
338 VolumeUUID targetVolumeUUID
;
339 VolumeStatusDBHandle vsdbhandle
= NULL
;
340 unsigned long targetVolumeStatus
;
342 if (mountPointPtr
== NULL
|| *mountPointPtr
== '\0')
343 return (FSUR_IO_FAIL
);
345 /* get the volume UUID to check if permissions should be used: */
346 targetVolumeStatus
= 0;
347 if (((result
= GetVolumeUUID(deviceNamePtr
, &targetVolumeUUID
, FALSE
)) != FSUR_IO_SUCCESS
) ||
348 (targetVolumeUUID
.v
.high
==0) ||
349 (targetVolumeUUID
.v
.low
== 0)) {
351 fprintf(stderr
, "hfs.util: DoMount: GetVolumeUUID returned %d.\n", result
);
354 if (gIsEjectable
== 0) {
355 result
= DoAdopt( deviceNamePtr
);
357 fprintf(stderr
, "hfs.util: DoMount: Auto-adopting %s; result = %d.\n", deviceNamePtr
, result
);
359 targetVolumeStatus
= VOLUME_USEPERMISSIONS
;
362 fprintf(stderr
, "hfs.util: DoMount: Not adopting ejectable %s.\n", deviceNamePtr
);
364 targetVolumeStatus
= 0;
368 /* We've got a real volume UUID! */
370 fprintf(stderr
, "hfs.util: DoMount: UUID = %08lX%08lX.\n", targetVolumeUUID
.v
.high
, targetVolumeUUID
.v
.low
);
372 if ((result
= OpenVolumeStatusDB(&vsdbhandle
)) != 0) {
373 /* Can't even get access to the volume info db; assume permissions are OK. */
375 fprintf(stderr
, "hfs.util: DoMount: OpenVolumeStatusDB returned %d; ignoring permissions.\n", result
);
377 targetVolumeStatus
= VOLUME_USEPERMISSIONS
;
380 fprintf(stderr
, "hfs.util: DoMount: Looking up volume status...\n");
382 if ((result
= GetVolumeStatusDBEntry(vsdbhandle
, &targetVolumeUUID
, &targetVolumeStatus
)) != 0) {
384 fprintf(stderr
, "hfs.util: DoMount: GetVolumeStatusDBEntry returned %d.\n", result
);
387 if (gIsEjectable
== 0) {
388 result
= DoAdopt( deviceNamePtr
);
390 fprintf(stderr
, "hfs.util: DoMount: Auto-adopting %s; result = %d.\n", deviceNamePtr
, result
);
392 targetVolumeStatus
= VOLUME_USEPERMISSIONS
;
395 fprintf(stderr
, "hfs.util: DoMount: Not adopting ejectable %s.\n", deviceNamePtr
);
397 targetVolumeStatus
= 0;
400 targetVolumeStatus
= 0;
403 (void)CloseVolumeStatusDB(vsdbhandle
);
410 isLockedstr
= isLocked
? gReadOnlyOption
: gReadWriteOption
;
411 isSetuidstr
= isSetuid
? gSuidOption
: gNoSuidOption
;
412 isDevstr
= isDev
? gDevOption
: gNoDevOption
;
415 (targetVolumeStatus
& VOLUME_USEPERMISSIONS
) ? gUsePermissionsOption
: gIgnorePermissionsOption
;
417 /* get default encoding value (for hfs volumes) */
418 #if READ_DEFAULT_ENCODING
419 encoding
= __CFStringGetDefaultEncodingForHFSUtil();
421 encoding
= CFStringGetSystemEncoding();
423 sprintf(encodeopt
, "-e=%d", (int)encoding
);
425 fprintf(stderr
, "hfs.util: %s %s -o -x -o %s -o %s -o -u=unknown,-g=unknown,-m=0777 -t %s %s %s ...\n",
426 gMountCommand
, isLockedstr
, encodeopt
, permissionsOption
, gHFS_FS_NAME
, deviceNamePtr
, mountPointPtr
);
428 (void) execl(gMountCommand
, gMountCommand
, isLockedstr
, "-o", isSetuidstr
, "-o", isDevstr
,
429 "-o", encodeopt
, "-o", permissionsOption
,
430 "-o", "-u=unknown,-g=unknown,-m=0777",
431 "-t", gHFS_FS_NAME
, deviceNamePtr
, mountPointPtr
, NULL
);
434 /* IF WE ARE HERE, WE WERE UNSUCCESFULL */
435 return (FSUR_IO_FAIL
);
439 return (FSUR_IO_FAIL
);
442 if ((wait4(pid
, (int *)&status
, 0, NULL
) == pid
) && (WIFEXITED(status
)))
443 result
= status
.w_retcode
;
447 return (result
== 0) ? FSUR_IO_SUCCESS
: FSUR_IO_FAIL
;
451 /* ****************************************** DoUnmount *********************************************
453 This routine will fire off a system command to unmount the given device.
455 theDeviceNamePtr - pointer to the device name (full path, like /dev/disk0s2).
457 returns FSUR_IO_SUCCESS everything is cool else FSUR_IO_FAIL.
458 *************************************************************************************************** */
460 DoUnmount(const char * theMountPointPtr
)
466 if (theMountPointPtr
== NULL
|| *theMountPointPtr
== '\0') return (FSUR_IO_FAIL
);
471 fprintf(stderr
, "hfs.util: %s %s ...\n", gUnmountCommand
, theMountPointPtr
);
473 (void) execl(gUnmountCommand
, gUnmountCommand
, theMountPointPtr
, NULL
);
475 /* IF WE ARE HERE, WE WERE UNSUCCESFULL */
476 return (FSUR_IO_FAIL
);
480 return (FSUR_IO_FAIL
);
483 if ((wait4(pid
, (int *)&status
, 0, NULL
) == pid
) && (WIFEXITED(status
)))
484 result
= status
.w_retcode
;
488 return (result
== 0 ? FSUR_IO_SUCCESS
: FSUR_IO_FAIL
);
493 /* ******************************************* DoProbe **********************************************
495 This routine will open the given raw device and check to make sure there is media that looks
498 theDeviceNamePtr - pointer to the device name (full path, like /dev/disk0s2).
500 returns FSUR_MOUNT_HIDDEN (previously FSUR_RECOGNIZED) if we can handle the media else one of the FSUR_xyz error codes.
501 *************************************************************************************************** */
503 DoProbe(char *deviceNamePtr
)
505 int result
= FSUR_UNRECOGNIZED
;
508 HFSMasterDirectoryBlock
* mdbPtr
;
509 HFSPlusVolumeHeader
* volHdrPtr
;
510 u_char volnameUTF8
[NAME_MAX
+1];
512 bufPtr
= (char *)malloc(HFS_BLOCK_SIZE
);
514 result
= FSUR_UNRECOGNIZED
;
518 mdbPtr
= (HFSMasterDirectoryBlock
*) bufPtr
;
519 volHdrPtr
= (HFSPlusVolumeHeader
*) bufPtr
;
521 fd
= open( deviceNamePtr
, O_RDONLY
, 0 );
523 result
= FSUR_IO_FAIL
;
528 * Read the HFS Master Directory Block from sector 2
530 result
= readAt(fd
, bufPtr
, (off_t
)(2 * HFS_BLOCK_SIZE
), HFS_BLOCK_SIZE
);
531 if (FSUR_IO_FAIL
== result
)
534 /* get classic HFS volume name (from MDB) */
535 if (mdbPtr
->drSigWord
== kHFSSigWord
&&
536 mdbPtr
->drEmbedSigWord
!= kHFSPlusSigWord
) {
539 CFStringEncoding encoding
;
541 /* Some poorly mastered HFS CDs have an empty MDB name field! */
542 if (mdbPtr
->drVN
[0] == '\0') {
543 strcpy(&mdbPtr
->drVN
[1], gHFS_FS_NAME_NAME
);
544 mdbPtr
->drVN
[0] = strlen(gHFS_FS_NAME_NAME
);
547 encoding
= CFStringGetSystemEncoding();
548 cfstr
= CFStringCreateWithPascalString(kCFAllocatorDefault
,
549 mdbPtr
->drVN
, encoding
);
550 cfOK
= CFStringGetCString(cfstr
, volnameUTF8
, NAME_MAX
,
551 kCFStringEncodingUTF8
);
554 if (!cfOK
&& encoding
!= kCFStringEncodingMacRoman
) {
556 /* default to MacRoman on conversion errors */
557 cfstr
= CFStringCreateWithPascalString(kCFAllocatorDefault
,
558 mdbPtr
->drVN
, kCFStringEncodingMacRoman
);
559 CFStringGetCString(cfstr
, volnameUTF8
, NAME_MAX
,
560 kCFStringEncodingUTF8
);
564 /* get HFS Plus volume name (from Catalog) */
565 } else if ((volHdrPtr
->signature
== kHFSPlusSigWord
) ||
566 (mdbPtr
->drSigWord
== kHFSSigWord
&&
567 mdbPtr
->drEmbedSigWord
== kHFSPlusSigWord
)) {
570 if (volHdrPtr
->signature
== kHFSPlusSigWord
) {
572 } else {/* embedded volume, first find offset */
573 result
= GetEmbeddedHFSPlusVol(mdbPtr
, &startOffset
);
574 if ( result
!= FSUR_IO_SUCCESS
)
578 result
= GetNameFromHFSPlusVolumeStartingAt(fd
, startOffset
,
581 result
= FSUR_UNRECOGNIZED
;
584 if (FSUR_IO_SUCCESS
== result
) {
587 CFMutableStringRef volumeName
;
588 CFIndex volumeNameLength
;
589 CFRange foundSubString
;
591 slash
= CFStringCreateWithCString(kCFAllocatorDefault
, "/", kCFStringEncodingUTF8
);
593 result
= FSUR_IO_FAIL
;
597 colon
= CFStringCreateWithCString(kCFAllocatorDefault
, ":", kCFStringEncodingUTF8
);
599 result
= FSUR_IO_FAIL
;
603 volumeName
= CFStringCreateMutableCopy(
606 CFStringCreateWithCString(kCFAllocatorDefault
, volnameUTF8
, kCFStringEncodingUTF8
));
607 if (volumeName
== NULL
) {
608 result
= FSUR_IO_FAIL
;
611 volumeNameLength
= CFStringGetLength(volumeName
);
613 while (CFStringFindWithOptions(volumeName
, slash
, CFRangeMake(0, volumeNameLength
-1), 0, &foundSubString
)) {
614 CFStringReplace(volumeName
, foundSubString
, colon
);
617 CFStringGetCString(volumeName
, volnameUTF8
, NAME_MAX
, kCFStringEncodingUTF8
);
618 write(1, volnameUTF8
, strlen(volnameUTF8
));
620 /* backwards compatibility */
621 DoFileSystemFile( FS_NAME_SUFFIX
, gHFS_FS_NAME
);
622 DoFileSystemFile( FS_LABEL_SUFFIX
, volnameUTF8
);
623 result
= FSUR_MOUNT_HIDDEN
;
640 /* **************************************** DoGetUUIDKey *******************************************
642 This routine will open the given block device and return the volume UUID in text form written to stdout.
644 theDeviceNamePtr - pointer to the device name (full path, like /dev/disk0s2).
646 returns FSUR_IO_SUCCESS or else one of the FSUR_xyz error codes.
647 *************************************************************************************************** */
649 DoGetUUIDKey( const char * theDeviceNamePtr
) {
651 VolumeUUID targetVolumeUUID
;
652 VolumeUUIDString UUIDString
;
653 char uuidLine
[VOLUMEUUIDLENGTH
+2];
655 if ((result
= GetVolumeUUID(theDeviceNamePtr
, &targetVolumeUUID
, FALSE
)) != FSUR_IO_SUCCESS
) goto Err_Exit
;
657 ConvertVolumeUUIDToString( &targetVolumeUUID
, UUIDString
);
658 strncpy(uuidLine
, UUIDString
, VOLUMEUUIDLENGTH
+1);
659 write(1, uuidLine
, strlen(uuidLine
));
660 result
= FSUR_IO_SUCCESS
;
668 /* *************************************** DoChangeUUIDKey ******************************************
670 This routine will change the UUID on the specified block device.
672 theDeviceNamePtr - pointer to the device name (full path, like /dev/disk0s2).
674 returns FSUR_IO_SUCCESS or else one of the FSUR_xyz error codes.
675 *************************************************************************************************** */
677 DoChangeUUIDKey( const char * theDeviceNamePtr
) {
679 VolumeUUID newVolumeUUID
;
681 GenerateVolumeUUID(&newVolumeUUID
);
682 result
= SetVolumeUUID(theDeviceNamePtr
, &newVolumeUUID
);
689 /* **************************************** DoAdopt *******************************************
691 This routine will add the UUID of the specified block device to the list of local volumes.
693 theDeviceNamePtr - pointer to the device name (full path, like /dev/disk0s2).
695 returns FSUR_IO_SUCCESS or else one of the FSUR_xyz error codes.
696 *************************************************************************************************** */
698 DoAdopt( const char * theDeviceNamePtr
) {
699 int result
, closeresult
;
700 VolumeUUID targetVolumeUUID
;
701 VolumeStatusDBHandle vsdbhandle
= NULL
;
702 unsigned long targetVolumeStatus
;
704 if ((result
= GetVolumeUUID(theDeviceNamePtr
, &targetVolumeUUID
, TRUE
)) != FSUR_IO_SUCCESS
) goto Err_Return
;
706 if ((result
= OpenVolumeStatusDB(&vsdbhandle
)) != 0) goto Err_Exit
;
707 if ((result
= GetVolumeStatusDBEntry(vsdbhandle
, &targetVolumeUUID
, &targetVolumeStatus
)) != 0) {
708 targetVolumeStatus
= 0;
710 targetVolumeStatus
= (targetVolumeStatus
& VOLUME_VALIDSTATUSBITS
) | VOLUME_USEPERMISSIONS
;
711 if ((result
= SetVolumeStatusDBEntry(vsdbhandle
, &targetVolumeUUID
, targetVolumeStatus
)) != 0) goto Err_Exit
;
713 result
= FSUR_IO_SUCCESS
;
717 closeresult
= CloseVolumeStatusDB(vsdbhandle
);
719 if (result
== FSUR_IO_SUCCESS
) result
= closeresult
;
722 if ((result
!= 0) && (result
!= FSUR_IO_SUCCESS
)) result
= FSUR_IO_FAIL
;
726 if (result
!= FSUR_IO_SUCCESS
) fprintf(stderr
, "DoAdopt: returning %d...\n", result
);
733 /* **************************************** DoDisown *******************************************
735 This routine will change the status of the specified block device to ignore its permissions.
737 theDeviceNamePtr - pointer to the device name (full path, like /dev/disk0s2).
739 returns FSUR_IO_SUCCESS or else one of the FSUR_xyz error codes.
740 *************************************************************************************************** */
742 DoDisown( const char * theDeviceNamePtr
) {
743 int result
, closeresult
;
744 VolumeUUID targetVolumeUUID
;
745 VolumeStatusDBHandle vsdbhandle
= NULL
;
746 unsigned long targetVolumeStatus
;
748 if ((result
= GetVolumeUUID(theDeviceNamePtr
, &targetVolumeUUID
, TRUE
)) != FSUR_IO_SUCCESS
) goto Err_Return
;
750 if ((result
= OpenVolumeStatusDB(&vsdbhandle
)) != 0) goto Err_Exit
;
751 if ((result
= GetVolumeStatusDBEntry(vsdbhandle
, &targetVolumeUUID
, &targetVolumeStatus
)) != 0) {
752 targetVolumeStatus
= 0;
754 targetVolumeStatus
= (targetVolumeStatus
& VOLUME_VALIDSTATUSBITS
) & ~VOLUME_USEPERMISSIONS
;
755 if ((result
= SetVolumeStatusDBEntry(vsdbhandle
, &targetVolumeUUID
, targetVolumeStatus
)) != 0) goto Err_Exit
;
757 result
= FSUR_IO_SUCCESS
;
761 closeresult
= CloseVolumeStatusDB(vsdbhandle
);
763 if (result
== FSUR_IO_SUCCESS
) result
= closeresult
;
766 if ((result
!= 0) && (result
!= FSUR_IO_SUCCESS
)) {
768 if (result
!= 0) fprintf(stderr
, "DoDisown: result = %d; changing to %d...\n", result
, FSUR_IO_FAIL
);
770 result
= FSUR_IO_FAIL
;
775 if (result
!= FSUR_IO_SUCCESS
) fprintf(stderr
, "DoDisown: returning %d...\n", result
);
782 get_multiplier(char c
)
784 if (tolower(c
) == 'k') {
786 } else if (tolower(c
) == 'm') {
788 } else if (tolower(c
) == 'g') {
789 return 1024 * 1024 * 1024;
795 /* **************************************** ParseArgs ********************************************
797 This routine will make sure the arguments passed in to us are cool.
798 Here is how this utility is used:
800 usage: hfs.util actionArg deviceArg [mountPointArg] [flagsArg]
802 -p (Probe for mounting)
803 -P (Probe for initializing - not supported)
805 -r (Repair - not supported)
808 -i (Initialize - not supported)
811 disk0s2 (for example)
814 /foo/bar/ (required for Mount and Force Mount actions)
817 (these are ignored for CDROMs)
818 either "readonly" OR "writable"
819 either "removable" OR "fixed"
820 either "nosuid" or "suid"
823 hfs.util -p disk0s2 removable writable
824 hfs.util -p disk0s2 removable readonly
825 hfs.util -m disk0s2 /my/hfs
828 argc - the number of arguments in argv.
829 argv - array of arguments.
831 returns FSUR_INVAL if we find a bad argument else 0.
832 *************************************************************************************************** */
834 ParseArgs(int argc
, const char *argv
[], const char ** actionPtr
,
835 const char ** mountPointPtr
, boolean_t
* isEjectablePtr
,
836 boolean_t
* isLockedPtr
, boolean_t
* isSetuidPtr
, boolean_t
* isDevPtr
)
838 int result
= FSUR_INVAL
;
839 int deviceLength
, doLengthCheck
= 1;
843 /* Must have at least 3 arguments and the action argument must start with a '-' */
844 if ( (argc
< 3) || (argv
[1][0] != '-') ) {
845 DoDisplayUsage( argv
);
849 /* we only support actions Probe, Mount, Force Mount, and Unmount */
851 * actionPtr
= & argv
[1][1];
853 switch ( argv
[1][1] ) {
855 /* action Probe and requires 5 arguments (need the flags) */
857 DoDisplayUsage( argv
);
865 /* Note: the device argument in argv[2] is checked further down but ignored. */
866 * mountPointPtr
= argv
[3];
867 index
= 0; /* No isEjectable/isLocked flags for unmount. */
871 case FSUC_MOUNT_FORCE
:
872 /* action Mount and ForceMount require 7 arguments (need the mountpoint and the flags) */
874 DoDisplayUsage( argv
);
877 * mountPointPtr
= argv
[3];
903 if (isdigit(argv
[2][0])) {
905 gJournalSize
= strtoul(argv
[2], &ptr
, 0);
907 gJournalSize
*= get_multiplier(*ptr
);
920 DoDisplayUsage( argv
);
925 /* Make sure device (argv[2]) is something reasonable */
926 deviceLength
= strlen( argv
[2] );
927 if ( doLengthCheck
&& (deviceLength
< 3 || deviceLength
> NAME_MAX
) ) {
928 DoDisplayUsage( argv
);
933 /* Flags: removable/fixed. */
934 if ( 0 == strcmp(argv
[index
],"removable") ) {
935 * isEjectablePtr
= 1;
936 } else if ( 0 == strcmp(argv
[index
],"fixed") ) {
937 * isEjectablePtr
= 0;
939 printf("hfs.util: ERROR: unrecognized flag (removable/fixed) argv[%d]='%s'\n",index
,argv
[index
]);
942 /* Flags: readonly/writable. */
943 if ( 0 == strcmp(argv
[index
+1],"readonly") ) {
945 } else if ( 0 == strcmp(argv
[index
+1],"writable") ) {
948 printf("hfs.util: ERROR: unrecognized flag (readonly/writable) argv[%d]='%s'\n",index
,argv
[index
+1]);
952 /* Flags: suid/nosuid. */
953 if ( 0 == strcmp(argv
[index
+2],"suid") ) {
955 } else if ( 0 == strcmp(argv
[index
+2],"nosuid") ) {
958 printf("hfs.util: ERROR: unrecognized flag (suid/nosuid) argv[%d]='%s'\n",index
,argv
[index
+2]);
961 /* Flags: dev/nodev. */
962 if ( 0 == strcmp(argv
[index
+3],"dev") ) {
964 } else if ( 0 == strcmp(argv
[index
+3],"nodev") ) {
967 printf("hfs.util: ERROR: unrecognized flag (dev/nodev) argv[%d]='%s'\n",index
,argv
[index
+3]);
982 /* *************************************** DoDisplayUsage ********************************************
984 This routine will do a printf of the correct usage for this utility.
986 argv - array of arguments.
989 *************************************************************************************************** */
991 DoDisplayUsage(const char *argv
[])
993 printf("usage: %s action_arg device_arg [mount_point_arg] [Flags] \n", argv
[0]);
994 printf("action_arg:\n");
995 printf(" -%c (Probe for mounting)\n", FSUC_PROBE
);
996 printf(" -%c (Mount)\n", FSUC_MOUNT
);
997 printf(" -%c (Unmount)\n", FSUC_UNMOUNT
);
998 printf(" -%c (Force Mount)\n", FSUC_MOUNT_FORCE
);
999 #ifdef HFS_UUID_SUPPORT
1000 printf(" -%c (Get UUID Key)\n", FSUC_GETUUID
);
1001 printf(" -%c (Set UUID Key)\n", FSUC_SETUUID
);
1002 #endif HFS_UUID_SUPPORT
1003 printf(" -%c (Adopt permissions)\n", FSUC_ADOPT
);
1004 printf(" -%c (Make a volume journaled)\n", FSUC_MKJNL
);
1005 printf(" -%c (Turn off journaling on a volume)\n", FSUC_UNJNL
);
1006 printf("device_arg:\n");
1007 printf(" device we are acting upon (for example, 'disk0s2')\n");
1008 printf(" if '-%c' or '-%c' is specified, this should be the\n", FSUC_MKJNL
, FSUC_UNJNL
);
1009 printf(" name of the volume we to act on (for example, '/Volumes/foo' or '/')\n");
1010 printf("mount_point_arg:\n");
1011 printf(" required for Mount and Force Mount \n");
1013 printf(" required for Mount, Force Mount and Probe\n");
1014 printf(" indicates removable or fixed (for example 'fixed')\n");
1015 printf(" indicates readonly or writable (for example 'readonly')\n");
1016 printf("Examples:\n");
1017 printf(" %s -p disk0s2 fixed writable\n", argv
[0]);
1018 printf(" %s -m disk0s2 /my/hfs removable readonly\n", argv
[0]);
1022 } /* DoDisplayUsage */
1025 /* ************************************** DoFileSystemFile *******************************************
1027 This routine will create a file system info file that is used by WorkSpace. After creating the
1028 file it will write whatever theContentsPtr points to the new file.
1029 We end up with a file something like:
1030 /System/Library/Filesystems/hfs.fs/hfs.name
1031 when our file system name is "hfs" and theFileNameSuffixPtr points to ".name"
1033 theFileNameSuffixPtr - pointer to a suffix we add to the file name we're creating.
1034 theContentsPtr - pointer to C string to write into the file.
1037 *************************************************************************************************** */
1039 DoFileSystemFile(char *fileNameSuffixPtr
, char *contentsPtr
)
1042 char fileName
[MAXPATHLEN
];
1044 sprintf(fileName
, "%s/%s%s/%s", FS_DIR_LOCATION
, gHFS_FS_NAME
,
1045 FS_DIR_SUFFIX
, gHFS_FS_NAME
);
1046 strcat(fileName
, fileNameSuffixPtr
);
1047 unlink(fileName
); /* delete existing string */
1049 if ( strlen( fileNameSuffixPtr
) ) {
1050 int oldMask
= umask(0);
1052 fd
= open( & fileName
[0], O_CREAT
| O_TRUNC
| O_WRONLY
, 0644 );
1055 write( fd
, contentsPtr
, strlen( contentsPtr
) );
1062 } /* DoFileSystemFile */
1067 -- Returns: FSUR_IO_SUCCESS, FSUR_IO_FAIL
1071 GetVolumeUUID(const char *deviceNamePtr
, VolumeUUID
*volumeUUIDPtr
, boolean_t generate
) {
1074 HFSMasterDirectoryBlock
* mdbPtr
;
1075 HFSPlusVolumeHeader
* volHdrPtr
;
1076 VolumeUUID
*finderInfoUUIDPtr
;
1079 bufPtr
= (char *)malloc(HFS_BLOCK_SIZE
);
1081 result
= FSUR_UNRECOGNIZED
;
1085 mdbPtr
= (HFSMasterDirectoryBlock
*) bufPtr
;
1086 volHdrPtr
= (HFSPlusVolumeHeader
*) bufPtr
;
1088 fd
= open( deviceNamePtr
, O_RDWR
, 0 );
1090 fd
= open( deviceNamePtr
, O_RDONLY
, 0);
1093 fprintf(stderr
, "hfs.util: GetVolumeUUID: device open failed (errno = %d).\n", errno
);
1095 result
= FSUR_IO_FAIL
;
1101 * Read the HFS Master Directory Block from sector 2
1103 result
= readAt(fd
, volHdrPtr
, (off_t
)(2 * HFS_BLOCK_SIZE
), HFS_BLOCK_SIZE
);
1104 if (result
!= FSUR_IO_SUCCESS
) {
1108 if (mdbPtr
->drSigWord
== kHFSSigWord
&&
1109 mdbPtr
->drEmbedSigWord
!= kHFSPlusSigWord
) {
1110 finderInfoUUIDPtr
= (VolumeUUID
*)(&mdbPtr
->drFndrInfo
[6]);
1111 if (generate
&& ((finderInfoUUIDPtr
->v
.high
== 0) || (finderInfoUUIDPtr
->v
.low
== 0))) {
1112 GenerateVolumeUUID(volumeUUIDPtr
);
1113 bcopy(volumeUUIDPtr
, finderInfoUUIDPtr
, sizeof(*finderInfoUUIDPtr
));
1114 result
= writeAt(fd
, volHdrPtr
, (off_t
)(2 * HFS_BLOCK_SIZE
), HFS_BLOCK_SIZE
);
1115 if (result
!= FSUR_IO_SUCCESS
) goto Err_Exit
;
1117 bcopy(finderInfoUUIDPtr
, volumeUUIDPtr
, sizeof(*volumeUUIDPtr
));
1118 result
= FSUR_IO_SUCCESS
;
1119 } else if ((volHdrPtr
->signature
== kHFSPlusSigWord
) ||
1120 ((mdbPtr
->drSigWord
== kHFSSigWord
) &&
1121 (mdbPtr
->drEmbedSigWord
== kHFSPlusSigWord
))) {
1124 if (volHdrPtr
->signature
== kHFSPlusSigWord
) {
1126 } else {/* embedded volume, first find offset */
1127 result
= GetEmbeddedHFSPlusVol(mdbPtr
, &startOffset
);
1128 if ( result
!= FSUR_IO_SUCCESS
) {
1133 result
= readAt( fd
, volHdrPtr
, startOffset
+ (off_t
)(2*HFS_BLOCK_SIZE
), HFS_BLOCK_SIZE
);
1134 if (result
!= FSUR_IO_SUCCESS
) {
1135 goto Err_Exit
; // return FSUR_IO_FAIL
1138 /* Verify that it is an HFS+ volume. */
1140 if (volHdrPtr
->signature
!= kHFSPlusSigWord
) {
1141 result
= FSUR_IO_FAIL
;
1145 finderInfoUUIDPtr
= (VolumeUUID
*)&volHdrPtr
->finderInfo
[24];
1146 if (generate
&& ((finderInfoUUIDPtr
->v
.high
== 0) || (finderInfoUUIDPtr
->v
.low
== 0))) {
1147 GenerateVolumeUUID(volumeUUIDPtr
);
1148 bcopy(volumeUUIDPtr
, finderInfoUUIDPtr
, sizeof(*finderInfoUUIDPtr
));
1149 result
= writeAt( fd
, volHdrPtr
, startOffset
+ (off_t
)(2*HFS_BLOCK_SIZE
), HFS_BLOCK_SIZE
);
1150 if (result
!= FSUR_IO_SUCCESS
) {
1154 bcopy(finderInfoUUIDPtr
, volumeUUIDPtr
, sizeof(*volumeUUIDPtr
));
1155 result
= FSUR_IO_SUCCESS
;
1157 result
= FSUR_UNRECOGNIZED
;
1161 if (fd
> 0) close(fd
);
1162 if (bufPtr
) free(bufPtr
);
1165 if (result
!= FSUR_IO_SUCCESS
) fprintf(stderr
, "hfs.util: GetVolumeUUID: result = %d...\n", result
);
1167 return (result
== FSUR_IO_SUCCESS
) ? FSUR_IO_SUCCESS
: FSUR_IO_FAIL
;
1175 -- Returns: FSUR_IO_SUCCESS, FSUR_IO_FAIL
1179 SetVolumeUUID(const char *deviceNamePtr
, VolumeUUID
*volumeUUIDPtr
) {
1182 HFSMasterDirectoryBlock
* mdbPtr
;
1183 HFSPlusVolumeHeader
* volHdrPtr
;
1184 VolumeUUID
*finderInfoUUIDPtr
;
1187 bufPtr
= (char *)malloc(HFS_BLOCK_SIZE
);
1189 result
= FSUR_UNRECOGNIZED
;
1193 mdbPtr
= (HFSMasterDirectoryBlock
*) bufPtr
;
1194 volHdrPtr
= (HFSPlusVolumeHeader
*) bufPtr
;
1196 fd
= open( deviceNamePtr
, O_RDWR
, 0 );
1199 fprintf(stderr
, "hfs.util: SetVolumeUUID: device open failed (errno = %d).\n", errno
);
1201 result
= FSUR_IO_FAIL
;
1206 * Read the HFS Master Directory Block from sector 2
1208 result
= readAt(fd
, volHdrPtr
, (off_t
)(2 * HFS_BLOCK_SIZE
), HFS_BLOCK_SIZE
);
1209 if (result
!= FSUR_IO_SUCCESS
) {
1213 if (mdbPtr
->drSigWord
== kHFSSigWord
&&
1214 mdbPtr
->drEmbedSigWord
!= kHFSPlusSigWord
) {
1215 finderInfoUUIDPtr
= (VolumeUUID
*)(&mdbPtr
->drFndrInfo
[6]);
1216 bcopy(volumeUUIDPtr
, finderInfoUUIDPtr
, sizeof(*finderInfoUUIDPtr
));
1217 result
= writeAt(fd
, volHdrPtr
, (off_t
)(2 * HFS_BLOCK_SIZE
), HFS_BLOCK_SIZE
);
1218 } else if ((volHdrPtr
->signature
== kHFSPlusSigWord
) ||
1219 ((mdbPtr
->drSigWord
== kHFSSigWord
) &&
1220 (mdbPtr
->drEmbedSigWord
== kHFSPlusSigWord
))) {
1223 if (volHdrPtr
->signature
== kHFSPlusSigWord
) {
1225 } else {/* embedded volume, first find offset */
1226 result
= GetEmbeddedHFSPlusVol(mdbPtr
, &startOffset
);
1227 if ( result
!= FSUR_IO_SUCCESS
) {
1232 result
= readAt( fd
, volHdrPtr
, startOffset
+ (off_t
)(2*HFS_BLOCK_SIZE
), HFS_BLOCK_SIZE
);
1233 if (result
!= FSUR_IO_SUCCESS
) {
1234 goto Err_Exit
; // return FSUR_IO_FAIL
1237 /* Verify that it is an HFS+ volume. */
1239 if (volHdrPtr
->signature
!= kHFSPlusSigWord
) {
1240 result
= FSUR_IO_FAIL
;
1244 finderInfoUUIDPtr
= (VolumeUUID
*)&volHdrPtr
->finderInfo
[24];
1245 bcopy(volumeUUIDPtr
, finderInfoUUIDPtr
, sizeof(*finderInfoUUIDPtr
));
1246 result
= writeAt( fd
, volHdrPtr
, startOffset
+ (off_t
)(2*HFS_BLOCK_SIZE
), HFS_BLOCK_SIZE
);
1247 if (result
!= FSUR_IO_SUCCESS
) {
1250 result
= FSUR_IO_SUCCESS
;
1252 result
= FSUR_UNRECOGNIZED
;
1256 if (fd
> 0) close(fd
);
1257 if (bufPtr
) free(bufPtr
);
1260 if (result
!= FSUR_IO_SUCCESS
) fprintf(stderr
, "hfs.util: SetVolumeUUID: result = %d...\n", result
);
1262 return (result
== FSUR_IO_SUCCESS
) ? FSUR_IO_SUCCESS
: FSUR_IO_FAIL
;
1268 -- GetEmbeddedHFSPlusVol
1270 -- In: hfsMasterDirectoryBlockPtr
1271 -- Out: startOffsetPtr - the disk offset at which the HFS+ volume starts
1272 (that is, 2 blocks before the volume header)
1277 GetEmbeddedHFSPlusVol (HFSMasterDirectoryBlock
* hfsMasterDirectoryBlockPtr
, off_t
* startOffsetPtr
)
1279 int result
= FSUR_IO_SUCCESS
;
1280 u_int32_t allocationBlockSize
, firstAllocationBlock
, startBlock
, blockCount
;
1282 if (hfsMasterDirectoryBlockPtr
->drSigWord
!= kHFSSigWord
) {
1283 result
= FSUR_UNRECOGNIZED
;
1287 allocationBlockSize
= hfsMasterDirectoryBlockPtr
->drAlBlkSiz
;
1288 firstAllocationBlock
= hfsMasterDirectoryBlockPtr
->drAlBlSt
;
1290 if (hfsMasterDirectoryBlockPtr
->drEmbedSigWord
!= kHFSPlusSigWord
) {
1291 result
= FSUR_UNRECOGNIZED
;
1295 startBlock
= hfsMasterDirectoryBlockPtr
->drEmbedExtent
.startBlock
;
1296 blockCount
= hfsMasterDirectoryBlockPtr
->drEmbedExtent
.blockCount
;
1298 if ( startOffsetPtr
)
1299 *startOffsetPtr
= ((u_int64_t
)startBlock
* (u_int64_t
)allocationBlockSize
) +
1300 ((u_int64_t
)firstAllocationBlock
* (u_int64_t
)HFS_BLOCK_SIZE
);
1310 -- GetNameFromHFSPlusVolumeStartingAt
1312 -- Caller's responsibility to allocate and release memory for the converted string.
1314 -- Returns: FSUR_IO_SUCCESS, FSUR_IO_FAIL
1318 GetNameFromHFSPlusVolumeStartingAt(int fd
, off_t hfsPlusVolumeOffset
, char * name_o
)
1320 int result
= FSUR_IO_SUCCESS
;
1321 u_int32_t blockSize
;
1322 char * bufPtr
= NULL
;
1323 HFSPlusVolumeHeader
* volHdrPtr
;
1324 BTNodeDescriptor
* bTreeNodeDescriptorPtr
;
1325 u_int32_t catalogNodeSize
;
1327 u_int32_t catalogExtCount
;
1328 HFSPlusExtentDescriptor
*catalogExtents
= NULL
;
1330 volHdrPtr
= (HFSPlusVolumeHeader
*)malloc(HFS_BLOCK_SIZE
);
1331 if ( ! volHdrPtr
) {
1332 result
= FSUR_IO_FAIL
;
1337 * Read the Volume Header
1338 * (This is a little redundant for a pure, unwrapped HFS+ volume)
1340 result
= readAt( fd
, volHdrPtr
, hfsPlusVolumeOffset
+ (off_t
)(2*HFS_BLOCK_SIZE
), HFS_BLOCK_SIZE
);
1341 if (result
== FSUR_IO_FAIL
) {
1343 fprintf(stderr
, "hfs.util: GetNameFromHFSPlusVolumeStartingAt: readAt failed\n");
1345 goto Return
; // return FSUR_IO_FAIL
1348 /* Verify that it is an HFS+ volume. */
1350 if (volHdrPtr
->signature
!= kHFSPlusSigWord
) {
1351 result
= FSUR_IO_FAIL
;
1353 fprintf(stderr
, "hfs.util: GetNameFromHFSPlusVolumeStartingAt: volHdrPtr->signature != kHFSPlusSigWord\n");
1358 blockSize
= volHdrPtr
->blockSize
;
1359 catalogExtents
= (HFSPlusExtentDescriptor
*) malloc(sizeof(HFSPlusExtentRecord
));
1360 if ( ! catalogExtents
) {
1361 result
= FSUR_IO_FAIL
;
1364 bcopy(volHdrPtr
->catalogFile
.extents
, catalogExtents
, sizeof(HFSPlusExtentRecord
));
1365 catalogExtCount
= kHFSPlusExtentDensity
;
1367 /* if there are overflow catalog extents, then go get them */
1368 if (catalogExtents
[7].blockCount
!= 0) {
1369 result
= GetCatalogOverflowExtents(fd
, hfsPlusVolumeOffset
, volHdrPtr
, &catalogExtents
, &catalogExtCount
);
1370 if (result
!= FSUR_IO_SUCCESS
)
1374 /* Read the header node of the catalog B-Tree */
1376 result
= GetBTreeNodeInfo(fd
, hfsPlusVolumeOffset
, blockSize
,
1377 catalogExtCount
, catalogExtents
,
1378 &catalogNodeSize
, &leafNode
);
1379 if (result
!= FSUR_IO_SUCCESS
)
1382 /* Read the first leaf node of the catalog b-tree */
1384 bufPtr
= (char *)malloc(catalogNodeSize
);
1386 result
= FSUR_IO_FAIL
;
1390 bTreeNodeDescriptorPtr
= (BTNodeDescriptor
*)bufPtr
;
1392 result
= ReadFile(fd
, bufPtr
, (off_t
) leafNode
* (off_t
) catalogNodeSize
, catalogNodeSize
,
1393 hfsPlusVolumeOffset
, blockSize
,
1394 catalogExtCount
, catalogExtents
);
1395 if (result
== FSUR_IO_FAIL
) {
1397 fprintf(stderr
, "hfs.util: ERROR: reading first leaf failed\n");
1399 goto Return
; // return FSUR_IO_FAIL
1405 HFSPlusCatalogKey
* k
;
1408 if ( bTreeNodeDescriptorPtr
->numRecords
< 1) {
1409 result
= FSUR_IO_FAIL
;
1411 fprintf(stderr
, "hfs.util: ERROR: bTreeNodeDescriptorPtr->numRecords < 1\n");
1416 // Get the offset (in bytes) of the first record from the list of offsets at the end of the node.
1418 p
= bufPtr
+ catalogNodeSize
- sizeof(u_int16_t
); // pointer arithmetic in bytes
1421 // Get a pointer to the first record.
1423 p
= bufPtr
+ *v
; // pointer arithmetic in bytes
1424 k
= (HFSPlusCatalogKey
*)p
;
1426 // There should be only one record whose parent is the root parent. It should be the first record.
1428 if (k
->parentID
!= kHFSRootParentID
) {
1429 result
= FSUR_IO_FAIL
;
1431 fprintf(stderr
, "hfs.util: ERROR: k->parentID != kHFSRootParentID\n");
1436 /* Extract the name of the root directory */
1438 cfstr
= CFStringCreateWithCharacters(kCFAllocatorDefault
, k
->nodeName
.unicode
, k
->nodeName
.length
);
1439 (void) CFStringGetCString(cfstr
, name_o
, NAME_MAX
, kCFStringEncodingUTF8
);
1443 result
= FSUR_IO_SUCCESS
;
1447 free((char*) volHdrPtr
);
1450 free((char*) catalogExtents
);
1453 free((char*)bufPtr
);
1457 } /* GetNameFromHFSPlusVolumeStartingAt */
1460 #pragma options align=mac68k
1462 BTNodeDescriptor node
;
1464 } HeaderRec
, *HeaderPtr
;
1465 #pragma options align=reset
1470 -- Returns: FSUR_IO_SUCCESS, FSUR_IO_FAIL
1474 GetBTreeNodeInfo(int fd
, off_t hfsPlusVolumeOffset
, u_int32_t blockSize
,
1475 u_int32_t extentCount
, const HFSPlusExtentDescriptor
*extentList
,
1476 u_int32_t
*nodeSize
, u_int32_t
*firstLeafNode
)
1479 HeaderRec
* bTreeHeaderPtr
= NULL
;
1481 bTreeHeaderPtr
= (HeaderRec
*) malloc(HFS_BLOCK_SIZE
);
1482 if (bTreeHeaderPtr
== NULL
)
1483 return (FSUR_IO_FAIL
);
1485 /* Read the b-tree header node */
1487 result
= ReadFile(fd
, bTreeHeaderPtr
, 0, HFS_BLOCK_SIZE
,
1488 hfsPlusVolumeOffset
, blockSize
,
1489 extentCount
, extentList
);
1490 if ( result
== FSUR_IO_FAIL
) {
1492 fprintf(stderr
, "hfs.util: ERROR: reading header node failed\n");
1497 if ( bTreeHeaderPtr
->node
.kind
!= kBTHeaderNode
) {
1498 result
= FSUR_IO_FAIL
;
1500 fprintf(stderr
, "hfs.util: ERROR: bTreeHeaderPtr->node.kind != kBTHeaderNode\n");
1505 *nodeSize
= bTreeHeaderPtr
->header
.nodeSize
;
1507 if (bTreeHeaderPtr
->header
.leafRecords
== 0)
1510 *firstLeafNode
= bTreeHeaderPtr
->header
.firstLeafNode
;
1513 free((char*) bTreeHeaderPtr
);
1517 } /* GetBTreeNodeInfo */
1523 -- Returns: FSUR_IO_SUCCESS, FSUR_IO_FAIL
1527 GetCatalogOverflowExtents(int fd
, off_t hfsPlusVolumeOffset
,
1528 HFSPlusVolumeHeader
*volHdrPtr
,
1529 HFSPlusExtentDescriptor
**catalogExtents
,
1530 u_int32_t
*catalogExtCount
)
1535 BTNodeDescriptor
* bTreeNodeDescriptorPtr
;
1536 HFSPlusExtentDescriptor
* extents
;
1538 char * bufPtr
= NULL
;
1542 listsize
= *catalogExtCount
* sizeof(HFSPlusExtentDescriptor
);
1543 extents
= *catalogExtents
;
1544 offset
= (off_t
)volHdrPtr
->extentsFile
.extents
[0].startBlock
*
1545 (off_t
)volHdrPtr
->blockSize
;
1547 /* Read the header node of the extents B-Tree */
1549 result
= GetBTreeNodeInfo(fd
, hfsPlusVolumeOffset
, volHdrPtr
->blockSize
,
1550 kHFSPlusExtentDensity
, volHdrPtr
->extentsFile
.extents
,
1551 &nodeSize
, &leafNode
);
1552 if (result
!= FSUR_IO_SUCCESS
|| leafNode
== 0)
1555 /* Calculate the logical position of the first leaf node */
1557 offset
= (off_t
) leafNode
* (off_t
) nodeSize
;
1559 /* Read the first leaf node of the extents b-tree */
1561 bufPtr
= (char *)malloc(nodeSize
);
1563 result
= FSUR_IO_FAIL
;
1567 bTreeNodeDescriptorPtr
= (BTNodeDescriptor
*)bufPtr
;
1570 result
= ReadFile(fd
, bufPtr
, offset
, nodeSize
,
1571 hfsPlusVolumeOffset
, volHdrPtr
->blockSize
,
1572 kHFSPlusExtentDensity
, volHdrPtr
->extentsFile
.extents
);
1573 if ( result
== FSUR_IO_FAIL
) {
1575 fprintf(stderr
, "hfs.util: ERROR: reading first leaf failed\n");
1580 if (bTreeNodeDescriptorPtr
->kind
!= kBTLeafNode
) {
1581 result
= FSUR_IO_FAIL
;
1585 for (i
= 1; i
<= bTreeNodeDescriptorPtr
->numRecords
; ++i
) {
1588 HFSPlusExtentKey
* k
;
1591 * Get the offset (in bytes) of the record from the
1592 * list of offsets at the end of the node
1594 p
= bufPtr
+ nodeSize
- (sizeof(u_int16_t
) * i
);
1597 /* Get a pointer to the record */
1599 p
= bufPtr
+ *v
; /* pointer arithmetic in bytes */
1600 k
= (HFSPlusExtentKey
*)p
;
1602 if (k
->fileID
!= kHFSCatalogFileID
)
1605 /* grow list and copy additional extents */
1606 listsize
+= sizeof(HFSPlusExtentRecord
);
1607 extents
= (HFSPlusExtentDescriptor
*) realloc(extents
, listsize
);
1608 bcopy(p
+ k
->keyLength
+ sizeof(u_int16_t
),
1609 &extents
[*catalogExtCount
], sizeof(HFSPlusExtentRecord
));
1611 *catalogExtCount
+= kHFSPlusExtentDensity
;
1612 *catalogExtents
= extents
;
1615 if ((leafNode
= bTreeNodeDescriptorPtr
->fLink
) != 0) {
1617 offset
= (off_t
) leafNode
* (off_t
) nodeSize
;
1632 * LogicalToPhysical - Map a logical file position and size to volume-relative physical
1633 * position and number of contiguous bytes at that position.
1636 * logicalOffset Logical offset in bytes from start of file
1637 * length Maximum number of bytes to map
1638 * blockSize Number of bytes per allocation block
1639 * extentCount Number of extents in file
1640 * extentList The file's extents
1643 * physicalOffset Physical offset in bytes from start of volume
1644 * availableBytes Number of bytes physically contiguous (up to length)
1646 * Returns: FSUR_IO_SUCCESS, FSUR_IO_FAIL
1648 static int LogicalToPhysical(off_t offset
, ssize_t length
, u_int32_t blockSize
,
1649 u_int32_t extentCount
, const HFSPlusExtentDescriptor
*extentList
,
1650 off_t
*physicalOffset
, ssize_t
*availableBytes
)
1653 u_int32_t logicalBlock
;
1655 u_int32_t blockCount
= 0;
1657 /* Determine allocation block containing logicalOffset */
1658 logicalBlock
= offset
/ blockSize
; /* This can't overflow for valid volumes */
1659 offset
%= blockSize
; /* Offset from start of allocation block */
1661 /* Find the extent containing logicalBlock */
1662 for (extent
= 0; extent
< extentCount
; ++extent
)
1664 blockCount
= extentList
[extent
].blockCount
;
1666 if (blockCount
== 0)
1667 return FSUR_IO_FAIL
; /* Tried to map past physical end of file */
1669 if (logicalBlock
< blockCount
)
1670 break; /* Found it! */
1672 logicalBlock
-= blockCount
;
1675 if (extent
>= extentCount
)
1676 return FSUR_IO_FAIL
; /* Tried to map past physical end of file */
1679 * When we get here, extentList[extent] is the extent containing logicalOffset.
1680 * The desired allocation block is logicalBlock blocks into the extent.
1683 /* Compute the physical starting position */
1684 temp
= extentList
[extent
].startBlock
+ logicalBlock
; /* First physical block */
1685 temp
*= blockSize
; /* Byte offset of first physical block */
1686 *physicalOffset
= temp
+ offset
;
1688 /* Compute the available contiguous bytes. */
1689 temp
= blockCount
- logicalBlock
; /* Number of blocks available in extent */
1691 temp
-= offset
; /* Number of bytes available */
1694 *availableBytes
= temp
;
1696 *availableBytes
= length
;
1698 return FSUR_IO_SUCCESS
;
1704 * ReadFile - Read bytes from a file. Handles cases where the starting and/or
1705 * ending position are not allocation or device block aligned.
1708 * fd Descriptor for reading the volume
1709 * buffer The bytes are read into here
1710 * offset Offset in file to start reading
1711 * length Number of bytes to read
1712 * volOffset Byte offset from start of device to start of volume
1713 * blockSize Number of bytes per allocation block
1714 * extentCount Number of extents in file
1715 * extentList The file's exents
1717 * Returns: FSUR_IO_SUCCESS, FSUR_IO_FAIL
1719 static int ReadFile(int fd
, void *buffer
, off_t offset
, ssize_t length
,
1720 off_t volOffset
, u_int32_t blockSize
,
1721 u_int32_t extentCount
, const HFSPlusExtentDescriptor
*extentList
)
1723 int result
= FSUR_IO_SUCCESS
;
1729 result
= LogicalToPhysical(offset
, length
, blockSize
, extentCount
, extentList
,
1730 &physOffset
, &physLength
);
1731 if (result
!= FSUR_IO_SUCCESS
)
1734 result
= readAt(fd
, buffer
, volOffset
+physOffset
, physLength
);
1735 if (result
!= FSUR_IO_SUCCESS
)
1738 length
-= physLength
;
1739 offset
+= physLength
;
1740 buffer
= (char *) buffer
+ physLength
;
1747 -- readAt = lseek() + read()
1749 -- Returns: FSUR_IO_SUCCESS, FSUR_IO_FAIL
1754 readAt( int fd
, void * bufPtr
, off_t offset
, ssize_t length
)
1759 void * rawData
= NULL
;
1762 ssize_t dataOffset
= 0;
1763 int result
= FSUR_IO_SUCCESS
;
1765 if (ioctl(fd
, DKIOCBLKSIZE
, &blocksize
) < 0) {
1767 fprintf(stderr
, "hfs.util: readAt: couldn't determine block size of device.\n");
1769 result
= FSUR_IO_FAIL
;
1772 /* put offset and length in terms of device blocksize */
1773 rawOffset
= offset
/ blocksize
* blocksize
;
1774 dataOffset
= offset
- rawOffset
;
1775 rawLength
= ((length
+ dataOffset
+ blocksize
- 1) / blocksize
) * blocksize
;
1776 rawData
= malloc(rawLength
);
1777 if (rawData
== NULL
) {
1778 result
= FSUR_IO_FAIL
;
1782 lseekResult
= lseek( fd
, rawOffset
, SEEK_SET
);
1783 if ( lseekResult
!= rawOffset
) {
1784 result
= FSUR_IO_FAIL
;
1788 readResult
= read(fd
, rawData
, rawLength
);
1789 if ( readResult
!= rawLength
) {
1791 fprintf(stderr
, "hfs.util: readAt: attempt to read data from device failed (errno = %d)?\n", errno
);
1793 result
= FSUR_IO_FAIL
;
1796 bcopy(rawData
+ dataOffset
, bufPtr
, length
);
1807 -- writeAt = lseek() + write()
1809 -- Returns: FSUR_IO_SUCCESS, FSUR_IO_FAIL
1814 writeAt( int fd
, void * bufPtr
, off_t offset
, ssize_t length
)
1818 ssize_t bytestransferred
;
1819 void * rawData
= NULL
;
1822 ssize_t dataOffset
= 0;
1823 int result
= FSUR_IO_SUCCESS
;
1825 if (ioctl(fd
, DKIOCBLKSIZE
, &blocksize
) < 0) {
1827 fprintf(stderr
, "hfs.util: couldn't determine block size of device.\n");
1829 result
= FSUR_IO_FAIL
;
1832 /* put offset and length in terms of device blocksize */
1833 rawOffset
= offset
/ blocksize
* blocksize
;
1834 dataOffset
= offset
- rawOffset
;
1835 rawLength
= ((length
+ dataOffset
+ blocksize
- 1) / blocksize
) * blocksize
;
1836 rawData
= malloc(rawLength
);
1837 if (rawData
== NULL
) {
1838 result
= FSUR_IO_FAIL
;
1842 deviceoffset
= lseek( fd
, rawOffset
, SEEK_SET
);
1843 if ( deviceoffset
!= rawOffset
) {
1844 result
= FSUR_IO_FAIL
;
1848 /* If the write isn't block-aligned, read the existing data before writing the new data: */
1849 if (((rawOffset
% blocksize
) != 0) || ((rawLength
% blocksize
) != 0)) {
1850 bytestransferred
= read(fd
, rawData
, rawLength
);
1851 if ( bytestransferred
!= rawLength
) {
1853 fprintf(stderr
, "writeAt: attempt to pre-read data from device failed (errno = %d)\n", errno
);
1855 result
= FSUR_IO_FAIL
;
1860 bcopy(bufPtr
, rawData
+ dataOffset
, length
); /* Copy in the new data */
1862 deviceoffset
= lseek( fd
, rawOffset
, SEEK_SET
);
1863 if ( deviceoffset
!= rawOffset
) {
1864 result
= FSUR_IO_FAIL
;
1868 bytestransferred
= write(fd
, rawData
, rawLength
);
1869 if ( bytestransferred
!= rawLength
) {
1871 fprintf(stderr
, "writeAt: attempt to write data to device failed?!");
1873 result
= FSUR_IO_FAIL
;
1878 if (rawData
) free(rawData
);
1885 /******************************************************************************
1887 * V O L U M E S T A T U S D A T A B A S E R O U T I N E S
1889 *****************************************************************************/
1891 #define DBHANDLESIGNATURE 0x75917737
1893 /* Flag values for operation options: */
1894 #define DBMARKPOSITION 1
1896 static char gVSDBPath
[] = "/var/db/volinfo.database";
1898 #define MAXIOMALLOC 16384
1900 /* Database layout: */
1907 char statusFlags
[8];
1914 struct VSDBRecord record
;
1918 #define DBKEYSEPARATOR ':'
1919 #define DBBLANKSPACE ' '
1920 #define DBRECORDTERMINATOR '\n'
1922 /* In-memory data structures: */
1925 unsigned long signature
;
1928 off_t recordPosition
;
1931 typedef struct VSDBState
*VSDBStatePtr
;
1934 unsigned long state
[5];
1935 unsigned long count
[2];
1936 unsigned char buffer
[64];
1941 /* Internal function prototypes: */
1942 static int LockDB(VSDBStatePtr dbstateptr
, int lockmode
);
1943 static int UnlockDB(VSDBStatePtr dbstateptr
);
1945 static int FindVolumeRecordByUUID(VSDBStatePtr dbstateptr
, VolumeUUID
*volumeID
, struct VSDBEntry
*dbentry
, unsigned long options
);
1946 static int AddVolumeRecord(VSDBStatePtr dbstateptr
, struct VSDBEntry
*dbentry
);
1947 static int UpdateVolumeRecord(VSDBStatePtr dbstateptr
, struct VSDBEntry
*dbentry
);
1948 static int GetVSDBEntry(VSDBStatePtr dbstateptr
, struct VSDBEntry
*dbentry
);
1949 static int CompareVSDBKeys(struct VSDBKey
*key1
, struct VSDBKey
*key2
);
1951 static void FormatULong(unsigned long u
, char *s
);
1952 static void FormatUUID(VolumeUUID
*volumeID
, char *UUIDField
);
1953 static void FormatDBKey(VolumeUUID
*volumeID
, struct VSDBKey
*dbkey
);
1954 static void FormatDBRecord(unsigned long volumeStatusFlags
, struct VSDBRecord
*dbrecord
);
1955 static void FormatDBEntry(VolumeUUID
*volumeID
, unsigned long volumeStatusFlags
, struct VSDBEntry
*dbentry
);
1956 static unsigned long ConvertHexStringToULong(const char *hs
, long maxdigits
);
1958 static void SHA1Transform(unsigned long state
[5], unsigned char buffer
[64]);
1959 static void SHA1Init(SHA1_CTX
* context
);
1960 static void SHA1Update(SHA1_CTX
* context
, void* data
, size_t len
);
1961 static void SHA1Final(unsigned char digest
[20], SHA1_CTX
* context
);
1965 /******************************************************************************
1967 * P U B L I S H E D I N T E R F A C E R O U T I N E S
1969 *****************************************************************************/
1971 void GenerateVolumeUUID(VolumeUUID
*newVolumeID
) {
1973 char randomInputBuffer
[26];
1974 unsigned char digest
[20];
1979 char sysctlstring
[128];
1981 struct loadavg sysloadavg
;
1982 struct vmtotal sysvmtotal
;
1985 /* Initialize the SHA-1 context for processing: */
1988 /* Now process successive bits of "random" input to seed the process: */
1990 /* The current system's uptime: */
1992 SHA1Update(&context
, &uptime
, sizeof(uptime
));
1994 /* The kernel's boot time: */
1996 mib
[1] = KERN_BOOTTIME
;
1997 datalen
= sizeof(sysdata
);
1998 sysctl(mib
, 2, &sysdata
, &datalen
, NULL
, 0);
1999 SHA1Update(&context
, &sysdata
, datalen
);
2001 /* The system's host id: */
2003 mib
[1] = KERN_HOSTID
;
2004 datalen
= sizeof(sysdata
);
2005 sysctl(mib
, 2, &sysdata
, &datalen
, NULL
, 0);
2006 SHA1Update(&context
, &sysdata
, datalen
);
2008 /* The system's host name: */
2010 mib
[1] = KERN_HOSTNAME
;
2011 datalen
= sizeof(sysctlstring
);
2012 sysctl(mib
, 2, sysctlstring
, &datalen
, NULL
, 0);
2013 SHA1Update(&context
, sysctlstring
, datalen
);
2015 /* The running kernel's OS release string: */
2017 mib
[1] = KERN_OSRELEASE
;
2018 datalen
= sizeof(sysctlstring
);
2019 sysctl(mib
, 2, sysctlstring
, &datalen
, NULL
, 0);
2020 SHA1Update(&context
, sysctlstring
, datalen
);
2022 /* The running kernel's version string: */
2024 mib
[1] = KERN_VERSION
;
2025 datalen
= sizeof(sysctlstring
);
2026 sysctl(mib
, 2, sysctlstring
, &datalen
, NULL
, 0);
2027 SHA1Update(&context
, sysctlstring
, datalen
);
2029 /* The system's load average: */
2031 mib
[1] = VM_LOADAVG
;
2032 datalen
= sizeof(sysloadavg
);
2033 sysctl(mib
, 2, &sysloadavg
, &datalen
, NULL
, 0);
2034 SHA1Update(&context
, &sysloadavg
, datalen
);
2036 /* The system's VM statistics: */
2039 datalen
= sizeof(sysvmtotal
);
2040 sysctl(mib
, 2, &sysvmtotal
, &datalen
, NULL
, 0);
2041 SHA1Update(&context
, &sysvmtotal
, datalen
);
2043 /* The current GMT (26 ASCII characters): */
2045 strncpy(randomInputBuffer
, asctime(gmtime(&now
)), 26); /* "Mon Mar 27 13:46:26 2000" */
2046 SHA1Update(&context
, randomInputBuffer
, 26);
2048 /* Pad the accumulated input and extract the final digest hash: */
2049 SHA1Final(digest
, &context
);
2051 memcpy(newVolumeID
, digest
, sizeof(*newVolumeID
));
2052 } while ((newVolumeID
->v
.high
== 0) || (newVolumeID
->v
.low
== 0));
2057 void ConvertVolumeUUIDStringToUUID(const char *UUIDString
, VolumeUUID
*volumeID
) {
2060 unsigned long nextdigit
;
2061 unsigned long high
= 0;
2062 unsigned long low
= 0;
2063 unsigned long carry
;
2065 for (i
= 0; (i
< VOLUMEUUIDLENGTH
) && ((c
= UUIDString
[i
]) != (char)0) ; ++i
) {
2066 if ((c
>= '0') && (c
<= '9')) {
2067 nextdigit
= c
- '0';
2068 } else if ((c
>= 'A') && (c
<= 'F')) {
2069 nextdigit
= c
- 'A' + 10;
2070 } else if ((c
>= 'a') && (c
<= 'f')) {
2071 nextdigit
= c
- 'a' + 10;
2075 carry
= ((low
& 0xF0000000) >> 28) & 0x0000000F;
2076 high
= (high
<< 4) | carry
;
2077 low
= (low
<< 4) | nextdigit
;
2080 volumeID
->v
.high
= high
;
2081 volumeID
->v
.low
= low
;
2086 void ConvertVolumeUUIDToString(VolumeUUID
*volumeID
, char *UUIDString
) {
2087 FormatUUID(volumeID
, UUIDString
);
2088 *(UUIDString
+16) = (char)0; /* Append a terminating null character */
2093 int OpenVolumeStatusDB(VolumeStatusDBHandle
*DBHandlePtr
) {
2094 VSDBStatePtr dbstateptr
;
2096 *DBHandlePtr
= NULL
;
2098 dbstateptr
= (VSDBStatePtr
)malloc(sizeof(*dbstateptr
));
2099 if (dbstateptr
== NULL
) {
2103 dbstateptr
->dbmode
= O_RDWR
;
2104 dbstateptr
->dbfile
= open(gVSDBPath
, O_RDWR
| O_CREAT
, S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IROTH
);
2105 if (dbstateptr
->dbfile
== -1) {
2107 The file couldn't be opened for read/write access:
2108 try read-only access before giving up altogether.
2110 dbstateptr
->dbmode
= O_RDONLY
;
2111 dbstateptr
->dbfile
= open(gVSDBPath
, O_RDONLY
| O_CREAT
, S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IROTH
);
2112 if (dbstateptr
->dbfile
== -1) {
2117 dbstateptr
->signature
= DBHANDLESIGNATURE
;
2118 *DBHandlePtr
= (VolumeStatusDBHandle
)dbstateptr
;
2124 int GetVolumeStatusDBEntry(VolumeStatusDBHandle DBHandle
, VolumeUUID
*volumeID
, unsigned long *VolumeStatus
) {
2125 VSDBStatePtr dbstateptr
= (VSDBStatePtr
)DBHandle
;
2126 struct VSDBEntry dbentry
;
2129 if (dbstateptr
->signature
!= DBHANDLESIGNATURE
) return EINVAL
;
2131 if ((result
= LockDB(dbstateptr
, LOCK_SH
)) != 0) return result
;
2133 if ((result
= FindVolumeRecordByUUID(dbstateptr
, volumeID
, &dbentry
, 0)) != 0) {
2136 *VolumeStatus
= VOLUME_RECORDED
| ConvertHexStringToULong(dbentry
.record
.statusFlags
, sizeof(dbentry
.record
.statusFlags
));
2141 UnlockDB(dbstateptr
);
2147 int SetVolumeStatusDBEntry(VolumeStatusDBHandle DBHandle
, VolumeUUID
*volumeID
, unsigned long VolumeStatus
) {
2148 VSDBStatePtr dbstateptr
= (VSDBStatePtr
)DBHandle
;
2149 struct VSDBEntry dbentry
;
2152 if (dbstateptr
->signature
!= DBHANDLESIGNATURE
) return EINVAL
;
2153 if (VolumeStatus
& ~VOLUME_VALIDSTATUSBITS
) return EINVAL
;
2155 if ((result
= LockDB(dbstateptr
, LOCK_EX
)) != 0) return result
;
2157 FormatDBEntry(volumeID
, VolumeStatus
, &dbentry
);
2158 if ((result
= FindVolumeRecordByUUID(dbstateptr
, volumeID
, NULL
, DBMARKPOSITION
)) == 0) {
2160 fprintf(stderr
,"AddLocalVolumeUUID: found record in database; updating in place.\n");
2162 result
= UpdateVolumeRecord(dbstateptr
, &dbentry
);
2163 } else if (result
== -1) {
2165 fprintf(stderr
,"AddLocalVolumeUUID: record not found in database; appending at end.\n");
2167 result
= AddVolumeRecord(dbstateptr
, &dbentry
);
2172 fsync(dbstateptr
->dbfile
);
2177 UnlockDB(dbstateptr
);
2183 int DeleteVolumeStatusDBEntry(VolumeStatusDBHandle DBHandle
, VolumeUUID
*volumeID
) {
2184 VSDBStatePtr dbstateptr
= (VSDBStatePtr
)DBHandle
;
2187 unsigned long iobuffersize
;
2188 void *iobuffer
= NULL
;
2190 unsigned long iotransfersize
;
2191 unsigned long bytestransferred
;
2193 if (dbstateptr
->signature
!= DBHANDLESIGNATURE
) return EINVAL
;
2195 if ((result
= LockDB(dbstateptr
, LOCK_EX
)) != 0) return result
;
2197 if ((result
= FindVolumeRecordByUUID(dbstateptr
, volumeID
, NULL
, DBMARKPOSITION
)) != 0) {
2199 fprintf(stderr
, "DeleteLocalVolumeUUID: No record with matching volume UUID in DB (result = %d).\n", result
);
2201 if (result
== -1) result
= 0; /* Entry wasn't in the database to begin with? */
2205 fprintf(stderr
, "DeleteLocalVolumeUUID: Found record with matching volume UUID...\n");
2207 if ((result
= stat(gVSDBPath
, &dbinfo
)) != 0) goto ErrExit
;
2208 if ((dbinfo
.st_size
- dbstateptr
->recordPosition
- sizeof(struct VSDBEntry
)) <= MAXIOMALLOC
) {
2209 iobuffersize
= dbinfo
.st_size
- dbstateptr
->recordPosition
- sizeof(struct VSDBEntry
);
2211 iobuffersize
= MAXIOMALLOC
;
2214 fprintf(stderr
, "DeleteLocalVolumeUUID: DB size = 0x%08lx; recordPosition = 0x%08lx;\n",
2215 (unsigned long)dbinfo
.st_size
, (unsigned long)dbstateptr
->recordPosition
);
2216 fprintf(stderr
, "DeleteLocalVolumeUUID: I/O buffer size = 0x%lx\n", iobuffersize
);
2218 if (iobuffersize
> 0) {
2219 iobuffer
= malloc(iobuffersize
);
2220 if (iobuffer
== NULL
) {
2225 dataoffset
= dbstateptr
->recordPosition
+ sizeof(struct VSDBEntry
);
2227 iotransfersize
= dbinfo
.st_size
- dataoffset
;
2228 if (iotransfersize
> 0) {
2229 if (iotransfersize
> iobuffersize
) iotransfersize
= iobuffersize
;
2232 fprintf(stderr
, "DeleteLocalVolumeUUID: reading 0x%08lx bytes starting at 0x%08lx ...\n", iotransfersize
, (unsigned long)dataoffset
);
2234 lseek(dbstateptr
->dbfile
, dataoffset
, SEEK_SET
);
2235 bytestransferred
= read(dbstateptr
->dbfile
, iobuffer
, iotransfersize
);
2236 if (bytestransferred
!= iotransfersize
) {
2242 fprintf(stderr
, "DeleteLocalVolumeUUID: writing 0x%08lx bytes starting at 0x%08lx ...\n", iotransfersize
, (unsigned long)(dataoffset
- (off_t
)sizeof(struct VSDBEntry
)));
2244 lseek(dbstateptr
->dbfile
, dataoffset
- (off_t
)sizeof(struct VSDBEntry
), SEEK_SET
);
2245 bytestransferred
= write(dbstateptr
->dbfile
, iobuffer
, iotransfersize
);
2246 if (bytestransferred
!= iotransfersize
) {
2251 dataoffset
+= (off_t
)iotransfersize
;
2253 } while (iotransfersize
> 0);
2256 fprintf(stderr
, "DeleteLocalVolumeUUID: truncating database file to 0x%08lx bytes.\n", (unsigned long)(dbinfo
.st_size
- (off_t
)(sizeof(struct VSDBEntry
))));
2258 if ((result
= ftruncate(dbstateptr
->dbfile
, dbinfo
.st_size
- (off_t
)(sizeof(struct VSDBEntry
)))) != 0) {
2262 fsync(dbstateptr
->dbfile
);
2268 if (iobuffer
) free(iobuffer
);
2269 UnlockDB(dbstateptr
);
2277 int CloseVolumeStatusDB(VolumeStatusDBHandle DBHandle
) {
2278 VSDBStatePtr dbstateptr
= (VSDBStatePtr
)DBHandle
;
2280 if (dbstateptr
->signature
!= DBHANDLESIGNATURE
) return EINVAL
;
2282 dbstateptr
->signature
= 0;
2284 close(dbstateptr
->dbfile
); /* Nothing we can do about any errors... */
2285 dbstateptr
->dbfile
= 0;
2294 /******************************************************************************
2296 * I N T E R N A L O N L Y D A T A B A S E R O U T I N E S
2298 *****************************************************************************/
2300 static int LockDB(VSDBStatePtr dbstateptr
, int lockmode
) {
2302 fprintf(stderr
, "LockDB: Locking VSDB file...\n");
2304 return flock(dbstateptr
->dbfile
, lockmode
);
2309 static int UnlockDB(VSDBStatePtr dbstateptr
) {
2311 fprintf(stderr
, "UnlockDB: Unlocking VSDB file...\n");
2313 return flock(dbstateptr
->dbfile
, LOCK_UN
);
2318 static int FindVolumeRecordByUUID(VSDBStatePtr dbstateptr
, VolumeUUID
*volumeID
, struct VSDBEntry
*targetEntry
, unsigned long options
) {
2319 struct VSDBKey searchkey
;
2320 struct VSDBEntry dbentry
;
2323 FormatDBKey(volumeID
, &searchkey
);
2324 lseek(dbstateptr
->dbfile
, 0, SEEK_SET
);
2327 result
= GetVSDBEntry(dbstateptr
, &dbentry
);
2328 if ((result
== 0) && (CompareVSDBKeys(&dbentry
.key
, &searchkey
) == 0)) {
2329 if (targetEntry
!= NULL
) {
2331 fprintf(stderr
, "FindVolumeRecordByUUID: copying %d. bytes from %08xl to %08l...\n", sizeof(*targetEntry
), &dbentry
, targetEntry
);
2333 memcpy(targetEntry
, &dbentry
, sizeof(*targetEntry
));
2337 } while (result
== 0);
2344 static int AddVolumeRecord(VSDBStatePtr dbstateptr
, struct VSDBEntry
*dbentry
) {
2346 VolumeUUIDString id
;
2350 strncpy(id
, dbentry
->key
.uuid
, sizeof(dbentry
->key
.uuid
));
2351 id
[sizeof(dbentry
->key
.uuid
)] = (char)0;
2352 fprintf(stderr
, "AddVolumeRecord: Adding record for UUID #%s...\n", id
);
2354 lseek(dbstateptr
->dbfile
, 0, SEEK_END
);
2355 return write(dbstateptr
->dbfile
, dbentry
, sizeof(struct VSDBEntry
));
2361 static int UpdateVolumeRecord(VSDBStatePtr dbstateptr
, struct VSDBEntry
*dbentry
) {
2363 VolumeUUIDString id
;
2367 strncpy(id
, dbentry
->key
.uuid
, sizeof(dbentry
->key
.uuid
));
2368 id
[sizeof(dbentry
->key
.uuid
)] = (char)0;
2369 fprintf(stderr
, "UpdateVolumeRecord: Updating record for UUID #%s at offset 0x%08lx in database...\n", id
, (unsigned long)dbstateptr
->recordPosition
);
2371 lseek(dbstateptr
->dbfile
, dbstateptr
->recordPosition
, SEEK_SET
);
2373 fprintf(stderr
, "UpdateVolumeRecord: Writing %d. bytes...\n", sizeof(*dbentry
));
2375 return write(dbstateptr
->dbfile
, dbentry
, sizeof(*dbentry
));
2380 static int GetVSDBEntry(VSDBStatePtr dbstateptr
, struct VSDBEntry
*dbentry
) {
2381 struct VSDBEntry entry
;
2384 VolumeUUIDString id
;
2387 dbstateptr
->recordPosition
= lseek(dbstateptr
->dbfile
, 0, SEEK_CUR
);
2388 #if 0 // DEBUG_TRACE
2389 fprintf(stderr
, "GetVSDBEntry: starting reading record at offset 0x%08lx...\n", (unsigned long)dbstateptr
->recordPosition
);
2391 result
= read(dbstateptr
->dbfile
, &entry
, sizeof(entry
));
2392 if ((result
!= sizeof(entry
)) ||
2393 (entry
.keySeparator
!= DBKEYSEPARATOR
) ||
2394 (entry
.space
!= DBBLANKSPACE
) ||
2395 (entry
.terminator
!= DBRECORDTERMINATOR
)) {
2400 strncpy(id
, entry
.key
.uuid
, sizeof(entry
.key
.uuid
));
2401 id
[sizeof(entry
.key
.uuid
)] = (char)0;
2402 fprintf(stderr
, "GetVSDBEntry: returning entry for UUID #%s...\n", id
);
2404 memcpy(dbentry
, &entry
, sizeof(*dbentry
));
2410 static int CompareVSDBKeys(struct VSDBKey
*key1
, struct VSDBKey
*key2
) {
2411 #if 0 // DEBUG_TRACE
2412 VolumeUUIDString id
;
2414 strncpy(id
, key1
->uuid
, sizeof(key1
->uuid
));
2415 id
[sizeof(key1
->uuid
)] = (char)0;
2416 fprintf(stderr
, "CompareVSDBKeys: comparing #%s to ", id
);
2417 strncpy(id
, key2
->uuid
, sizeof(key2
->uuid
));
2418 id
[sizeof(key2
->uuid
)] = (char)0;
2419 fprintf(stderr
, "%s (%d.)...\n", id
, sizeof(key1
->uuid
));
2422 return memcmp(key1
->uuid
, key2
->uuid
, sizeof(key1
->uuid
));
2427 /******************************************************************************
2429 * F O R M A T T I N G A N D C O N V E R S I O N R O U T I N E S
2431 *****************************************************************************/
2433 static void FormatULong(unsigned long u
, char *s
) {
2438 for (i
= 0; i
< 8; ++i
) {
2439 d
= ((u
& 0xF0000000) >> 28) & 0x0000000F;
2441 *digitptr
++ = (char)(d
+ '0');
2443 *digitptr
++ = (char)(d
- 10 + 'A');
2451 static void FormatUUID(VolumeUUID
*volumeID
, char *UUIDField
) {
2452 FormatULong(volumeID
->v
.high
, UUIDField
);
2453 FormatULong(volumeID
->v
.low
, UUIDField
+8);
2459 static void FormatDBKey(VolumeUUID
*volumeID
, struct VSDBKey
*dbkey
) {
2460 FormatUUID(volumeID
, dbkey
->uuid
);
2465 static void FormatDBRecord(unsigned long volumeStatusFlags
, struct VSDBRecord
*dbrecord
) {
2466 FormatULong(volumeStatusFlags
, dbrecord
->statusFlags
);
2471 static void FormatDBEntry(VolumeUUID
*volumeID
, unsigned long volumeStatusFlags
, struct VSDBEntry
*dbentry
) {
2472 FormatDBKey(volumeID
, &dbentry
->key
);
2473 dbentry
->keySeparator
= DBKEYSEPARATOR
;
2474 dbentry
->space
= DBBLANKSPACE
;
2475 FormatDBRecord(volumeStatusFlags
, &dbentry
->record
);
2476 #if 0 // DEBUG_TRACE
2477 dbentry
->terminator
= (char)0;
2478 fprintf(stderr
, "FormatDBEntry: '%s' (%d.)\n", dbentry
, sizeof(*dbentry
));
2480 dbentry
->terminator
= DBRECORDTERMINATOR
;
2485 static unsigned long ConvertHexStringToULong(const char *hs
, long maxdigits
) {
2488 unsigned long nextdigit
;
2492 for (i
= 0; (i
< 8) && ((c
= hs
[i
]) != (char)0) ; ++i
) {
2493 if ((c
>= '0') && (c
<= '9')) {
2494 nextdigit
= c
- '0';
2495 } else if ((c
>= 'A') && (c
<= 'F')) {
2496 nextdigit
= c
- 'A' + 10;
2497 } else if ((c
>= 'a') && (c
<= 'f')) {
2498 nextdigit
= c
- 'a' + 10;
2502 n
= (n
<< 4) + nextdigit
;
2510 /******************************************************************************
2512 * S H A - 1 I M P L E M E N T A T I O N R O U T I N E S
2514 *****************************************************************************/
2517 Derived from SHA-1 in C
2518 By Steve Reid <steve@edmweb.com>
2521 Test Vectors (from FIPS PUB 180-1)
2523 A9993E36 4706816A BA3E2571 7850C26C 9CD0D89D
2524 "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"
2525 84983E44 1C3BD26E BAAE4AA1 F95129E5 E54670F1
2526 A million repetitions of "a"
2527 34AA973C D4C4DAA4 F61EEB2B DBAD2731 6534016F
2530 /* #define LITTLE_ENDIAN * This should be #define'd if true. */
2531 /* #define SHA1HANDSOFF * Copies data before messing with it. */
2533 #define rol(value, bits) (((value) << (bits)) | ((value) >> (32 - (bits))))
2535 /* blk0() and blk() perform the initial expand. */
2536 /* I got the idea of expanding during the round function from SSLeay */
2537 #ifdef LITTLE_ENDIAN
2538 #define blk0(i) (block->l[i] = (rol(block->l[i],24)&0xFF00FF00) \
2539 |(rol(block->l[i],8)&0x00FF00FF))
2541 #define blk0(i) block->l[i]
2543 #define blk(i) (block->l[i&15] = rol(block->l[(i+13)&15]^block->l[(i+8)&15] \
2544 ^block->l[(i+2)&15]^block->l[i&15],1))
2546 /* (R0+R1), R2, R3, R4 are the different operations used in SHA1 */
2548 #define R0(v,w,x,y,z,i) z+=((w&(x^y))^y)+blk0(i)+0x5A827999+rol(v,5);w=rol(w,30);printf("t = %2d: %08lX %08lX %08lX %08lX %08lX\n", i, a, b, c, d, e);
2549 #define R1(v,w,x,y,z,i) z+=((w&(x^y))^y)+blk(i)+0x5A827999+rol(v,5);w=rol(w,30);printf("t = %2d: %08lX %08lX %08lX %08lX %08lX\n", i, a, b, c, d, e);
2550 #define R2(v,w,x,y,z,i) z+=(w^x^y)+blk(i)+0x6ED9EBA1+rol(v,5);w=rol(w,30);printf("t = %2d: %08lX %08lX %08lX %08lX %08lX\n", i, a, b, c, d, e);
2551 #define R3(v,w,x,y,z,i) z+=(((w|x)&y)|(w&x))+blk(i)+0x8F1BBCDC+rol(v,5);w=rol(w,30);printf("t = %2d: %08lX %08lX %08lX %08lX %08lX\n", i, a, b, c, d, e);
2552 #define R4(v,w,x,y,z,i) z+=(w^x^y)+blk(i)+0xCA62C1D6+rol(v,5);w=rol(w,30);printf("t = %2d: %08lX %08lX %08lX %08lX %08lX\n", i, a, b, c, d, e);
2554 #define R0(v,w,x,y,z,i) z+=((w&(x^y))^y)+blk0(i)+0x5A827999+rol(v,5);w=rol(w,30);
2555 #define R1(v,w,x,y,z,i) z+=((w&(x^y))^y)+blk(i)+0x5A827999+rol(v,5);w=rol(w,30);
2556 #define R2(v,w,x,y,z,i) z+=(w^x^y)+blk(i)+0x6ED9EBA1+rol(v,5);w=rol(w,30);
2557 #define R3(v,w,x,y,z,i) z+=(((w|x)&y)|(w&x))+blk(i)+0x8F1BBCDC+rol(v,5);w=rol(w,30);
2558 #define R4(v,w,x,y,z,i) z+=(w^x^y)+blk(i)+0xCA62C1D6+rol(v,5);w=rol(w,30);
2562 /* Hash a single 512-bit block. This is the core of the algorithm. */
2564 static void SHA1Transform(unsigned long state
[5], unsigned char buffer
[64])
2566 unsigned long a
, b
, c
, d
, e
;
2568 unsigned char c
[64];
2569 unsigned long l
[16];
2571 CHAR64LONG16
* block
;
2573 static unsigned char workspace
[64];
2574 block
= (CHAR64LONG16
*)workspace
;
2575 memcpy(block
, buffer
, 64);
2577 block
= (CHAR64LONG16
*)buffer
;
2579 /* Copy context->state[] to working vars */
2586 printf(" A B C D E\n");
2587 printf(" -------- -------- -------- -------- --------\n");
2588 printf(" %08lX %08lX %08lX %08lX %08lX\n", a
, b
, c
, d
, e
);
2590 /* 4 rounds of 20 operations each. Loop unrolled. */
2591 R0(a
,b
,c
,d
,e
, 0); R0(e
,a
,b
,c
,d
, 1); R0(d
,e
,a
,b
,c
, 2); R0(c
,d
,e
,a
,b
, 3);
2592 R0(b
,c
,d
,e
,a
, 4); R0(a
,b
,c
,d
,e
, 5); R0(e
,a
,b
,c
,d
, 6); R0(d
,e
,a
,b
,c
, 7);
2593 R0(c
,d
,e
,a
,b
, 8); R0(b
,c
,d
,e
,a
, 9); R0(a
,b
,c
,d
,e
,10); R0(e
,a
,b
,c
,d
,11);
2594 R0(d
,e
,a
,b
,c
,12); R0(c
,d
,e
,a
,b
,13); R0(b
,c
,d
,e
,a
,14); R0(a
,b
,c
,d
,e
,15);
2595 R1(e
,a
,b
,c
,d
,16); R1(d
,e
,a
,b
,c
,17); R1(c
,d
,e
,a
,b
,18); R1(b
,c
,d
,e
,a
,19);
2596 R2(a
,b
,c
,d
,e
,20); R2(e
,a
,b
,c
,d
,21); R2(d
,e
,a
,b
,c
,22); R2(c
,d
,e
,a
,b
,23);
2597 R2(b
,c
,d
,e
,a
,24); R2(a
,b
,c
,d
,e
,25); R2(e
,a
,b
,c
,d
,26); R2(d
,e
,a
,b
,c
,27);
2598 R2(c
,d
,e
,a
,b
,28); R2(b
,c
,d
,e
,a
,29); R2(a
,b
,c
,d
,e
,30); R2(e
,a
,b
,c
,d
,31);
2599 R2(d
,e
,a
,b
,c
,32); R2(c
,d
,e
,a
,b
,33); R2(b
,c
,d
,e
,a
,34); R2(a
,b
,c
,d
,e
,35);
2600 R2(e
,a
,b
,c
,d
,36); R2(d
,e
,a
,b
,c
,37); R2(c
,d
,e
,a
,b
,38); R2(b
,c
,d
,e
,a
,39);
2601 R3(a
,b
,c
,d
,e
,40); R3(e
,a
,b
,c
,d
,41); R3(d
,e
,a
,b
,c
,42); R3(c
,d
,e
,a
,b
,43);
2602 R3(b
,c
,d
,e
,a
,44); R3(a
,b
,c
,d
,e
,45); R3(e
,a
,b
,c
,d
,46); R3(d
,e
,a
,b
,c
,47);
2603 R3(c
,d
,e
,a
,b
,48); R3(b
,c
,d
,e
,a
,49); R3(a
,b
,c
,d
,e
,50); R3(e
,a
,b
,c
,d
,51);
2604 R3(d
,e
,a
,b
,c
,52); R3(c
,d
,e
,a
,b
,53); R3(b
,c
,d
,e
,a
,54); R3(a
,b
,c
,d
,e
,55);
2605 R3(e
,a
,b
,c
,d
,56); R3(d
,e
,a
,b
,c
,57); R3(c
,d
,e
,a
,b
,58); R3(b
,c
,d
,e
,a
,59);
2606 R4(a
,b
,c
,d
,e
,60); R4(e
,a
,b
,c
,d
,61); R4(d
,e
,a
,b
,c
,62); R4(c
,d
,e
,a
,b
,63);
2607 R4(b
,c
,d
,e
,a
,64); R4(a
,b
,c
,d
,e
,65); R4(e
,a
,b
,c
,d
,66); R4(d
,e
,a
,b
,c
,67);
2608 R4(c
,d
,e
,a
,b
,68); R4(b
,c
,d
,e
,a
,69); R4(a
,b
,c
,d
,e
,70); R4(e
,a
,b
,c
,d
,71);
2609 R4(d
,e
,a
,b
,c
,72); R4(c
,d
,e
,a
,b
,73); R4(b
,c
,d
,e
,a
,74); R4(a
,b
,c
,d
,e
,75);
2610 R4(e
,a
,b
,c
,d
,76); R4(d
,e
,a
,b
,c
,77); R4(c
,d
,e
,a
,b
,78); R4(b
,c
,d
,e
,a
,79);
2611 /* Add the working vars back into context.state[] */
2617 /* Wipe variables */
2618 a
= b
= c
= d
= e
= 0;
2622 /* SHA1Init - Initialize new context */
2624 static void SHA1Init(SHA1_CTX
* context
)
2626 /* SHA1 initialization constants */
2627 context
->state
[0] = 0x67452301;
2628 context
->state
[1] = 0xEFCDAB89;
2629 context
->state
[2] = 0x98BADCFE;
2630 context
->state
[3] = 0x10325476;
2631 context
->state
[4] = 0xC3D2E1F0;
2632 context
->count
[0] = context
->count
[1] = 0;
2636 /* Run your data through this. */
2638 static void SHA1Update(SHA1_CTX
* context
, void* data
, size_t len
)
2640 unsigned char *dataptr
= (char *)data
;
2643 j
= (context
->count
[0] >> 3) & 63;
2644 if ((context
->count
[0] += len
<< 3) < (len
<< 3)) context
->count
[1]++;
2645 context
->count
[1] += (len
>> 29);
2646 if ((j
+ len
) > 63) {
2647 memcpy(&context
->buffer
[j
], dataptr
, (i
= 64-j
));
2648 SHA1Transform(context
->state
, context
->buffer
);
2649 for ( ; i
+ 63 < len
; i
+= 64) {
2650 SHA1Transform(context
->state
, &dataptr
[i
]);
2655 memcpy(&context
->buffer
[j
], &dataptr
[i
], len
- i
);
2659 /* Add padding and return the message digest. */
2661 static void SHA1Final(unsigned char digest
[20], SHA1_CTX
* context
)
2664 unsigned char finalcount
[8];
2666 for (i
= 0; i
< 8; i
++) {
2667 finalcount
[i
] = (unsigned char)((context
->count
[(i
>= 4 ? 0 : 1)]
2668 >> ((3-(i
& 3)) * 8) ) & 255); /* Endian independent */
2670 SHA1Update(context
, (unsigned char *)"\200", 1);
2671 while ((context
->count
[0] & 504) != 448) {
2672 SHA1Update(context
, (unsigned char *)"\0", 1);
2674 SHA1Update(context
, finalcount
, 8); /* Should cause a SHA1Transform() */
2675 for (i
= 0; i
< 20; i
++) {
2676 digest
[i
] = (unsigned char)
2677 ((context
->state
[i
>>2] >> ((3-(i
& 3)) * 8) ) & 255);
2679 /* Wipe variables */
2681 memset(context
->buffer
, 0, 64);
2682 memset(context
->state
, 0, 20);
2683 memset(context
->count
, 0, 8);
2684 memset(&finalcount
, 0, 8);
2685 #ifdef SHA1HANDSOFF /* make SHA1Transform overwrite it's own static vars */
2686 SHA1Transform(context
->state
, context
->buffer
);