23
23
#include "../write-or-die.h"
24
24
#include "../repository.h"
25
25
#include "win32/fscache.h"
26
+ #include "../attr.h"
26
27
27
28
#define HCAST (type , handle ) ((type)(intptr_t)handle)
28
29
@@ -456,6 +457,54 @@ static void process_phantom_symlinks(void)
456
457
LeaveCriticalSection (& phantom_symlinks_cs );
457
458
}
458
459
460
+ static int create_phantom_symlink (wchar_t * wtarget , wchar_t * wlink )
461
+ {
462
+ int len ;
463
+
464
+ /* create file symlink */
465
+ if (!CreateSymbolicLinkW (wlink , wtarget , symlink_file_flags )) {
466
+ errno = err_win_to_posix (GetLastError ());
467
+ return -1 ;
468
+ }
469
+
470
+ /* convert to directory symlink if target exists */
471
+ switch (process_phantom_symlink (wtarget , wlink )) {
472
+ case PHANTOM_SYMLINK_RETRY : {
473
+ /* if target doesn't exist, add to phantom symlinks list */
474
+ wchar_t wfullpath [MAX_LONG_PATH ];
475
+ struct phantom_symlink_info * psi ;
476
+
477
+ /* convert to absolute path to be independent of cwd */
478
+ len = GetFullPathNameW (wlink , MAX_LONG_PATH , wfullpath , NULL );
479
+ if (!len || len >= MAX_LONG_PATH ) {
480
+ errno = err_win_to_posix (GetLastError ());
481
+ return -1 ;
482
+ }
483
+
484
+ /* over-allocate and fill phantom_symlink_info structure */
485
+ psi = xmalloc (sizeof (struct phantom_symlink_info ) +
486
+ sizeof (wchar_t ) * (len + wcslen (wtarget ) + 2 ));
487
+ psi -> wlink = (wchar_t * )(psi + 1 );
488
+ wcscpy (psi -> wlink , wfullpath );
489
+ psi -> wtarget = psi -> wlink + len + 1 ;
490
+ wcscpy (psi -> wtarget , wtarget );
491
+
492
+ EnterCriticalSection (& phantom_symlinks_cs );
493
+ psi -> next = phantom_symlinks ;
494
+ phantom_symlinks = psi ;
495
+ LeaveCriticalSection (& phantom_symlinks_cs );
496
+ break ;
497
+ }
498
+ case PHANTOM_SYMLINK_DIRECTORY :
499
+ /* if we created a dir symlink, process other phantom symlinks */
500
+ process_phantom_symlinks ();
501
+ break ;
502
+ default :
503
+ break ;
504
+ }
505
+ return 0 ;
506
+ }
507
+
459
508
/* Normalizes NT paths as returned by some low-level APIs. */
460
509
static wchar_t * normalize_ntpath (wchar_t * wbuf )
461
510
{
@@ -2879,7 +2928,38 @@ int link(const char *oldpath, const char *newpath)
2879
2928
return 0 ;
2880
2929
}
2881
2930
2882
- int symlink (const char * target , const char * link )
2931
+ enum symlink_type {
2932
+ SYMLINK_TYPE_UNSPECIFIED = 0 ,
2933
+ SYMLINK_TYPE_FILE ,
2934
+ SYMLINK_TYPE_DIRECTORY ,
2935
+ };
2936
+
2937
+ static enum symlink_type check_symlink_attr (struct index_state * index , const char * link )
2938
+ {
2939
+ static struct attr_check * check ;
2940
+ const char * value ;
2941
+
2942
+ if (!index )
2943
+ return SYMLINK_TYPE_UNSPECIFIED ;
2944
+
2945
+ if (!check )
2946
+ check = attr_check_initl ("symlink" , NULL );
2947
+
2948
+ git_check_attr (index , link , check );
2949
+
2950
+ value = check -> items [0 ].value ;
2951
+ if (ATTR_UNSET (value ))
2952
+ return SYMLINK_TYPE_UNSPECIFIED ;
2953
+ if (!strcmp (value , "file" ))
2954
+ return SYMLINK_TYPE_FILE ;
2955
+ if (!strcmp (value , "dir" ) || !strcmp (value , "directory" ))
2956
+ return SYMLINK_TYPE_DIRECTORY ;
2957
+
2958
+ warning (_ ("ignoring invalid symlink type '%s' for '%s'" ), value , link );
2959
+ return SYMLINK_TYPE_UNSPECIFIED ;
2960
+ }
2961
+
2962
+ int mingw_create_symlink (struct index_state * index , const char * target , const char * link )
2883
2963
{
2884
2964
wchar_t wtarget [MAX_LONG_PATH ], wlink [MAX_LONG_PATH ];
2885
2965
int len ;
@@ -2899,48 +2979,31 @@ int symlink(const char *target, const char *link)
2899
2979
if (wtarget [len ] == '/' )
2900
2980
wtarget [len ] = '\\' ;
2901
2981
2902
- /* create file symlink */
2903
- if (!CreateSymbolicLinkW (wlink , wtarget , symlink_file_flags )) {
2904
- errno = err_win_to_posix (GetLastError ());
2905
- return -1 ;
2906
- }
2907
-
2908
- /* convert to directory symlink if target exists */
2909
- switch (process_phantom_symlink (wtarget , wlink )) {
2910
- case PHANTOM_SYMLINK_RETRY : {
2911
- /* if target doesn't exist, add to phantom symlinks list */
2912
- wchar_t wfullpath [MAX_LONG_PATH ];
2913
- struct phantom_symlink_info * psi ;
2914
-
2915
- /* convert to absolute path to be independent of cwd */
2916
- len = GetFullPathNameW (wlink , MAX_LONG_PATH , wfullpath , NULL );
2917
- if (!len || len >= MAX_LONG_PATH ) {
2918
- errno = err_win_to_posix (GetLastError ());
2919
- return -1 ;
2920
- }
2921
-
2922
- /* over-allocate and fill phantom_symlink_info structure */
2923
- psi = xmalloc (sizeof (struct phantom_symlink_info )
2924
- + sizeof (wchar_t ) * (len + wcslen (wtarget ) + 2 ));
2925
- psi -> wlink = (wchar_t * )(psi + 1 );
2926
- wcscpy (psi -> wlink , wfullpath );
2927
- psi -> wtarget = psi -> wlink + len + 1 ;
2928
- wcscpy (psi -> wtarget , wtarget );
2929
-
2930
- EnterCriticalSection (& phantom_symlinks_cs );
2931
- psi -> next = phantom_symlinks ;
2932
- phantom_symlinks = psi ;
2933
- LeaveCriticalSection (& phantom_symlinks_cs );
2934
- break ;
2935
- }
2936
- case PHANTOM_SYMLINK_DIRECTORY :
2937
- /* if we created a dir symlink, process other phantom symlinks */
2982
+ switch (check_symlink_attr (index , link )) {
2983
+ case SYMLINK_TYPE_UNSPECIFIED :
2984
+ /* Create a phantom symlink: it is initially created as a file
2985
+ * symlink, but may change to a directory symlink later if/when
2986
+ * the target exists. */
2987
+ return create_phantom_symlink (wtarget , wlink );
2988
+ case SYMLINK_TYPE_FILE :
2989
+ if (!CreateSymbolicLinkW (wlink , wtarget , symlink_file_flags ))
2990
+ break ;
2991
+ return 0 ;
2992
+ case SYMLINK_TYPE_DIRECTORY :
2993
+ if (!CreateSymbolicLinkW (wlink , wtarget ,
2994
+ symlink_directory_flags ))
2995
+ break ;
2996
+ /* There may be dangling phantom symlinks that point at this
2997
+ * one, which should now morph into directory symlinks. */
2938
2998
process_phantom_symlinks ();
2939
- break ;
2999
+ return 0 ;
2940
3000
default :
2941
- break ;
3001
+ BUG ( "unhandled symlink type" ) ;
2942
3002
}
2943
- return 0 ;
3003
+
3004
+ /* CreateSymbolicLinkW failed. */
3005
+ errno = err_win_to_posix (GetLastError ());
3006
+ return -1 ;
2944
3007
}
2945
3008
2946
3009
#ifndef _WINNT_H
0 commit comments