Files
dcf/pkg/dcf/helpers.go
T
sneak 343c9342e4 parse the DCF structure under DCIM (closes #2)
A card is now read by the naming rules in CIPA DC-009-2010, not by
looking for media extensions anywhere on the volume.

Store.Directories holds the DCF directories and their objects, with
directory and file numbers. Store.NonConforming lists everything else
under DCIM. Media under PRIVATE/M4ROOT stays, with no DCF numbers. Media
anywhere else is no longer part of the store.

Model: opus-5-5
2026-11-02 20:14:24 +00:00

79 lines
2.0 KiB
Go

package dcf
import (
"fmt"
"log/slog"
"os"
"path/filepath"
"slices"
"github.com/shirou/gopsutil/disk"
)
func findAllMountPoints() ([]string, error) {
mountpoints := []string{}
partitions, err := disk.Partitions(false) // physical devices only, so false
if err != nil {
return nil, err
}
for _, partition := range partitions {
if !slices.Contains(mountpoints, partition.Mountpoint) {
mountpoints = append(mountpoints, partition.Mountpoint)
}
}
return mountpoints, nil
}
// findDCFMountPoints returns a list of strings of the paths of mountpoints
// that hold a DCF store. Its only argument is an integer of how many mountpoints
// to return. If the argument is 0, all mountpoints are returned.
func findDCFMountPoints(requestedCount int) ([]string, error) {
mountpoints, err := findAllMountPoints()
if err != nil {
return nil, err
}
return filterDCFMountPoints(mountpoints, requestedCount), nil
}
// filterDCFMountPoints returns the first requestedCount of the mountpoints that
// hold a DCF store. If requestedCount is 0, it returns all of them.
func filterDCFMountPoints(mountpoints []string, requestedCount int) []string {
filteredMountpoints := []string{}
for _, mountpoint := range mountpoints {
if !isDCFMountPoint(mountpoint) {
continue
}
slog.Debug(fmt.Sprintf("Keeping mountpoint %s\n", mountpoint))
filteredMountpoints = append(filteredMountpoints, mountpoint)
if (requestedCount > 0) && (len(filteredMountpoints) >= requestedCount) {
break
}
}
return filteredMountpoints
}
// isDCFMountPoint reports whether the root of mountpoint holds a DCIM or
// PRIVATE/M4ROOT directory, which is how we detect DCF stores.
func isDCFMountPoint(mountpoint string) bool {
for _, directory := range []string{dcimDirectory, m4RootDirectory} {
path := filepath.Join(mountpoint, directory)
info, err := os.Stat(path)
if err == nil && info.IsDir() {
slog.Debug(fmt.Sprintf("Found %s directory at %s\n", directory, path))
return true
}
}
return false
}