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" "os"
"path" "path"
"time" "time"
"errors"
) )
var ErrDirtyRepo = errors.New("Dirty repository without flag")
type FileSystemSwampFileData struct { type FileSystemSwampFileData struct {
path string path string
size int64 size int64
@ -46,6 +49,34 @@ type Repository struct {
Root string 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 { func (f Repository) absPath(filename string, namespace_ns string) string {
return path.Join(f.Root, namespace_ns, filename) return path.Join(f.Root, namespace_ns, filename)
} }

@ -3,17 +3,73 @@ package swampfile
import ( import (
"caj-larsson/bog/dataswamp/swampfile" "caj-larsson/bog/dataswamp/swampfile"
"path" "path"
"os"
"testing" "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) { func TestFsFileRepo(t *testing.T) {
var fac = func() swampfile.Repository { var fac = func() swampfile.Repository {
r := t.TempDir() repo, err := NewRepository(newRepoDir(t))
d := path.Join(r, "fs")
repo := Repository{d} if err != nil {
return &repo panic(err)
}
return repo
} }
swampfile.RepositoryContract(fac, t) 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 { func buildFileDataRepository(config FileConfig) swampfile.Repository {
fsSwampfileRepo := new(fs_swampfile.Repository) r, err := fs_swampfile.NewRepository(config.Path)
fsSwampfileRepo.Root = config.Path if err != nil {
return fsSwampfileRepo panic(err)
}
return r
} }
func buildUserAgentRepository(config DatabaseConfig) namespace.Repository { func buildUserAgentRepository(config DatabaseConfig) namespace.Repository {

Loading…
Cancel
Save