Skip to content

Commit 7d40c80

Browse files
committed
fix harmony file io
1 parent 0e619e4 commit 7d40c80

File tree

6 files changed

+74
-26
lines changed

6 files changed

+74
-26
lines changed

harmony/pushy/BuildProfile.ets

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
/**
22
* Use these variables when you tailor your ArkTS code. They must be of the const type.
33
*/
4-
export const HAR_VERSION = '3.1.0-0.0.7';
4+
import packageJson from '../../package.json'
5+
6+
export const HAR_VERSION = packageJson.version;
57
export const BUILD_MODE_NAME = 'debug';
68
export const DEBUG = true;
79
export const TARGET_NAME = 'default';

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

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { zlib } from '@kit.BasicServicesKit';
55
import { EventHub } from './EventHub';
66
import { DownloadTaskParams } from './DownloadTaskParams';
77
import Pushy from 'librnupdate.so';
8+
import { saveFileToSandbox } from './SaveFile';
89

910
interface ZipEntry {
1011
filename: string;
@@ -233,7 +234,8 @@ export class DownloadTask {
233234

234235
return { entries };
235236
} catch (error) {
236-
console.error('Failed to process unzipped files:', error);
237+
error.message = 'Failed to process unzipped files:' + error.message;
238+
console.error(error);
237239
throw error;
238240
}
239241
}
@@ -262,10 +264,6 @@ export class DownloadTask {
262264

263265
const copies = obj.copies as Record<string, string>;
264266
for (const [to, rawPath] of Object.entries(copies)) {
265-
if (!rawPath.startsWith('resources/rawfile/')) {
266-
// skip other resource
267-
continue;
268-
}
269267
let from = rawPath.replace('resources/rawfile/', '');
270268
if (from === '') {
271269
from = to;
@@ -311,7 +309,7 @@ export class DownloadTask {
311309
await fileIo.close(writer);
312310
continue;
313311
} catch (error) {
314-
console.error('Failed to process bundle patch:', error);
312+
error.message = 'Failed to process bundle patch:' + error.message;
315313
throw error;
316314
}
317315
}
@@ -432,15 +430,27 @@ export class DownloadTask {
432430

433431
for (const [from, targets] of copyList.entries()) {
434432
currentFrom = from;
435-
const fromContent = await resourceManager.getRawFileContent(from);
433+
if (from.startsWith('resources/base/media/')) {
434+
const mediaName = from
435+
.replace('resources/base/media/', '')
436+
.split('.')[0];
437+
const mediaBuffer = await resourceManager.getMediaByName(mediaName);
438+
for (const target of targets) {
439+
const fileStream = fileIo.createStreamSync(target, 'w+');
440+
fileStream.writeSync(mediaBuffer);
441+
fileStream.close();
442+
}
443+
continue;
444+
}
445+
const fromContent = await resourceManager.getRawFd(from);
436446
for (const target of targets) {
437-
const fileStream = fileIo.createStreamSync(target, 'w+');
438-
fileStream.writeSync(fromContent.buffer);
439-
fileStream.close();
447+
saveFileToSandbox(fromContent, target);
440448
}
441449
}
442450
} catch (error) {
443-
console.error('Copy from resource failed:', currentFrom, error.message);
451+
error.message =
452+
'Copy from resource failed:' + currentFrom + ',' + error.message;
453+
console.error(error);
444454
throw error;
445455
}
446456
}
@@ -473,7 +483,8 @@ export class DownloadTask {
473483
}
474484
}
475485
} catch (error) {
476-
console.error('Cleanup failed:', error);
486+
error.message = 'Cleanup failed:' + error.message;
487+
console.error(error);
477488
throw error;
478489
}
479490
}

