From 7263879208f0aeae3aea297a64732e68f23947ef Mon Sep 17 00:00:00 2001 From: qiyinxi Date: Sat, 5 Dec 2026 16:08:04 +0100 Subject: [PATCH] =?UTF-8?q?=E9=97=AE=E9=A2=98=E5=8C=85=EF=BC=9Amas-avd.jso?= =?UTF-8?q?n=20=E5=8E=BB=E4=B8=8D=E6=8E=89=E5=BA=94=E7=94=A8=E6=B8=85?= =?UTF-8?q?=E5=8D=95=E6=97=B6=E4=B8=8D=E9=99=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit #286 把实例的应用清单(包名、应用名、UID)存进 mas-avd.json,全量问题包 复制这份文件时会把它剔掉;但文件解析不了时,copyMetadata 原样复制, 清单就跟着进了 ZIP。 现在先把文件拷到打包目录外的临时文件里解析、剔除,只把干净的副本写进 问题包。解析不了、或者剔完还能找到 appProxyPackages(不在 instances. 下面),就不附 mas-avd.json,收集日志里记一行「未附 mas-avd.json」, 其他诊断照常带上。不带清单的文件照旧原样复制。 测试改成核最终 ZIP:mas-avd.json 写坏且带清单时,ZIP 里没有这份文件、 任何文件里都没有清单,summary 里有那一行。 Refs ModAVD/ModAVD#434 (cherry picked from commit 8c49a43f5664cef4e061c01b9a864ee984a1dfe9) --- internal/report/collect.go | 4 +- internal/report/proxypackages_windows_test.go | 79 +++++++++++++++++-- internal/report/selection.go | 41 +++++++--- 3 files changed, 104 insertions(+), 20 deletions(-) diff --git a/internal/report/collect.go b/internal/report/collect.go index 8208db91..c2a5d784 100644 --- a/internal/report/collect.go +++ b/internal/report/collect.go @@ -700,7 +700,9 @@ func (c *collector) instanceFiles() error { } else if c.selectedMetadata != nil { err = os.WriteFile(dst, c.selectedMetadata.MarshalPython(), 0600) } - if err != nil { + if errors.Is(err, errMetadataWithheld) { + c.L("未附 mas-avd.json:它解析不了,里面的应用清单去不掉") + } else if err != nil { c.L("复制 mas-avd.json 失败:" + errText(err)) } } else { diff --git a/internal/report/proxypackages_windows_test.go b/internal/report/proxypackages_windows_test.go index d1bc4e2f..2ff4ed05 100644 --- a/internal/report/proxypackages_windows_test.go +++ b/internal/report/proxypackages_windows_test.go @@ -4,6 +4,7 @@ package report import ( "context" + "errors" "os" "path/filepath" "strings" @@ -73,18 +74,80 @@ func TestCollectDropsProxyPackages(t *testing.T) { } } -// A mas-avd.json that does not parse is copied as it is in a full report (it was before). +// A mas-avd.json whose app lists cannot be taken out (it does not parse, or they sit where dropProxyPackages +// does not look) is not copied at all; one without app lists is copied as it is, even when it does not parse. func TestCopyMetadataBadFile(t *testing.T) { - dir := testTempDir(t) - src, dst := filepath.Join(dir, "src.json"), filepath.Join(dir, "dst.json") - const bad = `{"instances": {"1": {"appProxyPackages": [` - if err := os.WriteFile(src, []byte(bad), 0o644); err != nil { + for _, tc := range []struct { + name, text string + withheld bool + }{ + {"broken with lists", `{"instances": {"1": {"appProxyPackages": [{"package":"com.example.secret-app"`, true}, + {"lists elsewhere", `{"instances": [{"appProxyPackages": [{"package":"com.example.secret-app"}]}]}`, true}, + {"broken without lists", `{"instances": {"1": {"memoryMb": 4096`, false}, + } { + t.Run(tc.name, func(t *testing.T) { + dir := testTempDir(t) + src, dst := filepath.Join(dir, "src.json"), filepath.Join(dir, "dst.json") + if err := os.WriteFile(src, []byte(tc.text), 0o644); err != nil { + t.Fatal(err) + } + err := copyMetadata(src, dst) + b, readErr := os.ReadFile(dst) + if tc.withheld { + if !errors.Is(err, errMetadataWithheld) { + t.Errorf("err = %v, want errMetadataWithheld", err) + } + if readErr == nil { + t.Errorf("dst was written: %q", b) + } + return + } + if err != nil { + t.Fatal(err) + } + if string(b) != tc.text { + t.Errorf("got %q", b) + } + }) + } +} + +// TestCollectWithholdsBrokenMetadata: a full report of a root whose mas-avd.json has app lists but does not +// parse has no mas-avd.json and nothing of the lists anywhere in the ZIP, and says why in its log. +func TestCollectWithholdsBrokenMetadata(t *testing.T) { + f := newFixture(t, "full", testTempDir(t), "") + guardDials(t, f.adbPort) + meta := filepath.Join(f.root, root.MetadataFile) + b, err := os.ReadFile(meta) + if err != nil { t.Fatal(err) } - if err := copyMetadata(src, dst); err != nil { + const marker = "com.example.secret-app" + broken := strings.Replace(string(b), `"instances"`, `"appProxyPackages": [{"package": "`+marker+`"}], "instances"`, 1) + broken = strings.TrimRight(broken, " \r\n") + broken = broken[:len(broken)-1] // cut the closing brace: it no longer parses + if err := os.WriteFile(meta, []byte(broken), 0o644); err != nil { t.Fatal(err) } - if b, _ := os.ReadFile(dst); string(b) != bad { - t.Errorf("got %q", b) + res, err := collect(context.Background(), f.root, f.host, CollectOptions{ + OutDir: filepath.Join(f.base, "out"), StageDir: filepath.Join(f.base, "stage"), NoExplorer: true, + now: func() time.Time { return f.now }, getenv: f.getenv, pid: 4321, + }) + if err != nil { + t.Fatal(err) + } + _, files := readZip(t, res.ZipPath) + if _, ok := files[root.MetadataFile]; ok { + t.Error("the archive has mas-avd.json") + } + said := false + for name, data := range files { + if strings.Contains(string(data), marker) { + t.Errorf("%s in the archive has the app list", name) + } + said = said || strings.Contains(string(data), "未附 mas-avd.json") + } + if !said { + t.Error("nothing in the archive says mas-avd.json was left out") } } diff --git a/internal/report/selection.go b/internal/report/selection.go index 15231379..0656351f 100644 --- a/internal/report/selection.go +++ b/internal/report/selection.go @@ -3,6 +3,7 @@ package report import ( "bytes" "encoding/json" + "errors" "io" "os" "path/filepath" @@ -65,22 +66,40 @@ func dropProxyPackages(v *jsonord.Value) { } } -// copyMetadata copies mas-avd.json into a full report without the app lists. A file that does not parse is -// copied as it is. +// errMetadataWithheld: mas-avd.json has app lists that could not be taken out (it does not parse, or they are +// somewhere dropProxyPackages does not look), so the report goes without it. +var errMetadataWithheld = errors.New("mas-avd.json has app lists that could not be removed") + +// copyMetadata copies mas-avd.json into a full report without the app lists. The file is read and cleaned outside +// the report's staging folder, and only the clean copy is written to dst; when the lists cannot be taken out +// nothing is written and errMetadataWithheld is returned. func copyMetadata(src, dst string) error { - if _, err := copyShared(src, dst, 0); err != nil { + tmp, err := os.CreateTemp("", "modavd-meta-*.json") + if err != nil { return err } - data, err := os.ReadFile(dst) - if err != nil || !bytes.Contains(data, []byte(appproxy.PackagesMetaKey)) { - return nil + tmpPath := tmp.Name() + tmp.Close() + defer os.Remove(tmpPath) + if _, err := copyShared(src, tmpPath, 0); err != nil { + return err } - v, err := jsonord.Parse(bytes.TrimPrefix(data, []byte{0xef, 0xbb, 0xbf})) - if err != nil || v.Kind() != jsonord.Object { - return nil + data, err := os.ReadFile(tmpPath) + if err != nil { + return err } - dropProxyPackages(v) - return os.WriteFile(dst, v.MarshalPython(), 0600) + if bytes.Contains(data, []byte(appproxy.PackagesMetaKey)) { + v, err := jsonord.Parse(bytes.TrimPrefix(data, []byte{0xef, 0xbb, 0xbf})) + if err != nil || v.Kind() != jsonord.Object { + return errMetadataWithheld + } + dropProxyPackages(v) + data = v.MarshalPython() + if bytes.Contains(data, []byte(appproxy.PackagesMetaKey)) { + return errMetadataWithheld + } + } + return os.WriteFile(dst, data, 0600) } func (opt CollectOptions) metadata(r *root.Root) (*jsonord.Value, error) {