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.
93 lines
2.9 KiB
Go
93 lines
2.9 KiB
Go
//go:build windows && (amd64 || arm64)
|
|
|
|
package windows
|
|
|
|
import (
|
|
"errors"
|
|
"os"
|
|
"syscall"
|
|
"time"
|
|
"unsafe"
|
|
)
|
|
|
|
var procReplaceFileW = kernel32.NewProc("ReplaceFileW")
|
|
|
|
const (
|
|
replacefileIgnoreMergeErrors = 0x2
|
|
replacefileIgnoreACLErrors = 0x4
|
|
|
|
errorUnableToRemoveReplaced = syscall.Errno(1175)
|
|
errorUnableToMoveReplacement = syscall.Errno(1176)
|
|
errorUnableToMoveReplacement2 = syscall.Errno(1177)
|
|
errorSharingViolation = syscall.Errno(32)
|
|
errorLockViolation = syscall.Errno(33)
|
|
)
|
|
|
|
// replaceFileW calls ReplaceFileW without a backup file. Tests replace it
|
|
// to fail as the system does.
|
|
var replaceFileW = func(target, replacement *uint16) error {
|
|
ok, _, err := procReplaceFileW.Call(uintptr(unsafe.Pointer(target)), uintptr(unsafe.Pointer(replacement)), 0,
|
|
replacefileIgnoreMergeErrors|replacefileIgnoreACLErrors, 0, 0)
|
|
if ok != 0 {
|
|
return nil
|
|
}
|
|
return err
|
|
}
|
|
|
|
// ReplaceFile replaces target with ReplaceFileW, which keeps the target's
|
|
// security descriptor, attributes, alternate data streams and creation
|
|
// time, as Office and NSDocument's replacement do. Another program holding
|
|
// the target open without sharing deletes (an indexer, an antivirus, a
|
|
// cloud client) gets a moment to let go.
|
|
func (*Backend) ReplaceFile(target, replacement string) error {
|
|
if _, err := os.Lstat(target); os.IsNotExist(err) {
|
|
return os.Rename(replacement, target)
|
|
}
|
|
t, err := syscall.UTF16PtrFromString(target)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
r, err := syscall.UTF16PtrFromString(replacement)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
for attempt := 0; ; attempt++ {
|
|
callErr := replaceFileW(t, r)
|
|
if callErr == nil {
|
|
return nil
|
|
}
|
|
errno, _ := callErr.(syscall.Errno)
|
|
if errno == errorUnableToMoveReplacement || errno == errorUnableToMoveReplacement2 {
|
|
// Without a backup file, ReplaceFileW has deleted the target
|
|
// (1176) or moved it to a name of its own (1177), and the
|
|
// replacement, which holds the new contents, keeps its name.
|
|
return moveReplacement(replacement, target)
|
|
}
|
|
if !busy(errno) || attempt == 9 {
|
|
return &os.LinkError{Op: "replace", Old: replacement, New: target, Err: callErr}
|
|
}
|
|
time.Sleep(50 * time.Millisecond)
|
|
}
|
|
}
|
|
|
|
// moveReplacement renames replacement to target once ReplaceFileW has
|
|
// stopped halfway, waiting, as ReplaceFile does, for a program holding the
|
|
// replacement open, which is what usually stops it.
|
|
func moveReplacement(replacement, target string) error {
|
|
for attempt := 0; ; attempt++ {
|
|
err := os.Rename(replacement, target)
|
|
var errno syscall.Errno
|
|
if err == nil || !errors.As(err, &errno) || !busy(errno) || attempt == 9 {
|
|
return err
|
|
}
|
|
time.Sleep(50 * time.Millisecond)
|
|
}
|
|
}
|
|
|
|
// busy reports whether another program holding a file open may be why an
|
|
// operation on it failed.
|
|
func busy(errno syscall.Errno) bool {
|
|
return errno == errorSharingViolation || errno == errorLockViolation ||
|
|
errno == syscall.ERROR_ACCESS_DENIED || errno == errorUnableToRemoveReplaced
|
|
}
|