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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
|
// Copyright 2023 Google Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package build
import (
"crypto/sha1"
"encoding/hex"
"encoding/json"
"io"
"io/fs"
"os"
"path/filepath"
"sort"
"strings"
"android/soong/shared"
"android/soong/ui/metrics"
)
// Metadata about a staged file
type fileEntry struct {
Name string `json:"name"`
Mode fs.FileMode `json:"mode"`
Size int64 `json:"size"`
Sha1 string `json:"sha1"`
}
func fileEntryEqual(a fileEntry, b fileEntry) bool {
return a.Name == b.Name && a.Mode == b.Mode && a.Size == b.Size && a.Sha1 == b.Sha1
}
func sha1_hash(filename string) (string, error) {
f, err := os.Open(filename)
if err != nil {
return "", err
}
defer f.Close()
h := sha1.New()
if _, err := io.Copy(h, f); err != nil {
return "", err
}
return hex.EncodeToString(h.Sum(nil)), nil
}
// Subdirs of PRODUCT_OUT to scan
var stagingSubdirs = []string{
"apex",
"cache",
"coverage",
"data",
"debug_ramdisk",
"fake_packages",
"installer",
"oem",
"product",
"ramdisk",
"recovery",
"root",
"sysloader",
"system",
"system_dlkm",
"system_ext",
"system_other",
"testcases",
"test_harness_ramdisk",
"vendor",
"vendor_debug_ramdisk",
"vendor_kernel_ramdisk",
"vendor_ramdisk",
}
// Return an array of stagedFileEntrys, one for each file in the staging directories inside
// productOut
func takeStagingSnapshot(ctx Context, productOut string, subdirs []string) ([]fileEntry, error) {
var outer_err error
if !strings.HasSuffix(productOut, "/") {
productOut += "/"
}
result := []fileEntry{}
for _, subdir := range subdirs {
filepath.WalkDir(productOut+subdir,
func(filename string, dirent fs.DirEntry, err error) error {
// Ignore errors. The most common one is that one of the subdirectories
// hasn't been built, in which case we just report it as empty.
if err != nil {
ctx.Verbosef("scanModifiedStagingOutputs error: %s", err)
return nil
}
if dirent.Type().IsRegular() {
fileInfo, _ := dirent.Info()
relative := strings.TrimPrefix(filename, productOut)
sha, err := sha1_hash(filename)
if err != nil {
outer_err = err
}
result = append(result, fileEntry{
Name: relative,
Mode: fileInfo.Mode(),
Size: fileInfo.Size(),
Sha1: sha,
})
}
return nil
})
}
sort.Slice(result, func(l, r int) bool { return result[l].Name < result[r].Name })
return result, outer_err
}
// Read json into an array of fileEntry. On error return empty array.
func readJson(filename string) ([]fileEntry, error) {
buf, err := os.ReadFile(filename)
if err != nil {
// Not an error, just missing, which is empty.
return []fileEntry{}, nil
}
var result []fileEntry
err = json.Unmarshal(buf, &result)
if err != nil {
// Bad formatting. This is an error
return []fileEntry{}, err
}
return result, nil
}
// Write obj to filename.
func writeJson(filename string, obj interface{}) error {
buf, err := json.MarshalIndent(obj, "", " ")
if err != nil {
return err
}
return os.WriteFile(filename, buf, 0660)
}
type snapshotDiff struct {
Added []string `json:"added"`
Changed []string `json:"changed"`
Removed []string `json:"removed"`
}
// Diff the two snapshots, returning a snapshotDiff.
func diffSnapshots(previous []fileEntry, current []fileEntry) snapshotDiff {
result := snapshotDiff{
Added: []string{},
Changed: []string{},
Removed: []string{},
}
found := make(map[string]bool)
prev := make(map[string]fileEntry)
for _, pre := range previous {
prev[pre.Name] = pre
}
for _, cur := range current {
pre, ok := prev[cur.Name]
found[cur.Name] = true
// Added
if !ok {
result.Added = append(result.Added, cur.Name)
continue
}
// Changed
if !fileEntryEqual(pre, cur) {
result.Changed = append(result.Changed, cur.Name)
}
}
// Removed
for _, pre := range previous {
if !found[pre.Name] {
result.Removed = append(result.Removed, pre.Name)
}
}
// Sort the results
sort.Strings(result.Added)
sort.Strings(result.Changed)
sort.Strings(result.Removed)
return result
}
// Write a json files to dist:
// - A list of which files have changed in this build.
//
// And record in out/soong:
// - A list of all files in the staging directories, including their hashes.
func runStagingSnapshot(ctx Context, config Config) {
ctx.BeginTrace(metrics.RunSoong, "runStagingSnapshot")
defer ctx.EndTrace()
snapshotFilename := shared.JoinPath(config.SoongOutDir(), "staged_files.json")
// Read the existing snapshot file. If it doesn't exist, this is a full
// build, so all files will be treated as new.
previous, err := readJson(snapshotFilename)
if err != nil {
ctx.Fatal(err)
return
}
// Take a snapshot of the current out directory
current, err := takeStagingSnapshot(ctx, config.ProductOut(), stagingSubdirs)
if err != nil {
ctx.Fatal(err)
return
}
// Diff the snapshots
diff := diffSnapshots(previous, current)
// Write the diff (use RealDistDir, not one that might have been faked for bazel)
err = writeJson(shared.JoinPath(config.RealDistDir(), "modified_files.json"), diff)
if err != nil {
ctx.Fatal(err)
return
}
// Update the snapshot
err = writeJson(snapshotFilename, current)
if err != nil {
ctx.Fatal(err)
return
}
}
|