/** * 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 ); } }; }
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() );
@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 ) ); }
@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 ) ); }
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 ); }
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 ); }
/** * 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; }
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() );