]> gitweb.factorcode.org Git - factor.git/blob - basis/io/files/unix/unix.factor
io.files: implement (file-writer-secure)
[factor.git] / basis / io / files / unix / unix.factor
1 ! Copyright (C) 2005, 2008 Slava Pestov, Doug Coleman.
2 ! See https://factorcode.org/license.txt for BSD license.
3 USING: accessors byte-arrays continuations destructors environment
4 io.backend.unix io.files io.files.private io.pathnames io.ports kernel
5 libc literals math system unix unix.ffi ;
6 IN: io.files.unix
7
8 : (cwd) ( bufsiz -- path )
9     [
10         dup <byte-array> over [ getcwd ] unix-system-call nip
11     ] [
12         dup errno>> ERANGE = [
13             drop 2 * (cwd)
14         ] [ rethrow ] if
15     ] recover ;
16
17 M: unix cwd
18     4096 (cwd) ;
19
20 M: unix cd [ chdir ] unix-system-call drop ;
21
22 CONSTANT: read-flags flags{ O_RDONLY }
23
24 : open-read ( path -- fd ) read-flags file-mode open-file ;
25
26 M: unix (file-reader)
27     open-read <fd> init-fd <input-port> ;
28
29 CONSTANT: write-flags flags{ O_WRONLY O_CREAT O_TRUNC }
30
31 : open-write ( path -- fd )
32     write-flags file-mode open-file ;
33
34 M: unix (file-writer)
35     open-write <fd> init-fd <output-port> ;
36
37 CONSTANT: secure-write-flags flags{ O_WRONLY O_CREAT O_TRUNC O_EXCL }
38
39 : open-secure-write ( path -- fd )
40     secure-write-flags file-mode open-file ;
41
42 M: unix (file-writer-secure)
43     open-secure-write <fd> init-fd <output-port> ;
44
45 CONSTANT: append-flags flags{ O_WRONLY O_APPEND O_CREAT }
46
47 : open-append ( path -- fd )
48     [
49         append-flags file-mode open-file |dispose
50         dup 0 SEEK_END [ lseek ] unix-system-call drop
51     ] with-destructors ;
52
53 M: unix (file-appender)
54     open-append <fd> init-fd <output-port> ;
55
56 M: unix home "HOME" os-env ;