/
xattrs.go
201 lines (170 loc) · 6.09 KB
/
xattrs.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
// Copyright 2018-2021 CERN
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// In applying this license, CERN does not waive the privileges and immunities
// granted to it by virtue of its status as an Intergovernmental Organization
// or submit itself to any jurisdiction.
package node
import (
"context"
"io"
"strconv"
"github.com/pkg/xattr"
)
// Attributes is a map of string keys and byte array values
type Attributes map[string][]byte
// String reads a String value
func (md Attributes) String(key string) string {
return string(md[key])
}
// SetString sets a string value
func (md Attributes) SetString(key, val string) {
md[key] = []byte(val)
}
// Int64 reads an int64 value
func (md Attributes) Int64(key string) (int64, error) {
return strconv.ParseInt(string(md[key]), 10, 64)
}
// SetInt64 sets an int64 value
func (md Attributes) SetInt64(key string, val int64) {
md[key] = []byte(strconv.FormatInt(val, 10))
}
// UInt64 reads an uint64 value
func (md Attributes) UInt64(key string) (uint64, error) {
return strconv.ParseUint(string(md[key]), 10, 64)
}
// SetInt64 sets an uint64 value
func (md Attributes) SetUInt64(key string, val uint64) {
md[key] = []byte(strconv.FormatUint(val, 10))
}
// SetXattrs sets multiple extended attributes on the write-through cache/node
func (n *Node) SetXattrsWithContext(ctx context.Context, attribs map[string][]byte, acquireLock bool) (err error) {
if n.xattrsCache != nil {
for k, v := range attribs {
n.xattrsCache[k] = v
}
}
return n.lu.MetadataBackend().SetMultiple(ctx, n.InternalPath(), attribs, acquireLock)
}
// SetXattrs sets multiple extended attributes on the write-through cache/node
func (n *Node) SetXattrs(attribs map[string][]byte, acquireLock bool) (err error) {
return n.SetXattrsWithContext(context.Background(), attribs, acquireLock)
}
// SetXattr sets an extended attribute on the write-through cache/node
func (n *Node) SetXattr(ctx context.Context, key string, val []byte) (err error) {
if n.xattrsCache != nil {
n.xattrsCache[key] = val
}
return n.lu.MetadataBackend().Set(ctx, n.InternalPath(), key, val)
}
// SetXattrString sets a string extended attribute on the write-through cache/node
func (n *Node) SetXattrString(ctx context.Context, key, val string) (err error) {
if n.xattrsCache != nil {
n.xattrsCache[key] = []byte(val)
}
return n.lu.MetadataBackend().Set(ctx, n.InternalPath(), key, []byte(val))
}
// RemoveXattr removes an extended attribute from the write-through cache/node
func (n *Node) RemoveXattr(ctx context.Context, key string, acquireLock bool) error {
if n.xattrsCache != nil {
delete(n.xattrsCache, key)
}
return n.lu.MetadataBackend().Remove(ctx, n.InternalPath(), key, acquireLock)
}
// XattrsWithReader returns the extended attributes of the node. If the attributes have already
// been cached they are not read from disk again.
func (n *Node) XattrsWithReader(ctx context.Context, r io.Reader) (Attributes, error) {
if n.ID == "" {
// Do not try to read the attribute of an empty node. The InternalPath points to the
// base nodes directory in this case.
return Attributes{}, &xattr.Error{Op: "node.XattrsWithReader", Path: n.InternalPath(), Err: xattr.ENOATTR}
}
if n.xattrsCache != nil {
return n.xattrsCache, nil
}
var attrs Attributes
var err error
if r != nil {
attrs, err = n.lu.MetadataBackend().AllWithLockedSource(ctx, n.InternalPath(), r)
} else {
attrs, err = n.lu.MetadataBackend().All(ctx, n.InternalPath())
}
if err != nil {
return nil, err
}
n.xattrsCache = attrs
return n.xattrsCache, nil
}
// Xattrs returns the extended attributes of the node. If the attributes have already
// been cached they are not read from disk again.
func (n *Node) Xattrs(ctx context.Context) (Attributes, error) {
return n.XattrsWithReader(ctx, nil)
}
// Xattr returns an extended attribute of the node. If the attributes have already
// been cached it is not read from disk again.
func (n *Node) Xattr(ctx context.Context, key string) ([]byte, error) {
if n.ID == "" {
// Do not try to read the attribute of an empty node. The InternalPath points to the
// base nodes directory in this case.
return []byte{}, &xattr.Error{Op: "node.Xattr", Path: n.InternalPath(), Name: key, Err: xattr.ENOATTR}
}
if n.xattrsCache == nil {
attrs, err := n.lu.MetadataBackend().All(ctx, n.InternalPath())
if err != nil {
return []byte{}, err
}
n.xattrsCache = attrs
}
if val, ok := n.xattrsCache[key]; ok {
return val, nil
}
// wrap the error as xattr does
return []byte{}, &xattr.Error{Op: "node.Xattr", Path: n.InternalPath(), Name: key, Err: xattr.ENOATTR}
}
// XattrString returns the string representation of an attribute
func (n *Node) XattrString(ctx context.Context, key string) (string, error) {
b, err := n.Xattr(ctx, key)
if err != nil {
return "", err
}
return string(b), nil
}
// XattrInt32 returns the int32 representation of an attribute
func (n *Node) XattrInt32(ctx context.Context, key string) (int32, error) {
b, err := n.XattrString(ctx, key)
if err != nil {
return 0, err
}
typeInt, err := strconv.ParseInt(b, 10, 32)
if err != nil {
return 0, err
}
return int32(typeInt), nil
}
// XattrInt64 returns the int64 representation of an attribute
func (n *Node) XattrInt64(ctx context.Context, key string) (int64, error) {
b, err := n.XattrString(ctx, key)
if err != nil {
return 0, err
}
return strconv.ParseInt(b, 10, 64)
}
// XattrUint64 returns the uint64 representation of an attribute
func (n *Node) XattrUint64(ctx context.Context, key string) (uint64, error) {
b, err := n.XattrString(ctx, key)
if err != nil {
return 0, err
}
return strconv.ParseUint(b, 10, 64)
}