Skip to content

Commit 4bcf669

Browse files
committed
Merge branch 'kblees/kb/symlinks'
2 parents 52c0aa9 + 08f6d76 commit 4bcf669

File tree

8 files changed

+533
-165
lines changed

8 files changed

+533
-165
lines changed

compat/mingw.c

Lines changed: 487 additions & 146 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
@@ -18,7 +18,10 @@ static inline void finddata2dirent(struct dirent *ent, WIN32_FIND_DATAW *fdata)
1818
xwcstoutf(ent->d_name, fdata->cFileName, MAX_PATH * 3);
1919

2020
/* Set file type, based on WIN32_FIND_DATA */
21-
if (fdata->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
21+
if ((fdata->dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT)
22+
&& fdata->dwReserved0 == IO_REPARSE_TAG_SYMLINK)
23+
ent->d_type = DT_LNK;
24+
else if (fdata->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
2225
ent->d_type = DT_DIR;
2326
else
2427
ent->d_type = DT_REG;

compat/win32/fscache.c

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

202-
fse->st_mode = file_attr_to_st_mode(fdata->FileAttributes);
203-
fse->dirent.d_type = S_ISDIR(fse->st_mode) ? DT_DIR : DT_REG;
204-
fse->u.s.st_size = fdata->EndOfFile.LowPart |
205-
(((off_t)fdata->EndOfFile.HighPart) << 32);
202+
fse->st_mode = file_attr_to_st_mode(fdata->FileAttributes,
203+
fdata->EaSize);
204+
fse->dirent.d_type = S_ISREG(fse->st_mode) ? DT_REG :
205+
S_ISDIR(fse->st_mode) ? DT_DIR : DT_LNK;
206+
fse->u.s.st_size = S_ISLNK(fse->st_mode) ? MAX_LONG_PATH :
207+
fdata->EndOfFile.LowPart |
208+
(((off_t)fdata->EndOfFile.HighPart) << 32);
206209
filetime_to_timespec((FILETIME *)&(fdata->LastAccessTime),
207210
&(fse->u.s.st_atim));
208211
filetime_to_timespec((FILETIME *)&(fdata->LastWriteTime),
@@ -578,6 +581,18 @@ int fscache_lstat(const char *filename, struct stat *st)
578581
return -1;
579582
}
580583

584+
/*
585+
* Special case symbolic links: FindFirstFile()/FindNextFile() did not
586+
* provide us with the length of the target path.
587+
*/
588+
if (fse->u.s.st_size == MAX_LONG_PATH && S_ISLNK(fse->st_mode)) {
589+
char buf[MAX_LONG_PATH];
590+
int len = readlink(filename, buf, sizeof(buf) - 1);
591+
592+
if (len > 0)
593+
fse->u.s.st_size = len;
594+
}
595+
581596
/* copy stat data */
582597
st->st_ino = 0;
583598
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
@@ -465,6 +465,17 @@ int ie_modified(struct index_state *istate,
465465
* then we know it is.
466466
*/
467467
if ((changed & DATA_CHANGED) &&
468+
#ifdef GIT_WINDOWS_NATIVE
469+
/*
470+
* Work around Git for Windows v2.27.0 fixing a bug where symlinks'
471+
* target path lengths were not read at all, and instead recorded
472+
* as 4096: now, all symlinks would appear as modified.
473+
*
474+
* So let's just special-case symlinks with a target path length
475+
* (i.e. `sd_size`) of 4096 and force them to be re-checked.
476+
*/
477+
(!S_ISLNK(st->st_mode) || ce->ce_stat_data.sd_size != MAX_LONG_PATH) &&
478+
#endif
468479
(S_ISGITLINK(ce->ce_mode) || ce->ce_stat_data.sd_size != 0))
469480
return changed;
470481

strbuf.c

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

561-
#define STRBUF_MAXLINK (2*PATH_MAX)
562-
563561
int strbuf_readlink(struct strbuf *sb, const char *path, size_t hint)
564562
{
565563
size_t oldalloc = sb->alloc;
566564

567565
if (hint < 32)
568566
hint = 32;
569567

570-
while (hint < STRBUF_MAXLINK) {
568+
for (;;) {
571569
ssize_t len;
572570

573-
strbuf_grow(sb, hint);
574-
len = readlink(path, sb->buf, hint);
571+
strbuf_grow(sb, hint + 1);
572+
len = readlink(path, sb->buf, hint + 1);
575573
if (len < 0) {
576574
if (errno != ERANGE)
577575
break;
578-
} else if (len < hint) {
576+
} else if (len <= hint) {
579577
strbuf_setlen(sb, len);
580578
return 0;
581579
}

0 commit comments

Comments
 (0)