@@ -1054,7 +1054,7 @@ int mingw_lstat(const char *file_name, struct stat *buf)
10541054 buf -> st_uid = 0 ;
10551055 buf -> st_nlink = 1 ;
10561056 buf -> st_mode = file_attr_to_st_mode (fdata .dwFileAttributes ,
1057- reparse_tag );
1057+ reparse_tag , file_name );
10581058 buf -> st_size = S_ISLNK (buf -> st_mode ) ? link_len :
10591059 fdata .nFileSizeLow | (((off_t ) fdata .nFileSizeHigh ) << 32 );
10601060 buf -> st_dev = buf -> st_rdev = 0 ; /* not used by Git */
@@ -1105,7 +1105,7 @@ static int get_file_info_by_handle(HANDLE hnd, struct stat *buf)
11051105 buf -> st_gid = 0 ;
11061106 buf -> st_uid = 0 ;
11071107 buf -> st_nlink = 1 ;
1108- buf -> st_mode = file_attr_to_st_mode (fdata .dwFileAttributes , 0 );
1108+ buf -> st_mode = file_attr_to_st_mode (fdata .dwFileAttributes , 0 , NULL );
11091109 buf -> st_size = fdata .nFileSizeLow |
11101110 (((off_t )fdata .nFileSizeHigh )<<32 );
11111111 buf -> st_dev = buf -> st_rdev = 0 ; /* not used by Git */
@@ -2591,6 +2591,13 @@ int mingw_rename(const char *pold, const char *pnew)
25912591 return 0 ;
25922592 gle = GetLastError ();
25932593
2594+ if (gle == ERROR_ACCESS_DENIED && is_inside_windows_container ()) {
2595+ /* Fall back to copy to destination & remove source */
2596+ if (CopyFileW (wpold , wpnew , FALSE) && !mingw_unlink (pold ))
2597+ return 0 ;
2598+ gle = GetLastError ();
2599+ }
2600+
25942601 /* revert file attributes on failure */
25952602 if (attrs != INVALID_FILE_ATTRIBUTES )
25962603 SetFileAttributesW (wpnew , attrs );
@@ -3997,3 +4004,62 @@ int mingw_have_unix_sockets(void)
39974004 return ret ;
39984005}
39994006#endif
4007+
4008+ /*
4009+ * Based on https://stackoverflow.com/questions/43002803
4010+ *
4011+ * [HKLM\SYSTEM\CurrentControlSet\Services\cexecsvc]
4012+ * "DisplayName"="@%systemroot%\\system32\\cexecsvc.exe,-100"
4013+ * "ErrorControl"=dword:00000001
4014+ * "ImagePath"=hex(2):25,00,73,00,79,00,73,00,74,00,65,00,6d,00,72,00,6f,00,
4015+ * 6f,00,74,00,25,00,5c,00,73,00,79,00,73,00,74,00,65,00,6d,00,33,00,32,00,
4016+ * 5c,00,63,00,65,00,78,00,65,00,63,00,73,00,76,00,63,00,2e,00,65,00,78,00,
4017+ * 65,00,00,00
4018+ * "Start"=dword:00000002
4019+ * "Type"=dword:00000010
4020+ * "Description"="@%systemroot%\\system32\\cexecsvc.exe,-101"
4021+ * "ObjectName"="LocalSystem"
4022+ * "ServiceSidType"=dword:00000001
4023+ */
4024+ int is_inside_windows_container (void )
4025+ {
4026+ static int inside_container = -1 ; /* -1 uninitialized */
4027+ const char * key = "SYSTEM\\CurrentControlSet\\Services\\cexecsvc" ;
4028+ HKEY handle = NULL ;
4029+
4030+ if (inside_container != -1 )
4031+ return inside_container ;
4032+
4033+ inside_container = ERROR_SUCCESS ==
4034+ RegOpenKeyExA (HKEY_LOCAL_MACHINE , key , 0 , KEY_READ , & handle );
4035+ RegCloseKey (handle );
4036+
4037+ return inside_container ;
4038+ }
4039+
4040+ int file_attr_to_st_mode (DWORD attr , DWORD tag , const char * path )
4041+ {
4042+ int fMode = S_IREAD ;
4043+ if ((attr & FILE_ATTRIBUTE_REPARSE_POINT ) &&
4044+ tag == IO_REPARSE_TAG_SYMLINK ) {
4045+ int flag = S_IFLNK ;
4046+ char buf [MAX_LONG_PATH ];
4047+
4048+ /*
4049+ * Windows containers' mapped volumes are marked as reparse
4050+ * points and look like symbolic links, but they are not.
4051+ */
4052+ if (path && is_inside_windows_container () &&
4053+ readlink (path , buf , sizeof (buf )) > 27 &&
4054+ starts_with (buf , "/ContainerMappedDirectories/" ))
4055+ flag = S_IFDIR ;
4056+
4057+ fMode |= flag ;
4058+ } else if (attr & FILE_ATTRIBUTE_DIRECTORY )
4059+ fMode |= S_IFDIR ;
4060+ else
4061+ fMode |= S_IFREG ;
4062+ if (!(attr & FILE_ATTRIBUTE_READONLY ))
4063+ fMode |= S_IWRITE ;
4064+ return fMode ;
4065+ }
0 commit comments