@Override public FilePath createTempFile(String suffix, boolean deleteOnExit, boolean inTempDir) throws IOException { if (!inTempDir) { throw new IOException("File system is read-only"); } return new FilePathDisk().getPath(name).createTempFile(suffix, deleteOnExit, true); }
@Override public boolean canWrite() { return canWriteInternal(new File(name)); }
private static String translateFileName(String fileName) { if (fileName.startsWith("zip:")) { fileName = fileName.substring("zip:".length()); } int idx = fileName.indexOf('!'); if (idx >= 0) { fileName = fileName.substring(0, idx); } return FilePathDisk.expandUserHomeDirectory(fileName); }
@Override public FilePathDisk getPath(String path) { FilePathDisk p = new FilePathDisk(); p.name = translateFileName(path); return p; }
@Override public FilePath createTempFile(String suffix, boolean deleteOnExit, boolean inTempDir) throws IOException { String fileName = name + "."; String prefix = new File(fileName).getName(); File dir; if (inTempDir) { dir = new File(System.getProperty("java.io.tmpdir", ".")); } else { dir = new File(fileName).getAbsoluteFile().getParentFile(); } FileUtils.createDirectories(dir.getAbsolutePath()); while (true) { File f = new File(dir, prefix + getNextTempFileNamePart(false) + suffix); if (f.exists() || !f.createNewFile()) { // in theory, the random number could collide getNextTempFileNamePart(true); continue; } if (deleteOnExit) { try { f.deleteOnExit(); } catch (Throwable e) { // sometimes this throws a NullPointerException // at java.io.DeleteOnExitHook.add(DeleteOnExitHook.java:33) // we can ignore it } } return get(f.getCanonicalPath()); } }
@Override public boolean createFile() { File file = new File(name); for (int i = 0; i < SysProperties.MAX_FILE_RETRY; i++) { try { return file.createNewFile(); } catch (IOException e) { // 'access denied' is really a concurrent access problem wait(i); } } return false; }
@Override public FilePath getParent() { String p = new File(name).getParent(); return p == null ? null : getPath(p); }
@Override public OutputStream newOutputStream(boolean append) throws IOException { try { File file = new File(name); File parent = file.getParentFile(); if (parent != null) { FileUtils.createDirectories(parent.getAbsolutePath()); } FileOutputStream out = new FileOutputStream(name, append); IOUtils.trace("openFileOutputStream", name, out); return out; } catch (IOException e) { freeMemoryAndFinalize(); return new FileOutputStream(name); } }
@Override public void delete() { File file = new File(name); for (int i = 0; i < SysProperties.MAX_FILE_RETRY; i++) { IOUtils.trace("delete", name, null); boolean ok = file.delete(); if (ok || !file.exists()) { return; } wait(i); } throw DbException.get(ErrorCode.FILE_DELETE_FAILED_1, name); }
@Override public FilePathDisk toRealPath() { try { String fileName = new File(name).getCanonicalPath(); return getPath(fileName); } catch (IOException e) { throw DbException.convertIOException(e, name); } }
@Override public FileChannel open(String mode) throws IOException { FileDisk f; try { f = new FileDisk(name, mode); IOUtils.trace("open", name, f); } catch (IOException e) { freeMemoryAndFinalize(); try { f = new FileDisk(name, mode); } catch (IOException e2) { throw e; } } return f; }
@Override public void createDirectory() { File dir = new File(name); for (int i = 0; i < SysProperties.MAX_FILE_RETRY; i++) { if (dir.exists()) { if (dir.isDirectory()) { return; } throw DbException.get(ErrorCode.FILE_CREATION_FAILED_1, name + " (a file with this name already exists)"); } else if (dir.mkdir()) { return; } wait(i); } throw DbException.get(ErrorCode.FILE_CREATION_FAILED_1, name); }
@Override public List<FilePath> newDirectoryStream() { ArrayList<FilePath> list = new ArrayList<>(); File f = new File(name); try { String[] files = f.list(); if (files != null) { String base = f.getCanonicalPath(); if (!base.endsWith(SysProperties.FILE_SEPARATOR)) { base += SysProperties.FILE_SEPARATOR; } for (int i = 0, len = files.length; i < len; i++) { list.add(getPath(base + files[i])); } } return list; } catch (IOException e) { throw DbException.convertIOException(e, name); } }
/** * Translate the file name to the native format. This will replace '\' with * '/' and expand the home directory ('~'). * * @param fileName the file name * @return the native file name */ protected static String translateFileName(String fileName) { fileName = fileName.replace('\\', '/'); if (fileName.startsWith("file:")) { fileName = fileName.substring("file:".length()); } return expandUserHomeDirectory(fileName); }
return; wait(i);