public ContentReference(String contentIdentifier, byte[] hash, long timestamp) { this.contentIdentifier = contentIdentifier; if (hash == null || hash.length == 0) { this.hexHash = ""; } else { this.hexHash = HashUtil.bytesToHexString(hash); } }
public ExplodedContent(String relativePath) { this.relativePath = relativePath; this.content = emptyStream(); }
/** * Hashes a path, if the path points to a directory then hashes the contents recursively. * @param messageDigest the digest used to hash. * @param path the file/directory we want to hash. * @return the resulting hash. * @throws IOException */ public static byte[] hashPath(MessageDigest messageDigest, Path path) throws IOException { try (InputStream in = getRecursiveContentStream(path)) { return hashContent(messageDigest, in); } }
private static InputStream getRecursiveContentStream(Path path) { if (Files.isRegularFile(path)) { try { return new SequenceInputStream(new ByteArrayInputStream(path.getFileName().toString().getBytes(StandardCharsets.UTF_8)), Files.newInputStream(path)); } catch (IOException ex) { throw DeploymentRepositoryLogger.ROOT_LOGGER.hashingError(ex, path); } } else if (Files.isDirectory(path)) { try { Vector<InputStream> v = new Vector<>(); v.add(new ByteArrayInputStream(path.getFileName().toString().getBytes(StandardCharsets.UTF_8))); try(Stream<Path> paths = Files.list(path)) { v.addAll(paths.sorted((Path path1, Path path2) -> path1.compareTo(path2)).map(p -> getRecursiveContentStream(p)).collect(Collectors.toList())); } return new SequenceInputStream(v.elements()); } catch (IOException ex) { throw DeploymentRepositoryLogger.ROOT_LOGGER.hashingError(ex, path); } } return emptyStream(); }
synchronized (messageDigest) { messageDigest.reset(); sha1Bytes = HashUtil.hashPath(messageDigest, tmp);
public byte[] getHash() { if(hexHash.isEmpty()) { return new byte[0]; } return HashUtil.hexStringToByteArray(hexHash); }
if (!HashUtil.isEachHexHashInTable(reference.getHexHash())) { contentPath = Paths.get(reference.getContentIdentifier()); } else { if (HashUtil.isEachHexHashInTable(reference.getHexHash()) && this.readWrite) { //Otherwise this is not a deployment content if(!lock(reference.getHash())) { DeploymentRepositoryLogger.ROOT_LOGGER.contentDeletionError(DeploymentRepositoryLogger.ROOT_LOGGER.errorLockingDeployment(), contentPath.toString()); DeploymentRepositoryLogger.ROOT_LOGGER.contentDeletionError(ex, contentPath.toString()); } finally { if (HashUtil.isEachHexHashInTable(reference.getHexHash())) { unlock(reference.getHash());
Path contentDir = Files.createDirectory(tmp.resolve(CONTENT)); unzip(contentPath, contentDir); byte[] sha1Bytes = HashUtil.hashPath(messageDigest, contentDir); final Path realFile = getDeploymentContentFile(sha1Bytes, true); if (hasContent(sha1Bytes)) {
@Override public void addContentReference(byte[] hash, Object reference) { String hashString = HashUtil.bytesToHexString(hash); synchronized (deploymentHashReferences) { Set<Object> references = deploymentHashReferences.get(hashString); if (references == null) { references = new HashSet<Object>(); deploymentHashReferences.put(hashString, references); } references.add(reference); } }
deleteFileWithEmptyAncestorDirectories(targetFile); byte[] sha1Bytes = HashUtil.hashPath(messageDigest, contentDir); final Path realFile = getDeploymentContentFile(sha1Bytes, true); if (hasContent(sha1Bytes)) {
/** {@inheritDoc} */ @Override public File getDeploymentRoot(byte[] deploymentHash) { if (deploymentHash == null || deploymentHash.length == 0) { return deploymentRoot; } String hex = HashUtil.bytesToHexString(deploymentHash); File first = new File(deploymentRoot, hex.substring(0,2)); return new File(first, hex.substring(2)); }
byte[] sha1Bytes = HashUtil.hashPath(messageDigest, contentDir); final Path realFile = getDeploymentContentFile(sha1Bytes, true); if (hasContent(sha1Bytes)) {
/** {@inheritDoc} */ @Override public File getDeploymentRoot(byte[] hash) { if (hash == null || hash.length == 0) { return deploymentRoot; } String hex = HashUtil.bytesToHexString(hash); File first = new File(deploymentRoot, hex.substring(0,2)); return new File(first, hex.substring(2)); }
byte[] sha1Bytes = HashUtil.hashPath(messageDigest, contentDir); final Path realFile = getDeploymentContentFile(sha1Bytes, true); if (hasContent(sha1Bytes)) {
private boolean lock(byte[] hash) throws InterruptedException { String hashHex = HashUtil.bytesToHexString(hash); synchronized(lockedContents) { if(!lockedContents.containsKey(hashHex)) { lockedContents.put(hashHex, new ReentrantLock()); } return lockedContents.get(hashHex).tryLock(lockTimeout, TimeUnit.MILLISECONDS); } }
private void unlock(byte[] hash) { String hashHex = HashUtil.bytesToHexString(hash); synchronized (lockedContents) { if (lockedContents.containsKey(hashHex)) { ReentrantLock lock = lockedContents.get(hashHex); if (lock.isHeldByCurrentThread()) { lock.unlock(); if (!Files.exists(getDeploymentContentFile(hash))) { lockedContents.remove(hashHex); } } } } }
protected File getDeploymentHashDir(final byte[] deploymentHash, final boolean validate) { final String sha1 = HashUtil.bytesToHexString(deploymentHash); final String partA = sha1.substring(0,2); final String partB = sha1.substring(2); final File base = new File(getRepoRoot(), partA); if (validate) { validateDir(base); } final File hashDir = new File(base, partB); if (validate && !hashDir.exists() && !hashDir.mkdirs()) { throw DeploymentRepositoryMessages.MESSAGES.cannotCreateDirectory(hashDir.getAbsolutePath()); } return hashDir; }
protected Path getDeploymentHashDir(final byte[] deploymentHash, final boolean validate) { final String sha1 = HashUtil.bytesToHexString(deploymentHash); final String partA = sha1.substring(0, 2); final String partB = sha1.substring(2); final Path base = getRepoRoot().resolve(partA); if (validate) { validateDir(base); } final Path hashDir = base.resolve(partB); if (validate && !Files.exists(hashDir)) { try { Files.createDirectories(hashDir); } catch (IOException ioex) { throw DeploymentRepositoryLogger.ROOT_LOGGER.cannotCreateDirectory(ioex, hashDir.toAbsolutePath().toString()); } } return hashDir; }
@Override public void removeContent(byte[] hash, Object reference) { String hashString = HashUtil.bytesToHexString(hash); synchronized (deploymentHashReferences) { final Set<Object> references = deploymentHashReferences.get(hashString);
@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); } }