Exemplo n.º 1
0
// newStorage returns a new storage in path root with the given maxFileSize,
// or defaultMaxFileSize (512MB) if <= 0
func newStorage(root string, maxFileSize int64) (*storage, error) {
	fi, err := os.Stat(root)
	if os.IsNotExist(err) {
		return nil, fmt.Errorf("storage root %q doesn't exist", root)
	}
	if err != nil {
		return nil, fmt.Errorf("Failed to stat directory %q: %v", root, err)
	}
	if !fi.IsDir() {
		return nil, fmt.Errorf("storage root %q exists but is not a directory.", root)
	}
	index, _, err := kvfile.NewStorage(filepath.Join(root, "index.kv"))
	if maxFileSize <= 0 {
		maxFileSize = defaultMaxFileSize
	}
	s := &storage{root: root, index: index, maxFileSize: maxFileSize,
		Generationer: local.NewGenerationer(root),
	}
	if err := s.openCurrent(); err != nil {
		return nil, err
	}
	if _, _, err := s.StorageGeneration(); err != nil {
		return nil, fmt.Errorf("Error initialization generation for %q: %v", root, err)
	}
	return s, nil
}
Exemplo n.º 2
0
func (tester) test(t *testing.T, tfn func(*testing.T, func() *index.Index)) {
	var cleanup []func()
	defer func() {
		for _, fn := range cleanup {
			fn()
		}
	}()

	initIndex := func() *index.Index {
		td, err := ioutil.TempDir("", "kvfile-test")
		if err != nil {
			t.Fatal(err)
		}
		is, closer, err := kvfile.NewStorage(filepath.Join(td, "kvfile"))
		if err != nil {
			os.RemoveAll(td)
			t.Fatal(err)
		}
		cleanup = append(cleanup, func() {
			closer.Close()
			os.RemoveAll(td)
		})
		return index.New(is)
	}

	tfn(t, initIndex)
}
Exemplo n.º 3
0
func New(root string) (*storage, error) {
	fi, err := os.Stat(root)
	if os.IsNotExist(err) {
		return nil, fmt.Errorf("storage root %q doesn't exist", root)
	}
	if err != nil {
		return nil, fmt.Errorf("Failed to stat directory %q: %v", root, err)
	}
	if !fi.IsDir() {
		return nil, fmt.Errorf("storage root %q exists but is not a directory.", root)
	}
	index, _, err := kvfile.NewStorage(filepath.Join(root, "index.kv"))
	s := &storage{root: root, index: index, maxFileSize: defaultMaxFileSize}
	if err := s.openCurrent(); err != nil {
		return nil, err
	}
	return s, nil
}