-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfileutils.go
80 lines (71 loc) · 2.17 KB
/
fileutils.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
// helperFunctions
// Written by J.F. Gratton <[email protected]>
// Original filename: /fileutils.go
// Original timestamp: 2024/06/22 13:24
package helperFunctions
import (
cerr "github.com/jeanfrancoisgratton/customError"
"os"
"runtime"
"strings"
)
// Check which type of filesystem the mountpoint is.
// Currently only supports MacOS and linux; I've broken my Windows test VM and have no time for it
func GetFStype(mountpoint string) (string, *cerr.CustomError) {
switch os := runtime.GOOS; os {
case "darwin":
return getMacOSMountPointType(mountpoint)
case "linux":
return getLinuxMountPointType(mountpoint)
default:
return "", &cerr.CustomError{Fatality: cerr.Continuable,
Title: "Unsupported operating system", Message: "os type: " + os,
}
}
}
// Gets the type of the filesystem by reading the /proc/mounts
func getLinuxMountPointType(mountpoint string) (string, *cerr.CustomError) {
data, err := os.ReadFile("/proc/mounts")
if err != nil {
return "", &cerr.CustomError{Title: "Error reading /proc/mounts"}
}
lines := strings.Split(string(data), "\n")
for _, line := range lines {
if line == "" {
continue
}
fields := strings.Fields(line)
if len(fields) < 3 {
continue
}
if fields[1] == mountpoint {
return fields[2], nil
}
}
return "", &cerr.CustomError{Fatality: cerr.Continuable, Title: "mountpoint not found"}
}
func getMacOSMountPointType(mountpoint string) (string, *cerr.CustomError) {
return "", &cerr.CustomError{Fatality: cerr.Continuable, Title: "WIP", Message: "Unsupported for now.. stay tuned"}
//var stat unix.Statfs_t
//err := unix.Statfs(mountpoint, &stat)
//if err != nil {
// return "", &cerr.CustomError{Title: "Error with Statfs syscall", Message: err.Error()}
//}
//
//// The Type field indicates the file system type
//fsType := stat.Type
//
//// Mapping file system type to a human-readable string
//fsTypeName := ""
//switch fsType {
//case unix.MNT_ASYNC:
// fsTypeName = "async"
//case unix.MNT_LOCAL:
// fsTypeName = "local"
//// Add more cases as needed for different file system types
//default:
// fsTypeName = fmt.Sprintf("unknown (%d)", fsType)
//}
//
//return fsTypeName, nil
}