@@ -946,7 +946,7 @@ int mingw_lstat(const char *file_name, struct stat *buf)
946946 buf -> st_uid = 0 ;
947947 buf -> st_nlink = 1 ;
948948 buf -> st_mode = file_attr_to_st_mode (fdata .dwFileAttributes ,
949- findbuf .dwReserved0 );
949+ findbuf .dwReserved0 , file_name );
950950 buf -> st_size = S_ISLNK (buf -> st_mode ) ? MAX_LONG_PATH :
951951 fdata .nFileSizeLow | (((off_t ) fdata .nFileSizeHigh ) << 32 );
952952 buf -> st_dev = buf -> st_rdev = 0 ; /* not used by Git */
@@ -997,7 +997,7 @@ static int get_file_info_by_handle(HANDLE hnd, struct stat *buf)
997997 buf -> st_gid = 0 ;
998998 buf -> st_uid = 0 ;
999999 buf -> st_nlink = 1 ;
1000- buf -> st_mode = file_attr_to_st_mode (fdata .dwFileAttributes , 0 );
1000+ buf -> st_mode = file_attr_to_st_mode (fdata .dwFileAttributes , 0 , NULL );
10011001 buf -> st_size = fdata .nFileSizeLow |
10021002 (((off_t )fdata .nFileSizeHigh )<<32 );
10031003 buf -> st_dev = buf -> st_rdev = 0 ; /* not used by Git */
@@ -2265,6 +2265,13 @@ int mingw_rename(const char *pold, const char *pnew)
22652265 return 0 ;
22662266 gle = GetLastError ();
22672267
2268+ if (gle == ERROR_ACCESS_DENIED && is_inside_windows_container ()) {
2269+ /* Fall back to copy to destination & remove source */
2270+ if (CopyFileW (wpold , wpnew , FALSE) && !mingw_unlink (pold ))
2271+ return 0 ;
2272+ gle = GetLastError ();
2273+ }
2274+
22682275 /* revert file attributes on failure */
22692276 if (attrs != INVALID_FILE_ATTRIBUTES )
22702277 SetFileAttributesW (wpnew , attrs );
@@ -3221,3 +3228,62 @@ const char *program_data_config(void)
32213228 }
32223229 return * path .buf ? path .buf : NULL ;
32233230}
3231+
3232+ /*
3233+ * Based on https://stackoverflow.com/questions/43002803
3234+ *
3235+ * [HKLM\SYSTEM\CurrentControlSet\Services\cexecsvc]
3236+ * "DisplayName"="@%systemroot%\\system32\\cexecsvc.exe,-100"
3237+ * "ErrorControl"=dword:00000001
3238+ * "ImagePath"=hex(2):25,00,73,00,79,00,73,00,74,00,65,00,6d,00,72,00,6f,00,
3239+ * 6f,00,74,00,25,00,5c,00,73,00,79,00,73,00,74,00,65,00,6d,00,33,00,32,00,
3240+ * 5c,00,63,00,65,00,78,00,65,00,63,00,73,00,76,00,63,00,2e,00,65,00,78,00,
3241+ * 65,00,00,00
3242+ * "Start"=dword:00000002
3243+ * "Type"=dword:00000010
3244+ * "Description"="@%systemroot%\\system32\\cexecsvc.exe,-101"
3245+ * "ObjectName"="LocalSystem"
3246+ * "ServiceSidType"=dword:00000001
3247+ */
3248+ int is_inside_windows_container (void )
3249+ {
3250+ static int inside_container = -1 ; /* -1 uninitialized */
3251+ const char * key = "SYSTEM\\CurrentControlSet\\Services\\cexecsvc" ;
3252+ HKEY handle = NULL ;
3253+
3254+ if (inside_container != -1 )
3255+ return inside_container ;
3256+
3257+ inside_container = ERROR_SUCCESS ==
3258+ RegOpenKeyExA (HKEY_LOCAL_MACHINE , key , 0 , KEY_READ , & handle );
3259+ RegCloseKey (handle );
3260+
3261+ return inside_container ;
3262+ }
3263+
3264+ int file_attr_to_st_mode (DWORD attr , DWORD tag , const char * path )
3265+ {
3266+ int fMode = S_IREAD ;
3267+ if ((attr & FILE_ATTRIBUTE_REPARSE_POINT ) &&
3268+ tag == IO_REPARSE_TAG_SYMLINK ) {
3269+ int flag = S_IFLNK ;
3270+ char buf [MAX_LONG_PATH ];
3271+
3272+ /*
3273+ * Windows containers' mapped volumes are marked as reparse
3274+ * points and look like symbolic links, but they are not.
3275+ */
3276+ if (path && is_inside_windows_container () &&
3277+ readlink (path , buf , sizeof (buf )) > 27 &&
3278+ starts_with (buf , "/ContainerMappedDirectories/" ))
3279+ flag = S_IFDIR ;
3280+
3281+ fMode |= flag ;
3282+ } else if (attr & FILE_ATTRIBUTE_DIRECTORY )
3283+ fMode |= S_IFDIR ;
3284+ else
3285+ fMode |= S_IFREG ;
3286+ if (!(attr & FILE_ATTRIBUTE_READONLY ))
3287+ fMode |= S_IWRITE ;
3288+ return fMode ;
3289+ }
0 commit comments