1 ! Copyright (C) 2008 Doug Coleman.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors alien.c-types alien.syntax combinators csv
4 io.backend io.encodings.utf8 io.files io.files.info io.streams.string
5 io.files.unix kernel math.order namespaces sequences sorting
6 system unix unix.statfs.linux unix.statvfs.linux io.files.links
7 specialized-arrays.direct.uint arrays io.files.info.unix assocs
8 io.pathnames unix.types ;
9 IN: io.files.info.unix.linux
11 TUPLE: linux-file-system-info < unix-file-system-info
14 M: linux new-file-system-info linux-file-system-info new ;
16 M: linux file-system-statfs ( path -- byte-array )
17 "statfs64" <c-object> [ statfs64 io-error ] keep ;
19 M: linux statfs>file-system-info ( struct -- statfs )
21 [ statfs64-f_type >>type ]
22 [ statfs64-f_bsize >>block-size ]
23 [ statfs64-f_blocks >>blocks ]
24 [ statfs64-f_bfree >>blocks-free ]
25 [ statfs64-f_bavail >>blocks-available ]
26 [ statfs64-f_files >>files ]
27 [ statfs64-f_ffree >>files-free ]
28 [ statfs64-f_fsid 2 <direct-uint-array> >array >>id ]
29 [ statfs64-f_namelen >>namelen ]
30 [ statfs64-f_frsize >>preferred-block-size ]
31 ! [ statfs64-f_spare >>spare ]
34 M: linux file-system-statvfs ( path -- byte-array )
35 "statvfs64" <c-object> [ statvfs64 io-error ] keep ;
37 M: linux statvfs>file-system-info ( struct -- statfs )
39 [ statvfs64-f_flag >>flags ]
40 [ statvfs64-f_namemax >>name-max ]
43 TUPLE: mtab-entry file-system-name mount-point type options
44 frequency pass-number ;
46 : mtab-csv>mtab-entry ( csv -- mtab-entry )
47 [ mtab-entry new ] dip
49 [ first >>file-system-name ]
50 [ second >>mount-point ]
52 [ fourth <string-reader> csv first >>options ]
53 [ 4 swap nth >>frequency ]
54 [ 5 swap nth >>pass-number ]
57 : parse-mtab ( -- array )
59 "/etc/mtab" utf8 <file-reader>
60 CHAR: \s delimiter set csv
62 [ mtab-csv>mtab-entry ] map ;
66 [ mount-point>> file-system-info ] keep
68 [ file-system-name>> >>device-name ]
69 [ mount-point>> >>mount-point ]
74 : (find-mount-point) ( path mtab-paths -- mtab-entry )
75 [ follow-links ] dip 2dup at* [
78 drop [ parent-directory ] dip (find-mount-point)
81 : find-mount-point ( path -- mtab-entry )
82 parse-mtab [ [ mount-point>> ] keep ] H{ } map>assoc (find-mount-point) ;
84 ERROR: file-system-not-found ;
86 M: linux file-system-info ( path -- )
89 [ new-file-system-info ] dip
90 [ file-system-statfs statfs>file-system-info ]
91 [ file-system-statvfs statvfs>file-system-info ] bi
92 file-system-calculations
96 [ file-system-name>> >>device-name drop ]
97 [ mount-point>> >>mount-point drop ]