Changeset 1765 in MondoRescue for branches/2.2.5/mindi-busybox/libbb/loop.c


Ignore:
Timestamp:
Nov 4, 2007, 3:16:40 AM (17 years ago)
Author:
Bruno Cornec
Message:

Update to busybox 1.7.2

File:
1 edited

Legend:

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

    r821 r1765  
    44 *
    55 * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
     6 * Copyright (C) 2005 by Rob Landley <rob@landley.net>
    67 *
    78 * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
    89 */
    910
    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>
    1811#include "libbb.h"
    1912
     
    5649    int fd;
    5750    bb_loop_info loopinfo;
    58     char *dev=0;
     51    char *dev = 0;
    5952
    60     if ((fd = open(device, O_RDONLY)) < 0) return 0;
     53    fd = open(device, O_RDONLY);
     54    if (fd < 0) return 0;
    6155    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,
    6357                (char *)loopinfo.lo_file_name);
    6458    close(fd);
     
    7266    int fd, rc;
    7367
    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);
    7671    close(fd);
    7772
     
    8580   file/offset if it finds one.
    8681 */
    87 int set_loop(char **device, const char *file, int offset)
     82int set_loop(char **device, const char *file, unsigned long long offset)
    8883{
    89     char dev[20], *try;
     84    char dev[LOOP_NAMESIZE];
     85    char *try;
    9086    bb_loop_info loopinfo;
    9187    struct stat statbuf;
    92     int i, dfd, ffd, mode, rc=-1;
    93    
     88    int i, dfd, ffd, mode, rc = -1;
     89
    9490    /* 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    }
    9799
    98100    /* Find a loop device.  */
    99     try=*device ? : dev;
    100     for(i=0;rc;i++) {
     101    try = *device ? : dev;
     102    for (i = 0; rc; i++) {
    101103        sprintf(dev, LOOP_FORMAT, i);
    102104
    103105        /* 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;
    106108            break;
    107109        }
    108110        /* 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;
    112118
    113         rc=ioctl(dfd, BB_LOOP_GET_STATUS, &loopinfo);
     119        rc = ioctl(dfd, BB_LOOP_GET_STATUS, &loopinfo);
    114120
    115         /* If device free, claim it.  */
    116         if(rc && errno==ENXIO) {
     121        /* If device is free, claim it.  */
     122        if (rc && errno == ENXIO) {
    117123            memset(&loopinfo, 0, sizeof(loopinfo));
    118124            safe_strncpy((char *)loopinfo.lo_file_name, file, LO_NAME_SIZE);
    119125            loopinfo.lo_offset = offset;
    120126            /* 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);
    124132            }
    125133
     
    129137           without using losetup manually is problematic.)
    130138         */
    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        }
    133143        close(dfd);
    134 try_again:
    135         if(*device) break;
     144 try_again:
     145        if (*device) break;
    136146    }
    137147    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;
    142154}
Note: See TracChangeset for help on using the changeset viewer.