logo

utils-std

Collection of commonly available Unix tools
commit: 8bc0e68a8dcfd5a1ad4d16d111d6d72a91e78039
parent 104244c9d81efd49e8445283eb4f0782eddb6ad9
Author: Haelwenn (lanodan) Monnier <contact@hacktivis.me>
Date:   Sun,  5 May 2024 02:40:45 +0200

cmd/install: Add read-write loop for systems without copy_file_range

Diffstat:

Mcmd/install.c26++++++++++++++++++++++++++
Mconfigure5+----
Mtest-cmd/install.t12++++++++++++
3 files changed, 39 insertions(+), 4 deletions(-)

diff --git a/cmd/install.c b/cmd/install.c @@ -125,6 +125,7 @@ do_install(char *src, char *dest, bool is_dir) return -1; } +#ifdef HAS_COPY_FILE_RANGE off_t len = src_stat.st_size; off_t ret = -1; do @@ -142,6 +143,31 @@ do_install(char *src, char *dest, bool is_dir) } len -= ret; } while(len > 0 && ret > 0); +#else +#undef MIN +#define MIN(a, b) (((a) < (b)) ? (a) : (b)) + off_t len = src_stat.st_size; + do + { + char buf[BUFSIZ]; + ssize_t nread = read(src_fd, buf, MIN(BUFSIZ, len)); + if(nread < 0) + { + fprintf(stderr, "install: Error reading from '%s': %s\n", src, strerror(errno)); + errno = 0; + return -1; + } + + ssize_t nwrite = write(dest_fd, buf, (size_t)nread); + if(nwrite < 0) + { + fprintf(stderr, "install: Error writing to '%s': %s\n", dest_path, strerror(errno)); + errno = 0; + return -1; + } + len -= nwrite; + } while(len > 0); +#endif assert(errno == 0); diff --git a/configure b/configure @@ -241,10 +241,7 @@ check_conftest configure.d/splice.c && CFLAGS="${CFLAGS} -DHAS_SPLICE" if check_conftest configure.d/copy_file_range.c then - CFLAGS="${CFLAGS} -DCOPY_FILE_RANGE" -else - echo 'Disabling cmd/install' - echo 'cmd/install.' >> target_filter + CFLAGS="${CFLAGS} -DHAS_COPY_FILE_RANGE" fi if ! check_conftest configure.d/reallocarray.c; then diff --git a/test-cmd/install.t b/test-cmd/install.t @@ -20,6 +20,18 @@ chmod: Permissions changed from 00664/-rw-rw-r-- to 00755/-rwxr-xr-x for 'src' $ rm -f src dest +Integrity check + $ seq 1 2048 > content + $ wc -c content + 9133 content + $ install content content.inst + $ wc -c content content.inst + 9133 content + 9133 content.inst + 18266 total + $ cmp content content.inst + $ rm content content.inst + install -d $ test ! -e foo.d $ install -d foo.d