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
{
@@ -2921,7 +2970,38 @@ int link(const char *oldpath, const char *newpath)
2921
2970
return 0 ;
2922
2971
}
2923
2972
2924
- int symlink (const char * target , const char * link )
2973
+ enum symlink_type {
2974
+ SYMLINK_TYPE_UNSPECIFIED = 0 ,
2975
+ SYMLINK_TYPE_FILE ,
2976
+ SYMLINK_TYPE_DIRECTORY ,
2977
+ };
2978
+
2979
+ static enum symlink_type check_symlink_attr (struct index_state * index , const char * link )
2980
+ {
2981
+ static struct attr_check * check ;
2982
+ const char * value ;
2983
+
2984
+ if (!index )
2985
+ return SYMLINK_TYPE_UNSPECIFIED ;
2986
+
2987
+ if (!check )
2988
+ check = attr_check_initl ("symlink" , NULL );
2989
+
2990
+ git_check_attr (index , link , check );
2991
+
2992
+ value = check -> items [0 ].value ;
2993
+ if (ATTR_UNSET (value ))
2994
+ return SYMLINK_TYPE_UNSPECIFIED ;
2995
+ if (!strcmp (value , "file" ))
2996
+ return SYMLINK_TYPE_FILE ;
2997
+ if (!strcmp (value , "dir" ) || !strcmp (value , "directory" ))
2998
+ return SYMLINK_TYPE_DIRECTORY ;
2999
+
3000
+ warning (_ ("ignoring invalid symlink type '%s' for '%s'" ), value , link );
3001
+ return SYMLINK_TYPE_UNSPECIFIED ;
3002
+ }
3003
+
3004
+ int mingw_create_symlink (struct index_state * index , const char * target , const char * link )
2925
3005
{
2926
3006
wchar_t wtarget [MAX_LONG_PATH ], wlink [MAX_LONG_PATH ];
2927
3007
int len ;
@@ -2941,48 +3021,31 @@ int symlink(const char *target, const char *link)
2941
3021
if (wtarget [len ] == '/' )
2942
3022
wtarget [len ] = '\\' ;
2943
3023
2944
- /* create file symlink */
2945
- if (!CreateSymbolicLinkW (wlink , wtarget , symlink_file_flags )) {
2946
- errno = err_win_to_posix (GetLastError ());
2947
- return -1 ;
2948
- }
2949
-
2950
- /* convert to directory symlink if target exists */
2951
- switch (process_phantom_symlink (wtarget , wlink )) {
2952
- case PHANTOM_SYMLINK_RETRY : {
2953
- /* if target doesn't exist, add to phantom symlinks list */
2954
- wchar_t wfullpath [MAX_LONG_PATH ];
2955
- struct phantom_symlink_info * psi ;
2956
-
2957
- /* convert to absolute path to be independent of cwd */
2958
- len = GetFullPathNameW (wlink , MAX_LONG_PATH , wfullpath , NULL );
2959
- if (!len || len >= MAX_LONG_PATH ) {
2960
- errno = err_win_to_posix (GetLastError ());
2961
- return -1 ;
2962
- }
2963
-
2964
- /* over-allocate and fill phantom_symlink_info structure */
2965
- psi = xmalloc (sizeof (struct phantom_symlink_info )
2966
- + sizeof (wchar_t ) * (len + wcslen (wtarget ) + 2 ));
2967
- psi -> wlink = (wchar_t * )(psi + 1 );
2968
- wcscpy (psi -> wlink , wfullpath );
2969
- psi -> wtarget = psi -> wlink + len + 1 ;
2970
- wcscpy (psi -> wtarget , wtarget );
2971
-
2972
- EnterCriticalSection (& phantom_symlinks_cs );
2973
- psi -> next = phantom_symlinks ;
2974
- phantom_symlinks = psi ;
2975
- LeaveCriticalSection (& phantom_symlinks_cs );
2976
- break ;
2977
- }
2978
- case PHANTOM_SYMLINK_DIRECTORY :
2979
- /* if we created a dir symlink, process other phantom symlinks */
3024
+ switch (check_symlink_attr (index , link )) {
3025
+ case SYMLINK_TYPE_UNSPECIFIED :
3026
+ /* Create a phantom symlink: it is initially created as a file
3027
+ * symlink, but may change to a directory symlink later if/when
3028
+ * the target exists. */
3029
+ return create_phantom_symlink (wtarget , wlink );
3030
+ case SYMLINK_TYPE_FILE :
3031
+ if (!CreateSymbolicLinkW (wlink , wtarget , symlink_file_flags ))
3032
+ break ;
3033
+ return 0 ;
3034
+ case SYMLINK_TYPE_DIRECTORY :
3035
+ if (!CreateSymbolicLinkW (wlink , wtarget ,
3036
+ symlink_directory_flags ))
3037
+ break ;
3038
+ /* There may be dangling phantom symlinks that point at this
3039
+ * one, which should now morph into directory symlinks. */
2980
3040
process_phantom_symlinks ();
2981
- break ;
3041
+ return 0 ;
2982
3042
default :
2983
- break ;
3043
+ BUG ( "unhandled symlink type" ) ;
2984
3044
}
2985
- return 0 ;
3045
+
3046
+ /* CreateSymbolicLinkW failed. */
3047
+ errno = err_win_to_posix (GetLastError ());
3048
+ return -1 ;
2986
3049
}
2987
3050
2988
3051
#ifndef _WINNT_H
0 commit comments