| 
									
										
										
										
											2008-12-14 21:03:00 -05:00
										 |  |  | ! Copyright (C) 2008 Slava Pestov. | 
					
						
							|  |  |  | ! See http://factorcode.org/license.txt for BSD license. | 
					
						
							| 
									
										
										
										
											2014-10-24 20:16:40 -04:00
										 |  |  | USING: accessors alien.c-types alien.data alien.strings assocs | 
					
						
							|  |  |  | classes.struct continuations fry io.backend io.backend.unix | 
					
						
							| 
									
										
										
										
											2015-10-01 09:52:51 -04:00
										 |  |  | io.directories io.files io.files.info io.files.info.unix | 
					
						
							|  |  |  | io.files.types kernel libc literals math sequences system unix | 
					
						
							|  |  |  | unix.ffi vocabs ;
 | 
					
						
							| 
									
										
										
										
											2008-12-14 21:03:00 -05:00
										 |  |  | IN: io.directories.unix | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-04-01 15:43:27 -04:00
										 |  |  | CONSTANT: touch-mode flags{ O_WRONLY O_APPEND O_CREAT O_EXCL } | 
					
						
							| 
									
										
										
										
											2008-12-14 21:03:00 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-10-24 20:16:40 -04:00
										 |  |  | CONSTANT: mkdir-mode flags{ USER-ALL GROUP-ALL OTHER-ALL } ! 0o777 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-12-14 21:03:00 -05:00
										 |  |  | M: unix touch-file ( path -- )
 | 
					
						
							|  |  |  |     normalize-path | 
					
						
							|  |  |  |     dup exists? [ touch ] [ | 
					
						
							|  |  |  |         touch-mode file-mode open-file close-file | 
					
						
							|  |  |  |     ] if ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | M: unix move-file ( from to -- )
 | 
					
						
							| 
									
										
										
										
											2010-01-23 10:07:35 -05:00
										 |  |  |     [ normalize-path ] bi@ [ rename ] unix-system-call drop ;
 | 
					
						
							| 
									
										
										
										
											2008-12-14 21:03:00 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | M: unix delete-file ( path -- ) normalize-path unlink-file ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | M: unix make-directory ( path -- )
 | 
					
						
							| 
									
										
										
										
											2014-10-24 20:16:40 -04:00
										 |  |  |     normalize-path mkdir-mode [ mkdir ] unix-system-call drop ;
 | 
					
						
							| 
									
										
										
										
											2008-12-14 21:03:00 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | M: unix delete-directory ( path -- )
 | 
					
						
							| 
									
										
										
										
											2010-01-23 10:07:35 -05:00
										 |  |  |     normalize-path [ rmdir ] unix-system-call drop ;
 | 
					
						
							| 
									
										
										
										
											2008-12-14 21:03:00 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | M: unix copy-file ( from to -- )
 | 
					
						
							| 
									
										
										
										
											2012-07-16 22:38:49 -04:00
										 |  |  |     [ call-next-method ] | 
					
						
							| 
									
										
										
										
											2011-11-18 21:38:39 -05:00
										 |  |  |     [ [ file-permissions ] dip swap set-file-permissions ] 2bi ;
 | 
					
						
							| 
									
										
										
										
											2008-12-14 21:03:00 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | : with-unix-directory ( path quot -- )
 | 
					
						
							| 
									
										
										
										
											2013-07-11 12:46:26 -04:00
										 |  |  |     dupd '[ _ _ | 
					
						
							| 
									
										
										
										
											2014-11-21 12:29:45 -05:00
										 |  |  |         [ opendir dup [ throw-errno ] unless ] dip
 | 
					
						
							| 
									
										
										
										
											2013-07-11 12:46:26 -04:00
										 |  |  |         dupd curry swap '[ _ closedir io-error ] [ ] cleanup
 | 
					
						
							|  |  |  |     ] with-directory ; inline
 | 
					
						
							| 
									
										
										
										
											2008-12-14 21:03:00 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-07-11 12:46:26 -04:00
										 |  |  | : dirent-type>file-type ( type -- file-type )
 | 
					
						
							| 
									
										
										
										
											2012-07-16 22:38:49 -04:00
										 |  |  |     H{ | 
					
						
							| 
									
										
										
										
											2013-03-25 22:47:56 -04:00
										 |  |  |         { $ DT_BLK  +block-device+ } | 
					
						
							| 
									
										
										
										
											2012-07-16 22:38:49 -04:00
										 |  |  |         { $ DT_CHR  +character-device+ } | 
					
						
							|  |  |  |         { $ DT_DIR  +directory+ } | 
					
						
							|  |  |  |         { $ DT_LNK  +symbolic-link+ } | 
					
						
							|  |  |  |         { $ DT_SOCK +socket+ } | 
					
						
							|  |  |  |         { $ DT_FIFO +fifo+ } | 
					
						
							|  |  |  |         { $ DT_REG  +regular-file+ } | 
					
						
							|  |  |  |         { $ DT_WHT  +whiteout+ } | 
					
						
							|  |  |  |     } at* [ drop +unknown+ ] unless ;
 | 
					
						
							| 
									
										
										
										
											2008-12-14 21:03:00 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-07-11 12:46:26 -04:00
										 |  |  | ! An easy way to return +unknown+ is to mount a .iso on OSX and | 
					
						
							|  |  |  | ! call directory-entries on the mount point. | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-04-29 16:22:56 -04:00
										 |  |  | : next-dirent ( DIR* dirent* -- dirent* ? )
 | 
					
						
							|  |  |  |     f void* <ref> [ | 
					
						
							| 
									
										
										
										
											2014-11-21 13:19:12 -05:00
										 |  |  |         readdir_r [ (throw-errno) ] unless-zero
 | 
					
						
							| 
									
										
										
										
											2014-04-29 16:22:56 -04:00
										 |  |  |     ] 2keep void* deref ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : >directory-entry ( dirent* -- directory-entry )
 | 
					
						
							| 
									
										
										
										
											2015-10-01 09:52:51 -04:00
										 |  |  |     [ d_name>> alien>native-string ] | 
					
						
							| 
									
										
										
										
											2014-04-29 16:22:56 -04:00
										 |  |  |     [ d_type>> dirent-type>file-type ] bi
 | 
					
						
							|  |  |  |     dup +unknown+ = [ drop dup file-info type>> ] when
 | 
					
						
							|  |  |  |     <directory-entry> ; inline
 | 
					
						
							| 
									
										
										
										
											2008-12-14 21:03:00 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | M: unix (directory-entries) ( path -- seq )
 | 
					
						
							|  |  |  |     [ | 
					
						
							| 
									
										
										
										
											2014-04-29 16:22:56 -04:00
										 |  |  |         dirent <struct> | 
					
						
							|  |  |  |         '[ _ _ next-dirent ] [ >directory-entry ] produce nip
 | 
					
						
							| 
									
										
										
										
											2008-12-14 21:03:00 -05:00
										 |  |  |     ] with-unix-directory ;
 | 
					
						
							| 
									
										
										
										
											2009-05-07 17:53:32 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | os linux? [ "io.directories.unix.linux" require ] when
 |