From 499948047a774ca676c20808a4ab147fe259e863 Mon Sep 17 00:00:00 2001 From: Doug Coleman Date: Fri, 29 Feb 2008 00:10:37 -0600 Subject: [PATCH] rename io.files.temporary to io.files.unique write documentation --- .../io/files/temporary/backend/backend.factor | 5 -- extra/io/files/temporary/temporary.factor | 36 ------------- extra/io/files/unique/backend/backend.factor | 5 ++ extra/io/files/unique/unique-docs.factor | 50 +++++++++++++++++++ extra/io/files/unique/unique.factor | 48 ++++++++++++++++++ .../io/unix/files/temporary/temporary.factor | 12 ----- extra/io/unix/files/unique/unique.factor | 12 +++++ extra/io/unix/unix.factor | 4 +- .../temporary.factor => unique/unique.factor} | 4 +- extra/io/windows/windows.factor | 8 +-- 10 files changed, 123 insertions(+), 61 deletions(-) delete mode 100644 extra/io/files/temporary/backend/backend.factor delete mode 100644 extra/io/files/temporary/temporary.factor create mode 100644 extra/io/files/unique/backend/backend.factor create mode 100644 extra/io/files/unique/unique-docs.factor create mode 100644 extra/io/files/unique/unique.factor delete mode 100644 extra/io/unix/files/temporary/temporary.factor create mode 100644 extra/io/unix/files/unique/unique.factor rename extra/io/windows/files/{temporary/temporary.factor => unique/unique.factor} (63%) diff --git a/extra/io/files/temporary/backend/backend.factor b/extra/io/files/temporary/backend/backend.factor deleted file mode 100644 index 0fe4068621..0000000000 --- a/extra/io/files/temporary/backend/backend.factor +++ /dev/null @@ -1,5 +0,0 @@ -USING: io.backend ; -IN: io.files.temporary.backend - -HOOK: (temporary-file) io-backend ( path prefix suffix -- stream path ) -HOOK: temporary-path io-backend ( -- path ) diff --git a/extra/io/files/temporary/temporary.factor b/extra/io/files/temporary/temporary.factor deleted file mode 100644 index d46ddff8c6..0000000000 --- a/extra/io/files/temporary/temporary.factor +++ /dev/null @@ -1,36 +0,0 @@ -USING: kernel math math.bitfields combinators.lib math.parser -random sequences sequences.lib continuations namespaces -io.files io.backend io.nonblocking io arrays -io.files.temporary.backend system combinators vocabs.loader ; -USE: tools.walker -IN: io.files.temporary - -: random-letter ( -- ch ) - 26 random { CHAR: a CHAR: A } random + ; - -: random-ch ( -- ch ) - { t f } random - [ 10 random CHAR: 0 + ] [ random-letter ] if ; - -: random-name ( n -- string ) - [ drop random-ch ] "" map-as ; - -: ( prefix suffix -- path duplex-stream ) - temporary-path -rot - [ 10 random-name swap 3append path+ dup (temporary-file) ] 3curry - 10 retry ; - -: with-temporary-file ( quot -- path ) - >r f f r> with-stream ; - -: temporary-directory ( -- path ) - [ temporary-path 10 random-name path+ dup make-directory ] 10 retry ; - -: with-temporary-directory ( quot -- ) - >r temporary-directory r> - [ with-directory ] 2keep drop delete-tree ; - -{ - { [ unix? ] [ "io.unix.files.temporary" ] } - { [ windows? ] [ "io.windows.files.temporary" ] } -} cond require diff --git a/extra/io/files/unique/backend/backend.factor b/extra/io/files/unique/backend/backend.factor new file mode 100644 index 0000000000..228b6881f9 --- /dev/null +++ b/extra/io/files/unique/backend/backend.factor @@ -0,0 +1,5 @@ +USING: io.backend ; +IN: io.files.unique.backend + +HOOK: (make-unique-file) io-backend ( prefix suffix -- stream path ) +HOOK: temporary-path io-backend ( -- path ) diff --git a/extra/io/files/unique/unique-docs.factor b/extra/io/files/unique/unique-docs.factor new file mode 100644 index 0000000000..61f960d9f7 --- /dev/null +++ b/extra/io/files/unique/unique-docs.factor @@ -0,0 +1,50 @@ +USING: help.markup help.syntax io io.nonblocking kernel math +io.files.unique.private math.parser io.files ; +IN: io.files.unique + +ARTICLE: "unique" "Making and using unique files" +"Files:" +{ $subsection make-unique-file } +{ $subsection with-unique-file } +{ $subsection with-temporary-file } +"Directories:" +{ $subsection make-unique-directory } +{ $subsection with-unique-directory } +{ $subsection with-temporary-directory } ; + +ABOUT: "unique" + +HELP: make-unique-file ( prefix suffix -- path stream ) +{ $values { "prefix" "a string" } { "suffix" "a string" } +{ "path" "a pathname string" } { "stream" "an output stream" } } +{ $description "Creates a file that is guaranteed not to exist in a platform-specific temporary directory. The file name is composed of a prefix, a number of random digits and letters, and the suffix. Returns the full pathname and a " { $link } " stream." } +{ $errors "Throws an error if a new unique file cannot be created after a number of tries. Since each try generates a new random name, the most likely error is incorrect directory permissions on the temporary directory." } +{ $see-also with-unique-file } ; + +HELP: make-unique-directory ( -- path ) +{ $values { "path" "a pathname string" } } +{ $description "Creates a directory that is guaranteed not to exist in a platform-specific temporary directory and returns the full pathname." } +{ $errors "Throws an error if the directory cannot be created after a number of tries. Since each try generates a new random name, the most likely error is incorrect directory permissions on the temporary directory." } +{ $see-also with-unique-directory } ; + +HELP: with-unique-file ( quot -- path ) +{ $values { "quot" "a quotation" } { "path" "a pathname string" } } +{ $description "Creates a file with " { $link make-unique-file } " and calls " { $link with-stream } " on the newly created file. Returns the full pathname after the stream has been closed." } +{ $notes "The unique file will remain after calling this word." } +{ $see-also with-temporary-file } ; + +HELP: with-unique-directory ( quot -- path ) +{ $values { "quot" "a quotation" } { "path" "a pathname string" } } +{ $description "Creates a directory with " { $link make-unique-directory } " and calls " { $link with-directory } " on the newly created directory. Returns the full pathname after the quotation has been called." } +{ $notes "The directory will remain after calling this word." } +{ $see-also with-temporary-directory } ; + +HELP: with-temporary-file ( quot -- ) +{ $values { "quot" "a quotation" } } +{ $description "Creates a file with " { $link make-unique-file } " and calls " { $link with-stream } " on the newly created file. The file is deleted after the quotation returns." } +{ $see-also with-unique-file } ; + +HELP: with-temporary-directory ( quot -- ) +{ $values { "quot" "a quotation" } } +{ $description "Creates a directory with " { $link make-unique-directory } " and calls " { $link with-directory } " on the newly created directory. The directory is deleted after the quotation returns." } +{ $see-also with-unique-directory } ; diff --git a/extra/io/files/unique/unique.factor b/extra/io/files/unique/unique.factor new file mode 100644 index 0000000000..3a1c3c46b8 --- /dev/null +++ b/extra/io/files/unique/unique.factor @@ -0,0 +1,48 @@ +! Copyright (C) 2008 Doug Coleman. +! See http://factorcode.org/license.txt for BSD license. +USING: kernel math math.bitfields combinators.lib math.parser +random sequences sequences.lib continuations namespaces +io.files io.backend io.nonblocking io arrays +io.files.unique.backend system combinators vocabs.loader ; +IN: io.files.unique + + + +: make-unique-file ( prefix suffix -- path stream ) + temporary-path -rot + [ + unique-length random-name swap 3append path+ + dup (make-unique-file) + ] 3curry unique-retries retry ; + +: with-unique-file ( quot -- path ) + >r f f make-unique-file r> with-stream ; inline + +: with-temporary-file ( quot -- ) + with-unique-file delete-file ; inline + +: make-unique-directory ( -- path ) + [ + temporary-path unique-length random-name path+ + dup make-directory + ] unique-retries retry ; + +: with-unique-directory ( quot -- path ) + >r make-unique-directory r> + [ with-directory ] curry keep ; inline + +: with-temporary-directory ( quot -- ) + with-unique-directory delete-tree ; inline diff --git a/extra/io/unix/files/temporary/temporary.factor b/extra/io/unix/files/temporary/temporary.factor deleted file mode 100644 index 0ac6d7605e..0000000000 --- a/extra/io/unix/files/temporary/temporary.factor +++ /dev/null @@ -1,12 +0,0 @@ -USING: kernel io.nonblocking io.unix.backend math.bitfields -unix io.files.temporary.backend ; -IN: io.unix.files.temporary - -: open-temporary-flags ( -- flags ) - { O_RDWR O_CREAT O_EXCL } flags ; - -M: unix-io (temporary-file) ( path -- duplex-stream ) - open-temporary-flags file-mode open dup io-error - ; - -M: unix-io temporary-path ( -- path ) "/tmp" ; diff --git a/extra/io/unix/files/unique/unique.factor b/extra/io/unix/files/unique/unique.factor new file mode 100644 index 0000000000..185d9cd405 --- /dev/null +++ b/extra/io/unix/files/unique/unique.factor @@ -0,0 +1,12 @@ +USING: kernel io.nonblocking io.unix.backend math.bitfields +unix io.files.unique.backend ; +IN: io.unix.files.unique + +: open-unique-flags ( -- flags ) + { O_RDWR O_CREAT O_EXCL } flags ; + +M: unix-io (make-unique-file) ( path -- duplex-stream ) + open-unique-flags file-mode open dup io-error + ; + +M: unix-io temporary-path ( -- path ) "/tmp" ; diff --git a/extra/io/unix/unix.factor b/extra/io/unix/unix.factor index e740561cf9..a328a3baf4 100755 --- a/extra/io/unix/unix.factor +++ b/extra/io/unix/unix.factor @@ -1,6 +1,6 @@ USING: io.unix.backend io.unix.files io.unix.sockets io.timeouts -io.unix.launcher io.unix.mmap io.backend combinators namespaces -system vocabs.loader sequences ; +io.unix.launcher io.unix.mmap io.backend io.files.temporary +combinators namespaces system vocabs.loader sequences ; "io.unix." os append require diff --git a/extra/io/windows/files/temporary/temporary.factor b/extra/io/windows/files/unique/unique.factor similarity index 63% rename from extra/io/windows/files/temporary/temporary.factor rename to extra/io/windows/files/unique/unique.factor index d96ff49e15..01e654751e 100644 --- a/extra/io/windows/files/temporary/temporary.factor +++ b/extra/io/windows/files/unique/unique.factor @@ -1,7 +1,7 @@ USING: kernel system ; -IN: io.windows.files.temporary +IN: io.windows.files.unique -M: windows-io (temporary-file) ( path -- stream ) +M: windows-io (make-unique-file) ( path -- stream ) GENERIC_WRITE CREATE_NEW 0 open-file 0 ; M: windows-io temporary-path ( -- path ) diff --git a/extra/io/windows/windows.factor b/extra/io/windows/windows.factor index 9f2f2db0a5..2c2ad66221 100755 --- a/extra/io/windows/windows.factor +++ b/extra/io/windows/windows.factor @@ -2,10 +2,10 @@ ! See http://factorcode.org/license.txt for BSD license. USING: alien alien.c-types arrays destructors io io.backend io.buffers io.files io.nonblocking io.sockets io.binary -io.sockets.impl windows.errors strings io.streams.duplex kernel -math namespaces sequences windows windows.kernel32 -windows.shell32 windows.types windows.winsock splitting -continuations math.bitfields ; +io.sockets.impl io.windows.files.temporary windows.errors +strings io.streams.duplex kernel math namespaces sequences +windows windows.kernel32 windows.shell32 windows.types +windows.winsock splitting continuations math.bitfields ; IN: io.windows TUPLE: windows-nt-io ;