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 29, 2022
1 parent 682101e commit 0172ddb
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
4 changes: 2 additions & 2 deletions internal/exec/stages/files/filesystemEntries.go
Expand Up @@ -296,7 +296,7 @@ func (tmp fileEntry) create(l *log.Logger, u util.Util) error {
return fmt.Errorf("failed to create file %q: %v", op.Node.Path, err)
}
}
if err := u.SetPermissions(f.Mode, f.Node); err != nil {
if err := u.SetPermissions(cutil.IntToPtr(int(util.ToFileMode(f.Mode))), f.Node); err != nil {
return fmt.Errorf("error setting file permissions for %s: %v", f.Path, err)
}
return nil
Expand All @@ -323,7 +323,7 @@ func (tmp dirEntry) create(l *log.Logger, u util.Util) error {
return fmt.Errorf("error creating directory %s: A non-directory already exists and overwrite is false", d.Path)
}

if err := u.SetPermissions(d.Mode, d.Node); err != nil {
if err := u.SetPermissions(cutil.IntToPtr(int(util.ToFileMode(d.Mode))), d.Node); err != nil {
return fmt.Errorf("error setting directory permissions for %s: %v", d.Path, err)
}
return nil
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(*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 0172ddb

Please sign in to comment.