Files
openchamber-android-native/third_party/mygo/internal/fake/watch.go
T
OpenChamber 6c0ee61750 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.
2026-11-18 13:22:59 +08:00

122 lines
3.2 KiB
Go

package fake
import (
"errors"
"os"
"runtime"
"sync"
"syscall"
"time"
"github.com/egoist/mygo/internal/platform"
)
// FileWatch records the targets the core enrolls; tests report changes
// with Notify.
type FileWatch struct {
mu sync.Mutex
targets map[uint64]platform.FileWatchTarget
notify func(platform.FileWatchNotice)
closed bool
// Subtree makes Add accept Subtree targets, as Windows does.
Subtree bool
// NoFiles makes Add refuse targets that aren't directories, as every
// system backend does.
NoFiles bool
// AddHook, when set, runs in Add before the target is enrolled; an
// error it returns is Add's.
AddHook func(platform.FileWatchTarget) error
}
func (b *Backend) NewFileWatch(notify func(platform.FileWatchNotice)) (platform.FileWatch, error) {
b.mu.Lock()
defer b.mu.Unlock()
if b.FileWatchError != nil {
return nil, b.FileWatchError
}
w := &FileWatch{targets: map[uint64]platform.FileWatchTarget{}, notify: notify, Subtree: b.FileWatchSubtree, NoFiles: b.FileWatchNoFiles, AddHook: b.FileWatchAddHook}
b.FileWatches = append(b.FileWatches, w)
return w, nil
}
// LastFileWatch returns the FileWatch created last.
func (b *Backend) LastFileWatch() *FileWatch {
b.mu.Lock()
defer b.mu.Unlock()
return b.FileWatches[len(b.FileWatches)-1]
}
func (w *FileWatch) Add(t platform.FileWatchTarget) error {
if t.Subtree && !w.Subtree || !t.Directory && w.NoFiles {
return platform.ErrUnsupported
}
if hook := w.AddHook; hook != nil {
if err := hook(t); err != nil {
return err
}
}
w.mu.Lock()
defer w.mu.Unlock()
w.targets[t.ID] = t
return nil
}
func (w *FileWatch) Remove(id uint64) {
w.mu.Lock()
defer w.mu.Unlock()
delete(w.targets, id)
}
func (w *FileWatch) Close() {
w.mu.Lock()
defer w.mu.Unlock()
w.closed = true
clear(w.targets)
}
// Targets returns the enrolled targets.
func (w *FileWatch) Targets() []platform.FileWatchTarget {
w.mu.Lock()
defer w.mu.Unlock()
targets := make([]platform.FileWatchTarget, 0, len(w.targets))
for _, t := range w.targets {
targets = append(targets, t)
}
return targets
}
// Notify reports n, as a backend's goroutine would, unless the watch is
// closed.
func (w *FileWatch) Notify(n platform.FileWatchNotice) {
w.mu.Lock()
closed := w.closed
w.mu.Unlock()
if !closed {
w.notify(n)
}
}
// ReplaceFile renames replacement over target, with the target's
// permissions. On Windows it gives a reader holding the target open
// without sharing deletes a moment to let go, as the Windows backend does:
// a test reading the file while the app saves it is such a reader.
func (b *Backend) ReplaceFile(target, replacement string) error {
if b.ReplaceFileHook != nil {
return b.ReplaceFileHook(target, replacement)
}
if info, err := os.Stat(target); err == nil {
if err := os.Chmod(replacement, info.Mode().Perm()); err != nil {
return err
}
}
for attempt := 0; ; attempt++ {
err := os.Rename(replacement, target)
// ERROR_ACCESS_DENIED, ERROR_SHARING_VIOLATION, ERROR_LOCK_VIOLATION.
inUse := errors.Is(err, syscall.Errno(5)) || errors.Is(err, syscall.Errno(32)) || errors.Is(err, syscall.Errno(33))
if err == nil || runtime.GOOS != "windows" || !inUse || attempt == 9 {
return err
}
time.Sleep(50 * time.Millisecond)
}
}