*: refactoring to support streams

when creating a manifest from, or validating, a stream like a tar
archive, it requires thinking about some of the functions differently
than walking a directory tree.

This is the beginning of allowing for such features.

Signed-off-by: Vincent Batts <vbatts@hashbangbash.com>
This commit is contained in:
Vincent Batts 2016-04-15 18:39:18 -04:00 committed by Stephen Chung
parent 119cdc314c
commit faa80931af
11 changed files with 534 additions and 133 deletions

View file

@ -3,8 +3,10 @@
package mtree
import (
"archive/tar"
"crypto/sha1"
"fmt"
"io"
"os"
"os/user"
"strings"
@ -14,7 +16,11 @@ import (
)
var (
unameKeywordFunc = func(path string, info os.FileInfo) (string, error) {
unameKeywordFunc = func(path string, info os.FileInfo, r io.Reader) (string, error) {
if hdr, ok := info.Sys().(*tar.Header); ok {
return fmt.Sprintf("uname=%s", hdr.Uname), nil
}
stat := info.Sys().(*syscall.Stat_t)
u, err := user.LookupId(fmt.Sprintf("%d", stat.Uid))
if err != nil {
@ -22,19 +28,40 @@ var (
}
return fmt.Sprintf("uname=%s", u.Username), nil
}
uidKeywordFunc = func(path string, info os.FileInfo) (string, error) {
uidKeywordFunc = func(path string, info os.FileInfo, r io.Reader) (string, error) {
if hdr, ok := info.Sys().(*tar.Header); ok {
return fmt.Sprintf("uid=%d", hdr.Uid), nil
}
stat := info.Sys().(*syscall.Stat_t)
return fmt.Sprintf("uid=%d", stat.Uid), nil
}
gidKeywordFunc = func(path string, info os.FileInfo) (string, error) {
stat := info.Sys().(*syscall.Stat_t)
return fmt.Sprintf("gid=%d", stat.Gid), nil
gidKeywordFunc = func(path string, info os.FileInfo, r io.Reader) (string, error) {
if hdr, ok := info.Sys().(*tar.Header); ok {
return fmt.Sprintf("gid=%d", hdr.Gid), nil
}
if stat, ok := info.Sys().(*syscall.Stat_t); ok {
return fmt.Sprintf("gid=%d", stat.Gid), nil
}
return "", nil
}
nlinkKeywordFunc = func(path string, info os.FileInfo) (string, error) {
stat := info.Sys().(*syscall.Stat_t)
return fmt.Sprintf("nlink=%d", stat.Nlink), nil
nlinkKeywordFunc = func(path string, info os.FileInfo, r io.Reader) (string, error) {
if stat, ok := info.Sys().(*syscall.Stat_t); ok {
return fmt.Sprintf("nlink=%d", stat.Nlink), nil
}
return "", nil
}
xattrKeywordFunc = func(path string, info os.FileInfo) (string, error) {
xattrKeywordFunc = func(path string, info os.FileInfo, r io.Reader) (string, error) {
if hdr, ok := info.Sys().(*tar.Header); ok {
if len(hdr.Xattrs) == 0 {
return "", nil
}
klist := []string{}
for k, v := range hdr.Xattrs {
klist = append(klist, fmt.Sprintf("xattr.%s=%x", k, sha1.Sum([]byte(v))))
}
return strings.Join(klist, " "), nil
}
xlist, err := xattr.List(path)
if err != nil {
return "", err