diff options
author | Jeremy Lin <jeremy.lin@gmail.com> | 2021-11-22 13:59:09 -0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-11-22 14:59:09 -0700 |
commit | c8b5a81607c9c2799f46f42154ee311884723aa4 (patch) | |
tree | 4d2b804240d8925ef0f0c7639bbf6ed95bab342a /modules/caddyhttp/fileserver/browsetplcontext.go | |
parent | eead337324593ef5cd30ebce2be26da84de00787 (diff) |
fileserver: Fix handling of symlink sizes in directory listings (#4415)
Diffstat (limited to 'modules/caddyhttp/fileserver/browsetplcontext.go')
-rw-r--r-- | modules/caddyhttp/fileserver/browsetplcontext.go | 19 |
1 files changed, 12 insertions, 7 deletions
diff --git a/modules/caddyhttp/fileserver/browsetplcontext.go b/modules/caddyhttp/fileserver/browsetplcontext.go index 7dc4f08..2c57e52 100644 --- a/modules/caddyhttp/fileserver/browsetplcontext.go +++ b/modules/caddyhttp/fileserver/browsetplcontext.go @@ -24,10 +24,11 @@ import ( "time" "github.com/caddyserver/caddy/v2" + "github.com/caddyserver/caddy/v2/modules/caddyhttp" "github.com/dustin/go-humanize" ) -func (fsrv *FileServer) directoryListing(files []os.FileInfo, canGoUp bool, root, urlPath string, repl *caddy.Replacer) (browseTemplateContext, error) { +func (fsrv *FileServer) directoryListing(files []os.FileInfo, canGoUp bool, root, urlPath string, repl *caddy.Replacer) browseTemplateContext { filesToHide := fsrv.transformHidePaths(repl) var dirCount, fileCount int @@ -52,14 +53,18 @@ func (fsrv *FileServer) directoryListing(files []os.FileInfo, canGoUp bool, root fileCount++ } - fileIsSymlink := isSymlink(f) size := f.Size() + fileIsSymlink := isSymlink(f) if fileIsSymlink { - info, err := os.Stat(name) - if err != nil { - return browseTemplateContext{}, err + path := caddyhttp.SanitizedPathJoin(root, path.Join(urlPath, f.Name())) + fileInfo, err := os.Stat(path) + if err == nil { + size = fileInfo.Size() } - size = info.Size() + // An error most likely means the symlink target doesn't exist, + // which isn't entirely unusual and shouldn't fail the listing. + // In this case, just use the size of the symlink itself, which + // was already set above. } fileInfos = append(fileInfos, fileInfo{ @@ -80,7 +85,7 @@ func (fsrv *FileServer) directoryListing(files []os.FileInfo, canGoUp bool, root Items: fileInfos, NumDirs: dirCount, NumFiles: fileCount, - }, nil + } } // browseTemplateContext provides the template context for directory listings. |