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
{
@@ -3126,7 +3175,38 @@ int link(const char *oldpath, const char *newpath)
3126
3175
return 0 ;
3127
3176
}
3128
3177
3129
- int symlink (const char * target , const char * link )
3178
+ enum symlink_type {
3179
+ SYMLINK_TYPE_UNSPECIFIED = 0 ,
3180
+ SYMLINK_TYPE_FILE ,
3181
+ SYMLINK_TYPE_DIRECTORY ,
3182
+ };
3183
+
3184
+ static enum symlink_type check_symlink_attr (struct index_state * index , const char * link )
3185
+ {
3186
+ static struct attr_check * check ;
3187
+ const char * value ;
3188
+
3189
+ if (!index )
3190
+ return SYMLINK_TYPE_UNSPECIFIED ;
3191
+
3192
+ if (!check )
3193
+ check = attr_check_initl ("symlink" , NULL );
3194
+
3195
+ git_check_attr (index , link , check );
3196
+
3197
+ value = check -> items [0 ].value ;
3198
+ if (ATTR_UNSET (value ))
3199
+ return SYMLINK_TYPE_UNSPECIFIED ;
3200
+ if (!strcmp (value , "file" ))
3201
+ return SYMLINK_TYPE_FILE ;
3202
+ if (!strcmp (value , "dir" ) || !strcmp (value , "directory" ))
3203
+ return SYMLINK_TYPE_DIRECTORY ;
3204
+
3205
+ warning (_ ("ignoring invalid symlink type '%s' for '%s'" ), value , link );
3206
+ return SYMLINK_TYPE_UNSPECIFIED ;
3207
+ }
3208
+
3209
+ int mingw_create_symlink (struct index_state * index , const char * target , const char * link )
3130
3210
{
3131
3211
wchar_t wtarget [MAX_LONG_PATH ], wlink [MAX_LONG_PATH ];
3132
3212
int len ;
@@ -3146,48 +3226,31 @@ int symlink(const char *target, const char *link)
3146
3226
if (wtarget [len ] == '/' )
3147
3227
wtarget [len ] = '\\' ;
3148
3228
3149
- /* create file symlink */
3150
- if (!CreateSymbolicLinkW (wlink , wtarget , symlink_file_flags )) {
3151
- errno = err_win_to_posix (GetLastError ());
3152
- return -1 ;
3153
- }
3154
-
3155
- /* convert to directory symlink if target exists */
3156
- switch (process_phantom_symlink (wtarget , wlink )) {
3157
- case PHANTOM_SYMLINK_RETRY : {
3158
- /* if target doesn't exist, add to phantom symlinks list */
3159
- wchar_t wfullpath [MAX_LONG_PATH ];
3160
- struct phantom_symlink_info * psi ;
3161
-
3162
- /* convert to absolute path to be independent of cwd */
3163
- len = GetFullPathNameW (wlink , MAX_LONG_PATH , wfullpath , NULL );
3164
- if (!len || len >= MAX_LONG_PATH ) {
3165
- errno = err_win_to_posix (GetLastError ());
3166
- return -1 ;
3167
- }
3168
-
3169
- /* over-allocate and fill phantom_symlink_info structure */
3170
- psi = xmalloc (sizeof (struct phantom_symlink_info )
3171
- + sizeof (wchar_t ) * (len + wcslen (wtarget ) + 2 ));
3172
- psi -> wlink = (wchar_t * )(psi + 1 );
3173
- wcscpy (psi -> wlink , wfullpath );
3174
- psi -> wtarget = psi -> wlink + len + 1 ;
3175
- wcscpy (psi -> wtarget , wtarget );
3176
-
3177
- EnterCriticalSection (& phantom_symlinks_cs );
3178
- psi -> next = phantom_symlinks ;
3179
- phantom_symlinks = psi ;
3180
- LeaveCriticalSection (& phantom_symlinks_cs );
3181
- break ;
3182
- }
3183
- case PHANTOM_SYMLINK_DIRECTORY :
3184
- /* if we created a dir symlink, process other phantom symlinks */
3229
+ switch (check_symlink_attr (index , link )) {
3230
+ case SYMLINK_TYPE_UNSPECIFIED :
3231
+ /* Create a phantom symlink: it is initially created as a file
3232
+ * symlink, but may change to a directory symlink later if/when
3233
+ * the target exists. */
3234
+ return create_phantom_symlink (wtarget , wlink );
3235
+ case SYMLINK_TYPE_FILE :
3236
+ if (!CreateSymbolicLinkW (wlink , wtarget , symlink_file_flags ))
3237
+ break ;
3238
+ return 0 ;
3239
+ case SYMLINK_TYPE_DIRECTORY :
3240
+ if (!CreateSymbolicLinkW (wlink , wtarget ,
3241
+ symlink_directory_flags ))
3242
+ break ;
3243
+ /* There may be dangling phantom symlinks that point at this
3244
+ * one, which should now morph into directory symlinks. */
3185
3245
process_phantom_symlinks ();
3186
- break ;
3246
+ return 0 ;
3187
3247
default :
3188
- break ;
3248
+ BUG ( "unhandled symlink type" ) ;
3189
3249
}
3190
- return 0 ;
3250
+
3251
+ /* CreateSymbolicLinkW failed. */
3252
+ errno = err_win_to_posix (GetLastError ());
3253
+ return -1 ;
3191
3254
}
3192
3255
3193
3256
#ifndef _WINNT_H
0 commit comments