1 11 package org.eclipse.core.internal.filesystem; 12 13 import java.io.File ; 14 import java.io.IOException ; 15 import org.eclipse.core.filesystem.*; 16 import org.eclipse.core.filesystem.provider.FileStore; 17 import org.eclipse.core.internal.filesystem.local.LocalFile; 18 import org.eclipse.core.runtime.*; 19 import org.eclipse.osgi.util.NLS; 20 21 25 public class FileCache { 26 private static final String CACHE_DIR_NAME = "filecache"; 28 31 private static final Object creationLock = new Object (); 32 33 36 private static FileCache instance = null; 37 38 private File cacheDir; 39 40 46 public static FileCache getCache() throws CoreException { 47 synchronized (creationLock) { 48 if (instance == null) 49 instance = new FileCache(); 50 return instance; 51 } 52 } 53 54 59 private FileCache() throws CoreException { 60 IPath location = Activator.getCacheLocation(); 61 File cacheParent = new File (location.toFile(), CACHE_DIR_NAME); 62 cleanOldCache(cacheParent); 63 cacheParent.mkdirs(); 64 cacheDir = getUniqueDirectory(cacheParent, true); 66 } 67 68 75 public java.io.File cache(IFileStore source, IProgressMonitor monitor) throws CoreException { 76 try { 77 monitor.beginTask(NLS.bind(Messages.copying, toString()), 100); 78 IFileInfo myInfo = source.fetchInfo(EFS.NONE, Policy.subMonitorFor(monitor, 25)); 79 if (!myInfo.exists()) 80 return new File (cacheDir, "Non-Existent-" + System.currentTimeMillis()); File result; 82 if (myInfo.isDirectory()) { 83 result = getUniqueDirectory(cacheDir, false); 84 } else { 85 result = File.createTempFile(source.getFileSystem().getScheme(), "efs", cacheDir); } 87 monitor.worked(25); 88 IFileStore resultStore = new LocalFile(result); 89 source.copy(resultStore, EFS.OVERWRITE, Policy.subMonitorFor(monitor, 25)); 90 result.deleteOnExit(); 91 return result; 92 } catch (IOException e) { 93 Policy.error(EFS.ERROR_WRITE, NLS.bind(Messages.couldNotWrite, toString())); 94 return null; } finally { 96 monitor.done(); 97 } 98 } 99 100 104 private void cleanOldCache(File cacheParent) throws CoreException { 105 new LocalFile(cacheParent).delete(EFS.NONE, null); 107 } 108 109 117 private File getUniqueDirectory(File parent, boolean create) { 118 File dir; 119 long i = 0; 120 do { 122 dir = new File (parent, Long.toString(System.currentTimeMillis() + i++)); 123 } while (dir.exists()); 124 if (create) 125 dir.mkdir(); 126 return dir; 127 } 128 } 129
| Popular Tags
|