1
0
Fork 0
mirror of https://github.com/vbatts/go-mtree.git synced 2024-11-25 17:45:39 +00:00
go-mtree/keywords_linux.go
Vincent Batts 0898fd6d90 mtree: non-relative import path
Signed-off-by: Vincent Batts <vbatts@hashbangbash.com>
2016-03-31 14:04:53 -04:00

52 lines
1.3 KiB
Go

// +build linux
package mtree
import (
"crypto/sha1"
"fmt"
"os"
"os/user"
"strings"
"syscall"
"github.com/vbatts/go-mtree/xattr"
)
var (
unameKeywordFunc = func(path string, info os.FileInfo) (string, error) {
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
}
uidKeywordFunc = func(path string, info os.FileInfo) (string, error) {
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
}
nlinkKeywordFunc = func(path string, info os.FileInfo) (string, error) {
stat := info.Sys().(*syscall.Stat_t)
return fmt.Sprintf("nlink=%d", stat.Nlink), nil
}
xattrKeywordFunc = func(path string, info os.FileInfo) (string, error) {
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
}
)