@@ -335,6 +335,126 @@ static inline int is_wdir_sep(wchar_t wchar)
335335 return wchar == L'/' || wchar == L'\\' ;
336336}
337337
338+ static const wchar_t * make_relative_to (const wchar_t * path ,
339+ const wchar_t * relative_to , wchar_t * out ,
340+ size_t size )
341+ {
342+ size_t i = wcslen (relative_to ), len ;
343+
344+ /* Is `path` already absolute? */
345+ if (is_wdir_sep (path [0 ]) ||
346+ (iswalpha (path [0 ]) && path [1 ] == L':' && is_wdir_sep (path [2 ])))
347+ return path ;
348+
349+ while (i > 0 && !is_wdir_sep (relative_to [i - 1 ]))
350+ i -- ;
351+
352+ /* Is `relative_to` in the current directory? */
353+ if (!i )
354+ return path ;
355+
356+ len = wcslen (path );
357+ if (i + len + 1 > size ) {
358+ error ("Could not make '%ls' relative to '%ls' (too large)" ,
359+ path , relative_to );
360+ return NULL ;
361+ }
362+
363+ memcpy (out , relative_to , i * sizeof (wchar_t ));
364+ wcscpy (out + i , path );
365+ return out ;
366+ }
367+
368+ enum phantom_symlink_result {
369+ PHANTOM_SYMLINK_RETRY ,
370+ PHANTOM_SYMLINK_DONE ,
371+ PHANTOM_SYMLINK_DIRECTORY
372+ };
373+
374+ /*
375+ * Changes a file symlink to a directory symlink if the target exists and is a
376+ * directory.
377+ */
378+ static enum phantom_symlink_result
379+ process_phantom_symlink (const wchar_t * wtarget , const wchar_t * wlink )
380+ {
381+ HANDLE hnd ;
382+ BY_HANDLE_FILE_INFORMATION fdata ;
383+ wchar_t relative [MAX_LONG_PATH ];
384+ const wchar_t * rel ;
385+
386+ /* check that wlink is still a file symlink */
387+ if ((GetFileAttributesW (wlink )
388+ & (FILE_ATTRIBUTE_REPARSE_POINT | FILE_ATTRIBUTE_DIRECTORY ))
389+ != FILE_ATTRIBUTE_REPARSE_POINT )
390+ return PHANTOM_SYMLINK_DONE ;
391+
392+ /* make it relative, if necessary */
393+ rel = make_relative_to (wtarget , wlink , relative , ARRAY_SIZE (relative ));
394+ if (!rel )
395+ return PHANTOM_SYMLINK_DONE ;
396+
397+ /* let Windows resolve the link by opening it */
398+ hnd = CreateFileW (rel , 0 ,
399+ FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE , NULL ,
400+ OPEN_EXISTING , FILE_FLAG_BACKUP_SEMANTICS , NULL );
401+ if (hnd == INVALID_HANDLE_VALUE ) {
402+ errno = err_win_to_posix (GetLastError ());
403+ return PHANTOM_SYMLINK_RETRY ;
404+ }
405+
406+ if (!GetFileInformationByHandle (hnd , & fdata )) {
407+ errno = err_win_to_posix (GetLastError ());
408+ CloseHandle (hnd );
409+ return PHANTOM_SYMLINK_RETRY ;
410+ }
411+ CloseHandle (hnd );
412+
413+ /* if target exists and is a file, we're done */
414+ if (!(fdata .dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY ))
415+ return PHANTOM_SYMLINK_DONE ;
416+
417+ /* otherwise recreate the symlink with directory flag */
418+ if (DeleteFileW (wlink ) && CreateSymbolicLinkW (wlink , wtarget , 1 ))
419+ return PHANTOM_SYMLINK_DIRECTORY ;
420+
421+ errno = err_win_to_posix (GetLastError ());
422+ return PHANTOM_SYMLINK_RETRY ;
423+ }
424+
425+ /* keep track of newly created symlinks to non-existing targets */
426+ struct phantom_symlink_info {
427+ struct phantom_symlink_info * next ;
428+ wchar_t * wlink ;
429+ wchar_t * wtarget ;
430+ };
431+
432+ static struct phantom_symlink_info * phantom_symlinks = NULL ;
433+ static CRITICAL_SECTION phantom_symlinks_cs ;
434+
435+ static void process_phantom_symlinks (void )
436+ {
437+ struct phantom_symlink_info * current , * * psi ;
438+ EnterCriticalSection (& phantom_symlinks_cs );
439+ /* process phantom symlinks list */
440+ psi = & phantom_symlinks ;
441+ while ((current = * psi )) {
442+ enum phantom_symlink_result result = process_phantom_symlink (
443+ current -> wtarget , current -> wlink );
444+ if (result == PHANTOM_SYMLINK_RETRY ) {
445+ psi = & current -> next ;
446+ } else {
447+ /* symlink was processed, remove from list */
448+ * psi = current -> next ;
449+ free (current );
450+ /* if symlink was a directory, start over */
451+ if (result == PHANTOM_SYMLINK_DIRECTORY )
452+ psi = & phantom_symlinks ;
453+ }
454+ }
455+ LeaveCriticalSection (& phantom_symlinks_cs );
456+ }
457+
338458/* Normalizes NT paths as returned by some low-level APIs. */
339459static wchar_t * normalize_ntpath (wchar_t * wbuf )
340460{
@@ -518,6 +638,8 @@ int mingw_mkdir(const char *path, int mode)
518638 return -1 ;
519639
520640 ret = _wmkdir (wpath );
641+ if (!ret )
642+ process_phantom_symlinks ();
521643 if (!ret && needs_hiding (path ))
522644 return set_hidden_flag (wpath , 1 );
523645 return ret ;
@@ -2783,6 +2905,42 @@ int symlink(const char *target, const char *link)
27832905 errno = err_win_to_posix (GetLastError ());
27842906 return -1 ;
27852907 }
2908+
2909+ /* convert to directory symlink if target exists */
2910+ switch (process_phantom_symlink (wtarget , wlink )) {
2911+ case PHANTOM_SYMLINK_RETRY : {
2912+ /* if target doesn't exist, add to phantom symlinks list */
2913+ wchar_t wfullpath [MAX_LONG_PATH ];
2914+ struct phantom_symlink_info * psi ;
2915+
2916+ /* convert to absolute path to be independent of cwd */
2917+ len = GetFullPathNameW (wlink , MAX_LONG_PATH , wfullpath , NULL );
2918+ if (!len || len >= MAX_LONG_PATH ) {
2919+ errno = err_win_to_posix (GetLastError ());
2920+ return -1 ;
2921+ }
2922+
2923+ /* over-allocate and fill phantom_symlink_info structure */
2924+ psi = xmalloc (sizeof (struct phantom_symlink_info )
2925+ + sizeof (wchar_t ) * (len + wcslen (wtarget ) + 2 ));
2926+ psi -> wlink = (wchar_t * )(psi + 1 );
2927+ wcscpy (psi -> wlink , wfullpath );
2928+ psi -> wtarget = psi -> wlink + len + 1 ;
2929+ wcscpy (psi -> wtarget , wtarget );
2930+
2931+ EnterCriticalSection (& phantom_symlinks_cs );
2932+ psi -> next = phantom_symlinks ;
2933+ phantom_symlinks = psi ;
2934+ LeaveCriticalSection (& phantom_symlinks_cs );
2935+ break ;
2936+ }
2937+ case PHANTOM_SYMLINK_DIRECTORY :
2938+ /* if we created a dir symlink, process other phantom symlinks */
2939+ process_phantom_symlinks ();
2940+ break ;
2941+ default :
2942+ break ;
2943+ }
27862944 return 0 ;
27872945}
27882946
@@ -3743,6 +3901,7 @@ int wmain(int argc, const wchar_t **wargv)
37433901
37443902 /* initialize critical section for waitpid pinfo_t list */
37453903 InitializeCriticalSection (& pinfo_cs );
3904+ InitializeCriticalSection (& phantom_symlinks_cs );
37463905
37473906 /* initialize critical section for fscache */
37483907 InitializeCriticalSection (& fscache_cs );
0 commit comments