#include <unistd.h>
#include <errno.h>
#include <stdbool.h>
+#include <limits.h>
#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) {
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);
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);
}
}
-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");
}
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);