walk: create and insert metadata signature comments
create Entry's for metadata signature that describes time, machine, date, and user and append these Entry's to creator.dh.Entries before starting the actual walk. Signed-off-by: Stephen Chung <schung@redhat.com>
This commit is contained in:
parent
0b85ce1f81
commit
4b3a9bfd02
4 changed files with 71 additions and 3 deletions
|
@ -75,7 +75,7 @@ func main() {
|
||||||
}
|
}
|
||||||
|
|
||||||
// -p <path>
|
// -p <path>
|
||||||
var rootPath string = "."
|
var rootPath = "."
|
||||||
if *flPath != "" {
|
if *flPath != "" {
|
||||||
rootPath = *flPath
|
rootPath = *flPath
|
||||||
}
|
}
|
||||||
|
|
4
tar.go
4
tar.go
|
@ -10,11 +10,15 @@ import (
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// Streamer interface that wraps an io.ReadCloser with a function that will
|
||||||
|
// return it's Hierarchy
|
||||||
type Streamer interface {
|
type Streamer interface {
|
||||||
io.ReadCloser
|
io.ReadCloser
|
||||||
Hierarchy() (*DirectoryHierarchy, error)
|
Hierarchy() (*DirectoryHierarchy, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// NewTarStreamer streams a tar archive and creates a file hierarchy based off
|
||||||
|
// of the tar metadata headers
|
||||||
func NewTarStreamer(r io.Reader, keywords []string) Streamer {
|
func NewTarStreamer(r io.Reader, keywords []string) Streamer {
|
||||||
pR, pW := io.Pipe()
|
pR, pW := io.Pipe()
|
||||||
ts := &tarStream{
|
ts := &tarStream{
|
||||||
|
|
|
@ -9,7 +9,7 @@ import (
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
func ExampleTar() {
|
func ExampleStreamer() {
|
||||||
fh, err := os.Open("./testdata/test.tar")
|
fh, err := os.Open("./testdata/test.tar")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// handle error ...
|
// handle error ...
|
||||||
|
|
66
walk.go
66
walk.go
|
@ -1,10 +1,13 @@
|
||||||
package mtree
|
package mtree
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"os"
|
"os"
|
||||||
|
"os/user"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"sort"
|
"sort"
|
||||||
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
// ExcludeFunc is the type of function called on each path walked to determine
|
// ExcludeFunc is the type of function called on each path walked to determine
|
||||||
|
@ -19,7 +22,12 @@ var defaultSetKeywords = []string{"type=file", "nlink=1", "flags=none", "mode=06
|
||||||
// walked paths. The recommended default list is DefaultKeywords.
|
// walked paths. The recommended default list is DefaultKeywords.
|
||||||
func Walk(root string, exlcudes []ExcludeFunc, keywords []string) (*DirectoryHierarchy, error) {
|
func Walk(root string, exlcudes []ExcludeFunc, keywords []string) (*DirectoryHierarchy, error) {
|
||||||
creator := dhCreator{DH: &DirectoryHierarchy{}}
|
creator := dhCreator{DH: &DirectoryHierarchy{}}
|
||||||
// TODO insert signature and metadata comments first (user, machine, tree, date)
|
// insert signature and metadata comments first (user, machine, tree, date)
|
||||||
|
metadataEntries := signatureEntries(root)
|
||||||
|
for _, e := range metadataEntries {
|
||||||
|
e.Pos = len(creator.DH.Entries)
|
||||||
|
creator.DH.Entries = append(creator.DH.Entries, e)
|
||||||
|
}
|
||||||
err := startWalk(&creator, root, func(path string, info os.FileInfo, err error) error {
|
err := startWalk(&creator, root, func(path string, info os.FileInfo, err error) error {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
@ -277,3 +285,59 @@ func readOrderedDirNames(dirname string) ([]string, error) {
|
||||||
sort.Strings(dirnames)
|
sort.Strings(dirnames)
|
||||||
return append(names, dirnames...), nil
|
return append(names, dirnames...), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// signatureEntries is a simple helper function that returns a slice of Entry's
|
||||||
|
// that describe the metadata signature about the host. Items like date, user,
|
||||||
|
// machine, and tree (which is specified by argument `root`), are considered.
|
||||||
|
// These Entry's construct comments in the mtree specification, so if there is
|
||||||
|
// an error trying to obtain a particular metadata, we simply don't construct
|
||||||
|
// the Entry.
|
||||||
|
func signatureEntries(root string) []Entry {
|
||||||
|
var sigEntries []Entry
|
||||||
|
user, err := user.Current()
|
||||||
|
if err == nil {
|
||||||
|
userEntry := Entry{
|
||||||
|
Type: CommentType,
|
||||||
|
Raw: fmt.Sprintf("#%16s%s", "user: ", user.Username),
|
||||||
|
}
|
||||||
|
sigEntries = append(sigEntries, userEntry)
|
||||||
|
}
|
||||||
|
|
||||||
|
hostname, err := os.Hostname()
|
||||||
|
if err == nil {
|
||||||
|
hostEntry := Entry{
|
||||||
|
Type: CommentType,
|
||||||
|
Raw: fmt.Sprintf("#%16s%s", "machine: ", hostname),
|
||||||
|
}
|
||||||
|
sigEntries = append(sigEntries, hostEntry)
|
||||||
|
}
|
||||||
|
|
||||||
|
if tree := filepath.Clean(root); tree == "." || tree == ".." {
|
||||||
|
root, err := os.Getwd()
|
||||||
|
if err == nil {
|
||||||
|
// use parent directory of current directory
|
||||||
|
if tree == ".." {
|
||||||
|
root = filepath.Dir(root)
|
||||||
|
}
|
||||||
|
treeEntry := Entry{
|
||||||
|
Type: CommentType,
|
||||||
|
Raw: fmt.Sprintf("#%16s%s", "tree: ", filepath.Clean(root)),
|
||||||
|
}
|
||||||
|
sigEntries = append(sigEntries, treeEntry)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
treeEntry := Entry{
|
||||||
|
Type: CommentType,
|
||||||
|
Raw: fmt.Sprintf("#%16s%s", "tree: ", filepath.Clean(root)),
|
||||||
|
}
|
||||||
|
sigEntries = append(sigEntries, treeEntry)
|
||||||
|
}
|
||||||
|
|
||||||
|
dateEntry := Entry{
|
||||||
|
Type: CommentType,
|
||||||
|
Raw: fmt.Sprintf("#%16s%s", "date: ", time.Now().Format("Mon Jan 2 15:04:05 2006")),
|
||||||
|
}
|
||||||
|
sigEntries = append(sigEntries, dateEntry)
|
||||||
|
|
||||||
|
return sigEntries
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue