mirror of
https://github.com/moonD4rk/HackBrowserData.git
synced 2026-12-04 05:43:27 +01:00
fix(export): pack --zip before removing the outputs (#663)
CompressDir deleted each file as it was added and wrote the zip last, so a failure lost the output, and it packed and deleted every file in -d. The writer now packs only the files it wrote, renames the zip into place, and only then removes them.
This commit is contained in:
1 parent
5f7bfbd455
commit
8c9c9901a1
9 files changed
+198
-164
No files matched your search
@@ -18,7 +18,6 @@ import (
|
||||
"github.com/moond4rk/hackbrowserdata/internal/export"
|
||||
"github.com/moond4rk/hackbrowserdata/internal/record"
|
||||
"github.com/moond4rk/hackbrowserdata/types"
|
||||
"github.com/moond4rk/hackbrowserdata/utils/fileutil"
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -66,11 +65,11 @@ func TestExportGolden(t *testing.T) {
|
||||
tc := tc
|
||||
t.Run(tc.format, func(t *testing.T) {
|
||||
dir := filepath.Join(t.TempDir(), "results")
|
||||
exportWrite(t, dir, tc.format, exportStamps())
|
||||
exportWrite(t, dir, tc.format, false, exportStamps())
|
||||
got := bundleDir(t, dir)
|
||||
if tc.jsonFallback {
|
||||
jsonDir := filepath.Join(t.TempDir(), "results")
|
||||
exportWrite(t, jsonDir, "json", exportStamps())
|
||||
exportWrite(t, jsonDir, "json", false, exportStamps())
|
||||
assertJSONFallback(t, dir, jsonDir)
|
||||
got = bundleFiles(t, dir, "cookie.json")
|
||||
}
|
||||
@@ -82,29 +81,29 @@ func TestExportGolden(t *testing.T) {
|
||||
}
|
||||
|
||||
sparse := filepath.Join(t.TempDir(), "sparse")
|
||||
exportWrite(t, sparse, tc.format, exportSparseStamps())
|
||||
exportWrite(t, sparse, tc.format, false, exportSparseStamps())
|
||||
assert.Equal(t, exportCategoryFiles(tc.ext, category.Password, category.LocalStorage), exportDirFiles(t, sparse))
|
||||
})
|
||||
}
|
||||
|
||||
t.Run("zip", func(t *testing.T) {
|
||||
loose := filepath.Join(t.TempDir(), "results")
|
||||
exportWrite(t, loose, "csv", false, exportStamps())
|
||||
dir := filepath.Join(t.TempDir(), "results")
|
||||
exportWrite(t, dir, "csv", exportStamps())
|
||||
loose := readDirFiles(t, dir)
|
||||
require.NoError(t, fileutil.CompressDir(dir))
|
||||
exportWrite(t, dir, "csv", true, exportStamps())
|
||||
|
||||
assert.Equal(t, []string{"results.zip"}, exportDirFiles(t, dir))
|
||||
archive := filepath.Join(dir, "results.zip")
|
||||
assertGolden(t, "export-zip.golden", zipListing(t, archive))
|
||||
assert.Equal(t, loose, exportReadZip(t, archive))
|
||||
assert.Equal(t, readDirFiles(t, loose), exportReadZip(t, archive))
|
||||
})
|
||||
}
|
||||
|
||||
func exportWrite(t *testing.T, dir, format string, stamps []exportStamp) {
|
||||
func exportWrite(t *testing.T, dir, format string, compress bool, stamps []exportStamp) {
|
||||
t.Helper()
|
||||
f, err := export.ParseFormat(format)
|
||||
require.NoError(t, err)
|
||||
w, err := export.NewWriter(dir, f)
|
||||
w, err := export.NewWriter(dir, f, compress)
|
||||
require.NoError(t, err)
|
||||
for _, s := range stamps {
|
||||
require.NoError(t, writeResult(w, s.browser, types.ExtractResult{Profile: types.Profile{Name: s.profile}, Data: s.data}))
|
||||
|
||||
@@ -2,8 +2,6 @@ package main
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/moond4rk/hackbrowserdata/browser"
|
||||
"github.com/moond4rk/hackbrowserdata/internal/category"
|
||||
@@ -11,7 +9,6 @@ import (
|
||||
"github.com/moond4rk/hackbrowserdata/internal/log"
|
||||
"github.com/moond4rk/hackbrowserdata/internal/record"
|
||||
"github.com/moond4rk/hackbrowserdata/types"
|
||||
"github.com/moond4rk/hackbrowserdata/utils/fileutil"
|
||||
)
|
||||
|
||||
func extractAndWrite(browsers []browser.Browser, categories []category.Category, outputDir, outputFormat string, compress bool) error {
|
||||
@@ -19,7 +16,7 @@ func extractAndWrite(browsers []browser.Browser, categories []category.Category,
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
w, err := export.NewWriter(outputDir, format)
|
||||
w, err := export.NewWriter(outputDir, format, compress)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -35,16 +32,7 @@ func extractAndWrite(browsers []browser.Browser, categories []category.Category,
|
||||
}
|
||||
}
|
||||
}
|
||||
if err := w.Close(); err != nil {
|
||||
return err
|
||||
}
|
||||
if compress {
|
||||
if err := fileutil.CompressDir(outputDir); err != nil {
|
||||
return fmt.Errorf("compress: %w", err)
|
||||
}
|
||||
log.Infof("Compressed: %s/%s.zip", outputDir, filepath.Base(outputDir))
|
||||
}
|
||||
return nil
|
||||
return w.Close()
|
||||
}
|
||||
|
||||
// writeResult bridges today's BrowserData to the writer until the pipeline replaces it.
|
||||
|
||||
Reference in new issue
Block a user