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"
@@ -477,6 +478,54 @@ static void process_phantom_symlinks(void)
477478 LeaveCriticalSection (& phantom_symlinks_cs );
478479}
479480
481+ static int create_phantom_symlink (wchar_t * wtarget , wchar_t * wlink )
482+ {
483+ int len ;
484+
485+ /* create file symlink */
486+ if (!CreateSymbolicLinkW (wlink , wtarget , symlink_file_flags )) {
487+ errno = err_win_to_posix (GetLastError ());
488+ return -1 ;
489+ }
490+
491+ /* convert to directory symlink if target exists */
492+ switch (process_phantom_symlink (wtarget , wlink )) {
493+ case PHANTOM_SYMLINK_RETRY : {
494+ /* if target doesn't exist, add to phantom symlinks list */
495+ wchar_t wfullpath [MAX_LONG_PATH ];
496+ struct phantom_symlink_info * psi ;
497+
498+ /* convert to absolute path to be independent of cwd */
499+ len = GetFullPathNameW (wlink , MAX_LONG_PATH , wfullpath , NULL );
500+ if (!len || len >= MAX_LONG_PATH ) {
501+ errno = err_win_to_posix (GetLastError ());
502+ return -1 ;
503+ }
504+
505+ /* over-allocate and fill phantom_symlink_info structure */
506+ psi = xmalloc (sizeof (struct phantom_symlink_info ) +
507+ sizeof (wchar_t ) * (len + wcslen (wtarget ) + 2 ));
508+ psi -> wlink = (wchar_t * )(psi + 1 );
509+ wcscpy (psi -> wlink , wfullpath );
510+ psi -> wtarget = psi -> wlink + len + 1 ;
511+ wcscpy (psi -> wtarget , wtarget );
512+
513+ EnterCriticalSection (& phantom_symlinks_cs );
514+ psi -> next = phantom_symlinks ;
515+ phantom_symlinks = psi ;
516+ LeaveCriticalSection (& phantom_symlinks_cs );
517+ break ;
518+ }
519+ case PHANTOM_SYMLINK_DIRECTORY :
520+ /* if we created a dir symlink, process other phantom symlinks */
521+ process_phantom_symlinks ();
522+ break ;
523+ default :
524+ break ;
525+ }
526+ return 0 ;
527+ }
528+
480529/* Normalizes NT paths as returned by some low-level APIs. */
481530static wchar_t * normalize_ntpath (wchar_t * wbuf )
482531{
@@ -3246,7 +3295,38 @@ int link(const char *oldpath, const char *newpath)
32463295 return 0 ;
32473296}
32483297
3249- int symlink (const char * target , const char * link )
3298+ enum symlink_type {
3299+ SYMLINK_TYPE_UNSPECIFIED = 0 ,
3300+ SYMLINK_TYPE_FILE ,
3301+ SYMLINK_TYPE_DIRECTORY ,
3302+ };
3303+
3304+ static enum symlink_type check_symlink_attr (struct index_state * index , const char * link )
3305+ {
3306+ static struct attr_check * check ;
3307+ const char * value ;
3308+
3309+ if (!index )
3310+ return SYMLINK_TYPE_UNSPECIFIED ;
3311+
3312+ if (!check )
3313+ check = attr_check_initl ("symlink" , NULL );
3314+
3315+ git_check_attr (index , link , check );
3316+
3317+ value = check -> items [0 ].value ;
3318+ if (ATTR_UNSET (value ))
3319+ return SYMLINK_TYPE_UNSPECIFIED ;
3320+ if (!strcmp (value , "file" ))
3321+ return SYMLINK_TYPE_FILE ;
3322+ if (!strcmp (value , "dir" ) || !strcmp (value , "directory" ))
3323+ return SYMLINK_TYPE_DIRECTORY ;
3324+
3325+ warning (_ ("ignoring invalid symlink type '%s' for '%s'" ), value , link );
3326+ return SYMLINK_TYPE_UNSPECIFIED ;
3327+ }
3328+
3329+ int mingw_create_symlink (struct index_state * index , const char * target , const char * link )
32503330{
32513331 wchar_t wtarget [MAX_LONG_PATH ], wlink [MAX_LONG_PATH ];
32523332 int len ;
@@ -3266,48 +3346,31 @@ int symlink(const char *target, const char *link)
32663346 if (wtarget [len ] == '/' )
32673347 wtarget [len ] = '\\' ;
32683348
3269- /* create file symlink */
3270- if (!CreateSymbolicLinkW (wlink , wtarget , symlink_file_flags )) {
3271- errno = err_win_to_posix (GetLastError ());
3272- return -1 ;
3273- }
3274-
3275- /* convert to directory symlink if target exists */
3276- switch (process_phantom_symlink (wtarget , wlink )) {
3277- case PHANTOM_SYMLINK_RETRY : {
3278- /* if target doesn't exist, add to phantom symlinks list */
3279- wchar_t wfullpath [MAX_PATH ];
3280- struct phantom_symlink_info * psi ;
3281-
3282- /* convert to absolute path to be independent of cwd */
3283- len = GetFullPathNameW (wlink , MAX_PATH , wfullpath , NULL );
3284- if (!len || len >= MAX_PATH ) {
3285- errno = err_win_to_posix (GetLastError ());
3286- return -1 ;
3287- }
3288-
3289- /* over-allocate and fill phantom_symlink_info structure */
3290- psi = xmalloc (sizeof (struct phantom_symlink_info )
3291- + sizeof (wchar_t ) * (len + wcslen (wtarget ) + 2 ));
3292- psi -> wlink = (wchar_t * )(psi + 1 );
3293- wcscpy (psi -> wlink , wfullpath );
3294- psi -> wtarget = psi -> wlink + len + 1 ;
3295- wcscpy (psi -> wtarget , wtarget );
3296-
3297- EnterCriticalSection (& phantom_symlinks_cs );
3298- psi -> next = phantom_symlinks ;
3299- phantom_symlinks = psi ;
3300- LeaveCriticalSection (& phantom_symlinks_cs );
3301- break ;
3302- }
3303- case PHANTOM_SYMLINK_DIRECTORY :
3304- /* if we created a dir symlink, process other phantom symlinks */
3349+ switch (check_symlink_attr (index , link )) {
3350+ case SYMLINK_TYPE_UNSPECIFIED :
3351+ /* Create a phantom symlink: it is initially created as a file
3352+ * symlink, but may change to a directory symlink later if/when
3353+ * the target exists. */
3354+ return create_phantom_symlink (wtarget , wlink );
3355+ case SYMLINK_TYPE_FILE :
3356+ if (!CreateSymbolicLinkW (wlink , wtarget , symlink_file_flags ))
3357+ break ;
3358+ return 0 ;
3359+ case SYMLINK_TYPE_DIRECTORY :
3360+ if (!CreateSymbolicLinkW (wlink , wtarget ,
3361+ symlink_directory_flags ))
3362+ break ;
3363+ /* There may be dangling phantom symlinks that point at this
3364+ * one, which should now morph into directory symlinks. */
33053365 process_phantom_symlinks ();
3306- break ;
3366+ return 0 ;
33073367 default :
3308- break ;
3368+ BUG ( "unhandled symlink type" ) ;
33093369 }
3310- return 0 ;
3370+
3371+ /* CreateSymbolicLinkW failed. */
3372+ errno = err_win_to_posix (GetLastError ());
3373+ return -1 ;
33113374}
33123375
33133376int readlink (const char * path , char * buf , size_t bufsiz )
0 commit comments