]> gitweb.factorcode.org Git - factor.git/blob - extra/io/files/unique/unique.factor
Fixing basis -> extra dependencies
[factor.git] / extra / io / files / unique / unique.factor
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 ;
7 IN: io.files.unique
8
9 <PRIVATE
10 : random-letter ( -- ch )
11     26 random { CHAR: a CHAR: A } random + ;
12
13 : random-ch ( -- ch )
14     { t f } random
15     [ 10 random CHAR: 0 + ] [ random-letter ] if ;
16
17 : random-name ( n -- string )
18     [ random-ch ] "" replicate-as ;
19
20 : unique-length ( -- n ) 10 ; inline
21 : unique-retries ( -- n ) 10 ; inline
22 PRIVATE>
23
24 : make-unique-file ( prefix suffix -- path )
25     temporary-path -rot
26     [
27         unique-length random-name swap 3append append-path
28         dup (make-unique-file)
29     ] 3curry unique-retries retry ;
30
31 : with-unique-file ( prefix suffix quot -- )
32     >r make-unique-file r> keep delete-file ; inline
33
34 : make-unique-directory ( -- path )
35     [
36         temporary-path unique-length random-name append-path
37         dup make-directory
38     ] unique-retries retry ;
39
40 : with-unique-directory ( quot -- )
41     >r make-unique-directory r>
42     [ with-directory ] curry keep delete-tree ; inline
43
44 {
45     { [ os unix? ] [ "io.unix.files.unique" ] }
46     { [ os windows? ] [ "io.windows.files.unique" ] }
47 } cond require