| 
									
										
										
										
											2008-12-01 15:04:55 -05:00
										 |  |  | ! Copyright (C) 2008 Doug Coleman. | 
					
						
							|  |  |  | ! See http://factorcode.org/license.txt for BSD license. | 
					
						
							| 
									
										
										
										
											2008-12-01 17:28:22 -05:00
										 |  |  | USING: accessors alien.c-types alien.syntax combinators csv | 
					
						
							| 
									
										
										
										
											2008-12-15 03:18:59 -05:00
										 |  |  | io.backend io.encodings.utf8 io.files io.files.info io.streams.string | 
					
						
							| 
									
										
										
										
											2008-12-14 21:03:00 -05:00
										 |  |  | io.files.unix kernel math.order namespaces sequences sorting | 
					
						
							| 
									
										
										
										
											2008-12-18 20:57:21 -05:00
										 |  |  | system unix unix.statfs.linux unix.statvfs.linux io.files.links | 
					
						
							| 
									
										
										
										
											2008-12-18 19:32:00 -05:00
										 |  |  | specialized-arrays.direct.uint arrays io.files.info.unix assocs | 
					
						
							| 
									
										
										
										
											2009-01-09 15:31:02 -05:00
										 |  |  | io.pathnames unix.types ;
 | 
					
						
							| 
									
										
										
										
											2008-12-14 21:03:00 -05:00
										 |  |  | IN: io.files.info.unix.linux | 
					
						
							| 
									
										
										
										
											2008-12-01 15:04:55 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | TUPLE: linux-file-system-info < unix-file-system-info | 
					
						
							| 
									
										
										
										
											2008-12-02 21:41:57 -05:00
										 |  |  | namelen ;
 | 
					
						
							| 
									
										
										
										
											2008-12-01 15:04:55 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-12-01 17:28:22 -05:00
										 |  |  | M: linux new-file-system-info linux-file-system-info new ;
 | 
					
						
							| 
									
										
										
										
											2008-12-01 15:04:55 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | M: linux file-system-statfs ( path -- byte-array )
 | 
					
						
							| 
									
										
										
										
											2009-01-23 19:20:47 -05:00
										 |  |  |     "statfs64" <c-object> [ statfs64 io-error ] keep ;
 | 
					
						
							| 
									
										
										
										
											2008-12-01 15:04:55 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | M: linux statfs>file-system-info ( struct -- statfs )
 | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         [ statfs64-f_type >>type ] | 
					
						
							|  |  |  |         [ statfs64-f_bsize >>block-size ] | 
					
						
							|  |  |  |         [ statfs64-f_blocks >>blocks ] | 
					
						
							|  |  |  |         [ statfs64-f_bfree >>blocks-free ] | 
					
						
							|  |  |  |         [ statfs64-f_bavail >>blocks-available ] | 
					
						
							|  |  |  |         [ statfs64-f_files >>files ] | 
					
						
							|  |  |  |         [ statfs64-f_ffree >>files-free ] | 
					
						
							| 
									
										
										
										
											2008-12-10 15:28:22 -05:00
										 |  |  |         [ statfs64-f_fsid 2 <direct-uint-array> >array >>id ] | 
					
						
							| 
									
										
										
										
											2008-12-01 15:04:55 -05:00
										 |  |  |         [ statfs64-f_namelen >>namelen ] | 
					
						
							|  |  |  |         [ statfs64-f_frsize >>preferred-block-size ] | 
					
						
							| 
									
										
										
										
											2008-12-02 21:41:57 -05:00
										 |  |  |         ! [ statfs64-f_spare >>spare ] | 
					
						
							| 
									
										
										
										
											2008-12-01 15:04:55 -05:00
										 |  |  |     } cleave ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | M: linux file-system-statvfs ( path -- byte-array )
 | 
					
						
							| 
									
										
										
										
											2009-01-23 19:20:47 -05:00
										 |  |  |     "statvfs64" <c-object> [ statvfs64 io-error ] keep ;
 | 
					
						
							| 
									
										
										
										
											2008-12-01 15:04:55 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | M: linux statvfs>file-system-info ( struct -- statfs )
 | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         [ statvfs64-f_flag >>flags ] | 
					
						
							|  |  |  |         [ statvfs64-f_namemax >>name-max ] | 
					
						
							|  |  |  |     } cleave ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | TUPLE: mtab-entry file-system-name mount-point type options | 
					
						
							|  |  |  | frequency pass-number ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : mtab-csv>mtab-entry ( csv -- mtab-entry )
 | 
					
						
							|  |  |  |     [ mtab-entry new ] dip
 | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         [ first >>file-system-name ] | 
					
						
							|  |  |  |         [ second >>mount-point ] | 
					
						
							|  |  |  |         [ third >>type ] | 
					
						
							|  |  |  |         [ fourth <string-reader> csv first >>options ] | 
					
						
							|  |  |  |         [ 4 swap nth >>frequency ] | 
					
						
							|  |  |  |         [ 5 swap nth >>pass-number ] | 
					
						
							|  |  |  |     } cleave ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : parse-mtab ( -- array )
 | 
					
						
							|  |  |  |     [ | 
					
						
							|  |  |  |         "/etc/mtab" utf8 <file-reader> | 
					
						
							|  |  |  |         CHAR: \s delimiter set csv | 
					
						
							|  |  |  |     ] with-scope
 | 
					
						
							|  |  |  |     [ mtab-csv>mtab-entry ] map ;
 | 
					
						
							| 
									
										
										
										
											2008-12-01 17:28:22 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | M: linux file-systems | 
					
						
							|  |  |  |     parse-mtab [ | 
					
						
							|  |  |  |         [ mount-point>> file-system-info ] keep
 | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             [ file-system-name>> >>device-name ] | 
					
						
							|  |  |  |             [ mount-point>> >>mount-point ] | 
					
						
							|  |  |  |             [ type>> >>type ] | 
					
						
							|  |  |  |         } cleave
 | 
					
						
							|  |  |  |     ] map ;
 | 
					
						
							| 
									
										
										
										
											2008-12-02 21:41:57 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-12-18 19:32:00 -05:00
										 |  |  | : (find-mount-point) ( path mtab-paths -- mtab-entry )
 | 
					
						
							| 
									
										
										
										
											2009-02-18 14:34:45 -05:00
										 |  |  |     2dup at* [ | 
					
						
							| 
									
										
										
										
											2008-12-18 19:32:00 -05:00
										 |  |  |         2nip
 | 
					
						
							|  |  |  |     ] [ | 
					
						
							|  |  |  |         drop [ parent-directory ] dip (find-mount-point) | 
					
						
							|  |  |  |     ] if ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : find-mount-point ( path -- mtab-entry )
 | 
					
						
							| 
									
										
										
										
											2009-02-18 14:34:45 -05:00
										 |  |  |     canonicalize-path | 
					
						
							| 
									
										
										
										
											2008-12-18 19:32:00 -05:00
										 |  |  |     parse-mtab [ [ mount-point>> ] keep ] H{ } map>assoc (find-mount-point) ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-12-02 21:41:57 -05:00
										 |  |  | ERROR: file-system-not-found ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | M: linux file-system-info ( path -- )
 | 
					
						
							|  |  |  |     normalize-path | 
					
						
							|  |  |  |     [ | 
					
						
							|  |  |  |         [ new-file-system-info ] dip
 | 
					
						
							|  |  |  |         [ file-system-statfs statfs>file-system-info ] | 
					
						
							|  |  |  |         [ file-system-statvfs statvfs>file-system-info ] bi
 | 
					
						
							|  |  |  |         file-system-calculations | 
					
						
							|  |  |  |     ] keep
 | 
					
						
							| 
									
										
										
										
											2008-12-18 19:32:00 -05:00
										 |  |  |     find-mount-point | 
					
						
							| 
									
										
										
										
											2008-12-02 21:41:57 -05:00
										 |  |  |     { | 
					
						
							|  |  |  |         [ file-system-name>> >>device-name drop ] | 
					
						
							|  |  |  |         [ mount-point>> >>mount-point drop ] | 
					
						
							|  |  |  |         [ type>> >>type ] | 
					
						
							|  |  |  |     } 2cleave ;
 |