@@ -460,6 +460,54 @@ static void process_phantom_symlinks(void)
460
460
LeaveCriticalSection (& phantom_symlinks_cs );
461
461
}
462
462
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
+
463
511
/* Normalizes NT paths as returned by some low-level APIs. */
464
512
static wchar_t * normalize_ntpath (wchar_t * wbuf )
465
513
{
@@ -2917,48 +2965,7 @@ int symlink(const char *target, const char *link)
2917
2965
if (wtarget [len ] == '/' )
2918
2966
wtarget [len ] = '\\' ;
2919
2967
2920
- /* create file symlink */
2921
- if (!CreateSymbolicLinkW (wlink , wtarget , symlink_file_flags )) {
2922
- errno = err_win_to_posix (GetLastError ());
2923
- return -1 ;
2924
- }
2925
-
2926
- /* convert to directory symlink if target exists */
2927
- switch (process_phantom_symlink (wtarget , wlink )) {
2928
- case PHANTOM_SYMLINK_RETRY : {
2929
- /* if target doesn't exist, add to phantom symlinks list */
2930
- wchar_t wfullpath [MAX_LONG_PATH ];
2931
- struct phantom_symlink_info * psi ;
2932
-
2933
- /* convert to absolute path to be independent of cwd */
2934
- len = GetFullPathNameW (wlink , MAX_LONG_PATH , wfullpath , NULL );
2935
- if (!len || len >= MAX_LONG_PATH ) {
2936
- errno = err_win_to_posix (GetLastError ());
2937
- return -1 ;
2938
- }
2939
-
2940
- /* over-allocate and fill phantom_symlink_info structure */
2941
- psi = xmalloc (sizeof (struct phantom_symlink_info )
2942
- + sizeof (wchar_t ) * (len + wcslen (wtarget ) + 2 ));
2943
- psi -> wlink = (wchar_t * )(psi + 1 );
2944
- wcscpy (psi -> wlink , wfullpath );
2945
- psi -> wtarget = psi -> wlink + len + 1 ;
2946
- wcscpy (psi -> wtarget , wtarget );
2947
-
2948
- EnterCriticalSection (& phantom_symlinks_cs );
2949
- psi -> next = phantom_symlinks ;
2950
- phantom_symlinks = psi ;
2951
- LeaveCriticalSection (& phantom_symlinks_cs );
2952
- break ;
2953
- }
2954
- case PHANTOM_SYMLINK_DIRECTORY :
2955
- /* if we created a dir symlink, process other phantom symlinks */
2956
- process_phantom_symlinks ();
2957
- break ;
2958
- default :
2959
- break ;
2960
- }
2961
- return 0 ;
2968
+ return create_phantom_symlink (wtarget , wlink );
2962
2969
}
2963
2970
2964
2971
#ifndef _WINNT_H
0 commit comments