From 4ef2c1104079032336da9f2a7879f2432c2609ce Mon Sep 17 00:00:00 2001 From: Michael Muré Date: Sat, 5 Dec 2020 03:08:54 +0100 Subject: repo: finish RepoStorage move --- util/lamport/persisted_clock.go | 29 +++++++++++++++++------------ util/lamport/persisted_clock_test.go | 8 +++----- 2 files changed, 20 insertions(+), 17 deletions(-) (limited to 'util/lamport') diff --git a/util/lamport/persisted_clock.go b/util/lamport/persisted_clock.go index e70b01ef..b9246f73 100644 --- a/util/lamport/persisted_clock.go +++ b/util/lamport/persisted_clock.go @@ -5,30 +5,28 @@ import ( "fmt" "io/ioutil" "os" - "path/filepath" + + "github.com/go-git/go-billy/v5" + "github.com/go-git/go-billy/v5/util" ) var ErrClockNotExist = errors.New("clock doesn't exist") type PersistedClock struct { *MemClock + root billy.Filesystem filePath string } // NewPersistedClock create a new persisted Lamport clock -func NewPersistedClock(filePath string) (*PersistedClock, error) { +func NewPersistedClock(root billy.Filesystem, filePath string) (*PersistedClock, error) { clock := &PersistedClock{ MemClock: NewMemClock(), + root: root, filePath: filePath, } - dir := filepath.Dir(filePath) - err := os.MkdirAll(dir, 0777) - if err != nil { - return nil, err - } - - err = clock.Write() + err := clock.Write() if err != nil { return nil, err } @@ -37,8 +35,9 @@ func NewPersistedClock(filePath string) (*PersistedClock, error) { } // LoadPersistedClock load a persisted Lamport clock from a file -func LoadPersistedClock(filePath string) (*PersistedClock, error) { +func LoadPersistedClock(root billy.Filesystem, filePath string) (*PersistedClock, error) { clock := &PersistedClock{ + root: root, filePath: filePath, } @@ -71,13 +70,19 @@ func (pc *PersistedClock) Witness(time Time) error { } func (pc *PersistedClock) read() error { - content, err := ioutil.ReadFile(pc.filePath) + f, err := pc.root.Open(pc.filePath) if os.IsNotExist(err) { return ErrClockNotExist } if err != nil { return err } + defer f.Close() + + content, err := ioutil.ReadAll(f) + if err != nil { + return err + } var value uint64 n, err := fmt.Sscanf(string(content), "%d", &value) @@ -96,5 +101,5 @@ func (pc *PersistedClock) read() error { func (pc *PersistedClock) Write() error { data := []byte(fmt.Sprintf("%d", pc.counter)) - return ioutil.WriteFile(pc.filePath, data, 0644) + return util.WriteFile(pc.root, pc.filePath, data, 0644) } diff --git a/util/lamport/persisted_clock_test.go b/util/lamport/persisted_clock_test.go index aacec3bf..9ef690da 100644 --- a/util/lamport/persisted_clock_test.go +++ b/util/lamport/persisted_clock_test.go @@ -1,18 +1,16 @@ package lamport import ( - "io/ioutil" - "path" "testing" + "github.com/go-git/go-billy/v5/memfs" "github.com/stretchr/testify/require" ) func TestPersistedClock(t *testing.T) { - dir, err := ioutil.TempDir("", "") - require.NoError(t, err) + root := memfs.New() - c, err := NewPersistedClock(path.Join(dir, "test-clock")) + c, err := NewPersistedClock(root, "test-clock") require.NoError(t, err) testClock(t, c) -- cgit