aboutsummaryrefslogtreecommitdiff
path: root/utility.c
diff options
context:
space:
mode:
Diffstat (limited to 'utility.c')
-rw-r--r--utility.c70
1 files changed, 69 insertions, 1 deletions
diff --git a/utility.c b/utility.c
index 643c3b5f2..5ff03d7a0 100644
--- a/utility.c
+++ b/utility.c
@@ -1169,6 +1169,74 @@ extern int del_loop(const char *device)
1169 close(fd); 1169 close(fd);
1170 return( TRUE); 1170 return( TRUE);
1171} 1171}
1172#endif 1172
1173extern int set_loop(const char *device, const char *file, int offset, int *loopro)
1174{
1175 struct loop_info loopinfo;
1176 int fd, ffd, mode;
1177
1178 mode = *loopro ? O_RDONLY : O_RDWR;
1179 if ((ffd = open (file, mode)) < 0 && !*loopro
1180 && (errno != EROFS || (ffd = open (file, mode = O_RDONLY)) < 0)) {
1181 perror (file);
1182 return 1;
1183 }
1184 if ((fd = open (device, mode)) < 0) {
1185 close(ffd);
1186 perror (device);
1187 return 1;
1188 }
1189 *loopro = (mode == O_RDONLY);
1190
1191 memset(&loopinfo, 0, sizeof(loopinfo));
1192 strncpy(loopinfo.lo_name, file, LO_NAME_SIZE);
1193 loopinfo.lo_name[LO_NAME_SIZE-1] = 0;
1194
1195 loopinfo.lo_offset = offset;
1196
1197 loopinfo.lo_encrypt_key_size = 0;
1198 if (ioctl(fd, LOOP_SET_FD, ffd) < 0) {
1199 perror("ioctl: LOOP_SET_FD");
1200 close(fd);
1201 close(ffd);
1202 return 1;
1203 }
1204 if (ioctl(fd, LOOP_SET_STATUS, &loopinfo) < 0) {
1205 (void) ioctl(fd, LOOP_CLR_FD, 0);
1206 perror("ioctl: LOOP_SET_STATUS");
1207 close(fd);
1208 close(ffd);
1209 return 1;
1210 }
1211 close(fd);
1212 close(ffd);
1213 return 0;
1214}
1215
1216extern char *find_unused_loop_device (void)
1217{
1218 char dev[20];
1219 int i, fd;
1220 struct stat statbuf;
1221 struct loop_info loopinfo;
1222
1223 for(i = 0; i <= 7; i++) {
1224 sprintf(dev, "/dev/loop%d", i);
1225 if (stat (dev, &statbuf) == 0 && S_ISBLK(statbuf.st_mode)) {
1226 if ((fd = open (dev, O_RDONLY)) >= 0) {
1227 if(ioctl (fd, LOOP_GET_STATUS, &loopinfo) == -1) {
1228 if (errno == ENXIO) { /* probably free */
1229 close (fd);
1230 return strdup(dev);
1231 }
1232 }
1233 close (fd);
1234 }
1235 }
1236 }
1237 return NULL;
1238}
1239#endif /* BB_FEATURE_MOUNT_LOOP */
1240
1173 1241
1174/* END CODE */ 1242/* END CODE */