Skip to content

Commit fae8c24

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

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
@@ -427,6 +427,54 @@ static void process_phantom_symlinks(void)
427427
LeaveCriticalSection(&phantom_symlinks_cs);
428428
}
429429

430+
static int create_phantom_symlink(wchar_t *wtarget, wchar_t *wlink)
431+
{
432+
int len;
433+
434+
/* create file symlink */
435+
if (!CreateSymbolicLinkW(wlink, wtarget, symlink_file_flags)) {
436+
errno = err_win_to_posix(GetLastError());
437+
return -1;
438+
}
439+
440+
/* convert to directory symlink if target exists */
441+
switch (process_phantom_symlink(wtarget, wlink)) {
442+
case PHANTOM_SYMLINK_RETRY: {
443+
/* if target doesn't exist, add to phantom symlinks list */
444+
wchar_t wfullpath[MAX_LONG_PATH];
445+
struct phantom_symlink_info *psi;
446+
447+
/* convert to absolute path to be independent of cwd */
448+
len = GetFullPathNameW(wlink, MAX_LONG_PATH, wfullpath, NULL);
449+
if (!len || len >= MAX_LONG_PATH) {
450+
errno = err_win_to_posix(GetLastError());
451+
return -1;
452+
}
453+
454+
/* over-allocate and fill phantom_symlink_info structure */
455+
psi = xmalloc(sizeof(struct phantom_symlink_info) +
456+
sizeof(wchar_t) * (len + wcslen(wtarget) + 2));
457+
psi->wlink = (wchar_t *)(psi + 1);
458+
wcscpy(psi->wlink, wfullpath);
459+
psi->wtarget = psi->wlink + len + 1;
460+
wcscpy(psi->wtarget, wtarget);
461+
462+
EnterCriticalSection(&phantom_symlinks_cs);
463+
psi->next = phantom_symlinks;
464+
phantom_symlinks = psi;
465+
LeaveCriticalSection(&phantom_symlinks_cs);
466+
break;
467+
}
468+
case PHANTOM_SYMLINK_DIRECTORY:
469+
/* if we created a dir symlink, process other phantom symlinks */
470+
process_phantom_symlinks();
471+
break;
472+
default:
473+
break;
474+
}
475+
return 0;
476+
}
477+
430478
/* Normalizes NT paths as returned by some low-level APIs. */
431479
static wchar_t *normalize_ntpath(wchar_t *wbuf)
432480
{
@@ -2810,48 +2858,7 @@ int symlink(const char *target, const char *link)
28102858
if (wtarget[len] == '/')
28112859
wtarget[len] = '\\';
28122860

2813-
/* create file symlink */
2814-
if (!CreateSymbolicLinkW(wlink, wtarget, symlink_file_flags)) {
2815-
errno = err_win_to_posix(GetLastError());
2816-
return -1;
2817-
}
2818-
2819-
/* convert to directory symlink if target exists */
2820-
switch (process_phantom_symlink(wtarget, wlink)) {
2821-
case PHANTOM_SYMLINK_RETRY: {
2822-
/* if target doesn't exist, add to phantom symlinks list */
2823-
wchar_t wfullpath[MAX_LONG_PATH];
2824-
struct phantom_symlink_info *psi;
2825-
2826-
/* convert to absolute path to be independent of cwd */
2827-
len = GetFullPathNameW(wlink, MAX_LONG_PATH, wfullpath, NULL);
2828-
if (!len || len >= MAX_LONG_PATH) {
2829-
errno = err_win_to_posix(GetLastError());
2830-
return -1;
2831-
}
2832-
2833-
/* over-allocate and fill phantom_symlink_info structure */
2834-
psi = xmalloc(sizeof(struct phantom_symlink_info)
2835-
+ sizeof(wchar_t) * (len + wcslen(wtarget) + 2));
2836-
psi->wlink = (wchar_t *)(psi + 1);
2837-
wcscpy(psi->wlink, wfullpath);
2838-
psi->wtarget = psi->wlink + len + 1;
2839-
wcscpy(psi->wtarget, wtarget);
2840-
2841-
EnterCriticalSection(&phantom_symlinks_cs);
2842-
psi->next = phantom_symlinks;
2843-
phantom_symlinks = psi;
2844-
LeaveCriticalSection(&phantom_symlinks_cs);
2845-
break;
2846-
}
2847-
case PHANTOM_SYMLINK_DIRECTORY:
2848-
/* if we created a dir symlink, process other phantom symlinks */
2849-
process_phantom_symlinks();
2850-
break;
2851-
default:
2852-
break;
2853-
}
2854-
return 0;
2861+
return create_phantom_symlink(wtarget, wlink);
28552862
}
28562863

28572864
#ifndef _WINNT_H

0 commit comments

Comments
 (0)