Skip to content

Commit ff2af82

Browse files
committed
refs #84 - actually delete folders when calling remove directory on a bag
1 parent 2cd5d9e commit ff2af82

3 files changed

Lines changed: 39 additions & 35 deletions

File tree

src/main/java/gov/loc/repository/bagit/impl/AbstractBag.java

Lines changed: 39 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,19 @@
22

33
import java.io.Closeable;
44
import java.io.File;
5+
import java.io.IOException;
56
import java.text.MessageFormat;
67
import java.util.ArrayList;
78
import java.util.Collection;
89
import java.util.HashMap;
910
import java.util.HashSet;
11+
import java.util.Iterator;
1012
import java.util.List;
1113
import java.util.Map;
1214
import java.util.Map.Entry;
1315
import java.util.Set;
1416

17+
import org.apache.commons.io.FileUtils;
1518
import org.apache.commons.io.IOUtils;
1619
import org.apache.commons.logging.Log;
1720
import org.apache.commons.logging.LogFactory;
@@ -541,24 +544,25 @@ public void removePayloadDirectory(String filepath) {
541544
filepath = bagConstants.getDataDirectory() + "/" + filepath;
542545
}
543546

544-
if ((bagConstants.getDataDirectory() + "/").equals(filepath)) {
545-
return;
546-
}
547-
548-
log.debug("Removing payload directory " + filepath);
549-
547+
log.debug("remove all the files under " + filepath);
548+
//remove all the files. Need to create list so there is no concurrentModificationException
550549
List<String> deleteFilepaths = new ArrayList<String>();
550+
for(BagFile bagFile : this.getPayload()) {
551+
if (bagFile.getFilepath().startsWith(filepath)) {
552+
deleteFilepaths.add(bagFile.getFilepath());
553+
}
554+
}
555+
for(String deleteFilepath : deleteFilepaths) {
556+
log.debug("Removing " + deleteFilepath);
557+
this.removeBagFile(deleteFilepath);
558+
}
551559

552-
for(BagFile bagFile : this.getPayload()) {
553-
if (bagFile.getFilepath().startsWith(filepath)) {
554-
deleteFilepaths.add(bagFile.getFilepath());
555-
}
556-
}
557-
558-
for(String deleteFilepath : deleteFilepaths) {
559-
log.debug("Removing " + deleteFilepath);
560-
this.removeBagFile(deleteFilepath);
561-
}
560+
//now remove the empty directories
561+
try {
562+
FileUtils.deleteDirectory(new File(filepath));
563+
} catch (IOException e) {
564+
log.error("Could not delete payload directory [" + filepath + "]!", e);
565+
}
562566
}
563567

564568
@Override
@@ -570,21 +574,27 @@ public void removeTagDirectory(String filepath) {
570574
if (filepath.startsWith(bagConstants.getDataDirectory())) {
571575
throw new RuntimeException("Trying to remove payload");
572576
}
573-
574-
log.debug("Removing tag directory " + filepath);
575-
577+
578+
log.debug("remove all the files under " + filepath);
579+
//remove all the files. Need to create list so there is no concurrentModificationException
576580
List<String> deleteFilepaths = new ArrayList<String>();
581+
for(BagFile bagFile : this.getTags()) {
582+
if (bagFile.getFilepath().startsWith(filepath)) {
583+
deleteFilepaths.add(bagFile.getFilepath());
584+
}
585+
}
586+
for(String deleteFilepath : deleteFilepaths) {
587+
log.debug("Removing " + deleteFilepath);
588+
this.removeBagFile(deleteFilepath);
589+
}
577590

578-
for(BagFile bagFile : this.getTags()) {
579-
if (bagFile.getFilepath().startsWith(filepath)) {
580-
deleteFilepaths.add(bagFile.getFilepath());
581-
}
582-
}
583-
584-
for(String deleteFilepath : deleteFilepaths) {
585-
log.debug("Removing " + deleteFilepath);
586-
this.removeBagFile(deleteFilepath);
587-
}
591+
log.debug("Removing tag directory " + filepath);
592+
//now remove the empty directories
593+
try {
594+
FileUtils.deleteDirectory(new File(filepath));
595+
} catch (IOException e) {
596+
log.error("Could not delete tag directory [" + filepath + "]!", e);
597+
}
588598
}
589599

590600

src/test/java/gov/loc/repository/bagit/impl/AbstractBagImplTest.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -549,9 +549,6 @@ public void testRemoveDirectory() throws Exception {
549549

550550
bag.removePayloadDirectory("data/test1.txt");
551551
assertNotNull(bag.getBagFile("data/test1.txt"));
552-
553-
bag.removePayloadDirectory("data");
554-
assertNotNull(bag.getBagFile("data/test1.txt"));
555552
} finally {
556553
bag.close();
557554
}

src/test/java/gov/loc/repository/bagit/v0_96/BagInABagTests.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,6 @@ public void testRemoveDirectory() throws Exception
4141

4242
bag.removePayloadDirectory("data/bag/data/test1.txt");
4343
assertNotNull(bag.getBagFile("data/bag/data/test1.txt"));
44-
45-
bag.removePayloadDirectory("data");
46-
assertNotNull(bag.getBagFile("data/bag/manifest-md5.txt"));
4744
} finally {
4845
bag.close();
4946
}

0 commit comments

Comments
 (0)