8
8
#include "../cache.h"
9
9
#include "win32/lazyload.h"
10
10
#include "../config.h"
11
+ #include "../attr.h"
11
12
12
13
#define HCAST (type , handle ) ((type)(intptr_t)handle)
13
14
@@ -399,6 +400,54 @@ static void process_phantom_symlinks(void)
399
400
LeaveCriticalSection (& phantom_symlinks_cs );
400
401
}
401
402
403
+ static int create_phantom_symlink (wchar_t * wtarget , wchar_t * wlink )
404
+ {
405
+ int len ;
406
+
407
+ /* create file symlink */
408
+ if (!CreateSymbolicLinkW (wlink , wtarget , symlink_file_flags )) {
409
+ errno = err_win_to_posix (GetLastError ());
410
+ return -1 ;
411
+ }
412
+
413
+ /* convert to directory symlink if target exists */
414
+ switch (process_phantom_symlink (wtarget , wlink )) {
415
+ case PHANTOM_SYMLINK_RETRY : {
416
+ /* if target doesn't exist, add to phantom symlinks list */
417
+ wchar_t wfullpath [MAX_LONG_PATH ];
418
+ struct phantom_symlink_info * psi ;
419
+
420
+ /* convert to absolute path to be independent of cwd */
421
+ len = GetFullPathNameW (wlink , MAX_LONG_PATH , wfullpath , NULL );
422
+ if (!len || len >= MAX_LONG_PATH ) {
423
+ errno = err_win_to_posix (GetLastError ());
424
+ return -1 ;
425
+ }
426
+
427
+ /* over-allocate and fill phantom_symlink_info structure */
428
+ psi = xmalloc (sizeof (struct phantom_symlink_info ) +
429
+ sizeof (wchar_t ) * (len + wcslen (wtarget ) + 2 ));
430
+ psi -> wlink = (wchar_t * )(psi + 1 );
431
+ wcscpy (psi -> wlink , wfullpath );
432
+ psi -> wtarget = psi -> wlink + len + 1 ;
433
+ wcscpy (psi -> wtarget , wtarget );
434
+
435
+ EnterCriticalSection (& phantom_symlinks_cs );
436
+ psi -> next = phantom_symlinks ;
437
+ phantom_symlinks = psi ;
438
+ LeaveCriticalSection (& phantom_symlinks_cs );
439
+ break ;
440
+ }
441
+ case PHANTOM_SYMLINK_DIRECTORY :
442
+ /* if we created a dir symlink, process other phantom symlinks */
443
+ process_phantom_symlinks ();
444
+ break ;
445
+ default :
446
+ break ;
447
+ }
448
+ return 0 ;
449
+ }
450
+
402
451
/* Normalizes NT paths as returned by some low-level APIs. */
403
452
static wchar_t * normalize_ntpath (wchar_t * wbuf )
404
453
{
@@ -2448,6 +2497,35 @@ int link(const char *oldpath, const char *newpath)
2448
2497
return 0 ;
2449
2498
}
2450
2499
2500
+ enum symlink_type {
2501
+ SYMLINK_TYPE_UNSPECIFIED = 0 ,
2502
+ SYMLINK_TYPE_FILE ,
2503
+ SYMLINK_TYPE_DIRECTORY ,
2504
+ };
2505
+
2506
+ static enum symlink_type check_symlink_attr (const char * link )
2507
+ {
2508
+ static struct attr_check * check ;
2509
+ const char * value ;
2510
+ int r ;
2511
+
2512
+ if (!check )
2513
+ check = attr_check_initl ("symlink" , NULL );
2514
+
2515
+ r = git_check_attr (& the_index , link , check );
2516
+ assert (!r );
2517
+
2518
+ value = check -> items [0 ].value ;
2519
+ if (value == NULL )
2520
+ ;
2521
+ else if (!strcmp (value , "file" ))
2522
+ return SYMLINK_TYPE_FILE ;
2523
+ else if (!strcmp (value , "dir" ))
2524
+ return SYMLINK_TYPE_DIRECTORY ;
2525
+
2526
+ return SYMLINK_TYPE_UNSPECIFIED ;
2527
+ }
2528
+
2451
2529
int symlink (const char * target , const char * link )
2452
2530
{
2453
2531
wchar_t wtarget [MAX_LONG_PATH ], wlink [MAX_LONG_PATH ];
@@ -2468,48 +2546,31 @@ int symlink(const char *target, const char *link)
2468
2546
if (wtarget [len ] == '/' )
2469
2547
wtarget [len ] = '\\' ;
2470
2548
2471
- /* create file symlink */
2472
- if (!CreateSymbolicLinkW (wlink , wtarget , symlink_file_flags )) {
2473
- errno = err_win_to_posix (GetLastError ());
2474
- return -1 ;
2475
- }
2476
-
2477
- /* convert to directory symlink if target exists */
2478
- switch (process_phantom_symlink (wtarget , wlink )) {
2479
- case PHANTOM_SYMLINK_RETRY : {
2480
- /* if target doesn't exist, add to phantom symlinks list */
2481
- wchar_t wfullpath [MAX_LONG_PATH ];
2482
- struct phantom_symlink_info * psi ;
2483
-
2484
- /* convert to absolute path to be independent of cwd */
2485
- len = GetFullPathNameW (wlink , MAX_LONG_PATH , wfullpath , NULL );
2486
- if (!len || len >= MAX_LONG_PATH ) {
2487
- errno = err_win_to_posix (GetLastError ());
2488
- return -1 ;
2489
- }
2490
-
2491
- /* over-allocate and fill phantom_symlink_info structure */
2492
- psi = xmalloc (sizeof (struct phantom_symlink_info )
2493
- + sizeof (wchar_t ) * (len + wcslen (wtarget ) + 2 ));
2494
- psi -> wlink = (wchar_t * )(psi + 1 );
2495
- wcscpy (psi -> wlink , wfullpath );
2496
- psi -> wtarget = psi -> wlink + len + 1 ;
2497
- wcscpy (psi -> wtarget , wtarget );
2498
-
2499
- EnterCriticalSection (& phantom_symlinks_cs );
2500
- psi -> next = phantom_symlinks ;
2501
- phantom_symlinks = psi ;
2502
- LeaveCriticalSection (& phantom_symlinks_cs );
2503
- break ;
2504
- }
2505
- case PHANTOM_SYMLINK_DIRECTORY :
2506
- /* if we created a dir symlink, process other phantom symlinks */
2549
+ switch (check_symlink_attr (link )) {
2550
+ case SYMLINK_TYPE_UNSPECIFIED :
2551
+ /* Create a phantom symlink: it is initially created as a file
2552
+ * symlink, but may change to a directory symlink later if/when
2553
+ * the target exists. */
2554
+ return create_phantom_symlink (wtarget , wlink );
2555
+ case SYMLINK_TYPE_FILE :
2556
+ if (!CreateSymbolicLinkW (wlink , wtarget , symlink_file_flags ))
2557
+ break ;
2558
+ return 0 ;
2559
+ case SYMLINK_TYPE_DIRECTORY :
2560
+ if (!CreateSymbolicLinkW (wlink , wtarget ,
2561
+ symlink_directory_flags ))
2562
+ break ;
2563
+ /* There may be dangling phantom symlinks that point at this
2564
+ * one, which should now morph into directory symlinks. */
2507
2565
process_phantom_symlinks ();
2508
- break ;
2566
+ return 0 ;
2509
2567
default :
2510
- break ;
2568
+ BUG ( "unhandled symlink type" ) ;
2511
2569
}
2512
- return 0 ;
2570
+
2571
+ /* CreateSymbolicLinkW failed. */
2572
+ errno = err_win_to_posix (GetLastError ());
2573
+ return -1 ;
2513
2574
}
2514
2575
2515
2576
#ifndef _WINNT_H
0 commit comments