engine: resources: Allow symbolic modes for missing files

In 83a747794e a bug was introduced with
the implementation of symbolic modes, that would prevent a file resource
from passing the Validate step if you were using a symbolic mode, and
the file didn't already exist. If you didn't use symbolic modes and
those files weren't absent, then you wouldn't have noticed.

It might be worth looking into the API for symbolic parsing as well.
This commit is contained in:
James Shubin
2024-08-06 13:24:25 -04:00
parent 4abcd9cf01
commit 0354082f89

View File

@@ -239,18 +239,19 @@ func (obj *FileRes) isDir() bool {
// the case where the mode is not specified. The caller should check obj.Mode is // the case where the mode is not specified. The caller should check obj.Mode is
// not empty. // not empty.
func (obj *FileRes) mode() (os.FileMode, error) { func (obj *FileRes) mode() (os.FileMode, error) {
// First check if this is an octal number.
if n, err := strconv.ParseInt(obj.Mode, 8, 32); err == nil { if n, err := strconv.ParseInt(obj.Mode, 8, 32); err == nil {
return os.FileMode(n), nil return os.FileMode(n), nil
} }
// Try parsing symbolically by first getting the files current mode. // Try parsing symbolically by first getting the files current mode.
stat, err := os.Stat(obj.getPath()) from := os.FileMode(0) // default
if err != nil { if stat, err := os.Stat(obj.getPath()); err == nil {
return os.FileMode(0), errwrap.Wrapf(err, "failed to get the current file mode") from = stat.Mode()
} }
modes := strings.Split(obj.Mode, ",") modes := strings.Split(obj.Mode, ",")
m, err := engineUtil.ParseSymbolicModes(modes, stat.Mode(), FileModeAllowAssign) m, err := engineUtil.ParseSymbolicModes(modes, from, FileModeAllowAssign)
if err != nil { if err != nil {
return os.FileMode(0), errwrap.Wrapf(err, "mode should be an octal number or symbolic mode (%s)", obj.Mode) return os.FileMode(0), errwrap.Wrapf(err, "mode should be an octal number or symbolic mode (%s)", obj.Mode)
} }