]> gitweb.factorcode.org Git - factor.git/commitdiff
io.files.temp: use OS tmp dir; add cache-directory
authorJoe Groff <arcata@gmail.com>
Sat, 31 Mar 2012 21:45:59 +0000 (14:45 -0700)
committerJohn Benediktsson <mrjbq7@gmail.com>
Mon, 16 Apr 2012 18:55:22 +0000 (11:55 -0700)
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.

basis/io/files/temp/macosx/macosx.factor [new file with mode: 0644]
basis/io/files/temp/macosx/platforms.txt [new file with mode: 0644]
basis/io/files/temp/temp-docs.factor
basis/io/files/temp/temp.factor
basis/io/files/temp/unix/platforms.txt [new file with mode: 0644]
basis/io/files/temp/unix/unix.factor [new file with mode: 0644]
basis/io/files/temp/windows/platforms.txt [new file with mode: 0644]
basis/io/files/temp/windows/windows.factor [new file with mode: 0644]

diff --git a/basis/io/files/temp/macosx/macosx.factor b/basis/io/files/temp/macosx/macosx.factor
new file mode 100644 (file)
index 0000000..eb31b1e
--- /dev/null
@@ -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) ;
diff --git a/basis/io/files/temp/macosx/platforms.txt b/basis/io/files/temp/macosx/platforms.txt
new file mode 100644 (file)
index 0000000..6e806f4
--- /dev/null
@@ -0,0 +1 @@
+macosx
index f67788fa3e6ca4d6bb7bd7b7e54201b3046075ff..92b75a7e54f3c437cbbc4b778907ae77a10372f0 100644 (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"
index 7ace21932a5f697062b8930ffa573bb14f7b669d..f353d3151f55ece8a21ccf890ab26392b0801400 100644 (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 ;
\ 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
diff --git a/basis/io/files/temp/unix/platforms.txt b/basis/io/files/temp/unix/platforms.txt
new file mode 100644 (file)
index 0000000..509143d
--- /dev/null
@@ -0,0 +1 @@
+unix
diff --git a/basis/io/files/temp/unix/unix.factor b/basis/io/files/temp/unix/unix.factor
new file mode 100644 (file)
index 0000000..c68fcde
--- /dev/null
@@ -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) ;
diff --git a/basis/io/files/temp/windows/platforms.txt b/basis/io/files/temp/windows/platforms.txt
new file mode 100644 (file)
index 0000000..8e1a559
--- /dev/null
@@ -0,0 +1 @@
+windows
diff --git a/basis/io/files/temp/windows/windows.factor b/basis/io/files/temp/windows/windows.factor
new file mode 100644 (file)
index 0000000..95ab4a3
--- /dev/null
@@ -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) ;