@@ -1147,7 +1147,7 @@ int mingw_lstat(const char *file_name, struct stat *buf)
11471147 buf -> st_uid = 0 ;
11481148 buf -> st_nlink = 1 ;
11491149 buf -> st_mode = file_attr_to_st_mode (fdata .dwFileAttributes ,
1150- reparse_tag );
1150+ reparse_tag , file_name );
11511151 buf -> st_size = S_ISLNK (buf -> st_mode ) ? link_len :
11521152 fdata .nFileSizeLow | (((off_t ) fdata .nFileSizeHigh ) << 32 );
11531153 buf -> st_dev = buf -> st_rdev = 0 ; /* not used by Git */
@@ -1198,7 +1198,7 @@ static int get_file_info_by_handle(HANDLE hnd, struct stat *buf)
11981198 buf -> st_gid = 0 ;
11991199 buf -> st_uid = 0 ;
12001200 buf -> st_nlink = 1 ;
1201- buf -> st_mode = file_attr_to_st_mode (fdata .dwFileAttributes , 0 );
1201+ buf -> st_mode = file_attr_to_st_mode (fdata .dwFileAttributes , 0 , NULL );
12021202 buf -> st_size = fdata .nFileSizeLow |
12031203 (((off_t )fdata .nFileSizeHigh )<<32 );
12041204 buf -> st_dev = buf -> st_rdev = 0 ; /* not used by Git */
@@ -2792,6 +2792,13 @@ int mingw_rename(const char *pold, const char *pnew)
27922792 gle = GetLastError ();
27932793 }
27942794
2795+ if (gle == ERROR_ACCESS_DENIED && is_inside_windows_container ()) {
2796+ /* Fall back to copy to destination & remove source */
2797+ if (CopyFileW (wpold , wpnew , FALSE) && !mingw_unlink (pold ))
2798+ return 0 ;
2799+ gle = GetLastError ();
2800+ }
2801+
27952802 /* revert file attributes on failure */
27962803 if (attrs != INVALID_FILE_ATTRIBUTES )
27972804 SetFileAttributesW (wpnew , attrs );
@@ -4219,3 +4226,62 @@ int mingw_have_unix_sockets(void)
42194226 return ret ;
42204227}
42214228#endif
4229+
4230+ /*
4231+ * Based on https://stackoverflow.com/questions/43002803
4232+ *
4233+ * [HKLM\SYSTEM\CurrentControlSet\Services\cexecsvc]
4234+ * "DisplayName"="@%systemroot%\\system32\\cexecsvc.exe,-100"
4235+ * "ErrorControl"=dword:00000001
4236+ * "ImagePath"=hex(2):25,00,73,00,79,00,73,00,74,00,65,00,6d,00,72,00,6f,00,
4237+ * 6f,00,74,00,25,00,5c,00,73,00,79,00,73,00,74,00,65,00,6d,00,33,00,32,00,
4238+ * 5c,00,63,00,65,00,78,00,65,00,63,00,73,00,76,00,63,00,2e,00,65,00,78,00,
4239+ * 65,00,00,00
4240+ * "Start"=dword:00000002
4241+ * "Type"=dword:00000010
4242+ * "Description"="@%systemroot%\\system32\\cexecsvc.exe,-101"
4243+ * "ObjectName"="LocalSystem"
4244+ * "ServiceSidType"=dword:00000001
4245+ */
4246+ int is_inside_windows_container (void )
4247+ {
4248+ static int inside_container = -1 ; /* -1 uninitialized */
4249+ const char * key = "SYSTEM\\CurrentControlSet\\Services\\cexecsvc" ;
4250+ HKEY handle = NULL ;
4251+
4252+ if (inside_container != -1 )
4253+ return inside_container ;
4254+
4255+ inside_container = ERROR_SUCCESS ==
4256+ RegOpenKeyExA (HKEY_LOCAL_MACHINE , key , 0 , KEY_READ , & handle );
4257+ RegCloseKey (handle );
4258+
4259+ return inside_container ;
4260+ }
4261+
4262+ int file_attr_to_st_mode (DWORD attr , DWORD tag , const char * path )
4263+ {
4264+ int fMode = S_IREAD ;
4265+ if ((attr & FILE_ATTRIBUTE_REPARSE_POINT ) &&
4266+ tag == IO_REPARSE_TAG_SYMLINK ) {
4267+ int flag = S_IFLNK ;
4268+ char buf [MAX_LONG_PATH ];
4269+
4270+ /*
4271+ * Windows containers' mapped volumes are marked as reparse
4272+ * points and look like symbolic links, but they are not.
4273+ */
4274+ if (path && is_inside_windows_container () &&
4275+ readlink (path , buf , sizeof (buf )) > 27 &&
4276+ starts_with (buf , "/ContainerMappedDirectories/" ))
4277+ flag = S_IFDIR ;
4278+
4279+ fMode |= flag ;
4280+ } else if (attr & FILE_ATTRIBUTE_DIRECTORY )
4281+ fMode |= S_IFDIR ;
4282+ else
4283+ fMode |= S_IFREG ;
4284+ if (!(attr & FILE_ATTRIBUTE_READONLY ))
4285+ fMode |= S_IWRITE ;
4286+ return fMode ;
4287+ }
0 commit comments