]> gitweb.factorcode.org Git - factor.git/blob - basis/io/directories/unix/unix.factor
Merge branch 'master' of git://factorcode.org/git/factor into regexp
[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.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 ;
8 IN: io.directories.unix
9
10 : touch-mode ( -- n )
11     { O_WRONLY O_APPEND O_CREAT O_EXCL } flags ; foldable
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 io-error ;
21
22 M: unix delete-file ( path -- ) normalize-path unlink-file ;
23
24 M: unix make-directory ( path -- )
25     normalize-path OCT: 777 mkdir io-error ;
26
27 M: unix delete-directory ( path -- )
28     normalize-path rmdir io-error ;
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 : find-next-file ( DIR* -- byte-array )
38     "dirent" <c-object>
39     f <void*>
40     [ readdir_r 0 = [ (io-error) ] unless ] 2keep
41     *void* [ drop f ] unless ;
42
43 : dirent-type>file-type ( ch -- type )
44     {
45         { DT_BLK  [ +block-device+ ] }
46         { DT_CHR  [ +character-device+ ] }
47         { DT_DIR  [ +directory+ ] }
48         { DT_LNK  [ +symbolic-link+ ] }
49         { DT_SOCK [ +socket+ ] }
50         { DT_FIFO [ +fifo+ ] }
51         { DT_REG  [ +regular-file+ ] }
52         { DT_WHT  [ +whiteout+ ] }
53         [ drop +unknown+ ]
54     } case ;
55
56 M: unix >directory-entry ( byte-array -- directory-entry )
57     [ dirent-d_name utf8 alien>string ]
58     [ dirent-d_type dirent-type>file-type ] bi directory-entry boa ;
59
60 M: unix (directory-entries) ( path -- seq )
61     [
62         '[ _ find-next-file dup ]
63         [ >directory-entry ]
64         produce nip
65     ] with-unix-directory ;