Skip to content

Commit fdd6b16

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

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
@@ -399,6 +399,54 @@ static void process_phantom_symlinks(void)
399399
LeaveCriticalSection(&phantom_symlinks_cs);
400400
}
401401

402+
static int create_phantom_symlink(wchar_t *wtarget, wchar_t *wlink)
403+
{
404+
int len;
405+
406+
/* create file symlink */
407+
if (!CreateSymbolicLinkW(wlink, wtarget, symlink_file_flags)) {
408+
errno = err_win_to_posix(GetLastError());
409+
return -1;
410+
}
411+
412+
/* convert to directory symlink if target exists */
413+
switch (process_phantom_symlink(wtarget, wlink)) {
414+
case PHANTOM_SYMLINK_RETRY: {
415+
/* if target doesn't exist, add to phantom symlinks list */
416+
wchar_t wfullpath[MAX_LONG_PATH];
417+
struct phantom_symlink_info *psi;
418+
419+
/* convert to absolute path to be independent of cwd */
420+
len = GetFullPathNameW(wlink, MAX_LONG_PATH, wfullpath, NULL);
421+
if (!len || len >= MAX_LONG_PATH) {
422+
errno = err_win_to_posix(GetLastError());
423+
return -1;
424+
}
425+
426+
/* over-allocate and fill phantom_symlink_info structure */
427+
psi = xmalloc(sizeof(struct phantom_symlink_info) +
428+
sizeof(wchar_t) * (len + wcslen(wtarget) + 2));
429+
psi->wlink = (wchar_t *)(psi + 1);
430+
wcscpy(psi->wlink, wfullpath);
431+
psi->wtarget = psi->wlink + len + 1;
432+
wcscpy(psi->wtarget, wtarget);
433+
434+
EnterCriticalSection(&phantom_symlinks_cs);
435+
psi->next = phantom_symlinks;
436+
phantom_symlinks = psi;
437+
LeaveCriticalSection(&phantom_symlinks_cs);
438+
break;
439+
}
440+
case PHANTOM_SYMLINK_DIRECTORY:
441+
/* if we created a dir symlink, process other phantom symlinks */
442+
process_phantom_symlinks();
443+
break;
444+
default:
445+
break;
446+
}
447+
return 0;
448+
}
449+
402450
/* Normalizes NT paths as returned by some low-level APIs. */
403451
static wchar_t *normalize_ntpath(wchar_t *wbuf)
404452
{
@@ -2498,48 +2546,7 @@ int symlink(const char *target, const char *link)
24982546
if (wtarget[len] == '/')
24992547
wtarget[len] = '\\';
25002548

2501-
/* create file symlink */
2502-
if (!CreateSymbolicLinkW(wlink, wtarget, symlink_file_flags)) {
2503-
errno = err_win_to_posix(GetLastError());
2504-
return -1;
2505-
}
2506-
2507-
/* convert to directory symlink if target exists */
2508-
switch (process_phantom_symlink(wtarget, wlink)) {
2509-
case PHANTOM_SYMLINK_RETRY: {
2510-
/* if target doesn't exist, add to phantom symlinks list */
2511-
wchar_t wfullpath[MAX_LONG_PATH];
2512-
struct phantom_symlink_info *psi;
2513-
2514-
/* convert to absolute path to be independent of cwd */
2515-
len = GetFullPathNameW(wlink, MAX_LONG_PATH, wfullpath, NULL);
2516-
if (!len || len >= MAX_LONG_PATH) {
2517-
errno = err_win_to_posix(GetLastError());
2518-
return -1;
2519-
}
2520-
2521-
/* over-allocate and fill phantom_symlink_info structure */
2522-
psi = xmalloc(sizeof(struct phantom_symlink_info)
2523-
+ sizeof(wchar_t) * (len + wcslen(wtarget) + 2));
2524-
psi->wlink = (wchar_t *)(psi + 1);
2525-
wcscpy(psi->wlink, wfullpath);
2526-
psi->wtarget = psi->wlink + len + 1;
2527-
wcscpy(psi->wtarget, wtarget);
2528-
2529-
EnterCriticalSection(&phantom_symlinks_cs);
2530-
psi->next = phantom_symlinks;
2531-
phantom_symlinks = psi;
2532-
LeaveCriticalSection(&phantom_symlinks_cs);
2533-
break;
2534-
}
2535-
case PHANTOM_SYMLINK_DIRECTORY:
2536-
/* if we created a dir symlink, process other phantom symlinks */
2537-
process_phantom_symlinks();
2538-
break;
2539-
default:
2540-
break;
2541-
}
2542-
return 0;
2549+
return create_phantom_symlink(wtarget, wlink);
25432550
}
25442551

25452552
#ifndef _WINNT_H

0 commit comments

Comments
 (0)