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{
@@ -3151,7 +3200,38 @@ int link(const char *oldpath, const char *newpath)
31513200 return 0 ;
31523201}
31533202
3154- int symlink (const char * target , const char * link )
3203+ enum symlink_type {
3204+ SYMLINK_TYPE_UNSPECIFIED = 0 ,
3205+ SYMLINK_TYPE_FILE ,
3206+ SYMLINK_TYPE_DIRECTORY ,
3207+ };
3208+
3209+ static enum symlink_type check_symlink_attr (struct index_state * index , const char * link )
3210+ {
3211+ static struct attr_check * check ;
3212+ const char * value ;
3213+
3214+ if (!index )
3215+ return SYMLINK_TYPE_UNSPECIFIED ;
3216+
3217+ if (!check )
3218+ check = attr_check_initl ("symlink" , NULL );
3219+
3220+ git_check_attr (index , link , check );
3221+
3222+ value = check -> items [0 ].value ;
3223+ if (ATTR_UNSET (value ))
3224+ return SYMLINK_TYPE_UNSPECIFIED ;
3225+ if (!strcmp (value , "file" ))
3226+ return SYMLINK_TYPE_FILE ;
3227+ if (!strcmp (value , "dir" ) || !strcmp (value , "directory" ))
3228+ return SYMLINK_TYPE_DIRECTORY ;
3229+
3230+ warning (_ ("ignoring invalid symlink type '%s' for '%s'" ), value , link );
3231+ return SYMLINK_TYPE_UNSPECIFIED ;
3232+ }
3233+
3234+ int mingw_create_symlink (struct index_state * index , const char * target , const char * link )
31553235{
31563236 wchar_t wtarget [MAX_LONG_PATH ], wlink [MAX_LONG_PATH ];
31573237 int len ;
@@ -3171,48 +3251,31 @@ int symlink(const char *target, const char *link)
31713251 if (wtarget [len ] == '/' )
31723252 wtarget [len ] = '\\' ;
31733253
3174- /* create file symlink */
3175- if (!CreateSymbolicLinkW (wlink , wtarget , symlink_file_flags )) {
3176- errno = err_win_to_posix (GetLastError ());
3177- return -1 ;
3178- }
3179-
3180- /* convert to directory symlink if target exists */
3181- switch (process_phantom_symlink (wtarget , wlink )) {
3182- case PHANTOM_SYMLINK_RETRY : {
3183- /* if target doesn't exist, add to phantom symlinks list */
3184- wchar_t wfullpath [MAX_LONG_PATH ];
3185- struct phantom_symlink_info * psi ;
3186-
3187- /* convert to absolute path to be independent of cwd */
3188- len = GetFullPathNameW (wlink , MAX_LONG_PATH , wfullpath , NULL );
3189- if (!len || len >= MAX_LONG_PATH ) {
3190- errno = err_win_to_posix (GetLastError ());
3191- return -1 ;
3192- }
3193-
3194- /* over-allocate and fill phantom_symlink_info structure */
3195- psi = xmalloc (sizeof (struct phantom_symlink_info )
3196- + sizeof (wchar_t ) * (len + wcslen (wtarget ) + 2 ));
3197- psi -> wlink = (wchar_t * )(psi + 1 );
3198- wcscpy (psi -> wlink , wfullpath );
3199- psi -> wtarget = psi -> wlink + len + 1 ;
3200- wcscpy (psi -> wtarget , wtarget );
3201-
3202- EnterCriticalSection (& phantom_symlinks_cs );
3203- psi -> next = phantom_symlinks ;
3204- phantom_symlinks = psi ;
3205- LeaveCriticalSection (& phantom_symlinks_cs );
3206- break ;
3207- }
3208- case PHANTOM_SYMLINK_DIRECTORY :
3209- /* if we created a dir symlink, process other phantom symlinks */
3254+ switch (check_symlink_attr (index , link )) {
3255+ case SYMLINK_TYPE_UNSPECIFIED :
3256+ /* Create a phantom symlink: it is initially created as a file
3257+ * symlink, but may change to a directory symlink later if/when
3258+ * the target exists. */
3259+ return create_phantom_symlink (wtarget , wlink );
3260+ case SYMLINK_TYPE_FILE :
3261+ if (!CreateSymbolicLinkW (wlink , wtarget , symlink_file_flags ))
3262+ break ;
3263+ return 0 ;
3264+ case SYMLINK_TYPE_DIRECTORY :
3265+ if (!CreateSymbolicLinkW (wlink , wtarget ,
3266+ symlink_directory_flags ))
3267+ break ;
3268+ /* There may be dangling phantom symlinks that point at this
3269+ * one, which should now morph into directory symlinks. */
32103270 process_phantom_symlinks ();
3211- break ;
3271+ return 0 ;
32123272 default :
3213- break ;
3273+ BUG ( "unhandled symlink type" ) ;
32143274 }
3215- return 0 ;
3275+
3276+ /* CreateSymbolicLinkW failed. */
3277+ errno = err_win_to_posix (GetLastError ());
3278+ return -1 ;
32163279}
32173280
32183281#ifndef _WINNT_H
0 commit comments