-
Notifications
You must be signed in to change notification settings - Fork 149
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
show info about all mounted disks when no path is given
- Loading branch information
Daniel Milde
committed
Dec 27, 2020
1 parent
d6112a6
commit 764a662
Showing
8 changed files
with
202 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package main | ||
|
||
// Device struct | ||
type Device struct { | ||
name string | ||
mountPoint string | ||
size int64 | ||
free int64 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
// +build linux,amd64 | ||
|
||
package main | ||
|
||
import ( | ||
"bufio" | ||
"log" | ||
"os" | ||
"runtime" | ||
"strings" | ||
"syscall" | ||
) | ||
|
||
// GetDevicesInfo returns usage info about mounted devices (by calling Statfs syscall) | ||
func GetDevicesInfo() []*Device { | ||
if runtime.GOOS != "linux" { | ||
panic("Listing devices is not yet supported on " + runtime.GOOS) | ||
} | ||
|
||
devices := []*Device{} | ||
|
||
file, err := os.Open("/proc/mounts") | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
defer file.Close() | ||
|
||
scanner := bufio.NewScanner(file) | ||
for scanner.Scan() { | ||
line := scanner.Text() | ||
if line[0:4] == "/dev" { | ||
parts := strings.Fields(line) | ||
info := &syscall.Statfs_t{} | ||
syscall.Statfs(parts[1], info) | ||
|
||
device := &Device{ | ||
name: parts[0], | ||
mountPoint: parts[1], | ||
size: info.Bsize * int64(info.Blocks), | ||
free: info.Bsize * int64(info.Bavail), | ||
} | ||
devices = append(devices, device) | ||
} | ||
} | ||
|
||
if err := scanner.Err(); err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
return devices | ||
} |
Oops, something went wrong.