From b493e2920f5adc56eb096291f8e9bffbf2af8ba6 Mon Sep 17 00:00:00 2001 From: Vincent Batts Date: Mon, 31 Oct 2016 14:10:42 +0000 Subject: [PATCH] keywords: BSD compat for a few keywords Signed-off-by: Vincent Batts --- keywords_bsd.go | 42 +++++++++++++++++++++++++++++++++++++++++ keywords_linux.go | 2 ++ keywords_unsupported.go | 2 +- 3 files changed, 45 insertions(+), 1 deletion(-) diff --git a/keywords_bsd.go b/keywords_bsd.go index e81d5c1..3a82a9b 100644 --- a/keywords_bsd.go +++ b/keywords_bsd.go @@ -3,8 +3,12 @@ package mtree import ( + "archive/tar" + "fmt" "io" "os" + "os/user" + "syscall" ) var ( @@ -12,4 +16,42 @@ var ( // ideally this will pull in from here https://www.freebsd.org/cgi/man.cgi?query=chflags&sektion=2 return "", nil } + + 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 { + return "", err + } + return fmt.Sprintf("uname=%s", u.Username), nil + } + 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, 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, 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, r io.Reader) (string, error) { + return "", nil + } ) diff --git a/keywords_linux.go b/keywords_linux.go index bac97ad..ed77015 100644 --- a/keywords_linux.go +++ b/keywords_linux.go @@ -1,3 +1,5 @@ +// +build linux + package mtree import ( diff --git a/keywords_unsupported.go b/keywords_unsupported.go index 3234532..aba791b 100644 --- a/keywords_unsupported.go +++ b/keywords_unsupported.go @@ -1,4 +1,4 @@ -// +build !linux +// +build !linux,!darwin,!freebsd,!netbsd,!openbsd package mtree