Skip to content

Commit e767c87

Browse files
committed
capability: deprecate List, add ListKnown, ListSupported
Apparently, most users of capability.List wants the list of supported capabilities (i.e. they go on to exclude capabilities above the last known one). Let's provide ListSupported to such users. Also, provide ListKnown, and deprecate List. Finally, amend LastCap documentation with a link to ListSupported, as this is what most users are using it for. Signed-off-by: Kir Kolyshkin <[email protected]>
1 parent 11e4ea9 commit e767c87

File tree

4 files changed

+33
-4
lines changed

4 files changed

+33
-4
lines changed

capability/capability.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,8 @@ func NewFile2(path string) (Capabilities, error) {
137137

138138
// LastCap returns highest valid capability of the running kernel,
139139
// or an error if it can not be obtained.
140+
//
141+
// See also: [ListSupported].
140142
func LastCap() (Cap, error) {
141143
return lastCap()
142144
}

capability/enum.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77

88
package capability
99

10+
import "slices"
11+
1012
type CapType uint
1113

1214
func (c CapType) String() string {
@@ -301,3 +303,30 @@ const (
301303
// Introduced in kernel 5.9
302304
CAP_CHECKPOINT_RESTORE = Cap(40)
303305
)
306+
307+
// List returns the list of all capabilities known to the package.
308+
//
309+
// Deprecated: use [ListKnown] or [ListSupported] instead.
310+
func List() []Cap {
311+
return ListKnown()
312+
}
313+
314+
// ListKnown returns the list of all capabilities known to the package.
315+
func ListKnown() []Cap {
316+
return list()
317+
}
318+
319+
// ListSupported retuns the list of all capabilities known to the package,
320+
// except those that are not supported by the currently running Linux kernel.
321+
//
322+
// Returns nil on platforms other than Linux.
323+
func ListSupported() []Cap {
324+
last, err := LastCap()
325+
if err != nil {
326+
return nil
327+
}
328+
return slices.DeleteFunc(list(), func(c Cap) bool {
329+
// Remove caps not supported by the kernel.
330+
return c > last
331+
})
332+
}

capability/enum_gen.go

Lines changed: 1 addition & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

capability/enumgen/gen.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,7 @@ func (g *generator) writeStringFunc() {
4343

4444
func (g *generator) writeListFunc() {
4545
g.buf.WriteString("\n")
46-
g.buf.WriteString("// List returns list of all supported capabilities\n")
47-
g.buf.WriteString("func List() []Cap {\n")
46+
g.buf.WriteString("func list() []Cap {\n")
4847
g.buf.WriteString("return []Cap{\n")
4948
for _, cap := range g.caps {
5049
fmt.Fprintf(&g.buf, "%s,\n", cap)

0 commit comments

Comments
 (0)