feat: port the client to MyGo native UI (0.3.0)

Switch from Asuka to MyGo, keeping the API client and the feature set:

- servers: saved servers, passwordless and password login, proxy,
  self-signed certificates, per-server device tokens (Keystore), connect
  form, instance list sheet with switch/disconnect
- sessions: virtualized list, search, new session, rename/archive/delete,
  running indicator, event stream keeps it live
- chat: pushed window with system back bar, streaming Markdown via
  ui/markdown, reasoning and tool rows with image previews, footer menu
  (read aloud via a Kotlin TTS plugin, copy, fork), user message menu
  (copy, revert, fork), context usage ring and detail sheet, attachments
  (photos/files via the document picker), permissions
- release builds allow plain http:// LAN servers (Go's net/http dials
  directly, no cleartext manifest needed)
- a local fork of MyGo (third_party/mygo) lowers the Android host's
  minSdk to 24 (Android 7.0); Go 1.27.4 via mise

Version 0.3.0.
This commit is contained in:
OpenChamber committed 2026-11-18 13:22:59 +08:00
1 parent 2fb4a8ea79
commit 6c0ee61750
2538 files changed
+675779 -4792

No files matched your search

+370
View File
@@ -0,0 +1,370 @@
//go:build linux
// Package inotify watches files with Linux's inotify, for desktop Linux and
// Android.
package inotify
import (
"bytes"
"encoding/binary"
"errors"
"os"
"sync"
"syscall"
"github.com/egoist/mygo/internal/platform"
"github.com/egoist/mygo/internal/watchdriver"
)
// inotifyUser is a target of a watch sharing an inotify watch descriptor.
type inotifyUser struct {
watch *inotifyWatch
id uint64
}
// inotifyGeneration is the targets sharing an inotify watch descriptor:
// inotify returns the same descriptor for the same directory, of any
// watch, and a descriptor removed (retired) can still have events queued
// before its IN_IGNORED, while a new watch of the directory reuses it.
type inotifyGeneration struct {
users []inotifyUser
retired bool
}
// inotifyInstance is the inotify instance of the process, which every
// watch shares, so that watches cost watch descriptors
// (fs.inotify.max_user_watches), not instances (max_user_instances, 128
// on many systems for all the apps of a user), as fsnotify does. A
// goroutine reads it through the runtime's poller and hands each watch
// its notices.
type inotifyInstance struct {
fd int
file *os.File
users int
generations map[int][]*inotifyGeneration
}
// inotifyShared guards the instance, which lives while watches use it.
var inotifyShared struct {
sync.Mutex
instance *inotifyInstance
}
// inotifyWatch is a watch's driver: its targets are directories of the
// shared instance, whose goroutine queues the notices that Wait returns.
// Files are watched through their directory, which names them.
type inotifyWatch struct {
instance *inotifyInstance
targets map[uint64]int // by ID, their descriptors; guarded by inotifyShared
mu sync.Mutex
queue []platform.FileWatchNotice
err error
signal chan struct{}
}
// New starts a watch of the process's inotify instance, which it makes
// when no watch has it.
func New(notify func(platform.FileWatchNotice)) (platform.FileWatch, error) {
inotifyShared.Lock()
defer inotifyShared.Unlock()
in := inotifyShared.instance
if in == nil {
fd, err := syscall.InotifyInit1(syscall.IN_NONBLOCK | syscall.IN_CLOEXEC)
if err != nil {
return nil, err
}
// Non-blocking, so that the runtime's poller waits for it.
in = &inotifyInstance{fd: fd, file: os.NewFile(uintptr(fd), "inotify"), generations: make(map[int][]*inotifyGeneration)}
inotifyShared.instance = in
go in.read(in.file)
}
in.users++
d := &inotifyWatch{instance: in, targets: make(map[uint64]int), signal: make(chan struct{}, 1)}
return watchdriver.New(d, notify), nil
}
func (d *inotifyWatch) Add(t platform.FileWatchTarget) error {
if t.Subtree {
return platform.ErrUnsupported
}
if !t.Directory {
// The watch of its directory names what changes in a file.
return platform.ErrUnsupported
}
fd, err := syscall.Open(t.Path, syscall.O_RDONLY|syscall.O_DIRECTORY|syscall.O_NOFOLLOW|syscall.O_CLOEXEC, 0)
if err != nil {
return err
}
f := os.NewFile(uintptr(fd), t.Path)
defer f.Close()
info, err := f.Stat()
if err != nil {
return err
}
if !os.SameFile(info, t.Info) {
return errors.New("watch target identity changed")
}
inotifyShared.Lock()
defer inotifyShared.Unlock()
in := d.instance
if in.file == nil {
return errors.New("inotify instance closed")
}
mask := uint32(syscall.IN_CREATE | syscall.IN_MODIFY | syscall.IN_CLOSE_WRITE | syscall.IN_ATTRIB | syscall.IN_DELETE | syscall.IN_MOVED_FROM | syscall.IN_MOVED_TO | syscall.IN_DELETE_SELF | syscall.IN_MOVE_SELF | syscall.IN_UNMOUNT | syscall.IN_ONLYDIR | syscall.IN_DONT_FOLLOW)
wd, err := syscall.InotifyAddWatch(in.fd, t.Path, mask)
if err != nil {
return err
}
gens := in.generations[wd]
info, err = os.Lstat(t.Path)
if err != nil || !os.SameFile(info, t.Info) {
// Only a descriptor no target uses is removed.
if len(gens) == 0 || gens[len(gens)-1].retired {
syscall.InotifyRmWatch(in.fd, uint32(wd))
}
if err != nil {
return err
}
return errors.New("watch target identity changed")
}
if len(gens) == 0 || gens[len(gens)-1].retired {
gens = append(gens, &inotifyGeneration{})
}
g := gens[len(gens)-1]
g.users = append(g.users, inotifyUser{d, t.ID})
in.generations[wd] = gens
d.targets[t.ID] = wd
return nil
}
func (d *inotifyWatch) Remove(id uint64) {
inotifyShared.Lock()
defer inotifyShared.Unlock()
d.remove(id)
}
// remove removes target id, under inotifyShared.
func (d *inotifyWatch) remove(id uint64) {
wd, ok := d.targets[id]
if !ok {
return
}
delete(d.targets, id)
in := d.instance
for _, g := range in.generations[wd] {
for i, u := range g.users {
if u == (inotifyUser{d, id}) {
g.users = append(g.users[:i], g.users[i+1:]...)
if len(g.users) == 0 && !g.retired {
g.retired = true
if in.file != nil {
syscall.InotifyRmWatch(in.fd, uint32(wd))
}
}
return
}
}
}
}
type inotifyRecord struct {
wd int
mask, cookie uint32
name string
}
func parseInotify(buf []byte) ([]inotifyRecord, error) {
var records []inotifyRecord
for len(buf) > 0 {
if len(buf) < 16 {
return nil, errors.New("truncated inotify record")
}
length := uint64(binary.NativeEndian.Uint32(buf[12:16]))
if length > uint64(len(buf)-16) {
return nil, errors.New("oversized inotify name")
}
name := ""
if length > 0 {
data := buf[16 : 16+int(length)]
end := bytes.IndexByte(data, 0)
if end < 0 {
return nil, errors.New("unterminated inotify name")
}
name = string(data[:end])
}
records = append(records, inotifyRecord{int(int32(binary.NativeEndian.Uint32(buf))), binary.NativeEndian.Uint32(buf[4:8]), binary.NativeEndian.Uint32(buf[8:12]), name})
buf = buf[16+int(length):]
}
return records, nil
}
// read reads the instance's file until it is closed, and queues what it
// says for the watches.
func (in *inotifyInstance) read(file *os.File) {
var buf [65536]byte
for {
size, err := file.Read(buf[:])
if err == nil && size == 0 {
err = errors.New("inotify stream ended")
}
var records []inotifyRecord
if err == nil {
records, err = parseInotify(buf[:size])
}
inotifyShared.Lock()
if err != nil {
if in.file != nil {
// Lost: every watch using it ends, and new ones get another.
for w := range in.watches() {
w.post(nil, err)
}
in.close()
}
inotifyShared.Unlock()
return
}
notices := map[*inotifyWatch][]platform.FileWatchNotice{}
for _, r := range records {
in.dispatch(r, notices)
}
inotifyShared.Unlock()
for w, n := range notices {
w.post(n, nil)
}
}
}
// watches returns the watches with targets in the instance.
func (in *inotifyInstance) watches() map[*inotifyWatch]bool {
all := map[*inotifyWatch]bool{}
for _, gens := range in.generations {
for _, g := range gens {
for _, u := range g.users {
all[u.watch] = true
}
}
}
return all
}
// dispatch adds what r says to the notices of the watches it concerns,
// under inotifyShared.
func (in *inotifyInstance) dispatch(r inotifyRecord, notices map[*inotifyWatch][]platform.FileWatchNotice) {
if r.mask&syscall.IN_Q_OVERFLOW != 0 {
for w := range in.watches() {
notices[w] = append(notices[w], platform.FileWatchNotice{Err: platform.ErrFileWatchOverflow})
}
return
}
gens := in.generations[r.wd]
if len(gens) == 0 {
return
}
g := gens[0]
if r.mask&syscall.IN_IGNORED != 0 {
if len(gens) == 1 {
delete(in.generations, r.wd)
} else {
in.generations[r.wd] = gens[1:]
}
if !g.retired {
// The directory's filesystem was unmounted, or inotify
// dropped the watch for another reason.
for _, u := range g.users {
notices[u.watch] = append(notices[u.watch], platform.FileWatchNotice{Target: u.id, Kind: platform.FileWatchLost})
delete(u.watch.targets, u.id)
}
}
return
}
if g.retired || len(g.users) == 0 {
return
}
var kind platform.FileWatchKind
switch {
case r.mask&syscall.IN_UNMOUNT != 0:
return // IN_IGNORED follows
case r.mask&syscall.IN_MOVED_FROM != 0:
kind = platform.FileWatchMoveFrom
case r.mask&syscall.IN_MOVED_TO != 0:
kind = platform.FileWatchMoveTo
case r.mask&syscall.IN_CREATE != 0:
kind = platform.FileWatchCreate
case r.mask&(syscall.IN_DELETE|syscall.IN_DELETE_SELF) != 0:
kind = platform.FileWatchRemove
case r.mask&(syscall.IN_MODIFY|syscall.IN_CLOSE_WRITE) != 0:
kind = platform.FileWatchWrite
}
// IN_ATTRIB is dirty: the core looks, and metadata alone is not a
// write.
//
// A watch that added the directory twice hears of it once, through
// its latest target.
seen := map[*inotifyWatch]bool{}
for i := len(g.users) - 1; i >= 0; i-- {
u := g.users[i]
if !seen[u.watch] {
seen[u.watch] = true
notices[u.watch] = append(notices[u.watch], platform.FileWatchNotice{Target: u.id, Kind: kind, Name: r.name})
}
}
// A deleted directory's automatic IN_IGNORED is expected; retain its
// removal hint instead of turning it into stream loss.
if r.mask&syscall.IN_DELETE_SELF != 0 {
g.retired = true
}
}
// close closes the instance, under inotifyShared, which ends read.
func (in *inotifyInstance) close() {
if in.file != nil {
in.file.Close()
in.file = nil
}
if inotifyShared.instance == in {
inotifyShared.instance = nil
}
}
// post queues notices, or the error that ends the watch, for Wait.
func (d *inotifyWatch) post(notices []platform.FileWatchNotice, err error) {
d.mu.Lock()
d.queue = append(d.queue, notices...)
if err != nil && d.err == nil {
d.err = err
}
d.mu.Unlock()
d.Wake()
}
func (d *inotifyWatch) Wait() ([]platform.FileWatchNotice, error) {
d.mu.Lock()
if len(d.queue) == 0 && d.err == nil {
d.mu.Unlock()
<-d.signal
d.mu.Lock()
}
notices, err := d.queue, d.err
d.queue = nil
d.mu.Unlock()
return notices, err
}
func (d *inotifyWatch) Wake() {
select {
case d.signal <- struct{}{}:
default:
}
}
func (d *inotifyWatch) Close() {
inotifyShared.Lock()
defer inotifyShared.Unlock()
for id := range d.targets {
d.remove(id)
}
in := d.instance
if in.users--; in.users == 0 {
in.close()
}
}
+162
View File
@@ -0,0 +1,162 @@
//go:build linux
package inotify
import (
"encoding/binary"
"fmt"
"os"
"path/filepath"
"testing"
"time"
"github.com/egoist/mygo/internal/platform"
"github.com/egoist/mygo/internal/wait"
)
func TestWatchInotifyParser(t *testing.T) {
record := make([]byte, 20)
binary.NativeEndian.PutUint32(record, 5)
binary.NativeEndian.PutUint32(record[8:], 77)
binary.NativeEndian.PutUint32(record[12:], 4)
copy(record[16:], "abc")
got, err := parseInotify(record)
if err != nil || len(got) != 1 || got[0].name != "abc" || got[0].cookie != 77 || got[0].wd != 5 {
t.Fatalf("%+v %v", got, err)
}
for i := 1; i < len(record); i++ {
if _, err := parseInotify(record[:i]); err == nil {
t.Fatalf("accepted truncation %d", i)
}
}
malformed := append([]byte(nil), record...)
malformed[19] = 'x'
if _, err := parseInotify(malformed); err == nil {
t.Fatal("accepted unterminated name")
}
binary.NativeEndian.PutUint32(malformed[12:], 0xffffffff)
if _, err := parseInotify(malformed); err == nil {
t.Fatal("accepted oversized name")
}
}
func TestInotifyWatch(t *testing.T) {
dir := t.TempDir()
sub := filepath.Join(dir, "sub")
if err := os.Mkdir(sub, 0700); err != nil {
t.Fatal(err)
}
notices := make(chan platform.FileWatchNotice, 16)
w, err := New(func(n platform.FileWatchNotice) { notices <- n })
if err != nil {
t.Fatal(err)
}
defer w.Close()
add := func(id uint64, path string, subtree bool) error {
t.Helper()
info, err := os.Lstat(path)
if err != nil {
t.Fatal(err)
}
return w.Add(platform.FileWatchTarget{ID: id, Path: path, Directory: info.IsDir(), Info: info, Subtree: subtree})
}
if err := add(1, dir, true); err != platform.ErrUnsupported {
t.Fatalf("subtree: %v", err)
}
for id, path := range map[uint64]string{1: dir, 2: sub} {
if err := add(id, path, false); err != nil {
t.Fatal(err)
}
}
next := func(want platform.FileWatchNotice) {
t.Helper()
for {
n := wait.For(t, fmt.Sprintf("notice %+v", want), notices)
if n == want {
return
}
if n.Err != nil || n.Kind == platform.FileWatchLost {
t.Fatalf("notice %+v", n)
}
}
}
if err := os.WriteFile(filepath.Join(sub, "file"), nil, 0600); err != nil {
t.Fatal(err)
}
next(platform.FileWatchNotice{Target: 2, Kind: platform.FileWatchCreate, Name: "file"})
if err := os.WriteFile(filepath.Join(sub, "file"), []byte("written"), 0600); err != nil {
t.Fatal(err)
}
next(platform.FileWatchNotice{Target: 2, Kind: platform.FileWatchWrite, Name: "file"})
// A directory deleted is a removal, not a lost watch.
if err := os.RemoveAll(sub); err != nil {
t.Fatal(err)
}
next(platform.FileWatchNotice{Target: 2, Kind: platform.FileWatchRemove})
next(platform.FileWatchNotice{Target: 1, Kind: platform.FileWatchRemove, Name: "sub"})
w.Remove(2)
if err := os.Rename(filepath.Join(dir, "missing"), filepath.Join(dir, "x")); err == nil {
t.Fatal("renamed nothing")
}
if err := os.WriteFile(filepath.Join(dir, "a"), nil, 0600); err != nil {
t.Fatal(err)
}
if err := os.Rename(filepath.Join(dir, "a"), filepath.Join(dir, "b")); err != nil {
t.Fatal(err)
}
next(platform.FileWatchNotice{Target: 1, Kind: platform.FileWatchMoveFrom, Name: "a"})
next(platform.FileWatchNotice{Target: 1, Kind: platform.FileWatchMoveTo, Name: "b"})
}
// Watches share the inotify instance of the process, and the descriptor
// of a directory they both watch, which one removing leaves to the other.
func TestInotifyShared(t *testing.T) {
dir := t.TempDir()
watch := func() (platform.FileWatch, chan platform.FileWatchNotice) {
t.Helper()
notices := make(chan platform.FileWatchNotice, 16)
w, err := New(func(n platform.FileWatchNotice) { notices <- n })
if err != nil {
t.Fatal(err)
}
info, err := os.Lstat(dir)
if err != nil {
t.Fatal(err)
}
if err := w.Add(platform.FileWatchTarget{ID: 7, Path: dir, Directory: true, Info: info}); err != nil {
t.Fatal(err)
}
return w, notices
}
a, an := watch()
b, bn := watch()
defer b.Close()
inotifyShared.Lock()
in := inotifyShared.instance
users := in.users
inotifyShared.Unlock()
if users < 2 {
t.Fatalf("%d users", users)
}
next := func(notices chan platform.FileWatchNotice, name string) {
t.Helper()
select {
case n := <-notices:
if n.Target != 7 || n.Name != name || n.Kind != platform.FileWatchCreate {
t.Fatalf("notice %+v", n)
}
case <-time.After(wait.Guard):
t.Fatal("no notice")
}
}
if err := os.Mkdir(filepath.Join(dir, "x"), 0700); err != nil {
t.Fatal(err)
}
next(an, "x")
next(bn, "x")
a.Close()
if err := os.Mkdir(filepath.Join(dir, "y"), 0700); err != nil {
t.Fatal(err)
}
next(bn, "y")
}