remove stat tests
overhaul unique files
parent
0d86affd2a
commit
c1afb4b093
|
@ -1,5 +1,5 @@
|
|||
USING: io.backend ;
|
||||
IN: io.files.unique.backend
|
||||
|
||||
HOOK: (make-unique-file) io-backend ( path -- stream )
|
||||
HOOK: (make-unique-file) io-backend ( path -- )
|
||||
HOOK: temporary-path io-backend ( -- path )
|
||||
|
|
|
@ -6,18 +6,16 @@ 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 } ;
|
||||
{ $subsection with-unique-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 <writer> } " stream." }
|
||||
{ "path" "a pathname string" } }
|
||||
{ $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." }
|
||||
{ $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 } ;
|
||||
|
||||
|
@ -27,24 +25,12 @@ HELP: make-unique-directory ( -- path )
|
|||
{ $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 -- )
|
||||
HELP: with-unique-file ( prefix suffix 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 } ;
|
||||
{ $description "Creates a file with " { $link make-unique-file } " and calls the quotation with the path name on the stack." }
|
||||
{ $notes "The unique file will be deleted after calling this word." } ;
|
||||
|
||||
HELP: with-temporary-directory ( quot -- )
|
||||
HELP: with-unique-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 } ;
|
||||
{ $description "Creates a directory with " { $link make-unique-directory } " and calls the quotation with the pathname on the stack." }
|
||||
{ $notes "The directory will be deleted after calling this word." } ;
|
||||
|
|
|
@ -2,8 +2,8 @@
|
|||
! 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 ;
|
||||
io.files io arrays io.files.unique.backend system
|
||||
combinators vocabs.loader ;
|
||||
IN: io.files.unique
|
||||
|
||||
<PRIVATE
|
||||
|
@ -21,18 +21,15 @@ IN: io.files.unique
|
|||
: unique-retries ( -- n ) 10 ; inline
|
||||
PRIVATE>
|
||||
|
||||
: make-unique-file ( prefix suffix -- path stream )
|
||||
: make-unique-file ( prefix suffix -- path )
|
||||
temporary-path -rot
|
||||
[
|
||||
unique-length random-name swap 3append append-path
|
||||
dup (make-unique-file)
|
||||
] 3curry unique-retries retry ;
|
||||
|
||||
: with-unique-file ( quot -- path )
|
||||
>r f f make-unique-file r> rot [ with-stream ] dip ; inline
|
||||
|
||||
: with-temporary-file ( quot -- )
|
||||
with-unique-file delete-file ; inline
|
||||
: with-unique-file ( prefix suffix quot -- )
|
||||
>r make-unique-file r> keep delete-file ; inline
|
||||
|
||||
: make-unique-directory ( -- path )
|
||||
[
|
||||
|
@ -40,12 +37,9 @@ PRIVATE>
|
|||
dup make-directory
|
||||
] unique-retries retry ;
|
||||
|
||||
: with-unique-directory ( quot -- path )
|
||||
: with-unique-directory ( quot -- )
|
||||
>r make-unique-directory r>
|
||||
[ with-directory ] curry keep ; inline
|
||||
|
||||
: with-temporary-directory ( quot -- )
|
||||
with-unique-directory delete-tree ; inline
|
||||
[ with-directory ] curry keep delete-tree ; inline
|
||||
|
||||
{
|
||||
{ [ unix? ] [ "io.unix.files.unique" ] }
|
||||
|
|
|
@ -5,8 +5,7 @@ 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
|
||||
<writer> ;
|
||||
M: unix-io (make-unique-file) ( path -- )
|
||||
open-unique-flags file-mode open dup io-error close ;
|
||||
|
||||
M: unix-io temporary-path ( -- path ) "/tmp" ;
|
||||
|
|
|
@ -2,8 +2,9 @@ USING: kernel system io.files.unique.backend
|
|||
windows.kernel32 io.windows io.nonblocking ;
|
||||
IN: io.windows.files.unique
|
||||
|
||||
M: windows-io (make-unique-file) ( path -- stream )
|
||||
GENERIC_WRITE CREATE_NEW 0 open-file 0 <win32-file> <writer> ;
|
||||
M: windows-io (make-unique-file) ( path -- )
|
||||
GENERIC_WRITE CREATE_NEW 0 open-file
|
||||
CloseHandle win32-error=0/f ;
|
||||
|
||||
M: windows-io temporary-path ( -- path )
|
||||
"TEMP" os-env ;
|
||||
|
|
|
@ -1,8 +0,0 @@
|
|||
USING: kernel tools.test files.unique ;
|
||||
IN: unix.stat.tests
|
||||
|
||||
[ 123 ] [
|
||||
123 CHAR: a <repetition> [
|
||||
write
|
||||
] with-unique-file file-size>>
|
||||
] unit-test
|
Loading…
Reference in New Issue