fs swampfile repo flagfile to prevent deletion of non swamp files

master
Caj Larsson 3 years ago
parent 67e137a474
commit 0b6e27f129

@ -5,8 +5,11 @@ import (
"os"
"path"
"time"
"errors"
)
var ErrDirtyRepo = errors.New("Dirty repository without flag")
type FileSystemSwampFileData struct {
path string
size int64
@ -46,6 +49,34 @@ type Repository struct {
Root string
}
func NewRepository(root string) (*Repository, error) {
fi, err := os.ReadDir(root)
if err != nil {
return nil, err
}
flagpath := path.Join(root, ".bogrepo")
if len(fi) == 0 {
// New Repository, write flagfile
f, err := os.OpenFile(flagpath, os.O_CREATE, 0644)
if err != nil {
return nil, err
}
f.Close()
}
flagfile, err := os.OpenFile(flagpath, os.O_RDONLY, 0)
if err != nil {
return nil, ErrDirtyRepo
}
flagfile.Close()
return &Repository { root }, nil
}
func (f Repository) absPath(filename string, namespace_ns string) string {
return path.Join(f.Root, namespace_ns, filename)
}

@ -3,17 +3,73 @@ package swampfile
import (
"caj-larsson/bog/dataswamp/swampfile"
"path"
"os"
"testing"
"github.com/matryer/is"
)
func newRepoDir(t *testing.T) string {
r := t.TempDir()
d := path.Join(r, "fs")
err := os.Mkdir(d, 0755)
if err != nil {
panic(err)
}
return d
}
func TestFsFileRepo(t *testing.T) {
var fac = func() swampfile.Repository {
r := t.TempDir()
d := path.Join(r, "fs")
repo := Repository{d}
return &repo
repo, err := NewRepository(newRepoDir(t))
if err != nil {
panic(err)
}
return repo
}
swampfile.RepositoryContract(fac, t)
}
func TestEmptyDir(t *testing.T) {
is := is.New(t)
dir_path := newRepoDir(t)
_, err := NewRepository(dir_path)
is.NoErr(err)
_, err = os.OpenFile(path.Join(dir_path, ".bogrepo"), os.O_RDONLY, 0)
is.NoErr(err)
}
func TestDirtyDir(t *testing.T) {
is := is.New(t)
dir_path := newRepoDir(t)
_, err := os.OpenFile(path.Join(dir_path, "randomfile"), os.O_CREATE, 0644)
is.NoErr(err)
_, err = NewRepository(dir_path)
is.Equal(err, ErrDirtyRepo)
}
func TestDirtyWithFlag(t *testing.T) {
is := is.New(t)
dir_path := newRepoDir(t)
_, err := os.OpenFile(path.Join(dir_path, "randomfile"), os.O_CREATE, 0644)
is.NoErr(err)
_, err = os.OpenFile(path.Join(dir_path, ".bogrepo"), os.O_CREATE, 0644)
is.NoErr(err)
_, err = NewRepository(dir_path)
is.NoErr(err)
}

@ -24,9 +24,11 @@ type Bog struct {
}
func buildFileDataRepository(config FileConfig) swampfile.Repository {
fsSwampfileRepo := new(fs_swampfile.Repository)
fsSwampfileRepo.Root = config.Path
return fsSwampfileRepo
r, err := fs_swampfile.NewRepository(config.Path)
if err != nil {
panic(err)
}
return r
}
func buildUserAgentRepository(config DatabaseConfig) namespace.Repository {

Loading…
Cancel
Save