@@ -896,7 +896,7 @@ int mingw_lstat(const char *file_name, struct stat *buf)
896896 buf -> st_uid = 0 ;
897897 buf -> st_nlink = 1 ;
898898 buf -> st_mode = file_attr_to_st_mode (fdata .dwFileAttributes ,
899- findbuf .dwReserved0 );
899+ findbuf .dwReserved0 , file_name );
900900 buf -> st_size = S_ISLNK (buf -> st_mode ) ? MAX_LONG_PATH :
901901 fdata .nFileSizeLow | (((off_t ) fdata .nFileSizeHigh ) << 32 );
902902 buf -> st_dev = buf -> st_rdev = 0 ; /* not used by Git */
@@ -947,7 +947,7 @@ static int get_file_info_by_handle(HANDLE hnd, struct stat *buf)
947947 buf -> st_gid = 0 ;
948948 buf -> st_uid = 0 ;
949949 buf -> st_nlink = 1 ;
950- buf -> st_mode = file_attr_to_st_mode (fdata .dwFileAttributes , 0 );
950+ buf -> st_mode = file_attr_to_st_mode (fdata .dwFileAttributes , 0 , NULL );
951951 buf -> st_size = fdata .nFileSizeLow |
952952 (((off_t )fdata .nFileSizeHigh )<<32 );
953953 buf -> st_dev = buf -> st_rdev = 0 ; /* not used by Git */
@@ -2152,6 +2152,13 @@ int mingw_rename(const char *pold, const char *pnew)
21522152 return 0 ;
21532153 gle = GetLastError ();
21542154
2155+ if (gle == ERROR_ACCESS_DENIED && is_inside_windows_container ()) {
2156+ /* Fall back to copy to destination & remove source */
2157+ if (CopyFileW (wpold , wpnew , FALSE) && !mingw_unlink (pold ))
2158+ return 0 ;
2159+ gle = GetLastError ();
2160+ }
2161+
21552162 /* revert file attributes on failure */
21562163 if (attrs != INVALID_FILE_ATTRIBUTES )
21572164 SetFileAttributesW (wpnew , attrs );
@@ -3100,3 +3107,62 @@ const char *program_data_config(void)
31003107 }
31013108 return * path .buf ? path .buf : NULL ;
31023109}
3110+
3111+ /*
3112+ * Based on https://stackoverflow.com/questions/43002803
3113+ *
3114+ * [HKLM\SYSTEM\CurrentControlSet\Services\cexecsvc]
3115+ * "DisplayName"="@%systemroot%\\system32\\cexecsvc.exe,-100"
3116+ * "ErrorControl"=dword:00000001
3117+ * "ImagePath"=hex(2):25,00,73,00,79,00,73,00,74,00,65,00,6d,00,72,00,6f,00,
3118+ * 6f,00,74,00,25,00,5c,00,73,00,79,00,73,00,74,00,65,00,6d,00,33,00,32,00,
3119+ * 5c,00,63,00,65,00,78,00,65,00,63,00,73,00,76,00,63,00,2e,00,65,00,78,00,
3120+ * 65,00,00,00
3121+ * "Start"=dword:00000002
3122+ * "Type"=dword:00000010
3123+ * "Description"="@%systemroot%\\system32\\cexecsvc.exe,-101"
3124+ * "ObjectName"="LocalSystem"
3125+ * "ServiceSidType"=dword:00000001
3126+ */
3127+ int is_inside_windows_container (void )
3128+ {
3129+ static int inside_container = -1 ; /* -1 uninitialized */
3130+ const char * key = "SYSTEM\\CurrentControlSet\\Services\\cexecsvc" ;
3131+ HKEY handle = NULL ;
3132+
3133+ if (inside_container != -1 )
3134+ return inside_container ;
3135+
3136+ inside_container = ERROR_SUCCESS ==
3137+ RegOpenKeyExA (HKEY_LOCAL_MACHINE , key , 0 , KEY_READ , & handle );
3138+ RegCloseKey (handle );
3139+
3140+ return inside_container ;
3141+ }
3142+
3143+ int file_attr_to_st_mode (DWORD attr , DWORD tag , const char * path )
3144+ {
3145+ int fMode = S_IREAD ;
3146+ if ((attr & FILE_ATTRIBUTE_REPARSE_POINT ) &&
3147+ tag == IO_REPARSE_TAG_SYMLINK ) {
3148+ int flag = S_IFLNK ;
3149+ char buf [MAX_LONG_PATH ];
3150+
3151+ /*
3152+ * Windows containers' mapped volumes are marked as reparse
3153+ * points and look like symbolic links, but they are not.
3154+ */
3155+ if (path && is_inside_windows_container () &&
3156+ readlink (path , buf , sizeof (buf )) > 27 &&
3157+ starts_with (buf , "/ContainerMappedDirectories/" ))
3158+ flag = S_IFDIR ;
3159+
3160+ fMode |= flag ;
3161+ } else if (attr & FILE_ATTRIBUTE_DIRECTORY )
3162+ fMode |= S_IFDIR ;
3163+ else
3164+ fMode |= S_IFREG ;
3165+ if (!(attr & FILE_ATTRIBUTE_READONLY ))
3166+ fMode |= S_IWRITE ;
3167+ return fMode ;
3168+ }
0 commit comments