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

import (
4
	"errors"
5
	"fmt"
6
	"io"
7
	"os"
8
	"path"
9
	"sync"
10

11
	ds "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore"
12
	repo "github.com/jbenet/go-ipfs/repo"
13
	config "github.com/jbenet/go-ipfs/repo/config"
14
	component "github.com/jbenet/go-ipfs/repo/fsrepo/component"
15
	counter "github.com/jbenet/go-ipfs/repo/fsrepo/counter"
16
	dir "github.com/jbenet/go-ipfs/repo/fsrepo/dir"
17
	lockfile "github.com/jbenet/go-ipfs/repo/fsrepo/lock"
18
	serialize "github.com/jbenet/go-ipfs/repo/fsrepo/serialize"
19
	debugerror "github.com/jbenet/go-ipfs/util/debugerror"
20 21
)

22
var (
23 24 25

	// packageLock must be held to while performing any operation that modifies an
	// FSRepo's state field. This includes Init, Open, Close, and Remove.
26
	packageLock sync.Mutex // protects openersCounter and lockfiles
27 28 29
	// lockfiles holds references to the Closers that ensure that repos are
	// only accessed by one process at a time.
	lockfiles map[string]io.Closer
30
	// openersCounter prevents the fsrepo from being removed while there exist open
31 32 33 34 35 36
	// FSRepo handles. It also ensures that the Init is atomic.
	//
	// packageLock also protects numOpenedRepos
	//
	// If an operation is used when repo is Open and the operation does not
	// change the repo's state, the package lock does not need to be acquired.
37
	openersCounter *counter.Openers
38 39 40
)

func init() {
41
	openersCounter = counter.NewOpenersCounter()
42
	lockfiles = make(map[string]io.Closer)
43 44
}

45 46
// FSRepo represents an IPFS FileSystem Repo. It is safe for use by multiple
// callers.
47
type FSRepo struct {
Brian Tiger Chow's avatar
Brian Tiger Chow committed
48 49 50 51
	// state is the FSRepo's state (unopened, opened, closed)
	state state
	// path is the file-system path
	path string
52 53
	// configComponent is loaded when FSRepo is opened and kept up to date when
	// the FSRepo is modified.
Brian Tiger Chow's avatar
Brian Tiger Chow committed
54
	// TODO test
55
	configComponent    component.ConfigComponent
56
	datastoreComponent component.DatastoreComponent
57 58
}

59 60 61 62
type componentBuilder struct {
	Init          component.Initializer
	IsInitialized component.InitializationChecker
	OpenHandler   func(*FSRepo) error
63 64
}

Brian Tiger Chow's avatar
Brian Tiger Chow committed
65
// At returns a handle to an FSRepo at the provided |path|.
66
func At(repoPath string) *FSRepo {
67
	// This method must not have side-effects.
68
	return &FSRepo{
69 70
		path:  path.Clean(repoPath),
		state: unopened, // explicitly set for clarity
71 72 73
	}
}

Brian Tiger Chow's avatar
huh  
Brian Tiger Chow committed
74
func ConfigAt(repoPath string) (*config.Config, error) {
75 76 77 78 79

	// packageLock must be held to ensure that the Read is atomic.
	packageLock.Lock()
	defer packageLock.Unlock()

Brian Tiger Chow's avatar
huh  
Brian Tiger Chow committed
80 81 82 83
	configFilename, err := config.Filename(repoPath)
	if err != nil {
		return nil, err
	}
84
	return serialize.Load(configFilename)
Brian Tiger Chow's avatar
huh  
Brian Tiger Chow committed
85 86
}

Brian Tiger Chow's avatar
Brian Tiger Chow committed
87
// Init initializes a new FSRepo at the given path with the provided config.
88
// TODO add support for custom datastores.
89
func Init(path string, conf *config.Config) error {
90 91 92 93

	// packageLock must be held to ensure that the repo is not initialized more
	// than once.
	packageLock.Lock()
94
	defer packageLock.Unlock()
95 96

	if isInitializedUnsynced(path) {
97 98
		return nil
	}
99 100 101 102
	for _, b := range componentBuilders() {
		if err := b.Init(path, conf); err != nil {
			return err
		}
103 104 105 106
	}
	return nil
}

107
// Remove recursively removes the FSRepo at |path|.
108 109 110 111 112
func Remove(repoPath string) error {
	repoPath = path.Clean(repoPath)

	// packageLock must be held to ensure that the repo is not removed while
	// being accessed by others.
113 114
	packageLock.Lock()
	defer packageLock.Unlock()
115

116
	if openersCounter.NumOpeners(repoPath) != 0 {
117 118
		return errors.New("repo in use")
	}
119
	return os.RemoveAll(repoPath)
120 121
}

122 123 124
// LockedByOtherProcess returns true if the FSRepo is locked by another
// process. If true, then the repo cannot be opened by this process.
func LockedByOtherProcess(repoPath string) bool {
125 126 127
	repoPath = path.Clean(repoPath)

	// packageLock must be held to check the number of openers.
128 129
	packageLock.Lock()
	defer packageLock.Unlock()
130

131
	// NB: the lock is only held when repos are Open
132
	return lockfile.Locked(repoPath) && openersCounter.NumOpeners(repoPath) == 0
133 134
}

135
// Open returns an error if the repo is not initialized.
136
func (r *FSRepo) Open() error {
137 138 139 140

	// packageLock must be held to make sure that the repo is not destroyed by
	// another caller. It must not be released until initialization is complete
	// and the number of openers is incremeneted.
141 142
	packageLock.Lock()
	defer packageLock.Unlock()
143

144 145 146
	if r.state != unopened {
		return debugerror.Errorf("repo is %s", r.state)
	}
147
	if !isInitializedUnsynced(r.path) {
148
		return debugerror.New("ipfs not initialized, please run 'ipfs init'")
149
	}
150
	// check repo path, then check all constituent parts.
151
	// TODO acquire repo lock
152
	// TODO if err := initCheckDir(logpath); err != nil { // }
153
	if err := dir.Writable(r.path); err != nil {
154 155 156
		return err
	}

157 158
	for _, b := range componentBuilders() {
		if err := b.OpenHandler(r); err != nil {
159 160
			return err
		}
161 162
	}

163 164 165 166
	logpath, err := config.LogsPath("")
	if err != nil {
		return debugerror.Wrap(err)
	}
167
	if err := dir.Writable(logpath); err != nil {
168 169 170
		return debugerror.Errorf("logs: %s", err)
	}

171
	return r.transitionToOpened()
172 173
}

174 175 176 177 178 179 180 181
// Close closes the FSRepo, releasing held resources.
func (r *FSRepo) Close() error {
	packageLock.Lock()
	defer packageLock.Unlock()

	if r.state != opened {
		return debugerror.Errorf("repo is %s", r.state)
	}
182 183 184 185 186 187

	for _, closer := range r.components() {
		if err := closer.Close(); err != nil {
			return err
		}
	}
188
	return r.transitionToClosed()
189 190
}

191 192 193 194
// Config returns the FSRepo's config. This method must not be called if the
// repo is not open.
//
// Result when not Open is undefined. The method may panic if it pleases.
195
func (r *FSRepo) Config() *config.Config {
196 197 198 199 200 201 202 203 204

	// It is not necessary to hold the package lock since the repo is in an
	// opened state. The package lock is _not_ meant to ensure that the repo is
	// thread-safe. The package lock is only meant to guard againt removal and
	// coordinate the lockfile. However, we provide thread-safety to keep
	// things simple.
	packageLock.Lock()
	defer packageLock.Unlock()

205 206 207
	if r.state != opened {
		panic(fmt.Sprintln("repo is", r.state))
	}
208
	return r.configComponent.Config()
209 210
}

Brian Tiger Chow's avatar
Brian Tiger Chow committed
211
// SetConfig updates the FSRepo's config.
212
func (r *FSRepo) SetConfig(updated *config.Config) error {
213 214 215 216 217

	// packageLock is held to provide thread-safety.
	packageLock.Lock()
	defer packageLock.Unlock()

218
	return r.configComponent.SetConfig(updated)
219 220
}

Brian Tiger Chow's avatar
Brian Tiger Chow committed
221
// GetConfigKey retrieves only the value of a particular key.
222
func (r *FSRepo) GetConfigKey(key string) (interface{}, error) {
223 224 225
	packageLock.Lock()
	defer packageLock.Unlock()

226 227 228
	if r.state != opened {
		return nil, debugerror.Errorf("repo is %s", r.state)
	}
229
	return r.configComponent.GetConfigKey(key)
230 231
}

