1212#define SECURITY_WIN32
1313#include <sspi.h>
1414#include "win32/fscache.h"
15+ #include "../attr.h"
1516
1617#define HCAST (type , handle ) ((type)(intptr_t)handle)
1718
@@ -427,6 +428,54 @@ static void process_phantom_symlinks(void)
427428 LeaveCriticalSection (& phantom_symlinks_cs );
428429}
429430
431+ static int create_phantom_symlink (wchar_t * wtarget , wchar_t * wlink )
432+ {
433+ int len ;
434+
435+ /* create file symlink */
436+ if (!CreateSymbolicLinkW (wlink , wtarget , symlink_file_flags )) {
437+ errno = err_win_to_posix (GetLastError ());
438+ return -1 ;
439+ }
440+
441+ /* convert to directory symlink if target exists */
442+ switch (process_phantom_symlink (wtarget , wlink )) {
443+ case PHANTOM_SYMLINK_RETRY : {
444+ /* if target doesn't exist, add to phantom symlinks list */
445+ wchar_t wfullpath [MAX_LONG_PATH ];
446+ struct phantom_symlink_info * psi ;
447+
448+ /* convert to absolute path to be independent of cwd */
449+ len = GetFullPathNameW (wlink , MAX_LONG_PATH , wfullpath , NULL );
450+ if (!len || len >= MAX_LONG_PATH ) {
451+ errno = err_win_to_posix (GetLastError ());
452+ return -1 ;
453+ }
454+
455+ /* over-allocate and fill phantom_symlink_info structure */
456+ psi = xmalloc (sizeof (struct phantom_symlink_info ) +
457+ sizeof (wchar_t ) * (len + wcslen (wtarget ) + 2 ));
458+ psi -> wlink = (wchar_t * )(psi + 1 );
459+ wcscpy (psi -> wlink , wfullpath );
460+ psi -> wtarget = psi -> wlink + len + 1 ;
461+ wcscpy (psi -> wtarget , wtarget );
462+
463+ EnterCriticalSection (& phantom_symlinks_cs );
464+ psi -> next = phantom_symlinks ;
465+ phantom_symlinks = psi ;
466+ LeaveCriticalSection (& phantom_symlinks_cs );
467+ break ;
468+ }
469+ case PHANTOM_SYMLINK_DIRECTORY :
470+ /* if we created a dir symlink, process other phantom symlinks */
471+ process_phantom_symlinks ();
472+ break ;
473+ default :
474+ break ;
475+ }
476+ return 0 ;
477+ }
478+
430479/* Normalizes NT paths as returned by some low-level APIs. */
431480static wchar_t * normalize_ntpath (wchar_t * wbuf )
432481{
@@ -2816,7 +2865,38 @@ int link(const char *oldpath, const char *newpath)
28162865 return 0 ;
28172866}
28182867
2819- int symlink (const char * target , const char * link )
2868+ enum symlink_type {
2869+ SYMLINK_TYPE_UNSPECIFIED = 0 ,
2870+ SYMLINK_TYPE_FILE ,
2871+ SYMLINK_TYPE_DIRECTORY ,
2872+ };
2873+
2874+ static enum symlink_type check_symlink_attr (struct index_state * index , const char * link )
2875+ {
2876+ static struct attr_check * check ;
2877+ const char * value ;
2878+
2879+ if (!index )
2880+ return SYMLINK_TYPE_UNSPECIFIED ;
2881+
2882+ if (!check )
2883+ check = attr_check_initl ("symlink" , NULL );
2884+
2885+ git_check_attr (index , link , check );
2886+
2887+ value = check -> items [0 ].value ;
2888+ if (ATTR_UNSET (value ))
2889+ return SYMLINK_TYPE_UNSPECIFIED ;
2890+ if (!strcmp (value , "file" ))
2891+ return SYMLINK_TYPE_FILE ;
2892+ if (!strcmp (value , "dir" ) || !strcmp (value , "directory" ))
2893+ return SYMLINK_TYPE_DIRECTORY ;
2894+
2895+ warning (_ ("ignoring invalid symlink type '%s' for '%s'" ), value , link );
2896+ return SYMLINK_TYPE_UNSPECIFIED ;
2897+ }
2898+
2899+ int mingw_create_symlink (struct index_state * index , const char * target , const char * link )
28202900{
28212901 wchar_t wtarget [MAX_LONG_PATH ], wlink [MAX_LONG_PATH ];
28222902 int len ;
@@ -2836,48 +2916,31 @@ int symlink(const char *target, const char *link)
28362916 if (wtarget [len ] == '/' )
28372917 wtarget [len ] = '\\' ;
28382918
2839- /* create file symlink */
2840- if (!CreateSymbolicLinkW (wlink , wtarget , symlink_file_flags )) {
2841- errno = err_win_to_posix (GetLastError ());
2842- return -1 ;
2843- }
2844-
2845- /* convert to directory symlink if target exists */
2846- switch (process_phantom_symlink (wtarget , wlink )) {
2847- case PHANTOM_SYMLINK_RETRY : {
2848- /* if target doesn't exist, add to phantom symlinks list */
2849- wchar_t wfullpath [MAX_LONG_PATH ];
2850- struct phantom_symlink_info * psi ;
2851-
2852- /* convert to absolute path to be independent of cwd */
2853- len = GetFullPathNameW (wlink , MAX_LONG_PATH , wfullpath , NULL );
2854- if (!len || len >= MAX_LONG_PATH ) {
2855- errno = err_win_to_posix (GetLastError ());
2856- return -1 ;
2857- }
2858-
2859- /* over-allocate and fill phantom_symlink_info structure */
2860- psi = xmalloc (sizeof (struct phantom_symlink_info )
2861- + sizeof (wchar_t ) * (len + wcslen (wtarget ) + 2 ));
2862- psi -> wlink = (wchar_t * )(psi + 1 );
2863- wcscpy (psi -> wlink , wfullpath );
2864- psi -> wtarget = psi -> wlink + len + 1 ;
2865- wcscpy (psi -> wtarget , wtarget );
2866-
2867- EnterCriticalSection (& phantom_symlinks_cs );
2868- psi -> next = phantom_symlinks ;
2869- phantom_symlinks = psi ;
2870- LeaveCriticalSection (& phantom_symlinks_cs );
2871- break ;
2872- }
2873- case PHANTOM_SYMLINK_DIRECTORY :
2874- /* if we created a dir symlink, process other phantom symlinks */
2919+ switch (check_symlink_attr (index , link )) {
2920+ case SYMLINK_TYPE_UNSPECIFIED :
2921+ /* Create a phantom symlink: it is initially created as a file
2922+ * symlink, but may change to a directory symlink later if/when
2923+ * the target exists. */
2924+ return create_phantom_symlink (wtarget , wlink );
2925+ case SYMLINK_TYPE_FILE :
2926+ if (!CreateSymbolicLinkW (wlink , wtarget , symlink_file_flags ))
2927+ break ;
2928+ return 0 ;
2929+ case SYMLINK_TYPE_DIRECTORY :
2930+ if (!CreateSymbolicLinkW (wlink , wtarget ,
2931+ symlink_directory_flags ))
2932+ break ;
2933+ /* There may be dangling phantom symlinks that point at this
2934+ * one, which should now morph into directory symlinks. */
28752935 process_phantom_symlinks ();
2876- break ;
2936+ return 0 ;
28772937 default :
2878- break ;
2938+ BUG ( "unhandled symlink type" ) ;
28792939 }
2880- return 0 ;
2940+
2941+ /* CreateSymbolicLinkW failed. */
2942+ errno = err_win_to_posix (GetLastError ());
2943+ return -1 ;
28812944}
28822945
28832946#ifndef _WINNT_H
0 commit comments