Skip to content

Commit 1ab71a5

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

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

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

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

28622869
#ifndef _WINNT_H

0 commit comments

Comments
 (0)