ref: 87398fde457aab2082f3c07678f35004e19569be
parent: 5eb0d57733fe7db870841fe1b81a2f3c106d4e57
author: Michael Forney <mforney@mforney.org>
date: Mon Oct 3 01:34:48 EDT 2022
patch: fix deletion of files If we are deleting a file, we do not want to attempt to create an empty /dev/null.tmp$pid. finish() doesn't look at tmp for deletions during a successful run, so leave it set to nil. On a failure, guard tmp removal on tmp != nil.
--- a/sys/src/cmd/patch.c
+++ b/sys/src/cmd/patch.c
@@ -393,12 +393,11 @@
char *tmp;
int fd;
- if(strcmp(new, "/dev/null") == 0 && len != 0){
- sysfatal("diff modifies removed file");
- return;
- }
tmp = nil;
- if(!dryrun){
+ if(strcmp(new, "/dev/null") == 0){
+ if(len != 0)
+ sysfatal("diff modifies removed file");
+ }else if(!dryrun){
if(mkpath(new) == -1)
sysfatal("mkpath %s: %r", new);
if((tmp = smprint("%s.tmp%d", new, getpid())) == nil)
@@ -428,7 +427,7 @@
for(i = 0; i < nchanged; i++){
c = &changed[i];
if(!ok){
- if(remove(c->tmp) == -1)
+ if(c->tmp != nil && remove(c->tmp) == -1)
fprint(2, "remove %s: %r\n", c->tmp);
goto Free;
}