Pārlūkot izejas kodu

Added experimental fast-disk space loader

Toby Chui 1 gadu atpakaļ
vecāks
revīzija
b5f16dd152

+ 29 - 10
file_system.go

@@ -5,6 +5,7 @@ import (
 	"encoding/hex"
 	"encoding/json"
 	"errors"
+	"fmt"
 	"io"
 	"io/fs"
 	"log"
@@ -2457,17 +2458,35 @@ func system_fs_getFileProperties(w http.ResponseWriter, r *http.Request) {
 		if fsh.IsNetworkDrive() {
 			filesize = -1
 		} else {
-			var size int64 = 0
-			fshAbs.Walk(rpath, func(_ string, info os.FileInfo, err error) error {
-				if err != nil {
-					return err
-				}
-				if !info.IsDir() {
-					size += info.Size()
+			//Check if du exists
+			usefallback := true //Use fallback
+
+			if fsh.IsLocalDrive() {
+				//Try using native syscall to grab directory size
+				nativeSize, err := filesystem.GetDirectorySizeNative(fsh.Path)
+				if err == nil {
+					usefallback = false
+					filesize = nativeSize
+				} else {
+					fmt.Println(nativeSize)
 				}
-				return err
-			})
-			filesize = size
+			}
+
+			if usefallback {
+				// invalid platform. walk the whole file system
+				var size int64 = 0
+				fshAbs.Walk(rpath, func(_ string, info os.FileInfo, err error) error {
+					if err != nil {
+						return err
+					}
+					if !info.IsDir() {
+						size += info.Size()
+					}
+					return err
+				})
+				filesize = size
+			}
+
 		}
 	}
 

+ 16 - 18
mod/apt/apt.go

@@ -45,26 +45,24 @@ func (a *AptPackageManager) InstallIfNotExists(pkgname string, mustComply bool)
 	//log.Println(packageInfo)
 	if installed {
 		return nil
-	} else {
-		//Package not installed. Install if now if running in sudo mode
-		log.Println("Installing package " + pkgname + "...")
-		cmd := exec.Command("apt-get", "install", "-y", pkgname)
-		cmd.Stdout = os.Stdout
-		cmd.Stderr = os.Stderr
-		err := cmd.Run()
-		if err != nil {
-			if mustComply {
-				//Panic and terminate server process
-				log.Println("Installation failed on package: " + pkgname)
-				os.Exit(1)
-			} else {
-				log.Println("Installation failed on package: " + pkgname)
-			}
-			return err
-		}
-		return nil
 	}
 
+	//Package not installed. Install if now if running in sudo mode
+	log.Println("Installing package " + pkgname + "...")
+	cmd := exec.Command("apt-get", "install", "-y", pkgname)
+	cmd.Stdout = os.Stdout
+	cmd.Stderr = os.Stderr
+	err = cmd.Run()
+	if err != nil {
+		if mustComply {
+			//Panic and terminate server process
+			log.Println("Installation failed on package: " + pkgname)
+			os.Exit(1)
+		} else {
+			log.Println("Installation failed on package: " + pkgname)
+		}
+		return err
+	}
 	return nil
 }
 

+ 15 - 0
mod/filesystem/filesystem.go

@@ -339,6 +339,21 @@ func (fsh *FileSystemHandler) IsNetworkDrive() bool {
 	return arozfs.IsNetworkDrive(fsh.Filesystem)
 }
 
+// Check if a fsh is a local disk drive
+func (fsh *FileSystemHandler) IsLocalDrive() bool {
+	//Check if network drive
+	if arozfs.IsNetworkDrive(fsh.Filesystem) {
+		return false
+	}
+
+	//Check if mounted locally
+	if _, err := os.Stat(fsh.Path); os.IsNotExist(err) {
+		return false
+	}
+
+	return true
+}
+
 // Buffer a file to local tmp folder and return the tmp location for further processing
 func (fsh *FileSystemHandler) BufferRemoteToLocal(rpath string) (string, error) {
 	//Check if the remote file exists

+ 0 - 13
mod/filesystem/fsextend/fsextend.go

@@ -1,13 +0,0 @@
-package fsextend
-
-/*
-	fsextend.go
-
-	This module extend the file system handler function to virtualized / emulated
-	interfaces
-*/
-
-type VirtualizedFileSystemPathResolver interface {
-	VirtualPathToRealPath(string) (string, error)
-	RealPathToVirtualPath(string) (string, error)
-}

+ 48 - 0
mod/filesystem/static.go

@@ -19,6 +19,7 @@ import (
 	"net/url"
 
 	mimetype "github.com/gabriel-vasile/mimetype"
+	"imuslab.com/arozos/mod/apt"
 	"imuslab.com/arozos/mod/filesystem/arozfs"
 	"imuslab.com/arozos/mod/filesystem/shortcut"
 )
@@ -267,6 +268,53 @@ func WGlob(path string) ([]string, error) {
 	return files, nil
 }
 
+/*
+Get Directory Size with native syscall (local drive only)
+faster than GetDirectorySize if system support du
+*/
+func GetDirectorySizeNative(filename string) (int64, error) {
+	d, err := apt.PackageExists("du")
+	if err != nil || !d {
+		return 0, err
+	}
+
+	//Convert the filename to absolute path
+	abspath, err := filepath.Abs(filename)
+	if err != nil {
+		return 0, err
+	}
+
+	//du command exists
+	//use native syscall to get disk size
+	cmd := exec.Command("du", "-sb", abspath)
+	out, err := cmd.CombinedOutput()
+	if err == nil {
+		return 0, err
+	}
+
+	//Return value is something like 481491222874    /media/storage2
+	//We need to trim off the spaces
+	tmp := string(out)
+	tmp = strings.TrimSpace(tmp)
+	for strings.Contains(tmp, "  ") {
+		tmp = strings.ReplaceAll(tmp, "  ", " ")
+	}
+
+	chunks := strings.Split(tmp, " ")
+	if len(chunks) != 2 {
+		return 0, errors.New("malformed output")
+	}
+
+	//The first chunk should be the size in bytes
+	size, err := strconv.Atoi(chunks[0])
+	if err != nil {
+		return 0, errors.New("malformed output")
+	}
+
+	return int64(size), nil
+
+}
+
 /*
 Get Directory size, require filepath and include Hidden files option(true / false)
 Return total file size and file count

+ 0 - 5
web/Speedtest/init.agi

@@ -1,8 +1,3 @@
-/*
-	Unit Testing Module for AGI 
-
-*/
-
 
 //Define the launchInfo for the module
 var moduleLaunchInfo = {