Files
up_go/utils/MountDevice.go
T
2026-03-19 14:07:10 +07:00

56 lines
1.3 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
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
}