@@ -428,6 +428,54 @@ static void process_phantom_symlinks(void)
428
428
LeaveCriticalSection (& phantom_symlinks_cs );
429
429
}
430
430
431
+ static int create_phantom_symlink (wchar_t * wtarget , wchar_t * wlink )
432
+ {
433
+ int len ;
434
+
435
+ /* create file symlink */
436
+ if (!CreateSymbolicLinkW (wlink , wtarget , symlink_file_flags )) {
437
+ errno = err_win_to_posix (GetLastError ());
438
+ return -1 ;
439
+ }
440
+
441
+ /* convert to directory symlink if target exists */
442
+ switch (process_phantom_symlink (wtarget , wlink )) {
443
+ case PHANTOM_SYMLINK_RETRY : {
444
+ /* if target doesn't exist, add to phantom symlinks list */
445
+ wchar_t wfullpath [MAX_LONG_PATH ];
446
+ struct phantom_symlink_info * psi ;
447
+
448
+ /* convert to absolute path to be independent of cwd */
449
+ len = GetFullPathNameW (wlink , MAX_LONG_PATH , wfullpath , NULL );
450
+ if (!len || len >= MAX_LONG_PATH ) {
451
+ errno = err_win_to_posix (GetLastError ());
452
+ return -1 ;
453
+ }
454
+
455
+ /* over-allocate and fill phantom_symlink_info structure */
456
+ psi = xmalloc (sizeof (struct phantom_symlink_info ) +
457
+ sizeof (wchar_t ) * (len + wcslen (wtarget ) + 2 ));
458
+ psi -> wlink = (wchar_t * )(psi + 1 );
459
+ wcscpy (psi -> wlink , wfullpath );
460
+ psi -> wtarget = psi -> wlink + len + 1 ;
461
+ wcscpy (psi -> wtarget , wtarget );
462
+
463
+ EnterCriticalSection (& phantom_symlinks_cs );
464
+ psi -> next = phantom_symlinks ;
465
+ phantom_symlinks = psi ;
466
+ LeaveCriticalSection (& phantom_symlinks_cs );
467
+ break ;
468
+ }
469
+ case PHANTOM_SYMLINK_DIRECTORY :
470
+ /* if we created a dir symlink, process other phantom symlinks */
471
+ process_phantom_symlinks ();
472
+ break ;
473
+ default :
474
+ break ;
475
+ }
476
+ return 0 ;
477
+ }
478
+
431
479
/* Normalizes NT paths as returned by some low-level APIs. */
432
480
static wchar_t * normalize_ntpath (wchar_t * wbuf )
433
481
{
@@ -2830,48 +2878,7 @@ int symlink(const char *target, const char *link)
2830
2878
if (wtarget [len ] == '/' )
2831
2879
wtarget [len ] = '\\' ;
2832
2880
2833
- /* create file symlink */
2834
- if (!CreateSymbolicLinkW (wlink , wtarget , symlink_file_flags )) {
2835
- errno = err_win_to_posix (GetLastError ());
2836
- return -1 ;
2837
- }
2838
-
2839
- /* convert to directory symlink if target exists */
2840
- switch (process_phantom_symlink (wtarget , wlink )) {
2841
- case PHANTOM_SYMLINK_RETRY : {
2842
- /* if target doesn't exist, add to phantom symlinks list */
2843
- wchar_t wfullpath [MAX_LONG_PATH ];
2844
- struct phantom_symlink_info * psi ;
2845
-
2846
- /* convert to absolute path to be independent of cwd */
2847
- len = GetFullPathNameW (wlink , MAX_LONG_PATH , wfullpath , NULL );
2848
- if (!len || len >= MAX_LONG_PATH ) {
2849
- errno = err_win_to_posix (GetLastError ());
2850
- return -1 ;
2851
- }
2852
-
2853
- /* over-allocate and fill phantom_symlink_info structure */
2854
- psi = xmalloc (sizeof (struct phantom_symlink_info )
2855
- + sizeof (wchar_t ) * (len + wcslen (wtarget ) + 2 ));
2856
- psi -> wlink = (wchar_t * )(psi + 1 );
2857
- wcscpy (psi -> wlink , wfullpath );
2858
- psi -> wtarget = psi -> wlink + len + 1 ;
2859
- wcscpy (psi -> wtarget , wtarget );
2860
-
2861
- EnterCriticalSection (& phantom_symlinks_cs );
2862
- psi -> next = phantom_symlinks ;
2863
- phantom_symlinks = psi ;
2864
- LeaveCriticalSection (& phantom_symlinks_cs );
2865
- break ;
2866
- }
2867
- case PHANTOM_SYMLINK_DIRECTORY :
2868
- /* if we created a dir symlink, process other phantom symlinks */
2869
- process_phantom_symlinks ();
2870
- break ;
2871
- default :
2872
- break ;
2873
- }
2874
- return 0 ;
2881
+ return create_phantom_symlink (wtarget , wlink );
2875
2882
}
2876
2883
2877
2884
#ifndef _WINNT_H
0 commit comments