private File createDir(File dir, String debugTitle) { if (!dir.isDirectory() || !dir.exists()) { logger.debug("Create : " + dir.getAbsolutePath()); try { Files.createDirectories(dir.toPath()); } catch (IOException e) { throw new IllegalStateException("Unable to create " + debugTitle + dir.getAbsolutePath(), e); } } return dir; } }
public void unlock() { if (lockFile != null) { try { lockFile.release(); lockFile = null; } catch (IOException e) { logger.error("Error releasing lock", e); } } if (lockChannel != null) { try { lockChannel.close(); lockChannel = null; } catch (IOException e) { logger.error("Error closing file channel", e); } } if (lockRandomAccessFile != null) { try { lockRandomAccessFile.close(); lockRandomAccessFile = null; } catch (IOException e) { logger.error("Error closing file", e); } } } }
private void renameQuietly(File sourceFile, File targetFile) { boolean rename = sourceFile.renameTo(targetFile); // Check if the file was cached by another process during download if (!rename && !targetFile.exists()) { logger.warn(String.format("Unable to rename %s to %s", sourceFile.getAbsolutePath(), targetFile.getAbsolutePath())); logger.warn("A copy/delete will be tempted but with no guarantee of atomicity"); try { Files.move(sourceFile.toPath(), targetFile.toPath()); } catch (IOException e) { throw new IllegalStateException("Fail to move " + sourceFile.getAbsolutePath() + " to " + targetFile, e); } } }
FileCache(File dir, FileHashes fileHashes, Logger logger) { this.hashes = fileHashes; this.logger = logger; this.cacheDir = createDir(dir, "user cache: "); logger.info(String.format("User cache: %s", dir.getAbsolutePath())); this.tmpDir = createDir(new File(dir, "_tmp"), "temp dir"); }
/** * Look for a file in the cache by its filename and md5 checksum. If the file is not * present then return null. */ @CheckForNull public File get(String filename, String hash) { File cachedFile = new File(new File(cacheDir, hash), filename); if (cachedFile.exists()) { return cachedFile; } logger.debug(String.format("No file found in the cache with name %s and hash %s", filename, hash)); return null; }
private void unpack200(Path compressedFile, Path jarFile) { logger.debug("Unpacking plugin " + compressedFile); Pack200.Unpacker unpacker = Pack200.newUnpacker(); try { try (JarOutputStream jarStream = new JarOutputStream(new BufferedOutputStream(Files.newOutputStream(jarFile))); InputStream in = new GZIPInputStream(new BufferedInputStream(Files.newInputStream(compressedFile)))) { unpacker.unpack(in, jarStream); } } catch (IOException e) { throw new IllegalStateException(e); } }