Exemple #1
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
}
Exemple #2
0
func WriteVersionedBlob(bs blobstore.BlobStore, blobpath string, version byte) error {
	w, err := bs.OpenWriter(blobpath)
	if err != nil {
		return fmt.Errorf("Failed to open writer: %v", err)
	}

	if _, err := w.Write([]byte{version}); err != nil {
		return fmt.Errorf("Failed to blob write: %v", err)
	}

	if err := w.Close(); err != nil {
		return fmt.Errorf("Failed to close writer: %v", err)
	}

	return nil
}
Exemple #3
0
func Save(blobpath string, c btncrypt.Cipher, bs blobstore.BlobStore, cb EncodeCallback) error {
	var buf bytes.Buffer
	zw := zlib.NewWriter(&buf)
	enc := gob.NewEncoder(zw)

	es := []error{}
	if err := cb(enc); err != nil {
		es = append(es, fmt.Errorf("Failed to encode state: %v", err))
	}
	if err := zw.Close(); err != nil {
		es = append(es, fmt.Errorf("Failed to close zlib Writer: %v", err))
	}

	if err := util.ToErrors(es); err != nil {
		return err
	}

	w, err := bs.OpenWriter(blobpath)
	if err != nil {
		return err
	}
	cw, err := chunkstore.NewChunkWriter(w, c, chunkstore.ChunkHeader{
		PayloadLen:     uint32(buf.Len()),
		PayloadVersion: 1,
		OrigFilename:   blobpath,
		OrigOffset:     0,
	})
	if err != nil {
		return err
	}

	if _, err := cw.Write(buf.Bytes()); err != nil {
		es = append(es, fmt.Errorf("Failed to write to ChunkWriter: %v", err))
	}

	if err := cw.Close(); err != nil {
		es = append(es, fmt.Errorf("Failed to close ChunkWriter: %v", err))
	}
	if err := w.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
}
Exemple #4
0
func AssertBlobVersion(bs blobstore.BlobStore, blobpath string, expected version.Version) error {
	r, err := bs.OpenReader(blobpath)
	if err != nil {
		if expected == 0 && os.IsNotExist(err) {
			return nil
		}
		return fmt.Errorf("Failed to open reader: %v", err)
	}
	actual, err := TestQueryVersion(r)
	if err != nil {
		return fmt.Errorf("Failed to query version: %v", err)
	}
	if err := r.Close(); err != nil {
		return fmt.Errorf("Failed to close reader: %v", err)
	}

	if actual != expected {
		return fmt.Errorf("Expected version %d, but got %d", expected, actual)
	}

	return nil
}