1
0
Fork 0
forked from mirrors/tar-split
This commit is contained in:
Vincent Batts 2015-08-11 21:59:23 -04:00
parent e2a62d6b0d
commit 5c8d5cacba
13 changed files with 265 additions and 50 deletions

23
tar/verify/headers.go Normal file
View file

@ -0,0 +1,23 @@
package verify
import "time"
// PosixHeader is the structure from a POSIX tar header, to be marshalled from
// the tar stream, and available for on-disk comparison and verification
type PosixHeader struct {
Name string `json:"name,omitempty"`
Mode uint32 `json:"mode,omitempty"`
UID uint32 `json:"uid,omitempty"`
GID uint32 `json:"gid,omitempty"`
Size int `json:"size,omitempty"`
Mtime time.Time `json:"mtime,omitempty"`
Checksum []byte `json:"chksum,omitempty"`
LinkName string `json:"linkname,omitempty"`
Magic []byte `json:"magic,omitempty"`
Version string `json:"version,omitempty"`
Uname string `json:"uname,omitempty"`
Gname string `json:"gname,omitempty"`
DevMajor int `json:"devmajor,omitempty"`
DevMinor int `json:"devminor,omitempty"`
Prefix string `json:"prefix,omitempty"`
}

34
tar/verify/verify.go Normal file
View file

@ -0,0 +1,34 @@
package verify
import "fmt"
// CheckType is how the on disk attributes will be verified against the
// recorded header information
type CheckType int
// Check types for customizing how fuzzy or strict on-disk verification will be
// handled
const (
CheckDigest CheckType = iota
CheckFileSize
CheckFileMode
CheckFileUser
CheckFileGroup
CheckFileMtime
CheckFileDevice
CheckFileLink
CheckFileCaps
)
var (
// DefaultChecks is the default for verfication steps against each
// storage.VerficationEntry
DefaultChecks = CheckDigest | CheckFileAttributes
// CheckFileAttributes are the group of file attribute checks done
CheckFileAttributes = CheckFileSize | CheckFileMode | CheckFileUser |
CheckFileGroup | CheckFileMtime | CheckFileDevice | CheckFileCaps |
CheckFileLink
// ErrNotSupportedPlatform is when the platform does not support given features
ErrNotSupportedPlatform = fmt.Errorf("platform does not support this feature")
)

114
tar/verify/xattrs_linux.go Normal file
View file

@ -0,0 +1,114 @@
package verify
/*
Lgetxattr and Lsetxattr are copied directly from https://github.com/docker/docker
./pkg/system/xattr_linux.go commit 7e420ad8502089e66ce0ade92bf70574f894f287
Apache License Version 2.0, January 2004 https://www.apache.org/licenses/
Copyright 2013-2015 Docker, Inc.
*/
import (
"bytes"
"syscall"
"unsafe"
)
/*
func main() {
for _, arg := range os.Args[1:] {
keys, err := Listxattr(arg)
if err != nil {
fmt.Println(err)
continue
}
if len(keys) > 0 {
fmt.Printf("%s : %q\n", arg, keys)
for _, key := range keys {
buf, err := Lgetxattr(arg, key)
if err != nil {
fmt.Printf(" ERROR: %s\n", err)
continue
}
fmt.Printf(" %s = %s\n", key, string(buf))
}
}
}
}
*/
// Listxattr is a helper around the syscall.Listxattr
func Listxattr(path string) ([]string, error) {
buf := make([]byte, 1024)
sz, err := syscall.Listxattr(path, buf)
if err == syscall.ENODATA {
return nil, nil
}
if err == syscall.ERANGE && sz > 0 {
buf = make([]byte, sz)
sz, err = syscall.Listxattr(path, buf)
}
keys := []string{}
for _, key := range bytes.Split(bytes.TrimSpace(buf), []byte{0x0}) {
if string(key) != "" {
keys = append(keys, string(key))
}
}
return keys, nil
}
// Lgetxattr retrieves the value of the extended attribute identified by attr
// and associated with the given path in the file system.
// It will returns a nil slice and nil error if the xattr is not set.
func Lgetxattr(path string, attr string) ([]byte, error) {
pathBytes, err := syscall.BytePtrFromString(path)
if err != nil {
return nil, err
}
attrBytes, err := syscall.BytePtrFromString(attr)
if err != nil {
return nil, err
}
dest := make([]byte, 128)
destBytes := unsafe.Pointer(&dest[0])
sz, _, errno := syscall.Syscall6(syscall.SYS_LGETXATTR, uintptr(unsafe.Pointer(pathBytes)), uintptr(unsafe.Pointer(attrBytes)), uintptr(destBytes), uintptr(len(dest)), 0, 0)
if errno == syscall.ENODATA {
return nil, nil
}
if errno == syscall.ERANGE {
dest = make([]byte, sz)
destBytes := unsafe.Pointer(&dest[0])
sz, _, errno = syscall.Syscall6(syscall.SYS_LGETXATTR, uintptr(unsafe.Pointer(pathBytes)), uintptr(unsafe.Pointer(attrBytes)), uintptr(destBytes), uintptr(len(dest)), 0, 0)
}
if errno != 0 {
return nil, errno
}
return dest[:sz], nil
}
var _zero uintptr
// Lsetxattr sets the value of the extended attribute identified by attr
// and associated with the given path in the file system.
func Lsetxattr(path string, attr string, data []byte, flags int) error {
pathBytes, err := syscall.BytePtrFromString(path)
if err != nil {
return err
}
attrBytes, err := syscall.BytePtrFromString(attr)
if err != nil {
return err
}
var dataBytes unsafe.Pointer
if len(data) > 0 {
dataBytes = unsafe.Pointer(&data[0])
} else {
dataBytes = unsafe.Pointer(&_zero)
}
_, _, errno := syscall.Syscall6(syscall.SYS_LSETXATTR, uintptr(unsafe.Pointer(pathBytes)), uintptr(unsafe.Pointer(attrBytes)), uintptr(dataBytes), uintptr(len(data)), uintptr(flags), 0)
if errno != 0 {
return errno
}
return nil
}

View file

@ -0,0 +1,13 @@
// +build !linux
package verify
// Lgetxattr is not supported on platforms other than linux.
func Lgetxattr(path string, attr string) ([]byte, error) {
return nil, ErrNotSupportedPlatform
}
// Lsetxattr is not supported on platforms other than linux.
func Lsetxattr(path string, attr string, data []byte, flags int) error {
return ErrNotSupportedPlatform
}