fsrepo.go 1.04 KB
Newer Older
1 2 3
package fsrepo

import (
4 5
	"io"

6 7 8 9
	config "github.com/jbenet/go-ipfs/repo/config"
	util "github.com/jbenet/go-ipfs/util"
)

10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
type FSRepo struct {
	path   string
	config config.Config
}

func At(path string) *FSRepo {
	return &FSRepo{
		path: path,
	}
}

func (r *FSRepo) Open() error {
	// TODO may need to check that directory is writeable
	// TODO acquire repo lock
	return nil
}

func (r *FSRepo) SetConfig(conf *config.Config) error {
	configFilename, err := config.Filename(r.path)
	if err != nil {
		return err
	}
	if err := config.WriteConfigFile(configFilename, conf); err != nil {
		return err
	}
	r.config = *conf // copy so caller cannot modify the private config
	return nil
}

func (r *FSRepo) Close() error {
	return nil // TODO release repo lock
}

var _ io.Closer = &FSRepo{}

45 46 47 48 49 50 51 52 53 54 55
// ConfigIsInitialized returns true if the config exists in provided |path|.
func ConfigIsInitialized(path string) bool {
	configFilename, err := config.Filename(path)
	if err != nil {
		return false
	}
	if !util.FileExists(configFilename) {
		return false
	}
	return true
}