remove stat tests
overhaul unique files
							parent
							
								
									0d86affd2a
								
							
						
					
					
						commit
						c1afb4b093
					
				|  | @ -1,5 +1,5 @@ | ||||||
| USING: io.backend ; | USING: io.backend ; | ||||||
| IN: io.files.unique.backend | IN: io.files.unique.backend | ||||||
| 
 | 
 | ||||||
| HOOK: (make-unique-file) io-backend ( path -- stream ) | HOOK: (make-unique-file) io-backend ( path -- ) | ||||||
| HOOK: temporary-path io-backend ( -- path ) | HOOK: temporary-path io-backend ( -- path ) | ||||||
|  |  | ||||||
|  | @ -6,18 +6,16 @@ ARTICLE: "unique" "Making and using unique files" | ||||||
| "Files:" | "Files:" | ||||||
| { $subsection make-unique-file } | { $subsection make-unique-file } | ||||||
| { $subsection with-unique-file } | { $subsection with-unique-file } | ||||||
| { $subsection with-temporary-file } |  | ||||||
| "Directories:" | "Directories:" | ||||||
| { $subsection make-unique-directory } | { $subsection make-unique-directory } | ||||||
| { $subsection with-unique-directory } | { $subsection with-unique-directory } ; | ||||||
| { $subsection with-temporary-directory } ; |  | ||||||
| 
 | 
 | ||||||
| ABOUT: "unique" | ABOUT: "unique" | ||||||
| 
 | 
 | ||||||
| HELP: make-unique-file ( prefix suffix -- path stream ) | HELP: make-unique-file ( prefix suffix -- path stream ) | ||||||
| { $values { "prefix" "a string" } { "suffix" "a string" } | { $values { "prefix" "a string" } { "suffix" "a string" } | ||||||
| { "path" "a pathname string" } { "stream" "an output stream" } } | { "path" "a pathname string" } } | ||||||
| { $description "Creates a file that is guaranteed not to exist in a platform-specific temporary directory.  The file name is composed of a prefix, a number of random digits and letters, and the suffix.  Returns the full pathname and a " { $link <writer> } " stream." } | { $description "Creates a file that is guaranteed not to exist in a platform-specific temporary directory.  The file name is composed of a prefix, a number of random digits and letters, and the suffix.  Returns the full pathname." } | ||||||
| { $errors "Throws an error if a new unique file cannot be created after a number of tries.  Since each try generates a new random name, the most likely error is incorrect directory permissions on the temporary directory." } | { $errors "Throws an error if a new unique file cannot be created after a number of tries.  Since each try generates a new random name, the most likely error is incorrect directory permissions on the temporary directory." } | ||||||
| { $see-also with-unique-file } ; | { $see-also with-unique-file } ; | ||||||
| 
 | 
 | ||||||
|  | @ -27,24 +25,12 @@ HELP: make-unique-directory ( -- path ) | ||||||
| { $errors "Throws an error if the directory cannot be created after a number of tries.  Since each try generates a new random name, the most likely error is incorrect directory permissions on the temporary directory." } | { $errors "Throws an error if the directory cannot be created after a number of tries.  Since each try generates a new random name, the most likely error is incorrect directory permissions on the temporary directory." } | ||||||
| { $see-also with-unique-directory } ; | { $see-also with-unique-directory } ; | ||||||
| 
 | 
 | ||||||
| HELP: with-unique-file ( quot -- path ) | HELP: with-unique-file ( prefix suffix quot -- ) | ||||||
| { $values { "quot" "a quotation" } { "path" "a pathname string" } } |  | ||||||
| { $description "Creates a file with " { $link make-unique-file } " and calls " { $link with-stream } " on the newly created file.  Returns the full pathname after the stream has been closed." } |  | ||||||
| { $notes "The unique file will remain after calling this word." } |  | ||||||
| { $see-also with-temporary-file } ; |  | ||||||
| 
 |  | ||||||
| HELP: with-unique-directory ( quot -- path ) |  | ||||||
| { $values { "quot" "a quotation" } { "path" "a pathname string" } } |  | ||||||
| { $description "Creates a directory with " { $link make-unique-directory } " and calls " { $link with-directory } " on the newly created directory.  Returns the full pathname after the quotation has been called." } |  | ||||||
| { $notes "The directory will remain after calling this word." } |  | ||||||
| { $see-also with-temporary-directory } ; |  | ||||||
| 
 |  | ||||||
| HELP: with-temporary-file ( quot -- ) |  | ||||||
| { $values { "quot" "a quotation" } } | { $values { "quot" "a quotation" } } | ||||||
| { $description "Creates a file with " { $link make-unique-file } " and calls " { $link with-stream } " on the newly created file.  The file is deleted after the quotation returns." } | { $description "Creates a file with " { $link make-unique-file } " and calls the quotation with the path name on the stack." } | ||||||
| { $see-also with-unique-file } ; | { $notes "The unique file will be deleted after calling this word." } ; | ||||||
| 
 | 
 | ||||||
