mirror of
https://github.com/moonD4rk/HackBrowserData.git
synced 2026-12-04 05:43:27 +01:00
fix: report unreadable sources and tidy the pipeline (#675)
A Stat error other than not-exist was reported as absent, and the --zip archive was readable by other users. The rest is review follow-up: export no longer prints, one runner type, shared name helpers, a trimmed log API.
This commit is contained in:
1 parent
6d6191b4c6
commit
4e98ba7884
33 files changed
+303
-315
No files matched your search
+26
-12
@@ -11,7 +11,6 @@ import (
|
||||
"sort"
|
||||
|
||||
"github.com/moond4rk/hackbrowserdata/internal/category"
|
||||
"github.com/moond4rk/hackbrowserdata/internal/log"
|
||||
"github.com/moond4rk/hackbrowserdata/internal/record"
|
||||
)
|
||||
|
||||
@@ -21,6 +20,19 @@ type Writer struct {
|
||||
compress bool
|
||||
files map[category.Category]*categoryFile
|
||||
failed bool
|
||||
out Output
|
||||
}
|
||||
|
||||
// Output is what a clean Close left in the output dir: each category file in category order, and the zip with --zip.
|
||||
type Output struct {
|
||||
Dir string
|
||||
Files []File
|
||||
Zip string
|
||||
}
|
||||
|
||||
type File struct {
|
||||
Name string
|
||||
Count int // records written
|
||||
}
|
||||
|
||||
type categoryFile struct {
|
||||
@@ -71,12 +83,16 @@ func (w *Writer) write(c category.Category, records []record.Record) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Close finishes the files even after a failed Write, so they keep what was written; only a clean run lists them.
|
||||
// Close finishes the files even after a failed Write, so they keep what was written; only a clean run lists them in Output.
|
||||
func (w *Writer) Close() error {
|
||||
var errs []error
|
||||
var (
|
||||
errs []error
|
||||
files []File
|
||||
)
|
||||
for _, c := range category.All() {
|
||||
if cf, ok := w.files[c]; ok {
|
||||
errs = append(errs, cf.close())
|
||||
files = append(files, File{Name: cf.name, Count: cf.count})
|
||||
}
|
||||
}
|
||||
if err := errors.Join(errs...); err != nil {
|
||||
@@ -91,19 +107,17 @@ func (w *Writer) Close() error {
|
||||
}
|
||||
return nil
|
||||
}
|
||||
fmt.Fprintln(os.Stderr)
|
||||
log.Infof("Exported to %s/", w.dir)
|
||||
for _, c := range category.All() {
|
||||
if cf, ok := w.files[c]; ok {
|
||||
log.Infof(" %-24s %d entries", cf.name, cf.count)
|
||||
}
|
||||
}
|
||||
w.out = Output{Dir: w.dir, Files: files}
|
||||
if w.compress {
|
||||
return w.pack()
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (w *Writer) Output() Output {
|
||||
return w.out
|
||||
}
|
||||
|
||||
// pack removes the loose files only after the zip is in place, so a failure leaves every one of them where it was.
|
||||
func (w *Writer) pack() error {
|
||||
names := make([]string, 0, len(w.files))
|
||||
@@ -120,13 +134,13 @@ func (w *Writer) pack() error {
|
||||
return fmt.Errorf("compress: remove %s: %w", name, err)
|
||||
}
|
||||
}
|
||||
log.Infof("Compressed: %s/%s", w.dir, zipName)
|
||||
w.out.Zip = zipName
|
||||
return nil
|
||||
}
|
||||
|
||||
// writeZip writes the zip under a temp name beside dst and renames it into place once it is complete.
|
||||
func writeZip(dst, dir string, names []string) (err error) {
|
||||
tmp, err := os.OpenFile(dst+".tmp", os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0o666)
|
||||
tmp, err := os.OpenFile(dst+".tmp", os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0o600)
|
||||
if err != nil {
|
||||
return fmt.Errorf("create %s: %w", dst, err)
|
||||
}
|
||||
|
||||
@@ -9,6 +9,7 @@ import (
|
||||
"io"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
@@ -334,6 +335,20 @@ func TestClose_AfterFailedWriteKeepsEarlierFiles(t *testing.T) {
|
||||
var cookies []record.Cookie
|
||||
readJSON(t, filepath.Join(dir, "cookie.json"), &cookies)
|
||||
assert.Empty(t, cookies)
|
||||
assert.Zero(t, w.Output(), "a failed run lists nothing")
|
||||
}
|
||||
|
||||
func TestClose_OutputListsTheFilesInCategoryOrder(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
w, err := NewWriter(dir, JSON, false)
|
||||
require.NoError(t, err)
|
||||
require.NoError(t, w.Write(category.History, []record.Record{&record.History{URL: "https://a.example"}}))
|
||||
require.NoError(t, w.Write(category.Password, []record.Record{
|
||||
&record.Login{URL: "https://b.example"}, &record.Login{URL: "https://c.example"},
|
||||
}))
|
||||
require.NoError(t, w.Close())
|
||||
|
||||
assert.Equal(t, Output{Dir: dir, Files: []File{{Name: "password.json", Count: 2}, {Name: "history.json", Count: 1}}}, w.Output())
|
||||
}
|
||||
|
||||
// --- --zip ---
|
||||
@@ -369,6 +384,29 @@ func TestClose_ZipFailureKeepsTheFiles(t *testing.T) {
|
||||
assert.Equal(t, []string{"cookie.csv", "history.csv", "password.csv", "results.zip"}, dirNames(t, dir))
|
||||
}
|
||||
|
||||
func TestClose_ZipOutputNamesTheArchive(t *testing.T) {
|
||||
dir := filepath.Join(t.TempDir(), "results")
|
||||
w, err := NewWriter(dir, CSV, true)
|
||||
require.NoError(t, err)
|
||||
require.NoError(t, w.Write(category.Password, []record.Record{&record.Login{URL: "https://a.example"}}))
|
||||
require.NoError(t, w.Close())
|
||||
|
||||
assert.Equal(t, Output{Dir: dir, Files: []File{{Name: "password.csv", Count: 1}}, Zip: "results.zip"}, w.Output())
|
||||
}
|
||||
|
||||
// The zip holds the same records as the category files, so it gets their owner-only mode.
|
||||
func TestClose_ZipIsOwnerOnly(t *testing.T) {
|
||||
if runtime.GOOS == "windows" {
|
||||
t.Skip("Windows has no mode bits to check")
|
||||
}
|
||||
dir := filepath.Join(t.TempDir(), "results")
|
||||
require.NoError(t, zipAll(dir, chromeExtractions()...))
|
||||
|
||||
info, err := os.Stat(filepath.Join(dir, "results.zip"))
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, os.FileMode(0o600), info.Mode().Perm())
|
||||
}
|
||||
|
||||
func TestClose_ZipWithNothingWrittenFails(t *testing.T) {
|
||||
dir := filepath.Join(t.TempDir(), "results")
|
||||
require.Error(t, zipAll(dir))
|
||||
|
||||
Reference in new issue
Block a user