Changeset 2725 in MondoRescue for branches/2.2.9/mindi-busybox/libbb/loop.c


Ignore:
Timestamp:
Feb 25, 2011, 9:26:54 PM (13 years ago)
Author:
Bruno Cornec
Message:
  • Update mindi-busybox to 1.18.3 to avoid problems with the tar command which is now failing on recent versions with busybox 1.7.3
File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/2.2.9/mindi-busybox/libbb/loop.c

    r1765 r2725  
    66 * Copyright (C) 2005 by Rob Landley <rob@landley.net>
    77 *
    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.
    99 */
     10#include "libbb.h"
     11#include <linux/version.h>
    1012
    11 #include "libbb.h"
     13#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0)
    1214
    1315/* 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>
    1719typedef 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
    2024
    2125/* 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
    3134typedef struct {
    3235    int                lo_number;
     
    4548#endif
    4649
    47 char *query_loop(const char *device)
     50char* FAST_FUNC query_loop(const char *device)
    4851{
    4952    int fd;
    5053    bb_loop_info loopinfo;
    51     char *dev = 0;
     54    char *dev = NULL;
    5255
    5356    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    }
    5964
    6065    return dev;
    6166}
    6267
    63 
    64 int del_loop(const char *device)
     68int FAST_FUNC del_loop(const char *device)
    6569{
    6670    int fd, rc;
    6771
    6872    fd = open(device, O_RDONLY);
    69     if (fd < 0) return 1;
     73    if (fd < 0)
     74        return 1;
    7075    rc = ioctl(fd, LOOP_CLR_FD, 0);
    7176    close(fd);
     
    8085   file/offset if it finds one.
    8186 */
    82 int set_loop(char **device, const char *file, unsigned long long offset)
     87int FAST_FUNC set_loop(char **device, const char *file, unsigned long long offset)
    8388{
    8489    char dev[LOOP_NAMESIZE];
     
    99104
    100105    /* 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++) {
    103109        sprintf(dev, LOOP_FORMAT, i);
    104110
    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.  */
    107122            rc = -ENOENT;
    108123            break;
    109124        }
     125 try_to_open:
    110126        /* Open the sucker and check its loopiness.  */
    111127        dfd = open(try, mode);
     
    125141            loopinfo.lo_offset = offset;
    126142            /* 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)
    129145                    rc = 0;
    130146                else
     
    137153           without using losetup manually is problematic.)
    138154         */
    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        ) {
    141159            rc = -1;
    142160        }
     
    146164    }
    147165    close(ffd);
    148     if (!rc) {
     166    if (rc == 0) {
    149167        if (!*device)
    150168            *device = xstrdup(dev);
Note: See TracChangeset for help on using the changeset viewer.