forked from whamcloud/go-lustre
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fid.go
59 lines (50 loc) · 1.47 KB
/
fid.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
// Copyright (c) 2016 Intel Corporation. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package lustre
import "fmt"
// Fid is a pure Go representation of a Lustre file identifier. It is
// intended to be the only representation of a Lustre Fid outside of
// llapi.
type Fid struct {
Seq uint64
Oid uint32
Ver uint32
}
func (f *Fid) String() string {
return fmt.Sprintf("[0x%x:0x%x:0x%x]", f.Seq, f.Oid, f.Ver)
}
// IsZero is true if Fid is 0.
func (f *Fid) IsZero() bool {
return f.Seq == 0 && f.Oid == 0 && f.Ver == 0
}
// IsDotLustre is true if Fid is special .lustre entry.
func (f *Fid) IsDotLustre() bool {
return f.Seq == 0x200000002 && f.Oid == 0x1 && f.Ver == 0x0
}
// MarshalJSON converts a Fid to a string for JSON.
func (f *Fid) MarshalJSON() ([]byte, error) {
return []byte(`"` + f.String() + `"`), nil
}
// UnmarshalJSON converts fid string to Fid.
func (f *Fid) UnmarshalJSON(b []byte) (err error) {
// trim the '"'
if b[0] == '"' {
b = b[1 : len(b)-1]
}
newFid, err := ParseFid(string(b))
*f = *newFid
return err
}
// ParseFid converts a fid in string format to a Fid
func ParseFid(fidstr string) (*Fid, error) {
fid := &Fid{}
if fidstr[0] == '[' {
fidstr = fidstr[1 : len(fidstr)-1]
}
n, err := fmt.Sscanf(fidstr, "0x%x:0x%x:0x%x", &fid.Seq, &fid.Oid, &fid.Ver)
if err != nil || n != 3 {
return nil, fmt.Errorf("lustre: unable to parse fid string: %v", fidstr)
}
return fid, nil
}