Skip to content

Commit 2c65aca

Browse files
committed
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]>
1 parent ec9ce13 commit 2c65aca

File tree

1 file changed

+121
-0
lines changed

1 file changed

+121
-0
lines changed

compat/mingw.c

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,88 @@ static int retry_ask_yes_no(int *tries, const char *format, ...)
234234

235235
DECLARE_PROC_ADDR(kernel32.dll, BOOL, CreateSymbolicLinkW, LPCWSTR, LPCWSTR, DWORD);
236236

237+
enum phantom_symlink_result {
238+
PHANTOM_SYMLINK_RETRY,
239+
PHANTOM_SYMLINK_DONE,
240+
PHANTOM_SYMLINK_DIRECTORY
241+
};
242+
243+
/*
244+
* Changes a file symlink to a directory symlink if the target exists and is a
245+
* directory.
246+
*/
247+
static enum phantom_symlink_result process_phantom_symlink(
248+
const wchar_t *wtarget, const wchar_t *wlink) {
249+
HANDLE hnd;
250+
BY_HANDLE_FILE_INFORMATION fdata;
251+
252+
/* check that wlink is still a file symlink */
253+
if ((GetFileAttributesW(wlink)
254+
& (FILE_ATTRIBUTE_REPARSE_POINT | FILE_ATTRIBUTE_DIRECTORY))
255+
!= FILE_ATTRIBUTE_REPARSE_POINT)
256+
return PHANTOM_SYMLINK_DONE;
257+
258+
/* let Windows resolve the link by opening it */
259+
hnd = CreateFileW(wlink, 0,
260+
FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL,
261+
OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);
262+
if (hnd == INVALID_HANDLE_VALUE) {
263+
errno = err_win_to_posix(GetLastError());
264+
return PHANTOM_SYMLINK_RETRY;
265+
}
266+
267+
if (!GetFileInformationByHandle(hnd, &fdata)) {
268+
errno = err_win_to_posix(GetLastError());
269+
CloseHandle(hnd);
270+
return PHANTOM_SYMLINK_RETRY;
271+
}
272+
CloseHandle(hnd);
273+
274+
/* if target exists and is a file, we're done */
275+
if (!(fdata.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
276+
return PHANTOM_SYMLINK_DONE;
277+
278+
/* otherwise recreate the symlink with directory flag */
279+
if (DeleteFileW(wlink) && CreateSymbolicLinkW(wlink, wtarget, 1))
280+
return PHANTOM_SYMLINK_DIRECTORY;
281+
282+
errno = err_win_to_posix(GetLastError());
283+
return PHANTOM_SYMLINK_RETRY;
284+
}
285+
286+
/* keep track of newly created symlinks to non-existing targets */
287+
struct phantom_symlink_info {
288+
struct phantom_symlink_info *next;
289+
wchar_t *wlink;
290+
wchar_t *wtarget;
291+
};
292+
293+
static struct phantom_symlink_info *phantom_symlinks = NULL;
294+
static CRITICAL_SECTION phantom_symlinks_cs;
295+
296+
static void process_phantom_symlinks(void)
297+
{
298+
struct phantom_symlink_info *current, **psi;
299+
EnterCriticalSection(&phantom_symlinks_cs);
300+
/* process phantom symlinks list */
301+
psi = &phantom_symlinks;
302+
while ((current = *psi)) {
303+
enum phantom_symlink_result result = process_phantom_symlink(
304+
current->wtarget, current->wlink);
305+
if (result == PHANTOM_SYMLINK_RETRY) {
306+
psi = &current->next;
307+
} else {
308+
/* symlink was processed, remove from list */
309+
*psi = current->next;
310+
free(current);
311+
/* if symlink was a directory, start over */
312+
if (result == PHANTOM_SYMLINK_DIRECTORY)
313+
psi = &phantom_symlinks;
314+
}
315+
}
316+
LeaveCriticalSection(&phantom_symlinks_cs);
317+
}
318+
237319
/* Normalizes NT paths as returned by some low-level APIs. */
238320
static wchar_t *normalize_ntpath(wchar_t *wbuf)
239321
{
@@ -364,6 +446,8 @@ int mingw_mkdir(const char *path, int mode)
364446
return -1;
365447

366448
ret = _wmkdir(wpath);
449+
if (!ret)
450+
process_phantom_symlinks();
367451
if (!ret && hide_dotfiles == HIDE_DOTFILES_TRUE) {
368452
/*
369453
* In Windows a file or dir starting with a dot is not
@@ -2040,6 +2124,42 @@ int symlink(const char *target, const char *link)
20402124
errno = err_win_to_posix(GetLastError());
20412125
return -1;
20422126
}
2127+
2128+
/* convert to directory symlink if target exists */
2129+
switch (process_phantom_symlink(wtarget, wlink)) {
2130+
case PHANTOM_SYMLINK_RETRY: {
2131+
/* if target doesn't exist, add to phantom symlinks list */
2132+
wchar_t wfullpath[MAX_LONG_PATH];
2133+
struct phantom_symlink_info *psi;
2134+
2135+
/* convert to absolute path to be independent of cwd */
2136+
len = GetFullPathNameW(wlink, MAX_LONG_PATH, wfullpath, NULL);
2137+
if (!len || len >= MAX_LONG_PATH) {
2138+
errno = err_win_to_posix(GetLastError());
2139+
return -1;
2140+
}
2141+
2142+
/* over-allocate and fill phantom_smlink_info structure */
2143+
psi = xmalloc(sizeof(struct phantom_symlink_info)
2144+
+ sizeof(wchar_t) * (len + wcslen(wtarget) + 2));
2145+
psi->wlink = (wchar_t *)(psi + 1);
2146+
wcscpy(psi->wlink, wfullpath);
2147+
psi->wtarget = psi->wlink + len + 1;
2148+
wcscpy(psi->wtarget, wtarget);
2149+
2150+
EnterCriticalSection(&phantom_symlinks_cs);
2151+
psi->next = phantom_symlinks;
2152+
phantom_symlinks = psi;
2153+
LeaveCriticalSection(&phantom_symlinks_cs);
2154+
break;
2155+
}
2156+
case PHANTOM_SYMLINK_DIRECTORY:
2157+
/* if we created a dir symlink, process other phantom symlinks */
2158+
process_phantom_symlinks();
2159+
break;
2160+
default:
2161+
break;
2162+
}
20432163
return 0;
20442164
}
20452165

@@ -2521,6 +2641,7 @@ void mingw_startup()
25212641

25222642
/* initialize critical section for waitpid pinfo_t list */
25232643
InitializeCriticalSection(&pinfo_cs);
2644+
InitializeCriticalSection(&phantom_symlinks_cs);
25242645

25252646
/* set up default file mode and file modes for stdin/out/err */
25262647
_fmode = _O_BINARY;

0 commit comments

Comments
 (0)