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