| HELP: with-temporary-directory ( quot -- ) | HELP: with-unique-directory ( quot -- ) | ||||||
| { $values { "quot" "a quotation" } } | { $values { "quot" "a quotation" } } | ||||||
| { $description "Creates a directory with " { $link make-unique-directory } " and calls " { $link with-directory } " on the newly created directory.  The directory is deleted after the quotation returns." } | { $description "Creates a directory with " { $link make-unique-directory } " and calls the quotation with the pathname on the stack." } | ||||||
| { $see-also with-unique-directory } ; | { $notes "The directory will be deleted after calling this word." } ; | ||||||
|  |  | ||||||
|  | @ -2,8 +2,8 @@ | ||||||
| ! See http://factorcode.org/license.txt for BSD license. | ! See http://factorcode.org/license.txt for BSD license. | ||||||
| USING: kernel math math.bitfields combinators.lib math.parser | USING: kernel math math.bitfields combinators.lib math.parser | ||||||
| random sequences sequences.lib continuations namespaces | random sequences sequences.lib continuations namespaces | ||||||
| io.files io.backend io.nonblocking io arrays | io.files io arrays io.files.unique.backend system | ||||||
| io.files.unique.backend system combinators vocabs.loader ; | combinators vocabs.loader ; | ||||||
| IN: io.files.unique | IN: io.files.unique | ||||||
| 
 | 
 | ||||||
| <PRIVATE | <PRIVATE | ||||||
|  | @ -21,18 +21,15 @@ IN: io.files.unique | ||||||
| : unique-retries ( -- n ) 10 ; inline | : unique-retries ( -- n ) 10 ; inline | ||||||
| PRIVATE> | PRIVATE> | ||||||
| 
 | 
 | ||||||
| : make-unique-file ( prefix suffix -- path stream ) | : make-unique-file ( prefix suffix -- path ) | ||||||
|     temporary-path -rot |     temporary-path -rot | ||||||
|     [ |     [ | ||||||
|         unique-length random-name swap 3append append-path |         unique-length random-name swap 3append append-path | ||||||
|         dup (make-unique-file) |         dup (make-unique-file) | ||||||
|     ] 3curry unique-retries retry ; |     ] 3curry unique-retries retry ; | ||||||
| 
 | 
 | ||||||
| : with-unique-file ( quot -- path ) | : with-unique-file ( prefix suffix quot -- ) | ||||||
|     >r f f make-unique-file r> rot [ with-stream ] dip ; inline |     >r make-unique-file r> keep delete-file ; inline | ||||||
| 
 |  | ||||||
| : with-temporary-file ( quot -- ) |  | ||||||
|     with-unique-file delete-file ; inline |  | ||||||
| 
 | 
 | ||||||
| : make-unique-directory ( -- path ) | : make-unique-directory ( -- path ) | ||||||
|     [ |     [ | ||||||
|  | @ -40,12 +37,9 @@ PRIVATE> | ||||||
|         dup make-directory |         dup make-directory | ||||||
|     ] unique-retries retry ; |     ] unique-retries retry ; | ||||||
| 
 | 
 | ||||||
| : with-unique-directory ( quot -- path ) | : with-unique-directory ( quot -- ) | ||||||
|     >r make-unique-directory r> |     >r make-unique-directory r> | ||||||
|     [ with-directory ] curry keep ; inline |     [ with-directory ] curry keep delete-tree ; inline | ||||||
| 
 |  | ||||||
| : with-temporary-directory ( quot -- ) |  | ||||||
|     with-unique-directory delete-tree ; inline |  | ||||||
| 
 | 
 | ||||||
| { | { | ||||||
|     { [ unix? ] [ "io.unix.files.unique" ] } |     { [ unix? ] [ "io.unix.files.unique" ] } | ||||||
|  |  | ||||||
|  | @ -5,8 +5,7 @@ IN: io.unix.files.unique | ||||||
| : open-unique-flags ( -- flags ) | : open-unique-flags ( -- flags ) | ||||||
|     { O_RDWR O_CREAT O_EXCL } flags ; |     { O_RDWR O_CREAT O_EXCL } flags ; | ||||||
| 
 | 
 | ||||||
| M: unix-io (make-unique-file) ( path -- duplex-stream ) | M: unix-io (make-unique-file) ( path -- ) | ||||||
|     open-unique-flags file-mode open dup io-error |     open-unique-flags file-mode open dup io-error close ; | ||||||
|     <writer> ; |  | ||||||
| 
 | 
 | ||||||
| M: unix-io temporary-path ( -- path ) "/tmp" ; | M: unix-io temporary-path ( -- path ) "/tmp" ; | ||||||
|  |  | ||||||
|  | @ -2,8 +2,9 @@ USING: kernel system io.files.unique.backend | ||||||
| windows.kernel32 io.windows io.nonblocking ; | windows.kernel32 io.windows io.nonblocking ; | ||||||
| IN: io.windows.files.unique | IN: io.windows.files.unique | ||||||
| 
 | 
 | ||||||
| M: windows-io (make-unique-file) ( path -- stream ) | M: windows-io (make-unique-file) ( path -- ) | ||||||
|     GENERIC_WRITE CREATE_NEW 0 open-file 0 <win32-file> <writer> ; |     GENERIC_WRITE CREATE_NEW 0 open-file | ||||||
|  |     CloseHandle win32-error=0/f ; | ||||||
| 
 | 
 | ||||||
| M: windows-io temporary-path ( -- path ) | M: windows-io temporary-path ( -- path ) | ||||||
|     "TEMP" os-env ; |     "TEMP" os-env ; | ||||||
|  |  | ||||||
|  | @ -1,8 +0,0 @@ | ||||||
| USING: kernel tools.test files.unique ; |  | ||||||
| IN: unix.stat.tests |  | ||||||
| 
 |  | ||||||
| [ 123 ] [ |  | ||||||
|     123 CHAR: a <repetition> [ |  | ||||||
|         write |  | ||||||
|     ] with-unique-file file-size>> |  | ||||||
| ] unit-test |  | ||||||
		Loading…
	
		Reference in New Issue