Commit 57c3a8a8 authored by Grzegorz Bizon's avatar Grzegorz Bizon

Sort metadata entries before passing to IO buffer

parent f1d320ed
...@@ -8,8 +8,8 @@ import ( ...@@ -8,8 +8,8 @@ import (
"io" "io"
"os" "os"
"path" "path"
"sort"
"strconv" "strconv"
"strings"
"time" "time"
) )
...@@ -22,8 +22,6 @@ type metadata struct { ...@@ -22,8 +22,6 @@ type metadata struct {
Comment string `json:"comment,omitempty"` Comment string `json:"comment,omitempty"`
} }
type zipDirMap map[string]*zip.File
const MetadataHeaderPrefix = "\x00\x00\x00&" // length of string below, encoded properly const MetadataHeaderPrefix = "\x00\x00\x00&" // length of string below, encoded properly
const MetadataHeader = "GitLab Build Artifacts Metadata 0.0.2\n" const MetadataHeader = "GitLab Build Artifacts Metadata 0.0.2\n"
...@@ -70,44 +68,43 @@ func generateZipMetadata(output io.Writer, archive *zip.Reader) error { ...@@ -70,44 +68,43 @@ func generateZipMetadata(output io.Writer, archive *zip.Reader) error {
return err return err
} }
// Create map od directories in zip archive // Create map of files in zip archive
dirMap := make(zipDirMap, len(archive.File)) zipMap := make(map[string]*zip.File, len(archive.File))
for _, entry := range archive.File {
if strings.HasSuffix(entry.Name, "/") {
dirMap[entry.Name] = entry
}
}
// Add missing entries // Add missing entries
var missingEntries []*zip.File
for _, entry := range archive.File { for _, entry := range archive.File {
zipMap[entry.Name] = entry
entryPath := entry.Name entryPath := entry.Name
for { for {
entryPath = path.Dir(entryPath) entryPath = path.Dir(entryPath)
entryDir := entryPath + "/"
if entryPath == "." || entryPath == "/" { if entryPath == "." || entryPath == "/" {
break break
} }
if _, ok := dirMap[entryPath]; !ok { if _, ok := zipMap[entryDir]; !ok {
var missingHeader zip.FileHeader var missingHeader zip.FileHeader
missingHeader.Name = entryPath missingHeader.Name = entryDir
missingHeader.SetModTime(time.Now()) missingHeader.SetModTime(time.Now())
missingHeader.SetMode(os.FileMode(uint32(0755))) missingHeader.SetMode(os.FileMode(uint32(0755)))
missingEntry := &zip.File{FileHeader: missingHeader} missingEntry := &zip.File{FileHeader: missingHeader}
dirMap[entryPath] = missingEntry zipMap[entryDir] = missingEntry
missingEntries = append(missingEntries, missingEntry)
} }
} }
} }
archive.File = append(archive.File, missingEntries...) // Sort paths
var sortedPaths []string
for path, _ := range zipMap {
sortedPaths = append(sortedPaths, path)
}
sort.Strings(sortedPaths)
// Write all files // Write all files
for _, entry := range archive.File { for _, path := range sortedPaths {
if err := writeZipEntryMetadata(output, entry); err != nil { if err := writeZipEntryMetadata(output, zipMap[path]); err != nil {
return err return err
} }
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment