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
{
@@ -3063,7 +3112,38 @@ int link(const char *oldpath, const char *newpath)
3063
3112
return 0 ;
3064
3113
}
3065
3114
3066
- int symlink (const char * target , const char * link )
3115
+ enum symlink_type {
3116
+ SYMLINK_TYPE_UNSPECIFIED = 0 ,
3117
+ SYMLINK_TYPE_FILE ,
3118
+ SYMLINK_TYPE_DIRECTORY ,
3119
+ };
3120
+
3121
+ static enum symlink_type check_symlink_attr (struct index_state * index , const char * link )
3122
+ {
3123
+ static struct attr_check * check ;
3124
+ const char * value ;
3125
+
3126
+ if (!index )
3127
+ return SYMLINK_TYPE_UNSPECIFIED ;
3128
+
3129
+ if (!check )
3130
+ check = attr_check_initl ("symlink" , NULL );
3131
+
3132
+ git_check_attr (index , link , check );
3133
+
3134
+ value = check -> items [0 ].value ;
3135
+ if (ATTR_UNSET (value ))
3136
+ return SYMLINK_TYPE_UNSPECIFIED ;
3137
+ if (!strcmp (value , "file" ))
3138
+ return SYMLINK_TYPE_FILE ;
3139
+ if (!strcmp (value , "dir" ) || !strcmp (value , "directory" ))
3140
+ return SYMLINK_TYPE_DIRECTORY ;
3141
+
3142
+ warning (_ ("ignoring invalid symlink type '%s' for '%s'" ), value , link );
3143
+ return SYMLINK_TYPE_UNSPECIFIED ;
3144
+ }
3145
+
3146
+ int mingw_create_symlink (struct index_state * index , const char * target , const char * link )
3067
3147
{
3068
3148
wchar_t wtarget [MAX_LONG_PATH ], wlink [MAX_LONG_PATH ];
3069
3149
int len ;
@@ -3083,48 +3163,31 @@ int symlink(const char *target, const char *link)
3083
3163
if (wtarget [len ] == '/' )
3084
3164
wtarget [len ] = '\\' ;
3085
3165
3086
- /* create file symlink */
3087
- if (!CreateSymbolicLinkW (wlink , wtarget , symlink_file_flags )) {
3088
- errno = err_win_to_posix (GetLastError ());
3089
- return -1 ;
3090
- }
3091
-
3092
- /* convert to directory symlink if target exists */
3093
- switch (process_phantom_symlink (wtarget , wlink )) {
3094
- case PHANTOM_SYMLINK_RETRY : {
3095
- /* if target doesn't exist, add to phantom symlinks list */
3096
- wchar_t wfullpath [MAX_LONG_PATH ];
3097
- struct phantom_symlink_info * psi ;
3098
-
3099
- /* convert to absolute path to be independent of cwd */
3100
- len = GetFullPathNameW (wlink , MAX_LONG_PATH , wfullpath , NULL );
3101
- if (!len || len >= MAX_LONG_PATH ) {
3102
- errno = err_win_to_posix (GetLastError ());
3103
- return -1 ;
3104
- }
3105
-
3106
- /* over-allocate and fill phantom_symlink_info structure */
3107
- psi = xmalloc (sizeof (struct phantom_symlink_info )
3108
- + sizeof (wchar_t ) * (len + wcslen (wtarget ) + 2 ));
3109
- psi -> wlink = (wchar_t * )(psi + 1 );
3110
- wcscpy (psi -> wlink , wfullpath );
3111
- psi -> wtarget = psi -> wlink + len + 1 ;
3112
- wcscpy (psi -> wtarget , wtarget );
3113
-
3114
- EnterCriticalSection (& phantom_symlinks_cs );
3115
- psi -> next = phantom_symlinks ;
3116
- phantom_symlinks = psi ;
3117
- LeaveCriticalSection (& phantom_symlinks_cs );
3118
- break ;
3119
- }
3120
- case PHANTOM_SYMLINK_DIRECTORY :
3121
- /* if we created a dir symlink, process other phantom symlinks */
3166
+ switch (check_symlink_attr (index , link )) {
3167
+ case SYMLINK_TYPE_UNSPECIFIED :
3168
+ /* Create a phantom symlink: it is initially created as a file
3169
+ * symlink, but may change to a directory symlink later if/when
3170
+ * the target exists. */
3171
+ return create_phantom_symlink (wtarget , wlink );
3172
+ case SYMLINK_TYPE_FILE :
3173
+ if (!CreateSymbolicLinkW (wlink , wtarget , symlink_file_flags ))
3174
+ break ;
3175
+ return 0 ;
3176
+ case SYMLINK_TYPE_DIRECTORY :
3177
+ if (!CreateSymbolicLinkW (wlink , wtarget ,
3178
+ symlink_directory_flags ))
3179
+ break ;
3180
+ /* There may be dangling phantom symlinks that point at this
3181
+ * one, which should now morph into directory symlinks. */
3122
3182
process_phantom_symlinks ();
3123
- break ;
3183
+ return 0 ;
3124
3184
default :
3125
- break ;
3185
+ BUG ( "unhandled symlink type" ) ;
3126
3186
}
3127
- return 0 ;
3187
+
3188
+ /* CreateSymbolicLinkW failed. */
3189
+ errno = err_win_to_posix (GetLastError ());
3190
+ return -1 ;
3128
3191
}
3129
3192
3130
3193
#ifndef _WINNT_H
0 commit comments