Skip to content

Commit 645aa08

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, keep the 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. Signed-off-by: Karsten Blees <[email protected]>
1 parent fff83df commit 645aa08

File tree

1 file changed

+122
-0
lines changed

1 file changed

+122
-0
lines changed

compat/mingw.c

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,89 @@ 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 *woldpath, const wchar_t *wnewpath) {
249+
HANDLE hnd;
250+
BY_HANDLE_FILE_INFORMATION fdata;
251+
252+
/* check that wnewpath is still a file symlink */
253+
if ((GetFileAttributesW(wnewpath)
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(wnewpath, 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(wnewpath) && CreateSymbolicLinkW(wnewpath, woldpath, 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 *wnewpath;
290+
wchar_t *woldpath;
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 *next, **psi;
299+
EnterCriticalSection(&phantom_symlinks_cs);
300+
/* process phantom symlinks list */
301+
psi = &phantom_symlinks;
302+
while (*psi) {
303+
enum phantom_symlink_result result = process_phantom_symlink(
304+
(*psi)->woldpath, (*psi)->wnewpath);
305+
if (result == PHANTOM_SYMLINK_RETRY) {
306+
psi = &(*psi)->next;
307+
} else {
308+
/* symlink was processed, remove from list */
309+
next = (*psi)->next;
310+
free(*psi);
311+
*psi = next;
312+
/* if symlink was a directory, start over */
313+
if (result == PHANTOM_SYMLINK_DIRECTORY)
314+
psi = &phantom_symlinks;
315+
}
316+
}
317+
LeaveCriticalSection(&phantom_symlinks_cs);
318+
}
319+
237320
/* Normalizes NT paths as returned by some low-level APIs. */
238321
static wchar_t *normalize_ntpath(wchar_t *wbuf)
239322
{
@@ -361,6 +444,8 @@ int mingw_mkdir(const char *path, int mode)
361444
return -1;
362445

363446
ret = _wmkdir(wpath);
447+
if (!ret)
448+
process_phantom_symlinks();
364449
if (!ret && hide_dotfiles == HIDE_DOTFILES_TRUE) {
365450
/*
366451
* In Windows a file or dir starting with a dot is not
@@ -2037,6 +2122,42 @@ int symlink(const char *oldpath, const char *newpath)
20372122
errno = err_win_to_posix(GetLastError());
20382123
return -1;
20392124
}
2125+
2126+
/* convert to directory symlink if target exists */
2127+
switch (process_phantom_symlink(woldpath, wnewpath)) {
2128+
case PHANTOM_SYMLINK_RETRY: {
2129+
/* if target doesn't exist, add to phantom symlinks list */
2130+
wchar_t wfullpath[MAX_LONG_PATH];
2131+
struct phantom_symlink_info *psi;
2132+
2133+
/* convert to absolute path to be independent of cwd */
2134+
len = GetFullPathNameW(wnewpath, MAX_LONG_PATH, wfullpath, NULL);
2135+
if (!len || len >= MAX_LONG_PATH) {
2136+
errno = err_win_to_posix(GetLastError());
2137+
return -1;
2138+
}
2139+
2140+
/* over-allocate and fill phantom_smlink_info structure */
2141+
psi = xmalloc(sizeof(struct phantom_symlink_info)
2142+
+ sizeof(wchar_t) * (len + wcslen(woldpath) + 2));
2143+
psi->wnewpath = (wchar_t *)(psi + 1);
2144+
wcscpy(psi->wnewpath, wfullpath);
2145+
psi->woldpath = psi->wnewpath + len + 1;
2146+
wcscpy(psi->woldpath, woldpath);
2147+
2148+
EnterCriticalSection(&phantom_symlinks_cs);
2149+
psi->next = phantom_symlinks;
2150+
phantom_symlinks = psi;
2151+
LeaveCriticalSection(&phantom_symlinks_cs);
2152+
break;
2153+
}
2154+
case PHANTOM_SYMLINK_DIRECTORY:
2155+
/* if we created a dir symlink, process other phantom symlinks */
2156+
process_phantom_symlinks();
2157+
break;
2158+
default:
2159+
break;
2160+
}
20402161
return 0;
20412162
}
20422163

@@ -2512,6 +2633,7 @@ void mingw_startup()
25122633

25132634
/* initialize critical section for waitpid pinfo_t list */
25142635
InitializeCriticalSection(&pinfo_cs);
2636+
InitializeCriticalSection(&phantom_symlinks_cs);
25152637

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

0 commit comments

Comments
 (0)