瀏覽代碼

Added WIP sharefs

Toby Chui 3 年之前
父節點
當前提交
7faa8ab1ff

+ 23 - 22
mod/filesystem/abstractions/emptyfs/emptyfs.go

@@ -1,11 +1,12 @@
 package emptyfs
 
 import (
-	"errors"
 	"io"
 	"os"
 	"path/filepath"
 	"time"
+
+	"imuslab.com/arozos/mod/filesystem/fserror"
 )
 
 /*
@@ -23,43 +24,43 @@ func NewEmptyFileSystemAbstraction() EmptyFileSystemAbstraction {
 }
 
 func (l EmptyFileSystemAbstraction) Chmod(filename string, mode os.FileMode) error {
-	return nil
+	return fserror.ErrNullOperation
 }
 func (l EmptyFileSystemAbstraction) Chown(filename string, uid int, gid int) error {
-	return nil
+	return fserror.ErrNullOperation
 }
 func (l EmptyFileSystemAbstraction) Chtimes(filename string, atime time.Time, mtime time.Time) error {
-	return nil
+	return fserror.ErrNullOperation
 }
 func (l EmptyFileSystemAbstraction) Create(filename string) (*os.File, error) {
-	return nil, nil
+	return nil, fserror.ErrNullOperation
 }
 func (l EmptyFileSystemAbstraction) Mkdir(filename string, mode os.FileMode) error {
-	return nil
+	return fserror.ErrNullOperation
 }
 func (l EmptyFileSystemAbstraction) MkdirAll(filename string, mode os.FileMode) error {
-	return nil
+	return fserror.ErrNullOperation
 }
 func (l EmptyFileSystemAbstraction) Name() string {
 	return ""
 }
 func (l EmptyFileSystemAbstraction) Open(filename string) (*os.File, error) {
-	return nil, nil
+	return nil, fserror.ErrNullOperation
 }
 func (l EmptyFileSystemAbstraction) OpenFile(filename string, flag int, perm os.FileMode) (*os.File, error) {
-	return nil, nil
+	return nil, fserror.ErrNullOperation
 }
 func (l EmptyFileSystemAbstraction) Remove(filename string) error {
-	return nil
+	return fserror.ErrNullOperation
 }
 func (l EmptyFileSystemAbstraction) RemoveAll(path string) error {
-	return nil
+	return fserror.ErrNullOperation
 }
 func (l EmptyFileSystemAbstraction) Rename(oldname, newname string) error {
-	return nil
+	return fserror.ErrNullOperation
 }
 func (l EmptyFileSystemAbstraction) Stat(filename string) (os.FileInfo, error) {
-	return nil, nil
+	return nil, fserror.ErrNullOperation
 }
 
 /*
@@ -67,11 +68,11 @@ func (l EmptyFileSystemAbstraction) Stat(filename string) (os.FileInfo, error) {
 */
 
 func (l EmptyFileSystemAbstraction) VirtualPathToRealPath(subpath string, username string) (string, error) {
-	return "", errors.New("empty filesystem abstraction")
+	return "", fserror.ErrVpathResolveFailed
 }
 
 func (l EmptyFileSystemAbstraction) RealPathToVirtualPath(fullpath string, username string) (string, error) {
-	return "", errors.New("empty filesystem abstraction")
+	return "", fserror.ErrRpathResolveFailed
 }
 
 func (l EmptyFileSystemAbstraction) FileExists(realpath string) bool {
@@ -83,7 +84,7 @@ func (l EmptyFileSystemAbstraction) IsDir(realpath string) bool {
 }
 
 func (l EmptyFileSystemAbstraction) Glob(realpathWildcard string) ([]string, error) {
-	return []string{}, nil
+	return []string{}, fserror.ErrNullOperation
 }
 
 func (l EmptyFileSystemAbstraction) GetFileSize(realpath string) int64 {
@@ -91,22 +92,22 @@ func (l EmptyFileSystemAbstraction) GetFileSize(realpath string) int64 {
 }
 
 func (l EmptyFileSystemAbstraction) GetModTime(realpath string) (int64, error) {
-	return 0, errors.New("empty filesystem abstraction")
+	return 0, fserror.ErrOperationNotSupported
 }
 
 func (l EmptyFileSystemAbstraction) WriteFile(filename string, content []byte, mode os.FileMode) error {
-	return nil
+	return fserror.ErrNullOperation
 }
 func (l EmptyFileSystemAbstraction) ReadFile(filename string) ([]byte, error) {
-	return []byte(""), errors.New("empty filesystem abstraction")
+	return []byte(""), fserror.ErrOperationNotSupported
 }
 func (l EmptyFileSystemAbstraction) WriteStream(filename string, stream io.Reader, mode os.FileMode) error {
-	return errors.New("operation not supported on dummy file system")
+	return fserror.ErrNullOperation
 }
 func (l EmptyFileSystemAbstraction) ReadStream(filename string) (io.ReadCloser, error) {
-	return nil, errors.New("operation not supported on dummy file system")
+	return nil, fserror.ErrOperationNotSupported
 }
 
 func (l EmptyFileSystemAbstraction) Walk(root string, walkFn filepath.WalkFunc) error {
-	return errors.New("empty filesystem abstraction")
+	return fserror.ErrOperationNotSupported
 }

+ 3 - 5
mod/filesystem/abstractions/localfs/localfs.go

@@ -1,15 +1,14 @@
 package localfs
 
 import (
-	"errors"
 	"io"
-	"log"
 	"os"
 	"path/filepath"
 	"strings"
 	"time"
 
 	"imuslab.com/arozos/mod/common"
+	"imuslab.com/arozos/mod/filesystem/fserror"
 )
 
 /*
@@ -91,7 +90,7 @@ func (l LocalFileSystemAbstraction) VirtualPathToRealPath(subpath string, userna
 	} else if l.Hierarchy == "public" {
 		return filepath.ToSlash(filepath.Join(l.Rootpath, subpath)), nil
 	}
-	return "", errors.New("unsupported filesystem hierarchy")
+	return "", fserror.ErrVpathResolveFailed
 }
 
 func (l LocalFileSystemAbstraction) RealPathToVirtualPath(fullpath string, username string) (string, error) {
@@ -150,7 +149,7 @@ func (l LocalFileSystemAbstraction) RealPathToVirtualPath(fullpath string, usern
 	*/
 	fullpath = filepath.ToSlash(fullpath)
 	if strings.HasPrefix(fullpath, l.UUID+":") && !common.FileExists(fullpath) {
-		return "", errors.New("invalid usage: Given path is vpath")
+		return "", fserror.ErrRpathResolveFailed
 	}
 	prefix := filepath.ToSlash(filepath.Join(l.Rootpath, "users", username))
 	if l.Hierarchy == "public" {
@@ -172,7 +171,6 @@ func (l LocalFileSystemAbstraction) IsDir(realpath string) bool {
 	}
 	fi, err := l.Stat(realpath)
 	if err != nil {
-		log.Fatal(err)
 		return false
 	}
 	switch mode := fi.Mode(); {

+ 30 - 8
mod/filesystem/abstractions/sharefs/sharefs.go

@@ -5,7 +5,10 @@ import (
 	"io"
 	"os"
 	"path/filepath"
+	"strings"
 	"time"
+
+	"imuslab.com/arozos/mod/filesystem/fserror"
 )
 
 /*
@@ -16,10 +19,15 @@ import (
 */
 
 type ShareFileSystemAbstraction struct {
+	UUID                     string
+	ShareUUIDToVpathResolver func(string) (string, error)
 }
 
-func NewShareFileSystemAbstraction() ShareFileSystemAbstraction {
-	return ShareFileSystemAbstraction{}
+func NewShareFileSystemAbstraction(uuid string, resolverFunction func(string) (string, error)) ShareFileSystemAbstraction {
+	return ShareFileSystemAbstraction{
+		UUID:                     uuid,
+		ShareUUIDToVpathResolver: resolverFunction,
+	}
 }
 
 func (l ShareFileSystemAbstraction) Chmod(filename string, mode os.FileMode) error {
@@ -67,7 +75,21 @@ func (l ShareFileSystemAbstraction) Stat(filename string) (os.FileInfo, error) {
 */
 
 func (l ShareFileSystemAbstraction) VirtualPathToRealPath(subpath string, username string) (string, error) {
-	return "", errors.New("SHAREFS WORK IN PROGRESS")
+	subpath = filepath.ToSlash(filepath.Clean(subpath))
+	if strings.HasPrefix(subpath, l.UUID+":") {
+		subpath = strings.TrimPrefix(subpath, l.UUID+":")
+	}
+	subpath = subpath[1:] //Trim off the first / from the subpath
+	uuid := subpath
+	if strings.Contains(subpath, "/") {
+		uuid = strings.Split(subpath, "/")[0]
+	}
+
+	redirectVpath, err := l.ShareUUIDToVpathResolver(uuid)
+	if err != nil {
+		return "", err
+	}
+	return "", fserror.NewRedirectionError(redirectVpath)
 }
 
 func (l ShareFileSystemAbstraction) RealPathToVirtualPath(fullpath string, username string) (string, error) {
@@ -91,22 +113,22 @@ func (l ShareFileSystemAbstraction) GetFileSize(realpath string) int64 {
 }
 
 func (l ShareFileSystemAbstraction) GetModTime(realpath string) (int64, error) {
-	return 0, errors.New("empty filesystem abstraction")
+	return 0, fserror.ErrNullOperation
 }
 
 func (l ShareFileSystemAbstraction) WriteFile(filename string, content []byte, mode os.FileMode) error {
 	return nil
 }
 func (l ShareFileSystemAbstraction) ReadFile(filename string) ([]byte, error) {
-	return []byte(""), errors.New("empty filesystem abstraction")
+	return []byte(""), fserror.ErrNullOperation
 }
 func (l ShareFileSystemAbstraction) WriteStream(filename string, stream io.Reader, mode os.FileMode) error {
-	return errors.New("operation not supported on dummy file system")
+	return fserror.ErrOperationNotSupported
 }
 func (l ShareFileSystemAbstraction) ReadStream(filename string) (io.ReadCloser, error) {
-	return nil, errors.New("operation not supported on dummy file system")
+	return nil, fserror.ErrOperationNotSupported
 }
 
 func (l ShareFileSystemAbstraction) Walk(root string, walkFn filepath.WalkFunc) error {
-	return errors.New("empty filesystem abstraction")
+	return fserror.ErrOperationNotSupported
 }

+ 8 - 1
mod/filesystem/filesystem.go

@@ -224,6 +224,13 @@ func NewFileSystemHandler(option FileSystemOption) (*FileSystemHandler, error) {
 			Closed:                false,
 		}, nil
 	} else if option.Filesystem == "share" {
+		shareFsAbstraction := sharefs.NewShareFileSystemAbstraction(
+			"share",
+			func(s string) (string, error) {
+				log.Println(s)
+				return "user:/", nil
+			},
+		)
 		return &FileSystemHandler{
 			Name:                  option.Name,
 			UUID:                  option.Uuid,
@@ -235,7 +242,7 @@ func NewFileSystemHandler(option FileSystemOption) (*FileSystemHandler, error) {
 			HierarchyConfig:       nil,
 			InitiationTime:        time.Now().Unix(),
 			FilesystemDatabase:    nil,
-			FileSystemAbstraction: sharefs.NewShareFileSystemAbstraction(),
+			FileSystemAbstraction: shareFsAbstraction,
 			Filesystem:            fstype,
 			Closed:                false,
 		}, nil

+ 23 - 0
mod/filesystem/fserror/fserror.go

@@ -0,0 +1,23 @@
+package fserror
+
+/*
+	fserror.go
+
+	This package handle error related to file systems.
+	See comments below for usage.
+*/
+import "errors"
+
+var (
+	//Resolve errors
+	ErrVpathResolveFailed = errors.New("FS_VPATH_RESOLVE_FAILED")
+	ErrRpathResolveFailed = errors.New("FS_RPATH_RESOLVE_FAILED")
+
+	//Operation errors
+	ErrOperationNotSupported = errors.New("FS_OPR_NOT_SUPPORTED")
+	ErrNullOperation         = errors.New("FS_NULL_OPR")
+)
+
+func NewRedirectionError(targetVpath string) error {
+	return errors.New("Redirect:" + targetVpath)
+}

+ 1 - 1
mod/share/share.go

@@ -1192,7 +1192,7 @@ func (s *Manager) ValidateAndClearShares() {
 		pathHash, err := s.GetPathHashFromShare(thisShareOption)
 		if err != nil {
 			//Unable to resolve path hash. Filesystem handler is gone?
-			s.options.ShareEntryTable.RemoveShareByUUID(thisShareOption.UUID)
+			//s.options.ShareEntryTable.RemoveShareByUUID(thisShareOption.UUID)
 			return true
 		}
 		if !s.ShareIsValid(thisShareOption) {

+ 3 - 0
web/SystemAO/file_system/file_explorer.html

@@ -1566,6 +1566,9 @@
                                             listDirectory(currentRoot);
                                         }else if (redirectAction == "userroot"){
                                             listDirectory("user:/")
+                                        }else{
+                                            //Try to breakdown the redirection path
+                                            listDirectory(redirectAction, undefined, false);
                                         }
                                         return
                                     }