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 ;
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 M: unix copy-file ( from to -- )
31 [ normalize-path ] bi@ call-next-method ;
33 : with-unix-directory ( path quot -- )
34 [ opendir dup [ (io-error) ] unless ] dip
35 dupd curry swap '[ _ closedir io-error ] [ ] cleanup ; inline
37 HOOK: find-next-file os ( DIR* -- byte-array )
39 M: unix find-next-file ( DIR* -- byte-array )
42 [ readdir_r 0 = [ (io-error) ] unless ] 2keep
43 *void* [ drop f ] unless ;
45 : dirent-type>file-type ( ch -- type )
47 { DT_BLK [ +block-device+ ] }
48 { DT_CHR [ +character-device+ ] }
49 { DT_DIR [ +directory+ ] }
50 { DT_LNK [ +symbolic-link+ ] }
51 { DT_SOCK [ +socket+ ] }
52 { DT_FIFO [ +fifo+ ] }
53 { DT_REG [ +regular-file+ ] }
54 { DT_WHT [ +whiteout+ ] }
58 TUPLE: unix-directory-entry < directory-entry ino off reclen ;
60 M: unix >directory-entry ( byte-array -- directory-entry )
62 [ dirent-d_name utf8 alien>string ]
63 [ dirent-d_type dirent-type>file-type ]
67 } cleave unix-directory-entry boa ;
69 M: unix (directory-entries) ( path -- seq )
71 '[ _ find-next-file dup ]
74 ] with-unix-directory ;
76 os linux? [ "io.directories.unix.linux" require ] when