fsrepo.go 10.2 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
	lockfile "github.com/jbenet/go-ipfs/repo/fsrepo/lock"
17
	serialize "github.com/jbenet/go-ipfs/repo/fsrepo/serialize"
Brian Tiger Chow's avatar
Brian Tiger Chow committed
18
	dir "github.com/jbenet/go-ipfs/thirdparty/dir"
Jeromy's avatar
Jeromy committed
19
	u "github.com/jbenet/go-ipfs/util"
20
	debugerror "github.com/jbenet/go-ipfs/util/debugerror"
21 22
)

23
var (
24 25 26

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

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

46 47
// FSRepo represents an IPFS FileSystem Repo. It is safe for use by multiple
// callers.
48
type FSRepo struct {
Brian Tiger Chow's avatar
Brian Tiger Chow committed
49 50 51 52
	// state is the FSRepo's state (unopened, opened, closed)
	state state
	// path is the file-system path
	path string
53 54
	// 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
55
	// TODO test
56
	configComponent    component.ConfigComponent
57
	datastoreComponent component.DatastoreComponent
58
	eventlogComponent  component.EventlogComponent
59 60
}

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

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

76 77 78
// 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
79
func ConfigAt(repoPath string) (*config.Config, error) {
80 81 82 83 84

	// 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
85 86 87 88
	configFilename, err := config.Filename(repoPath)
	if err != nil {
		return nil, err
	}
89
	return serialize.Load(configFilename)
Brian Tiger Chow's avatar
huh  
Brian Tiger Chow committed
90 91
}

Brian Tiger Chow's avatar
Brian Tiger Chow committed
92
// Init initializes a new FSRepo at the given path with the provided config.
93
// TODO add support for custom datastores.
94
func Init(path string, conf *config.Config) error {
95 96 97 98

	// packageLock must be held to ensure that the repo is not initialized more
	// than once.
	packageLock.Lock()
99
	defer packageLock.Unlock()
100 101

	if isInitializedUnsynced(path) {
102 103
		return nil
	}
104 105 106 107
	for _, b := range componentBuilders() {
		if err := b.Init(path, conf); err != nil {
			return err
		}
108 109 110 111
	}
	return nil
}

112
// Remove recursively removes the FSRepo at |path|.
113 114 115 116 117
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.
118 119
	packageLock.Lock()
	defer packageLock.Unlock()
120

121
	if openersCounter.NumOpeners(repoPath) != 0 {
122 123
		return errors.New("repo in use")
	}
124
	return os.RemoveAll(repoPath)
125 126
}

127 128 129
// 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 {
130 131 132
	repoPath = path.Clean(repoPath)

	// packageLock must be held to check the number of openers.
133 134
	packageLock.Lock()
	defer packageLock.Unlock()
135

136
	// NB: the lock is only held when repos are Open
137
	return lockfile.Locked(repoPath) && openersCounter.NumOpeners(repoPath) == 0
138 139
}

140
// Open returns an error if the repo is not initialized.
141
func (r *FSRepo) Open() error {
142 143 144 145

	// 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.
146 147
	packageLock.Lock()
	defer packageLock.Unlock()
148

Jeromy's avatar
Jeromy committed
149 150 151 152 153 154
	expPath, err := u.TildeExpansion(r.path)
	if err != nil {
		return err
	}
	r.path = expPath

155 156 157
	if r.state != unopened {
		return debugerror.Errorf("repo is %s", r.state)
	}
158
	if !isInitializedUnsynced(r.path) {
159
		return debugerror.New("ipfs not initialized, please run 'ipfs init'")
160
	}
161
	// check repo path, then check all constituent parts.
162
	// TODO acquire repo lock
163
	// TODO if err := initCheckDir(logpath); err != nil { // }
164
	if err := dir.Writable(r.path); err != nil {
165 166 167
		return err
	}

168 169
	for _, b := range componentBuilders() {
		if err := b.OpenHandler(r); err != nil {
170 171
			return err
		}
172 173
	}

174
	return r.transitionToOpened()
175 176
}

177 178 179 180 181 182 183 184
// 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)
	}
185 186 187 188 189 190

	for _, closer := range r.components() {
		if err := closer.Close(); err != nil {
			return err
		}
	}
191
	return r.transitionToClosed()
192 193
}

194 195 196 197
// 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.
198
func (r *FSRepo) Config() *config.Config {
199 200 201 202 203 204 205 206 207

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

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

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

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

221
	return r.configComponent.SetConfig(updated)
222 223
}

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

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

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

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

246 247 248 249 250 251 252 253 254
// 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
}

255
var _ io.Closer = &FSRepo{}
256
var _ repo.Repo = &FSRepo{}
257

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

265
	return isInitializedUnsynced(path)
266 267
}

268 269
// private methods below this point. NB: packageLock must held by caller.

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

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

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

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

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

326 327
		// ConfigComponent must be initialized first because other components
		// depend on it.
328 329 330 331
		componentBuilder{
			Init:          component.InitConfigComponent,
			IsInitialized: component.ConfigComponentIsInitialized,
			OpenHandler: func(r *FSRepo) error {
332 333
				c := component.ConfigComponent{}
				c.SetPath(r.path)
334
				if err := c.Open(nil); err != nil {
335 336
					return err
				}
337
				r.configComponent = c
338 339 340 341
				return nil
			},
		},

342 343 344 345 346 347 348
		// DatastoreComponent
		componentBuilder{
			Init:          component.InitDatastoreComponent,
			IsInitialized: component.DatastoreComponentIsInitialized,
			OpenHandler: func(r *FSRepo) error {
				c := component.DatastoreComponent{}
				c.SetPath(r.path)
349
				if err := c.Open(r.configComponent.Config()); err != nil {
350 351 352 353 354 355
					return err
				}
				r.datastoreComponent = c
				return nil
			},
		},
356 357 358 359 360 361 362 363

		// EventlogComponent
		componentBuilder{
			Init:          component.InitEventlogComponent,
			IsInitialized: component.EventlogComponentIsInitialized,
			OpenHandler: func(r *FSRepo) error {
				c := component.EventlogComponent{}
				c.SetPath(r.path)
364
				if err := c.Open(r.configComponent.Config()); err != nil {
365 366 367 368 369 370
					return err
				}
				r.eventlogComponent = c
				return nil
			},
		},
371 372
	}
}