diff --git a/extra/io/files/temporary/backend/backend.factor b/extra/io/files/temporary/backend/backend.factor new file mode 100644 index 0000000000..0fe4068621 --- /dev/null +++ b/extra/io/files/temporary/backend/backend.factor @@ -0,0 +1,5 @@ +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 new file mode 100644 index 0000000000..d46ddff8c6 --- /dev/null +++ b/extra/io/files/temporary/temporary.factor @@ -0,0 +1,36 @@ +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/unix/files/temporary/temporary.factor b/extra/io/unix/files/temporary/temporary.factor new file mode 100644 index 0000000000..0ac6d7605e --- /dev/null +++ b/extra/io/unix/files/temporary/temporary.factor @@ -0,0 +1,12 @@ +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/windows/files/temporary/temporary.factor b/extra/io/windows/files/temporary/temporary.factor new file mode 100644 index 0000000000..d96ff49e15 --- /dev/null +++ b/extra/io/windows/files/temporary/temporary.factor @@ -0,0 +1,8 @@ +USING: kernel system ; +IN: io.windows.files.temporary + +M: windows-io (temporary-file) ( path -- stream ) + GENERIC_WRITE CREATE_NEW 0 open-file 0 ; + +M: windows-io temporary-path ( -- path ) + "TEMP" os-env ;