@@ -944,7 +944,7 @@ int mingw_lstat(const char *file_name, struct stat *buf)
944944 buf -> st_uid = 0 ;
945945 buf -> st_nlink = 1 ;
946946 buf -> st_mode = file_attr_to_st_mode (fdata .dwFileAttributes ,
947- findbuf .dwReserved0 );
947+ findbuf .dwReserved0 , file_name );
948948 buf -> st_size = S_ISLNK (buf -> st_mode ) ? MAX_LONG_PATH :
949949 fdata .nFileSizeLow | (((off_t ) fdata .nFileSizeHigh ) << 32 );
950950 buf -> st_dev = buf -> st_rdev = 0 ; /* not used by Git */
@@ -995,7 +995,7 @@ static int get_file_info_by_handle(HANDLE hnd, struct stat *buf)
995995 buf -> st_gid = 0 ;
996996 buf -> st_uid = 0 ;
997997 buf -> st_nlink = 1 ;
998- buf -> st_mode = file_attr_to_st_mode (fdata .dwFileAttributes , 0 );
998+ buf -> st_mode = file_attr_to_st_mode (fdata .dwFileAttributes , 0 , NULL );
999999 buf -> st_size = fdata .nFileSizeLow |
10001000 (((off_t )fdata .nFileSizeHigh )<<32 );
10011001 buf -> st_dev = buf -> st_rdev = 0 ; /* not used by Git */
@@ -2088,6 +2088,13 @@ int mingw_rename(const char *pold, const char *pnew)
20882088 return 0 ;
20892089 gle = GetLastError ();
20902090
2091+ if (gle == ERROR_ACCESS_DENIED && is_inside_windows_container ()) {
2092+ /* Fall back to copy to destination & remove source */
2093+ if (CopyFileW (wpold , wpnew , FALSE) && !mingw_unlink (pold ))
2094+ return 0 ;
2095+ gle = GetLastError ();
2096+ }
2097+
20912098 /* revert file attributes on failure */
20922099 if (attrs != INVALID_FILE_ATTRIBUTES )
20932100 SetFileAttributesW (wpnew , attrs );
@@ -3034,3 +3041,62 @@ const char *program_data_config(void)
30343041 }
30353042 return * path .buf ? path .buf : NULL ;
30363043}
3044+
3045+ /*
3046+ * Based on https://stackoverflow.com/questions/43002803
3047+ *
3048+ * [HKLM\SYSTEM\CurrentControlSet\Services\cexecsvc]
3049+ * "DisplayName"="@%systemroot%\\system32\\cexecsvc.exe,-100"
3050+ * "ErrorControl"=dword:00000001
3051+ * "ImagePath"=hex(2):25,00,73,00,79,00,73,00,74,00,65,00,6d,00,72,00,6f,00,
3052+ * 6f,00,74,00,25,00,5c,00,73,00,79,00,73,00,74,00,65,00,6d,00,33,00,32,00,
3053+ * 5c,00,63,00,65,00,78,00,65,00,63,00,73,00,76,00,63,00,2e,00,65,00,78,00,
3054+ * 65,00,00,00
3055+ * "Start"=dword:00000002
3056+ * "Type"=dword:00000010
3057+ * "Description"="@%systemroot%\\system32\\cexecsvc.exe,-101"
3058+ * "ObjectName"="LocalSystem"
3059+ * "ServiceSidType"=dword:00000001
3060+ */
3061+ int is_inside_windows_container (void )
3062+ {
3063+ static int inside_container = -1 ; /* -1 uninitialized */
3064+ const char * key = "SYSTEM\\CurrentControlSet\\Services\\cexecsvc" ;
3065+ HKEY handle = NULL ;
3066+
3067+ if (inside_container != -1 )
3068+ return inside_container ;
3069+
3070+ inside_container = ERROR_SUCCESS ==
3071+ RegOpenKeyExA (HKEY_LOCAL_MACHINE , key , 0 , KEY_READ , & handle );
3072+ RegCloseKey (handle );
3073+
3074+ return inside_container ;
3075+ }
3076+
3077+ int file_attr_to_st_mode (DWORD attr , DWORD tag , const char * path )
3078+ {
3079+ int fMode = S_IREAD ;
3080+ if ((attr & FILE_ATTRIBUTE_REPARSE_POINT ) &&
3081+ tag == IO_REPARSE_TAG_SYMLINK ) {
3082+ int flag = S_IFLNK ;
3083+ char buf [MAX_LONG_PATH ];
3084+
3085+ /*
3086+ * Windows containers' mapped volumes are marked as reparse
3087+ * points and look like symbolic links, but they are not.
3088+ */
3089+ if (path && is_inside_windows_container () &&
3090+ readlink (path , buf , sizeof (buf )) > 27 &&
3091+ starts_with (buf , "/ContainerMappedDirectories/" ))
3092+ flag = S_IFDIR ;
3093+
3094+ fMode |= flag ;
3095+ } else if (attr & FILE_ATTRIBUTE_DIRECTORY )
3096+ fMode |= S_IFDIR ;
3097+ else
3098+ fMode |= S_IFREG ;
3099+ if (!(attr & FILE_ATTRIBUTE_READONLY ))
3100+ fMode |= S_IWRITE ;
3101+ return fMode ;
3102+ }
0 commit comments