Skip to content

Commit dacdc89

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

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
{
@@ -3151,48 +3199,7 @@ int symlink(const char *target, const char *link)
31513199
if (wtarget[len] == '/')
31523200
wtarget[len] = '\\';
31533201

3154-
/* create file symlink */
3155-
if (!CreateSymbolicLinkW(wlink, wtarget, symlink_file_flags)) {
3156-
errno = err_win_to_posix(GetLastError());
3157-
return -1;
3158-
}
3159-
3160-
/* convert to directory symlink if target exists */
3161-
switch (process_phantom_symlink(wtarget, wlink)) {
3162-
case PHANTOM_SYMLINK_RETRY: {
3163-
/* if target doesn't exist, add to phantom symlinks list */
3164-
wchar_t wfullpath[MAX_LONG_PATH];
3165-
struct phantom_symlink_info *psi;
3166-
3167-
/* convert to absolute path to be independent of cwd */
3168-
len = GetFullPathNameW(wlink, MAX_LONG_PATH, wfullpath, NULL);
3169-
if (!len || len >= MAX_LONG_PATH) {
3170-
errno = err_win_to_posix(GetLastError());
3171-
return -1;
3172-
}
3173-
3174-
/* over-allocate and fill phantom_symlink_info structure */
3175-
psi = xmalloc(sizeof(struct phantom_symlink_info)
3176-
+ sizeof(wchar_t) * (len + wcslen(wtarget) + 2));
3177-
psi->wlink = (wchar_t *)(psi + 1);
3178-
wcscpy(psi->wlink, wfullpath);
3179-
psi->wtarget = psi->wlink + len + 1;
3180-
wcscpy(psi->wtarget, wtarget);
3181-
3182-
EnterCriticalSection(&phantom_symlinks_cs);
3183-
psi->next = phantom_symlinks;
3184-
phantom_symlinks = psi;
3185-
LeaveCriticalSection(&phantom_symlinks_cs);
3186-
break;
3187-
}
3188-
case PHANTOM_SYMLINK_DIRECTORY:
3189-
/* if we created a dir symlink, process other phantom symlinks */
3190-
process_phantom_symlinks();
3191-
break;
3192-
default:
3193-
break;
3194-
}
3195-
return 0;
3202+
return create_phantom_symlink(wtarget, wlink);
31963203
}
31973204

31983205
#ifndef _WINNT_H

0 commit comments

Comments
 (0)