4
4
#include "git-compat-util.h"
5
5
#include "abspath.h"
6
6
#include "alloc.h"
7
+ #include "attr.h"
7
8
#include "config.h"
8
9
#include "dir.h"
9
10
#include "environment.h"
@@ -454,6 +455,54 @@ static void process_phantom_symlinks(void)
454
455
LeaveCriticalSection (& phantom_symlinks_cs );
455
456
}
456
457
458
+ static int create_phantom_symlink (wchar_t * wtarget , wchar_t * wlink )
459
+ {
460
+ int len ;
461
+
462
+ /* create file symlink */
463
+ if (!CreateSymbolicLinkW (wlink , wtarget , symlink_file_flags )) {
464
+ errno = err_win_to_posix (GetLastError ());
465
+ return -1 ;
466
+ }
467
+
468
+ /* convert to directory symlink if target exists */
469
+ switch (process_phantom_symlink (wtarget , wlink )) {
470
+ case PHANTOM_SYMLINK_RETRY : {
471
+ /* if target doesn't exist, add to phantom symlinks list */
472
+ wchar_t wfullpath [MAX_LONG_PATH ];
473
+ struct phantom_symlink_info * psi ;
474
+
475
+ /* convert to absolute path to be independent of cwd */
476
+ len = GetFullPathNameW (wlink , MAX_LONG_PATH , wfullpath , NULL );
477
+ if (!len || len >= MAX_LONG_PATH ) {
478
+ errno = err_win_to_posix (GetLastError ());
479
+ return -1 ;
480
+ }
481
+
482
+ /* over-allocate and fill phantom_symlink_info structure */
483
+ psi = xmalloc (sizeof (struct phantom_symlink_info ) +
484
+ sizeof (wchar_t ) * (len + wcslen (wtarget ) + 2 ));
485
+ psi -> wlink = (wchar_t * )(psi + 1 );
486
+ wcscpy (psi -> wlink , wfullpath );
487
+ psi -> wtarget = psi -> wlink + len + 1 ;
488
+ wcscpy (psi -> wtarget , wtarget );
489
+
490
+ EnterCriticalSection (& phantom_symlinks_cs );
491
+ psi -> next = phantom_symlinks ;
492
+ phantom_symlinks = psi ;
493
+ LeaveCriticalSection (& phantom_symlinks_cs );
494
+ break ;
495
+ }
496
+ case PHANTOM_SYMLINK_DIRECTORY :
497
+ /* if we created a dir symlink, process other phantom symlinks */
498
+ process_phantom_symlinks ();
499
+ break ;
500
+ default :
501
+ break ;
502
+ }
503
+ return 0 ;
504
+ }
505
+
457
506
/* Normalizes NT paths as returned by some low-level APIs. */
458
507
static wchar_t * normalize_ntpath (wchar_t * wbuf )
459
508
{
@@ -3124,7 +3173,38 @@ int link(const char *oldpath, const char *newpath)
3124
3173
return 0 ;
3125
3174
}
3126
3175
3127
- int symlink (const char * target , const char * link )
3176
+ enum symlink_type {
3177
+ SYMLINK_TYPE_UNSPECIFIED = 0 ,
3178
+ SYMLINK_TYPE_FILE ,
3179
+ SYMLINK_TYPE_DIRECTORY ,
3180
+ };
3181
+
3182
+ static enum symlink_type check_symlink_attr (struct index_state * index , const char * link )
3183
+ {
3184
+ static struct attr_check * check ;
3185
+ const char * value ;
3186
+
3187
+ if (!index )
3188
+ return SYMLINK_TYPE_UNSPECIFIED ;
3189
+
3190
+ if (!check )
3191
+ check = attr_check_initl ("symlink" , NULL );
3192
+
3193
+ git_check_attr (index , link , check );
3194
+
3195
+ value = check -> items [0 ].value ;
3196
+ if (ATTR_UNSET (value ))
3197
+ return SYMLINK_TYPE_UNSPECIFIED ;
3198
+ if (!strcmp (value , "file" ))
3199
+ return SYMLINK_TYPE_FILE ;
3200
+ if (!strcmp (value , "dir" ) || !strcmp (value , "directory" ))
3201
+ return SYMLINK_TYPE_DIRECTORY ;
3202
+
3203
+ warning (_ ("ignoring invalid symlink type '%s' for '%s'" ), value , link );
3204
+ return SYMLINK_TYPE_UNSPECIFIED ;
3205
+ }
3206
+
3207
+ int mingw_create_symlink (struct index_state * index , const char * target , const char * link )
3128
3208
{
3129
3209
wchar_t wtarget [MAX_LONG_PATH ], wlink [MAX_LONG_PATH ];
3130
3210
int len ;
@@ -3144,48 +3224,31 @@ int symlink(const char *target, const char *link)
3144
3224
if (wtarget [len ] == '/' )
3145
3225
wtarget [len ] = '\\' ;
3146
3226
3147
- /* create file symlink */
3148
- if (!CreateSymbolicLinkW (wlink , wtarget , symlink_file_flags )) {
3149
- errno = err_win_to_posix (GetLastError ());
3150
- return -1 ;
3151
- }
3152
-
3153
- /* convert to directory symlink if target exists */
3154
- switch (process_phantom_symlink (wtarget , wlink )) {
3155
- case PHANTOM_SYMLINK_RETRY : {
3156
- /* if target doesn't exist, add to phantom symlinks list */
3157
- wchar_t wfullpath [MAX_LONG_PATH ];
3158
- struct phantom_symlink_info * psi ;
3159
-
3160
- /* convert to absolute path to be independent of cwd */
3161
- len = GetFullPathNameW (wlink , MAX_LONG_PATH , wfullpath , NULL );
3162
- if (!len || len >= MAX_LONG_PATH ) {
3163
- errno = err_win_to_posix (GetLastError ());
3164
- return -1 ;
3165
- }
3166
-
3167
- /* over-allocate and fill phantom_symlink_info structure */
3168
- psi = xmalloc (sizeof (struct phantom_symlink_info )
3169
- + sizeof (wchar_t ) * (len + wcslen (wtarget ) + 2 ));
3170
- psi -> wlink = (wchar_t * )(psi + 1 );
3171
- wcscpy (psi -> wlink , wfullpath );
3172
- psi -> wtarget = psi -> wlink + len + 1 ;
3173
- wcscpy (psi -> wtarget , wtarget );
3174
-
3175
- EnterCriticalSection (& phantom_symlinks_cs );
3176
- psi -> next = phantom_symlinks ;
3177
- phantom_symlinks = psi ;
3178
- LeaveCriticalSection (& phantom_symlinks_cs );
3179
- break ;
3180
- }
3181
- case PHANTOM_SYMLINK_DIRECTORY :
3182
- /* if we created a dir symlink, process other phantom symlinks */
3227
+ switch (check_symlink_attr (index , link )) {
3228
+ case SYMLINK_TYPE_UNSPECIFIED :
3229
+ /* Create a phantom symlink: it is initially created as a file
3230
+ * symlink, but may change to a directory symlink later if/when
3231
+ * the target exists. */
3232
+ return create_phantom_symlink (wtarget , wlink );
3233
+ case SYMLINK_TYPE_FILE :
3234
+ if (!CreateSymbolicLinkW (wlink , wtarget , symlink_file_flags ))
3235
+ break ;
3236
+ return 0 ;
3237
+ case SYMLINK_TYPE_DIRECTORY :
3238
+ if (!CreateSymbolicLinkW (wlink , wtarget ,
3239
+ symlink_directory_flags ))
3240
+ break ;
3241
+ /* There may be dangling phantom symlinks that point at this
3242
+ * one, which should now morph into directory symlinks. */
3183
3243
process_phantom_symlinks ();
3184
- break ;
3244
+ return 0 ;
3185
3245
default :
3186
- break ;
3246
+ BUG ( "unhandled symlink type" ) ;
3187
3247
}
3188
- return 0 ;
3248
+
3249
+ /* CreateSymbolicLinkW failed. */
3250
+ errno = err_win_to_posix (GetLastError ());
3251
+ return -1 ;
3189
3252
}
3190
3253
3191
3254
#ifndef _WINNT_H
0 commit comments