container/mount: mount data escape helper function
All checks were successful
Test / Create distribution (push) Successful in 33s
Test / Sandbox (push) Successful in 2m0s
Test / Hakurei (push) Successful in 2m56s
Test / Sandbox (race detector) (push) Successful in 3m57s
Test / Hpkg (push) Successful in 4m7s
Test / Hakurei (race detector) (push) Successful in 4m38s
Test / Flake checks (push) Successful in 1m18s
All checks were successful
Test / Create distribution (push) Successful in 33s
Test / Sandbox (push) Successful in 2m0s
Test / Hakurei (push) Successful in 2m56s
Test / Sandbox (race detector) (push) Successful in 3m57s
Test / Hpkg (push) Successful in 4m7s
Test / Hakurei (race detector) (push) Successful in 4m38s
Test / Flake checks (push) Successful in 1m18s
For formatting user-supplied path strings into overlayfs mount data. Signed-off-by: Ophestra <cat@gensokyo.uk>
This commit is contained in:
parent
347a79df72
commit
ff66296378
@ -5,6 +5,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"strings"
|
||||||
. "syscall"
|
. "syscall"
|
||||||
|
|
||||||
"hakurei.app/container/vfs"
|
"hakurei.app/container/vfs"
|
||||||
@ -183,3 +184,20 @@ func parentPerm(perm os.FileMode) os.FileMode {
|
|||||||
}
|
}
|
||||||
return os.FileMode(pperm)
|
return os.FileMode(pperm)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// escapeOverlayDataSegment escapes a string for formatting into the data argument of an overlay mount call.
|
||||||
|
func escapeOverlayDataSegment(s string) string {
|
||||||
|
if s == zeroString {
|
||||||
|
return zeroString
|
||||||
|
}
|
||||||
|
|
||||||
|
if f := strings.SplitN(s, "\x00", 2); len(f) > 0 {
|
||||||
|
s = f[0]
|
||||||
|
}
|
||||||
|
|
||||||
|
return strings.NewReplacer(
|
||||||
|
`\`, `\\`,
|
||||||
|
`,`, `\,`,
|
||||||
|
`:`, `\:`,
|
||||||
|
).Replace(s)
|
||||||
|
}
|
||||||
|
49
container/mount_test.go
Normal file
49
container/mount_test.go
Normal file
@ -0,0 +1,49 @@
|
|||||||
|
package container
|
||||||
|
|
||||||
|
import (
|
||||||
|
"os"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestParentPerm(t *testing.T) {
|
||||||
|
testCases := []struct {
|
||||||
|
perm os.FileMode
|
||||||
|
want os.FileMode
|
||||||
|
}{
|
||||||
|
{0755, 0755},
|
||||||
|
{0750, 0750},
|
||||||
|
{0705, 0705},
|
||||||
|
{0700, 0700},
|
||||||
|
{050, 0750},
|
||||||
|
{05, 0705},
|
||||||
|
{0, 0700},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tc := range testCases {
|
||||||
|
t.Run(tc.perm.String(), func(t *testing.T) {
|
||||||
|
if got := parentPerm(tc.perm); got != tc.want {
|
||||||
|
t.Errorf("parentPerm: %#o, want %#o", got, tc.want)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestEscapeOverlayDataSegment(t *testing.T) {
|
||||||
|
testCases := []struct {
|
||||||
|
name string
|
||||||
|
s string
|
||||||
|
want string
|
||||||
|
}{
|
||||||
|
{"zero", zeroString, zeroString},
|
||||||
|
{"multi", `\\\:,:,\\\`, `\\\\\\\:\,\:\,\\\\\\`},
|
||||||
|
{"bwrap", `/path :,\`, `/path \:\,\\`},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tc := range testCases {
|
||||||
|
t.Run(tc.name, func(t *testing.T) {
|
||||||
|
if got := escapeOverlayDataSegment(tc.s); got != tc.want {
|
||||||
|
t.Errorf("escapeOverlayDataSegment: %s, want %s", got, tc.want)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user