xref: /haiku/src/bin/mount.c (revision 893988af824e65e49e55f517b157db8386e8002b)
1 /*
2  * Copyright 2001-2005, Axel Dörfler, axeld@pinc-software.de. All rights reserved.
3  * Distributed under the terms of the MIT License.
4  */
5 
6 /**	Mounts a volume with the specified file system */
7 
8 
9 #include <fs_volume.h>
10 
11 #include <sys/stat.h>
12 #include <unistd.h>
13 #include <stdio.h>
14 #include <string.h>
15 
16 
17 static void
18 usage(const char *programName)
19 {
20 
21 	printf("usage: %s [-ro] [-t fstype] [-p parameter] [device] directory\n"
22 		"\t-ro\tmounts the volume read-only\n"
23 		"\t-t\tspecifies the file system to use (defaults to automatic recognition)\n"
24 		"\t-p\tspecifies parameters to pass to the file system (-o also accepted)\n"
25 		"\tif device is not specified, NULL is passed (for in-memory filesystems)\n",programName);
26 	exit(1);
27 }
28 
29 
30 int
31 main(int argc, char **argv)
32 {
33 	const char *programName = argv[0];
34 	const char *device = NULL;
35 	const char *mountPoint;
36 	const char *parameter = NULL;
37 	const char *fs = NULL;
38 	struct stat mountStat;
39 	dev_t volume;
40 	uint32 flags = 0;
41 
42 	/* prettify the program name */
43 
44 	if (strrchr(programName, '/'))
45 		programName = strrchr(programName, '/') + 1;
46 
47 	/* get all options */
48 
49 	while (*++argv) {
50 		char *arg = *argv;
51 		argc--;
52 		if (*arg != '-')
53 			break;
54 
55 		if (!strcmp(++arg, "ro") && (flags & B_MOUNT_READ_ONLY) == 0)
56 			flags |= B_MOUNT_READ_ONLY;
57 		else if (!strcmp(arg, "t") && fs == NULL) {
58 			if (argc <= 1)
59 				break;
60 			fs = *++argv;
61 			argc--;
62 		} else if ((!strcmp(arg, "p") || !strcmp(arg, "o")) && parameter == NULL) {
63 			if (argc <= 1)
64 				break;
65 			parameter = *++argv;
66 			argc--;
67 		} else
68 			usage(programName);
69 	}
70 
71 	/* check the arguments */
72 
73 	if (argc > 1) {
74 		device = argv[0];
75 		argv++;
76 		argc--;
77 	}
78 	mountPoint = argv[0];
79 
80 	if (mountPoint == NULL)
81 		usage(programName);
82 
83 	if (stat(mountPoint, &mountStat) < 0) {
84 		fprintf(stderr, "%s: The mount point '%s' is not accessible\n", programName, mountPoint);
85 		return 1;
86 	}
87 	if (!S_ISDIR(mountStat.st_mode)) {
88 		fprintf(stderr, "%s: The mount point '%s' is not a directory\n", programName, mountPoint);
89 		return 1;
90 	}
91 
92 	/* do the work */
93 
94 	volume = fs_mount_volume(mountPoint, device, fs, flags, parameter);
95 	if (volume < B_OK) {
96 		fprintf(stderr, "%s: %s\n", programName, strerror(volume));
97 		return 1;
98 	}
99 	return 0;
100 }
101 
102