Commit f1d320ed authored by Grzegorz Bizon's avatar Grzegorz Bizon

Refactor function that generates artifacts metadata

parent 3353d2e7
...@@ -9,6 +9,7 @@ import ( ...@@ -9,6 +9,7 @@ import (
"os" "os"
"path" "path"
"strconv" "strconv"
"strings"
"time" "time"
) )
...@@ -21,7 +22,7 @@ type metadata struct { ...@@ -21,7 +22,7 @@ type metadata struct {
Comment string `json:"comment,omitempty"` Comment string `json:"comment,omitempty"`
} }
type zipFileMap map[string]*zip.File 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"
...@@ -47,71 +48,66 @@ func (m metadata) writeEncoded(output io.Writer) error { ...@@ -47,71 +48,66 @@ func (m metadata) writeEncoded(output io.Writer) error {
} }
func writeZipEntryMetadata(output io.Writer, entry *zip.File) error { func writeZipEntryMetadata(output io.Writer, entry *zip.File) error {
err := writeString(output, entry.Name) if err := writeString(output, entry.Name); err != nil {
if err != nil {
return err return err
} }
err = newMetadata(entry).writeEncoded(output) if err := newMetadata(entry).writeEncoded(output); err != nil {
if err != nil {
return err return err
} }
return nil return nil
} }
func handleZipEntryMetadata(output io.Writer, entry *zip.File, fileMap zipFileMap) error { func generateZipMetadata(output io.Writer, archive *zip.Reader) error {
var dirNodes []string // Write metadata header
entryPath := entry.Name if err := writeString(output, MetadataHeader); err != nil {
return err
for {
entryPath = path.Dir(entryPath)
if entryPath == "." || entryPath == "/" {
break
}
dirNodes = append([]string{entryPath + "/"}, dirNodes...)
} }
for _, d := range dirNodes { // Write empty error header
if _, ok := fileMap[d]; !ok { if err := writeString(output, "{}"); err != nil {
var missingHeader zip.FileHeader return err
missingHeader.Name = d
missingHeader.SetModTime(time.Now())
missingHeader.SetMode(os.FileMode(uint32(0755)))
missingEntry := &zip.File{FileHeader: missingHeader}
fileMap[d] = missingEntry
writeZipEntryMetadata(output, missingEntry)
}
} }
err := writeZipEntryMetadata(output, entry) // Create map od directories in zip archive
return err dirMap := make(zipDirMap, len(archive.File))
} for _, entry := range archive.File {
if strings.HasSuffix(entry.Name, "/") {
func generateZipFileMap(zipEntries []*zip.File) zipFileMap { dirMap[entry.Name] = entry
fileMap := make(zipFileMap, len(zipEntries)) }
for _, entry := range zipEntries {
fileMap[entry.Name] = entry
} }
return fileMap // Add missing entries
} var missingEntries []*zip.File
func generateZipMetadata(output io.Writer, archive *zip.Reader) error { for _, entry := range archive.File {
if err := writeString(output, MetadataHeader); err != nil { entryPath := entry.Name
return err
for {
entryPath = path.Dir(entryPath)
if entryPath == "." || entryPath == "/" {
break
}
if _, ok := dirMap[entryPath]; !ok {
var missingHeader zip.FileHeader
missingHeader.Name = entryPath
missingHeader.SetModTime(time.Now())
missingHeader.SetMode(os.FileMode(uint32(0755)))
missingEntry := &zip.File{FileHeader: missingHeader}
dirMap[entryPath] = missingEntry
missingEntries = append(missingEntries, missingEntry)
}
}
} }
// Write empty error string archive.File = append(archive.File, missingEntries...)
if err := writeString(output, "{}"); err != nil {
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, fileMap); err != nil { if err := writeZipEntryMetadata(output, entry); 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