diff --git a/docs/subsystems/sync-backup.md b/docs/subsystems/sync-backup.md index 216d7d90d..10b1a5804 100644 --- a/docs/subsystems/sync-backup.md +++ b/docs/subsystems/sync-backup.md @@ -49,6 +49,16 @@ can replace it with this computer's setup, keeping the old file first. HTML and XML are not offered for replacement. GitHub diagnostics name the repository and `magpie github upload`, rather than WebDAV's recovery path. +A local file that is there but can't be read is unknown, never empty or the +defaults. `backup.Collect` stops on an unreadable providers.json (with its groups +and order), settings.json (`settings.LoadChecked`: unreadable, or +not parsing with no good `.bak`) or library.json, and on a gateway key +store that fails to export. A backup, an export and a sync all end with that +error, nothing sealed or pushed; the sync's status shows it. A missing or +empty settings.json is still none (`TestCollectStopsOnUnreadableSettings`, +`TestSyncStopsOnUnreadableProviders`, `TestSyncStopsOnUnreadableSettings`, +#2269). + Switching services keeps all inactive kinds in `Config.Servers`. `Other` remains the most recently left server for compatibility with older saved configurations and clients. Only the active server is used. Empty credential diff --git a/internal/backup/backup.go b/internal/backup/backup.go index 60e9b8674..e1b7ce633 100644 --- a/internal/backup/backup.go +++ b/internal/backup/backup.go @@ -208,8 +208,14 @@ func Collect(keys bool, app string) (Bundle, error) { searches = append(searches, a) } b.Searches = &searches + // a settings.json that can't be read stops it too: carried as the + // defaults, it would put them over the settings where it is put back, + // or on every other computer sync mirrors it to (#2269) if _, err := os.Stat(settings.Path()); err == nil { - s := settings.Load() + s, err := settings.LoadChecked() + if err != nil { + return b, err + } if !keys { s.LANKey, s.LANKeyID = "", "" s.OTel.Headers = nil diff --git a/internal/backup/settings_unreadable_test.go b/internal/backup/settings_unreadable_test.go new file mode 100644 index 000000000..c424ebeb6 --- /dev/null +++ b/internal/backup/settings_unreadable_test.go @@ -0,0 +1,40 @@ +package backup + +import ( + "os" + "strings" + "testing" + + "github.com/yetone/magpie/internal/lastgood" + "github.com/yetone/magpie/internal/settings" +) + +// A settings.json that is there but can't be read stops the backup, as an +// unreadable providers.json does: carried as the defaults, it would put them +// over the settings wherever the backup is put back (#2269). +func TestCollectStopsOnUnreadableSettings(t *testing.T) { + home(t) + if err := settings.Save(settings.Settings{Theme: "dark", Lang: "ja", TextSize: 125}); err != nil { + t.Fatal(err) + } + whole, err := os.ReadFile(settings.Path()) + if err != nil { + t.Fatal(err) + } + if err := os.WriteFile(settings.Path(), whole[:len(whole)/2], 0o600); err != nil { + t.Fatal(err) + } + os.Remove(lastgood.Bak(settings.Path())) + b, err := Collect(true, "test") + if err == nil || !strings.Contains(err.Error(), "settings") { + t.Fatalf("Collect read a settings.json that can't be read as settings: %v %+v", err, b.Settings) + } + + // a missing file is no settings, not an error + if err := os.Remove(settings.Path()); err != nil { + t.Fatal(err) + } + if b, err := Collect(true, "test"); err != nil || b.Settings != nil { + t.Fatalf("no settings.json: %v %+v", err, b.Settings) + } +} diff --git a/internal/davsync/unreadable_test.go b/internal/davsync/unreadable_test.go index b9521eb8a..0a517bbf4 100644 --- a/internal/davsync/unreadable_test.go +++ b/internal/davsync/unreadable_test.go @@ -10,7 +10,9 @@ import ( "testing" "github.com/yetone/magpie/internal/backup" + "github.com/yetone/magpie/internal/lastgood" "github.com/yetone/magpie/internal/provider" + "github.com/yetone/magpie/internal/settings" ) // A providers.json that can't be read is never pushed as no providers: a @@ -81,3 +83,73 @@ func TestSyncStopsOnUnreadableProviders(t *testing.T) { t.Fatalf("b lost its providers to a's broken file: %v", got) } } + +// A settings.json that can't be read is never pushed as the defaults: a +// sync stops on it and says so, the server keeps the settings, and another +// computer mirroring the server keeps its own (#2269). +func TestSyncStopsOnUnreadableSettings(t *testing.T) { + fake := &fakeDAV{files: map[string][]byte{}, etags: map[string]string{}, dirs: map[string]bool{"/dav": true}} + srv := httptest.NewServer(fake) + defer srv.Close() + ctx := context.Background() + cfg := Config{URL: srv.URL + "/dav/", User: "me", Password: "pw", Passphrase: "correct horse", Keys: true} + + a, b := newComputer(t), newComputer(t) + b.use(t) + if err := Configure(cfg); err != nil { + t.Fatal(err) + } + a.use(t) + if err := settings.Save(settings.Settings{Theme: "dark", Lang: "ja", TextSize: 125}); err != nil { + t.Fatal(err) + } + if err := Configure(cfg); err != nil { + t.Fatal(err) + } + if err := Now(ctx); err != nil { + t.Fatal(err) + } + b.use(t) + if err := Now(ctx); err != nil { + t.Fatal(err) + } + if s := settings.Load(); s.Theme != "dark" || s.Lang != "ja" { + t.Fatalf("b's settings: %s %s", s.Theme, s.Lang) + } + + // a's file is cut short, with no last good generation to fall back to + a.use(t) + whole, err := os.ReadFile(settings.Path()) + if err != nil { + t.Fatal(err) + } + broken := whole[:len(whole)/2] + if err := os.WriteFile(settings.Path(), broken, 0o600); err != nil { + t.Fatal(err) + } + os.Remove(lastgood.Bak(settings.Path())) + puts := fake.puts + if err := Now(ctx); err == nil { + t.Fatal("a synced a broken settings.json") + } + if fake.puts != puts { + t.Fatal("a pushed a broken settings.json") + } + if after, _ := os.ReadFile(settings.Path()); !bytes.Equal(after, broken) { + t.Fatal("a's broken settings.json was written over") + } + if st := Status(); st.Error == "" { + t.Fatal("a's sync status doesn't say it stopped") + } + remote, err := backup.Open(fake.files["/dav/magpie/magpie.magpie-backup"], "correct horse") + if err != nil || remote.Settings == nil || remote.Settings.Theme != "dark" { + t.Fatalf("on the server: %v %+v", err, remote.Settings) + } + b.use(t) + if err := Now(ctx); err != nil { + t.Fatal(err) + } + if s := settings.Load(); s.Theme != "dark" || s.Lang != "ja" || s.TextSize != 125 { + t.Fatalf("b lost its settings to a's broken file: %s %s %d", s.Theme, s.Lang, s.TextSize) + } +} diff --git a/internal/library/carry.go b/internal/library/carry.go index 16c7a4f16..bab5893a9 100644 --- a/internal/library/carry.go +++ b/internal/library/carry.go @@ -55,8 +55,10 @@ const ( func Collect() (*Bundle, error) { mu.Lock() defer mu.Unlock() - if _, err := os.Stat(path()); err != nil { + if _, err := os.Stat(path()); errors.Is(err, fs.ErrNotExist) { return nil, nil + } else if err != nil { // there, but unknown: never carried as no library (#2269) + return nil, err } l, err := load() if err != nil {