@@ -944,7 +944,7 @@ int mingw_lstat(const char *file_name, struct stat *buf)
944
944
buf -> st_uid = 0 ;
945
945
buf -> st_nlink = 1 ;
946
946
buf -> st_mode = file_attr_to_st_mode (fdata .dwFileAttributes ,
947
- findbuf .dwReserved0 );
947
+ findbuf .dwReserved0 , file_name );
948
948
buf -> st_size = S_ISLNK (buf -> st_mode ) ? MAX_LONG_PATH :
949
949
fdata .nFileSizeLow | (((off_t ) fdata .nFileSizeHigh ) << 32 );
950
950
buf -> st_dev = buf -> st_rdev = 0 ; /* not used by Git */
@@ -995,7 +995,7 @@ static int get_file_info_by_handle(HANDLE hnd, struct stat *buf)
995
995
buf -> st_gid = 0 ;
996
996
buf -> st_uid = 0 ;
997
997
buf -> st_nlink = 1 ;
998
- buf -> st_mode = file_attr_to_st_mode (fdata .dwFileAttributes , 0 );
998
+ buf -> st_mode = file_attr_to_st_mode (fdata .dwFileAttributes , 0 , NULL );
999
999
buf -> st_size = fdata .nFileSizeLow |
1000
1000
(((off_t )fdata .nFileSizeHigh )<<32 );
1001
1001
buf -> st_dev = buf -> st_rdev = 0 ; /* not used by Git */
@@ -2360,6 +2360,13 @@ int mingw_rename(const char *pold, const char *pnew)
2360
2360
return 0 ;
2361
2361
gle = GetLastError ();
2362
2362
2363
+ if (gle == ERROR_ACCESS_DENIED && is_inside_windows_container ()) {
2364
+ /* Fall back to copy to destination & remove source */
2365
+ if (CopyFileW (wpold , wpnew , FALSE) && !mingw_unlink (pold ))
2366
+ return 0 ;
2367
+ gle = GetLastError ();
2368
+ }
2369
+
2363
2370
/* revert file attributes on failure */
2364
2371
if (attrs != INVALID_FILE_ATTRIBUTES )
2365
2372
SetFileAttributesW (wpnew , attrs );
@@ -3439,3 +3446,62 @@ int uname(struct utsname *buf)
3439
3446
"%u" , (v >> 16 ) & 0x7fff );
3440
3447
return 0 ;
3441
3448
}
3449
+
3450
+ /*
3451
+ * Based on https://stackoverflow.com/questions/43002803
3452
+ *
3453
+ * [HKLM\SYSTEM\CurrentControlSet\Services\cexecsvc]
3454
+ * "DisplayName"="@%systemroot%\\system32\\cexecsvc.exe,-100"
3455
+ * "ErrorControl"=dword:00000001
3456
+ * "ImagePath"=hex(2):25,00,73,00,79,00,73,00,74,00,65,00,6d,00,72,00,6f,00,
3457
+ * 6f,00,74,00,25,00,5c,00,73,00,79,00,73,00,74,00,65,00,6d,00,33,00,32,00,
3458
+ * 5c,00,63,00,65,00,78,00,65,00,63,00,73,00,76,00,63,00,2e,00,65,00,78,00,
3459
+ * 65,00,00,00
3460
+ * "Start"=dword:00000002
3461
+ * "Type"=dword:00000010
3462
+ * "Description"="@%systemroot%\\system32\\cexecsvc.exe,-101"
3463
+ * "ObjectName"="LocalSystem"
3464
+ * "ServiceSidType"=dword:00000001
3465
+ */
3466
+ int is_inside_windows_container (void )
3467
+ {
3468
+ static int inside_container = -1 ; /* -1 uninitialized */
3469
+ const char * key = "SYSTEM\\CurrentControlSet\\Services\\cexecsvc" ;
3470
+ HKEY handle = NULL ;
3471
+
3472
+ if (inside_container != -1 )
3473
+ return inside_container ;
3474
+
3475
+ inside_container = ERROR_SUCCESS ==
3476
+ RegOpenKeyExA (HKEY_LOCAL_MACHINE , key , 0 , KEY_READ , & handle );
3477
+ RegCloseKey (handle );
3478
+
3479
+ return inside_container ;
3480
+ }
3481
+
3482
+ int file_attr_to_st_mode (DWORD attr , DWORD tag , const char * path )
3483
+ {
3484
+ int fMode = S_IREAD ;
3485
+ if ((attr & FILE_ATTRIBUTE_REPARSE_POINT ) &&
3486
+ tag == IO_REPARSE_TAG_SYMLINK ) {
3487
+ int flag = S_IFLNK ;
3488
+ char buf [MAX_LONG_PATH ];
3489
+
3490
+ /*
3491
+ * Windows containers' mapped volumes are marked as reparse
3492
+ * points and look like symbolic links, but they are not.
3493
+ */
3494
+ if (path && is_inside_windows_container () &&
3495
+ readlink (path , buf , sizeof (buf )) > 27 &&
3496
+ starts_with (buf , "/ContainerMappedDirectories/" ))
3497
+ flag = S_IFDIR ;
3498
+
3499
+ fMode |= flag ;
3500
+ } else if (attr & FILE_ATTRIBUTE_DIRECTORY )
3501
+ fMode |= S_IFDIR ;
3502
+ else
3503
+ fMode |= S_IFREG ;
3504
+ if (!(attr & FILE_ATTRIBUTE_READONLY ))
3505
+ fMode |= S_IWRITE ;
3506
+ return fMode ;
3507
+ }
0 commit comments