From a6c438782e73fa0e347228f30f33cf6b8d482d4b Mon Sep 17 00:00:00 2001 From: Ophestra Date: Sun, 3 Aug 2025 15:35:32 +0900 Subject: [PATCH] container/mount: mount data escape helper function Will be useful for overlayfs and any future use case that requires formatting stuff into mount data. Signed-off-by: Ophestra --- container/mount.go | 18 +++++++++++++++ container/mount_test.go | 49 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 67 insertions(+) create mode 100644 container/mount_test.go diff --git a/container/mount.go b/container/mount.go index 7ee614b..65c7a10 100644 --- a/container/mount.go +++ b/container/mount.go @@ -5,6 +5,7 @@ import ( "fmt" "os" "path/filepath" + "strings" . "syscall" "hakurei.app/container/vfs" @@ -183,3 +184,20 @@ func parentPerm(perm os.FileMode) os.FileMode { } return os.FileMode(pperm) } + +// escapeMountDataSegment escapes a string for formatting into the data argument of mount. +func escapeMountDataSegment(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) +} diff --git a/container/mount_test.go b/container/mount_test.go new file mode 100644 index 0000000..555ede1 --- /dev/null +++ b/container/mount_test.go @@ -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 TestEscapeMountDataSegment(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 := escapeMountDataSegment(tc.s); got != tc.want { + t.Errorf("escapeMountDataSegment: %s, want %s", got, tc.want) + } + }) + } +}