Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a trailer to write the uncompressed archive size #23

Merged
merged 1 commit into from
Dec 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions browser.go
Original file line number Diff line number Diff line change
Expand Up @@ -621,6 +621,7 @@ func handleArchive(
}

w.Header().Set("Content-Type", contentType)
w.Header().Set("Trailer", "Omegaup-Uncompressed-Size")
var z archive
if contentType == "application/gzip" {
gz := gzip.NewWriter(w)
Expand All @@ -632,6 +633,7 @@ func handleArchive(
}
defer z.Close()

var uncompressedSize int64
err = tree.Walk(func(parent string, entry *git.TreeEntry) error {
select {
case <-ctx.Done():
Expand Down Expand Up @@ -664,6 +666,7 @@ func handleArchive(
defer blob.Free()

// Object is a blob.
uncompressedSize += blob.Size()
w, err := z.Create(fullPath, blob.Size())
if err != nil {
return errors.Wrap(
Expand All @@ -687,6 +690,7 @@ func handleArchive(
"failed to walk the repository",
)
}
w.Header().Set("Omegaup-Uncompressed-Size", strconv.FormatInt(uncompressedSize, 10))
return nil
}

Expand Down
19 changes: 18 additions & 1 deletion browser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"bytes"
"compress/gzip"
"context"
"io"
"net/http"
"net/http/httptest"
"reflect"
Expand Down Expand Up @@ -209,12 +210,15 @@ func TestHandleArchiveCommitTarball(t *testing.T) {
if "application/gzip" != response.Header().Get("Content-Type") {
t.Fatalf("Content-Type. Expected %s, got %s", "application/gzip", response.Header().Get("Content-Type"))
}
trailers := response.Result().Trailer
if _, ok := trailers["Omegaup-Uncompressed-Size"]; !ok {
t.Errorf("Omegaup-Uncompressed-Size was not present in the trailers: %v", trailers)
}

gz, err := gzip.NewReader(bytes.NewReader(response.Body.Bytes()))
if err != nil {
t.Fatalf("Error opening gzip from response: %v", err)
}
defer gz.Close()

a := tar.NewReader(gz)
hdr, err := a.Next()
Expand All @@ -225,6 +229,19 @@ func TestHandleArchiveCommitTarball(t *testing.T) {
if "empty" != hdr.Name {
t.Errorf("Expected %s, got %v", "empty", hdr.Name)
}
_, err = io.Copy(io.Discard, a)
if err != nil {
t.Fatalf("Error reading tar file: %v", err)
}
hdr, err = a.Next()
if err == nil {
t.Fatalf("Tarball has unexpected extra files: %v", hdr)
}
gz.Close()

if "0" != trailers.Get("Omegaup-Uncompressed-Size") {
t.Errorf("Omegaup-Uncompressed-Size trailer. Expected 0, got %v", trailers.Get("Omegaup-Uncompressed-Size"))
}
}

func TestHandleArchiveCommitTarballMismatchedTree(t *testing.T) {
Expand Down