Skip to content

Commit 26bc82a

Browse files
kbleesdscho
authored andcommitted
Win32: symlink: add support for symlinks to directories
Symlinks on Windows have a flag that indicates whether the target is a file or a directory. Symlinks of wrong type simply don't work. This even affects core Win32 APIs (e.g. DeleteFile() refuses to delete directory symlinks). However, CreateFile() with FILE_FLAG_BACKUP_SEMANTICS doesn't seem to care. Check the target type by first creating a tentative file symlink, opening it, and checking the type of the resulting handle. If it is a directory, recreate the symlink with the directory flag set. It is possible to create symlinks before the target exists (or in case of symlinks to symlinks: before the target type is known). If this happens, create a tentative file symlink and postpone the directory decision: keep a list of phantom symlinks to be processed whenever a new directory is created in mingw_mkdir(). Limitations: This algorithm may fail if a link target changes from file to directory or vice versa, or if the target directory is created in another process. Signed-off-by: Karsten Blees <[email protected]> Signed-off-by: Johannes Schindelin <[email protected]>
1 parent 67ccfb0 commit 26bc82a

File tree

1 file changed

+159
-0
lines changed

1 file changed

+159
-0
lines changed

compat/mingw.c

Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,126 @@ static inline int is_wdir_sep(wchar_t wchar)
335335
return wchar == L'/' || wchar == L'\\';
336336
}
337337

