@@ -906,7 +906,7 @@ int mingw_lstat(const char *file_name, struct stat *buf)
906906 buf -> st_uid = 0 ;
907907 buf -> st_nlink = 1 ;
908908 buf -> st_mode = file_attr_to_st_mode (fdata .dwFileAttributes ,
909- findbuf .dwReserved0 );
909+ findbuf .dwReserved0 , file_name );
910910 buf -> st_size = S_ISLNK (buf -> st_mode ) ? MAX_LONG_PATH :
911911 fdata .nFileSizeLow | (((off_t ) fdata .nFileSizeHigh ) << 32 );
912912 buf -> st_dev = buf -> st_rdev = 0 ; /* not used by Git */
@@ -957,7 +957,7 @@ static int get_file_info_by_handle(HANDLE hnd, struct stat *buf)
957957 buf -> st_gid = 0 ;
958958 buf -> st_uid = 0 ;
959959 buf -> st_nlink = 1 ;
960- buf -> st_mode = file_attr_to_st_mode (fdata .dwFileAttributes , 0 );
960+ buf -> st_mode = file_attr_to_st_mode (fdata .dwFileAttributes , 0 , NULL );
961961 buf -> st_size = fdata .nFileSizeLow |
962962 (((off_t )fdata .nFileSizeHigh )<<32 );
963963 buf -> st_dev = buf -> st_rdev = 0 ; /* not used by Git */
@@ -2172,6 +2172,13 @@ int mingw_rename(const char *pold, const char *pnew)
21722172 return 0 ;
21732173 gle = GetLastError ();
21742174
2175+ if (gle == ERROR_ACCESS_DENIED && is_inside_windows_container ()) {
2176+ /* Fall back to copy to destination & remove source */
2177+ if (CopyFileW (wpold , wpnew , FALSE) && !mingw_unlink (pold ))
2178+ return 0 ;
2179+ gle = GetLastError ();
2180+ }
2181+
21752182 /* revert file attributes on failure */
21762183 if (attrs != INVALID_FILE_ATTRIBUTES )
21772184 SetFileAttributesW (wpnew , attrs );
@@ -3120,3 +3127,62 @@ const char *program_data_config(void)
31203127 }
31213128 return * path .buf ? path .buf : NULL ;
31223129}
3130+
3131+ /*
3132+ * Based on https://stackoverflow.com/questions/43002803
3133+ *
3134+ * [HKLM\SYSTEM\CurrentControlSet\Services\cexecsvc]
3135+ * "DisplayName"="@%systemroot%\\system32\\cexecsvc.exe,-100"
3136+ * "ErrorControl"=dword:00000001
3137+ * "ImagePath"=hex(2):25,00,73,00,79,00,73,00,74,00,65,00,6d,00,72,00,6f,00,
3138+ * 6f,00,74,00,25,00,5c,00,73,00,79,00,73,00,74,00,65,00,6d,00,33,00,32,00,
3139+ * 5c,00,63,00,65,00,78,00,65,00,63,00,73,00,76,00,63,00,2e,00,65,00,78,00,
3140+ * 65,00,00,00
3141+ * "Start"=dword:00000002
3142+ * "Type"=dword:00000010
3143+ * "Description"="@%systemroot%\\system32\\cexecsvc.exe,-101"
3144+ * "ObjectName"="LocalSystem"
3145+ * "ServiceSidType"=dword:00000001
3146+ */
3147+ int is_inside_windows_container (void )
3148+ {
3149+ static int inside_container = -1 ; /* -1 uninitialized */
3150+ const char * key = "SYSTEM\\CurrentControlSet\\Services\\cexecsvc" ;
3151+ HKEY handle = NULL ;
3152+
3153+ if (inside_container != -1 )
3154+ return inside_container ;
3155+
3156+ inside_container = ERROR_SUCCESS ==
3157+ RegOpenKeyExA (HKEY_LOCAL_MACHINE , key , 0 , KEY_READ , & handle );
3158+ RegCloseKey (handle );
3159+
3160+ return inside_container ;
3161+ }
3162+
3163+ int file_attr_to_st_mode (DWORD attr , DWORD tag , const char * path )
3164+ {
3165+ int fMode = S_IREAD ;
3166+ if ((attr & FILE_ATTRIBUTE_REPARSE_POINT ) &&
3167+ tag == IO_REPARSE_TAG_SYMLINK ) {
3168+ int flag = S_IFLNK ;
3169+ char buf [MAX_LONG_PATH ];
3170+
3171+ /*
3172+ * Windows containers' mapped volumes are marked as reparse
3173+ * points and look like symbolic links, but they are not.
3174+ */
3175+ if (path && is_inside_windows_container () &&
3176+ readlink (path , buf , sizeof (buf )) > 27 &&
3177+ starts_with (buf , "/ContainerMappedDirectories/" ))
3178+ flag = S_IFDIR ;
3179+
3180+ fMode |= flag ;
3181+ } else if (attr & FILE_ATTRIBUTE_DIRECTORY )
3182+ fMode |= S_IFDIR ;
3183+ else
3184+ fMode |= S_IFREG ;
3185+ if (!(attr & FILE_ATTRIBUTE_READONLY ))
3186+ fMode |= S_IWRITE ;
3187+ return fMode ;
3188+ }
0 commit comments