@@ -426,6 +426,54 @@ static void process_phantom_symlinks(void)
426
426
LeaveCriticalSection (& phantom_symlinks_cs );
427
427
}
428
428
429
+ static int create_phantom_symlink (wchar_t * wtarget , wchar_t * wlink )
430
+ {
431
+ int len ;
432
+
433
+ /* create file symlink */
434
+ if (!CreateSymbolicLinkW (wlink , wtarget , symlink_file_flags )) {
435
+ errno = err_win_to_posix (GetLastError ());
436
+ return -1 ;
437
+ }
438
+
439
+ /* convert to directory symlink if target exists */
440
+ switch (process_phantom_symlink (wtarget , wlink )) {
441
+ case PHANTOM_SYMLINK_RETRY : {
442
+ /* if target doesn't exist, add to phantom symlinks list */
443
+ wchar_t wfullpath [MAX_LONG_PATH ];
444
+ struct phantom_symlink_info * psi ;
445
+
446
+ /* convert to absolute path to be independent of cwd */
447
+ len = GetFullPathNameW (wlink , MAX_LONG_PATH , wfullpath , NULL );
448
+ if (!len || len >= MAX_LONG_PATH ) {
449
+ errno = err_win_to_posix (GetLastError ());
450
+ return -1 ;
451
+ }
452
+
453
+ /* over-allocate and fill phantom_symlink_info structure */
454
+ psi = xmalloc (sizeof (struct phantom_symlink_info ) +
455
+ sizeof (wchar_t ) * (len + wcslen (wtarget ) + 2 ));
456
+ psi -> wlink = (wchar_t * )(psi + 1 );
457
+ wcscpy (psi -> wlink , wfullpath );
458
+ psi -> wtarget = psi -> wlink + len + 1 ;
459
+ wcscpy (psi -> wtarget , wtarget );
460
+
461
+ EnterCriticalSection (& phantom_symlinks_cs );
462
+ psi -> next = phantom_symlinks ;
463
+ phantom_symlinks = psi ;
464
+ LeaveCriticalSection (& phantom_symlinks_cs );
465
+ break ;
466
+ }
467
+ case PHANTOM_SYMLINK_DIRECTORY :
468
+ /* if we created a dir symlink, process other phantom symlinks */
469
+ process_phantom_symlinks ();
470
+ break ;
471
+ default :
472
+ break ;
473
+ }
474
+ return 0 ;
475
+ }
476
+
429
477
/* Normalizes NT paths as returned by some low-level APIs. */
430
478
static wchar_t * normalize_ntpath (wchar_t * wbuf )
431
479
{
@@ -2502,48 +2550,7 @@ int symlink(const char *target, const char *link)
2502
2550
if (wtarget [len ] == '/' )
2503
2551
wtarget [len ] = '\\' ;
2504
2552
2505
- /* create file symlink */
2506
- if (!CreateSymbolicLinkW (wlink , wtarget , symlink_file_flags )) {
2507
- errno = err_win_to_posix (GetLastError ());
2508
- return -1 ;
2509
- }
2510
-
2511
- /* convert to directory symlink if target exists */
2512
- switch (process_phantom_symlink (wtarget , wlink )) {
2513
- case PHANTOM_SYMLINK_RETRY : {
2514
- /* if target doesn't exist, add to phantom symlinks list */
2515
- wchar_t wfullpath [MAX_LONG_PATH ];
2516
- struct phantom_symlink_info * psi ;
2517
-
2518
- /* convert to absolute path to be independent of cwd */
2519
- len = GetFullPathNameW (wlink , MAX_LONG_PATH , wfullpath , NULL );
2520
- if (!len || len >= MAX_LONG_PATH ) {
2521
- errno = err_win_to_posix (GetLastError ());
2522
- return -1 ;
2523
- }
2524
-
2525
- /* over-allocate and fill phantom_symlink_info structure */
2526
- psi = xmalloc (sizeof (struct phantom_symlink_info )
2527
- + sizeof (wchar_t ) * (len + wcslen (wtarget ) + 2 ));
2528
- psi -> wlink = (wchar_t * )(psi + 1 );
2529
- wcscpy (psi -> wlink , wfullpath );
2530
- psi -> wtarget = psi -> wlink + len + 1 ;
2531
- wcscpy (psi -> wtarget , wtarget );
2532
-
2533
- EnterCriticalSection (& phantom_symlinks_cs );
2534
- psi -> next = phantom_symlinks ;
2535
- phantom_symlinks = psi ;
2536
- LeaveCriticalSection (& phantom_symlinks_cs );
2537
- break ;
2538
- }
2539
- case PHANTOM_SYMLINK_DIRECTORY :
2540
- /* if we created a dir symlink, process other phantom symlinks */
2541
- process_phantom_symlinks ();
2542
- break ;
2543
- default :
2544
- break ;
2545
- }
2546
- return 0 ;
2553
+ return create_phantom_symlink (wtarget , wlink );
2547
2554
}
2548
2555
2549
2556
#ifndef _WINNT_H
0 commit comments