Commit eebe7a29 authored by Grzegorz Bizon's avatar Grzegorz Bizon

Add missing entries in build artifacts archive metadata

We need full directory structure, but since ZIP does not require it, we
need to calculate missing entries for directories when we generate
archive metadata.
parent 4fe063cf
...@@ -23,7 +23,7 @@ func main() { ...@@ -23,7 +23,7 @@ func main() {
} }
if len(os.Args) != 2 { if len(os.Args) != 2 {
fmt.Fprintf(os.Stderr, "Usage: %s FILE.ZIP", progName) fmt.Fprintf(os.Stderr, "Usage: %s FILE.ZIP\n", progName)
os.Exit(1) os.Exit(1)
} }
if err := zipartifacts.GenerateZipMetadataFromFile(os.Args[1], os.Stdout); err != nil { if err := zipartifacts.GenerateZipMetadataFromFile(os.Args[1], os.Stdout); err != nil {
......
...@@ -8,6 +8,8 @@ import ( ...@@ -8,6 +8,8 @@ import (
"io" "io"
"os" "os"
"strconv" "strconv"
"strings"
"time"
) )
type metadata struct { type metadata struct {
...@@ -55,22 +57,60 @@ func writeZipEntryMetadata(output io.Writer, entry *zip.File) error { ...@@ -55,22 +57,60 @@ 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 {
var dirNodes []string
var calculateEntryNodes func(string)
calculateEntryNodes = func(str string) {
idx := strings.LastIndex(str, "/")
if idx < 0 {
return
}
dir := str[:idx]
dirNodes = append([]string{dir + "/"}, dirNodes...)
calculateEntryNodes(dir)
}
calculateEntryNodes(entry.Name)
for _, d := range dirNodes {
if !hasZipPathEntry(d, entries) {
var missingHeader zip.FileHeader
missingHeader.Name = d
missingHeader.SetModTime(time.Now())
missingHeader.SetMode(os.FileMode(uint32(0755)))
missingEntry := &zip.File{FileHeader: missingHeader}
writeZipEntryMetadata(output, missingEntry)
}
}
err := writeZipEntryMetadata(output, entry)
return err
}
func hasZipPathEntry(path string, entries []*zip.File) bool {
for _, e := range entries {
if e.Name == path {
return true
}
}
return false
}
func generateZipMetadata(output io.Writer, archive *zip.Reader) error { func generateZipMetadata(output io.Writer, archive *zip.Reader) error {
err := writeString(output, MetadataHeader) if err := writeString(output, MetadataHeader); err != nil {
if err != nil {
return err return err
} }
// Write empty error string // Write empty error string
err = writeString(output, "{}") if err := writeString(output, "{}"); err != nil {
if err != nil {
return err return err
} }
// Write all files // Write all files
for _, entry := range archive.File { for _, entry := range archive.File {
err = writeZipEntryMetadata(output, entry) if err := handleZipEntryMetadata(output, entry, archive.File); err != nil {
if 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