static Consumer<FileHandle> handleRename( File to ) { return fileHandle -> { try { fileHandle.rename( to ); } catch ( IOException e ) { throw new UncheckedIOException( e ); } }; }
/** * 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 ); } }; }
private void deleteNativeIndexFile( DatabaseLayout directoryStructure ) throws IOException { Optional<FileHandle> indexFile = fileSystem.streamFilesRecursive( NativeLabelScanStore.getLabelScanStoreFile( directoryStructure ) ).findFirst(); if ( indexFile.isPresent() ) { try { indexFile.get().delete(); } catch ( NoSuchFileException e ) { // Already deleted, ignore } } }
@Test void streamFilesRecursiveMustNotSeeFilesRenamedIntoSubDirectory() throws Exception { File a = existingFile( "a" ); File sub = existingDirectory( "sub" ); File target = new File( sub, "target" ); Set<File> observedFiles = new HashSet<>(); fsa.streamFilesRecursive( a.getParentFile() ).forEach( fh -> { File file = fh.getFile(); observedFiles.add( file ); if ( file.equals( a ) ) { handleRename( target ).accept( fh ); } } ); assertThat( observedFiles, containsInAnyOrder( a ) ); }
for ( FileHandle fileHandle : fileHandles ) Path sourcePath = fileHandle.getFile().toPath(); Path zipFsPath = fileSystem.isDirectory( sourceToCompress ) ? zipFs.getPath( rootPath.relativize( sourcePath ).toString() ) : zipFs.getPath( sourcePath.getFileName().toString() );
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 ); } }
public static void moveAwayDbWithPageCache( File from, File to, PageCache pageCache, FileFilter filter ) { final Stream<FileHandle> fileHandleStream; try { fileHandleStream = pageCache.getCachedFileSystem().streamFilesRecursive( from ); } catch ( IOException e ) { // Directory does not exist, has possibly been moved with file system previous to this call. return; } final Consumer<FileHandle> handleRename = FileHandle.handleRenameBetweenDirectories( from, to ); fileHandleStream.filter( fh -> filter.accept( fh.getFile() ) ).forEach( handleRename ); }
@Test void streamFilesRecursiveMustRenameFiles() throws Exception { File a = existingFile( "a" ); File b = nonExistingFile( "b" ); // does not yet exist File base = a.getParentFile(); fsa.streamFilesRecursive( base ).forEach( handleRename( b ) ); List<File> filepaths = fsa.streamFilesRecursive( base ).map( FileHandle::getFile ).collect( toList() ); assertThat( filepaths, containsInAnyOrder( b.getCanonicalFile() ) ); }
private Stream<FileHandle> acceptedPageCachedFiles( File storeDir ) throws IOException { try { Stream<FileHandle> stream = pageCache.getCachedFileSystem().streamFilesRecursive( storeDir ); Predicate<FileHandle> acceptableFiles = fh -> fileFilter.accept( storeDir, fh.getRelativeFile().getPath() ); return stream.filter( acceptableFiles ); } catch ( NoSuchFileException e ) { // This is fine. Just ignore empty or non-existing directories. return Stream.empty(); } }
public static void cleanStoreDir( File storeDir, PageCache pageCache ) throws IOException { for ( File file : relevantDbFiles( storeDir ) ) { FileUtils.deleteRecursively( file ); } pageCache.getCachedFileSystem().streamFilesRecursive( storeDir ) .filter( fh -> DEEP_STORE_FILE_FILTER.accept( fh.getFile() ) ).forEach( HANDLE_DELETE ); }
@Test void streamFilesRecursiveMustNotSeeFilesRenamedIntoBaseDirectory() throws Exception { File a = existingFile( "a" ); File sub = existingDirectory( "sub" ); File x = new File( sub, "x" ); ensureExists( x ); File target = nonExistingFile( "target" ); Set<File> observedFiles = new HashSet<>(); fsa.streamFilesRecursive( a.getParentFile() ).forEach( fh -> { File file = fh.getFile(); observedFiles.add( file ); if ( file.equals( x ) ) { handleRename( target ).accept( fh ); } } ); assertThat( observedFiles, containsInAnyOrder( a, x ) ); }
@Test void streamFilesRecursiveMustDeleteSubDirectoriesEmptiedByFileRename() throws Exception { File sub = existingDirectory( "sub" ); File x = new File( sub, "x" ); ensureExists( x ); File target = nonExistingFile( "target" ); fsa.streamFilesRecursive( sub ).forEach( handleRename( target ) ); assertFalse( fsa.isDirectory( sub ) ); assertFalse( fsa.fileExists( sub ) ); }
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 boolean isEmpty( File storeDir, List<File> filesToLookFor ) throws IOException { // 'files' can be null if the directory doesn't exist. This is fine, we just ignore it then. File[] files = fs.listFiles( storeDir, fileFilter ); if ( files != null ) { for ( File file : files ) { if ( filesToLookFor.contains( file ) ) { return false; } } } Iterable<FileHandle> fileHandles = acceptedPageCachedFiles( storeDir )::iterator; for ( FileHandle fh : fileHandles ) { if ( filesToLookFor.contains( fh.getFile() ) ) { return false; } } return true; }
private void deleteNativeIndexFile( DatabaseLayout directoryStructure ) throws IOException { Optional<FileHandle> indexFile = fileSystem.streamFilesRecursive( NativeLabelScanStore.getLabelScanStoreFile( directoryStructure ) ).findFirst(); if ( indexFile.isPresent() ) { try { indexFile.get().delete(); } catch ( NoSuchFileException e ) { // Already deleted, ignore } } }
@Test void streamFilesRecursiveMustDeleteMultipleLayersOfSubDirectoriesIfTheyBecomeEmptyByRename() throws Exception { File sub = existingDirectory( "sub" ); File subsub = new File( sub, "subsub" ); ensureDirectoryExists( subsub ); File x = new File( subsub, "x" ); ensureExists( x ); File target = nonExistingFile( "target" ); fsa.streamFilesRecursive( sub ).forEach( handleRename( target ) ); assertFalse( fsa.isDirectory( subsub ) ); assertFalse( fsa.fileExists( subsub ) ); assertFalse( fsa.isDirectory( sub ) ); assertFalse( fsa.fileExists( sub ) ); }
@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 ); }
for ( FileHandle fileHandle : fileHandles ) Path sourcePath = fileHandle.getFile().toPath(); Path zipFsPath = fileSystem.isDirectory( sourceToCompress ) ? zipFs.getPath( rootPath.relativize( sourcePath ).toString() ) : zipFs.getPath( sourcePath.getFileName().toString() );
public void delete( File storeDir, LogFiles logFiles ) throws IOException { // 'files' can be null if the directory doesn't exist. This is fine, we just ignore it then. File[] files = fs.listFiles( storeDir, fileFilter ); if ( files != null ) { for ( File file : files ) { fs.deleteRecursively( file ); } } File[] txLogs = fs.listFiles( logFiles.logFilesDirectory() ); if ( txLogs != null ) { for ( File txLog : txLogs ) { fs.deleteFile( txLog ); } } Iterable<FileHandle> iterator = acceptedPageCachedFiles( storeDir )::iterator; for ( FileHandle fh : iterator ) { fh.delete(); } }