Browse Source

Fixed path issue for Go 1.21 or above

TC pushbot 5 1 year ago
parent
commit
4a77a00b26
4 changed files with 13 additions and 66 deletions
  1. 0 40
      mod/agi/agi.file.go
  2. 4 6
      mod/filesystem/arozfs/arozfs.go
  3. 5 2
      mod/filesystem/static.go
  4. 4 18
      mod/user/internal.go

+ 0 - 40
mod/agi/agi.file.go

@@ -171,46 +171,6 @@ func (g *Gateway) injectFileLibFunctions(vm *otto.Otto, u *user.User, scriptFsh
 		return reply
 	})
 
-	//Listdir
-	//readdir("user:/Desktop") => return filelist in array
-	/*
-		vm.Set("_filelib_readdir", func(call otto.FunctionCall) otto.Value {
-			vpath, err := call.Argument(0).ToString()
-			if err != nil {
-				g.raiseError(err)
-				return otto.FalseValue()
-			}
-
-			//Translate the virtual path to realpath
-			fsh, rpath, err := virtualPathToRealPath(vpath, u)
-			if err != nil {
-				g.raiseError(err)
-				return otto.FalseValue()
-			}
-			fshAbs := fsh.FileSystemAbstraction
-
-			rpath = filepath.ToSlash(filepath.Clean(rpath)) + "/*"
-			fileList, err := fshAbs.Glob(rpath)
-			if err != nil {
-				g.raiseError(err)
-				return otto.FalseValue()
-			}
-
-			//Translate all paths to virtual paths
-			results := []string{}
-			for _, file := range fileList {
-				isHidden, _ := hidden.IsHidden(file, true)
-				if !isHidden {
-					thisRpath, _ := fshAbs.RealPathToVirtualPath(file, u.Username)
-					results = append(results, thisRpath)
-				}
-			}
-
-			reply, _ := vm.ToValue(results)
-			return reply
-		})
-	*/
-
 	//Usage
 	//filelib.walk("user:/") => list everything recursively
 	//filelib.walk("user:/", "folder") => list all folder recursively

+ 4 - 6
mod/filesystem/arozfs/arozfs.go

@@ -96,24 +96,22 @@ func GetSupportedFileSystemTypes() []string {
 
 // Generic virtual path to real path translator
 func GenericVirtualPathToRealPathTranslator(uuid string, hierarchy string, subpath string, username string) (string, error) {
-	subpath = ToSlash(filepath.Clean(subpath))
 	subpath = ToSlash(filepath.Clean(strings.TrimSpace(subpath)))
-	if strings.HasPrefix(subpath, "./") {
-		subpath = subpath[1:]
-	}
+	subpath = strings.TrimPrefix(subpath, "./")
 
 	if subpath == "." || subpath == "" {
 		subpath = "/"
 	}
+
 	if strings.HasPrefix(subpath, uuid+":") {
 		//This is full virtual path. Trim the uuid and correct the subpath
 		subpath = strings.TrimPrefix(subpath, uuid+":")
 	}
 
 	if hierarchy == "user" {
-		return filepath.ToSlash(filepath.Clean(filepath.Join("users", username, subpath))), nil
+		return ToSlash(filepath.Clean(filepath.Join("users", username, subpath))), nil
 	} else if hierarchy == "public" {
-		return filepath.ToSlash(filepath.Clean(subpath)), nil
+		return ToSlash(filepath.Clean(subpath)), nil
 	}
 	return "", errors.New("unsupported filesystem hierarchy")
 }

+ 5 - 2
mod/filesystem/static.go

@@ -103,13 +103,16 @@ func GetIDFromVirtualPath(vpath string) (string, string, error) {
 	}
 
 	//Clean up the virutal path
-	vpath = filepath.ToSlash(filepath.Clean(vpath))
+	vpath = arozfs.ToSlash(filepath.Clean(vpath))
 
 	tmp := strings.Split(vpath, ":")
 	vdID := tmp[0]
+	if strings.HasPrefix(vdID, "./") {
+		//For newer go version where Clean return with ./ prefix
+		vdID = strings.TrimPrefix(vdID, "./")
+	}
 	pathSlice := tmp[1:]
 	path := strings.Join(pathSlice, ":")
-
 	return vdID, path, nil
 }
 

+ 4 - 18
mod/user/internal.go

@@ -2,8 +2,6 @@ package user
 
 import (
 	"errors"
-	"path/filepath"
-	"strings"
 
 	fs "imuslab.com/arozos/mod/filesystem"
 )
@@ -12,7 +10,7 @@ import (
 	Private functions
 */
 
-//Get a fs handler from a virtual path, quick function for getIDFromHandler + GetHandlerFromID
+// Get a fs handler from a virtual path, quick function for getIDFromHandler + GetHandlerFromID
 func getHandlerFromVirtualPath(storages []*fs.FileSystemHandler, vpath string) (*fs.FileSystemHandler, error) {
 	vid, _, err := getIDFromVirtualPath(vpath)
 	if err != nil {
@@ -22,7 +20,7 @@ func getHandlerFromVirtualPath(storages []*fs.FileSystemHandler, vpath string) (
 	return getHandlerFromID(storages, vid)
 }
 
-//Get a fs handler from the given virtial device id
+// Get a fs handler from the given virtial device id
 func getHandlerFromID(storages []*fs.FileSystemHandler, vid string) (*fs.FileSystemHandler, error) {
 	for _, storage := range storages {
 		if storage.UUID == vid {
@@ -34,19 +32,7 @@ func getHandlerFromID(storages []*fs.FileSystemHandler, vid string) (*fs.FileSys
 	return &fs.FileSystemHandler{}, errors.New("handler Not Found")
 }
 
-//Get the ID part of a virtual path, return ID, subpath and error
+// Get the ID part of a virtual path, return ID, subpath and error
 func getIDFromVirtualPath(vpath string) (string, string, error) {
-	if !strings.Contains(vpath, ":") {
-		return "", "", errors.New("path missing Virtual Device ID. Given: " + vpath)
-	}
-
-	//Clean up the virutal path
-	vpath = filepath.ToSlash(filepath.Clean(vpath))
-
-	tmp := strings.Split(vpath, ":")
-	vdID := tmp[0]
-	pathSlice := tmp[1:]
-	path := strings.Join(pathSlice, ":")
-
-	return vdID, path, nil
+	return fs.GetIDFromVirtualPath(vpath)
 }