Changeset 1770 in MondoRescue for branches/stable/mindi-busybox/libbb/loop.c
- Timestamp:
- Nov 6, 2007, 11:01:53 AM (16 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/stable/mindi-busybox/libbb/loop.c
r821 r1770 4 4 * 5 5 * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org> 6 * Copyright (C) 2005 by Rob Landley <rob@landley.net> 6 7 * 7 8 * Licensed under the GPL v2 or later, see the file LICENSE in this tarball. 8 9 */ 9 10 10 11 #include <features.h>12 #include <stdio.h>13 #include <errno.h>14 #include <fcntl.h>15 #include <string.h>16 #include <unistd.h>17 #include <sys/ioctl.h>18 11 #include "libbb.h" 19 12 … … 56 49 int fd; 57 50 bb_loop_info loopinfo; 58 char *dev =0;51 char *dev = 0; 59 52 60 if ((fd = open(device, O_RDONLY)) < 0) return 0; 53 fd = open(device, O_RDONLY); 54 if (fd < 0) return 0; 61 55 if (!ioctl(fd, BB_LOOP_GET_STATUS, &loopinfo)) 62 dev =bb_xasprintf("%ld %s", (long) loopinfo.lo_offset,56 dev = xasprintf("%ld %s", (long) loopinfo.lo_offset, 63 57 (char *)loopinfo.lo_file_name); 64 58 close(fd); … … 72 66 int fd, rc; 73 67 74 if ((fd = open(device, O_RDONLY)) < 0) return 1; 75 rc=ioctl(fd, LOOP_CLR_FD, 0); 68 fd = open(device, O_RDONLY); 69 if (fd < 0) return 1; 70 rc = ioctl(fd, LOOP_CLR_FD, 0); 76 71 close(fd); 77 72 … … 85 80 file/offset if it finds one. 86 81 */ 87 int set_loop(char **device, const char *file, intoffset)82 int set_loop(char **device, const char *file, unsigned long long offset) 88 83 { 89 char dev[20], *try; 84 char dev[LOOP_NAMESIZE]; 85 char *try; 90 86 bb_loop_info loopinfo; 91 87 struct stat statbuf; 92 int i, dfd, ffd, mode, rc =-1;93 88 int i, dfd, ffd, mode, rc = -1; 89 94 90 /* Open the file. Barf if this doesn't work. */ 95 if((ffd = open(file, mode=O_RDWR))<0 && (ffd = open(file,mode=O_RDONLY))<0) 96 return -errno; 91 mode = O_RDWR; 92 ffd = open(file, mode); 93 if (ffd < 0) { 94 mode = O_RDONLY; 95 ffd = open(file, mode); 96 if (ffd < 0) 97 return -errno; 98 } 97 99 98 100 /* Find a loop device. */ 99 try =*device ? : dev;100 for (i=0;rc;i++) {101 try = *device ? : dev; 102 for (i = 0; rc; i++) { 101 103 sprintf(dev, LOOP_FORMAT, i); 102 104 103 105 /* Ran out of block devices, return failure. */ 104 if (stat(try, &statbuf) || !S_ISBLK(statbuf.st_mode)) {105 rc =-ENOENT;106 if (stat(try, &statbuf) || !S_ISBLK(statbuf.st_mode)) { 107 rc = -ENOENT; 106 108 break; 107 109 } 108 110 /* Open the sucker and check its loopiness. */ 109 if((dfd=open(try, mode))<0 && errno==EROFS) 110 dfd=open(try, mode = O_RDONLY); 111 if(dfd<0) goto try_again; 111 dfd = open(try, mode); 112 if (dfd < 0 && errno == EROFS) { 113 mode = O_RDONLY; 114 dfd = open(try, mode); 115 } 116 if (dfd < 0) 117 goto try_again; 112 118 113 rc =ioctl(dfd, BB_LOOP_GET_STATUS, &loopinfo);119 rc = ioctl(dfd, BB_LOOP_GET_STATUS, &loopinfo); 114 120 115 /* If device free, claim it. */116 if (rc && errno==ENXIO) {121 /* If device is free, claim it. */ 122 if (rc && errno == ENXIO) { 117 123 memset(&loopinfo, 0, sizeof(loopinfo)); 118 124 safe_strncpy((char *)loopinfo.lo_file_name, file, LO_NAME_SIZE); 119 125 loopinfo.lo_offset = offset; 120 126 /* Associate free loop device with file. */ 121 if(!ioctl(dfd, LOOP_SET_FD, ffd)) { 122 if (!ioctl(dfd, BB_LOOP_SET_STATUS, &loopinfo)) rc=0; 123 else ioctl(dfd, LOOP_CLR_FD, 0); 127 if (!ioctl(dfd, LOOP_SET_FD, ffd)) { 128 if (!ioctl(dfd, BB_LOOP_SET_STATUS, &loopinfo)) 129 rc = 0; 130 else 131 ioctl(dfd, LOOP_CLR_FD, 0); 124 132 } 125 133 … … 129 137 without using losetup manually is problematic.) 130 138 */ 131 } else if(strcmp(file,(char *)loopinfo.lo_file_name) 132 || offset!=loopinfo.lo_offset) rc=-1; 139 } else if (strcmp(file, (char *)loopinfo.lo_file_name) != 0 140 || offset != loopinfo.lo_offset) { 141 rc = -1; 142 } 133 143 close(dfd); 134 try_again:135 if (*device) break;144 try_again: 145 if (*device) break; 136 146 } 137 147 close(ffd); 138 if(!rc) { 139 if(!*device) *device=strdup(dev); 140 return mode==O_RDONLY ? 1 : 0; 141 } else return rc; 148 if (!rc) { 149 if (!*device) 150 *device = xstrdup(dev); 151 return (mode == O_RDONLY); /* 1:ro, 0:rw */ 152 } 153 return rc; 142 154 }
Note:
See TracChangeset
for help on using the changeset viewer.