@@ -454,6 +454,54 @@ static void process_phantom_symlinks(void)
454
454
LeaveCriticalSection (& phantom_symlinks_cs );
455
455
}
456
456
457
+ static int create_phantom_symlink (wchar_t * wtarget , wchar_t * wlink )
458
+ {
459
+ int len ;
460
+
461
+ /* create file symlink */
462
+ if (!CreateSymbolicLinkW (wlink , wtarget , symlink_file_flags )) {
463
+ errno = err_win_to_posix (GetLastError ());
464
+ return -1 ;
465
+ }
466
+
467
+ /* convert to directory symlink if target exists */
468
+ switch (process_phantom_symlink (wtarget , wlink )) {
469
+ case PHANTOM_SYMLINK_RETRY : {
470
+ /* if target doesn't exist, add to phantom symlinks list */
471
+ wchar_t wfullpath [MAX_LONG_PATH ];
472
+ struct phantom_symlink_info * psi ;
473
+
474
+ /* convert to absolute path to be independent of cwd */
475
+ len = GetFullPathNameW (wlink , MAX_LONG_PATH , wfullpath , NULL );
476
+ if (!len || len >= MAX_LONG_PATH ) {
477
+ errno = err_win_to_posix (GetLastError ());
478
+ return -1 ;
479
+ }
480
+
481
+ /* over-allocate and fill phantom_symlink_info structure */
482
+ psi = xmalloc (sizeof (struct phantom_symlink_info ) +
483
+ sizeof (wchar_t ) * (len + wcslen (wtarget ) + 2 ));
484
+ psi -> wlink = (wchar_t * )(psi + 1 );
485
+ wcscpy (psi -> wlink , wfullpath );
486
+ psi -> wtarget = psi -> wlink + len + 1 ;
487
+ wcscpy (psi -> wtarget , wtarget );
488
+
489
+ EnterCriticalSection (& phantom_symlinks_cs );
490
+ psi -> next = phantom_symlinks ;
491
+ phantom_symlinks = psi ;
492
+ LeaveCriticalSection (& phantom_symlinks_cs );
493
+ break ;
494
+ }
495
+ case PHANTOM_SYMLINK_DIRECTORY :
496
+ /* if we created a dir symlink, process other phantom symlinks */
497
+ process_phantom_symlinks ();
498
+ break ;
499
+ default :
500
+ break ;
501
+ }
502
+ return 0 ;
503
+ }
504
+
457
505
/* Normalizes NT paths as returned by some low-level APIs. */
458
506
static wchar_t * normalize_ntpath (wchar_t * wbuf )
459
507
{
@@ -3116,48 +3164,7 @@ int symlink(const char *target, const char *link)
3116
3164
if (wtarget [len ] == '/' )
3117
3165
wtarget [len ] = '\\' ;
3118
3166
3119
- /* create file symlink */
3120
- if (!CreateSymbolicLinkW (wlink , wtarget , symlink_file_flags )) {
3121
- errno = err_win_to_posix (GetLastError ());
3122
- return -1 ;
3123
- }
3124
-
3125
- /* convert to directory symlink if target exists */
3126
- switch (process_phantom_symlink (wtarget , wlink )) {
3127
- case PHANTOM_SYMLINK_RETRY : {
3128
- /* if target doesn't exist, add to phantom symlinks list */
3129
- wchar_t wfullpath [MAX_LONG_PATH ];
3130
- struct phantom_symlink_info * psi ;
3131
-
3132
- /* convert to absolute path to be independent of cwd */
3133
- len = GetFullPathNameW (wlink , MAX_LONG_PATH , wfullpath , NULL );
3134
- if (!len || len >= MAX_LONG_PATH ) {
3135
- errno = err_win_to_posix (GetLastError ());
3136
- return -1 ;
3137
- }
3138
-
3139
- /* over-allocate and fill phantom_symlink_info structure */
3140
- psi = xmalloc (sizeof (struct phantom_symlink_info )
3141
- + sizeof (wchar_t ) * (len + wcslen (wtarget ) + 2 ));
3142
- psi -> wlink = (wchar_t * )(psi + 1 );
3143
- wcscpy (psi -> wlink , wfullpath );
3144
- psi -> wtarget = psi -> wlink + len + 1 ;
3145
- wcscpy (psi -> wtarget , wtarget );
3146
-
3147
- EnterCriticalSection (& phantom_symlinks_cs );
3148
- psi -> next = phantom_symlinks ;
3149
- phantom_symlinks = psi ;
3150
- LeaveCriticalSection (& phantom_symlinks_cs );
3151
- break ;
3152
- }
3153
- case PHANTOM_SYMLINK_DIRECTORY :
3154
- /* if we created a dir symlink, process other phantom symlinks */
3155
- process_phantom_symlinks ();
3156
- break ;
3157
- default :
3158
- break ;
3159
- }
3160
- return 0 ;
3167
+ return create_phantom_symlink (wtarget , wlink );
3161
3168
}
3162
3169
3163
3170
#ifndef _WINNT_H
0 commit comments