diff options
author | Denys Vlasenko <vda.linux@googlemail.com> | 2011-09-12 02:13:47 +0200 |
---|---|---|
committer | Denys Vlasenko <vda.linux@googlemail.com> | 2011-09-12 08:30:16 +0200 |
commit | 13e709c53f700a18a660feebdf72c613a233bf48 (patch) | |
tree | 248b913b7bb6629b751bbd5a79a7e3c6b55dd1af | |
parent | dd1061b6a79b0161597799e825bfefc27993ace5 (diff) | |
download | busybox-w32-13e709c53f700a18a660feebdf72c613a233bf48.tar.gz busybox-w32-13e709c53f700a18a660feebdf72c613a233bf48.tar.bz2 busybox-w32-13e709c53f700a18a660feebdf72c613a233bf48.zip |
losetup: implement -r option. Closes 4033.
function old new delta
packed_usage 28595 28633 +38
losetup_main 285 290 +5
singlemount 906 908 +2
set_loop 674 672 -2
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
-rw-r--r-- | include/libbb.h | 2 | ||||
-rw-r--r-- | libbb/loop.c | 10 | ||||
-rw-r--r-- | util-linux/losetup.c | 16 | ||||
-rw-r--r-- | util-linux/mount.c | 2 |
4 files changed, 17 insertions, 13 deletions
diff --git a/include/libbb.h b/include/libbb.h index 21cbe1cac..feae85259 100644 --- a/include/libbb.h +++ b/include/libbb.h | |||
@@ -1164,7 +1164,7 @@ extern int del_loop(const char *device) FAST_FUNC; | |||
1164 | /* If *devname is not NULL, use that name, otherwise try to find free one, | 1164 | /* If *devname is not NULL, use that name, otherwise try to find free one, |
1165 | * malloc and return it in *devname. | 1165 | * malloc and return it in *devname. |
1166 | * return value: 1: read-only loopdev was setup, 0: rw, < 0: error */ | 1166 | * return value: 1: read-only loopdev was setup, 0: rw, < 0: error */ |
1167 | extern int set_loop(char **devname, const char *file, unsigned long long offset) FAST_FUNC; | 1167 | extern int set_loop(char **devname, const char *file, unsigned long long offset, int ro) FAST_FUNC; |
1168 | 1168 | ||
1169 | /* Like bb_ask below, but asks on stdin with no timeout. */ | 1169 | /* Like bb_ask below, but asks on stdin with no timeout. */ |
1170 | char *bb_ask_stdin(const char * prompt) FAST_FUNC; | 1170 | char *bb_ask_stdin(const char * prompt) FAST_FUNC; |
diff --git a/libbb/loop.c b/libbb/loop.c index b798932fa..b3a520848 100644 --- a/libbb/loop.c +++ b/libbb/loop.c | |||
@@ -84,7 +84,7 @@ int FAST_FUNC del_loop(const char *device) | |||
84 | search will re-use an existing loop device already bound to that | 84 | search will re-use an existing loop device already bound to that |
85 | file/offset if it finds one. | 85 | file/offset if it finds one. |
86 | */ | 86 | */ |
87 | int FAST_FUNC set_loop(char **device, const char *file, unsigned long long offset) | 87 | int FAST_FUNC set_loop(char **device, const char *file, unsigned long long offset, int ro) |
88 | { | 88 | { |
89 | char dev[LOOP_NAMESIZE]; | 89 | char dev[LOOP_NAMESIZE]; |
90 | char *try; | 90 | char *try; |
@@ -93,11 +93,13 @@ int FAST_FUNC set_loop(char **device, const char *file, unsigned long long offse | |||
93 | int i, dfd, ffd, mode, rc = -1; | 93 | int i, dfd, ffd, mode, rc = -1; |
94 | 94 | ||
95 | /* Open the file. Barf if this doesn't work. */ | 95 | /* Open the file. Barf if this doesn't work. */ |
96 | mode = O_RDWR; | 96 | mode = ro ? O_RDONLY : O_RDWR; |
97 | ffd = open(file, mode); | 97 | ffd = open(file, mode); |
98 | if (ffd < 0) { | 98 | if (ffd < 0) { |
99 | mode = O_RDONLY; | 99 | if (mode != O_RDONLY) { |
100 | ffd = open(file, mode); | 100 | mode = O_RDONLY; |
101 | ffd = open(file, mode); | ||
102 | } | ||
101 | if (ffd < 0) | 103 | if (ffd < 0) |
102 | return -errno; | 104 | return -errno; |
103 | } | 105 | } |
diff --git a/util-linux/losetup.c b/util-linux/losetup.c index 9b7c49f50..21108d0bf 100644 --- a/util-linux/losetup.c +++ b/util-linux/losetup.c | |||
@@ -8,11 +8,12 @@ | |||
8 | */ | 8 | */ |
9 | 9 | ||
10 | //usage:#define losetup_trivial_usage | 10 | //usage:#define losetup_trivial_usage |
11 | //usage: "[-o OFS] LOOPDEV FILE - associate loop devices\n" | 11 | //usage: "[-r] [-o OFS] 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 [-f] - show" |
14 | //usage:#define losetup_full_usage "\n\n" | 14 | //usage:#define losetup_full_usage "\n\n" |
15 | //usage: " -o OFS Start OFS bytes into FILE" | 15 | //usage: " -o OFS Start OFS bytes into FILE" |
16 | //usage: "\n -r Read-only" | ||
16 | //usage: "\n -f Show first free loop device" | 17 | //usage: "\n -f Show first free loop device" |
17 | //usage: | 18 | //usage: |
18 | //usage:#define losetup_notes_usage | 19 | //usage:#define losetup_notes_usage |
@@ -37,11 +38,12 @@ int losetup_main(int argc UNUSED_PARAM, char **argv) | |||
37 | OPT_d = (1 << 0), | 38 | OPT_d = (1 << 0), |
38 | OPT_o = (1 << 1), | 39 | OPT_o = (1 << 1), |
39 | OPT_f = (1 << 2), | 40 | OPT_f = (1 << 2), |
41 | OPT_r = (1 << 3), /* must be last */ | ||
40 | }; | 42 | }; |
41 | 43 | ||
42 | /* max 2 args, all opts are mutually exclusive */ | 44 | /* max 2 args, -d,-o,-f opts are mutually exclusive */ |
43 | opt_complementary = "?2:d--of:o--df:f--do"; | 45 | opt_complementary = "?2:d--of:o--df:f--do"; |
44 | opt = getopt32(argv, "do:f", &opt_o); | 46 | opt = getopt32(argv, "do:fr", &opt_o); |
45 | argv += optind; | 47 | argv += optind; |
46 | 48 | ||
47 | if (opt == OPT_o) | 49 | if (opt == OPT_o) |
@@ -63,12 +65,12 @@ int losetup_main(int argc UNUSED_PARAM, char **argv) | |||
63 | bb_show_usage(); | 65 | bb_show_usage(); |
64 | 66 | ||
65 | if (argv[1]) { | 67 | if (argv[1]) { |
66 | /* [-o OFS] BLOCKDEV FILE */ | 68 | /* [-r] [-o OFS] BLOCKDEV FILE */ |
67 | if (set_loop(&argv[0], argv[1], offset) < 0) | 69 | if (set_loop(&argv[0], argv[1], offset, (opt / OPT_r)) < 0) |
68 | bb_simple_perror_msg_and_die(argv[0]); | 70 | bb_simple_perror_msg_and_die(argv[0]); |
69 | return EXIT_SUCCESS; | 71 | return EXIT_SUCCESS; |
70 | } | 72 | } |
71 | /* [-o OFS] BLOCKDEV */ | 73 | /* [-r] [-o OFS] BLOCKDEV */ |
72 | s = query_loop(argv[0]); | 74 | s = query_loop(argv[0]); |
73 | if (!s) | 75 | if (!s) |
74 | bb_simple_perror_msg_and_die(argv[0]); | 76 | bb_simple_perror_msg_and_die(argv[0]); |
@@ -78,7 +80,7 @@ int losetup_main(int argc UNUSED_PARAM, char **argv) | |||
78 | return EXIT_SUCCESS; | 80 | return EXIT_SUCCESS; |
79 | } | 81 | } |
80 | 82 | ||
81 | /* [-o OFS|-f] with no params */ | 83 | /* [-r] [-o OFS|-f] with no params */ |
82 | n = 0; | 84 | n = 0; |
83 | while (1) { | 85 | while (1) { |
84 | char *s; | 86 | char *s; |
diff --git a/util-linux/mount.c b/util-linux/mount.c index 05e532cda..b51ab1782 100644 --- a/util-linux/mount.c +++ b/util-linux/mount.c | |||
@@ -1809,7 +1809,7 @@ static int singlemount(struct mntent *mp, int ignore_busy) | |||
1809 | if (ENABLE_FEATURE_MOUNT_LOOP && S_ISREG(st.st_mode)) { | 1809 | if (ENABLE_FEATURE_MOUNT_LOOP && S_ISREG(st.st_mode)) { |
1810 | loopFile = bb_simplify_path(mp->mnt_fsname); | 1810 | loopFile = bb_simplify_path(mp->mnt_fsname); |
1811 | mp->mnt_fsname = NULL; // will receive malloced loop dev name | 1811 | mp->mnt_fsname = NULL; // will receive malloced loop dev name |
1812 | if (set_loop(&mp->mnt_fsname, loopFile, 0) < 0) { | 1812 | if (set_loop(&mp->mnt_fsname, loopFile, 0, /*ro:*/ 0) < 0) { |
1813 | if (errno == EPERM || errno == EACCES) | 1813 | if (errno == EPERM || errno == EACCES) |
1814 | bb_error_msg(bb_msg_perm_denied_are_you_root); | 1814 | bb_error_msg(bb_msg_perm_denied_are_you_root); |
1815 | else | 1815 | else |