Files
HackBrowserData/internal/app/archive.go
T
Roger fce4f288df refactor: drop the archive manifest (#678)
Restore must read v1.1.0's manifest-less archives from the layout, and that path reads every new archive too,
so the manifest only described the layout a second time.
2026-11-02 11:11:55 +08:00

78 lines
2.1 KiB
Go

package app
import (
"context"
"errors"
"fmt"
"github.com/moond4rk/hackbrowserdata/internal/browser"
"github.com/moond4rk/hackbrowserdata/internal/browser/chromium"
"github.com/moond4rk/hackbrowserdata/internal/log"
"github.com/moond4rk/hackbrowserdata/internal/snapshot"
)
// Archive returns how many files and directories it captured.
func Archive(ctx context.Context, discovery Discovery, options Options, archivePath string) (int, error) {
var dirs []browser.UserDataDir
for _, dir := range discovery.Dirs {
if dir.Browser.Base == browser.Chromium {
dirs = append(dirs, dir)
}
}
files := archiveFiles(NewPlan(Discovery{Dirs: dirs}, options), dirs)
snapshotDir, err := snapshot.Temp()
if err != nil {
return 0, err
}
defer closeSnapshot(snapshotDir)
if err := snapshotDir.Capture(ctx, files); err != nil {
return 0, err
}
fileCount := countCaptured(snapshotDir, files)
if fileCount == 0 {
return 0, errors.New("no decryption-relevant files found to archive")
}
if err := snapshotDir.Zip(archivePath); err != nil {
return 0, fmt.Errorf("write archive %s: %w", archivePath, err)
}
return fileCount, nil
}
func archiveFiles(plan Plan, dirs []browser.UserDataDir) []snapshot.File {
var files []snapshot.File
seen := make(map[snapshot.File]bool)
add := func(file snapshot.File) {
if !seen[file] {
seen[file] = true
files = append(files, file)
}
}
for _, dir := range dirs {
for _, profile := range dir.Profiles {
for _, fileSpec := range chromium.ArchiveFileSpecs(dir) {
if file, ok, err := firstOnDisk(dir, profile, fileSpec); err == nil && ok {
add(file)
}
}
}
for _, file := range filesOf(plan, dir.ID) {
add(file)
}
}
return files
}
// countCaptured counts the files that reached the snapshot, reporting each one whose copy failed.
func countCaptured(snapshotDir *snapshot.Dir, files []snapshot.File) int {
fileCount := 0
for _, file := range files {
if _, err := snapshotDir.Snapshot(file.UserDataDirID).Path(file.RelativePath); err != nil {
log.Warnf("archive: acquire %s/%s: %v", file.UserDataDirID, file.RelativePath, err)
continue
}
fileCount++
}
return fileCount
}