]>
git.cameronkatri.com Git - apple_cmds.git/blob - diskdev_cmds/fstyp.tproj/fstyp_ntfs.c
2 * Copyright (c) 2006 Apple Computer, 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@
33 /* copied from diskdev_cmds/fsck_msdos/dosfs.h */
34 #define DOSBOOTBLOCKSIZE 512
35 #define MAX_SECTOR_SIZE 4096
41 char *rawname(char *name
);
42 char *unrawname(char *name
);
43 int checkVolHdr(const unsigned char *volhdr
);
44 char *blockcheck(char *origname
);
49 * prefer to use raw device. TODO: suppose block device is valid but
50 * the corresponding raw device is not valid, then we fail. this is
51 * probably no the desired behavior.
55 main(int argc
, char **argv
)
57 unsigned char volhdr
[MAX_SECTOR_SIZE
] = {0};
64 if ((progname
= strrchr(*argv
, '/')))
72 devname
= blockcheck(argv
[1]);
74 if (devname
!= NULL
) {
75 if ((fd
= open(devname
, O_RDONLY
, 0)) < 0) {
77 } else if (read(fd
, volhdr
, MAX_SECTOR_SIZE
) != MAX_SECTOR_SIZE
) {
80 retval
= checkVolHdr(volhdr
);
96 fprintf(stdout
, "usage: %s device\n", progname
);
100 /* copied from diskdev_cmds/fsck_hfs/utilities.c */
104 static char rawbuf
[32];
107 if ((dp
= strrchr(name
, '/')) == 0)
110 (void) strlcpy(rawbuf
, name
, sizeof(rawbuf
));
112 (void) strlcat(rawbuf
, "/r", sizeof(rawbuf
));
113 (void) strlcat(rawbuf
, &dp
[1], sizeof(rawbuf
));
118 /* copied from diskdev_cmds/fsck_hfs/utilities.c */
120 unrawname(char *name
)
126 if ((dp
= strrchr(name
, '/')) == 0)
128 if (stat(name
, &stb
) < 0)
130 if ((stb
.st_mode
& S_IFMT
) != S_IFCHR
)
134 dp_len
= strlen(&dp
[2]) + 1;
135 (void)memmove(&dp
[1], &dp
[2], dp_len
);
141 * copied from diskdev_cmds/fsck_hfs/utilities.c, and modified:
142 * 1) remove "hotroot"
143 * 2) if error, return NULL
144 * 3) if not a char device, return NULL (effectively, this is treated
145 * as error even if accessing the block device might have been OK)
148 blockcheck(char *origname
)
150 struct stat stblock
, stchar
;
157 if (stat(newname
, &stblock
) < 0) {
159 fprintf(stderr
, "Can't stat %s\n", newname
);
162 if ((stblock
.st_mode
& S_IFMT
) == S_IFBLK
) {
163 raw
= rawname(newname
);
164 if (stat(raw
, &stchar
) < 0) {
166 fprintf(stderr
, "Can't stat %s\n", raw
);
169 if ((stchar
.st_mode
& S_IFMT
) == S_IFCHR
) {
172 fprintf(stderr
, "%s is not a character device\n", raw
);
175 } else if ((stblock
.st_mode
& S_IFMT
) == S_IFCHR
&& !retried
) {
176 newname
= unrawname(newname
);
180 /* not a block or character device */
185 * (sanity) check the volume header in volhdr
187 * return 1 if volhdr is a NTFS volhdr, 0 otherwise
190 checkVolHdr(const unsigned char *volhdr
)
192 /* NTFS volumes have an OEMid of NTFS followed by four spaces. */
193 const char *ntfs_oemid
= "NTFS ";
199 * Only check the OEMid. This should be sufficiently specific so it
200 * does not match anything else. If it ever does it would be easy to
201 * check more bootsector fields for validity...
203 if (memcmp(ntfs_oemid
, volhdr
+ 3, 8))