44#include "git-compat-util.h"
55#include "abspath.h"
66#include "alloc.h"
7+ #include "attr.h"
78#include "config.h"
89#include "dir.h"
910#include "environment.h"
@@ -454,6 +455,54 @@ static void process_phantom_symlinks(void)
454455 LeaveCriticalSection (& phantom_symlinks_cs );
455456}
456457
458+ static int create_phantom_symlink (wchar_t * wtarget , wchar_t * wlink )
459+ {
460+ int len ;
461+
462+ /* create file symlink */
463+ if (!CreateSymbolicLinkW (wlink , wtarget , symlink_file_flags )) {
464+ errno = err_win_to_posix (GetLastError ());
465+ return -1 ;
466+ }
467+
468+ /* convert to directory symlink if target exists */
469+ switch (process_phantom_symlink (wtarget , wlink )) {
470+ case PHANTOM_SYMLINK_RETRY : {
471+ /* if target doesn't exist, add to phantom symlinks list */
472+ wchar_t wfullpath [MAX_LONG_PATH ];
473+ struct phantom_symlink_info * psi ;
474+
475+ /* convert to absolute path to be independent of cwd */
476+ len = GetFullPathNameW (wlink , MAX_LONG_PATH , wfullpath , NULL );
477+ if (!len || len >= MAX_LONG_PATH ) {
478+ errno = err_win_to_posix (GetLastError ());
479+ return -1 ;
480+ }
481+
482+ /* over-allocate and fill phantom_symlink_info structure */
483+ psi = xmalloc (sizeof (struct phantom_symlink_info ) +
484+ sizeof (wchar_t ) * (len + wcslen (wtarget ) + 2 ));
485+ psi -> wlink = (wchar_t * )(psi + 1 );
486+ wcscpy (psi -> wlink , wfullpath );
487+ psi -> wtarget = psi -> wlink + len + 1 ;
488+ wcscpy (psi -> wtarget , wtarget );
489+
490+ EnterCriticalSection (& phantom_symlinks_cs );
491+ psi -> next = phantom_symlinks ;
492+ phantom_symlinks = psi ;
493+ LeaveCriticalSection (& phantom_symlinks_cs );
494+ break ;
495+ }
496+ case PHANTOM_SYMLINK_DIRECTORY :
497+ /* if we created a dir symlink, process other phantom symlinks */
498+ process_phantom_symlinks ();
499+ break ;
500+ default :
501+ break ;
502+ }
503+ return 0 ;
504+ }
505+
457506/* Normalizes NT paths as returned by some low-level APIs. */
458507static wchar_t * normalize_ntpath (wchar_t * wbuf )
459508{
@@ -3128,7 +3177,38 @@ int link(const char *oldpath, const char *newpath)
31283177 return 0 ;
31293178}
31303179
3131- int symlink (const char * target , const char * link )
3180+ enum symlink_type {
3181+ SYMLINK_TYPE_UNSPECIFIED = 0 ,
3182+ SYMLINK_TYPE_FILE ,
3183+ SYMLINK_TYPE_DIRECTORY ,
3184+ };
3185+
3186+ static enum symlink_type check_symlink_attr (struct index_state * index , const char * link )
3187+ {
3188+ static struct attr_check * check ;
3189+ const char * value ;
3190+
3191+ if (!index )
3192+ return SYMLINK_TYPE_UNSPECIFIED ;
3193+
3194+ if (!check )
3195+ check = attr_check_initl ("symlink" , NULL );
3196+
3197+ git_check_attr (index , link , check );
3198+
3199+ value = check -> items [0 ].value ;
3200+ if (ATTR_UNSET (value ))
3201+ return SYMLINK_TYPE_UNSPECIFIED ;
3202+ if (!strcmp (value , "file" ))
3203+ return SYMLINK_TYPE_FILE ;
3204+ if (!strcmp (value , "dir" ) || !strcmp (value , "directory" ))
3205+ return SYMLINK_TYPE_DIRECTORY ;
3206+
3207+ warning (_ ("ignoring invalid symlink type '%s' for '%s'" ), value , link );
3208+ return SYMLINK_TYPE_UNSPECIFIED ;
3209+ }
3210+
3211+ int mingw_create_symlink (struct index_state * index , const char * target , const char * link )
31323212{
31333213 wchar_t wtarget [MAX_LONG_PATH ], wlink [MAX_LONG_PATH ];
31343214 int len ;
@@ -3148,48 +3228,31 @@ int symlink(const char *target, const char *link)
31483228 if (wtarget [len ] == '/' )
31493229 wtarget [len ] = '\\' ;
31503230
3151- /* create file symlink */
3152- if (!CreateSymbolicLinkW (wlink , wtarget , symlink_file_flags )) {
3153- errno = err_win_to_posix (GetLastError ());
3154- return -1 ;
3155- }
3156-
3157- /* convert to directory symlink if target exists */
3158- switch (process_phantom_symlink (wtarget , wlink )) {
3159- case PHANTOM_SYMLINK_RETRY : {
3160- /* if target doesn't exist, add to phantom symlinks list */
3161- wchar_t wfullpath [MAX_LONG_PATH ];
3162- struct phantom_symlink_info * psi ;
3163-
3164- /* convert to absolute path to be independent of cwd */
3165- len = GetFullPathNameW (wlink , MAX_LONG_PATH , wfullpath , NULL );
3166- if (!len || len >= MAX_LONG_PATH ) {
3167- errno = err_win_to_posix (GetLastError ());
3168- return -1 ;
3169- }
3170-
3171- /* over-allocate and fill phantom_symlink_info structure */
3172- psi = xmalloc (sizeof (struct phantom_symlink_info )
3173- + sizeof (wchar_t ) * (len + wcslen (wtarget ) + 2 ));
3174- psi -> wlink = (wchar_t * )(psi + 1 );
3175- wcscpy (psi -> wlink , wfullpath );
3176- psi -> wtarget = psi -> wlink + len + 1 ;
3177- wcscpy (psi -> wtarget , wtarget );
3178-
3179- EnterCriticalSection (& phantom_symlinks_cs );
3180- psi -> next = phantom_symlinks ;
3181- phantom_symlinks = psi ;
3182- LeaveCriticalSection (& phantom_symlinks_cs );
3183- break ;
3184- }
3185- case PHANTOM_SYMLINK_DIRECTORY :
3186- /* if we created a dir symlink, process other phantom symlinks */
3231+ switch (check_symlink_attr (index , link )) {
3232+ case SYMLINK_TYPE_UNSPECIFIED :
3233+ /* Create a phantom symlink: it is initially created as a file
3234+ * symlink, but may change to a directory symlink later if/when
3235+ * the target exists. */
3236+ return create_phantom_symlink (wtarget , wlink );
3237+ case SYMLINK_TYPE_FILE :
3238+ if (!CreateSymbolicLinkW (wlink , wtarget , symlink_file_flags ))
3239+ break ;
3240+ return 0 ;
3241+ case SYMLINK_TYPE_DIRECTORY :
3242+ if (!CreateSymbolicLinkW (wlink , wtarget ,
3243+ symlink_directory_flags ))
3244+ break ;
3245+ /* There may be dangling phantom symlinks that point at this
3246+ * one, which should now morph into directory symlinks. */
31873247 process_phantom_symlinks ();
3188- break ;
3248+ return 0 ;
31893249 default :
3190- break ;
3250+ BUG ( "unhandled symlink type" ) ;
31913251 }
3192- return 0 ;
3252+
3253+ /* CreateSymbolicLinkW failed. */
3254+ errno = err_win_to_posix (GetLastError ());
3255+ return -1 ;
31933256}
31943257
31953258#ifndef _WINNT_H
0 commit comments