Exemple #1
0
func (app *App) openStorage(backend string) (kv.KV, error) {
	switch backend {
	case "local":
		kvpath := filepath.Join(app.DataDir, "chunks")
		return kvfiles.Open(kvpath)
	}
	if backend != "" && backend[0] == '/' {
		return kvfiles.Open(backend)
	}
	idx := strings.IndexByte(backend, ':')
	if idx != -1 {
		scheme, rest := backend[:idx], backend[idx+1:]
		switch scheme {
		case "peerkey":
			var key peer.PublicKey
			if err := key.Set(rest); err != nil {
				return nil, err
			}
			p, err := app.DialPeer(&key)
			if err != nil {
				return nil, err
			}
			// TODO Close
			return kvpeer.Open(p)
		}
	}
	return nil, errors.New("unknown storage backend")
}
Exemple #2
0
func (c *casCommand) Setup() (ok bool) {
	path := filepath.Join(cli.Bazil.Config.DataDir.String(), c.Config.Path)
	kvstore, err := kvfiles.Open(path)
	if err != nil {
		log.Printf("cannot open CAS: %v", err)
		return false
	}

	c.State.Store = kvchunks.New(kvstore)
	return true
}
Exemple #3
0
func (app *App) openKV(conf *wire.KV) (kv.KV, error) {
	var kvstores []kv.KV

	if conf.Local != nil {
		kvpath := filepath.Join(app.DataDir, "chunks")
		var s kv.KV
		var err error
		s, err = kvfiles.Open(kvpath)
		if err != nil {
			return nil, err
		}
		if conf.Local.Secret != nil {
			var secret [32]byte
			copy(secret[:], conf.Local.Secret)
			s = untrusted.New(s, &secret)
		}
		kvstores = append(kvstores, s)
	}

	for _, ext := range conf.External {
		var s kv.KV
		var err error
		s, err = kvfiles.Open(ext.Path)
		if err != nil {
			return nil, err
		}
		if ext.Secret != nil {
			var secret [32]byte
			copy(secret[:], ext.Secret)
			s = untrusted.New(s, &secret)
		}
		kvstores = append(kvstores, s)
	}

	if len(conf.XXX_unrecognized) > 0 {
		return nil, fmt.Errorf("unknown storage: %v", conf)
	}

	return kvmulti.New(kvstores...), nil
}
Exemple #4
0
func TestAdd(t *testing.T) {
	temp := tempdir.New(t)
	defer temp.Cleanup()

	k, err := kvfiles.Open(temp.Path)
	if err != nil {
		t.Fatalf("kvfiles.Open fail: %v\n", err)
	}

	err = k.Put([]byte("quux"), []byte("foobar"))
	if err != nil {
		t.Fatalf("c.Put fail: %v\n", err)
	}
}
Exemple #5
0
func TestGet(t *testing.T) {
	temp := tempdir.New(t)
	defer temp.Cleanup()

	c, err := kvfiles.Open(temp.Path)
	if err != nil {
		t.Fatalf("kvfiles.Open fail: %v\n", err)
	}

	err = c.Put([]byte("quux"), []byte("foobar"))
	if err != nil {
		t.Fatalf("c.Put fail: %v\n", err)
	}

	data, err := c.Get([]byte("quux"))
	if err != nil {
		t.Fatalf("c.Get failed: %v", err)
	}
	if g, e := string(data), "foobar"; g != e {
		t.Fatalf("c.Get gave wrong content: %q != %q", g, e)
	}
}
Exemple #6
0
func (c *casCommand) Setup() (ok bool) {
	path := filepath.Join(cli.Bazil.Config.DataDir.String(), c.Config.Path)
	var kvstore kv.KV
	var err error
	kvstore, err = kvfiles.Open(path)
	if err != nil {
		log.Printf("cannot open CAS: %v", err)
		return false
	}

	if c.Config.Sharing != "" {
		var secret [32]byte
		if err := c.getSharingKey(c.Config.Sharing, &secret); err != nil {
			log.Printf("cannot get sharing key: %q: %v", c.Config.Sharing, err)
			return false
		}
		kvstore = untrusted.New(kvstore, &secret)
	}

	c.State.Store = kvchunks.New(kvstore)
	return true
}
Exemple #7
0
func TestGetNotFoundError(t *testing.T) {
	temp := tempdir.New(t)
	defer temp.Cleanup()

	c, err := kvfiles.Open(temp.Path)
	if err != nil {
		t.Fatalf("kvfiles.Open fail: %v\n", err)
	}

	const KEY = "\x8d\xf3\x1f\x60\xd6\xae\xab\xd0\x1b\x7d\xc8\x3f\x27\x7d\x0e\x24\xcb\xe1\x04\xf7\x29\x0f\xf8\x90\x77\xa7\xeb\x58\x64\x60\x68\xed\xfe\x1a\x83\x02\x28\x66\xc4\x6f\x65\xfb\x91\x61\x2e\x51\x6e\x0e\xcf\xa5\xcb\x25\xfc\x16\xb3\x7d\x2c\x8d\x73\x73\x2f\xe7\x4c\xb2"
	_, err = c.Get([]byte(KEY))
	if err == nil {
		t.Fatalf("c.Get should have failed")
	}
	nf, ok := err.(kv.NotFoundError)
	if !ok {
		t.Fatalf("c.Get error is of wrong type: %T: %v", err, err)
	}

	if g, w := string(nf.Key), KEY; g != w {
		t.Errorf("NotFoundError Key is wrong: %x != %x", g, w)
	}
}