Skip to content

Commit ab4d3e1

Browse files
authored
fix: nil browsing data in case error and walkdir instead of walk (#229)
* fix: nil browsing data in case error Signed-off-by: Andrii Ursulenko <[email protected]> * fix: ignore walk error, use walkdir instead of walk --------- Signed-off-by: Andrii Ursulenko <[email protected]> Co-authored-by: Andrii Ursulenko <[email protected]>
1 parent a2c3cd1 commit ab4d3e1

File tree

3 files changed

+15
-21
lines changed

3 files changed

+15
-21
lines changed

browser/browser.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,11 +87,13 @@ func pickFirefox(name, profile string) []Browser {
8787
} else {
8888
profile = fileutil.ParentDir(profile)
8989
}
90+
9091
if !fileutil.IsDirExists(filepath.Clean(profile)) {
9192
log.Noticef("find browser firefox %s failed, profile folder does not exist", v.name)
9293
continue
9394
}
94-
if multiFirefox, err := firefox.New(v.name, v.storage, profile, v.items); err == nil {
95+
96+
if multiFirefox, err := firefox.New(profile, v.items); err == nil {
9597
for _, b := range multiFirefox {
9698
log.Noticef("find browser firefox %s success", b.Name())
9799
browsers = append(browsers, b)
@@ -100,8 +102,10 @@ func pickFirefox(name, profile string) []Browser {
100102
log.Error(err)
101103
}
102104
}
105+
103106
return browsers
104107
}
108+
105109
return nil
106110
}
107111

browser/firefox/firefox.go

Lines changed: 9 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -23,18 +23,11 @@ type Firefox struct {
2323

2424
var ErrProfilePathNotFound = errors.New("profile path not found")
2525

26-
// New returns a new Firefox instance.
27-
func New(name, storage, profilePath string, items []item.Item) ([]*Firefox, error) {
28-
f := &Firefox{
29-
name: name,
30-
storage: storage,
31-
profilePath: profilePath,
32-
items: items,
33-
}
34-
multiItemPaths, err := f.getMultiItemPath(f.profilePath, f.items)
35-
if err != nil {
36-
return nil, err
37-
}
26+
// New returns new Firefox instances.
27+
func New(profilePath string, items []item.Item) ([]*Firefox, error) {
28+
multiItemPaths := make(map[string]map[item.Item]string)
29+
// ignore walk dir error since it can be produced by a single entry
30+
_ = filepath.WalkDir(profilePath, firefoxWalkFunc(items, multiItemPaths))
3831

3932
firefoxList := make([]*Firefox, 0, len(multiItemPaths))
4033
for name, itemPaths := range multiItemPaths {
@@ -44,13 +37,8 @@ func New(name, storage, profilePath string, items []item.Item) ([]*Firefox, erro
4437
itemPaths: itemPaths,
4538
})
4639
}
47-
return firefoxList, nil
48-
}
4940

50-
func (f *Firefox) getMultiItemPath(profilePath string, items []item.Item) (map[string]map[item.Item]string, error) {
51-
multiItemPaths := make(map[string]map[item.Item]string)
52-
err := filepath.Walk(profilePath, firefoxWalkFunc(items, multiItemPaths))
53-
return multiItemPaths, err
41+
return firefoxList, nil
5442
}
5543

5644
func (f *Firefox) copyItemToLocal() error {
@@ -63,8 +51,8 @@ func (f *Firefox) copyItemToLocal() error {
6351
return nil
6452
}
6553

66-
func firefoxWalkFunc(items []item.Item, multiItemPaths map[string]map[item.Item]string) filepath.WalkFunc {
67-
return func(path string, info fs.FileInfo, err error) error {
54+
func firefoxWalkFunc(items []item.Item, multiItemPaths map[string]map[item.Item]string) fs.WalkDirFunc {
55+
return func(path string, info fs.DirEntry, err error) error {
6856
for _, v := range items {
6957
if info.Name() == v.FileName() {
7058
parentBaseDir := fileutil.ParentBaseDir(path)
@@ -75,6 +63,7 @@ func firefoxWalkFunc(items []item.Item, multiItemPaths map[string]map[item.Item]
7563
}
7664
}
7765
}
66+
7867
return err
7968
}
8069
}

cmd/hack-browser-data/main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ func Execute() {
5353
data, err := b.BrowsingData(isFullExport)
5454
if err != nil {
5555
log.Error(err)
56+
continue
5657
}
5758
data.Output(outputDir, b.Name(), outputFormat)
5859
}

0 commit comments

Comments
 (0)