2015-10-01 8:47 GMT+02:00 HP Wei <hpwe...@gmail.com>:
> Thanks for suggesting to look at the source of (directory-entries)
>
> I see that the iterator over a directory is the word: with-unix-directory
> and (directory-entries) uses produce to collect the entries into a sequence.
>
> I did not find a word in sequences that is similar to produce but does a
> ‘reduce’ action
> — sot that I could simply replace ‘produce’ in the definition of
> (directory-entries).

You can do it like this:

USING: accessors alien.strings classes.struct combinators
continuations io.backend io.directories.unix io.files.info kernel math
sequences unix.ffi ;
FROM: io.directories.unix.linux => next-dirent ;
IN: examples.sequences

DEFER: directory-size

: entry-size-file ( name -- size )
    file-info size>> ;

: entry-size-dir ( name -- size )
    dup { "." ".." } member? [ drop 0 ] [
        normalize-path directory-size
    ] if ;

: entry-size ( dirent* -- size )
    [ d_name>> alien>native-string ] [ d_type>> ] bi {
        { DT_REG [ entry-size-file ] }
        { DT_DIR [ entry-size-dir ] }
        [ 2drop 0 ]
    } case ;

: dirent-size ( unix-dir dirent -- size/f )
    next-dirent [ entry-size ] [ drop f ] if ;

: (directory-size) ( unix-dir dirent -- total )
    2dup dirent-size [ -rot (directory-size) + ] [ 2drop 0 ] if* ;

: directory-size ( path -- total )
    [
        [ dirent <struct> (directory-size) ] with-unix-directory
    ] [ 2drop 0 ] recover ;

The code gets a little tricky because you didn't want to ever load a
full directory listing into memory.


-- 
mvh/best regards Björn Lindqvist

------------------------------------------------------------------------------
_______________________________________________
Factor-talk mailing list
Factor-talk@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/factor-talk

Reply via email to