-
Notifications
You must be signed in to change notification settings - Fork 0
/
node.go
147 lines (128 loc) · 2.94 KB
/
node.go
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package xsum
import (
"encoding/hex"
"io"
"os"
"github.com/sclevine/xsum/encoding"
)
type File struct {
Hash Hash
Path string
Mask Mask
Stdin bool
}
func (f *File) sum() ([]byte, error) {
if f.Stdin {
return f.Hash.Data(io.NopCloser(os.Stdin))
}
return f.Hash.File(f.Path)
}
func (f *File) stat(subdir bool) (os.FileInfo, error) {
if f.Stdin {
return os.Stdin.Stat()
}
if !f.follow(subdir) {
return os.Lstat(f.Path)
}
return os.Stat(f.Path)
}
func (f *File) xattr(subdir bool) (*Xattr, error) {
hashes, err := getXattr(f.Path, f.Hash, f.follow(subdir))
if err != nil {
return nil, err
}
return &Xattr{
HashType: hashToEncoding(f.Hash.String()),
Hashes: hashes,
}, nil
}
func (f *File) follow(subdir bool) bool {
inclusive := f.Mask.Attr&AttrInclusive != 0
return f.Mask.Attr&AttrFollow != 0 || (!inclusive && !subdir)
}
type Node struct {
File
Sum []byte
Mode os.FileMode
Sys *Sys
Xattr *Xattr
Err error
}
type Sys struct {
UID, GID *uint32
Mtime, Ctime *encoding.Timespec
Rdev *uint64
}
type Xattr struct {
HashType encoding.HashType
Hashes []encoding.NamedHash
}
func (n *Node) String() string {
if n.Mode&os.ModeDir != 0 || n.Mask.Attr&AttrInclusive != 0 {
return n.Hash.String() + ":" + n.SumString() + ":" + n.Mask.String()
}
return n.Hash.String() + ":" + n.SumString()
}
func (n *Node) Hex() string {
if n.Mode&os.ModeDir != 0 || n.Mask.Attr&AttrInclusive != 0 {
return n.Hash.String() + ":" + n.SumString() + ":" + n.Mask.Hex()
}
return n.Hash.String() + ":" + n.SumString()
}
func (n *Node) SumString() string {
return hex.EncodeToString(n.Sum)
}
const (
sModeSetuid = 04000
sModeSetgid = 02000
sModeSticky = 01000
)
func hashFileAttr(n *Node) ([]byte, error) {
if n.Sys == nil && n.Mask.Attr&(AttrUID|AttrGID|AttrSpecial|AttrMtime|AttrCtime) != 0 {
return nil, ErrNoStat
}
if n.Xattr == nil && n.Mask.Attr&AttrX != 0 {
return nil, ErrNoXattr
}
var specialMask os.FileMode
if n.Mask.Mode&sModeSetuid != 0 {
specialMask |= os.ModeSetuid
}
if n.Mask.Mode&sModeSetgid != 0 {
specialMask |= os.ModeSetgid
}
if n.Mask.Mode&sModeSticky != 0 {
specialMask |= os.ModeSticky
}
permMask := os.FileMode(n.Mask.Mode) & os.ModePerm
modeMask := os.ModeType | permMask | specialMask
sys := &encoding.Sys{}
if n.Mask.Attr&AttrUID != 0 {
sys.UID = n.Sys.UID
}
if n.Mask.Attr&AttrGID != 0 {
sys.GID = n.Sys.GID
}
if n.Mask.Attr&AttrMtime != 0 {
sys.Mtime = n.Sys.Mtime
}
if n.Mask.Attr&AttrCtime != 0 {
sys.Ctime = n.Sys.Ctime
}
if n.Mask.Attr&AttrSpecial != 0 {
sys.Rdev = n.Sys.Rdev
}
if n.Mask.Attr&AttrX != 0 {
sys.XattrHashType = n.Xattr.HashType
sys.XattrHashes = n.Xattr.Hashes
}
hashType := encoding.HashNone
if len(n.Sum) != 0 { // check no-data attr or not?
hashType = hashToEncoding(n.Hash.String())
}
der, err := encoding.FileASN1DER(hashType, n.Sum, n.Mode, modeMask, sys)
if err != nil {
return nil, err
}
return n.Hash.Metadata(der)
}