Skip to content

Commit 2bbeaee

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

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
@@ -444,6 +444,54 @@ static void process_phantom_symlinks(void)
444444
LeaveCriticalSection(&phantom_symlinks_cs);
445445
}
446446

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

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

29182925
#ifndef _WINNT_H

0 commit comments

Comments
 (0)