Skip to content

Commit c3fb67e

Browse files
dschoGit for Windows Build Agent
authored andcommitted
Merge branch 'kblees/kb/symlinks'
2 parents 50f0d84 + 1c6bf8d commit c3fb67e

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
@@ -200,10 +200,13 @@ static struct fsentry *fseentry_create_entry(struct fscache *cache,
200200
fdata->FileAttributes & FILE_ATTRIBUTE_REPARSE_POINT ?
201201
fdata->EaSize : 0;
202202

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

585+
/*
586+
* Special case symbolic links: FindFirstFile()/FindNextFile() did not
587+
* provide us with the length of the target path.
588+
*/
589+
if (fse->u.s.st_size == MAX_LONG_PATH && S_ISLNK(fse->st_mode)) {
590+
char buf[MAX_LONG_PATH];
591+
int len = readlink(filename, buf, sizeof(buf) - 1);
592+
593+
if (len > 0)
594+
fse->u.s.st_size = len;
595+
}
596+
582597
/* copy stat data */
583598
st->st_ino = 0;
584599
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
@@ -479,6 +479,17 @@ int ie_modified(struct index_state *istate,
479479
* then we know it is.
480480
*/
481481
if ((changed & DATA_CHANGED) &&
482+
#ifdef GIT_WINDOWS_NATIVE
483+
/*
484+
* Work around Git for Windows v2.27.0 fixing a bug where symlinks'
485+
* target path lengths were not read at all, and instead recorded
486+
* as 4096: now, all symlinks would appear as modified.
487+
*
488+
* So let's just special-case symlinks with a target path length
489+
* (i.e. `sd_size`) of 4096 and force them to be re-checked.
490+
*/
491+
(!S_ISLNK(st->st_mode) || ce->ce_stat_data.sd_size != MAX_LONG_PATH) &&
492+
#endif
482493
(S_ISGITLINK(ce->ce_mode) || ce->ce_stat_data.sd_size != 0))
483494
return changed;
484495

strbuf.c

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

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

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

571-
while (hint < STRBUF_MAXLINK) {
569+
for (;;) {
572570
ssize_t len;
573571

574-
strbuf_grow(sb, hint);
575-
len = readlink(path, sb->buf, hint);
572+
strbuf_grow(sb, hint + 1);
573+
len = readlink(path, sb->buf, hint + 1);
576574
if (len < 0) {
577575
if (errno != ERANGE)
578576
break;
579-
} else if (len < hint) {
577+
} else if (len <= hint) {
580578
strbuf_setlen(sb, len);
581579
return 0;
582580
}

0 commit comments

Comments
 (0)