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

import (
4
	"fmt"
5
	"io"
6 7
	"os"
	"path/filepath"
8

9 10
	config "github.com/jbenet/go-ipfs/repo/config"
	util "github.com/jbenet/go-ipfs/util"
11
	"github.com/jbenet/go-ipfs/util/debugerror"
12 13
)

14
type FSRepo struct {
15
	state  state
16
	path   string
17
	config *config.Config
18 19 20 21
}

func At(path string) *FSRepo {
	return &FSRepo{
22 23
		path:  path,
		state: unopened, // explicitly set for clarity
24 25 26
	}
}

27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
func Init(path string, conf *config.Config) error {
	if IsInitialized(path) {
		return nil
	}
	configFilename, err := config.Filename(path)
	if err != nil {
		return err
	}
	if err := writeConfigFile(configFilename, conf); err != nil {
		return err
	}
	return nil
}

// Open returns an error if the repo is not initialized.
42
func (r *FSRepo) Open() error {
43 44 45 46 47 48
	if r.state != unopened {
		return debugerror.Errorf("repo is %s", r.state)
	}
	if !IsInitialized(r.path) {
		return debugerror.New("repo is not initialized")
	}
49
	// check repo path, then check all constituent parts.
50
	// TODO acquire repo lock
51 52 53 54 55
	// TODO if err := initCheckDir(logpath); err != nil { // }
	if err := initCheckDir(r.path); err != nil {
		return err
	}

56 57 58 59 60 61 62 63 64 65
	configFilename, err := config.Filename(r.path)
	if err != nil {
		return err
	}
	conf, err := Load(configFilename)
	if err != nil {
		return err
	}
	r.config = conf

66 67 68 69 70 71 72 73 74
	// datastore
	dspath, err := config.DataStorePath("")
	if err != nil {
		return err
	}
	if err := initCheckDir(dspath); err != nil {
		return debugerror.Errorf("datastore: %s", err)
	}

75 76 77 78 79 80 81 82
	logpath, err := config.LogsPath("")
	if err != nil {
		return debugerror.Wrap(err)
	}
	if err := initCheckDir(logpath); err != nil {
		return debugerror.Errorf("logs: %s", err)
	}

83
	r.state = opened
84 85 86
	return nil
}

87 88 89 90 91 92 93
func (r *FSRepo) Config() *config.Config {
	if r.state != opened {
		panic(fmt.Sprintln("repo is", r.state))
	}
	return r.config
}

94
func (r *FSRepo) SetConfig(conf *config.Config) error {
95 96 97
	if r.state != opened {
		panic(fmt.Sprintln("repo is", r.state))
	}
98 99 100 101
	configFilename, err := config.Filename(r.path)
	if err != nil {
		return err
	}
102
	if err := writeConfigFile(configFilename, conf); err != nil {
103 104
		return err
	}
105
	*r.config = *conf // copy so caller cannot modify the private config
106 107 108 109
	return nil
}

func (r *FSRepo) Close() error {
110 111 112
	if r.state != opened {
		return debugerror.Errorf("repo is %s", r.state)
	}
113 114 115 116 117
	return nil // TODO release repo lock
}

var _ io.Closer = &FSRepo{}

118 119
// IsInitialized returns true if the repo is initialized at provided |path|.
func IsInitialized(path string) bool {
120 121 122 123 124 125 126 127 128
	configFilename, err := config.Filename(path)
	if err != nil {
		return false
	}
	if !util.FileExists(configFilename) {
		return false
	}
	return true
}
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143

// initCheckDir ensures the directory exists and is writable
func initCheckDir(path string) error {
	// Construct the path if missing
	if err := os.MkdirAll(path, os.ModePerm); err != nil {
		return err
	}
	// Check the directory is writeable
	if f, err := os.Create(filepath.Join(path, "._check_writeable")); err == nil {
		os.Remove(f.Name())
	} else {
		return debugerror.New("'" + path + "' is not writeable")
	}
	return nil
}