[d4a172b] | 1 | /*
|
---|
| 2 | * Copyright (c) 2009 Jakub Jermar
|
---|
| 3 | * All rights reserved.
|
---|
| 4 | *
|
---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
---|
| 6 | * modification, are permitted provided that the following conditions
|
---|
| 7 | * are met:
|
---|
| 8 | *
|
---|
| 9 | * - Redistributions of source code must retain the above copyright
|
---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 13 | * documentation and/or other materials provided with the distribution.
|
---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
---|
| 15 | * derived from this software without specific prior written permission.
|
---|
| 16 | *
|
---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 27 | */
|
---|
| 28 |
|
---|
[2e1b9dc] | 29 | #include <loc.h>
|
---|
[d4a172b] | 30 | #include <stdio.h>
|
---|
| 31 | #include <stdlib.h>
|
---|
[680708d] | 32 | #include <str_error.h>
|
---|
[d4a172b] | 33 | #include <vfs/vfs.h>
|
---|
[35b7d86e] | 34 | #include <vfs/vfs_mtab.h>
|
---|
[c08fb04] | 35 | #include <adt/list.h>
|
---|
[d4a172b] | 36 | #include <errno.h>
|
---|
[e6cb880] | 37 | #include <getopt.h>
|
---|
[41047bf] | 38 | #include <inttypes.h>
|
---|
[1d6dd2a] | 39 | #include <str.h>
|
---|
[d4a172b] | 40 | #include "config.h"
|
---|
| 41 | #include "util.h"
|
---|
| 42 | #include "errors.h"
|
---|
| 43 | #include "entry.h"
|
---|
| 44 | #include "mount.h"
|
---|
| 45 | #include "cmds.h"
|
---|
| 46 |
|
---|
| 47 | static const char *cmdname = "mount";
|
---|
| 48 |
|
---|
[e6cb880] | 49 | static struct option const long_options[] = {
|
---|
| 50 | { "help", no_argument, 0, 'h' },
|
---|
[4979403] | 51 | { "instance", required_argument, 0, 'i' },
|
---|
[b14d9f9] | 52 | { "types", no_argument, 0, 't' },
|
---|
[e6cb880] | 53 | { 0, 0, 0, 0 }
|
---|
| 54 | };
|
---|
| 55 |
|
---|
| 56 | /* Displays help for mount in various levels */
|
---|
[d4a172b] | 57 | void help_cmd_mount(unsigned int level)
|
---|
| 58 | {
|
---|
| 59 | static char helpfmt[] =
|
---|
[b14d9f9] | 60 | "Usage: %s <fstype> <mp> [dev] [<moptions>]\n"
|
---|
| 61 | "Options:\n"
|
---|
| 62 | " -h, --help A short option summary\n"
|
---|
| 63 | " -i, --instance ## Mount a specific instance\n"
|
---|
| 64 | " -t, --types List available file system types\n";
|
---|
| 65 |
|
---|
[d4a172b] | 66 | if (level == HELP_SHORT) {
|
---|
| 67 | printf("'%s' mounts a file system.\n", cmdname);
|
---|
| 68 | } else {
|
---|
| 69 | help_cmd_mount(HELP_SHORT);
|
---|
| 70 | printf(helpfmt, cmdname);
|
---|
| 71 | }
|
---|
| 72 | return;
|
---|
| 73 | }
|
---|
| 74 |
|
---|
[c08fb04] | 75 | static void print_mtab_list(void)
|
---|
| 76 | {
|
---|
| 77 | LIST_INITIALIZE(mtab_list);
|
---|
[8d6a41c] | 78 | mtab_ent_t *old_ent = NULL;
|
---|
[2e1b9dc] | 79 | char *svc_name;
|
---|
[b7fd2a0] | 80 | errno_t rc;
|
---|
[2e1b9dc] | 81 |
|
---|
[6afc9d7] | 82 | vfs_get_mtab_list(&mtab_list);
|
---|
[c08fb04] | 83 |
|
---|
[feeac0d] | 84 | list_foreach(mtab_list, link, mtab_ent_t, mtab_ent) {
|
---|
[c08fb04] | 85 | if (old_ent)
|
---|
| 86 | free(old_ent);
|
---|
| 87 |
|
---|
[8d6a41c] | 88 | old_ent = mtab_ent;
|
---|
[c08fb04] | 89 |
|
---|
[41047bf] | 90 | printf("%s", mtab_ent->fs_name);
|
---|
[2e1b9dc] | 91 |
|
---|
| 92 | printf(" %s", mtab_ent->mp);
|
---|
| 93 |
|
---|
| 94 | rc = loc_service_get_name(mtab_ent->service_id, &svc_name);
|
---|
| 95 | if (rc == EOK) {
|
---|
| 96 | printf(" %s", svc_name);
|
---|
| 97 | free(svc_name);
|
---|
| 98 | } else {
|
---|
| 99 | printf(" (%" PRIun ")", mtab_ent->service_id);
|
---|
| 100 | }
|
---|
[c08fb04] | 101 |
|
---|
[2e1b9dc] | 102 | putchar('\n');
|
---|
[c08fb04] | 103 | }
|
---|
| 104 |
|
---|
| 105 | if (old_ent)
|
---|
| 106 | free(old_ent);
|
---|
| 107 | }
|
---|
| 108 |
|
---|
[b14d9f9] | 109 | static void print_fstypes(void)
|
---|
| 110 | {
|
---|
[b7fd2a0] | 111 | errno_t rc;
|
---|
[b14d9f9] | 112 | vfs_fstypes_t fstypes;
|
---|
| 113 | char **p;
|
---|
| 114 |
|
---|
| 115 | rc = vfs_fstypes(&fstypes);
|
---|
| 116 | if (rc != EOK) {
|
---|
| 117 | printf("Error getting list of available file system types.\n");
|
---|
| 118 | return;
|
---|
| 119 | }
|
---|
| 120 |
|
---|
| 121 | printf("Available file system types:\n");
|
---|
| 122 | p = fstypes.fstypes;
|
---|
| 123 | while (*p != NULL)
|
---|
| 124 | printf("\t%s\n", *p++);
|
---|
| 125 | vfs_fstypes_free(&fstypes);
|
---|
| 126 | }
|
---|
| 127 |
|
---|
[d4a172b] | 128 | /* Main entry point for mount, accepts an array of arguments */
|
---|
| 129 | int cmd_mount(char **argv)
|
---|
| 130 | {
|
---|
| 131 | unsigned int argc;
|
---|
[a000878c] | 132 | const char *mopts = "";
|
---|
[2ecc9d4] | 133 | const char *dev = "";
|
---|
[b7fd2a0] | 134 | errno_t rc;
|
---|
[d5c1051] | 135 | int c, opt_ind;
|
---|
[4979403] | 136 | unsigned int instance = 0;
|
---|
| 137 | bool instance_set = false;
|
---|
| 138 | char **t_argv;
|
---|
[d4a172b] | 139 |
|
---|
| 140 | argc = cli_count_args(argv);
|
---|
| 141 |
|
---|
[948222e4] | 142 | c = 0;
|
---|
| 143 | optreset = 1;
|
---|
| 144 | optind = 0;
|
---|
| 145 | opt_ind = 0;
|
---|
| 146 |
|
---|
| 147 | while (c != -1) {
|
---|
[b14d9f9] | 148 | c = getopt_long(argc, argv, "i:ht", long_options, &opt_ind);
|
---|
[e6cb880] | 149 | switch (c) {
|
---|
| 150 | case 'h':
|
---|
| 151 | help_cmd_mount(HELP_LONG);
|
---|
| 152 | return CMD_SUCCESS;
|
---|
[4979403] | 153 | case 'i':
|
---|
| 154 | instance = (unsigned int) strtol(optarg, NULL, 10);
|
---|
| 155 | instance_set = true;
|
---|
| 156 | break;
|
---|
[b14d9f9] | 157 | case 't':
|
---|
| 158 | print_fstypes();
|
---|
| 159 | return CMD_SUCCESS;
|
---|
[e6cb880] | 160 | }
|
---|
| 161 | }
|
---|
| 162 |
|
---|
[4979403] | 163 | if (instance_set) {
|
---|
| 164 | argc -= 2;
|
---|
| 165 | t_argv = &argv[2];
|
---|
| 166 | } else
|
---|
| 167 | t_argv = &argv[0];
|
---|
| 168 |
|
---|
[c08fb04] | 169 | if ((argc == 2) || (argc > 5)) {
|
---|
[e6cb880] | 170 | printf("%s: invalid number of arguments. Try `mount --help'\n",
|
---|
[d4a172b] | 171 | cmdname);
|
---|
| 172 | return CMD_FAILURE;
|
---|
| 173 | }
|
---|
[c08fb04] | 174 | if (argc == 1) {
|
---|
| 175 | print_mtab_list();
|
---|
| 176 | return CMD_SUCCESS;
|
---|
| 177 | }
|
---|
[2ecc9d4] | 178 | if (argc > 3)
|
---|
[4979403] | 179 | dev = t_argv[3];
|
---|
[d4a172b] | 180 | if (argc == 5)
|
---|
[4979403] | 181 | mopts = t_argv[4];
|
---|
[d4a172b] | 182 |
|
---|
[80743a1] | 183 | rc = vfs_mount_path(t_argv[2], t_argv[1], dev, mopts, 0, instance);
|
---|
[d4a172b] | 184 | if (rc != EOK) {
|
---|
[680708d] | 185 | printf("Unable to mount %s filesystem to %s on %s (rc=%s)\n",
|
---|
[80743a1] | 186 | t_argv[2], t_argv[1], t_argv[3], str_error(rc));
|
---|
[b14d9f9] | 187 | if (rc == ENOFS)
|
---|
| 188 | print_fstypes();
|
---|
[d4a172b] | 189 | return CMD_FAILURE;
|
---|
| 190 | }
|
---|
| 191 |
|
---|
| 192 | return CMD_SUCCESS;
|
---|
| 193 | }
|
---|