@@ -2491,7 +2491,7 @@ int mingw_accept(int sockfd1, struct sockaddr *sa, socklen_t *sz)
2491
2491
int mingw_rename (const char * pold , const char * pnew )
2492
2492
{
2493
2493
static int supports_file_rename_info_ex = 1 ;
2494
- DWORD attrs , gle ;
2494
+ DWORD attrs = INVALID_FILE_ATTRIBUTES , gle ;
2495
2495
int tries = 0 ;
2496
2496
wchar_t wpold [MAX_LONG_PATH ], wpnew [MAX_LONG_PATH ];
2497
2497
int wpnew_len ;
@@ -2502,15 +2502,6 @@ int mingw_rename(const char *pold, const char *pnew)
2502
2502
if (wpnew_len < 0 )
2503
2503
return -1 ;
2504
2504
2505
- /*
2506
- * Try native rename() first to get errno right.
2507
- * It is based on MoveFile(), which cannot overwrite existing files.
2508
- */
2509
- if (!_wrename (wpold , wpnew ))
2510
- return 0 ;
2511
- if (errno != EEXIST )
2512
- return -1 ;
2513
-
2514
2505
repeat :
2515
2506
if (supports_file_rename_info_ex ) {
2516
2507
/*
@@ -2582,13 +2573,22 @@ int mingw_rename(const char *pold, const char *pnew)
2582
2573
* to retry.
2583
2574
*/
2584
2575
} else {
2585
- if (MoveFileExW (wpold , wpnew , MOVEFILE_REPLACE_EXISTING ))
2576
+ if (MoveFileExW (wpold , wpnew ,
2577
+ MOVEFILE_REPLACE_EXISTING | MOVEFILE_COPY_ALLOWED ))
2586
2578
return 0 ;
2587
2579
gle = GetLastError ();
2588
2580
}
2589
2581
2590
- /* TODO: translate more errors */
2591
- if (gle == ERROR_ACCESS_DENIED &&
2582
+ /* revert file attributes on failure */
2583
+ if (attrs != INVALID_FILE_ATTRIBUTES )
2584
+ SetFileAttributesW (wpnew , attrs );
2585
+
2586
+ if (!is_file_in_use_error (gle )) {
2587
+ errno = err_win_to_posix (gle );
2588
+ return -1 ;
2589
+ }
2590
+
2591
+ if (attrs == INVALID_FILE_ATTRIBUTES &&
2592
2592
(attrs = GetFileAttributesW (wpnew )) != INVALID_FILE_ATTRIBUTES ) {
2593
2593
if (attrs & FILE_ATTRIBUTE_DIRECTORY ) {
2594
2594
DWORD attrsold = GetFileAttributesW (wpold );
@@ -2600,16 +2600,10 @@ int mingw_rename(const char *pold, const char *pnew)
2600
2600
return -1 ;
2601
2601
}
2602
2602
if ((attrs & FILE_ATTRIBUTE_READONLY ) &&
2603
- SetFileAttributesW (wpnew , attrs & ~FILE_ATTRIBUTE_READONLY )) {
2604
- if (MoveFileExW (wpold , wpnew , MOVEFILE_REPLACE_EXISTING ))
2605
- return 0 ;
2606
- gle = GetLastError ();
2607
- /* revert file attributes on failure */
2608
- SetFileAttributesW (wpnew , attrs );
2609
- }
2603
+ SetFileAttributesW (wpnew , attrs & ~FILE_ATTRIBUTE_READONLY ))
2604
+ goto repeat ;
2610
2605
}
2611
- if (gle == ERROR_ACCESS_DENIED &&
2612
- retry_ask_yes_no (& tries , "Rename from '%s' to '%s' failed. "
2606
+ if (retry_ask_yes_no (& tries , "Rename from '%s' to '%s' failed. "
2613
2607
"Should I try again?" , pold , pnew ))
2614
2608
goto repeat ;
2615
2609
0 commit comments