diff options
author | Rob Landley <rob@landley.net> | 2020-06-01 03:29:47 -0500 |
---|---|---|
committer | Rob Landley <rob@landley.net> | 2020-06-01 03:29:47 -0500 |
commit | a0849e8645a197179a5411a2e5b6478a38feff1f (patch) | |
tree | 17547ba514dcb5523dbf3758b6cf404dc546866f | |
parent | 9078931262538847f34d2a76c154ecdd5ad0efe1 (diff) | |
download | toybox-a0849e8645a197179a5411a2e5b6478a38feff1f.tar.gz |
Use copy_file_range() when available.
-rw-r--r-- | lib/portability.c | 30 | ||||
-rw-r--r-- | lib/xwrap.c | 23 |
2 files changed, 30 insertions, 23 deletions
diff --git a/lib/portability.c b/lib/portability.c index 0b6a2d9b..91c01900 100644 --- a/lib/portability.c +++ b/lib/portability.c @@ -606,3 +606,33 @@ int get_block_device_size(int fd, unsigned long long* size) return (ioctl(fd, BLKGETSIZE64, size) >= 0); } #endif + +// TODO copy_file_range +// Return bytes copied from in to out. If bytes <0 copy all of in to out. +// If consuemd isn't null, amount read saved there (return is written or error) +long long sendfile_len(int in, int out, long long bytes, long long *consumed) +{ + long long total = 0, len, ww; + + if (consumed) *consumed = 0; + if (in<0) return 0; + while (bytes != total) { + ww = 0; + len = bytes-total; + if (bytes<0 || len>sizeof(libbuf)) len = sizeof(libbuf); + +#if CFG_TOYBOX_COPYFILERANGE + len = copy_file_range(in, 0, out, 0, bytes, 0); +#else + ww = len = read(in, libbuf, len); +#endif + if (!len && errno==EAGAIN) continue; + if (len<1) break; + if (consumed) *consumed += len; + if (ww && writeall(out, libbuf, len) != len) return -1; + total += len; + } + + return total; +} + diff --git a/lib/xwrap.c b/lib/xwrap.c index b4a52c9e..c09923ba 100644 --- a/lib/xwrap.c +++ b/lib/xwrap.c @@ -814,29 +814,6 @@ void xpidfile(char *name) close(fd); } -// Return bytes copied from in to out. If bytes <0 copy all of in to out. -// If consuemd isn't null, amount read saved there (return is written or error) -long long sendfile_len(int in, int out, long long bytes, long long *consumed) -{ - long long total = 0, len; - - if (consumed) *consumed = 0; - if (in<0) return 0; - while (bytes != total) { - len = bytes-total; - if (bytes<0 || len>sizeof(libbuf)) len = sizeof(libbuf); - - len = read(in, libbuf, len); - if (!len && errno==EAGAIN) continue; - if (len<1) break; - if (consumed) *consumed += len; - if (writeall(out, libbuf, len) != len) return -1; - total += len; - } - - return total; -} - // error_exit if we couldn't copy all bytes long long xsendfile_len(int in, int out, long long bytes) { |