// Apply applies the migration in question.
// This migration merely adds a version file.
func (m Migration) Apply(opts migrate.Options) error {
	repolk, err := lock.Lock(opts.Path)
	if err != nil {
		return err
	}
	defer repolk.Close()

	repo := mfsr.RepoPath(opts.Path)

	// first, check if there is a version file.
	// if there is, bail out.
	if v, err := repo.Version(); err == nil {
		return fmt.Errorf("repo at %s is version %s (not 0)", opts.Path, v)
	} else if !strings.Contains(err.Error(), "no version file in repo") {
		return err
	}

	// add the version file
	if err := repo.WriteVersion("1"); err != nil {
		return err
	}

	if opts.Verbose {
		fmt.Println("wrote version file")
	}

	return nil
}
func (m Migration) Revert(opts migrate.Options) error {
	repolk, err := lock.Lock2(opts.Path) // lock repo.lock
	if err != nil {
		return err
	}
	defer repolk.Close()

	repo := mfsr.RepoPath(opts.Path)
	if err := repo.CheckVersion("2"); err != nil {
		return err
	}

	// 1) Move directory back to .go-ipfs
	npath, err := reverseIpfsDir(opts.Path)
	if err != nil {
		return err
	}
	if opts.Verbose {
		fmt.Println("moved ipfs directory from .ipfs to .go-ipfs")
	}

	// 2) move blocks back from flatfs to leveldb
	err = transferBlocksFromFlatDB(npath, opts.Verbose)
	if err != nil {
		return err
	}
	if opts.Verbose {
		fmt.Println("moved blocks from flatfs to leveldb")
	}

	// 3) change version number back down
	repo = mfsr.RepoPath(npath)
	err = repo.WriteVersion("1")
	if err != nil {
		return err
	}
	if opts.Verbose {
		fmt.Println("lowered version number to 1")
	}

	return nil
}
Beispiel #3
0
func GetVersion(ipfsdir string) (int, error) {
	ver, err := mfsr.RepoPath(ipfsdir).Version()
	if _, ok := err.(mfsr.VersionFileNotFound); ok {
		// No version file in repo == version 0
		return 0, nil
	}

	if err != nil {
		return 0, err
	}

	vnum, err := strconv.Atoi(ver)
	if err != nil {
		return 0, err
	}

	return vnum, nil
}
// Revert un-applies the migration in question. This should be best-effort.
// Some migrations are definitively one-way. If so, return an error.
func (m Migration) Revert(opts migrate.Options) error {
	repo := mfsr.RepoPath(opts.Path)

	repolk, err := lock.Lock(opts.Path)
	if err != nil {
		return err
	}
	defer repolk.Close()

	if err := repo.CheckVersion("1"); err != nil {
		return err
	}

	// remove the version file
	if err := os.Remove(repo.VersionFile()); err != nil {
		return err
	}
	if opts.Verbose {
		fmt.Println("deleted version file")
	}

	return nil
}
func (m Migration) Apply(opts migrate.Options) error {

	// lock the daemon.lock file. and if we succeed, remove it at the end.
	// we remove it because camlistore/lock doesn't, and we changed the filename.
	// so we don't want this one around anymore.
	repolk, err := lock.Lock1(opts.Path)
	if err != nil {
		return err
	}
	closedLock := false
	defer func() {
		if !closedLock { // unlock only if we didn't close below
			repolk.Close()
		}
	}()

	repo := mfsr.RepoPath(opts.Path)

	if err := repo.CheckVersion("1"); err != nil {
		return err
	}

	// 1) run some sanity checks to make sure we should even bother
	err = sanityChecks(opts)
	if err != nil {
		return err
	}
	if opts.Verbose {
		fmt.Println("performed sanity check")
	}

	// 2) Transfer blocks out of leveldb into flatDB
	err = transferBlocksToFlatDB(opts.Path, opts.Verbose)
	if err != nil {
		return err
	}
	if opts.Verbose {
		fmt.Println("moved blocks from leveldb to flatfs")
	}

	// 3) move ipfs path from .go-ipfs to .ipfs
	newpath, err := moveIpfsDir(opts.Path)
	if err != nil {
		return err
	}
	if opts.Verbose {
		fmt.Println("moved ipfs directory from .go-ipfs to .ipfs")
	}

	// 4) Update version number
	repo = mfsr.RepoPath(newpath)
	err = repo.WriteVersion("2")
	if err != nil {
		return err
	}
	if opts.Verbose {
		fmt.Println("updated version file")
	}

	// 5) Remove daemon.lock file
	if opts.Verbose {
		fmt.Println("removing daemon.lock file")
	}
	repolk.Close()
	closedLock = true
	lock.Remove1(newpath) // ok if this fails.
	return nil
}