Changeset 1765 in MondoRescue for branches/2.2.5/mindi-busybox/libbb/copyfd.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/copyfd.c

    r821 r1765  
    88 */
    99
    10 #include <errno.h>
    11 #include <stdlib.h>
    12 #include <string.h>
    13 #include <unistd.h>
    14 
    1510#include "libbb.h"
    16 
    1711
    1812#if BUFSIZ < 4096
     
    2115#endif
    2216
     17/* Used by NOFORK applets (e.g. cat) - must not use xmalloc */
    2318
    24 static ssize_t bb_full_fd_action(int src_fd, int dst_fd, size_t size)
     19static off_t bb_full_fd_action(int src_fd, int dst_fd, off_t size)
    2520{
    2621    int status = -1;
    27     size_t total = 0;
    28     RESERVE_CONFIG_BUFFER(buffer,BUFSIZ);
     22    off_t total = 0;
     23    char buffer[BUFSIZ];
    2924
    30     if (src_fd < 0) goto out;
    31     while (!size || total < size)
    32     {
    33         ssize_t wrote, xread;
     25    if (src_fd < 0)
     26        goto out;
    3427
    35         xread = safe_read(src_fd, buffer,
    36                 (!size || size - total > BUFSIZ) ? BUFSIZ : size - total);
     28    if (!size) {
     29        size = BUFSIZ;
     30        status = 1; /* copy until eof */
     31    }
    3732
    38         if (xread > 0) {
    39             /* A -1 dst_fd means we need to fake it... */
    40             wrote = (dst_fd < 0) ? xread : bb_full_write(dst_fd, buffer, xread);
    41             if (wrote < xread) {
     33    while (1) {
     34        ssize_t rd;
     35
     36        rd = safe_read(src_fd, buffer, size > BUFSIZ ? BUFSIZ : size);
     37
     38        if (!rd) { /* eof - all done */
     39            status = 0;
     40            break;
     41        }
     42        if (rd < 0) {
     43            bb_perror_msg(bb_msg_read_error);
     44            break;
     45        }
     46        /* dst_fd == -1 is a fake, else... */
     47        if (dst_fd >= 0) {
     48            ssize_t wr = full_write(dst_fd, buffer, rd);
     49            if (wr < rd) {
    4250                bb_perror_msg(bb_msg_write_error);
    4351                break;
    4452            }
    45             total += wrote;
    46             if (total == size) status = 0;
    47         } else if (xread < 0) {
    48             bb_perror_msg(bb_msg_read_error);
    49             break;
    50         } else if (xread == 0) {
    51             /* All done. */
    52             status = 0;
    53             break;
     53        }
     54        total += rd;
     55        if (status < 0) { /* if we aren't copying till EOF... */
     56            size -= rd;
     57            if (!size) {
     58                /* 'size' bytes copied - all done */
     59                status = 0;
     60                break;
     61            }
    5462        }
    5563    }
    56 
    57 out:
    58     RELEASE_CONFIG_BUFFER(buffer);
    59 
    60     return status ? status : (ssize_t)total;
     64 out:
     65    return status ? -1 : total;
    6166}
    6267
    6368
    64 int bb_copyfd_size(int fd1, int fd2, const off_t size)
     69#if 0
     70void complain_copyfd_and_die(off_t sz)
     71{
     72    if (sz != -1)
     73        bb_error_msg_and_die("short read");
     74    /* if sz == -1, bb_copyfd_XX already complained */
     75    xfunc_die();
     76}
     77#endif
     78
     79off_t bb_copyfd_size(int fd1, int fd2, off_t size)
    6580{
    6681    if (size) {
    67         return(bb_full_fd_action(fd1, fd2, size));
     82        return bb_full_fd_action(fd1, fd2, size);
    6883    }
    69     return(0);
     84    return 0;
    7085}
    7186
    72 int bb_copyfd_eof(int fd1, int fd2)
     87void bb_copyfd_exact_size(int fd1, int fd2, off_t size)
    7388{
    74     return(bb_full_fd_action(fd1, fd2, 0));
     89    off_t sz = bb_copyfd_size(fd1, fd2, size);
     90    if (sz == size)
     91        return;
     92    if (sz != -1)
     93        bb_error_msg_and_die("short read");
     94    /* if sz == -1, bb_copyfd_XX already complained */
     95    xfunc_die();
    7596}
     97
     98off_t bb_copyfd_eof(int fd1, int fd2)
     99{
     100    return bb_full_fd_action(fd1, fd2, 0);
     101}
Note: See TracChangeset for help on using the changeset viewer.