Files
Roger fed6c722e2 refactor: open SQLite in one place and stream records (#688)
* refactor: stream records through sqlitex and the collector

A row that failed to scan was logged at debug and dropped, so it never counted toward partial; it now reaches Collector.Fail.
Every SQLite open goes through sqlitex with options instead of a bool, and extractors add records as they read them.

* refactor: read SQLite rows with plain loops

EachRow hid each extractor's loop and its skip rule behind two callbacks; every extractor now opens, queries, and walks
its own rows, so the control flow reads top to bottom.

* refactor(browser): drop the memoizing key closures

Firefox derives its key once before the login loop, and Yandex loads its data key at the first row, so the state sits
in plain variables where the loop uses it instead of inside a closure.

* fix(safari): report unreadable localStorage origins

A broken origin, database or row was only logged at debug level, so it never counted toward a partial result.
2026-11-09 17:25:36 +08:00

78 lines
1.8 KiB
Go

package safari
import (
"fmt"
"os"
"time"
"github.com/moond4rk/plist"
"github.com/moond4rk/hackbrowserdata/internal/browser"
"github.com/moond4rk/hackbrowserdata/internal/record"
)
type bookmarkNode struct {
Type string `plist:"WebBookmarkType"`
Title string `plist:"Title"`
URLString string `plist:"URLString"`
URIDictionary uriDictionary `plist:"URIDictionary"`
Children []bookmarkNode `plist:"Children"`
}
type uriDictionary struct {
Title string `plist:"title"`
}
const (
bookmarkTypeLeaf = "WebBookmarkTypeLeaf"
bookmarkTypeList = "WebBookmarkTypeList"
)
func extractBookmarks(path string, out browser.Collector) error {
file, err := os.Open(path)
if err != nil {
return fmt.Errorf("open bookmarks: %w", err)
}
defer file.Close()
var root bookmarkNode
if err := plist.NewDecoder(file).Decode(&root); err != nil {
return fmt.Errorf("decode bookmarks: %w", err)
}
var leaves int64
walkBookmarks(root.Children, "", &leaves, out)
return nil
}
// walkBookmarks numbers leaves from 1 across all folders: the plist stores no number, and per-folder indexes repeat.
func walkBookmarks(nodes []bookmarkNode, folder string, leaves *int64, out browser.Collector) {
for _, node := range nodes {
switch node.Type {
case bookmarkTypeLeaf:
title := node.URIDictionary.Title
if title == "" {
title = node.Title
}
if node.URLString == "" {
continue
}
*leaves++
out.Add(&record.Bookmark{
ID: *leaves,
Name: title,
URL: node.URLString,
Folder: folder,
Type: "bookmark",
CreatedAt: time.Time{},
})
case bookmarkTypeList:
name := node.Title
if name == "com.apple.ReadingList" {
name = "ReadingList"
}
walkBookmarks(node.Children, name, leaves, out)
}
}
}