source: branches/stable/mindi-busybox/coreutils/dd.c @ 1770

Last change on this file since 1770 was 1770, checked in by Bruno Cornec, 13 years ago
  • Better output for mindi-busybox revision
  • Remove dummy file created on NFS - report from Arnaud Tiger <arnaud.tiger_at_hp.com>
  • strace useful for debug
  • fix new versions for pb (2.0.0 for mindi and 1.7.2 for mindi-busybox)
  • fix build process for mindi-busybox + options used in that version (dd for label-partitions-as-necessary)
  • fix typo in label-partitions-as-necessary which doesn't seem to work
  • Update to busybox 1.7.2
  • perl is now required at restore time to support uuid swap partitions (and will be used for many other thigs

in the future for sure)

  • next mindi version will be 2.0.0 due to all the changes made in it (udev may break working distros)
  • small optimization in mindi on keyboard handling (one single find instead of multiple)
  • better interaction for USB device when launching mindi manually
  • attempt to automatically guess block disk size for ramdisk
  • fix typos in bkphw
  • Fix the remaining problem with UUID support for swap partitions
  • Updates mondoarchive man page for USB support
  • Adds preliminary Hardware support to mindi (Proliant SSSTK)
  • Tries to add udev support also for rhel4
  • Fix UUID support which was still broken.
  • Be conservative in test for the start-nfs script
  • Update config file for mindi-busybox for 1.7.2 migration
  • Try to run around a busybox bug (1.2.2 pb on inexistant links)
  • Add build content for mindi-busybox in pb
  • Remove distributions content for mindi-busybox
  • Fix a warning on inexistant raidtab
  • Solve problem on tmpfs in restore init (Problem of inexistant symlink and busybox)
  • Create MONDO_CACHE and use it everywhere + creation at start
  • Really never try to eject a USB device
  • Fix a issue with &> usage (replaced with 1> and 2>)
  • Adds magic file to depllist in order to have file working + ldd which helps for debugging issues
  • tty modes correct to avoid sh error messages
  • Use ext3 normally and not ext2 instead
  • USB device should be corrected after reading (take 1st part)
  • Adds a mount_USB_here function derived from mount_CDROM_here
  • usb detection place before /dev detection in device name at restore time
  • Fix when restoring from USB: media is asked in interactive mode
  • Adds USB support for mondorestore
  • mount_cdrom => mount_media
  • elilo.efi is now searched throughout /boot/efi and not in a fixed place as there is no standard
  • untar-and-softlink => untar (+ interface change)
  • suppress useless softlinks creation/removal in boot process
  • avoids udevd messages on groups
  • Increase # of disks to 99 as in mindi at restore time (should be a conf file parameter)
  • skip existing big file creation
  • seems to work correctly for USB mindi boot
  • Adds group and tty link to udev conf
  • Always load usb-torage (even 2.6) to initiate USB bus discovery
  • Better printing of messages
  • Attempt to fix a bug in supporting OpenSusE 10.3 kernel for initramfs (mindi may now use multiple regex for kernel initrd detection)
  • Links were not correctly done as non relative for modules in mindi
  • exclusion of modules denied now works
  • Also create modules in their ordinary place, so that classical modprobe works + copy modules.dep
  • Fix bugs for DENY_MODS handling
  • Add device /dev/console for udev
  • ide-generic should now really be excluded
  • Fix a bug in major number for tty
  • If udev then adds modprobe/insmod to rootfs
  • tty0 is also cretaed with udev
  • ide-generic put rather in DENY_MODS
  • udevd remove from deplist s handled in mindi directly
  • better default for mindi when using --usb
  • Handles dynamically linked busybox (in case we want to use it soon ;-)
  • Adds fixed devices to create for udev
  • ide-generic should not be part of the initrd when using libata v2
  • support a dynamically linked udev (case on Ubuntu 7.10 and Mandriva 2008.0 so should be quite generic) This will give incitation to move to dyn. linked binaries in the initrd which will help for other tasks (ia6 4)
  • Improvement in udev support (do not use cl options not available in busybox)
  • Udev in mindi
    • auto creation of the right links at boot time with udev-links.conf(from Mandriva 2008.0)
    • rework startup of udev as current makes kernel crash (from Mandriva 2008.0)
    • add support for 64 bits udev
  • Try to render MyInsmod? silent at boot time
  • Adds udev support (mandatory for newest distributions to avoid remapping of devices in a different way as on the original system)
  • We also need vaft format support for USB boot
  • Adds libusual support (Ubuntu 7.10 needs it for USB)
  • Improve Ubuntu/Debian? keyboard detection and support
  • pbinit adapted to new pb (0.8.10). Filtering of docs done in it
  • Suppress some mondo warnings and errors on USB again
  • Tries to fix lack of files in deb mindi package
  • Verify should now work for USB devices
  • More log/mesages improvement for USB support
  • - Supress g_erase_tmpdir_and_scratchdir
  • Improve some log messages for USB support
  • Try to improve install in mindi to avoid issues with isolinux.cfg not installed vene if in the pkg :-(
  • Improve mindi-busybox build
  • In conformity with pb 0.8.9
  • Add support for Ubuntu 7.10 in build process
  • Add USB Key button to Menu UI (CD streamer removed)
  • Attempt to fix error messages on tmp/scratch files at the end by removing those dir at the latest possible.
  • Fix a bug linked to the size of the -E param which could be used (Arnaud Tiger/René? Ribaud).
  • Integrate ~/.pbrc content into mondorescue.pb (required project-builder >= 0.8.7)
  • Put mondorescue in conformity with new pb filtering rules
  • Add USB support at restore time (no test done yet). New start-usb script PB varibale added where useful
  • Unmounting USB device before removal of temporary scratchdir
  • Stil refining USB copy back to mondo (one command was not executed)
  • No need to have the image subdor in the csratchdir when USB.
  • umount the USB partition before attempting to use it
  • Remove useless copy from mindi to mondo at end of USB handling

(risky merge, we are raising the limits of 2 diverging branches. The status of stable is not completely sure as such. Will need lots of tests, but it's not yet done :-()
(merge -r1692:1769 $SVN_M/branches/2.2.5)

File size: 7.2 KB
Line 
1/* vi: set sw=4 ts=4: */
2/*
3 * Mini dd implementation for busybox
4 *
5 *
6 * Copyright (C) 2000,2001  Matt Kraai
7 *
8 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
9 */
10
11#include <signal.h>  /* For FEATURE_DD_SIGNAL_HANDLING */
12#include "libbb.h"
13
14/* This is a NOEXEC applet. Be very careful! */
15
16
17static const struct suffix_mult dd_suffixes[] = {
18    { "c", 1 },
19    { "w", 2 },
20    { "b", 512 },
21    { "kD", 1000 },
22    { "k", 1024 },
23    { "K", 1024 },  /* compat with coreutils dd */
24    { "MD", 1000000 },
25    { "M", 1048576 },
26    { "GD", 1000000000 },
27    { "G", 1073741824 },
28    { }
29};
30
31struct globals {
32    off_t out_full, out_part, in_full, in_part;
33};
34#define G (*(struct globals*)&bb_common_bufsiz1)
35/* We have to zero it out because of NOEXEC */
36#define INIT_G() memset(&G, 0, sizeof(G))
37
38
39static void dd_output_status(int ATTRIBUTE_UNUSED cur_signal)
40{
41    /* Deliberately using %u, not %d */
42    fprintf(stderr, "%"OFF_FMT"u+%"OFF_FMT"u records in\n"
43            "%"OFF_FMT"u+%"OFF_FMT"u records out\n",
44            G.in_full, G.in_part,
45            G.out_full, G.out_part);
46}
47
48static ssize_t full_write_or_warn(int fd, const void *buf, size_t len,
49    const char *const filename)
50{
51    ssize_t n = full_write(fd, buf, len);
52    if (n < 0)
53        bb_perror_msg("writing '%s'", filename);
54    return n;
55}
56
57static bool write_and_stats(int fd, const void *buf, size_t len, size_t obs,
58    const char *filename)
59{
60    ssize_t n = full_write_or_warn(fd, buf, len, filename);
61    if (n < 0)
62        return 1;
63    if (n == obs)
64        G.out_full++;
65    else if (n) /* > 0 */
66        G.out_part++;
67    return 0;
68}
69
70#if ENABLE_LFS
71#define XATOU_SFX xatoull_sfx
72#else
73#define XATOU_SFX xatoul_sfx
74#endif
75
76int dd_main(int argc, char **argv);
77int dd_main(int argc, char **argv)
78{
79    enum {
80        FLAG_SYNC    = 1 << 0,
81        FLAG_NOERROR = 1 << 1,
82        FLAG_NOTRUNC = 1 << 2,
83        FLAG_TWOBUFS = 1 << 3,
84        FLAG_COUNT   = 1 << 4,
85    };
86    static const char keywords[] ALIGN1 =
87        "bs=\0""count=\0""seek=\0""skip=\0""if=\0""of=\0"
88#if ENABLE_FEATURE_DD_IBS_OBS
89        "ibs=\0""obs=\0""conv=\0""notrunc\0""sync\0""noerror\0"
90#endif
91        ;
92    enum {
93        OP_bs = 1,
94        OP_count,
95        OP_seek,
96        OP_skip,
97        OP_if,
98        OP_of,
99#if ENABLE_FEATURE_DD_IBS_OBS
100        OP_ibs,
101        OP_obs,
102        OP_conv,
103        OP_conv_notrunc,
104        OP_conv_sync,
105        OP_conv_noerror,
106#endif
107    };
108    size_t ibs = 512, obs = 512;
109    ssize_t n, w;
110    char *ibuf, *obuf;
111    /* And these are all zeroed at once! */
112    struct {
113        int flags;
114        int ifd, ofd;
115        size_t oc;
116        off_t count;
117        off_t seek, skip;
118        const char *infile, *outfile;
119#if ENABLE_FEATURE_DD_SIGNAL_HANDLING
120        struct sigaction sigact;
121#endif
122    } Z;
123#define flags   (Z.flags  )
124#define ifd     (Z.ifd    )
125#define ofd     (Z.ofd    )
126#define oc      (Z.oc     )
127#define count   (Z.count  )
128#define seek    (Z.seek   )
129#define skip    (Z.skip   )
130#define infile  (Z.infile )
131#define outfile (Z.outfile)
132#define sigact  (Z.sigact )
133
134    memset(&Z, 0, sizeof(Z));
135    INIT_G();
136
137#if ENABLE_FEATURE_DD_SIGNAL_HANDLING
138    sigact.sa_handler = dd_output_status;
139    sigact.sa_flags = SA_RESTART;
140    sigemptyset(&sigact.sa_mask);
141    sigaction(SIGUSR1, &sigact, NULL);
142#endif
143
144    for (n = 1; n < argc; n++) {
145        smalluint key_len;
146        smalluint what;
147        char *key;
148        char *arg = argv[n];
149
150//XXX:FIXME: we reject plain "dd --" This would cost ~20 bytes, so..
151//if (*arg == '-' && *++arg == '-' && !*++arg) continue;
152        key = strstr(arg, "=");
153        if (key == NULL)
154            bb_show_usage();
155        key_len = key - arg + 1;
156        key = xstrndup(arg, key_len);
157        what = index_in_strings(keywords, key) + 1;
158        if (ENABLE_FEATURE_CLEAN_UP)
159            free(key);
160        if (what == 0)
161            bb_show_usage();
162        arg += key_len;
163        /* Must fit into positive ssize_t */
164#if ENABLE_FEATURE_DD_IBS_OBS
165            if (what == OP_ibs) {
166                ibs = xatoul_range_sfx(arg, 1, ((size_t)-1L)/2, dd_suffixes);
167                continue;
168            }
169            if (what == OP_obs) {
170                obs = xatoul_range_sfx(arg, 1, ((size_t)-1L)/2, dd_suffixes);
171                continue;
172            }
173            if (what == OP_conv) {
174                while (1) {
175                    /* find ',', replace them with nil so we can use arg for
176                     * index_in_strings() without copying.
177                     * We rely on arg being non-null, else strchr would fault.
178                     */
179                    key = strchr(arg, ',');
180                    if (key)
181                        *key = '\0';
182                    what = index_in_strings(keywords, arg) + 1;
183                    if (what < OP_conv_notrunc)
184                        bb_error_msg_and_die(bb_msg_invalid_arg, arg, "conv");
185                    if (what == OP_conv_notrunc)
186                        flags |= FLAG_NOTRUNC;
187                    if (what == OP_conv_sync)
188                        flags |= FLAG_SYNC;
189                    if (what == OP_conv_noerror)
190                        flags |= FLAG_NOERROR;
191                    if (!key) /* no ',' left, so this was the last specifier */
192                        break;
193                    arg = key + 1; /* skip this keyword and ',' */
194                }
195                continue;
196            }
197#endif
198        if (what == OP_bs) {
199            ibs = obs = xatoul_range_sfx(arg, 1, ((size_t)-1L)/2, dd_suffixes);
200            continue;
201        }
202        /* These can be large: */
203        if (what == OP_count) {
204            flags |= FLAG_COUNT;
205            count = XATOU_SFX(arg, dd_suffixes);
206            continue;
207        }
208        if (what == OP_seek) {
209            seek = XATOU_SFX(arg, dd_suffixes);
210            continue;
211        }
212        if (what == OP_skip) {
213            skip = XATOU_SFX(arg, dd_suffixes);
214            continue;
215        }
216        if (what == OP_if) {
217            infile = arg;
218            continue;
219        }
220        if (what == OP_of)
221            outfile = arg;
222    }
223//XXX:FIXME for huge ibs or obs, malloc'ing them isn't the brightest idea ever
224    ibuf = obuf = xmalloc(ibs);
225    if (ibs != obs) {
226        flags |= FLAG_TWOBUFS;
227        obuf = xmalloc(obs);
228    }
229    if (infile != NULL)
230        ifd = xopen(infile, O_RDONLY);
231    else {
232        /* ifd = STDIN_FILENO; - it's zero and it's already there */
233        infile = bb_msg_standard_input;
234    }
235    if (outfile != NULL) {
236        int oflag = O_WRONLY | O_CREAT;
237
238        if (!seek && !(flags & FLAG_NOTRUNC))
239            oflag |= O_TRUNC;
240
241        ofd = xopen(outfile, oflag);
242
243        if (seek && !(flags & FLAG_NOTRUNC)) {
244            if (ftruncate(ofd, seek * obs) < 0) {
245                struct stat st;
246
247                if (fstat(ofd, &st) < 0 || S_ISREG(st.st_mode) ||
248                        S_ISDIR(st.st_mode))
249                    goto die_outfile;
250            }
251        }
252    } else {
253        ofd = STDOUT_FILENO;
254        outfile = bb_msg_standard_output;
255    }
256    if (skip) {
257        if (lseek(ifd, skip * ibs, SEEK_CUR) < 0) {
258            while (skip-- > 0) {
259                n = safe_read(ifd, ibuf, ibs);
260                if (n < 0)
261                    goto die_infile;
262                if (n == 0)
263                    break;
264            }
265        }
266    }
267    if (seek) {
268        if (lseek(ofd, seek * obs, SEEK_CUR) < 0)
269            goto die_outfile;
270    }
271
272    while (!(flags & FLAG_COUNT) || (G.in_full + G.in_part != count)) {
273        if (flags & FLAG_NOERROR) /* Pre-zero the buffer if conv=noerror */
274            memset(ibuf, 0, ibs);
275        n = safe_read(ifd, ibuf, ibs);
276        if (n == 0)
277            break;
278        if (n < 0) {
279            if (flags & FLAG_NOERROR) {
280                n = ibs;
281                bb_perror_msg("%s", infile);
282            } else
283                goto die_infile;
284        }
285        if ((size_t)n == ibs)
286            G.in_full++;
287        else {
288            G.in_part++;
289            if (flags & FLAG_SYNC) {
290                memset(ibuf + n, '\0', ibs - n);
291                n = ibs;
292            }
293        }
294        if (flags & FLAG_TWOBUFS) {
295            char *tmp = ibuf;
296            while (n) {
297                size_t d = obs - oc;
298
299                if (d > n)
300                    d = n;
301                memcpy(obuf + oc, tmp, d);
302                n -= d;
303                tmp += d;
304                oc += d;
305                if (oc == obs) {
306                    if (write_and_stats(ofd, obuf, obs, obs, outfile))
307                        goto out_status;
308                    oc = 0;
309                }
310            }
311        } else if (write_and_stats(ofd, ibuf, n, obs, outfile))
312            goto out_status;
313    }
314
315    if (ENABLE_FEATURE_DD_IBS_OBS && oc) {
316        w = full_write_or_warn(ofd, obuf, oc, outfile);
317        if (w < 0) goto out_status;
318        if (w > 0)
319            G.out_part++;
320    }
321    if (close(ifd) < 0) {
322 die_infile:
323        bb_perror_msg_and_die("%s", infile);
324    }
325
326    if (close(ofd) < 0) {
327 die_outfile:
328        bb_perror_msg_and_die("%s", outfile);
329    }
330 out_status:
331    dd_output_status(0);
332
333    return EXIT_SUCCESS;
334}
Note: See TracBrowser for help on using the repository browser.