Skip to content

Commit

Permalink
Fix: Re-mouting fuse failure and expose mountPath on FuseDriveResponse (
Browse files Browse the repository at this point in the history
#261)

Also, refactor a bunch of things around FUSE
  • Loading branch information
perfectmak authored Nov 27, 2020
1 parent 20167d2 commit fb4a99b
Show file tree
Hide file tree
Showing 21 changed files with 1,029 additions and 682 deletions.
18 changes: 18 additions & 0 deletions core/fsds/data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,17 @@ package fsds
import (
"context"
"os"
"path/filepath"
"strings"
)

// FileReadWriterCloser implements interfaces to read, copy, seek and close.
type FileReadWriterCloser interface {
Read(ctx context.Context, data []byte, offset int64) (int, error)
Write(ctx context.Context, data []byte, offset int64) (int, error)
Close(ctx context.Context) error
Stats(ctx context.Context) (*DirEntry, error)
Truncate(ctx context.Context, size uint64) error
}

// FSDataSource is data source of file/directories and their information
Expand All @@ -25,6 +29,10 @@ type FSDataSource interface {
Open(ctx context.Context, path string) (FileReadWriterCloser, error)
// CreateEntry should create a directory or file based on the mode at the path
CreateEntry(ctx context.Context, path string, mode os.FileMode) (*DirEntry, error)
// RenameEntry should rename the directory entry from old to new
RenameEntry(ctx context.Context, oldPath, newPath string) error
// DeleteEntry should delete the item at the path
DeleteEntry(ctx context.Context, path string) error
}

// TLFDataSource represents a data source handler for a particular top level file.
Expand All @@ -33,3 +41,13 @@ type TLFDataSource struct {
basePath string
FSDataSource
}

// Returns child path inside data source
func (t *TLFDataSource) ChildPath(path string) string {
return strings.TrimPrefix(path, t.basePath)
}

// returns the path with the datasource base path prefixed
func (t *TLFDataSource) ParentPath(path string) string {
return filepath.Join(t.basePath, path)
}
42 changes: 23 additions & 19 deletions core/fsds/dir_entry.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package fsds
import (
"fmt"
"os"
"os/user"
"path/filepath"
"strconv"
"strings"
"time"
Expand All @@ -13,8 +13,8 @@ import (
"github.com/FleekHQ/space-daemon/core/space/domain"
)

var StandardFileAccessMode os.FileMode = 0600 // -rw-------
var StandardDirAccessMode = os.ModeDir | 0700 // drwx------
var StandardFileAccessMode os.FileMode = 0777 // -rw-------
var StandardDirAccessMode = os.ModeDir | 0777 //0700 // drwx------
var RestrictedDirAccessMode = os.ModeDir | 0500 // dr-x------ only allow reading and opening directory for user

// DirEntry implements the DirEntryOps
Expand All @@ -28,6 +28,22 @@ func NewDirEntry(entry domain.DirEntry) *DirEntry {
return NewDirEntryWithMode(entry, 0)
}

func NewDirEntryFromFileInfo(info os.FileInfo, path string) *DirEntry {
return &DirEntry{
entry: domain.DirEntry{
Path: filepath.Dir(path),
IsDir: info.IsDir(),
Name: filepath.Base(path),
SizeInBytes: fmt.Sprintf("%d", info.Size()),
Created: info.ModTime().Format(time.RFC3339),
Updated: info.ModTime().Format(time.RFC3339),
FileExtension: filepath.Ext(path),
},
mode: StandardFileAccessMode,
dbId: "",
}
}

func NewDirEntryWithMode(entry domain.DirEntry, mode os.FileMode) *DirEntry {
return &DirEntry{
entry: entry,
Expand Down Expand Up @@ -84,25 +100,13 @@ func (d *DirEntry) Mode() os.FileMode {
return StandardFileAccessMode
}

func (d *DirEntry) Uid() string {
func (d *DirEntry) Uid() uint32 {
// for now return id of currently logged in user
currentUser, err := user.Current()
if err != nil {
log.Error("Uid() Error fetching user.Current()", err)
return "0"
}

return currentUser.Uid
return uint32(os.Getuid())
}

func (d *DirEntry) Gid() string {
currentUser, err := user.Current()
if err != nil {
log.Error("Gid() Error fetching user.Current()", err)
return "0"
}

return currentUser.Gid
func (d *DirEntry) Gid() uint32 {
return uint32(os.Getgid())
}

// Ctime implements the DirEntryAttribute Interface
Expand Down
92 changes: 0 additions & 92 deletions core/fsds/files/read_write_wrapper.go

This file was deleted.

58 changes: 54 additions & 4 deletions core/fsds/files_ds.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@ import (
"io/ioutil"
"os"
"path/filepath"
"syscall"
"time"

"github.com/FleekHQ/space-daemon/core/fsds/files"

"github.com/FleekHQ/space-daemon/core/space/domain"

"github.com/FleekHQ/space-daemon/core/space"
Expand Down Expand Up @@ -107,7 +106,7 @@ func (f *filesDataSource) Open(ctx context.Context, path string) (FileReadWriter
return nil, err
}

return files.OpenSpaceFilesHandler(f.service, openFileInfo.Location, path, DefaultBucketName)
return OpenSpaceFilesHandler(f.service, openFileInfo.Location, path, DefaultBucketName), nil
}

// Create the entry at the specified path and return a DirEntry representing it.
Expand All @@ -133,7 +132,8 @@ func (f *filesDataSource) CreateEntry(ctx context.Context, path string, mode os.
}

// create an empty file to uploaded to the specified path
newFilePath := fmt.Sprintf("%s%s", os.TempDir(), path)
newFilePath := filepath.Join(os.TempDir(), path)
_ = os.MkdirAll(filepath.Dir(newFilePath), os.ModePerm)
err := ioutil.WriteFile(newFilePath, []byte{}, mode)
if err != nil {
log.Error("Error creating empty file", err, "newFilePath:"+newFilePath)
Expand Down Expand Up @@ -166,3 +166,53 @@ func (f *filesDataSource) CreateEntry(ctx context.Context, path string, mode os.
Updated: time.Now().Format(time.RFC3339),
}), nil
}

// RenameEntry for now only supports renaming of empty folders
// Depending on user request and textile support, non-empty folders and file renames will be supported
func (f *filesDataSource) RenameEntry(ctx context.Context, oldPath, newPath string) error {
log.Debug("FileDS RenameEntry", "oldPath:"+oldPath, "newPath:"+newPath)
entry, err := f.Get(ctx, oldPath)
if err != nil {
return err
}

if !entry.IsDir() {
log.Warn("FileDS trying to rename an entry that is not a directory")
return syscall.ENOTSUP
}

childEntries, err := f.GetChildren(ctx, oldPath)
if err != nil {
log.Error("failed to get children of old path", err, "oldPath:"+oldPath)
return err
}

if len(childEntries) != 0 && !areAllEntriesHidden(childEntries) {
log.Warn("FileDS renaming directory that is not empty")
// folder is not empty, so just error out
return syscall.ENOTSUP
// in the future, we should do a recursive copy to the newPath and then delete old path
}

if err = f.service.CreateFolder(ctx, newPath, DefaultBucketName); err != nil {
log.Error("failed to new path create folder", err, "newPath:"+newPath)
return syscall.ENOTSUP
}

return f.service.RemoveDirOrFile(ctx, oldPath, DefaultBucketName)
}

func areAllEntriesHidden(entries []*DirEntry) bool {
for _, entry := range entries {
baseName := filepath.Base(entry.Name())
if !blackListedDirEntryNames[baseName] {
return false
}
}
return true
}

func (f *filesDataSource) DeleteEntry(ctx context.Context, path string) error {
log.Debug("FileDS DeletEntry", "path:"+path)
return f.service.RemoveDirOrFile(ctx, path, DefaultBucketName)
}
Loading

0 comments on commit fb4a99b

Please sign in to comment.