Changeset 2725 in MondoRescue for branches/2.2.9/mindi-busybox/libbb/loop.c
- Timestamp:
- Feb 25, 2011, 9:26:54 PM (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/2.2.9/mindi-busybox/libbb/loop.c
r1765 r2725 6 6 * Copyright (C) 2005 by Rob Landley <rob@landley.net> 7 7 * 8 * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.8 * Licensed under GPLv2 or later, see file LICENSE in this source tree. 9 9 */ 10 #include "libbb.h" 11 #include <linux/version.h> 10 12 11 #i nclude "libbb.h"13 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0) 12 14 13 15 /* For 2.6, use the cleaned up header to get the 64 bit API. */ 14 #include <linux/version.h> 15 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0) 16 # include <linux/loop.h>16 // Commented out per Rob's request 17 //# include "fix_u32.h" /* some old toolchains need __u64 for linux/loop.h */ 18 # include <linux/loop.h> 17 19 typedef struct loop_info64 bb_loop_info; 18 #define BB_LOOP_SET_STATUS LOOP_SET_STATUS64 19 #define BB_LOOP_GET_STATUS LOOP_GET_STATUS64 20 # define BB_LOOP_SET_STATUS LOOP_SET_STATUS64 21 # define BB_LOOP_GET_STATUS LOOP_GET_STATUS64 22 23 #else 20 24 21 25 /* For 2.4 and earlier, use the 32 bit API (and don't trust the headers) */ 22 #else 23 /* Stuff stolen from linux/loop.h for 2.4 and earlier kernels*/ 24 #include <linux/posix_types.h> 25 #define LO_NAME_SIZE 64 26 #define LO_KEY_SIZE 32 27 #define LOOP_SET_FD 0x4C00 28 #define LOOP_CLR_FD 0x4C01 29 #define BB_LOOP_SET_STATUS 0x4C02 30 #define BB_LOOP_GET_STATUS 0x4C03 26 /* Stuff stolen from linux/loop.h for 2.4 and earlier kernels */ 27 # include <linux/posix_types.h> 28 # define LO_NAME_SIZE 64 29 # define LO_KEY_SIZE 32 30 # define LOOP_SET_FD 0x4C00 31 # define LOOP_CLR_FD 0x4C01 32 # define BB_LOOP_SET_STATUS 0x4C02 33 # define BB_LOOP_GET_STATUS 0x4C03 31 34 typedef struct { 32 35 int lo_number; … … 45 48 #endif 46 49 47 char *query_loop(const char *device)50 char* FAST_FUNC query_loop(const char *device) 48 51 { 49 52 int fd; 50 53 bb_loop_info loopinfo; 51 char *dev = 0;54 char *dev = NULL; 52 55 53 56 fd = open(device, O_RDONLY); 54 if (fd < 0) return 0; 55 if (!ioctl(fd, BB_LOOP_GET_STATUS, &loopinfo)) 56 dev = xasprintf("%ld %s", (long) loopinfo.lo_offset, 57 (char *)loopinfo.lo_file_name); 58 close(fd); 57 if (fd >= 0) { 58 if (ioctl(fd, BB_LOOP_GET_STATUS, &loopinfo) == 0) { 59 dev = xasprintf("%"OFF_FMT"u %s", (off_t) loopinfo.lo_offset, 60 (char *)loopinfo.lo_file_name); 61 } 62 close(fd); 63 } 59 64 60 65 return dev; 61 66 } 62 67 63 64 int del_loop(const char *device) 68 int FAST_FUNC del_loop(const char *device) 65 69 { 66 70 int fd, rc; 67 71 68 72 fd = open(device, O_RDONLY); 69 if (fd < 0) return 1; 73 if (fd < 0) 74 return 1; 70 75 rc = ioctl(fd, LOOP_CLR_FD, 0); 71 76 close(fd); … … 80 85 file/offset if it finds one. 81 86 */ 82 int 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) 83 88 { 84 89 char dev[LOOP_NAMESIZE]; … … 99 104 100 105 /* Find a loop device. */ 101 try = *device ? : dev; 102 for (i = 0; rc; i++) { 106 try = *device ? *device : dev; 107 /* 1048575 is a max possible minor number in Linux circa 2010 */ 108 for (i = 0; rc && i < 1048576; i++) { 103 109 sprintf(dev, LOOP_FORMAT, i); 104 110 105 /* Ran out of block devices, return failure. */ 106 if (stat(try, &statbuf) || !S_ISBLK(statbuf.st_mode)) { 111 IF_FEATURE_MOUNT_LOOP_CREATE(errno = 0;) 112 if (stat(try, &statbuf) != 0 || !S_ISBLK(statbuf.st_mode)) { 113 if (ENABLE_FEATURE_MOUNT_LOOP_CREATE 114 && errno == ENOENT 115 && try == dev 116 ) { 117 /* Node doesn't exist, try to create it. */ 118 if (mknod(dev, S_IFBLK|0644, makedev(7, i)) == 0) 119 goto try_to_open; 120 } 121 /* Ran out of block devices, return failure. */ 107 122 rc = -ENOENT; 108 123 break; 109 124 } 125 try_to_open: 110 126 /* Open the sucker and check its loopiness. */ 111 127 dfd = open(try, mode); … … 125 141 loopinfo.lo_offset = offset; 126 142 /* Associate free loop device with file. */ 127 if ( !ioctl(dfd, LOOP_SET_FD, ffd)) {128 if ( !ioctl(dfd, BB_LOOP_SET_STATUS, &loopinfo))143 if (ioctl(dfd, LOOP_SET_FD, ffd) == 0) { 144 if (ioctl(dfd, BB_LOOP_SET_STATUS, &loopinfo) == 0) 129 145 rc = 0; 130 146 else … … 137 153 without using losetup manually is problematic.) 138 154 */ 139 } else if (strcmp(file, (char *)loopinfo.lo_file_name) != 0 140 || offset != loopinfo.lo_offset) { 155 } else 156 if (strcmp(file, (char *)loopinfo.lo_file_name) != 0 157 || offset != loopinfo.lo_offset 158 ) { 141 159 rc = -1; 142 160 } … … 146 164 } 147 165 close(ffd); 148 if ( !rc) {166 if (rc == 0) { 149 167 if (!*device) 150 168 *device = xstrdup(dev);
Note:
See TracChangeset
for help on using the changeset viewer.