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

import (
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
4 5
	"errors"
	"fmt"
6
	"io"
7
	"os"
Jeromy's avatar
Jeromy committed
8
	"path/filepath"
9
	"strconv"
10
	"strings"
11
	"sync"
12

13
	"github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/mitchellh/go-homedir"
14 15 16 17
	repo "github.com/ipfs/go-ipfs/repo"
	"github.com/ipfs/go-ipfs/repo/common"
	config "github.com/ipfs/go-ipfs/repo/config"
	lockfile "github.com/ipfs/go-ipfs/repo/fsrepo/lock"
18
	mfsr "github.com/ipfs/go-ipfs/repo/fsrepo/migrations"
19 20
	serialize "github.com/ipfs/go-ipfs/repo/fsrepo/serialize"
	dir "github.com/ipfs/go-ipfs/thirdparty/dir"
21
	logging "gx/ipfs/QmNQynaz7qfriSUJkiEZUrm2Wen1u3Kj9goZzWtrPyu7XR/go-log"
22
	"gx/ipfs/QmTxLSvdhwg68WJimdS6icLPhZi28aTp6b7uihC2Yb47Xk/go-datastore/measure"
23
	util "gx/ipfs/QmZNVWh8LLjAavuQ2JXuFmuYH3C11xo988vSgp7UQrTRj1/go-ipfs-util"
24 25
)

Jeromy's avatar
Jeromy committed
26 27
var log = logging.Logger("fsrepo")

28
// version number that we are currently expecting to see
29
var RepoVersion = 4
30

31 32
var migrationInstructions = `See https://github.com/ipfs/fs-repo-migrations/blob/master/run.md
Sorry for the inconvenience. In the future, these will run automatically.`
33

34 35 36 37 38
var errIncorrectRepoFmt = `Repo has incorrect version: %s
Program version is: %s
Please run the ipfs migration tool before continuing.
` + migrationInstructions

39 40 41 42 43 44
var programTooLowMessage = `Your programs version (%d) is lower than your repos (%d).
Please update ipfs to a version that supports the existing repo, or run
a migration in reverse.

See https://github.com/ipfs/fs-repo-migrations/blob/master/run.md for details.`

45
var (
Jeromy's avatar
Jeromy committed
46 47 48
	ErrNoVersion     = errors.New("no version file found, please run 0-to-1 migration tool.\n" + migrationInstructions)
	ErrOldRepo       = errors.New("ipfs repo found in old '~/.go-ipfs' location, please run migration tool.\n" + migrationInstructions)
	ErrNeedMigration = errors.New("ipfs repo needs migration.")
49
)
50

51 52 53 54 55 56 57
type NoRepoError struct {
	Path string
}

var _ error = NoRepoError{}

func (err NoRepoError) Error() string {
rht's avatar
rht committed
58
	return fmt.Sprintf("no ipfs repo found in %s.\nplease run: ipfs init", err.Path)
59 60
}

61
const apiFile = "api"
62

63
var (
64 65 66

	// packageLock must be held to while performing any operation that modifies an
	// FSRepo's state field. This includes Init, Open, Close, and Remove.
Tommi Virtanen's avatar
Tommi Virtanen committed
67
	packageLock sync.Mutex
68

69 70 71 72 73 74 75 76 77 78 79 80 81
	// onlyOne keeps track of open FSRepo instances.
	//
	// TODO: once command Context / Repo integration is cleaned up,
	// this can be removed. Right now, this makes ConfigCmd.Run
	// function try to open the repo twice:
	//
	//     $ ipfs daemon &
	//     $ ipfs config foo
	//
	// The reason for the above is that in standalone mode without the
	// daemon, `ipfs config` tries to save work by not building the
	// full IpfsNode, but accessing the Repo directly.
	onlyOne repo.OnlyOne
82 83
)

84 85
// FSRepo represents an IPFS FileSystem Repo. It is safe for use by multiple
// callers.
86
type FSRepo struct {
87 88
	// has Close been called already
	closed bool
Brian Tiger Chow's avatar
Brian Tiger Chow committed
89 90
	// path is the file-system path
	path string
91 92 93
	// lockfile is the file system lock to prevent others from opening
	// the same fsrepo path concurrently
	lockfile io.Closer
94
	config   *config.Config
95
	ds       repo.Datastore
96 97
}

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

