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.
--- /dev/null
+! (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) ;
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"
-! 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 ;
\ No newline at end of file
+ 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
--- /dev/null
+! (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) ;
--- /dev/null
+! (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) ;