1 ! Copyright (C) 2008 Doug Coleman.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: kernel math math.bitwise combinators.lib math.parser
4 random sequences sequences.lib continuations namespaces
5 io.files io arrays io.files.unique.backend system
6 combinators vocabs.loader ;
10 : random-letter ( -- ch )
11 26 random { CHAR: a CHAR: A } random + ;
15 [ 10 random CHAR: 0 + ] [ random-letter ] if ;
17 : random-name ( n -- string )
18 [ random-ch ] "" replicate-as ;
20 : unique-length ( -- n ) 10 ; inline
21 : unique-retries ( -- n ) 10 ; inline
24 : make-unique-file ( prefix suffix -- path )
27 unique-length random-name swap 3append append-path
28 dup (make-unique-file)
29 ] 3curry unique-retries retry ;
31 : with-unique-file ( prefix suffix quot -- )
32 >r make-unique-file r> keep delete-file ; inline
34 : make-unique-directory ( -- path )
36 temporary-path unique-length random-name append-path
38 ] unique-retries retry ;
40 : with-unique-directory ( quot -- )
41 >r make-unique-directory r>
42 [ with-directory ] curry keep delete-tree ; inline
45 { [ os unix? ] [ "io.unix.files.unique" ] }
46 { [ os windows? ] [ "io.windows.files.unique" ] }