22
33import java .io .Closeable ;
44import java .io .File ;
5+ import java .io .IOException ;
56import java .text .MessageFormat ;
67import java .util .ArrayList ;
78import java .util .Collection ;
89import java .util .HashMap ;
910import java .util .HashSet ;
11+ import java .util .Iterator ;
1012import java .util .List ;
1113import java .util .Map ;
1214import java .util .Map .Entry ;
1315import java .util .Set ;
1416
17+ import org .apache .commons .io .FileUtils ;
1518import org .apache .commons .io .IOUtils ;
1619import org .apache .commons .logging .Log ;
1720import 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
0 commit comments