Exemplo n.º 1
0
func Test_ChunkIOWrite_OverflowUpdate(t *testing.T) {
	testbh := &TestBlobHandle{}
	cio := chunkstore.NewChunkIO(testbh, TestCipher())
	if err := cio.PWrite(0, HelloWorld); err != nil {
		t.Errorf("failed to PWrite to ChunkIO: %v", err)
		return
	}
	if err := cio.PWrite(7, HogeFugaPiyo); err != nil {
		t.Errorf("failed to PWrite to ChunkIO: %v", err)
		return
	}
	if err := cio.Close(); err != nil {
		t.Errorf("failed to Close ChunkIO: %v", err)
		return
	}

	cr, err := chunkstore.NewChunkReader(bytes.NewBuffer(testbh.Buf), TestCipher())
	if err != nil {
		t.Errorf("failed to create chunk reader: %v", err)
		return
	}
	exp := []byte("Hello, hogefugapiyo")
	if cr.Length() != len(exp) {
		t.Errorf("failed to recover payload len")
	}
	readtgt := make([]byte, len(exp))
	if _, err := io.ReadFull(cr, readtgt); err != nil {
		t.Errorf("failed to Read from ChunkReader: %v", err)
		return
	}
	if !bytes.Equal(readtgt, exp) {
		t.Errorf("Read content invalid")
		return
	}
}
Exemplo n.º 2
0
func Restore(blobpath string, c btncrypt.Cipher, bs blobstore.BlobStore, cb DecodeCallback) error {
	r, err := bs.OpenReader(blobpath)
	if err != nil {
		return err
	}

	cr, err := chunkstore.NewChunkReader(r, c)
	if err != nil {
		return err
	}
	logger.Debugf(mylog, "serialized blob size: %d", cr.Length())
	zr, err := zlib.NewReader(&io.LimitedReader{cr, int64(cr.Length())})
	if err != nil {
		return err
	}
	logger.Debugf(mylog, "statesnapshot.Restore: zlib init success!")
	dec := gob.NewDecoder(zr)

	es := []error{}
	if err := cb(dec); err != nil {
		es = append(es, fmt.Errorf("Failed to decode state: %v", err))
	}
	if err := zr.Close(); err != nil {
		es = append(es, fmt.Errorf("Failed to close zlib Reader: %v", err))
	}
	if err := r.Close(); err != nil {
		es = append(es, fmt.Errorf("Failed to close BlobHandle: %v", err))
	}

	if err := util.ToErrors(es); err != nil {
		return err
	}
	return nil
}
Exemplo n.º 3
0
func Test_ChunkIOWrite_NewHello_ChunkReaderRead(t *testing.T) {
	testbh := &TestBlobHandle{}
	cio := chunkstore.NewChunkIO(testbh, TestCipher())
	if err := cio.PWrite(0, HelloWorld); err != nil {
		t.Errorf("failed to PWrite to ChunkIO: %v", err)
		return
	}
	readtgt := make([]byte, len(HelloWorld))
	if err := cio.PRead(0, readtgt); err != nil {
		t.Errorf("failed to PRead from ChunkIO: %v", err)
		return
	}
	if !bytes.Equal(readtgt, HelloWorld) {
		t.Errorf("Read content invalid")
		return
	}
	if err := cio.Close(); err != nil {
		t.Errorf("failed to Close ChunkIO: %v", err)
		return
	}

	cr, err := chunkstore.NewChunkReader(bytes.NewBuffer(testbh.Buf), TestCipher())
	if err != nil {
		t.Errorf("failed to create chunk reader: %v", err)
		return
	}
	if cr.Length() != len(HelloWorld) {
		t.Errorf("failed to recover payload len")
	}
	readtgt2 := make([]byte, len(HelloWorld))
	if _, err := io.ReadFull(cr, readtgt2); err != nil {
		t.Errorf("failed to Read from ChunkReader: %v", err)
		return
	}
	if !bytes.Equal(readtgt2, HelloWorld) {
		t.Errorf("Read content invalid")
		return
	}
}
Exemplo n.º 4
0
func main() {
	log.SetFlags(log.Ldate | log.Ltime | log.Lshortfile)

	flag.Usage = Usage
	flag.Parse()

	if flag.NArg() != 1 {
		Usage()
		os.Exit(2)
	}
	filepath := flag.Arg(0)

	f, err := os.Open(filepath)
	if err != nil {
		log.Printf("Failed to read file: %s", filepath)
	}
	defer f.Close()

	password := util.StringFromFileOrDie(*flagPasswordFile, "password")
	key := btncrypt.KeyFromPassword(password)
	c, err := btncrypt.NewCipher(key)
	if err != nil {
		log.Printf("Failed to init Cipher: %v", err)
		return
	}

	cr, err := chunkstore.NewChunkReader(f, c)
	if err != nil {
		log.Printf("Failed to init ChunkReader: %v", err)
		return
	}

	if *flagHeader {
		log.Printf("Header: %+v", cr.Header())
	}
	io.Copy(os.Stdout, cr)
}