summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJeff Carr <[email protected]>2025-08-17 17:02:29 -0500
committerJeff Carr <[email protected]>2025-08-17 22:57:06 -0500
commit25c604aeee7cd821d27dfd4c09bf0626e9312535 (patch)
treee7b875d4077ffb7fa265c7a1bd8fefafbc9d0235
parentbd5b16d8755bc9e5f0dc90162bc4724d319c725e (diff)
protobuf file for block devices
-rw-r--r--.gitignore1
-rw-r--r--Makefile10
-rw-r--r--block.proto20
-rw-r--r--doDrives.go11
-rw-r--r--doGui.go58
-rw-r--r--getDrives.go4
-rw-r--r--main.go2
-rw-r--r--structs.go12
-rw-r--r--watchForNewDrives.go4
9 files changed, 101 insertions, 21 deletions
diff --git a/.gitignore b/.gitignore
index aecd2d5..42ff27e 100644
--- a/.gitignore
+++ b/.gitignore
@@ -3,4 +3,5 @@ go.mod
go.sum
fixup
*.so
+*pb.go
/files/*
diff --git a/Makefile b/Makefile
index 5601d2a..f53f5da 100644
--- a/Makefile
+++ b/Makefile
@@ -2,7 +2,7 @@ VERSION = $(shell git describe --tags)
GUIVERSION = $(shell git describe --tags)
BUILDTIME = $(shell date +%Y.%m.%d)
-all: verbose
+all: block.pb.go verbose
# -fixup --gui-check-plugin ../../../toolkits/gocui/gocui.so
fixup --gui gocui --gui-verbose --gui-file ../../toolkits/gocui/gocui.so
@@ -32,9 +32,17 @@ goimports:
clean:
rm -f go.*
+ rm -f *.pb.go
+ go-mod-clean purge
gpl:
wit-test --witcom
check-git-clean:
@git diff-index --quiet HEAD -- || (echo "Git repository is dirty, please commit your changes first"; exit 1)
+
+block.pb.go: block.proto
+ autogenpb --proto block.proto
+
+commit:
+ FORGE_URL="https://forge.grid.wit.com/" forge commit --all
diff --git a/block.proto b/block.proto
new file mode 100644
index 0000000..e29e9c0
--- /dev/null
+++ b/block.proto
@@ -0,0 +1,20 @@
+syntax = "proto3";
+
+package main;
+
+message Block {
+ string name = 1; // `autogenpb:sort` `autogenpb:unique`
+ uint32 major = 2;
+ uint32 minor = 3;
+ uint64 size_bytes = 4;
+ bool removable = 5;
+ bool read_only = 6;
+ string type = 7; // e.g., "disk", "part", "rom", "loop"
+ repeated string mountpoints = 8;
+}
+
+message Blocks { // `autogenpb:marshal` `autogenpb:mutex`
+ string uuid = 1; // `autogenpb:uuid:abe10848-cf2b-4440-b5a8-0aaa2ce50aad`
+ string version = 2; // `autogenpb:version:v0.0.1`
+ repeated Block Blocks = 3; // THIS MUST BE Block and then Blocks
+}
diff --git a/doDrives.go b/doDrives.go
index a1e0b91..6ded27a 100644
--- a/doDrives.go
+++ b/doDrives.go
@@ -17,7 +17,11 @@ func doDrives() {
}
for _, p := range parts {
- log.Printf("Device: /dev/%s\tSize: %d KiB\n", p.Name, p.Blocks)
+ log.Printf("RAW Device: /dev/%s\tSize: %d KiB\n", p.Name, p.Blocks)
+ devname := "/dev/" + p.Name
+ if blkpb := me.pb.FindByName(devname); blkpb != nil {
+ blkpb.SizeBytes = uint64(p.Blocks) * 1024
+ }
}
blockDir := "/sys/block"
@@ -44,11 +48,12 @@ func doDrives() {
used := false
for _, part := range partitions {
partName := filepath.Base(part)
+ partDev := "/dev/" + partName
if partName == dev {
+ log.Printf(" SKIP MAIN Partition: %s\n", partDev)
continue // skip the main device
}
hasPartition = true
- partDev := "/dev/" + partName
isMounted := mounts[partDev]
log.Printf(" Partition: %s [%v]\n", partDev, isMounted)
if isMounted {
@@ -61,7 +66,7 @@ func doDrives() {
}
if !used {
- log.Println(" → Drive appears to be unused.")
+ log.Println(" * Drive appears to be unused.")
}
}
return nil
diff --git a/doGui.go b/doGui.go
index bc8890b..9a902ee 100644
--- a/doGui.go
+++ b/doGui.go
@@ -6,6 +6,7 @@ package main
// An app to submit patches for the 30 GO GUI repos
import (
+ "fmt"
"os"
"strings"
"time"
@@ -13,16 +14,45 @@ import (
"go.wit.com/gui"
"go.wit.com/lib/gadgets"
"go.wit.com/lib/gui/shell"
+ "go.wit.com/lib/protobuf/virtpb"
"go.wit.com/log"
)
func debug() {
for {
- time.Sleep(3 * time.Second)
- log.Info("TODO: use this?")
+ time.Sleep(10 * time.Second)
+ log.Info("TODO: use this debug loop for something?")
}
}
+// adds a new drive to the dropdown menu and protobuf
+func addDrive(devname string, displayDesc string) *Block {
+ if blkpb := me.pb.FindByName(devname); blkpb != nil {
+ return blkpb
+ }
+
+ s := log.Sprintf("%-12s %-8s", devname, displayDesc)
+ log.Info(s)
+ me.dd.AddText(s)
+
+ blkpb := me.pb.InsertByName(devname)
+ return blkpb
+}
+
+func switchDrive(blk *Block) {
+ me.currentDev = blk
+ me.parted.SetText("Partition " + blk.Name)
+
+ log.Info("check if", me.currentDev.Name, "is in use")
+ result, err := shell.RunVerbose([]string{"parted", me.currentDev.Name, "print"})
+ log.Info("result =", result.Stdout, "err =", err)
+ out := strings.Join(result.Stdout, "\n")
+ if err != nil {
+ out += fmt.Sprintf("err = %v", err)
+ }
+ me.driveInfoBox.SetText(out)
+}
+
func doGui() {
me.myGui = gui.New()
me.myGui.InitEmbed(resources)
@@ -51,19 +81,21 @@ func drawWindow(win *gadgets.GenericWindow) {
if len(fields) < 1 {
return
}
- me.currentDev = fields[0]
- me.parted.SetText("Partition " + me.currentDev)
+ if blkpb := me.pb.FindByName(fields[0]); blkpb != nil {
+ switchDrive(blkpb)
+ } else {
+ log.Info("couldn't find in protobuf:", fields)
+ }
}
grid.NextRow()
// a button to format or blank a drive
me.parted = grid.NewButton("select drive", func() {
- if me.currentDev == "" {
+ if me.currentDev == nil {
log.Info("You must select a drive first")
return
}
- log.Info("check if", me.currentDev, "is in use")
- shell.RunVerbose([]string{"parted", me.currentDev, "print"})
+ log.Info("TODO: figure out if the drive is in use")
})
grid.NextRow()
@@ -72,6 +104,16 @@ func drawWindow(win *gadgets.GenericWindow) {
doDrives()
})
- doDrives2()
+ grid.NewButton("show blkpb", func() {
+ for blk := range me.pb.IterAll() {
+ log.Printf("found %-12s %s\n", blk.Name, virtpb.HumanFormatBytes(int64(blk.SizeBytes)))
+ }
+ })
+ grid.NextRow()
+ grid = win.Middle.RawGrid()
+ me.driveInfoBox = grid.NewLabel("<drive info>")
+
+ doDrives2()
+ doDrives()
}
diff --git a/getDrives.go b/getDrives.go
index a818645..13dacc6 100644
--- a/getDrives.go
+++ b/getDrives.go
@@ -43,9 +43,7 @@ func doDrives2() {
for _, info := range devs {
devPath := "/dev/" + info.Name
if info.IsRaw {
- s := log.Sprintf("%-12s -> %-8s (raw)", devPath, info.Type)
- log.Info(s)
- me.dd.AddText(s)
+ addDrive(devPath, log.Sprintf("-> %-8s (raw)", info.Type))
} else {
if info.Parent != "" {
log.Printf("%-12s -> partition of /dev/%s\n", devPath, info.Parent)
diff --git a/main.go b/main.go
index 3e6acdf..8d7085d 100644
--- a/main.go
+++ b/main.go
@@ -49,6 +49,8 @@ func main() {
os.Exit(0)
}
+ me.pb = NewBlocks()
+
/*
if argv.Drives != nil {
doDrives()
diff --git a/structs.go b/structs.go
index 2fb0554..bac17be 100644
--- a/structs.go
+++ b/structs.go
@@ -12,9 +12,11 @@ var me *autoType
// this app's variables
type autoType struct {
- pp *arg.Parser // go-arg preprocessor
- myGui *gui.Node // the gui toolkit handle
- dd *gui.Node // the drives dropdown list
- parted *gui.Node // the current drive to run parted on
- currentDev string // the current dev entry to work on
+ pp *arg.Parser // go-arg preprocessor
+ myGui *gui.Node // the gui toolkit handle
+ dd *gui.Node // the drives dropdown list
+ parted *gui.Node // the current drive to run parted on
+ currentDev *Block // the current dev entry to work on
+ pb *Blocks // the block dev protobuf
+ driveInfoBox *gui.Node // displays the drive info
}
diff --git a/watchForNewDrives.go b/watchForNewDrives.go
index 3f50707..bf92721 100644
--- a/watchForNewDrives.go
+++ b/watchForNewDrives.go
@@ -44,7 +44,9 @@ func listenForBlockEvents() {
msg := parseUevent(buf[:n])
if msg["SUBSYSTEM"] == "block" && msg["ACTION"] == "add" {
log.Printf("New block device added: %s\n", msg["DEVNAME"])
- me.dd.AddText("/dev/" + msg["DEVNAME"] + " new")
+ devname := "/dev/" + msg["DEVNAME"]
+ me.dd.AddText(devname + " new")
+ me.pb.InsertByName(devname)
}
log.Printf("New syscall.NETLINK_KOBJECT_UEVENT: %v\n", msg)
}