-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmisc.go
262 lines (232 loc) · 5.76 KB
/
misc.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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
// SPDX-License-Identifier: 0BSD
package libstoragemgmt
import (
"encoding/json"
"fmt"
"os"
"path/filepath"
"time"
)
func getPluginIpcPath(pluginName string) string {
return fmt.Sprintf("%s/%s", udsPath(), pluginName)
}
func getPlugins(path string) []string {
var plugins []string
// If we are walking the path when the daemon is starting we can get errors, loop
// until we walk without errors or run out of time trying. It is possible that
// when we are walking the directory we only get a subset of the plugins that are
// present.
for i := 0; i < 10; i++ {
plugins = nil
err := filepath.Walk(path, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.IsDir() == false && (info.Mode()&os.ModeSocket != 0) {
plugins = append(plugins, path)
}
return nil
})
if err == nil {
break
} else {
time.Sleep(time.Millisecond * 200)
}
}
return plugins
}
func checkDaemonExists() bool {
var present = false
var udsPath = udsPath()
// The unix domain socket needs to exist
if _, err := os.Stat(udsPath); os.IsNotExist(err) {
return present
}
for _, pluginPath := range getPlugins(udsPath) {
var trans, err = newTransport(pluginPath, false)
if err == nil {
present = true
trans.close()
}
break
}
return present
}
// Callers should check for errors, then job id, then vol, but if they don't we want to make
// sure we we don't return something unexpected by mistake.
func ensureExclusiveVol(vol *Volume, job *string, err error) (*Volume, *string, error) {
if err != nil {
return nil, nil, err
} else if job != nil {
return nil, job, nil
}
return vol, nil, nil
}
// Callers should check for errors, then job id, then file system, but if they don't we want to make
// sure we we don't return something unexpected by mistake.
func ensureExclusiveFs(fs *FileSystem, job *string, err error) (*FileSystem, *string, error) {
if err != nil {
return nil, nil, err
} else if job != nil {
return nil, job, nil
}
return fs, nil, nil
}
// Callers should check for errors, then job id, then snapshot, but if they don't we want to make
// sure we we don't return something unexpected by mistake.
func ensureExclusiveSs(ss *FileSystemSnapShot, job *string, err error) (*FileSystemSnapShot, *string, error) {
if err != nil {
return nil, nil, err
} else if job != nil {
return nil, job, nil
}
return ss, nil, nil
}
// LsmBool is used to express booleans as we use 0 == false, 1 = true
// for the JSON RPC interface.
type LsmBool bool
// UnmarshalJSON used for custom JSON serialization
func (bit LsmBool) UnmarshalJSON(b []byte) error {
bit = LsmBool(string(b) == "1")
return nil
}
// MarshalJSON used to custom JSON serialization
func (bit LsmBool) MarshalJSON() ([]byte, error) {
if bit {
return []byte("1"), nil
}
return []byte("0"), nil
}
// MarshalJSON custom marshal for BlockRange
// ref. http://choly.ca/post/go-json-marshalling/
func (b *BlockRange) MarshalJSON() ([]byte, error) {
type Alias BlockRange
return json.Marshal(&struct {
Class string `json:"class"`
*Alias
}{
Class: "BlockRange",
Alias: (*Alias)(b),
})
}
// MarshalJSON used to custom JSON serialization
func (s *System) MarshalJSON() ([]byte, error) {
type Alias System
return json.Marshal(&struct {
Class string `json:"class"`
*Alias
}{
Class: "System",
Alias: (*Alias)(s),
})
}
// MarshalJSON used to custom JSON serialization
func (v *Volume) MarshalJSON() ([]byte, error) {
type Alias Volume
return json.Marshal(&struct {
Class string `json:"class"`
*Alias
}{
Class: "Volume",
Alias: (*Alias)(v),
})
}
// MarshalJSON used to custom JSON serialization
func (p *Pool) MarshalJSON() ([]byte, error) {
type Alias Pool
return json.Marshal(&struct {
Class string `json:"class"`
*Alias
}{
Class: "Pool",
Alias: (*Alias)(p),
})
}
// MarshalJSON used to custom JSON serialization
func (d *Disk) MarshalJSON() ([]byte, error) {
type Alias Disk
return json.Marshal(&struct {
Class string `json:"class"`
*Alias
}{
Class: "Disk",
Alias: (*Alias)(d),
})
}
// MarshalJSON used to custom JSON serialization
func (f *FileSystem) MarshalJSON() ([]byte, error) {
type Alias FileSystem
return json.Marshal(&struct {
Class string `json:"class"`
*Alias
}{
Class: "FileSystem",
Alias: (*Alias)(f),
})
}
// MarshalJSON used to custom JSON serialization
func (n *NfsExport) MarshalJSON() ([]byte, error) {
type Alias NfsExport
return json.Marshal(&struct {
Class string `json:"class"`
*Alias
}{
Class: "NfsExport",
Alias: (*Alias)(n),
})
}
// MarshalJSON used to custom JSON serialization
func (ag *AccessGroup) MarshalJSON() ([]byte, error) {
type Alias AccessGroup
return json.Marshal(&struct {
Class string `json:"class"`
*Alias
}{
Class: "AccessGroup",
Alias: (*Alias)(ag),
})
}
// MarshalJSON used to custom JSON serialization
func (tp *TargetPort) MarshalJSON() ([]byte, error) {
type Alias TargetPort
return json.Marshal(&struct {
Class string `json:"class"`
*Alias
}{
Class: "TargetPort",
Alias: (*Alias)(tp),
})
}
// MarshalJSON used to custom JSON serialization
func (tp *Battery) MarshalJSON() ([]byte, error) {
type Alias Battery
return json.Marshal(&struct {
Class string `json:"class"`
*Alias
}{
Class: "Battery",
Alias: (*Alias)(tp),
})
}
// MarshalJSON used to custom JSON serialization
func (c *Capabilities) MarshalJSON() ([]byte, error) {
type Alias Capabilities
return json.Marshal(&struct {
Class string `json:"class"`
*Alias
}{
Class: "Capabilities",
Alias: (*Alias)(c),
})
}
// MarshalJSON used to custom JSON serialization
func (f *FileSystemSnapShot) MarshalJSON() ([]byte, error) {
type Alias FileSystemSnapShot
return json.Marshal(&struct {
Class string `json:"class"`
*Alias
}{
Class: "FsSnapshot",
Alias: (*Alias)(f),
})
}