Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize control files creation and fixes files being truncated on some SMB file servers. #129

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 13 additions & 10 deletions src/main/java/gov/loc/repository/bagit/writer/BagitFileWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;
Expand All @@ -12,6 +11,7 @@
import org.slf4j.LoggerFactory;

import gov.loc.repository.bagit.domain.Version;
import java.io.BufferedWriter;

/**
* Responsible for writing the bagit.txt to the filesystem
Expand All @@ -36,14 +36,17 @@ private BagitFileWriter(){
public static void writeBagitFile(final Version version, final Charset encoding, final Path outputDir) throws IOException{
final Path bagitPath = outputDir.resolve("bagit.txt");
logger.debug(messages.getString("write_bagit_file_to_path"), outputDir);

final String firstLine = "BagIt-Version: " + version + System.lineSeparator();
logger.debug(messages.getString("writing_line_to_file"), firstLine, bagitPath);
Files.write(bagitPath, firstLine.getBytes(StandardCharsets.UTF_8),
StandardOpenOption.WRITE, StandardOpenOption.TRUNCATE_EXISTING, StandardOpenOption.CREATE);

final String secondLine = "Tag-File-Character-Encoding: " + encoding + System.lineSeparator();
logger.debug(messages.getString("writing_line_to_file"), secondLine, bagitPath);
Files.write(bagitPath, secondLine.getBytes(StandardCharsets.UTF_8), StandardOpenOption.WRITE, StandardOpenOption.APPEND);

try (BufferedWriter writer = Files.newBufferedWriter(bagitPath,
StandardOpenOption.TRUNCATE_EXISTING, StandardOpenOption.CREATE)) {

final String firstLine = "BagIt-Version: " + version + System.lineSeparator();
logger.debug(messages.getString("writing_line_to_file"), firstLine, bagitPath);
writer.append(firstLine);

final String secondLine = "Tag-File-Character-Encoding: " + encoding + System.lineSeparator();
logger.debug(messages.getString("writing_line_to_file"), secondLine, bagitPath);
writer.append(secondLine);
}
}
}
14 changes: 9 additions & 5 deletions src/main/java/gov/loc/repository/bagit/writer/FetchWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import org.slf4j.LoggerFactory;

import gov.loc.repository.bagit.domain.FetchItem;
import java.io.BufferedWriter;

/**
* Responsible for writing out the list of {@link FetchItem} to the fetch.txt file on the filesystem
Expand All @@ -37,11 +38,14 @@ private FetchWriter(){
public static void writeFetchFile(final List<FetchItem> itemsToFetch, final Path outputDir, final Path bagitRootDir, final Charset charsetName) throws IOException{
logger.debug(messages.getString("writing_fetch_file_to_path"), outputDir);
final Path fetchFilePath = outputDir.resolve("fetch.txt");

for(final FetchItem item : itemsToFetch){
final String line = formatFetchLine(item, bagitRootDir);
logger.debug(messages.getString("writing_line_to_file"), line, fetchFilePath);
Files.write(fetchFilePath, line.getBytes(charsetName), StandardOpenOption.APPEND, StandardOpenOption.CREATE);

try (BufferedWriter writer = Files.newBufferedWriter(fetchFilePath, charsetName,
StandardOpenOption.APPEND, StandardOpenOption.CREATE)) {
for (final FetchItem item : itemsToFetch) {
final String line = formatFetchLine(item, bagitRootDir);
logger.debug(messages.getString("writing_line_to_file"), line, fetchFilePath);
writer.append(line);
}
}
}

Expand Down
19 changes: 11 additions & 8 deletions src/main/java/gov/loc/repository/bagit/writer/ManifestWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import org.slf4j.LoggerFactory;

import gov.loc.repository.bagit.domain.Manifest;
import java.io.BufferedWriter;

/**
* Responsible for writing out a {@link Manifest} to the filesystem
Expand Down Expand Up @@ -65,14 +66,16 @@ private static void writeManifests(final Set<Manifest> manifests, final Path out

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);

try (BufferedWriter writer = Files.newBufferedWriter(manifestPath, charsetName,
StandardOpenOption.APPEND, StandardOpenOption.CREATE)) {
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);
writer.append(line);
}
}
}
}
Expand Down