100 101
// Open the FSRepo at path. Returns an error if the repo is not
// initialized.
102 103 104 105 106 107 108 109
func Open(repoPath string) (repo.Repo, error) {
	fn := func() (repo.Repo, error) {
		return open(repoPath)
	}
	return onlyOne.Open(repoPath, fn)
}

func open(repoPath string) (repo.Repo, error) {
110 111 112
	packageLock.Lock()
	defer packageLock.Unlock()

113
	r, err := newFSRepo(repoPath)
114 115 116 117
	if err != nil {
		return nil, err
	}

118 119 120
	// Check if its initialized
	if err := checkInitialized(r.path); err != nil {
		return nil, err
121
	}
Tommi Virtanen's avatar
Tommi Virtanen committed
122

Tommi Virtanen's avatar
Tommi Virtanen committed
123 124 125 126 127 128 129 130 131 132 133 134
	r.lockfile, err = lockfile.Lock(r.path)
	if err != nil {
		return nil, err
	}
	keepLocked := false
	defer func() {
		// unlock on error, leave it locked on success
		if !keepLocked {
			r.lockfile.Close()
		}
	}()

135
	// Check version, and error out if not matching
136
	ver, err := mfsr.RepoPath(r.path).Version()
137 138
	if err != nil {
		if os.IsNotExist(err) {
139
			return nil, ErrNoVersion
140 141 142 143
		}
		return nil, err
	}

144
	if RepoVersion > ver {
Jeromy's avatar
Jeromy committed
145
		return nil, ErrNeedMigration
146 147 148
	} else if ver > RepoVersion {
		// program version too low for existing repo
		return nil, fmt.Errorf(programTooLowMessage, RepoVersion, ver)
149 150
	}

151 152 153 154 155 156 157 158 159 160 161 162 163
	// check repo path, then check all constituent parts.
	if err := dir.Writable(r.path); err != nil {
		return nil, err
	}

	if err := r.openConfig(); err != nil {
		return nil, err
	}

	if err := r.openDatastore(); err != nil {
		return nil, err
	}

Tommi Virtanen's avatar
Tommi Virtanen committed
164
	keepLocked = true
165
	return r, nil
166 167
}

168
func newFSRepo(rpath string) (*FSRepo, error) {
169
	expPath, err := homedir.Expand(filepath.Clean(rpath))
170 171 172 173 174 175 176 177 178 179 180
	if err != nil {
		return nil, err
	}

	return &FSRepo{path: expPath}, nil
}

func checkInitialized(path string) error {
	if !isInitializedUnsynced(path) {
		alt := strings.Replace(path, ".ipfs", ".go-ipfs", 1)
		if isInitializedUnsynced(alt) {
181
			return ErrOldRepo
182
		}
183
		return NoRepoError{Path: path}
184 185 186 187
	}
	return nil
}

188 189 190
// 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
191
func ConfigAt(repoPath string) (*config.Config, error) {
192 193 194 195 196

	// 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
197 198 199 200
	configFilename, err := config.Filename(repoPath)
	if err != nil {
		return nil, err
	}
201
	return serialize.Load(configFilename)
Brian Tiger Chow's avatar
huh  
Brian Tiger Chow committed
202 203
}

204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233
// 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
234
// Init initializes a new FSRepo at the given path with the provided config.
235
// TODO add support for custom datastores.
236
func Init(repoPath string, conf *config.Config) error {
237 238 239 240

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

243
	if isInitializedUnsynced(repoPath) {
244 245
		return nil
	}
246

247
	if err := initConfig(repoPath, conf); err != nil {
248 249 250
		return err
	}

251 252
	if err := initDefaultDatastore(repoPath, conf); err != nil {
		return err
253 254
	}

255 256 257 258
	if err := mfsr.RepoPath(repoPath).WriteVersion(RepoVersion); err != nil {
		return err
	}

259 260 261
	return nil
}

262 263
// LockedByOtherProcess returns true if the FSRepo is locked by another
// process. If true, then the repo cannot be opened by this process.
264
func LockedByOtherProcess(repoPath string) (bool, error) {
Jeromy's avatar
Jeromy committed
265
	repoPath = filepath.Clean(repoPath)
michael's avatar
michael committed
266 267 268 269 270
	locked, err := lockfile.Locked(repoPath)
	if locked {
		log.Debugf("(%t)<->Lock is held at %s", locked, repoPath)
	}
	return locked, err
271 272
}

