aboutsummaryrefslogtreecommitdiff
path: root/libbb/loop.c
diff options
context:
space:
mode:
authorvda <vda@69ca8d6d-28ef-0310-b511-8ec308f3f277>2006-10-08 12:49:22 +0000
committervda <vda@69ca8d6d-28ef-0310-b511-8ec308f3f277>2006-10-08 12:49:22 +0000
commit87d25a2b8535dc627a02eb539fa3946be2a24647 (patch)
treefc4d14a910593d1235318bb36abe5e9f72d2039e /libbb/loop.c
parent81177b14907e73f11560f69e0b4ec34371f1a7d5 (diff)
downloadbusybox-w32-87d25a2b8535dc627a02eb539fa3946be2a24647.tar.gz
busybox-w32-87d25a2b8535dc627a02eb539fa3946be2a24647.tar.bz2
busybox-w32-87d25a2b8535dc627a02eb539fa3946be2a24647.zip
attempt to regularize atoi mess.
git-svn-id: svn://busybox.net/trunk/busybox@16342 69ca8d6d-28ef-0310-b511-8ec308f3f277
Diffstat (limited to 'libbb/loop.c')
-rw-r--r--libbb/loop.c60
1 files changed, 36 insertions, 24 deletions
diff --git a/libbb/loop.c b/libbb/loop.c
index d22b39800..1b296d99b 100644
--- a/libbb/loop.c
+++ b/libbb/loop.c
@@ -48,11 +48,12 @@ char *query_loop(const char *device)
48{ 48{
49 int fd; 49 int fd;
50 bb_loop_info loopinfo; 50 bb_loop_info loopinfo;
51 char *dev=0; 51 char *dev = 0;
52 52
53 if ((fd = open(device, O_RDONLY)) < 0) return 0; 53 fd = open(device, O_RDONLY);
54 if (fd < 0) return 0;
54 if (!ioctl(fd, BB_LOOP_GET_STATUS, &loopinfo)) 55 if (!ioctl(fd, BB_LOOP_GET_STATUS, &loopinfo))
55 dev=xasprintf("%ld %s", (long) loopinfo.lo_offset, 56 dev = xasprintf("%ld %s", (long) loopinfo.lo_offset,
56 (char *)loopinfo.lo_file_name); 57 (char *)loopinfo.lo_file_name);
57 close(fd); 58 close(fd);
58 59
@@ -64,8 +65,9 @@ int del_loop(const char *device)
64{ 65{
65 int fd, rc; 66 int fd, rc;
66 67
67 if ((fd = open(device, O_RDONLY)) < 0) return 1; 68 fd = open(device, O_RDONLY);
68 rc=ioctl(fd, LOOP_CLR_FD, 0); 69 if (fd < 0) return 1;
70 rc = ioctl(fd, LOOP_CLR_FD, 0);
69 close(fd); 71 close(fd);
70 72
71 return rc; 73 return rc;
@@ -77,7 +79,7 @@ int del_loop(const char *device)
77 search will re-use an existing loop device already bound to that 79 search will re-use an existing loop device already bound to that
78 file/offset if it finds one. 80 file/offset if it finds one.
79 */ 81 */
80int set_loop(char **device, const char *file, int offset) 82int set_loop(char **device, const char *file, unsigned long long offset)
81{ 83{
82 char dev[20], *try; 84 char dev[20], *try;
83 bb_loop_info loopinfo; 85 bb_loop_info loopinfo;
@@ -85,34 +87,43 @@ int set_loop(char **device, const char *file, int offset)
85 int i, dfd, ffd, mode, rc=-1; 87 int i, dfd, ffd, mode, rc=-1;
86 88
87 /* Open the file. Barf if this doesn't work. */ 89 /* Open the file. Barf if this doesn't work. */
88 if((ffd = open(file, mode=O_RDWR))<0 && (ffd = open(file,mode=O_RDONLY))<0) 90 mode = O_RDWR;
89 return -errno; 91 ffd = open(file, mode);
92 if (ffd < 0) {
93 mode = O_RDONLY;
94 ffd = open(file, mode);
95 if (ffd < 0)
96 return -errno;
97 }
90 98
91 /* Find a loop device. */ 99 /* Find a loop device. */
92 try=*device ? : dev; 100 try = *device ? : dev;
93 for(i=0;rc;i++) { 101 for (i=0;rc;i++) {
94 sprintf(dev, LOOP_FORMAT, i); 102 sprintf(dev, LOOP_FORMAT, i);
95 103
96 /* Ran out of block devices, return failure. */ 104 /* Ran out of block devices, return failure. */
97 if(stat(try, &statbuf) || !S_ISBLK(statbuf.st_mode)) { 105 if (stat(try, &statbuf) || !S_ISBLK(statbuf.st_mode)) {
98 rc=-ENOENT; 106 rc=-ENOENT;
99 break; 107 break;
100 } 108 }
101 /* Open the sucker and check its loopiness. */ 109 /* Open the sucker and check its loopiness. */
102 if((dfd=open(try, mode))<0 && errno==EROFS) 110 dfd = open(try, mode);
103 dfd=open(try, mode = O_RDONLY); 111 if (dfd < 0 && errno == EROFS) {
104 if(dfd<0) goto try_again; 112 mode = O_RDONLY;
113 dfd = open(try, mode);
114 }
115 if (dfd < 0) goto try_again;
105 116
106 rc=ioctl(dfd, BB_LOOP_GET_STATUS, &loopinfo); 117 rc = ioctl(dfd, BB_LOOP_GET_STATUS, &loopinfo);
107 118
108 /* If device free, claim it. */ 119 /* If device free, claim it. */
109 if(rc && errno==ENXIO) { 120 if (rc && errno == ENXIO) {
110 memset(&loopinfo, 0, sizeof(loopinfo)); 121 memset(&loopinfo, 0, sizeof(loopinfo));
111 safe_strncpy((char *)loopinfo.lo_file_name, file, LO_NAME_SIZE); 122 safe_strncpy((char *)loopinfo.lo_file_name, file, LO_NAME_SIZE);
112 loopinfo.lo_offset = offset; 123 loopinfo.lo_offset = offset;
113 /* Associate free loop device with file. */ 124 /* Associate free loop device with file. */
114 if(!ioctl(dfd, LOOP_SET_FD, ffd)) { 125 if (!ioctl(dfd, LOOP_SET_FD, ffd)) {
115 if (!ioctl(dfd, BB_LOOP_SET_STATUS, &loopinfo)) rc=0; 126 if (!ioctl(dfd, BB_LOOP_SET_STATUS, &loopinfo)) rc = 0;
116 else ioctl(dfd, LOOP_CLR_FD, 0); 127 else ioctl(dfd, LOOP_CLR_FD, 0);
117 } 128 }
118 129
@@ -121,15 +132,16 @@ int set_loop(char **device, const char *file, int offset)
121 file isn't pretty either. In general, mounting the same file twice 132 file isn't pretty either. In general, mounting the same file twice
122 without using losetup manually is problematic.) 133 without using losetup manually is problematic.)
123 */ 134 */
124 } else if(strcmp(file,(char *)loopinfo.lo_file_name) 135 } else if (strcmp(file,(char *)loopinfo.lo_file_name)
125 || offset!=loopinfo.lo_offset) rc=-1; 136 || offset!=loopinfo.lo_offset) rc = -1;
126 close(dfd); 137 close(dfd);
127try_again: 138try_again:
128 if(*device) break; 139 if (*device) break;
129 } 140 }
130 close(ffd); 141 close(ffd);
131 if(!rc) { 142 if (!rc) {
132 if(!*device) *device=strdup(dev); 143 if (!*device) *device = strdup(dev);
133 return mode==O_RDONLY ? 1 : 0; 144 return mode==O_RDONLY ? 1 : 0;
134 } else return rc; 145 }
146 return rc;
135} 147}