diff options
Diffstat (limited to 'internal/admin/yaml_util.go')
| -rw-r--r-- | internal/admin/yaml_util.go | 21 |
1 files changed, 21 insertions, 0 deletions
diff --git a/internal/admin/yaml_util.go b/internal/admin/yaml_util.go index 8cd3252..e1d4942 100644 --- a/internal/admin/yaml_util.go +++ b/internal/admin/yaml_util.go @@ -157,6 +157,21 @@ func snapshotFile(path string) ([]byte, error) { return os.ReadFile(path) } +// snapshotBefore reads a file's content for an undo record and reports +// whether it existed. A missing file returns (nil, false, nil) — a create. A +// read failure on an EXISTING file returns an error: treating it as a create +// would make the undo record delete the real file, so the caller must abort. +func snapshotBefore(path string) ([]byte, bool, error) { + data, err := os.ReadFile(path) + if err == nil { + return data, true, nil + } + if os.IsNotExist(err) { + return nil, false, nil + } + return nil, false, err +} + func findEntityPath(dataDir, subdir, id string) (string, error) { _, path, err := readYAMLFile(dataDir, subdir, id) if err == nil { @@ -167,6 +182,9 @@ func findEntityPath(dataDir, subdir, id string) (string, error) { } func moveEntityFile(dataDir, subdir, id, targetDir string) (string, string, []byte, error) { + if !validEntityID(id) { + return "", "", nil, fmt.Errorf("invalid id: %q", id) + } if targetDir != "" && !validEntityID(targetDir) { return "", "", nil, fmt.Errorf("invalid directory: %q", targetDir) } @@ -219,6 +237,9 @@ func createEntityDir(dataDir, subdir, name string) error { } func renameEntityFile(dataDir, subdir, oldID, newID string) (string, string, []byte, error) { + if !validEntityID(oldID) { + return "", "", nil, fmt.Errorf("invalid id: %q", oldID) + } if !validEntityID(newID) { return "", "", nil, fmt.Errorf("invalid new id: %q", newID) } |
