Skip to content

Commit e1fb9b7

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]>
1 parent 687a1c1 commit e1fb9b7

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
@@ -272,6 +272,88 @@ int mingw_core_config(const char *var, const char *value, void *cb)
272272

273273
DECLARE_PROC_ADDR(kernel32.dll, BOOLEAN, CreateSymbolicLinkW, LPCWSTR, LPCWSTR, DWORD);
274274

275+
enum phantom_symlink_result {
276+
PHANTOM_SYMLINK_RETRY,
277+
PHANTOM_SYMLINK_DONE,
278+
PHANTOM_SYMLINK_DIRECTORY
279+
};
280+
281+
/*
282+
* Changes a file symlink to a directory symlink if the target exists and is a
283+
* directory.
284+
*/
285+
static enum phantom_symlink_result process_phantom_symlink(
286+
const wchar_t *wtarget, const wchar_t *wlink) {
287+
HANDLE hnd;
288+
BY_HANDLE_FILE_INFORMATION fdata;
289+
290+
/* check that wlink is still a file symlink */
291+
if ((GetFileAttributesW(wlink)
292+
& (FILE_ATTRIBUTE_REPARSE_POINT | FILE_ATTRIBUTE_DIRECTORY))
293+
!= FILE_ATTRIBUTE_REPARSE_POINT)
294+
return PHANTOM_SYMLINK_DONE;
295+
296+
/* let Windows resolve the link by opening it */
297+
hnd = CreateFileW(wlink, 0,
298+
FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL,
299+
OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);
300+
if (hnd == INVALID_HANDLE_VALUE) {
301+
errno = err_win_to_posix(GetLastError());
302+
return PHANTOM_SYMLINK_RETRY;
303+
}
304+
305+
if (!GetFileInformationByHandle(hnd, &fdata)) {
306+
errno = err_win_to_posix(GetLastError());
307+
CloseHandle(hnd);
308+
return PHANTOM_SYMLINK_RETRY;
309+
}
310+
CloseHandle(hnd);
311+
312+
/* if target exists and is a file, we're done */
313+
if (!(fdata.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
314+
return PHANTOM_SYMLINK_DONE;
315+
316+
/* otherwise recreate the symlink with directory flag */
317+
if (DeleteFileW(wlink) && CreateSymbolicLinkW(wlink, wtarget, 1))
318+
return PHANTOM_SYMLINK_DIRECTORY;
319+
320+
errno = err_win_to_posix(GetLastError());
321+
return PHANTOM_SYMLINK_RETRY;
322+
}
323+
324+
/* keep track of newly created symlinks to non-existing targets */
325+
struct phantom_symlink_info {
326+
struct phantom_symlink_info *next;
327+
wchar_t *wlink;
328+
wchar_t *wtarget;
329+
};
330+
331+
static struct phantom_symlink_info *phantom_symlinks = NULL;
332+
static CRITICAL_SECTION phantom_symlinks_cs;
333+
334+
static void process_phantom_symlinks(void)
335+
{
336+
struct phantom_symlink_info *current, **psi;
337+
EnterCriticalSection(&phantom_symlinks_cs);
338+
/* process phantom symlinks list */
339+
psi = &phantom_symlinks;
340+
while ((current = *psi)) {
341+
enum phantom_symlink_result result = process_phantom_symlink(
342+
current->wtarget, current->wlink);
343+
if (result == PHANTOM_SYMLINK_RETRY) {
344+
psi = &current->next;
345+
} else {
346+
/* symlink was processed, remove from list */
347+
*psi = current->next;
348+
free(current);
349+
/* if symlink was a directory, start over */
350+
if (result == PHANTOM_SYMLINK_DIRECTORY)
351+
psi = &phantom_symlinks;
352+
}
353+
}
354+
LeaveCriticalSection(&phantom_symlinks_cs);
355+
}
356+
275357
/* Normalizes NT paths as returned by some low-level APIs. */
276358
static wchar_t *normalize_ntpath(wchar_t *wbuf)
277359
{
@@ -424,6 +506,8 @@ int mingw_mkdir(const char *path, int mode)
424506
return -1;
425507

426508
ret = _wmkdir(wpath);
509+
if (!ret)
510+
process_phantom_symlinks();
427511
if (!ret && needs_hiding(path))
428512
return set_hidden_flag(wpath, 1);
429513
return ret;
@@ -2151,6 +2235,42 @@ int symlink(const char *target, const char *link)
21512235
errno = err_win_to_posix(GetLastError());
21522236
return -1;
21532237
}
2238+
2239+
/* convert to directory symlink if target exists */
2240+
switch (process_phantom_symlink(wtarget, wlink)) {
2241+
case PHANTOM_SYMLINK_RETRY: {
2242+
/* if target doesn't exist, add to phantom symlinks list */
2243+
wchar_t wfullpath[MAX_LONG_PATH];
2244+
struct phantom_symlink_info *psi;
2245+
2246+
/* convert to absolute path to be independent of cwd */
2247+
len = GetFullPathNameW(wlink, MAX_LONG_PATH, wfullpath, NULL);
2248+
if (!len || len >= MAX_LONG_PATH) {
2249+
errno = err_win_to_posix(GetLastError());
2250+
return -1;
2251+
}
2252+
2253+
/* over-allocate and fill phantom_smlink_info structure */
2254+
psi = xmalloc(sizeof(struct phantom_symlink_info)
2255+
+ sizeof(wchar_t) * (len + wcslen(wtarget) + 2));
2256+
psi->wlink = (wchar_t *)(psi + 1);
2257+
wcscpy(psi->wlink, wfullpath);
2258+
psi->wtarget = psi->wlink + len + 1;
2259+
wcscpy(psi->wtarget, wtarget);
2260+
2261+
EnterCriticalSection(&phantom_symlinks_cs);
2262+
psi->next = phantom_symlinks;
2263+
phantom_symlinks = psi;
2264+
LeaveCriticalSection(&phantom_symlinks_cs);
2265+
break;
2266+
}
2267+
case PHANTOM_SYMLINK_DIRECTORY:
2268+
/* if we created a dir symlink, process other phantom symlinks */
2269+
process_phantom_symlinks();
2270+
break;
2271+
default:
2272+
break;
2273+
}
21542274
return 0;
21552275
}
21562276

@@ -2695,6 +2815,7 @@ void mingw_startup(void)
26952815

26962816
/* initialize critical section for waitpid pinfo_t list */
26972817
InitializeCriticalSection(&pinfo_cs);
2818+
InitializeCriticalSection(&phantom_symlinks_cs);
26982819

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

0 commit comments

Comments
 (0)