Skip to content

Commit 5d70b1b

Browse files
piscisaureusGit for Windows Build Agent
authored andcommitted
Win32: symlink: move phantom symlink creation to a separate function
Signed-off-by: Bert Belder <[email protected]>
1 parent 01bc0ac commit 5d70b1b

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

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

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

24222429
#ifndef _WINNT_H

0 commit comments

Comments
 (0)