Skip to content

Commit 68d5ad8

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

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
{
@@ -2814,48 +2862,7 @@ int symlink(const char *target, const char *link)
28142862
if (wtarget[len] == '/')
28152863
wtarget[len] = '\\';
28162864

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

28612868
#ifndef _WINNT_H

0 commit comments

Comments
 (0)