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