Skip to content

Commit 04e8d5d

Browse files
committed
refs #108 - fixed error when bagging in place and data file was named 'data'
1 parent b4b3ef4 commit 04e8d5d

File tree

2 files changed

+23
-18
lines changed

2 files changed

+23
-18
lines changed

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

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,6 @@ private static Bag bagInPlace(final Version version, final Path root, final Coll
122122
logger.info(messages.getString("creating_bag"), bag.getVersion(), root);
123123
bag.setRootDir(root);
124124

125-
createDirectories(bag);
126125
moveDataFilesIfNeeded(bag, includeHidden);
127126

128127
createBagitFile(bag);
@@ -136,32 +135,28 @@ private static Bag bagInPlace(final Version version, final Path root, final Coll
136135
return bag;
137136
}
138137

139-
//create the data or .bagit directory
140-
private static void createDirectories(final Bag bag) throws IOException{
141-
if(bag.getVersion().isSameOrNewer(DOT_BAGIT_VERSION)){
142-
//create .bagit directory
138+
private static void moveDataFilesIfNeeded(final Bag bag, final boolean includeHidden) throws IOException {
139+
if(bag.getVersion().isOlder(DOT_BAGIT_VERSION)) {
140+
Path tempDir = bag.getRootDir().resolve(System.currentTimeMillis() + ".temp");
141+
Files.createDirectory(tempDir);
142+
moveDataFiles(bag.getRootDir(), tempDir, includeHidden);
143+
Files.move(tempDir, PathUtils.getDataDir(bag));
144+
}
145+
else {
143146
final Path dotbagitDir = bag.getRootDir().resolve(".bagit");
144147
Files.createDirectories(dotbagitDir);
145148
}
146-
else{
147-
//create data directory
148-
final Path dataDir = bag.getRootDir().resolve("data");
149-
Files.createDirectories(dataDir);
150-
}
151149
}
152150

153151
private static void createBagitFile(final Bag bag) throws IOException{
154152
BagitFileWriter.writeBagitFile(bag.getVersion(), bag.getFileEncoding(), PathUtils.getBagitDir(bag));
155153
}
156154

157-
private static void moveDataFilesIfNeeded(final Bag bag, final boolean includeHidden) throws IOException{
158-
if(bag.getVersion().isOlder(DOT_BAGIT_VERSION)){
159-
final Path dataDir = PathUtils.getDataDir(bag);
160-
try(final DirectoryStream<Path> directoryStream = Files.newDirectoryStream(bag.getRootDir())){
161-
for(final Path path : directoryStream){
162-
if(!path.equals(dataDir) && (!PathUtils.isHidden(path) || includeHidden)){
163-
Files.move(path, dataDir.resolve(path.getFileName()));
164-
}
155+
private static void moveDataFiles(final Path rootDir, final Path dataDir, final boolean includeHidden) throws IOException{
156+
try(final DirectoryStream<Path> directoryStream = Files.newDirectoryStream(rootDir)){
157+
for(final Path path : directoryStream){
158+
if(!path.equals(dataDir) && (!PathUtils.isHidden(path) || includeHidden)){
159+
Files.move(path, dataDir.resolve(path.getFileName()));
165160
}
166161
}
167162
}

src/test/java/gov/loc/repository/bagit/creator/BagCreatorTest.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,16 @@ public class BagCreatorTest extends Assert {
2525
@Rule
2626
public TemporaryFolder folder= new TemporaryFolder();
2727

28+
@Test
29+
public void testBagInPlaceWithFileNamedData() throws IOException, NoSuchAlgorithmException{
30+
File testFolder = folder.newFolder();
31+
File dataFile = new File(testFolder, "data");
32+
Files.createFile(dataFile.toPath());
33+
34+
BagCreator.bagInPlace(Paths.get(testFolder.toURI()), Arrays.asList(StandardSupportedAlgorithms.MD5), false);
35+
assertTrue(Files.exists(testFolder.toPath().resolve("data").resolve("data")));
36+
}
37+
2838
@Test
2939
public void testBagInPlace() throws IOException, NoSuchAlgorithmException{
3040
TestStructure structure = createTestStructure();

0 commit comments

Comments
 (0)