1 ! Copyright (C) 2008 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors alien.c-types alien.strings combinators
4 continuations destructors fry io io.backend io.backend.unix
5 io.directories io.encodings.binary io.encodings.utf8 io.files
6 io.pathnames io.files.types kernel math.bitwise sequences system
8 IN: io.directories.unix
11 { O_WRONLY O_APPEND O_CREAT O_EXCL } flags ; foldable
13 M: unix touch-file ( path -- )
15 dup exists? [ touch ] [
16 touch-mode file-mode open-file close-file
19 M: unix move-file ( from to -- )
20 [ normalize-path ] bi@ rename io-error ;
22 M: unix delete-file ( path -- ) normalize-path unlink-file ;
24 M: unix make-directory ( path -- )
25 normalize-path OCT: 777 mkdir io-error ;
27 M: unix delete-directory ( path -- )
28 normalize-path rmdir io-error ;
30 : (copy-file) ( from to -- )
31 dup parent-directory make-directories
32 binary <file-writer> [
33 swap binary <file-reader> [
38 M: unix copy-file ( from to -- )
39 [ normalize-path ] bi@ (copy-file) ;
41 : with-unix-directory ( path quot -- )
42 [ opendir dup [ (io-error) ] unless ] dip
43 dupd curry swap '[ _ closedir io-error ] [ ] cleanup ; inline
45 : find-next-file ( DIR* -- byte-array )
48 [ readdir_r 0 = [ (io-error) ] unless ] 2keep
49 *void* [ drop f ] unless ;
51 : dirent-type>file-type ( ch -- type )
53 { DT_BLK [ +block-device+ ] }
54 { DT_CHR [ +character-device+ ] }
55 { DT_DIR [ +directory+ ] }
56 { DT_LNK [ +symbolic-link+ ] }
57 { DT_SOCK [ +socket+ ] }
58 { DT_FIFO [ +fifo+ ] }
59 { DT_REG [ +regular-file+ ] }
60 { DT_WHT [ +whiteout+ ] }
64 M: unix >directory-entry ( byte-array -- directory-entry )
65 [ dirent-d_name utf8 alien>string ]
66 [ dirent-d_type dirent-type>file-type ] bi directory-entry boa ;
68 M: unix (directory-entries) ( path -- seq )
70 '[ _ find-next-file dup ]
73 ] with-unix-directory ;