diff options
author | Rob Landley <rob@landley.net> | 2022-11-01 16:37:25 -0500 |
---|---|---|
committer | Rob Landley <rob@landley.net> | 2022-11-01 16:37:25 -0500 |
commit | f02f67de63bf8781cd8a48f9ae9f17189909914f (patch) | |
tree | af8c372dd3384e5a375b647c7126e60b467b170c | |
parent | a249f56843ee4c406c0e09d91590a645f4f05461 (diff) | |
download | toybox-f02f67de63bf8781cd8a48f9ae9f17189909914f.tar.gz |
patch --dry-run should not create or delete files.
-rwxr-xr-x | tests/patch.test | 26 | ||||
-rw-r--r-- | toys/posix/patch.c | 9 |
2 files changed, 32 insertions, 3 deletions
diff --git a/tests/patch.test b/tests/patch.test index 5cc60014..c18bfe1e 100755 --- a/tests/patch.test +++ b/tests/patch.test @@ -4,6 +4,14 @@ #testing "name" "command" "result" "infile" "stdin" +testing "dry run doesn't create file" \ + "patch --dry-run >/dev/null && [ ! -e bork ] && echo yes" "yes\n" "" " +--- /dev/null ++++ bork +@@ -0,0 +1,1 @@ ++one +" + testing "create file" "patch >/dev/null && cat bork" "one\ntwo\nthree\n" "" " --- /dev/null +++ bork @@ -12,6 +20,7 @@ testing "create file" "patch >/dev/null && cat bork" "one\ntwo\nthree\n" "" " +two +three " + testing "insert in middle" "patch > /dev/null && cat bork" \ "one\nfour\ntwo\nthree\n" "" " --- bork @@ -85,6 +94,23 @@ testing "quoted name" "patch > /dev/null && cat 'fruit bat'" \ @@ -0,0 +1 @@ +hello ' + +testing "dry run doesn't delete file" \ + "patch --dry-run > /dev/null && [ -e 'fruit bat' ] && echo yes" "yes\n" "" ' +--- "fruit bat" ++++ /dev/null +@@ -1 +0,0 @@ +-hello +' + +testing "delete file" \ + "patch > /dev/null && [ ! -e 'fruit bat' ] && echo yes" "yes\n" "" ' +--- "fruit bat" ++++ /dev/null +@@ -1 +0,0 @@ +-hello +' + # todo bork bork2 # We hit a bug, test the bugfix. diff --git a/toys/posix/patch.c b/toys/posix/patch.c index 4b8c61c3..eebcddaa 100644 --- a/toys/posix/patch.c +++ b/toys/posix/patch.c @@ -449,7 +449,7 @@ void patch_main(void) if (del) { if (!FLAG(s)) printf("removing %s\n", name); - xunlink(name); + if (!FLAG(dry_run)) xunlink(name); state = 0; // If we've got a file to open, do so. } else if (!FLAG(p) || i <= TT.p) { @@ -457,8 +457,11 @@ void patch_main(void) if ((!strcmp(oldname, "/dev/null") || !oldsum) && access(name, F_OK)) { if (!FLAG(s)) printf("creating %s\n", name); - if (mkpath(name)) perror_exit("mkpath %s", name); - TT.filein = xcreate(name, O_CREAT|O_EXCL|O_RDWR, 0666); + if (FLAG(dry_run)) TT.filein = xopen("/dev/null", O_RDWR); + else { + if (mkpath(name)) perror_exit("mkpath %s", name); + TT.filein = xcreate(name, O_CREAT|O_EXCL|O_RDWR, 0666); + } } else { if (!FLAG(s)) printf("patching %s\n", name); TT.filein = xopenro(name); |