Permalink
Browse files

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.
  • Loading branch information...
1 parent b0a2027 commit f11e64e5a89c0dcf9d4d1555968b5733afa3491e @jckarter jckarter committed with mrjbq7 Mar 31, 2012
@@ -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) ;
@@ -0,0 +1 @@
+macosx
@@ -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"
@@ -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
@@ -0,0 +1 @@
+unix
@@ -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) ;
@@ -0,0 +1 @@
+windows
@@ -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) ;

0 comments on commit f11e64e

Please sign in to comment.