| 
									
										
										
										
											2009-01-13 16:48:59 -05:00
										 |  |  | ! Copyright (C) 2008, 2009 Doug Coleman. | 
					
						
							| 
									
										
										
										
											2008-11-14 01:25:00 -05:00
										 |  |  | ! See http://factorcode.org/license.txt for BSD license. | 
					
						
							| 
									
										
										
										
											2009-01-13 16:48:59 -05:00
										 |  |  | USING: accessors arrays calendar combinators fry io io.directories | 
					
						
							|  |  |  | io.files.info kernel math math.parser prettyprint sequences system | 
					
						
							| 
									
										
										
										
											2011-11-02 14:23:41 -04:00
										 |  |  | vocabs sorting.slots calendar.format ;
 | 
					
						
							| 
									
										
										
										
											2008-12-02 22:49:59 -05:00
										 |  |  | IN: tools.files | 
					
						
							| 
									
										
										
										
											2008-11-14 01:25:00 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-11-14 01:51:14 -05:00
										 |  |  | <PRIVATE
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-01-13 16:48:59 -05:00
										 |  |  | : dir-or-size ( file-info -- str )
 | 
					
						
							|  |  |  |     dup directory? [ | 
					
						
							| 
									
										
										
										
											2009-01-29 23:19:07 -05:00
										 |  |  |         drop "<DIR>" 20 CHAR: \s pad-tail
 | 
					
						
							| 
									
										
										
										
											2009-01-13 16:48:59 -05:00
										 |  |  |     ] [ | 
					
						
							| 
									
										
										
										
											2009-01-29 23:19:07 -05:00
										 |  |  |         size>> number>string 20 CHAR: \s pad-head
 | 
					
						
							| 
									
										
										
										
											2009-01-13 16:48:59 -05:00
										 |  |  |     ] if ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : listing-time ( timestamp -- string )
 | 
					
						
							| 
									
										
										
										
											2008-11-14 01:25:00 -05:00
										 |  |  |     [ hour>> ] [ minute>> ] bi
 | 
					
						
							| 
									
										
										
										
											2009-01-29 23:19:07 -05:00
										 |  |  |     [ number>string 2 CHAR: 0 pad-head ] bi@ ":" glue ;
 | 
					
						
							| 
									
										
										
										
											2008-11-14 01:25:00 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-01-13 19:44:47 -05:00
										 |  |  | : listing-date ( timestamp -- string )
 | 
					
						
							| 
									
										
										
										
											2008-11-14 01:25:00 -05:00
										 |  |  |     [ month>> month-abbreviation ] | 
					
						
							| 
									
										
										
										
											2009-01-29 23:19:07 -05:00
										 |  |  |     [ day>> number>string 2 CHAR: \s pad-head ] | 
					
						
							| 
									
										
										
										
											2008-11-14 01:25:00 -05:00
										 |  |  |     [ | 
					
						
							|  |  |  |         dup year>> dup now year>> =
 | 
					
						
							| 
									
										
										
										
											2009-01-13 16:48:59 -05:00
										 |  |  |         [ drop listing-time ] [ nip number>string ] if
 | 
					
						
							| 
									
										
										
										
											2009-01-29 23:19:07 -05:00
										 |  |  |         5 CHAR: \s pad-head
 | 
					
						
							| 
									
										
										
										
											2008-11-14 01:25:00 -05:00
										 |  |  |     ] tri 3array " " join ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : read>string ( ? -- string ) "r" "-" ? ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : write>string ( ? -- string ) "w" "-" ? ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-11-14 01:44:01 -05:00
										 |  |  | : execute>string ( ? -- string ) "x" "-" ? ; inline
 | 
					
						
							| 
									
										
										
										
											2008-11-14 01:25:00 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-11-14 01:51:14 -05:00
										 |  |  | PRIVATE>
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-02-17 13:36:27 -05:00
										 |  |  | SYMBOLS: +file-name+ +file-name/type+ +permissions+ +file-type+ | 
					
						
							|  |  |  | +nlinks+ +file-size+ +file-date+ +file-time+ +file-datetime+ | 
					
						
							|  |  |  | +uid+ +gid+ +user+ +group+ +link-target+ +unix-datetime+ | 
					
						
							|  |  |  | +directory-or-size+ ;
 | 
					
						
							| 
									
										
										
										
											2009-01-13 16:48:59 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | TUPLE: listing-tool path specs sort ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | TUPLE: file-listing directory-entry file-info ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | C: <file-listing> file-listing | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : <listing-tool> ( path -- listing-tool )
 | 
					
						
							|  |  |  |     listing-tool new
 | 
					
						
							|  |  |  |         swap >>path | 
					
						
							| 
									
										
										
										
											2009-02-17 13:36:27 -05:00
										 |  |  |         { +file-name+ } >>specs ;
 | 
					
						
							| 
									
										
										
										
											2009-01-13 16:48:59 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | : list-slow? ( listing-tool -- ? )
 | 
					
						
							| 
									
										
										
										
											2009-02-17 13:36:27 -05:00
										 |  |  |     specs>> { +file-name+ } sequence= not ;
 | 
					
						
							| 
									
										
										
										
											2009-01-13 16:48:59 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | ERROR: unknown-file-spec symbol ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | HOOK: file-spec>string os ( file-listing spec -- string )
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | M: object file-spec>string ( file-listing spec -- string )
 | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2009-02-17 13:36:27 -05:00
										 |  |  |         { +file-name+ [ directory-entry>> name>> ] } | 
					
						
							|  |  |  |         { +directory-or-size+ [ file-info>> dir-or-size ] } | 
					
						
							|  |  |  |         { +file-size+ [ file-info>> size>> number>string ] } | 
					
						
							|  |  |  |         { +file-date+ [ file-info>> modified>> listing-date ] } | 
					
						
							|  |  |  |         { +file-time+ [ file-info>> modified>> listing-time ] } | 
					
						
							|  |  |  |         { +file-datetime+ [ file-info>> modified>> timestamp>ymdhms ] } | 
					
						
							| 
									
										
										
										
											2015-08-13 19:13:05 -04:00
										 |  |  |         [ unknown-file-spec ] | 
					
						
							| 
									
										
										
										
											2009-01-13 16:48:59 -05:00
										 |  |  |     } case ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : list-files-fast ( listing-tool -- array )
 | 
					
						
							|  |  |  |     path>> [ [ name>> 1array ] map ] with-directory-entries ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : list-files-slow ( listing-tool -- array )
 | 
					
						
							|  |  |  |     [ path>> ] [ sort>> ] [ specs>> ] tri '[ | 
					
						
							| 
									
										
										
										
											2009-05-15 23:31:50 -04:00
										 |  |  |         [ dup name>> link-info file-listing boa ] map
 | 
					
						
							|  |  |  |         _ [ sort-by ] when*
 | 
					
						
							|  |  |  |         [ _ [ file-spec>string ] with map ] map
 | 
					
						
							| 
									
										
										
										
											2009-01-13 16:48:59 -05:00
										 |  |  |     ] with-directory-entries ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-29 19:43:15 -04:00
										 |  |  | : list-files ( listing-tool -- array )
 | 
					
						
							| 
									
										
										
										
											2009-01-13 16:48:59 -05:00
										 |  |  |     dup list-slow? [ list-files-slow ] [ list-files-fast ] if ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | HOOK: (directory.) os ( path -- lines )
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : directory. ( path -- ) (directory.) simple-table. ;
 | 
					
						
							| 
									
										
										
										
											2008-11-14 01:25:00 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-02-17 13:36:27 -05:00
										 |  |  | SYMBOLS: +device-name+ +mount-point+ +type+ | 
					
						
							|  |  |  | +available-space+ +free-space+ +used-space+ +total-space+ | 
					
						
							|  |  |  | +percent-used+ +percent-free+ ;
 | 
					
						
							| 
									
										
										
										
											2008-12-08 00:51:13 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | : percent ( real -- integer ) 100 * >integer ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : file-system-spec ( file-system-info obj -- str )
 | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2009-02-17 13:36:27 -05:00
										 |  |  |         { +device-name+ [ device-name>> "" or ] } | 
					
						
							|  |  |  |         { +mount-point+ [ mount-point>> "" or ] } | 
					
						
							|  |  |  |         { +type+ [ type>> "" or ] } | 
					
						
							|  |  |  |         { +available-space+ [ available-space>> 0 or ] } | 
					
						
							|  |  |  |         { +free-space+ [ free-space>> 0 or ] } | 
					
						
							|  |  |  |         { +used-space+ [ used-space>> 0 or ] } | 
					
						
							|  |  |  |         { +total-space+ [ total-space>> 0 or ] } | 
					
						
							|  |  |  |         { +percent-used+ [ | 
					
						
							| 
									
										
										
										
											2008-12-09 01:58:34 -05:00
										 |  |  |             [ used-space>> ] [ total-space>> ] bi
 | 
					
						
							| 
									
										
										
										
											2009-01-13 16:48:59 -05:00
										 |  |  |             [ 0 or ] bi@ dup 0 =
 | 
					
						
							| 
									
										
										
										
											2008-12-08 00:51:13 -05:00
										 |  |  |             [ 2drop 0 ] [ / percent ] if
 | 
					
						
							|  |  |  |         ] } | 
					
						
							|  |  |  |     } case ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : file-systems-info ( spec -- seq )
 | 
					
						
							|  |  |  |     file-systems swap '[ _ [ file-system-spec ] with map ] map ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-12-09 18:50:47 -05:00
										 |  |  | : print-file-systems ( spec -- )
 | 
					
						
							| 
									
										
										
										
											2008-12-08 00:51:13 -05:00
										 |  |  |     [ file-systems-info ] | 
					
						
							|  |  |  |     [ [ unparse ] map ] bi prefix simple-table. ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-05-15 23:31:50 -04:00
										 |  |  | CONSTANT: default-file-systems-spec | 
					
						
							| 
									
										
										
										
											2009-01-13 16:48:59 -05:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2009-02-17 13:36:27 -05:00
										 |  |  |         +device-name+ +available-space+ +free-space+ +used-space+ | 
					
						
							|  |  |  |         +total-space+ +percent-used+ +mount-point+ | 
					
						
							| 
									
										
										
										
											2009-05-15 23:31:50 -04:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : file-systems. ( -- )
 | 
					
						
							|  |  |  |     default-file-systems-spec print-file-systems ;
 | 
					
						
							| 
									
										
										
										
											2008-12-09 18:50:47 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-11-14 01:25:00 -05:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2008-12-02 22:49:59 -05:00
										 |  |  |     { [ os unix? ] [ "tools.files.unix" ] } | 
					
						
							|  |  |  |     { [ os windows? ] [ "tools.files.windows" ] } | 
					
						
							| 
									
										
										
										
											2009-01-13 19:44:47 -05:00
										 |  |  | } cond require |