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
@@ -429,6 +430,54 @@ static void process_phantom_symlinks(void)
429
430
LeaveCriticalSection (& phantom_symlinks_cs );
430
431
}
431
432
433
+ static int create_phantom_symlink (wchar_t * wtarget , wchar_t * wlink )
434
+ {
435
+ int len ;
436
+
437
+ /* create file symlink */
438
+ if (!CreateSymbolicLinkW (wlink , wtarget , symlink_file_flags )) {
439
+ errno = err_win_to_posix (GetLastError ());
440
+ return -1 ;
441
+ }
442
+
443
+ /* convert to directory symlink if target exists */
444
+ switch (process_phantom_symlink (wtarget , wlink )) {
445
+ case PHANTOM_SYMLINK_RETRY : {
446
+ /* if target doesn't exist, add to phantom symlinks list */
447
+ wchar_t wfullpath [MAX_LONG_PATH ];
448
+ struct phantom_symlink_info * psi ;
449
+
450
+ /* convert to absolute path to be independent of cwd */
451
+ len = GetFullPathNameW (wlink , MAX_LONG_PATH , wfullpath , NULL );
452
+ if (!len || len >= MAX_LONG_PATH ) {
453
+ errno = err_win_to_posix (GetLastError ());
454
+ return -1 ;
455
+ }
456
+
457
+ /* over-allocate and fill phantom_symlink_info structure */
458
+ psi = xmalloc (sizeof (struct phantom_symlink_info ) +
459
+ sizeof (wchar_t ) * (len + wcslen (wtarget ) + 2 ));
460
+ psi -> wlink = (wchar_t * )(psi + 1 );
461
+ wcscpy (psi -> wlink , wfullpath );
462
+ psi -> wtarget = psi -> wlink + len + 1 ;
463
+ wcscpy (psi -> wtarget , wtarget );
464
+
465
+ EnterCriticalSection (& phantom_symlinks_cs );
466
+ psi -> next = phantom_symlinks ;
467
+ phantom_symlinks = psi ;
468
+ LeaveCriticalSection (& phantom_symlinks_cs );
469
+ break ;
470
+ }
471
+ case PHANTOM_SYMLINK_DIRECTORY :
472
+ /* if we created a dir symlink, process other phantom symlinks */
473
+ process_phantom_symlinks ();
474
+ break ;
475
+ default :
476
+ break ;
477
+ }
478
+ return 0 ;
479
+ }
480
+
432
481
/* Normalizes NT paths as returned by some low-level APIs. */
433
482
static wchar_t * normalize_ntpath (wchar_t * wbuf )
434
483
{
@@ -2846,7 +2895,38 @@ int link(const char *oldpath, const char *newpath)
2846
2895
return 0 ;
2847
2896
}
2848
2897
2849
- int symlink (const char * target , const char * link )
2898
+ enum symlink_type {
2899
+ SYMLINK_TYPE_UNSPECIFIED = 0 ,
2900
+ SYMLINK_TYPE_FILE ,
2901
+ SYMLINK_TYPE_DIRECTORY ,
2902
+ };
2903
+
2904
+ static enum symlink_type check_symlink_attr (struct index_state * index , const char * link )
2905
+ {
2906
+ static struct attr_check * check ;
2907
+ const char * value ;
2908
+
2909
+ if (!index )
2910
+ return SYMLINK_TYPE_UNSPECIFIED ;
2911
+
2912
+ if (!check )
2913
+ check = attr_check_initl ("symlink" , NULL );
2914
+
2915
+ git_check_attr (index , link , check );
2916
+
2917
+ value = check -> items [0 ].value ;
2918
+ if (ATTR_UNSET (value ))
2919
+ return SYMLINK_TYPE_UNSPECIFIED ;
2920
+ if (!strcmp (value , "file" ))
2921
+ return SYMLINK_TYPE_FILE ;
2922
+ if (!strcmp (value , "dir" ) || !strcmp (value , "directory" ))
2923
+ return SYMLINK_TYPE_DIRECTORY ;
2924
+
2925
+ warning (_ ("ignoring invalid symlink type '%s' for '%s'" ), value , link );
2926
+ return SYMLINK_TYPE_UNSPECIFIED ;
2927
+ }
2928
+
2929
+ int mingw_create_symlink (struct index_state * index , const char * target , const char * link )
2850
2930
{
2851
2931
wchar_t wtarget [MAX_LONG_PATH ], wlink [MAX_LONG_PATH ];
2852
2932
int len ;
@@ -2866,48 +2946,31 @@ int symlink(const char *target, const char *link)
2866
2946
if (wtarget [len ] == '/' )
2867
2947
wtarget [len ] = '\\' ;
2868
2948
2869
- /* create file symlink */
2870
- if (!CreateSymbolicLinkW (wlink , wtarget , symlink_file_flags )) {
2871
- errno = err_win_to_posix (GetLastError ());
2872
- return -1 ;
2873
- }
2874
-
2875
- /* convert to directory symlink if target exists */
2876
- switch (process_phantom_symlink (wtarget , wlink )) {
2877
- case PHANTOM_SYMLINK_RETRY : {
2878
- /* if target doesn't exist, add to phantom symlinks list */
2879
- wchar_t wfullpath [MAX_LONG_PATH ];
2880
- struct phantom_symlink_info * psi ;
2881
-
2882
- /* convert to absolute path to be independent of cwd */
2883
- len = GetFullPathNameW (wlink , MAX_LONG_PATH , wfullpath , NULL );
2884
- if (!len || len >= MAX_LONG_PATH ) {
2885
- errno = err_win_to_posix (GetLastError ());
2886
- return -1 ;
2887
- }
2888
-
2889
- /* over-allocate and fill phantom_symlink_info structure */
2890
- psi = xmalloc (sizeof (struct phantom_symlink_info )
2891
- + sizeof (wchar_t ) * (len + wcslen (wtarget ) + 2 ));
2892
- psi -> wlink = (wchar_t * )(psi + 1 );
2893
- wcscpy (psi -> wlink , wfullpath );
2894
- psi -> wtarget = psi -> wlink + len + 1 ;
2895
- wcscpy (psi -> wtarget , wtarget );
2896
-
2897
- EnterCriticalSection (& phantom_symlinks_cs );
2898
- psi -> next = phantom_symlinks ;
2899
- phantom_symlinks = psi ;
2900
- LeaveCriticalSection (& phantom_symlinks_cs );
2901
- break ;
2902
- }
2903
- case PHANTOM_SYMLINK_DIRECTORY :
2904
- /* if we created a dir symlink, process other phantom symlinks */
2949
+ switch (check_symlink_attr (index , link )) {
2950
+ case SYMLINK_TYPE_UNSPECIFIED :
2951
+ /* Create a phantom symlink: it is initially created as a file
2952
+ * symlink, but may change to a directory symlink later if/when
2953
+ * the target exists. */
2954
+ return create_phantom_symlink (wtarget , wlink );
2955
+ case SYMLINK_TYPE_FILE :
2956
+ if (!CreateSymbolicLinkW (wlink , wtarget , symlink_file_flags ))
2957
+ break ;
2958
+ return 0 ;
2959
+ case SYMLINK_TYPE_DIRECTORY :
2960
+ if (!CreateSymbolicLinkW (wlink , wtarget ,
2961
+ symlink_directory_flags ))
2962
+ break ;
2963
+ /* There may be dangling phantom symlinks that point at this
2964
+ * one, which should now morph into directory symlinks. */
2905
2965
process_phantom_symlinks ();
2906
- break ;
2966
+ return 0 ;
2907
2967
default :
2908
- break ;
2968
+ BUG ( "unhandled symlink type" ) ;
2909
2969
}
2910
- return 0 ;
2970
+
2971
+ /* CreateSymbolicLinkW failed. */
2972
+ errno = err_win_to_posix (GetLastError ());
2973
+ return -1 ;
2911
2974
}
2912
2975
2913
2976
#ifndef _WINNT_H
0 commit comments