@@ -427,6 +427,54 @@ static void process_phantom_symlinks(void)
427427 LeaveCriticalSection (& phantom_symlinks_cs );
428428}
429429
430+ static int create_phantom_symlink (wchar_t * wtarget , wchar_t * wlink )
431+ {
432+ int len ;
433+
434+ /* create file symlink */
435+ if (!CreateSymbolicLinkW (wlink , wtarget , symlink_file_flags )) {
436+ errno = err_win_to_posix (GetLastError ());
437+ return -1 ;
438+ }
439+
440+ /* convert to directory symlink if target exists */
441+ switch (process_phantom_symlink (wtarget , wlink )) {
442+ case PHANTOM_SYMLINK_RETRY : {
443+ /* if target doesn't exist, add to phantom symlinks list */
444+ wchar_t wfullpath [MAX_LONG_PATH ];
445+ struct phantom_symlink_info * psi ;
446+
447+ /* convert to absolute path to be independent of cwd */
448+ len = GetFullPathNameW (wlink , MAX_LONG_PATH , wfullpath , NULL );
449+ if (!len || len >= MAX_LONG_PATH ) {
450+ errno = err_win_to_posix (GetLastError ());
451+ return -1 ;
452+ }
453+
454+ /* over-allocate and fill phantom_symlink_info structure */
455+ psi = xmalloc (sizeof (struct phantom_symlink_info ) +
456+ sizeof (wchar_t ) * (len + wcslen (wtarget ) + 2 ));
457+ psi -> wlink = (wchar_t * )(psi + 1 );
458+ wcscpy (psi -> wlink , wfullpath );
459+ psi -> wtarget = psi -> wlink + len + 1 ;
460+ wcscpy (psi -> wtarget , wtarget );
461+
462+ EnterCriticalSection (& phantom_symlinks_cs );
463+ psi -> next = phantom_symlinks ;
464+ phantom_symlinks = psi ;
465+ LeaveCriticalSection (& phantom_symlinks_cs );
466+ break ;
467+ }
468+ case PHANTOM_SYMLINK_DIRECTORY :
469+ /* if we created a dir symlink, process other phantom symlinks */
470+ process_phantom_symlinks ();
471+ break ;
472+ default :
473+ break ;
474+ }
475+ return 0 ;
476+ }
477+
430478/* Normalizes NT paths as returned by some low-level APIs. */
431479static wchar_t * normalize_ntpath (wchar_t * wbuf )
432480{
@@ -2829,48 +2877,7 @@ int symlink(const char *target, const char *link)
28292877 if (wtarget [len ] == '/' )
28302878 wtarget [len ] = '\\' ;
28312879
2832- /* create file symlink */
2833- if (!CreateSymbolicLinkW (wlink , wtarget , symlink_file_flags )) {
2834- errno = err_win_to_posix (GetLastError ());
2835- return -1 ;
2836- }
2837-
2838- /* convert to directory symlink if target exists */
2839- switch (process_phantom_symlink (wtarget , wlink )) {
2840- case PHANTOM_SYMLINK_RETRY : {
2841- /* if target doesn't exist, add to phantom symlinks list */
2842- wchar_t wfullpath [MAX_LONG_PATH ];
2843- struct phantom_symlink_info * psi ;
2844-
2845- /* convert to absolute path to be independent of cwd */
2846- len = GetFullPathNameW (wlink , MAX_LONG_PATH , wfullpath , NULL );
2847- if (!len || len >= MAX_LONG_PATH ) {
2848- errno = err_win_to_posix (GetLastError ());
2849- return -1 ;
2850- }
2851-
2852- /* over-allocate and fill phantom_symlink_info structure */
2853- psi = xmalloc (sizeof (struct phantom_symlink_info )
2854- + sizeof (wchar_t ) * (len + wcslen (wtarget ) + 2 ));
2855- psi -> wlink = (wchar_t * )(psi + 1 );
2856- wcscpy (psi -> wlink , wfullpath );
2857- psi -> wtarget = psi -> wlink + len + 1 ;
2858- wcscpy (psi -> wtarget , wtarget );
2859-
2860- EnterCriticalSection (& phantom_symlinks_cs );
2861- psi -> next = phantom_symlinks ;
2862- phantom_symlinks = psi ;
2863- LeaveCriticalSection (& phantom_symlinks_cs );
2864- break ;
2865- }
2866- case PHANTOM_SYMLINK_DIRECTORY :
2867- /* if we created a dir symlink, process other phantom symlinks */
2868- process_phantom_symlinks ();
2869- break ;
2870- default :
2871- break ;
2872- }
2873- return 0 ;
2880+ return create_phantom_symlink (wtarget , wlink );
28742881}
28752882
28762883#ifndef _WINNT_H
0 commit comments