update: the install-folder check removes only folders it made, and refuses a folder it can't look at
Installable (the Windows install-location dialog's check) walked up from the chosen folder to the first one exists() said was there, and removed the topmost one below it with os.RemoveAll. exists() was os.Stat == nil, so a folder whose Stat failed for any other reason than "not there" (access denied, a share that doesn't answer) counted as one the check had made, and was removed with everything in it, up to a drive root. Now: - present(p) tells "there", "not there" (fs.ErrNotExist only) and unknown apart; statPath is os.Stat, swapped by tests. - Installable refuses with "can't tell whether X is there" when any folder on the way is unknown, before making anything. It makes the missing folders one by one with os.Mkdir, records only those it made (one made by someone else meanwhile, ErrExist, isn't recorded), and removes them deepest first with os.Remove, so nothing it didn't make, and no folder that got content, is removed. - settle's "is magpie.exe there" before installFresh (which renames over to) refuses too when the answer is unknown; the download runs on. - The other exists() callers were checked: RemoveStaleNew skips a staged file it can't look at, writeCom moves a magpie.com aside only when it is seen there, keepWebView only writes a note, installed() and EnsureCompanion remove nothing. Unknown already meant "leave it" there, so they keep exists(). Tests (internal/update/install_unknown_test.go): a Stat that answers permission denied for an existing ancestor holding a file, for the chosen folder itself holding a file, and for an installed magpie.exe; each asserts the folder and file survive (checked before the error). Plus folders made are removed while existing ones with files stay, and present's three answers. Run at this tree (parent80ccaba64): - mote-run go test -count=1 -timeout 45m ./internal/update ./internal/gui: ok update 141s, ok gui 261s. - gofmt -l internal/: nothing. - mote-run GOOS=windows go vet ./...: ok. GOOS=linux go vet ./... fails in wails' linux files (cgo off when cross-compiling, the same at any tree); GOOS=linux go vet -tags nogui ./... (the code standards' form): ok. GOOS=windows go build: ok. - win-run go test -count=1 ./internal/update on xiaomi-gpu: every test passes but TestPackagedByDpkg, red the same on origin/main80ccaba64(its magpie:amd64.list fixture is an NTFS stream name), filed as #4603. Broken by hand: Installable put back to the walk-up + RemoveAll and settle back to !exists(to), with the injectable stat kept. All three new tests went red: Tools/notes.txt and magpie/settings.json "is gone", and magpie.exe held "MZ new download" instead of "MZ installed". Restored, green. Not tried: the real Windows dialog against an access-denied folder or a dead network share (the permission error is injected); a folder getting content between Mkdir and the cleanup (no hook there, os.Remove refusing a non-empty folder is relied on); the full go test ./... suite.
This commit is contained in:
+23
-1
@@ -2,7 +2,9 @@ package update
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io/fs"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
@@ -169,7 +171,27 @@ func onPath(dir, path string) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
// exists is whether p is there and could be looked at: false too when
|
||||
// that is unknown. Anything that makes, moves over or removes on its
|
||||
// answer asks present instead, which tells unknown apart.
|
||||
func exists(p string) bool {
|
||||
_, err := os.Stat(p)
|
||||
_, err := statPath(p)
|
||||
return err == nil
|
||||
}
|
||||
|
||||
// present is whether p is there: an error when that is unknown (Stat
|
||||
// failed for a reason other than its not being there, such as a folder
|
||||
// that may not be looked into).
|
||||
func present(p string) (bool, error) {
|
||||
_, err := statPath(p)
|
||||
switch {
|
||||
case err == nil:
|
||||
return true, nil
|
||||
case errors.Is(err, fs.ErrNotExist):
|
||||
return false, nil
|
||||
}
|
||||
return false, err
|
||||
}
|
||||
|
||||
// statPath is os.Stat; tests make it fail.
|
||||
var statPath = os.Stat
|
||||
|
||||
+38
-10
@@ -105,6 +105,12 @@ func InstallDir(picked string) string {
|
||||
// Installable says why dir can't be installed in, nil when it can: a
|
||||
// file is written into it and removed, and the folders made for that are
|
||||
// removed again, so a folder looked at and then not chosen isn't left.
|
||||
//
|
||||
// Only the folders this call made itself are removed, one by one from
|
||||
// the deepest up and only while empty, so a folder that was there
|
||||
// already is never taken: a folder whose Stat fails for any reason but
|
||||
// "not there" (no permission to look, a share that doesn't answer) is
|
||||
// unknown, and the check refuses rather than make or remove anything.
|
||||
func Installable(dir string) error {
|
||||
if !filepath.IsAbs(dir) {
|
||||
return fmt.Errorf("%s is not a full path", dir)
|
||||
@@ -112,18 +118,35 @@ func Installable(dir string) error {
|
||||
if downloadFolder(dir) {
|
||||
return ErrDownloadFolder
|
||||
}
|
||||
top := ""
|
||||
for d := filepath.Clean(dir); !exists(d); d = filepath.Dir(d) {
|
||||
top = d
|
||||
if filepath.Dir(d) == d {
|
||||
// the folders missing on the way to dir, outermost first
|
||||
var missing []string
|
||||
for d := filepath.Clean(dir); ; d = filepath.Dir(d) {
|
||||
there, err := present(d)
|
||||
if err != nil {
|
||||
return fmt.Errorf("can't tell whether %s is there: %w", d, err)
|
||||
}
|
||||
if there {
|
||||
break
|
||||
}
|
||||
if filepath.Dir(d) == d {
|
||||
return fmt.Errorf("%s is not there", d)
|
||||
}
|
||||
missing = append([]string{d}, missing...)
|
||||
}
|
||||
if top != "" {
|
||||
defer os.RemoveAll(top)
|
||||
}
|
||||
if err := os.MkdirAll(dir, 0o755); err != nil {
|
||||
return writeErr(err)
|
||||
var made []string
|
||||
defer func() {
|
||||
for i := len(made) - 1; i >= 0; i-- {
|
||||
os.Remove(made[i])
|
||||
}
|
||||
}()
|
||||
for _, d := range missing {
|
||||
if err := os.Mkdir(d, 0o755); err != nil {
|
||||
if errors.Is(err, fs.ErrExist) {
|
||||
continue // made by someone else meanwhile: theirs, kept
|
||||
}
|
||||
return writeErr(err)
|
||||
}
|
||||
made = append(made, d)
|
||||
}
|
||||
f, err := os.CreateTemp(dir, ".magpie-install-*")
|
||||
if err != nil {
|
||||
@@ -215,7 +238,12 @@ func settle(exe string) (string, error) {
|
||||
log.Println("install: keeping the folder chosen:", err)
|
||||
}
|
||||
}
|
||||
if !exists(to) {
|
||||
there, err := present(to)
|
||||
if err != nil {
|
||||
// a file there that can't be looked at would be moved over
|
||||
return "", fmt.Errorf("can't tell whether %s is there: %w", to, err)
|
||||
}
|
||||
if !there {
|
||||
if err := installFresh(exe, to); err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
@@ -0,0 +1,150 @@
|
||||
package update
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"io/fs"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// denyStat has statPath answer "permission denied" for the paths given,
|
||||
// as Windows does for a folder the user may not look into, and a share
|
||||
// that doesn't answer does in its own words: they are there, but Stat
|
||||
// can't say so.
|
||||
func denyStat(t *testing.T, paths ...string) {
|
||||
t.Helper()
|
||||
was := statPath
|
||||
t.Cleanup(func() { statPath = was })
|
||||
statPath = func(p string) (fs.FileInfo, error) {
|
||||
for _, d := range paths {
|
||||
if samePath(p, d) {
|
||||
return nil, &fs.PathError{Op: "stat", Path: p, Err: fs.ErrPermission}
|
||||
}
|
||||
}
|
||||
return os.Stat(p)
|
||||
}
|
||||
}
|
||||
|
||||
// keeps fails the test unless file is still there holding want.
|
||||
func keeps(t *testing.T, file, want string) {
|
||||
t.Helper()
|
||||
b, err := os.ReadFile(file)
|
||||
if err != nil {
|
||||
t.Fatalf("%s is gone: %v", file, err)
|
||||
}
|
||||
if string(b) != want {
|
||||
t.Fatalf("%s holds %q, want %q", file, b, want)
|
||||
}
|
||||
}
|
||||
|
||||
// A folder on the way to the one checked that can't be looked at is
|
||||
// unknown, not missing: the check refuses, and the folder and what is in
|
||||
// it stay. Taken for missing, it was the folder the check had "made",
|
||||
// and was removed with everything in it.
|
||||
func TestInstallableKeepsAnAncestorItCantLookAt(t *testing.T) {
|
||||
newInstallFixture(t)
|
||||
tmp := t.TempDir()
|
||||
theirs := filepath.Join(tmp, "Tools")
|
||||
file := filepath.Join(theirs, "notes.txt")
|
||||
if err := os.MkdirAll(theirs, 0o755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.WriteFile(file, []byte("mine"), 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
denyStat(t, theirs)
|
||||
err := Installable(filepath.Join(theirs, "new", "magpie"))
|
||||
keeps(t, file, "mine")
|
||||
if err == nil || !errors.Is(err, fs.ErrPermission) {
|
||||
t.Fatalf("a folder that can't be looked at: %v", err)
|
||||
}
|
||||
if _, err := os.Stat(filepath.Join(theirs, "new")); !errors.Is(err, fs.ErrNotExist) {
|
||||
t.Fatalf("the check made a folder under one it couldn't look at: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
// The folder checked itself, there with the user's files, that can't be
|
||||
// looked at: refused, and nothing in it removed.
|
||||
func TestInstallableKeepsTheFolderItCantLookAt(t *testing.T) {
|
||||
newInstallFixture(t)
|
||||
tmp := t.TempDir()
|
||||
dir := filepath.Join(tmp, "magpie")
|
||||
file := filepath.Join(dir, "settings.json")
|
||||
if err := os.MkdirAll(dir, 0o755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.WriteFile(file, []byte("{}"), 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
denyStat(t, dir)
|
||||
err := Installable(dir)
|
||||
keeps(t, file, "{}")
|
||||
if err == nil {
|
||||
t.Fatal("a folder that can't be looked at accepted")
|
||||
}
|
||||
}
|
||||
|
||||
// Only the folders the check made are removed: every folder that was
|
||||
// there before stays, with what is in it.
|
||||
func TestInstallableRemovesOnlyWhatItMade(t *testing.T) {
|
||||
newInstallFixture(t)
|
||||
tmp := t.TempDir()
|
||||
keep := filepath.Join(tmp, "keep.txt")
|
||||
if err := os.WriteFile(keep, []byte("x"), 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := Installable(filepath.Join(tmp, "a", "b", "magpie")); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
keeps(t, keep, "x")
|
||||
if _, err := os.Stat(filepath.Join(tmp, "a")); !errors.Is(err, fs.ErrNotExist) {
|
||||
t.Fatalf("the folders made were left: %v", err)
|
||||
}
|
||||
b := filepath.Join(tmp, "a", "b")
|
||||
if err := os.MkdirAll(b, 0o755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.WriteFile(filepath.Join(b, "user.txt"), []byte("u"), 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := Installable(filepath.Join(b, "magpie")); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
keeps(t, filepath.Join(b, "user.txt"), "u")
|
||||
if _, err := os.Stat(filepath.Join(b, "magpie")); !errors.Is(err, fs.ErrNotExist) {
|
||||
t.Fatalf("the folder made was left: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
// An installed magpie.exe that can't be looked at isn't moved over by a
|
||||
// fresh install: settle says it can't tell, and the download runs on.
|
||||
func TestSettleLeavesAnExeItCantLookAt(t *testing.T) {
|
||||
f := newInstallFixture(t)
|
||||
if err := os.MkdirAll(filepath.Dir(f.to), 0o755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.WriteFile(f.to, []byte("MZ installed"), 0o755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
denyStat(t, f.to)
|
||||
to, err := settle(f.exe)
|
||||
keeps(t, f.to, "MZ installed")
|
||||
if err == nil || to != "" {
|
||||
t.Fatalf("settle = %q, %v; want an error", to, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPresent(t *testing.T) {
|
||||
tmp := t.TempDir()
|
||||
if ok, err := present(tmp); !ok || err != nil {
|
||||
t.Fatalf("present(there) = %v, %v", ok, err)
|
||||
}
|
||||
if ok, err := present(filepath.Join(tmp, "no")); ok || err != nil {
|
||||
t.Fatalf("present(missing) = %v, %v", ok, err)
|
||||
}
|
||||
denyStat(t, tmp)
|
||||
if ok, err := present(tmp); ok || err == nil {
|
||||
t.Fatalf("present(unknown) = %v, %v", ok, err)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user