Skip to content

Commit 37fa549

Browse files
authored
feat: optimize cleanup logic and fix static resource bloat & fd leaks (#598)
1 parent 336b6ab commit 37fa549

4 files changed

Lines changed: 58 additions & 21 deletions

File tree

android/src/main/java/cn/reactnative/modules/update/DownloadTask.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,9 @@ private void doFullPatch() throws IOException {
258258
}
259259
}
260260

261+
if (params.targetFile.exists()) {
262+
params.targetFile.delete();
263+
}
261264
}
262265

263266
private void doPatchFromApk() throws IOException, JSONException {
@@ -300,6 +303,9 @@ private void doPatchFromApk() throws IOException, JSONException {
300303
}
301304

302305
bundledResourceCopier.copyFromResource(copyList, contents.copyCrcs);
306+
if (params.targetFile.exists()) {
307+
params.targetFile.delete();
308+
}
303309
}
304310

305311
private void doPatchFromPpk() throws IOException, JSONException {
@@ -326,15 +332,17 @@ private void doPatchFromPpk() throws IOException, JSONException {
326332
contents.copyTos.toArray(new String[0]),
327333
contents.deletes.toArray(new String[0])
328334
);
329-
335+
if (params.targetFile.exists()) {
336+
params.targetFile.delete();
337+
}
330338
}
331339

332340
private void doCleanUp() {
333341
cleanupOldEntries(
334342
params.unzipDirectory.getAbsolutePath(),
335343
params.hash,
336344
params.originHash,
337-
7
345+
3
338346
);
339347
}
340348

harmony/pushy/src/main/ets/DownloadTask.ts

Lines changed: 46 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -442,6 +442,13 @@ export class DownloadTask {
442442
await this.downloadFile(params);
443443
await this.recreateDirectory(params.unzipDirectory);
444444
await zlib.decompressFile(params.targetFile, params.unzipDirectory);
445+
try {
446+
if (fileIo.accessSync(params.targetFile)) {
447+
await fileIo.unlink(params.targetFile);
448+
}
449+
} catch (e) {
450+
console.error('Failed to delete temporary zip file after decompression:', e);
451+
}
445452
}
446453

447454
private async doPatchFromApp(params: DownloadTaskParams): Promise<void> {
@@ -484,6 +491,13 @@ export class DownloadTask {
484491
),
485492
params.unzipDirectory,
486493
);
494+
try {
495+
if (fileIo.accessSync(params.targetFile)) {
496+
await fileIo.unlink(params.targetFile);
497+
}
498+
} catch (e) {
499+
console.error('Failed to delete temporary zip file after patching:', e);
500+
}
487501
}
488502

489503
private async doPatchFromPpk(params: DownloadTaskParams): Promise<void> {
@@ -517,6 +531,13 @@ export class DownloadTask {
517531
enableMerge: plan.enableMerge,
518532
});
519533
console.info('Patch from PPK completed');
534+
try {
535+
if (fileIo.accessSync(params.targetFile)) {
536+
await fileIo.unlink(params.targetFile);
537+
}
538+
} catch (e) {
539+
console.error('Failed to delete temporary patch file after patching:', e);
540+
}
520541
}
521542

522543
private async copyFromResource(
@@ -548,27 +569,35 @@ export class DownloadTask {
548569
parentDirs.map(dir => this.ensureDirectory(dir)),
549570
);
550571
await Promise.all(
551-
targets.map(target => this.writeFileContent(target, mediaBuffer.buffer)),
572+
targets.map(target => this.writeFileContent(target, mediaBuffer)),
552573
);
553574
continue;
554575
}
555576
const fromContent = await resourceManager.getRawFd(currentFrom);
556-
const [firstTarget, ...restTargets] = targets;
557-
const parentDirs = [
558-
...new Set(
559-
targets.map(t => t.substring(0, t.lastIndexOf('/'))).filter(Boolean),
560-
),
561-
];
562-
await Promise.all(
563-
parentDirs.map(dir => this.ensureDirectory(dir))
564-
);
565-
if (fileIo.accessSync(firstTarget)) {
566-
await fileIo.unlink(firstTarget);
577+
try {
578+
const [firstTarget, ...restTargets] = targets;
579+
const parentDirs = [
580+
...new Set(
581+
targets.map(t => t.substring(0, t.lastIndexOf('/'))).filter(Boolean),
582+
),
583+
];
584+
await Promise.all(
585+
parentDirs.map(dir => this.ensureDirectory(dir))
586+
);
587+
if (fileIo.accessSync(firstTarget)) {
588+
await fileIo.unlink(firstTarget);
589+
}
590+
saveFileToSandbox(fromContent, firstTarget);
591+
await Promise.all(
592+
restTargets.map(target => this.copySandboxFile(firstTarget, target)),
593+
);
594+
} finally {
595+
try {
596+
await resourceManager.closeRawFd(currentFrom);
597+
} catch (closeError) {
598+
console.error(`Failed to close raw fd for ${currentFrom}:`, closeError);
599+
}
567600
}
568-
saveFileToSandbox(fromContent, firstTarget);
569-
await Promise.all(
570-
restTargets.map(target => this.copySandboxFile(firstTarget, target)),
571-
);
572601
}
573602
} catch (error) {
574603
error.message =
@@ -589,7 +618,7 @@ export class DownloadTask {
589618
params.unzipDirectory,
590619
params.hash || '',
591620
params.originHash || '',
592-
7,
621+
3,
593622
);
594623
} catch (error) {
595624
error.message = 'Cleanup failed:' + error.message;

harmony/pushy/src/main/ets/UpdateContext.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -513,7 +513,7 @@ export class UpdateContext {
513513
this.rootDir,
514514
state.currentVersion || '',
515515
state.lastVersion || '',
516-
7,
516+
3,
517517
);
518518
}
519519

ios/RCTPushy/RCTPushy.mm

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -748,7 +748,7 @@ - (void)clearInvalidFiles
748748
PushyToStdString(downloadDir),
749749
state.current_version,
750750
state.last_version,
751-
7
751+
3
752752
);
753753
if (!status.ok) {
754754
RCTLogWarn(@"Pushy cleanup error: %s", status.message.c_str());

0 commit comments

Comments
 (0)