shithub: snarflog

Download patch

ref: 3aadede9a41a1a7c4928631b9681a67c1c6ac2a3
parent: dbe70e0ee435c2cced5537c72cd9270a2d80a0bb
author: penny <penny@limitedideas.org>
date: Tue Dec 2 13:45:15 EST 2025

exit when the original attach clunks

--- a/main.go
+++ b/main.go
@@ -13,24 +13,10 @@
 	"github.com/knusbaum/go9p/proto"
 )
 
-type snarfile struct {
-	fs.BaseFile
-	snarfStream fs.Stream
-	mu          sync.Mutex
-	writemap    map[uint64][]byte
-}
-
-func (f *snarfile) Write(fid uint64, offset uint64, data []byte) (uint32, error) {
-	f.mu.Lock()
-	defer f.mu.Unlock()
-	f.writemap[fid] = append(f.writemap[fid], data...)
-	return uint32(len(data)), nil
-}
-
 func (f *snarfile) Close(fid uint64) error {
 	f.mu.Lock()
 	defer f.mu.Unlock()
-
+	defer f.BaseFile.Close(fid)
 	if data, ok := f.writemap[fid]; ok {
 		realwrite, err := os.OpenFile("/mnt/snarflog/rsnarf", os.O_WRONLY, 0644)
 		if err != nil {
@@ -56,6 +42,52 @@
 	return nil
 }
 
+type snarfile struct {
+	fs.BaseFile
+	snarfStream fs.Stream
+	mu          sync.Mutex
+	writemap    map[uint64][]byte
+}
+
+type snarfFSState struct {
+	mu       sync.Mutex
+	firstfid uint32
+}
+
+type snarfFS struct {
+	go9p.Srv
+	state *snarfFSState
+}
+
+func (srv snarfFS) Attach(connection go9p.Conn, attach *proto.TAttach) (proto.FCall, error) {
+	srv.state.mu.Lock()
+	if srv.state.firstfid == 0 {
+		srv.state.firstfid = attach.Fid
+	}
+	defer srv.state.mu.Unlock()
+	return srv.Srv.Attach(connection, attach)
+}
+
+func (srv snarfFS) Clunk(connection go9p.Conn, clunk *proto.TClunk) (proto.FCall, error) {
+	srv.state.mu.Lock()
+	defer srv.state.mu.Unlock()
+	if srv.state.firstfid != 0 {
+		if clunk.Fid == srv.state.firstfid {
+			os.Exit(1)
+		}
+	}
+	return srv.Srv.Clunk(connection, clunk)
+}
+
+func (f *snarfile) Write(fid uint64, offset uint64, data []byte) (uint32, error) {
+	f.mu.Lock()
+	defer f.mu.Unlock()
+	f.writemap[fid] = append(f.writemap[fid], data...)
+	return uint32(len(data)), nil
+}
+
+
+
 func (f *snarfile) Read(fid uint64, offset uint64, count uint64) ([]byte, error) {
 	real, err := os.Open("/mnt/snarflog/rsnarf")
 	if err != nil {
@@ -140,7 +172,11 @@
 	}
 	
 	go func() {
-		if err := go9p.PostSrv("snarflog", fid.Server()); err != nil {
+		server := snarfFS{
+			Srv:   fid.Server(),
+			state: &snarfFSState{},
+		}
+		if err := go9p.PostSrv("snarflog", server); err != nil {
 			fmt.Printf("Failed to post server: %v\n", err)
 			os.Exit(1)
 		}
--