aboutsummaryrefslogtreecommitdiffstats
path: root/filter/files.go
blob: e8c0cd3f7302b1e7f01e653a080ac2989b453c53 (plain) (blame)
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package main

import (
	"fmt"
	"path"
	"sort"
	"syscall"
)

type FileFlag struct {
	Value uint64
	Name  string
}

var FileFlags = []FileFlag{
	{Value: syscall.O_APPEND, Name: "O_APPEND"},
	{Value: syscall.O_ASYNC, Name: "O_ASYNC"},
	{Value: syscall.O_CLOEXEC, Name: "O_CLOEXEC"},
	{Value: syscall.O_CREAT, Name: "O_CREAT"},
	{Value: syscall.O_DIRECT, Name: "O_DIRECT"},
	{Value: syscall.O_DIRECTORY, Name: "O_DIRECTORY"},
	{Value: syscall.O_DSYNC, Name: "O_DSYNC"},
	{Value: syscall.O_EXCL, Name: "O_EXCL"},
	{Value: syscall.O_FSYNC, Name: "O_FSYNC"},
	{Value: syscall.O_NDELAY, Name: "O_NDELAY"},
	{Value: syscall.O_NOATIME, Name: "O_NOATIME"},
	{Value: syscall.O_NOCTTY, Name: "O_NOCTTY"},
	{Value: syscall.O_NOFOLLOW, Name: "O_NOFOLLOW"},
	{Value: syscall.O_NONBLOCK, Name: "O_NONBLOCK"},
	{Value: syscall.O_RDONLY, Name: "O_RDONLY"},
	{Value: syscall.O_RDWR, Name: "O_RDWR"},
	{Value: syscall.O_RSYNC, Name: "O_RSYNC"},
	{Value: syscall.O_SYNC, Name: "O_SYNC"},
	{Value: syscall.O_TRUNC, Name: "O_TRUNC"},
	{Value: syscall.O_WRONLY, Name: "O_WRONLY"},
}

func filtFiles(pRawFileData *[]File) {
	rawFileData := *pRawFileData
	files = make([]File, 0)

	// 所有文件按照特定顺序排
	sort.Slice(rawFileData, func(i, j int) bool {
		pi := &rawFileData[i]
		pj := &rawFileData[j]

		if pi.FileName < pj.FileName {
			return true
		} else if pi.FileName > pj.FileName {
			return false
		}
		if pi.Pid < pj.Pid {
			return true
		} else if pi.Pid > pj.Pid {
			return false
		}
		if pi.Fd < pj.Fd {
			return true
		} else if pi.Fd > pj.Fd {
			return false
		}
		if pi.OpenTimestamp.Before(pj.OpenTimestamp) {
			return true
		} else {
			return false
		}
	})

	for _, file := range rawFileData {
		tgid := findTgid[file.Pid]
		pTgidNode, exists := helloTree[tgid]
		if !exists {
			continue
		}
		if file.CloseTimestamp.IsZero() {
			index, exists := pTgidNode.FindPid[file.Pid]
			if !exists || index < 0 || index >= len(pTgidNode.Threads) {
				continue
			}
			file.CloseTimestamp = pTgidNode.Threads[index].ExitTimestamp
		}
		file.FileName = path.Clean(file.FileName)
		files = append(files, file)
	}
}

// 解析 Flags[1] 的值为描述性字符串
func parseFlags(flag uint64) string {
	var result string
	for _, fileFlag := range FileFlags {
		if flag&fileFlag.Value == fileFlag.Value {
			if result != "" {
				result += " | "
			}
			result += fileFlag.Name
		}
	}
	if result == "" {
		return fmt.Sprintf("0x%x", flag) // 返回原始十六进制值
	}
	return result
}