feat(dataroot): guard data folder writes and migrations
This commit is contained in:
1 parent
2ffddf94b4
commit
6dcf33f71b
38 files changed
+1104
-128
No files matched your search
+18
-5
@@ -12,7 +12,7 @@ import (
|
||||
"strings"
|
||||
|
||||
"github.com/Nwflower/reims-go/internal/config"
|
||||
"github.com/Nwflower/reims-go/internal/datamigrate"
|
||||
"github.com/Nwflower/reims-go/internal/dataroot"
|
||||
"github.com/Nwflower/reims-go/internal/driver"
|
||||
"github.com/Nwflower/reims-go/internal/host"
|
||||
"github.com/Nwflower/reims-go/internal/i18n"
|
||||
@@ -70,10 +70,8 @@ func run(args []string) (i18n.Locale, error) {
|
||||
if *dataRootFlag != "" {
|
||||
cfg.DataRoot = *dataRootFlag
|
||||
}
|
||||
// Same data-folder upgrade the GUI runs on startup.
|
||||
if _, err := datamigrate.Run(cfg.DataRoot, version.Version); err != nil {
|
||||
fmt.Fprintln(os.Stderr, "warning:", err)
|
||||
}
|
||||
// Same check and upgrade the GUI runs on startup (internal/dataroot).
|
||||
root := dataroot.Open(cfg.DataRoot, version.Version)
|
||||
// Load enabled static language packs from <DataRoot>/plugins before resolving
|
||||
// effective locale so "auto" and custom --lang tags (e.g. ja-JP, zh-TW) work in CLI.
|
||||
plugin.LoadStaticLocalePacks(cfg.DataRoot)
|
||||
@@ -89,6 +87,11 @@ func run(args []string) (i18n.Locale, error) {
|
||||
printUsage(loc)
|
||||
return loc, nil
|
||||
}
|
||||
// A folder open read-only (written by a newer release, damaged version
|
||||
// record, failed upgrade) only serves commands that never write to it.
|
||||
if root.ReadOnly && !readOnlySafeCommand(rest[0]) {
|
||||
return loc, dataroot.Error(loc, root)
|
||||
}
|
||||
|
||||
switch rest[0] {
|
||||
case "doctor":
|
||||
@@ -633,3 +636,13 @@ func resolveCLIDiskPath(d reims.VMDetail) string {
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
// readOnlySafeCommand reports whether cmd runs without writing to the data
|
||||
// folder, so it may run while the folder is open read-only.
|
||||
func readOnlySafeCommand(cmd string) bool {
|
||||
switch cmd {
|
||||
case "version", "-v", "--version", "help", "-h", "--help", "doctor":
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
Reference in new issue
Block a user