2323#include "../write-or-die.h"
2424#include "../repository.h"
2525#include "win32/fscache.h"
26+ #include "../attr.h"
2627
2728#define HCAST (type , handle ) ((type)(intptr_t)handle)
2829
@@ -459,6 +460,54 @@ static void process_phantom_symlinks(void)
459460 LeaveCriticalSection (& phantom_symlinks_cs );
460461}
461462
463+ static int create_phantom_symlink (wchar_t * wtarget , wchar_t * wlink )
464+ {
465+ int len ;
466+
467+ /* create file symlink */
468+ if (!CreateSymbolicLinkW (wlink , wtarget , symlink_file_flags )) {
469+ errno = err_win_to_posix (GetLastError ());
470+ return -1 ;
471+ }
472+
473+ /* convert to directory symlink if target exists */
474+ switch (process_phantom_symlink (wtarget , wlink )) {
475+ case PHANTOM_SYMLINK_RETRY : {
476+ /* if target doesn't exist, add to phantom symlinks list */
477+ wchar_t wfullpath [MAX_LONG_PATH ];
478+ struct phantom_symlink_info * psi ;
479+
480+ /* convert to absolute path to be independent of cwd */
481+ len = GetFullPathNameW (wlink , MAX_LONG_PATH , wfullpath , NULL );
482+ if (!len || len >= MAX_LONG_PATH ) {
483+ errno = err_win_to_posix (GetLastError ());
484+ return -1 ;
485+ }
486+
487+ /* over-allocate and fill phantom_symlink_info structure */
488+ psi = xmalloc (sizeof (struct phantom_symlink_info ) +
489+ sizeof (wchar_t ) * (len + wcslen (wtarget ) + 2 ));
490+ psi -> wlink = (wchar_t * )(psi + 1 );
491+ wcscpy (psi -> wlink , wfullpath );
492+ psi -> wtarget = psi -> wlink + len + 1 ;
493+ wcscpy (psi -> wtarget , wtarget );
494+
495+ EnterCriticalSection (& phantom_symlinks_cs );
496+ psi -> next = phantom_symlinks ;
497+ phantom_symlinks = psi ;
498+ LeaveCriticalSection (& phantom_symlinks_cs );
499+ break ;
500+ }
501+ case PHANTOM_SYMLINK_DIRECTORY :
502+ /* if we created a dir symlink, process other phantom symlinks */
503+ process_phantom_symlinks ();
504+ break ;
505+ default :
506+ break ;
507+ }
508+ return 0 ;
509+ }
510+
462511/* Normalizes NT paths as returned by some low-level APIs. */
463512static wchar_t * normalize_ntpath (wchar_t * wbuf )
464513{
@@ -2916,7 +2965,38 @@ int link(const char *oldpath, const char *newpath)
29162965 return 0 ;
29172966}
29182967
2919- int symlink (const char * target , const char * link )
2968+ enum symlink_type {
2969+ SYMLINK_TYPE_UNSPECIFIED = 0 ,
2970+ SYMLINK_TYPE_FILE ,
2971+ SYMLINK_TYPE_DIRECTORY ,
2972+ };
2973+
2974+ static enum symlink_type check_symlink_attr (struct index_state * index , const char * link )
2975+ {
2976+ static struct attr_check * check ;
2977+ const char * value ;
2978+
2979+ if (!index )
2980+ return SYMLINK_TYPE_UNSPECIFIED ;
2981+
2982+ if (!check )
2983+ check = attr_check_initl ("symlink" , NULL );
2984+
2985+ git_check_attr (index , link , check );
2986+
2987+ value = check -> items [0 ].value ;
2988+ if (ATTR_UNSET (value ))
2989+ return SYMLINK_TYPE_UNSPECIFIED ;
2990+ if (!strcmp (value , "file" ))
2991+ return SYMLINK_TYPE_FILE ;
2992+ if (!strcmp (value , "dir" ) || !strcmp (value , "directory" ))
2993+ return SYMLINK_TYPE_DIRECTORY ;
2994+
2995+ warning (_ ("ignoring invalid symlink type '%s' for '%s'" ), value , link );
2996+ return SYMLINK_TYPE_UNSPECIFIED ;
2997+ }
2998+
2999+ int mingw_create_symlink (struct index_state * index , const char * target , const char * link )
29203000{
29213001 wchar_t wtarget [MAX_LONG_PATH ], wlink [MAX_LONG_PATH ];
29223002 int len ;
@@ -2936,48 +3016,31 @@ int symlink(const char *target, const char *link)
29363016 if (wtarget [len ] == '/' )
29373017 wtarget [len ] = '\\' ;
29383018
2939- /* create file symlink */
2940- if (!CreateSymbolicLinkW (wlink , wtarget , symlink_file_flags )) {
2941- errno = err_win_to_posix (GetLastError ());
2942- return -1 ;
2943- }
2944-
2945- /* convert to directory symlink if target exists */
2946- switch (process_phantom_symlink (wtarget , wlink )) {
2947- case PHANTOM_SYMLINK_RETRY : {
2948- /* if target doesn't exist, add to phantom symlinks list */
2949- wchar_t wfullpath [MAX_LONG_PATH ];
2950- struct phantom_symlink_info * psi ;
2951-
2952- /* convert to absolute path to be independent of cwd */
2953- len = GetFullPathNameW (wlink , MAX_LONG_PATH , wfullpath , NULL );
2954- if (!len || len >= MAX_LONG_PATH ) {
2955- errno = err_win_to_posix (GetLastError ());
2956- return -1 ;
2957- }
2958-
2959- /* over-allocate and fill phantom_symlink_info structure */
2960- psi = xmalloc (sizeof (struct phantom_symlink_info )
2961- + sizeof (wchar_t ) * (len + wcslen (wtarget ) + 2 ));
2962- psi -> wlink = (wchar_t * )(psi + 1 );
2963- wcscpy (psi -> wlink , wfullpath );
2964- psi -> wtarget = psi -> wlink + len + 1 ;
2965- wcscpy (psi -> wtarget , wtarget );
2966-
2967- EnterCriticalSection (& phantom_symlinks_cs );
2968- psi -> next = phantom_symlinks ;
2969- phantom_symlinks = psi ;
2970- LeaveCriticalSection (& phantom_symlinks_cs );
2971- break ;
2972- }
2973- case PHANTOM_SYMLINK_DIRECTORY :
2974- /* if we created a dir symlink, process other phantom symlinks */
3019+ switch (check_symlink_attr (index , link )) {
3020+ case SYMLINK_TYPE_UNSPECIFIED :
3021+ /* Create a phantom symlink: it is initially created as a file
3022+ * symlink, but may change to a directory symlink later if/when
3023+ * the target exists. */
3024+ return create_phantom_symlink (wtarget , wlink );
3025+ case SYMLINK_TYPE_FILE :
3026+ if (!CreateSymbolicLinkW (wlink , wtarget , symlink_file_flags ))
3027+ break ;
3028+ return 0 ;
3029+ case SYMLINK_TYPE_DIRECTORY :
3030+ if (!CreateSymbolicLinkW (wlink , wtarget ,
3031+ symlink_directory_flags ))
3032+ break ;
3033+ /* There may be dangling phantom symlinks that point at this
3034+ * one, which should now morph into directory symlinks. */
29753035 process_phantom_symlinks ();
2976- break ;
3036+ return 0 ;
29773037 default :
2978- break ;
3038+ BUG ( "unhandled symlink type" ) ;
29793039 }
2980- return 0 ;
3040+
3041+ /* CreateSymbolicLinkW failed. */
3042+ errno = err_win_to_posix (GetLastError ());
3043+ return -1 ;
29813044}
29823045
29833046#ifndef _WINNT_H
0 commit comments