Skip to content

Commit 13c6798

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 6eb04f8 commit 13c6798

File tree

1 file changed

+121
-0
lines changed

1 file changed

+121
-0
lines changed

compat/mingw.c

+121
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;
@@ -2162,6 +2246,42 @@ int symlink(const char *target, const char *link)
21622246
errno = err_win_to_posix(GetLastError());
21632247
return -1;
21642248
}
2249+
2250+
/* convert to directory symlink if target exists */
2251+
switch (process_phantom_symlink(wtarget, wlink)) {
2252+
case PHANTOM_SYMLINK_RETRY: {
2253+
/* if target doesn't exist, add to phantom symlinks list */
2254+
wchar_t wfullpath[MAX_LONG_PATH];
2255+
struct phantom_symlink_info *psi;
2256+
2257+
/* convert to absolute path to be independent of cwd */
2258+
len = GetFullPathNameW(wlink, MAX_LONG_PATH, wfullpath, NULL);
2259+
if (!len || len >= MAX_LONG_PATH) {
2260+
errno = err_win_to_posix(GetLastError());
2261+
return -1;
2262+
}
2263+
2264+
/* over-allocate and fill phantom_smlink_info structure */
2265+
psi = xmalloc(sizeof(struct phantom_symlink_info)
2266+
+ sizeof(wchar_t) * (len + wcslen(wtarget) + 2));
2267+
psi->wlink = (wchar_t *)(psi + 1);
2268+
wcscpy(psi->wlink, wfullpath);
2269+
psi->wtarget = psi->wlink + len + 1;
2270+
wcscpy(psi->wtarget, wtarget);
2271+
2272+
EnterCriticalSection(&phantom_symlinks_cs);
2273+
psi->next = phantom_symlinks;
2274+
phantom_symlinks = psi;
2275+
LeaveCriticalSection(&phantom_symlinks_cs);
2276+
break;
2277+
}
2278+
case PHANTOM_SYMLINK_DIRECTORY:
2279+
/* if we created a dir symlink, process other phantom symlinks */
2280+
process_phantom_symlinks();
2281+
break;
2282+
default:
2283+
break;
2284+
}
21652285
return 0;
21662286
}
21672287

@@ -2706,6 +2826,7 @@ void mingw_startup(void)
27062826

27072827
/* initialize critical section for waitpid pinfo_t list */
27082828
InitializeCriticalSection(&pinfo_cs);
2829+
InitializeCriticalSection(&phantom_symlinks_cs);
27092830

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

0 commit comments

Comments
 (0)