@@ -1039,7 +1039,7 @@ int mingw_lstat(const char *file_name, struct stat *buf)
10391039 buf -> st_uid = 0 ;
10401040 buf -> st_nlink = 1 ;
10411041 buf -> st_mode = file_attr_to_st_mode (fdata .dwFileAttributes ,
1042- reparse_tag );
1042+ reparse_tag , file_name );
10431043 buf -> st_size = S_ISLNK (buf -> st_mode ) ? link_len :
10441044 fdata .nFileSizeLow | (((off_t ) fdata .nFileSizeHigh ) << 32 );
10451045 buf -> st_dev = buf -> st_rdev = 0 ; /* not used by Git */
@@ -1090,7 +1090,7 @@ static int get_file_info_by_handle(HANDLE hnd, struct stat *buf)
10901090 buf -> st_gid = 0 ;
10911091 buf -> st_uid = 0 ;
10921092 buf -> st_nlink = 1 ;
1093- buf -> st_mode = file_attr_to_st_mode (fdata .dwFileAttributes , 0 );
1093+ buf -> st_mode = file_attr_to_st_mode (fdata .dwFileAttributes , 0 , NULL );
10941094 buf -> st_size = fdata .nFileSizeLow |
10951095 (((off_t )fdata .nFileSizeHigh )<<32 );
10961096 buf -> st_dev = buf -> st_rdev = 0 ; /* not used by Git */
@@ -2576,6 +2576,13 @@ int mingw_rename(const char *pold, const char *pnew)
25762576 return 0 ;
25772577 gle = GetLastError ();
25782578
2579+ if (gle == ERROR_ACCESS_DENIED && is_inside_windows_container ()) {
2580+ /* Fall back to copy to destination & remove source */
2581+ if (CopyFileW (wpold , wpnew , FALSE) && !mingw_unlink (pold ))
2582+ return 0 ;
2583+ gle = GetLastError ();
2584+ }
2585+
25792586 /* revert file attributes on failure */
25802587 if (attrs != INVALID_FILE_ATTRIBUTES )
25812588 SetFileAttributesW (wpnew , attrs );
@@ -3908,3 +3915,62 @@ int uname(struct utsname *buf)
39083915 "%u" , (v >> 16 ) & 0x7fff );
39093916 return 0 ;
39103917}
3918+
3919+ /*
3920+ * Based on https://stackoverflow.com/questions/43002803
3921+ *
3922+ * [HKLM\SYSTEM\CurrentControlSet\Services\cexecsvc]
3923+ * "DisplayName"="@%systemroot%\\system32\\cexecsvc.exe,-100"
3924+ * "ErrorControl"=dword:00000001
3925+ * "ImagePath"=hex(2):25,00,73,00,79,00,73,00,74,00,65,00,6d,00,72,00,6f,00,
3926+ * 6f,00,74,00,25,00,5c,00,73,00,79,00,73,00,74,00,65,00,6d,00,33,00,32,00,
3927+ * 5c,00,63,00,65,00,78,00,65,00,63,00,73,00,76,00,63,00,2e,00,65,00,78,00,
3928+ * 65,00,00,00
3929+ * "Start"=dword:00000002
3930+ * "Type"=dword:00000010
3931+ * "Description"="@%systemroot%\\system32\\cexecsvc.exe,-101"
3932+ * "ObjectName"="LocalSystem"
3933+ * "ServiceSidType"=dword:00000001
3934+ */
3935+ int is_inside_windows_container (void )
3936+ {
3937+ static int inside_container = -1 ; /* -1 uninitialized */
3938+ const char * key = "SYSTEM\\CurrentControlSet\\Services\\cexecsvc" ;
3939+ HKEY handle = NULL ;
3940+
3941+ if (inside_container != -1 )
3942+ return inside_container ;
3943+
3944+ inside_container = ERROR_SUCCESS ==
3945+ RegOpenKeyExA (HKEY_LOCAL_MACHINE , key , 0 , KEY_READ , & handle );
3946+ RegCloseKey (handle );
3947+
3948+ return inside_container ;
3949+ }
3950+
3951+ int file_attr_to_st_mode (DWORD attr , DWORD tag , const char * path )
3952+ {
3953+ int fMode = S_IREAD ;
3954+ if ((attr & FILE_ATTRIBUTE_REPARSE_POINT ) &&
3955+ tag == IO_REPARSE_TAG_SYMLINK ) {
3956+ int flag = S_IFLNK ;
3957+ char buf [MAX_LONG_PATH ];
3958+
3959+ /*
3960+ * Windows containers' mapped volumes are marked as reparse
3961+ * points and look like symbolic links, but they are not.
3962+ */
3963+ if (path && is_inside_windows_container () &&
3964+ readlink (path , buf , sizeof (buf )) > 27 &&
3965+ starts_with (buf , "/ContainerMappedDirectories/" ))
3966+ flag = S_IFDIR ;
3967+
3968+ fMode |= flag ;
3969+ } else if (attr & FILE_ATTRIBUTE_DIRECTORY )
3970+ fMode |= S_IFDIR ;
3971+ else
3972+ fMode |= S_IFREG ;
3973+ if (!(attr & FILE_ATTRIBUTE_READONLY ))
3974+ fMode |= S_IWRITE ;
3975+ return fMode ;
3976+ }
0 commit comments