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