aboutsummaryrefslogtreecommitdiffhomepage
path: root/vfs/mangle.go
diff options
context:
space:
mode:
authorOphestra <cat@gensokyo.uk>2026-03-17 15:16:46 +0900
committerOphestra <cat@gensokyo.uk>2026-03-17 15:30:30 +0900
commite9a72490db44426605debd3cc756a7924931d706 (patch)
tree1e8f8099c46aa16023367404ec2c9fb769db2c8e /vfs/mangle.go
parent0a12d456ce5869042dc06c43c360c43cab942d6b (diff)
vfs: move from container
This package is not container-specific. Signed-off-by: Ophestra <cat@gensokyo.uk>
Diffstat (limited to 'vfs/mangle.go')
-rw-r--r--vfs/mangle.go32
1 files changed, 32 insertions, 0 deletions
diff --git a/vfs/mangle.go b/vfs/mangle.go
new file mode 100644
index 00000000..af7a0463
--- /dev/null
+++ b/vfs/mangle.go
@@ -0,0 +1,32 @@
+package vfs
+
+import "strings"
+
+// Unmangle reverses mangling of strings done by the kernel. Its behaviour is
+// consistent with the equivalent function in util-linux.
+func Unmangle(s string) string {
+ if !strings.ContainsRune(s, '\\') {
+ return s
+ }
+
+ v := make([]byte, len(s))
+ var (
+ j int
+ c byte
+ )
+ for i := 0; i < len(s); i++ {
+ c = s[i]
+ if c == '\\' && len(s) > i+3 &&
+ (s[i+1] == '0' || s[i+1] == '1') &&
+ (s[i+2] >= '0' && s[i+2] <= '7') &&
+ (s[i+3] >= '0' && s[i+3] <= '7') {
+ c = ((s[i+1] - '0') << 6) |
+ ((s[i+2] - '0') << 3) |
+ (s[i+3] - '0')
+ i += 3
+ }
+ v[j] = c
+ j++
+ }
+ return string(v[:j])
+}