Skip to content

Commit cc38550

Browse files
authored
fix(coreadi): stage both libraries before replacing installed files (#243)
1 parent 32c1a90 commit cc38550

3 files changed

Lines changed: 256 additions & 66 deletions

File tree

‎internal/service/coreadi.go‎

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
package service
2+
3+
import (
4+
"archive/zip"
5+
"fmt"
6+
"io"
7+
"os"
8+
"path/filepath"
9+
"sort"
10+
"sync"
11+
)
12+
13+
var coreADIUpdateMu sync.Mutex
14+
15+
// installCoreADILibraries extracts both required libraries before replacing
16+
// either installed file. An incomplete or corrupt APK leaves existing libs intact.
17+
func installCoreADILibraries(apkPath, destDir, pkgArch string) error {
18+
archive, err := zip.OpenReader(apkPath)
19+
if err != nil {
20+
return fmt.Errorf("open apk zip failed: %w", err)
21+
}
22+
defer func() { _ = archive.Close() }()
23+
24+
parent := filepath.Dir(destDir)
25+
if err := os.MkdirAll(parent, 0o755); err != nil {
26+
return fmt.Errorf("create lib parent dir failed: %w", err)
27+
}
28+
stagingDir, err := os.MkdirTemp(parent, ".coreadi-*")
29+
if err != nil {
30+
return fmt.Errorf("create lib staging dir failed: %w", err)
31+
}
32+
defer func() { _ = os.RemoveAll(stagingDir) }()
33+
34+
names := []string{"libstoreservicescore.so", "libCoreADI.so"}
35+
required := make(map[string]string, len(names))
36+
for _, name := range names {
37+
required[filepath.ToSlash(filepath.Join("lib", pkgArch, name))] = name
38+
}
39+
for _, file := range archive.File {
40+
name, wanted := required[file.Name]
41+
if !wanted {
42+
continue
43+
}
44+
src, err := file.Open()
45+
if err != nil {
46+
return fmt.Errorf("open apk entry %s failed: %w", file.Name, err)
47+
}
48+
dst, err := os.OpenFile(filepath.Join(stagingDir, name), os.O_WRONLY|os.O_CREATE|os.O_EXCL, 0o644)
49+
if err != nil {
50+
_ = src.Close()
51+
return fmt.Errorf("stage apk entry %s failed: %w", file.Name, err)
52+
}
53+
_, copyErr := io.Copy(dst, src)
54+
closeErr := dst.Close()
55+
_ = src.Close()
56+
if copyErr != nil {
57+
return fmt.Errorf("extract apk entry %s failed: %w", file.Name, copyErr)
58+
}
59+
if closeErr != nil {
60+
return fmt.Errorf("close staged apk entry %s failed: %w", file.Name, closeErr)
61+
}
62+
delete(required, file.Name)
63+
}
64+
if len(required) != 0 {
65+
missing := make([]string, 0, len(required))
66+
for key := range required {
67+
missing = append(missing, key)
68+
}
69+
sort.Strings(missing)
70+
return fmt.Errorf("missing files in apk: %v", missing)
71+
}
72+
73+
if err := os.MkdirAll(destDir, 0o755); err != nil {
74+
return fmt.Errorf("create destination dir failed: %w", err)
75+
}
76+
for _, name := range names {
77+
if err := os.Rename(filepath.Join(stagingDir, name), filepath.Join(destDir, name)); err != nil {
78+
return fmt.Errorf("install library %s failed: %w", name, err)
79+
}
80+
}
81+
return nil
82+
}

‎internal/service/coreadi_test.go‎

Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
package service
2+
3+
import (
4+
"archive/zip"
5+
"os"
6+
"path/filepath"
7+
"strings"
8+
"testing"
9+
)
10+
11+
func fakeAPK(t *testing.T, files map[string]string) string {
12+
t.Helper()
13+
path := filepath.Join(t.TempDir(), "applemusic.apk")
14+
out, err := os.Create(path)
15+
if err != nil {
16+
t.Fatal(err)
17+
}
18+
z := zip.NewWriter(out)
19+
for name, contents := range files {
20+
w, err := z.Create(name)
21+
if err != nil {
22+
t.Fatal(err)
23+
}
24+
if _, err := w.Write([]byte(contents)); err != nil {
25+
t.Fatal(err)
26+
}
27+
}
28+
if err := z.Close(); err != nil {
29+
t.Fatal(err)
30+
}
31+
if err := out.Close(); err != nil {
32+
t.Fatal(err)
33+
}
34+
return path
35+
}
36+
37+
func TestInstallCoreADILibraries(t *testing.T) {
38+
for _, tc := range []struct {
39+
name string
40+
contents map[string]string
41+
wantError bool
42+
expectedOne, expectedTwo string
43+
}{
44+
{"valid apk", map[string]string{"lib/x86_64/libstoreservicescore.so": "new-stores", "lib/x86_64/libCoreADI.so": "new-core"}, false, "new-stores", "new-core"},
45+
{"missing library", map[string]string{"lib/x86_64/libstoreservicescore.so": "new-stores"}, true, "old-stores", "old-core"},
46+
{"wrong architecture", map[string]string{"lib/arm64-v8a/libstoreservicescore.so": "new-stores", "lib/arm64-v8a/libCoreADI.so": "new-core"}, true, "old-stores", "old-core"},
47+
} {
48+
t.Run(tc.name, func(t *testing.T) {
49+
dir := filepath.Join(t.TempDir(), "x86_64")
50+
if err := os.MkdirAll(dir, 0o755); err != nil {
51+
t.Fatal(err)
52+
}
53+
for name, value := range map[string]string{"libstoreservicescore.so": "old-stores", "libCoreADI.so": "old-core"} {
54+
if err := os.WriteFile(filepath.Join(dir, name), []byte(value), 0o644); err != nil {
55+
t.Fatal(err)
56+
}
57+
}
58+
err := installCoreADILibraries(fakeAPK(t, tc.contents), dir, "x86_64")
59+
if (err != nil) != tc.wantError {
60+
t.Fatalf("err=%v, wantError=%v", err, tc.wantError)
61+
}
62+
for name, want := range map[string]string{"libstoreservicescore.so": tc.expectedOne, "libCoreADI.so": tc.expectedTwo} {
63+
got, e := os.ReadFile(filepath.Join(dir, name))
64+
if e != nil || string(got) != want {
65+
t.Fatalf("%s: %s / %v; want %s", name, got, e, want)
66+
}
67+
}
68+
entries, e := os.ReadDir(filepath.Dir(dir))
69+
if e != nil {
70+
t.Fatal(e)
71+
}
72+
for _, entry := range entries {
73+
if strings.HasPrefix(entry.Name(), ".coreadi-") {
74+
t.Fatalf("staging directory left behind: %s", entry.Name())
75+
}
76+
}
77+
})
78+
}
79+
}
80+
81+
func TestCorruptAPKPreservesOriginal(t *testing.T) {
82+
dir := filepath.Join(t.TempDir(), "x86_64")
83+
if err := os.MkdirAll(dir, 0o755); err != nil {
84+
t.Fatal(err)
85+
}
86+
old := filepath.Join(dir, "libstoreservicescore.so")
87+
if err := os.WriteFile(old, []byte("old-stores"), 0o644); err != nil {
88+
t.Fatal(err)
89+
}
90+
invalid := filepath.Join(t.TempDir(), "invalid.apk")
91+
if err := os.WriteFile(invalid, []byte("bad zip"), 0o644); err != nil {
92+
t.Fatal(err)
93+
}
94+
if err := installCoreADILibraries(invalid, dir, "x86_64"); err == nil {
95+
t.Fatal("expected invalid archive to fail")
96+
}
97+
if got, _ := os.ReadFile(old); string(got) != "old-stores" {
98+
t.Fatalf("old library modified: %s", got)
99+
}
100+
}
101+
102+
func TestBadSecondLibraryPreservesOriginals(t *testing.T) {
103+
first := "lib/x86_64/libstoreservicescore.so"
104+
second := "lib/x86_64/libCoreADI.so"
105+
archive := filepath.Join(t.TempDir(), "corrupt.apk")
106+
out, err := os.Create(archive)
107+
if err != nil {
108+
t.Fatal(err)
109+
}
110+
writer := zip.NewWriter(out)
111+
for _, file := range []struct{ name, data string }{{first, "first-library-good"}, {second, "second-library-corrupt"}} {
112+
entry, err := writer.CreateHeader(&zip.FileHeader{Name: file.name, Method: zip.Store})
113+
if err != nil {
114+
t.Fatal(err)
115+
}
116+
if _, err := entry.Write([]byte(file.data)); err != nil {
117+
t.Fatal(err)
118+
}
119+
}
120+
if err := writer.Close(); err != nil {
121+
t.Fatal(err)
122+
}
123+
if err := out.Close(); err != nil {
124+
t.Fatal(err)
125+
}
126+
bytes, err := os.ReadFile(archive)
127+
if err != nil {
128+
t.Fatal(err)
129+
}
130+
off := strings.Index(string(bytes), "second-library-corrupt")
131+
if off < 0 {
132+
t.Fatal("second library not found in fixture")
133+
}
134+
bytes[off] = 'X' // ZIP structure intact; CRC must fail for second file
135+
if err := os.WriteFile(archive, bytes, 0o600); err != nil {
136+
t.Fatal(err)
137+
}
138+
dest := filepath.Join(t.TempDir(), "lib", "x86_64")
139+
if err := os.MkdirAll(dest, 0o755); err != nil {
140+
t.Fatal(err)
141+
}
142+
for name, value := range map[string]string{"libstoreservicescore.so": "old-one", "libCoreADI.so": "old-two"} {
143+
if err := os.WriteFile(filepath.Join(dest, name), []byte(value), 0o644); err != nil {
144+
t.Fatal(err)
145+
}
146+
}
147+
if err := installCoreADILibraries(archive, dest, "x86_64"); err == nil {
148+
t.Fatal("want CRC error")
149+
}
150+
for name, value := range map[string]string{"libstoreservicescore.so": "old-one", "libCoreADI.so": "old-two"} {
151+
b, err := os.ReadFile(filepath.Join(dest, name))
152+
if err != nil || string(b) != value {
153+
t.Fatalf("%s changed to %q (%v)", name, b, err)
154+
}
155+
}
156+
}

‎internal/service/service.go‎

Lines changed: 18 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package service
22

33
import (
4-
"archive/zip"
54
"context"
65
"errors"
76
"fmt"
@@ -187,7 +186,11 @@ func SetLanguage(lang string) {
187186

188187
// UpdateCoreADI downloads the latest Apple Music APK and saves it to DataDir/PlumeImpactor/lib
189188
func UpdateCoreADI() error {
190-
// Determine package arch used inside the APK zip
189+
// Serialize updates so concurrent requests cannot install different
190+
// revisions of the library pair.
191+
coreADIUpdateMu.Lock()
192+
defer coreADIUpdateMu.Unlock()
193+
191194
pkgArch := "x86_64"
192195
if runtime.GOARCH == "arm64" {
193196
pkgArch = "arm64-v8a"
@@ -198,9 +201,7 @@ func UpdateCoreADI() error {
198201
if err != nil {
199202
return fmt.Errorf("download failed: %w", err)
200203
}
201-
defer func() {
202-
_ = resp.Body.Close()
203-
}()
204+
defer func() { _ = resp.Body.Close() }()
204205
if resp.StatusCode != stdhttp.StatusOK {
205206
return fmt.Errorf("download failed, status: %d", resp.StatusCode)
206207
}
@@ -209,75 +210,26 @@ func UpdateCoreADI() error {
209210
if err := os.MkdirAll(tmpDir, os.ModePerm); err != nil {
210211
return fmt.Errorf("create tmp dir failed: %w", err)
211212
}
212-
tmpPath := filepath.Join(tmpDir, "applemusic.apk")
213-
214-
tmpOut, err := os.Create(tmpPath)
213+
tmpOut, err := os.CreateTemp(tmpDir, "applemusic-*.apk")
215214
if err != nil {
216215
return fmt.Errorf("create temp file failed: %w", err)
217216
}
218-
if _, err := io.Copy(tmpOut, resp.Body); err != nil {
219-
_ = tmpOut.Close()
220-
return fmt.Errorf("save temp file failed: %w", err)
221-
}
222-
_ = tmpOut.Close()
223-
224-
defer func() {
225-
_ = os.Remove(tmpPath)
226-
}()
227-
228-
// open zip and extract specific lib files
229-
zr, err := zip.OpenReader(tmpPath)
230-
if err != nil {
231-
return fmt.Errorf("open apk zip failed: %w", err)
232-
}
233-
defer func() {
234-
_ = zr.Close()
235-
}()
236-
237-
destDir := filepath.Join(app.Config.Server.DataDir, "PlumeImpactor", "lib", pkgArch)
238-
if err := os.MkdirAll(destDir, os.ModePerm); err != nil {
239-
return fmt.Errorf("create dir failed: %w", err)
240-
}
217+
tmpPath := tmpOut.Name()
218+
defer func() { _ = os.Remove(tmpPath) }()
241219

242-
targets := map[string]string{
243-
filepath.ToSlash(filepath.Join("lib", pkgArch, "libstoreservicescore.so")): "libstoreservicescore.so",
244-
filepath.ToSlash(filepath.Join("lib", pkgArch, "libCoreADI.so")): "libCoreADI.so",
220+
_, copyErr := io.Copy(tmpOut, resp.Body)
221+
closeErr := tmpOut.Close()
222+
if copyErr != nil {
223+
return fmt.Errorf("save temp file failed: %w", copyErr)
245224
}
246-
247-
for _, f := range zr.File {
248-
name := f.Name
249-
// normalize slashes
250-
name = filepath.ToSlash(name)
251-
if destName, ok := targets[name]; ok {
252-
rc, err := f.Open()
253-
if err != nil {
254-
return fmt.Errorf("open zip entry failed: %w", err)
255-
}
256-
dstPath := filepath.Join(destDir, destName)
257-
outf, err := os.Create(dstPath)
258-
if err != nil {
259-
_ = rc.Close()
260-
return fmt.Errorf("create dest file failed: %w", err)
261-
}
262-
if _, err := io.Copy(outf, rc); err != nil {
263-
_ = rc.Close()
264-
_ = outf.Close()
265-
return fmt.Errorf("copy entry failed: %w", err)
266-
}
267-
_ = rc.Close()
268-
_ = outf.Close()
269-
delete(targets, name)
270-
}
225+
if closeErr != nil {
226+
return fmt.Errorf("close temp file failed: %w", closeErr)
271227
}
272228

273-
if len(targets) > 0 {
274-
missing := []string{}
275-
for k := range targets {
276-
missing = append(missing, k)
277-
}
278-
return fmt.Errorf("missing files in apk: %v", missing)
229+
destDir := filepath.Join(app.Config.Server.DataDir, "PlumeImpactor", "lib", pkgArch)
230+
if err := installCoreADILibraries(tmpPath, destDir, pkgArch); err != nil {
231+
return err
279232
}
280-
281233
log.Infof("Update CoreADI success. path: %s", destDir)
282234
return nil
283235
}

0 commit comments

Comments
 (0)
⌂ 换网址