Skip to content

Commit 27c9ddb

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

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
@@ -460,6 +460,54 @@ static void process_phantom_symlinks(void)
460460
LeaveCriticalSection(&phantom_symlinks_cs);
461461
}
462462

463+
static int create_phantom_symlink(wchar_t *wtarget, wchar_t *wlink)
464+
{
465+
int len;
466+
467+
/* create file symlink */
468+
if (!CreateSymbolicLinkW(wlink, wtarget, symlink_file_flags)) {
469+
errno = err_win_to_posix(GetLastError());
470+
return -1;
471+
}
472+
473+
/* convert to directory symlink if target exists */
474+
switch (process_phantom_symlink(wtarget, wlink)) {
475+
case PHANTOM_SYMLINK_RETRY: {
476+
/* if target doesn't exist, add to phantom symlinks list */
477+
wchar_t wfullpath[MAX_LONG_PATH];
478+
struct phantom_symlink_info *psi;
479+
480+
/* convert to absolute path to be independent of cwd */
481+
len = GetFullPathNameW(wlink, MAX_LONG_PATH, wfullpath, NULL);
482+
if (!len || len >= MAX_LONG_PATH) {
483+
errno = err_win_to_posix(GetLastError());
484+
return -1;
485+
}
486+
487+
/* over-allocate and fill phantom_symlink_info structure */
488+
psi = xmalloc(sizeof(struct phantom_symlink_info) +
489+
sizeof(wchar_t) * (len + wcslen(wtarget) + 2));
490+
psi->wlink = (wchar_t *)(psi + 1);
491+
wcscpy(psi->wlink, wfullpath);
492+
psi->wtarget = psi->wlink + len + 1;
493+
wcscpy(psi->wtarget, wtarget);
494+
495+
EnterCriticalSection(&phantom_symlinks_cs);
496+
psi->next = phantom_symlinks;
497+
phantom_symlinks = psi;
498+
LeaveCriticalSection(&phantom_symlinks_cs);
499+
break;
500+
}
501+
case PHANTOM_SYMLINK_DIRECTORY:
502+
/* if we created a dir symlink, process other phantom symlinks */
503+
process_phantom_symlinks();
504+
break;
505+
default:
506+
break;
507+
}
508+
return 0;
509+
}
510+
463511
/* Normalizes NT paths as returned by some low-level APIs. */
464512
static wchar_t *normalize_ntpath(wchar_t *wbuf)
465513
{
@@ -3063,48 +3111,7 @@ int symlink(const char *target, const char *link)
30633111
if (wtarget[len] == '/')
30643112
wtarget[len] = '\\';
30653113

3066-
/* create file symlink */
3067-
if (!CreateSymbolicLinkW(wlink, wtarget, symlink_file_flags)) {
3068-
errno = err_win_to_posix(GetLastError());
3069-
return -1;
3070-
}
3071-
3072-
/* convert to directory symlink if target exists */
3073-
switch (process_phantom_symlink(wtarget, wlink)) {
3074-
case PHANTOM_SYMLINK_RETRY: {
3075-
/* if target doesn't exist, add to phantom symlinks list */
3076-
wchar_t wfullpath[MAX_LONG_PATH];
3077-
struct phantom_symlink_info *psi;
3078-
3079-
/* convert to absolute path to be independent of cwd */
3080-
len = GetFullPathNameW(wlink, MAX_LONG_PATH, wfullpath, NULL);
3081-
if (!len || len >= MAX_LONG_PATH) {
3082-
errno = err_win_to_posix(GetLastError());
3083-
return -1;
3084-
}
3085-
3086-
/* over-allocate and fill phantom_symlink_info structure */
3087-
psi = xmalloc(sizeof(struct phantom_symlink_info)
3088-
+ sizeof(wchar_t) * (len + wcslen(wtarget) + 2));
3089-
psi->wlink = (wchar_t *)(psi + 1);
3090-
wcscpy(psi->wlink, wfullpath);
3091-
psi->wtarget = psi->wlink + len + 1;
3092-
wcscpy(psi->wtarget, wtarget);
3093-
3094-
EnterCriticalSection(&phantom_symlinks_cs);
3095-
psi->next = phantom_symlinks;
3096-
phantom_symlinks = psi;
3097-
LeaveCriticalSection(&phantom_symlinks_cs);
3098-
break;
3099-
}
3100-
case PHANTOM_SYMLINK_DIRECTORY:
3101-
/* if we created a dir symlink, process other phantom symlinks */
3102-
process_phantom_symlinks();
3103-
break;
3104-
default:
3105-
break;
3106-
}
3107-
return 0;
3114+
return create_phantom_symlink(wtarget, wlink);
31083115
}
31093116

31103117
#ifndef _WINNT_H

0 commit comments

Comments
 (0)