-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdetect_root.go
58 lines (53 loc) · 1.26 KB
/
detect_root.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
package main
import (
"io/fs"
)
var excludedFiles = map[string]bool{
"wget.log": true,
"nohup.out": true,
}
const traverseLimit = 5
// detectRoot attempts to find possible website root by traversing down the
// filesystem till there is single directory entry.
//
// It's a very conservative heuristic, it may not be able to determine the root
// successfully, in which case it returns nil (if there's an error), or the
// parent filesystem itself.
func detectRoot(fsys fs.FS) (fs.FS, error) {
depth := 0
for depth < traverseLimit {
sub, err := getSingleChild(fsys)
if err != nil {
return nil, err
}
if sub == nil {
// cannot go further down the filesystem
return fsys, nil
}
fsys = sub
depth++
}
return fsys, nil
}
// getSingleChild returns the single directory child of dir if applicable, or
// nil if there is no single directory child.
//
// both fs and error can be nil
func getSingleChild(fsys fs.FS) (fs.FS, error) {
children, err := fs.ReadDir(fsys, ".")
if err != nil {
return nil, err
}
count := 0
var last fs.DirEntry = nil
for _, child := range children {
if !excludedFiles[child.Name()] {
count++
last = child
}
}
if count == 1 && last != nil && last.IsDir() {
return fs.Sub(fsys, last.Name())
}
return nil, nil
}