refactor: name dirs by what they hold, trim exports (#692)

Bare dir now always means a user data dir, so the snapshot root, the export listing, and the
keys.json kind get their own words; names used only inside their package are no longer exported.
This commit is contained in:
Roger authored and GitHub committed 2026-11-10 17:00:43 +08:00
1 parent 2e7ee3c69f
commit a7741eaab2
40 files changed
+242 -256

No files matched your search

+8 -8
View File
@@ -94,7 +94,7 @@ func discoverProfiles(dir browser.UserDataDir) ([]browser.Profile, error) {
var profiles []browser.Profile
for _, entry := range entries {
if entry.IsDir() && !isSkippedDir(entry.Name()) && isProfileDir(filepath.Join(dir.Path, entry.Name())) {
if entry.IsDir() && !isSkippedProfileDir(entry.Name()) && isProfileDir(filepath.Join(dir.Path, entry.Name())) {
profiles = append(profiles, browser.Profile{Name: entry.Name(), RelativePath: entry.Name()})
}
}
@@ -110,23 +110,23 @@ func discoverProfiles(dir browser.UserDataDir) ([]browser.Profile, error) {
// Copied or restored trees may lack the marker, which no category reads, so a subdir with profile files is one too.
for _, entry := range entries {
if entry.IsDir() && !isSkippedDir(entry.Name()) && hasAnyFile(filepath.Join(dir.Path, entry.Name()), profileFiles) {
if entry.IsDir() && !isSkippedProfileDir(entry.Name()) && hasAnyFile(filepath.Join(dir.Path, entry.Name()), profileFiles) {
profiles = append(profiles, browser.Profile{Name: entry.Name(), RelativePath: entry.Name()})
}
}
return profiles, nil
}
func isProfileDir(dir string) bool {
func isProfileDir(path string) bool {
for _, name := range profileMarkers {
if _, err := os.Stat(filepath.Join(dir, name)); err == nil {
if _, err := os.Stat(filepath.Join(path, name)); err == nil {
return true
}
}
return false
}
func isSkippedDir(name string) bool {
func isSkippedProfileDir(name string) bool {
switch name {
case "System Profile", "Guest Profile", "Snapshot":
return true
@@ -146,9 +146,9 @@ func profileFilePaths(dir browser.UserDataDir) []string {
return paths
}
func hasAnyFile(dir string, paths []string) bool {
for _, relativePath := range paths {
if _, err := os.Stat(filepath.Join(dir, filepath.FromSlash(relativePath))); err == nil {
func hasAnyFile(path string, relativePaths []string) bool {
for _, relativePath := range relativePaths {
if _, err := os.Stat(filepath.Join(path, filepath.FromSlash(relativePath))); err == nil {
return true
}
}
+9 -9
View File
@@ -7,13 +7,13 @@ import (
"github.com/moond4rk/hackbrowserdata/internal/category"
)
// kindChromium is the keys.json kind of every Chromium browser no override covers.
const kindChromium = "chromium"
// vaultKindChromium is the keys.json kind of every Chromium browser no override covers.
const vaultKindChromium = "chromium"
// override is a fork that stores data its own way: its browsers, its keys.json kind, and the extractors it swaps.
type override struct {
browserNames []string
kind string
vaultKind string
extractors browser.Extractors
}
@@ -21,7 +21,7 @@ type override struct {
var overrides = []override{
{
browserNames: []string{"yandex"},
kind: "chromium-yandex",
vaultKind: "chromium-yandex",
extractors: browser.Extractors{
category.Password: newDecryptingExtractor(extractYandexPasswords, yaPassmanData),
category.CreditCard: newDecryptingExtractor(extractYandexCreditCards, yaCreditCards),
@@ -29,7 +29,7 @@ var overrides = []override{
},
{
browserNames: []string{"opera", "opera-gx", "vought"},
kind: "chromium-opera",
vaultKind: "chromium-opera",
extractors: browser.Extractors{
category.Extension: newPlainExtractor(extractOperaExtensions, securePreferences),
},
@@ -50,18 +50,18 @@ func overrideFor(browserName string) (override, bool) {
// VaultKind names a vault's on-disk format, so a reader without the browser in its table still picks its files.
func VaultKind(chromiumBrowser browser.Browser) string {
if fork, ok := overrideFor(chromiumBrowser.Name); ok {
return fork.kind
return fork.vaultKind
}
return kindChromium
return vaultKindChromium
}
// BrowserNameForKind names the browser whose extractors read a vault of kind; a plain Chromium vault keeps its own.
func BrowserNameForKind(kind, name string) (string, error) {
if kind == kindChromium {
if kind == vaultKindChromium {
return name, nil
}
for _, fork := range overrides {
if fork.kind == kind {
if fork.vaultKind == kind {
return fork.browserNames[0], nil
}
}
+6 -6
View File
@@ -25,11 +25,11 @@ func TestOverrides_NameChromiumBrowsers(t *testing.T) {
// TestOverrides_KindsAndNamesAreUnique keeps BrowserNameForKind and overrideFor from depending on table order.
func TestOverrides_KindsAndNamesAreUnique(t *testing.T) {
kinds := map[string]bool{kindChromium: true}
kinds := map[string]bool{vaultKindChromium: true}
names := map[string]bool{}
for _, fork := range overrides {
assert.Falsef(t, kinds[fork.kind], "kind %q is used twice", fork.kind)
kinds[fork.kind] = true
assert.Falsef(t, kinds[fork.vaultKind], "kind %q is used twice", fork.vaultKind)
kinds[fork.vaultKind] = true
for _, name := range fork.browserNames {
assert.Falsef(t, names[name], "browser %q is in two overrides", name)
names[name] = true
@@ -42,7 +42,7 @@ func TestVaultKind_RoundTripsEveryFork(t *testing.T) {
for _, fork := range overrides {
for _, name := range fork.browserNames {
kind := VaultKind(browser.Browser{Name: name})
require.Equalf(t, fork.kind, kind, "override %q", name)
require.Equalf(t, fork.vaultKind, kind, "override %q", name)
forkBrowserName, err := BrowserNameForKind(kind, "unknown")
require.NoError(t, err)
restored, ok := overrideFor(forkBrowserName)
@@ -50,8 +50,8 @@ func TestVaultKind_RoundTripsEveryFork(t *testing.T) {
assert.Equalf(t, categoriesOf(fork.extractors), categoriesOf(restored.extractors), "kind %s of %q", kind, name)
}
}
assert.Equal(t, kindChromium, VaultKind(browser.Browser{Name: "chrome"}))
plainBrowserName, err := BrowserNameForKind(kindChromium, "unknown")
assert.Equal(t, vaultKindChromium, VaultKind(browser.Browser{Name: "chrome"}))
plainBrowserName, err := BrowserNameForKind(vaultKindChromium, "unknown")
require.NoError(t, err)
assert.Equal(t, "unknown", plainBrowserName)
_, err = BrowserNameForKind("bogus", "unknown")
+4 -4
View File
@@ -57,15 +57,15 @@ func TestProfileSnapshotPath_FailedCopyIsReported(t *testing.T) {
require.NoError(t, os.MkdirAll(filepath.Join(from, "Default", "Network", "Cookies"), 0o700))
writeSnapshotFile(t, from, "Default/Cookies")
snapshotDir, err := snapshot.Temp()
root, err := snapshot.Temp()
require.NoError(t, err)
defer func() { require.NoError(t, snapshotDir.Close()) }()
require.NoError(t, snapshotDir.Capture(context.Background(), []snapshot.File{
defer func() { require.NoError(t, root.Close()) }()
require.NoError(t, root.Capture(context.Background(), []snapshot.File{
{UserDataDirID: "chrome", UserDataDirPath: from, RelativePath: "Default/Network/Cookies"},
{UserDataDirID: "chrome", UserDataDirPath: from, RelativePath: "Default/Cookies"},
}))
profileSnapshot := NewProfileSnapshot(snapshotDir.Snapshot("chrome"), Profile{Name: "Default", RelativePath: "Default"}, nil)
profileSnapshot := NewProfileSnapshot(root.Snapshot("chrome"), Profile{Name: "Default", RelativePath: "Default"}, nil)
_, err = profileSnapshot.Path(Source{Path: "Network/Cookies", Fallbacks: []string{"Cookies"}})
require.Error(t, err)
assert.NotErrorIs(t, err, fs.ErrNotExist)
+2 -2
View File
@@ -93,14 +93,14 @@ func discoverProfiles(dir browser.UserDataDir) ([]browser.Profile, error) {
return profiles, nil
}
func hasDataFile(dir string) bool {
func hasDataFile(path string) bool {
for _, extractor := range defaultExtractors {
for _, source := range extractor.Sources {
if source.Optional {
continue
}
for _, relativePath := range source.Paths() {
if _, err := os.Stat(filepath.Join(dir, filepath.FromSlash(relativePath))); err == nil {
if _, err := os.Stat(filepath.Join(path, filepath.FromSlash(relativePath))); err == nil {
return true
}
}
+5 -5
View File
@@ -39,21 +39,21 @@ func namedProfiles(library string) []browser.Profile {
// readNamedProfilesFromDB reads a copy, never Safari's live file; (nil, err) means unreadable, and any slice is final.
func readNamedProfilesFromDB(library string) ([]browser.Profile, error) {
snapshotDir, err := snapshot.Temp()
root, err := snapshot.Temp()
if err != nil {
return nil, err
}
defer func() {
if err := snapshotDir.Close(); err != nil {
if err := root.Close(); err != nil {
log.Warnf("%v", err)
}
}()
if err := snapshotDir.Capture(context.Background(), []snapshot.File{{
if err := root.Capture(context.Background(), []snapshot.File{{
UserDataDirID: discoveryUserDataDirID, UserDataDirPath: library, RelativePath: tabsDatabasePath,
}}); err != nil {
return nil, fmt.Errorf("copy SafariTabs.db: %w", err)
}
path, err := snapshotDir.Snapshot(discoveryUserDataDirID).Path(tabsDatabasePath)
path, err := root.Snapshot(discoveryUserDataDirID).Path(tabsDatabasePath)
if err != nil {
return nil, fmt.Errorf("copy SafariTabs.db: %w", err)
}
@@ -109,7 +109,7 @@ func readNamedProfilesFromDir(library string) []browser.Profile {
return out
}
// newNamedProfile sets Dir to the profile's own History.db directory, so its UUID travels with it to Extractors.
// newNamedProfile sets RelativePath to the profile's own History.db directory, so its UUID travels with it to Extractors.
func newNamedProfile(upperUUID, title string) browser.Profile {
return browser.Profile{Name: resolveProfileName(title, upperUUID), RelativePath: namedProfilesDir + "/" + upperUUID}
}