319 lines
8.6 KiB
Go
319 lines
8.6 KiB
Go
package fs
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"gosvc/logger"
|
|
"io"
|
|
"time"
|
|
|
|
"robotfs/pkg/meta"
|
|
"robotfs/pkg/storage"
|
|
"robotfs/utils"
|
|
)
|
|
|
|
type FileSystem struct {
|
|
root string
|
|
meta meta.MetaStore
|
|
store *storage.Storage
|
|
locker *utils.LockManager
|
|
}
|
|
|
|
func NewFileSystem(meta meta.MetaStore, store *storage.Storage, root string) *FileSystem {
|
|
return &FileSystem{
|
|
root: root,
|
|
meta: meta,
|
|
store: store,
|
|
locker: utils.NewLockManager(),
|
|
}
|
|
}
|
|
|
|
func (fs *FileSystem) BeginTransaction(ctx context.Context) (context.Context, error) {
|
|
return fs.meta.BeginTransaction(ctx)
|
|
}
|
|
|
|
func (fs *FileSystem) CommitTransaction(ctx context.Context) error {
|
|
return fs.meta.CommitTransaction(ctx)
|
|
}
|
|
|
|
func (fs *FileSystem) RollbackTransaction(ctx context.Context) error {
|
|
return fs.meta.RollbackTransaction(ctx)
|
|
}
|
|
|
|
var (
|
|
Root = &utils.Entry{
|
|
FullPath: utils.FullPath("/"),
|
|
IsDir: true,
|
|
CreateTime: time.Now().Unix(),
|
|
LastModificationTime: time.Now().Unix(),
|
|
}
|
|
)
|
|
|
|
func (fs *FileSystem) FindEntry(ctx context.Context, p utils.FullPath) (entry *utils.Entry, err error) {
|
|
if p == "/" {
|
|
return Root, nil
|
|
}
|
|
|
|
entry, err = fs.meta.FindEntry(ctx, p)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return
|
|
}
|
|
|
|
func (fs *FileSystem) MakeDirectory(ctx context.Context, path utils.FullPath) error {
|
|
locker := fs.locker.AcquireLock(string(path))
|
|
defer fs.locker.ReleaseLock(locker)
|
|
|
|
logger.Info("making directory => %s", path)
|
|
|
|
if string(path) == "/" {
|
|
return nil
|
|
}
|
|
|
|
if entry, err := fs.FindEntry(ctx, path); err == nil && entry != nil {
|
|
return fmt.Errorf("directory %s already exists", path)
|
|
}
|
|
|
|
parentDir, _ := path.DirAndName()
|
|
if parentDir != "/" {
|
|
if parentEntry, _ := fs.FindEntry(ctx, utils.FullPath(parentDir)); parentEntry == nil {
|
|
return fmt.Errorf("parent directory %s does not exist", parentDir)
|
|
}
|
|
}
|
|
|
|
if err := utils.Mkdir(fs.root, string(path)); err != nil {
|
|
return err
|
|
}
|
|
|
|
entry := utils.NewDirEntry(path)
|
|
return fs.meta.InsertEntry(ctx, entry)
|
|
}
|
|
|
|
func (fs *FileSystem) ListDirectoryEntries(ctx context.Context, p utils.FullPath, startFileName string, inclusive bool, limit int64) (entries []*utils.Entry, hasMore bool, lastFileName string, err error) {
|
|
locker := fs.locker.AcquireRLock(string(p))
|
|
defer fs.locker.ReleaseLock(locker)
|
|
|
|
logger.Info("listing directory %s => startFileName:%s inclusive:%t count:%d", p, startFileName, inclusive, limit)
|
|
|
|
lastFileName, err = fs.StreamListDirectoryEntries(ctx, p, startFileName, inclusive, limit, func(entry *utils.Entry) bool {
|
|
entries = append(entries, entry)
|
|
return true
|
|
})
|
|
|
|
hasMore = int64(len(entries)) >= limit
|
|
if hasMore {
|
|
entries = entries[:limit]
|
|
}
|
|
|
|
return entries, hasMore, lastFileName, err
|
|
}
|
|
|
|
func (fs *FileSystem) StreamListDirectoryEntries(ctx context.Context, p utils.FullPath, startFileName string, inclusive bool, limit int64, eachEntryFunc meta.ListEachEntryFunc) (lastFileName string, err error) {
|
|
|
|
lastFileName, err = fs.doListDirectoryEntries(ctx, p, startFileName, inclusive, limit, eachEntryFunc)
|
|
return
|
|
}
|
|
|
|
func (fs *FileSystem) doListDirectoryEntries(ctx context.Context, p utils.FullPath, startFileName string, inclusive bool, limit int64, eachEntryFunc meta.ListEachEntryFunc) (lastFileName string, err error) {
|
|
lastFileName, err = fs.meta.ListDirectoryEntries(ctx, p, startFileName, inclusive, limit, func(entry *utils.Entry) bool {
|
|
select {
|
|
case <-ctx.Done():
|
|
return false
|
|
default:
|
|
return eachEntryFunc(entry)
|
|
}
|
|
})
|
|
return
|
|
}
|
|
|
|
func (fs *FileSystem) CreateFile(ctx context.Context, path utils.FullPath, reader io.Reader, contentType string, fileSize int64) error {
|
|
locker := fs.locker.AcquireLock(string(path))
|
|
defer fs.locker.ReleaseLock(locker)
|
|
|
|
logger.Info("uploading file => %s", path)
|
|
|
|
if string(path) == "/" {
|
|
return fmt.Errorf("cannot create file %s", path)
|
|
}
|
|
|
|
if entry, _ := fs.FindEntry(ctx, path); entry != nil {
|
|
return fmt.Errorf("file %s already exists", path)
|
|
}
|
|
|
|
parentDir, _ := path.DirAndName()
|
|
if parentDir != "/" {
|
|
if parentEntry, _ := fs.FindEntry(ctx, utils.FullPath(parentDir)); parentEntry == nil {
|
|
return fmt.Errorf("parent directory %s does not exist", parentDir)
|
|
}
|
|
}
|
|
|
|
output, err := fs.store.UploadFile(path.ToS3Key(), reader, contentType)
|
|
if err != nil {
|
|
return fmt.Errorf("upload file failed: %v", err)
|
|
}
|
|
|
|
if output.ETag == nil {
|
|
empty := ""
|
|
output.ETag = &empty
|
|
}
|
|
|
|
if output.VersionID == nil {
|
|
empty := ""
|
|
output.VersionID = &empty
|
|
}
|
|
|
|
entry := utils.NewFileEntry(path, path.ToS3Key(), uint64(fileSize), contentType, *output.ETag, *output.VersionID)
|
|
if err := fs.meta.InsertEntry(ctx, entry); err != nil {
|
|
return fmt.Errorf("create file entry failed: %v", err)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (fs *FileSystem) DownloadFile(ctx context.Context, path utils.FullPath) (*utils.S3ReadSeeker, *utils.Entry, error) {
|
|
locker := fs.locker.AcquireRLock(string(path))
|
|
defer fs.locker.ReleaseLock(locker)
|
|
|
|
logger.Info("downloading file => %s", path)
|
|
|
|
entry, err := fs.FindEntry(ctx, path)
|
|
if err != nil {
|
|
return nil, nil, fmt.Errorf("find entry failed: %v", err)
|
|
}
|
|
|
|
if entry.IsDir {
|
|
return nil, nil, fmt.Errorf("cannot download directory")
|
|
}
|
|
|
|
downloader, err := fs.store.DownloadFile(entry.S3Key)
|
|
if err != nil {
|
|
return nil, nil, fmt.Errorf("download file failed: %v", err)
|
|
}
|
|
|
|
return downloader, entry, nil
|
|
}
|
|
|
|
func (fs *FileSystem) DeleteFile(ctx context.Context, path utils.FullPath, isDir bool) error {
|
|
locker := fs.locker.AcquireLock(string(path))
|
|
defer fs.locker.ReleaseLock(locker)
|
|
|
|
logger.Info("deleting file => %s", path)
|
|
|
|
if string(path) == "/" {
|
|
return fmt.Errorf("cannot delete root")
|
|
}
|
|
|
|
entry, err := fs.FindEntry(ctx, path)
|
|
if err != nil {
|
|
return fmt.Errorf("find entry failed: %v", err)
|
|
}
|
|
|
|
if !isDir {
|
|
if err := fs.store.DeleteObject(entry.S3Key); err != nil {
|
|
return fmt.Errorf("delete file failed: %v", err)
|
|
}
|
|
}
|
|
|
|
if err := fs.meta.DeleteEntry(ctx, path); err != nil {
|
|
return fmt.Errorf("delete entry failed: %v", err)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (fs *FileSystem) RenameFile(ctx context.Context, srcPath, dstPath utils.FullPath, isDir bool) error {
|
|
locker := fs.locker.AcquireOrderedLock(string(srcPath), string(dstPath))
|
|
defer fs.locker.ReleaseLocks(locker)
|
|
|
|
logger.Info("moving file %s => %s", srcPath, dstPath)
|
|
|
|
if string(srcPath) == "/" || string(dstPath) == "/" {
|
|
return fmt.Errorf("cannot rename root")
|
|
}
|
|
|
|
srcEntry, err := fs.FindEntry(ctx, srcPath)
|
|
if err != nil {
|
|
return fmt.Errorf("find src entry failed: %v", err)
|
|
}
|
|
|
|
if dstEntry, _ := fs.FindEntry(ctx, dstPath); dstEntry != nil {
|
|
return fmt.Errorf("dst entry %s already exists", dstPath)
|
|
}
|
|
|
|
parentDir, _ := dstPath.DirAndName()
|
|
if parentDir != "/" {
|
|
if parentEntry, _ := fs.FindEntry(ctx, utils.FullPath(parentDir)); parentEntry == nil {
|
|
return fmt.Errorf("parent directory %s does not exist", parentDir)
|
|
}
|
|
}
|
|
|
|
if isDir {
|
|
return fmt.Errorf("rename not file")
|
|
}
|
|
|
|
if err := utils.Move(fs.root, string(srcPath), string(dstPath)); err != nil {
|
|
return err
|
|
}
|
|
|
|
newEntry := utils.NewFileEntry(dstPath, dstPath.ToS3Key(), srcEntry.Size, srcEntry.ContentType, srcEntry.Etag, srcEntry.VersionID)
|
|
if err := fs.meta.UpdateEntry(ctx, newEntry); err != nil {
|
|
return fmt.Errorf("update entry failed: %v", err)
|
|
}
|
|
|
|
if err := fs.meta.DeleteEntry(ctx, srcPath); err != nil {
|
|
return fmt.Errorf("delete src entry failed: %v", err)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (fs *FileSystem) CopyFile(ctx context.Context, srcPath, dstPath utils.FullPath, isDir bool) error {
|
|
locker := fs.locker.AcquireOrderedLock(string(srcPath), string(dstPath))
|
|
defer fs.locker.ReleaseLocks(locker)
|
|
|
|
logger.Info("copying file %s => %s", srcPath, dstPath)
|
|
|
|
if string(srcPath) == "/" || string(dstPath) == "/" {
|
|
return fmt.Errorf("cannot copy root")
|
|
}
|
|
|
|
srcEntry, err := fs.FindEntry(ctx, srcPath)
|
|
if err != nil {
|
|
return fmt.Errorf("find src entry failed: %v", err)
|
|
}
|
|
|
|
if dstEntry, err := fs.FindEntry(ctx, dstPath); dstEntry != nil && err == nil {
|
|
return fmt.Errorf("dst entry %s already exists", dstPath)
|
|
}
|
|
|
|
parentDir, _ := dstPath.DirAndName()
|
|
if parentDir != "/" {
|
|
if parentEntry, _ := fs.FindEntry(ctx, utils.FullPath(parentDir)); parentEntry == nil {
|
|
return fmt.Errorf("parent directory %s does not exist", parentDir)
|
|
}
|
|
}
|
|
|
|
if isDir {
|
|
return fmt.Errorf("copy not file")
|
|
}
|
|
|
|
if err := utils.Clone(fs.root, string(srcPath), string(dstPath)); err != nil {
|
|
return err
|
|
}
|
|
|
|
newEntry := utils.NewFileEntry(dstPath, dstPath.ToS3Key(), srcEntry.Size, srcEntry.ContentType, srcEntry.Etag, srcEntry.VersionID)
|
|
if err := fs.meta.InsertEntry(ctx, newEntry); err != nil {
|
|
return fmt.Errorf("insert new entry failed: %v", err)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (fs *FileSystem) Shutdown() {
|
|
if fs.meta != nil {
|
|
fs.meta.Shutdown()
|
|
}
|
|
}
|