[3320] | 1 | /* vi: set sw=4 ts=4: */
|
---|
| 2 | /*
|
---|
| 3 | * Mini losetup implementation for busybox
|
---|
| 4 | *
|
---|
| 5 | * Copyright (C) 2002 Matt Kraai.
|
---|
| 6 | *
|
---|
| 7 | * Licensed under GPLv2 or later, see file LICENSE in this source tree.
|
---|
| 8 | */
|
---|
| 9 |
|
---|
| 10 | #include "libbb.h"
|
---|
| 11 |
|
---|
| 12 | int losetup_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
|
---|
| 13 | int losetup_main(int argc UNUSED_PARAM, char **argv)
|
---|
| 14 | {
|
---|
| 15 | unsigned opt;
|
---|
| 16 | int n;
|
---|
| 17 | char *opt_o;
|
---|
| 18 | unsigned long long offset = 0;
|
---|
| 19 | enum {
|
---|
| 20 | OPT_d = (1 << 0),
|
---|
| 21 | OPT_o = (1 << 1),
|
---|
| 22 | OPT_f = (1 << 2),
|
---|
| 23 | };
|
---|
| 24 |
|
---|
| 25 | /* max 2 args, all opts are mutually exclusive */
|
---|
| 26 | opt_complementary = "?2:d--of:o--df:f--do";
|
---|
| 27 | opt = getopt32(argv, "do:f", &opt_o);
|
---|
| 28 | argv += optind;
|
---|
| 29 |
|
---|
| 30 | if (opt == OPT_o)
|
---|
| 31 | offset = xatoull(opt_o);
|
---|
| 32 |
|
---|
| 33 | if (opt == OPT_d) {
|
---|
| 34 | /* -d BLOCKDEV */
|
---|
| 35 | if (!argv[0] || argv[1])
|
---|
| 36 | bb_show_usage();
|
---|
| 37 | if (del_loop(argv[0]))
|
---|
| 38 | bb_simple_perror_msg_and_die(argv[0]);
|
---|
| 39 | return EXIT_SUCCESS;
|
---|
| 40 | }
|
---|
| 41 |
|
---|
| 42 | if (argv[0]) {
|
---|
| 43 | char *s;
|
---|
| 44 |
|
---|
| 45 | if (opt == OPT_f) /* -f should not have arguments */
|
---|
| 46 | bb_show_usage();
|
---|
| 47 |
|
---|
| 48 | if (argv[1]) {
|
---|
| 49 | /* [-o OFS] BLOCKDEV FILE */
|
---|
| 50 | if (set_loop(&argv[0], argv[1], offset) < 0)
|
---|
| 51 | bb_simple_perror_msg_and_die(argv[0]);
|
---|
| 52 | return EXIT_SUCCESS;
|
---|
| 53 | }
|
---|
| 54 | /* [-o OFS] BLOCKDEV */
|
---|
| 55 | s = query_loop(argv[0]);
|
---|
| 56 | if (!s)
|
---|
| 57 | bb_simple_perror_msg_and_die(argv[0]);
|
---|
| 58 | printf("%s: %s\n", argv[0], s);
|
---|
| 59 | if (ENABLE_FEATURE_CLEAN_UP)
|
---|
| 60 | free(s);
|
---|
| 61 | return EXIT_SUCCESS;
|
---|
| 62 | }
|
---|
| 63 |
|
---|
| 64 | /* [-o OFS|-f] with no params */
|
---|
| 65 | n = 0;
|
---|
| 66 | while (1) {
|
---|
| 67 | char *s;
|
---|
| 68 | char dev[LOOP_NAMESIZE];
|
---|
| 69 |
|
---|
| 70 | sprintf(dev, LOOP_FORMAT, n);
|
---|
| 71 | s = query_loop(dev);
|
---|
| 72 | n++;
|
---|
| 73 | if (!s) {
|
---|
| 74 | if (n > 9 && errno && errno != ENXIO)
|
---|
| 75 | return EXIT_SUCCESS;
|
---|
| 76 | if (opt == OPT_f) {
|
---|
| 77 | puts(dev);
|
---|
| 78 | return EXIT_SUCCESS;
|
---|
| 79 | }
|
---|
| 80 | } else {
|
---|
| 81 | if (opt != OPT_f)
|
---|
| 82 | printf("%s: %s\n", dev, s);
|
---|
| 83 | if (ENABLE_FEATURE_CLEAN_UP)
|
---|
| 84 | free(s);
|
---|
| 85 | }
|
---|
| 86 | }
|
---|
| 87 | return EXIT_SUCCESS;
|
---|
| 88 | }
|
---|