aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorvda <vda@69ca8d6d-28ef-0310-b511-8ec308f3f277>2007-04-19 00:09:34 +0000
committervda <vda@69ca8d6d-28ef-0310-b511-8ec308f3f277>2007-04-19 00:09:34 +0000
commitf3ed3d787ee3079c43760fe1ea1475cffdc1d8bb (patch)
treecd86426279c964e2960cbe51c4bf0e1e97263492
parentb411532a0827ceda4b69406b07d72c1ad1e4098d (diff)
downloadbusybox-w32-f3ed3d787ee3079c43760fe1ea1475cffdc1d8bb.tar.gz
busybox-w32-f3ed3d787ee3079c43760fe1ea1475cffdc1d8bb.tar.bz2
busybox-w32-f3ed3d787ee3079c43760fe1ea1475cffdc1d8bb.zip
loop device code: readability improvement
git-svn-id: svn://busybox.net/trunk/busybox@18494 69ca8d6d-28ef-0310-b511-8ec308f3f277
-rw-r--r--libbb/loop.c24
-rw-r--r--util-linux/losetup.c9
2 files changed, 20 insertions, 13 deletions
diff --git a/libbb/loop.c b/libbb/loop.c
index 9818a30cb..9559d429a 100644
--- a/libbb/loop.c
+++ b/libbb/loop.c
@@ -84,7 +84,7 @@ int set_loop(char **device, const char *file, unsigned long long offset)
84 char dev[20], *try; 84 char dev[20], *try;
85 bb_loop_info loopinfo; 85 bb_loop_info loopinfo;
86 struct stat statbuf; 86 struct stat statbuf;
87 int i, dfd, ffd, mode, rc=-1; 87 int i, dfd, ffd, mode, rc = -1;
88 88
89 /* Open the file. Barf if this doesn't work. */ 89 /* Open the file. Barf if this doesn't work. */
90 mode = O_RDWR; 90 mode = O_RDWR;
@@ -98,12 +98,12 @@ int set_loop(char **device, const char *file, unsigned long long offset)
98 98
99 /* Find a loop device. */ 99 /* Find a loop device. */
100 try = *device ? : dev; 100 try = *device ? : dev;
101 for (i=0;rc;i++) { 101 for (i = 0; rc; i++) {
102 sprintf(dev, LOOP_FORMAT, i); 102 sprintf(dev, LOOP_FORMAT, i);
103 103
104 /* Ran out of block devices, return failure. */ 104 /* Ran out of block devices, return failure. */
105 if (stat(try, &statbuf) || !S_ISBLK(statbuf.st_mode)) { 105 if (stat(try, &statbuf) || !S_ISBLK(statbuf.st_mode)) {
106 rc=-ENOENT; 106 rc = -ENOENT;
107 break; 107 break;
108 } 108 }
109 /* Open the sucker and check its loopiness. */ 109 /* Open the sucker and check its loopiness. */
@@ -112,19 +112,22 @@ int set_loop(char **device, const char *file, unsigned long long offset)
112 mode = O_RDONLY; 112 mode = O_RDONLY;
113 dfd = open(try, mode); 113 dfd = open(try, mode);
114 } 114 }
115 if (dfd < 0) goto try_again; 115 if (dfd < 0)
116 goto try_again;
116 117
117 rc = ioctl(dfd, BB_LOOP_GET_STATUS, &loopinfo); 118 rc = ioctl(dfd, BB_LOOP_GET_STATUS, &loopinfo);
118 119
119 /* If device free, claim it. */ 120 /* If device is free, claim it. */
120 if (rc && errno == ENXIO) { 121 if (rc && errno == ENXIO) {
121 memset(&loopinfo, 0, sizeof(loopinfo)); 122 memset(&loopinfo, 0, sizeof(loopinfo));
122 safe_strncpy((char *)loopinfo.lo_file_name, file, LO_NAME_SIZE); 123 safe_strncpy((char *)loopinfo.lo_file_name, file, LO_NAME_SIZE);
123 loopinfo.lo_offset = offset; 124 loopinfo.lo_offset = offset;
124 /* Associate free loop device with file. */ 125 /* Associate free loop device with file. */
125 if (!ioctl(dfd, LOOP_SET_FD, ffd)) { 126 if (!ioctl(dfd, LOOP_SET_FD, ffd)) {
126 if (!ioctl(dfd, BB_LOOP_SET_STATUS, &loopinfo)) rc = 0; 127 if (!ioctl(dfd, BB_LOOP_SET_STATUS, &loopinfo))
127 else ioctl(dfd, LOOP_CLR_FD, 0); 128 rc = 0;
129 else
130 ioctl(dfd, LOOP_CLR_FD, 0);
128 } 131 }
129 132
130 /* If this block device already set up right, re-use it. 133 /* If this block device already set up right, re-use it.
@@ -132,7 +135,7 @@ int set_loop(char **device, const char *file, unsigned long long offset)
132 file isn't pretty either. In general, mounting the same file twice 135 file isn't pretty either. In general, mounting the same file twice
133 without using losetup manually is problematic.) 136 without using losetup manually is problematic.)
134 */ 137 */
135 } else if (strcmp(file,(char *)loopinfo.lo_file_name) 138 } else if (strcmp(file, (char *)loopinfo.lo_file_name) != 0
136 || offset != loopinfo.lo_offset) { 139 || offset != loopinfo.lo_offset) {
137 rc = -1; 140 rc = -1;
138 } 141 }
@@ -142,8 +145,9 @@ try_again:
142 } 145 }
143 close(ffd); 146 close(ffd);
144 if (!rc) { 147 if (!rc) {
145 if (!*device) *device = xstrdup(dev); 148 if (!*device)
146 return mode==O_RDONLY ? 1 : 0; 149 *device = xstrdup(dev);
150 return (mode == O_RDONLY) ? 1 : 0;
147 } 151 }
148 return rc; 152 return rc;
149} 153}
diff --git a/util-linux/losetup.c b/util-linux/losetup.c
index b220c9878..b1e4b08b0 100644
--- a/util-linux/losetup.c
+++ b/util-linux/losetup.c
@@ -45,9 +45,11 @@ int losetup_main(int argc, char **argv)
45 bb_perror_nomsg_and_die(); 45 bb_perror_nomsg_and_die();
46 } else if (argc == 1) { 46 } else if (argc == 1) {
47 char *s = query_loop(argv[0]); 47 char *s = query_loop(argv[0]);
48 if (!s) bb_perror_nomsg_and_die(); 48 if (!s)
49 bb_perror_nomsg_and_die();
49 printf("%s: %s\n", argv[0], s); 50 printf("%s: %s\n", argv[0], s);
50 if (ENABLE_FEATURE_CLEAN_UP) free(s); 51 if (ENABLE_FEATURE_CLEAN_UP)
52 free(s);
51 } else { 53 } else {
52 char dev[sizeof(LOOP_NAME"0")] = LOOP_NAME"0"; 54 char dev[sizeof(LOOP_NAME"0")] = LOOP_NAME"0";
53 char c; 55 char c;
@@ -57,7 +59,8 @@ int losetup_main(int argc, char **argv)
57 s = query_loop(dev); 59 s = query_loop(dev);
58 if (s) { 60 if (s) {
59 printf("%s: %s\n", dev, s); 61 printf("%s: %s\n", dev, s);
60 if (ENABLE_FEATURE_CLEAN_UP) free(s); 62 if (ENABLE_FEATURE_CLEAN_UP)
63 free(s);
61 } 64 }
62 } 65 }
63 } 66 }