print trees in order
This commit is contained in:
parent
503c61ac58
commit
8794f159de
56
tar-read.go
56
tar-read.go
@ -6,7 +6,6 @@ import (
|
|||||||
"crypto/md5"
|
"crypto/md5"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"hash"
|
|
||||||
"io"
|
"io"
|
||||||
"os"
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
@ -29,6 +28,7 @@ func main() {
|
|||||||
tarReader := tar.NewReader(f)
|
tarReader := tar.NewReader(f)
|
||||||
targetName := "manifest.json"
|
targetName := "manifest.json"
|
||||||
var m Manifest
|
var m Manifest
|
||||||
|
var trees []*Tree
|
||||||
for {
|
for {
|
||||||
header, err := tarReader.Next()
|
header, err := tarReader.Next()
|
||||||
|
|
||||||
@ -50,13 +50,17 @@ func main() {
|
|||||||
case tar.TypeDir:
|
case tar.TypeDir:
|
||||||
continue
|
continue
|
||||||
case tar.TypeReg:
|
case tar.TypeReg:
|
||||||
fmt.Println("File: ", name)
|
//fmt.Println("File: ", name)
|
||||||
if strings.HasSuffix(name, "layer.tar") {
|
if strings.HasSuffix(name, "layer.tar") {
|
||||||
fmt.Println("Containing:")
|
fmt.Println("Containing:")
|
||||||
printFilesInTar(tarReader, header)
|
tree := NewTree()
|
||||||
|
tree.name = strings.TrimSuffix(name, "layer.tar")
|
||||||
|
fileInfos := getFileList(tarReader, header)
|
||||||
|
for _, element := range fileInfos {
|
||||||
|
tree.AddPath(element.path, element)
|
||||||
|
}
|
||||||
|
trees = append(trees, tree)
|
||||||
}
|
}
|
||||||
// show the contents
|
|
||||||
// io.Copy(os.Stdout, tarReader)
|
|
||||||
default:
|
default:
|
||||||
fmt.Printf("%s : %c %s %s\n",
|
fmt.Printf("%s : %c %s %s\n",
|
||||||
"hmmm?",
|
"hmmm?",
|
||||||
@ -67,10 +71,11 @@ func main() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
fmt.Printf("%+v\n", m)
|
fmt.Printf("%+v\n", m)
|
||||||
|
fmt.Printf("%+v\n", trees)
|
||||||
}
|
}
|
||||||
|
|
||||||
func printFilesInTar(parentReader *tar.Reader, h *tar.Header) {
|
func getFileList(parentReader *tar.Reader, h *tar.Header) []FileChangeInfo {
|
||||||
hasher := md5.New
|
var files []FileChangeInfo
|
||||||
size := h.Size
|
size := h.Size
|
||||||
tarredBytes := make([]byte, size)
|
tarredBytes := make([]byte, size)
|
||||||
_, err := parentReader.Read(tarredBytes)
|
_, err := parentReader.Read(tarredBytes)
|
||||||
@ -95,14 +100,12 @@ func printFilesInTar(parentReader *tar.Reader, h *tar.Header) {
|
|||||||
|
|
||||||
switch header.Typeflag {
|
switch header.Typeflag {
|
||||||
case tar.TypeDir:
|
case tar.TypeDir:
|
||||||
fmt.Println(" Directory: ", name)
|
files = append(files, makeEntry(tarReader, header, name))
|
||||||
continue
|
|
||||||
case tar.TypeReg:
|
case tar.TypeReg:
|
||||||
fmt.Println(" File: ", name)
|
files = append(files, makeEntry(tarReader, header, name))
|
||||||
// show the contents
|
continue
|
||||||
// io.Copy(os.Stdout, tarReader)
|
|
||||||
case tar.TypeSymlink:
|
case tar.TypeSymlink:
|
||||||
fmt.Println(" SymLink", name)
|
files = append(files, makeEntry(tarReader, header, name))
|
||||||
default:
|
default:
|
||||||
fmt.Printf("%s : %c %s %s\n",
|
fmt.Printf("%s : %c %s %s\n",
|
||||||
"hmmm?",
|
"hmmm?",
|
||||||
@ -112,16 +115,35 @@ func printFilesInTar(parentReader *tar.Reader, h *tar.Header) {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return files
|
||||||
}
|
}
|
||||||
|
|
||||||
func makeEntry(r *tar.Reader, h *tar.Header, hasher *hash.Hash) FileChangeInfo {
|
func makeEntry(r *tar.Reader, h *tar.Header, path string) FileChangeInfo {
|
||||||
|
if h.Typeflag == tar.TypeDir {
|
||||||
|
return FileChangeInfo{
|
||||||
|
path: path,
|
||||||
|
typeflag: h.Typeflag,
|
||||||
|
md5sum: zeros,
|
||||||
|
}
|
||||||
|
}
|
||||||
fileBytes := make([]byte, h.Size)
|
fileBytes := make([]byte, h.Size)
|
||||||
|
_, err := r.Read(fileBytes)
|
||||||
|
if err != nil && err != io.EOF {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
hash := md5.Sum(fileBytes)
|
||||||
|
return FileChangeInfo{
|
||||||
|
path: path,
|
||||||
|
typeflag: h.Typeflag,
|
||||||
|
md5sum: hash,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var zeros = [16]byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
|
||||||
|
|
||||||
type FileChangeInfo struct {
|
type FileChangeInfo struct {
|
||||||
fileName string
|
path string
|
||||||
typeflag int
|
typeflag byte
|
||||||
md5sum [16]byte
|
md5sum [16]byte
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user