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