diff --git a/convert.go b/convert.go index de9d4d8..3448ae2 100644 --- a/convert.go +++ b/convert.go @@ -28,7 +28,14 @@ func UpgradeV0toV1(path string, prefixLen int) error { } func DowngradeV1toV0(path string) error { - err := os.Remove(filepath.Join(path, SHARDING_FN)) + fun, err := ReadShardFunc(path) + if err != nil { + return err + } else if fun.funName != "prefix" { + return fmt.Errorf("%s: can only downgrade datastore that use the 'prefix' sharding function", path) + } + + err = os.Remove(filepath.Join(path, SHARDING_FN)) if err != nil { return err } diff --git a/convert_test.go b/convert_test.go index f41d220..d8d4e4e 100644 --- a/convert_test.go +++ b/convert_test.go @@ -96,7 +96,7 @@ func TestMoveRestart(t *testing.T) { // there should be nothing left in the new datastore rmEmptyDatastore(t, v2dir) - + // try the move again, again should fail createDatastore(t, v2dir, flatfs.NextToLast(2)) err = flatfs.Move(v1dir, v2dir, nil) @@ -139,12 +139,12 @@ func TestUpgradeDownload(t *testing.T) { keys, blocks := populateDatastore(t, tempdir) checkKeys(t, tempdir, keys, blocks) - //err := flatfs.UpgradeV0toV1(tempdir, 3) - //if err == nil { - // t.Fatalf("UpgradeV0toV1 on already v1 should fail.") - //} + err := flatfs.UpgradeV0toV1(tempdir, 3) + if err == nil { + t.Fatalf("UpgradeV0toV1 on already v1 should fail.") + } - err := flatfs.DowngradeV1toV0(tempdir) + err = flatfs.DowngradeV1toV0(tempdir) if err != nil { t.Fatalf("DowngradeV1toV0 fail: %v\n", err) } @@ -165,6 +165,18 @@ func TestUpgradeDownload(t *testing.T) { checkKeys(t, tempdir, keys, blocks) } +func TestDownloadNonPrefix(t *testing.T) { + tempdir, cleanup := tempdir(t) + defer cleanup() + + createDatastore(t, tempdir, flatfs.NextToLast(2)) + + err := flatfs.DowngradeV1toV0(tempdir) + if err == nil { + t.Fatalf("DowngradeV1toV0 should have failed", err) + } +} + func createDatastore(t *testing.T, dir string, fun *flatfs.ShardIdV1) { err := flatfs.Create(dir, fun) if err != nil { diff --git a/shard.go b/shard.go index 8ee4729..acfa450 100644 --- a/shard.go +++ b/shard.go @@ -121,7 +121,7 @@ func ReadShardFunc(dir string) (*ShardIdV1, error) { } func WriteShardFunc(dir string, id *ShardIdV1) error { - file, err := os.Create(filepath.Join(dir, SHARDING_FN)) + file, err := os.OpenFile(filepath.Join(dir, SHARDING_FN), os.O_WRONLY|os.O_CREATE|os.O_EXCL, 0666) if err != nil { return err }