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