Skip to content

Commit

Permalink
refs #84 - actually delete folders when calling remove directory on a…
Browse files Browse the repository at this point in the history
… bag
  • Loading branch information
johnscancella committed Feb 23, 2017
1 parent 2cd5d9e commit ff2af82
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 35 deletions.
68 changes: 39 additions & 29 deletions src/main/java/gov/loc/repository/bagit/impl/AbstractBag.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,19 @@

import java.io.Closeable;
import java.io.File;
import java.io.IOException;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
Expand Down Expand Up @@ -541,24 +544,25 @@ public void removePayloadDirectory(String filepath) {
filepath = bagConstants.getDataDirectory() + "/" + filepath;
}

if ((bagConstants.getDataDirectory() + "/").equals(filepath)) {
return;
}

log.debug("Removing payload directory " + filepath);

log.debug("remove all the files under " + filepath);
//remove all the files. Need to create list so there is no concurrentModificationException
List<String> deleteFilepaths = new ArrayList<String>();
for(BagFile bagFile : this.getPayload()) {
if (bagFile.getFilepath().startsWith(filepath)) {
deleteFilepaths.add(bagFile.getFilepath());
}
}
for(String deleteFilepath : deleteFilepaths) {
log.debug("Removing " + deleteFilepath);
this.removeBagFile(deleteFilepath);
}

for(BagFile bagFile : this.getPayload()) {
if (bagFile.getFilepath().startsWith(filepath)) {
deleteFilepaths.add(bagFile.getFilepath());
}
}

for(String deleteFilepath : deleteFilepaths) {
log.debug("Removing " + deleteFilepath);
this.removeBagFile(deleteFilepath);
}
//now remove the empty directories
try {
FileUtils.deleteDirectory(new File(filepath));
} catch (IOException e) {
log.error("Could not delete payload directory [" + filepath + "]!", e);
}
}

@Override
Expand All @@ -570,21 +574,27 @@ public void removeTagDirectory(String filepath) {
if (filepath.startsWith(bagConstants.getDataDirectory())) {
throw new RuntimeException("Trying to remove payload");
}
log.debug("Removing tag directory " + filepath);


log.debug("remove all the files under " + filepath);
//remove all the files. Need to create list so there is no concurrentModificationException
List<String> deleteFilepaths = new ArrayList<String>();
for(BagFile bagFile : this.getTags()) {
if (bagFile.getFilepath().startsWith(filepath)) {
deleteFilepaths.add(bagFile.getFilepath());
}
}
for(String deleteFilepath : deleteFilepaths) {
log.debug("Removing " + deleteFilepath);
this.removeBagFile(deleteFilepath);
}

for(BagFile bagFile : this.getTags()) {
if (bagFile.getFilepath().startsWith(filepath)) {
deleteFilepaths.add(bagFile.getFilepath());
}
}

for(String deleteFilepath : deleteFilepaths) {
log.debug("Removing " + deleteFilepath);
this.removeBagFile(deleteFilepath);
}
log.debug("Removing tag directory " + filepath);
//now remove the empty directories
try {
FileUtils.deleteDirectory(new File(filepath));
} catch (IOException e) {
log.error("Could not delete tag directory [" + filepath + "]!", e);
}
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -549,9 +549,6 @@ public void testRemoveDirectory() throws Exception {

bag.removePayloadDirectory("data/test1.txt");
assertNotNull(bag.getBagFile("data/test1.txt"));

bag.removePayloadDirectory("data");
assertNotNull(bag.getBagFile("data/test1.txt"));
} finally {
bag.close();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,6 @@ public void testRemoveDirectory() throws Exception

bag.removePayloadDirectory("data/bag/data/test1.txt");
assertNotNull(bag.getBagFile("data/bag/data/test1.txt"));

bag.removePayloadDirectory("data");
assertNotNull(bag.getBagFile("data/bag/manifest-md5.txt"));
} finally {
bag.close();
}
Expand Down

0 comments on commit ff2af82

Please sign in to comment.