Skip to content

Commit 01fb449

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

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
{
@@ -2378,48 +2426,7 @@ int symlink(const char *target, const char *link)
23782426
if (wtarget[len] == '/')
23792427
wtarget[len] = '\\';
23802428

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

24252432
#ifndef _WINNT_H

0 commit comments

Comments
 (0)