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

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

12
	ds "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore"
13
	repo "github.com/jbenet/go-ipfs/repo"
14
	"github.com/jbenet/go-ipfs/repo/common"
15
	config "github.com/jbenet/go-ipfs/repo/config"
16
	component "github.com/jbenet/go-ipfs/repo/fsrepo/component"
17
	counter "github.com/jbenet/go-ipfs/repo/fsrepo/counter"
18
	lockfile "github.com/jbenet/go-ipfs/repo/fsrepo/lock"
19
	serialize "github.com/jbenet/go-ipfs/repo/fsrepo/serialize"
Brian Tiger Chow's avatar
Brian Tiger Chow committed
20
	dir "github.com/jbenet/go-ipfs/thirdparty/dir"
Jeromy's avatar
Jeromy committed
21
	u "github.com/jbenet/go-ipfs/util"
22
	util "github.com/jbenet/go-ipfs/util"
23
	debugerror "github.com/jbenet/go-ipfs/util/debugerror"
24 25
)

26
var (
27 28 29

	// packageLock must be held to while performing any operation that modifies an
	// FSRepo's state field. This includes Init, Open, Close, and Remove.
30
	packageLock sync.Mutex // protects openersCounter and lockfiles
31 32 33
	// lockfiles holds references to the Closers that ensure that repos are
	// only accessed by one process at a time.
	lockfiles map[string]io.Closer
34
	// openersCounter prevents the fsrepo from being removed while there exist open
35 36 37 38 39 40
	// 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.
41
	openersCounter *counter.Openers
42 43 44
)

func init() {
45
	openersCounter = counter.NewOpenersCounter()
46
	lockfiles = make(map[string]io.Closer)
47 48
}

49 50
// FSRepo represents an IPFS FileSystem Repo. It is safe for use by multiple
// callers.
51
type FSRepo struct {
Brian Tiger Chow's avatar
Brian Tiger Chow committed
52 53 54 55
	// state is the FSRepo's state (unopened, opened, closed)
	state state
	// path is the file-system path
	path string
56 57 58
	// config is set on Open, guarded by packageLock
	config *config.Config

Brian Tiger Chow's avatar
Brian Tiger Chow committed
59
	// TODO test
60
	datastoreComponent component.DatastoreComponent
61
	eventlogComponent  component.EventlogComponent
62 63
}

Tommi Virtanen's avatar
Tommi Virtanen committed
64 65
var _ repo.Repo = (*FSRepo)(nil)

66 67 68 69
type componentBuilder struct {
	Init          component.Initializer
	IsInitialized component.InitializationChecker
	OpenHandler   func(*FSRepo) error
70 71
}

Brian Tiger Chow's avatar
Brian Tiger Chow committed
72
// At returns a handle to an FSRepo at the provided |path|.
73
func At(repoPath string) *FSRepo {
74
	// This method must not have side-effects.
75
	return &FSRepo{
76 77
		path:  path.Clean(repoPath),
		state: unopened, // explicitly set for clarity
78 79 80
	}
}

81 82 83
// ConfigAt returns an error if the FSRepo at the given path is not
// initialized. This function allows callers to read the config file even when
// another process is running and holding the lock.
Brian Tiger Chow's avatar
huh  
Brian Tiger Chow committed
84
func ConfigAt(repoPath string) (*config.Config, error) {
85 86 87 88 89

	// 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
90 91 92 93
	configFilename, err := config.Filename(repoPath)
	if err != nil {
		return nil, err
	}
94
	return serialize.Load(configFilename)
Brian Tiger Chow's avatar
huh  
Brian Tiger Chow committed
95 96
}

97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
// configIsInitialized returns true if the repo is initialized at
// 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
}

func initConfig(path string, conf *config.Config) error {
	if configIsInitialized(path) {
		return nil
	}
	configFilename, err := config.Filename(path)
	if err != nil {
		return err
	}
	// initialization is the one time when it's okay to write to the config
	// without reading the config from disk and merging any user-provided keys
	// that may exist.
	if err := serialize.WriteConfigFile(configFilename, conf); err != nil {
		return err
	}
	return nil
}

Brian Tiger Chow's avatar
Brian Tiger Chow committed
127
// Init initializes a new FSRepo at the given path with the provided config.
128
// TODO add support for custom datastores.
129
func Init(path string, conf *config.Config) error {
130 131 132 133

	// packageLock must be held to ensure that the repo is not initialized more
	// than once.
	packageLock.Lock()
134
	defer packageLock.Unlock()
135 136

	if isInitializedUnsynced(path) {
137 138
		return nil
	}
139 140 141 142 143

	if err := initConfig(path, conf); err != nil {
		return err
	}

144 145 146 147
	for _, b := range componentBuilders() {
		if err := b.Init(path, conf); err != nil {
			return err
		}
148 149 150 151
	}
	return nil
}

