diff options
author | Ori Bernstein <ori@eigenstate.org> | 2022-11-06 05:09:15 +0000 |
---|---|---|
committer | Ori Bernstein <ori@eigenstate.org> | 2022-11-06 05:09:15 +0000 |
commit | c6e2c07ffbe754be9eeb64be8cf6da19a7a7ee02 (patch) | |
tree | 0fd67535f0633828a782d1585bbb73ded7fe1216 /sys/src/cmd | |
parent | 744475a503e3af4597a4a038c3686c25abb48ee0 (diff) |
patch: preserve permissions of original file when patching
Diffstat (limited to 'sys/src/cmd')
-rw-r--r-- | sys/src/cmd/patch.c | 15 |
1 files changed, 11 insertions, 4 deletions
diff --git a/sys/src/cmd/patch.c b/sys/src/cmd/patch.c index f200c0483..8f1b763bb 100644 --- a/sys/src/cmd/patch.c +++ b/sys/src/cmd/patch.c @@ -39,6 +39,7 @@ struct Fbuf { int lastfuzz; char *buf; int len; + int mode; }; struct Fchg { @@ -389,7 +390,7 @@ mkpath(char *path) } void -blat(char *old, char *new, char *o, usize len) +blat(char *old, char *new, char *o, usize len, int mode) { char *tmp; int fd; @@ -403,7 +404,7 @@ blat(char *old, char *new, char *o, usize len) sysfatal("mkpath %s: %r", new); if((tmp = smprint("%s.tmp%d", new, getpid())) == nil) sysfatal("smprint: %r"); - if((fd = create(tmp, OWRITE, 0666)) == -1) + if((fd = create(tmp, OWRITE, mode|0200)) == -1) sysfatal("open %s: %r", tmp); if(write(fd, o, len) != len) sysfatal("write %s: %r", tmp); @@ -466,9 +467,12 @@ slurp(Fbuf *f, char *path) int n, i, fd, sz, len, nlines, linesz; char *buf; int *lines; + Dir *d; if((fd = open(path, OREAD)) == -1) sysfatal("open %s: %r", path); + if((d = dirfstat(fd)) == nil) + sysfatal("stat %s: %r", path); sz = 8192; len = 0; buf = emalloc(sz); @@ -505,6 +509,9 @@ slurp(Fbuf *f, char *path) f->nlines = nlines; f->lastln = 0; f->lastfuzz = 0; + f->mode = d->mode; + free(d); + close(fd); } char* @@ -587,7 +594,7 @@ apply(Patch *p, char *fname) if(curfile != nil){ if(!dryrun) o = append(o, &osz, e, f.buf + f.len); - blat(prevh->oldpath, prevh->newpath, o, osz); + blat(prevh->oldpath, prevh->newpath, o, osz, f.mode); osz = 0; } if(!dryrun){ @@ -608,7 +615,7 @@ apply(Patch *p, char *fname) if(curfile != nil){ if(!dryrun) o = append(o, &osz, e, f.buf + f.len); - blat(h->oldpath, h->newpath, o, osz); + blat(h->oldpath, h->newpath, o, osz, f.mode); } free(o); return 0; |