1 ! Copyright (C) 2004, 2008 Slava Pestov, Doug Coleman.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors arrays combinators combinators.short-circuit
4 destructors fry io io.backend io.encodings.binary io.files
5 io.files.types io.pathnames kernel namespaces sequences
6 system vocabs vocabs.loader ;
9 : set-current-directory ( path -- )
10 absolute-path current-directory set ;
12 : with-directory ( path quot -- )
13 [ absolute-path current-directory ] dip with-variable ; inline
15 ! Creating directories
16 HOOK: make-directory io-backend ( path -- )
18 : make-directories ( path -- )
19 normalize-path trim-tail-separators dup {
25 dup parent-directory make-directories
30 TUPLE: directory-entry name type ;
32 HOOK: >directory-entry os ( byte-array -- directory-entry )
34 HOOK: (directory-entries) os ( path -- seq )
36 : directory-entries ( path -- seq )
39 [ name>> { "." ".." } member? not ] filter ;
41 : directory-files ( path -- seq )
42 directory-entries [ name>> ] map! ;
44 : with-directory-entries ( path quot -- )
45 '[ "" directory-entries @ ] with-directory ; inline
47 : with-directory-files ( path quot -- )
48 '[ "" directory-files @ ] with-directory ; inline
51 HOOK: touch-file io-backend ( path -- )
54 HOOK: delete-file io-backend ( path -- )
56 HOOK: delete-directory io-backend ( path -- )
58 : to-directory ( from to -- from to' )
59 over file-name append-path ;
61 ! Moving and renaming files
62 HOOK: move-file io-backend ( from to -- )
64 : move-file-into ( from to -- )
65 to-directory move-file ;
67 : move-files-into ( files to -- )
68 to-directory '[ _ move-file ] each ;
71 HOOK: copy-file io-backend ( from to -- )
74 dup parent-directory make-directories
75 binary <file-writer> [
76 swap binary <file-reader> [
81 : copy-file-into ( from to -- )
82 to-directory copy-file ;
84 : copy-files-into ( files to -- )
85 to-directory '[ _ copy-file ] each ;
88 { [ os unix? ] [ "io.directories.unix" require ] }
89 { [ os windows? ] [ "io.directories.windows" require ] }