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{
@@ -3209,7 +3258,38 @@ int link(const char *oldpath, const char *newpath)
32093258 return 0 ;
32103259}
32113260
3212- int symlink (const char * target , const char * link )
3261+ enum symlink_type {
3262+ SYMLINK_TYPE_UNSPECIFIED = 0 ,
3263+ SYMLINK_TYPE_FILE ,
3264+ SYMLINK_TYPE_DIRECTORY ,
3265+ };
3266+
3267+ static enum symlink_type check_symlink_attr (struct index_state * index , const char * link )
3268+ {
3269+ static struct attr_check * check ;
3270+ const char * value ;
3271+
3272+ if (!index )
3273+ return SYMLINK_TYPE_UNSPECIFIED ;
3274+
3275+ if (!check )
3276+ check = attr_check_initl ("symlink" , NULL );
3277+
3278+ git_check_attr (index , link , check );
3279+
3280+ value = check -> items [0 ].value ;
3281+ if (ATTR_UNSET (value ))
3282+ return SYMLINK_TYPE_UNSPECIFIED ;
3283+ if (!strcmp (value , "file" ))
3284+ return SYMLINK_TYPE_FILE ;
3285+ if (!strcmp (value , "dir" ) || !strcmp (value , "directory" ))
3286+ return SYMLINK_TYPE_DIRECTORY ;
3287+
3288+ warning (_ ("ignoring invalid symlink type '%s' for '%s'" ), value , link );
3289+ return SYMLINK_TYPE_UNSPECIFIED ;
3290+ }
3291+
3292+ int mingw_create_symlink (struct index_state * index , const char * target , const char * link )
32133293{
32143294 wchar_t wtarget [MAX_LONG_PATH ], wlink [MAX_LONG_PATH ];
32153295 int len ;
@@ -3229,48 +3309,31 @@ int symlink(const char *target, const char *link)
32293309 if (wtarget [len ] == '/' )
32303310 wtarget [len ] = '\\' ;
32313311
3232- /* create file symlink */
3233- if (!CreateSymbolicLinkW (wlink , wtarget , symlink_file_flags )) {
3234- errno = err_win_to_posix (GetLastError ());
3235- return -1 ;
3236- }
3237-
3238- /* convert to directory symlink if target exists */
3239- switch (process_phantom_symlink (wtarget , wlink )) {
3240- case PHANTOM_SYMLINK_RETRY : {
3241- /* if target doesn't exist, add to phantom symlinks list */
3242- wchar_t wfullpath [MAX_PATH ];
3243- struct phantom_symlink_info * psi ;
3244-
3245- /* convert to absolute path to be independent of cwd */
3246- len = GetFullPathNameW (wlink , MAX_PATH , wfullpath , NULL );
3247- if (!len || len >= MAX_PATH ) {
3248- errno = err_win_to_posix (GetLastError ());
3249- return -1 ;
3250- }
3251-
3252- /* over-allocate and fill phantom_symlink_info structure */
3253- psi = xmalloc (sizeof (struct phantom_symlink_info )
3254- + sizeof (wchar_t ) * (len + wcslen (wtarget ) + 2 ));
3255- psi -> wlink = (wchar_t * )(psi + 1 );
3256- wcscpy (psi -> wlink , wfullpath );
3257- psi -> wtarget = psi -> wlink + len + 1 ;
3258- wcscpy (psi -> wtarget , wtarget );
3259-
3260- EnterCriticalSection (& phantom_symlinks_cs );
3261- psi -> next = phantom_symlinks ;
3262- phantom_symlinks = psi ;
3263- LeaveCriticalSection (& phantom_symlinks_cs );
3264- break ;
3265- }
3266- case PHANTOM_SYMLINK_DIRECTORY :
3267- /* if we created a dir symlink, process other phantom symlinks */
3312+ switch (check_symlink_attr (index , link )) {
3313+ case SYMLINK_TYPE_UNSPECIFIED :
3314+ /* Create a phantom symlink: it is initially created as a file
3315+ * symlink, but may change to a directory symlink later if/when
3316+ * the target exists. */
3317+ return create_phantom_symlink (wtarget , wlink );
3318+ case SYMLINK_TYPE_FILE :
3319+ if (!CreateSymbolicLinkW (wlink , wtarget , symlink_file_flags ))
3320+ break ;
3321+ return 0 ;
3322+ case SYMLINK_TYPE_DIRECTORY :
3323+ if (!CreateSymbolicLinkW (wlink , wtarget ,
3324+ symlink_directory_flags ))
3325+ break ;
3326+ /* There may be dangling phantom symlinks that point at this
3327+ * one, which should now morph into directory symlinks. */
32683328 process_phantom_symlinks ();
3269- break ;
3329+ return 0 ;
32703330 default :
3271- break ;
3331+ BUG ( "unhandled symlink type" ) ;
32723332 }
3273- return 0 ;
3333+
3334+ /* CreateSymbolicLinkW failed. */
3335+ errno = err_win_to_posix (GetLastError ());
3336+ return -1 ;
32743337}
32753338
32763339int readlink (const char * path , char * buf , size_t bufsiz )
0 commit comments