Skip to content

Commit c50230f

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 f731182 commit c50230f

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

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

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

365447
ret = _wmkdir(wpath);
448+
if (!ret)
449+
process_phantom_symlinks();
366450
if (!ret && hide_dotfiles == HIDE_DOTFILES_TRUE) {
367451
/*
368452
* In Windows a file or dir starting with a dot is not
@@ -2066,6 +2150,42 @@ int symlink(const char *target, const char *link)
20662150
errno = err_win_to_posix(GetLastError());
20672151
return -1;
20682152
}
2153+
2154+
/* convert to directory symlink if target exists */
2155+
switch (process_phantom_symlink(wtarget, wlink)) {
2156+
case PHANTOM_SYMLINK_RETRY: {
2157+
/* if target doesn't exist, add to phantom symlinks list */
2158+
wchar_t wfullpath[MAX_LONG_PATH];
2159+
struct phantom_symlink_info *psi;
2160+
2161+
/* convert to absolute path to be independent of cwd */
2162+
len = GetFullPathNameW(wlink, MAX_LONG_PATH, wfullpath, NULL);
2163+
if (!len || len >= MAX_LONG_PATH) {
2164+
errno = err_win_to_posix(GetLastError());
2165+
return -1;
2166+
}
2167+
2168+
/* over-allocate and fill phantom_smlink_info structure */
2169+
psi = xmalloc(sizeof(struct phantom_symlink_info)
2170+
+ sizeof(wchar_t) * (len + wcslen(wtarget) + 2));
2171+
psi->wlink = (wchar_t *)(psi + 1);
2172+
wcscpy(psi->wlink, wfullpath);
2173+
psi->wtarget = psi->wlink + len + 1;
2174+
wcscpy(psi->wtarget, wtarget);
2175+
2176+
EnterCriticalSection(&phantom_symlinks_cs);
2177+
psi->next = phantom_symlinks;
2178+
phantom_symlinks = psi;
2179+
LeaveCriticalSection(&phantom_symlinks_cs);
2180+
break;
2181+
}
2182+
case PHANTOM_SYMLINK_DIRECTORY:
2183+
/* if we created a dir symlink, process other phantom symlinks */
2184+
process_phantom_symlinks();
2185+
break;
2186+
default:
2187+
break;
2188+
}
20692189
return 0;
20702190
}
20712191

@@ -2550,6 +2670,7 @@ void mingw_startup()
25502670

25512671
/* initialize critical section for waitpid pinfo_t list */
25522672
InitializeCriticalSection(&pinfo_cs);
2673+
InitializeCriticalSection(&phantom_symlinks_cs);
25532674

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

0 commit comments

Comments
 (0)