parent
							
								
									260acff952
								
							
						
					
					
						commit
						48e305ee49
					
				| 
						 | 
				
			
			@ -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 )
 | 
			
		||||
| 
						 | 
				
			
			@ -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 ;
 | 
			
		||||
 | 
			
		||||
: <temporary-file> ( 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 <temporary-file> 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
 | 
			
		||||
| 
						 | 
				
			
			@ -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
 | 
			
		||||
    <writer> ;
 | 
			
		||||
 | 
			
		||||
M: unix-io temporary-path ( -- path ) "/tmp" ;
 | 
			
		||||
| 
						 | 
				
			
			@ -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 <writer> ;
 | 
			
		||||
 | 
			
		||||
M: windows-io temporary-path ( -- path )
 | 
			
		||||
    "TEMP" os-env ;
 | 
			
		||||
		Loading…
	
		Reference in New Issue