@@ -285,81 +285,101 @@ func PartitionsWithContext(ctx context.Context, all bool) ([]PartitionStat, erro
285
285
return nil , err
286
286
}
287
287
288
- ret := make ([]PartitionStat , 0 , len (lines ))
288
+ var ret []PartitionStat
289
+ if useMounts { // use mounts file
290
+ ret = parseFieldsOnMounts (lines , all , fs )
291
+ return ret , nil
292
+ }
293
+
294
+ // use mountinfo
295
+ ret , err = parseFieldsOnMountinfo (ctx , lines , all , fs , filename )
296
+ if err != nil {
297
+ return nil , fmt .Errorf ("error parsing mountinfo file %s: %w" , filename , err )
298
+ }
299
+
300
+ return ret , nil
301
+ }
289
302
303
+ func parseFieldsOnMounts (lines []string , all bool , fs []string ) []PartitionStat {
304
+ ret := make ([]PartitionStat , 0 , len (lines ))
290
305
for _ , line := range lines {
291
- var d PartitionStat
292
- if useMounts {
293
- fields := strings .Fields (line )
294
-
295
- d = PartitionStat {
296
- Device : fields [0 ],
297
- Mountpoint : unescapeFstab (fields [1 ]),
298
- Fstype : fields [2 ],
299
- Opts : strings .Fields (fields [3 ]),
300
- }
306
+ fields := strings .Fields (line )
301
307
302
- if ! all {
303
- if d .Device == "none" || ! common .StringsHas (fs , d .Fstype ) {
304
- continue
305
- }
306
- }
307
- } else {
308
- // a line of 1/mountinfo has the following structure:
309
- // 36 35 98:0 /mnt1 /mnt2 rw,noatime master:1 - ext3 /dev/root rw,errors=continue
310
- // (1) (2) (3) (4) (5) (6) (7) (8) (9) (10) (11)
311
-
312
- // split the mountinfo line by the separator hyphen
313
- parts := strings .Split (line , " - " )
314
- if len (parts ) != 2 {
315
- return nil , fmt .Errorf ("found invalid mountinfo line in file %s: %s " , filename , line )
308
+ d := PartitionStat {
309
+ Device : fields [0 ],
310
+ Mountpoint : unescapeFstab (fields [1 ]),
311
+ Fstype : fields [2 ],
312
+ Opts : strings .Split (fields [3 ], "," ),
313
+ }
314
+
315
+ if ! all {
316
+ if d .Device == "none" || ! common .StringsHas (fs , d .Fstype ) {
317
+ continue
316
318
}
319
+ }
320
+ ret = append (ret , d )
321
+ }
317
322
318
- fields := strings .Fields (parts [0 ])
319
- blockDeviceID := fields [2 ]
320
- mountPoint := fields [4 ]
321
- mountOpts := strings .Split (fields [5 ], "," )
323
+ return ret
324
+ }
322
325
323
- if rootDir := fields [3 ]; rootDir != "" && rootDir != "/" {
324
- mountOpts = append (mountOpts , "bind" )
325
- }
326
+ func parseFieldsOnMountinfo (ctx context.Context , lines []string , all bool , fs []string , filename string ) ([]PartitionStat , error ) {
327
+ ret := make ([]PartitionStat , 0 , len (lines ))
326
328
327
- fields = strings .Fields (parts [1 ])
328
- fstype := fields [0 ]
329
- device := fields [1 ]
329
+ for _ , line := range lines {
330
+ // a line of 1/mountinfo has the following structure:
331
+ // 36 35 98:0 /mnt1 /mnt2 rw,noatime master:1 - ext3 /dev/root rw,errors=continue
332
+ // (1) (2) (3) (4) (5) (6) (7) (8) (9) (10) (11)
333
+
334
+ // split the mountinfo line by the separator hyphen
335
+ parts := strings .Split (line , " - " )
336
+ if len (parts ) != 2 {
337
+ return nil , fmt .Errorf ("found invalid mountinfo line in file %s: %s " , filename , line )
338
+ }
330
339
331
- d = PartitionStat {
332
- Device : device ,
333
- Mountpoint : unescapeFstab (mountPoint ),
334
- Fstype : fstype ,
335
- Opts : mountOpts ,
336
- }
340
+ fields := strings .Fields (parts [0 ])
341
+ blockDeviceID := fields [2 ]
342
+ mountPoint := fields [4 ]
343
+ mountOpts := strings .Split (fields [5 ], "," )
337
344
338
- if ! all {
339
- if d .Device == "none" || ! common .StringsHas (fs , d .Fstype ) {
340
- continue
341
- }
345
+ if rootDir := fields [3 ]; rootDir != "" && rootDir != "/" {
346
+ mountOpts = append (mountOpts , "bind" )
347
+ }
348
+
349
+ fields = strings .Fields (parts [1 ])
350
+ fstype := fields [0 ]
351
+ device := fields [1 ]
352
+
353
+ d := PartitionStat {
354
+ Device : device ,
355
+ Mountpoint : unescapeFstab (mountPoint ),
356
+ Fstype : fstype ,
357
+ Opts : mountOpts ,
358
+ }
359
+
360
+ if ! all {
361
+ if d .Device == "none" || ! common .StringsHas (fs , d .Fstype ) {
362
+ continue
342
363
}
364
+ }
343
365
344
- if strings .HasPrefix (d .Device , "/dev/mapper/" ) {
345
- devpath , err := filepath .EvalSymlinks (common .HostDevWithContext (ctx , strings .Replace (d .Device , "/dev" , "" , 1 )))
346
- if err == nil {
347
- d .Device = devpath
348
- }
366
+ if strings .HasPrefix (d .Device , "/dev/mapper/" ) {
367
+ devpath , err := filepath .EvalSymlinks (common .HostDevWithContext (ctx , strings .Replace (d .Device , "/dev" , "" , 1 )))
368
+ if err == nil {
369
+ d .Device = devpath
349
370
}
371
+ }
350
372
351
- // /dev/root is not the real device name
352
- // so we get the real device name from its major/minor number
353
- if d .Device == "/dev/root" {
354
- devpath , err := os .Readlink (common .HostSysWithContext (ctx , "/dev/block/" + blockDeviceID ))
355
- if err == nil {
356
- d .Device = strings .Replace (d .Device , "root" , filepath .Base (devpath ), 1 )
357
- }
373
+ // /dev/root is not the real device name
374
+ // so we get the real device name from its major/minor number
375
+ if d .Device == "/dev/root" {
376
+ devpath , err := os .Readlink (common .HostSysWithContext (ctx , "/dev/block/" + blockDeviceID ))
377
+ if err == nil {
378
+ d .Device = strings .Replace (d .Device , "root" , filepath .Base (devpath ), 1 )
358
379
}
359
380
}
360
381
ret = append (ret , d )
361
382
}
362
-
363
383
return ret , nil
364
384
}
365
385
0 commit comments