Update localfs.go

This commit is contained in:
Seb3thehacker 2022-03-23 22:04:22 +00:00 committed by GitHub
parent b814486d0e
commit 743ef5d7af
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -2,6 +2,7 @@ package localfs
import (
"encoding/json"
"errors"
"io"
"io/ioutil"
"net/http"
@ -16,6 +17,7 @@ import (
type LocalfsBackend struct {
metaPath string
filesPath string
locksPath string
}
type MetadataJSON struct {
@ -127,6 +129,41 @@ func (b LocalfsBackend) writeMetadata(key string, metadata backends.Metadata) er
return nil
}
func (b LocalfsBackend) Lock(filename string) (err error) {
lockPath := path.Join(b.locksPath, filename)
lock, err := os.Create(lockPath)
if err != nil {
return err
}
lock.Close()
return
}
func (b LocalfsBackend) Unlock(filename string) (err error) {
lockPath := path.Join(b.locksPath, filename)
err = os.Remove(lockPath)
if err != nil {
return err
}
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) {
filePath := path.Join(b.filesPath, key)
@ -201,9 +238,10 @@ func (b LocalfsBackend) List() ([]string, error) {
return output, nil
}
func NewLocalfsBackend(metaPath string, filesPath string) LocalfsBackend {
func NewLocalfsBackend(metaPath string, filesPath string, locksPath string) LocalfsBackend {
return LocalfsBackend{
metaPath: metaPath,
filesPath: filesPath,
locksPath: locksPath,
}
}