338+
static const wchar_t *make_relative_to(const wchar_t *path,
339+
const wchar_t *relative_to, wchar_t *out,
340+
size_t size)
341+
{
342+
size_t i = wcslen(relative_to), len;
343+
344+
/* Is `path` already absolute? */
345+
if (is_wdir_sep(path[0]) ||
346+
(iswalpha(path[0]) && path[1] == L':' && is_wdir_sep(path[2])))
347+
return path;
348+
349+
while (i > 0 && !is_wdir_sep(relative_to[i - 1]))
350+
i--;
351+
352+
/* Is `relative_to` in the current directory? */
353+
if (!i)
354+
return path;
355+
356+
len = wcslen(path);
357+
if (i + len + 1 > size) {
358+
error("Could not make '%ls' relative to '%ls' (too large)",
359+
path, relative_to);
360+
return NULL;
361+
}
362+
363+
memcpy(out, relative_to, i * sizeof(wchar_t));
364+
wcscpy(out + i, path);
365+
return out;
366+
}
367+
368+
enum phantom_symlink_result {
369+
PHANTOM_SYMLINK_RETRY,
370+
PHANTOM_SYMLINK_DONE,
371+
PHANTOM_SYMLINK_DIRECTORY
372+
};
373+
374+
/*
375+
* Changes a file symlink to a directory symlink if the target exists and is a
376+
* directory.
377+
*/
378+
static enum phantom_symlink_result
379+
process_phantom_symlink(const wchar_t *wtarget, const wchar_t *wlink)
380+
{
381+
HANDLE hnd;
382+
BY_HANDLE_FILE_INFORMATION fdata;
383+
wchar_t relative[MAX_LONG_PATH];
384+
const wchar_t *rel;
385+
386+
/* check that wlink is still a file symlink */
387+
if ((GetFileAttributesW(wlink)
388+
& (FILE_ATTRIBUTE_REPARSE_POINT | FILE_ATTRIBUTE_DIRECTORY))
389+
!= FILE_ATTRIBUTE_REPARSE_POINT)
390+
return PHANTOM_SYMLINK_DONE;
391+
392+
/* make it relative, if necessary */
393+
rel = make_relative_to(wtarget, wlink, relative, ARRAY_SIZE(relative));
394+
if (!rel)
395+
return PHANTOM_SYMLINK_DONE;
396+
397+
/* let Windows resolve the link by opening it */
398+
hnd = CreateFileW(rel, 0,
399+
FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL,
400+
OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);
401+
if (hnd == INVALID_HANDLE_VALUE) {
402+
errno = err_win_to_posix(GetLastError());
403+
return PHANTOM_SYMLINK_RETRY;
404+
}
405+
406+
if (!GetFileInformationByHandle(hnd, &fdata)) {
407+
errno = err_win_to_posix(GetLastError());
408+
CloseHandle(hnd);
409+
return PHANTOM_SYMLINK_RETRY;
410+
}
411+
CloseHandle(hnd);
412+
413+
/* if target exists and is a file, we're done */
414+
if (!(fdata.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
415+
return PHANTOM_SYMLINK_DONE;
416+
417+
/* otherwise recreate the symlink with directory flag */
418+
if (DeleteFileW(wlink) && CreateSymbolicLinkW(wlink, wtarget, 1))
419+
return PHANTOM_SYMLINK_DIRECTORY;
420+
421+
errno = err_win_to_posix(GetLastError());
422+
return PHANTOM_SYMLINK_RETRY;
423+
}
424+
425+
/* keep track of newly created symlinks to non-existing targets */
426+
struct phantom_symlink_info {
427+
struct phantom_symlink_info *next;
428+
wchar_t *wlink;
429+
wchar_t *wtarget;
430+
};
431+
432+
static struct phantom_symlink_info *phantom_symlinks = NULL;
433+
static CRITICAL_SECTION phantom_symlinks_cs;
434+
435+
static void process_phantom_symlinks(void)
436+
{
437+
struct phantom_symlink_info *current, **psi;
438+
EnterCriticalSection(&phantom_symlinks_cs);
439+
/* process phantom symlinks list */
440+
psi = &phantom_symlinks;
441+
while ((current = *psi)) {
442+
enum phantom_symlink_result result = process_phantom_symlink(
443+
current->wtarget, current->wlink);
444+
if (result == PHANTOM_SYMLINK_RETRY) {
445+
psi = &current->next;
446+
} else {
447+
/* symlink was processed, remove from list */
448+
*psi = current->next;
449+
free(current);
450+
/* if symlink was a directory, start over */
451+
if (result == PHANTOM_SYMLINK_DIRECTORY)
452+
psi = &phantom_symlinks;
453+
}
454+
}
455+
LeaveCriticalSection(&phantom_symlinks_cs);
456+
}
457+
338458
/* Normalizes NT paths as returned by some low-level APIs. */
339459
static wchar_t *normalize_ntpath(wchar_t *wbuf)
340460
{
@@ -518,6 +638,8 @@ int mingw_mkdir(const char *path, int mode)
518638
return -1;
519639

520640
ret = _wmkdir(wpath);
641+
if (!ret)
642+
process_phantom_symlinks();
521643
if (!ret && needs_hiding(path))
522644
return set_hidden_flag(wpath, 1);
523645
return ret;
@@ -2779,6 +2901,42 @@ int symlink(const char *target, const char *link)
27792901
errno = err_win_to_posix(GetLastError());
27802902
return -1;
27812903
}
2904+
2905+
/* convert to directory symlink if target exists */
2906+
switch (process_phantom_symlink(wtarget, wlink)) {
2907+
case PHANTOM_SYMLINK_RETRY: {
2908+
/* if target doesn't exist, add to phantom symlinks list */
2909+
wchar_t wfullpath[MAX_LONG_PATH];
2910+
struct phantom_symlink_info *psi;
2911+
2912+
/* convert to absolute path to be independent of cwd */
2913+
len = GetFullPathNameW(wlink, MAX_LONG_PATH, wfullpath, NULL);
2914+
if (!len || len >= MAX_LONG_PATH) {
2915+
errno = err_win_to_posix(GetLastError());
2916+
return -1;
2917+
}
2918+
2919+
/* over-allocate and fill phantom_symlink_info structure */
2920+
psi = xmalloc(sizeof(struct phantom_symlink_info)
2921+
+ sizeof(wchar_t) * (len + wcslen(wtarget) + 2));
2922+
psi->wlink = (wchar_t *)(psi + 1);
2923+
wcscpy(psi->wlink, wfullpath);
2924+
psi->wtarget = psi->wlink + len + 1;
2925+
wcscpy(psi->wtarget, wtarget);
2926+
2927+
EnterCriticalSection(&phantom_symlinks_cs);
2928+
psi->next = phantom_symlinks;
2929+
phantom_symlinks = psi;
2930+
LeaveCriticalSection(&phantom_symlinks_cs);
2931+
break;
2932+
}
2933+
case PHANTOM_SYMLINK_DIRECTORY:
2934+
/* if we created a dir symlink, process other phantom symlinks */
2935+
process_phantom_symlinks();
2936+
break;
2937+
default:
2938+
break;
2939+
}
27822940
return 0;
27832941
}
27842942

@@ -3739,6 +3897,7 @@ int wmain(int argc, const wchar_t **wargv)
37393897

37403898
/* initialize critical section for waitpid pinfo_t list */
37413899
InitializeCriticalSection(&pinfo_cs);
3900+
InitializeCriticalSection(&phantom_symlinks_cs);
37423901

37433902
/* initialize critical section for fscache */
37443903
InitializeCriticalSection(&fscache_cs);

0 commit comments

Comments
 (0)