Skip to content

Commit aea608f

Browse files
committed
Revert "refs #84 - actually delete folders when calling remove directory on a bag"
This reverts commit ff2af82.
1 parent ff2af82 commit aea608f

3 files changed

Lines changed: 35 additions & 39 deletions

File tree

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

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

33
import java.io.Closeable;
44
import java.io.File;
5-
import java.io.IOException;
65
import java.text.MessageFormat;
76
import java.util.ArrayList;
87
import java.util.Collection;
98
import java.util.HashMap;
109
import java.util.HashSet;
11-
import java.util.Iterator;
1210
import java.util.List;
1311
import java.util.Map;
1412
import java.util.Map.Entry;
1513
import java.util.Set;
1614

17-
import org.apache.commons.io.FileUtils;
1815
import org.apache.commons.io.IOUtils;
1916
import org.apache.commons.logging.Log;
2017
import org.apache.commons.logging.LogFactory;
@@ -544,25 +541,24 @@ public void removePayloadDirectory(String filepath) {
544541
filepath = bagConstants.getDataDirectory() + "/" + filepath;
545542
}
546543

547-
log.debug("remove all the files under " + filepath);
548-
//remove all the files. Need to create list so there is no concurrentModificationException
544+
if ((bagConstants.getDataDirectory() + "/").equals(filepath)) {
545+
return;
546+
}
547+
548+
log.debug("Removing payload directory " + filepath);
549+
549550
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-
}
559551

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-
}
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+
}
566562
}
567563

568564
@Override
@@ -574,27 +570,21 @@ public void removeTagDirectory(String filepath) {
574570
if (filepath.startsWith(bagConstants.getDataDirectory())) {
575571
throw new RuntimeException("Trying to remove payload");
576572
}
577-
578-
log.debug("remove all the files under " + filepath);
579-
//remove all the files. Need to create list so there is no concurrentModificationException
580-
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-
}
590573

591574
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-
}
575+
576+
List<String> deleteFilepaths = new ArrayList<String>();
577+
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+
}
598588
}
599589

600590

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -549,6 +549,9 @@ 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"));
552555
} finally {
553556
bag.close();
554557
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ 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"));
4447
} finally {
4548
bag.close();
4649
}

0 commit comments

Comments
 (0)