diff --git a/basis/io/directories/directories-docs.factor b/basis/io/directories/directories-docs.factor index 804a2f4a8d..3871f9be41 100644 --- a/basis/io/directories/directories-docs.factor +++ b/basis/io/directories/directories-docs.factor @@ -46,17 +46,21 @@ HELP: directory-files { $values { "path" "a pathname string" } { "seq" "a sequence of filenames" } } { $description "Outputs the contents of a directory named by " { $snippet "path" } "." } ; -HELP: directory-tree-files -{ $values { "path" "a pathname string" } { "seq" "a sequence of filenames" } } -{ $description "Outputs a sequence of all files and subdirectories inside the directory named by " { $snippet "path" } " or recursively inside its subdirectories." } ; - HELP: with-directory-files { $values { "path" "a pathname string" } { "quot" quotation } } -{ $description "Calls the quotation with the directory file names on the stack and with the directory set as the " { $link current-directory } ". Restores the current directory after the quotation is called." } ; - -HELP: with-directory-tree-files -{ $values { "path" "a pathname string" } { "quot" quotation } } -{ $description "Calls the quotation with the recursive directory file names on the stack and with the directory set as the " { $link current-directory } ". Restores the current directory after the quotation is called." } ; +{ $description "Calls the quotation with the directory file names on the stack and with the directory set as the " { $link current-directory } ". Restores the current directory after the quotation is called." } +{ $examples + "Print all files in your home directory which are larger than a megabyte:" + { $code + """USING: io.directoies io.files.info io.pathnames ; +home [ + [ + dup link-info size>> 20 2^ > + [ print ] [ drop ] if + ] each +] with-directory-files""" + } +} ; HELP: with-directory-entries { $values { "path" "a pathname string" } { "quot" quotation } } diff --git a/basis/io/directories/directories-tests.factor b/basis/io/directories/directories-tests.factor index 742a927b4b..b703421b45 100644 --- a/basis/io/directories/directories-tests.factor +++ b/basis/io/directories/directories-tests.factor @@ -22,24 +22,6 @@ IN: io.directories.tests ] with-directory-files ] unit-test -[ { "classes/tuple/tuple.factor" } ] [ - "resource:core" [ - "." directory-tree-files [ "classes/tuple/tuple.factor" = ] filter - ] with-directory -] unit-test - -[ { "classes/tuple" } ] [ - "resource:core" [ - "." directory-tree-files [ "classes/tuple" = ] filter - ] with-directory -] unit-test - -[ { "classes/tuple/tuple.factor" } ] [ - "resource:core" [ - [ "classes/tuple/tuple.factor" = ] filter - ] with-directory-tree-files -] unit-test - [ ] [ "blahblah" temp-file dup exists? [ delete-directory ] [ drop ] if ] unit-test [ ] [ "blahblah" temp-file make-directory ] unit-test [ t ] [ "blahblah" temp-file file-info directory? ] unit-test diff --git a/basis/io/directories/directories.factor b/basis/io/directories/directories.factor index d12adc5f41..c164f01800 100644 --- a/basis/io/directories/directories.factor +++ b/basis/io/directories/directories.factor @@ -37,30 +37,16 @@ HOOK: (directory-entries) os ( path -- seq ) normalize-path (directory-entries) [ name>> { "." ".." } member? not ] filter ; - + : directory-files ( path -- seq ) directory-entries [ name>> ] map ; -: directory-tree-files ( path -- seq ) - dup directory-entries - [ - dup type>> +directory+ = - [ name>> - [ append-path directory-tree-files ] - [ [ prepend-path ] curry map ] - [ prefix ] tri - ] [ nip name>> 1array ] if - ] with map concat ; - : with-directory-entries ( path quot -- ) '[ "" directory-entries @ ] with-directory ; inline : with-directory-files ( path quot -- ) '[ "" directory-files @ ] with-directory ; inline -: with-directory-tree-files ( path quot -- ) - '[ "" directory-tree-files @ ] with-directory ; inline - ! Touching files HOOK: touch-file io-backend ( path -- ) diff --git a/basis/io/directories/hierarchy/hierarchy-docs.factor b/basis/io/directories/hierarchy/hierarchy-docs.factor index b45fe49d9b..232cad1291 100644 --- a/basis/io/directories/hierarchy/hierarchy-docs.factor +++ b/basis/io/directories/hierarchy/hierarchy-docs.factor @@ -1,6 +1,14 @@ -USING: help.markup help.syntax ; +USING: help.markup help.syntax quotations io.pathnames ; IN: io.directories.hierarchy +HELP: directory-tree-files +{ $values { "path" "a pathname string" } { "seq" "a sequence of filenames" } } +{ $description "Outputs a sequence of all files and subdirectories inside the directory named by " { $snippet "path" } " or recursively inside its subdirectories." } ; + +HELP: with-directory-tree-files +{ $values { "path" "a pathname string" } { "quot" quotation } } +{ $description "Calls the quotation with the recursive directory file names on the stack and with the directory set as the " { $link current-directory } ". Restores the current directory after the quotation is called." } ; + HELP: delete-tree { $values { "path" "a pathname string" } } { $description "Deletes a file or directory, recursing into subdirectories." } @@ -31,6 +39,11 @@ $nl { "Words named " { $snippet { $emphasis "operation" } "-file" } " which work on regular files only." } { "Words named " { $snippet { $emphasis "operation" } "-tree" } " works on directory trees recursively, and also accepts regular files." } } +"Listing directory trees recursively:" +{ $subsections + directory-tree-files + with-directory-tree-files +} "Deleting directory trees recursively:" { $subsections delete-tree } "Copying directory trees recursively:" diff --git a/basis/io/directories/hierarchy/hierarchy.factor b/basis/io/directories/hierarchy/hierarchy.factor index 4a2955ccaf..d39fbc39a2 100644 --- a/basis/io/directories/hierarchy/hierarchy.factor +++ b/basis/io/directories/hierarchy/hierarchy.factor @@ -1,10 +1,24 @@ ! Copyright (C) 2004, 2008 Slava Pestov, Doug Coleman. ! See http://factorcode.org/license.txt for BSD license. -USING: kernel accessors sequences combinators fry io.directories -io.pathnames io.files.info io.files.types io.files.links -io.backend ; +USING: accessors arrays kernel sequences combinators fry +io.directories io.pathnames io.files.info io.files.types +io.files.links io.backend ; IN: io.directories.hierarchy +: directory-tree-files ( path -- seq ) + dup directory-entries + [ + dup type>> +directory+ = + [ name>> + [ append-path directory-tree-files ] + [ [ prepend-path ] curry map ] + [ prefix ] tri + ] [ nip name>> 1array ] if + ] with map concat ; + +: with-directory-tree-files ( path quot -- ) + '[ "" directory-tree-files @ ] with-directory ; inline + : delete-tree ( path -- ) dup link-info directory? [ [ [ [ delete-tree ] each ] with-directory-files ] @@ -28,4 +42,3 @@ DEFER: copy-tree-into : copy-trees-into ( files to -- ) '[ _ copy-tree-into ] each ; - diff --git a/basis/vocabs/metadata/resources/resources.factor b/basis/vocabs/metadata/resources/resources.factor index d8f9bdcffd..1ff002d13a 100644 --- a/basis/vocabs/metadata/resources/resources.factor +++ b/basis/vocabs/metadata/resources/resources.factor @@ -1,6 +1,6 @@ ! (c)2010 Joe Groff bsd license -USING: arrays fry globs io.directories io.files.info -io.pathnames kernel regexp sequences vocabs.loader +USING: arrays fry globs io.directories io.directories.hierarchy +io.files.info io.pathnames kernel regexp sequences vocabs.loader vocabs.metadata ; IN: vocabs.metadata.resources