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.
86 lines
2.8 KiB
Go
86 lines
2.8 KiB
Go
package mygo
|
|
|
|
import (
|
|
"errors"
|
|
"io/fs"
|
|
"os"
|
|
"path/filepath"
|
|
"runtime"
|
|
"strings"
|
|
"syscall"
|
|
"time"
|
|
)
|
|
|
|
// Windows refuses to replace or delete a file that another program has
|
|
// open without sharing its deletion, as Go opens files, and antivirus
|
|
// software and indexers do while they scan new files; and it refuses to
|
|
// open a file while it is being replaced. The app's own files (window
|
|
// states, recovery copies, recent documents) are replaced in place, so
|
|
// their renames and reads retry for a few seconds while Windows reports
|
|
// them in use, rather than lose what they hold. The app's own reads share
|
|
// the file's deletion (readShared) and its replaces rename with POSIX
|
|
// semantics (renameOver), so that a goroutine reading an app file never
|
|
// keeps another from saving it.
|
|
|
|
// rename renames a file of the app, retrying while Windows reports it in
|
|
// use.
|
|
func rename(from, to string) error {
|
|
return retryInUse(func() error { return os.Rename(from, to) })
|
|
}
|
|
|
|
// replaceAppFile renames from over to, its sibling, retrying while
|
|
// Windows reports it in use: a read by readAppFile does not, only another
|
|
// program holding to open without sharing its deletion.
|
|
func replaceAppFile(from, to string) error {
|
|
return retryInUse(func() error { return renameOver(from, to) })
|
|
}
|
|
|
|
// readAppFile reads a file of the app, retrying while Windows reports it
|
|
// in use, or reports it missing while it is being replaced: Windows can
|
|
// fail to open the name for a moment while a rename replaces the file.
|
|
func readAppFile(path string) (data []byte, err error) {
|
|
read := func() error {
|
|
data, err = readShared(path)
|
|
return err
|
|
}
|
|
for i := 1; ; i++ {
|
|
err = retryInUse(read)
|
|
if i == 5 || runtime.GOOS != "windows" || !errors.Is(err, fs.ErrNotExist) {
|
|
return data, err
|
|
}
|
|
if !beingReplaced(path) {
|
|
// A replace that finished since the read left the file in place.
|
|
err = retryInUse(read)
|
|
return data, err
|
|
}
|
|
time.Sleep(time.Duration(i) * 10 * time.Millisecond)
|
|
}
|
|
}
|
|
|
|
// beingReplaced reports whether path is being replaced: writeAppFile's
|
|
// temporary sibling (createSibling) stays in place until its rename is
|
|
// done.
|
|
func beingReplaced(path string) bool {
|
|
dir, base := filepath.Split(path)
|
|
entries, _ := os.ReadDir(dir)
|
|
for _, e := range entries {
|
|
if name := e.Name(); strings.HasPrefix(name, "."+base+".") && strings.HasSuffix(name, ".tmp") {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
func retryInUse(f func() error) error {
|
|
for i := 1; ; i++ {
|
|
err := f()
|
|
if err == nil || i == 10 || runtime.GOOS != "windows" ||
|
|
// ERROR_SHARING_VIOLATION, or ERROR_ACCESS_DENIED for a file
|
|
// being deleted or replaced, or a directory holding an open file.
|
|
!errors.Is(err, syscall.Errno(32)) && !errors.Is(err, syscall.Errno(5)) {
|
|
return err
|
|
}
|
|
time.Sleep(time.Duration(i) * 100 * time.Millisecond)
|
|
}
|
|
}
|