summaryrefslogtreecommitdiff
path: root/internal/file/flags.go
blob: 002de9cb7f6add93bd1a06b9ff19fd8ed7624563 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package file

import (
	"fmt"
	"os"
	"strings"
	"syscall"
)

// TODO: syscalls fcntl and dup3 and many more can also manipulate open flags
// Check the tracepoint formates for 'flags'
// Maybe need new BPF type and pass through the ring
var flagsToHumanCache = map[int32]string{-1: "O_?"}

type tuple struct {
	syscallNr int
	str       string
}

var flagsToHuman = []tuple{
	{syscall.O_RDONLY, "O_RDONLY"},
	{syscall.O_WRONLY, "O_WRONLY"},
	{syscall.O_RDWR, "O_RDWR"},
	{syscall.O_ACCMODE, "O_ACCMODE"},
	{syscall.O_APPEND, "O_APPEND"},
	{syscall.O_ASYNC, "O_ASYNC"},
	{syscall.O_CLOEXEC, "O_CLOEXEC"},
	{syscall.O_CREAT, "O_CREAT"},
	{syscall.O_DIRECT, "O_DIRECT"},
	{syscall.O_DIRECTORY, "O_DIRECTORY"},
	{syscall.O_DSYNC, "O_DSYNC"},
	{syscall.O_EXCL, "O_EXCL"},
	{syscall.O_NOATIME, "O_NOATIME"},
	{syscall.O_NOCTTY, "O_NOCTTY"},
	{syscall.O_NOFOLLOW, "O_NOFOLLOW"},
	{syscall.O_NONBLOCK, "O_NONBLOCK"},
	{syscall.O_SYNC, "O_SYNC"},
	{syscall.O_TRUNC, "O_TRUNC"},
}

func flagsToStr(flags int32) string {
	if str, ok := flagsToHumanCache[flags]; ok {
		return str
	}
	str := strings.Join(flagsToStrs(flags), "|")
	flagsToHumanCache[flags] = fmt.Sprintf("%O=>%s", flags, str)
	return str
}

func flagsToStrs(flags int32) (result []string) {
	if int(flags)&(os.O_WRONLY|os.O_RDWR) == 0 {
		// Must be read only then
		result = append(result, "O_RDONLY")
	}
	for _, toHuman := range flagsToHuman[1:] {
		if int(flags)&toHuman.syscallNr == toHuman.syscallNr {
			result = append(result, toHuman.str)
		}
	}
	if len(result) == 0 {
		result = append(result, "non=>O_RDONLY")
	}
	return
}