Skip to content

Commit

Permalink
Merge branch 'master' into tree-stacking
Browse files Browse the repository at this point in the history
  • Loading branch information
wagoodman committed May 25, 2018
2 parents 86e979c + a096415 commit 2a4e6b7
Show file tree
Hide file tree
Showing 2 changed files with 118 additions and 4 deletions.
121 changes: 117 additions & 4 deletions tar-read.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@ package main

import (
"archive/tar"
"bytes"
"crypto/md5"
"encoding/json"
"fmt"
"io"
"os"
"strings"
)

func main() {
Expand All @@ -22,7 +26,9 @@ func main() {
// }

tarReader := tar.NewReader(f)

targetName := "manifest.json"
var m Manifest
var trees []*Tree
for {
header, err := tarReader.Next()

Expand All @@ -36,14 +42,70 @@ func main() {
}

name := header.Name
if name == targetName {
m = handleManifest(tarReader, header)
}

switch header.Typeflag {
case tar.TypeDir:
continue
case tar.TypeReg:
fmt.Println("File: ", name)
// show the contents
// io.Copy(os.Stdout, tarReader)
//fmt.Println("File: ", name)
if strings.HasSuffix(name, "layer.tar") {
fmt.Println("Containing:")
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)
}
default:
fmt.Printf("%s : %c %s %s\n",
"hmmm?",
header.Typeflag,
"in file",
name,
)
}
}
fmt.Printf("%+v\n", m)
fmt.Printf("%+v\n", trees)
}

func getFileList(parentReader *tar.Reader, h *tar.Header) []FileChangeInfo {
var files []FileChangeInfo
size := h.Size
tarredBytes := make([]byte, size)
_, err := parentReader.Read(tarredBytes)
if err != nil {
panic(err)
}
r := bytes.NewReader(tarredBytes)
tarReader := tar.NewReader(r)
for {
header, err := tarReader.Next()

if err == io.EOF {
break
}

if err != nil {
fmt.Println(err)
os.Exit(1)
}

name := header.Name

switch header.Typeflag {
case tar.TypeDir:
files = append(files, makeEntry(tarReader, header, name))
case tar.TypeReg:
files = append(files, makeEntry(tarReader, header, name))
continue
case tar.TypeSymlink:
files = append(files, makeEntry(tarReader, header, name))
default:
fmt.Printf("%s : %c %s %s\n",
"hmmm?",
Expand All @@ -53,4 +115,55 @@ func main() {
)
}
}
return files
}

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)
_, 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 {
path string
typeflag byte
md5sum [16]byte
}

type Manifest struct {
Config string
RepoTags []string
Layers []string
}

func handleManifest(r *tar.Reader, header *tar.Header) Manifest {
size := header.Size
manifestBytes := make([]byte, size)
_, err := r.Read(manifestBytes)
if err != nil {
panic(err)
}
var m [1]Manifest
err = json.Unmarshal(manifestBytes, &m)
if err != nil {
panic(err)
}
return m[0]
}
1 change: 1 addition & 0 deletions tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const (
type Tree struct {
root *Node
size int
name string
}

type Node struct {
Expand Down

0 comments on commit 2a4e6b7

Please sign in to comment.