public static boolean copy ( FileSystem srcFS , Path src , FileSystem dstFS , Path dst , boolean deleteSource , boolean overwrite , boolean validate , Configuration conf , IOThrottler throttler ) throws IOException { dst = checkDest ( src . getName ( ) , dstFS , dst , overwrite ) ; FileStatus srcFileStatus = srcFS . getFileStatus ( src ) ; if ( srcFileStatus == null ) { throw new FileNotFoundException ( "File not found: " + src ) ; } if ( srcFileStatus . isDir ( ) ) { checkDependencies ( srcFS , src , dstFS , dst ) ; if ( ! dstFS . mkdirs ( dst ) ) { return false ; } FileStatus contents [ ] = srcFS . listStatus ( src ) ; for ( int i = 0 ; i < contents . length ; i ++ ) { copy ( srcFS , contents [ i ] . getPath ( ) , dstFS , new Path ( dst , contents [ i ] . getPath ( ) . getName ( ) ) , deleteSource , overwrite , validate , conf , throttler ) ; } } else { InputStream in = null ; OutputStream out = null ; try { in = srcFS . open ( src ) ; out = dstFS . create ( dst , overwrite ) ; IOUtils . copyBytes ( in , out , conf . getInt ( "io.file.buffer.size" , 4096 ) , true , throttler ) ; // validate file size if ( validate ) { InjectionHandler . processEventIO ( InjectionEventCore . FILE_TRUNCATION , dstFS , dst ) ; FileStatus dstFileStatus = dstFS . getFileStatus ( dst ) ; if ( dstFileStatus == null || dstFileStatus . getLen ( ) != srcFileStatus . getLen ( ) ) { throw new IOException ( "Mismatched file length: src=" + src + " dst=" + dst ) ; } } } catch ( IOException e ) { IOUtils . closeStream ( out ) ; IOUtils . closeStream ( in ) ; dstFS . delete ( dst , true ) ; throw e ; } } if ( deleteSource ) { return srcFS . delete ( src , true ) ; } else { return true ; } }
Copy files between file systems