harmony/pushy/src/main/ets/PushyFileJSBundleProvider.ets renamed to harmony/pushy/src/main/ets/PushyFileJSBundleProvider.ts

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
11
import {
22
FileJSBundle,
3-
HotReloadConfig,
43
JSBundleProvider,
5-
JSBundleProviderError
4+
JSBundleProviderError,
65
} from '@rnoh/react-native-openharmony';
76
import common from '@ohos.app.ability.common';
87
import fs from '@ohos.file.fs';
98
import { UpdateContext } from './UpdateContext';
109

1110
export class PushyFileJSBundleProvider extends JSBundleProvider {
1211
private updateContext: UpdateContext;
13-
private path: string = ''
12+
private path: string = '';
1413

1514
constructor(context: common.UIAbilityContext) {
1615
super();
@@ -26,24 +25,26 @@ export class PushyFileJSBundleProvider extends JSBundleProvider {
2625
if (!this.path) {
2726
throw new JSBundleProviderError({
2827
whatHappened: 'No pushy bundle found. using default bundle',
29-
howCanItBeFixed: ['']
30-
})
28+
howCanItBeFixed: [''],
29+
});
3130
}
3231
try {
3332
await fs.access(this.path, fs.OpenMode.READ_ONLY);
3433
return {
35-
filePath: this.path
36-
}
34+
filePath: this.path,
35+
};
3736
} catch (error) {
3837
throw new JSBundleProviderError({
3938
whatHappened: `Couldn't load JSBundle from ${this.path}`,
4039
extraData: error,
41-
howCanItBeFixed: [`Check if a bundle exists at "${this.path}" on your device.`]
42-
})
40+
howCanItBeFixed: [
41+
`Check if a bundle exists at "${this.path}" on your device.`,
42+
],
43+
});
4344
}
4445
}
4546

4647
getAppKeys(): string[] {
4748
return [];
4849
}
49-
}
50+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import resourceManager from '@ohos.resourceManager';
2+
import fs, { ReadOptions } from '@ohos.file.fs';
3+
4+
export const saveFileToSandbox = (
5+
from: resourceManager.RawFileDescriptor,
6+
toPath: string,
7+
) => {
8+
let to = fs.openSync(toPath, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE);
9+
10+
let bufferSize = 30000;
11+
let buffer = new ArrayBuffer(bufferSize); // 创建buffer缓冲区
12+
// 要copy的文件的offset和length
13+
let currentOffset = from.offset;
14+
let readOption: ReadOptions = {
15+
offset: currentOffset, // 期望读取文件的位置。可选,默认从当前位置开始读
16+
length: bufferSize, // 每次期望读取数据的长度。可选,默认缓冲区长度
17+
};
18+
// 后面len会一直减,直到没有
19+
while (true) {
20+
// 读取buffer容量的内容
21+
let readLength = fs.readSync(from.fd, buffer, readOption);
22+
// 写入buffer容量的内容
23+
fs.writeSync(to.fd, buffer, { length: readLength });
24+
// 判断后续内容,修改读文件的参数
25+
// buffer没读满代表文件读完了
26+
if (readLength < bufferSize) {
27+
break;
28+
}
29+
if (readOption.offset) {
30+
readOption.offset += readLength;
31+
}
32+
}
33+
fs.close(to);
34+
};

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ export class UpdateModuleImpl {
6969
): Promise<boolean> {
7070
const hash = options.hash;
7171
if (!hash) {
72-
throw Error('hash不能为空');
72+
throw Error('empty hash');
7373
}
7474

7575
try {
@@ -87,7 +87,7 @@ export class UpdateModuleImpl {
8787
return true;
8888
} catch (error) {
8989
logger.error(TAG, `markSuccess failed: ${error}`);
90-
throw Error(`执行报错: ${error.message}`);
90+
throw error;
9191
}
9292
}
9393

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-native-update",
3-
"version": "10.37.6",
3+
"version": "10.37.7",
44
"description": "react-native hot update",
55
"main": "src/index",
66
"scripts": {

0 commit comments

Comments
 (0)