22
22
#include "../write-or-die.h"
23
23
#include "../repository.h"
24
24
#include "win32/fscache.h"
25
+ #include "../attr.h"
25
26
26
27
#define HCAST (type , handle ) ((type)(intptr_t)handle)
27
28
@@ -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
{
@@ -2892,7 +2941,38 @@ int link(const char *oldpath, const char *newpath)
2892
2941
return 0 ;
2893
2942
}
2894
2943
2895
- int symlink (const char * target , const char * link )
2944
+ enum symlink_type {
2945
+ SYMLINK_TYPE_UNSPECIFIED = 0 ,
2946
+ SYMLINK_TYPE_FILE ,
2947
+ SYMLINK_TYPE_DIRECTORY ,
2948
+ };
2949
+
2950
+ static enum symlink_type check_symlink_attr (struct index_state * index , const char * link )
2951
+ {
2952
+ static struct attr_check * check ;
2953
+ const char * value ;
2954
+
2955
+ if (!index )
2956
+ return SYMLINK_TYPE_UNSPECIFIED ;
2957
+
2958
+ if (!check )
2959
+ check = attr_check_initl ("symlink" , NULL );
2960
+
2961
+ git_check_attr (index , link , check );
2962
+
2963
+ value = check -> items [0 ].value ;
2964
+ if (ATTR_UNSET (value ))
2965
+ return SYMLINK_TYPE_UNSPECIFIED ;
2966
+ if (!strcmp (value , "file" ))
2967
+ return SYMLINK_TYPE_FILE ;
2968
+ if (!strcmp (value , "dir" ) || !strcmp (value , "directory" ))
2969
+ return SYMLINK_TYPE_DIRECTORY ;
2970
+
2971
+ warning (_ ("ignoring invalid symlink type '%s' for '%s'" ), value , link );
2972
+ return SYMLINK_TYPE_UNSPECIFIED ;
2973
+ }
2974
+
2975
+ int mingw_create_symlink (struct index_state * index , const char * target , const char * link )
2896
2976
{
2897
2977
wchar_t wtarget [MAX_LONG_PATH ], wlink [MAX_LONG_PATH ];
2898
2978
int len ;
@@ -2912,48 +2992,31 @@ int symlink(const char *target, const char *link)
2912
2992
if (wtarget [len ] == '/' )
2913
2993
wtarget [len ] = '\\' ;
2914
2994
2915
- /* create file symlink */
2916
- if (!CreateSymbolicLinkW (wlink , wtarget , symlink_file_flags )) {
2917
- errno = err_win_to_posix (GetLastError ());
2918
- return -1 ;
2919
- }
2920
-
2921
- /* convert to directory symlink if target exists */
2922
- switch (process_phantom_symlink (wtarget , wlink )) {
2923
- case PHANTOM_SYMLINK_RETRY : {
2924
- /* if target doesn't exist, add to phantom symlinks list */
2925
- wchar_t wfullpath [MAX_LONG_PATH ];
2926
- struct phantom_symlink_info * psi ;
2927
-
2928
- /* convert to absolute path to be independent of cwd */
2929
- len = GetFullPathNameW (wlink , MAX_LONG_PATH , wfullpath , NULL );
2930
- if (!len || len >= MAX_LONG_PATH ) {
2931
- errno = err_win_to_posix (GetLastError ());
2932
- return -1 ;
2933
- }
2934
-
2935
- /* over-allocate and fill phantom_symlink_info structure */
2936
- psi = xmalloc (sizeof (struct phantom_symlink_info )
2937
- + sizeof (wchar_t ) * (len + wcslen (wtarget ) + 2 ));
2938
- psi -> wlink = (wchar_t * )(psi + 1 );
2939
- wcscpy (psi -> wlink , wfullpath );
2940
- psi -> wtarget = psi -> wlink + len + 1 ;
2941
- wcscpy (psi -> wtarget , wtarget );
2942
-
2943
- EnterCriticalSection (& phantom_symlinks_cs );
2944
- psi -> next = phantom_symlinks ;
2945
- phantom_symlinks = psi ;
2946
- LeaveCriticalSection (& phantom_symlinks_cs );
2947
- break ;
2948
- }
2949
- case PHANTOM_SYMLINK_DIRECTORY :
2950
- /* if we created a dir symlink, process other phantom symlinks */
2995
+ switch (check_symlink_attr (index , link )) {
2996
+ case SYMLINK_TYPE_UNSPECIFIED :
2997
+ /* Create a phantom symlink: it is initially created as a file
2998
+ * symlink, but may change to a directory symlink later if/when
2999
+ * the target exists. */
3000
+ return create_phantom_symlink (wtarget , wlink );
3001
+ case SYMLINK_TYPE_FILE :
3002
+ if (!CreateSymbolicLinkW (wlink , wtarget , symlink_file_flags ))
3003
+ break ;
3004
+ return 0 ;
3005
+ case SYMLINK_TYPE_DIRECTORY :
3006
+ if (!CreateSymbolicLinkW (wlink , wtarget ,
3007
+ symlink_directory_flags ))
3008
+ break ;
3009
+ /* There may be dangling phantom symlinks that point at this
3010
+ * one, which should now morph into directory symlinks. */
2951
3011
process_phantom_symlinks ();
2952
- break ;
3012
+ return 0 ;
2953
3013
default :
2954
- break ;
3014
+ BUG ( "unhandled symlink type" ) ;
2955
3015
}
2956
- return 0 ;
3016
+
3017
+ /* CreateSymbolicLinkW failed. */
3018
+ errno = err_win_to_posix (GetLastError ());
3019
+ return -1 ;
2957
3020
}
2958
3021
2959
3022
#ifndef _WINNT_H
0 commit comments