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