Skip to content

Commit a550920

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

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
@@ -456,6 +456,54 @@ static void process_phantom_symlinks(void)
456456
LeaveCriticalSection(&phantom_symlinks_cs);
457457
}
458458

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

2900-
/* create file symlink */
2901-
if (!CreateSymbolicLinkW(wlink, wtarget, symlink_file_flags)) {
2902-
errno = err_win_to_posix(GetLastError());
2903-
return -1;
2904-
}
2905-
2906-
/* convert to directory symlink if target exists */
2907-
switch (process_phantom_symlink(wtarget, wlink)) {
2908-
case PHANTOM_SYMLINK_RETRY: {
2909-
/* if target doesn't exist, add to phantom symlinks list */
2910-
wchar_t wfullpath[MAX_LONG_PATH];
2911-
struct phantom_symlink_info *psi;
2912-
2913-
/* convert to absolute path to be independent of cwd */
2914-
len = GetFullPathNameW(wlink, MAX_LONG_PATH, wfullpath, NULL);
2915-
if (!len || len >= MAX_LONG_PATH) {
2916-
errno = err_win_to_posix(GetLastError());
2917-
return -1;
2918-
}
2919-
2920-
/* over-allocate and fill phantom_symlink_info structure */
2921-
psi = xmalloc(sizeof(struct phantom_symlink_info)
2922-
+ sizeof(wchar_t) * (len + wcslen(wtarget) + 2));
2923-
psi->wlink = (wchar_t *)(psi + 1);
2924-
wcscpy(psi->wlink, wfullpath);
2925-
psi->wtarget = psi->wlink + len + 1;
2926-
wcscpy(psi->wtarget, wtarget);
2927-
2928-
EnterCriticalSection(&phantom_symlinks_cs);
2929-
psi->next = phantom_symlinks;
2930-
phantom_symlinks = psi;
2931-
LeaveCriticalSection(&phantom_symlinks_cs);
2932-
break;
2933-
}
2934-
case PHANTOM_SYMLINK_DIRECTORY:
2935-
/* if we created a dir symlink, process other phantom symlinks */
2936-
process_phantom_symlinks();
2937-
break;
2938-
default:
2939-
break;
2940-
}
2941-
return 0;
2948+
return create_phantom_symlink(wtarget, wlink);
29422949
}
29432950

29442951
#ifndef _WINNT_H

0 commit comments

Comments
 (0)