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 <fcntl.h>
29#include <unistd.h>
30#include <sys/conf.h>
31#include <sys/filio.h>
32#include <sys/ioctl.h>
33#include <sys/types.h>
34#include <sys/stat.h>
35
36#include <CoreFoundation/CoreFoundation.h>
37
38#include "create.h"
39#include "destroy.h"
40#include "props.h"
41#include "status.h"
42#include "util.h"
43
44char *gDeviceName;
45int gDebug = 0, gVerbose = 0;
46
47static void
48usage(void) {
49	static const char *ustr = "\
50\t-status <diskname>\n\
51\t-create <diskname> [-msize=<size>] [owner-uid=<uid>] [owner-gid=<gid>] \
52[dev-name=<name>] [owner-mode=<mode>] [other-property=<value>]\n\
53\t-destroy <diskname>\n\
54\t[-properties] <diskname> [owner-uid[=<uid>]] [owner-gid[=<gid>]] \
55[dev-name[=<name>]] [owner-mode[=<mode>]] [other-property[=<value>]]";
56
57	errx(1, "usage: One of \n%s\n", ustr);
58}
59
60/*
61 * There's probably a better way to do this using IOKit; when
62 * I find out how, I'll make this code do it.
63 */
64static int
65isDisk(const char *dev) {
66	int fd;
67	int retval = 0;
68	int type;
69
70	if (gDebug)
71		return 1;
72
73	fd = open(dev, O_RDONLY);
74	if (fd == -1) {
75		warn("cannot open %s", dev);
76		goto done;
77	}
78
79	if (ioctl(fd, FIODTYPE, &type) == -1) {
80		goto done;
81	}
82
83	if (type == D_DISK) {
84		retval = 1;
85	}
86done:
87	close(fd);
88	return retval;
89}
90
91int main(int ac, char **argv) {
92	char **args = NULL;
93	int argLen;
94	char *cp;
95	char **av = argv;
96	enum cmd  {
97		CMD_UNKNOWN = 0,
98		CMD_STATUS, CMD_CREATE, CMD_DESTROY, CMD_PROPS
99	} cmd;
100
101	if (ac == 1) {
102		usage();
103	}
104
105	av++;
106
107	while (*av != NULL) {
108		if (!strcmp(av[0], "-D")) {
109			gDebug++;
110			av++;
111			warnx("setting debug to level %d", gDebug);
112			continue;
113		}
114		if (!strcmp(av[0], "-v")) {
115			gVerbose++;
116			av++;
117			continue;
118		}
119		break;
120	}
121	cp = *av++;
122
123	argLen = strlen(cp);
124
125	if (strncasecmp(cp, "-status", argLen) == 0) {
126		cmd = CMD_STATUS;
127		gDeviceName = *av;
128		args = av + 1;
129	} else if (strncasecmp(cp, "-destroy", argLen) == 0) {
130		cmd = CMD_DESTROY;
131		gDeviceName = *av;
132		args = av + 1;
133	} else if (strncasecmp(cp, "-create", argLen) == 0) {
134		cmd = CMD_CREATE;
135		gDeviceName = *av;
136		args = av + 1;
137	} else if (strncasecmp(cp, "-properties", argLen) == 0 ||
138		cp[0] != '-') {
139		cmd = CMD_PROPS;
140		if (cp[0] != '-') {
141			gDeviceName = cp;
142			args = av;
143		} else {
144			gDeviceName = *av;
145			args = av + 1;
146		}
147	} else {
148		cmd = CMD_UNKNOWN;
149	}
150
151	if (!isDisk(gDeviceName)) {
152		errx(1, "%s must be a disk device", gDeviceName);
153	}
154
155	switch (cmd) {
156	case CMD_UNKNOWN:	usage(); break;
157	case CMD_STATUS:	doStatus(gDeviceName);	// no arguments
158				break;
159	case CMD_CREATE:	doCreate(gDeviceName, args);
160				break;
161	case CMD_DESTROY:	doDestroy(gDeviceName);  break;
162	case CMD_PROPS:		doProps(gDeviceName, args);
163				break;
164	}
165	return 0;
166}
167
168