From d3e7c9060a14ac579f899fd01c936319132cbc97 Mon Sep 17 00:00:00 2001 From: Javi Fontan Date: Fri, 22 Dec 2017 17:16:59 +0100 Subject: Check reference also in norwfs SetRef Signed-off-by: Javi Fontan --- .../internal/dotgit/dotgit_setref_norwfs.go | 27 ++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) (limited to 'storage/filesystem/internal/dotgit') diff --git a/storage/filesystem/internal/dotgit/dotgit_setref_norwfs.go b/storage/filesystem/internal/dotgit/dotgit_setref_norwfs.go index 8b1e662..5695bd3 100644 --- a/storage/filesystem/internal/dotgit/dotgit_setref_norwfs.go +++ b/storage/filesystem/internal/dotgit/dotgit_setref_norwfs.go @@ -2,9 +2,13 @@ package dotgit -import "gopkg.in/src-d/go-git.v4/plumbing" +import ( + "fmt" -// There are some filesystems tha don't support opening files in RDWD mode. + "gopkg.in/src-d/go-git.v4/plumbing" +) + +// There are some filesystems that don't support opening files in RDWD mode. // In these filesystems the standard SetRef function can not be used as i // reads the reference file to check that it's not modified before updating it. // @@ -12,6 +16,25 @@ import "gopkg.in/src-d/go-git.v4/plumbing" // making it compatible with these simple filesystems. This is usually not // a problem as they should be accessed by only one process at a time. func (d *DotGit) setRef(fileName, content string, old *plumbing.Reference) error { + _, err := d.fs.Stat(fileName) + if err == nil && old != nil { + fRead, err := d.fs.Open(fileName) + if err != nil { + return err + } + + ref, err := d.readReferenceFrom(fRead, old.Name().String()) + fRead.Close() + + if err != nil { + return err + } + + if ref.Hash() != old.Hash() { + return fmt.Errorf("reference has changed concurrently") + } + } + f, err := d.fs.Create(fileName) if err != nil { return err -- cgit