@@ -779,26 +779,17 @@ CAMLexport wchar_t *caml_win32_getenv(wchar_t const *lpName)
779
779
780
780
int caml_win32_rename (const wchar_t * oldpath , const wchar_t * newpath )
781
781
{
782
- /* First handle corner-cases not handled by MoveFileEx:
783
- - dir to empty dir - positive - should succeed
782
+ /* First handle corner-case not handled by MoveFileEx:
784
783
- dir to existing file - should fail */
784
+ DWORD new_attribs ;
785
785
DWORD old_attribs = GetFileAttributes (oldpath );
786
786
if ((old_attribs != INVALID_FILE_ATTRIBUTES ) &&
787
- (old_attribs & FILE_ATTRIBUTE_DIRECTORY ) != 0 &&
788
- (old_attribs & FILE_ATTRIBUTE_HIDDEN ) == 0 &&
789
- (old_attribs & FILE_ATTRIBUTE_SYSTEM ) == 0 ) {
790
- DWORD new_attribs = GetFileAttributes (newpath );
787
+ (old_attribs & FILE_ATTRIBUTE_DIRECTORY ) != 0 ) {
788
+ new_attribs = GetFileAttributes (newpath );
791
789
if ((new_attribs != INVALID_FILE_ATTRIBUTES ) &&
792
- (new_attribs & FILE_ATTRIBUTE_HIDDEN ) == 0 &&
793
- (new_attribs & FILE_ATTRIBUTE_SYSTEM ) == 0 ) {
794
- if ((new_attribs & FILE_ATTRIBUTE_DIRECTORY ) != 0 ) {
795
- /* Try to delete and fall though.
796
- RemoveDirectoryW fails on non-empty dirs as intended. */
797
- RemoveDirectoryW (newpath );
798
- } else {
790
+ (new_attribs & FILE_ATTRIBUTE_DIRECTORY ) == 0 ) {
799
791
errno = ENOTDIR ;
800
792
return -1 ;
801
- }
802
793
}
803
794
}
804
795
/* MOVEFILE_REPLACE_EXISTING: to be closer to POSIX
@@ -812,6 +803,23 @@ int caml_win32_rename(const wchar_t * oldpath, const wchar_t * newpath)
812
803
MOVEFILE_COPY_ALLOWED )) {
813
804
return 0 ;
814
805
}
806
+
807
+ /* Another cornercase not handled by MoveFileEx:
808
+ - dir to empty dir - positive - should succeed */
809
+ if ((old_attribs != INVALID_FILE_ATTRIBUTES ) &&
810
+ (old_attribs & FILE_ATTRIBUTE_DIRECTORY ) != 0 &&
811
+ (new_attribs != INVALID_FILE_ATTRIBUTES ) &&
812
+ (new_attribs & FILE_ATTRIBUTE_DIRECTORY ) != 0 ) {
813
+ /* Try to delete: RemoveDirectoryW fails on non-empty dirs as intended.
814
+ Then try again. */
815
+ RemoveDirectoryW (newpath );
816
+ if (MoveFileEx (oldpath , newpath ,
817
+ MOVEFILE_REPLACE_EXISTING | MOVEFILE_WRITE_THROUGH |
818
+ MOVEFILE_COPY_ALLOWED )) {
819
+ return 0 ;
820
+ }
821
+ }
822
+
815
823
/* Modest attempt at mapping Win32 error codes to POSIX error codes.
816
824
The __dosmaperr() function from the CRT does a better job but is
817
825
generally not accessible. */
0 commit comments