Skip to content

Commit e13c7c6

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

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

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

3067-
/* create file symlink */
3068-
if (!CreateSymbolicLinkW(wlink, wtarget, symlink_file_flags)) {
3069-
errno = err_win_to_posix(GetLastError());
3070-
return -1;
3071-
}
3072-
3073-
/* convert to directory symlink if target exists */
3074-
switch (process_phantom_symlink(wtarget, wlink)) {
3075-
case PHANTOM_SYMLINK_RETRY: {
3076-
/* if target doesn't exist, add to phantom symlinks list */
3077-
wchar_t wfullpath[MAX_LONG_PATH];
3078-
struct phantom_symlink_info *psi;
3079-
3080-
/* convert to absolute path to be independent of cwd */
3081-
len = GetFullPathNameW(wlink, MAX_LONG_PATH, wfullpath, NULL);
3082-
if (!len || len >= MAX_LONG_PATH) {
3083-
errno = err_win_to_posix(GetLastError());
3084-
return -1;
3085-
}
3086-
3087-
/* over-allocate and fill phantom_symlink_info structure */
3088-
psi = xmalloc(sizeof(struct phantom_symlink_info)
3089-
+ sizeof(wchar_t) * (len + wcslen(wtarget) + 2));
3090-
psi->wlink = (wchar_t *)(psi + 1);
3091-
wcscpy(psi->wlink, wfullpath);
3092-
psi->wtarget = psi->wlink + len + 1;
3093-
wcscpy(psi->wtarget, wtarget);
3094-
3095-
EnterCriticalSection(&phantom_symlinks_cs);
3096-
psi->next = phantom_symlinks;
3097-
phantom_symlinks = psi;
3098-
LeaveCriticalSection(&phantom_symlinks_cs);
3099-
break;
3100-
}
3101-
case PHANTOM_SYMLINK_DIRECTORY:
3102-
/* if we created a dir symlink, process other phantom symlinks */
3103-
process_phantom_symlinks();
3104-
break;
3105-
default:
3106-
break;
3107-
}
3108-
return 0;
3115+
return create_phantom_symlink(wtarget, wlink);
31093116
}
31103117

31113118
#ifndef _WINNT_H

0 commit comments

Comments
 (0)