diff --git a/node/repo/fsrepo.go b/node/repo/fsrepo.go index cfcb52f8d..0e4ad472c 100644 --- a/node/repo/fsrepo.go +++ b/node/repo/fsrepo.go @@ -32,6 +32,12 @@ type FsRepo struct { var _ Repo = &FsRepo{} +func NewFS(path string) (*FsRepo, error) { + return &FsRepo{ + path: path, + }, nil +} + // APIEndpoint returns endpoint of API in this repo func (fsr *FsRepo) APIEndpoint() (multiaddr.Multiaddr, error) { p := filepath.Join(fsr.path, fsAPI) @@ -60,6 +66,14 @@ func (fsr *FsRepo) APIEndpoint() (multiaddr.Multiaddr, error) { // Lock acquires exclusive lock on this repo func (fsr *FsRepo) Lock() (LockedRepo, error) { + locked, err := fslock.Locked(fsr.path, fsLock) + if err != nil { + return nil, xerrors.Errorf("could not check lock status: %w", err) + } + if locked { + return nil, ErrRepoAlreadyLocked + } + closer, err := fslock.Lock(fsr.path, fsLock) if err != nil { return nil, xerrors.Errorf("could not lock the repo: %w", err) @@ -76,7 +90,13 @@ type fsLockedRepo struct { } func (fsr *fsLockedRepo) Close() error { - err := fsr.closer.Close() + err := os.Remove(fsr.join(fsAPI)) + + if err != nil && !os.IsNotExist(err) { + return xerrors.Errorf("could not remove API file: %w", err) + } + + err = fsr.closer.Close() fsr.closer = nil return err } @@ -150,7 +170,7 @@ func (fsr *fsLockedRepo) SetAPIEndpoint(ma multiaddr.Multiaddr) error { if err := fsr.stillValid(); err != nil { return err } - return ioutil.WriteFile(fsr.join(fsAPI), ma.Bytes(), 0666) + return ioutil.WriteFile(fsr.join(fsAPI), []byte(ma.String()), 0666) } func (fsr *fsLockedRepo) Wallet() (interface{}, error) { diff --git a/node/repo/fsrepo_test.go b/node/repo/fsrepo_test.go new file mode 100644 index 000000000..0275debdd --- /dev/null +++ b/node/repo/fsrepo_test.go @@ -0,0 +1,26 @@ +package repo + +import "io/ioutil" +import "os" +import "testing" + +func genFsRepo(t *testing.T) (*FsRepo, func()) { + path, err := ioutil.TempDir("", "lotus-repo-*") + if err != nil { + t.Fatal(err) + } + + repo, err := NewFS(path) + if err != nil { + t.Fatal(err) + } + return repo, func() { + os.RemoveAll(path) + } +} + +func TestFsBasic(t *testing.T) { + repo, closer := genFsRepo(t) + defer closer() + basicTest(t, repo) +} diff --git a/node/repo/memrepo_test.go b/node/repo/memrepo_test.go index 27b6d9f4b..965bc02c1 100644 --- a/node/repo/memrepo_test.go +++ b/node/repo/memrepo_test.go @@ -2,56 +2,9 @@ package repo import ( "testing" - - "github.com/multiformats/go-multiaddr" - "github.com/stretchr/testify/assert" ) -func TestMemRepo(t *testing.T) { +func TestMemBasic(t *testing.T) { repo := NewMemory(nil) - apima, err := repo.APIEndpoint() - if assert.Error(t, err) { - assert.Equal(t, ErrNoAPIEndpoint, err) - } - assert.Nil(t, apima, "with no api endpoint, return should be nil") - - lrepo, err := repo.Lock() - assert.NoError(t, err, "should be able to lock once") - assert.NotNil(t, lrepo, "locked repo shouldn't be nil") - - { - lrepo2, err := repo.Lock() - if assert.Error(t, err) { - assert.Equal(t, ErrRepoAlreadyLocked, err) - } - assert.Nil(t, lrepo2, "with locked repo errors, nil should be returned") - } - - err = lrepo.Close() - assert.NoError(t, err, "should be able to unlock") - - lrepo, err = repo.Lock() - assert.NoError(t, err, "should be able to relock") - assert.NotNil(t, lrepo, "locked repo shouldn't be nil") - - ma, err := multiaddr.NewMultiaddr("/ip4/127.0.0.1/tcp/43244") - assert.NoError(t, err, "creating multiaddr shouldn't error") - - err = lrepo.SetAPIEndpoint(ma) - assert.NoError(t, err, "setting multiaddr shouldn't error") - - apima, err = repo.APIEndpoint() - assert.NoError(t, err, "setting multiaddr shouldn't error") - assert.Equal(t, ma, apima, "returned API multiaddr should be the same") - - err = lrepo.Close() - assert.NoError(t, err, "should be able to close") - - apima, err = repo.APIEndpoint() - - if assert.Error(t, err) { - assert.Equal(t, ErrNoAPIEndpoint, err, "after closing repo, api should be nil") - } - assert.Nil(t, apima, "with closed repo, apima should be set back to nil") - + basicTest(t, repo) } diff --git a/node/repo/repo_test.go b/node/repo/repo_test.go new file mode 100644 index 000000000..2e90534a8 --- /dev/null +++ b/node/repo/repo_test.go @@ -0,0 +1,55 @@ +package repo + +import ( + "testing" + + "github.com/multiformats/go-multiaddr" + "github.com/stretchr/testify/assert" +) + +func basicTest(t *testing.T, repo Repo) { + apima, err := repo.APIEndpoint() + if assert.Error(t, err) { + assert.Equal(t, ErrNoAPIEndpoint, err) + } + assert.Nil(t, apima, "with no api endpoint, return should be nil") + + lrepo, err := repo.Lock() + assert.NoError(t, err, "should be able to lock once") + assert.NotNil(t, lrepo, "locked repo shouldn't be nil") + + { + lrepo2, err := repo.Lock() + if assert.Error(t, err) { + assert.Equal(t, ErrRepoAlreadyLocked, err) + } + assert.Nil(t, lrepo2, "with locked repo errors, nil should be returned") + } + + err = lrepo.Close() + assert.NoError(t, err, "should be able to unlock") + + lrepo, err = repo.Lock() + assert.NoError(t, err, "should be able to relock") + assert.NotNil(t, lrepo, "locked repo shouldn't be nil") + + ma, err := multiaddr.NewMultiaddr("/ip4/127.0.0.1/tcp/43244") + assert.NoError(t, err, "creating multiaddr shouldn't error") + + err = lrepo.SetAPIEndpoint(ma) + assert.NoError(t, err, "setting multiaddr shouldn't error") + + apima, err = repo.APIEndpoint() + assert.NoError(t, err, "setting multiaddr shouldn't error") + assert.Equal(t, ma, apima, "returned API multiaddr should be the same") + + err = lrepo.Close() + assert.NoError(t, err, "should be able to close") + + apima, err = repo.APIEndpoint() + + if assert.Error(t, err) { + assert.Equal(t, ErrNoAPIEndpoint, err, "after closing repo, api should be nil") + } + assert.Nil(t, apima, "with closed repo, apima should be set back to nil") +}