This repository was archived by the owner on Mar 8, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathanalysis.go
More file actions
120 lines (101 loc) · 2.36 KB
/
Copy pathanalysis.go
File metadata and controls
120 lines (101 loc) · 2.36 KB
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package main
import (
"bytes"
"debug/elf"
"encoding/json"
"fmt"
"net/http"
"os"
)
const (
codeCategory SectionCategory = "code"
dataCategory SectionCategory = "data"
bssCategory SectionCategory = "bss"
unknownCategory SectionCategory = "unknown"
)
const (
ghActionRefName = "GITHUB_REF_NAME"
ghActionSha = "GITHUB_SHA"
ghActionRepo = "GITHUB_REPOSITORY"
)
const (
headerRepo = "X-Felf-Repo"
)
type Size struct {
Text uint64 `json:"text,omitempty"`
Data uint64 `json:"data,omitempty"`
Bss uint64 `json:"bss,omitempty"`
}
type Payload struct {
Repo string `json:"-"`
Ref string `json:"ref,omitempty"`
Sha string `json:"sha,omitempty"`
Size Size `json:"size"`
}
type SectionCategory string
func newPayload() (*Payload, error) {
ref := os.Getenv(ghActionRefName)
if len(ref) == 0 {
return nil, fmt.Errorf(fmt.Sprintf("%s not found", ghActionRefName))
}
sha := os.Getenv(ghActionSha)
if len(sha) != 40 {
return nil, fmt.Errorf("malformed sha, not 40 characters long")
}
repo := os.Getenv(ghActionRepo)
if len(repo) == 0 {
return nil, fmt.Errorf(fmt.Sprintf("%s not found", ghActionRepo))
}
return &Payload{
Repo: repo,
Ref: ref,
Sha: sha,
}, nil
}
func pushPayload(token string, url string, payload *Payload) (*http.Response, error) {
body, err := json.Marshal(payload)
if err != nil {
return nil, err
}
req, _ := http.NewRequest("POST", url, bytes.NewReader(body))
req.Header.Add(headerRepo, payload.Repo)
req.Header.Add("Authorization", "Bearer "+token)
req.Header.Add("Content-Type", "application/json")
client := &http.Client{
CheckRedirect: func(req *http.Request, via []*http.Request) error {
return http.ErrUseLastResponse
},
}
return client.Do(req)
}
func newSize(file *elf.File) Size {
result := Size{}
for _, section := range file.Sections {
if section.Type == elf.SHT_NULL {
continue
}
switch category(section) {
case codeCategory:
result.Text += section.Size
case dataCategory:
result.Data += section.Size
case bssCategory:
result.Bss += section.Size
}
}
return result
}
func category(section *elf.Section) SectionCategory {
if (section.Flags & elf.SHF_ALLOC) == 0 {
return unknownCategory
}
if section.Type != elf.SHT_NOBITS {
if (section.Flags & elf.SHF_WRITE) == 0 {
return codeCategory
} else {
return dataCategory
}
} else {
return bssCategory
}
}