2008-02-29 01:10:37 -05:00
|
|
|
! Copyright (C) 2008 Doug Coleman.
|
|
|
|
! See http://factorcode.org/license.txt for BSD license.
|
2008-12-14 22:21:44 -05:00
|
|
|
USING: arrays combinators continuations fry io io.backend
|
2008-12-15 02:13:35 -05:00
|
|
|
io.directories io.directories.hierarchy io.files io.pathnames
|
2008-12-14 22:21:44 -05:00
|
|
|
kernel math math.bitwise math.parser namespaces random
|
|
|
|
sequences system vocabs.loader ;
|
2008-02-29 01:10:37 -05:00
|
|
|
IN: io.files.unique
|
|
|
|
|
2008-12-08 22:32:36 -05:00
|
|
|
HOOK: touch-unique-file io-backend ( path -- )
|
|
|
|
HOOK: temporary-path io-backend ( -- path )
|
|
|
|
|
2008-11-28 17:14:55 -05:00
|
|
|
SYMBOL: unique-length
|
|
|
|
SYMBOL: unique-retries
|
|
|
|
|
|
|
|
10 unique-length set-global
|
|
|
|
10 unique-retries set-global
|
|
|
|
|
2008-02-29 01:10:37 -05:00
|
|
|
<PRIVATE
|
2008-11-28 17:14:55 -05:00
|
|
|
|
2008-02-29 01:10:37 -05:00
|
|
|
: 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 )
|
2008-06-13 02:51:46 -04:00
|
|
|
[ random-ch ] "" replicate-as ;
|
2008-02-29 01:10:37 -05:00
|
|
|
|
|
|
|
PRIVATE>
|
|
|
|
|
2008-12-08 22:32:36 -05:00
|
|
|
: (make-unique-file) ( path prefix suffix -- path )
|
|
|
|
'[
|
|
|
|
_ _ _ unique-length get random-name glue append-path
|
|
|
|
dup touch-unique-file
|
|
|
|
] unique-retries get retry ;
|
|
|
|
|
2008-03-20 17:52:22 -04:00
|
|
|
: make-unique-file ( prefix suffix -- path )
|
2008-12-08 22:32:36 -05:00
|
|
|
[ temporary-path ] 2dip (make-unique-file) ;
|
|
|
|
|
|
|
|
: make-unique-file* ( prefix suffix -- path )
|
|
|
|
[ current-directory get ] 2dip (make-unique-file) ;
|
2008-02-29 01:10:37 -05:00
|
|
|
|
2008-11-28 17:14:55 -05:00
|
|
|
: with-unique-file ( prefix suffix quot: ( path -- ) -- )
|
|
|
|
[ make-unique-file ] dip [ delete-file ] bi ; inline
|
2008-02-29 01:10:37 -05:00
|
|
|
|
|
|
|
: make-unique-directory ( -- path )
|
|
|
|
[
|
2008-11-28 17:14:55 -05:00
|
|
|
temporary-path unique-length get random-name append-path
|
2008-02-29 01:10:37 -05:00
|
|
|
dup make-directory
|
2008-11-28 17:14:55 -05:00
|
|
|
] unique-retries get retry ;
|
2008-02-29 01:10:37 -05:00
|
|
|
|
2008-11-28 17:14:55 -05:00
|
|
|
: with-unique-directory ( quot: ( -- ) -- )
|
|
|
|
[ make-unique-directory ] dip
|
|
|
|
'[ _ with-directory ] [ delete-tree ] bi ; inline
|
2008-02-29 18:44:53 -05:00
|
|
|
|
|
|
|
{
|
2008-12-14 21:03:00 -05:00
|
|
|
{ [ os unix? ] [ "io.files.unique.unix" ] }
|
|
|
|
{ [ os windows? ] [ "io.files.unique.windows" ] }
|
2008-02-29 18:44:53 -05:00
|
|
|
} cond require
|