use of gov.loc.repository.bagit.domain.Manifest in project bagit-java by LibraryOfCongress.
the class BagWriter method writeTagManifestFiles.
/*
* Write the tag manifest files
*/
private static void writeTagManifestFiles(final Set<Manifest> manifests, final Path outputDir, final Path bagRootDir) throws IOException {
for (final Manifest manifest : manifests) {
for (final Entry<Path, String> entry : manifest.getFileToChecksumMap().entrySet()) {
final Path relativeLocation = bagRootDir.relativize(entry.getKey());
final Path writeTo = outputDir.resolve(relativeLocation);
final Path writeToParent = writeTo.getParent();
if (!Files.exists(writeTo) && writeToParent != null) {
Files.createDirectories(writeToParent);
Files.copy(entry.getKey(), writeTo);
}
}
}
}
use of gov.loc.repository.bagit.domain.Manifest in project bagit-java by LibraryOfCongress.
the class BagWriterTest method testGetCorrectRelativeOuputPath.
@Test
public void testGetCorrectRelativeOuputPath() throws Exception {
Path root = Paths.get(folder.newFolder().toURI());
Bag bag = BagCreator.bagInPlace(root, Arrays.asList(StandardSupportedAlgorithms.MD5), false);
Path testFile = root.resolve("data").resolve("fooFile.txt");
Files.createFile(testFile);
Manifest manifest = (Manifest) bag.getPayLoadManifests().toArray()[0];
manifest.getFileToChecksumMap().put(testFile, "CHECKSUM");
bag.getPayLoadManifests().add(manifest);
Path newRoot = Paths.get(folder.newFolder().toURI());
BagWriter.write(bag, newRoot);
assertTrue(Files.exists(newRoot.resolve("data").resolve("fooFile.txt")));
}
use of gov.loc.repository.bagit.domain.Manifest in project bagit-java by LibraryOfCongress.
the class BagVerifier method isValid.
/**
* See <a href="https://tools.ietf.org/html/draft-kunze-bagit-13#section-3">https://tools.ietf.org/html/draft-kunze-bagit-13#section-3</a><br>
* A bag is <b>valid</b> if the bag is complete and every checksum has been
* verified against the contents of its corresponding file.
*
* @param bag the {@link Bag} object to check
* @param ignoreHiddenFiles ignore hidden files unless explicitly listed in manifest(s)
*
* @throws CorruptChecksumException when the computed hash doesn't match given hash
* @throws IOException if there was an error with the file
* @throws MissingPayloadManifestException if there is not at least one payload manifest
* @throws MissingBagitFileException if there is no bagit.txt file
* @throws MissingPayloadDirectoryException if there is no /data directory
* @throws FileNotInPayloadDirectoryException if a manifest lists a file but it is not in the payload directory
* @throws InterruptedException if the threads are interrupted when checking if all files are listed in manifest(s)
* @throws MaliciousPathException if there is path that is referenced in the manifest that is outside the bag root directory
* @throws VerificationException some other exception happened during processing so capture it here.
* @throws UnsupportedAlgorithmException if the manifest uses a algorithm that isn't supported
* @throws InvalidBagitFileFormatException if the manifest is not formatted properly
*/
public void isValid(final Bag bag, final boolean ignoreHiddenFiles) throws IOException, MissingPayloadManifestException, MissingBagitFileException, MissingPayloadDirectoryException, FileNotInPayloadDirectoryException, InterruptedException, MaliciousPathException, CorruptChecksumException, VerificationException, UnsupportedAlgorithmException, InvalidBagitFileFormatException {
logger.info(messages.getString("checking_bag_is_valid"), bag.getRootDir());
isComplete(bag, ignoreHiddenFiles);
logger.debug(messages.getString("checking_payload_checksums"));
for (final Manifest payloadManifest : bag.getPayLoadManifests()) {
checkHashes(payloadManifest);
}
logger.debug(messages.getString("checking_tag_file_checksums"));
for (final Manifest tagManifest : bag.getTagManifests()) {
checkHashes(tagManifest);
}
}
use of gov.loc.repository.bagit.domain.Manifest in project bagit-java by LibraryOfCongress.
the class BagCreatorTest method testBagInPlace.
@Test
public void testBagInPlace() throws IOException, NoSuchAlgorithmException {
TestStructure structure = createTestStructure();
Bag bag = BagCreator.bagInPlace(Paths.get(folder.getRoot().toURI()), Arrays.asList(StandardSupportedAlgorithms.MD5), false);
assertEquals(new Version(0, 97), bag.getVersion());
File expectedManifest = new File(folder.getRoot(), "manifest-md5.txt");
assertTrue(expectedManifest.exists());
File expectedTagManifest = new File(folder.getRoot(), "tagmanifest-md5.txt");
assertTrue(expectedTagManifest.exists());
File bagitFile = new File(folder.getRoot(), "bagit.txt");
assertTrue(bagitFile.exists());
// make sure the hidden folder was not included in the data directory
File hiddenFolder = new File(bag.getRootDir().resolve("data").toFile(), ".hiddenFolder");
assertFalse(hiddenFolder.exists());
for (Manifest manifest : bag.getPayLoadManifests()) {
for (Path expectedPayloadFile : manifest.getFileToChecksumMap().keySet()) {
assertTrue(structure.regularPayloadFiles.contains(expectedPayloadFile));
}
}
}
use of gov.loc.repository.bagit.domain.Manifest in project bagit-java by LibraryOfCongress.
the class ManifestWriter method writeManifests.
/*
* Generic method to write manifests
*/
private static void writeManifests(final Set<Manifest> manifests, final Path outputDir, final Path relativeTo, final String filenameBase, final Charset charsetName) throws IOException {
for (final Manifest manifest : manifests) {
final Path manifestPath = outputDir.resolve(filenameBase + manifest.getAlgorithm().getBagitName() + ".txt");
logger.debug(messages.getString("writing_manifest_to_path"), manifestPath);
Files.deleteIfExists(manifestPath);
Files.createFile(manifestPath);
for (final Entry<Path, String> entry : manifest.getFileToChecksumMap().entrySet()) {
// there are 2 spaces between the checksum and the path so that the manifests are compatible with the md5sum tools available on most unix systems.
// This may cause problems on windows due to it being text mode, in which case either replace with a * or try verifying in binary mode with --binary
final String line = entry.getValue() + " " + RelativePathWriter.formatRelativePathString(relativeTo, entry.getKey());
logger.debug(messages.getString("writing_line_to_file"), line, manifestPath);
Files.write(manifestPath, line.getBytes(charsetName), StandardOpenOption.APPEND, StandardOpenOption.CREATE);
}
}
}
Aggregations