1 ! Copyright (C) 2008 Doug Coleman, Eduardo Cavazos.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors assocs combinators combinators.short-circuit
4 io.files io.files.types io.pathnames kernel math strings system
9 TUPLE: file-info-tuple type size size-on-disk permissions created modified
12 HOOK: file-info os ( path -- info )
14 : ?file-info ( path -- info/f )
15 dup file-exists? [ file-info ] [ drop f ] if ;
17 HOOK: link-info os ( path -- info )
19 : ?link-info ( path -- info/f )
20 dup file-exists? [ link-info ] [ drop f ] if ;
24 : >file-info ( path/info -- info )
25 dup { [ string? ] [ pathname? ] } 1|| [ file-info ] when ;
29 : directory? ( path/info -- ? )
30 >file-info type>> +directory+ = ;
32 : regular-file? ( path/info -- ? )
33 >file-info type>> +regular-file+ = ;
35 : symbolic-link? ( path/info -- ? )
36 >file-info type>> +symbolic-link+ = ;
38 : sparse-file? ( path/info -- ? )
39 >file-info [ size-on-disk>> ] [ size>> ] bi < ;
42 HOOK: file-systems os ( -- array )
44 TUPLE: file-system-info-tuple device-name mount-point type
45 available-space free-space used-space total-space ;
47 HOOK: file-system-info os ( path -- file-system-info )
49 HOOK: file-readable? os ( path -- ? )
50 HOOK: file-writable? os ( path -- ? )
51 HOOK: file-executable? os ( path -- ? )
53 HOOK: mount-points os ( -- assoc )
55 M: object mount-points
56 file-systems [ [ mount-point>> ] keep ] H{ } map>assoc ;
58 : (find-mount-point) ( path assoc -- object )
59 [ resolve-symlinks canonicalize-path-full ] dip
63 drop [ parent-directory ] dip (find-mount-point)
66 : find-mount-point ( path -- object )
67 mount-points (find-mount-point) ;
70 { [ os unix? ] [ "io.files.info.unix" ] }
71 { [ os windows? ] [ "io.files.info.windows" ] }