diff --git a/acl/export.go b/acl/export.go index 104de51..13a2a3f 100644 --- a/acl/export.go +++ b/acl/export.go @@ -25,7 +25,25 @@ const ( Other = C.ACL_OTHER ) -type Perm C.acl_perm_t +type ( + Perm C.acl_perm_t + Perms []Perm +) + +func (ps Perms) String() string { + var s = []byte("---") + for _, p := range ps { + switch p { + case Read: + s[0] = 'r' + case Write: + s[1] = 'w' + case Execute: + s[2] = 'x' + } + } + return string(s) +} func UpdatePerm(path string, uid int, perms ...Perm) error { // read acl from file diff --git a/internal/system/acl.go b/internal/system/acl.go index efc4884..f927f18 100644 --- a/internal/system/acl.go +++ b/internal/system/acl.go @@ -28,7 +28,7 @@ func (sys *I) UpdatePermType(et Enablement, path string, perms ...acl.Perm) *I { type ACL struct { et Enablement path string - perms []acl.Perm + perms acl.Perms } func (a *ACL) Type() Enablement { @@ -36,20 +36,18 @@ func (a *ACL) Type() Enablement { } func (a *ACL) apply(sys *I) error { - fmsg.VPrintf("applying ACL %s uid: %d type: %s path: %q", - a, sys.uid, TypeString(a.et), a.path) + fmsg.VPrintln("applying ACL", a) return fmsg.WrapErrorSuffix(acl.UpdatePerm(a.path, sys.uid, a.perms...), fmt.Sprintf("cannot apply ACL entry to %q:", a.path)) } func (a *ACL) revert(sys *I, ec *Criteria) error { if ec.hasType(a) { - fmsg.VPrintf("stripping ACL %s uid: %d type: %s path: %q", - a, sys.uid, TypeString(a.et), a.path) + fmsg.VPrintln("stripping ACL", a) return fmsg.WrapErrorSuffix(acl.UpdatePerm(a.path, sys.uid), fmt.Sprintf("cannot strip ACL entry from %q:", a.path)) } else { - fmsg.VPrintln("skipping ACL", a, "uid:", sys.uid, "tag:", TypeString(a.et), "path:", a.path) + fmsg.VPrintln("skipping ACL", a) return nil } } @@ -67,16 +65,6 @@ func (a *ACL) Path() string { } func (a *ACL) String() string { - var s = []byte("---") - for _, p := range a.perms { - switch p { - case acl.Read: - s[0] = 'r' - case acl.Write: - s[1] = 'w' - case acl.Execute: - s[2] = 'x' - } - } - return string(s) + return fmt.Sprintf("%s type: %s path: %q", + a.perms, TypeString(a.et), a.path) } diff --git a/internal/system/acl_test.go b/internal/system/acl_test.go index fe456c7..ed449ef 100644 --- a/internal/system/acl_test.go +++ b/internal/system/acl_test.go @@ -49,21 +49,22 @@ func TestUpdatePermType(t *testing.T) { func TestACL_String(t *testing.T) { testCases := []struct { want string + et Enablement perms []acl.Perm }{ - {"---", []acl.Perm{}}, - {"r--", []acl.Perm{acl.Read}}, - {"-w-", []acl.Perm{acl.Write}}, - {"--x", []acl.Perm{acl.Execute}}, - {"rw-", []acl.Perm{acl.Read, acl.Write}}, - {"r-x", []acl.Perm{acl.Read, acl.Execute}}, - {"rwx", []acl.Perm{acl.Read, acl.Write, acl.Execute}}, - {"rwx", []acl.Perm{acl.Read, acl.Write, acl.Write, acl.Execute}}, + {`--- type: Process path: "/nonexistent"`, Process, []acl.Perm{}}, + {`r-- type: User path: "/nonexistent"`, User, []acl.Perm{acl.Read}}, + {`-w- type: Wayland path: "/nonexistent"`, EWayland, []acl.Perm{acl.Write}}, + {`--x type: X11 path: "/nonexistent"`, EX11, []acl.Perm{acl.Execute}}, + {`rw- type: D-Bus path: "/nonexistent"`, EDBus, []acl.Perm{acl.Read, acl.Write}}, + {`r-x type: PulseAudio path: "/nonexistent"`, EPulse, []acl.Perm{acl.Read, acl.Execute}}, + {`rwx type: User path: "/nonexistent"`, User, []acl.Perm{acl.Read, acl.Write, acl.Execute}}, + {`rwx type: Process path: "/nonexistent"`, Process, []acl.Perm{acl.Read, acl.Write, acl.Write, acl.Execute}}, } for _, tc := range testCases { t.Run(tc.want, func(t *testing.T) { - a := &ACL{perms: tc.perms} + a := &ACL{et: tc.et, perms: tc.perms, path: "/nonexistent"} if got := a.String(); got != tc.want { t.Errorf("String() = %v, want %v", got, tc.want)