Skip to content

Commit bd6939f

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

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
@@ -416,6 +416,54 @@ static void process_phantom_symlinks(void)
416416
LeaveCriticalSection(&phantom_symlinks_cs);
417417
}
418418

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

2380-
/* create file symlink */
2381-
if (!CreateSymbolicLinkW(wlink, wtarget, symlink_file_flags)) {
2382-
errno = err_win_to_posix(GetLastError());
2383-
return -1;
2384-
}
2385-
2386-
/* convert to directory symlink if target exists */
2387-
switch (process_phantom_symlink(wtarget, wlink)) {
2388-
case PHANTOM_SYMLINK_RETRY: {
2389-
/* if target doesn't exist, add to phantom symlinks list */
2390-
wchar_t wfullpath[MAX_LONG_PATH];
2391-
struct phantom_symlink_info *psi;
2392-
2393-
/* convert to absolute path to be independent of cwd */
2394-
len = GetFullPathNameW(wlink, MAX_LONG_PATH, wfullpath, NULL);
2395-
if (!len || len >= MAX_LONG_PATH) {
2396-
errno = err_win_to_posix(GetLastError());
2397-
return -1;
2398-
}
2399-
2400-
/* over-allocate and fill phantom_symlink_info structure */
2401-
psi = xmalloc(sizeof(struct phantom_symlink_info)
2402-
+ sizeof(wchar_t) * (len + wcslen(wtarget) + 2));
2403-
psi->wlink = (wchar_t *)(psi + 1);
2404-
wcscpy(psi->wlink, wfullpath);
2405-
psi->wtarget = psi->wlink + len + 1;
2406-
wcscpy(psi->wtarget, wtarget);
2407-
2408-
EnterCriticalSection(&phantom_symlinks_cs);
2409-
psi->next = phantom_symlinks;
2410-
phantom_symlinks = psi;
2411-
LeaveCriticalSection(&phantom_symlinks_cs);
2412-
break;
2413-
}
2414-
case PHANTOM_SYMLINK_DIRECTORY:
2415-
/* if we created a dir symlink, process other phantom symlinks */
2416-
process_phantom_symlinks();
2417-
break;
2418-
default:
2419-
break;
2420-
}
2421-
return 0;
2428+
return create_phantom_symlink(wtarget, wlink);
24222429
}
24232430

24242431
#ifndef _WINNT_H

0 commit comments

Comments
 (0)