66 "net/url"
77 "os"
88 "path"
9- "strings"
109)
1110
1211type 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
0 commit comments