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-unixfs
Commits
9f67ede6
Commit
9f67ede6
authored
Jan 14, 2015
by
Brian Tiger Chow
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
refactor(fsrepo) extract component.Component
parent
f37646bf
Changes
5
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
101 additions
and
81 deletions
+101
-81
repo/fsrepo/component/component.go
repo/fsrepo/component/component.go
+14
-0
repo/fsrepo/component/config.go
repo/fsrepo/component/config.go
+32
-34
repo/fsrepo/fsrepo.go
repo/fsrepo/fsrepo.go
+46
-38
repo/fsrepo/serialize/serialize.go
repo/fsrepo/serialize/serialize.go
+7
-7
repo/fsrepo/serialize/serialize_test.go
repo/fsrepo/serialize/serialize_test.go
+2
-2
No files found.
repo/fsrepo/component/component.go
0 → 100644
View file @
9f67ede6
package
component
import
(
"io"
"github.com/jbenet/go-ipfs/repo/config"
)
type
Component
interface
{
Open
()
error
io
.
Closer
}
type
Initializer
func
(
path
string
,
conf
*
config
.
Config
)
error
type
InitializationChecker
func
(
path
string
)
bool
repo/fsrepo/co
nfig_co
mponent.go
→
repo/fsrepo/component
/config
.go
View file @
9f67ede6
package
fsrepo
package
component
import
(
common
"github.com/jbenet/go-ipfs/repo/common"
config
"github.com/jbenet/go-ipfs/repo/config"
serialize
"github.com/jbenet/go-ipfs/repo/fsrepo/serialize"
util
"github.com/jbenet/go-ipfs/util"
)
var
_
component
=
&
configComponent
{}
var
_
componentInitializationChecker
=
configComponentIsInitialized
var
_
Component
=
&
ConfigComponent
{}
var
_
Initializer
=
InitConfigComponent
var
_
InitializationChecker
=
ConfigComponentIsInitialized
//
c
onfigComponent abstracts the config component of the FSRepo.
//
C
onfigComponent abstracts the config component of the FSRepo.
// NB: create with makeConfigComponent function.
type
configComponent
struct
{
path
string
// required at instantiation
// NOT THREAD-SAFE
type
ConfigComponent
struct
{
Path
string
// required at instantiation
config
*
config
.
Config
// assigned on Open()
}
// makeConfigComponent instantiates a valid configComponent.
func
makeConfigComponent
(
path
string
)
configComponent
{
return
configComponent
{
path
:
path
}
}
// fsrepoConfigInit initializes the FSRepo's configComponent.
func
initConfigComponent
(
path
string
,
conf
*
config
.
Config
)
error
{
if
configComponentIsInitialized
(
path
)
{
// fsrepoConfigInit initializes the FSRepo's ConfigComponent.
func
InitConfigComponent
(
path
string
,
conf
*
config
.
Config
)
error
{
if
ConfigComponentIsInitialized
(
path
)
{
return
nil
}
configFilename
,
err
:=
config
.
Filename
(
path
)
...
...
@@ -33,19 +31,19 @@ func initConfigComponent(path string, conf *config.Config) error {
// initialization is the one time when it's okay to write to the config
// without reading the config from disk and merging any user-provided keys
// that may exist.
if
err
:=
w
riteConfigFile
(
configFilename
,
conf
);
err
!=
nil
{
if
err
:=
serialize
.
W
riteConfigFile
(
configFilename
,
conf
);
err
!=
nil
{
return
err
}
return
nil
}
// Open returns an error if the config file is not present.
func
(
c
*
c
onfigComponent
)
Open
()
error
{
configFilename
,
err
:=
config
.
Filename
(
c
.
p
ath
)
func
(
c
*
C
onfigComponent
)
Open
()
error
{
configFilename
,
err
:=
config
.
Filename
(
c
.
P
ath
)
if
err
!=
nil
{
return
err
}
conf
,
err
:=
l
oad
(
configFilename
)
conf
,
err
:=
serialize
.
L
oad
(
configFilename
)
if
err
!=
nil
{
return
err
}
...
...
@@ -54,46 +52,46 @@ func (c *configComponent) Open() error {
}
// Close satisfies the fsrepoComponent interface.
func
(
c
*
c
onfigComponent
)
Close
()
error
{
func
(
c
*
C
onfigComponent
)
Close
()
error
{
return
nil
// config doesn't need to be closed.
}
func
(
c
*
c
onfigComponent
)
Config
()
*
config
.
Config
{
func
(
c
*
C
onfigComponent
)
Config
()
*
config
.
Config
{
return
c
.
config
}
// SetConfig updates the config file.
func
(
c
*
c
onfigComponent
)
SetConfig
(
updated
*
config
.
Config
)
error
{
func
(
c
*
C
onfigComponent
)
SetConfig
(
updated
*
config
.
Config
)
error
{
return
c
.
setConfigUnsynced
(
updated
)
}
// GetConfigKey retrieves only the value of a particular key.
func
(
c
*
c
onfigComponent
)
GetConfigKey
(
key
string
)
(
interface
{},
error
)
{
filename
,
err
:=
config
.
Filename
(
c
.
p
ath
)
func
(
c
*
C
onfigComponent
)
GetConfigKey
(
key
string
)
(
interface
{},
error
)
{
filename
,
err
:=
config
.
Filename
(
c
.
P
ath
)
if
err
!=
nil
{
return
nil
,
err
}
var
cfg
map
[
string
]
interface
{}
if
err
:=
r
eadConfigFile
(
filename
,
&
cfg
);
err
!=
nil
{
if
err
:=
serialize
.
R
eadConfigFile
(
filename
,
&
cfg
);
err
!=
nil
{
return
nil
,
err
}
return
common
.
MapGetKV
(
cfg
,
key
)
}
// SetConfigKey writes the value of a particular key.
func
(
c
*
c
onfigComponent
)
SetConfigKey
(
key
string
,
value
interface
{})
error
{
filename
,
err
:=
config
.
Filename
(
c
.
p
ath
)
func
(
c
*
C
onfigComponent
)
SetConfigKey
(
key
string
,
value
interface
{})
error
{
filename
,
err
:=
config
.
Filename
(
c
.
P
ath
)
if
err
!=
nil
{
return
err
}
var
mapconf
map
[
string
]
interface
{}
if
err
:=
r
eadConfigFile
(
filename
,
&
mapconf
);
err
!=
nil
{
if
err
:=
serialize
.
R
eadConfigFile
(
filename
,
&
mapconf
);
err
!=
nil
{
return
err
}
if
err
:=
common
.
MapSetKV
(
mapconf
,
key
,
value
);
err
!=
nil
{
return
err
}
if
err
:=
w
riteConfigFile
(
filename
,
mapconf
);
err
!=
nil
{
if
err
:=
serialize
.
W
riteConfigFile
(
filename
,
mapconf
);
err
!=
nil
{
return
err
}
// in order to get the updated values, read updated config from the
...
...
@@ -105,9 +103,9 @@ func (c *configComponent) SetConfigKey(key string, value interface{}) error {
return
c
.
setConfigUnsynced
(
conf
)
// TODO roll this into this method
}
//
c
onfigComponentIsInitialized returns true if the repo is initialized at
//
C
onfigComponentIsInitialized returns true if the repo is initialized at
// provided |path|.
func
c
onfigComponentIsInitialized
(
path
string
)
bool
{
func
C
onfigComponentIsInitialized
(
path
string
)
bool
{
configFilename
,
err
:=
config
.
Filename
(
path
)
if
err
!=
nil
{
return
false
...
...
@@ -119,8 +117,8 @@ func configComponentIsInitialized(path string) bool {
}
// setConfigUnsynced is for private use.
func
(
r
*
c
onfigComponent
)
setConfigUnsynced
(
updated
*
config
.
Config
)
error
{
configFilename
,
err
:=
config
.
Filename
(
r
.
p
ath
)
func
(
r
*
C
onfigComponent
)
setConfigUnsynced
(
updated
*
config
.
Config
)
error
{
configFilename
,
err
:=
config
.
Filename
(
r
.
P
ath
)
if
err
!=
nil
{
return
err
}
...
...
@@ -128,7 +126,7 @@ func (r *configComponent) setConfigUnsynced(updated *config.Config) error {
// as a map, write the updated struct values to the map and write the map
// to disk.
var
mapconf
map
[
string
]
interface
{}
if
err
:=
r
eadConfigFile
(
configFilename
,
&
mapconf
);
err
!=
nil
{
if
err
:=
serialize
.
R
eadConfigFile
(
configFilename
,
&
mapconf
);
err
!=
nil
{
return
err
}
m
,
err
:=
config
.
ToMap
(
updated
)
...
...
@@ -138,7 +136,7 @@ func (r *configComponent) setConfigUnsynced(updated *config.Config) error {
for
k
,
v
:=
range
m
{
mapconf
[
k
]
=
v
}
if
err
:=
w
riteConfigFile
(
configFilename
,
mapconf
);
err
!=
nil
{
if
err
:=
serialize
.
W
riteConfigFile
(
configFilename
,
mapconf
);
err
!=
nil
{
return
err
}
*
r
.
config
=
*
updated
// copy so caller cannot modify this private config
...
...
repo/fsrepo/fsrepo.go
View file @
9f67ede6
...
...
@@ -11,8 +11,10 @@ import (
repo
"github.com/jbenet/go-ipfs/repo"
config
"github.com/jbenet/go-ipfs/repo/config"
component
"github.com/jbenet/go-ipfs/repo/fsrepo/component"
lockfile
"github.com/jbenet/go-ipfs/repo/fsrepo/lock"
opener
"github.com/jbenet/go-ipfs/repo/fsrepo/opener"
serialize
"github.com/jbenet/go-ipfs/repo/fsrepo/serialize"
debugerror
"github.com/jbenet/go-ipfs/util/debugerror"
)
...
...
@@ -49,21 +51,20 @@ type FSRepo struct {
// config is loaded when FSRepo is opened and kept up to date when the
// FSRepo is modified.
// TODO test
configComponent
configComponent
configComponent
co
mponent
.
Co
nfigComponent
}
type
component
interface
{
Open
()
error
io
.
Closer
type
componentBuilder
struct
{
Init
component
.
Initializer
IsInitialized
component
.
InitializationChecker
OpenHandler
func
(
*
FSRepo
)
error
}
type
componentInitializationChecker
func
(
path
string
)
bool
// At returns a handle to an FSRepo at the provided |path|.
func
At
(
repoPath
string
)
*
FSRepo
{
// This method must not have side-effects.
return
&
FSRepo
{
path
:
path
.
Clean
(
repoPath
),
configComponent
:
makeConfigComponent
(
repoPath
),
state
:
unopened
,
// explicitly set for clarity
}
}
...
...
@@ -78,7 +79,7 @@ func ConfigAt(repoPath string) (*config.Config, error) {
if
err
!=
nil
{
return
nil
,
err
}
return
l
oad
(
configFilename
)
return
serialize
.
L
oad
(
configFilename
)
}
// Init initializes a new FSRepo at the given path with the provided config.
...
...
@@ -93,10 +94,11 @@ func Init(path string, conf *config.Config) error {
if
isInitializedUnsynced
(
path
)
{
return
nil
}
if
err
:=
initConfigComponent
(
path
,
conf
);
err
!=
nil
{
for
_
,
b
:=
range
componentBuilders
()
{
if
err
:=
b
.
Init
(
path
,
conf
);
err
!=
nil
{
return
err
}
}
return
nil
}
...
...
@@ -150,19 +152,10 @@ func (r *FSRepo) Open() error {
return
err
}
for
_
,
opener
:=
range
r
.
components
()
{
if
err
:=
opener
.
Open
();
err
!=
nil
{
return
err
}
}
// datastore
dspath
,
err
:=
config
.
DataStorePath
(
""
)
if
err
!=
nil
{
for
_
,
b
:=
range
componentBuilders
()
{
if
err
:=
b
.
OpenHandler
(
r
);
err
!=
nil
{
return
err
}
if
err
:=
initCheckDir
(
dspath
);
err
!=
nil
{
return
debugerror
.
Errorf
(
"datastore: %s"
,
err
)
}
logpath
,
err
:=
config
.
LogsPath
(
""
)
...
...
@@ -255,18 +248,7 @@ func IsInitialized(path string) bool {
packageLock
.
Lock
()
defer
packageLock
.
Unlock
()
// componentInitCheckers are functions that indicate whether the component
// is isInitialized
var
componentInitCheckers
=
[]
componentInitializationChecker
{
configComponentIsInitialized
,
// TODO add datastore component initialization checker
}
for
_
,
isInitialized
:=
range
componentInitCheckers
{
if
!
isInitialized
(
path
)
{
return
false
}
}
return
true
return
isInitializedUnsynced
(
path
)
}
// private methods below this point. NB: packageLock must held by caller.
...
...
@@ -274,7 +256,12 @@ func IsInitialized(path string) bool {
// isInitializedUnsynced reports whether the repo is initialized. Caller must
// hold openerCounter lock.
func
isInitializedUnsynced
(
path
string
)
bool
{
return
configComponentIsInitialized
(
path
)
for
_
,
b
:=
range
componentBuilders
()
{
if
!
b
.
IsInitialized
(
path
)
{
return
false
}
}
return
true
}
// initCheckDir ensures the directory exists and is writable
...
...
@@ -327,9 +314,30 @@ func transitionToClosed(r *FSRepo) error {
}
// components returns the FSRepo's constituent components
func
(
r
*
FSRepo
)
components
()
[]
component
{
return
[]
component
{
func
(
r
*
FSRepo
)
components
()
[]
component
.
Component
{
return
[]
component
.
Component
{
&
r
.
configComponent
,
// TODO add datastore
}
}
func
componentBuilders
()
[]
componentBuilder
{
return
[]
componentBuilder
{
// ConfigComponent
componentBuilder
{
Init
:
component
.
InitConfigComponent
,
IsInitialized
:
component
.
ConfigComponentIsInitialized
,
OpenHandler
:
func
(
r
*
FSRepo
)
error
{
cc
:=
component
.
ConfigComponent
{
Path
:
r
.
path
}
if
err
:=
cc
.
Open
();
err
!=
nil
{
return
err
}
r
.
configComponent
=
cc
return
nil
},
},
// TODO add datastore builder
}
}
repo/fsrepo/serialize.go
→
repo/fsrepo/serialize
/serialize
.go
View file @
9f67ede6
...
...
@@ -15,8 +15,8 @@ import (
var
log
=
util
.
Logger
(
"fsrepo"
)
//
r
eadConfigFile reads the config from `filename` into `cfg`.
func
r
eadConfigFile
(
filename
string
,
cfg
interface
{})
error
{
//
R
eadConfigFile reads the config from `filename` into `cfg`.
func
R
eadConfigFile
(
filename
string
,
cfg
interface
{})
error
{
f
,
err
:=
os
.
Open
(
filename
)
if
err
!=
nil
{
return
err
...
...
@@ -28,8 +28,8 @@ func readConfigFile(filename string, cfg interface{}) error {
return
nil
}
//
w
riteConfigFile writes the config from `cfg` into `filename`.
func
w
riteConfigFile
(
filename
string
,
cfg
interface
{})
error
{
//
W
riteConfigFile writes the config from `cfg` into `filename`.
func
W
riteConfigFile
(
filename
string
,
cfg
interface
{})
error
{
err
:=
os
.
MkdirAll
(
filepath
.
Dir
(
filename
),
0775
)
if
err
!=
nil
{
return
err
...
...
@@ -55,15 +55,15 @@ func encode(w io.Writer, value interface{}) error {
return
err
}
//
l
oad reads given file and returns the read config, or error.
func
l
oad
(
filename
string
)
(
*
config
.
Config
,
error
)
{
//
L
oad reads given file and returns the read config, or error.
func
L
oad
(
filename
string
)
(
*
config
.
Config
,
error
)
{
// if nothing is there, fail. User must run 'ipfs init'
if
!
util
.
FileExists
(
filename
)
{
return
nil
,
debugerror
.
New
(
"ipfs not initialized, please run 'ipfs init'"
)
}
var
cfg
config
.
Config
err
:=
r
eadConfigFile
(
filename
,
&
cfg
)
err
:=
R
eadConfigFile
(
filename
,
&
cfg
)
if
err
!=
nil
{
return
nil
,
err
}
...
...
repo/fsrepo/serialize_test.go
→
repo/fsrepo/serialize
/serialize
_test.go
View file @
9f67ede6
...
...
@@ -11,11 +11,11 @@ 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
)
}
cfgRead
,
err
:=
l
oad
(
filename
)
cfgRead
,
err
:=
L
oad
(
filename
)
if
err
!=
nil
{
t
.
Error
(
err
)
return
...
...
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