xref: /haiku/src/bin/mount.c (revision 285fe0a395fcb3f5043d354965f5ec0ec074c3c1)
117049c45SAxel Dörfler /*
2da6d1a70SAxel Dörfler  * Copyright 2001-2005, Axel Dörfler, axeld@pinc-software.de. All rights reserved.
3da6d1a70SAxel Dörfler  * Distributed under the terms of the MIT License.
417049c45SAxel Dörfler  */
517049c45SAxel Dörfler 
617049c45SAxel Dörfler /**	Mounts a volume with the specified file system */
717049c45SAxel Dörfler 
817049c45SAxel Dörfler 
917049c45SAxel Dörfler #include <fs_volume.h>
1017049c45SAxel Dörfler 
1117049c45SAxel Dörfler #include <sys/stat.h>
1217049c45SAxel Dörfler #include <unistd.h>
1317049c45SAxel Dörfler #include <stdio.h>
14*285fe0a3SFrançois Revol #include <stdlib.h>
1517049c45SAxel Dörfler #include <string.h>
1617049c45SAxel Dörfler 
1717049c45SAxel Dörfler 
1817049c45SAxel Dörfler static void
usage(const char * programName)1917049c45SAxel Dörfler usage(const char *programName)
2017049c45SAxel Dörfler {
2117049c45SAxel Dörfler 
22b3e65bfdSFrançois Revol 	printf("usage: %s [-ro] [-t fstype] [-p parameter] [device] directory\n"
2317049c45SAxel Dörfler 		"\t-ro\tmounts the volume read-only\n"
24b3e65bfdSFrançois Revol 		"\t-t\tspecifies the file system to use (defaults to automatic recognition)\n"
25b3e65bfdSFrançois Revol 		"\t-p\tspecifies parameters to pass to the file system (-o also accepted)\n"
26b3e65bfdSFrançois Revol 		"\tif device is not specified, NULL is passed (for in-memory filesystems)\n",programName);
2715e29cfaSJérôme Duval 	exit(1);
2817049c45SAxel Dörfler }
2917049c45SAxel Dörfler 
3017049c45SAxel Dörfler 
3117049c45SAxel Dörfler int
main(int argc,char ** argv)3217049c45SAxel Dörfler main(int argc, char **argv)
3317049c45SAxel Dörfler {
3417049c45SAxel Dörfler 	const char *programName = argv[0];
35b3e65bfdSFrançois Revol 	const char *device = NULL;
36b3e65bfdSFrançois Revol 	const char *mountPoint;
3717049c45SAxel Dörfler 	const char *parameter = NULL;
3817049c45SAxel Dörfler 	const char *fs = NULL;
3917049c45SAxel Dörfler 	struct stat mountStat;
404daad179SAxel Dörfler 	dev_t volume;
4117049c45SAxel Dörfler 	uint32 flags = 0;
4217049c45SAxel Dörfler 
4317049c45SAxel Dörfler 	/* prettify the program name */
4417049c45SAxel Dörfler 
4517049c45SAxel Dörfler 	if (strrchr(programName, '/'))
4617049c45SAxel Dörfler 		programName = strrchr(programName, '/') + 1;
4717049c45SAxel Dörfler 
4817049c45SAxel Dörfler 	/* get all options */
4917049c45SAxel Dörfler 
5017049c45SAxel Dörfler 	while (*++argv) {
5117049c45SAxel Dörfler 		char *arg = *argv;
523bb475bdSJérôme Duval 		argc--;
5317049c45SAxel Dörfler 		if (*arg != '-')
5417049c45SAxel Dörfler 			break;
5517049c45SAxel Dörfler 
5617049c45SAxel Dörfler 		if (!strcmp(++arg, "ro") && (flags & B_MOUNT_READ_ONLY) == 0)
5717049c45SAxel Dörfler 			flags |= B_MOUNT_READ_ONLY;
583bb475bdSJérôme Duval 		else if (!strcmp(arg, "t") && fs == NULL) {
593bb475bdSJérôme Duval 			if (argc <= 1)
603bb475bdSJérôme Duval 				break;
6117049c45SAxel Dörfler 			fs = *++argv;
623bb475bdSJérôme Duval 			argc--;
63b3e65bfdSFrançois Revol 		} else if ((!strcmp(arg, "p") || !strcmp(arg, "o")) && parameter == NULL) {
643bb475bdSJérôme Duval 			if (argc <= 1)
653bb475bdSJérôme Duval 				break;
6617049c45SAxel Dörfler 			parameter = *++argv;
673bb475bdSJérôme Duval 			argc--;
683bb475bdSJérôme Duval 		} else
6917049c45SAxel Dörfler 			usage(programName);
7017049c45SAxel Dörfler 	}
7117049c45SAxel Dörfler 
7217049c45SAxel Dörfler 	/* check the arguments */
7317049c45SAxel Dörfler 
74b3e65bfdSFrançois Revol 	if (argc > 1) {
7517049c45SAxel Dörfler 		device = argv[0];
76b3e65bfdSFrançois Revol 		argv++;
77b3e65bfdSFrançois Revol 		argc--;
78b3e65bfdSFrançois Revol 	}
79b3e65bfdSFrançois Revol 	mountPoint = argv[0];
8017049c45SAxel Dörfler 
81b3e65bfdSFrançois Revol 	if (mountPoint == NULL)
8217049c45SAxel Dörfler 		usage(programName);
8317049c45SAxel Dörfler 
8417049c45SAxel Dörfler 	if (stat(mountPoint, &mountStat) < 0) {
8517049c45SAxel Dörfler 		fprintf(stderr, "%s: The mount point '%s' is not accessible\n", programName, mountPoint);
8615e29cfaSJérôme Duval 		return 1;
8717049c45SAxel Dörfler 	}
8817049c45SAxel Dörfler 	if (!S_ISDIR(mountStat.st_mode)) {
8917049c45SAxel Dörfler 		fprintf(stderr, "%s: The mount point '%s' is not a directory\n", programName, mountPoint);
9015e29cfaSJérôme Duval 		return 1;
9117049c45SAxel Dörfler 	}
9217049c45SAxel Dörfler 
9317049c45SAxel Dörfler 	/* do the work */
9417049c45SAxel Dörfler 
954daad179SAxel Dörfler 	volume = fs_mount_volume(mountPoint, device, fs, flags, parameter);
964daad179SAxel Dörfler 	if (volume < B_OK) {
974daad179SAxel Dörfler 		fprintf(stderr, "%s: %s\n", programName, strerror(volume));
9815e29cfaSJérôme Duval 		return 1;
9917049c45SAxel Dörfler 	}
10017049c45SAxel Dörfler 	return 0;
10117049c45SAxel Dörfler }
10217049c45SAxel Dörfler 
103