@Override public void run() { try { for (String path : paths) { try { deleteRecursively(PathUtil.resolveSecurely(runtimeDeployedPath, path)); } catch (IOException ex) { ServerLogger.DEPLOYMENT_LOGGER.couldNotDeleteFile(ex, path, managementName); } } } finally { result.countDown(); } } };
@Override public void copyExplodedContentFiles(byte[] deploymentHash, List<String> relativePaths, Path target) throws ExplodedContentException { final Path contentPath = getDeploymentContentFile(deploymentHash); try { if (Files.exists(contentPath) && Files.isDirectory(contentPath)) { for (String relativePath : relativePaths) { copyRecursively(resolveSecurely(contentPath, relativePath), resolveSecurely(target, relativePath), true); } } } catch (IOException ioex) { DeploymentRepositoryLogger.ROOT_LOGGER.warn(ioex); throw DeploymentRepositoryLogger.ROOT_LOGGER.errorCopyingDeployment(ioex, target.toString()); } }
public static Path readFile(Path src, Path tempDir) throws IOException { if(isFile(src)) { return src; } else { Path file = getFile(src); if(isArchive(file)) { Path relativePath = file.relativize(src); Path target = createTempDirectory(tempDir, "unarchive"); unzip(file, target); return readFile(target.resolve(relativePath), tempDir); } else { throw new FileNotFoundException(src.toString()); } } }
@Override public byte[] explodeSubContent(byte[] deploymentHash, String relativePath) throws ExplodedContentException { Path contentPath = getDeploymentContentFile(deploymentHash); Path sourcePath = resolveSecurely(contentPath, relativePath); try { if (Files.exists(contentPath) && Files.isDirectory(contentPath) && this.readWrite) { Path tmp = createTempDirectory(repoRoot.toPath(), CONTENT); Path contentDir = tmp.resolve(CONTENT); copyRecursively(contentPath, contentDir, true); Path targetPath = resolveSecurely(contentDir, relativePath); if (!Files.exists(sourcePath)) { throw DeploymentRepositoryLogger.ROOT_LOGGER.archiveNotFound(sourcePath.toString()); if (Files.isDirectory(sourcePath) || !isArchive(sourcePath)) { throw DeploymentRepositoryLogger.ROOT_LOGGER.notAnArchive(sourcePath.toString()); deleteRecursively(targetPath); unzip(sourcePath, targetPath); byte[] sha1Bytes = HashUtil.hashPath(messageDigest, contentDir); final Path realFile = getDeploymentContentFile(sha1Bytes, true); deleteRecursively(tmp); } catch (IOException ioex) { DeploymentRepositoryLogger.ROOT_LOGGER.cannotDeleteTempFile(ioex, tmp.toAbsolutePath().toString()); } else { moveTempToPermanent(contentDir, realFile); deleteRecursively(tmp); DeploymentRepositoryLogger.ROOT_LOGGER.contentAdded(realFile.toAbsolutePath().toString());
try { if (Files.exists(contentPath) && Files.isDirectory(contentPath) && this.readWrite) { Path tmp = createTempDirectory(repoRoot.toPath(), CONTENT); Path contentDir = tmp.resolve(CONTENT).toAbsolutePath(); copyRecursively(contentPath, contentDir, false); for (String path : paths) { Path targetFile = resolveSecurely(contentDir, path); deleteFileWithEmptyAncestorDirectories(targetFile); deleteRecursively(tmp); } catch (IOException ioex) { DeploymentRepositoryLogger.ROOT_LOGGER.cannotDeleteTempFile(ioex, tmp.toAbsolutePath().toString()); } else { moveTempToPermanent(contentDir, realFile); deleteRecursively(tmp); DeploymentRepositoryLogger.ROOT_LOGGER.contentAdded(realFile.toAbsolutePath().toString());
List<ContentRepositoryElement> result = new ArrayList<>(); if (Files.exists(rootPath)) { if(isArchive(rootPath)) { return listZipContent(rootPath, filter); Path file = getFile(rootPath); if(isArchive(file)) { Path relativePath = file.relativize(rootPath); Path target = createTempDirectory(tempDir, "unarchive"); unzip(file, target); return listFiles(target.resolve(relativePath), tempDir, filter); } else { throw new FileNotFoundException(rootPath.toString());
@Override public TypedInputStream readContent(byte[] deploymentHash, String path) throws ExplodedContentException { Path tmpDir = null; try { if(!lock(deploymentHash)) { throw DeploymentRepositoryLogger.ROOT_LOGGER.errorLockingDeployment(); } Path src = resolveSecurely(getDeploymentContentFile(deploymentHash), path); tmpDir = Files.createTempDirectory(tmpRoot.toPath(), HashUtil.bytesToHexString(deploymentHash)); Path file = PathUtil.readFile(src, tmpDir); Path tmp = Files.createTempFile(tmpRoot.toPath(), CONTENT, getFileExtension(src)); Files.copy(file, tmp, StandardCopyOption.COPY_ATTRIBUTES, StandardCopyOption.REPLACE_EXISTING); return new TemporaryFileInputStream(tmp); } catch (InterruptedException ex) { Thread.currentThread().interrupt(); throw new RuntimeException(ex); } catch (IOException ex) { DeploymentRepositoryLogger.ROOT_LOGGER.warn(ex); throw DeploymentRepositoryLogger.ROOT_LOGGER.errorAccessingDeployment(ex); } finally { unlock(deploymentHash); deleteSilentlyRecursively(tmpDir); } }
if (Files.isDirectory(contentPath) || !isArchive(contentPath)) { throw DeploymentRepositoryLogger.ROOT_LOGGER.notAnArchive(contentPath.toString()); Path tmp = createTempDirectory(repoRoot.toPath(), CONTENT); Path contentDir = Files.createDirectory(tmp.resolve(CONTENT)); unzip(contentPath, contentDir); byte[] sha1Bytes = HashUtil.hashPath(messageDigest, contentDir); final Path realFile = getDeploymentContentFile(sha1Bytes, true); deleteRecursively(tmp); } catch (IOException ioex) { DeploymentRepositoryLogger.ROOT_LOGGER.cannotDeleteTempFile(ioex, tmp.toAbsolutePath().toString()); } else { moveTempToPermanent(contentDir, realFile); deleteRecursively(tmp); DeploymentRepositoryLogger.ROOT_LOGGER.contentExploded(realFile.toAbsolutePath().toString());
@Override public List<ContentRepositoryElement> listContent(byte[] deploymentHash, String path, ContentFilter filter) throws ExplodedContentException { Path tmpDir = null; try { if (!lock(deploymentHash)) { throw DeploymentRepositoryLogger.ROOT_LOGGER.errorLockingDeployment(); } tmpDir = Files.createTempDirectory(tmpRoot.toPath(), HashUtil.bytesToHexString(deploymentHash)); final Path rootPath = resolveSecurely(getDeploymentContentFile(deploymentHash), path); List<ContentRepositoryElement> result = PathUtil.listFiles(rootPath, tmpDir, filter); return result; } catch (InterruptedException ex) { Thread.currentThread().interrupt(); throw new RuntimeException(ex); } catch (IOException ex) { DeploymentRepositoryLogger.ROOT_LOGGER.warn(ex); throw DeploymentRepositoryLogger.ROOT_LOGGER.errorAccessingDeployment(ex); } finally { unlock(deploymentHash); if(tmpDir != null) { deleteSilentlyRecursively(tmpDir); } } }
/** * Delete a path recursively, not throwing Exception if it fails or if the path is null. * @param path a Path pointing to a file or a directory that may not exists anymore. */ public static void deleteSilentlyRecursively(final Path path) { if (path != null) { try { deleteRecursively(path); } catch (IOException ioex) { DeploymentRepositoryLogger.ROOT_LOGGER.cannotDeleteFile(ioex, path); } } }
copyRecursively(tmpFile, localTmp, true); try { Files.move(localTmp, permanentFile); copyRecursively(localTmp, permanentFile, true); } catch (IOException e) { deleteRecursively(permanentFile); throw e; deleteRecursively(tmpFile); } catch (IOException ioex) { DeploymentRepositoryLogger.ROOT_LOGGER.cannotDeleteTempFile(ioex, tmpFile.toString()); deleteRecursively(localTmp); } catch (IOException ioex) { DeploymentRepositoryLogger.ROOT_LOGGER.cannotDeleteTempFile(ioex, localTmp.toString());
private static void unzip(final ZipFile zip, final Path targetDir) throws IOException { final Enumeration<? extends ZipEntry> entries = zip.entries(); while (entries.hasMoreElements()) { final ZipEntry entry = entries.nextElement(); final String name = entry.getName(); final Path current = resolveSecurely(targetDir, name); if (entry.isDirectory()) { if (!Files.exists(current)) { Files.createDirectories(current); } } else { if (Files.notExists(current.getParent())) { Files.createDirectories(current.getParent()); } try (final InputStream eis = zip.getInputStream(entry)) { Files.copy(eis, current); } } try { Files.getFileAttributeView(current, BasicFileAttributeView.class).setTimes(entry.getLastModifiedTime(), entry.getLastAccessTime(), entry.getCreationTime()); } catch (IOException e) { //ignore, if we cannot set it, world will not end } } }
@Override public boolean acceptFile(Path rootPath, Path file, InputStream in) throws IOException { Path relativePath = rootPath.relativize(file); if(this.depth < 0 || this.depth >= relativePath.getNameCount()) { if(archiveOnly) { if(in != null) { return PathUtil.isArchive(in); } return PathUtil.isArchive(file); } return true; } return false; }
@Override public void copyExplodedContent(byte[] deploymentHash, final Path target) throws ExplodedContentException { final Path contentPath = getDeploymentContentFile(deploymentHash); try { if (Files.exists(contentPath) && Files.isDirectory(contentPath)) { copyRecursively(contentPath, target, false); } } catch (IOException ioex) { DeploymentRepositoryLogger.ROOT_LOGGER.warn(ioex); throw DeploymentRepositoryLogger.ROOT_LOGGER.errorCopyingDeployment(ioex, target.toString()); } }
try { if (Files.exists(contentPath) && Files.isDirectory(contentPath) && this.readWrite) { Path tmp = createTempDirectory(repoRoot.toPath(), CONTENT); Path contentDir = tmp.resolve(CONTENT); copyRecursively(contentPath, contentDir, overwrite); for (ExplodedContent newContent : addFiles) { Path targetFile = resolveSecurely(contentDir, newContent.getRelativePath()); if (!Files.exists(targetFile)) { Files.createDirectories(targetFile.getParent()); deleteRecursively(tmp); } catch (IOException ioex) { DeploymentRepositoryLogger.ROOT_LOGGER.cannotDeleteTempFile(ioex, tmp.toAbsolutePath().toString()); } else { moveTempToPermanent(contentDir, realFile); deleteRecursively(tmp); DeploymentRepositoryLogger.ROOT_LOGGER.contentAdded(realFile.toAbsolutePath().toString());
@Override public void deleteDeployment(ContentReference reference) { File deployment = getDeploymentRoot(reference); if (deployment != deploymentRoot) { try { PathUtil.deleteRecursively(deployment.toPath()); if (deployment.getParentFile().list() != null && deployment.getParentFile().list().length == 0) { deployment.getParentFile().delete(); } } catch (IOException ex) { DeploymentRepositoryLogger.ROOT_LOGGER.couldNotDeleteDeployment(ex, deployment.getAbsolutePath()); } } } }
@Override public void run() { try { for (String path : paths) { try { deleteRecursively(PathUtil.resolveSecurely(runtimeDeployedPath, path)); } catch (IOException ex) { ServerLogger.DEPLOYMENT_LOGGER.couldNotDeleteFile(ex, path, managementName); } } } finally { result.countDown(); } } };
deleteRecursively(tmp); } catch (IOException ioex) { DeploymentRepositoryLogger.ROOT_LOGGER.cannotDeleteTempFile(ioex, tmp.toAbsolutePath().toString());
deleteRecursively(contentPath); } catch (IOException ex) { DeploymentRepositoryLogger.ROOT_LOGGER.contentDeletionError(ex, contentPath.toString());