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
7 unix unix.stat vocabs.loader classes.struct unix.ffi literals ;
8 IN: io.directories.unix
10 CONSTANT: touch-mode flags{ O_WRONLY O_APPEND O_CREAT O_EXCL }
12 M: unix touch-file ( path -- )
14 dup exists? [ touch ] [
15 touch-mode file-mode open-file close-file
18 M: unix move-file ( from to -- )
19 [ normalize-path ] bi@ [ rename ] unix-system-call drop ;
21 M: unix delete-file ( path -- ) normalize-path unlink-file ;
23 M: unix make-directory ( path -- )
24 normalize-path OCT: 777 [ mkdir ] unix-system-call drop ;
26 M: unix delete-directory ( path -- )
27 normalize-path [ rmdir ] unix-system-call drop ;
29 M: unix copy-file ( from to -- )
30 [ normalize-path ] bi@ call-next-method ;
32 : with-unix-directory ( path quot -- )
33 [ opendir dup [ (io-error) ] unless ] dip
34 dupd curry swap '[ _ closedir io-error ] [ ] cleanup ; inline
36 HOOK: find-next-file os ( DIR* -- byte-array )
38 M: unix find-next-file ( DIR* -- byte-array )
41 [ readdir_r 0 = [ (io-error) ] unless ] 2keep
42 *void* [ drop f ] unless ;
44 : dirent-type>file-type ( ch -- type )
46 { DT_BLK [ +block-device+ ] }
47 { DT_CHR [ +character-device+ ] }
48 { DT_DIR [ +directory+ ] }
49 { DT_LNK [ +symbolic-link+ ] }
50 { DT_SOCK [ +socket+ ] }
51 { DT_FIFO [ +fifo+ ] }
52 { DT_REG [ +regular-file+ ] }
53 { DT_WHT [ +whiteout+ ] }
57 M: unix >directory-entry ( byte-array -- directory-entry )
59 [ d_name>> underlying>> utf8 alien>string ]
60 [ d_type>> dirent-type>file-type ]
61 } cleave directory-entry boa ;
63 M: unix (directory-entries) ( path -- seq )
65 '[ _ find-next-file dup ]
68 ] with-unix-directory ;
70 os linux? [ "io.directories.unix.linux" require ] when