@@ -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
{
@@ -3124,48 +3172,7 @@ int symlink(const char *target, const char *link)
3124
3172
if (wtarget [len ] == '/' )
3125
3173
wtarget [len ] = '\\' ;
3126
3174
3127
- /* create file symlink */
3128
- if (!CreateSymbolicLinkW (wlink , wtarget , symlink_file_flags )) {
3129
- errno = err_win_to_posix (GetLastError ());
3130
- return -1 ;
3131
- }
3132
-
3133
- /* convert to directory symlink if target exists */
3134
- switch (process_phantom_symlink (wtarget , wlink )) {
3135
- case PHANTOM_SYMLINK_RETRY : {
3136
- /* if target doesn't exist, add to phantom symlinks list */
3137
- wchar_t wfullpath [MAX_LONG_PATH ];
3138
- struct phantom_symlink_info * psi ;
3139
-
3140
- /* convert to absolute path to be independent of cwd */
3141
- len = GetFullPathNameW (wlink , MAX_LONG_PATH , wfullpath , NULL );
3142
- if (!len || len >= MAX_LONG_PATH ) {
3143
- errno = err_win_to_posix (GetLastError ());
3144
- return -1 ;
3145
- }
3146
-
3147
- /* over-allocate and fill phantom_symlink_info structure */
3148
- psi = xmalloc (sizeof (struct phantom_symlink_info )
3149
- + sizeof (wchar_t ) * (len + wcslen (wtarget ) + 2 ));
3150
- psi -> wlink = (wchar_t * )(psi + 1 );
3151
- wcscpy (psi -> wlink , wfullpath );
3152
- psi -> wtarget = psi -> wlink + len + 1 ;
3153
- wcscpy (psi -> wtarget , wtarget );
3154
-
3155
- EnterCriticalSection (& phantom_symlinks_cs );
3156
- psi -> next = phantom_symlinks ;
3157
- phantom_symlinks = psi ;
3158
- LeaveCriticalSection (& phantom_symlinks_cs );
3159
- break ;
3160
- }
3161
- case PHANTOM_SYMLINK_DIRECTORY :
3162
- /* if we created a dir symlink, process other phantom symlinks */
3163
- process_phantom_symlinks ();
3164
- break ;
3165
- default :
3166
- break ;
3167
- }
3168
- return 0 ;
3175
+ return create_phantom_symlink (wtarget , wlink );
3169
3176
}
3170
3177
3171
3178
#ifndef _WINNT_H
0 commit comments