@@ -447,6 +447,54 @@ static void process_phantom_symlinks(void)
447
447
LeaveCriticalSection (& phantom_symlinks_cs );
448
448
}
449
449
450
+ static int create_phantom_symlink (wchar_t * wtarget , wchar_t * wlink )
451
+ {
452
+ int len ;
453
+
454
+ /* create file symlink */
455
+ if (!CreateSymbolicLinkW (wlink , wtarget , symlink_file_flags )) {
456
+ errno = err_win_to_posix (GetLastError ());
457
+ return -1 ;
458
+ }
459
+
460
+ /* convert to directory symlink if target exists */
461
+ switch (process_phantom_symlink (wtarget , wlink )) {
462
+ case PHANTOM_SYMLINK_RETRY : {
463
+ /* if target doesn't exist, add to phantom symlinks list */
464
+ wchar_t wfullpath [MAX_LONG_PATH ];
465
+ struct phantom_symlink_info * psi ;
466
+
467
+ /* convert to absolute path to be independent of cwd */
468
+ len = GetFullPathNameW (wlink , MAX_LONG_PATH , wfullpath , NULL );
469
+ if (!len || len >= MAX_LONG_PATH ) {
470
+ errno = err_win_to_posix (GetLastError ());
471
+ return -1 ;
472
+ }
473
+
474
+ /* over-allocate and fill phantom_symlink_info structure */
475
+ psi = xmalloc (sizeof (struct phantom_symlink_info ) +
476
+ sizeof (wchar_t ) * (len + wcslen (wtarget ) + 2 ));
477
+ psi -> wlink = (wchar_t * )(psi + 1 );
478
+ wcscpy (psi -> wlink , wfullpath );
479
+ psi -> wtarget = psi -> wlink + len + 1 ;
480
+ wcscpy (psi -> wtarget , wtarget );
481
+
482
+ EnterCriticalSection (& phantom_symlinks_cs );
483
+ psi -> next = phantom_symlinks ;
484
+ phantom_symlinks = psi ;
485
+ LeaveCriticalSection (& phantom_symlinks_cs );
486
+ break ;
487
+ }
488
+ case PHANTOM_SYMLINK_DIRECTORY :
489
+ /* if we created a dir symlink, process other phantom symlinks */
490
+ process_phantom_symlinks ();
491
+ break ;
492
+ default :
493
+ break ;
494
+ }
495
+ return 0 ;
496
+ }
497
+
450
498
/* Normalizes NT paths as returned by some low-level APIs. */
451
499
static wchar_t * normalize_ntpath (wchar_t * wbuf )
452
500
{
@@ -2874,48 +2922,7 @@ int symlink(const char *target, const char *link)
2874
2922
if (wtarget [len ] == '/' )
2875
2923
wtarget [len ] = '\\' ;
2876
2924
2877
- /* create file symlink */
2878
- if (!CreateSymbolicLinkW (wlink , wtarget , symlink_file_flags )) {
2879
- errno = err_win_to_posix (GetLastError ());
2880
- return -1 ;
2881
- }
2882
-
2883
- /* convert to directory symlink if target exists */
2884
- switch (process_phantom_symlink (wtarget , wlink )) {
2885
- case PHANTOM_SYMLINK_RETRY : {
2886
- /* if target doesn't exist, add to phantom symlinks list */
2887
- wchar_t wfullpath [MAX_LONG_PATH ];
2888
- struct phantom_symlink_info * psi ;
2889
-
2890
- /* convert to absolute path to be independent of cwd */
2891
- len = GetFullPathNameW (wlink , MAX_LONG_PATH , wfullpath , NULL );
2892
- if (!len || len >= MAX_LONG_PATH ) {
2893
- errno = err_win_to_posix (GetLastError ());
2894
- return -1 ;
2895
- }
2896
-
2897
- /* over-allocate and fill phantom_symlink_info structure */
2898
- psi = xmalloc (sizeof (struct phantom_symlink_info )
2899
- + sizeof (wchar_t ) * (len + wcslen (wtarget ) + 2 ));
2900
- psi -> wlink = (wchar_t * )(psi + 1 );
2901
- wcscpy (psi -> wlink , wfullpath );
2902
- psi -> wtarget = psi -> wlink + len + 1 ;
2903
- wcscpy (psi -> wtarget , wtarget );
2904
-
2905
- EnterCriticalSection (& phantom_symlinks_cs );
2906
- psi -> next = phantom_symlinks ;
2907
- phantom_symlinks = psi ;
2908
- LeaveCriticalSection (& phantom_symlinks_cs );
2909
- break ;
2910
- }
2911
- case PHANTOM_SYMLINK_DIRECTORY :
2912
- /* if we created a dir symlink, process other phantom symlinks */
2913
- process_phantom_symlinks ();
2914
- break ;
2915
- default :
2916
- break ;
2917
- }
2918
- return 0 ;
2925
+ return create_phantom_symlink (wtarget , wlink );
2919
2926
}
2920
2927
2921
2928
#ifndef _WINNT_H
0 commit comments