Skip to content

Commit

Permalink
incus-agent: Add support for template ownership/mode
Browse files Browse the repository at this point in the history
Signed-off-by: Stéphane Graber <[email protected]>
  • Loading branch information
stgraber committed Feb 6, 2024
1 parent 62fdb1f commit bf61cd6
Showing 1 changed file with 42 additions and 1 deletion.
43 changes: 42 additions & 1 deletion cmd/incus-agent/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ package main
import (
"fmt"
"io"
"io/fs"
"os"
"path/filepath"
"strconv"

"gopkg.in/yaml.v2"

Expand Down Expand Up @@ -52,6 +54,39 @@ func templatesApply(path string) ([]string, error) {
return fmt.Errorf("Failed to create template file: %w", err)
}
} else {
// UID and GID
fileUID := int64(0)
fileGID := int64(0)

if tpl.UID != "" {
id, err := strconv.ParseInt(tpl.UID, 10, 64)
if err != nil {
return fmt.Errorf("Bad file UID %q for %q: %w", tpl.UID, tplPath, err)
}

fileUID = id
}

if tpl.GID != "" {
id, err := strconv.ParseInt(tpl.GID, 10, 64)
if err != nil {
return fmt.Errorf("Bad file GID %q for %q: %w", tpl.GID, tplPath, err)
}

fileGID = id
}

// Mode
fileMode := fs.FileMode(0644)
if tpl.Mode != "" {
mode, err := strconv.ParseInt(tpl.Mode, 0, 0)
if err != nil {
return fmt.Errorf("Bad mode %q for %q: %w", tpl.Mode, tplPath, err)
}

fileMode = os.FileMode(mode) & os.ModePerm
}

// Create the directories leading to the file.
err := os.MkdirAll(filepath.Dir(tplPath), 0755)
if err != nil {
Expand All @@ -64,8 +99,14 @@ func templatesApply(path string) ([]string, error) {
return err
}

// Fix ownership.
err = w.Chown(int(fileUID), int(fileGID))
if err != nil {
return err
}

// Fix mode.
err = w.Chmod(0644)
err = w.Chmod(fileMode)
if err != nil {
return err
}
Expand Down

0 comments on commit bf61cd6

Please sign in to comment.