Skip to content

Commit 4d005da

Browse files
committed
fix(archive): skip aliased root which is filtered
1 parent 3ea17e5 commit 4d005da

File tree

2 files changed

+38
-64
lines changed

2 files changed

+38
-64
lines changed

src/serverHandler/archive.go

Lines changed: 30 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"net/url"
77
"os"
88
"path"
9-
"strings"
109
)
1110

1211
type archiveCallback func(f *os.File, fInfo os.FileInfo, relPath string) error
@@ -16,25 +15,25 @@ func (h *handler) visitFs(
1615
statFs bool,
1716
archiveCallback archiveCallback,
1817
) {
19-
alias, hasAlias := h.aliases.byUrlPath(rawRequestPath)
20-
2118
var fsPath string
19+
alias, hasAlias := h.aliases.byUrlPath(rawRequestPath)
2220
if hasAlias {
2321
fsPath = alias.fsPath
22+
if alias.urlPath != "/" {
23+
statFs = true
24+
}
2425
} else {
2526
fsPath = initFsPath
2627
}
2728

28-
if hasAlias && rawRequestPath != "/" {
29-
statFs = true
30-
}
31-
3229
var fInfo os.FileInfo
3330
var childInfos []os.FileInfo
34-
if statFs {
35-
// wrap func to run defer ASAP
36-
err := func() error {
37-
f, err := os.Open(fsPath)
31+
// wrap func to run defer ASAP
32+
err := func() error {
33+
var f *os.File
34+
var err error
35+
if statFs {
36+
f, err = os.Open(fsPath)
3837
if f != nil {
3938
defer f.Close()
4039
}
@@ -51,51 +50,32 @@ func (h *handler) visitFs(
5150
return err
5251
}
5352
}
53+
} else {
54+
fInfo = newFakeFileInfo(path.Base(fsPath), true)
55+
}
5456

55-
if len(relPath) > 0 {
56-
if err := archiveCallback(f, fInfo, relPath); err != nil {
57-
return err
58-
}
57+
if len(relPath) > 0 {
58+
if err := archiveCallback(f, fInfo, relPath); err != nil {
59+
return err
5960
}
61+
}
6062

61-
if f != nil && fInfo.IsDir() {
62-
childInfos, err = f.Readdir(0)
63-
if h.errHandler.LogError(err) {
64-
return err
65-
}
66-
childInfos = h.FilterItems(childInfos)
63+
if f != nil && fInfo.IsDir() {
64+
childInfos, err = f.Readdir(0)
65+
if h.errHandler.LogError(err) {
66+
return err
6767
}
68-
69-
return nil
70-
}()
71-
if err != nil {
72-
return
7368
}
74-
} else {
75-
fInfo = newFakeFileInfo(path.Base(fsPath), true)
69+
70+
return nil
71+
}()
72+
if err != nil {
73+
return
7674
}
7775

7876
if fInfo.IsDir() {
79-
childAliases := map[string]string{}
80-
for _, alias := range h.aliases {
81-
if alias.isChildOf(rawRequestPath) {
82-
childAliases[alias.urlPath] = alias.fsPath
83-
continue
84-
}
85-
86-
// just generate a path for walk down, mapped value is not important
87-
if alias.isSuccessorOf(rawRequestPath) {
88-
succPath := alias.urlPath[len(rawRequestPath):]
89-
if succPath[0] == '/' {
90-
succPath = succPath[1:]
91-
}
92-
childName := succPath[:strings.Index(succPath, "/")]
93-
childUrlPath := util.CleanUrlPath(rawRequestPath + "/" + childName)
94-
childFsPath := fsPath + "/" + childName
95-
childAliases[childUrlPath] = childFsPath
96-
continue
97-
}
98-
}
77+
childInfos, _, _ := h.mergeAlias(rawRequestPath, fInfo, childInfos)
78+
childInfos = h.FilterItems(childInfos)
9979

10080
// childInfo can be regular dir/file, or aliased item that shadows regular dir/file
10181
for _, childInfo := range childInfos {
@@ -104,19 +84,12 @@ func (h *handler) visitFs(
10484
childRawRequestPath := util.CleanUrlPath(rawRequestPath + childPath)
10585
childRelPath := relPath + childPath
10686

107-
if childAliasedFsPath, hasChildAlias := childAliases[childRawRequestPath]; hasChildAlias {
108-
h.visitFs(childAliasedFsPath, childRawRequestPath, childRelPath, statFs, archiveCallback)
109-
delete(childAliases, childRawRequestPath) // delete walked alias
87+
if childAlias, hasChildAlias := h.aliases.byUrlPath(childRawRequestPath); hasChildAlias {
88+
h.visitFs(childAlias.fsPath, childRawRequestPath, childRelPath, statFs, archiveCallback)
11089
} else {
11190
h.visitFs(childFsPath, childRawRequestPath, childRelPath, statFs, archiveCallback)
11291
}
11392
}
114-
115-
// rest aliases are standalone aliases that not shadows exists dir/file
116-
for childRawRequestPath, childAliasedFsPath := range childAliases {
117-
childRelPath := relPath + "/" + path.Base(childRawRequestPath)
118-
h.visitFs(childAliasedFsPath, childRawRequestPath, childRelPath, statFs, archiveCallback)
119-
}
12093
}
12194
}
12295

test/case/019.alias.archive.bash

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,19 @@ cleanup() {
66

77
source "$root"/lib.bash
88

9-
"$ghfs" -l 3003 -r "$fs"/vhost2 --archive / -a :/go:"$fs"/vhost1/go -a :/hello/world:"$fs"/vhost1/world -E '' &
9+
"$ghfs" -l 3003 -r "$fs"/vhost2 --archive / -a :/go:"$fs"/vhost1/go -a :/hello/world:"$fs"/vhost1/world -a :/yes:"$fs"/vhost1/yes -H yes -E '' &
1010
sleep 0.05 # wait server ready
1111
cleanup
1212

1313
archive="$fs"/downloaded/archive.tar.tmp
1414
curl_get_body 'http://127.0.0.1:3003/?tar' > "$archive"
15-
(tar -tf "$archive" | grep -q '^a/') || fail "a/ not in $(basename $archive)"
16-
(tar -tf "$archive" | grep -q '^b/') || fail "b/ not in $(basename $archive)"
17-
(tar -tf "$archive" | grep -q '^file1.txt') || fail "file1.txt not in $(basename $archive)"
18-
(tar -tf "$archive" | grep -q '^file2.txt') || fail "file2.txt not in $(basename $archive)"
19-
(tar -tf "$archive" | grep -q 'go/index.txt') || fail "go/index.txt not in $(basename $archive)"
20-
(tar -tf "$archive" | grep -q 'hello/world/index.txt') || fail "hello/world/index.txt not in $(basename $archive)"
15+
(tar -tf "$archive" | grep -q '^a/') || fail "a/ should in $(basename $archive)"
16+
(tar -tf "$archive" | grep -q '^b/') || fail "b/ should in $(basename $archive)"
17+
(tar -tf "$archive" | grep -q '^file1.txt') || fail "file1.txt should in $(basename $archive)"
18+
(tar -tf "$archive" | grep -q '^file2.txt') || fail "file2.txt should in $(basename $archive)"
19+
(tar -tf "$archive" | grep -q 'go/index.txt') || fail "go/index.txt should in $(basename $archive)"
20+
(tar -tf "$archive" | grep -q 'hello/world/index.txt') || fail "hello/world/index.txt should in $(basename $archive)"
21+
(tar -tf "$archive" | grep -q 'yes/index.txt') && fail "yes/index.txt should not in $(basename $archive)"
2122

2223
cleanup
2324
jobs -p | xargs kill

0 commit comments

Comments
 (0)