1/*
2 * Copyright (c) 2004 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
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
11 * file.
12 *
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.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23
24#include <stdio.h>
25#include <string.h>
26#include <stdlib.h>
27#include <err.h>
28#include <sys/types.h>
29#include <sys/stat.h>
30#include <dirent.h>
31
32#include <CoreFoundation/CoreFoundation.h>
33
34#include "util.h"
35
36static char *
37findRealDevice(char *dev) {
38	struct dirent *dp;
39	DIR *dir;
40	struct stat sbuf;
41	ino_t mine;
42	char *dName;
43	char *retval = NULL;
44
45	if (stat(dev, &sbuf) == -1) {
46		warn("cannot stat device file %s", dev);
47		return NULL;
48	}
49	if (sbuf.st_nlink == 1) {   // assume this is the real device
50		return NULL;
51	}
52#define BEGINS(x, y) (!strncmp((x), (y), strlen((y))))
53	if (BEGINS(dev, "/dev/rdisk") || BEGINS(dev, "rdisk") ||
54		BEGINS(dev, "/dev/disk") || BEGINS(dev, "disk")) {
55		return NULL;
56	}
57
58	if (BEGINS(dev, "/dev/")) {
59		dName = dev + 4;
60	} else {
61		dName = dev;
62	}
63
64#undef BEGINS
65	mine = sbuf.st_ino;
66
67	dir = opendir("/dev");
68	while ((dp = readdir(dir))) {
69		char *tmp = dp->d_name;
70		char dbuf[6 + strlen(tmp)];
71		memcpy(dbuf, "/dev/", 6);
72		memcpy(dbuf + 5, tmp, sizeof(dbuf) - 5);
73		if (!strcmp(dbuf, dName))
74			continue;
75		tmp = strrchr(dbuf, 's');
76		if (!tmp)
77			continue;
78		if (dp->d_fileno == mine) {
79			retval = strndup(dbuf, tmp - dbuf);
80			break;
81		}
82	}
83	closedir(dir);
84	return retval;
85}
86
87void
88doStatus(const char *dev) {
89	char *realDev = findRealDevice((char*)dev);
90	printf("Device %s\n", dev);
91	if (realDev) {
92		printf("\tReal device is %s\n", realDev);
93	} else {
94		realDev = (char*)dev;
95	}
96	dev = realDev;
97	// XXX -- need to ensure this is an AppleLabel partition
98	if (IsAppleLabel(dev) != 1) {
99		printf("\t* * * NOT A VALID LABEL * * *\n");
100		return;
101	}
102	printf("Metadata size = %u\n", GetMetadataSize(dev));
103	if (VerifyChecksum(dev) == 0)
104		printf("Metadata checksum is good\n");
105	else
106		printf("\t* * * Checksum is bad * * *\n");
107
108	return;
109}
110