USING: help.markup help.syntax io.files.private io.pathnames
-quotations sequences ;
+math quotations sequences ;
IN: io.directories
HELP: cwd
{ $description "Updates the modification time of a file or directory. If the file does not exist, creates a new, empty file." }
{ $errors "Throws an error if the file could not be touched." } ;
+HELP: truncate-file
+{ $values { "path" "a pathname string" } { "n" integer } }
+{ $description "Set the length of the file to " { $snippet "n" } " bytes. If the file was previously longer, the extra data is lost. If the file was previously shorter, the behavior is platform-dependent on whether the file is extended with zeros (Unix) or the contents of the extended portion are undefined (Windows)." }
+{ $errors "Throws an error if the file does not exist or the truncate operation fails." } ;
+
HELP: move-file
{ $values { "from" "a pathname string" } { "to" "a pathname string" } }
{ $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." }
: find-files-larger-than ( path size -- seq )
'[ link-info size>> _ > ] find-files ;
-! Touching files
HOOK: touch-file io-backend ( path -- )
-! Deleting files
+HOOK: truncate-file io-backend ( path n -- )
+
HOOK: delete-file io-backend ( path -- )
HOOK: delete-directory io-backend ( path -- )
: to-directory ( from to -- from to' )
over file-name append-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 -- )
: move-files-into ( files to -- )
'[ _ move-file-into ] each ;
-! Copying files
HOOK: copy-file io-backend ( from to -- )
M: object copy-file
touch-mode file-mode open-file close-file
] if ;
+M: unix truncate-file
+ [ normalize-path ] dip [ truncate ] unix-system-call drop ;
+
M: unix move-file-atomically
[ normalize-path ] bi@ [ rename ] unix-system-call drop ;
IN: io.directories.windows
M: windows touch-file
- [
- normalize-path
- maybe-create-file [ &dispose ] dip
- [ drop ] [ handle>> f now dup (set-file-times) ] if
- ] with-destructors ;
+ normalize-path maybe-create-file '[
+ _ [ drop ] [ handle>> f now dup (set-file-times) ] if
+ ] with-disposal ;
+
+M: windows truncate-file
+ [ normalize-path open-file ] dip '[
+ [ _ 0 FILE_END set-file-pointer ]
+ [ set-end-of-file ] bi
+ ] with-disposal ;
M: windows move-file
[ normalize-path ] bi@ MoveFile win32-error=0/f ;
OPEN_ALWAYS 0 open-file
GetLastError ERROR_ALREADY_EXISTS = not ;
-: set-file-pointer ( handle length method -- )
+: set-file-pointer ( win32-file length method -- )
[ [ handle>> ] dip d>w/w LONG <ref> ] dip SetFilePointer
INVALID_SET_FILE_POINTER = [ "SetFilePointer failed" throw ] when ;
+: set-end-of-file ( win32-file -- )
+ handle>> SetEndOfFile zero? [ windows-error ] unless ;
+
M: windows (file-reader)
open-read <input-port> ;
: make-fifo ( path mode -- ) [ mkfifo ] unix-system-call drop ;
-: truncate-file ( path n -- ) [ truncate ] unix-system-call drop ;
-
: touch ( filename -- ) f [ utime ] unix-system-call drop ;
: change-file-times ( filename access modification -- )