Commit 53e345d2 authored by Grzegorz Bizon's avatar Grzegorz Bizon

Do not make assumptions about non existing entries in artifacts

parent 57c3a8a8
...@@ -10,12 +10,11 @@ import ( ...@@ -10,12 +10,11 @@ import (
"path" "path"
"sort" "sort"
"strconv" "strconv"
"time"
) )
type metadata struct { type metadata struct {
Modified int64 `json:"modified"` Modified int64 `json:"modified,omitempty"`
Mode string `json:"mode"` Mode string `json:"mode,omitempty"`
CRC uint32 `json:"crc,omitempty"` CRC uint32 `json:"crc,omitempty"`
Size uint64 `json:"size,omitempty"` Size uint64 `json:"size,omitempty"`
Zipped uint64 `json:"zipped,omitempty"` Zipped uint64 `json:"zipped,omitempty"`
...@@ -26,6 +25,10 @@ const MetadataHeaderPrefix = "\x00\x00\x00&" // length of string below, encoded ...@@ -26,6 +25,10 @@ const MetadataHeaderPrefix = "\x00\x00\x00&" // length of string below, encoded
const MetadataHeader = "GitLab Build Artifacts Metadata 0.0.2\n" const MetadataHeader = "GitLab Build Artifacts Metadata 0.0.2\n"
func newMetadata(file *zip.File) metadata { func newMetadata(file *zip.File) metadata {
if file == nil {
return metadata{}
}
return metadata{ return metadata{
Modified: file.ModTime().Unix(), Modified: file.ModTime().Unix(),
Mode: strconv.FormatUint(uint64(file.Mode().Perm()), 8), Mode: strconv.FormatUint(uint64(file.Mode().Perm()), 8),
...@@ -45,8 +48,8 @@ func (m metadata) writeEncoded(output io.Writer) error { ...@@ -45,8 +48,8 @@ func (m metadata) writeEncoded(output io.Writer) error {
return writeBytes(output, j) return writeBytes(output, j)
} }
func writeZipEntryMetadata(output io.Writer, entry *zip.File) error { func writeZipEntryMetadata(output io.Writer, path string, entry *zip.File) error {
if err := writeString(output, entry.Name); err != nil { if err := writeString(output, path); err != nil {
return err return err
} }
...@@ -58,12 +61,11 @@ func writeZipEntryMetadata(output io.Writer, entry *zip.File) error { ...@@ -58,12 +61,11 @@ func writeZipEntryMetadata(output io.Writer, entry *zip.File) error {
} }
func generateZipMetadata(output io.Writer, archive *zip.Reader) error { func generateZipMetadata(output io.Writer, archive *zip.Reader) error {
// Write metadata header
if err := writeString(output, MetadataHeader); err != nil { if err := writeString(output, MetadataHeader); err != nil {
return err return err
} }
// Write empty error header // Write empty error header that we may need in the future
if err := writeString(output, "{}"); err != nil { if err := writeString(output, "{}"); err != nil {
return err return err
} }
...@@ -74,29 +76,17 @@ func generateZipMetadata(output io.Writer, archive *zip.Reader) error { ...@@ -74,29 +76,17 @@ func generateZipMetadata(output io.Writer, archive *zip.Reader) error {
// Add missing entries // Add missing entries
for _, entry := range archive.File { for _, entry := range archive.File {
zipMap[entry.Name] = entry zipMap[entry.Name] = entry
entryPath := entry.Name
for { for entryPath := path.Dir(entry.Name); entryPath != "." && entryPath != "/"; entryPath = path.Dir(entryPath) {
entryPath = path.Dir(entryPath)
entryDir := entryPath + "/" entryDir := entryPath + "/"
if entryPath == "." || entryPath == "/" {
break
}
if _, ok := zipMap[entryDir]; !ok { if _, ok := zipMap[entryDir]; !ok {
var missingHeader zip.FileHeader zipMap[entryDir] = nil
missingHeader.Name = entryDir
missingHeader.SetModTime(time.Now())
missingHeader.SetMode(os.FileMode(uint32(0755)))
missingEntry := &zip.File{FileHeader: missingHeader}
zipMap[entryDir] = missingEntry
} }
} }
} }
// Sort paths // Sort paths
var sortedPaths []string sortedPaths := make([]string, 0, len(zipMap))
for path, _ := range zipMap { for path, _ := range zipMap {
sortedPaths = append(sortedPaths, path) sortedPaths = append(sortedPaths, path)
} }
...@@ -104,7 +94,7 @@ func generateZipMetadata(output io.Writer, archive *zip.Reader) error { ...@@ -104,7 +94,7 @@ func generateZipMetadata(output io.Writer, archive *zip.Reader) error {
// Write all files // Write all files
for _, path := range sortedPaths { for _, path := range sortedPaths {
if err := writeZipEntryMetadata(output, zipMap[path]); err != nil { if err := writeZipEntryMetadata(output, path, 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