2016-03-16 19:59:34 +00:00
|
|
|
// +build linux
|
|
|
|
|
|
|
|
package mtree
|
|
|
|
|
|
|
|
import (
|
2016-04-15 22:39:18 +00:00
|
|
|
"archive/tar"
|
2016-03-17 17:54:59 +00:00
|
|
|
"crypto/sha1"
|
2016-03-16 19:59:34 +00:00
|
|
|
"fmt"
|
2016-04-15 22:39:18 +00:00
|
|
|
"io"
|
2016-03-16 19:59:34 +00:00
|
|
|
"os"
|
|
|
|
"os/user"
|
2016-03-17 17:54:59 +00:00
|
|
|
"strings"
|
2016-03-16 19:59:34 +00:00
|
|
|
"syscall"
|
2016-03-17 17:54:59 +00:00
|
|
|
|
2016-03-31 18:04:53 +00:00
|
|
|
"github.com/vbatts/go-mtree/xattr"
|
2016-03-16 19:59:34 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2016-04-15 22:39:18 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2016-03-16 19:59:34 +00:00
|
|
|
stat := info.Sys().(*syscall.Stat_t)
|
|
|
|
u, err := user.LookupId(fmt.Sprintf("%d", stat.Uid))
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return fmt.Sprintf("uname=%s", u.Username), nil
|
|
|
|
}
|
2016-04-15 22:39:18 +00:00
|
|
|
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
|
|
|
|
}
|
2016-03-16 19:59:34 +00:00
|
|
|
stat := info.Sys().(*syscall.Stat_t)
|
|
|
|
return fmt.Sprintf("uid=%d", stat.Uid), nil
|
|
|
|
}
|
2016-04-15 22:39:18 +00:00
|
|
|
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
|
2016-03-16 19:59:34 +00:00
|
|
|
}
|
2016-04-15 22:39:18 +00:00
|
|
|
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
|
2016-03-16 19:59:34 +00:00
|
|
|
}
|
2016-04-15 22:39:18 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2016-03-17 17:54:59 +00:00
|
|
|
xlist, err := xattr.List(path)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
klist := make([]string, len(xlist))
|
|
|
|
for i := range xlist {
|
|
|
|
data, err := xattr.Get(path, xlist[i])
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
klist[i] = fmt.Sprintf("xattr.%s=%x", xlist[i], sha1.Sum(data))
|
|
|
|
}
|
|
|
|
return strings.Join(klist, " "), nil
|
|
|
|
}
|
2016-03-16 19:59:34 +00:00
|
|
|
)
|