2016-10-31 14:10:42 +00:00
|
|
|
// +build linux
|
|
|
|
|
2016-03-16 19:59:34 +00:00
|
|
|
package mtree
|
|
|
|
|
|
|
|
import (
|
2016-04-15 22:39:18 +00:00
|
|
|
"archive/tar"
|
2016-07-27 21:40:51 +00:00
|
|
|
"encoding/base64"
|
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
|
|
|
|
2017-02-15 16:10:30 +00:00
|
|
|
"github.com/vbatts/go-mtree/pkg/govis"
|
2016-03-31 18:04:53 +00:00
|
|
|
"github.com/vbatts/go-mtree/xattr"
|
2016-03-16 19:59:34 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2016-10-31 17:25:11 +00:00
|
|
|
// this is bsd specific https://www.freebsd.org/cgi/man.cgi?query=chflags&sektion=2
|
2016-11-18 00:47:31 +00:00
|
|
|
flagsKeywordFunc = func(path string, info os.FileInfo, r io.Reader) (KeyVal, error) {
|
|
|
|
return emptyKV, nil
|
2016-10-31 17:25:11 +00:00
|
|
|
}
|
|
|
|
|
2016-11-18 00:47:31 +00:00
|
|
|
unameKeywordFunc = func(path string, info os.FileInfo, r io.Reader) (KeyVal, error) {
|
2016-04-15 22:39:18 +00:00
|
|
|
if hdr, ok := info.Sys().(*tar.Header); ok {
|
2016-11-18 00:47:31 +00:00
|
|
|
return KeyVal(fmt.Sprintf("uname=%s", hdr.Uname)), nil
|
2016-04-15 22:39:18 +00:00
|
|
|
}
|
|
|
|
|
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 {
|
2016-11-18 00:47:31 +00:00
|
|
|
return emptyKV, err
|
2016-03-16 19:59:34 +00:00
|
|
|
}
|
2016-11-18 00:47:31 +00:00
|
|
|
return KeyVal(fmt.Sprintf("uname=%s", u.Username)), nil
|
2016-03-16 19:59:34 +00:00
|
|
|
}
|
2017-06-15 15:59:27 +00:00
|
|
|
gnameKeywordFunc = func(path string, info os.FileInfo, r io.Reader) (KeyVal, error) {
|
|
|
|
if hdr, ok := info.Sys().(*tar.Header); ok {
|
|
|
|
return KeyVal(fmt.Sprintf("gname=%s", hdr.Gname)), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
stat := info.Sys().(*syscall.Stat_t)
|
2017-06-15 17:07:52 +00:00
|
|
|
g, err := lookupGroupID(fmt.Sprintf("%d", stat.Gid))
|
2017-06-15 15:59:27 +00:00
|
|
|
if err != nil {
|
|
|
|
return emptyKV, err
|
|
|
|
}
|
|
|
|
return KeyVal(fmt.Sprintf("gname=%s", g.Name)), nil
|
|
|
|
}
|
2016-11-18 00:47:31 +00:00
|
|
|
uidKeywordFunc = func(path string, info os.FileInfo, r io.Reader) (KeyVal, error) {
|
2016-04-15 22:39:18 +00:00
|
|
|
if hdr, ok := info.Sys().(*tar.Header); ok {
|
2016-11-18 00:47:31 +00:00
|
|
|
return KeyVal(fmt.Sprintf("uid=%d", hdr.Uid)), nil
|
2016-04-15 22:39:18 +00:00
|
|
|
}
|
2016-03-16 19:59:34 +00:00
|
|
|
stat := info.Sys().(*syscall.Stat_t)
|
2016-11-18 00:47:31 +00:00
|
|
|
return KeyVal(fmt.Sprintf("uid=%d", stat.Uid)), nil
|
2016-03-16 19:59:34 +00:00
|
|
|
}
|
2016-11-18 00:47:31 +00:00
|
|
|
gidKeywordFunc = func(path string, info os.FileInfo, r io.Reader) (KeyVal, error) {
|
2016-04-15 22:39:18 +00:00
|
|
|
if hdr, ok := info.Sys().(*tar.Header); ok {
|
2016-11-18 00:47:31 +00:00
|
|
|
return KeyVal(fmt.Sprintf("gid=%d", hdr.Gid)), nil
|
2016-04-15 22:39:18 +00:00
|
|
|
}
|
|
|
|
if stat, ok := info.Sys().(*syscall.Stat_t); ok {
|
2016-11-18 00:47:31 +00:00
|
|
|
return KeyVal(fmt.Sprintf("gid=%d", stat.Gid)), nil
|
2016-04-15 22:39:18 +00:00
|
|
|
}
|
2016-11-18 00:47:31 +00:00
|
|
|
return emptyKV, nil
|
2016-03-16 19:59:34 +00:00
|
|
|
}
|
2016-11-18 00:47:31 +00:00
|
|
|
nlinkKeywordFunc = func(path string, info os.FileInfo, r io.Reader) (KeyVal, error) {
|
2016-04-15 22:39:18 +00:00
|
|
|
if stat, ok := info.Sys().(*syscall.Stat_t); ok {
|
2016-11-18 00:47:31 +00:00
|
|
|
return KeyVal(fmt.Sprintf("nlink=%d", stat.Nlink)), nil
|
2016-04-15 22:39:18 +00:00
|
|
|
}
|
2016-11-18 00:47:31 +00:00
|
|
|
return emptyKV, nil
|
2016-03-16 19:59:34 +00:00
|
|
|
}
|
2016-11-18 00:47:31 +00:00
|
|
|
xattrKeywordFunc = func(path string, info os.FileInfo, r io.Reader) (KeyVal, error) {
|
2016-04-15 22:39:18 +00:00
|
|
|
if hdr, ok := info.Sys().(*tar.Header); ok {
|
|
|
|
if len(hdr.Xattrs) == 0 {
|
2016-11-18 00:47:31 +00:00
|
|
|
return emptyKV, nil
|
2016-04-15 22:39:18 +00:00
|
|
|
}
|
2016-11-18 00:47:31 +00:00
|
|
|
klist := []KeyVal{}
|
2016-04-15 22:39:18 +00:00
|
|
|
for k, v := range hdr.Xattrs {
|
2017-02-15 16:10:30 +00:00
|
|
|
encKey, err := govis.Vis(k, DefaultVisFlags)
|
2016-12-17 15:57:45 +00:00
|
|
|
if err != nil {
|
|
|
|
return emptyKV, err
|
|
|
|
}
|
|
|
|
klist = append(klist, KeyVal(fmt.Sprintf("xattr.%s=%s", encKey, base64.StdEncoding.EncodeToString([]byte(v)))))
|
2016-04-15 22:39:18 +00:00
|
|
|
}
|
2016-11-18 00:47:31 +00:00
|
|
|
return KeyVal(strings.Join(KeyValToString(klist), " ")), nil
|
2016-04-15 22:39:18 +00:00
|
|
|
}
|
2016-09-01 19:52:24 +00:00
|
|
|
if !info.Mode().IsRegular() && !info.Mode().IsDir() {
|
2016-11-18 00:47:31 +00:00
|
|
|
return emptyKV, nil
|
2016-09-01 19:52:24 +00:00
|
|
|
}
|
2016-04-15 22:39:18 +00:00
|
|
|
|
2016-03-17 17:54:59 +00:00
|
|
|
xlist, err := xattr.List(path)
|
|
|
|
if err != nil {
|
2016-11-18 00:47:31 +00:00
|
|
|
return emptyKV, err
|
2016-03-17 17:54:59 +00:00
|
|
|
}
|
2016-11-18 00:47:31 +00:00
|
|
|
klist := make([]KeyVal, len(xlist))
|
2016-03-17 17:54:59 +00:00
|
|
|
for i := range xlist {
|
|
|
|
data, err := xattr.Get(path, xlist[i])
|
|
|
|
if err != nil {
|
2016-11-18 00:47:31 +00:00
|
|
|
return emptyKV, err
|
2016-03-17 17:54:59 +00:00
|
|
|
}
|
2017-02-15 16:10:30 +00:00
|
|
|
encKey, err := govis.Vis(xlist[i], DefaultVisFlags)
|
2016-12-17 15:57:45 +00:00
|
|
|
if err != nil {
|
|
|
|
return emptyKV, err
|
|
|
|
}
|
|
|
|
klist[i] = KeyVal(fmt.Sprintf("xattr.%s=%s", encKey, base64.StdEncoding.EncodeToString(data)))
|
2016-03-17 17:54:59 +00:00
|
|
|
}
|
2016-11-18 00:47:31 +00:00
|
|
|
return KeyVal(strings.Join(KeyValToString(klist), " ")), nil
|
2016-03-17 17:54:59 +00:00
|
|
|
}
|
2016-03-16 19:59:34 +00:00
|
|
|
)
|