@@ -1030,7 +1030,7 @@ int mingw_lstat(const char *file_name, struct stat *buf)
10301030 buf -> st_uid = 0 ;
10311031 buf -> st_nlink = 1 ;
10321032 buf -> st_mode = file_attr_to_st_mode (fdata .dwFileAttributes ,
1033- reparse_tag );
1033+ reparse_tag , file_name );
10341034 buf -> st_size = S_ISLNK (buf -> st_mode ) ? link_len :
10351035 fdata .nFileSizeLow | (((off_t ) fdata .nFileSizeHigh ) << 32 );
10361036 buf -> st_dev = buf -> st_rdev = 0 ; /* not used by Git */
@@ -1081,7 +1081,7 @@ static int get_file_info_by_handle(HANDLE hnd, struct stat *buf)
10811081 buf -> st_gid = 0 ;
10821082 buf -> st_uid = 0 ;
10831083 buf -> st_nlink = 1 ;
1084- buf -> st_mode = file_attr_to_st_mode (fdata .dwFileAttributes , 0 );
1084+ buf -> st_mode = file_attr_to_st_mode (fdata .dwFileAttributes , 0 , NULL );
10851085 buf -> st_size = fdata .nFileSizeLow |
10861086 (((off_t )fdata .nFileSizeHigh )<<32 );
10871087 buf -> st_dev = buf -> st_rdev = 0 ; /* not used by Git */
@@ -2562,6 +2562,13 @@ int mingw_rename(const char *pold, const char *pnew)
25622562 return 0 ;
25632563 gle = GetLastError ();
25642564
2565+ if (gle == ERROR_ACCESS_DENIED && is_inside_windows_container ()) {
2566+ /* Fall back to copy to destination & remove source */
2567+ if (CopyFileW (wpold , wpnew , FALSE) && !mingw_unlink (pold ))
2568+ return 0 ;
2569+ gle = GetLastError ();
2570+ }
2571+
25652572 /* revert file attributes on failure */
25662573 if (attrs != INVALID_FILE_ATTRIBUTES )
25672574 SetFileAttributesW (wpnew , attrs );
@@ -3894,3 +3901,62 @@ int uname(struct utsname *buf)
38943901 "%u" , (v >> 16 ) & 0x7fff );
38953902 return 0 ;
38963903}
3904+
3905+ /*
3906+ * Based on https://stackoverflow.com/questions/43002803
3907+ *
3908+ * [HKLM\SYSTEM\CurrentControlSet\Services\cexecsvc]
3909+ * "DisplayName"="@%systemroot%\\system32\\cexecsvc.exe,-100"
3910+ * "ErrorControl"=dword:00000001
3911+ * "ImagePath"=hex(2):25,00,73,00,79,00,73,00,74,00,65,00,6d,00,72,00,6f,00,
3912+ * 6f,00,74,00,25,00,5c,00,73,00,79,00,73,00,74,00,65,00,6d,00,33,00,32,00,
3913+ * 5c,00,63,00,65,00,78,00,65,00,63,00,73,00,76,00,63,00,2e,00,65,00,78,00,
3914+ * 65,00,00,00
3915+ * "Start"=dword:00000002
3916+ * "Type"=dword:00000010
3917+ * "Description"="@%systemroot%\\system32\\cexecsvc.exe,-101"
3918+ * "ObjectName"="LocalSystem"
3919+ * "ServiceSidType"=dword:00000001
3920+ */
3921+ int is_inside_windows_container (void )
3922+ {
3923+ static int inside_container = -1 ; /* -1 uninitialized */
3924+ const char * key = "SYSTEM\\CurrentControlSet\\Services\\cexecsvc" ;
3925+ HKEY handle = NULL ;
3926+
3927+ if (inside_container != -1 )
3928+ return inside_container ;
3929+
3930+ inside_container = ERROR_SUCCESS ==
3931+ RegOpenKeyExA (HKEY_LOCAL_MACHINE , key , 0 , KEY_READ , & handle );
3932+ RegCloseKey (handle );
3933+
3934+ return inside_container ;
3935+ }
3936+
3937+ int file_attr_to_st_mode (DWORD attr , DWORD tag , const char * path )
3938+ {
3939+ int fMode = S_IREAD ;
3940+ if ((attr & FILE_ATTRIBUTE_REPARSE_POINT ) &&
3941+ tag == IO_REPARSE_TAG_SYMLINK ) {
3942+ int flag = S_IFLNK ;
3943+ char buf [MAX_LONG_PATH ];
3944+
3945+ /*
3946+ * Windows containers' mapped volumes are marked as reparse
3947+ * points and look like symbolic links, but they are not.
3948+ */
3949+ if (path && is_inside_windows_container () &&
3950+ readlink (path , buf , sizeof (buf )) > 27 &&
3951+ starts_with (buf , "/ContainerMappedDirectories/" ))
3952+ flag = S_IFDIR ;
3953+
3954+ fMode |= flag ;
3955+ } else if (attr & FILE_ATTRIBUTE_DIRECTORY )
3956+ fMode |= S_IFDIR ;
3957+ else
3958+ fMode |= S_IFREG ;
3959+ if (!(attr & FILE_ATTRIBUTE_READONLY ))
3960+ fMode |= S_IWRITE ;
3961+ return fMode ;
3962+ }
0 commit comments