@@ -895,7 +895,7 @@ int mingw_lstat(const char *file_name, struct stat *buf)
895895 buf -> st_uid = 0 ;
896896 buf -> st_nlink = 1 ;
897897 buf -> st_mode = file_attr_to_st_mode (fdata .dwFileAttributes ,
898- findbuf .dwReserved0 );
898+ findbuf .dwReserved0 , file_name );
899899 buf -> st_size = S_ISLNK (buf -> st_mode ) ? MAX_LONG_PATH :
900900 fdata .nFileSizeLow | (((off_t ) fdata .nFileSizeHigh ) << 32 );
901901 buf -> st_dev = buf -> st_rdev = 0 ; /* not used by Git */
@@ -946,7 +946,7 @@ static int get_file_info_by_handle(HANDLE hnd, struct stat *buf)
946946 buf -> st_gid = 0 ;
947947 buf -> st_uid = 0 ;
948948 buf -> st_nlink = 1 ;
949- buf -> st_mode = file_attr_to_st_mode (fdata .dwFileAttributes , 0 );
949+ buf -> st_mode = file_attr_to_st_mode (fdata .dwFileAttributes , 0 , NULL );
950950 buf -> st_size = fdata .nFileSizeLow |
951951 (((off_t )fdata .nFileSizeHigh )<<32 );
952952 buf -> st_dev = buf -> st_rdev = 0 ; /* not used by Git */
@@ -2039,6 +2039,13 @@ int mingw_rename(const char *pold, const char *pnew)
20392039 return 0 ;
20402040 gle = GetLastError ();
20412041
2042+ if (gle == ERROR_ACCESS_DENIED && is_inside_windows_container ()) {
2043+ /* Fall back to copy to destination & remove source */
2044+ if (CopyFileW (wpold , wpnew , FALSE) && !mingw_unlink (pold ))
2045+ return 0 ;
2046+ gle = GetLastError ();
2047+ }
2048+
20422049 /* revert file attributes on failure */
20432050 if (attrs != INVALID_FILE_ATTRIBUTES )
20442051 SetFileAttributesW (wpnew , attrs );
@@ -2975,3 +2982,62 @@ const char *program_data_config(void)
29752982 }
29762983 return * path .buf ? path .buf : NULL ;
29772984}
2985+
2986+ /*
2987+ * Based on https://stackoverflow.com/questions/43002803
2988+ *
2989+ * [HKLM\SYSTEM\CurrentControlSet\Services\cexecsvc]
2990+ * "DisplayName"="@%systemroot%\\system32\\cexecsvc.exe,-100"
2991+ * "ErrorControl"=dword:00000001
2992+ * "ImagePath"=hex(2):25,00,73,00,79,00,73,00,74,00,65,00,6d,00,72,00,6f,00,
2993+ * 6f,00,74,00,25,00,5c,00,73,00,79,00,73,00,74,00,65,00,6d,00,33,00,32,00,
2994+ * 5c,00,63,00,65,00,78,00,65,00,63,00,73,00,76,00,63,00,2e,00,65,00,78,00,
2995+ * 65,00,00,00
2996+ * "Start"=dword:00000002
2997+ * "Type"=dword:00000010
2998+ * "Description"="@%systemroot%\\system32\\cexecsvc.exe,-101"
2999+ * "ObjectName"="LocalSystem"
3000+ * "ServiceSidType"=dword:00000001
3001+ */
3002+ int is_inside_windows_container (void )
3003+ {
3004+ static int inside_container = -1 ; /* -1 uninitialized */
3005+ const char * key = "SYSTEM\\CurrentControlSet\\Services\\cexecsvc" ;
3006+ HKEY handle = NULL ;
3007+
3008+ if (inside_container != -1 )
3009+ return inside_container ;
3010+
3011+ inside_container = ERROR_SUCCESS ==
3012+ RegOpenKeyExA (HKEY_LOCAL_MACHINE , key , 0 , KEY_READ , & handle );
3013+ RegCloseKey (handle );
3014+
3015+ return inside_container ;
3016+ }
3017+
3018+ int file_attr_to_st_mode (DWORD attr , DWORD tag , const char * path )
3019+ {
3020+ int fMode = S_IREAD ;
3021+ if ((attr & FILE_ATTRIBUTE_REPARSE_POINT ) &&
3022+ tag == IO_REPARSE_TAG_SYMLINK ) {
3023+ int flag = S_IFLNK ;
3024+ char buf [MAX_LONG_PATH ];
3025+
3026+ /*
3027+ * Windows containers' mapped volumes are marked as reparse
3028+ * points and look like symbolic links, but they are not.
3029+ */
3030+ if (path && is_inside_windows_container () &&
3031+ readlink (path , buf , sizeof (buf )) > 27 &&
3032+ starts_with (buf , "/ContainerMappedDirectories/" ))
3033+ flag = S_IFDIR ;
3034+
3035+ fMode |= flag ;
3036+ } else if (attr & FILE_ATTRIBUTE_DIRECTORY )
3037+ fMode |= S_IFDIR ;
3038+ else
3039+ fMode |= S_IFREG ;
3040+ if (!(attr & FILE_ATTRIBUTE_READONLY ))
3041+ fMode |= S_IWRITE ;
3042+ return fMode ;
3043+ }
0 commit comments