Skip to content

Commit b1b0f4a

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 bf64f06 commit b1b0f4a

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
@@ -231,6 +231,88 @@ static int retry_ask_yes_no(int *tries, const char *format, ...)
231231

232232
DECLARE_PROC_ADDR(kernel32.dll, BOOL, CreateSymbolicLinkW, LPCWSTR, LPCWSTR, DWORD);
233233

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

385467
ret = _wmkdir(wpath);
468+
if (!ret)
469+
process_phantom_symlinks();
386470
if (!ret && needs_hiding(path))
387471
return set_hidden_flag(wpath, 1);
388472
return ret;
@@ -2112,6 +2196,42 @@ int symlink(const char *target, const char *link)
21122196
errno = err_win_to_posix(GetLastError());
21132197
return -1;
21142198
}
2199+
2200+
/* convert to directory symlink if target exists */
2201+
switch (process_phantom_symlink(wtarget, wlink)) {
2202+
case PHANTOM_SYMLINK_RETRY: {
2203+
/* if target doesn't exist, add to phantom symlinks list */
2204+
wchar_t wfullpath[MAX_LONG_PATH];
2205+
struct phantom_symlink_info *psi;
2206+
2207+
/* convert to absolute path to be independent of cwd */
2208+
len = GetFullPathNameW(wlink, MAX_LONG_PATH, wfullpath, NULL);
2209+
if (!len || len >= MAX_LONG_PATH) {
2210+
errno = err_win_to_posix(GetLastError());
2211+
return -1;
2212+
}
2213+
2214+
/* over-allocate and fill phantom_smlink_info structure */
2215+
psi = xmalloc(sizeof(struct phantom_symlink_info)
2216+
+ sizeof(wchar_t) * (len + wcslen(wtarget) + 2));
2217+
psi->wlink = (wchar_t *)(psi + 1);
2218+
wcscpy(psi->wlink, wfullpath);
2219+
psi->wtarget = psi->wlink + len + 1;
2220+
wcscpy(psi->wtarget, wtarget);
2221+
2222+
EnterCriticalSection(&phantom_symlinks_cs);
2223+
psi->next = phantom_symlinks;
2224+
phantom_symlinks = psi;
2225+
LeaveCriticalSection(&phantom_symlinks_cs);
2226+
break;
2227+
}
2228+
case PHANTOM_SYMLINK_DIRECTORY:
2229+
/* if we created a dir symlink, process other phantom symlinks */
2230+
process_phantom_symlinks();
2231+
break;
2232+
default:
2233+
break;
2234+
}
21152235
return 0;
21162236
}
21172237

@@ -2596,6 +2716,7 @@ void mingw_startup(void)
25962716

25972717
/* initialize critical section for waitpid pinfo_t list */
25982718
InitializeCriticalSection(&pinfo_cs);
2719+
InitializeCriticalSection(&phantom_symlinks_cs);
25992720

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

0 commit comments

Comments
 (0)