From 408b615c3c0c478a5b6485155f2fffc57ab452eb Mon Sep 17 00:00:00 2001 From: Aleksa Sarai Date: Sat, 12 Nov 2016 15:03:20 +1100 Subject: [PATCH] entries: comment with keyword metadata Since not every keyword applies to every type entry, include a comment with the keywords the manifest was generated with. Signed-off-by: Aleksa Sarai Signed-off-by: Vincent Batts --- tar.go | 9 +++++++-- walk.go | 22 ++++++++++++++++++++-- 2 files changed, 27 insertions(+), 4 deletions(-) diff --git a/tar.go b/tar.go index 8165841..8d9e0dd 100644 --- a/tar.go +++ b/tar.go @@ -82,8 +82,13 @@ func (ts *tarStream) readHeaders() { Set: nil, Keywords: []KeyVal{"type=dir"}, } - metadataEntries := signatureEntries("") - for _, e := range metadataEntries { + // insert signature and metadata comments first (user, machine, tree, date) + for _, e := range signatureEntries("") { + e.Pos = len(ts.creator.DH.Entries) + ts.creator.DH.Entries = append(ts.creator.DH.Entries, e) + } + // insert keyword metadata next + for _, e := range keywordEntries(ts.keywords) { e.Pos = len(ts.creator.DH.Entries) ts.creator.DH.Entries = append(ts.creator.DH.Entries, e) } diff --git a/walk.go b/walk.go index 5209d6e..7f5b171 100644 --- a/walk.go +++ b/walk.go @@ -7,6 +7,7 @@ import ( "os/user" "path/filepath" "sort" + "strings" "time" ) @@ -28,11 +29,16 @@ var defaultSetKeywords = []KeyVal{"type=file", "nlink=1", "flags=none", "mode=06 func Walk(root string, excludes []ExcludeFunc, keywords []Keyword) (*DirectoryHierarchy, error) { creator := dhCreator{DH: &DirectoryHierarchy{}} // insert signature and metadata comments first (user, machine, tree, date) - metadataEntries := signatureEntries(root) - for _, e := range metadataEntries { + for _, e := range signatureEntries(root) { e.Pos = len(creator.DH.Entries) creator.DH.Entries = append(creator.DH.Entries, e) } + // insert keyword metadata next + for _, e := range keywordEntries(keywords) { + e.Pos = len(creator.DH.Entries) + creator.DH.Entries = append(creator.DH.Entries, e) + } + // walk the directory and add entries err := startWalk(&creator, root, func(path string, info os.FileInfo, err error) error { if err != nil { return err @@ -356,3 +362,15 @@ func signatureEntries(root string) []Entry { return sigEntries } + +// keywordEntries returns a slice of entries including a comment of the +// keywords requested when generating this manifest. +func keywordEntries(keywords []Keyword) []Entry { + // Convert all of the keywords to zero-value keyvals. + return []Entry{ + { + Type: CommentType, + Raw: fmt.Sprintf("#%16s%s", "keywords: ", strings.Join(FromKeywords(keywords), ",")), + }, + } +}