Brian Tiger Chow's avatar
Brian Tiger Chow committed
232
// SetConfigKey writes the value of a particular key.
233
func (r *FSRepo) SetConfigKey(key string, value interface{}) error {
234 235 236
	packageLock.Lock()
	defer packageLock.Unlock()

237 238 239
	if r.state != opened {
		return debugerror.Errorf("repo is %s", r.state)
	}
240
	return r.configComponent.SetConfigKey(key, value)
241 242
}

243 244 245 246 247 248 249 250 251
// Datastore returns a repo-owned datastore. If FSRepo is Closed, return value
// is undefined.
func (r *FSRepo) Datastore() ds.ThreadSafeDatastore {
	packageLock.Lock()
	d := r.datastoreComponent.Datastore()
	packageLock.Unlock()
	return d
}

252
var _ io.Closer = &FSRepo{}
253
var _ repo.Repo = &FSRepo{}
254

255 256
// IsInitialized returns true if the repo is initialized at provided |path|.
func IsInitialized(path string) bool {
257 258
	// packageLock is held to ensure that another caller doesn't attempt to
	// Init or Remove the repo while this call is in progress.
259 260
	packageLock.Lock()
	defer packageLock.Unlock()
261

262
	return isInitializedUnsynced(path)
263 264
}

265 266
// private methods below this point. NB: packageLock must held by caller.

267
// isInitializedUnsynced reports whether the repo is initialized. Caller must
268
// hold the packageLock.
269
func isInitializedUnsynced(path string) bool {
270 271 272 273 274 275
	for _, b := range componentBuilders() {
		if !b.IsInitialized(path) {
			return false
		}
	}
	return true
276
}
277

278
// transitionToOpened manages the state transition to |opened|. Caller must hold
279
// the package mutex.
280
func (r *FSRepo) transitionToOpened() error {
281
	r.state = opened
282
	if countBefore := openersCounter.NumOpeners(r.path); countBefore == 0 { // #first
283 284 285 286 287 288
		closer, err := lockfile.Lock(r.path)
		if err != nil {
			return err
		}
		lockfiles[r.path] = closer
	}
289
	return openersCounter.AddOpener(r.path)
290 291 292
}

// transitionToClosed manages the state transition to |closed|. Caller must
293
// hold the package mutex.
294
func (r *FSRepo) transitionToClosed() error {
295
	r.state = closed
296
	if err := openersCounter.RemoveOpener(r.path); err != nil {
297 298
		return err
	}
299
	if countAfter := openersCounter.NumOpeners(r.path); countAfter == 0 {
300 301 302 303 304 305 306
		closer, ok := lockfiles[r.path]
		if !ok {
			return errors.New("package error: lockfile is not held")
		}
		if err := closer.Close(); err != nil {
			return err
		}
307
		delete(lockfiles, r.path)
308 309 310
	}
	return nil
}
311

312
// components returns the FSRepo's constituent components
313 314
func (r *FSRepo) components() []component.Component {
	return []component.Component{
315
		&r.configComponent,
316
		&r.datastoreComponent,
317 318
	}
}
319 320 321 322 323 324 325 326 327

func componentBuilders() []componentBuilder {
	return []componentBuilder{

		// ConfigComponent
		componentBuilder{
			Init:          component.InitConfigComponent,
			IsInitialized: component.ConfigComponentIsInitialized,
			OpenHandler: func(r *FSRepo) error {
328 329 330
				c := component.ConfigComponent{}
				c.SetPath(r.path)
				if err := c.Open(); err != nil {
331 332
					return err
				}
333
				r.configComponent = c
334 335 336 337
				return nil
			},
		},

338 339 340 341 342 343 344 345 346 347 348 349 350 351
		// DatastoreComponent
		componentBuilder{
			Init:          component.InitDatastoreComponent,
			IsInitialized: component.DatastoreComponentIsInitialized,
			OpenHandler: func(r *FSRepo) error {
				c := component.DatastoreComponent{}
				c.SetPath(r.path)
				if err := c.Open(); err != nil {
					return err
				}
				r.datastoreComponent = c
				return nil
			},
		},
352 353
	}
}