Files
HackBrowserData/internal/app/plan.go
T
Roger 7e6f325508 refactor(app): per-stage inputs and per-dir planning (#689)
* refactor(app): per-command flags and per-stage inputs

One Options bag fed every stage and cmd patched restore's keys into it after validation; each stage now takes what it
reads, dir-level failures are their own type, and cmd prints what a run exported.

* refactor(app): group the plan by dir and profile

Execute now walks the plan's own shape instead of filtering a flat list and looking profiles up by name.

* refactor(app): file code by command and stage

Pure moves: each command's flags and validation sit beside its stage, and execute.go keeps only the runner.

* refactor: read the keychain password in cmd

Prompting and reading stdin are terminal work, so app now receives the password func; cobra already rejects both flags.

* refactor(app): plan each user data dir right before it runs

A run-wide Plan had to drop its extractors and rebuild them with the keychain; per-dir planning keeps one extractor.
2026-11-09 17:25:36 +08:00

102 lines
3.5 KiB
Go

package app
import (
"errors"
"io/fs"
"os"
"path/filepath"
"github.com/moond4rk/hackbrowserdata/internal/browser"
"github.com/moond4rk/hackbrowserdata/internal/category"
"github.com/moond4rk/hackbrowserdata/internal/snapshot"
)
// dirPlan is what runDir does for one user data dir, decided from which files exist before anything is copied.
type dirPlan struct {
extractions []plannedExtraction // to run, in output order
files []snapshot.File // what they read, each copied once
skipped []Result // requested extractions that will not run, and why
}
// plannedExtraction keeps the extractor that planning checked, so the run uses that same extractor.
type plannedExtraction struct {
Extraction
profile browser.Profile
extractor browser.Extractor
}
type extractorsFunc func(browser.UserDataDir, browser.Profile) browser.Extractors
func planDir(dir browser.UserDataDir, categories []category.Category, extractors extractorsFunc) dirPlan {
var plan dirPlan
captured := make(map[snapshot.File]bool)
for _, profile := range dir.Profiles {
profileExtractors := extractors(dir, profile)
for _, dataCategory := range categories {
extraction := Extraction{UserDataDirID: dir.ID, ProfileName: profile.Name, Category: dataCategory}
extractor, supported := profileExtractors[dataCategory]
if !supported {
plan.skipped = append(plan.skipped, Result{Extraction: extraction, Outcome: OutcomeUnsupported})
continue
}
files, present, err := locate(dir, profile, extractor.Sources)
switch {
case err != nil:
plan.skipped = append(plan.skipped, Result{Extraction: extraction, Outcome: OutcomeError, Err: err})
case !present:
plan.skipped = append(plan.skipped, Result{Extraction: extraction, Outcome: OutcomeAbsent})
default:
plan.extractions = append(plan.extractions, plannedExtraction{Extraction: extraction, profile: profile, extractor: extractor})
plan.files = appendNew(plan.files, captured, files...)
}
}
}
return plan
}
func appendNew(files []snapshot.File, seen map[snapshot.File]bool, more ...snapshot.File) []snapshot.File {
for _, file := range more {
if !seen[file] {
seen[file] = true
files = append(files, file)
}
}
return files
}
// locate needs every required file and one of the profile's own: Local State alone never makes an extraction present.
// One that reads no file always runs, and a candidate that cannot be checked is an error, not absence.
func locate(dir browser.UserDataDir, profile browser.Profile, sources []browser.Source) (files []snapshot.File, present bool, err error) {
if len(sources) == 0 {
return nil, true, nil
}
for _, source := range sources {
file, ok, err := firstOnDisk(dir, profile, source)
switch {
case err != nil:
return nil, false, err
case ok:
files = append(files, file)
present = present || !source.Shared
case !source.Optional:
return nil, false, nil
}
}
return files, present, nil
}
func firstOnDisk(dir browser.UserDataDir, profile browser.Profile, source browser.Source) (snapshot.File, bool, error) {
for _, relativePath := range source.CandidatePaths(profile) {
info, err := os.Stat(filepath.Join(dir.Path, filepath.FromSlash(relativePath)))
switch {
case err == nil && info.IsDir() == source.IsDir:
return snapshot.File{
UserDataDirID: string(dir.ID), UserDataDirPath: dir.Path, RelativePath: relativePath, IsDir: source.IsDir,
}, true, nil
case err != nil && !errors.Is(err, fs.ErrNotExist):
return snapshot.File{}, false, err
}
}
return snapshot.File{}, false, nil
}