@@ -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
{
@@ -2503,48 +2551,7 @@ int symlink(const char *target, const char *link)
2503
2551
if (wtarget [len ] == '/' )
2504
2552
wtarget [len ] = '\\' ;
2505
2553
2506
- /* create file symlink */
2507
- if (!CreateSymbolicLinkW (wlink , wtarget , symlink_file_flags )) {
2508
- errno = err_win_to_posix (GetLastError ());
2509
- return -1 ;
2510
- }
2511
-
2512
- /* convert to directory symlink if target exists */
2513
- switch (process_phantom_symlink (wtarget , wlink )) {
2514
- case PHANTOM_SYMLINK_RETRY : {
2515
- /* if target doesn't exist, add to phantom symlinks list */
2516
- wchar_t wfullpath [MAX_LONG_PATH ];
2517
- struct phantom_symlink_info * psi ;
2518
-
2519
- /* convert to absolute path to be independent of cwd */
2520
- len = GetFullPathNameW (wlink , MAX_LONG_PATH , wfullpath , NULL );
2521
- if (!len || len >= MAX_LONG_PATH ) {
2522
- errno = err_win_to_posix (GetLastError ());
2523
- return -1 ;
2524
- }
2525
-
2526
- /* over-allocate and fill phantom_symlink_info structure */
2527
- psi = xmalloc (sizeof (struct phantom_symlink_info )
2528
- + sizeof (wchar_t ) * (len + wcslen (wtarget ) + 2 ));
2529
- psi -> wlink = (wchar_t * )(psi + 1 );
2530
- wcscpy (psi -> wlink , wfullpath );
2531
- psi -> wtarget = psi -> wlink + len + 1 ;
2532
- wcscpy (psi -> wtarget , wtarget );
2533
-
2534
- EnterCriticalSection (& phantom_symlinks_cs );
2535
- psi -> next = phantom_symlinks ;
2536
- phantom_symlinks = psi ;
2537
- LeaveCriticalSection (& phantom_symlinks_cs );
2538
- break ;
2539
- }
2540
- case PHANTOM_SYMLINK_DIRECTORY :
2541
- /* if we created a dir symlink, process other phantom symlinks */
2542
- process_phantom_symlinks ();
2543
- break ;
2544
- default :
2545
- break ;
2546
- }
2547
- return 0 ;
2554
+ return create_phantom_symlink (wtarget , wlink );
2548
2555
}
2549
2556
2550
2557
#ifndef _WINNT_H
0 commit comments