@@ -2578,7 +2578,7 @@ int mingw_accept(int sockfd1, struct sockaddr *sa, socklen_t *sz)
2578
2578
int mingw_rename (const char * pold , const char * pnew )
2579
2579
{
2580
2580
static int supports_file_rename_info_ex = 1 ;
2581
- DWORD attrs , gle ;
2581
+ DWORD attrs = INVALID_FILE_ATTRIBUTES , gle ;
2582
2582
int tries = 0 ;
2583
2583
wchar_t wpold [MAX_LONG_PATH ], wpnew [MAX_LONG_PATH ];
2584
2584
int wpnew_len ;
@@ -2589,15 +2589,6 @@ int mingw_rename(const char *pold, const char *pnew)
2589
2589
if (wpnew_len < 0 )
2590
2590
return -1 ;
2591
2591
2592
- /*
2593
- * Try native rename() first to get errno right.
2594
- * It is based on MoveFile(), which cannot overwrite existing files.
2595
- */
2596
- if (!_wrename (wpold , wpnew ))
2597
- return 0 ;
2598
- if (errno != EEXIST )
2599
- return -1 ;
2600
-
2601
2592
repeat :
2602
2593
if (supports_file_rename_info_ex ) {
2603
2594
/*
@@ -2669,13 +2660,22 @@ int mingw_rename(const char *pold, const char *pnew)
2669
2660
* to retry.
2670
2661
*/
2671
2662
} else {
2672
- if (MoveFileExW (wpold , wpnew , MOVEFILE_REPLACE_EXISTING ))
2663
+ if (MoveFileExW (wpold , wpnew ,
2664
+ MOVEFILE_REPLACE_EXISTING | MOVEFILE_COPY_ALLOWED ))
2673
2665
return 0 ;
2674
2666
gle = GetLastError ();
2675
2667
}
2676
2668
2677
- /* TODO: translate more errors */
2678
- if (gle == ERROR_ACCESS_DENIED &&
2669
+ /* revert file attributes on failure */
2670
+ if (attrs != INVALID_FILE_ATTRIBUTES )
2671
+ SetFileAttributesW (wpnew , attrs );
2672
+
2673
+ if (!is_file_in_use_error (gle )) {
2674
+ errno = err_win_to_posix (gle );
2675
+ return -1 ;
2676
+ }
2677
+
2678
+ if (attrs == INVALID_FILE_ATTRIBUTES &&
2679
2679
(attrs = GetFileAttributesW (wpnew )) != INVALID_FILE_ATTRIBUTES ) {
2680
2680
if (attrs & FILE_ATTRIBUTE_DIRECTORY ) {
2681
2681
DWORD attrsold = GetFileAttributesW (wpold );
@@ -2687,16 +2687,10 @@ int mingw_rename(const char *pold, const char *pnew)
2687
2687
return -1 ;
2688
2688
}
2689
2689
if ((attrs & FILE_ATTRIBUTE_READONLY ) &&
2690
- SetFileAttributesW (wpnew , attrs & ~FILE_ATTRIBUTE_READONLY )) {
2691
- if (MoveFileExW (wpold , wpnew , MOVEFILE_REPLACE_EXISTING ))
2692
- return 0 ;
2693
- gle = GetLastError ();
2694
- /* revert file attributes on failure */
2695
- SetFileAttributesW (wpnew , attrs );
2696
- }
2690
+ SetFileAttributesW (wpnew , attrs & ~FILE_ATTRIBUTE_READONLY ))
2691
+ goto repeat ;
2697
2692
}
2698
- if (gle == ERROR_ACCESS_DENIED &&
2699
- retry_ask_yes_no (& tries , "Rename from '%s' to '%s' failed. "
2693
+ if (retry_ask_yes_no (& tries , "Rename from '%s' to '%s' failed. "
2700
2694
"Should I try again?" , pold , pnew ))
2701
2695
goto repeat ;
2702
2696
0 commit comments