]> git.ipfire.org Git - thirdparty/FORT-validator.git/commitdiff
Rename the exhaust stream functions, simplify arg list
authorAlberto Leiva Popper <ydahhrk@gmail.com>
Thu, 29 Aug 2024 22:27:23 +0000 (16:27 -0600)
committerAlberto Leiva Popper <ydahhrk@gmail.com>
Thu, 29 Aug 2024 22:27:23 +0000 (16:27 -0600)
src/rsync/rsync.c
test/rsync/rsync_test.c

index 1b7cb083c41787e7434548a48b44e5147227d803..049417b11dde869ea480e472e9cd2f6888cc6ca1 100644 (file)
@@ -160,11 +160,11 @@ log_buffer(char const *buffer, ssize_t read, int type)
 }
 
 /*
- * Consumes (and throws away) all the bytes in read stream fd_pipe[type][0],
+ * Consumes (and throws away) all the bytes in read stream @fd,
  * then closes it after end of stream.
  */
 static int
-read_pipe(int fd_pipe[2][2], int type)
+exhaust_read_fd(int fd, int type)
 {
        char buffer[4096];
        ssize_t count;
@@ -173,7 +173,7 @@ read_pipe(int fd_pipe[2][2], int type)
        long epoch, delta, timeout;
 
        memset(&pfd, 0, sizeof(pfd));
-       pfd[0].fd = fd_pipe[type][0];
+       pfd[0].fd = fd;
        pfd[0].events = POLLIN;
 
        epoch = get_current_millis();
@@ -188,19 +188,19 @@ read_pipe(int fd_pipe[2][2], int type)
                        if (errno == EINTR)
                                continue;
                        pr_val_err("rsync bad poll");
-                       close(fd_pipe[type][0]);
+                       close(fd);
                        return 1;
                }
                if (pfd[0].revents & (POLLERR|POLLNVAL)) {
                        pr_val_err("rsync bad fd: %i", pfd[0].fd);
                        return 1;
                } else if (pfd[0].revents & (POLLIN|POLLHUP)) {
-                       count = read(fd_pipe[type][0], buffer, sizeof(buffer));
+                       count = read(fd, buffer, sizeof(buffer));
                        if (count == -1) {
                                if (errno == EINTR)
                                        continue;
                                error = errno;
-                               close(fd_pipe[type][0]); /* Close read end */
+                               close(fd); /* Close read end */
                                pr_val_err("rsync buffer read error: %s",
                                    strerror(error));
                                return -error;
@@ -214,19 +214,19 @@ read_pipe(int fd_pipe[2][2], int type)
                delta = get_current_millis() - epoch;
                if (delta < 0) {
                        pr_val_err("This clock does not seem monotonic. I'm going to have to give up this rsync.");
-                       close(fd_pipe[type][0]);
+                       close(fd);
                        return 1;
                }
                if (delta >= timeout)
                        goto timed_out; /* Read took too long */
        }
 
-       close(fd_pipe[type][0]); /* Close read end */
+       close(fd); /* Close read end */
        return 0;
 
 timed_out:
        pr_val_err("rsync transfer timeout reached");
-       close(fd_pipe[type][0]);
+       close(fd);
        return 1;
 }
 
@@ -237,24 +237,22 @@ timed_out:
  * (IIRC, waitpid() doesn't do this reliably.)
  */
 static int
-read_pipes(int fds[2][2])
+exhaust_pipes(int fds[2][2])
 {
        int error;
 
-       /* Won't be needed (sterr/stdout write ends) */
-       close(fds[0][1]);
-       close(fds[1][1]);
+       close(fds[0][1]); /* Standard error, write end */
+       close(fds[1][1]); /* Standard output, write end */
 
-       /* stderr pipe */
-       error = read_pipe(fds, 0);
+       /* Standard error, read end */
+       error = exhaust_read_fd(fds[0][0], 0);
        if (error) {
-               /* Close the other pipe pending to read */
                close(fds[1][0]);
                return error;
        }
 
-       /* stdout pipe, always logs to info */
-       return read_pipe(fds, 1);
+       /* Standard output, read end */
+       return exhaust_read_fd(fds[1][0], 1);
 }
 
 /*
@@ -329,7 +327,7 @@ rsync_download(char const *src, char const *dst, bool is_directory)
                }
 
                /* This code is run by us. */
-               error = read_pipes(fork_fds);
+               error = exhaust_pipes(fork_fds);
                if (error)
                        kill(child_pid, SIGTERM); /* Stop the child */
 
index 084788ea342cdb95a893cc6298ae8bcfa1d52afc..586298e0c924d69e1aeda829231f68fd8555cd7f 100644 (file)
@@ -73,52 +73,44 @@ rsync_drip_feed(void *arg)
 }
 
 static void
-prepare_test(int fds[2][2], pthread_t *thread, void *(*rsync_simulator)(void *))
+prepare_test(int fds[2], pthread_t *thread, void *(*rsync_simulator)(void *))
 {
        int *arg;
 
-       ck_assert_int_eq(0, pipe(fds[0]));
-       memset(fds[1], 0, sizeof(fds[1]));
+       ck_assert_int_eq(0, pipe(fds));
 
-       arg = pmalloc(sizeof(fds[0][1]));
-       *arg = fds[0][1];
+       arg = pmalloc(sizeof(fds[1]));
+       *arg = fds[1];
        ck_assert_int_eq(0, pthread_create(thread, NULL, rsync_simulator, arg));
 }
 
 static void
-finish_test(int fds[2][2], pthread_t thread)
+finish_test(pthread_t thread)
 {
-       close(fds[0][0]);
        pthread_join(thread, NULL);
 }
 
 START_TEST(read_pipe_test) /* Tests the read_pipe() function */
 {
-       /*
-        * [0][0] = standard error, read end
-        * [0][1] = standard error, write end
-        * [1][0] = standard output, read end (unused during this test)
-        * [1][1] = standard output, write end (unused during this test)
-        */
-       int fds[2][2];
+       int fds[2];
        pthread_t rsync_writer;
 
        printf("This test needs to exhaust some timeouts. Please be patient.\n");
 
        printf("Normal transfer\n");
        prepare_test(fds, &rsync_writer, rsync_fast);
-       ck_assert_int_eq(0, read_pipe(fds, 0));
-       finish_test(fds, rsync_writer);
+       ck_assert_int_eq(0, exhaust_read_fd(fds[0], 0));
+       finish_test(rsync_writer);
 
        printf("Stalled transfer\n");
        prepare_test(fds, &rsync_writer, rsync_stalled);
-       ck_assert_int_eq(1, read_pipe(fds, 0));
-       finish_test(fds, rsync_writer);
+       ck_assert_int_eq(1, exhaust_read_fd(fds[0], 0));
+       finish_test(rsync_writer);
 
        printf("Drip-feed\n");
        prepare_test(fds, &rsync_writer, rsync_drip_feed);
-       ck_assert_int_eq(1, read_pipe(fds, 0));
-       finish_test(fds, rsync_writer);
+       ck_assert_int_eq(1, exhaust_read_fd(fds[0], 0));
+       finish_test(rsync_writer);
 }
 END_TEST