Skip to content

Commit 626fb89

Browse files
committed
fixed keeping of empty directories with .keep files in them
1 parent 267313f commit 626fb89

11 files changed

Lines changed: 90 additions & 13 deletions

File tree

build.gradle

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,3 +73,11 @@ pmdTest.enabled = false
7373
pmd {
7474
ruleSets = ["java-basic", "java-braces", "java-empty"]
7575
}
76+
77+
//Keep this for easy viewing of html findbugs report
78+
//tasks.withType(FindBugs) {
79+
// reports {
80+
// xml.enabled = false
81+
// html.enabled = true
82+
// }
83+
//}

src/main/java/gov/loc/repository/bagit/creator/AddPayloadToBagManifestVistor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) th
5050

5151
@Override
5252
public FileVisitResult visitFile(Path path, BasicFileAttributes attrs)throws IOException{
53-
if(!includeHiddenFiles && Files.isHidden(path)){
53+
if(!includeHiddenFiles && Files.isHidden(path) && !path.endsWith(".keep")){
5454
logger.debug("Skipping [{}] since we are ignoring hidden files", path);
5555
}
5656
else{

src/main/java/gov/loc/repository/bagit/reader/BagReader.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import gov.loc.repository.bagit.hash.BagitAlgorithmNameToSupportedAlgorithmMapping;
2222
import gov.loc.repository.bagit.hash.StandardBagitAlgorithmNameToSupportedAlgorithmMapping;
2323
import gov.loc.repository.bagit.hash.SupportedAlgorithm;
24+
import gov.loc.repository.bagit.util.PathUtils;
2425
import gov.loc.repository.bagit.verify.PayloadFileExistsInManifestVistor;
2526
import javafx.util.Pair;
2627

@@ -129,11 +130,13 @@ public Bag readAllManifests(Path rootDir, Bag bag) throws IOException{
129130
DirectoryStream<Path> manifests = getAllManifestFiles(rootDir);
130131

131132
for (Path path : manifests){
132-
if(path.getFileName().toString().startsWith("tagmanifest-")){
133+
String filename = PathUtils.getFilename(path);
134+
135+
if(filename.startsWith("tagmanifest-")){
133136
logger.debug("Found tag manifest [{}]", path);
134137
newBag.getTagManifests().add(readManifest(path, bag.getRootDir()));
135138
}
136-
else if(path.getFileName().toString().startsWith("manifest-")){
139+
else if(filename.startsWith("manifest-")){
137140
logger.debug("Found payload manifest [{}]", path);
138141
newBag.getPayLoadManifests().add(readManifest(path, bag.getRootDir()));
139142
}
@@ -145,7 +148,9 @@ else if(path.getFileName().toString().startsWith("manifest-")){
145148
protected DirectoryStream<Path> getAllManifestFiles(Path rootDir) throws IOException{
146149
DirectoryStream.Filter<Path> filter = new DirectoryStream.Filter<Path>() {
147150
public boolean accept(Path file) throws IOException {
148-
return file.getFileName().toString().startsWith("tagmanifest-") || file.getFileName().toString().startsWith("manifest-");
151+
if(file == null || file.getFileName() == null){ return false;}
152+
String filename = PathUtils.getFilename(file);
153+
return filename.startsWith("tagmanifest-") || filename.startsWith("manifest-");
149154
}
150155
};
151156

@@ -161,7 +166,7 @@ public boolean accept(Path file) throws IOException {
161166
*/
162167
public Manifest readManifest(Path manifestFile, Path bagRootDir) throws IOException{
163168
logger.debug("Reading manifest [{}]", manifestFile);
164-
String alg = manifestFile.getFileName().toString().split("[-\\.]")[1];
169+
String alg = PathUtils.getFilename(manifestFile).split("[-\\.]")[1];
165170
SupportedAlgorithm algorithm = nameMapping.getMessageDigestName(alg);
166171

167172
Manifest manifest = new Manifest(algorithm);
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package gov.loc.repository.bagit.util;
2+
3+
import java.nio.file.Path;
4+
5+
public class PathUtils {
6+
7+
/**
8+
* Needed to get rid of findbugs "dodgy code warnings" in regards to getting the filename of a path as a string
9+
*
10+
* @param path
11+
* @return the filename or an empty string
12+
*/
13+
public static String getFilename(Path path){
14+
String filename = "";
15+
if(path != null){
16+
Path filenamePath = path.getFileName();
17+
if(filenamePath != null){
18+
filename = filenamePath.toString();
19+
}
20+
}
21+
22+
return filename;
23+
}
24+
}

src/main/java/gov/loc/repository/bagit/verify/BagVerifier.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import gov.loc.repository.bagit.reader.BagReader;
3636
import gov.loc.repository.bagit.tasks.CheckIfFileExistsTask;
3737
import gov.loc.repository.bagit.tasks.CheckManifestHashsTask;
38+
import gov.loc.repository.bagit.util.PathUtils;
3839
import javafx.util.Pair;
3940

4041
/**
@@ -257,7 +258,7 @@ protected void checkIfAtLeastOnePayloadManifestsExist(Path rootDir, Version vers
257258
}
258259

259260
for(Path path : directoryStream){
260-
if(path.getFileName().toString().startsWith("manifest-")){
261+
if(PathUtils.getFilename(path).startsWith("manifest-")){
261262
logger.debug("Found payload manifest file [{}]", path.getFileName());
262263
hasAtLeastOneManifest = true;
263264
}
@@ -282,7 +283,8 @@ protected Set<Path> getAllFilesListedInManifests(Bag bag) throws IOException{
282283
BagReader reader = new BagReader(nameMapping);
283284

284285
for(Path path : directoryStream){
285-
if(path.getFileName().toString().startsWith("tagmanifest-") || path.getFileName().toString().startsWith("manifest-")){
286+
String filename = PathUtils.getFilename(path);
287+
if(filename.startsWith("tagmanifest-") || filename.startsWith("manifest-")){
286288
logger.debug("Getting files and checksums listed in [{}]", path);
287289
Manifest manifest = reader.readManifest(path, bag.getRootDir());
288290
filesListedInManifests.addAll(manifest.getFileToChecksumMap().keySet());

src/main/java/gov/loc/repository/bagit/verify/FileCountAndTotalSizeVistor.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,15 @@ public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) th
3636

3737
@Override
3838
public FileVisitResult visitFile(Path path, BasicFileAttributes attrs) throws IOException{
39-
count++;
40-
long size = Files.size(path);
41-
logger.debug("File [{}] hash a size of [{}] bytes", path, size);
42-
totalSize += size;
39+
if(!ignoreHiddenFiles && Files.isHidden(path) && !path.endsWith(".keep")){
40+
logger.debug("Skipping [{}] since we are ignoring hidden files", path);
41+
}
42+
else{
43+
count++;
44+
long size = Files.size(path);
45+
logger.debug("File [{}] hash a size of [{}] bytes", path, size);
46+
totalSize += size;
47+
}
4348

4449
return FileVisitResult.CONTINUE;
4550
}

src/main/java/gov/loc/repository/bagit/writer/BagWriter.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -191,8 +191,9 @@ protected static void writeAdditionalTagPayloadFiles(Set<Manifest> manifests, Pa
191191
for(Entry<Path, String> entry : manifest.getFileToChecksumMap().entrySet()){
192192
Path relativeLocation = bagRootDir.relativize(entry.getKey());
193193
Path writeTo = outputDir.resolve(relativeLocation);
194-
if(!Files.exists(writeTo)){
195-
Files.createDirectories(writeTo.getParent());
194+
Path writeToParent = writeTo.getParent();
195+
if(!Files.exists(writeTo) && writeToParent != null){
196+
Files.createDirectories(writeToParent);
196197
Files.copy(entry.getKey(), writeTo);
197198
}
198199
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package gov.loc.repository.bagit.creator;
2+
3+
import java.nio.file.Files;
4+
import java.nio.file.Path;
5+
import java.nio.file.Paths;
6+
import java.security.MessageDigest;
7+
8+
import org.junit.Assert;
9+
import org.junit.Test;
10+
11+
import gov.loc.repository.bagit.domain.Manifest;
12+
import gov.loc.repository.bagit.hash.StandardSupportedAlgorithms;
13+
14+
public class AddPayloadToBagManifestVistorTest extends Assert {
15+
16+
@Test
17+
public void includeDotKeepFilesInManifest() throws Exception{
18+
Manifest manifest = new Manifest(StandardSupportedAlgorithms.MD5);
19+
MessageDigest messageDigest = MessageDigest.getInstance("MD5");
20+
boolean includeHiddenFiles = false;
21+
Path start = Paths.get(getClass().getClassLoader().getResource("dotKeepExampleBag").getFile()).resolve("data");
22+
23+
AddPayloadToBagManifestVistor sut = new AddPayloadToBagManifestVistor(manifest, messageDigest, includeHiddenFiles);
24+
Files.walkFileTree(start, sut);
25+
26+
assertEquals(1, manifest.getFileToChecksumMap().size());
27+
assertTrue(manifest.getFileToChecksumMap().containsKey(start.resolve("fooDir/.keep")));
28+
}
29+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
BagIt-Version: 0.97
2+
Tag-File-Character-Encoding: UTF-8

src/test/resources/dotKeepExampleBag/data/fooDir/.keep

Whitespace-only changes.

0 commit comments

Comments
 (0)