public static boolean isValidSnapshot ( File f ) throws IOException { if ( f == null || Util . getZxidFromName ( f . getName ( ) , "snapshot" ) == - 1 ) return false ; // Check for a valid snapshot RandomAccessFile raf = new RandomAccessFile ( f , "r" ) ; // including the header and the last / bytes // the snapshot should be atleast 10 bytes if ( raf . length ( ) < 10 ) { return false ; } try { raf . seek ( raf . length ( ) - 5 ) ; byte bytes [ ] = new byte [ 5 ] ; int readlen = 0 ; int l ; while ( readlen < 5 && ( l = raf . read ( bytes , readlen , bytes . length - readlen ) ) >= 0 ) { readlen += l ; } if ( readlen != bytes . length ) { LOG . info ( "Invalid snapshot " + f + " too short, len = " + readlen ) ; return false ; } ByteBuffer bb = ByteBuffer . wrap ( bytes ) ; int len = bb . getInt ( ) ; byte b = bb . get ( ) ; if ( len != 1 || b != ' ' ) { LOG . info ( "Invalid snapshot " + f + " len = " + len + " byte = " + ( b & 0xff ) ) ; return false ; } } finally { raf . close ( ) ; } return true ; }
Verifies that the file is a valid snapshot . Snapshot may be invalid if it s incomplete as in a situation when the server dies while in the process of storing a snapshot . Any file that is not a snapshot is also an invalid snapshot .