Ignore:
Timestamp:
Dec 20, 2016, 4:07:32 PM (7 years ago)
Author:
Bruno Cornec
Message:

New 3?3 banch for incorporation of latest busybox 1.25. Changing minor version to handle potential incompatibilities.

Location:
branches/3.3
Files:
1 edited
1 copied

Legend:

Unmodified
Added
Removed
  • branches/3.3/mindi-busybox/util-linux/losetup.c

    r3232 r3621  
    99
    1010//usage:#define losetup_trivial_usage
    11 //usage:       "[-r] [-o OFS] LOOPDEV FILE - associate loop devices\n"
     11//usage:       "[-r] [-o OFS] {-f|LOOPDEV} FILE - associate loop devices\n"
    1212//usage:       "    losetup -d LOOPDEV - disassociate\n"
    13 //usage:       "    losetup [-f] - show"
     13//usage:       "    losetup -a - show status\n"
     14//usage:       "    losetup -f - show next free loop device"
    1415//usage:#define losetup_full_usage "\n\n"
    1516//usage:       "    -o OFS  Start OFS bytes into FILE"
    1617//usage:     "\n    -r  Read-only"
    17 //usage:     "\n    -f  Show first free loop device"
     18//usage:     "\n    -f  Show/use next free loop device"
    1819//usage:
    1920//usage:#define losetup_notes_usage
    20 //usage:       "No arguments will display all current associations.\n"
    2121//usage:       "One argument (losetup /dev/loop1) will display the current association\n"
    2222//usage:       "(if any), or disassociate it (with -d). The display shows the offset\n"
     
    2828#include "libbb.h"
    2929
     30/* 1048575 is a max possible minor number in Linux circa 2010 */
     31/* for now use something less extreme */
     32#define MAX_LOOP_NUM 1023
     33
    3034int losetup_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
    3135int losetup_main(int argc UNUSED_PARAM, char **argv)
    3236{
    3337    unsigned opt;
    34     int n;
    3538    char *opt_o;
    36     unsigned long long offset = 0;
     39    char dev[LOOP_NAMESIZE];
    3740    enum {
    3841        OPT_d = (1 << 0),
    3942        OPT_o = (1 << 1),
    4043        OPT_f = (1 << 2),
    41         OPT_r = (1 << 3), /* must be last */
     44        OPT_a = (1 << 3),
     45        OPT_r = (1 << 4), /* must be last */
    4246    };
    4347
    44     /* max 2 args, -d,-o,-f opts are mutually exclusive */
    45     opt_complementary = "?2:d--of:o--df:f--do";
    46     opt = getopt32(argv, "do:fr", &opt_o);
     48    opt_complementary = "?2:d--ofar:a--ofr";
     49    opt = getopt32(argv, "do:far", &opt_o);
    4750    argv += optind;
    4851
    49     if (opt == OPT_o)
    50         offset = xatoull(opt_o);
    51 
    52     if (opt == OPT_d) {
    53         /* -d BLOCKDEV */
    54         if (!argv[0] || argv[1])
    55             bb_show_usage();
    56         if (del_loop(argv[0]))
    57             bb_simple_perror_msg_and_die(argv[0]);
    58         return EXIT_SUCCESS;
    59     }
    60 
    61     if (argv[0]) {
     52    /* LOOPDEV */
     53    if (!opt && argv[0] && !argv[1]) {
    6254        char *s;
    6355
    64         if (opt == OPT_f) /* -f should not have arguments */
    65             bb_show_usage();
    66 
    67         if (argv[1]) {
    68             /* [-r] [-o OFS] BLOCKDEV FILE */
    69             if (set_loop(&argv[0], argv[1], offset, (opt / OPT_r)) < 0)
    70                 bb_simple_perror_msg_and_die(argv[0]);
    71             return EXIT_SUCCESS;
    72         }
    73         /* [-r] [-o OFS] BLOCKDEV */
    7456        s = query_loop(argv[0]);
    7557        if (!s)
     
    8163    }
    8264
    83     /* [-r] [-o OFS|-f] with no params */
    84     n = 0;
    85     while (1) {
     65    /* -d LOOPDEV */
     66    if (opt == OPT_d && argv[0]) {
     67        if (del_loop(argv[0]))
     68            bb_simple_perror_msg_and_die(argv[0]);
     69        return EXIT_SUCCESS;
     70    }
     71
     72    /* -a */
     73    if (opt == OPT_a) {
     74        int n;
     75        for (n = 0; n < MAX_LOOP_NUM; n++) {
     76            char *s;
     77
     78            sprintf(dev, LOOP_FORMAT, n);
     79            s = query_loop(dev);
     80            if (s) {
     81                printf("%s: %s\n", dev, s);
     82                free(s);
     83            }
     84        }
     85        return EXIT_SUCCESS;
     86    }
     87
     88    /* contains -f */
     89    if (opt & OPT_f) {
    8690        char *s;
    87         char dev[LOOP_NAMESIZE];
     91        int n = 0;
    8892
    89         sprintf(dev, LOOP_FORMAT, n);
    90         s = query_loop(dev);
    91         n++;
    92         if (!s) {
    93             if (n > 9 && errno && errno != ENXIO)
    94                 return EXIT_SUCCESS;
    95             if (opt == OPT_f) {
    96                 puts(dev);
    97                 return EXIT_SUCCESS;
    98             }
    99         } else {
    100             if (opt != OPT_f)
    101                 printf("%s: %s\n", dev, s);
    102             if (ENABLE_FEATURE_CLEAN_UP)
    103                 free(s);
     93        do {
     94            if (n > MAX_LOOP_NUM)
     95                bb_error_msg_and_die("no free loop devices");
     96            sprintf(dev, LOOP_FORMAT, n++);
     97            s = query_loop(dev);
     98            free(s);
     99        } while (s);
     100        /* now: dev is next free "/dev/loopN" */
     101        if ((opt == OPT_f) && !argv[0]) {
     102            puts(dev);
     103            return EXIT_SUCCESS;
    104104        }
    105105    }
    106     return EXIT_SUCCESS;
     106
     107    /* [-r] [-o OFS] {-f|LOOPDEV} FILE */
     108    if (argv[0] && ((opt & OPT_f) || argv[1])) {
     109        unsigned long long offset = 0;
     110        char *d = dev;
     111
     112        if (opt & OPT_o)
     113            offset = xatoull(opt_o);
     114        if (!(opt & OPT_f))
     115            d = *argv++;
     116
     117        if (argv[0]) {
     118            if (set_loop(&d, argv[0], offset, (opt & OPT_r)) < 0)
     119                bb_simple_perror_msg_and_die(argv[0]);
     120            return EXIT_SUCCESS;
     121        }
     122    }
     123
     124    bb_show_usage(); /* does not return */
     125    /*return EXIT_FAILURE;*/
    107126}
Note: See TracChangeset for help on using the changeset viewer.