Skip to content

Commit ce83624

Browse files
piscisaureusGit for Windows Build Agent
authored andcommitted
Win32: symlink: move phantom symlink creation to a separate function
Signed-off-by: Bert Belder <[email protected]>
1 parent d86c46b commit ce83624

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
{
@@ -3124,48 +3172,7 @@ int symlink(const char *target, const char *link)
31243172
if (wtarget[len] == '/')
31253173
wtarget[len] = '\\';
31263174

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

31713178
#ifndef _WINNT_H

0 commit comments

Comments
 (0)