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{
@@ -3289,7 +3338,38 @@ int link(const char *oldpath, const char *newpath)
32893338 return 0 ;
32903339}
32913340
3292- int symlink (const char * target , const char * link )
3341+ enum symlink_type {
3342+ SYMLINK_TYPE_UNSPECIFIED = 0 ,
3343+ SYMLINK_TYPE_FILE ,
3344+ SYMLINK_TYPE_DIRECTORY ,
3345+ };
3346+
3347+ static enum symlink_type check_symlink_attr (struct index_state * index , const char * link )
3348+ {
3349+ static struct attr_check * check ;
3350+ const char * value ;
3351+
3352+ if (!index )
3353+ return SYMLINK_TYPE_UNSPECIFIED ;
3354+
3355+ if (!check )
3356+ check = attr_check_initl ("symlink" , NULL );
3357+
3358+ git_check_attr (index , link , check );
3359+
3360+ value = check -> items [0 ].value ;
3361+ if (ATTR_UNSET (value ))
3362+ return SYMLINK_TYPE_UNSPECIFIED ;
3363+ if (!strcmp (value , "file" ))
3364+ return SYMLINK_TYPE_FILE ;
3365+ if (!strcmp (value , "dir" ) || !strcmp (value , "directory" ))
3366+ return SYMLINK_TYPE_DIRECTORY ;
3367+
3368+ warning (_ ("ignoring invalid symlink type '%s' for '%s'" ), value , link );
3369+ return SYMLINK_TYPE_UNSPECIFIED ;
3370+ }
3371+
3372+ int mingw_create_symlink (struct index_state * index , const char * target , const char * link )
32933373{
32943374 wchar_t wtarget [MAX_LONG_PATH ], wlink [MAX_LONG_PATH ];
32953375 int len ;
@@ -3309,48 +3389,31 @@ int symlink(const char *target, const char *link)
33093389 if (wtarget [len ] == '/' )
33103390 wtarget [len ] = '\\' ;
33113391
3312- /* create file symlink */
3313- if (!CreateSymbolicLinkW (wlink , wtarget , symlink_file_flags )) {
3314- errno = err_win_to_posix (GetLastError ());
3315- return -1 ;
3316- }
3317-
3318- /* convert to directory symlink if target exists */
3319- switch (process_phantom_symlink (wtarget , wlink )) {
3320- case PHANTOM_SYMLINK_RETRY : {
3321- /* if target doesn't exist, add to phantom symlinks list */
3322- wchar_t wfullpath [MAX_PATH ];
3323- struct phantom_symlink_info * psi ;
3324-
3325- /* convert to absolute path to be independent of cwd */
3326- len = GetFullPathNameW (wlink , MAX_PATH , wfullpath , NULL );
3327- if (!len || len >= MAX_PATH ) {
3328- errno = err_win_to_posix (GetLastError ());
3329- return -1 ;
3330- }
3331-
3332- /* over-allocate and fill phantom_symlink_info structure */
3333- psi = xmalloc (sizeof (struct phantom_symlink_info )
3334- + sizeof (wchar_t ) * (len + wcslen (wtarget ) + 2 ));
3335- psi -> wlink = (wchar_t * )(psi + 1 );
3336- wcscpy (psi -> wlink , wfullpath );
3337- psi -> wtarget = psi -> wlink + len + 1 ;
3338- wcscpy (psi -> wtarget , wtarget );
3339-
3340- EnterCriticalSection (& phantom_symlinks_cs );
3341- psi -> next = phantom_symlinks ;
3342- phantom_symlinks = psi ;
3343- LeaveCriticalSection (& phantom_symlinks_cs );
3344- break ;
3345- }
3346- case PHANTOM_SYMLINK_DIRECTORY :
3347- /* if we created a dir symlink, process other phantom symlinks */
3392+ switch (check_symlink_attr (index , link )) {
3393+ case SYMLINK_TYPE_UNSPECIFIED :
3394+ /* Create a phantom symlink: it is initially created as a file
3395+ * symlink, but may change to a directory symlink later if/when
3396+ * the target exists. */
3397+ return create_phantom_symlink (wtarget , wlink );
3398+ case SYMLINK_TYPE_FILE :
3399+ if (!CreateSymbolicLinkW (wlink , wtarget , symlink_file_flags ))
3400+ break ;
3401+ return 0 ;
3402+ case SYMLINK_TYPE_DIRECTORY :
3403+ if (!CreateSymbolicLinkW (wlink , wtarget ,
3404+ symlink_directory_flags ))
3405+ break ;
3406+ /* There may be dangling phantom symlinks that point at this
3407+ * one, which should now morph into directory symlinks. */
33483408 process_phantom_symlinks ();
3349- break ;
3409+ return 0 ;
33503410 default :
3351- break ;
3411+ BUG ( "unhandled symlink type" ) ;
33523412 }
3353- return 0 ;
3413+
3414+ /* CreateSymbolicLinkW failed. */
3415+ errno = err_win_to_posix (GetLastError ());
3416+ return -1 ;
33543417}
33553418
33563419int readlink (const char * path , char * buf , size_t bufsiz )
0 commit comments