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