Skip to content

Commit

Permalink
Apply review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisburr committed Oct 5, 2020
1 parent e5a53e3 commit a7e6020
Showing 1 changed file with 26 additions and 9 deletions.
35 changes: 26 additions & 9 deletions internal/pkg/buildcfg/confgen/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,31 @@ type Define struct {

// WriteLine writes a line of configuration.
func (d Define) WriteLine() (s string) {
if len(d.Words) == 3 && len(d.Words[2]) == 1 {
s = "const " + d.Words[1] + " = " + d.Words[2]
} else {
s = d.Words[2]
s = d.Words[2]
if len(d.Words) > 3 {
for _, w := range d.Words[3:] {
s += " + " + w
}
s = "var " + d.Words[1] + " = RelocatePath(" + s + ")"
}

return s
// Apply runtime relocation to some variables
switch d.Words[1] {
case
"BINDIR",
"LIBEXECDIR",
"SYSCONFDIR",
"SESSIONDIR",
"SINGULARITY_CONFDIR",
"PLUGIN_ROOTDIR":
return "var " + d.Words[1] + " = RelocatePath(" + s + ")"
default:
var varType = "const"
// Some variables are defined relative to others and cannot be const
if strings.Contains(s, "SINGULARITY_CONFDIR") {
varType = "var"
}
return varType + " " + d.Words[1] + " = " + s
}
}

var confgenTemplate = template.Must(template.New("").Parse(`// Code generated by go generate; DO NOT EDIT.
Expand All @@ -53,6 +67,11 @@ import (
)
func RelocatePath(original string) (string) {
// For security, never allow relocation when built with SetUID support
if SINGULARITY_SUID_INSTALL == 1 {
return original
}
if ! strings.HasPrefix(original, "{{.Prefix}}") {
return original
}
Expand All @@ -70,10 +89,8 @@ func RelocatePath(original string) (string) {
case "starter":
// PREFIX/libexec/singularity/bin/starter
prefix = filepath.Dir(filepath.Dir(filepath.Dir(prefix)))
case "bash_completion":
return original
default:
panic("Unrecognized executable: " + executablePath)
return original
}
relativePath, err := filepath.Rel("{{.Prefix}}", original)
Expand Down

0 comments on commit a7e6020

Please sign in to comment.