@@ -461,6 +461,54 @@ static void process_phantom_symlinks(void)
461461 LeaveCriticalSection (& phantom_symlinks_cs );
462462}
463463
464+ static int create_phantom_symlink (wchar_t * wtarget , wchar_t * wlink )
465+ {
466+ int len ;
467+
468+ /* create file symlink */
469+ if (!CreateSymbolicLinkW (wlink , wtarget , symlink_file_flags )) {
470+ errno = err_win_to_posix (GetLastError ());
471+ return -1 ;
472+ }
473+
474+ /* convert to directory symlink if target exists */
475+ switch (process_phantom_symlink (wtarget , wlink )) {
476+ case PHANTOM_SYMLINK_RETRY : {
477+ /* if target doesn't exist, add to phantom symlinks list */
478+ wchar_t wfullpath [MAX_LONG_PATH ];
479+ struct phantom_symlink_info * psi ;
480+
481+ /* convert to absolute path to be independent of cwd */
482+ len = GetFullPathNameW (wlink , MAX_LONG_PATH , wfullpath , NULL );
483+ if (!len || len >= MAX_LONG_PATH ) {
484+ errno = err_win_to_posix (GetLastError ());
485+ return -1 ;
486+ }
487+
488+ /* over-allocate and fill phantom_symlink_info structure */
489+ psi = xmalloc (sizeof (struct phantom_symlink_info ) +
490+ sizeof (wchar_t ) * (len + wcslen (wtarget ) + 2 ));
491+ psi -> wlink = (wchar_t * )(psi + 1 );
492+ wcscpy (psi -> wlink , wfullpath );
493+ psi -> wtarget = psi -> wlink + len + 1 ;
494+ wcscpy (psi -> wtarget , wtarget );
495+
496+ EnterCriticalSection (& phantom_symlinks_cs );
497+ psi -> next = phantom_symlinks ;
498+ phantom_symlinks = psi ;
499+ LeaveCriticalSection (& phantom_symlinks_cs );
500+ break ;
501+ }
502+ case PHANTOM_SYMLINK_DIRECTORY :
503+ /* if we created a dir symlink, process other phantom symlinks */
504+ process_phantom_symlinks ();
505+ break ;
506+ default :
507+ break ;
508+ }
509+ return 0 ;
510+ }
511+
464512/* Normalizes NT paths as returned by some low-level APIs. */
465513static wchar_t * normalize_ntpath (wchar_t * wbuf )
466514{
@@ -3140,48 +3188,7 @@ int symlink(const char *target, const char *link)
31403188 if (wtarget [len ] == '/' )
31413189 wtarget [len ] = '\\' ;
31423190
3143- /* create file symlink */
3144- if (!CreateSymbolicLinkW (wlink , wtarget , symlink_file_flags )) {
3145- errno = err_win_to_posix (GetLastError ());
3146- return -1 ;
3147- }
3148-
3149- /* convert to directory symlink if target exists */
3150- switch (process_phantom_symlink (wtarget , wlink )) {
3151- case PHANTOM_SYMLINK_RETRY : {
3152- /* if target doesn't exist, add to phantom symlinks list */
3153- wchar_t wfullpath [MAX_LONG_PATH ];
3154- struct phantom_symlink_info * psi ;
3155-
3156- /* convert to absolute path to be independent of cwd */
3157- len = GetFullPathNameW (wlink , MAX_LONG_PATH , wfullpath , NULL );
3158- if (!len || len >= MAX_LONG_PATH ) {
3159- errno = err_win_to_posix (GetLastError ());
3160- return -1 ;
3161- }
3162-
3163- /* over-allocate and fill phantom_symlink_info structure */
3164- psi = xmalloc (sizeof (struct phantom_symlink_info )
3165- + sizeof (wchar_t ) * (len + wcslen (wtarget ) + 2 ));
3166- psi -> wlink = (wchar_t * )(psi + 1 );
3167- wcscpy (psi -> wlink , wfullpath );
3168- psi -> wtarget = psi -> wlink + len + 1 ;
3169- wcscpy (psi -> wtarget , wtarget );
3170-
3171- EnterCriticalSection (& phantom_symlinks_cs );
3172- psi -> next = phantom_symlinks ;
3173- phantom_symlinks = psi ;
3174- LeaveCriticalSection (& phantom_symlinks_cs );
3175- break ;
3176- }
3177- case PHANTOM_SYMLINK_DIRECTORY :
3178- /* if we created a dir symlink, process other phantom symlinks */
3179- process_phantom_symlinks ();
3180- break ;
3181- default :
3182- break ;
3183- }
3184- return 0 ;
3191+ return create_phantom_symlink (wtarget , wlink );
31853192}
31863193
31873194#ifndef _WINNT_H
0 commit comments