resolve darcs stupidity
[org.ibex.core.git] / src / org / ibex / core / LocalStorage.java
1 // Copyright 2004 Adam Megacz, see the COPYING file for licensing [GPL]
2 package org.ibex;
3
4 import java.io.*;
5 import org.ibex.util.*;
6 import org.bouncycastle.util.encoders.Base64;
7
8 /** Manages access to ~/.ibex */
9 public class LocalStorage {
10
11     static String ibexDirName = System.getProperty("user.home") + java.io.File.separatorChar + ".ibex";
12
13     static java.io.File ibexDir = null;
14     static java.io.File cacheDir = null;
15
16     static {
17         try {
18             ibexDir = new java.io.File(ibexDirName);
19             if (!ibexDir.mkdirs()) ibexDir = null;
20             try {
21                 cacheDir = new java.io.File(ibexDirName + java.io.File.separatorChar + "cache");
22                 if (!cacheDir.mkdirs()) cacheDir = null;
23             } catch (Exception e) {
24                 Log.warn(LocalStorage.class, "unable to create cache directory " +
25                          ibexDirName + java.io.File.separatorChar + "cache");
26             }
27         } catch (Exception e) {
28             Log.warn(LocalStorage.class, "unable to create ibex directory " + ibexDirName);
29         }
30     }
31
32     // FEATURE: we ought to be able to do stuff like sha1-checking and date checking on cached resources    
33     public static class Cache {
34
35         private static void delTree(java.io.File f) throws IOException {
36             if (f.isDirectory()) {
37                 String[] s = f.list();
38                 for(int i=0; i<s.length; i++)
39                     delTree(new java.io.File(f.getPath() + java.io.File.separatorChar + s[i]));
40             }
41             f.delete();
42         }
43
44         public static void flush() throws IOException {
45             delTree(cacheDir);
46             cacheDir.mkdirs();
47         }
48
49         public static java.io.File getCacheFileForKey(String key) {
50             // FEATURE: be smarter here
51             return new java.io.File(cacheDir.getPath() + File.separatorChar + new String(Base64.encode(key.getBytes())));
52         }
53
54     }
55 }