Skip to content

Commit f087676

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

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
@@ -460,6 +460,54 @@ static void process_phantom_symlinks(void)
460460
LeaveCriticalSection(&phantom_symlinks_cs);
461461
}
462462

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

2920-
/* create file symlink */
2921-
if (!CreateSymbolicLinkW(wlink, wtarget, symlink_file_flags)) {
2922-
errno = err_win_to_posix(GetLastError());
2923-
return -1;
2924-
}
2925-
2926-
/* convert to directory symlink if target exists */
2927-
switch (process_phantom_symlink(wtarget, wlink)) {
2928-
case PHANTOM_SYMLINK_RETRY: {
2929-
/* if target doesn't exist, add to phantom symlinks list */
2930-
wchar_t wfullpath[MAX_LONG_PATH];
2931-
struct phantom_symlink_info *psi;
2932-
2933-
/* convert to absolute path to be independent of cwd */
2934-
len = GetFullPathNameW(wlink, MAX_LONG_PATH, wfullpath, NULL);
2935-
if (!len || len >= MAX_LONG_PATH) {
2936-
errno = err_win_to_posix(GetLastError());
2937-
return -1;
2938-
}
2939-
2940-
/* over-allocate and fill phantom_symlink_info structure */
2941-
psi = xmalloc(sizeof(struct phantom_symlink_info)
2942-
+ sizeof(wchar_t) * (len + wcslen(wtarget) + 2));
2943-
psi->wlink = (wchar_t *)(psi + 1);
2944-
wcscpy(psi->wlink, wfullpath);
2945-
psi->wtarget = psi->wlink + len + 1;
2946-
wcscpy(psi->wtarget, wtarget);
2947-
2948-
EnterCriticalSection(&phantom_symlinks_cs);
2949-
psi->next = phantom_symlinks;
2950-
phantom_symlinks = psi;
2951-
LeaveCriticalSection(&phantom_symlinks_cs);
2952-
break;
2953-
}
2954-
case PHANTOM_SYMLINK_DIRECTORY:
2955-
/* if we created a dir symlink, process other phantom symlinks */
2956-
process_phantom_symlinks();
2957-
break;
2958-
default:
2959-
break;
2960-
}
2961-
return 0;
2968+
return create_phantom_symlink(wtarget, wlink);
29622969
}
29632970

29642971
#ifndef _WINNT_H

0 commit comments

Comments
 (0)