initial commit

This commit is contained in:
wt committed 2026-03-19 14:07:10 +07:00
commit 85d50ee958
26 files changed
+2146

No files matched your search

+55
View File
@@ -0,0 +1,55 @@
package utils
import (
"bufio"
"fmt"
"os"
"syscall"
)
// mount_device checks if the device is already mounted at MOUNT_POINT,
// and if not, attempts to mount it as vfat or ext4.
// Returns 0 on success, -1 on error.
func MountDevice() int {
var DEVICE = os.Getenv("DEVICE")
var MOUNT_POINT = os.Getenv("MOUNT_POINT")
if EnsureDir(MOUNT_POINT) < 0 {
return -1
}
// Check if already mounted
f, err := os.Open("/proc/mounts")
if err == nil {
defer f.Close()
scanner := bufio.NewScanner(f)
for scanner.Scan() {
line := scanner.Text()
// Simple substring check (equivalent to strstr)
if contains(line, MOUNT_POINT) {
return 0
}
}
// Ignore scanner errors for simplicity
}
// Try mounting as vfat, then ext4
if err := syscall.Mount(DEVICE, MOUNT_POINT, "vfat", 0, ""); err != nil {
if err2 := syscall.Mount(DEVICE, MOUNT_POINT, "ext4", 0, ""); err2 != nil {
// Both failed – print error (like perror) and return -1
fmt.Fprintf(os.Stderr, "mount: %v (vfat) / %v (ext4)\n", err, err2)
return -1
}
}
return 0
}
func contains(s, substr string) bool {
// Simple substring check, equivalent to strstr(s, substr) != NULL
// You may also use strings.Contains if imported.
for i := 0; i+len(substr) <= len(s); i++ {
if s[i:i+len(substr)] == substr {
return true
}
}
return false
}