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> 1417049c45SAxel Dörfler #include <string.h> 1517049c45SAxel Dörfler 1617049c45SAxel Dörfler 1717049c45SAxel Dörfler static void 1817049c45SAxel Dörfler usage(const char *programName) 1917049c45SAxel Dörfler { 2017049c45SAxel Dörfler 2134a29026SJérôme Duval printf("usage: %s [-ro] [-t fstype] [-p parameter] device directory\n" 2217049c45SAxel Dörfler "\t-ro\tmounts the volume read-only\n" 2317049c45SAxel Dörfler "\t-t\tspecifies the file system to use (defaults to automatic recognition)\n",programName); 2417049c45SAxel Dörfler exit(0); 2517049c45SAxel Dörfler } 2617049c45SAxel Dörfler 2717049c45SAxel Dörfler 2817049c45SAxel Dörfler int 2917049c45SAxel Dörfler main(int argc, char **argv) 3017049c45SAxel Dörfler { 3117049c45SAxel Dörfler const char *programName = argv[0]; 3217049c45SAxel Dörfler const char *device, *mountPoint; 3317049c45SAxel Dörfler const char *parameter = NULL; 3417049c45SAxel Dörfler const char *fs = NULL; 3517049c45SAxel Dörfler struct stat mountStat; 364daad179SAxel Dörfler dev_t volume; 3717049c45SAxel Dörfler uint32 flags = 0; 3817049c45SAxel Dörfler 3917049c45SAxel Dörfler /* prettify the program name */ 4017049c45SAxel Dörfler 4117049c45SAxel Dörfler if (strrchr(programName, '/')) 4217049c45SAxel Dörfler programName = strrchr(programName, '/') + 1; 4317049c45SAxel Dörfler 4417049c45SAxel Dörfler /* get all options */ 4517049c45SAxel Dörfler 4617049c45SAxel Dörfler while (*++argv) { 4717049c45SAxel Dörfler char *arg = *argv; 48*3bb475bdSJérôme Duval argc--; 4917049c45SAxel Dörfler if (*arg != '-') 5017049c45SAxel Dörfler break; 5117049c45SAxel Dörfler 5217049c45SAxel Dörfler if (!strcmp(++arg, "ro") && (flags & B_MOUNT_READ_ONLY) == 0) 5317049c45SAxel Dörfler flags |= B_MOUNT_READ_ONLY; 54*3bb475bdSJérôme Duval else if (!strcmp(arg, "t") && fs == NULL) { 55*3bb475bdSJérôme Duval if (argc <= 1) 56*3bb475bdSJérôme Duval break; 5717049c45SAxel Dörfler fs = *++argv; 58*3bb475bdSJérôme Duval argc--; 59*3bb475bdSJérôme Duval } else if (!strcmp(arg, "p") && parameter == NULL) { 60*3bb475bdSJérôme Duval if (argc <= 1) 61*3bb475bdSJérôme Duval break; 6217049c45SAxel Dörfler parameter = *++argv; 63*3bb475bdSJérôme Duval argc--; 64*3bb475bdSJérôme Duval } else 6517049c45SAxel Dörfler usage(programName); 6617049c45SAxel Dörfler } 6717049c45SAxel Dörfler 6817049c45SAxel Dörfler /* check the arguments */ 6917049c45SAxel Dörfler 7017049c45SAxel Dörfler device = argv[0]; 7117049c45SAxel Dörfler mountPoint = argv[1]; 7217049c45SAxel Dörfler 7317049c45SAxel Dörfler if (device == NULL || mountPoint == NULL) 7417049c45SAxel Dörfler usage(programName); 7517049c45SAxel Dörfler 7617049c45SAxel Dörfler if (stat(mountPoint, &mountStat) < 0) { 7717049c45SAxel Dörfler fprintf(stderr, "%s: The mount point '%s' is not accessible\n", programName, mountPoint); 7817049c45SAxel Dörfler return -1; 7917049c45SAxel Dörfler } 8017049c45SAxel Dörfler if (!S_ISDIR(mountStat.st_mode)) { 8117049c45SAxel Dörfler fprintf(stderr, "%s: The mount point '%s' is not a directory\n", programName, mountPoint); 8217049c45SAxel Dörfler return -1; 8317049c45SAxel Dörfler } 8417049c45SAxel Dörfler 8517049c45SAxel Dörfler /* do the work */ 8617049c45SAxel Dörfler 874daad179SAxel Dörfler volume = fs_mount_volume(mountPoint, device, fs, flags, parameter); 884daad179SAxel Dörfler if (volume < B_OK) { 894daad179SAxel Dörfler fprintf(stderr, "%s: %s\n", programName, strerror(volume)); 9017049c45SAxel Dörfler return -1; 9117049c45SAxel Dörfler } 9217049c45SAxel Dörfler return 0; 9317049c45SAxel Dörfler } 9417049c45SAxel Dörfler 95