Commit b49a06df authored by Kevin Atkinson's avatar Kevin Atkinson

Minor cleanups.

parent fbc4d441
......@@ -40,9 +40,11 @@ var _ datastore.Datastore = (*Datastore)(nil)
type ShardFunc func(string) string
var DatastoreExists = errors.New("Datastore already exist")
var DatastoreDoesNotExist = errors.New("Datastore directory does not exist")
var ShardingFileMissing = errors.New("SHARDING file not found in datastore")
var (
ErrDatastoreExists = errors.New("Datastore already exist")
ErrDatastoreDoesNotExist = errors.New("Datastore directory does not exist")
ErrShardingFileMissing = errors.New("SHARDING file not found in datastore")
)
const IPFS_DEF_SHARD = "/repo/flatfs/shard/v1/next-to-last/2"
......@@ -60,7 +62,7 @@ func Create(path string, funStr string) error {
dsFun, err := ReadShardFunc(path)
switch err {
case ShardingFileMissing:
case ErrShardingFileMissing:
// fixme: make sure directory is empty and return an error if
// it is not
err := WriteShardFunc(path, fun)
......@@ -74,7 +76,7 @@ func Create(path string, funStr string) error {
return fmt.Errorf("specified shard func '%s' does not match repo shard func '%s'",
fun.String(), dsFun.String())
}
return DatastoreExists
return ErrDatastoreExists
default:
return err
}
......@@ -83,7 +85,7 @@ func Create(path string, funStr string) error {
func Open(path string, sync bool) (*Datastore, error) {
_, err := os.Stat(path)
if os.IsNotExist(err) {
return nil, DatastoreDoesNotExist
return nil, ErrDatastoreDoesNotExist
} else if err != nil {
return nil, err
}
......@@ -93,10 +95,6 @@ func Open(path string, sync bool) (*Datastore, error) {
return nil, err
}
if err != nil {
return nil, fmt.Errorf("unable to parse shard func: %v", err)
}
fs := &Datastore{
path: path,
shardFunc: shardId.String(),
......@@ -109,7 +107,7 @@ func Open(path string, sync bool) (*Datastore, error) {
// convince method, fixme: maybe just call it "New"?
func CreateOrOpen(path string, fun string, sync bool) (*Datastore, error) {
err := Create(path, fun)
if err != nil && err != DatastoreExists {
if err != nil && err != ErrDatastoreExists {
return nil, err
}
return Open(path, sync)
......
......@@ -32,10 +32,11 @@ func ParseShardFunc(str string) (*ShardIdV1, error) {
return nil, fmt.Errorf("empty shard identifier")
}
if str[0] == '/' {
if !strings.HasPrefix(str, PREFIX) {
trimmed := strings.TrimPrefix(str, PREFIX)
if str == trimmed { // nothing trimmed
return nil, fmt.Errorf("invalid prefix in shard identifier: %s", str)
}
str = str[len(PREFIX):]
str = trimmed
}
parts := strings.Split(str, "/")
if len(parts) == 3 {
......@@ -74,7 +75,7 @@ func ParseShardFunc(str string) (*ShardIdV1, error) {
func ReadShardFunc(dir string) (*ShardIdV1, error) {
buf, err := ioutil.ReadFile(filepath.Join(dir, "SHARDING"))
if os.IsNotExist(err) {
return nil, ShardingFileMissing
return nil, ErrShardingFileMissing
} else if err != nil {
return nil, err
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment