From 68651d77d64ea36f3c2eef143144f6da28823a12 Mon Sep 17 00:00:00 2001 From: Vincent Batts Date: Thu, 15 Jun 2017 10:59:27 -0500 Subject: [PATCH] keyword: add missing `gname` keyword functions Fixes: #128 Reported-by: Lennart Poettering Signed-off-by: Vincent Batts --- keywordfunc.go | 1 + keywords_bsd.go => keywordfuncs_bsd.go | 12 ++++++++++++ keywordfuncs_linux.go | 12 ++++++++++++ keywordfuncs_unsupported.go | 6 ++++++ 4 files changed, 31 insertions(+) rename keywords_bsd.go => keywordfuncs_bsd.go (80%) diff --git a/keywordfunc.go b/keywordfunc.go index e88fa2e..266fa77 100644 --- a/keywordfunc.go +++ b/keywordfunc.go @@ -34,6 +34,7 @@ var ( "gid": gidKeywordFunc, // The file group as a numeric value "nlink": nlinkKeywordFunc, // The number of hard links the file is expected to have "uname": unameKeywordFunc, // The file owner as a symbolic name + "gname": gnameKeywordFunc, // The file group as a symbolic name "mode": modeKeywordFunc, // The current file's permissions as a numeric (octal) or symbolic value "cksum": cksumKeywordFunc, // The checksum of the file using the default algorithm specified by the cksum(1) utility "md5": hasherKeywordFunc("md5digest", md5.New), // The MD5 message digest of the file diff --git a/keywords_bsd.go b/keywordfuncs_bsd.go similarity index 80% rename from keywords_bsd.go rename to keywordfuncs_bsd.go index 43a8073..eb5cc3f 100644 --- a/keywords_bsd.go +++ b/keywordfuncs_bsd.go @@ -29,6 +29,18 @@ var ( } return KeyVal(fmt.Sprintf("uname=%s", u.Username)), nil } + 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) + g, err := user.LookupGroupId(fmt.Sprintf("%d", stat.Gid)) + if err != nil { + return emptyKV, err + } + return KeyVal(fmt.Sprintf("gname=%s", g.Name)), nil + } uidKeywordFunc = func(path string, info os.FileInfo, r io.Reader) (KeyVal, error) { if hdr, ok := info.Sys().(*tar.Header); ok { return KeyVal(fmt.Sprintf("uid=%d", hdr.Uid)), nil diff --git a/keywordfuncs_linux.go b/keywordfuncs_linux.go index bab7a8d..1fd843d 100644 --- a/keywordfuncs_linux.go +++ b/keywordfuncs_linux.go @@ -34,6 +34,18 @@ var ( } return KeyVal(fmt.Sprintf("uname=%s", u.Username)), nil } + 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) + g, err := user.LookupGroupId(fmt.Sprintf("%d", stat.Gid)) + if err != nil { + return emptyKV, err + } + return KeyVal(fmt.Sprintf("gname=%s", g.Name)), nil + } uidKeywordFunc = func(path string, info os.FileInfo, r io.Reader) (KeyVal, error) { if hdr, ok := info.Sys().(*tar.Header); ok { return KeyVal(fmt.Sprintf("uid=%d", hdr.Uid)), nil diff --git a/keywordfuncs_unsupported.go b/keywordfuncs_unsupported.go index e890ccb..a582f79 100644 --- a/keywordfuncs_unsupported.go +++ b/keywordfuncs_unsupported.go @@ -20,6 +20,12 @@ var ( } return emptyKV, nil } + 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 + } + return emptyKV, nil + } uidKeywordFunc = func(path string, info os.FileInfo, r io.Reader) (KeyVal, error) { if hdr, ok := info.Sys().(*tar.Header); ok { return KeyVal(fmt.Sprintf("uid=%d", hdr.Uid)), nil