9
9
#include "win32/lazyload.h"
10
10
#include "../config.h"
11
11
#include "dir.h"
12
+ #include "../attr.h"
12
13
13
14
#define HCAST (type , handle ) ((type)(intptr_t)handle)
14
15
@@ -413,6 +414,54 @@ static void process_phantom_symlinks(void)
413
414
LeaveCriticalSection (& phantom_symlinks_cs );
414
415
}
415
416
417
+ static int create_phantom_symlink (wchar_t * wtarget , wchar_t * wlink )
418
+ {
419
+ int len ;
420
+
421
+ /* create file symlink */
422
+ if (!CreateSymbolicLinkW (wlink , wtarget , symlink_file_flags )) {
423
+ errno = err_win_to_posix (GetLastError ());
424
+ return -1 ;
425
+ }
426
+
427
+ /* convert to directory symlink if target exists */
428
+ switch (process_phantom_symlink (wtarget , wlink )) {
429
+ case PHANTOM_SYMLINK_RETRY : {
430
+ /* if target doesn't exist, add to phantom symlinks list */
431
+ wchar_t wfullpath [MAX_LONG_PATH ];
432
+ struct phantom_symlink_info * psi ;
433
+
434
+ /* convert to absolute path to be independent of cwd */
435
+ len = GetFullPathNameW (wlink , MAX_LONG_PATH , wfullpath , NULL );
436
+ if (!len || len >= MAX_LONG_PATH ) {
437
+ errno = err_win_to_posix (GetLastError ());
438
+ return -1 ;
439
+ }
440
+
441
+ /* over-allocate and fill phantom_symlink_info structure */
442
+ psi = xmalloc (sizeof (struct phantom_symlink_info ) +
443
+ sizeof (wchar_t ) * (len + wcslen (wtarget ) + 2 ));
444
+ psi -> wlink = (wchar_t * )(psi + 1 );
445
+ wcscpy (psi -> wlink , wfullpath );
446
+ psi -> wtarget = psi -> wlink + len + 1 ;
447
+ wcscpy (psi -> wtarget , wtarget );
448
+
449
+ EnterCriticalSection (& phantom_symlinks_cs );
450
+ psi -> next = phantom_symlinks ;
451
+ phantom_symlinks = psi ;
452
+ LeaveCriticalSection (& phantom_symlinks_cs );
453
+ break ;
454
+ }
455
+ case PHANTOM_SYMLINK_DIRECTORY :
456
+ /* if we created a dir symlink, process other phantom symlinks */
457
+ process_phantom_symlinks ();
458
+ break ;
459
+ default :
460
+ break ;
461
+ }
462
+ return 0 ;
463
+ }
464
+
416
465
/* Normalizes NT paths as returned by some low-level APIs. */
417
466
static wchar_t * normalize_ntpath (wchar_t * wbuf )
418
467
{
@@ -2333,6 +2382,33 @@ int link(const char *oldpath, const char *newpath)
2333
2382
return 0 ;
2334
2383
}
2335
2384
2385
+ enum symlink_type {
2386
+ SYMLINK_TYPE_UNSPECIFIED = 0 ,
2387
+ SYMLINK_TYPE_FILE ,
2388
+ SYMLINK_TYPE_DIRECTORY ,
2389
+ };
2390
+
2391
+ static enum symlink_type check_symlink_attr (const char * link )
2392
+ {
2393
+ static struct attr_check * check ;
2394
+ const char * value ;
2395
+
2396
+ if (!check )
2397
+ check = attr_check_initl ("symlink" , NULL );
2398
+
2399
+ git_check_attr (& the_index , link , check );
2400
+
2401
+ value = check -> items [0 ].value ;
2402
+ if (value == NULL )
2403
+ ;
2404
+ else if (!strcmp (value , "file" ))
2405
+ return SYMLINK_TYPE_FILE ;
2406
+ else if (!strcmp (value , "dir" ))
2407
+ return SYMLINK_TYPE_DIRECTORY ;
2408
+
2409
+ return SYMLINK_TYPE_UNSPECIFIED ;
2410
+ }
2411
+
2336
2412
int symlink (const char * target , const char * link )
2337
2413
{
2338
2414
wchar_t wtarget [MAX_LONG_PATH ], wlink [MAX_LONG_PATH ];
@@ -2353,48 +2429,31 @@ int symlink(const char *target, const char *link)
2353
2429
if (wtarget [len ] == '/' )
2354
2430
wtarget [len ] = '\\' ;
2355
2431
2356
- /* create file symlink */
2357
- if (!CreateSymbolicLinkW (wlink , wtarget , symlink_file_flags )) {
2358
- errno = err_win_to_posix (GetLastError ());
2359
- return -1 ;
2360
- }
2361
-
2362
- /* convert to directory symlink if target exists */
2363
- switch (process_phantom_symlink (wtarget , wlink )) {
2364
- case PHANTOM_SYMLINK_RETRY : {
2365
- /* if target doesn't exist, add to phantom symlinks list */
2366
- wchar_t wfullpath [MAX_LONG_PATH ];
2367
- struct phantom_symlink_info * psi ;
2368
-
2369
- /* convert to absolute path to be independent of cwd */
2370
- len = GetFullPathNameW (wlink , MAX_LONG_PATH , wfullpath , NULL );
2371
- if (!len || len >= MAX_LONG_PATH ) {
2372
- errno = err_win_to_posix (GetLastError ());
2373
- return -1 ;
2374
- }
2375
-
2376
- /* over-allocate and fill phantom_symlink_info structure */
2377
- psi = xmalloc (sizeof (struct phantom_symlink_info )
2378
- + sizeof (wchar_t ) * (len + wcslen (wtarget ) + 2 ));
2379
- psi -> wlink = (wchar_t * )(psi + 1 );
2380
- wcscpy (psi -> wlink , wfullpath );
2381
- psi -> wtarget = psi -> wlink + len + 1 ;
2382
- wcscpy (psi -> wtarget , wtarget );
2383
-
2384
- EnterCriticalSection (& phantom_symlinks_cs );
2385
- psi -> next = phantom_symlinks ;
2386
- phantom_symlinks = psi ;
2387
- LeaveCriticalSection (& phantom_symlinks_cs );
2388
- break ;
2389
- }
2390
- case PHANTOM_SYMLINK_DIRECTORY :
2391
- /* if we created a dir symlink, process other phantom symlinks */
2432
+ switch (check_symlink_attr (link )) {
2433
+ case SYMLINK_TYPE_UNSPECIFIED :
2434
+ /* Create a phantom symlink: it is initially created as a file
2435
+ * symlink, but may change to a directory symlink later if/when
2436
+ * the target exists. */
2437
+ return create_phantom_symlink (wtarget , wlink );
2438
+ case SYMLINK_TYPE_FILE :
2439
+ if (!CreateSymbolicLinkW (wlink , wtarget , symlink_file_flags ))
2440
+ break ;
2441
+ return 0 ;
2442
+ case SYMLINK_TYPE_DIRECTORY :
2443
+ if (!CreateSymbolicLinkW (wlink , wtarget ,
2444
+ symlink_directory_flags ))
2445
+ break ;
2446
+ /* There may be dangling phantom symlinks that point at this
2447
+ * one, which should now morph into directory symlinks. */
2392
2448
process_phantom_symlinks ();
2393
- break ;
2449
+ return 0 ;
2394
2450
default :
2395
- break ;
2451
+ BUG ( "unhandled symlink type" ) ;
2396
2452
}
2397
- return 0 ;
2453
+
2454
+ /* CreateSymbolicLinkW failed. */
2455
+ errno = err_win_to_posix (GetLastError ());
2456
+ return -1 ;
2398
2457
}
2399
2458
2400
2459
#ifndef _WINNT_H
0 commit comments