2525#include "../write-or-die.h"
2626#include "../repository.h"
2727#include "win32/fscache.h"
28+ #include "../attr.h"
2829
2930#define HCAST (type , handle ) ((type)(intptr_t)handle)
3031
@@ -461,6 +462,54 @@ static void process_phantom_symlinks(void)
461462 LeaveCriticalSection (& phantom_symlinks_cs );
462463}
463464
465+ static int create_phantom_symlink (wchar_t * wtarget , wchar_t * wlink )
466+ {
467+ int len ;
468+
469+ /* create file symlink */
470+ if (!CreateSymbolicLinkW (wlink , wtarget , symlink_file_flags )) {
471+ errno = err_win_to_posix (GetLastError ());
472+ return -1 ;
473+ }
474+
475+ /* convert to directory symlink if target exists */
476+ switch (process_phantom_symlink (wtarget , wlink )) {
477+ case PHANTOM_SYMLINK_RETRY : {
478+ /* if target doesn't exist, add to phantom symlinks list */
479+ wchar_t wfullpath [MAX_LONG_PATH ];
480+ struct phantom_symlink_info * psi ;
481+
482+ /* convert to absolute path to be independent of cwd */
483+ len = GetFullPathNameW (wlink , MAX_LONG_PATH , wfullpath , NULL );
484+ if (!len || len >= MAX_LONG_PATH ) {
485+ errno = err_win_to_posix (GetLastError ());
486+ return -1 ;
487+ }
488+
489+ /* over-allocate and fill phantom_symlink_info structure */
490+ psi = xmalloc (sizeof (struct phantom_symlink_info ) +
491+ sizeof (wchar_t ) * (len + wcslen (wtarget ) + 2 ));
492+ psi -> wlink = (wchar_t * )(psi + 1 );
493+ wcscpy (psi -> wlink , wfullpath );
494+ psi -> wtarget = psi -> wlink + len + 1 ;
495+ wcscpy (psi -> wtarget , wtarget );
496+
497+ EnterCriticalSection (& phantom_symlinks_cs );
498+ psi -> next = phantom_symlinks ;
499+ phantom_symlinks = psi ;
500+ LeaveCriticalSection (& phantom_symlinks_cs );
501+ break ;
502+ }
503+ case PHANTOM_SYMLINK_DIRECTORY :
504+ /* if we created a dir symlink, process other phantom symlinks */
505+ process_phantom_symlinks ();
506+ break ;
507+ default :
508+ break ;
509+ }
510+ return 0 ;
511+ }
512+
464513/* Normalizes NT paths as returned by some low-level APIs. */
465514static wchar_t * normalize_ntpath (wchar_t * wbuf )
466515{
@@ -3140,7 +3189,38 @@ int link(const char *oldpath, const char *newpath)
31403189 return 0 ;
31413190}
31423191
3143- int symlink (const char * target , const char * link )
3192+ enum symlink_type {
3193+ SYMLINK_TYPE_UNSPECIFIED = 0 ,
3194+ SYMLINK_TYPE_FILE ,
3195+ SYMLINK_TYPE_DIRECTORY ,
3196+ };
3197+
3198+ static enum symlink_type check_symlink_attr (struct index_state * index , const char * link )
3199+ {
3200+ static struct attr_check * check ;
3201+ const char * value ;
3202+
3203+ if (!index )
3204+ return SYMLINK_TYPE_UNSPECIFIED ;
3205+
3206+ if (!check )
3207+ check = attr_check_initl ("symlink" , NULL );
3208+
3209+ git_check_attr (index , link , check );
3210+
3211+ value = check -> items [0 ].value ;
3212+ if (ATTR_UNSET (value ))
3213+ return SYMLINK_TYPE_UNSPECIFIED ;
3214+ if (!strcmp (value , "file" ))
3215+ return SYMLINK_TYPE_FILE ;
3216+ if (!strcmp (value , "dir" ) || !strcmp (value , "directory" ))
3217+ return SYMLINK_TYPE_DIRECTORY ;
3218+
3219+ warning (_ ("ignoring invalid symlink type '%s' for '%s'" ), value , link );
3220+ return SYMLINK_TYPE_UNSPECIFIED ;
3221+ }
3222+
3223+ int mingw_create_symlink (struct index_state * index , const char * target , const char * link )
31443224{
31453225 wchar_t wtarget [MAX_LONG_PATH ], wlink [MAX_LONG_PATH ];
31463226 int len ;
@@ -3160,48 +3240,31 @@ int symlink(const char *target, const char *link)
31603240 if (wtarget [len ] == '/' )
31613241 wtarget [len ] = '\\' ;
31623242
3163- /* create file symlink */
3164- if (!CreateSymbolicLinkW (wlink , wtarget , symlink_file_flags )) {
3165- errno = err_win_to_posix (GetLastError ());
3166- return -1 ;
3167- }
3168-
3169- /* convert to directory symlink if target exists */
3170- switch (process_phantom_symlink (wtarget , wlink )) {
3171- case PHANTOM_SYMLINK_RETRY : {
3172- /* if target doesn't exist, add to phantom symlinks list */
3173- wchar_t wfullpath [MAX_LONG_PATH ];
3174- struct phantom_symlink_info * psi ;
3175-
3176- /* convert to absolute path to be independent of cwd */
3177- len = GetFullPathNameW (wlink , MAX_LONG_PATH , wfullpath , NULL );
3178- if (!len || len >= MAX_LONG_PATH ) {
3179- errno = err_win_to_posix (GetLastError ());
3180- return -1 ;
3181- }
3182-
3183- /* over-allocate and fill phantom_symlink_info structure */
3184- psi = xmalloc (sizeof (struct phantom_symlink_info )
3185- + sizeof (wchar_t ) * (len + wcslen (wtarget ) + 2 ));
3186- psi -> wlink = (wchar_t * )(psi + 1 );
3187- wcscpy (psi -> wlink , wfullpath );
3188- psi -> wtarget = psi -> wlink + len + 1 ;
3189- wcscpy (psi -> wtarget , wtarget );
3190-
3191- EnterCriticalSection (& phantom_symlinks_cs );
3192- psi -> next = phantom_symlinks ;
3193- phantom_symlinks = psi ;
3194- LeaveCriticalSection (& phantom_symlinks_cs );
3195- break ;
3196- }
3197- case PHANTOM_SYMLINK_DIRECTORY :
3198- /* if we created a dir symlink, process other phantom symlinks */
3243+ switch (check_symlink_attr (index , link )) {
3244+ case SYMLINK_TYPE_UNSPECIFIED :
3245+ /* Create a phantom symlink: it is initially created as a file
3246+ * symlink, but may change to a directory symlink later if/when
3247+ * the target exists. */
3248+ return create_phantom_symlink (wtarget , wlink );
3249+ case SYMLINK_TYPE_FILE :
3250+ if (!CreateSymbolicLinkW (wlink , wtarget , symlink_file_flags ))
3251+ break ;
3252+ return 0 ;
3253+ case SYMLINK_TYPE_DIRECTORY :
3254+ if (!CreateSymbolicLinkW (wlink , wtarget ,
3255+ symlink_directory_flags ))
3256+ break ;
3257+ /* There may be dangling phantom symlinks that point at this
3258+ * one, which should now morph into directory symlinks. */
31993259 process_phantom_symlinks ();
3200- break ;
3260+ return 0 ;
32013261 default :
3202- break ;
3262+ BUG ( "unhandled symlink type" ) ;
32033263 }
3204- return 0 ;
3264+
3265+ /* CreateSymbolicLinkW failed. */
3266+ errno = err_win_to_posix (GetLastError ());
3267+ return -1 ;
32053268}
32063269
32073270#ifndef _WINNT_H
0 commit comments