Commit 524fda2f authored by Grzegorz Bizon's avatar Grzegorz Bizon

Use Go `map` instead of iterating entries multiple times

parent eebe7a29
...@@ -7,8 +7,8 @@ import ( ...@@ -7,8 +7,8 @@ import (
"encoding/json" "encoding/json"
"io" "io"
"os" "os"
"path"
"strconv" "strconv"
"strings"
"time" "time"
) )
...@@ -21,6 +21,8 @@ type metadata struct { ...@@ -21,6 +21,8 @@ type metadata struct {
Comment string `json:"comment,omitempty"` Comment string `json:"comment,omitempty"`
} }
type zipFileMap 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"
...@@ -57,28 +59,26 @@ func writeZipEntryMetadata(output io.Writer, entry *zip.File) error { ...@@ -57,28 +59,26 @@ func writeZipEntryMetadata(output io.Writer, entry *zip.File) error {
return nil return nil
} }
func handleZipEntryMetadata(output io.Writer, entry *zip.File, entries []*zip.File) error { func handleZipEntryMetadata(output io.Writer, entry *zip.File, fileMap zipFileMap) error {
var dirNodes []string var dirNodes []string
entryPath := entry.Name
var calculateEntryNodes func(string) for {
calculateEntryNodes = func(str string) { entryPath = path.Dir(entryPath)
idx := strings.LastIndex(str, "/") if entryPath == "." || entryPath == "/" {
if idx < 0 { break
return
} }
dir := str[:idx] dirNodes = append([]string{entryPath + "/"}, dirNodes...)
dirNodes = append([]string{dir + "/"}, dirNodes...)
calculateEntryNodes(dir)
} }
calculateEntryNodes(entry.Name)
for _, d := range dirNodes { for _, d := range dirNodes {
if !hasZipPathEntry(d, entries) { if _, ok := fileMap[d]; !ok {
var missingHeader zip.FileHeader var missingHeader zip.FileHeader
missingHeader.Name = d missingHeader.Name = d
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}
fileMap[d] = missingEntry
writeZipEntryMetadata(output, missingEntry) writeZipEntryMetadata(output, missingEntry)
} }
...@@ -88,14 +88,14 @@ func handleZipEntryMetadata(output io.Writer, entry *zip.File, entries []*zip.Fi ...@@ -88,14 +88,14 @@ func handleZipEntryMetadata(output io.Writer, entry *zip.File, entries []*zip.Fi
return err return err
} }
func hasZipPathEntry(path string, entries []*zip.File) bool { func generateZipFileMap(zipEntries []*zip.File) zipFileMap {
for _, e := range entries { fileMap := make(map[string]*zip.File)
if e.Name == path {
return true for _, entry := range zipEntries {
} fileMap[entry.Name] = entry
} }
return false return fileMap
} }
func generateZipMetadata(output io.Writer, archive *zip.Reader) error { func generateZipMetadata(output io.Writer, archive *zip.Reader) error {
...@@ -108,9 +108,10 @@ func generateZipMetadata(output io.Writer, archive *zip.Reader) error { ...@@ -108,9 +108,10 @@ func generateZipMetadata(output io.Writer, archive *zip.Reader) error {
return err return err
} }
fileMap := generateZipFileMap(archive.File)
// Write all files // Write all files
for _, entry := range archive.File { for _, entry := range archive.File {
if err := handleZipEntryMetadata(output, entry, archive.File); err != nil { if err := handleZipEntryMetadata(output, entry, fileMap); 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