@@ -424,6 +424,54 @@ static void process_phantom_symlinks(void)
424
424
LeaveCriticalSection (& phantom_symlinks_cs );
425
425
}
426
426
427
+ static int create_phantom_symlink (wchar_t * wtarget , wchar_t * wlink )
428
+ {
429
+ int len ;
430
+
431
+ /* create file symlink */
432
+ if (!CreateSymbolicLinkW (wlink , wtarget , symlink_file_flags )) {
433
+ errno = err_win_to_posix (GetLastError ());
434
+ return -1 ;
435
+ }
436
+
437
+ /* convert to directory symlink if target exists */
438
+ switch (process_phantom_symlink (wtarget , wlink )) {
439
+ case PHANTOM_SYMLINK_RETRY : {
440
+ /* if target doesn't exist, add to phantom symlinks list */
441
+ wchar_t wfullpath [MAX_LONG_PATH ];
442
+ struct phantom_symlink_info * psi ;
443
+
444
+ /* convert to absolute path to be independent of cwd */
445
+ len = GetFullPathNameW (wlink , MAX_LONG_PATH , wfullpath , NULL );
446
+ if (!len || len >= MAX_LONG_PATH ) {
447
+ errno = err_win_to_posix (GetLastError ());
448
+ return -1 ;
449
+ }
450
+
451
+ /* over-allocate and fill phantom_symlink_info structure */
452
+ psi = xmalloc (sizeof (struct phantom_symlink_info ) +
453
+ sizeof (wchar_t ) * (len + wcslen (wtarget ) + 2 ));
454
+ psi -> wlink = (wchar_t * )(psi + 1 );
455
+ wcscpy (psi -> wlink , wfullpath );
456
+ psi -> wtarget = psi -> wlink + len + 1 ;
457
+ wcscpy (psi -> wtarget , wtarget );
458
+
459
+ EnterCriticalSection (& phantom_symlinks_cs );
460
+ psi -> next = phantom_symlinks ;
461
+ phantom_symlinks = psi ;
462
+ LeaveCriticalSection (& phantom_symlinks_cs );
463
+ break ;
464
+ }
465
+ case PHANTOM_SYMLINK_DIRECTORY :
466
+ /* if we created a dir symlink, process other phantom symlinks */
467
+ process_phantom_symlinks ();
468
+ break ;
469
+ default :
470
+ break ;
471
+ }
472
+ return 0 ;
473
+ }
474
+
427
475
/* Normalizes NT paths as returned by some low-level APIs. */
428
476
static wchar_t * normalize_ntpath (wchar_t * wbuf )
429
477
{
@@ -2681,48 +2729,7 @@ int symlink(const char *target, const char *link)
2681
2729
if (wtarget [len ] == '/' )
2682
2730
wtarget [len ] = '\\' ;
2683
2731
2684
- /* create file symlink */
2685
- if (!CreateSymbolicLinkW (wlink , wtarget , symlink_file_flags )) {
2686
- errno = err_win_to_posix (GetLastError ());
2687
- return -1 ;
2688
- }
2689
-
2690
- /* convert to directory symlink if target exists */
2691
- switch (process_phantom_symlink (wtarget , wlink )) {
2692
- case PHANTOM_SYMLINK_RETRY : {
2693
- /* if target doesn't exist, add to phantom symlinks list */
2694
- wchar_t wfullpath [MAX_LONG_PATH ];
2695
- struct phantom_symlink_info * psi ;
2696
-
2697
- /* convert to absolute path to be independent of cwd */
2698
- len = GetFullPathNameW (wlink , MAX_LONG_PATH , wfullpath , NULL );
2699
- if (!len || len >= MAX_LONG_PATH ) {
2700
- errno = err_win_to_posix (GetLastError ());
2701
- return -1 ;
2702
- }
2703
-
2704
- /* over-allocate and fill phantom_symlink_info structure */
2705
- psi = xmalloc (sizeof (struct phantom_symlink_info )
2706
- + sizeof (wchar_t ) * (len + wcslen (wtarget ) + 2 ));
2707
- psi -> wlink = (wchar_t * )(psi + 1 );
2708
- wcscpy (psi -> wlink , wfullpath );
2709
- psi -> wtarget = psi -> wlink + len + 1 ;
2710
- wcscpy (psi -> wtarget , wtarget );
2711
-
2712
- EnterCriticalSection (& phantom_symlinks_cs );
2713
- psi -> next = phantom_symlinks ;
2714
- phantom_symlinks = psi ;
2715
- LeaveCriticalSection (& phantom_symlinks_cs );
2716
- break ;
2717
- }
2718
- case PHANTOM_SYMLINK_DIRECTORY :
2719
- /* if we created a dir symlink, process other phantom symlinks */
2720
- process_phantom_symlinks ();
2721
- break ;
2722
- default :
2723
- break ;
2724
- }
2725
- return 0 ;
2732
+ return create_phantom_symlink (wtarget , wlink );
2726
2733
}
2727
2734
2728
2735
#ifndef _WINNT_H
0 commit comments