| 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 |
|
|---|
| 29 | #include <loc.h>
|
|---|
| 30 | #include <stdio.h>
|
|---|
| 31 | #include <stdlib.h>
|
|---|
| 32 | #include <str_error.h>
|
|---|
| 33 | #include <vfs/vfs.h>
|
|---|
| 34 | #include <vfs/vfs_mtab.h>
|
|---|
| 35 | #include <adt/list.h>
|
|---|
| 36 | #include <errno.h>
|
|---|
| 37 | #include <getopt.h>
|
|---|
| 38 | #include <inttypes.h>
|
|---|
| 39 | #include <str.h>
|
|---|
| 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 |
|
|---|
| 49 | static struct option const long_options[] = {
|
|---|
| 50 | { "help", no_argument, 0, 'h' },
|
|---|
| 51 | { "instance", required_argument, 0, 'i' },
|
|---|
| 52 | { "types", no_argument, 0, 't' },
|
|---|
| 53 | { 0, 0, 0, 0 }
|
|---|
| 54 | };
|
|---|
| 55 |
|
|---|
| 56 |
|
|---|
| 57 | /* Displays help for mount in various levels */
|
|---|
| 58 | void help_cmd_mount(unsigned int level)
|
|---|
| 59 | {
|
|---|
| 60 | static char helpfmt[] =
|
|---|
| 61 | "Usage: %s <fstype> <mp> [dev] [<moptions>]\n"
|
|---|
| 62 | "Options:\n"
|
|---|
| 63 | " -h, --help A short option summary\n"
|
|---|
| 64 | " -i, --instance ## Mount a specific instance\n"
|
|---|
| 65 | " -t, --types List available file system types\n";
|
|---|
| 66 |
|
|---|
| 67 | if (level == HELP_SHORT) {
|
|---|
| 68 | printf("'%s' mounts a file system.\n", cmdname);
|
|---|
| 69 | } else {
|
|---|
| 70 | help_cmd_mount(HELP_SHORT);
|
|---|
| 71 | printf(helpfmt, cmdname);
|
|---|
| 72 | }
|
|---|
| 73 | return;
|
|---|
| 74 | }
|
|---|
| 75 |
|
|---|
| 76 | static void print_mtab_list(void)
|
|---|
| 77 | {
|
|---|
| 78 | LIST_INITIALIZE(mtab_list);
|
|---|
| 79 | mtab_ent_t *old_ent = NULL;
|
|---|
| 80 | char *svc_name;
|
|---|
| 81 | errno_t rc;
|
|---|
| 82 |
|
|---|
| 83 | vfs_get_mtab_list(&mtab_list);
|
|---|
| 84 |
|
|---|
| 85 | list_foreach(mtab_list, link, mtab_ent_t, mtab_ent) {
|
|---|
| 86 | if (old_ent)
|
|---|
| 87 | free(old_ent);
|
|---|
| 88 |
|
|---|
| 89 | old_ent = mtab_ent;
|
|---|
| 90 |
|
|---|
| 91 | printf("%s", mtab_ent->fs_name);
|
|---|
| 92 |
|
|---|
| 93 | printf(" %s", mtab_ent->mp);
|
|---|
| 94 |
|
|---|
| 95 | rc = loc_service_get_name(mtab_ent->service_id, &svc_name);
|
|---|
| 96 | if (rc == EOK) {
|
|---|
| 97 | printf(" %s", svc_name);
|
|---|
| 98 | free(svc_name);
|
|---|
| 99 | } else {
|
|---|
| 100 | printf(" (%" PRIun ")", mtab_ent->service_id);
|
|---|
| 101 | }
|
|---|
| 102 |
|
|---|
| 103 | putchar('\n');
|
|---|
| 104 | }
|
|---|
| 105 |
|
|---|
| 106 | if (old_ent)
|
|---|
| 107 | free(old_ent);
|
|---|
| 108 | }
|
|---|
| 109 |
|
|---|
| 110 | static void print_fstypes(void)
|
|---|
| 111 | {
|
|---|
| 112 | errno_t rc;
|
|---|
| 113 | vfs_fstypes_t fstypes;
|
|---|
| 114 | char **p;
|
|---|
| 115 |
|
|---|
| 116 | rc = vfs_fstypes(&fstypes);
|
|---|
| 117 | if (rc != EOK) {
|
|---|
| 118 | printf("Error getting list of available file system types.\n");
|
|---|
| 119 | return;
|
|---|
| 120 | }
|
|---|
| 121 |
|
|---|
| 122 | printf("Available file system types:\n");
|
|---|
| 123 | p = fstypes.fstypes;
|
|---|
| 124 | while (*p != NULL)
|
|---|
| 125 | printf("\t%s\n", *p++);
|
|---|
| 126 | vfs_fstypes_free(&fstypes);
|
|---|
| 127 | }
|
|---|
| 128 |
|
|---|
| 129 | /* Main entry point for mount, accepts an array of arguments */
|
|---|
| 130 | int cmd_mount(char **argv)
|
|---|
| 131 | {
|
|---|
| 132 | unsigned int argc;
|
|---|
| 133 | const char *mopts = "";
|
|---|
| 134 | const char *dev = "";
|
|---|
| 135 | errno_t rc;
|
|---|
| 136 | int c, opt_ind;
|
|---|
| 137 | unsigned int instance = 0;
|
|---|
| 138 | bool instance_set = false;
|
|---|
| 139 | char **t_argv;
|
|---|
| 140 |
|
|---|
| 141 | argc = cli_count_args(argv);
|
|---|
| 142 |
|
|---|
| 143 | for (c = 0, optreset = 1, optind = 0, opt_ind = 0; c != -1;) {
|
|---|
| 144 | c = getopt_long(argc, argv, "i:ht", long_options, &opt_ind);
|
|---|
| 145 | switch (c) {
|
|---|
| 146 | case 'h':
|
|---|
| 147 | help_cmd_mount(HELP_LONG);
|
|---|
| 148 | return CMD_SUCCESS;
|
|---|
| 149 | case 'i':
|
|---|
| 150 | instance = (unsigned int) strtol(optarg, NULL, 10);
|
|---|
| 151 | instance_set = true;
|
|---|
| 152 | break;
|
|---|
| 153 | case 't':
|
|---|
| 154 | print_fstypes();
|
|---|
| 155 | return CMD_SUCCESS;
|
|---|
| 156 | }
|
|---|
| 157 | }
|
|---|
| 158 |
|
|---|
| 159 | if (instance_set) {
|
|---|
| 160 | argc -= 2;
|
|---|
| 161 | t_argv = &argv[2];
|
|---|
| 162 | } else
|
|---|
| 163 | t_argv = &argv[0];
|
|---|
| 164 |
|
|---|
| 165 | if ((argc == 2) || (argc > 5)) {
|
|---|
| 166 | printf("%s: invalid number of arguments. Try `mount --help'\n",
|
|---|
| 167 | cmdname);
|
|---|
| 168 | return CMD_FAILURE;
|
|---|
| 169 | }
|
|---|
| 170 | if (argc == 1) {
|
|---|
| 171 | print_mtab_list();
|
|---|
| 172 | return CMD_SUCCESS;
|
|---|
| 173 | }
|
|---|
| 174 | if (argc > 3)
|
|---|
| 175 | dev = t_argv[3];
|
|---|
| 176 | if (argc == 5)
|
|---|
| 177 | mopts = t_argv[4];
|
|---|
| 178 |
|
|---|
| 179 | rc = vfs_mount_path(t_argv[2], t_argv[1], dev, mopts, 0, instance);
|
|---|
| 180 | if (rc != EOK) {
|
|---|
| 181 | printf("Unable to mount %s filesystem to %s on %s (rc=%s)\n",
|
|---|
| 182 | t_argv[2], t_argv[1], t_argv[3], str_error(rc));
|
|---|
| 183 | if (rc == ENOFS)
|
|---|
| 184 | print_fstypes();
|
|---|
| 185 | return CMD_FAILURE;
|
|---|
| 186 | }
|
|---|
| 187 |
|
|---|
| 188 | return CMD_SUCCESS;
|
|---|
| 189 | }
|
|---|
| 190 |
|
|---|