forked from bicomsystems/go-libzfs
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtypes.go
executable file
·171 lines (153 loc) · 3.63 KB
/
types.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
package zfs
// #include <stdlib.h>
// #include <libzfs.h>
// #include "common.h"
// #include "zpool.h"
// #include "zfs.h"
import "C"
import (
"encoding/json"
"fmt"
"strconv"
"time"
"errors"
)
var stringToDatasetPropDic = make(map[string]DatasetProp)
var stringToPoolPropDic = make(map[string]PoolProp)
var zfsMaxDatasetProp DatasetProp
var zfsMaxPoolProp PoolProp
func init() {
if C.ZFS_NUM_PROPS > DatasetNumProps {
zfsMaxDatasetProp = DatasetNumProps
} else {
zfsMaxDatasetProp = DatasetProp(C.ZFS_NUM_PROPS)
}
if C.ZPOOL_NUM_PROPS > PoolNumProps {
zfsMaxPoolProp = PoolNumProps
} else {
zfsMaxPoolProp = PoolProp(C.ZPOOL_NUM_PROPS)
}
for i := DatasetPropType; i < zfsMaxDatasetProp; i++ {
stringToDatasetPropDic[i.String()] = i
}
for i := PoolPropName; i < zfsMaxPoolProp; i++ {
stringToPoolPropDic[i.String()] = i
}
}
func (p DatasetProp) String() string {
return C.GoString(C.zfs_prop_to_name((C.zfs_prop_t)(p)))
}
func (p DatasetProp) MarshalJSON() ([]byte, error) {
s := p.String()
return json.Marshal(s)
}
func (p *DatasetProp) UnmarshalJSON(b []byte) error {
var j string
err := json.Unmarshal(b, &j)
if err != nil {
return err
}
prop, ok := stringToDatasetPropDic[j]
if !ok {
return fmt.Errorf("prop \"%s\" not exists", j)
}
*p = prop
return err
}
func (p PoolProp) String() string {
return C.GoString(C.zpool_prop_to_name((C.zpool_prop_t)(p)))
}
func (p PoolProp) MarshalJSON() ([]byte, error) {
s := p.String()
return json.Marshal(s)
}
func (p *PoolProp) UnmarshalJSON(b []byte) error {
var j string
err := json.Unmarshal(b, &j)
if err != nil {
return err
}
prop, ok := stringToPoolPropDic[j]
if !ok {
return fmt.Errorf("prop \"%s\" not exists", j)
}
*p = prop
return err
}
//{"guid": {"value":"16859519823695578253", "source":"-"}}
func (p DatasetProperties) MarshalJSON() ([]byte, error) {
props := make(map[string]PropertyValue)
maxUint64 := strconv.FormatUint(C.UINT64_MAX, 10)
for prop, value := range p {
name := prop.String()
if maxUint64 != value.Value && value.Value != "none" {
if prop == DatasetPropCreation {
time_int, _ := strconv.ParseInt(value.Value, 10, 64)
value.Value = time.Unix(time_int, 0).Format("2006-01-02T15:04:05-0700")
}
props[name] = value
}
}
return json.Marshal(props)
}
func (p *DatasetProperties) UnmarshalJSON(b []byte) error {
if p == nil {
return errors.New("map is nil. use make")
}
props := make(map[string]PropertyValue)
err := json.Unmarshal(b, &props)
if err != nil {
return err
}
for key, value := range props {
prop, ok := stringToDatasetPropDic[key]
if !ok {
return fmt.Errorf("property \"%s\" not exist", key)
}
(*p)[prop] = value
}
return nil
}
func (p DatasetProperties) String() string {
data, err := json.Marshal(p)
if err != nil {
return ""
}
return string(data)
}
func (p PoolProperties) MarshalJSON() ([]byte, error) {
props := make(map[string]PropertyValue)
maxUint64 := strconv.FormatUint(C.UINT64_MAX, 10)
for prop, value := range p {
name := prop.String()
if maxUint64 != value.Value && value.Value != "none" {
props[name] = value
}
}
return json.Marshal(props)
}
func (p *PoolProperties) UnmarshalJSON(b []byte) error {
if p == nil {
return errors.New("map is nil. use make")
}
props := make(map[string]PropertyValue)
err := json.Unmarshal(b, &props)
if err != nil {
return err
}
for key, value := range props {
prop, ok := stringToPoolPropDic[key]
if !ok {
return fmt.Errorf("property \"%s\" not exist", key)
}
(*p)[prop] = value
}
return nil
}
func (p PoolProperties) String() string {
data, err := json.Marshal(p)
if err != nil {
return ""
}
return string(data)
}