Skip to content

Commit 09055ff

Browse files
committed
Merge branch 'kblees/kb/symlinks'
2 parents 7e9caa7 + 5b1fc7a commit 09055ff

File tree

8 files changed

+531
-163
lines changed

8 files changed

+531
-163
lines changed

compat/mingw.c

Lines changed: 485 additions & 144 deletions
Large diffs are not rendered by default.

compat/mingw.h

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -123,10 +123,6 @@ struct utsname {
123123
* trivial stubs
124124
*/
125125

126-
static inline int readlink(const char *path, char *buf, size_t bufsiz)
127-
{ errno = ENOSYS; return -1; }
128-
static inline int symlink(const char *oldpath, const char *newpath)
129-
{ errno = ENOSYS; return -1; }
130126
static inline int fchmod(int fildes, mode_t mode)
131127
{ errno = ENOSYS; return -1; }
132128
#ifndef __MINGW64_VERSION_MAJOR
@@ -217,6 +213,8 @@ int setitimer(int type, struct itimerval *in, struct itimerval *out);
217213
int sigaction(int sig, struct sigaction *in, struct sigaction *out);
218214
int link(const char *oldpath, const char *newpath);
219215
int uname(struct utsname *buf);
216+
int symlink(const char *target, const char *link);
217+
int readlink(const char *path, char *buf, size_t bufsiz);
220218

221219
/*
222220
* replacements of existing functions

compat/win32.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,12 @@
66
#include <windows.h>
77
#endif
88

9-
static inline int file_attr_to_st_mode (DWORD attr)
9+
static inline int file_attr_to_st_mode (DWORD attr, DWORD tag)
1010
{
1111
int fMode = S_IREAD;
12-
if (attr & FILE_ATTRIBUTE_DIRECTORY)
12+
if ((attr & FILE_ATTRIBUTE_REPARSE_POINT) && tag == IO_REPARSE_TAG_SYMLINK)
13+
fMode |= S_IFLNK;
14+
else if (attr & FILE_ATTRIBUTE_DIRECTORY)
1315
fMode |= S_IFDIR;
1416
else
1517
fMode |= S_IFREG;

compat/win32/dirent.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,10 @@ static inline void finddata2dirent(struct dirent *ent, WIN32_FIND_DATAW *fdata)
1515
xwcstoutf(ent->d_name, fdata->cFileName, MAX_PATH * 3);
1616

1717
/* Set file type, based on WIN32_FIND_DATA */
18-
if (fdata->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
18+
if ((fdata->dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT)
19+
&& fdata->dwReserved0 == IO_REPARSE_TAG_SYMLINK)
20+
ent->d_type = DT_LNK;
21+
else if (fdata->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
1922
ent->d_type = DT_DIR;
2023
else
2124
ent->d_type = DT_REG;

compat/win32/fscache.c

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -194,10 +194,13 @@ static struct fsentry *fseentry_create_entry(struct fscache *cache,
194194
fdata->FileAttributes & FILE_ATTRIBUTE_REPARSE_POINT ?
195195
fdata->EaSize : 0;
196196

197-
fse->st_mode = file_attr_to_st_mode(fdata->FileAttributes);
198-
fse->dirent.d_type = S_ISDIR(fse->st_mode) ? DT_DIR : DT_REG;
199-
fse->u.s.st_size = fdata->EndOfFile.LowPart |
200-
(((off_t)fdata->EndOfFile.HighPart) << 32);
197+
fse->st_mode = file_attr_to_st_mode(fdata->FileAttributes,
198+
fdata->EaSize);
199+
fse->dirent.d_type = S_ISREG(fse->st_mode) ? DT_REG :
200+
S_ISDIR(fse->st_mode) ? DT_DIR : DT_LNK;
201+
fse->u.s.st_size = S_ISLNK(fse->st_mode) ? MAX_LONG_PATH :
202+
fdata->EndOfFile.LowPart |
203+
(((off_t)fdata->EndOfFile.HighPart) << 32);
201204
filetime_to_timespec((FILETIME *)&(fdata->LastAccessTime),
202205
&(fse->u.s.st_atim));
203206
filetime_to_timespec((FILETIME *)&(fdata->LastWriteTime),
@@ -571,6 +574,18 @@ int fscache_lstat(const char *filename, struct stat *st)
571574
if (!fse)
572575
return -1;
573576

577+
/*
578+
* Special case symbolic links: FindFirstFile()/FindNextFile() did not
579+
* provide us with the length of the target path.
580+
*/
581+
if (fse->u.s.st_size == MAX_LONG_PATH && S_ISLNK(fse->st_mode)) {
582+
char buf[MAX_LONG_PATH];
583+
int len = readlink(filename, buf, sizeof(buf) - 1);
584+
585+
if (len > 0)
586+
fse->u.s.st_size = len;
587+
}
588+
574589
/* copy stat data */
575590
st->st_ino = 0;
576591
st->st_gid = 0;

lockfile.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,14 @@ static void trim_last_path_component(struct strbuf *path)
1717
int i = path->len;
1818

1919
/* back up past trailing slashes, if any */
20-
while (i && path->buf[i - 1] == '/')
20+
while (i && is_dir_sep(path->buf[i - 1]))
2121
i--;
2222

2323
/*
2424
* then go backwards until a slash, or the beginning of the
2525
* string
2626
*/
27-
while (i && path->buf[i - 1] != '/')
27+
while (i && !is_dir_sep(path->buf[i - 1]))
2828
i--;
2929

3030
strbuf_setlen(path, i);

read-cache.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -453,6 +453,17 @@ int ie_modified(struct index_state *istate,
453453
* then we know it is.
454454
*/
455455
if ((changed & DATA_CHANGED) &&
456+
#ifdef GIT_WINDOWS_NATIVE
457+
/*
458+
* Work around Git for Windows v2.27.0 fixing a bug where symlinks'
459+
* target path lengths were not read at all, and instead recorded
460+
* as 4096: now, all symlinks would appear as modified.
461+
*
462+
* So let's just special-case symlinks with a target path length
463+
* (i.e. `sd_size`) of 4096 and force them to be re-checked.
464+
*/
465+
(!S_ISLNK(st->st_mode) || ce->ce_stat_data.sd_size != MAX_LONG_PATH) &&
466+
#endif
456467
(S_ISGITLINK(ce->ce_mode) || ce->ce_stat_data.sd_size != 0))
457468
return changed;
458469

strbuf.c

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -556,24 +556,22 @@ ssize_t strbuf_write(struct strbuf *sb, FILE *f)
556556
return sb->len ? fwrite(sb->buf, 1, sb->len, f) : 0;
557557
}
558558

559-
#define STRBUF_MAXLINK (2*PATH_MAX)
560-
561559
int strbuf_readlink(struct strbuf *sb, const char *path, size_t hint)
562560
{
563561
size_t oldalloc = sb->alloc;
564562

565563
if (hint < 32)
566564
hint = 32;
567565

568-
while (hint < STRBUF_MAXLINK) {
566+
for (;;) {
569567
ssize_t len;
570568

571-
strbuf_grow(sb, hint);
572-
len = readlink(path, sb->buf, hint);
569+
strbuf_grow(sb, hint + 1);
570+
len = readlink(path, sb->buf, hint + 1);
573571
if (len < 0) {
574572
if (errno != ERANGE)
575573
break;
576-
} else if (len < hint) {
574+
} else if (len <= hint) {
577575
strbuf_setlen(sb, len);
578576
return 0;
579577
}

0 commit comments

Comments
 (0)