shithub: mycel

Download patch

ref: f74ef2d6fc0188c94e824879883a4e5477ca82fe
parent: 36c2256362c7ed26713b98b5cb6e9c6f3138448f
author: Philip Silva <philip.silva@protonmail.com>
date: Fri Oct 22 12:46:06 EDT 2021

gojafs via /mnt / srv

--- a/cmd/gojafs/main.go
+++ b/cmd/gojafs/main.go
@@ -5,7 +5,6 @@
 	"bufio"
 	"encoding/json"
 	"fmt"
-	"github.com/knusbaum/go9p"
 	"github.com/knusbaum/go9p/fs"
 	"github.com/psilva261/opossum"
 	"github.com/psilva261/opossum/cmd/gojafs/domino"
@@ -54,8 +53,8 @@
 	root.AddChild(c)
 	lctl := (*fs.ListenFileListener)(c)
 	go Ctl(lctl)
-	log.Infof("go9p.ServeReadWriter...\n")
-	return go9p.ServeReadWriter(r, w, gojafs.Server())
+	log.Infof("post fs...\n")
+	return post(gojafs.Server())
 }
 
 func Ctl(lctl *fs.ListenFileListener) {
@@ -225,4 +224,5 @@
 	if err := Main(os.Stdin, os.Stdout); err != nil {
 		log.Fatalf("Main: %+v", err)
 	}
+	select{}
 }
