Skip to content

Commit 330f8c7

Browse files
authored
Use diskutil on macOS to extract volume name and path for FAT mounts (#4928)
* Use diskutil on macOS to extract volume name and path for FAT mounts * Improve FAT mount detection on macOS by parsing diskutil output into a map * Simplify conditional check
1 parent e238eb2 commit 330f8c7

File tree

1 file changed

+42
-7
lines changed

1 file changed

+42
-7
lines changed

main.go

Lines changed: 42 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1013,15 +1013,50 @@ func findFATMounts(options *compileopts.Options) ([]mountPoint, error) {
10131013
return nil, fmt.Errorf("could not list mount points: %w", err)
10141014
}
10151015
for _, elem := range list {
1016-
// TODO: find a way to check for the filesystem type.
1017-
// (Only return FAT filesystems).
1018-
points = append(points, mountPoint{
1019-
name: elem.Name(),
1020-
path: filepath.Join("/Volumes", elem.Name()),
1021-
})
1016+
volumePath := filepath.Join("/Volumes", elem.Name())
1017+
if _, err := os.Stat(volumePath); err != nil {
1018+
continue
1019+
}
1020+
1021+
cmd := exec.Command("diskutil", "info", volumePath)
1022+
var out bytes.Buffer
1023+
cmd.Stdout = &out
1024+
if err := cmd.Run(); err != nil {
1025+
continue // skip if diskutil failed
1026+
}
1027+
1028+
diskInfo := map[string]string{}
1029+
scanner := bufio.NewScanner(&out)
1030+
for scanner.Scan() {
1031+
line := scanner.Text()
1032+
parts := strings.SplitN(line, ":", 2)
1033+
if len(parts) != 2 {
1034+
continue
1035+
}
1036+
key := strings.TrimSpace(parts[0])
1037+
value := strings.TrimSpace(parts[1])
1038+
diskInfo[key] = value
1039+
}
1040+
if err := scanner.Err(); err != nil {
1041+
continue
1042+
}
1043+
1044+
volName, okv := diskInfo["Volume Name"]
1045+
fsType, okf := diskInfo["File System Personality"]
1046+
if !okv || !okf {
1047+
continue
1048+
}
1049+
1050+
// Check if a filesystem type is FAT-based
1051+
if strings.Contains(strings.ToUpper(fsType), "FAT") {
1052+
points = append(points, mountPoint{
1053+
name: volName,
1054+
path: volumePath,
1055+
})
1056+
}
10221057
}
10231058
sort.Slice(points, func(i, j int) bool {
1024-
return points[i].path < points[j].name
1059+
return points[i].path < points[j].path
10251060
})
10261061
return points, nil
10271062
case "linux":

0 commit comments

Comments
 (0)