@@ -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
{
@@ -2815,48 +2863,7 @@ int symlink(const char *target, const char *link)
2815
2863
if (wtarget [len ] == '/' )
2816
2864
wtarget [len ] = '\\' ;
2817
2865
2818
- /* create file symlink */
2819
- if (!CreateSymbolicLinkW (wlink , wtarget , symlink_file_flags )) {
2820
- errno = err_win_to_posix (GetLastError ());
2821
- return -1 ;
2822
- }
2823
-
2824
- /* convert to directory symlink if target exists */
2825
- switch (process_phantom_symlink (wtarget , wlink )) {
2826
- case PHANTOM_SYMLINK_RETRY : {
2827
- /* if target doesn't exist, add to phantom symlinks list */
2828
- wchar_t wfullpath [MAX_LONG_PATH ];
2829
- struct phantom_symlink_info * psi ;
2830
-
2831
- /* convert to absolute path to be independent of cwd */
2832
- len = GetFullPathNameW (wlink , MAX_LONG_PATH , wfullpath , NULL );
2833
- if (!len || len >= MAX_LONG_PATH ) {
2834
- errno = err_win_to_posix (GetLastError ());
2835
- return -1 ;
2836
- }
2837
-
2838
- /* over-allocate and fill phantom_symlink_info structure */
2839
- psi = xmalloc (sizeof (struct phantom_symlink_info )
2840
- + sizeof (wchar_t ) * (len + wcslen (wtarget ) + 2 ));
2841
- psi -> wlink = (wchar_t * )(psi + 1 );
2842
- wcscpy (psi -> wlink , wfullpath );
2843
- psi -> wtarget = psi -> wlink + len + 1 ;
2844
- wcscpy (psi -> wtarget , wtarget );
2845
-
2846
- EnterCriticalSection (& phantom_symlinks_cs );
2847
- psi -> next = phantom_symlinks ;
2848
- phantom_symlinks = psi ;
2849
- LeaveCriticalSection (& phantom_symlinks_cs );
2850
- break ;
2851
- }
2852
- case PHANTOM_SYMLINK_DIRECTORY :
2853
- /* if we created a dir symlink, process other phantom symlinks */
2854
- process_phantom_symlinks ();
2855
- break ;
2856
- default :
2857
- break ;
2858
- }
2859
- return 0 ;
2866
+ return create_phantom_symlink (wtarget , wlink );
2860
2867
}
2861
2868
2862
2869
#ifndef _WINNT_H
0 commit comments