14
14
#define SECURITY_WIN32
15
15
#include <sspi.h>
16
16
#include "win32/fscache.h"
17
+ #include "../attr.h"
17
18
18
19
#define HCAST (type , handle ) ((type)(intptr_t)handle)
19
20
@@ -447,6 +448,54 @@ static void process_phantom_symlinks(void)
447
448
LeaveCriticalSection (& phantom_symlinks_cs );
448
449
}
449
450
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
+
450
499
/* Normalizes NT paths as returned by some low-level APIs. */
451
500
static wchar_t * normalize_ntpath (wchar_t * wbuf )
452
501
{
@@ -2870,7 +2919,38 @@ int link(const char *oldpath, const char *newpath)
2870
2919
return 0 ;
2871
2920
}
2872
2921
2873
- int symlink (const char * target , const char * link )
2922
+ enum symlink_type {
2923
+ SYMLINK_TYPE_UNSPECIFIED = 0 ,
2924
+ SYMLINK_TYPE_FILE ,
2925
+ SYMLINK_TYPE_DIRECTORY ,
2926
+ };
2927
+
2928
+ static enum symlink_type check_symlink_attr (struct index_state * index , const char * link )
2929
+ {
2930
+ static struct attr_check * check ;
2931
+ const char * value ;
2932
+
2933
+ if (!index )
2934
+ return SYMLINK_TYPE_UNSPECIFIED ;
2935
+
2936
+ if (!check )
2937
+ check = attr_check_initl ("symlink" , NULL );
2938
+
2939
+ git_check_attr (index , NULL , link , check );
2940
+
2941
+ value = check -> items [0 ].value ;
2942
+ if (ATTR_UNSET (value ))
2943
+ return SYMLINK_TYPE_UNSPECIFIED ;
2944
+ if (!strcmp (value , "file" ))
2945
+ return SYMLINK_TYPE_FILE ;
2946
+ if (!strcmp (value , "dir" ) || !strcmp (value , "directory" ))
2947
+ return SYMLINK_TYPE_DIRECTORY ;
2948
+
2949
+ warning (_ ("ignoring invalid symlink type '%s' for '%s'" ), value , link );
2950
+ return SYMLINK_TYPE_UNSPECIFIED ;
2951
+ }
2952
+
2953
+ int mingw_create_symlink (struct index_state * index , const char * target , const char * link )
2874
2954
{
2875
2955
wchar_t wtarget [MAX_LONG_PATH ], wlink [MAX_LONG_PATH ];
2876
2956
int len ;
@@ -2890,48 +2970,31 @@ int symlink(const char *target, const char *link)
2890
2970
if (wtarget [len ] == '/' )
2891
2971
wtarget [len ] = '\\' ;
2892
2972
2893
- /* create file symlink */
2894
- if (!CreateSymbolicLinkW (wlink , wtarget , symlink_file_flags )) {
2895
- errno = err_win_to_posix (GetLastError ());
2896
- return -1 ;
2897
- }
2898
-
2899
- /* convert to directory symlink if target exists */
2900
- switch (process_phantom_symlink (wtarget , wlink )) {
2901
- case PHANTOM_SYMLINK_RETRY : {
2902
- /* if target doesn't exist, add to phantom symlinks list */
2903
- wchar_t wfullpath [MAX_LONG_PATH ];
2904
- struct phantom_symlink_info * psi ;
2905
-
2906
- /* convert to absolute path to be independent of cwd */
2907
- len = GetFullPathNameW (wlink , MAX_LONG_PATH , wfullpath , NULL );
2908
- if (!len || len >= MAX_LONG_PATH ) {
2909
- errno = err_win_to_posix (GetLastError ());
2910
- return -1 ;
2911
- }
2912
-
2913
- /* over-allocate and fill phantom_symlink_info structure */
2914
- psi = xmalloc (sizeof (struct phantom_symlink_info )
2915
- + sizeof (wchar_t ) * (len + wcslen (wtarget ) + 2 ));
2916
- psi -> wlink = (wchar_t * )(psi + 1 );
2917
- wcscpy (psi -> wlink , wfullpath );
2918
- psi -> wtarget = psi -> wlink + len + 1 ;
2919
- wcscpy (psi -> wtarget , wtarget );
2920
-
2921
- EnterCriticalSection (& phantom_symlinks_cs );
2922
- psi -> next = phantom_symlinks ;
2923
- phantom_symlinks = psi ;
2924
- LeaveCriticalSection (& phantom_symlinks_cs );
2925
- break ;
2926
- }
2927
- case PHANTOM_SYMLINK_DIRECTORY :
2928
- /* if we created a dir symlink, process other phantom symlinks */
2973
+ switch (check_symlink_attr (index , link )) {
2974
+ case SYMLINK_TYPE_UNSPECIFIED :
2975
+ /* Create a phantom symlink: it is initially created as a file
2976
+ * symlink, but may change to a directory symlink later if/when
2977
+ * the target exists. */
2978
+ return create_phantom_symlink (wtarget , wlink );
2979
+ case SYMLINK_TYPE_FILE :
2980
+ if (!CreateSymbolicLinkW (wlink , wtarget , symlink_file_flags ))
2981
+ break ;
2982
+ return 0 ;
2983
+ case SYMLINK_TYPE_DIRECTORY :
2984
+ if (!CreateSymbolicLinkW (wlink , wtarget ,
2985
+ symlink_directory_flags ))
2986
+ break ;
2987
+ /* There may be dangling phantom symlinks that point at this
2988
+ * one, which should now morph into directory symlinks. */
2929
2989
process_phantom_symlinks ();
2930
- break ;
2990
+ return 0 ;
2931
2991
default :
2932
- break ;
2992
+ BUG ( "unhandled symlink type" ) ;
2933
2993
}
2934
- return 0 ;
2994
+
2995
+ /* CreateSymbolicLinkW failed. */
2996
+ errno = err_win_to_posix (GetLastError ());
2997
+ return -1 ;
2935
2998
}
2936
2999
2937
3000
#ifndef _WINNT_H
0 commit comments