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
5 io.files.unix libc libc.linux kernel math.order namespaces sequences
6 sorting system unix unix.statfs.linux unix.statvfs.linux io.files.links
7 arrays io.files.info.unix assocs io.pathnames unix.types
9 FROM: csv => delimiter ;
10 IN: io.files.info.unix.linux
12 TUPLE: linux-file-system-info < unix-file-system-info
15 M: linux new-file-system-info linux-file-system-info new ;
17 M: linux file-system-statfs ( path -- statfs )
18 \ statfs64 <struct> [ statfs64 io-error ] keep ;
20 M: linux statfs>file-system-info ( file-system-info statfs -- file-system-info' )
23 [ f_bsize>> >>block-size ]
24 [ f_blocks>> >>blocks ]
25 [ f_bfree>> >>blocks-free ]
26 [ f_bavail>> >>blocks-available ]
28 [ f_ffree>> >>files-free ]
30 [ f_namelen>> >>namelen ]
31 [ f_frsize>> >>preferred-block-size ]
32 ! [ statfs64-f_spare >>spare ]
35 M: linux file-system-statvfs ( path -- statvfs )
36 \ statvfs64 <struct> [ statvfs64 io-error ] keep ;
38 M: linux statvfs>file-system-info ( file-system-info statfs -- file-system-info' )
41 [ f_namemax>> >>name-max ]
44 TUPLE: mtab-entry file-system-name mount-point type options
45 frequency pass-number ;
47 : mtab-csv>mtab-entry ( csv -- mtab-entry )
48 [ mtab-entry new ] dip
50 [ first >>file-system-name ]
51 [ second >>mount-point ]
53 [ fourth string>csv first >>options ]
54 [ 4 swap ?nth [ 0 ] unless* >>frequency ]
55 [ 5 swap ?nth [ 0 ] unless* >>pass-number ]
58 : parse-mtab ( -- array )
59 CHAR: \s [ "/etc/mtab" utf8 file>csv ] with-delimiter
60 [ mtab-csv>mtab-entry ] map ;
64 [ mount-point>> file-system-info ] keep
66 [ file-system-name>> >>device-name ]
67 [ mount-point>> >>mount-point ]
72 : (find-mount-point) ( path mtab-paths -- mtab-entry )
76 drop [ parent-directory ] dip (find-mount-point)
79 : find-mount-point ( path -- mtab-entry )
81 parse-mtab [ [ mount-point>> ] keep ] H{ } map>assoc (find-mount-point) ;
83 ERROR: file-system-not-found ;
85 M: linux file-system-info ( path -- file-system-info )
88 [ new-file-system-info ] dip
89 [ file-system-statfs statfs>file-system-info ]
90 [ file-system-statvfs statvfs>file-system-info ] bi
91 file-system-calculations
95 [ file-system-name>> >>device-name drop ]
96 [ mount-point>> >>mount-point drop ]