swampfile.Repository.DeleteOlderThan

master
Caj Larsson 3 years ago
parent 0b6e27f129
commit 7bb9bf90c2

@ -22,4 +22,5 @@ var (
ErrUpdateFailed = errors.New("update failed")
ErrDeleteFailed = errors.New("delete failed")
ErrUnacceptablePath = errors.New("unacceptable path")
ErrCorrupted = errors.New("repository contains unexpected data")
)

@ -3,6 +3,7 @@ package swampfile
import (
"github.com/matryer/is"
"testing"
"time"
)
func RepositoryContract(fac func() Repository, t *testing.T) {
@ -47,4 +48,19 @@ func basicFileOperationContract(fac func() Repository, t *testing.T) {
is.Equal(err, ErrNotExists)
is.True(deleted_file == nil)
// DeleteOlderThan
expiring_file, err := repo.Create("deep/dir/expiring.new", "ua1")
is.NoErr(err)
expiring_file.Write([]byte(testdata))
expiring_file.Close()
err = repo.DeleteOlderThan("ua1", time.Date(2000, 1, 1, 0, 0, 0, 0, time.UTC))
is.NoErr(err)
expired_file, err := repo.Open("deep/dir/expiring.new", "ua1")
is.Equal(err, ErrNotExists)
is.True(expired_file == nil)
}

@ -1,7 +1,10 @@
package swampfile
import "time"
type Repository interface {
Create(filename string, user_agent_label string) (SwampInFile, error)
Open(filename string, user_agent_label string) (SwampOutFile, error)
Delete(filename string, user_agent_label string)
Create(filename string, namespace_stub string) (SwampInFile, error)
Open(filename string, namespace_stub string) (SwampOutFile, error)
Delete(filename string, namespace_stub string)
DeleteOlderThan(namespace_stub string, ts time.Time) error
}

@ -4,6 +4,8 @@ import (
"caj-larsson/bog/dataswamp/swampfile"
"os"
"path"
"path/filepath"
"io/fs"
"time"
"errors"
)
@ -122,3 +124,31 @@ func (f Repository) Delete(filename string, namespace_ns string) {
abs_path := f.absPath(filename, namespace_ns)
os.Remove(abs_path)
}
func (r Repository) DeleteOlderThan(namespace_stub string, ts time.Time) error {
dr := path.Join(r.Root, namespace_stub)
err := filepath.Walk(dr, func(path string, info fs.FileInfo, err error) error {
if err != nil {
return err
}
mode := info.Mode()
if mode.IsRegular() {
if ts.Before(info.ModTime()) {
err = os.Remove(path)
if err != nil {
panic(err)
}
}
return nil
}
if !mode.IsDir() {
return swampfile.ErrCorrupted
}
return nil
})
return err
}

@ -4,11 +4,13 @@ import (
"os"
"path"
"time"
// "io"
"io/fs"
"caj-larsson/bog/dataswamp/swampfile"
"github.com/spf13/afero"
)
type SwampFile struct {
filename string
file afero.File
@ -54,7 +56,7 @@ func NewRepository() swampfile.Repository {
}
func (r Repository) Create(filename string, namespace_stub string) (swampfile.SwampInFile, error) {
abs_path := path.Join(filename, namespace_stub)
abs_path := path.Join(namespace_stub, filename)
dir := path.Dir(abs_path)
r.fs.MkdirAll(dir, 0750)
file, err := r.fs.OpenFile(abs_path, os.O_RDWR|os.O_CREATE, 0644)
@ -69,7 +71,7 @@ func (r Repository) Create(filename string, namespace_stub string) (swampfile.Sw
}
func (r Repository) Open(filename string, namespace_stub string) (swampfile.SwampOutFile, error) {
abs_path := path.Join(filename, namespace_stub)
abs_path := path.Join(namespace_stub, filename)
dir := path.Dir(abs_path)
r.fs.MkdirAll(dir, 0750)
@ -84,6 +86,27 @@ func (r Repository) Open(filename string, namespace_stub string) (swampfile.Swam
}
func (r Repository) Delete(filename string, namespace_stub string) {
abs_path := path.Join(filename, namespace_stub)
abs_path := path.Join(namespace_stub, filename)
r.fs.Remove(abs_path)
}
func (r Repository) DeleteOlderThan(namespace_stub string, ts time.Time) error {
err := afero.Walk(r.fs, namespace_stub, func(path string, info fs.FileInfo, err error) error {
if err != nil {
return err
}
if info.Mode().IsRegular() && ts.Before(info.ModTime()) {
r.fs.Remove(path)
return nil
}
if !info.Mode().IsDir() {
return swampfile.ErrCorrupted
}
return nil
})
return err
}

Loading…
Cancel
Save