Skip to content

Commit

Permalink
Merge pull request #546 from jbenet/feat/repo-fsrepo
Browse files Browse the repository at this point in the history
feat(repo): FSRepo
  • Loading branch information
Brian Tiger Chow committed Jan 13, 2015
2 parents 5cade6a + 01f54d6 commit e5bd30b
Show file tree
Hide file tree
Showing 40 changed files with 956 additions and 536 deletions.
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ test_go_short:
test_go_expensive:
go test ./...

test_go_race:
go test ./... -race

test_sharness_short:
cd test/sharness/ && make

Expand Down
121 changes: 39 additions & 82 deletions cmd/ipfs/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,19 @@ import (
"bytes"
"encoding/base64"
"fmt"
"os"
"path"
"path/filepath"

context "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"
cmds "github.com/jbenet/go-ipfs/commands"
config "github.com/jbenet/go-ipfs/config"
core "github.com/jbenet/go-ipfs/core"
corecmds "github.com/jbenet/go-ipfs/core/commands"
imp "github.com/jbenet/go-ipfs/importer"
chunk "github.com/jbenet/go-ipfs/importer/chunk"
ci "github.com/jbenet/go-ipfs/p2p/crypto"
peer "github.com/jbenet/go-ipfs/p2p/peer"
repo "github.com/jbenet/go-ipfs/repo"
config "github.com/jbenet/go-ipfs/repo/config"
fsrepo "github.com/jbenet/go-ipfs/repo/fsrepo"
u "github.com/jbenet/go-ipfs/util"
debugerror "github.com/jbenet/go-ipfs/util/debugerror"
)
Expand All @@ -34,7 +33,6 @@ var initCmd = &cmds.Command{
cmds.IntOption("bits", "b", "Number of bits to use in the generated RSA private key (defaults to 4096)"),
cmds.StringOption("passphrase", "p", "Passphrase for encrypting the private key"),
cmds.BoolOption("force", "f", "Overwrite existing config (if it exists)"),
cmds.StringOption("datastore", "d", "Location for the IPFS data store"),

// TODO need to decide whether to expose the override as a file or a
// directory. That is: should we allow the user to also specify the
Expand All @@ -43,11 +41,6 @@ var initCmd = &cmds.Command{
},
Run: func(req cmds.Request) (interface{}, error) {

dspathOverride, _, err := req.Option("d").String() // if !found it's okay. Let == ""
if err != nil {
return nil, err
}

force, _, err := req.Option("f").Bool() // if !found, it's okay force == false
if err != nil {
return nil, err
Expand All @@ -61,11 +54,11 @@ var initCmd = &cmds.Command{
nBitsForKeypair = nBitsForKeypairDefault
}

return doInit(req.Context().ConfigRoot, dspathOverride, force, nBitsForKeypair)
return doInit(req.Context().ConfigRoot, force, nBitsForKeypair)
},
}

var errCannotInitConfigExists = debugerror.New(`ipfs configuration file already exists!
var errRepoExists = debugerror.New(`ipfs configuration file already exists!
Reinitializing would overwrite your keys.
(use -f to force overwrite)
`)
Expand All @@ -85,29 +78,34 @@ IPFS and are now interfacing with the ipfs merkledag!
For a short demo of what you can do, enter 'ipfs tour'
`

func initWithDefaults(configRoot string) error {
_, err := doInit(configRoot, "", false, nBitsForKeypairDefault)
func initWithDefaults(repoRoot string) error {
_, err := doInit(repoRoot, false, nBitsForKeypairDefault)
return debugerror.Wrap(err)
}

func doInit(configRoot string, dspathOverride string, force bool, nBitsForKeypair int) (interface{}, error) {

u.POut("initializing ipfs node at %s\n", configRoot)
func doInit(repoRoot string, force bool, nBitsForKeypair int) (interface{}, error) {

configFilename, err := config.Filename(configRoot)
if err != nil {
return nil, debugerror.New("Couldn't get home directory path")
}
u.POut("initializing ipfs node at %s\n", repoRoot)

if u.FileExists(configFilename) && !force {
return nil, errCannotInitConfigExists
if fsrepo.IsInitialized(repoRoot) && !force {
return nil, errRepoExists
}

conf, err := initConfig(configFilename, dspathOverride, nBitsForKeypair)
conf, err := initConfig(nBitsForKeypair)
if err != nil {
return nil, err
}

if fsrepo.IsInitialized(repoRoot) {
if err := fsrepo.Remove(repoRoot); err != nil {
return nil, err
}
}
if err := fsrepo.Init(repoRoot, conf); err != nil {
return nil, err
}
if err := repo.ConfigureEventLogger(conf.Logs); err != nil {
return nil, err
}
err = addTheWelcomeFile(conf)
if err != nil {
return nil, err
Expand Down Expand Up @@ -144,28 +142,19 @@ func addTheWelcomeFile(conf *config.Config) error {
return nil
}

func datastoreConfig(dspath string) (config.Datastore, error) {
ds := config.Datastore{}
if len(dspath) == 0 {
var err error
dspath, err = config.DataStorePath("")
if err != nil {
return ds, err
}
}
ds.Path = dspath
ds.Type = "leveldb"

err := initCheckDir(dspath)
func datastoreConfig() (*config.Datastore, error) {
dspath, err := config.DataStorePath("")
if err != nil {
return ds, debugerror.Errorf("datastore: %s", err)
return nil, err
}

return ds, nil
return &config.Datastore{
Path: dspath,
Type: "leveldb",
}, nil
}

func initConfig(configFilename string, dspathOverride string, nBitsForKeypair int) (*config.Config, error) {
ds, err := datastoreConfig(dspathOverride)
func initConfig(nBitsForKeypair int) (*config.Config, error) {
ds, err := datastoreConfig()
if err != nil {
return nil, err
}
Expand All @@ -175,7 +164,7 @@ func initConfig(configFilename string, dspathOverride string, nBitsForKeypair in
return nil, err
}

logConfig, err := initLogs("") // TODO allow user to override dir
logConfig, err := initLogs()
if err != nil {
return nil, err
}
Expand All @@ -198,8 +187,8 @@ func initConfig(configFilename string, dspathOverride string, nBitsForKeypair in
},

Bootstrap: bootstrapPeers,
Datastore: ds,
Logs: logConfig,
Datastore: *ds,
Logs: *logConfig,
Identity: identity,

// setup the node mount points.
Expand All @@ -213,10 +202,6 @@ func initConfig(configFilename string, dspathOverride string, nBitsForKeypair in
Version: config.VersionDefaultValue(),
}

if err := config.WriteConfigFile(configFilename, conf); err != nil {
return nil, err
}

return conf, nil
}

Expand Down Expand Up @@ -252,42 +237,14 @@ func identityConfig(nbits int) (config.Identity, error) {
return ident, nil
}

// initLogs initializes the event logger at the specified path. It uses the
// default log path if no path is provided.
func initLogs(logpath string) (config.Logs, error) {
if len(logpath) == 0 {
var err error
logpath, err = config.LogsPath("")
if err != nil {
return config.Logs{}, debugerror.Wrap(err)
}
}
err := initCheckDir(logpath)
// initLogs initializes the event logger.
func initLogs() (*config.Logs, error) {
logpath, err := config.LogsPath("")
if err != nil {
return config.Logs{}, debugerror.Errorf("logs: %s", err)
return nil, err
}
conf := config.Logs{
Filename: path.Join(logpath, "events.log"),
}
err = repo.ConfigureEventLogger(conf)
if err != nil {
return conf, err
}
return conf, nil
}

// initCheckDir ensures the directory exists and is writable
func initCheckDir(path string) error {
// Construct the path if missing
if err := os.MkdirAll(path, os.ModePerm); err != nil {
return err
}

// Check the directory is writeable
if f, err := os.Create(filepath.Join(path, "._check_writeable")); err == nil {
os.Remove(f.Name())
} else {
return debugerror.New("'" + path + "' is not writeable")
}
return nil
return &conf, nil
}
11 changes: 6 additions & 5 deletions cmd/ipfs/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,11 @@ import (
cmds "github.com/jbenet/go-ipfs/commands"
cmdsCli "github.com/jbenet/go-ipfs/commands/cli"
cmdsHttp "github.com/jbenet/go-ipfs/commands/http"
config "github.com/jbenet/go-ipfs/config"
core "github.com/jbenet/go-ipfs/core"
daemon "github.com/jbenet/go-ipfs/core/daemon"
repo "github.com/jbenet/go-ipfs/repo"
config "github.com/jbenet/go-ipfs/repo/config"
fsrepo "github.com/jbenet/go-ipfs/repo/fsrepo"
updates "github.com/jbenet/go-ipfs/updates"
u "github.com/jbenet/go-ipfs/util"
"github.com/jbenet/go-ipfs/util/debugerror"
Expand Down Expand Up @@ -444,12 +445,12 @@ func getConfigRoot(req cmds.Request) (string, error) {
}

func loadConfig(path string) (*config.Config, error) {
configFile, err := config.Filename(path)
if err != nil {
r := fsrepo.At(path)
if err := r.Open(); err != nil {
return nil, err
}

return config.Load(configFile)
defer r.Close()
return r.Config(), nil
}

// startProfiling begins CPU profiling and returns a `stop` function to be
Expand Down
10 changes: 6 additions & 4 deletions cmd/ipfs/tour.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import (
"os"

cmds "github.com/jbenet/go-ipfs/commands"
config "github.com/jbenet/go-ipfs/config"
config "github.com/jbenet/go-ipfs/repo/config"
fsrepo "github.com/jbenet/go-ipfs/repo/fsrepo"
tour "github.com/jbenet/go-ipfs/tour"
)

Expand Down Expand Up @@ -187,9 +188,10 @@ func tourGet(id tour.ID) (*tour.Topic, error) {

// TODO share func
func writeConfig(path string, cfg *config.Config) error {
filename, err := config.Filename(path)
if err != nil {
r := fsrepo.At(path)
if err := r.Open(); err != nil {
return err
}
return config.WriteConfigFile(filename, cfg)
defer r.Close()
return r.SetConfig(cfg)
}
2 changes: 1 addition & 1 deletion commands/http/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"strings"

cmds "github.com/jbenet/go-ipfs/commands"
config "github.com/jbenet/go-ipfs/config"
config "github.com/jbenet/go-ipfs/repo/config"
)

const (
Expand Down
2 changes: 1 addition & 1 deletion commands/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import (

context "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"

"github.com/jbenet/go-ipfs/config"
"github.com/jbenet/go-ipfs/core"
"github.com/jbenet/go-ipfs/repo/config"
u "github.com/jbenet/go-ipfs/util"
)

Expand Down
Loading

0 comments on commit e5bd30b

Please sign in to comment.