Skip to content

Commit 9847064

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

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
{
@@ -2921,48 +2969,7 @@ int symlink(const char *target, const char *link)
29212969
if (wtarget[len] == '/')
29222970
wtarget[len] = '\\';
29232971

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

29682975
#ifndef _WINNT_H

0 commit comments

Comments
 (0)