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{
@@ -3219,7 +3268,38 @@ int link(const char *oldpath, const char *newpath)
32193268 return 0 ;
32203269}
32213270
3222- int symlink (const char * target , const char * link )
3271+ enum symlink_type {
3272+ SYMLINK_TYPE_UNSPECIFIED = 0 ,
3273+ SYMLINK_TYPE_FILE ,
3274+ SYMLINK_TYPE_DIRECTORY ,
3275+ };
3276+
3277+ static enum symlink_type check_symlink_attr (struct index_state * index , const char * link )
3278+ {
3279+ static struct attr_check * check ;
3280+ const char * value ;
3281+
3282+ if (!index )
3283+ return SYMLINK_TYPE_UNSPECIFIED ;
3284+
3285+ if (!check )
3286+ check = attr_check_initl ("symlink" , NULL );
3287+
3288+ git_check_attr (index , link , check );
3289+
3290+ value = check -> items [0 ].value ;
3291+ if (ATTR_UNSET (value ))
3292+ return SYMLINK_TYPE_UNSPECIFIED ;
3293+ if (!strcmp (value , "file" ))
3294+ return SYMLINK_TYPE_FILE ;
3295+ if (!strcmp (value , "dir" ) || !strcmp (value , "directory" ))
3296+ return SYMLINK_TYPE_DIRECTORY ;
3297+
3298+ warning (_ ("ignoring invalid symlink type '%s' for '%s'" ), value , link );
3299+ return SYMLINK_TYPE_UNSPECIFIED ;
3300+ }
3301+
3302+ int mingw_create_symlink (struct index_state * index , const char * target , const char * link )
32233303{
32243304 wchar_t wtarget [MAX_LONG_PATH ], wlink [MAX_LONG_PATH ];
32253305 int len ;
@@ -3239,48 +3319,31 @@ int symlink(const char *target, const char *link)
32393319 if (wtarget [len ] == '/' )
32403320 wtarget [len ] = '\\' ;
32413321
3242- /* create file symlink */
3243- if (!CreateSymbolicLinkW (wlink , wtarget , symlink_file_flags )) {
3244- errno = err_win_to_posix (GetLastError ());
3245- return -1 ;
3246- }
3247-
3248- /* convert to directory symlink if target exists */
3249- switch (process_phantom_symlink (wtarget , wlink )) {
3250- case PHANTOM_SYMLINK_RETRY : {
3251- /* if target doesn't exist, add to phantom symlinks list */
3252- wchar_t wfullpath [MAX_PATH ];
3253- struct phantom_symlink_info * psi ;
3254-
3255- /* convert to absolute path to be independent of cwd */
3256- len = GetFullPathNameW (wlink , MAX_PATH , wfullpath , NULL );
3257- if (!len || len >= MAX_PATH ) {
3258- errno = err_win_to_posix (GetLastError ());
3259- return -1 ;
3260- }
3261-
3262- /* over-allocate and fill phantom_symlink_info structure */
3263- psi = xmalloc (sizeof (struct phantom_symlink_info )
3264- + sizeof (wchar_t ) * (len + wcslen (wtarget ) + 2 ));
3265- psi -> wlink = (wchar_t * )(psi + 1 );
3266- wcscpy (psi -> wlink , wfullpath );
3267- psi -> wtarget = psi -> wlink + len + 1 ;
3268- wcscpy (psi -> wtarget , wtarget );
3269-
3270- EnterCriticalSection (& phantom_symlinks_cs );
3271- psi -> next = phantom_symlinks ;
3272- phantom_symlinks = psi ;
3273- LeaveCriticalSection (& phantom_symlinks_cs );
3274- break ;
3275- }
3276- case PHANTOM_SYMLINK_DIRECTORY :
3277- /* if we created a dir symlink, process other phantom symlinks */
3322+ switch (check_symlink_attr (index , link )) {
3323+ case SYMLINK_TYPE_UNSPECIFIED :
3324+ /* Create a phantom symlink: it is initially created as a file
3325+ * symlink, but may change to a directory symlink later if/when
3326+ * the target exists. */
3327+ return create_phantom_symlink (wtarget , wlink );
3328+ case SYMLINK_TYPE_FILE :
3329+ if (!CreateSymbolicLinkW (wlink , wtarget , symlink_file_flags ))
3330+ break ;
3331+ return 0 ;
3332+ case SYMLINK_TYPE_DIRECTORY :
3333+ if (!CreateSymbolicLinkW (wlink , wtarget ,
3334+ symlink_directory_flags ))
3335+ break ;
3336+ /* There may be dangling phantom symlinks that point at this
3337+ * one, which should now morph into directory symlinks. */
32783338 process_phantom_symlinks ();
3279- break ;
3339+ return 0 ;
32803340 default :
3281- break ;
3341+ BUG ( "unhandled symlink type" ) ;
32823342 }
3283- return 0 ;
3343+
3344+ /* CreateSymbolicLinkW failed. */
3345+ errno = err_win_to_posix (GetLastError ());
3346+ return -1 ;
32843347}
32853348
32863349int readlink (const char * path , char * buf , size_t bufsiz )
0 commit comments