Browse Source

Integrated du into the arozos core

TC pushbot 5 4 years ago
parent
commit
2fe1ef02f9

+ 44 - 0
mod/storage/du/diskusage.go

@@ -0,0 +1,44 @@
+// +build !windows
+
+package du
+
+import "syscall"
+
+// DiskUsage contains usage data and provides user-friendly access methods
+type DiskUsage struct {
+	stat *syscall.Statfs_t
+}
+
+// NewDiskUsages returns an object holding the disk usage of volumePath
+// or nil in case of error (invalid path, etc)
+func NewDiskUsage(volumePath string) *DiskUsage {
+
+	var stat syscall.Statfs_t
+	syscall.Statfs(volumePath, &stat)
+	return &DiskUsage{&stat}
+}
+
+// Free returns total free bytes on file system
+func (du *DiskUsage) Free() uint64 {
+	return du.stat.Bfree * uint64(du.stat.Bsize)
+}
+
+// Available return total available bytes on file system to an unprivileged user
+func (du *DiskUsage) Available() uint64 {
+	return du.stat.Bavail * uint64(du.stat.Bsize)
+}
+
+// Size returns total size of the file system
+func (du *DiskUsage) Size() uint64 {
+	return uint64(du.stat.Blocks) * uint64(du.stat.Bsize)
+}
+
+// Used returns total bytes used in file system
+func (du *DiskUsage) Used() uint64 {
+	return du.Size() - du.Free()
+}
+
+// Usage returns percentage of use on the file system
+func (du *DiskUsage) Usage() float32 {
+	return float32(du.Used()) / float32(du.Size())
+}

+ 17 - 0
mod/storage/du/diskusage_test.go

@@ -0,0 +1,17 @@
+package du
+
+import (
+	"fmt"
+	"testing"
+)
+
+var KB = uint64(1024)
+
+func TestNewDiskUsage(t *testing.T) {
+	usage := NewDiskUsage(".")
+	fmt.Println("Free:", usage.Free()/(KB*KB))
+	fmt.Println("Available:", usage.Available()/(KB*KB))
+	fmt.Println("Size:", usage.Size()/(KB*KB))
+	fmt.Println("Used:", usage.Used()/(KB*KB))
+	fmt.Println("Usage:", usage.Usage()*100, "%")
+}

+ 55 - 0
mod/storage/du/diskusage_windows.go

@@ -0,0 +1,55 @@
+package du
+
+import (
+	"syscall"
+	"unsafe"
+)
+
+type DiskUsage struct {
+	freeBytes  int64
+	totalBytes int64
+	availBytes int64
+}
+
+// NewDiskUsages returns an object holding the disk usage of volumePath
+// or nil in case of error (invalid path, etc)
+func NewDiskUsage(volumePath string) *DiskUsage {
+
+	h := syscall.MustLoadDLL("kernel32.dll")
+	c := h.MustFindProc("GetDiskFreeSpaceExW")
+
+	du := &DiskUsage{}
+
+	c.Call(
+		uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(volumePath))),
+		uintptr(unsafe.Pointer(&du.freeBytes)),
+		uintptr(unsafe.Pointer(&du.totalBytes)),
+		uintptr(unsafe.Pointer(&du.availBytes)))
+
+	return du
+}
+
+// Free returns total free bytes on file system
+func (du *DiskUsage) Free() uint64 {
+	return uint64(du.freeBytes)
+}
+
+// Available returns total available bytes on file system to an unprivileged user
+func (du *DiskUsage) Available() uint64 {
+	return uint64(du.availBytes)
+}
+
+// Size returns total size of the file system
+func (du *DiskUsage) Size() uint64 {
+	return uint64(du.totalBytes)
+}
+
+// Used returns total bytes used in file system
+func (du *DiskUsage) Used() uint64 {
+	return du.Size() - du.Free()
+}
+
+// Usage returns percentage of use on the file system
+func (du *DiskUsage) Usage() float32 {
+	return float32(du.Used()) / float32(du.Size())
+}

+ 6 - 8
mod/storage/static.go

@@ -1,18 +1,16 @@
 package storage
 
+import du "imuslab.com/arozos/mod/storage/du"
 
-import (
-	du "github.com/ricochet2200/go-disk-usage/du"
-)
 /*
 	File for putting return structs
 
 */
 
-func GetDriveCapacity(drive string) (uint64, uint64, uint64){
+func GetDriveCapacity(drive string) (uint64, uint64, uint64) {
 	usage := du.NewDiskUsage(drive)
-	free := usage.Free();
-	total := usage.Size();
-	avi := usage.Available();
+	free := usage.Free()
+	total := usage.Size()
+	avi := usage.Available()
 	return free, total, avi
-}
+}