Skip to content

Commit 9338741

Browse files
francislavoiemholt
andauthored
browse: Exclude symlink target size from total, show arrow on size (#6412)
* fileserver: Exclude symlink target size from total, show arrow on size * Keep both totals * Linter doesn't like my spelling :( * Stop parallelizing tests for now * Update modules/caddyhttp/fileserver/browse.html * Minor renamings --------- Co-authored-by: Matthew Holt <mholt@users.noreply.github.com>
1 parent 88c7e53 commit 9338741

File tree

3 files changed

+24
-4
lines changed

3 files changed

+24
-4
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ jobs:
157157
158158
# The environment is fresh, so there's no point in keeping accepting and adding the key.
159159
rsync -arz -e "ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null" --progress --delete --exclude '.git' . "$CI_USER"@ci-s390x.caddyserver.com:/var/tmp/"$short_sha"
160-
ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -t "$CI_USER"@ci-s390x.caddyserver.com "cd /var/tmp/$short_sha; go version; go env; printf "\n\n";CGO_ENABLED=0 go test -tags nobadger -v ./..."
160+
ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -t "$CI_USER"@ci-s390x.caddyserver.com "cd /var/tmp/$short_sha; go version; go env; printf "\n\n";CGO_ENABLED=0 go test -p 1 -tags nobadger -v ./..."
161161
test_result=$?
162162
163163
# There's no need leaving the files around

modules/caddyhttp/fileserver/browse.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -991,7 +991,7 @@ <h1>
991991
<div class="sizebar">
992992
<div class="sizebar-bar"></div>
993993
<div class="sizebar-text">
994-
{{.HumanSize}}
994+
{{if .IsSymlink}}↱&nbsp;{{end}}{{.HumanSize}}
995995
</div>
996996
</div>
997997
</td>

modules/caddyhttp/fileserver/browsetplcontext.go

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,13 @@ func (fsrv *FileServer) directoryListing(ctx context.Context, fileSystem fs.FS,
8080
}
8181

8282
size := info.Size()
83+
84+
if !isDir {
85+
// increase the total by the symlink's size, not the target's size,
86+
// by incrementing before we follow the symlink
87+
tplCtx.TotalFileSize += size
88+
}
89+
8390
fileIsSymlink := isSymlink(info)
8491
symlinkPath := ""
8592
if fileIsSymlink {
@@ -103,7 +110,8 @@ func (fsrv *FileServer) directoryListing(ctx context.Context, fileSystem fs.FS,
103110
}
104111

105112
if !isDir {
106-
tplCtx.TotalFileSize += size
113+
// increase the total including the symlink target's size
114+
tplCtx.TotalFileSizeFollowingSymlinks += size
107115
}
108116

109117
u := url.URL{Path: "./" + name} // prepend with "./" to fix paths with ':' in the name
@@ -150,9 +158,15 @@ type browseTemplateContext struct {
150158
// The number of files (items that aren't directories) in the listing.
151159
NumFiles int `json:"num_files"`
152160

153-
// The total size of all files in the listing.
161+
// The total size of all files in the listing. Only includes the
162+
// size of the files themselves, not the size of symlink targets
163+
// (i.e. the calculation of this value does not follow symlinks).
154164
TotalFileSize int64 `json:"total_file_size"`
155165

166+
// The total size of all files in the listing, including the
167+
// size of the files targeted by symlinks.
168+
TotalFileSizeFollowingSymlinks int64 `json:"total_file_size_following_symlinks"`
169+
156170
// Sort column used
157171
Sort string `json:"sort,omitempty"`
158172

@@ -288,6 +302,12 @@ func (btc browseTemplateContext) HumanTotalFileSize() string {
288302
return humanize.IBytes(uint64(btc.TotalFileSize))
289303
}
290304

305+
// HumanTotalFileSizeFollowingSymlinks is the same as HumanTotalFileSize
306+
// except the returned value reflects the size of symlink targets.
307+
func (btc browseTemplateContext) HumanTotalFileSizeFollowingSymlinks() string {
308+
return humanize.IBytes(uint64(btc.TotalFileSizeFollowingSymlinks))
309+
}
310+
291311
// HumanModTime returns the modified time of the file
292312
// as a human-readable string given by format.
293313
func (fi fileInfo) HumanModTime(format string) string {

0 commit comments

Comments
 (0)