2008-10-07 20:25:22 -04:00
|
|
|
! Copyright (C) 2005, 2008 Slava Pestov, Doug Coleman.
|
2007-09-20 18:09:08 -04:00
|
|
|
! See http://factorcode.org/license.txt for BSD license.
|
2008-05-13 19:24:46 -04:00
|
|
|
USING: io.backend io.ports io.unix.backend io.files io
|
2008-03-13 23:08:57 -04:00
|
|
|
unix unix.stat unix.time kernel math continuations
|
2008-09-05 20:29:14 -04:00
|
|
|
math.bitwise byte-arrays alien combinators calendar
|
2008-04-03 19:34:47 -04:00
|
|
|
io.encodings.binary accessors sequences strings system
|
2008-10-07 20:25:22 -04:00
|
|
|
io.files.private destructors vocabs.loader calendar.unix
|
2008-10-18 22:24:14 -04:00
|
|
|
unix.stat alien.c-types arrays unix.users unix.groups
|
|
|
|
environment ;
|
2007-09-20 18:09:08 -04:00
|
|
|
IN: io.unix.files
|
|
|
|
|
2008-04-02 21:09:56 -04:00
|
|
|
M: unix cwd ( -- path )
|
2008-05-14 04:55:33 -04:00
|
|
|
MAXPATHLEN [ <byte-array> ] keep getcwd
|
2008-03-13 23:08:57 -04:00
|
|
|
[ (io-error) ] unless* ;
|
2008-02-05 14:11:36 -05:00
|
|
|
|
2008-05-13 23:36:18 -04:00
|
|
|
M: unix cd ( path -- ) [ chdir ] unix-system-call drop ;
|
2008-02-05 14:11:36 -05:00
|
|
|
|
2008-01-24 22:45:56 -05:00
|
|
|
: read-flags O_RDONLY ; inline
|
|
|
|
|
2008-05-13 19:07:31 -04:00
|
|
|
: open-read ( path -- fd ) O_RDONLY file-mode open-file ;
|
2007-09-20 18:09:08 -04:00
|
|
|
|
2008-04-02 21:09:56 -04:00
|
|
|
M: unix (file-reader) ( path -- stream )
|
2008-07-03 18:44:44 -04:00
|
|
|
open-read <fd> init-fd <input-port> ;
|
2007-09-20 18:09:08 -04:00
|
|
|
|
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
|
|
|
|
2008-05-14 04:55:33 -04:00
|
|
|
: open-write ( path -- fd )
|
|
|
|
write-flags file-mode open-file ;
|
2007-09-20 18:09:08 -04:00
|
|
|
|
2008-04-02 21:09:56 -04:00
|
|
|
M: unix (file-writer) ( path -- stream )
|
2008-07-03 18:44:44 -04:00
|
|
|
open-write <fd> init-fd <output-port> ;
|
2007-09-20 18:09:08 -04:00
|
|
|
|
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 )
|
2008-05-14 04:55:33 -04:00
|
|
|
[
|
2008-05-15 00:23:12 -04:00
|
|
|
append-flags file-mode open-file |dispose
|
2008-05-14 04:55:33 -04:00
|
|
|
dup 0 SEEK_END lseek io-error
|
|
|
|
] with-destructors ;
|
2007-09-20 18:09:08 -04:00
|
|
|
|
2008-04-02 21:09:56 -04:00
|
|
|
M: unix (file-appender) ( path -- stream )
|
2008-07-03 18:44:44 -04:00
|
|
|
open-append <fd> init-fd <output-port> ;
|
2007-09-20 18:09:08 -04:00
|
|
|
|
2008-03-27 19:13:55 -04:00
|
|
|
: touch-mode ( -- n )
|
2008-02-27 15:59:15 -05:00
|
|
|
{ O_WRONLY O_APPEND O_CREAT O_EXCL } flags ; foldable
|
|
|
|
|
2008-04-02 21:09:56 -04:00
|
|
|
M: unix touch-file ( path -- )
|
2008-04-01 20:51:49 -04:00
|
|
|
normalize-path
|
2008-05-12 18:11:40 -04:00
|
|
|
dup exists? [ touch ] [
|
2008-05-14 00:00:41 -04:00
|
|
|
touch-mode file-mode open-file close-file
|
2008-05-09 17:46:24 -04:00
|
|
|
] if ;
|
2008-02-27 15:59:15 -05:00
|
|
|
|
2008-04-02 21:09:56 -04:00
|
|
|
M: unix move-file ( from to -- )
|
2008-04-01 20:51:49 -04:00
|
|
|
[ normalize-path ] bi@ rename io-error ;
|
2007-09-20 18:09:08 -04:00
|
|
|
|
2008-05-13 19:40:41 -04:00
|
|
|
M: unix delete-file ( path -- ) normalize-path unlink-file ;
|
2007-09-20 18:09:08 -04:00
|
|
|
|
2008-04-02 21:09:56 -04:00
|
|
|
M: unix make-directory ( path -- )
|
2008-04-01 20:51:49 -04:00
|
|
|
normalize-path OCT: 777 mkdir io-error ;
|
2007-09-20 18:09:08 -04:00
|
|
|
|
2008-04-02 21:09:56 -04:00
|
|
|
M: unix delete-directory ( path -- )
|
2008-04-01 20:51:49 -04:00
|
|
|
normalize-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 ;
|
|
|
|
|
2008-04-02 21:09:56 -04:00
|
|
|
M: unix copy-file ( from to -- )
|
2008-04-01 20:51:49 -04:00
|
|
|
[ normalize-path ] bi@
|
2008-03-13 23:08:57 -04:00
|
|
|
[ (copy-file) ]
|
2008-04-11 15:09:09 -04:00
|
|
|
[ swap file-info permissions>> chmod io-error ]
|
2008-03-13 23:08:57 -04:00
|
|
|
2bi ;
|
2008-02-29 00:46:27 -05:00
|
|
|
|
2008-10-06 18:17:14 -04:00
|
|
|
HOOK: stat>file-info os ( stat -- file-info )
|
2008-02-29 00:46:27 -05:00
|
|
|
|
2008-10-06 18:17:14 -04:00
|
|
|
HOOK: stat>type os ( stat -- file-info )
|
|
|
|
|
|
|
|
HOOK: new-file-info os ( -- class )
|
|
|
|
|
|
|
|
TUPLE: unix-file-info < file-info uid gid dev ino
|
|
|
|
nlink rdev blocks blocksize ;
|
2008-03-06 13:05:47 -05:00
|
|
|
|
2008-04-02 21:09:56 -04:00
|
|
|
M: unix file-info ( path -- info )
|
2008-05-13 14:37:25 -04:00
|
|
|
normalize-path file-status stat>file-info ;
|
2008-03-13 23:08:57 -04:00
|
|
|
|
2008-04-02 21:09:56 -04:00
|
|
|
M: unix link-info ( path -- info )
|
2008-05-13 14:37:25 -04:00
|
|
|
normalize-path link-status stat>file-info ;
|
2008-03-30 02:13:29 -04:00
|
|
|
|
2008-04-02 21:09:56 -04:00
|
|
|
M: unix make-link ( path1 path2 -- )
|
2008-04-01 20:51:49 -04:00
|
|
|
normalize-path symlink io-error ;
|
2008-03-30 02:13:29 -04:00
|
|
|
|
2008-04-02 21:09:56 -04:00
|
|
|
M: unix read-link ( path -- path' )
|
2008-10-06 18:17:14 -04:00
|
|
|
normalize-path read-symbolic-link ;
|
|
|
|
|
|
|
|
M: unix new-file-info ( -- class ) unix-file-info new ;
|
|
|
|
|
|
|
|
M: unix stat>file-info ( stat -- file-info )
|
|
|
|
[ new-file-info ] dip
|
|
|
|
{
|
|
|
|
[ stat>type >>type ]
|
|
|
|
[ stat-st_size >>size ]
|
|
|
|
[ stat-st_mode >>permissions ]
|
2008-10-06 23:51:06 -04:00
|
|
|
[ stat-st_ctimespec timespec>unix-time >>created ]
|
|
|
|
[ stat-st_mtimespec timespec>unix-time >>modified ]
|
|
|
|
[ stat-st_atimespec timespec>unix-time >>accessed ]
|
2008-10-06 18:17:14 -04:00
|
|
|
[ stat-st_uid >>uid ]
|
|
|
|
[ stat-st_gid >>gid ]
|
|
|
|
[ stat-st_dev >>dev ]
|
|
|
|
[ stat-st_ino >>ino ]
|
|
|
|
[ stat-st_nlink >>nlink ]
|
|
|
|
[ stat-st_rdev >>rdev ]
|
|
|
|
[ stat-st_blocks >>blocks ]
|
|
|
|
[ stat-st_blksize >>blocksize ]
|
|
|
|
} cleave ;
|
|
|
|
|
|
|
|
M: unix stat>type ( stat -- type )
|
|
|
|
stat-st_mode S_IFMT bitand {
|
|
|
|
{ S_IFREG [ +regular-file+ ] }
|
|
|
|
{ S_IFDIR [ +directory+ ] }
|
|
|
|
{ S_IFCHR [ +character-device+ ] }
|
|
|
|
{ S_IFBLK [ +block-device+ ] }
|
|
|
|
{ S_IFIFO [ +fifo+ ] }
|
|
|
|
{ S_IFLNK [ +symbolic-link+ ] }
|
|
|
|
{ S_IFSOCK [ +socket+ ] }
|
|
|
|
[ drop +unknown+ ]
|
|
|
|
} case ;
|
|
|
|
|
|
|
|
! Linux has no extra fields in its stat struct
|
|
|
|
os {
|
2008-10-06 23:51:06 -04:00
|
|
|
{ macosx [ "io.unix.files.bsd" require ] }
|
|
|
|
{ netbsd [ "io.unix.files.bsd" require ] }
|
|
|
|
{ openbsd [ "io.unix.files.bsd" require ] }
|
|
|
|
{ freebsd [ "io.unix.files.bsd" require ] }
|
2008-10-06 18:54:50 -04:00
|
|
|
{ linux [ ] }
|
2008-10-06 18:17:14 -04:00
|
|
|
} case
|
2008-10-07 20:25:22 -04:00
|
|
|
|
|
|
|
<PRIVATE
|
|
|
|
|
|
|
|
: stat-mode ( path -- mode )
|
|
|
|
normalize-path file-status stat-st_mode ;
|
|
|
|
|
|
|
|
: chmod-set-bit ( path mask ? -- )
|
|
|
|
[ dup stat-mode ] 2dip
|
2008-10-08 15:18:50 -04:00
|
|
|
[ bitor ] [ unmask ] if chmod io-error ;
|
2008-10-07 20:25:22 -04:00
|
|
|
|
|
|
|
: file-mode? ( path mask -- ? ) [ stat-mode ] dip mask? ;
|
|
|
|
|
|
|
|
PRIVATE>
|
|
|
|
|
2008-10-08 14:03:55 -04:00
|
|
|
: UID OCT: 0004000 ; inline
|
|
|
|
: GID OCT: 0002000 ; inline
|
|
|
|
: STICKY OCT: 0001000 ; inline
|
|
|
|
: USER-ALL OCT: 0000700 ; inline
|
|
|
|
: USER-READ OCT: 0000400 ; inline
|
|
|
|
: USER-WRITE OCT: 0000200 ; inline
|
|
|
|
: USER-EXECUTE OCT: 0000100 ; inline
|
|
|
|
: GROUP-ALL OCT: 0000070 ; inline
|
|
|
|
: GROUP-READ OCT: 0000040 ; inline
|
|
|
|
: GROUP-WRITE OCT: 0000020 ; inline
|
|
|
|
: GROUP-EXECUTE OCT: 0000010 ; inline
|
|
|
|
: OTHER-ALL OCT: 0000007 ; inline
|
|
|
|
: OTHER-READ OCT: 0000004 ; inline
|
|
|
|
: OTHER-WRITE OCT: 0000002 ; inline
|
|
|
|
: OTHER-EXECUTE OCT: 0000001 ; inline
|
|
|
|
|
2008-10-18 18:29:38 -04:00
|
|
|
GENERIC: uid? ( obj -- ? )
|
|
|
|
GENERIC: gid? ( obj -- ? )
|
|
|
|
GENERIC: sticky? ( obj -- ? )
|
|
|
|
GENERIC: user-read? ( obj -- ? )
|
|
|
|
GENERIC: user-write? ( obj -- ? )
|
|
|
|
GENERIC: user-execute? ( obj -- ? )
|
|
|
|
GENERIC: group-read? ( obj -- ? )
|
|
|
|
GENERIC: group-write? ( obj -- ? )
|
|
|
|
GENERIC: group-execute? ( obj -- ? )
|
|
|
|
GENERIC: other-read? ( obj -- ? )
|
|
|
|
GENERIC: other-write? ( obj -- ? )
|
|
|
|
GENERIC: other-execute? ( obj -- ? )
|
|
|
|
|
|
|
|
M: integer uid? ( integer -- ? ) UID mask? ;
|
|
|
|
M: integer gid? ( integer -- ? ) GID mask? ;
|
|
|
|
M: integer sticky? ( integer -- ? ) STICKY mask? ;
|
|
|
|
M: integer user-read? ( integer -- ? ) USER-READ mask? ;
|
|
|
|
M: integer user-write? ( integer -- ? ) USER-WRITE mask? ;
|
|
|
|
M: integer user-execute? ( integer -- ? ) USER-EXECUTE mask? ;
|
|
|
|
M: integer group-read? ( integer -- ? ) GROUP-READ mask? ;
|
|
|
|
M: integer group-write? ( integer -- ? ) GROUP-WRITE mask? ;
|
|
|
|
M: integer group-execute? ( integer -- ? ) GROUP-EXECUTE mask? ;
|
|
|
|
M: integer other-read? ( integer -- ? ) OTHER-READ mask? ;
|
|
|
|
M: integer other-write? ( integer -- ? ) OTHER-WRITE mask? ;
|
|
|
|
M: integer other-execute? ( integer -- ? ) OTHER-EXECUTE mask? ;
|
2008-10-18 18:48:33 -04:00
|
|
|
|
|
|
|
M: file-info uid? ( file-info -- ? ) permissions>> uid? ;
|
|
|
|
M: file-info gid? ( file-info -- ? ) permissions>> gid? ;
|
|
|
|
M: file-info sticky? ( file-info -- ? ) permissions>> sticky? ;
|
|
|
|
M: file-info user-read? ( file-info -- ? ) permissions>> user-read? ;
|
|
|
|
M: file-info user-write? ( file-info -- ? ) permissions>> user-write? ;
|
|
|
|
M: file-info user-execute? ( file-info -- ? ) permissions>> user-execute? ;
|
|
|
|
M: file-info group-read? ( file-info -- ? ) permissions>> group-read? ;
|
|
|
|
M: file-info group-write? ( file-info -- ? ) permissions>> group-write? ;
|
|
|
|
M: file-info group-execute? ( file-info -- ? ) permissions>> group-execute? ;
|
|
|
|
M: file-info other-read? ( file-info -- ? ) permissions>> other-read? ;
|
|
|
|
M: file-info other-write? ( file-info -- ? ) permissions>> other-write? ;
|
|
|
|
M: file-info other-execute? ( file-info -- ? ) permissions>> other-execute? ;
|
2008-10-18 18:29:38 -04:00
|
|
|
|
|
|
|
M: string uid? ( path -- ? ) UID file-mode? ;
|
|
|
|
M: string gid? ( path -- ? ) GID file-mode? ;
|
|
|
|
M: string sticky? ( path -- ? ) STICKY file-mode? ;
|
|
|
|
M: string user-read? ( path -- ? ) USER-READ file-mode? ;
|
|
|
|
M: string user-write? ( path -- ? ) USER-WRITE file-mode? ;
|
|
|
|
M: string user-execute? ( path -- ? ) USER-EXECUTE file-mode? ;
|
|
|
|
M: string group-read? ( path -- ? ) GROUP-READ file-mode? ;
|
|
|
|
M: string group-write? ( path -- ? ) GROUP-WRITE file-mode? ;
|
|
|
|
M: string group-execute? ( path -- ? ) GROUP-EXECUTE file-mode? ;
|
|
|
|
M: string other-read? ( path -- ? ) OTHER-READ file-mode? ;
|
|
|
|
M: string other-write? ( path -- ? ) OTHER-WRITE file-mode? ;
|
|
|
|
M: string other-execute? ( path -- ? ) OTHER-EXECUTE file-mode? ;
|
2008-10-07 20:25:22 -04:00
|
|
|
|
|
|
|
: set-uid ( path ? -- ) UID swap chmod-set-bit ;
|
|
|
|
: set-gid ( path ? -- ) GID swap chmod-set-bit ;
|
|
|
|
: set-sticky ( path ? -- ) STICKY swap chmod-set-bit ;
|
|
|
|
: set-user-read ( path ? -- ) USER-READ swap chmod-set-bit ;
|
|
|
|
: set-user-write ( path ? -- ) USER-WRITE swap chmod-set-bit ;
|
|
|
|
: set-user-execute ( path ? -- ) USER-EXECUTE swap chmod-set-bit ;
|
|
|
|
: set-group-read ( path ? -- ) GROUP-READ swap chmod-set-bit ;
|
|
|
|
: set-group-write ( path ? -- ) GROUP-WRITE swap chmod-set-bit ;
|
|
|
|
: set-group-execute ( path ? -- ) GROUP-EXECUTE swap chmod-set-bit ;
|
|
|
|
: set-other-read ( path ? -- ) OTHER-READ swap chmod-set-bit ;
|
|
|
|
: set-other-write ( path ? -- ) OTHER-WRITE swap chmod-set-bit ;
|
|
|
|
: set-other-execute ( path ? -- ) OTHER-EXECUTE swap chmod-set-bit ;
|
|
|
|
|
2008-10-08 14:03:55 -04:00
|
|
|
: set-file-permissions ( path n -- )
|
2008-10-07 20:25:22 -04:00
|
|
|
[ normalize-path ] dip chmod io-error ;
|
|
|
|
|
2008-10-08 14:03:55 -04:00
|
|
|
: file-permissions ( path -- n )
|
|
|
|
normalize-path file-info permissions>> ;
|
|
|
|
|
2008-10-07 20:25:22 -04:00
|
|
|
<PRIVATE
|
|
|
|
|
|
|
|
: make-timeval-array ( array -- byte-array )
|
|
|
|
[ length "timeval" <c-array> ] keep
|
|
|
|
dup length [ over [ pick set-timeval-nth ] [ 2drop ] if ] 2each ;
|
|
|
|
|
|
|
|
: timestamp>timeval ( timestamp -- timeval )
|
|
|
|
unix-1970 time- duration>milliseconds make-timeval ;
|
|
|
|
|
|
|
|
: timestamps>byte-array ( timestamps -- byte-array )
|
|
|
|
[ dup [ timestamp>timeval ] when ] map make-timeval-array ;
|
|
|
|
|
|
|
|
PRIVATE>
|
|
|
|
|
|
|
|
: set-file-times ( path timestamps -- )
|
|
|
|
#! set access, write
|
|
|
|
[ normalize-path ] dip
|
|
|
|
timestamps>byte-array utimes io-error ;
|
|
|
|
|
|
|
|
: set-file-access-time ( path timestamp -- )
|
|
|
|
f 2array set-file-times ;
|
|
|
|
|
2008-10-08 15:18:50 -04:00
|
|
|
: set-file-modified-time ( path timestamp -- )
|
2008-10-07 20:25:22 -04:00
|
|
|
f swap 2array set-file-times ;
|
|
|
|
|
|
|
|
: set-file-ids ( path uid gid -- )
|
|
|
|
[ normalize-path ] 2dip
|
|
|
|
[ [ -1 ] unless* ] bi@ chown io-error ;
|
|
|
|
|
2008-10-08 15:18:50 -04:00
|
|
|
GENERIC: set-file-user ( path string/id -- )
|
2008-10-07 20:25:22 -04:00
|
|
|
|
|
|
|
GENERIC: set-file-group ( path string/id -- )
|
|
|
|
|
2008-10-08 15:18:50 -04:00
|
|
|
M: integer set-file-user ( path uid -- )
|
2008-10-07 20:25:22 -04:00
|
|
|
f set-file-ids ;
|
|
|
|
|
2008-10-08 15:18:50 -04:00
|
|
|
M: string set-file-user ( path string -- )
|
|
|
|
user-id f set-file-ids ;
|
2008-10-07 20:25:22 -04:00
|
|
|
|
|
|
|
M: integer set-file-group ( path gid -- )
|
|
|
|
f swap set-file-ids ;
|
|
|
|
|
|
|
|
M: string set-file-group ( path string -- )
|
|
|
|
group-id
|
|
|
|
f swap set-file-ids ;
|
|
|
|
|
2008-10-08 15:18:50 -04:00
|
|
|
: file-user-id ( path -- uid )
|
2008-10-08 14:03:55 -04:00
|
|
|
normalize-path file-info uid>> ;
|
2008-10-07 20:25:22 -04:00
|
|
|
|
2008-10-08 14:03:55 -04:00
|
|
|
: file-username ( path -- string )
|
2008-10-08 15:18:50 -04:00
|
|
|
file-user-id username ;
|
2008-10-07 20:25:22 -04:00
|
|
|
|
2008-10-08 14:03:55 -04:00
|
|
|
: file-group-id ( path -- gid )
|
|
|
|
normalize-path file-info gid>> ;
|
2008-10-07 20:25:22 -04:00
|
|
|
|
2008-10-08 14:03:55 -04:00
|
|
|
: file-group-name ( path -- string )
|
|
|
|
file-group-id group-name ;
|
2008-10-18 22:24:14 -04:00
|
|
|
|
|
|
|
M: unix home "HOME" os-env ;
|