io.directories: improve file moving words

move-file previously had inconsistent behavior on Windows and unixes.
This unifies the behavior to the common case (just get 'er done), while
also introducing an obviously named word, move-file-atomically, to
handle the case where you need an atomic file primitive.

Fixes #1772
char-rename
Benjamin Pollack 2017-01-05 18:26:55 -05:00 committed by John Benediktsson
parent dac41e1bf7
commit 7184771c31
5 changed files with 21 additions and 3 deletions

View File

@ -104,7 +104,13 @@ HELP: touch-file
HELP: move-file
{ $values { "from" "a pathname string" } { "to" "a pathname string" } }
{ $description "Moves or renames a file." }
{ $description "Moves or renames a file. This operation is not guaranteed to be atomic. In particular, if you attempt to move a file across volumes, this will copy the file and then delete the original in a nontransactional manner." }
{ $errors "Throws an error if the file does not exist or if the move operation fails." }
{ $see-also move-file-atomically } ;
HELP: move-file-atomically
{ $values { "from" "a pathname string" } { "to" "a pathname string" } }
{ $description "Moves or renames a file as an atomic operation." }
{ $errors "Throws an error if the file does not exist or if the move operation fails." } ;
HELP: move-file-into

View File

@ -84,6 +84,7 @@ HOOK: delete-directory io-backend ( path -- )
! Moving and renaming files
HOOK: move-file io-backend ( from to -- )
HOOK: move-file-atomically io-backend ( from to -- )
: move-file-into ( from to -- )
to-directory move-file ;

View File

@ -17,9 +17,16 @@ M: unix touch-file ( path -- )
touch-mode file-mode open-file close-file
] if ;
M: unix move-file ( from to -- )
M: unix move-file-atomically ( from to -- )
[ normalize-path ] bi@ [ rename ] unix-system-call drop ;
M: unix move-file ( from to -- )
[ move-file-atomically ] [
dup errno>> EXDEV = [
drop [ copy-file ] [ drop delete-file ] 2bi
] [ rethrow ] if
] recover ;
M: unix delete-file ( path -- ) normalize-path unlink-file ;
M: unix make-directory ( path -- )

View File

@ -17,6 +17,9 @@ M: windows touch-file ( path -- )
M: windows move-file ( from to -- )
[ normalize-path ] bi@ MoveFile win32-error=0/f ;
M: windows move-file-atomically ( from to -- )
[ normalize-path ] bi@ 0 MoveFileEx win32-error=0/f ;
ERROR: file-delete-failed path error ;
: delete-file-throws ( path -- )

View File

@ -1676,7 +1676,8 @@ FUNCTION: LPVOID MapViewOfFileEx ( HANDLE hFileMappingObject,
! FUNCTION: Module32NextW
! FUNCTION: MoveFileA
! FUNCTION: MoveFileExA
! FUNCTION: MoveFileExW
FUNCTION: BOOL MoveFileExW ( LPCSTR lpExistingFile, LPCSTR lpNewFileName, DWORD dwFlags )
ALIAS: MoveFileEx MoveFileExW
FUNCTION: BOOL MoveFileW ( LPCTSTR lpExistingFileName, LPCTSTR lpNewFileName )
ALIAS: MoveFile MoveFileW
! FUNCTION: MoveFileWithProgressA