273 274 275 276 277
// APIAddr returns the registered API addr, according to the api file
// in the fsrepo. This is a concurrent operation, meaning that any
// process may read this file. modifying this file, therefore, should
// use "mv" to replace the whole file and avoid interleaved read/writes.
func APIAddr(repoPath string) (string, error) {
Jeromy's avatar
Jeromy committed
278 279
	repoPath = filepath.Clean(repoPath)
	apiFilePath := filepath.Join(repoPath, apiFile)
280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305

	// if there is no file, assume there is no api addr.
	f, err := os.Open(apiFilePath)
	if err != nil {
		if os.IsNotExist(err) {
			return "", repo.ErrApiNotRunning
		}
		return "", err
	}
	defer f.Close()

	// read up to 2048 bytes. io.ReadAll is a vulnerability, as
	// someone could hose the process by putting a massive file there.
	buf := make([]byte, 2048)
	n, err := f.Read(buf)
	if err != nil && err != io.EOF {
		return "", err
	}

	s := string(buf[:n])
	s = strings.TrimSpace(s)
	return s, nil
}

// SetAPIAddr writes the API Addr to the /api file.
func (r *FSRepo) SetAPIAddr(addr string) error {
Jeromy's avatar
Jeromy committed
306
	f, err := os.Create(filepath.Join(r.path, apiFile))
307 308 309 310 311 312 313 314 315
	if err != nil {
		return err
	}
	defer f.Close()

	_, err = f.WriteString(addr)
	return err
}

316 317 318 319 320 321 322 323 324 325 326 327 328 329
// 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
}

330 331
// openDatastore returns an error if the config file is not present.
func (r *FSRepo) openDatastore() error {
332 333 334 335 336 337 338 339 340
	switch r.config.Datastore.Type {
	case "default", "leveldb", "":
		d, err := openDefaultDatastore(r)
		if err != nil {
			return err
		}
		r.ds = d
	default:
		return fmt.Errorf("unknown datastore type: %s", r.config.Datastore.Type)
341
	}
342

343 344 345 346 347 348 349 350 351 352 353 354 355 356
	// Wrap it with metrics gathering
	//
	// Add our PeerID to metrics paths to keep them unique
	//
	// As some tests just pass a zero-value Config to fsrepo.Init,
	// cope with missing PeerID.
	id := r.config.Identity.PeerID
	if id == "" {
		// the tests pass in a zero Config; cope with it
		id = fmt.Sprintf("uninitialized_%p", r)
	}
	prefix := "fsrepo." + id + ".datastore"
	r.ds = measure.New(prefix, r.ds)

357 358 359
	return nil
}

360 361 362 363 364
// Close closes the FSRepo, releasing held resources.
func (r *FSRepo) Close() error {
	packageLock.Lock()
	defer packageLock.Unlock()

365
	if r.closed {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
366
		return errors.New("repo is closed")
367
	}
368

Jeromy's avatar
Jeromy committed
369
	err := os.Remove(filepath.Join(r.path, apiFile))
michael's avatar
michael committed
370
	if err != nil && !os.IsNotExist(err) {
Jeromy's avatar
Jeromy committed
371 372 373
		log.Warning("error removing api file: ", err)
	}

374 375 376 377
	if err := r.ds.Close(); err != nil {
		return err
	}

378 379 380 381 382 383
	// This code existed in the previous versions, but
	// EventlogComponent.Close was never called. Preserving here
	// pending further discussion.
	//
	// TODO It isn't part of the current contract, but callers may like for us
	// to disable logging once the component is closed.
Jeromy's avatar
Jeromy committed
384
	// logging.Configure(logging.Output(os.Stderr))
385

386 387 388 389 390
	r.closed = true
	if err := r.lockfile.Close(); err != nil {
		return err
	}
	return nil
391 392
}

393
// Result when not Open is undefined. The method may panic if it pleases.
394
func (r *FSRepo) Config() (*config.Config, error) {
395 396 397 398 399 400 401 402 403

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

404
	if r.closed {
405
		return nil, errors.New("cannot access config, repo not open")
406
	}
407
	return r.config, nil
408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434
}

// 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
435 436
}

Brian Tiger Chow's avatar
Brian Tiger Chow committed
437
// SetConfig updates the FSRepo's config.
438
func (r *FSRepo) SetConfig(updated *config.Config) error {
439 440 441 442 443

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

444
	return r.setConfigUnsynced(updated)
445 446
}

