Skip to content

Commit 3d6e7d6

Browse files
piscisaureusdscho
authored andcommitted
Win32: symlink: move phantom symlink creation to a separate function
Signed-off-by: Bert Belder <[email protected]>
1 parent 0fd33c4 commit 3d6e7d6

File tree

1 file changed

+49
-42
lines changed

1 file changed

+49
-42
lines changed

compat/mingw.c

Lines changed: 49 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -413,6 +413,54 @@ static void process_phantom_symlinks(void)
413413
LeaveCriticalSection(&phantom_symlinks_cs);
414414
}
415415

416+
static int create_phantom_symlink(wchar_t *wtarget, wchar_t *wlink)
417+
{
418+
int len;
419+
420+
/* create file symlink */
421+
if (!CreateSymbolicLinkW(wlink, wtarget, symlink_file_flags)) {
422+
errno = err_win_to_posix(GetLastError());
423+
return -1;
424+
}
425+
426+
/* convert to directory symlink if target exists */
427+
switch (process_phantom_symlink(wtarget, wlink)) {
428+
case PHANTOM_SYMLINK_RETRY: {
429+
/* if target doesn't exist, add to phantom symlinks list */
430+
wchar_t wfullpath[MAX_LONG_PATH];
431+
struct phantom_symlink_info *psi;
432+
433+
/* convert to absolute path to be independent of cwd */
434+
len = GetFullPathNameW(wlink, MAX_LONG_PATH, wfullpath, NULL);
435+
if (!len || len >= MAX_LONG_PATH) {
436+
errno = err_win_to_posix(GetLastError());
437+
return -1;
438+
}
439+
440+
/* over-allocate and fill phantom_symlink_info structure */
441+
psi = xmalloc(sizeof(struct phantom_symlink_info) +
442+
sizeof(wchar_t) * (len + wcslen(wtarget) + 2));
443+
psi->wlink = (wchar_t *)(psi + 1);
444+
wcscpy(psi->wlink, wfullpath);
445+
psi->wtarget = psi->wlink + len + 1;
446+
wcscpy(psi->wtarget, wtarget);
447+
448+
EnterCriticalSection(&phantom_symlinks_cs);
449+
psi->next = phantom_symlinks;
450+
phantom_symlinks = psi;
451+
LeaveCriticalSection(&phantom_symlinks_cs);
452+
break;
453+
}
454+
case PHANTOM_SYMLINK_DIRECTORY:
455+
/* if we created a dir symlink, process other phantom symlinks */
456+
process_phantom_symlinks();
457+
break;
458+
default:
459+
break;
460+
}
461+
return 0;
462+
}
463+
416464
/* Normalizes NT paths as returned by some low-level APIs. */
417465
static wchar_t *normalize_ntpath(wchar_t *wbuf)
418466
{
@@ -2353,48 +2401,7 @@ int symlink(const char *target, const char *link)
23532401
if (wtarget[len] == '/')
23542402
wtarget[len] = '\\';
23552403

2356-
/* create file symlink */
2357-
if (!CreateSymbolicLinkW(wlink, wtarget, symlink_file_flags)) {
2358-
errno = err_win_to_posix(GetLastError());
2359-
return -1;
2360-
}
2361-
2362-
/* convert to directory symlink if target exists */
2363-
switch (process_phantom_symlink(wtarget, wlink)) {
2364-
case PHANTOM_SYMLINK_RETRY: {
2365-
/* if target doesn't exist, add to phantom symlinks list */
2366-
wchar_t wfullpath[MAX_LONG_PATH];
2367-
struct phantom_symlink_info *psi;
2368-
2369-
/* convert to absolute path to be independent of cwd */
2370-
len = GetFullPathNameW(wlink, MAX_LONG_PATH, wfullpath, NULL);
2371-
if (!len || len >= MAX_LONG_PATH) {
2372-
errno = err_win_to_posix(GetLastError());
2373-
return -1;
2374-
}
2375-
2376-
/* over-allocate and fill phantom_symlink_info structure */
2377-
psi = xmalloc(sizeof(struct phantom_symlink_info)
2378-
+ sizeof(wchar_t) * (len + wcslen(wtarget) + 2));
2379-
psi->wlink = (wchar_t *)(psi + 1);
2380-
wcscpy(psi->wlink, wfullpath);
2381-
psi->wtarget = psi->wlink + len + 1;
2382-
wcscpy(psi->wtarget, wtarget);
2383-
2384-
EnterCriticalSection(&phantom_symlinks_cs);
2385-
psi->next = phantom_symlinks;
2386-
phantom_symlinks = psi;
2387-
LeaveCriticalSection(&phantom_symlinks_cs);
2388-
break;
2389-
}
2390-
case PHANTOM_SYMLINK_DIRECTORY:
2391-
/* if we created a dir symlink, process other phantom symlinks */
2392-
process_phantom_symlinks();
2393-
break;
2394-
default:
2395-
break;
2396-
}
2397-
return 0;
2404+
return create_phantom_symlink(wtarget, wlink);
23982405
}
23992406

24002407
#ifndef _WINNT_H

0 commit comments

Comments
 (0)