From: Baptiste Daroussin Date: Tue, 10 Jan 2023 12:52:19 +0000 (+0100) Subject: rework dumpfd2fd to make fallback more easily testable X-Git-Tag: RELEASE_1_4_0_a2~6 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=9173b286d533ed2bcff635d1e0bf221479e392bd;p=thirdparty%2Fmlmmj.git rework dumpfd2fd to make fallback more easily testable shm are not easy to use in a portable manner --- diff --git a/include/wrappers.h b/include/wrappers.h index ab56037c..8567c97e 100644 --- a/include/wrappers.h +++ b/include/wrappers.h @@ -29,5 +29,6 @@ ssize_t readn(int fd, void *vptr, size_t n); int random_int(void); int dumpfd2fd(int infd, int outfd); +int copy_file(int infd, int outfd, size_t bufsiz); #endif /* WRAPPERS_H */ diff --git a/src/dumpfd2fd.c b/src/dumpfd2fd.c index bd272446..542f82d3 100644 --- a/src/dumpfd2fd.c +++ b/src/dumpfd2fd.c @@ -26,18 +26,19 @@ #include #include #include +#include #include "config.h" #define DUMPBUF 4096 -static int -_copy_file(int from, int to) +int +copy_file(int from, int to, size_t bufsiz) { - char buf[DUMPBUF]; + char buf[bufsiz]; ssize_t r, wresid, w = 0; char *bufp; - r = read(from, buf, DUMPBUF); + r = read(from, buf, bufsiz); if (r < 0) return (r); for (bufp = buf, wresid = r; ; bufp += w, wresid -= w) { @@ -52,25 +53,24 @@ _copy_file(int from, int to) int dumpfd2fd(int from, int to) { -#ifdef HAVE_COPY_FILE_RANGE2 +#ifdef HAVE_COPY_FILE_RANGE bool cfr = true; #endif int r; do { -#ifdef HAVE_COPY_FILE_RANGE2 +#ifdef HAVE_COPY_FILE_RANGE if (cfr) { r = copy_file_range(from, NULL, to, NULL, SSIZE_MAX, 0); if (r < 0 && errno == EINVAL) { - printf("la\n"); /* probably a non seekable FD */ cfr = false; } } if (!cfr) { #endif - r = _copy_file(from, to); -#ifdef HAVE_COPY_FILE_RANGE2 + r = copy_file(from, to, DUMPBUF); +#ifdef HAVE_COPY_FILE_RANGE } #endif } while (r > 0); diff --git a/tests/mlmmj.c b/tests/mlmmj.c index f931a8d1..4eff084f 100644 --- a/tests/mlmmj.c +++ b/tests/mlmmj.c @@ -118,9 +118,9 @@ ATF_TC_WITHOUT_HEAD(statctrl); ATF_TC_WITHOUT_HEAD(is_subbed_in); ATF_TC_WITHOUT_HEAD(getaddrsfromfile); ATF_TC_WITHOUT_HEAD(dumpfd2fd); -ATF_TC_WITHOUT_HEAD(dumpfd2fd_0); -ATF_TC_WITHOUT_HEAD(dumpfd2fd_1); -ATF_TC_WITHOUT_HEAD(dumpfd2fd_2); +ATF_TC_WITHOUT_HEAD(copy_file); +ATF_TC_WITHOUT_HEAD(copy_file_1); +ATF_TC_WITHOUT_HEAD(copy_file_2); ATF_TC_WITHOUT_HEAD(controls); ATF_TC_WITHOUT_HEAD(incindexfile); ATF_TC_WITHOUT_HEAD(log_oper); @@ -1736,60 +1736,50 @@ ATF_TC_BODY(dumpfd2fd, tc) } } -ATF_TC_BODY(dumpfd2fd_0, tc) +ATF_TC_BODY(copy_file, tc) { - /* non seekable */ - int shm = shm_open("/tmp/myshm", O_RDWR | O_CREAT, 0644); - ftruncate(shm, strlen("bla\n")); - dprintf(shm, "bla\n"); - close(shm); - int from = shm_open("/tmp/myshm", O_RDONLY, 0644); + FILE * f = fopen("from.txt", "w+"); + fprintf(f, "bla\n"); + fclose(f); + int from = open("from.txt", O_RDONLY); int to = open("to.txt", O_WRONLY|O_CREAT|O_TRUNC, 0644); - int ret = dumpfd2fd(from, to); + int ret = copy_file(from, to, BUFSIZ); close(from); close(to); - shm_unlink("/tmp/myshm"); - ATF_REQUIRE_EQ_MSG(ret, 0, "Invalid simple copy %d", ret); + ATF_REQUIRE_EQ_MSG(ret, 4, "Invalid simple copy"); if (!atf_utils_compare_file("to.txt", "bla\n")) { atf_utils_cat_file("to.txt", ">>"); atf_tc_fail("Unexpected output"); } } -ATF_TC_BODY(dumpfd2fd_1, tc) +ATF_TC_BODY(copy_file_1, tc) { - /* empty non seekable */ + fclose(fopen("from.txt", "w+")); int to = open("to.txt", O_WRONLY|O_CREAT|O_TRUNC, 0644); - int from = shm_open("/tmp/myshm", O_RDONLY, 0644); - int ret = dumpfd2fd(from, to); + int from = open("from.txt", O_RDONLY); + int ret = copy_file(from, to, BUFSIZ); close(from); close(to); - ATF_REQUIRE_EQ_MSG(ret, -1, "Invalid simple copy %d", ret); + ATF_REQUIRE_EQ_MSG(ret, 0, "Invalid simple copy"); if (!atf_utils_compare_file("to.txt", "")) { atf_utils_cat_file("to.txt", ">>"); atf_tc_fail("Unexpected output"); } } -ATF_TC_BODY(dumpfd2fd_2, tc) +ATF_TC_BODY(copy_file_2, tc) { - /* large non seekable */ - int shm = shm_open("/tmp/myshm", O_RDWR | O_CREAT, 0644); - char buf[5000]; - for (int i = 0; i < 5000; i++) - buf[i]= 'a'; - buf[4999] = '\0'; - ftruncate(shm, strlen(buf)); - write(shm, buf, 5000); - close(shm); - int from = shm_open("/tmp/myshm", O_RDONLY, 0644); + FILE *f = fopen("from.txt", "w+"); + fprintf(f, "bla\n"); + fclose(f); + int from = open("from.txt", O_RDONLY); int to = open("to.txt", O_WRONLY|O_CREAT|O_TRUNC, 0644); - int ret = dumpfd2fd(from, to); + int ret = copy_file(from, to, 3); close(from); close(to); - shm_unlink("/tmp/myshm"); - ATF_REQUIRE_EQ_MSG(ret, 0, "Invalid simple copy %d", ret); - if (!atf_utils_compare_file("to.txt", buf)) { + ATF_REQUIRE_EQ_MSG(ret, 3, "Invalid simple copy"); + if (!atf_utils_compare_file("to.txt", "bla")) { atf_utils_cat_file("to.txt", ">"); atf_tc_fail("Unexpected output"); } @@ -1931,9 +1921,9 @@ ATF_TP_ADD_TCS(tp) ATF_TP_ADD_TC(tp, is_subbed_in); ATF_TP_ADD_TC(tp, getaddrsfromfile); ATF_TP_ADD_TC(tp, dumpfd2fd); - ATF_TP_ADD_TC(tp, dumpfd2fd_0); - ATF_TP_ADD_TC(tp, dumpfd2fd_1); - ATF_TP_ADD_TC(tp, dumpfd2fd_2); + ATF_TP_ADD_TC(tp, copy_file); + ATF_TP_ADD_TC(tp, copy_file_1); + ATF_TP_ADD_TC(tp, copy_file_2); ATF_TP_ADD_TC(tp, controls); ATF_TP_ADD_TC(tp, incindexfile); ATF_TP_ADD_TC(tp, log_oper);