-
Notifications
You must be signed in to change notification settings - Fork 1
/
util.go
64 lines (57 loc) · 1.8 KB
/
util.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package nopfs
import (
"fmt"
"io/fs"
"os"
"path/filepath"
"strings"
)
// GetDenylistFiles returns a list of ".deny" files found in
// $XDG_CONFIG_HOME/ipfs/denylists and /etc/ipfs/denylists. The files are
// sortered by their names in their respective directories.
func GetDenylistFiles() ([]string, error) {
// First, look for denylists in $XDG_CONFIG_HOME/ipfs/denylists
xdgConfigHome := os.Getenv("XDG_CONFIG_HOME")
if xdgConfigHome == "" {
xdgConfigHome = os.Getenv("HOME") + "/.config"
}
ipfsDenylistPath := filepath.Join(xdgConfigHome, "ipfs", "denylists")
ipfsDenylistFiles, err := GetDenylistFilesInDir(ipfsDenylistPath)
if err != nil {
return nil, err
}
// Then, look for denylists in /etc/ipfs/denylists
etcDenylistPath := "/etc/ipfs/denylists"
etcDenylistFiles, err := GetDenylistFilesInDir(etcDenylistPath)
if err != nil {
return nil, err
}
return append(ipfsDenylistFiles, etcDenylistFiles...), nil
}
// GetDenylistFilesInDir returns a list of ".deny" files found in the given
// directory. The files are sortered by their names. It returns an empty list
// and no error if the directory does not exist.
func GetDenylistFilesInDir(dirpath string) ([]string, error) {
var denylistFiles []string
// WalkDir outputs files in lexical order.
err := filepath.WalkDir(dirpath, func(path string, d fs.DirEntry, err error) error {
if err != nil {
return err
}
if !d.IsDir() && filepath.Ext(path) == ".deny" {
denylistFiles = append(denylistFiles, path)
}
return nil
})
if !os.IsNotExist(err) && err != nil {
return nil, fmt.Errorf("error walking %s: %w", dirpath, err)
}
return denylistFiles, nil
}
// cutPrefix imported from go1.20
func cutPrefix(s, prefix string) (after string, found bool) {
if !strings.HasPrefix(s, prefix) {
return s, false
}
return s[len(prefix):], true
}