@@ -1273,7 +1273,7 @@ int mingw_lstat(const char *file_name, struct stat *buf)
12731273 buf -> st_uid = 0 ;
12741274 buf -> st_nlink = 1 ;
12751275 buf -> st_mode = file_attr_to_st_mode (fdata .dwFileAttributes ,
1276- reparse_tag );
1276+ reparse_tag , file_name );
12771277 buf -> st_size = S_ISLNK (buf -> st_mode ) ? link_len :
12781278 fdata .nFileSizeLow | (((off_t ) fdata .nFileSizeHigh ) << 32 );
12791279 buf -> st_dev = buf -> st_rdev = 0 ; /* not used by Git */
@@ -1324,7 +1324,7 @@ static int get_file_info_by_handle(HANDLE hnd, struct stat *buf)
13241324 buf -> st_gid = 0 ;
13251325 buf -> st_uid = 0 ;
13261326 buf -> st_nlink = 1 ;
1327- buf -> st_mode = file_attr_to_st_mode (fdata .dwFileAttributes , 0 );
1327+ buf -> st_mode = file_attr_to_st_mode (fdata .dwFileAttributes , 0 , NULL );
13281328 buf -> st_size = fdata .nFileSizeLow |
13291329 (((off_t )fdata .nFileSizeHigh )<<32 );
13301330 buf -> st_dev = buf -> st_rdev = 0 ; /* not used by Git */
@@ -2913,6 +2913,13 @@ int mingw_rename(const char *pold, const char *pnew)
29132913 gle = GetLastError ();
29142914 }
29152915
2916+ if (gle == ERROR_ACCESS_DENIED && is_inside_windows_container ()) {
2917+ /* Fall back to copy to destination & remove source */
2918+ if (CopyFileW (wpold , wpnew , FALSE) && !mingw_unlink (pold , 1 ))
2919+ return 0 ;
2920+ gle = GetLastError ();
2921+ }
2922+
29162923 /* revert file attributes on failure */
29172924 if (attrs != INVALID_FILE_ATTRIBUTES )
29182925 SetFileAttributesW (wpnew , attrs );
@@ -4250,3 +4257,62 @@ int mingw_have_unix_sockets(void)
42504257 return ret ;
42514258}
42524259#endif
4260+
4261+ /*
4262+ * Based on https://stackoverflow.com/questions/43002803
4263+ *
4264+ * [HKLM\SYSTEM\CurrentControlSet\Services\cexecsvc]
4265+ * "DisplayName"="@%systemroot%\\system32\\cexecsvc.exe,-100"
4266+ * "ErrorControl"=dword:00000001
4267+ * "ImagePath"=hex(2):25,00,73,00,79,00,73,00,74,00,65,00,6d,00,72,00,6f,00,
4268+ * 6f,00,74,00,25,00,5c,00,73,00,79,00,73,00,74,00,65,00,6d,00,33,00,32,00,
4269+ * 5c,00,63,00,65,00,78,00,65,00,63,00,73,00,76,00,63,00,2e,00,65,00,78,00,
4270+ * 65,00,00,00
4271+ * "Start"=dword:00000002
4272+ * "Type"=dword:00000010
4273+ * "Description"="@%systemroot%\\system32\\cexecsvc.exe,-101"
4274+ * "ObjectName"="LocalSystem"
4275+ * "ServiceSidType"=dword:00000001
4276+ */
4277+ int is_inside_windows_container (void )
4278+ {
4279+ static int inside_container = -1 ; /* -1 uninitialized */
4280+ const char * key = "SYSTEM\\CurrentControlSet\\Services\\cexecsvc" ;
4281+ HKEY handle = NULL ;
4282+
4283+ if (inside_container != -1 )
4284+ return inside_container ;
4285+
4286+ inside_container = ERROR_SUCCESS ==
4287+ RegOpenKeyExA (HKEY_LOCAL_MACHINE , key , 0 , KEY_READ , & handle );
4288+ RegCloseKey (handle );
4289+
4290+ return inside_container ;
4291+ }
4292+
4293+ int file_attr_to_st_mode (DWORD attr , DWORD tag , const char * path )
4294+ {
4295+ int fMode = S_IREAD ;
4296+ if ((attr & FILE_ATTRIBUTE_REPARSE_POINT ) &&
4297+ tag == IO_REPARSE_TAG_SYMLINK ) {
4298+ int flag = S_IFLNK ;
4299+ char buf [MAX_LONG_PATH ];
4300+
4301+ /*
4302+ * Windows containers' mapped volumes are marked as reparse
4303+ * points and look like symbolic links, but they are not.
4304+ */
4305+ if (path && is_inside_windows_container () &&
4306+ readlink (path , buf , sizeof (buf )) > 27 &&
4307+ starts_with (buf , "/ContainerMappedDirectories/" ))
4308+ flag = S_IFDIR ;
4309+
4310+ fMode |= flag ;
4311+ } else if (attr & FILE_ATTRIBUTE_DIRECTORY )
4312+ fMode |= S_IFDIR ;
4313+ else
4314+ fMode |= S_IFREG ;
4315+ if (!(attr & FILE_ATTRIBUTE_READONLY ))
4316+ fMode |= S_IWRITE ;
4317+ return fMode ;
4318+ }
0 commit comments