1313#define SECURITY_WIN32
1414#include <sspi.h>
1515#include "win32/fscache.h"
16+ #include "../attr.h"
1617
1718#define HCAST (type , handle ) ((type)(intptr_t)handle)
1819
@@ -428,6 +429,54 @@ static void process_phantom_symlinks(void)
428429 LeaveCriticalSection (& phantom_symlinks_cs );
429430}
430431
432+ static int create_phantom_symlink (wchar_t * wtarget , wchar_t * wlink )
433+ {
434+ int len ;
435+
436+ /* create file symlink */
437+ if (!CreateSymbolicLinkW (wlink , wtarget , symlink_file_flags )) {
438+ errno = err_win_to_posix (GetLastError ());
439+ return -1 ;
440+ }
441+
442+ /* convert to directory symlink if target exists */
443+ switch (process_phantom_symlink (wtarget , wlink )) {
444+ case PHANTOM_SYMLINK_RETRY : {
445+ /* if target doesn't exist, add to phantom symlinks list */
446+ wchar_t wfullpath [MAX_LONG_PATH ];
447+ struct phantom_symlink_info * psi ;
448+
449+ /* convert to absolute path to be independent of cwd */
450+ len = GetFullPathNameW (wlink , MAX_LONG_PATH , wfullpath , NULL );
451+ if (!len || len >= MAX_LONG_PATH ) {
452+ errno = err_win_to_posix (GetLastError ());
453+ return -1 ;
454+ }
455+
456+ /* over-allocate and fill phantom_symlink_info structure */
457+ psi = xmalloc (sizeof (struct phantom_symlink_info ) +
458+ sizeof (wchar_t ) * (len + wcslen (wtarget ) + 2 ));
459+ psi -> wlink = (wchar_t * )(psi + 1 );
460+ wcscpy (psi -> wlink , wfullpath );
461+ psi -> wtarget = psi -> wlink + len + 1 ;
462+ wcscpy (psi -> wtarget , wtarget );
463+
464+ EnterCriticalSection (& phantom_symlinks_cs );
465+ psi -> next = phantom_symlinks ;
466+ phantom_symlinks = psi ;
467+ LeaveCriticalSection (& phantom_symlinks_cs );
468+ break ;
469+ }
470+ case PHANTOM_SYMLINK_DIRECTORY :
471+ /* if we created a dir symlink, process other phantom symlinks */
472+ process_phantom_symlinks ();
473+ break ;
474+ default :
475+ break ;
476+ }
477+ return 0 ;
478+ }
479+
431480/* Normalizes NT paths as returned by some low-level APIs. */
432481static wchar_t * normalize_ntpath (wchar_t * wbuf )
433482{
@@ -2817,7 +2866,38 @@ int link(const char *oldpath, const char *newpath)
28172866 return 0 ;
28182867}
28192868
2820- int symlink (const char * target , const char * link )
2869+ enum symlink_type {
2870+ SYMLINK_TYPE_UNSPECIFIED = 0 ,
2871+ SYMLINK_TYPE_FILE ,
2872+ SYMLINK_TYPE_DIRECTORY ,
2873+ };
2874+
2875+ static enum symlink_type check_symlink_attr (struct index_state * index , const char * link )
2876+ {
2877+ static struct attr_check * check ;
2878+ const char * value ;
2879+
2880+ if (!index )
2881+ return SYMLINK_TYPE_UNSPECIFIED ;
2882+
2883+ if (!check )
2884+ check = attr_check_initl ("symlink" , NULL );
2885+
2886+ git_check_attr (index , link , check );
2887+
2888+ value = check -> items [0 ].value ;
2889+ if (ATTR_UNSET (value ))
2890+ return SYMLINK_TYPE_UNSPECIFIED ;
2891+ if (!strcmp (value , "file" ))
2892+ return SYMLINK_TYPE_FILE ;
2893+ if (!strcmp (value , "dir" ) || !strcmp (value , "directory" ))
2894+ return SYMLINK_TYPE_DIRECTORY ;
2895+
2896+ warning (_ ("ignoring invalid symlink type '%s' for '%s'" ), value , link );
2897+ return SYMLINK_TYPE_UNSPECIFIED ;
2898+ }
2899+
2900+ int mingw_create_symlink (struct index_state * index , const char * target , const char * link )
28212901{
28222902 wchar_t wtarget [MAX_LONG_PATH ], wlink [MAX_LONG_PATH ];
28232903 int len ;
@@ -2837,48 +2917,31 @@ int symlink(const char *target, const char *link)
28372917 if (wtarget [len ] == '/' )
28382918 wtarget [len ] = '\\' ;
28392919
2840- /* create file symlink */
2841- if (!CreateSymbolicLinkW (wlink , wtarget , symlink_file_flags )) {
2842- errno = err_win_to_posix (GetLastError ());
2843- return -1 ;
2844- }
2845-
2846- /* convert to directory symlink if target exists */
2847- switch (process_phantom_symlink (wtarget , wlink )) {
2848- case PHANTOM_SYMLINK_RETRY : {
2849- /* if target doesn't exist, add to phantom symlinks list */
2850- wchar_t wfullpath [MAX_LONG_PATH ];
2851- struct phantom_symlink_info * psi ;
2852-
2853- /* convert to absolute path to be independent of cwd */
2854- len = GetFullPathNameW (wlink , MAX_LONG_PATH , wfullpath , NULL );
2855- if (!len || len >= MAX_LONG_PATH ) {
2856- errno = err_win_to_posix (GetLastError ());
2857- return -1 ;
2858- }
2859-
2860- /* over-allocate and fill phantom_symlink_info structure */
2861- psi = xmalloc (sizeof (struct phantom_symlink_info )
2862- + sizeof (wchar_t ) * (len + wcslen (wtarget ) + 2 ));
2863- psi -> wlink = (wchar_t * )(psi + 1 );
2864- wcscpy (psi -> wlink , wfullpath );
2865- psi -> wtarget = psi -> wlink + len + 1 ;
2866- wcscpy (psi -> wtarget , wtarget );
2867-
2868- EnterCriticalSection (& phantom_symlinks_cs );
2869- psi -> next = phantom_symlinks ;
2870- phantom_symlinks = psi ;
2871- LeaveCriticalSection (& phantom_symlinks_cs );
2872- break ;
2873- }
2874- case PHANTOM_SYMLINK_DIRECTORY :
2875- /* if we created a dir symlink, process other phantom symlinks */
2920+ switch (check_symlink_attr (index , link )) {
2921+ case SYMLINK_TYPE_UNSPECIFIED :
2922+ /* Create a phantom symlink: it is initially created as a file
2923+ * symlink, but may change to a directory symlink later if/when
2924+ * the target exists. */
2925+ return create_phantom_symlink (wtarget , wlink );
2926+ case SYMLINK_TYPE_FILE :
2927+ if (!CreateSymbolicLinkW (wlink , wtarget , symlink_file_flags ))
2928+ break ;
2929+ return 0 ;
2930+ case SYMLINK_TYPE_DIRECTORY :
2931+ if (!CreateSymbolicLinkW (wlink , wtarget ,
2932+ symlink_directory_flags ))
2933+ break ;
2934+ /* There may be dangling phantom symlinks that point at this
2935+ * one, which should now morph into directory symlinks. */
28762936 process_phantom_symlinks ();
2877- break ;
2937+ return 0 ;
28782938 default :
2879- break ;
2939+ BUG ( "unhandled symlink type" ) ;
28802940 }
2881- return 0 ;
2941+
2942+ /* CreateSymbolicLinkW failed. */
2943+ errno = err_win_to_posix (GetLastError ());
2944+ return -1 ;
28822945}
28832946
28842947#ifndef _WINNT_H
0 commit comments