Skip to content
Browse files

Reimplemented FileUtils.moveFile in that it falls back to copying if …

…rename fails

(cherry picked from commit 62d36d0)

Conflicts:

	kernel/src/main/java/org/neo4j/kernel/impl/util/FileUtils.java
	kernel/src/test/java/org/neo4j/kernel/impl/AbstractNeo4jTestCase.java
  • Loading branch information...
1 parent 3b5e35e commit 55a7a1cbf2f114089d84eeecd8145a5abf52d627 @tinwelint tinwelint committed Apr 12, 2012
View
2 kernel/src/main/java/org/neo4j/kernel/impl/storemigration/StoreFiles.java
@@ -73,7 +73,7 @@ public static void move( File fromDirectory, File toDirectory )
static void moveFile( String fileName, File fromDirectory,
File toDirectory ) throws IOException
{
- if ( FileUtils.moveFile( new File( fromDirectory, fileName ),
+ if ( FileUtils.moveFileToDirectory( new File( fromDirectory, fileName ),
toDirectory ) == null )
{
throw new IOException( "Move of file " + fileName + " from "
View
54 kernel/src/main/java/org/neo4j/kernel/impl/util/FileUtils.java
@@ -117,27 +117,65 @@ public static boolean deleteFile( File file )
}
return deletedFiles.toArray( new File[deletedFiles.size()] );
}
+
+ /**
+ * Utility method that moves a file from its current location to the
+ * new target location. If rename fails (for example if the target is
+ * another disk) a copy/delete will be performed instead. This is not a rename,
+ * use {@link #renameFile(File, File)} instead.
+ *
+ * @param toMove The File object to move.
+ * @param target Target file to move to.
+ * @return the new file, null iff the move was unsuccessful
+ * @throws IOException
+ */
+ public static void moveFile( File toMove, File target ) throws IOException
+ {
+ if ( !toMove.exists() )
+ throw new NotFoundException( "Source file[" + toMove.getName()
+ + "] not found" );
+ if ( target.exists() )
+ throw new NotFoundException( "Target file[" + target.getName()
+ + "] already exists" );
+
+ if ( toMove.renameTo( target ) )
+ return;
+
+ if ( toMove.isDirectory() )
+ {
+ target.mkdirs();
+ copyRecursively( toMove, target );
+ deleteRecursively( toMove );
+ }
+ else
+ {
+ copyFile( toMove, target );
+ deleteFile( toMove );
+ }
+ }
/**
* Utility method that moves a file from its current location to the
- * provided target directory. This is not a rename,
- * use {@link #renameFile(File, File)} instead. The reason this exists is
- * for convenience and error checking.
+ * provided target directory. If rename fails (for example if the target is
+ * another disk) a copy/delete will be performed instead. This is not a rename,
+ * use {@link #renameFile(File, File)} instead.
*
- * @param toMove The File object to move. Cannot be a directory.
+ * @param toMove The File object to move.
* @param targetDirectory
* @return the new file, null iff the move was unsuccessful
+ * @throws IOException
*/
- public static File moveFile( File toMove, File targetDirectory )
+ public static File moveFileToDirectory( File toMove, File targetDirectory ) throws IOException
{
if ( !targetDirectory.isDirectory() )
{
throw new IllegalArgumentException(
"Move target must be a directory, not " + targetDirectory );
}
- String oldName = toMove.getName();
- File endFile = new File( targetDirectory, oldName );
- return renameFile( toMove, endFile ) ? toMove : null;
+
+ File target = new File( targetDirectory, toMove.getName() );
+ moveFile( toMove, target );
+ return target;
}
public static boolean renameFile( File srcFile, File renameToFile )
View
13 kernel/src/test/java/org/neo4j/kernel/impl/AbstractNeo4jTestCase.java
@@ -20,6 +20,7 @@
package org.neo4j.kernel.impl;
import java.io.File;
+import java.io.IOException;
import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
@@ -43,6 +44,7 @@
import org.neo4j.kernel.impl.nioneo.store.PropertyStore;
import org.neo4j.kernel.impl.nioneo.xa.NeoStoreXaConnection;
import org.neo4j.kernel.impl.transaction.XaDataSourceManager;
+import org.neo4j.kernel.impl.util.FileUtils;
import org.neo4j.test.ImpermanentGraphDatabase;
@AbstractNeo4jTestCase.RequiresPersistentGraphDatabase( false )
@@ -82,6 +84,17 @@ public GraphDatabaseService getGraphDb()
private static void setupGraphDatabase( boolean requiresPersistentGraphDatabase )
{
AbstractNeo4jTestCase.requiresPersistentGraphDatabase = requiresPersistentGraphDatabase;
+ if ( requiresPersistentGraphDatabase )
+ {
+ try
+ {
+ FileUtils.deleteRecursively( new File( getStorePath( "neo-test" ) ) );
+ }
+ catch ( IOException e )
+ {
+ throw new RuntimeException( e );
+ }
+ }
graphDb = requiresPersistentGraphDatabase ? new EmbeddedGraphDatabase( getStorePath( "neo-test" ) ) : new ImpermanentGraphDatabase();
}
View
81 kernel/src/test/java/org/neo4j/kernel/impl/util/TestFileUtils.java
@@ -0,0 +1,81 @@
+/**
+ * Copyright (c) 2002-2012 "Neo Technology,"
+ * Network Engine for Objects in Lund AB [http://neotechnology.com]
+ *
+ * This file is part of Neo4j.
+ *
+ * Neo4j is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+package org.neo4j.kernel.impl.util;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+
+import java.io.File;
+import java.io.IOException;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.neo4j.test.TargetDirectory;
+
+public class TestFileUtils
+{
+ private File path;
+
+ @Before
+ public void doBefore() throws Exception
+ {
+ path = TargetDirectory.forTest( getClass() ).directory( "path", true );
+ }
+
+ @Test
+ public void moveFileToDirectory() throws Exception
+ {
+ File file = touchFile( "source" );
+ File targetDir = directory( "dir" );
+
+ File newLocationOfFile = FileUtils.moveFileToDirectory( file, targetDir );
+ assertTrue( newLocationOfFile.exists() );
+ assertFalse( file.exists() );
+ assertEquals( newLocationOfFile, targetDir.listFiles()[0] );
+ }
+
+ @Test
+ public void moveFile() throws Exception
+ {
+ File file = touchFile( "source" );
+ File targetDir = directory( "dir" );
+
+ File newLocationOfFile = new File( targetDir, "new-name" );
+ FileUtils.moveFile( file, newLocationOfFile );
+ assertTrue( newLocationOfFile.exists() );
+ assertFalse( file.exists() );
+ assertEquals( newLocationOfFile, targetDir.listFiles()[0] );
+ }
+
+ private File directory( String name ) throws IOException
+ {
+ File dir = new File( path, name );
+ dir.mkdirs();
+ return dir;
+ }
+
+ private File touchFile( String name ) throws IOException
+ {
+ File file = new File( path, name );
+ file.createNewFile();
+ return file;
+ }
+}

0 comments on commit 55a7a1c

Please sign in to comment.
Something went wrong with that request. Please try again.