24
24
#include "../write-or-die.h"
25
25
#include "../repository.h"
26
26
#include "win32/fscache.h"
27
+ #include "../attr.h"
27
28
28
29
#define HCAST (type , handle ) ((type)(intptr_t)handle)
29
30
@@ -460,6 +461,54 @@ static void process_phantom_symlinks(void)
460
461
LeaveCriticalSection (& phantom_symlinks_cs );
461
462
}
462
463
464
+ static int create_phantom_symlink (wchar_t * wtarget , wchar_t * wlink )
465
+ {
466
+ int len ;
467
+
468
+ /* create file symlink */
469
+ if (!CreateSymbolicLinkW (wlink , wtarget , symlink_file_flags )) {
470
+ errno = err_win_to_posix (GetLastError ());
471
+ return -1 ;
472
+ }
473
+
474
+ /* convert to directory symlink if target exists */
475
+ switch (process_phantom_symlink (wtarget , wlink )) {
476
+ case PHANTOM_SYMLINK_RETRY : {
477
+ /* if target doesn't exist, add to phantom symlinks list */
478
+ wchar_t wfullpath [MAX_LONG_PATH ];
479
+ struct phantom_symlink_info * psi ;
480
+
481
+ /* convert to absolute path to be independent of cwd */
482
+ len = GetFullPathNameW (wlink , MAX_LONG_PATH , wfullpath , NULL );
483
+ if (!len || len >= MAX_LONG_PATH ) {
484
+ errno = err_win_to_posix (GetLastError ());
485
+ return -1 ;
486
+ }
487
+
488
+ /* over-allocate and fill phantom_symlink_info structure */
489
+ psi = xmalloc (sizeof (struct phantom_symlink_info ) +
490
+ sizeof (wchar_t ) * (len + wcslen (wtarget ) + 2 ));
491
+ psi -> wlink = (wchar_t * )(psi + 1 );
492
+ wcscpy (psi -> wlink , wfullpath );
493
+ psi -> wtarget = psi -> wlink + len + 1 ;
494
+ wcscpy (psi -> wtarget , wtarget );
495
+
496
+ EnterCriticalSection (& phantom_symlinks_cs );
497
+ psi -> next = phantom_symlinks ;
498
+ phantom_symlinks = psi ;
499
+ LeaveCriticalSection (& phantom_symlinks_cs );
500
+ break ;
501
+ }
502
+ case PHANTOM_SYMLINK_DIRECTORY :
503
+ /* if we created a dir symlink, process other phantom symlinks */
504
+ process_phantom_symlinks ();
505
+ break ;
506
+ default :
507
+ break ;
508
+ }
509
+ return 0 ;
510
+ }
511
+
463
512
/* Normalizes NT paths as returned by some low-level APIs. */
464
513
static wchar_t * normalize_ntpath (wchar_t * wbuf )
465
514
{
@@ -2917,7 +2966,38 @@ int link(const char *oldpath, const char *newpath)
2917
2966
return 0 ;
2918
2967
}
2919
2968
2920
- int symlink (const char * target , const char * link )
2969
+ enum symlink_type {
2970
+ SYMLINK_TYPE_UNSPECIFIED = 0 ,
2971
+ SYMLINK_TYPE_FILE ,
2972
+ SYMLINK_TYPE_DIRECTORY ,
2973
+ };
2974
+
2975
+ static enum symlink_type check_symlink_attr (struct index_state * index , const char * link )
2976
+ {
2977
+ static struct attr_check * check ;
2978
+ const char * value ;
2979
+
2980
+ if (!index )
2981
+ return SYMLINK_TYPE_UNSPECIFIED ;
2982
+
2983
+ if (!check )
2984
+ check = attr_check_initl ("symlink" , NULL );
2985
+
2986
+ git_check_attr (index , link , check );
2987
+
2988
+ value = check -> items [0 ].value ;
2989
+ if (ATTR_UNSET (value ))
2990
+ return SYMLINK_TYPE_UNSPECIFIED ;
2991
+ if (!strcmp (value , "file" ))
2992
+ return SYMLINK_TYPE_FILE ;
2993
+ if (!strcmp (value , "dir" ) || !strcmp (value , "directory" ))
2994
+ return SYMLINK_TYPE_DIRECTORY ;
2995
+
2996
+ warning (_ ("ignoring invalid symlink type '%s' for '%s'" ), value , link );
2997
+ return SYMLINK_TYPE_UNSPECIFIED ;
2998
+ }
2999
+
3000
+ int mingw_create_symlink (struct index_state * index , const char * target , const char * link )
2921
3001
{
2922
3002
wchar_t wtarget [MAX_LONG_PATH ], wlink [MAX_LONG_PATH ];
2923
3003
int len ;
@@ -2937,48 +3017,31 @@ int symlink(const char *target, const char *link)
2937
3017
if (wtarget [len ] == '/' )
2938
3018
wtarget [len ] = '\\' ;
2939
3019
2940
- /* create file symlink */
2941
- if (!CreateSymbolicLinkW (wlink , wtarget , symlink_file_flags )) {
2942
- errno = err_win_to_posix (GetLastError ());
2943
- return -1 ;
2944
- }
2945
-
2946
- /* convert to directory symlink if target exists */
2947
- switch (process_phantom_symlink (wtarget , wlink )) {
2948
- case PHANTOM_SYMLINK_RETRY : {
2949
- /* if target doesn't exist, add to phantom symlinks list */
2950
- wchar_t wfullpath [MAX_LONG_PATH ];
2951
- struct phantom_symlink_info * psi ;
2952
-
2953
- /* convert to absolute path to be independent of cwd */
2954
- len = GetFullPathNameW (wlink , MAX_LONG_PATH , wfullpath , NULL );
2955
- if (!len || len >= MAX_LONG_PATH ) {
2956
- errno = err_win_to_posix (GetLastError ());
2957
- return -1 ;
2958
- }
2959
-
2960
- /* over-allocate and fill phantom_symlink_info structure */
2961
- psi = xmalloc (sizeof (struct phantom_symlink_info )
2962
- + sizeof (wchar_t ) * (len + wcslen (wtarget ) + 2 ));
2963
- psi -> wlink = (wchar_t * )(psi + 1 );
2964
- wcscpy (psi -> wlink , wfullpath );
2965
- psi -> wtarget = psi -> wlink + len + 1 ;
2966
- wcscpy (psi -> wtarget , wtarget );
2967
-
2968
- EnterCriticalSection (& phantom_symlinks_cs );
2969
- psi -> next = phantom_symlinks ;
2970
- phantom_symlinks = psi ;
2971
- LeaveCriticalSection (& phantom_symlinks_cs );
2972
- break ;
2973
- }
2974
- case PHANTOM_SYMLINK_DIRECTORY :
2975
- /* if we created a dir symlink, process other phantom symlinks */
3020
+ switch (check_symlink_attr (index , link )) {
3021
+ case SYMLINK_TYPE_UNSPECIFIED :
3022
+ /* Create a phantom symlink: it is initially created as a file
3023
+ * symlink, but may change to a directory symlink later if/when
3024
+ * the target exists. */
3025
+ return create_phantom_symlink (wtarget , wlink );
3026
+ case SYMLINK_TYPE_FILE :
3027
+ if (!CreateSymbolicLinkW (wlink , wtarget , symlink_file_flags ))
3028
+ break ;
3029
+ return 0 ;
3030
+ case SYMLINK_TYPE_DIRECTORY :
3031
+ if (!CreateSymbolicLinkW (wlink , wtarget ,
3032
+ symlink_directory_flags ))
3033
+ break ;
3034
+ /* There may be dangling phantom symlinks that point at this
3035
+ * one, which should now morph into directory symlinks. */
2976
3036
process_phantom_symlinks ();
2977
- break ;
3037
+ return 0 ;
2978
3038
default :
2979
- break ;
3039
+ BUG ( "unhandled symlink type" ) ;
2980
3040
}
2981
- return 0 ;
3041
+
3042
+ /* CreateSymbolicLinkW failed. */
3043
+ errno = err_win_to_posix (GetLastError ());
3044
+ return -1 ;
2982
3045
}
2983
3046
2984
3047
#ifndef _WINNT_H
0 commit comments