1414#define SECURITY_WIN32
1515#include <sspi.h>
1616#include "win32/fscache.h"
17+ #include "../attr.h"
1718
1819#define HCAST (type , handle ) ((type)(intptr_t)handle)
1920
@@ -444,6 +445,54 @@ static void process_phantom_symlinks(void)
444445 LeaveCriticalSection (& phantom_symlinks_cs );
445446}
446447
448+ static int create_phantom_symlink (wchar_t * wtarget , wchar_t * wlink )
449+ {
450+ int len ;
451+
452+ /* create file symlink */
453+ if (!CreateSymbolicLinkW (wlink , wtarget , symlink_file_flags )) {
454+ errno = err_win_to_posix (GetLastError ());
455+ return -1 ;
456+ }
457+
458+ /* convert to directory symlink if target exists */
459+ switch (process_phantom_symlink (wtarget , wlink )) {
460+ case PHANTOM_SYMLINK_RETRY : {
461+ /* if target doesn't exist, add to phantom symlinks list */
462+ wchar_t wfullpath [MAX_LONG_PATH ];
463+ struct phantom_symlink_info * psi ;
464+
465+ /* convert to absolute path to be independent of cwd */
466+ len = GetFullPathNameW (wlink , MAX_LONG_PATH , wfullpath , NULL );
467+ if (!len || len >= MAX_LONG_PATH ) {
468+ errno = err_win_to_posix (GetLastError ());
469+ return -1 ;
470+ }
471+
472+ /* over-allocate and fill phantom_symlink_info structure */
473+ psi = xmalloc (sizeof (struct phantom_symlink_info ) +
474+ sizeof (wchar_t ) * (len + wcslen (wtarget ) + 2 ));
475+ psi -> wlink = (wchar_t * )(psi + 1 );
476+ wcscpy (psi -> wlink , wfullpath );
477+ psi -> wtarget = psi -> wlink + len + 1 ;
478+ wcscpy (psi -> wtarget , wtarget );
479+
480+ EnterCriticalSection (& phantom_symlinks_cs );
481+ psi -> next = phantom_symlinks ;
482+ phantom_symlinks = psi ;
483+ LeaveCriticalSection (& phantom_symlinks_cs );
484+ break ;
485+ }
486+ case PHANTOM_SYMLINK_DIRECTORY :
487+ /* if we created a dir symlink, process other phantom symlinks */
488+ process_phantom_symlinks ();
489+ break ;
490+ default :
491+ break ;
492+ }
493+ return 0 ;
494+ }
495+
447496/* Normalizes NT paths as returned by some low-level APIs. */
448497static wchar_t * normalize_ntpath (wchar_t * wbuf )
449498{
@@ -2858,7 +2907,38 @@ int link(const char *oldpath, const char *newpath)
28582907 return 0 ;
28592908}
28602909
2861- int symlink (const char * target , const char * link )
2910+ enum symlink_type {
2911+ SYMLINK_TYPE_UNSPECIFIED = 0 ,
2912+ SYMLINK_TYPE_FILE ,
2913+ SYMLINK_TYPE_DIRECTORY ,
2914+ };
2915+
2916+ static enum symlink_type check_symlink_attr (struct index_state * index , const char * link )
2917+ {
2918+ static struct attr_check * check ;
2919+ const char * value ;
2920+
2921+ if (!index )
2922+ return SYMLINK_TYPE_UNSPECIFIED ;
2923+
2924+ if (!check )
2925+ check = attr_check_initl ("symlink" , NULL );
2926+
2927+ git_check_attr (index , link , check );
2928+
2929+ value = check -> items [0 ].value ;
2930+ if (ATTR_UNSET (value ))
2931+ return SYMLINK_TYPE_UNSPECIFIED ;
2932+ if (!strcmp (value , "file" ))
2933+ return SYMLINK_TYPE_FILE ;
2934+ if (!strcmp (value , "dir" ) || !strcmp (value , "directory" ))
2935+ return SYMLINK_TYPE_DIRECTORY ;
2936+
2937+ warning (_ ("ignoring invalid symlink type '%s' for '%s'" ), value , link );
2938+ return SYMLINK_TYPE_UNSPECIFIED ;
2939+ }
2940+
2941+ int mingw_create_symlink (struct index_state * index , const char * target , const char * link )
28622942{
28632943 wchar_t wtarget [MAX_LONG_PATH ], wlink [MAX_LONG_PATH ];
28642944 int len ;
@@ -2878,48 +2958,31 @@ int symlink(const char *target, const char *link)
28782958 if (wtarget [len ] == '/' )
28792959 wtarget [len ] = '\\' ;
28802960
2881- /* create file symlink */
2882- if (!CreateSymbolicLinkW (wlink , wtarget , symlink_file_flags )) {
2883- errno = err_win_to_posix (GetLastError ());
2884- return -1 ;
2885- }
2886-
2887- /* convert to directory symlink if target exists */
2888- switch (process_phantom_symlink (wtarget , wlink )) {
2889- case PHANTOM_SYMLINK_RETRY : {
2890- /* if target doesn't exist, add to phantom symlinks list */
2891- wchar_t wfullpath [MAX_LONG_PATH ];
2892- struct phantom_symlink_info * psi ;
2893-
2894- /* convert to absolute path to be independent of cwd */
2895- len = GetFullPathNameW (wlink , MAX_LONG_PATH , wfullpath , NULL );
2896- if (!len || len >= MAX_LONG_PATH ) {
2897- errno = err_win_to_posix (GetLastError ());
2898- return -1 ;
2899- }
2900-
2901- /* over-allocate and fill phantom_symlink_info structure */
2902- psi = xmalloc (sizeof (struct phantom_symlink_info )
2903- + sizeof (wchar_t ) * (len + wcslen (wtarget ) + 2 ));
2904- psi -> wlink = (wchar_t * )(psi + 1 );
2905- wcscpy (psi -> wlink , wfullpath );
2906- psi -> wtarget = psi -> wlink + len + 1 ;
2907- wcscpy (psi -> wtarget , wtarget );
2908-
2909- EnterCriticalSection (& phantom_symlinks_cs );
2910- psi -> next = phantom_symlinks ;
2911- phantom_symlinks = psi ;
2912- LeaveCriticalSection (& phantom_symlinks_cs );
2913- break ;
2914- }
2915- case PHANTOM_SYMLINK_DIRECTORY :
2916- /* if we created a dir symlink, process other phantom symlinks */
2961+ switch (check_symlink_attr (index , link )) {
2962+ case SYMLINK_TYPE_UNSPECIFIED :
2963+ /* Create a phantom symlink: it is initially created as a file
2964+ * symlink, but may change to a directory symlink later if/when
2965+ * the target exists. */
2966+ return create_phantom_symlink (wtarget , wlink );
2967+ case SYMLINK_TYPE_FILE :
2968+ if (!CreateSymbolicLinkW (wlink , wtarget , symlink_file_flags ))
2969+ break ;
2970+ return 0 ;
2971+ case SYMLINK_TYPE_DIRECTORY :
2972+ if (!CreateSymbolicLinkW (wlink , wtarget ,
2973+ symlink_directory_flags ))
2974+ break ;
2975+ /* There may be dangling phantom symlinks that point at this
2976+ * one, which should now morph into directory symlinks. */
29172977 process_phantom_symlinks ();
2918- break ;
2978+ return 0 ;
29192979 default :
2920- break ;
2980+ BUG ( "unhandled symlink type" ) ;
29212981 }
2922- return 0 ;
2982+
2983+ /* CreateSymbolicLinkW failed. */
2984+ errno = err_win_to_posix (GetLastError ());
2985+ return -1 ;
29232986}
29242987
29252988#ifndef _WINNT_H
0 commit comments