152
// Remove recursively removes the FSRepo at |path|.
153 154 155 156 157
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.
158 159
	packageLock.Lock()
	defer packageLock.Unlock()
160

161
	if openersCounter.NumOpeners(repoPath) != 0 {
162 163
		return errors.New("repo in use")
	}
164
	return os.RemoveAll(repoPath)
165 166
}

167 168 169
// 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 {
170 171 172
	repoPath = path.Clean(repoPath)

	// packageLock must be held to check the number of openers.
173 174
	packageLock.Lock()
	defer packageLock.Unlock()
175

176
	// NB: the lock is only held when repos are Open
177
	return lockfile.Locked(repoPath) && openersCounter.NumOpeners(repoPath) == 0
178 179
}

180 181 182 183 184 185 186 187 188 189 190 191 192 193
// openConfig returns an error if the config file is not present.
func (r *FSRepo) openConfig() error {
	configFilename, err := config.Filename(r.path)
	if err != nil {
		return err
	}
	conf, err := serialize.Load(configFilename)
	if err != nil {
		return err
	}
	r.config = conf
	return nil
}

194
// Open returns an error if the repo is not initialized.
195
func (r *FSRepo) Open() error {
196 197 198 199

	// 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.
200 201
	packageLock.Lock()
	defer packageLock.Unlock()
202

Jeromy's avatar
Jeromy committed
203 204 205 206 207 208
	expPath, err := u.TildeExpansion(r.path)
	if err != nil {
		return err
	}
	r.path = expPath

209 210 211
	if r.state != unopened {
		return debugerror.Errorf("repo is %s", r.state)
	}
212
	if !isInitializedUnsynced(r.path) {
213
		return debugerror.New("ipfs not initialized, please run 'ipfs init'")
214
	}
215
	// check repo path, then check all constituent parts.
216
	// TODO acquire repo lock
217
	// TODO if err := initCheckDir(logpath); err != nil { // }
218
	if err := dir.Writable(r.path); err != nil {
219 220 221
		return err
	}

222 223 224 225
	if err := r.openConfig(); err != nil {
		return err
	}

226 227
	for _, b := range componentBuilders() {
		if err := b.OpenHandler(r); err != nil {
228 229
			return err
		}
230 231
	}

232
	return r.transitionToOpened()
233 234
}

235 236 237 238 239 240 241 242
// 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)
	}
243 244 245 246 247 248

	for _, closer := range r.components() {
		if err := closer.Close(); err != nil {
			return err
		}
	}
249
	return r.transitionToClosed()
250 251
}

252 253 254 255
// 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.
256
func (r *FSRepo) Config() *config.Config {
257 258 259 260 261 262 263 264 265

	// 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()

266 267 268
	if r.state != opened {
		panic(fmt.Sprintln("repo is", r.state))
	}
269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296
	return r.config
}

// setConfigUnsynced is for private use.
func (r *FSRepo) setConfigUnsynced(updated *config.Config) error {
	configFilename, err := config.Filename(r.path)
	if err != nil {
		return err
	}
	// to avoid clobbering user-provided keys, must read the config from disk
	// as a map, write the updated struct values to the map and write the map
	// to disk.
	var mapconf map[string]interface{}
	if err := serialize.ReadConfigFile(configFilename, &mapconf); err != nil {
		return err
	}
	m, err := config.ToMap(updated)
	if err != nil {
		return err
	}
	for k, v := range m {
		mapconf[k] = v
	}
	if err := serialize.WriteConfigFile(configFilename, mapconf); err != nil {
		return err
	}
	*r.config = *updated // copy so caller cannot modify this private config
	return nil
297 298
}

Brian Tiger Chow's avatar
Brian Tiger Chow committed
299
// SetConfig updates the FSRepo's config.
300
func (r *FSRepo) SetConfig(updated *config.Config) error {
301 302 303 304 305

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

306
	return r.setConfigUnsynced(updated)
307 308
}

Brian Tiger Chow's avatar
Brian Tiger Chow committed
309
// GetConfigKey retrieves only the value of a particular key.
310
func (r *FSRepo) GetConfigKey(key string) (interface{}, error) {
311 312 313
	packageLock.Lock()
	defer packageLock.Unlock()

314 315 316
	if r.state != opened {
		return nil, debugerror.Errorf("repo is %s", r.state)
	}
317 318 319 320 321 322 323 324 325 326

	filename, err := config.Filename(r.path)
	if err != nil {
		return nil, err
	}
	var cfg map[string]interface{}
	if err := serialize.ReadConfigFile(filename, &cfg); err != nil {
		return nil, err
	}
	return common.MapGetKV(cfg, key)
327 328
}