Brian Tiger Chow's avatar
Brian Tiger Chow committed
447
// GetConfigKey retrieves only the value of a particular key.
448
func (r *FSRepo) GetConfigKey(key string) (interface{}, error) {
449 450 451
	packageLock.Lock()
	defer packageLock.Unlock()

452
	if r.closed {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
453
		return nil, errors.New("repo is closed")
454
	}
455 456 457 458 459 460 461 462 463 464

	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)
465 466
}

Brian Tiger Chow's avatar
Brian Tiger Chow committed
467
// SetConfigKey writes the value of a particular key.
468
func (r *FSRepo) SetConfigKey(key string, value interface{}) error {
469 470 471
	packageLock.Lock()
	defer packageLock.Unlock()

472
	if r.closed {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
473
		return errors.New("repo is closed")
474
	}
475 476 477 478 479 480 481 482 483

	filename, err := config.Filename(r.path)
	if err != nil {
		return err
	}
	var mapconf map[string]interface{}
	if err := serialize.ReadConfigFile(filename, &mapconf); err != nil {
		return err
	}
484

485 486 487 488 489 490 491 492
	// Load private key to guard against it being overwritten.
	// NOTE: this is a temporary measure to secure this field until we move
	// keys out of the config file.
	pkval, err := common.MapGetKV(mapconf, config.PrivKeySelector)
	if err != nil {
		return err
	}

493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529
	// Get the type of the value associated with the key
	oldValue, err := common.MapGetKV(mapconf, key)
	ok := true
	if err != nil {
		// key-value does not exist yet
		switch v := value.(type) {
		case string:
			value, err = strconv.ParseBool(v)
			if err != nil {
				value, err = strconv.Atoi(v)
				if err != nil {
					value, err = strconv.ParseFloat(v, 32)
					if err != nil {
						value = v
					}
				}
			}
		default:
		}
	} else {
		switch oldValue.(type) {
		case bool:
			value, ok = value.(bool)
		case int:
			value, ok = value.(int)
		case float32:
			value, ok = value.(float32)
		case string:
			value, ok = value.(string)
		default:
			value = value
		}
		if !ok {
			return fmt.Errorf("Wrong config type, expected %T", oldValue)
		}
	}

530 531 532
	if err := common.MapSetKV(mapconf, key, value); err != nil {
		return err
	}
533

534 535 536 537 538
	// replace private key, in case it was overwritten.
	if err := common.MapSetKV(mapconf, "Identity.PrivKey", pkval); err != nil {
		return err
	}

539 540
	// This step doubles as to validate the map against the struct
	// before serialization
541 542 543 544 545 546 547 548
	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
549 550
}

551 552
// Datastore returns a repo-owned datastore. If FSRepo is Closed, return value
// is undefined.
553
func (r *FSRepo) Datastore() repo.Datastore {
554
	packageLock.Lock()
555
	d := r.ds
556 557 558 559
	packageLock.Unlock()
	return d
}

rht's avatar
rht committed
560 561 562 563 564 565 566 567 568
// GetStorageUsage computes the storage space taken by the repo in bytes
func (r *FSRepo) GetStorageUsage() (uint64, error) {
	pth, err := config.PathRoot()
	if err != nil {
		return 0, err
	}

	var du uint64
	err = filepath.Walk(pth, func(p string, f os.FileInfo, err error) error {
rht's avatar
rht committed
569 570 571 572 573 574 575
		if err != nil {
			log.Debugf("filepath.Walk error: %s", err)
			return nil
		}
		if f != nil {
			du += uint64(f.Size())
		}
rht's avatar
rht committed
576 577 578 579 580
		return nil
	})
	return du, err
}

581
var _ io.Closer = &FSRepo{}
582
var _ repo.Repo = &FSRepo{}
583

584 585
// IsInitialized returns true if the repo is initialized at provided |path|.
func IsInitialized(path string) bool {
586 587
	// packageLock is held to ensure that another caller doesn't attempt to
	// Init or Remove the repo while this call is in progress.
588 589
	packageLock.Lock()
	defer packageLock.Unlock()
590

591
	return isInitializedUnsynced(path)
592 593
}

594 595
// private methods below this point. NB: packageLock must held by caller.

596
// isInitializedUnsynced reports whether the repo is initialized. Caller must
597
// hold the packageLock.
598
func isInitializedUnsynced(repoPath string) bool {
599
	return configIsInitialized(repoPath)
600
}