Skip to content

Commit

Permalink
internal/exec/*: preserve special mode bits for a given file/dir
Browse files Browse the repository at this point in the history
This allows Ignition to preserve the special mode bits for specs >=
3.4.0

Fixes: coreos#1301
  • Loading branch information
sohankunkerkar committed Mar 3, 2022
1 parent 682101e commit 59a7954
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
6 changes: 6 additions & 0 deletions internal/exec/stages/files/filesystemEntries.go
Expand Up @@ -393,6 +393,9 @@ func (s stage) getOrderedCreationList(config types.Config) ([]filesystemEntry, e
// Map from paths in the config to where they resolve for duplicate checking
paths := map[string]string{}
for _, d := range config.Storage.Directories {
if d.Mode != nil {
d.Mode = cutil.IntToPtr(int(util.ToFileMode(d.Mode)))
}
path, err := s.JoinPath(d.Path)
if err != nil {
return nil, err
Expand All @@ -407,6 +410,9 @@ func (s stage) getOrderedCreationList(config types.Config) ([]filesystemEntry, e
}

for _, f := range config.Storage.Files {
if f.Mode != nil {
f.Mode = cutil.IntToPtr(int(util.ToFileMode(f.Mode)))
}
path, err := s.JoinPath(f.Path)
if err != nil {
return nil, err
Expand Down
20 changes: 20 additions & 0 deletions internal/exec/util/file.go
Expand Up @@ -25,6 +25,7 @@ import (
"os"
"path/filepath"
"strconv"
"syscall"

cutil "github.com/coreos/ignition/v2/config/util"
"github.com/coreos/ignition/v2/config/v3_4_experimental/types"
Expand Down Expand Up @@ -169,6 +170,25 @@ func (u Util) SetPermissions(mode *int, node types.Node) error {
return nil
}

// ToFileMode converts Go permission bits to POSIX permission bits.
func ToFileMode(m *int) os.FileMode {
mode := uint32(int(*m))
res := os.FileMode(mode & 0000777)

if mode&syscall.S_ISGID != 0 {
res |= os.ModeSetgid

}
if mode&syscall.S_ISUID != 0 {
res |= os.ModeSetuid
}
if mode&syscall.S_ISVTX != 0 {
res |= os.ModeSticky
}

return res
}

// PerformFetch performs a fetch operation generated by PrepareFetch, retrieving
// the file and writing it to disk. Any encountered errors are returned.
func (u Util) PerformFetch(f FetchOp) error {
Expand Down

0 comments on commit 59a7954

Please sign in to comment.