Skip to content

Commit 42d4555

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

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

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

3129-
/* create file symlink */
3130-
if (!CreateSymbolicLinkW(wlink, wtarget, symlink_file_flags)) {
3131-
errno = err_win_to_posix(GetLastError());
3132-
return -1;
3133-
}
3134-
3135-
/* convert to directory symlink if target exists */
3136-
switch (process_phantom_symlink(wtarget, wlink)) {
3137-
case PHANTOM_SYMLINK_RETRY: {
3138-
/* if target doesn't exist, add to phantom symlinks list */
3139-
wchar_t wfullpath[MAX_LONG_PATH];
3140-
struct phantom_symlink_info *psi;
3141-
3142-
/* convert to absolute path to be independent of cwd */
3143-
len = GetFullPathNameW(wlink, MAX_LONG_PATH, wfullpath, NULL);
3144-
if (!len || len >= MAX_LONG_PATH) {
3145-
errno = err_win_to_posix(GetLastError());
3146-
return -1;
3147-
}
3148-
3149-
/* over-allocate and fill phantom_symlink_info structure */
3150-
psi = xmalloc(sizeof(struct phantom_symlink_info)
3151-
+ sizeof(wchar_t) * (len + wcslen(wtarget) + 2));
3152-
psi->wlink = (wchar_t *)(psi + 1);
3153-
wcscpy(psi->wlink, wfullpath);
3154-
psi->wtarget = psi->wlink + len + 1;
3155-
wcscpy(psi->wtarget, wtarget);
3156-
3157-
EnterCriticalSection(&phantom_symlinks_cs);
3158-
psi->next = phantom_symlinks;
3159-
phantom_symlinks = psi;
3160-
LeaveCriticalSection(&phantom_symlinks_cs);
3161-
break;
3162-
}
3163-
case PHANTOM_SYMLINK_DIRECTORY:
3164-
/* if we created a dir symlink, process other phantom symlinks */
3165-
process_phantom_symlinks();
3166-
break;
3167-
default:
3168-
break;
3169-
}
3170-
return 0;
3177+
return create_phantom_symlink(wtarget, wlink);
31713178
}
31723179

31733180
#ifndef _WINNT_H

0 commit comments

Comments
 (0)