--- a/cmd/gojafs/main_plan9.go
+++ b/cmd/gojafs/main_plan9.go
@@ -2,9 +2,11 @@
 
 import (
 	"fmt"
+	"github.com/knusbaum/go9p"
 	"github.com/psilva261/opossum/logger"
 	"io"
 	"os"
+	"syscall"
 )
 
 func Init() (err error) {
@@ -40,4 +42,27 @@
 
 func openXhr() (rwc io.ReadWriteCloser, err error) {
 	return os.OpenFile(mtpt+"/xhr", os.O_RDWR, 0600)
+}
+
+func post(srv go9p.Srv) (err error) {
+	f1, f2, err := os.Pipe()
+	if err != nil {
+		return fmt.Errorf("pipe: %w", err)
+	}
+
+	go func() {
+		err = go9p.ServeReadWriter(f1, f1, srv)
+		if err != nil {
+			log.Errorf("serve rw: %v", err)
+		}
+	}()
+
+	if err = syscall.Mount(int(f2.Fd()), -1, "/mnt/goja", syscall.MCREATE, ""); err != nil {
+		return fmt.Errorf("mount: %w", err)
+	}
+	return
+}
+
+func callGojaCtl() (rwc io.ReadWriteCloser, err error) {
+	return os.OpenFile("/mnt/goja/ctl", os.O_RDWR, 0600)
 }
--- a/cmd/gojafs/main_test.go
+++ b/cmd/gojafs/main_test.go
@@ -1,83 +1,38 @@
 package main
 
 import (
-	"9fans.net/go/plan9"
-	"9fans.net/go/plan9/client"
 	"bufio"
 	"bytes"
-	"fmt"
 	"io"
 	"net"
-	"os/user"
 	"strings"
 	"testing"
 )
 
-func connect() (fsys *client.Fsys, c io.Closer, err error) {
-	u, err := user.Current()
-	if err != nil {
-		return
-	}
-	un := u.Username
-	c1, c2 := net.Pipe()
-
-	go func() {
-		if err = Main(c1, c1); err != nil && err != io.EOF {
-			panic(err.Error())
-		}
-	}()
-
-	conn, err := client.NewConn(c2)
-	if err != nil {
-		return
-	}
-	fsys, err = conn.Attach(nil, un, "")
-	if err != nil {
-		return
-	}
-	return fsys, conn, nil
-}
-
-func call(fsys *client.Fsys, fn, cmd string, args ...string) (resp string, err error) {
-	fid, err := fsys.Open(fn, plan9.ORDWR)
-	if err != nil {
-		return
-	}
-	defer fid.Close()
-	fid.Write([]byte(cmd + "\n"))
+func call(fn, cmd string, args ...string) (resp string, err error) {
+	conn, rwc := net.Pipe()
+	go ctl(conn)
+	defer rwc.Close()
+	rwc.Write([]byte(cmd + "\n"))
 	for _, arg := range args {
-		fid.Write([]byte(arg + "\n"))
+		rwc.Write([]byte(arg + "\n"))
 	}
-	r := bufio.NewReader(fid)
+	r := bufio.NewReader(rwc)
 	b := bytes.NewBuffer([]byte{})
 	_, err = io.Copy(b, r)
-	if !strings.Contains(err.Error(), io.ErrClosedPipe.Error()) {
+	/*if !strings.Contains(err.Error(), io.ErrClosedPipe.Error()) {
 		return "", fmt.Errorf("unexpected error: %v", err)
-	}
+	}*/
 	return b.String(), nil
 }
 
 func TestMain(t *testing.T) {
-	t.Logf("connect...")
-	fsys, c, err := connect()
-	if err != nil {
-		t.Fatalf("%v", err)
-	}
-	defer c.Close()
-	t.Logf("stat...")
-	d, err := fsys.Stat("ctl")
-	if err != nil {
-		t.Fatalf("%v", err)
-	}
-	if d.Name != "ctl" {
-		t.Fail()
-	}
 	htm = "<html><h1 id=title>hello</h1></html>"
 	js = []string{
 		"document.getElementById('title').innerHTML='world'",
 	}
 	t.Logf("call start...")
-	resp, err := call(fsys, "ctl", "start")
+	resp, err := call("ctl", "start")
 	if err != nil {
 		t.Fatalf("%v", err)
 	}
@@ -88,11 +43,6 @@
 }
 
 func TestClick(t *testing.T) {
-	fsys, c, err := connect()
-	if err != nil {
-		t.Fatalf("%v", err)
-	}
-	defer c.Close()
 	htm = "<html><h1 id=title>hello</h1></html>"
 	js = []string{
 		`var c = 1;
@@ -100,11 +50,11 @@
 			c = 3;
 		});`,
 	}
-	_, err = call(fsys, "ctl", "start")
+	_, err := call("ctl", "start")
 	if err != nil {
 		t.Fatalf("%v", err)
 	}
-	_, err = call(fsys, "ctl", "click", "#title")
+	_, err = call("ctl", "click", "#title")
 	if err != nil {
 		t.Fatalf("%v", err)
 	}
--- a/cmd/gojafs/main_unix.go
+++ b/cmd/gojafs/main_unix.go
@@ -1,5 +1,4 @@
 //go:build !plan9
-// +build !plan9
 
 package main
 
@@ -7,6 +6,7 @@
 	"9fans.net/go/plan9"
 	"9fans.net/go/plan9/client"
 	"fmt"
+	"github.com/knusbaum/go9p"
 	"github.com/psilva261/opossum/logger"
 	"io"
 	"os/user"
@@ -84,4 +84,8 @@
 
 func openXhr() (rwc io.ReadWriteCloser, err error) {
 	return fsys.Open("xhr", plan9.ORDWR)
+}
+
+func post(srv go9p.Srv) (err error) {
+	return go9p.PostSrv("goja", srv)
 }
--- a/js/js.go
+++ b/js/js.go
@@ -1,8 +1,6 @@
 package js
 
 import (
-	"9fans.net/go/plan9"
-	"9fans.net/go/plan9/client"
 	"bufio"
 	"bytes"
 	"context"
@@ -14,7 +12,6 @@
 	"io"
 	"os"
 	"os/exec"
-	"os/user"
 	"strings"
 	"time"
 )
@@ -31,7 +28,6 @@
 	fetcher opossum.Fetcher
 	nt      *nodes.Node
 
-	fsys   *client.Fsys
 	cancel context.CancelFunc
 )
 
@@ -41,19 +37,16 @@
 }
 
 func call(fn, cmd string, args ...string) (resp string, err error) {
-	if fsys == nil {
-		return "", fmt.Errorf("fsys nil")
-	}
-	fid, err := fsys.Open(fn, plan9.ORDWR)
+	rwc, err := callGojaCtl()
 	if err != nil {
-		return
+		return "", fmt.Errorf("call goja ctl: %v", err)
 	}
-	defer fid.Close()
-	fid.Write([]byte(cmd + "\n"))
+	defer rwc.Close()
+	rwc.Write([]byte(cmd + "\n"))
 	for _, arg := range args {
-		fid.Write([]byte(arg + "\n"))
+		rwc.Write([]byte(arg + "\n"))
 	}
-	r := bufio.NewReader(fid)
+	r := bufio.NewReader(rwc)
 	b := bytes.NewBuffer([]byte{})
 	_, err = io.Copy(b, r)
 	if err != nil && !strings.Contains(err.Error(), io.ErrClosedPipe.Error()) {
@@ -72,46 +65,19 @@
 	ctx, cancel = context.WithCancel(context.Background())
 	cmd := exec.CommandContext(ctx, "gojafs", args...)
 	cmd.Stderr = os.Stderr
-	stdin, err := cmd.StdinPipe()
-	if err != nil {
-		return "", false, fmt.Errorf("stdin pipe: %w", err)
-	}
-	stdout, err := cmd.StdoutPipe()
-	if err != nil {
-		return "", false, fmt.Errorf("stdout pipe: %w", err)
-	}
-	rwc := &ReadWriteCloser{
-		Reader: stdout,
-		Writer: stdin,
-		Closer: stdin,
-	}
 
 	log.Infof("cmd.Start...")
 	if err = cmd.Start(); err != nil {
-		return "", false, fmt.Errorf("start: %w", err)
+		return "", false, fmt.Errorf("cmd start: %w", err)
 	}
 	// Prevent Zombie processes after stopping
 	go cmd.Wait()
 
-	conn, err := client.NewConn(rwc)
-	if err != nil {
-		return "", false, fmt.Errorf("new conn: %w", err)
-	}
-	log.Infof("cmd.Connected...")
-	u, err := user.Current()
-	if err != nil {
-		return
-	}
-	un := u.Username
-	fsys, err = conn.Attach(nil, un, "")
-	if err != nil {
-		return
-	}
-	log.Infof("cmd.Attached...")
+	<-time.After(5*time.Second)
 
 	resp, err := call("ctl", "start")
 	if err != nil {
-		return "", false, fmt.Errorf("%v", err)
+		return "", false, fmt.Errorf("call start: %v", err)
 	}
 
 	if resp != "" {
--- a/js/js_test.go
+++ b/js/js_test.go
@@ -46,11 +46,11 @@
 
 	NewJS(simpleHTML, nil, nil)
 	resHtm, changed, err := Start(string(buf), script)
-	if !changed {
-		t.Fatalf("changed=%v", changed)
-	}
 	if err != nil {
 		t.Fatalf("%v", err)
+	}
+	if !changed {
+		t.Fatalf("changed=%v", changed)
 	}
 	t.Logf("resHtm=%v", resHtm)