@@ -930,7 +930,7 @@ int mingw_lstat(const char *file_name, struct stat *buf)
930930 buf -> st_uid = 0 ;
931931 buf -> st_nlink = 1 ;
932932 buf -> st_mode = file_attr_to_st_mode (fdata .dwFileAttributes ,
933- findbuf .dwReserved0 );
933+ findbuf .dwReserved0 , file_name );
934934 buf -> st_size = S_ISLNK (buf -> st_mode ) ? MAX_LONG_PATH :
935935 fdata .nFileSizeLow | (((off_t ) fdata .nFileSizeHigh ) << 32 );
936936 buf -> st_dev = buf -> st_rdev = 0 ; /* not used by Git */
@@ -981,7 +981,7 @@ static int get_file_info_by_handle(HANDLE hnd, struct stat *buf)
981981 buf -> st_gid = 0 ;
982982 buf -> st_uid = 0 ;
983983 buf -> st_nlink = 1 ;
984- buf -> st_mode = file_attr_to_st_mode (fdata .dwFileAttributes , 0 );
984+ buf -> st_mode = file_attr_to_st_mode (fdata .dwFileAttributes , 0 , NULL );
985985 buf -> st_size = fdata .nFileSizeLow |
986986 (((off_t )fdata .nFileSizeHigh )<<32 );
987987 buf -> st_dev = buf -> st_rdev = 0 ; /* not used by Git */
@@ -2233,6 +2233,13 @@ int mingw_rename(const char *pold, const char *pnew)
22332233 return 0 ;
22342234 gle = GetLastError ();
22352235
2236+ if (gle == ERROR_ACCESS_DENIED && is_inside_windows_container ()) {
2237+ /* Fall back to copy to destination & remove source */
2238+ if (CopyFileW (wpold , wpnew , FALSE) && !mingw_unlink (pold ))
2239+ return 0 ;
2240+ gle = GetLastError ();
2241+ }
2242+
22362243 /* revert file attributes on failure */
22372244 if (attrs != INVALID_FILE_ATTRIBUTES )
22382245 SetFileAttributesW (wpnew , attrs );
@@ -3206,3 +3213,62 @@ const char *program_data_config(void)
32063213 }
32073214 return * path .buf ? path .buf : NULL ;
32083215}
3216+
3217+ /*
3218+ * Based on https://stackoverflow.com/questions/43002803
3219+ *
3220+ * [HKLM\SYSTEM\CurrentControlSet\Services\cexecsvc]
3221+ * "DisplayName"="@%systemroot%\\system32\\cexecsvc.exe,-100"
3222+ * "ErrorControl"=dword:00000001
3223+ * "ImagePath"=hex(2):25,00,73,00,79,00,73,00,74,00,65,00,6d,00,72,00,6f,00,
3224+ * 6f,00,74,00,25,00,5c,00,73,00,79,00,73,00,74,00,65,00,6d,00,33,00,32,00,
3225+ * 5c,00,63,00,65,00,78,00,65,00,63,00,73,00,76,00,63,00,2e,00,65,00,78,00,
3226+ * 65,00,00,00
3227+ * "Start"=dword:00000002
3228+ * "Type"=dword:00000010
3229+ * "Description"="@%systemroot%\\system32\\cexecsvc.exe,-101"
3230+ * "ObjectName"="LocalSystem"
3231+ * "ServiceSidType"=dword:00000001
3232+ */
3233+ int is_inside_windows_container (void )
3234+ {
3235+ static int inside_container = -1 ; /* -1 uninitialized */
3236+ const char * key = "SYSTEM\\CurrentControlSet\\Services\\cexecsvc" ;
3237+ HKEY handle = NULL ;
3238+
3239+ if (inside_container != -1 )
3240+ return inside_container ;
3241+
3242+ inside_container = ERROR_SUCCESS ==
3243+ RegOpenKeyEx (HKEY_LOCAL_MACHINE , key , 0 , KEY_READ , & handle );
3244+ RegCloseKey (handle );
3245+
3246+ return inside_container ;
3247+ }
3248+
3249+ int file_attr_to_st_mode (DWORD attr , DWORD tag , const char * path )
3250+ {
3251+ int fMode = S_IREAD ;
3252+ if ((attr & FILE_ATTRIBUTE_REPARSE_POINT ) &&
3253+ tag == IO_REPARSE_TAG_SYMLINK ) {
3254+ int flag = S_IFLNK ;
3255+ char buf [MAX_LONG_PATH ];
3256+
3257+ /*
3258+ * Windows containers' mapped volumes are marked as reparse
3259+ * points and look like symbolic links, but they are not.
3260+ */
3261+ if (path && is_inside_windows_container () &&
3262+ readlink (path , buf , sizeof (buf )) > 27 &&
3263+ starts_with (buf , "/ContainerMappedDirectories/" ))
3264+ flag = S_IFDIR ;
3265+
3266+ fMode |= flag ;
3267+ } else if (attr & FILE_ATTRIBUTE_DIRECTORY )
3268+ fMode |= S_IFDIR ;
3269+ else
3270+ fMode |= S_IFREG ;
3271+ if (!(attr & FILE_ATTRIBUTE_READONLY ))
3272+ fMode |= S_IWRITE ;
3273+ return fMode ;
3274+ }
0 commit comments