Implement file-info
parent
b71d7bc422
commit
c9b73f062b
|
@ -1,10 +1,11 @@
|
||||||
! Copyright (C) 2004, 2008 Slava Pestov.
|
! Copyright (C) 2004, 2008 Slava Pestov.
|
||||||
! See http://factorcode.org/license.txt for BSD license.
|
! See http://factorcode.org/license.txt for BSD license.
|
||||||
IN: io.files
|
|
||||||
USING: io.backend io.files.private io hashtables kernel math
|
USING: io.backend io.files.private io hashtables kernel math
|
||||||
memory namespaces sequences strings assocs arrays definitions
|
memory namespaces sequences strings assocs arrays definitions
|
||||||
system combinators splitting sbufs continuations ;
|
system combinators splitting sbufs continuations ;
|
||||||
|
|
||||||
|
IN: io.files
|
||||||
|
|
||||||
! Pathnames
|
! Pathnames
|
||||||
: path-separator? ( ch -- ? ) windows? "/\\" "/" ? member? ;
|
: path-separator? ( ch -- ? ) windows? "/\\" "/" ? member? ;
|
||||||
|
|
||||||
|
@ -50,6 +51,19 @@ TUPLE: no-parent-directory path ;
|
||||||
{ [ t ] [ drop ] }
|
{ [ t ] [ drop ] }
|
||||||
} cond ;
|
} cond ;
|
||||||
|
|
||||||
|
TUPLE: file-info type size permissions modified ;
|
||||||
|
|
||||||
|
HOOK: file-info io-backend ( path -- info )
|
||||||
|
|
||||||
|
SYMBOL: +regular-file+
|
||||||
|
SYMBOL: +directory+
|
||||||
|
SYMBOL: +character-device+
|
||||||
|
SYMBOL: +block-device+
|
||||||
|
SYMBOL: +fifo+
|
||||||
|
SYMBOL: +symbolic-link+
|
||||||
|
SYMBOL: +socket+
|
||||||
|
SYMBOL: +unknown+
|
||||||
|
|
||||||
! File metadata
|
! File metadata
|
||||||
: stat ( path -- directory? permissions length modified )
|
: stat ( path -- directory? permissions length modified )
|
||||||
normalize-pathname (stat) ;
|
normalize-pathname (stat) ;
|
||||||
|
|
|
@ -1,8 +1,8 @@
|
||||||
! Copyright (C) 2005, 2008 Slava Pestov.
|
! Copyright (C) 2005, 2008 Slava Pestov.
|
||||||
! See http://factorcode.org/license.txt for BSD license.
|
! See http://factorcode.org/license.txt for BSD license.
|
||||||
USING: io.backend io.nonblocking io.unix.backend io.files io
|
USING: io.backend io.nonblocking io.unix.backend io.files io
|
||||||
unix unix.stat kernel math continuations math.bitfields byte-arrays
|
unix unix.stat kernel math continuations math.bitfields byte-arrays
|
||||||
alien ;
|
alien combinators combinators.cleave calendar ;
|
||||||
|
|
||||||
IN: io.unix.files
|
IN: io.unix.files
|
||||||
|
|
||||||
|
@ -68,3 +68,24 @@ M: unix-io delete-directory ( path -- )
|
||||||
|
|
||||||
M: unix-io copy-file ( from to -- )
|
M: unix-io copy-file ( from to -- )
|
||||||
>r dup file-permissions over r> (copy-file) chmod io-error ;
|
>r dup file-permissions over r> (copy-file) chmod io-error ;
|
||||||
|
|
||||||
|
: 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 ;
|
||||||
|
|
|
@ -27,3 +27,7 @@ C-STRUCT: stat
|
||||||
|
|
||||||
FUNCTION: int stat ( char* pathname, stat* buf ) ;
|
FUNCTION: int stat ( char* pathname, stat* buf ) ;
|
||||||
FUNCTION: int lstat ( char* pathname, stat* buf ) ;
|
FUNCTION: int lstat ( char* pathname, stat* buf ) ;
|
||||||
|
|
||||||
|
: stat-st_atim stat-st_atimespec ;
|
||||||
|
: stat-st_mtim stat-st_mtimespec ;
|
||||||
|
: stat-st_ctim stat-st_ctimespec ;
|
|
@ -1,5 +1,6 @@
|
||||||
|
|
||||||
USING: kernel system combinators alien.syntax math vocabs.loader ;
|
USING: kernel system combinators alien.syntax alien.c-types
|
||||||
|
math io.unix.backend vocabs.loader ;
|
||||||
|
|
||||||
IN: unix.stat
|
IN: unix.stat
|
||||||
|
|
||||||
|
@ -55,11 +56,21 @@ FUNCTION: int fchmod ( int fd, mode_t mode ) ;
|
||||||
|
|
||||||
FUNCTION: int mkdir ( char* path, mode_t mode ) ;
|
FUNCTION: int mkdir ( char* path, mode_t mode ) ;
|
||||||
|
|
||||||
|
! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
||||||
|
<<
|
||||||
|
os
|
||||||
|
{
|
||||||
|
{ "linux" [ "unix.stat.linux" require ] }
|
||||||
|
{ "macosx" [ "unix.stat.macosx" require ] }
|
||||||
|
[ drop ]
|
||||||
|
}
|
||||||
|
case
|
||||||
|
>>
|
||||||
! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
||||||
|
|
||||||
{
|
: check-status ( n -- ) io-error ;
|
||||||
{ [ linux? ] [ "unix.stat.linux" require ] }
|
|
||||||
{ [ t ] [ ] }
|
|
||||||
}
|
|
||||||
cond
|
|
||||||
|
|
||||||
|
: stat* ( pathname -- stat )
|
||||||
|
"stat" <c-object> dup >r
|
||||||
|
stat check-status
|
||||||
|
r> ;
|
||||||
|
|
Loading…
Reference in New Issue