Skip to content

Commit

Permalink
Use temporary folder
Browse files Browse the repository at this point in the history
  • Loading branch information
elliotwutingfeng committed Dec 22, 2022
1 parent 6f30a9c commit 4f76569
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 41 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ mem_report:
go tool pprof mem.prof

build_cli:
go build -o ./dist/fasttld -ldflags "-X 'github.com/elliotwutingfeng/go-fasttld/cmd/fasttld.version=v0.4.2'" ./cmd/main.go
go build -o ./dist/fasttld -ldflags "-X 'github.com/elliotwutingfeng/go-fasttld/cmd/fasttld.version=v0.4.3'" ./cmd/main.go

demo:
go run ./examples/demo.go
Expand Down
41 changes: 22 additions & 19 deletions fasttld.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ import (
"errors"
"log"
"net/url"
"os"
"strconv"
"strings"

"github.com/spf13/afero"
"github.com/tidwall/hashmap"
"golang.org/x/net/idna"
)
Expand Down Expand Up @@ -431,31 +431,34 @@ func New(n SuffixListParams) (*FastTLD, error) {
tldTrie, err := trieConstruct(n.IncludePrivateSuffix, "")
return &FastTLD{cacheFilePath: "", tldTrie: tldTrie, includePrivateSuffix: n.IncludePrivateSuffix}, err
}
// If cacheFilePath is unreachable, use default Public Suffix List file.
if isValid, _ := checkCacheFile(n.CacheFilePath); !isValid {
defaultCacheFolderPath, defaultCacheFilePath, err := getDefaultCachePaths()
if err != nil || os.MkdirAll(defaultCacheFolderPath, 0644) != nil {
// default Public Suffix List file cannot be opened
extractor := &FastTLD{cacheFilePath: n.CacheFilePath, tldTrie: &trie{}, includePrivateSuffix: n.IncludePrivateSuffix}
// If cacheFilePath is unreachable, use temporary folder
if isValid, _ := checkCacheFile(extractor.cacheFilePath); !isValid {
filesystem := new(afero.OsFs)
defaultCacheFolderPath := afero.GetTempDir(filesystem, "")
defaultCacheFilePath := defaultCacheFolderPath + defaultPSLFileName
defaultCacheFolder, err := filesystem.Open(defaultCacheFolderPath)
if err != nil {
// temporary folder not accessible, fallback to inline Public Suffix list
return inlinePSL(err, n)
}
n.CacheFilePath = defaultCacheFilePath
if isValid, lastModifiedHours := checkCacheFile(n.CacheFilePath); !isValid || lastModifiedHours > pslMaxAgeHours {
if file, err := os.OpenFile(n.CacheFilePath, os.O_CREATE|os.O_WRONLY, 0644); err == nil {
if err := update(file, publicSuffixListSources); err != nil {
log.Println(err)
}
defer file.Close()
defer defaultCacheFolder.Close()
extractor.cacheFilePath = defaultCacheFilePath
isValid, lastModifiedHours := checkCacheFile(extractor.cacheFilePath)
if !isValid || lastModifiedHours > pslMaxAgeHours {
// update Public Suffix list cache if it is outdated
if updateErr := extractor.Update(); updateErr != nil {
// update failed, fallback to inline Public Suffix list
return inlinePSL(err, n)
}
}
if isValid, _ := checkCacheFile(n.CacheFilePath); !isValid {
return inlinePSL(err, n)
return extractor, err
}
}

tldTrie, err := trieConstruct(n.IncludePrivateSuffix, n.CacheFilePath)
tldTrie, err := trieConstruct(n.IncludePrivateSuffix, extractor.cacheFilePath)
if err != nil {
return inlinePSL(err, n)
}

return &FastTLD{cacheFilePath: n.CacheFilePath, tldTrie: tldTrie, includePrivateSuffix: n.IncludePrivateSuffix}, err
extractor.tldTrie = tldTrie
return extractor, err
}
19 changes: 4 additions & 15 deletions psl.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,17 +160,6 @@ func update(file afero.File,
return nil
}

func getDefaultCachePaths() (string, string, error) {
currentFilePath, ok := getCurrentFilePath()
if !ok {
return "", "", errors.New("Cannot get path to current module file")
}
defaultCacheFolderPath := currentFilePath + string(os.PathSeparator) + defaultPSLFolder
defaultCacheFilePath := defaultCacheFolderPath + string(os.PathSeparator) + defaultPSLFileName

return defaultCacheFolderPath, defaultCacheFilePath, nil
}

func checkCacheFile(cacheFilePath string) (bool, float64) {
cacheFilePath, pathValidErr := filepath.Abs(strings.TrimSpace(cacheFilePath))
stat, fileinfoErr := os.Stat(cacheFilePath)
Expand All @@ -193,10 +182,10 @@ func checkCacheFile(cacheFilePath string) (bool, float64) {
// Update updates the default Public Suffix list file and updates its suffix trie using the updated file.
// If cache file path is not the same as the default cache file path, this will be a no-op.
func (f *FastTLD) Update() error {
defaultCacheFolderPath, defaultCacheFilePath, err := getDefaultCachePaths()
if err := os.MkdirAll(defaultCacheFolderPath, 0644); err != nil {
return err
}
filesystem := new(afero.OsFs)
defaultCacheFolderPath := afero.GetTempDir(filesystem, "")
defaultCacheFilePath := defaultCacheFolderPath + defaultPSLFileName

if f.cacheFilePath != defaultCacheFilePath {
return errors.New("No-op. Only default Public Suffix list file can be updated")
}
Expand Down
6 changes: 0 additions & 6 deletions psl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,9 +167,3 @@ func TestFileLastModifiedHours(t *testing.T) {
}
defer file.Close()
}

func TestGetDefaultCachePaths(t *testing.T) {
if _, _, err := getDefaultCachePaths(); err != nil {
t.Errorf("Expected no error, got %v", err)
}
}

0 comments on commit 4f76569

Please sign in to comment.