Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
What's new
10
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Open sidebar
dms3
go-dms3
Commits
1b700864
Commit
1b700864
authored
Jan 12, 2015
by
Brian Tiger Chow
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
refactor(repo): all config writes must go through repo
parent
405afd2a
Changes
8
Hide whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
132 additions
and
55 deletions
+132
-55
cmd/ipfs/init.go
cmd/ipfs/init.go
+21
-14
cmd/ipfs/tour.go
cmd/ipfs/tour.go
+5
-4
core/commands/bootstrap.go
core/commands/bootstrap.go
+18
-26
repo/fsrepo/fsrepo.go
repo/fsrepo/fsrepo.go
+54
-6
repo/fsrepo/serialize.go
repo/fsrepo/serialize.go
+3
-3
repo/fsrepo/serialize_test.go
repo/fsrepo/serialize_test.go
+1
-1
repo/fsrepo/state.go
repo/fsrepo/state.go
+22
-0
repo/repo.go
repo/repo.go
+8
-1
No files found.
cmd/ipfs/init.go
View file @
1b700864
...
...
@@ -78,16 +78,16 @@ IPFS and are now interfacing with the ipfs merkledag!
For a short demo of what you can do, enter 'ipfs tour'
`
func
initWithDefaults
(
config
Root
string
)
error
{
_
,
err
:=
doInit
(
config
Root
,
false
,
nBitsForKeypairDefault
)
func
initWithDefaults
(
repo
Root
string
)
error
{
_
,
err
:=
doInit
(
repo
Root
,
false
,
nBitsForKeypairDefault
)
return
debugerror
.
Wrap
(
err
)
}
func
doInit
(
config
Root
string
,
force
bool
,
nBitsForKeypair
int
)
(
interface
{},
error
)
{
func
doInit
(
repo
Root
string
,
force
bool
,
nBitsForKeypair
int
)
(
interface
{},
error
)
{
u
.
POut
(
"initializing ipfs node at %s
\n
"
,
config
Root
)
u
.
POut
(
"initializing ipfs node at %s
\n
"
,
repo
Root
)
if
fsrepo
.
Config
IsInitialized
(
config
Root
)
&&
!
force
{
if
fsrepo
.
IsInitialized
(
repo
Root
)
&&
!
force
{
return
nil
,
errCannotInitConfigExists
}
...
...
@@ -96,16 +96,23 @@ func doInit(configRoot string, force bool, nBitsForKeypair int) (interface{}, er
return
nil
,
err
}
r
:=
fsrepo
.
At
(
configRoot
)
if
err
:=
r
.
Open
();
err
!=
nil
{
return
nil
,
err
}
if
err
:=
r
.
SetConfig
(
conf
);
err
!=
nil
{
return
nil
,
err
}
if
err
:=
r
.
Close
();
err
!=
nil
{
return
nil
,
err
if
!
fsrepo
.
IsInitialized
(
repoRoot
)
{
if
err
:=
fsrepo
.
Init
(
repoRoot
,
conf
);
err
!=
nil
{
return
nil
,
err
}
}
else
{
r
:=
fsrepo
.
At
(
repoRoot
)
if
err
:=
r
.
Open
();
err
!=
nil
{
return
nil
,
err
}
if
err
:=
r
.
SetConfig
(
conf
);
err
!=
nil
{
return
nil
,
err
}
if
err
:=
r
.
Close
();
err
!=
nil
{
return
nil
,
err
}
}
if
err
:=
repo
.
ConfigureEventLogger
(
conf
.
Logs
);
err
!=
nil
{
return
nil
,
err
}
...
...
cmd/ipfs/tour.go
View file @
1b700864
...
...
@@ -9,8 +9,8 @@ import (
cmds
"github.com/jbenet/go-ipfs/commands"
config
"github.com/jbenet/go-ipfs/repo/config"
tour
"github.com/jbenet/go-ipfs/tour"
fsrepo
"github.com/jbenet/go-ipfs/repo/fsrepo"
tour
"github.com/jbenet/go-ipfs/tour"
)
var
tourCmd
=
&
cmds
.
Command
{
...
...
@@ -188,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
fsrepo
.
WriteConfigFile
(
filename
,
cfg
)
defer
r
.
Close
()
return
r
.
SetConfig
(
cfg
)
}
core/commands/bootstrap.go
View file @
1b700864
...
...
@@ -5,6 +5,7 @@ import (
"io"
cmds
"github.com/jbenet/go-ipfs/commands"
repo
"github.com/jbenet/go-ipfs/repo"
config
"github.com/jbenet/go-ipfs/repo/config"
"github.com/jbenet/go-ipfs/repo/fsrepo"
u
"github.com/jbenet/go-ipfs/util"
...
...
@@ -77,15 +78,12 @@ in the bootstrap list).
return
nil
,
err
}
filename
,
err
:=
config
.
Filename
(
req
.
Context
()
.
ConfigRoot
)
if
err
!=
nil
{
return
nil
,
err
}
cfg
,
err
:=
req
.
Context
()
.
GetConfig
()
if
err
!=
nil
{
r
:=
fsrepo
.
At
(
req
.
Context
()
.
ConfigRoot
)
if
err
:=
r
.
Open
();
err
!=
nil
{
return
nil
,
err
}
defer
r
.
Close
()
cfg
:=
r
.
Config
()
deflt
,
_
,
err
:=
req
.
Option
(
"default"
)
.
Bool
()
if
err
!=
nil
{
...
...
@@ -102,7 +100,7 @@ in the bootstrap list).
inputPeers
=
append
(
inputPeers
,
defltPeers
...
)
}
added
,
err
:=
bootstrapAdd
(
filename
,
cfg
,
inputPeers
)
added
,
err
:=
bootstrapAdd
(
r
,
cfg
,
inputPeers
)
if
err
!=
nil
{
return
nil
,
err
}
...
...
@@ -147,15 +145,12 @@ var bootstrapRemoveCmd = &cmds.Command{
return
nil
,
err
}
filename
,
err
:=
config
.
Filename
(
req
.
Context
()
.
ConfigRoot
)
if
err
!=
nil
{
return
nil
,
err
}
cfg
,
err
:=
req
.
Context
()
.
GetConfig
()
if
err
!=
nil
{
r
:=
fsrepo
.
At
(
req
.
Context
()
.
ConfigRoot
)
if
err
:=
r
.
Open
();
err
!=
nil
{
return
nil
,
err
}
defer
r
.
Close
()
cfg
:=
r
.
Config
()
all
,
_
,
err
:=
req
.
Option
(
"all"
)
.
Bool
()
if
err
!=
nil
{
...
...
@@ -164,9 +159,9 @@ var bootstrapRemoveCmd = &cmds.Command{
var
removed
[]
config
.
BootstrapPeer
if
all
{
removed
,
err
=
bootstrapRemoveAll
(
filename
,
cfg
)
removed
,
err
=
bootstrapRemoveAll
(
r
,
cfg
)
}
else
{
removed
,
err
=
bootstrapRemove
(
filename
,
cfg
,
input
)
removed
,
err
=
bootstrapRemove
(
r
,
cfg
,
input
)
}
if
err
!=
nil
{
return
nil
,
err
...
...
@@ -233,7 +228,7 @@ func bootstrapWritePeers(w io.Writer, prefix string, peers []config.BootstrapPee
return
nil
}
func
bootstrapAdd
(
filename
string
,
cfg
*
config
.
Config
,
peers
[]
config
.
BootstrapPeer
)
([]
config
.
BootstrapPeer
,
error
)
{
func
bootstrapAdd
(
r
repo
.
Interface
,
cfg
*
config
.
Config
,
peers
[]
config
.
BootstrapPeer
)
([]
config
.
BootstrapPeer
,
error
)
{
added
:=
make
([]
config
.
BootstrapPeer
,
0
,
len
(
peers
))
for
_
,
peer
:=
range
peers
{
...
...
@@ -251,15 +246,14 @@ func bootstrapAdd(filename string, cfg *config.Config, peers []config.BootstrapP
}
}
err
:=
fsrepo
.
WriteConfigFile
(
filename
,
cfg
)
if
err
!=
nil
{
if
err
:=
r
.
SetConfig
(
cfg
);
err
!=
nil
{
return
nil
,
err
}
return
added
,
nil
}
func
bootstrapRemove
(
filename
string
,
cfg
*
config
.
Config
,
toRemove
[]
config
.
BootstrapPeer
)
([]
config
.
BootstrapPeer
,
error
)
{
func
bootstrapRemove
(
r
repo
.
Interface
,
cfg
*
config
.
Config
,
toRemove
[]
config
.
BootstrapPeer
)
([]
config
.
BootstrapPeer
,
error
)
{
removed
:=
make
([]
config
.
BootstrapPeer
,
0
,
len
(
toRemove
))
keep
:=
make
([]
config
.
BootstrapPeer
,
0
,
len
(
cfg
.
Bootstrap
))
...
...
@@ -279,21 +273,19 @@ func bootstrapRemove(filename string, cfg *config.Config, toRemove []config.Boot
}
cfg
.
Bootstrap
=
keep
err
:=
fsrepo
.
WriteConfigFile
(
filename
,
cfg
)
if
err
!=
nil
{
if
err
:=
r
.
SetConfig
(
cfg
);
err
!=
nil
{
return
nil
,
err
}
return
removed
,
nil
}
func
bootstrapRemoveAll
(
filename
string
,
cfg
*
config
.
Config
)
([]
config
.
BootstrapPeer
,
error
)
{
func
bootstrapRemoveAll
(
r
repo
.
Interface
,
cfg
*
config
.
Config
)
([]
config
.
BootstrapPeer
,
error
)
{
removed
:=
make
([]
config
.
BootstrapPeer
,
len
(
cfg
.
Bootstrap
))
copy
(
removed
,
cfg
.
Bootstrap
)
cfg
.
Bootstrap
=
nil
err
:=
fsrepo
.
WriteConfigFile
(
filename
,
cfg
)
if
err
!=
nil
{
if
err
:=
r
.
SetConfig
(
cfg
);
err
!=
nil
{
return
nil
,
err
}
...
...
repo/fsrepo/fsrepo.go
View file @
1b700864
package
fsrepo
import
(
"fmt"
"io"
"os"
"path/filepath"
...
...
@@ -11,17 +12,40 @@ import (
)
type
FSRepo
struct
{
state
state
path
string
config
config
.
Config
config
*
config
.
Config
}
func
At
(
path
string
)
*
FSRepo
{
return
&
FSRepo
{
path
:
path
,
path
:
path
,
state
:
unopened
,
// explicitly set for clarity
}
}
func
Init
(
path
string
,
conf
*
config
.
Config
)
error
{
if
IsInitialized
(
path
)
{
return
nil
}
configFilename
,
err
:=
config
.
Filename
(
path
)
if
err
!=
nil
{
return
err
}
if
err
:=
writeConfigFile
(
configFilename
,
conf
);
err
!=
nil
{
return
err
}
return
nil
}
// Open returns an error if the repo is not initialized.
func
(
r
*
FSRepo
)
Open
()
error
{
if
r
.
state
!=
unopened
{
return
debugerror
.
Errorf
(
"repo is %s"
,
r
.
state
)
}
if
!
IsInitialized
(
r
.
path
)
{
return
debugerror
.
New
(
"repo is not initialized"
)
}
// check repo path, then check all constituent parts.
// TODO acquire repo lock
// TODO if err := initCheckDir(logpath); err != nil { // }
...
...
@@ -29,6 +53,16 @@ func (r *FSRepo) Open() error {
return
err
}
configFilename
,
err
:=
config
.
Filename
(
r
.
path
)
if
err
!=
nil
{
return
err
}
conf
,
err
:=
Load
(
configFilename
)
if
err
!=
nil
{
return
err
}
r
.
config
=
conf
// datastore
dspath
,
err
:=
config
.
DataStorePath
(
""
)
if
err
!=
nil
{
...
...
@@ -46,29 +80,43 @@ func (r *FSRepo) Open() error {
return
debugerror
.
Errorf
(
"logs: %s"
,
err
)
}
r
.
state
=
opened
return
nil
}
func
(
r
*
FSRepo
)
Config
()
*
config
.
Config
{
if
r
.
state
!=
opened
{
panic
(
fmt
.
Sprintln
(
"repo is"
,
r
.
state
))
}
return
r
.
config
}
func
(
r
*
FSRepo
)
SetConfig
(
conf
*
config
.
Config
)
error
{
if
r
.
state
!=
opened
{
panic
(
fmt
.
Sprintln
(
"repo is"
,
r
.
state
))
}
configFilename
,
err
:=
config
.
Filename
(
r
.
path
)
if
err
!=
nil
{
return
err
}
if
err
:=
W
riteConfigFile
(
configFilename
,
conf
);
err
!=
nil
{
if
err
:=
w
riteConfigFile
(
configFilename
,
conf
);
err
!=
nil
{
return
err
}
r
.
config
=
*
conf
// copy so caller cannot modify the private config
*
r
.
config
=
*
conf
// copy so caller cannot modify the private config
return
nil
}
func
(
r
*
FSRepo
)
Close
()
error
{
if
r
.
state
!=
opened
{
return
debugerror
.
Errorf
(
"repo is %s"
,
r
.
state
)
}
return
nil
// TODO release repo lock
}
var
_
io
.
Closer
=
&
FSRepo
{}
//
Config
IsInitialized returns true if the
config exists in
provided |path|.
func
Config
IsInitialized
(
path
string
)
bool
{
// IsInitialized returns true if the
repo is initialized at
provided |path|.
func
IsInitialized
(
path
string
)
bool
{
configFilename
,
err
:=
config
.
Filename
(
path
)
if
err
!=
nil
{
return
false
...
...
repo/fsrepo/serialize.go
View file @
1b700864
...
...
@@ -30,7 +30,7 @@ func ReadConfigFile(filename string, cfg interface{}) error {
}
// WriteConfigFile writes the config from `cfg` into `filename`.
func
W
riteConfigFile
(
filename
string
,
cfg
interface
{})
error
{
func
w
riteConfigFile
(
filename
string
,
cfg
interface
{})
error
{
err
:=
os
.
MkdirAll
(
filepath
.
Dir
(
filename
),
0775
)
if
err
!=
nil
{
return
err
...
...
@@ -125,7 +125,7 @@ func WriteConfigKey(filename, key string, value interface{}) error {
}
}
return
W
riteConfigFile
(
filename
,
cfg
)
return
w
riteConfigFile
(
filename
,
cfg
)
}
// Load reads given file and returns the read config, or error.
...
...
@@ -165,5 +165,5 @@ func RecordUpdateCheck(cfg *config.Config, filename string) {
log
.
Error
(
"config.Version.CheckPeriod not set. config broken?"
)
}
W
riteConfigFile
(
filename
,
cfg
)
w
riteConfigFile
(
filename
,
cfg
)
}
repo/fsrepo/serialize_test.go
View file @
1b700864
...
...
@@ -11,7 +11,7 @@ func TestConfig(t *testing.T) {
const
dsPath
=
"/path/to/datastore"
cfgWritten
:=
new
(
config
.
Config
)
cfgWritten
.
Datastore
.
Path
=
dsPath
err
:=
W
riteConfigFile
(
filename
,
cfgWritten
)
err
:=
w
riteConfigFile
(
filename
,
cfgWritten
)
if
err
!=
nil
{
t
.
Error
(
err
)
}
...
...
repo/fsrepo/state.go
0 → 100644
View file @
1b700864
package
fsrepo
type
state
int
const
(
unopened
=
iota
opened
closed
)
func
(
s
state
)
String
()
string
{
switch
s
{
case
unopened
:
return
"unopened"
case
opened
:
return
"opened"
case
closed
:
return
"closed"
default
:
return
"invalid"
}
}
repo/repo.go
View file @
1b700864
package
repo
import
util
"github.com/jbenet/go-ipfs/util"
import
(
config
"github.com/jbenet/go-ipfs/repo/config"
util
"github.com/jbenet/go-ipfs/util"
)
type
Interface
interface
{
SetConfig
(
*
config
.
Config
)
error
}
// IsInitialized returns true if the path is home to an initialized IPFS
// repository.
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment