| 
									
										
										
										
											2008-02-05 14:11:36 -05:00
										 |  |  | ! Copyright (C) 2005, 2008 Slava Pestov. | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | ! See http://factorcode.org/license.txt for BSD license. | 
					
						
							|  |  |  | USING: io.backend io.nonblocking io.unix.backend io.files io | 
					
						
							| 
									
										
										
										
											2008-03-04 22:05:58 -05:00
										 |  |  | unix unix.stat unix.time kernel math continuations math.bitfields | 
					
						
							|  |  |  | byte-arrays alien combinators combinators.cleave calendar | 
					
						
							|  |  |  | io.encodings.binary ;
 | 
					
						
							| 
									
										
										
										
											2008-02-28 13:46:01 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | IN: io.unix.files | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-02-05 14:11:36 -05:00
										 |  |  | M: unix-io cwd | 
					
						
							| 
									
										
										
										
											2008-02-06 00:14:10 -05:00
										 |  |  |     MAXPATHLEN dup <byte-array> swap
 | 
					
						
							|  |  |  |     getcwd [ (io-error) ] unless* ;
 | 
					
						
							| 
									
										
										
										
											2008-02-05 14:11:36 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | M: unix-io cd | 
					
						
							|  |  |  |     chdir io-error ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-01-24 22:45:56 -05:00
										 |  |  | : read-flags O_RDONLY ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | : open-read ( path -- fd )
 | 
					
						
							|  |  |  |     O_RDONLY file-mode open dup io-error ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-02-24 02:37:05 -05:00
										 |  |  | M: unix-io (file-reader) ( path -- stream )
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  |     open-read <reader> ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-02-02 14:29:09 -05:00
										 |  |  | : write-flags { O_WRONLY O_CREAT O_TRUNC } flags ; inline
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : open-write ( path -- fd )
 | 
					
						
							|  |  |  |     write-flags file-mode open dup io-error ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-02-24 02:37:05 -05:00
										 |  |  | M: unix-io (file-writer) ( path -- stream )
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  |     open-write <writer> ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-02-02 14:29:09 -05:00
										 |  |  | : append-flags { O_WRONLY O_APPEND O_CREAT } flags ; inline
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : open-append ( path -- fd )
 | 
					
						
							|  |  |  |     append-flags file-mode open dup io-error | 
					
						
							|  |  |  |     [ dup 0 SEEK_END lseek io-error ] [ ] [ close ] cleanup ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-02-24 02:37:05 -05:00
										 |  |  | M: unix-io (file-appender) ( path -- stream )
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  |     open-append <writer> ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-02-27 15:59:15 -05:00
										 |  |  | : touch-mode | 
					
						
							|  |  |  |     { O_WRONLY O_APPEND O_CREAT O_EXCL } flags ; foldable
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | M: unix-io touch-file ( path -- )
 | 
					
						
							|  |  |  |     touch-mode file-mode open | 
					
						
							|  |  |  |     dup 0 < [ err_no EEXIST = [ err_no io-error ] unless ] when
 | 
					
						
							|  |  |  |     close ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | M: unix-io move-file ( from to -- )
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  |     rename io-error ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | M: unix-io delete-file ( path -- )
 | 
					
						
							|  |  |  |     unlink io-error ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | M: unix-io make-directory ( path -- )
 | 
					
						
							|  |  |  |     OCT: 777 mkdir io-error ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | M: unix-io delete-directory ( path -- )
 | 
					
						
							|  |  |  |     rmdir io-error ;
 | 
					
						
							| 
									
										
										
										
											2008-02-27 15:59:15 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | : (copy-file) ( from to -- )
 | 
					
						
							|  |  |  |     dup parent-directory make-directories | 
					
						
							| 
									
										
										
										
											2008-03-04 22:05:58 -05:00
										 |  |  |     binary <file-writer> [ | 
					
						
							|  |  |  |         swap binary <file-reader> [ | 
					
						
							| 
									
										
										
										
											2008-02-27 15:59:15 -05:00
										 |  |  |             swap stream-copy
 | 
					
						
							|  |  |  |         ] with-disposal | 
					
						
							|  |  |  |     ] with-disposal ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | M: unix-io copy-file ( from to -- )
 | 
					
						
							| 
									
										
										
										
											2008-02-27 17:31:13 -05:00
										 |  |  |     >r dup file-permissions over r> (copy-file) chmod io-error ;
 | 
					
						
							| 
									
										
										
										
											2008-02-29 00:46:27 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | : stat>type ( stat -- type )
 | 
					
						
							|  |  |  |     stat-st_mode { | 
					
						
							|  |  |  |         { [ dup S_ISREG  ] [ +regular-file+     ] } | 
					
						
							|  |  |  |         { [ dup S_ISDIR  ] [ +directory+        ] } | 
					
						
							|  |  |  |         { [ dup S_ISCHR  ] [ +character-device+ ] } | 
					
						
							|  |  |  |         { [ dup S_ISBLK  ] [ +block-device+     ] } | 
					
						
							|  |  |  |         { [ dup S_ISFIFO ] [ +fifo+             ] } | 
					
						
							|  |  |  |         { [ dup S_ISLNK  ] [ +symbolic-link+    ] } | 
					
						
							|  |  |  |         { [ dup S_ISSOCK ] [ +socket+           ] } | 
					
						
							|  |  |  |         { [ t            ] [ +unknown+          ] } | 
					
						
							|  |  |  |       } cond nip ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | M: unix-io file-info ( path -- info )
 | 
					
						
							|  |  |  |     stat* { | 
					
						
							|  |  |  |         [ stat>type ] | 
					
						
							|  |  |  |         [ stat-st_size ] | 
					
						
							|  |  |  |         [ stat-st_mode ] | 
					
						
							|  |  |  |         [ stat-st_mtim timespec-sec seconds unix-1970 time+ ] | 
					
						
							|  |  |  |     } cleave
 | 
					
						
							|  |  |  |     \ file-info construct-boa ;
 | 
					
						
							| 
									
										
										
										
											2008-03-06 13:05:47 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | M: unix-io link-info ( path -- info )
 | 
					
						
							|  |  |  |     lstat* { | 
					
						
							|  |  |  |         [ stat>type ] | 
					
						
							|  |  |  |         [ stat-st_size ] | 
					
						
							|  |  |  |         [ stat-st_mode ] | 
					
						
							|  |  |  |         [ stat-st_mtim timespec-sec seconds unix-1970 time+ ] | 
					
						
							|  |  |  |     } cleave
 | 
					
						
							|  |  |  |     \ file-info construct-boa ;
 |