package bsdaily import ( "fmt" "log/slog" "os" "path/filepath" "sort" "time" ) func FindLatestDailySnapshot() (dir string, snapshotDate time.Time, err error) { entries, err := os.ReadDir(SnapshotBase) if err != nil { return "", time.Time{}, fmt.Errorf("reading snapshot directory %s: %w", SnapshotBase, err) } type snapshot struct { name string date time.Time } var snapshots []snapshot for _, e := range entries { if !e.IsDir() { continue } m := snapshotPattern.FindStringSubmatch(e.Name()) if m == nil { continue } d, err := time.Parse("2006-01-02", m[1]) if err != nil { slog.Warn("skipping snapshot with unparseable date", "name", e.Name(), "error", err) continue } snapshots = append(snapshots, snapshot{name: e.Name(), date: d}) } if len(snapshots) == 0 { return "", time.Time{}, fmt.Errorf("no daily snapshots found in %s", SnapshotBase) } sort.Slice(snapshots, func(i, j int) bool { return snapshots[i].date.After(snapshots[j].date) }) latest := snapshots[0] dir = filepath.Join(SnapshotBase, latest.name) dbPath := filepath.Join(dir, DBFilename) if _, err := os.Stat(dbPath); err != nil { return "", time.Time{}, fmt.Errorf("database not found in snapshot %s: %w", dir, err) } return dir, latest.date, nil }