forked from textileio/go-textile
-
Notifications
You must be signed in to change notification settings - Fork 0
/
init.go
130 lines (108 loc) · 3.06 KB
/
init.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
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
127
128
129
130
package repo
import (
"context"
"crypto/rand"
"errors"
"fmt"
"os"
"path"
libp2pc "gx/ipfs/QmPvyPwuCgJ7pDmrKDxRtsScJgBaM5h4EpRL2qQJsmXf4n/go-libp2p-crypto"
"gx/ipfs/QmUJYo4etAQqFfSS2rarFAE97eNGB8ej64YkRT2SmsYD4r/go-ipfs/core"
"gx/ipfs/QmUJYo4etAQqFfSS2rarFAE97eNGB8ej64YkRT2SmsYD4r/go-ipfs/namesys"
"gx/ipfs/QmUJYo4etAQqFfSS2rarFAE97eNGB8ej64YkRT2SmsYD4r/go-ipfs/repo/fsrepo"
logging "gx/ipfs/QmZChCsSt8DctjceaL56Eibc29CVQq4dGKRXC5JRZ6Ppae/go-log"
"github.com/textileio/textile-go/ipfs"
"github.com/textileio/textile-go/repo/config"
)
var log = logging.Logger("tex-repo")
var ErrRepoExists = errors.New("repo not empty, reinitializing would overwrite your account")
var ErrRepoDoesNotExist = errors.New("repo does not exist, initialization is required")
var ErrMigrationRequired = errors.New("repo needs migration")
var ErrRepoCorrupted = errors.New("repo is corrupted")
const repover = "6"
func Init(repoPath string, version string) error {
if err := checkWriteable(repoPath); err != nil {
return err
}
if fsrepo.IsInitialized(repoPath) {
return ErrRepoExists
}
log.Infof("initializing repo at %s", repoPath)
// create an identity for the ipfs peer
sk, _, err := libp2pc.GenerateEd25519Key(rand.Reader)
if err != nil {
return err
}
peerIdentity, err := ipfs.IdentityConfig(sk)
if err != nil {
return err
}
// initialize ipfs config
conf, err := config.InitIpfs(peerIdentity)
if err != nil {
return err
}
if err := fsrepo.Init(repoPath, conf); err != nil {
return err
}
// write default textile config
tconf, err := config.Init(version)
if err != nil {
return err
}
if err := config.Write(repoPath, tconf); err != nil {
return err
}
// write repo version
repoverFile, err := os.Create(path.Join(repoPath, "repover"))
if err != nil {
return err
}
defer repoverFile.Close()
if _, err := repoverFile.Write([]byte(repover)); err != nil {
return err
}
return initializeIpnsKeyspace(repoPath)
}
func checkWriteable(dir string) error {
_, err := os.Stat(dir)
if err == nil {
// dir exists, make sure we can write to it
testfile := path.Join(dir, "test")
fi, err := os.Create(testfile)
if err != nil {
if os.IsPermission(err) {
return fmt.Errorf("%s is not writeable by the current user", dir)
}
return fmt.Errorf("unexpected error while checking writeablility of repo root: %s", err)
}
fi.Close()
return os.Remove(testfile)
}
if os.IsNotExist(err) {
// dir doesnt exist, check that we can create it
return os.Mkdir(dir, 0775)
}
if os.IsPermission(err) {
return fmt.Errorf("cannot write to %s, incorrect permissions", err)
}
return err
}
func initializeIpnsKeyspace(repoRoot string) error {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
r, err := fsrepo.Open(repoRoot)
if err != nil { // NB: repo is owned by the node
return err
}
nd, err := core.NewNode(ctx, &core.BuildCfg{Repo: r})
if err != nil {
return err
}
defer nd.Close()
err = nd.SetupOfflineRouting()
if err != nil {
return err
}
return namesys.InitializeKeyspace(ctx, nd.Namesys, nd.Pinning, nd.PrivateKey)
}