86 lines
1.8 KiB
Go
86 lines
1.8 KiB
Go
package engine
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"time"
|
|
|
|
"robotfs/pb"
|
|
"robotfs/store"
|
|
"robotfs/utils"
|
|
)
|
|
|
|
type FileSystemManager struct {
|
|
meta store.MetaStore
|
|
storage *StorageManager
|
|
}
|
|
|
|
func NewFileSystemManager(meta store.MetaStore, storage *StorageManager) *FileSystemManager {
|
|
return &FileSystemManager{
|
|
meta: meta,
|
|
storage: storage,
|
|
}
|
|
}
|
|
|
|
func (f *FileSystemManager) BeginTransaction(ctx context.Context) (context.Context, error) {
|
|
return f.meta.BeginTransaction(ctx)
|
|
}
|
|
|
|
func (f *FileSystemManager) CommitTransaction(ctx context.Context) error {
|
|
return f.meta.CommitTransaction(ctx)
|
|
}
|
|
|
|
func (f *FileSystemManager) RollbackTransaction(ctx context.Context) error {
|
|
return f.meta.RollbackTransaction(ctx)
|
|
}
|
|
|
|
var (
|
|
Root = &utils.Entry{
|
|
FullPath: utils.FullPath("/"),
|
|
IsDir: true,
|
|
CreateTime: time.Now().Unix(),
|
|
LastModificationTime: time.Now().Unix(),
|
|
}
|
|
)
|
|
|
|
func (f *FileSystemManager) FindEntry(ctx context.Context, p utils.FullPath) (entry *pb.FileEntry, err error) {
|
|
if p == "/" {
|
|
return nil, nil
|
|
}
|
|
|
|
entry, err = f.meta.FindEntry(ctx, p)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return
|
|
}
|
|
|
|
func (f *FileSystemManager) MakeDirectory(ctx context.Context, path utils.FullPath) error {
|
|
if string(path) == "/" {
|
|
return nil
|
|
}
|
|
|
|
if entry, _ := f.FindEntry(ctx, path); entry != nil {
|
|
return fmt.Errorf("directory %s already exists", path)
|
|
}
|
|
|
|
parentDir, _ := path.DirAndName()
|
|
if parentDir != "/" {
|
|
if parentEntry, _ := f.FindEntry(ctx, utils.FullPath(parentDir)); parentEntry == nil {
|
|
return fmt.Errorf("parent directory %s does not exist", parentDir)
|
|
}
|
|
}
|
|
|
|
entry := utils.MakeEntry(path, true)
|
|
return f.meta.InsertEntry(ctx, entry)
|
|
}
|
|
|
|
func (f *FileSystemManager) Shutdown() error {
|
|
if f.meta != nil {
|
|
f.meta.Shutdown()
|
|
}
|
|
|
|
return nil
|
|
}
|