]> gitweb.factorcode.org Git - factor.git/blob - basis/io/directories/unix/unix.factor
Move <ref>, deref and little-endian? from alien.c-types to alien.data, remove <c...
[factor.git] / basis / io / directories / unix / unix.factor
1 ! Copyright (C) 2008 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors alien.c-types alien.data alien.strings
4 combinators continuations destructors fry io io.backend
5 io.backend.unix io.directories io.encodings.binary
6 io.encodings.utf8 io.files io.pathnames io.files.types kernel
7 math.bitwise sequences system unix unix.stat vocabs.loader
8 classes.struct unix.ffi literals ;
9 IN: io.directories.unix
10
11 CONSTANT: touch-mode flags{ O_WRONLY O_APPEND O_CREAT O_EXCL }
12
13 M: unix touch-file ( path -- )
14     normalize-path
15     dup exists? [ touch ] [
16         touch-mode file-mode open-file close-file
17     ] if ;
18
19 M: unix move-file ( from to -- )
20     [ normalize-path ] bi@ [ rename ] unix-system-call drop ;
21
22 M: unix delete-file ( path -- ) normalize-path unlink-file ;
23
24 M: unix make-directory ( path -- )
25     normalize-path OCT: 777 [ mkdir ] unix-system-call drop ;
26
27 M: unix delete-directory ( path -- )
28     normalize-path [ rmdir ] unix-system-call drop ;
29
30 M: unix copy-file ( from to -- )
31     [ normalize-path ] bi@ call-next-method ;
32
33 : with-unix-directory ( path quot -- )
34     [ opendir dup [ (io-error) ] unless ] dip
35     dupd curry swap '[ _ closedir io-error ] [ ] cleanup ; inline
36
37 HOOK: find-next-file os ( DIR* -- byte-array )
38
39 M: unix find-next-file ( DIR* -- byte-array )
40     dirent <struct>
41     f void* <ref>
42     [ readdir_r 0 = [ (io-error) ] unless ] 2keep
43     void* deref [ drop f ] unless ;
44
45 : dirent-type>file-type ( ch -- type )
46     {
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+ ] }
55         [ drop +unknown+ ]
56     } case ;
57
58 M: unix >directory-entry ( byte-array -- directory-entry )
59     {
60         [ d_name>> underlying>> utf8 alien>string ]
61         [ d_type>> dirent-type>file-type ]
62     } cleave directory-entry boa ;
63
64 M: unix (directory-entries) ( path -- seq )
65     [
66         '[ _ find-next-file dup ]
67         [ >directory-entry ]
68         produce nip
69     ] with-unix-directory ;
70
71 os linux? [ "io.directories.unix.linux" require ] when