@@ -456,6 +456,54 @@ static void process_phantom_symlinks(void)
456
456
LeaveCriticalSection (& phantom_symlinks_cs );
457
457
}
458
458
459
+ static int create_phantom_symlink (wchar_t * wtarget , wchar_t * wlink )
460
+ {
461
+ int len ;
462
+
463
+ /* create file symlink */
464
+ if (!CreateSymbolicLinkW (wlink , wtarget , symlink_file_flags )) {
465
+ errno = err_win_to_posix (GetLastError ());
466
+ return -1 ;
467
+ }
468
+
469
+ /* convert to directory symlink if target exists */
470
+ switch (process_phantom_symlink (wtarget , wlink )) {
471
+ case PHANTOM_SYMLINK_RETRY : {
472
+ /* if target doesn't exist, add to phantom symlinks list */
473
+ wchar_t wfullpath [MAX_LONG_PATH ];
474
+ struct phantom_symlink_info * psi ;
475
+
476
+ /* convert to absolute path to be independent of cwd */
477
+ len = GetFullPathNameW (wlink , MAX_LONG_PATH , wfullpath , NULL );
478
+ if (!len || len >= MAX_LONG_PATH ) {
479
+ errno = err_win_to_posix (GetLastError ());
480
+ return -1 ;
481
+ }
482
+
483
+ /* over-allocate and fill phantom_symlink_info structure */
484
+ psi = xmalloc (sizeof (struct phantom_symlink_info ) +
485
+ sizeof (wchar_t ) * (len + wcslen (wtarget ) + 2 ));
486
+ psi -> wlink = (wchar_t * )(psi + 1 );
487
+ wcscpy (psi -> wlink , wfullpath );
488
+ psi -> wtarget = psi -> wlink + len + 1 ;
489
+ wcscpy (psi -> wtarget , wtarget );
490
+
491
+ EnterCriticalSection (& phantom_symlinks_cs );
492
+ psi -> next = phantom_symlinks ;
493
+ phantom_symlinks = psi ;
494
+ LeaveCriticalSection (& phantom_symlinks_cs );
495
+ break ;
496
+ }
497
+ case PHANTOM_SYMLINK_DIRECTORY :
498
+ /* if we created a dir symlink, process other phantom symlinks */
499
+ process_phantom_symlinks ();
500
+ break ;
501
+ default :
502
+ break ;
503
+ }
504
+ return 0 ;
505
+ }
506
+
459
507
/* Normalizes NT paths as returned by some low-level APIs. */
460
508
static wchar_t * normalize_ntpath (wchar_t * wbuf )
461
509
{
@@ -2892,48 +2940,7 @@ int symlink(const char *target, const char *link)
2892
2940
if (wtarget [len ] == '/' )
2893
2941
wtarget [len ] = '\\' ;
2894
2942
2895
- /* create file symlink */
2896
- if (!CreateSymbolicLinkW (wlink , wtarget , symlink_file_flags )) {
2897
- errno = err_win_to_posix (GetLastError ());
2898
- return -1 ;
2899
- }
2900
-
2901
- /* convert to directory symlink if target exists */
2902
- switch (process_phantom_symlink (wtarget , wlink )) {
2903
- case PHANTOM_SYMLINK_RETRY : {
2904
- /* if target doesn't exist, add to phantom symlinks list */
2905
- wchar_t wfullpath [MAX_LONG_PATH ];
2906
- struct phantom_symlink_info * psi ;
2907
-
2908
- /* convert to absolute path to be independent of cwd */
2909
- len = GetFullPathNameW (wlink , MAX_LONG_PATH , wfullpath , NULL );
2910
- if (!len || len >= MAX_LONG_PATH ) {
2911
- errno = err_win_to_posix (GetLastError ());
2912
- return -1 ;
2913
- }
2914
-
2915
- /* over-allocate and fill phantom_symlink_info structure */
2916
- psi = xmalloc (sizeof (struct phantom_symlink_info )
2917
- + sizeof (wchar_t ) * (len + wcslen (wtarget ) + 2 ));
2918
- psi -> wlink = (wchar_t * )(psi + 1 );
2919
- wcscpy (psi -> wlink , wfullpath );
2920
- psi -> wtarget = psi -> wlink + len + 1 ;
2921
- wcscpy (psi -> wtarget , wtarget );
2922
-
2923
- EnterCriticalSection (& phantom_symlinks_cs );
2924
- psi -> next = phantom_symlinks ;
2925
- phantom_symlinks = psi ;
2926
- LeaveCriticalSection (& phantom_symlinks_cs );
2927
- break ;
2928
- }
2929
- case PHANTOM_SYMLINK_DIRECTORY :
2930
- /* if we created a dir symlink, process other phantom symlinks */
2931
- process_phantom_symlinks ();
2932
- break ;
2933
- default :
2934
- break ;
2935
- }
2936
- return 0 ;
2943
+ return create_phantom_symlink (wtarget , wlink );
2937
2944
}
2938
2945
2939
2946
#ifndef _WINNT_H
0 commit comments