1414#define SECURITY_WIN32
1515#include <sspi.h>
1616#include "win32/fscache.h"
17+ #include "../attr.h"
1718
1819#define HCAST (type , handle ) ((type)(intptr_t)handle)
1920
@@ -447,6 +448,54 @@ static void process_phantom_symlinks(void)
447448 LeaveCriticalSection (& phantom_symlinks_cs );
448449}
449450
451+ static int create_phantom_symlink (wchar_t * wtarget , wchar_t * wlink )
452+ {
453+ int len ;
454+
455+ /* create file symlink */
456+ if (!CreateSymbolicLinkW (wlink , wtarget , symlink_file_flags )) {
457+ errno = err_win_to_posix (GetLastError ());
458+ return -1 ;
459+ }
460+
461+ /* convert to directory symlink if target exists */
462+ switch (process_phantom_symlink (wtarget , wlink )) {
463+ case PHANTOM_SYMLINK_RETRY : {
464+ /* if target doesn't exist, add to phantom symlinks list */
465+ wchar_t wfullpath [MAX_LONG_PATH ];
466+ struct phantom_symlink_info * psi ;
467+
468+ /* convert to absolute path to be independent of cwd */
469+ len = GetFullPathNameW (wlink , MAX_LONG_PATH , wfullpath , NULL );
470+ if (!len || len >= MAX_LONG_PATH ) {
471+ errno = err_win_to_posix (GetLastError ());
472+ return -1 ;
473+ }
474+
475+ /* over-allocate and fill phantom_symlink_info structure */
476+ psi = xmalloc (sizeof (struct phantom_symlink_info ) +
477+ sizeof (wchar_t ) * (len + wcslen (wtarget ) + 2 ));
478+ psi -> wlink = (wchar_t * )(psi + 1 );
479+ wcscpy (psi -> wlink , wfullpath );
480+ psi -> wtarget = psi -> wlink + len + 1 ;
481+ wcscpy (psi -> wtarget , wtarget );
482+
483+ EnterCriticalSection (& phantom_symlinks_cs );
484+ psi -> next = phantom_symlinks ;
485+ phantom_symlinks = psi ;
486+ LeaveCriticalSection (& phantom_symlinks_cs );
487+ break ;
488+ }
489+ case PHANTOM_SYMLINK_DIRECTORY :
490+ /* if we created a dir symlink, process other phantom symlinks */
491+ process_phantom_symlinks ();
492+ break ;
493+ default :
494+ break ;
495+ }
496+ return 0 ;
497+ }
498+
450499/* Normalizes NT paths as returned by some low-level APIs. */
451500static wchar_t * normalize_ntpath (wchar_t * wbuf )
452501{
@@ -2861,7 +2910,38 @@ int link(const char *oldpath, const char *newpath)
28612910 return 0 ;
28622911}
28632912
2864- int symlink (const char * target , const char * link )
2913+ enum symlink_type {
2914+ SYMLINK_TYPE_UNSPECIFIED = 0 ,
2915+ SYMLINK_TYPE_FILE ,
2916+ SYMLINK_TYPE_DIRECTORY ,
2917+ };
2918+
2919+ static enum symlink_type check_symlink_attr (struct index_state * index , const char * link )
2920+ {
2921+ static struct attr_check * check ;
2922+ const char * value ;
2923+
2924+ if (!index )
2925+ return SYMLINK_TYPE_UNSPECIFIED ;
2926+
2927+ if (!check )
2928+ check = attr_check_initl ("symlink" , NULL );
2929+
2930+ git_check_attr (index , link , check );
2931+
2932+ value = check -> items [0 ].value ;
2933+ if (ATTR_UNSET (value ))
2934+ return SYMLINK_TYPE_UNSPECIFIED ;
2935+ if (!strcmp (value , "file" ))
2936+ return SYMLINK_TYPE_FILE ;
2937+ if (!strcmp (value , "dir" ) || !strcmp (value , "directory" ))
2938+ return SYMLINK_TYPE_DIRECTORY ;
2939+
2940+ warning (_ ("ignoring invalid symlink type '%s' for '%s'" ), value , link );
2941+ return SYMLINK_TYPE_UNSPECIFIED ;
2942+ }
2943+
2944+ int mingw_create_symlink (struct index_state * index , const char * target , const char * link )
28652945{
28662946 wchar_t wtarget [MAX_LONG_PATH ], wlink [MAX_LONG_PATH ];
28672947 int len ;
@@ -2881,48 +2961,31 @@ int symlink(const char *target, const char *link)
28812961 if (wtarget [len ] == '/' )
28822962 wtarget [len ] = '\\' ;
28832963
2884- /* create file symlink */
2885- if (!CreateSymbolicLinkW (wlink , wtarget , symlink_file_flags )) {
2886- errno = err_win_to_posix (GetLastError ());
2887- return -1 ;
2888- }
2889-
2890- /* convert to directory symlink if target exists */
2891- switch (process_phantom_symlink (wtarget , wlink )) {
2892- case PHANTOM_SYMLINK_RETRY : {
2893- /* if target doesn't exist, add to phantom symlinks list */
2894- wchar_t wfullpath [MAX_LONG_PATH ];
2895- struct phantom_symlink_info * psi ;
2896-
2897- /* convert to absolute path to be independent of cwd */
2898- len = GetFullPathNameW (wlink , MAX_LONG_PATH , wfullpath , NULL );
2899- if (!len || len >= MAX_LONG_PATH ) {
2900- errno = err_win_to_posix (GetLastError ());
2901- return -1 ;
2902- }
2903-
2904- /* over-allocate and fill phantom_symlink_info structure */
2905- psi = xmalloc (sizeof (struct phantom_symlink_info )
2906- + sizeof (wchar_t ) * (len + wcslen (wtarget ) + 2 ));
2907- psi -> wlink = (wchar_t * )(psi + 1 );
2908- wcscpy (psi -> wlink , wfullpath );
2909- psi -> wtarget = psi -> wlink + len + 1 ;
2910- wcscpy (psi -> wtarget , wtarget );
2911-
2912- EnterCriticalSection (& phantom_symlinks_cs );
2913- psi -> next = phantom_symlinks ;
2914- phantom_symlinks = psi ;
2915- LeaveCriticalSection (& phantom_symlinks_cs );
2916- break ;
2917- }
2918- case PHANTOM_SYMLINK_DIRECTORY :
2919- /* if we created a dir symlink, process other phantom symlinks */
2964+ switch (check_symlink_attr (index , link )) {
2965+ case SYMLINK_TYPE_UNSPECIFIED :
2966+ /* Create a phantom symlink: it is initially created as a file
2967+ * symlink, but may change to a directory symlink later if/when
2968+ * the target exists. */
2969+ return create_phantom_symlink (wtarget , wlink );
2970+ case SYMLINK_TYPE_FILE :
2971+ if (!CreateSymbolicLinkW (wlink , wtarget , symlink_file_flags ))
2972+ break ;
2973+ return 0 ;
2974+ case SYMLINK_TYPE_DIRECTORY :
2975+ if (!CreateSymbolicLinkW (wlink , wtarget ,
2976+ symlink_directory_flags ))
2977+ break ;
2978+ /* There may be dangling phantom symlinks that point at this
2979+ * one, which should now morph into directory symlinks. */
29202980 process_phantom_symlinks ();
2921- break ;
2981+ return 0 ;
29222982 default :
2923- break ;
2983+ BUG ( "unhandled symlink type" ) ;
29242984 }
2925- return 0 ;
2985+
2986+ /* CreateSymbolicLinkW failed. */
2987+ errno = err_win_to_posix (GetLastError ());
2988+ return -1 ;
29262989}
29272990
29282991#ifndef _WINNT_H
0 commit comments