Fast and extensible multi-platform HTTP/1-2-3 web server with automatic HTTPS
Mirrored from https://github.com/Slazee/caddy.git
Synced Nov 12, 10:45 UTC
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>
Francis Lavoie committed
Jul 7, 2024 at 09:01 UTC
9338741ca79a74247ced86bc26e4994138470852
3 files changed
+24
-4
.github/workflows/ci.yml
+1
-1
@@ -157,7 +157,7 @@ jobs:
157
158
# The environment is fresh, so there's no point in keeping accepting and adding the key.
159
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 ./..."
161
test_result=$?
162
163
# There's no need leaving the files around
modules/caddyhttp/fileserver/browse.html
+1
-1
@@ -991,7 +991,7 @@ footer {
991
<div class="sizebar">
992
<div class="sizebar-bar"></div>
993
<div class="sizebar-text">
994
- {{.HumanSize}}
994
+ {{if .IsSymlink}}↱ {{end}}{{.HumanSize}}
995
</div>
996
</div>
997
</td>
modules/caddyhttp/fileserver/browsetplcontext.go
+22
-2
@@ -80,6 +80,13 @@ func (fsrv *FileServer) directoryListing(ctx context.Context, fileSystem fs.FS,
80
}
81
82
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
+
90
fileIsSymlink := isSymlink(info)
91
symlinkPath := ""
92
if fileIsSymlink {
@@ -103,7 +110,8 @@ func (fsrv *FileServer) directoryListing(ctx context.Context, fileSystem fs.FS,
110
}
111
112
if !isDir {
106
- tplCtx.TotalFileSize += size
113
+ // increase the total including the symlink target's size
114
+ tplCtx.TotalFileSizeFollowingSymlinks += size
115
}
116
117
u := url.URL{Path: "./" + name} // prepend with "./" to fix paths with ':' in the name
@@ -150,9 +158,15 @@ type browseTemplateContext struct {
158
// The number of files (items that aren't directories) in the listing.
159
NumFiles int `json:"num_files"`
160
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).
164
TotalFileSize int64 `json:"total_file_size"`
165
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
+
170
// Sort column used
171
Sort string `json:"sort,omitempty"`
172
@@ -288,6 +302,12 @@ func (btc browseTemplateContext) HumanTotalFileSize() string {
302
return humanize.IBytes(uint64(btc.TotalFileSize))
303
}
304
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
+
311
// HumanModTime returns the modified time of the file
312
// as a human-readable string given by format.
313
func (fi fileInfo) HumanModTime(format string) string {