File: keywordfuncs_linux.go

package info (click to toggle)
golang-github-vbatts-go-mtree 0.5.4%2Bds-1~exp1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 796 kB
  • sloc: sh: 198; makefile: 80
file content (108 lines) | stat: -rw-r--r-- 3,154 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
//go:build linux
// +build linux

package mtree

import (
	"archive/tar"
	"encoding/base64"
	"fmt"
	"io"
	"os"
	"os/user"
	"syscall"

	"github.com/vbatts/go-mtree/pkg/govis"
	"github.com/vbatts/go-mtree/xattr"
)

var (
	// this is bsd specific https://www.freebsd.org/cgi/man.cgi?query=chflags&sektion=2
	flagsKeywordFunc = func(path string, info os.FileInfo, r io.Reader) ([]KeyVal, error) {
		return nil, nil
	}

	unameKeywordFunc = func(path string, info os.FileInfo, r io.Reader) ([]KeyVal, error) {
		if hdr, ok := info.Sys().(*tar.Header); ok {
			return []KeyVal{KeyVal(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 nil, nil
		}
		return []KeyVal{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{KeyVal(fmt.Sprintf("gname=%s", hdr.Gname))}, nil
		}

		stat := info.Sys().(*syscall.Stat_t)
		g, err := lookupGroupID(fmt.Sprintf("%d", stat.Gid))
		if err != nil {
			return nil, nil
		}
		return []KeyVal{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{KeyVal(fmt.Sprintf("uid=%d", hdr.Uid))}, nil
		}
		stat := info.Sys().(*syscall.Stat_t)
		return []KeyVal{KeyVal(fmt.Sprintf("uid=%d", stat.Uid))}, nil
	}
	gidKeywordFunc = func(path string, info os.FileInfo, r io.Reader) ([]KeyVal, error) {
		if hdr, ok := info.Sys().(*tar.Header); ok {
			return []KeyVal{KeyVal(fmt.Sprintf("gid=%d", hdr.Gid))}, nil
		}
		if stat, ok := info.Sys().(*syscall.Stat_t); ok {
			return []KeyVal{KeyVal(fmt.Sprintf("gid=%d", stat.Gid))}, nil
		}
		return nil, nil
	}
	nlinkKeywordFunc = func(path string, info os.FileInfo, r io.Reader) ([]KeyVal, error) {
		if stat, ok := info.Sys().(*syscall.Stat_t); ok {
			return []KeyVal{KeyVal(fmt.Sprintf("nlink=%d", stat.Nlink))}, nil
		}
		return nil, nil
	}
	xattrKeywordFunc = func(path string, info os.FileInfo, r io.Reader) ([]KeyVal, error) {
		if hdr, ok := info.Sys().(*tar.Header); ok {
			if len(hdr.PAXRecords) == 0 {
				return nil, nil
			}
			klist := []KeyVal{}
			for k, v := range hdr.PAXRecords {
				encKey, err := govis.Vis(k, DefaultVisFlags)
				if err != nil {
					return nil, nil
				}
				klist = append(klist, KeyVal(fmt.Sprintf("xattr.%s=%s", encKey, base64.StdEncoding.EncodeToString([]byte(v)))))
			}
			return klist, nil
		}
		if !info.Mode().IsRegular() && !info.Mode().IsDir() {
			return nil, nil
		}

		xlist, err := xattr.List(path)
		if err != nil {
			return nil, nil
		}
		klist := make([]KeyVal, len(xlist))
		for i := range xlist {
			data, err := xattr.Get(path, xlist[i])
			if err != nil {
				return nil, nil
			}
			encKey, err := govis.Vis(xlist[i], DefaultVisFlags)
			if err != nil {
				return nil, nil
			}
			klist[i] = KeyVal(fmt.Sprintf("xattr.%s=%s", encKey, base64.StdEncoding.EncodeToString(data)))
		}
		return klist, nil
	}
)