Brian Tiger Chow's avatar
Brian Tiger Chow committed
329
// SetConfigKey writes the value of a particular key.
330
func (r *FSRepo) SetConfigKey(key string, value interface{}) error {
331 332 333
	packageLock.Lock()
	defer packageLock.Unlock()

334 335 336
	if r.state != opened {
		return debugerror.Errorf("repo is %s", r.state)
	}
337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362

	filename, err := config.Filename(r.path)
	if err != nil {
		return err
	}
	switch v := value.(type) {
	case string:
		if i, err := strconv.Atoi(v); err == nil {
			value = i
		}
	}
	var mapconf map[string]interface{}
	if err := serialize.ReadConfigFile(filename, &mapconf); err != nil {
		return err
	}
	if err := common.MapSetKV(mapconf, key, value); err != nil {
		return err
	}
	conf, err := config.FromMap(mapconf)
	if err != nil {
		return err
	}
	if err := serialize.WriteConfigFile(filename, mapconf); err != nil {
		return err
	}
	return r.setConfigUnsynced(conf) // TODO roll this into this method
363 364
}

365 366 367 368 369 370 371 372 373
// 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
}

374
var _ io.Closer = &FSRepo{}
375
var _ repo.Repo = &FSRepo{}
376

377 378
// IsInitialized returns true if the repo is initialized at provided |path|.
func IsInitialized(path string) bool {
379 380
	// packageLock is held to ensure that another caller doesn't attempt to
	// Init or Remove the repo while this call is in progress.
381 382
	packageLock.Lock()
	defer packageLock.Unlock()
383

384
	return isInitializedUnsynced(path)
385 386
}

387 388
// private methods below this point. NB: packageLock must held by caller.

389
// isInitializedUnsynced reports whether the repo is initialized. Caller must
390
// hold the packageLock.
391
func isInitializedUnsynced(path string) bool {
392 393 394
	if !configIsInitialized(path) {
		return false
	}
395 396 397 398 399 400
	for _, b := range componentBuilders() {
		if !b.IsInitialized(path) {
			return false
		}
	}
	return true
401
}
402

403
// transitionToOpened manages the state transition to |opened|. Caller must hold
404
// the package mutex.
405
func (r *FSRepo) transitionToOpened() error {
406
	r.state = opened
407
	if countBefore := openersCounter.NumOpeners(r.path); countBefore == 0 { // #first
408 409 410 411 412 413
		closer, err := lockfile.Lock(r.path)
		if err != nil {
			return err
		}
		lockfiles[r.path] = closer
	}
414
	return openersCounter.AddOpener(r.path)
415 416 417
}

// transitionToClosed manages the state transition to |closed|. Caller must
418
// hold the package mutex.
419
func (r *FSRepo) transitionToClosed() error {
420
	r.state = closed
421
	if err := openersCounter.RemoveOpener(r.path); err != nil {
422 423
		return err
	}
424
	if countAfter := openersCounter.NumOpeners(r.path); countAfter == 0 {
425 426 427 428 429 430 431
		closer, ok := lockfiles[r.path]
		if !ok {
			return errors.New("package error: lockfile is not held")
		}
		if err := closer.Close(); err != nil {
			return err
		}
432
		delete(lockfiles, r.path)
433 434 435
	}
	return nil
}
436

437
// components returns the FSRepo's constituent components
438 439
func (r *FSRepo) components() []component.Component {
	return []component.Component{
440
		&r.datastoreComponent,
441 442
	}
}
443 444 445 446

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

447 448 449 450 451 452 453
		// DatastoreComponent
		componentBuilder{
			Init:          component.InitDatastoreComponent,
			IsInitialized: component.DatastoreComponentIsInitialized,
			OpenHandler: func(r *FSRepo) error {
				c := component.DatastoreComponent{}
				c.SetPath(r.path)
454
				if err := c.Open(r.config); err != nil {
455 456 457 458 459 460
					return err
				}
				r.datastoreComponent = c
				return nil
			},
		},
461 462 463 464 465 466 467 468

		// EventlogComponent
		componentBuilder{
			Init:          component.InitEventlogComponent,
			IsInitialized: component.EventlogComponentIsInitialized,
			OpenHandler: func(r *FSRepo) error {
				c := component.EventlogComponent{}
				c.SetPath(r.path)
469
				if err := c.Open(r.config); err != nil {
470 471 472 473 474 475
					return err
				}
				r.eventlogComponent = c
				return nil
			},
		},
476 477
	}
}