static Consumer<FileHandle> handleRename( File to ) { return fileHandle -> { try { fileHandle.rename( to ); } catch ( IOException e ) { throw new UncheckedIOException( e ); } }; }
private void moveNativeIndexFile( DatabaseLayout storeStructure, File nativeLabelIndex ) throws IOException { Optional<FileHandle> nativeIndexFileHandle = fileSystem.streamFilesRecursive( nativeLabelIndex ).findFirst(); if ( nativeIndexFileHandle.isPresent() ) { nativeIndexFileHandle.get().rename( storeStructure.labelScanStore() ); } }
/** * Create a consumer of FileHandle that uses fileHandle.rename to move file held by file handle to move from * directory to directory. * <p> * Possibly IOException will be wrapped in UncheckedIOException * * @param from Directory to move file from. * @param to Directory to move file to. * @return A new Consumer that moves the file wrapped by the file handle. */ static Consumer<FileHandle> handleRenameBetweenDirectories( File from, File to ) { return fileHandle -> { try { fileHandle.rename( FileUtils.pathToFileAfterMove( from, to, fileHandle.getFile() ) ); } catch ( IOException e ) { throw new UncheckedIOException( e ); } }; }
@Test void streamFilesRecursiveRenameMustCanonicaliseTargetFile() throws Exception { // File 'b' should canonicalise from 'b/poke/..' to 'b', which is a file that doesn't exists. // Thus, this should not throw a NoSuchFileException for the 'poke' directory. File a = existingFile( "a" ); File b = new File( new File( new File( path, "b" ), "poke" ), ".." ); FileHandle handle = fsa.streamFilesRecursive( a ).findAny().get(); handle.rename( b ); }
@Test void streamFilesRecursiveRenameMustCanonicaliseSourceFile() throws Exception { // File 'a' should canonicalise from 'a/poke/..' to 'a', which is a file that exists. // Thus, this should not throw a NoSuchFileException. File a = new File( new File( existingFile( "a" ), "poke" ), ".." ); File b = nonExistingFile( "b" ); FileHandle handle = fsa.streamFilesRecursive( a ).findAny().get(); handle.rename( b ); // must not throw }
@Test void streamFilesRecursiveMustNotThrowWhenTargetFileOfRenameAlreadyExistsAndUsingReplaceExisting() throws Exception { File a = existingFile( "a" ); File b = existingFile( "b" ); FileHandle handle = fsa.streamFilesRecursive( a ).findAny().get(); handle.rename( b, StandardCopyOption.REPLACE_EXISTING ); }
@Test void streamFilesRecursiveMustThrowWhenTargetFileOfRenameAlreadyExists() throws Exception { File a = existingFile( "a" ); File b = existingFile( "b" ); FileHandle handle = fsa.streamFilesRecursive( a ).findAny().get(); assertThrows( FileAlreadyExistsException.class, () -> handle.rename( b ) ); }
@Test void streamFilesRecursiveMustCreateMissingPathDirectoriesImpliedByFileRename() throws Exception { File a = existingFile( "a" ); File sub = new File( path, "sub" ); // does not exists File target = new File( sub, "b" ); FileHandle handle = fsa.streamFilesRecursive( a ).findAny().get(); handle.rename( target ); assertTrue( fsa.isDirectory( sub ) ); assertTrue( fsa.fileExists( target ) ); }
@Test void streamFilesRecursiveRenameMustNotChangeSourceFileContents() throws Exception { File a = existingFile( "a" ); File b = nonExistingFile( "b" ); generateFileWithRecords( a, recordCount ); FileHandle handle = fsa.streamFilesRecursive( a ).findAny().get(); handle.rename( b ); verifyRecordsInFile( b, recordCount ); }
@Test void streamFilesRecursiveRenameTargetFileMustBeRenamed() throws Exception { File a = existingFile( "a" ); File b = nonExistingFile( "b" ); FileHandle handle = fsa.streamFilesRecursive( a ).findAny().get(); handle.rename( b ); assertTrue( fsa.fileExists( b ) ); }
@Test void streamFilesRecursiveSourceFileMustNotBeMappableAfterRename() throws Exception { File a = existingFile( "a" ); File b = nonExistingFile( "b" ); FileHandle handle = fsa.streamFilesRecursive( a ).findAny().get(); handle.rename( b ); assertFalse( fsa.fileExists( a ) ); }
@Test void streamFilesRecursiveRenameMustNotChangeSourceFileContentsWithReplaceExisting() throws Exception { File a = existingFile( "a" ); File b = existingFile( "b" ); generateFileWithRecords( a, recordCount ); generateFileWithRecords( b, recordCount + recordsPerFilePage ); // Fill 'b' with random data try ( StoreChannel channel = fsa.open( b, OpenMode.READ_WRITE ) ) { ThreadLocalRandom rng = ThreadLocalRandom.current(); int fileSize = (int) channel.size(); ByteBuffer buffer = ByteBuffer.allocate( fileSize ); for ( int i = 0; i < fileSize; i++ ) { buffer.put( i, (byte) rng.nextInt() ); } buffer.rewind(); channel.writeAll( buffer ); } // Do the rename FileHandle handle = fsa.streamFilesRecursive( a ).findAny().get(); handle.rename( b, REPLACE_EXISTING ); // Then verify that the old random data we put in 'b' has been replaced with the contents of 'a' verifyRecordsInFile( b, recordCount ); }
static Consumer<FileHandle> handleRename( File to ) { return fileHandle -> { try { fileHandle.rename( to ); } catch ( IOException e ) { throw new UncheckedIOException( e ); } }; }
@Override public void move( File toDir, CopyOption... copyOptions ) throws IOException { Optional<FileHandle> handle = pageCache.getCachedFileSystem().streamFilesRecursive( file ).findAny(); boolean directoryExistsInCachedSystem = handle.isPresent(); if ( directoryExistsInCachedSystem ) { handle.get().rename( new File( toDir, file.getName() ), copyOptions ); } }
private void moveNativeIndexFile( DatabaseLayout storeStructure, File nativeLabelIndex ) throws IOException { Optional<FileHandle> nativeIndexFileHandle = fileSystem.streamFilesRecursive( nativeLabelIndex ).findFirst(); if ( nativeIndexFileHandle.isPresent() ) { nativeIndexFileHandle.get().rename( storeStructure.labelScanStore() ); } }
/** * Create a consumer of FileHandle that uses fileHandle.rename to move file held by file handle to move from * directory to directory. * <p> * Possibly IOException will be wrapped in UncheckedIOException * * @param from Directory to move file from. * @param to Directory to move file to. * @return A new Consumer that moves the file wrapped by the file handle. */ static Consumer<FileHandle> handleRenameBetweenDirectories( File from, File to ) { return fileHandle -> { try { fileHandle.rename( FileUtils.pathToFileAfterMove( from, to, fileHandle.getFile() ) ); } catch ( IOException e ) { throw new UncheckedIOException( e ); } }; }
public void moveTo( File source, File target, LogFiles logFiles ) throws IOException { fs.mkdirs( logFiles.logFilesDirectory() ); for ( File candidate : fs.listFiles( source, fileFilter ) ) { File destination = logFiles.isLogFile( candidate) ? logFiles.logFilesDirectory() : target; fs.moveToDirectory( candidate, destination ); } Iterable<FileHandle> fileHandles = acceptedPageCachedFiles( source )::iterator; for ( FileHandle fh : fileHandles ) { fh.rename( new File( target, fh.getRelativeFile().getPath() ), StandardCopyOption.REPLACE_EXISTING ); } }