@cryptotaxi247 / kubo / commits / c419a489e

make ipfs understand the new migration

Jeromy committed Apr 9, 2015 at 17:13 UTC c419a489e1ab684e7ff1aab0d2cbe6b99b540768
3 files changed +41 -30
cmd/ipfs/daemon.go
+5 -10
@@ -103,21 +103,16 @@ func daemonFunc(req cmds.Request, res cmds.Response) {
103 }
104 }
105
106 - // To ensure that IPFS has been initialized, fetch the config. Do this
107 - // _before_ acquiring the daemon lock so the user gets an appropriate error
108 - // message.
109 - // NB: It's safe to read the config without the daemon lock, but not safe
110 - // to write.
111 - ctx := req.Context()
112 - cfg, err := ctx.GetConfig()
106 + // acquire the repo lock _before_ constructing a node. we need to make
107 + // sure we are permitted to access the resources (datastore, etc.)
108 + repo, err := fsrepo.Open(req.Context().ConfigRoot)
109 if err != nil {
110 res.SetError(err, cmds.ErrNormal)
111 return
112 }
113
118 - // acquire the repo lock _before_ constructing a node. we need to make
119 - // sure we are permitted to access the resources (datastore, etc.)
120 - repo, err := fsrepo.Open(req.Context().ConfigRoot)
114 + ctx := req.Context()
115 + cfg, err := ctx.GetConfig()
116 if err != nil {
117 res.SetError(err, cmds.ErrNormal)
118 return
repo/fsrepo/fsrepo.go
+35 -19
@@ -4,10 +4,10 @@ import (
4 "errors"
5 "fmt"
6 "io"
7 - "io/ioutil"
7 "os"
8 "path"
9 "strconv"
10 + "strings"
11 "sync"
12
13 ds "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore"
@@ -28,8 +28,13 @@ import (
28 ds2 "github.com/ipfs/go-ipfs/util/datastore2"
29 )
30
31 +// version number that we are currently expecting to see
32 var RepoVersion = "2"
33
34 +var incorrectRepoFormat = "Repo has incorrect version: '%s'\nProgram version is: '%s'\nPlease run the appropriate migration tool before continuing"
35 +
36 +var ErrNoVersion = errors.New("version check failed, no version file found, please run 0-to-1 migration tool.")
37 +
38 const (
39 leveldbDirectory = "datastore"
40 flatfsDirectory = "blocks"
@@ -87,13 +92,14 @@ func open(repoPath string) (repo.Repo, error) {
92 packageLock.Lock()
93 defer packageLock.Unlock()
94
90 - expPath, err := u.TildeExpansion(path.Clean(repoPath))
95 + r, err := newFSRepo(repoPath)
96 if err != nil {
97 return nil, err
98 }
99
95 - r := &FSRepo{
96 - path: expPath,
100 + // Check if its initialized
101 + if err := checkInitialized(r.path); err != nil {
102 + return nil, err
103 }
104
105 r.lockfile, err = lockfile.Lock(r.path)
@@ -108,30 +114,20 @@ func open(repoPath string) (repo.Repo, error) {
114 }
115 }()
116
111 - if !isInitializedUnsynced(r.path) {
112 - return nil, errors.New("ipfs not initialized, please run 'ipfs init'")
113 - }
114 -
117 // Check version, and error out if not matching
116 - ver, err := ioutil.ReadFile(path.Join(expPath, "version"))
118 + ver, err := mfsr.RepoPath(r.path).Version()
119 if err != nil {
120 if os.IsNotExist(err) {
119 - return nil, errors.New("version check failed, no version file found, please run 0-to-1 migration tool.")
121 + return nil, ErrNoVersion
122 }
123 return nil, err
124 }
125
124 - vers := string(ver)[:1]
125 -
126 - if vers != RepoVersion {
127 - return nil, fmt.Errorf("Repo has incorrect version: '%s'\nProgram version is: '%s'\nPlease run the appropriate migration tool before continuing",
128 - vers, RepoVersion)
129 -
126 + if ver != RepoVersion {
127 + return nil, fmt.Errorf(incorrectRepoFormat, ver, RepoVersion)
128 }
129
130 // check repo path, then check all constituent parts.
133 - // TODO acquire repo lock
134 - // TODO if err := initCheckDir(logpath); err != nil { // }
131 if err := dir.Writable(r.path); err != nil {
132 return nil, err
133 }
@@ -144,13 +140,33 @@ func open(repoPath string) (repo.Repo, error) {
140 return nil, err
141 }
142
147 - // log.Debugf("writing eventlogs to ...", c.path)
143 + // setup eventlogger
144 configureEventLoggerAtRepoPath(r.config, r.path)
145
146 keepLocked = true
147 return r, nil
148 }
149
150 +func newFSRepo(rpath string) (*FSRepo, error) {
151 + expPath, err := u.TildeExpansion(path.Clean(rpath))
152 + if err != nil {
153 + return nil, err
154 + }
155 +
156 + return &FSRepo{path: expPath}, nil
157 +}
158 +
159 +func checkInitialized(path string) error {
160 + if !isInitializedUnsynced(path) {
161 + alt := strings.Replace(path, ".ipfs", ".go-ipfs", 1)
162 + if isInitializedUnsynced(alt) {
163 + return debugerror.New("ipfs repo found in old '.go-ipfs' location, please run migration tool")
164 + }
165 + return debugerror.New("ipfs not initialized, please run 'ipfs init'")
166 + }
167 + return nil
168 +}
169 +
170 // ConfigAt returns an error if the FSRepo at the given path is not
171 // initialized. This function allows callers to read the config file even when
172 // another process is running and holding the lock.
repo/fsrepo/migrations/1-to-2/main.go
+1 -1
@@ -124,7 +124,7 @@ func transferBlocksToFlatDB(repopath string) error {
124
125 blockspath := path.Join(repopath, "blocks")
126 err = os.Mkdir(blockspath, 0777)
127 - if err != nil {
127 + if err != nil && !os.IsExist(err) {
128 return err
129 }
130