Skip to content

Commit e623bdc

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

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
@@ -447,6 +447,54 @@ static void process_phantom_symlinks(void)
447447
LeaveCriticalSection(&phantom_symlinks_cs);
448448
}
449449

450+
static int create_phantom_symlink(wchar_t *wtarget, wchar_t *wlink)
451+
{
452+
int len;
453+
454+
/* create file symlink */
455+
if (!CreateSymbolicLinkW(wlink, wtarget, symlink_file_flags)) {
456+
errno = err_win_to_posix(GetLastError());
457+
return -1;
458+
}
459+
460+
/* convert to directory symlink if target exists */
461+
switch (process_phantom_symlink(wtarget, wlink)) {
462+
case PHANTOM_SYMLINK_RETRY: {
463+
/* if target doesn't exist, add to phantom symlinks list */
464+
wchar_t wfullpath[MAX_LONG_PATH];
465+
struct phantom_symlink_info *psi;
466+
467+
/* convert to absolute path to be independent of cwd */
468+
len = GetFullPathNameW(wlink, MAX_LONG_PATH, wfullpath, NULL);
469+
if (!len || len >= MAX_LONG_PATH) {
470+
errno = err_win_to_posix(GetLastError());
471+
return -1;
472+
}
473+
474+
/* over-allocate and fill phantom_symlink_info structure */
475+
psi = xmalloc(sizeof(struct phantom_symlink_info) +
476+
sizeof(wchar_t) * (len + wcslen(wtarget) + 2));
477+
psi->wlink = (wchar_t *)(psi + 1);
478+
wcscpy(psi->wlink, wfullpath);
479+
psi->wtarget = psi->wlink + len + 1;
480+
wcscpy(psi->wtarget, wtarget);
481+
482+
EnterCriticalSection(&phantom_symlinks_cs);
483+
psi->next = phantom_symlinks;
484+
phantom_symlinks = psi;
485+
LeaveCriticalSection(&phantom_symlinks_cs);
486+
break;
487+
}
488+
case PHANTOM_SYMLINK_DIRECTORY:
489+
/* if we created a dir symlink, process other phantom symlinks */
490+
process_phantom_symlinks();
491+
break;
492+
default:
493+
break;
494+
}
495+
return 0;
496+
}
497+
450498
/* Normalizes NT paths as returned by some low-level APIs. */
451499
static wchar_t *normalize_ntpath(wchar_t *wbuf)
452500
{
@@ -2883,48 +2931,7 @@ int symlink(const char *target, const char *link)
28832931
if (wtarget[len] == '/')
28842932
wtarget[len] = '\\';
28852933

2886-
/* create file symlink */
2887-
if (!CreateSymbolicLinkW(wlink, wtarget, symlink_file_flags)) {
2888-
errno = err_win_to_posix(GetLastError());
2889-
return -1;
2890-
}
2891-
2892-
/* convert to directory symlink if target exists */
2893-
switch (process_phantom_symlink(wtarget, wlink)) {
2894-
case PHANTOM_SYMLINK_RETRY: {
2895-
/* if target doesn't exist, add to phantom symlinks list */
2896-
wchar_t wfullpath[MAX_LONG_PATH];
2897-
struct phantom_symlink_info *psi;
2898-
2899-
/* convert to absolute path to be independent of cwd */
2900-
len = GetFullPathNameW(wlink, MAX_LONG_PATH, wfullpath, NULL);
2901-
if (!len || len >= MAX_LONG_PATH) {
2902-
errno = err_win_to_posix(GetLastError());
2903-
return -1;
2904-
}
2905-
2906-
/* over-allocate and fill phantom_symlink_info structure */
2907-
psi = xmalloc(sizeof(struct phantom_symlink_info)
2908-
+ sizeof(wchar_t) * (len + wcslen(wtarget) + 2));
2909-
psi->wlink = (wchar_t *)(psi + 1);
2910-
wcscpy(psi->wlink, wfullpath);
2911-
psi->wtarget = psi->wlink + len + 1;
2912-
wcscpy(psi->wtarget, wtarget);
2913-
2914-
EnterCriticalSection(&phantom_symlinks_cs);
2915-
psi->next = phantom_symlinks;
2916-
phantom_symlinks = psi;
2917-
LeaveCriticalSection(&phantom_symlinks_cs);
2918-
break;
2919-
}
2920-
case PHANTOM_SYMLINK_DIRECTORY:
2921-
/* if we created a dir symlink, process other phantom symlinks */
2922-
process_phantom_symlinks();
2923-
break;
2924-
default:
2925-
break;
2926-
}
2927-
return 0;
2934+
return create_phantom_symlink(wtarget, wlink);
29282935
}
29292936

29302937
#ifndef _WINNT_H

0 commit comments

Comments
 (0)