@@ -416,6 +416,54 @@ static void process_phantom_symlinks(void)
416
416
LeaveCriticalSection (& phantom_symlinks_cs );
417
417
}
418
418
419
+ static int create_phantom_symlink (wchar_t * wtarget , wchar_t * wlink )
420
+ {
421
+ int len ;
422
+
423
+ /* create file symlink */
424
+ if (!CreateSymbolicLinkW (wlink , wtarget , symlink_file_flags )) {
425
+ errno = err_win_to_posix (GetLastError ());
426
+ return -1 ;
427
+ }
428
+
429
+ /* convert to directory symlink if target exists */
430
+ switch (process_phantom_symlink (wtarget , wlink )) {
431
+ case PHANTOM_SYMLINK_RETRY : {
432
+ /* if target doesn't exist, add to phantom symlinks list */
433
+ wchar_t wfullpath [MAX_LONG_PATH ];
434
+ struct phantom_symlink_info * psi ;
435
+
436
+ /* convert to absolute path to be independent of cwd */
437
+ len = GetFullPathNameW (wlink , MAX_LONG_PATH , wfullpath , NULL );
438
+ if (!len || len >= MAX_LONG_PATH ) {
439
+ errno = err_win_to_posix (GetLastError ());
440
+ return -1 ;
441
+ }
442
+
443
+ /* over-allocate and fill phantom_symlink_info structure */
444
+ psi = xmalloc (sizeof (struct phantom_symlink_info ) +
445
+ sizeof (wchar_t ) * (len + wcslen (wtarget ) + 2 ));
446
+ psi -> wlink = (wchar_t * )(psi + 1 );
447
+ wcscpy (psi -> wlink , wfullpath );
448
+ psi -> wtarget = psi -> wlink + len + 1 ;
449
+ wcscpy (psi -> wtarget , wtarget );
450
+
451
+ EnterCriticalSection (& phantom_symlinks_cs );
452
+ psi -> next = phantom_symlinks ;
453
+ phantom_symlinks = psi ;
454
+ LeaveCriticalSection (& phantom_symlinks_cs );
455
+ break ;
456
+ }
457
+ case PHANTOM_SYMLINK_DIRECTORY :
458
+ /* if we created a dir symlink, process other phantom symlinks */
459
+ process_phantom_symlinks ();
460
+ break ;
461
+ default :
462
+ break ;
463
+ }
464
+ return 0 ;
465
+ }
466
+
419
467
/* Normalizes NT paths as returned by some low-level APIs. */
420
468
static wchar_t * normalize_ntpath (wchar_t * wbuf )
421
469
{
@@ -2378,48 +2426,7 @@ int symlink(const char *target, const char *link)
2378
2426
if (wtarget [len ] == '/' )
2379
2427
wtarget [len ] = '\\' ;
2380
2428
2381
- /* create file symlink */
2382
- if (!CreateSymbolicLinkW (wlink , wtarget , symlink_file_flags )) {
2383
- errno = err_win_to_posix (GetLastError ());
2384
- return -1 ;
2385
- }
2386
-
2387
- /* convert to directory symlink if target exists */
2388
- switch (process_phantom_symlink (wtarget , wlink )) {
2389
- case PHANTOM_SYMLINK_RETRY : {
2390
- /* if target doesn't exist, add to phantom symlinks list */
2391
- wchar_t wfullpath [MAX_LONG_PATH ];
2392
- struct phantom_symlink_info * psi ;
2393
-
2394
- /* convert to absolute path to be independent of cwd */
2395
- len = GetFullPathNameW (wlink , MAX_LONG_PATH , wfullpath , NULL );
2396
- if (!len || len >= MAX_LONG_PATH ) {
2397
- errno = err_win_to_posix (GetLastError ());
2398
- return -1 ;
2399
- }
2400
-
2401
- /* over-allocate and fill phantom_symlink_info structure */
2402
- psi = xmalloc (sizeof (struct phantom_symlink_info )
2403
- + sizeof (wchar_t ) * (len + wcslen (wtarget ) + 2 ));
2404
- psi -> wlink = (wchar_t * )(psi + 1 );
2405
- wcscpy (psi -> wlink , wfullpath );
2406
- psi -> wtarget = psi -> wlink + len + 1 ;
2407
- wcscpy (psi -> wtarget , wtarget );
2408
-
2409
- EnterCriticalSection (& phantom_symlinks_cs );
2410
- psi -> next = phantom_symlinks ;
2411
- phantom_symlinks = psi ;
2412
- LeaveCriticalSection (& phantom_symlinks_cs );
2413
- break ;
2414
- }
2415
- case PHANTOM_SYMLINK_DIRECTORY :
2416
- /* if we created a dir symlink, process other phantom symlinks */
2417
- process_phantom_symlinks ();
2418
- break ;
2419
- default :
2420
- break ;
2421
- }
2422
- return 0 ;
2429
+ return create_phantom_symlink (wtarget , wlink );
2423
2430
}
2424
2431
2425
2432
#ifndef _WINNT_H
0 commit comments