@@ -149,19 +149,13 @@ func RunSmbStatusVersion() (string, error) {
149
149
return ver , nil
150
150
}
151
151
152
- // RunSmbStatusShares executes 'smbstatus -S' on host container
152
+ // RunSmbStatusShares executes 'smbstatus -S --json ' on host container
153
153
func RunSmbStatusShares () ([]SmbStatusTreeCon , error ) {
154
- // Case 1: using new json output
155
154
dat , err := executeSmbStatusCommand ("-S --json" )
156
- if err == nil {
157
- return parseSmbStatusSharesAsJSON (dat )
158
- }
159
- // Case 2: fallback to raw-text output
160
- dat , err = executeSmbStatusCommand ("-S" )
161
- if err == nil {
162
- return parseSmbStatusShares (dat )
155
+ if err != nil {
156
+ return []SmbStatusTreeCon {}, err
163
157
}
164
- return [] SmbStatusTreeCon {}, err
158
+ return parseSmbStatusSharesAsJSON ( dat )
165
159
}
166
160
167
161
func parseSmbStatusSharesAsJSON (dat string ) ([]SmbStatusTreeCon , error ) {
@@ -176,15 +170,6 @@ func parseSmbStatusSharesAsJSON(dat string) ([]SmbStatusTreeCon, error) {
176
170
return tcons , nil
177
171
}
178
172
179
- // RunSmbStatusLocks executes 'smbstatus -L' on host container
180
- func RunSmbStatusLocks () ([]SmbStatusLock , error ) {
181
- dat , err := executeSmbStatusCommand ("-L" )
182
- if err != nil {
183
- return []SmbStatusLock {}, err
184
- }
185
- return parseSmbStatusLocks (dat )
186
- }
187
-
188
173
// RunSmbStatusLocks executes 'smbstatus -L --json' on host container
189
174
func RunSmbStatusLockedFiles () ([]SmbStatusLockedFile , error ) {
190
175
dat , err := executeSmbStatusCommand ("-L --json" )
@@ -206,15 +191,6 @@ func parseSmbStatusLocksAsJSON(dat string) ([]SmbStatusLockedFile, error) {
206
191
return lockedFiles , nil
207
192
}
208
193
209
- // RunSmbStatusProcs executes 'smbstatus -p' on host container
210
- func RunSmbStatusProcs () ([]SmbStatusProc , error ) {
211
- dat , err := executeSmbStatusCommand ("-p" )
212
- if err != nil {
213
- return []SmbStatusProc {}, err
214
- }
215
- return parseSmbStatusProcs (dat )
216
- }
217
-
218
194
// SmbStatusSharesByMachine converts the output of RunSmbStatusShares into map
219
195
// indexed by machine's host
220
196
func SmbStatusSharesByMachine () (map [string ][]SmbStatusTreeCon , error ) {
@@ -251,185 +227,6 @@ func executeCommand(command string, arg ...string) (string, error) {
251
227
return res , nil
252
228
}
253
229
254
- // parseSmbStatusShares parses to output of 'smbstatus -S' into internal
255
- // representation.
256
- func parseSmbStatusShares (data string ) ([]SmbStatusTreeCon , error ) {
257
- shares := []SmbStatusTreeCon {}
258
- serviceIndex := 0
259
- pidIndex := 0
260
- machineIndex := 0
261
- connectedAtIndex := 0
262
- encryptionIndex := 0
263
- signingIndex := 0
264
- hasDashLine := false
265
- lines := strings .Split (data , "\n " )
266
- for _ , line := range lines {
267
- ln := strings .TrimSpace (line )
268
- // Ignore empty and coment lines
269
- if len (ln ) == 0 || ln [0 ] == '#' {
270
- continue
271
- }
272
- // Detect the all-dash line
273
- if strings .HasPrefix (ln , "------" ) {
274
- hasDashLine = true
275
- continue
276
- }
277
- // Parse header line into index of data
278
- if strings .HasPrefix (ln , "Service" ) {
279
- serviceIndex = strings .Index (ln , "Service" )
280
- pidIndex = strings .Index (ln , "pid" )
281
- machineIndex = strings .Index (ln , "Machine" )
282
- connectedAtIndex = strings .Index (ln , "Connected at" )
283
- encryptionIndex = strings .Index (ln , "Encryption" )
284
- signingIndex = strings .Index (ln , "Signing" )
285
- continue
286
- }
287
- // Ignore lines before header
288
- if ! hasDashLine {
289
- continue
290
- }
291
- // Parse data into internal repr
292
- share := SmbStatusTreeCon {}
293
- share .Service = parseSubstr (ln , serviceIndex )
294
- share .ServerID .PID = parseSubstr (ln , pidIndex )
295
- share .Machine = parseSubstr (ln , machineIndex )
296
- share .ConnectedAt = parseSubstr2 (ln , connectedAtIndex , encryptionIndex )
297
- share .Encryption .Cipher = parseSubstr (ln , encryptionIndex )
298
- share .Signing .Cipher = parseSubstr (ln , signingIndex )
299
-
300
- // Ignore "IPC$"
301
- if share .Service == "IPC$" {
302
- continue
303
- }
304
-
305
- shares = append (shares , share )
306
- }
307
- return shares , nil
308
- }
309
-
310
- // parseSmbStatusProcs parses to output of 'smbstatus -p' into internal
311
- // representation.
312
- func parseSmbStatusProcs (data string ) ([]SmbStatusProc , error ) {
313
- procs := []SmbStatusProc {}
314
- pidIndex := 0
315
- usernameIndex := 0
316
- groupIndex := 0
317
- machineIndex := 0
318
- protocolVersionIndex := 0
319
- encryptionIndex := 0
320
- signingIndex := 0
321
- hasDashLine := false
322
- lines := strings .Split (data , "\n " )
323
- for _ , line := range lines {
324
- ln := strings .TrimSpace (line )
325
- // Ignore empty and coment lines
326
- if len (ln ) == 0 || ln [0 ] == '#' {
327
- continue
328
- }
329
- // Detect the all-dash line
330
- if strings .HasPrefix (ln , "------" ) {
331
- hasDashLine = true
332
- continue
333
- }
334
- // Parse header line into index of data
335
- if strings .HasPrefix (ln , "PID" ) {
336
- pidIndex = strings .Index (ln , "PID" )
337
- usernameIndex = strings .Index (ln , "Username" )
338
- groupIndex = strings .Index (ln , "Group" )
339
- machineIndex = strings .Index (ln , "Machine" )
340
- protocolVersionIndex = strings .Index (ln , "Protocol Version" )
341
- encryptionIndex = strings .Index (ln , "Encryption" )
342
- signingIndex = strings .Index (ln , "Signing" )
343
- continue
344
- }
345
- // Ignore lines before header
346
- if ! hasDashLine {
347
- continue
348
- }
349
- // Parse data into internal repr
350
- proc := SmbStatusProc {}
351
- proc .PID = parseSubstr (ln , pidIndex )
352
- proc .Username = parseSubstr (ln , usernameIndex )
353
- proc .Group = parseSubstr (ln , groupIndex )
354
- proc .Machine = parseSubstr (ln , machineIndex )
355
- proc .ProtocolVersion = parseSubstr (ln , protocolVersionIndex )
356
- proc .Encryption = parseSubstr (ln , encryptionIndex )
357
- proc .Signing = parseSubstr (ln , signingIndex )
358
- procs = append (procs , proc )
359
- }
360
- return procs , nil
361
- }
362
-
363
- // parseSmbStatusLocks parses to output of 'smbstatus -L' into internal
364
- // representation.
365
- func parseSmbStatusLocks (data string ) ([]SmbStatusLock , error ) {
366
- locks := []SmbStatusLock {}
367
- pidIndex := 0
368
- userIndex := 0
369
- denyModeIndex := 0
370
- accessIndex := 0
371
- rwIndex := 0
372
- oplockIndex := 0
373
- sharePathIndex := 0
374
- hasDashLine := false
375
- lines := strings .Split (data , "\n " )
376
- for _ , line := range lines {
377
- ln := strings .TrimSpace (line )
378
- // Ignore empty and coment lines
379
- if len (ln ) == 0 || ln [0 ] == '#' {
380
- continue
381
- }
382
- // Detect the all-dash line
383
- if strings .HasPrefix (ln , "------" ) {
384
- hasDashLine = true
385
- continue
386
- }
387
- // Ignore generic-info line
388
- if strings .HasPrefix (ln , "Locked files" ) {
389
- continue
390
- }
391
- // Parse header line into index of data
392
- if strings .HasPrefix (ln , "Pid" ) {
393
- pidIndex = strings .Index (ln , "Pid" )
394
- userIndex = strings .Index (ln , "User" )
395
- denyModeIndex = strings .Index (ln , "DenyMode" )
396
- accessIndex = strings .Index (ln , "Access" )
397
- rwIndex = strings .Index (ln , "R/W" )
398
- oplockIndex = strings .Index (ln , "Oplock" )
399
- sharePathIndex = strings .Index (ln , "SharePath" )
400
- continue
401
- }
402
- // Ignore lines before header
403
- if ! hasDashLine {
404
- continue
405
- }
406
- // Parse data into internal repr
407
- lock := SmbStatusLock {}
408
- lock .PID = parseSubstr (ln , pidIndex )
409
- lock .UserID = parseSubstr (ln , userIndex )
410
- lock .DenyMode = parseSubstr (ln , denyModeIndex )
411
- lock .Access = parseSubstr (ln , accessIndex )
412
- lock .RW = parseSubstr (ln , rwIndex )
413
- lock .Oplock = parseSubstr (ln , oplockIndex )
414
- lock .SharePath = parseSubstr (ln , sharePathIndex )
415
- locks = append (locks , lock )
416
- }
417
- return locks , nil
418
- }
419
-
420
- func parseSubstr (s string , startIndex int ) string {
421
- sub := strings .TrimSpace (s [startIndex :])
422
- fields := strings .Fields (sub )
423
- if len (fields ) == 0 {
424
- return ""
425
- }
426
- return fields [0 ]
427
- }
428
-
429
- func parseSubstr2 (s string , startIndex , endIndex int ) string {
430
- return strings .TrimSpace (s [startIndex :endIndex ])
431
- }
432
-
433
230
func ParseTime (s string ) (time.Time , error ) {
434
231
layouts := []string {
435
232
time .ANSIC ,
0 commit comments