aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--util-linux/losetup.c116
1 files changed, 65 insertions, 51 deletions
diff --git a/util-linux/losetup.c b/util-linux/losetup.c
index 21108d0bf..c69763335 100644
--- a/util-linux/losetup.c
+++ b/util-linux/losetup.c
@@ -8,16 +8,16 @@
8 */ 8 */
9 9
10//usage:#define losetup_trivial_usage 10//usage:#define losetup_trivial_usage
11//usage: "[-r] [-o OFS] LOOPDEV FILE - associate loop devices\n" 11//usage: "[-r] [-o OFS] {-f|LOOPDEV} FILE - associate loop devices\n"
12//usage: " losetup -d LOOPDEV - disassociate\n" 12//usage: " losetup -d LOOPDEV - disassociate\n"
13//usage: " losetup [-f] - show" 13//usage: " losetup -a - show status of all\n"
14//usage: " losetup -f - show next available"
14//usage:#define losetup_full_usage "\n\n" 15//usage:#define losetup_full_usage "\n\n"
15//usage: " -o OFS Start OFS bytes into FILE" 16//usage: " -o OFS Start OFS bytes into FILE"
16//usage: "\n -r Read-only" 17//usage: "\n -r Read-only"
17//usage: "\n -f Show first free loop device" 18//usage: "\n -f Show/find first free loop device"
18//usage: 19//usage:
19//usage:#define losetup_notes_usage 20//usage:#define losetup_notes_usage
20//usage: "No arguments will display all current associations.\n"
21//usage: "One argument (losetup /dev/loop1) will display the current association\n" 21//usage: "One argument (losetup /dev/loop1) will display the current association\n"
22//usage: "(if any), or disassociate it (with -d). The display shows the offset\n" 22//usage: "(if any), or disassociate it (with -d). The display shows the offset\n"
23//usage: "and filename of the file the loop device is currently bound to.\n\n" 23//usage: "and filename of the file the loop device is currently bound to.\n\n"
@@ -31,77 +31,91 @@ int losetup_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
31int losetup_main(int argc UNUSED_PARAM, char **argv) 31int losetup_main(int argc UNUSED_PARAM, char **argv)
32{ 32{
33 unsigned opt; 33 unsigned opt;
34 int n;
35 char *opt_o; 34 char *opt_o;
36 unsigned long long offset = 0; 35 char dev[LOOP_NAMESIZE];
37 enum { 36 enum {
38 OPT_d = (1 << 0), 37 OPT_d = (1 << 0),
39 OPT_o = (1 << 1), 38 OPT_o = (1 << 1),
40 OPT_f = (1 << 2), 39 OPT_f = (1 << 2),
41 OPT_r = (1 << 3), /* must be last */ 40 OPT_a = (1 << 3),
41 OPT_r = (1 << 4), /* must be last */
42 }; 42 };
43 43
44 /* max 2 args, -d,-o,-f opts are mutually exclusive */ 44 opt_complementary = "?2:d--ofar:a--ofr";
45 opt_complementary = "?2:d--of:o--df:f--do"; 45 opt = getopt32(argv, "do:far", &opt_o);
46 opt = getopt32(argv, "do:fr", &opt_o);
47 argv += optind; 46 argv += optind;
48 47
49 if (opt == OPT_o) 48 /* LOOPDEV */
50 offset = xatoull(opt_o); 49 if (!opt && argv[0] && !argv[1]) {
50 char *s;
51 51
52 s = query_loop(argv[0]);
53 if (!s)
54 bb_simple_perror_msg_and_die(argv[0]);
55 printf("%s: %s\n", argv[0], s);
56 if (ENABLE_FEATURE_CLEAN_UP)
57 free(s);
58 return EXIT_SUCCESS;
59 }
60
61 /* -d LOOPDEV */
52 if (opt == OPT_d) { 62 if (opt == OPT_d) {
53 /* -d BLOCKDEV */
54 if (!argv[0] || argv[1])
55 bb_show_usage();
56 if (del_loop(argv[0])) 63 if (del_loop(argv[0]))
57 bb_simple_perror_msg_and_die(argv[0]); 64 bb_simple_perror_msg_and_die(argv[0]);
58 return EXIT_SUCCESS; 65 return EXIT_SUCCESS;
59 } 66 }
60 67
61 if (argv[0]) { 68 /* -a */
62 char *s; 69 if (opt == OPT_a) {
63 70 int n;
64 if (opt == OPT_f) /* -f should not have arguments */ 71 for (n = 0; n < 10; n++) {
65 bb_show_usage(); 72 char *s;
66 73
67 if (argv[1]) { 74 sprintf(dev, LOOP_FORMAT, n);
68 /* [-r] [-o OFS] BLOCKDEV FILE */ 75 s = query_loop(dev);
69 if (set_loop(&argv[0], argv[1], offset, (opt / OPT_r)) < 0) 76 if (s) {
70 bb_simple_perror_msg_and_die(argv[0]); 77 printf("%s: %s\n", dev, s);
71 return EXIT_SUCCESS; 78 if (ENABLE_FEATURE_CLEAN_UP)
79 free(s);
80 }
72 } 81 }
73 /* [-r] [-o OFS] BLOCKDEV */
74 s = query_loop(argv[0]);
75 if (!s)
76 bb_simple_perror_msg_and_die(argv[0]);
77 printf("%s: %s\n", argv[0], s);
78 if (ENABLE_FEATURE_CLEAN_UP)
79 free(s);
80 return EXIT_SUCCESS; 82 return EXIT_SUCCESS;
81 } 83 }
82 84
83 /* [-r] [-o OFS|-f] with no params */ 85 /* contains -f */
84 n = 0; 86 if (opt & OPT_f) {
85 while (1) {
86 char *s; 87 char *s;
87 char dev[LOOP_NAMESIZE]; 88 int n = 0;
88 89
89 sprintf(dev, LOOP_FORMAT, n); 90 do {
90 s = query_loop(dev); 91 sprintf(dev, LOOP_FORMAT, n);
91 n++; 92 s = query_loop(dev);
92 if (!s) { 93 if (s && ENABLE_FEATURE_CLEAN_UP)
93 if (n > 9 && errno && errno != ENXIO)
94 return EXIT_SUCCESS;
95 if (opt == OPT_f) {
96 puts(dev);
97 return EXIT_SUCCESS;
98 }
99 } else {
100 if (opt != OPT_f)
101 printf("%s: %s\n", dev, s);
102 if (ENABLE_FEATURE_CLEAN_UP)
103 free(s); 94 free(s);
95 } while (s);
96 if ((opt == OPT_f) && !argv[0]) {
97 puts(dev);
98 return EXIT_SUCCESS;
104 } 99 }
105 } 100 }
106 return EXIT_SUCCESS; 101
102 /* [-r] [-o OFS] {-f|LOOPDEV} FILE */
103 if (argv[0] && ((opt & OPT_f) || argv[1])) {
104 unsigned long long offset = 0;
105 char *d = dev;
106
107 if (opt == OPT_o)
108 offset = xatoull(opt_o);
109 if (opt != OPT_f)
110 d = *(argv++);
111
112 if (argv[0]) {
113 if (set_loop(&d, argv[0], offset, (opt / OPT_r)) < 0)
114 bb_simple_perror_msg_and_die(argv[0]);
115 return EXIT_SUCCESS;
116 }
117 }
118
119 bb_show_usage();
120 return EXIT_FAILURE;
107} 121}