Skip to content

Commit b84c49e

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

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

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

2862-
/* create file symlink */
2863-
if (!CreateSymbolicLinkW(wlink, wtarget, symlink_file_flags)) {
2864-
errno = err_win_to_posix(GetLastError());
2865-
return -1;
2866-
}
2867-
2868-
/* convert to directory symlink if target exists */
2869-
switch (process_phantom_symlink(wtarget, wlink)) {
2870-
case PHANTOM_SYMLINK_RETRY: {
2871-
/* if target doesn't exist, add to phantom symlinks list */
2872-
wchar_t wfullpath[MAX_LONG_PATH];
2873-
struct phantom_symlink_info *psi;
2874-
2875-
/* convert to absolute path to be independent of cwd */
2876-
len = GetFullPathNameW(wlink, MAX_LONG_PATH, wfullpath, NULL);
2877-
if (!len || len >= MAX_LONG_PATH) {
2878-
errno = err_win_to_posix(GetLastError());
2879-
return -1;
2880-
}
2881-
2882-
/* over-allocate and fill phantom_symlink_info structure */
2883-
psi = xmalloc(sizeof(struct phantom_symlink_info)
2884-
+ sizeof(wchar_t) * (len + wcslen(wtarget) + 2));
2885-
psi->wlink = (wchar_t *)(psi + 1);
2886-
wcscpy(psi->wlink, wfullpath);
2887-
psi->wtarget = psi->wlink + len + 1;
2888-
wcscpy(psi->wtarget, wtarget);
2889-
2890-
EnterCriticalSection(&phantom_symlinks_cs);
2891-
psi->next = phantom_symlinks;
2892-
phantom_symlinks = psi;
2893-
LeaveCriticalSection(&phantom_symlinks_cs);
2894-
break;
2895-
}
2896-
case PHANTOM_SYMLINK_DIRECTORY:
2897-
/* if we created a dir symlink, process other phantom symlinks */
2898-
process_phantom_symlinks();
2899-
break;
2900-
default:
2901-
break;
2902-
}
2903-
return 0;
2910+
return create_phantom_symlink(wtarget, wlink);
29042911
}
29052912

29062913
#ifndef _WINNT_H

0 commit comments

Comments
 (0)