aboutsummaryrefslogtreecommitdiffstats
path: root/util
diff options
context:
space:
mode:
authorMichael Muré <batolettre@gmail.com>2020-12-05 03:08:54 +0100
committerMichael Muré <batolettre@gmail.com>2020-12-08 13:07:51 +0100
commit4ef2c1104079032336da9f2a7879f2432c2609ce (patch)
treec233921001ae0f43d5d1c62ad50148d2c5bb2e35 /util
parentbca9ae82745ffd619fd321f4200016c184849f94 (diff)
downloadgit-bug-4ef2c1104079032336da9f2a7879f2432c2609ce.tar.gz
repo: finish RepoStorage move
Diffstat (limited to 'util')
-rw-r--r--util/lamport/persisted_clock.go29
-rw-r--r--util/lamport/persisted_clock_test.go8
2 files changed, 20 insertions, 17 deletions
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)