Make core.sharedRepository work under cygwin 1.7

When core.sharedRepository is used, set_shared_perm() in path.c needs lstat() to return the correct POSIX permissions. The default for cygwin is core.ignoreCygwinFSTricks = false, which means that the fast implementation in do_stat() is used instead of lstat(). lstat() under cygwin uses the Windows security model to implement POSIX-like permissions. The user, group or everyone bits can be set individually. do_stat() simplifes the file permission bits, and may return a wrong value. The read-only attribute of a file is used to calculate the permissions, resulting in either rw-r--r-- or r--r--r-- One effect of the simplified do_stat() is that t1301 fails. Add a function cygwin_get_st_mode_bits() which returns the POSIX permissions. When not compiling for cygwin, true_mode_bits() in path.c is used. Side note: t1301 passes under cygwin 1.5. The "user write" bit is synchronized with the "read only" attribute of a file: $ chmod 444 x $ attrib x A R C:\temp\pt\x cygwin 1.7 would show A C:\temp\pt\x Signed-off-by: Torsten Bögershausen <tboegi@web.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Torsten Bögershausen committed Mar 23, 2013 at 13:40 UTC 0117c2f043183fb99e9b046b0df7d64c1b296624
4 files changed +36 -3
compat/cygwin.c
+13
@@ -1,3 +1,4 @@
1 +#define CYGWIN_C
2 #define WIN32_LEAN_AND_MEAN
3 #ifdef CYGWIN_V15_WIN32API
4 #include "../git-compat-util.h"
@@ -10,6 +11,18 @@
11 #endif
12 #include "../cache.h" /* to read configuration */
13
14 +/*
15 + * Return POSIX permission bits, regardless of core.ignorecygwinfstricks
16 + */
17 +int cygwin_get_st_mode_bits(const char *path, int *mode)
18 +{
19 + struct stat st;
20 + if (lstat(path, &st) < 0)
21 + return -1;
22 + *mode = st.st_mode;
23 + return 0;
24 +}
25 +
26 static inline void filetime_to_timespec(const FILETIME *ft, struct timespec *ts)
27 {
28 long long winTime = ((long long)ft->dwHighDateTime << 32) +
compat/cygwin.h
+5
@@ -4,6 +4,11 @@
4 typedef int (*stat_fn_t)(const char*, struct stat*);
5 extern stat_fn_t cygwin_stat_fn;
6 extern stat_fn_t cygwin_lstat_fn;
7 +int cygwin_get_st_mode_bits(const char *path, int *mode);
8
9 +#define get_st_mode_bits(p,m) cygwin_get_st_mode_bits((p),(m))
10 +#ifndef CYGWIN_C
11 +/* cygwin.c needs the original lstat() */
12 #define stat(path, buf) (*cygwin_stat_fn)(path, buf)
13 #define lstat(path, buf) (*cygwin_lstat_fn)(path, buf)
14 +#endif
git-compat-util.h
+1
@@ -163,6 +163,7 @@
163 typedef long intptr_t;
164 typedef unsigned long uintptr_t;
165 #endif
166 +int get_st_mode_bits(const char *path, int *mode);
167 #if defined(__CYGWIN__)
168 #undef _XOPEN_SOURCE
169 #include <grp.h>
path.c
+17 -3
@@ -14,6 +14,22 @@
14 #include "strbuf.h"
15 #include "string-list.h"
16
17 +#ifndef get_st_mode_bits
18 +/*
19 + * The replacement lstat(2) we use on Cygwin is incomplete and
20 + * may return wrong permission bits. Most of the time we do not care,
21 + * but the callsites of this wrapper do care.
22 + */
23 +int get_st_mode_bits(const char *path, int *mode)
24 +{
25 + struct stat st;
26 + if (lstat(path, &st) < 0)
27 + return -1;
28 + *mode = st.st_mode;
29 + return 0;
30 +}
31 +#endif
32 +
33 static char bad_path[] = "/bad-path/";
34
35 static char *get_pathname(void)
@@ -391,7 +407,6 @@ const char *enter_repo(const char *path, int strict)
407
408 int set_shared_perm(const char *path, int mode)
409 {
394 - struct stat st;
410 int tweak, shared, orig_mode;
411
412 if (!shared_repository) {
@@ -400,9 +415,8 @@ int set_shared_perm(const char *path, int mode)
415 return 0;
416 }
417 if (!mode) {
403 - if (lstat(path, &st) < 0)
418 + if (get_st_mode_bits(path, &mode) < 0)
419 return -1;
405 - mode = st.st_mode;
420 orig_mode = mode;
421 } else
422 orig_mode = 0;