Skip to content

Commit 09eb3e4

Browse files
piscisaureusmjcheetham
authored andcommitted
Win32: symlink: move phantom symlink creation to a separate function
Signed-off-by: Bert Belder <[email protected]>
1 parent 46e6fb9 commit 09eb3e4

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
{
@@ -2892,48 +2940,7 @@ int symlink(const char *target, const char *link)
28922940
if (wtarget[len] == '/')
28932941
wtarget[len] = '\\';
28942942

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

29392946
#ifndef _WINNT_H

0 commit comments

Comments
 (0)