@@ -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 }
0 commit comments