@matheusbsbs / matheus-bagliano / commits / 185c975faa
Fork 0

Do not take mode bits from index after type change.

When we do not trust executable bit from lstat(2), we copied existing ce_mode bits without checking if the filesystem object is a regular file (which is the only thing we apply the "trust executable bit" business) nor if the blob in the index is a regular file (otherwise, we should do the same as registering a new regular file, which is to default non-executable). Noticed by Johannes Sixt. Signed-off-by: Junio C Hamano <junkio@cox.net>

Junio C Hamano committed Feb 16, 2007 at 22:43 UTC 185c975faaa790a98a4e00f124461473283500d6
6 files changed +46 -16
builtin-apply.c
+1 -1
@@ -1988,7 +1988,7 @@ static int check_patch(struct patch *patch, struct patch *prev_patch)
1988 return error("%s: %s", old_name, strerror(errno));
1989
1990 if (!cached)
1991 - st_mode = ntohl(create_ce_mode(st.st_mode));
1991 + st_mode = ntohl(ce_mode_from_stat(ce, st.st_mode));
1992
1993 if (patch->is_new < 0)
1994 patch->is_new = 0;
builtin-update-index.c
+7 -6
@@ -109,16 +109,17 @@ static int add_file_to_cache(const char *path)
109 ce->ce_flags = htons(namelen);
110 fill_stat_cache_info(ce, &st);
111
112 - ce->ce_mode = create_ce_mode(st.st_mode);
113 - if (!trust_executable_bit) {
112 + if (trust_executable_bit)
113 + ce->ce_mode = create_ce_mode(st.st_mode);
114 + else {
115 /* If there is an existing entry, pick the mode bits
116 * from it, otherwise assume unexecutable.
117 */
118 + struct cache_entry *ent;
119 int pos = cache_name_pos(path, namelen);
118 - if (0 <= pos)
119 - ce->ce_mode = active_cache[pos]->ce_mode;
120 - else if (S_ISREG(st.st_mode))
121 - ce->ce_mode = create_ce_mode(S_IFREG | 0666);
120 +
121 + ent = (0 <= pos) ? active_cache[pos] : NULL;
122 + ce->ce_mode = ce_mode_from_stat(ent, st.st_mode);
123 }
124
125 if (index_path(ce->sha1, path, &st, !info_only))
cache.h
+10
@@ -106,6 +106,16 @@ static inline unsigned int create_ce_mode(unsigned int mode)
106 return htonl(S_IFLNK);
107 return htonl(S_IFREG | ce_permissions(mode));
108 }
109 +static inline unsigned int ce_mode_from_stat(struct cache_entry *ce, unsigned int mode)
110 +{
111 + extern int trust_executable_bit;
112 + if (!trust_executable_bit && S_ISREG(mode)) {
113 + if (ce && S_ISREG(ntohl(ce->ce_mode)))
114 + return ce->ce_mode;
115 + return create_ce_mode(0666);
116 + }
117 + return create_ce_mode(mode);
118 +}
119 #define canon_mode(mode) \
120 (S_ISREG(mode) ? (S_IFREG | ce_permissions(mode)) : \
121 S_ISLNK(mode) ? S_IFLNK : S_IFDIR)
diff-lib.c
+1 -3
@@ -170,9 +170,7 @@ static int get_stat_data(struct cache_entry *ce,
170 }
171 changed = ce_match_stat(ce, &st, 0);
172 if (changed) {
173 - mode = create_ce_mode(st.st_mode);
174 - if (!trust_executable_bit && S_ISREG(st.st_mode))
175 - mode = ce->ce_mode;
173 + mode = ce_mode_from_stat(ce, st.st_mode);
174 sha1 = no_sha1;
175 }
176 }
read-cache.c
+7 -6
@@ -344,16 +344,17 @@ int add_file_to_index(const char *path, int verbose)
344 ce->ce_flags = htons(namelen);
345 fill_stat_cache_info(ce, &st);
346
347 - ce->ce_mode = create_ce_mode(st.st_mode);
348 - if (!trust_executable_bit) {
347 + if (trust_executable_bit)
348 + ce->ce_mode = create_ce_mode(st.st_mode);
349 + else {
350 /* If there is an existing entry, pick the mode bits
351 * from it, otherwise assume unexecutable.
352 */
353 + struct cache_entry *ent;
354 int pos = cache_name_pos(path, namelen);
353 - if (pos >= 0)
354 - ce->ce_mode = active_cache[pos]->ce_mode;
355 - else if (S_ISREG(st.st_mode))
356 - ce->ce_mode = create_ce_mode(S_IFREG | 0666);
355 +
356 + ent = (0 <= pos) ? active_cache[pos] : NULL;
357 + ce->ce_mode = ce_mode_from_stat(ent, st.st_mode);
358 }
359
360 if (index_path(ce->sha1, path, &st, 1))
t/t3700-add.sh
+20
@@ -30,6 +30,16 @@ test_expect_success \
30 *) echo fail; git-ls-files --stage xfoo1; (exit 1);;
31 esac'
32
33 +test_expect_success 'git-add: filemode=0 should not get confused by symlink' '
34 + rm -f xfoo1 &&
35 + ln -s foo xfoo1 &&
36 + git-add xfoo1 &&
37 + case "`git-ls-files --stage xfoo1`" in
38 + 120000" "*xfoo1) echo ok;;
39 + *) echo fail; git-ls-files --stage xfoo1; (exit 1);;
40 + esac
41 +'
42 +
43 test_expect_success \
44 'git-update-index --add: Test that executable bit is not used...' \
45 'git config core.filemode 0 &&
@@ -41,6 +51,16 @@ test_expect_success \
51 *) echo fail; git-ls-files --stage xfoo2; (exit 1);;
52 esac'
53
54 +test_expect_success 'git-add: filemode=0 should not get confused by symlink' '
55 + rm -f xfoo2 &&
56 + ln -s foo xfoo2 &&
57 + git update-index --add xfoo2 &&
58 + case "`git-ls-files --stage xfoo2`" in
59 + 120000" "*xfoo2) echo ok;;
60 + *) echo fail; git-ls-files --stage xfoo2; (exit 1);;
61 + esac
62 +'
63 +
64 test_expect_success \
65 'git-update-index --add: Test that executable bit is not used...' \
66 'git config core.filemode 0 &&