From 7ab529557d8f22b2f304e1f78f5ddad18785a4bd Mon Sep 17 00:00:00 2001 From: James Farrell Date: Tue, 17 May 2022 23:25:21 +0000 Subject: [PATCH] Handle diffing FIFOs. Also exit immediately if a seek fails. In addition to the supplied test, also tested manually with: ./toybox diff <(echo -e "1\n2") <(echo -e "1\n3") However, the "testing" function in runtest.sh doesn't handle such syntax. --- tests/diff.test | 15 ++++++++++++++- toys/pending/diff.c | 31 +++++++++++++++++++++++++------ 2 files changed, 39 insertions(+), 7 deletions(-) diff --git a/tests/diff.test b/tests/diff.test index c87c7a66..1f56ae64 100644 --- a/tests/diff.test +++ b/tests/diff.test @@ -41,4 +41,17 @@ testing "--strip-trailing-cr on" "diff -u --strip-trailing-cr a b" "" "" "" echo -e "1\n2" > aa echo -e "1\n3" > bb -testing "line format" "diff --unchanged-line-format=U%l --old-line-format=D%l --new-line-format=A%l aa bb" "U1D2A3" "" "" \ No newline at end of file +testing "line format" "diff --unchanged-line-format=U%l --old-line-format=D%l --new-line-format=A%l aa bb" "U1D2A3" "" "" + +mkfifo fifo1 +mkfifo fifo2 +echo -e "1\n2" > fifo1& +echo -e "1\n3" > fifo2& +expected='--- fifo1 ++++ fifo2 +@@ -1,2 +1,2 @@ + 1 +-2 ++3 +' +testing "fifos" "diff -L fifo1 -L fifo2 fifo1 fifo2" "$expected" "" "" \ No newline at end of file diff --git a/toys/pending/diff.c b/toys/pending/diff.c index e8a134a9..aee7253f 100644 --- a/toys/pending/diff.c +++ b/toys/pending/diff.c @@ -203,6 +203,20 @@ static FILE* read_stdin() return fdopen(tmpfd, "r"); } +static FILE* read_fifo(char* name) +{ + char *tmp_name; + int srcfd = open(name, O_RDONLY); + int tmpfd = xtempfile("fifo", &tmp_name); + + unlink(tmp_name); + free(tmp_name); + + xsendfile(srcfd, tmpfd); + close(srcfd); + return fdopen(tmpfd, "r"); +} + static int read_tok(FILE *fp, off_t *off, int tok) { int t = 0, is_space; @@ -285,7 +299,7 @@ static int * create_j_vector() v[i] = xzalloc(size * sizeof(struct v_vector)); TT.offset[i] = xzalloc(size * sizeof(int)); file[i].len = 0; - fseek(file[i].fp, 0, SEEK_SET); + if (fseek(file[i].fp, 0, SEEK_SET)) perror_exit("fseek failed"); while (1) { tok = read_tok(file[i].fp, &off, tok); @@ -365,8 +379,8 @@ static int * create_j_vector() for (i = 1; i <= file[0].len; i++) { // jackpot? if (!J[i]) continue; - fseek(file[0].fp, TT.offset[0][i - 1], SEEK_SET); - fseek(file[1].fp, TT.offset[1][J[i] - 1], SEEK_SET); + if (fseek(file[0].fp, TT.offset[0][i - 1], SEEK_SET)) perror_exit("fseek failed"); + if (fseek(file[1].fp, TT.offset[1][J[i] - 1], SEEK_SET)) perror_exit("fseek failed"); for (j = J[i]; i <= file[0].len && J[i] == j; i++, j++) { int tok0 = 0, tok1 = 0; @@ -393,6 +407,7 @@ static int *diff(char **files) for (i = 0; i < 2; i++) { if (IS_STDIN(files[i])) file[i].fp = read_stdin(); + else if (S_ISFIFO(TT.st[i].st_mode)) file[i].fp = read_fifo(files[i]); else file[i].fp = fopen(files[i], "r"); if (!file[i].fp){ @@ -406,8 +421,8 @@ static int *diff(char **files) bufi = toybuf; bufj = (toybuf + s); - fseek(file[0].fp, 0, SEEK_SET); - fseek(file[1].fp, 0, SEEK_SET); + if (fseek(file[0].fp, 0, SEEK_SET)) perror_exit("fseek failed"); + if (fseek(file[1].fp, 0, SEEK_SET)) perror_exit("fseek failed"); if (toys.optflags & FLAG_a) return create_j_vector(); @@ -443,7 +458,7 @@ static void print_diff(int a, int b, char c, int *off_set, FILE *fp) } for (i = a; i <= b; i++) { - fseek(fp, off_set[i - 1], SEEK_SET); + if (fseek(fp, off_set[i - 1], SEEK_SET)) perror_exit("fseek failed"); if (TT.new_line_format) { if (c == '+') fmt = TT.new_line_format; else if (c == '-') fmt = TT.old_line_format; @@ -907,6 +922,10 @@ void diff_main(void) && (S_ISDIR(TT.st[0].st_mode) || S_ISDIR(TT.st[1].st_mode))) error_exit("can't compare stdin to directory"); + if ((S_ISFIFO(TT.st[0].st_mode) || S_ISFIFO(TT.st[1].st_mode)) + && (S_ISDIR(TT.st[0].st_mode) || S_ISDIR(TT.st[1].st_mode))) + error_exit("can't compare pipe to directory"); + if (TT.unchanged_line_format || TT.old_line_format || TT.new_line_format) { if (S_ISDIR(TT.st[0].st_mode) && S_ISDIR(TT.st[1].st_mode)) error_exit("can't use line format with directories"); -- 2.39.2