got patch: keep permissions after patching a file ok stsp@
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
diff --git a/got/got.c b/got/got.c
index a56994a..6e4f19a 100644
--- a/got/got.c
+++ b/got/got.c
@@ -7253,7 +7253,7 @@ cmd_patch(int argc, char *argv[])
goto done;
#ifndef PROFILE
- if (pledge("stdio rpath wpath cpath proc exec sendfd flock",
+ if (pledge("stdio rpath wpath cpath fattr proc exec sendfd flock",
NULL) == -1)
err(1, "pledge");
#endif
diff --git a/lib/patch.c b/lib/patch.c
index 93a9570..8fc9edf 100644
--- a/lib/patch.c
+++ b/lib/patch.c
@@ -388,10 +388,12 @@ apply_hunk(FILE *tmp, struct got_patch_hunk *h, long *lineno)
}
static const struct got_error *
-patch_file(struct got_patch *p, const char *path, FILE *tmp, int nop)
+patch_file(struct got_patch *p, const char *path, FILE *tmp, int nop,
+ mode_t *mode)
{
const struct got_error *err = NULL;
struct got_patch_hunk *h;
+ struct stat sb;
size_t i;
long lineno = 0;
FILE *orig;
@@ -418,6 +420,12 @@ patch_file(struct got_patch *p, const char *path, FILE *tmp, int nop)
goto done;
}
+ if (fstat(fileno(orig), &sb) == -1) {
+ err = got_error_from_errno("fstat");
+ goto done;
+ }
+ *mode = sb.st_mode;
+
copypos = 0;
STAILQ_FOREACH(h, &p->head, entries) {
if (h->lines == NULL)
@@ -466,15 +474,9 @@ patch_file(struct got_patch *p, const char *path, FILE *tmp, int nop)
}
}
-
- if (p->new == NULL) {
- struct stat sb;
-
- if (fstat(fileno(orig), &sb) == -1)
- err = got_error_from_errno("fstat");
- else if (sb.st_size != copypos)
- err = got_error(GOT_ERR_PATCH_DONT_APPLY);
- } else if (!nop && !feof(orig))
+ if (p->new == NULL && sb.st_size != copypos)
+ err = got_error(GOT_ERR_PATCH_DONT_APPLY);
+ else if (!nop && !feof(orig))
err = copy(tmp, orig, copypos, -1);
done:
@@ -596,6 +598,7 @@ apply_patch(struct got_worktree *worktree, struct got_repository *repo,
char *oldpath = NULL, *newpath = NULL;
char *tmppath = NULL, *template = NULL;
FILE *tmp = NULL;
+ mode_t mode = GOT_DEFAULT_FILE_MODE;
TAILQ_INIT(&oldpaths);
TAILQ_INIT(&newpaths);
@@ -628,7 +631,7 @@ apply_patch(struct got_worktree *worktree, struct got_repository *repo,
err = got_opentemp_named(&tmppath, &tmp, template);
if (err)
goto done;
- err = patch_file(p, oldpath, tmp, nop);
+ err = patch_file(p, oldpath, tmp, nop, &mode);
if (err)
goto done;
@@ -641,6 +644,11 @@ apply_patch(struct got_worktree *worktree, struct got_repository *repo,
goto done;
}
+ if (fchmod(fileno(tmp), mode) == -1) {
+ err = got_error_from_errno2("chmod", newpath);
+ goto done;
+ }
+
if (rename(tmppath, newpath) == -1) {
err = got_error_from_errno3("rename", tmppath, newpath);
goto done;
diff --git a/regress/cmdline/patch.sh b/regress/cmdline/patch.sh
index 41f290c..6d468d0 100755
--- a/regress/cmdline/patch.sh
+++ b/regress/cmdline/patch.sh
@@ -967,6 +967,47 @@ EOF
test_done $testroot $ret
}
+test_patch_preserve_perm() {
+ local testroot=`test_init patch_preserve_perm`
+
+ got checkout $testroot/repo $testroot/wt > /dev/null
+ ret=$?
+ if [ $ret -ne 0 ]; then
+ test_done $testroot $ret
+ return 1
+ fi
+
+ chmod +x $testroot/wt/alpha
+ (cd $testroot/wt && got commit -m 'alpha executable') > /dev/null
+ ret=$?
+ if [ $ret -ne 0 ]; then
+ test_done $testroot $ret
+ return 1
+ fi
+
+ cat <<EOF > $testroot/wt/patch
+--- alpha
++++ alpha
+@@ -1 +1,2 @@
+ alpha
++was edited
+EOF
+
+ (cd $testroot/wt && got patch patch) > /dev/null
+ ret=$?
+ if [ $ret -ne 0 ]; then
+ test_done $testroot $ret
+ return 1
+ fi
+
+ if [ ! -x $testroot/wt/alpha ]; then
+ echo "alpha is no more executable!" >&2
+ test_done $testroot 1
+ return 1
+ fi
+ test_done $testroot 0
+}
+
test_parseargs "$@"
run_test test_patch_simple_add_file
run_test test_patch_simple_rm_file
@@ -982,3 +1023,4 @@ run_test test_patch_equals_for_context
run_test test_patch_rename
run_test test_patch_illegal_status
run_test test_patch_nop
+run_test test_patch_preserve_perm