@@ -429,6 +429,54 @@ static void process_phantom_symlinks(void)
429
429
LeaveCriticalSection (& phantom_symlinks_cs );
430
430
}
431
431
432
+ static int create_phantom_symlink (wchar_t * wtarget , wchar_t * wlink )
433
+ {
434
+ int len ;
435
+
436
+ /* create file symlink */
437
+ if (!CreateSymbolicLinkW (wlink , wtarget , symlink_file_flags )) {
438
+ errno = err_win_to_posix (GetLastError ());
439
+ return -1 ;
440
+ }
441
+
442
+ /* convert to directory symlink if target exists */
443
+ switch (process_phantom_symlink (wtarget , wlink )) {
444
+ case PHANTOM_SYMLINK_RETRY : {
445
+ /* if target doesn't exist, add to phantom symlinks list */
446
+ wchar_t wfullpath [MAX_LONG_PATH ];
447
+ struct phantom_symlink_info * psi ;
448
+
449
+ /* convert to absolute path to be independent of cwd */
450
+ len = GetFullPathNameW (wlink , MAX_LONG_PATH , wfullpath , NULL );
451
+ if (!len || len >= MAX_LONG_PATH ) {
452
+ errno = err_win_to_posix (GetLastError ());
453
+ return -1 ;
454
+ }
455
+
456
+ /* over-allocate and fill phantom_symlink_info structure */
457
+ psi = xmalloc (sizeof (struct phantom_symlink_info ) +
458
+ sizeof (wchar_t ) * (len + wcslen (wtarget ) + 2 ));
459
+ psi -> wlink = (wchar_t * )(psi + 1 );
460
+ wcscpy (psi -> wlink , wfullpath );
461
+ psi -> wtarget = psi -> wlink + len + 1 ;
462
+ wcscpy (psi -> wtarget , wtarget );
463
+
464
+ EnterCriticalSection (& phantom_symlinks_cs );
465
+ psi -> next = phantom_symlinks ;
466
+ phantom_symlinks = psi ;
467
+ LeaveCriticalSection (& phantom_symlinks_cs );
468
+ break ;
469
+ }
470
+ case PHANTOM_SYMLINK_DIRECTORY :
471
+ /* if we created a dir symlink, process other phantom symlinks */
472
+ process_phantom_symlinks ();
473
+ break ;
474
+ default :
475
+ break ;
476
+ }
477
+ return 0 ;
478
+ }
479
+
432
480
/* Normalizes NT paths as returned by some low-level APIs. */
433
481
static wchar_t * normalize_ntpath (wchar_t * wbuf )
434
482
{
@@ -2859,48 +2907,7 @@ int symlink(const char *target, const char *link)
2859
2907
if (wtarget [len ] == '/' )
2860
2908
wtarget [len ] = '\\' ;
2861
2909
2862
- /* create file symlink */
2863
- if (!CreateSymbolicLinkW (wlink , wtarget , symlink_file_flags )) {
2864
- errno = err_win_to_posix (GetLastError ());
2865
- return -1 ;
2866
- }
2867
-
2868
- /* convert to directory symlink if target exists */
2869
- switch (process_phantom_symlink (wtarget , wlink )) {
2870
- case PHANTOM_SYMLINK_RETRY : {
2871
- /* if target doesn't exist, add to phantom symlinks list */
2872
- wchar_t wfullpath [MAX_LONG_PATH ];
2873
- struct phantom_symlink_info * psi ;
2874
-
2875
- /* convert to absolute path to be independent of cwd */
2876
- len = GetFullPathNameW (wlink , MAX_LONG_PATH , wfullpath , NULL );
2877
- if (!len || len >= MAX_LONG_PATH ) {
2878
- errno = err_win_to_posix (GetLastError ());
2879
- return -1 ;
2880
- }
2881
-
2882
- /* over-allocate and fill phantom_symlink_info structure */
2883
- psi = xmalloc (sizeof (struct phantom_symlink_info )
2884
- + sizeof (wchar_t ) * (len + wcslen (wtarget ) + 2 ));
2885
- psi -> wlink = (wchar_t * )(psi + 1 );
2886
- wcscpy (psi -> wlink , wfullpath );
2887
- psi -> wtarget = psi -> wlink + len + 1 ;
2888
- wcscpy (psi -> wtarget , wtarget );
2889
-
2890
- EnterCriticalSection (& phantom_symlinks_cs );
2891
- psi -> next = phantom_symlinks ;
2892
- phantom_symlinks = psi ;
2893
- LeaveCriticalSection (& phantom_symlinks_cs );
2894
- break ;
2895
- }
2896
- case PHANTOM_SYMLINK_DIRECTORY :
2897
- /* if we created a dir symlink, process other phantom symlinks */
2898
- process_phantom_symlinks ();
2899
- break ;
2900
- default :
2901
- break ;
2902
- }
2903
- return 0 ;
2910
+ return create_phantom_symlink (wtarget , wlink );
2904
2911
}
2905
2912
2906
2913
#ifndef _WINNT_H
0 commit comments