@@ -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 */
@@ -2251,6 +2251,13 @@ int mingw_rename(const char *pold, const char *pnew)
22512251 return 0 ;
22522252 gle = GetLastError ();
22532253
2254+ if (gle == ERROR_ACCESS_DENIED && is_inside_windows_container ()) {
2255+ /* Fall back to copy to destination & remove source */
2256+ if (CopyFileW (wpold , wpnew , FALSE) && !mingw_unlink (pold ))
2257+ return 0 ;
2258+ gle = GetLastError ();
2259+ }
2260+
22542261 /* revert file attributes on failure */
22552262 if (attrs != INVALID_FILE_ATTRIBUTES )
22562263 SetFileAttributesW (wpnew , attrs );
@@ -3227,3 +3234,62 @@ const char *program_data_config(void)
32273234 }
32283235 return * path .buf ? path .buf : NULL ;
32293236}
3237+
3238+ /*
3239+ * Based on https://stackoverflow.com/questions/43002803
3240+ *
3241+ * [HKLM\SYSTEM\CurrentControlSet\Services\cexecsvc]
3242+ * "DisplayName"="@%systemroot%\\system32\\cexecsvc.exe,-100"
3243+ * "ErrorControl"=dword:00000001
3244+ * "ImagePath"=hex(2):25,00,73,00,79,00,73,00,74,00,65,00,6d,00,72,00,6f,00,
3245+ * 6f,00,74,00,25,00,5c,00,73,00,79,00,73,00,74,00,65,00,6d,00,33,00,32,00,
3246+ * 5c,00,63,00,65,00,78,00,65,00,63,00,73,00,76,00,63,00,2e,00,65,00,78,00,
3247+ * 65,00,00,00
3248+ * "Start"=dword:00000002
3249+ * "Type"=dword:00000010
3250+ * "Description"="@%systemroot%\\system32\\cexecsvc.exe,-101"
3251+ * "ObjectName"="LocalSystem"
3252+ * "ServiceSidType"=dword:00000001
3253+ */
3254+ int is_inside_windows_container (void )
3255+ {
3256+ static int inside_container = -1 ; /* -1 uninitialized */
3257+ const char * key = "SYSTEM\\CurrentControlSet\\Services\\cexecsvc" ;
3258+ HKEY handle = NULL ;
3259+
3260+ if (inside_container != -1 )
3261+ return inside_container ;
3262+
3263+ inside_container = ERROR_SUCCESS ==
3264+ RegOpenKeyExA (HKEY_LOCAL_MACHINE , key , 0 , KEY_READ , & handle );
3265+ RegCloseKey (handle );
3266+
3267+ return inside_container ;
3268+ }
3269+
3270+ int file_attr_to_st_mode (DWORD attr , DWORD tag , const char * path )
3271+ {
3272+ int fMode = S_IREAD ;
3273+ if ((attr & FILE_ATTRIBUTE_REPARSE_POINT ) &&
3274+ tag == IO_REPARSE_TAG_SYMLINK ) {
3275+ int flag = S_IFLNK ;
3276+ char buf [MAX_LONG_PATH ];
3277+
3278+ /*
3279+ * Windows containers' mapped volumes are marked as reparse
3280+ * points and look like symbolic links, but they are not.
3281+ */
3282+ if (path && is_inside_windows_container () &&
3283+ readlink (path , buf , sizeof (buf )) > 27 &&
3284+ starts_with (buf , "/ContainerMappedDirectories/" ))
3285+ flag = S_IFDIR ;
3286+
3287+ fMode |= flag ;
3288+ } else if (attr & FILE_ATTRIBUTE_DIRECTORY )
3289+ fMode |= S_IFDIR ;
3290+ else
3291+ fMode |= S_IFREG ;
3292+ if (!(attr & FILE_ATTRIBUTE_READONLY ))
3293+ fMode |= S_IWRITE ;
3294+ return fMode ;
3295+ }
0 commit comments