Add check for locks in cleanup.go

This commit is contained in:
BBaoVanC 2021-12-23 18:24:50 -06:00
parent 4512264e84
commit 4a9a4f7be2
No known key found for this signature in database
GPG Key ID: 18089E4E3CCF1D3A
5 changed files with 33 additions and 1 deletions

View File

@ -2,6 +2,7 @@ package localfs
import ( import (
"encoding/json" "encoding/json"
"errors"
"io" "io"
"io/ioutil" "io/ioutil"
"net/http" "net/http"
@ -151,6 +152,18 @@ func (b LocalfsBackend) Unlock(filename string) (err error) {
return return
} }
func (b LocalfsBackend) CheckLock(filename string) (locked bool, err error) {
lockPath := path.Join(b.locksPath, filename)
if _, err := os.Stat(lockPath); errors.Is(err, os.ErrNotExist) {
return false, nil
} else {
return true, nil
}
return false, err
}
func (b LocalfsBackend) Put(key string, r io.Reader, expiry time.Time, deleteKey, accessKey string, srcIp string) (m backends.Metadata, err error) { func (b LocalfsBackend) Put(key string, r io.Reader, expiry time.Time, deleteKey, accessKey string, srcIp string) (m backends.Metadata, err error) {
filePath := path.Join(b.filesPath, key) filePath := path.Join(b.filesPath, key)

View File

@ -167,6 +167,11 @@ func (b S3Backend) Unlock(filename string) (err error) {
return return
} }
func (b S3Backend) CheckLock(filename string) (locked bool, err error) {
log.Printf("Locking is not supported on S3")
return
}
func (b S3Backend) Put(key string, r io.Reader, expiry time.Time, deleteKey, accessKey string, srcIp string) (m backends.Metadata, err error) { func (b S3Backend) Put(key string, r io.Reader, expiry time.Time, deleteKey, accessKey string, srcIp string) (m backends.Metadata, err error) {
tmpDst, err := ioutil.TempFile("", "linx-server-upload") tmpDst, err := ioutil.TempFile("", "linx-server-upload")
if err != nil { if err != nil {

View File

@ -14,6 +14,7 @@ type StorageBackend interface {
Get(key string) (Metadata, io.ReadCloser, error) Get(key string) (Metadata, io.ReadCloser, error)
Lock(filename string) (error) Lock(filename string) (error)
Unlock(filename string) (error) Unlock(filename string) (error)
CheckLock(filename string) (bool, error)
Put(key string, r io.Reader, expiry time.Time, deleteKey, accessKey string, srcIp string) (Metadata, error) Put(key string, r io.Reader, expiry time.Time, deleteKey, accessKey string, srcIp string) (Metadata, error)
PutMetadata(key string, m Metadata) error PutMetadata(key string, m Metadata) error
ServeFile(key string, w http.ResponseWriter, r *http.Request) error ServeFile(key string, w http.ResponseWriter, r *http.Request) error
@ -27,4 +28,3 @@ type MetaStorageBackend interface {
var NotFoundErr = errors.New("File not found.") var NotFoundErr = errors.New("File not found.")
var FileEmptyError = errors.New("Empty file") var FileEmptyError = errors.New("Empty file")
var FileLockedError = errors.New("Locked file")

View File

@ -17,6 +17,15 @@ func Cleanup(filesDir string, metaDir string, locksDir string, noLogs bool) {
} }
for _, filename := range files { for _, filename := range files {
locked, err := fileBackend.CheckLock(filename)
if err != nil {
log.Printf("Error checking if %s is locked: %s", filename, err)
}
if locked {
log.Printf("%s is locked, it will be ignored", filename)
continue
}
metadata, err := fileBackend.Head(filename) metadata, err := fileBackend.Head(filename)
if err != nil { if err != nil {
if !noLogs { if !noLogs {
@ -36,7 +45,9 @@ func Cleanup(filesDir string, metaDir string, locksDir string, noLogs bool) {
func PeriodicCleanup(minutes time.Duration, filesDir string, metaDir string, locksDir string, noLogs bool) { func PeriodicCleanup(minutes time.Duration, filesDir string, metaDir string, locksDir string, noLogs bool) {
c := time.Tick(minutes) c := time.Tick(minutes)
for range c { for range c {
log.Printf("Running periodic cleanup")
Cleanup(filesDir, metaDir, locksDir, noLogs) Cleanup(filesDir, metaDir, locksDir, noLogs)
log.Printf("Finished periodic cleanup")
} }
} }

View File

@ -9,12 +9,15 @@ import (
func main() { func main() {
var filesDir string var filesDir string
var metaDir string var metaDir string
var locksDir string
var noLogs bool var noLogs bool
flag.StringVar(&filesDir, "filespath", "files/", flag.StringVar(&filesDir, "filespath", "files/",
"path to files directory") "path to files directory")
flag.StringVar(&metaDir, "metapath", "meta/", flag.StringVar(&metaDir, "metapath", "meta/",
"path to metadata directory") "path to metadata directory")
flag.StringVar(&metaDir, "lockspath", "locks/",
"path to metadata directory")
flag.BoolVar(&noLogs, "nologs", false, flag.BoolVar(&noLogs, "nologs", false,
"don't log deleted files") "don't log deleted files")
flag.Parse() flag.Parse()