8
8
"os"
9
9
"os/exec"
10
10
"strings"
11
- "time"
12
11
)
13
12
14
13
// SmbStatusServerID represents a server_id output field
@@ -77,8 +76,8 @@ type SmbStatusLockedFile struct {
77
76
NumPendingDeletes int `json:"num_pending_deletes"`
78
77
}
79
78
80
- // SmbStatusJSON represents output of 'smbstatus --json'
81
- type SmbStatusJSON struct {
79
+ // SmbStatus represents output of 'smbstatus --json'
80
+ type SmbStatus struct {
82
81
Timestamp string `json:"timestamp"`
83
82
Version string `json:"version"`
84
83
SmbConf string `json:"smb_conf"`
@@ -87,39 +86,15 @@ type SmbStatusJSON struct {
87
86
LockedFiles map [string ]SmbStatusLockedFile `json:"locked_files"`
88
87
}
89
88
90
- // SmbStatusProc represents a single entry from the output of 'smbstatus -p'
91
- type SmbStatusProc struct {
92
- PID string
93
- Username string
94
- Group string
95
- Machine string
96
- ProtocolVersion string
97
- Encryption string
98
- Signing string
99
- }
100
-
101
- // SmbStatusLock represents a single entry from the output of 'smbstatus -L'
102
- type SmbStatusLock struct {
103
- PID string
104
- UserID string
105
- DenyMode string
106
- Access string
107
- RW string
108
- Oplock string
109
- SharePath string
110
- Name string
111
- Time string
112
- }
113
-
114
- // SmbStatusJSON represents output of 'smbstatus -L --json'
115
- type SmbStatusLocksJSON struct {
89
+ // SmbStatusLocks represents output of 'smbstatus -L --json'
90
+ type SmbStatusLocks struct {
116
91
Timestamp string `json:"timestamp"`
117
92
Version string `json:"version"`
118
93
SmbConf string `json:"smb_conf"`
119
94
OpenFiles map [string ]SmbStatusLockedFile `json:"open_files"`
120
95
}
121
96
122
- // LocateSmbStatus finds the local executable of 'smbstatus' on host container .
97
+ // LocateSmbStatus finds the local executable of 'smbstatus' on host.
123
98
func LocateSmbStatus () (string , error ) {
124
99
knowns := []string {
125
100
"/usr/bin/smbstatus" ,
@@ -149,18 +124,18 @@ func RunSmbStatusVersion() (string, error) {
149
124
return ver , nil
150
125
}
151
126
152
- // RunSmbStatusShares executes 'smbstatus -S --json' on host container
127
+ // RunSmbStatusShares executes 'smbstatus --shares --json' on host
153
128
func RunSmbStatusShares () ([]SmbStatusTreeCon , error ) {
154
- dat , err := executeSmbStatusCommand ("-S --json" )
129
+ dat , err := executeSmbStatusCommand ("--shares --json" )
155
130
if err != nil {
156
131
return []SmbStatusTreeCon {}, err
157
132
}
158
- return parseSmbStatusSharesAsJSON (dat )
133
+ return parseSmbStatusTreeCons (dat )
159
134
}
160
135
161
- func parseSmbStatusSharesAsJSON (dat string ) ([]SmbStatusTreeCon , error ) {
136
+ func parseSmbStatusTreeCons (dat string ) ([]SmbStatusTreeCon , error ) {
162
137
tcons := []SmbStatusTreeCon {}
163
- res , err := parseSmbStatusJSON (dat )
138
+ res , err := parseSmbStatus (dat )
164
139
if err != nil {
165
140
return tcons , err
166
141
}
@@ -170,18 +145,18 @@ func parseSmbStatusSharesAsJSON(dat string) ([]SmbStatusTreeCon, error) {
170
145
return tcons , nil
171
146
}
172
147
173
- // RunSmbStatusLocks executes 'smbstatus -L --json' on host container
174
- func RunSmbStatusLockedFiles () ([]SmbStatusLockedFile , error ) {
175
- dat , err := executeSmbStatusCommand ("-L --json" )
148
+ // RunSmbStatusLocks executes 'smbstatus --locks --json' on host
149
+ func RunSmbStatusLocks () ([]SmbStatusLockedFile , error ) {
150
+ dat , err := executeSmbStatusCommand ("--locks --json" )
176
151
if err != nil {
177
152
return []SmbStatusLockedFile {}, err
178
153
}
179
- return parseSmbStatusLocksAsJSON (dat )
154
+ return parseSmbStatusLockedFiles (dat )
180
155
}
181
156
182
- func parseSmbStatusLocksAsJSON (dat string ) ([]SmbStatusLockedFile , error ) {
157
+ func parseSmbStatusLockedFiles (dat string ) ([]SmbStatusLockedFile , error ) {
183
158
lockedFiles := []SmbStatusLockedFile {}
184
- res , err := parseSmbStatusLocksJSON (dat )
159
+ res , err := parseSmbStatusLocks (dat )
185
160
if err != nil {
186
161
return lockedFiles , err
187
162
}
@@ -194,16 +169,16 @@ func parseSmbStatusLocksAsJSON(dat string) ([]SmbStatusLockedFile, error) {
194
169
// SmbStatusSharesByMachine converts the output of RunSmbStatusShares into map
195
170
// indexed by machine's host
196
171
func SmbStatusSharesByMachine () (map [string ][]SmbStatusTreeCon , error ) {
197
- shares , err := RunSmbStatusShares ()
172
+ tcons , err := RunSmbStatusShares ()
198
173
if err != nil {
199
174
return map [string ][]SmbStatusTreeCon {}, err
200
175
}
201
- return makeSmbSharesMap (shares ), nil
176
+ return makeSmbSharesMap (tcons ), nil
202
177
}
203
178
204
- func makeSmbSharesMap (shares []SmbStatusTreeCon ) map [string ][]SmbStatusTreeCon {
179
+ func makeSmbSharesMap (tcons []SmbStatusTreeCon ) map [string ][]SmbStatusTreeCon {
205
180
ret := map [string ][]SmbStatusTreeCon {}
206
- for _ , share := range shares {
181
+ for _ , share := range tcons {
207
182
ret [share .Machine ] = append (ret [share .Machine ], share )
208
183
}
209
184
return ret
@@ -227,33 +202,18 @@ func executeCommand(command string, arg ...string) (string, error) {
227
202
return res , nil
228
203
}
229
204
230
- func ParseTime (s string ) (time.Time , error ) {
231
- layouts := []string {
232
- time .ANSIC ,
233
- time .UnixDate ,
234
- time .RFC3339 ,
235
- }
236
- for _ , layout := range layouts {
237
- if t , err := time .Parse (layout , s ); err == nil {
238
- return t , nil
239
- }
240
- }
241
- // samba's lib/util/time.c uses non standad layout...
242
- return time.Time {}, errors .New ("unknow time format " + s )
243
- }
244
-
245
- // parseSmbStatusJSON parses to output of 'smbstatus --json' into internal
205
+ // parseSmbStatus parses to output of 'smbstatus --json' into internal
246
206
// representation.
247
- func parseSmbStatusJSON (data string ) (* SmbStatusJSON , error ) {
248
- res := SmbStatusJSON {}
207
+ func parseSmbStatus (data string ) (* SmbStatus , error ) {
208
+ res := SmbStatus {}
249
209
err := json .Unmarshal ([]byte (data ), & res )
250
210
return & res , err
251
211
}
252
212
253
- // parseSmbStatusJSON parses to output of 'smbstatus -L --json' into internal
254
- // representation.
255
- func parseSmbStatusLocksJSON (data string ) (* SmbStatusLocksJSON , error ) {
256
- res := SmbStatusLocksJSON {}
213
+ // parseSmbStatusLocks parses to output of 'smbstatus --locks --json' into
214
+ // internal representation.
215
+ func parseSmbStatusLocks (data string ) (* SmbStatusLocks , error ) {
216
+ res := SmbStatusLocks {}
257
217
err := json .Unmarshal ([]byte (data ), & res )
258
218
return & res , err
259
219
}
0 commit comments