| 1 | /*
|
|---|
| 2 | * Copyright (c) 2017 Jiri Svoboda
|
|---|
| 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 | /** @addtogroup vol
|
|---|
| 30 | * @{
|
|---|
| 31 | */
|
|---|
| 32 | /** @file Volume administration (interface to volsrv).
|
|---|
| 33 | */
|
|---|
| 34 |
|
|---|
| 35 | #include <errno.h>
|
|---|
| 36 | #include <io/table.h>
|
|---|
| 37 | #include <loc.h>
|
|---|
| 38 | #include <stdio.h>
|
|---|
| 39 | #include <stdlib.h>
|
|---|
| 40 | #include <str.h>
|
|---|
| 41 | #include <vfs/vfs.h>
|
|---|
| 42 | #include <vol.h>
|
|---|
| 43 |
|
|---|
| 44 | #define NAME "vol"
|
|---|
| 45 |
|
|---|
| 46 | typedef enum {
|
|---|
| 47 | vcmd_eject,
|
|---|
| 48 | vcmd_insert,
|
|---|
| 49 | vcmd_help,
|
|---|
| 50 | vcmd_list,
|
|---|
| 51 | vcmd_cfglist,
|
|---|
| 52 | } vol_cmd_t;
|
|---|
| 53 |
|
|---|
| 54 | /** Find volume by current mount point. */
|
|---|
| 55 | static errno_t vol_cmd_part_by_mp(vol_t *vol, const char *mp,
|
|---|
| 56 | service_id_t *rid)
|
|---|
| 57 | {
|
|---|
| 58 | vol_part_info_t vinfo;
|
|---|
| 59 | service_id_t *part_ids = NULL;
|
|---|
| 60 | char *canon_mp_buf = NULL;
|
|---|
| 61 | char *canon_mp;
|
|---|
| 62 | size_t nparts;
|
|---|
| 63 | size_t i;
|
|---|
| 64 | errno_t rc;
|
|---|
| 65 |
|
|---|
| 66 | canon_mp_buf = str_dup(mp);
|
|---|
| 67 | if (canon_mp_buf == NULL) {
|
|---|
| 68 | printf("Out of memory.\n");
|
|---|
| 69 | rc = ENOMEM;
|
|---|
| 70 | goto out;
|
|---|
| 71 | }
|
|---|
| 72 |
|
|---|
| 73 | canon_mp = vfs_absolutize(canon_mp_buf, NULL);
|
|---|
| 74 | if (canon_mp == NULL) {
|
|---|
| 75 | printf("Invalid volume path '%s'.\n", mp);
|
|---|
| 76 | rc = EINVAL;
|
|---|
| 77 | goto out;
|
|---|
| 78 | }
|
|---|
| 79 |
|
|---|
| 80 | rc = vol_get_parts(vol, &part_ids, &nparts);
|
|---|
| 81 | if (rc != EOK) {
|
|---|
| 82 | printf("Error getting list of volumes.\n");
|
|---|
| 83 | goto out;
|
|---|
| 84 | }
|
|---|
| 85 |
|
|---|
| 86 | for (i = 0; i < nparts; i++) {
|
|---|
| 87 | rc = vol_part_info(vol, part_ids[i], &vinfo);
|
|---|
| 88 | if (rc != EOK) {
|
|---|
| 89 | printf("Error getting volume information.\n");
|
|---|
| 90 | rc = EIO;
|
|---|
| 91 | goto out;
|
|---|
| 92 | }
|
|---|
| 93 |
|
|---|
| 94 | if (str_cmp(vinfo.cur_mp, canon_mp) == 0) {
|
|---|
| 95 | *rid = part_ids[i];
|
|---|
| 96 | rc = EOK;
|
|---|
| 97 | goto out;
|
|---|
| 98 | }
|
|---|
| 99 | }
|
|---|
| 100 |
|
|---|
| 101 | rc = ENOENT;
|
|---|
| 102 | out:
|
|---|
| 103 | free(part_ids);
|
|---|
| 104 | free(canon_mp_buf);
|
|---|
| 105 | return rc;
|
|---|
| 106 | }
|
|---|
| 107 |
|
|---|
| 108 | static errno_t vol_cmd_eject(const char *volspec)
|
|---|
| 109 | {
|
|---|
| 110 | vol_t *vol = NULL;
|
|---|
| 111 | service_id_t part_id;
|
|---|
| 112 | errno_t rc;
|
|---|
| 113 |
|
|---|
| 114 | rc = vol_create(&vol);
|
|---|
| 115 | if (rc != EOK) {
|
|---|
| 116 | printf("Error contacting volume service.\n");
|
|---|
| 117 | goto out;
|
|---|
| 118 | }
|
|---|
| 119 |
|
|---|
| 120 | rc = vol_cmd_part_by_mp(vol, volspec, &part_id);
|
|---|
| 121 | if (rc != EOK) {
|
|---|
| 122 | printf("Error looking up volume '%s'.\n", volspec);
|
|---|
| 123 | goto out;
|
|---|
| 124 | }
|
|---|
| 125 |
|
|---|
| 126 | rc = vol_part_eject(vol, part_id);
|
|---|
| 127 | if (rc != EOK) {
|
|---|
| 128 | printf("Error ejecting volume.\n");
|
|---|
| 129 | goto out;
|
|---|
| 130 | }
|
|---|
| 131 |
|
|---|
| 132 | rc = EOK;
|
|---|
| 133 | out:
|
|---|
| 134 | vol_destroy(vol);
|
|---|
| 135 | return rc;
|
|---|
| 136 | }
|
|---|
| 137 |
|
|---|
| 138 | static errno_t vol_cmd_insert(const char *volspec)
|
|---|
| 139 | {
|
|---|
| 140 | vol_t *vol = NULL;
|
|---|
| 141 | service_id_t svc_id;
|
|---|
| 142 | errno_t rc;
|
|---|
| 143 |
|
|---|
| 144 | rc = loc_service_get_id(volspec, &svc_id, 0);
|
|---|
| 145 | if (rc != EOK) {
|
|---|
| 146 | printf("Error looking up service '%s'.\n", volspec);
|
|---|
| 147 | goto out;
|
|---|
| 148 | }
|
|---|
| 149 |
|
|---|
| 150 | rc = vol_create(&vol);
|
|---|
| 151 | if (rc != EOK) {
|
|---|
| 152 | printf("Error contacting volume service.\n");
|
|---|
| 153 | goto out;
|
|---|
| 154 | }
|
|---|
| 155 |
|
|---|
| 156 | rc = vol_part_insert(vol, svc_id);
|
|---|
| 157 | if (rc != EOK) {
|
|---|
| 158 | printf("Error inserting volume.\n");
|
|---|
| 159 | goto out;
|
|---|
| 160 | }
|
|---|
| 161 |
|
|---|
| 162 | rc = EOK;
|
|---|
| 163 | out:
|
|---|
| 164 | vol_destroy(vol);
|
|---|
| 165 | return rc;
|
|---|
| 166 | }
|
|---|
| 167 |
|
|---|
| 168 | static errno_t vol_cmd_list(void)
|
|---|
| 169 | {
|
|---|
| 170 | vol_t *vol = NULL;
|
|---|
| 171 | vol_part_info_t vinfo;
|
|---|
| 172 | service_id_t *part_ids = NULL;
|
|---|
| 173 | char *svc_name;
|
|---|
| 174 | char *sfstype;
|
|---|
| 175 | size_t nparts;
|
|---|
| 176 | size_t i;
|
|---|
| 177 | table_t *table = NULL;
|
|---|
| 178 | errno_t rc;
|
|---|
| 179 |
|
|---|
| 180 | rc = vol_create(&vol);
|
|---|
| 181 | if (rc != EOK) {
|
|---|
| 182 | printf("Error contacting volume service.\n");
|
|---|
| 183 | goto out;
|
|---|
| 184 | }
|
|---|
| 185 |
|
|---|
| 186 | rc = vol_get_parts(vol, &part_ids, &nparts);
|
|---|
| 187 | if (rc != EOK) {
|
|---|
| 188 | printf("Error getting list of volumes.\n");
|
|---|
| 189 | goto out;
|
|---|
| 190 | }
|
|---|
| 191 |
|
|---|
| 192 | rc = table_create(&table);
|
|---|
| 193 | if (rc != EOK) {
|
|---|
| 194 | printf("Out of memory.\n");
|
|---|
| 195 | goto out;
|
|---|
| 196 | }
|
|---|
| 197 |
|
|---|
| 198 | table_header_row(table);
|
|---|
| 199 | table_printf(table, "Volume Name\t" "Resource\t" "Content\t" "Auto\t"
|
|---|
| 200 | "Mounted at\n");
|
|---|
| 201 |
|
|---|
| 202 | for (i = 0; i < nparts; i++) {
|
|---|
| 203 | rc = vol_part_info(vol, part_ids[i], &vinfo);
|
|---|
| 204 | if (rc != EOK) {
|
|---|
| 205 | printf("Error getting volume information.\n");
|
|---|
| 206 | return EIO;
|
|---|
| 207 | }
|
|---|
| 208 |
|
|---|
| 209 | rc = loc_service_get_name(part_ids[i], &svc_name);
|
|---|
| 210 | if (rc != EOK) {
|
|---|
| 211 | printf("Error getting service name.\n");
|
|---|
| 212 | return EIO;
|
|---|
| 213 | }
|
|---|
| 214 |
|
|---|
| 215 | rc = vol_pcnt_fs_format(vinfo.pcnt, vinfo.fstype, &sfstype);
|
|---|
| 216 | if (rc != EOK) {
|
|---|
| 217 | printf("Out of memory.\n");
|
|---|
| 218 | free(svc_name);
|
|---|
| 219 | return ENOMEM;
|
|---|
| 220 | }
|
|---|
| 221 |
|
|---|
| 222 | table_printf(table, "%s\t" "%s\t" "%s\t" "%s\t" "%s\n",
|
|---|
| 223 | vinfo.label, svc_name, sfstype,
|
|---|
| 224 | vinfo.cur_mp_auto ? "Yes" : "", vinfo.cur_mp);
|
|---|
| 225 |
|
|---|
| 226 | free(svc_name);
|
|---|
| 227 | free(sfstype);
|
|---|
| 228 | svc_name = NULL;
|
|---|
| 229 | }
|
|---|
| 230 |
|
|---|
| 231 | rc = table_print_out(table, stdout);
|
|---|
| 232 | if (rc != EOK)
|
|---|
| 233 | printf("Error printing table.\n");
|
|---|
| 234 | out:
|
|---|
| 235 | table_destroy(table);
|
|---|
| 236 | vol_destroy(vol);
|
|---|
| 237 | free(part_ids);
|
|---|
| 238 |
|
|---|
| 239 | return rc;
|
|---|
| 240 | }
|
|---|
| 241 |
|
|---|
| 242 | /** List volume configuration entries.
|
|---|
| 243 | *
|
|---|
| 244 | * @return EOK on success or an error code
|
|---|
| 245 | */
|
|---|
| 246 | static errno_t vol_cmd_cfglist(void)
|
|---|
| 247 | {
|
|---|
| 248 | vol_t *vol = NULL;
|
|---|
| 249 | vol_info_t vinfo;
|
|---|
| 250 | volume_id_t *volume_ids = NULL;
|
|---|
| 251 | size_t nvols;
|
|---|
| 252 | size_t i;
|
|---|
| 253 | table_t *table = NULL;
|
|---|
| 254 | errno_t rc;
|
|---|
| 255 |
|
|---|
| 256 | rc = vol_create(&vol);
|
|---|
| 257 | if (rc != EOK) {
|
|---|
| 258 | printf("Error contacting volume service.\n");
|
|---|
| 259 | goto out;
|
|---|
| 260 | }
|
|---|
| 261 |
|
|---|
| 262 | rc = vol_get_volumes(vol, &volume_ids, &nvols);
|
|---|
| 263 | if (rc != EOK) {
|
|---|
| 264 | printf("Error getting list of volumes.\n");
|
|---|
| 265 | goto out;
|
|---|
| 266 | }
|
|---|
| 267 |
|
|---|
| 268 | rc = table_create(&table);
|
|---|
| 269 | if (rc != EOK) {
|
|---|
| 270 | printf("Out of memory.\n");
|
|---|
| 271 | goto out;
|
|---|
| 272 | }
|
|---|
| 273 |
|
|---|
| 274 | table_header_row(table);
|
|---|
| 275 | table_printf(table, "Volume Name\t" "Path\n");
|
|---|
| 276 |
|
|---|
| 277 | for (i = 0; i < nvols; i++) {
|
|---|
| 278 | rc = vol_info(vol, volume_ids[i], &vinfo);
|
|---|
| 279 | if (rc != EOK) {
|
|---|
| 280 | printf("Error getting volume information.\n");
|
|---|
| 281 | return EIO;
|
|---|
| 282 | }
|
|---|
| 283 |
|
|---|
| 284 | table_printf(table, "%s\t" "%s\n", vinfo.label, vinfo.path);
|
|---|
| 285 | }
|
|---|
| 286 |
|
|---|
| 287 | rc = table_print_out(table, stdout);
|
|---|
| 288 | if (rc != EOK)
|
|---|
| 289 | printf("Error printing table.\n");
|
|---|
| 290 | out:
|
|---|
| 291 | table_destroy(table);
|
|---|
| 292 | vol_destroy(vol);
|
|---|
| 293 | free(volume_ids);
|
|---|
| 294 |
|
|---|
| 295 | return rc;
|
|---|
| 296 | }
|
|---|
| 297 |
|
|---|
| 298 | static void print_syntax(void)
|
|---|
| 299 | {
|
|---|
| 300 | printf("Syntax:\n");
|
|---|
| 301 | printf(" %s List present volumes\n", NAME);
|
|---|
| 302 | printf(" %s -c List volume configuration entries\n", NAME);
|
|---|
| 303 | printf(" %s -h Print help\n", NAME);
|
|---|
| 304 | printf(" %s eject <mp> Eject volume mounted in a directory\n", NAME);
|
|---|
| 305 | printf(" %s insert <svc> Insert volume based on service identifier\n", NAME);
|
|---|
| 306 | }
|
|---|
| 307 |
|
|---|
| 308 | int main(int argc, char *argv[])
|
|---|
| 309 | {
|
|---|
| 310 | char *cmd;
|
|---|
| 311 | char *volspec;
|
|---|
| 312 | vol_cmd_t vcmd;
|
|---|
| 313 | int i;
|
|---|
| 314 | errno_t rc = EINVAL;
|
|---|
| 315 |
|
|---|
| 316 | if (argc < 2) {
|
|---|
| 317 | vcmd = vcmd_list;
|
|---|
| 318 | i = 1;
|
|---|
| 319 | } else {
|
|---|
| 320 | cmd = argv[1];
|
|---|
| 321 | i = 2;
|
|---|
| 322 | if (str_cmp(cmd, "-h") == 0) {
|
|---|
| 323 | vcmd = vcmd_help;
|
|---|
| 324 | } else if (str_cmp(cmd, "-c") == 0) {
|
|---|
| 325 | vcmd = vcmd_cfglist;
|
|---|
| 326 | } else if (str_cmp(cmd, "eject") == 0) {
|
|---|
| 327 | vcmd = vcmd_eject;
|
|---|
| 328 | if (argc <= i) {
|
|---|
| 329 | printf("Parameter missing.\n");
|
|---|
| 330 | goto syntax_error;
|
|---|
| 331 | }
|
|---|
| 332 | volspec = argv[i++];
|
|---|
| 333 | } else if (str_cmp(cmd, "insert") == 0) {
|
|---|
| 334 | vcmd = vcmd_insert;
|
|---|
| 335 | if (argc <= i) {
|
|---|
| 336 | printf("Parameter missing.\n");
|
|---|
| 337 | goto syntax_error;
|
|---|
| 338 | }
|
|---|
| 339 | volspec = argv[i++];
|
|---|
| 340 | } else {
|
|---|
| 341 | printf("Invalid sub-command '%s'.\n", cmd);
|
|---|
| 342 | goto syntax_error;
|
|---|
| 343 | }
|
|---|
| 344 | }
|
|---|
| 345 |
|
|---|
| 346 | if (argc > i) {
|
|---|
| 347 | printf("Unexpected argument '%s'.\n", argv[i]);
|
|---|
| 348 | goto syntax_error;
|
|---|
| 349 | }
|
|---|
| 350 |
|
|---|
| 351 | switch (vcmd) {
|
|---|
| 352 | case vcmd_eject:
|
|---|
| 353 | rc = vol_cmd_eject(volspec);
|
|---|
| 354 | break;
|
|---|
| 355 | case vcmd_insert:
|
|---|
| 356 | rc = vol_cmd_insert(volspec);
|
|---|
| 357 | break;
|
|---|
| 358 | case vcmd_help:
|
|---|
| 359 | print_syntax();
|
|---|
| 360 | rc = EOK;
|
|---|
| 361 | break;
|
|---|
| 362 | case vcmd_list:
|
|---|
| 363 | rc = vol_cmd_list();
|
|---|
| 364 | break;
|
|---|
| 365 | case vcmd_cfglist:
|
|---|
| 366 | rc = vol_cmd_cfglist();
|
|---|
| 367 | break;
|
|---|
| 368 | }
|
|---|
| 369 |
|
|---|
| 370 | if (rc != EOK)
|
|---|
| 371 | return 1;
|
|---|
| 372 |
|
|---|
| 373 | return 0;
|
|---|
| 374 |
|
|---|
| 375 | syntax_error:
|
|---|
| 376 | printf("Use %s -h to get help.\n", NAME);
|
|---|
| 377 | return 1;
|
|---|
| 378 | }
|
|---|
| 379 |
|
|---|
| 380 | /** @}
|
|---|
| 381 | */
|
|---|