io.files.temp: use OS tmp dir; add cache-directory

Change temp-directory to return an OS-sanctioned temporary directory instead of "resource:temp". Add a new function cache-directory to return an OS-sanctioned directory for storing staging images etc. Fixes #365.
Windows implementation needs to be finished and tested.
db4
Joe Groff 2012-03-31 14:45:59 -07:00 committed by John Benediktsson
parent ca4d5cbde0
commit 480545a683
8 changed files with 115 additions and 7 deletions

View File

@ -0,0 +1,43 @@
! (c)2012 Joe Groff bsd license
USING: alien.c-types alien.syntax cocoa.plists cocoa.runtime
cocoa.types core-foundation.strings io.directories io.files
io.files.temp io.pathnames kernel memoize sequences system ;
IN: io.files.temp.macosx
<PRIVATE
FUNCTION: id NSTemporaryDirectory ( ) ;
TYPEDEF: NSUInteger NSSearchPathDirectory
CONSTANT: NSCachesDirectory 13
TYPEDEF: NSUInteger NSSearchPathDomainMask
CONSTANT: NSUserDomainMask 1
FUNCTION: id NSSearchPathForDirectoriesInDomains (
NSSearchPathDirectory directory,
NSSearchPathDomainMask domainMask,
char expandTilde
) ;
CONSTANT: factor-bundle-name "org.factorcode.Factor"
: (make-factor-bundle-subdir) ( path -- path )
factor-bundle-name append-path dup make-directories ;
: (first-existing) ( paths -- path )
[ exists? ] map-find nip
[ "no user cache directory found" throw ] unless* ; inline
PRIVATE>
MEMO: (temp-directory) ( -- path )
NSTemporaryDirectory CF>string (make-factor-bundle-subdir) ;
M: macosx temp-directory (temp-directory) ;
MEMO: (cache-directory) ( -- path )
NSCachesDirectory NSUserDomainMask 1 NSSearchPathForDirectoriesInDomains
plist> (first-existing) (make-factor-bundle-subdir) ;
M: macosx cache-directory (cache-directory) ;

View File

@ -0,0 +1 @@
macosx

View File

@ -2,10 +2,16 @@ USING: help.markup help.syntax ;
IN: io.files.temp
ARTICLE: "io.files.temp" "Temporary files"
"Pathnames relative to Factor's temporary files directory:"
"Pathnames relative to the system's temporary file directory:"
{ $subsections
temp-directory
temp-file
}
"Pathnames relative to Factor's cache directory, used to store persistent intermediate files and resources:"
{ $subsections
cache-directory
cache-file
} ;
ABOUT: "io.files.temp"

View File

@ -1,10 +1,19 @@
! Copyright (C) 2008 Slava Pestov, Doug Coleman.
! See http://factorcode.org/license.txt for BSD license.
USING: kernel io.pathnames io.directories ;
! (c)2012 Joe Groff bsd license
USING: combinators io.pathnames kernel system vocabs ;
IN: io.files.temp
: temp-directory ( -- path )
"temp" resource-path dup make-directories ;
HOOK: temp-directory os ( -- path )
HOOK: cache-directory os ( -- path )
: temp-file ( name -- path )
temp-directory prepend-path ;
temp-directory prepend-path ;
: cache-file ( name -- path )
cache-directory prepend-path ;
{
{ [ os windows? ] [ "io.files.temp.windows" ] }
{ [ os macosx? ] [ "io.files.temp.macosx" ] }
{ [ os unix? ] [ "io.files.temp.unix" ] }
[ "unknown io.files.temp platform" throw ]
} cond require

View File

@ -0,0 +1 @@
unix

View File

@ -0,0 +1,14 @@
! (c)2012 Joe Groff bsd license
USING: io.directories io.files.temp io.pathnames kernel memoize
system ;
IN: io.files.temp.unix
MEMO: (temp-directory) ( -- path )
"/tmp/factor-temp" dup make-directories ;
M: unix temp-directory (temp-directory) ;
MEMO: (cache-directory) ( -- path )
home ".factor-cache" append-path dup make-directories ;
M: unix cache-directory (cache-directory) ;

View File

@ -0,0 +1 @@
windows

View File

@ -0,0 +1,33 @@
! (c)2012 Joe Groff bsd license
USING: ;
SPECIALIZED-ARRAY: WCHAR
IN: io.files.temp.windows
<PRIVATE
: (get-temp-directory) ( -- path )
MAX_PATH dup <WCHAR-array> [ GetTempPath ] keep
swap win32-error
utf16n alien>string ;
: (get-appdata-directory) ( -- path )
f
CSIDL_LOCAL_APPDATA CSIDL_FLAG_CREATE bitor
f
0
MAX_PATH <WCHAR-array>
[ SHGetFolderPath ] keep
swap win32-error
utf16n alien>string ;
PRIVATE>
MEMO: (temp-directory) ( -- path )
(get-temp-directory) "factorcode.org\\Factor" append-path dup make-directories ;
M: windows temp-directory (temp-directory) ;
MEMO: (cache-directory) ( -- path )
(get-appdata-directory) "factorcode.org\\Factor" append-path dup make-directories ;
M: windows cache-directory (cache-directory) ;