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 Apr 10, 2022
1 parent 682101e commit 44f6bbc
Showing 1 changed file with 20 additions and 2 deletions.
22 changes: 20 additions & 2 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 @@ -152,8 +153,7 @@ func (u Util) WriteLink(s types.Link) error {

func (u Util) SetPermissions(mode *int, node types.Node) error {
if mode != nil {
mode := os.FileMode(*mode)
if err := os.Chmod(node.Path, mode); err != nil {
if err := os.Chmod(node.Path, toFileMode(*mode)); err != nil {
return fmt.Errorf("failed to change mode of %s: %v", node.Path, err)
}
}
Expand All @@ -169,6 +169,24 @@ 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(m)
res := os.FileMode(mode & 0777)

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 44f6bbc

Please sign in to comment.