lib/blockdevice.js
function( size, step, limit, callback ) {

    var args = [].slice.call( arguments )
    callback = args.pop()

    if( this.fd == null )
      return callback.call( this, new Error( 'Invalid file descriptor' ) )

    limit = args.pop() || 0x2000
    step  = args.pop() || 0x80
    size  = args.pop() || 0x200

    var self = this

    var readBlock = function() {

      var block = Buffer.allocUnsafe( size )

      self.fs.read( self.fd, block, 0, size, 0, function( error, bytesRead ) {

        if( error != null ) {
          // EINVAL tells us that the block size
          // ain't just right (yet); everything
          // else is probably user error
          if( error.code !== 'EINVAL' )
            return callback.call( self, error )
          // Increase the blocksize by `step`
          size += step
          // Attempt next size read
          return readBlock()
        }

        // Check if bytes read correspond
        // to current block size
        if( size !== bytesRead ) {
          error = new Error( 'Size and bytes read mismatch: ' + size + ', ' + bytesRead )
          return callback.call( self, error )
        }

        // Check if the limit has been reached,
        // and terminate with error, if so
        if( size > limit ) {
          error = new Error( 'Reached limit of ' + limit + ' bytes' )
          return callback.call( self, error )
        }

        // We've successfully found the
        // smallest readable block size;
        // stop looking...
        self.blockSize = size
        callback.call( self, null, size )

      })

    }

    // Start reading blocks
    readBlock()

    return this

  }
function( size, step, limit, callback ) {

    var args = [].slice.call( arguments )
    callback = args.pop()

    if( this.fd == null )
      return callback.call( this, new Error( 'Invalid file descriptor' ) )

    limit = args.pop() || 0x2000
    step  = args.pop() || 0x80
    size  = args.pop() || 0x200

    var self = this

    var readBlock = function() {

      var block = Buffer.allocUnsafe( size )

      self.fs.read( self.fd, block, 0, size, 0, function( error, bytesRead ) {

        if( error != null ) {
          // EINVAL tells us that the block size
          // ain't just right (yet); everything
          // else is probably user error
          if( error.code !== 'EINVAL' )
            return callback.call( self, error )
          // Increase the blocksize by `step`
          size += step
          // Attempt next size read
          return readBlock()
        }

        // Check if bytes read correspond
        // to current block size
        if( size !== bytesRead ) {
          error = new Error( 'Size and bytes read mismatch: ' + size + ', ' + bytesRead )
          return callback.call( self, error )
        }

        // Check if the limit has been reached,
        // and terminate with error, if so
        if( size > limit ) {
          error = new Error( 'Reached limit of ' + limit + ' bytes' )
          return callback.call( self, error )
        }

        // We've successfully found the
        // smallest readable block size;
        // stop looking...
        self.blockSize = size
        callback.call( self, null, size )

      })

    }

    // Start reading blocks
    readBlock()

    return this

  }
function ( size , step , limit , callback ) { var args = [ ] . slice . call ( arguments ) callback = args . pop ( ) if ( this . fd == null ) return callback . call ( this , new Error ( 'Invalid file descriptor' ) ) limit = args . pop ( ) || 0x2000 step = args . pop ( ) || 0x80 size = args . pop ( ) || 0x200 var self = this var readBlock = function ( ) { var block = Buffer . allocUnsafe ( size ) self . fs . read ( self . fd , block , 0 , size , 0 , function ( error , bytesRead ) { if ( error != null ) { // EINVAL tells us that the block size // ain't just right (yet); everything // else is probably user error if ( error . code !== 'EINVAL' ) return callback . call ( self , error ) // Increase the blocksize by `step` size += step // Attempt next size read return readBlock ( ) } // Check if bytes read correspond // to current block size if ( size !== bytesRead ) { error = new Error ( 'Size and bytes read mismatch: ' + size + ', ' + bytesRead ) return callback . call ( self , error ) } // Check if the limit has been reached, // and terminate with error, if so if ( size > limit ) { error = new Error ( 'Reached limit of ' + limit + ' bytes' ) return callback . call ( self , error ) } // We've successfully found the // smallest readable block size; // stop looking... self . blockSize = size callback . call ( self , null , size ) } ) } // Start reading blocks readBlock ( ) return this }
Primitive logical block size detection
@param  {Number} [size=128]
@param  {Number} [step=128]
@param  {Number} [limit=8192]
@param  {Function} callback( err, blockSize )
@return {BlockDevice}
Primitive logical block size detection