From df911911703ec29961bcfc6bc0829bb982611147 Mon Sep 17 00:00:00 2001 From: Ibrahim Ansari Date: Wed, 6 Nov 2024 03:53:47 +0530 Subject: [PATCH] Add device unmount logic for macOS/Linux --- app/devices_darwin.go | 8 ++++++-- app/devices_unix.go | 16 +++++++++++++++- 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/app/devices_darwin.go b/app/devices_darwin.go index d20cff1..fa6b49d 100644 --- a/app/devices_darwin.go +++ b/app/devices_darwin.go @@ -62,13 +62,17 @@ func GetDevices() ([]Device, error) { // UnmountDevice unmounts a block device's partitons before flashing to it. func UnmountDevice(device string) error { - // Check if device is mounted. + // Check if device exists. stat, err := os.Stat(device) if err != nil { return err } else if stat.Mode().Type()&fs.ModeDevice == 0 { return ErrNotBlockDevice } - // FIXME: Discover device partitions and recursively unmount them. + // Unmount all partitions of disk using `diskutil`. + _, err = exec.Command("diskutil", "unmountDisk", device).Output() + if err != nil { + return err + } return nil } diff --git a/app/devices_unix.go b/app/devices_unix.go index 086eee1..7bbd86c 100644 --- a/app/devices_unix.go +++ b/app/devices_unix.go @@ -71,6 +71,20 @@ func UnmountDevice(device string) error { } else if stat.Mode().Type()&fs.ModeDevice == 0 { return ErrNotBlockDevice } - // FIXME: Discover device partitions and recursively unmount them. + // Discover mounted device partitions. + mounts, err := exec.Command("mount").Output() + if err != nil { + return err + } + // Unmount device partitions. + for _, mount := range strings.Split(string(mounts), "\n") { + if strings.HasPrefix(mount, device) { + partition := strings.Fields(mount)[0] + err = exec.Command("umount", partition).Run() + if err != nil { + return err + } + } + } return nil }