Skip to content

Commit dcd66cb

Browse files
committed
fs update;
1 parent d6a79b9 commit dcd66cb

File tree

11 files changed

+211
-47
lines changed

11 files changed

+211
-47
lines changed

FS.common.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -637,7 +637,7 @@ var RNFS = {
637637
},
638638

639639
MainBundlePath: RNFSManager.RNFSMainBundlePath,
640-
CachesDirectoryPath: RNFSManager.RNFSCachesDirectoryPath,
640+
CachesDirectoryPath: (isIOS || isWindows )? RNFSManager.RNFSCachesDirectoryPath:RNFSManager.getConstants().FileCachePath,
641641
ExternalCachesDirectoryPath: RNFSManager.RNFSExternalCachesDirectoryPath,
642642
DocumentDirectoryPath: (isIOS || isWindows )? RNFSManager.RNFSDocumentDirectoryPath:RNFSManager.getConstants().FileSandBoxPath,
643643
DownloadDirectoryPath: RNFSManager.RNFSDownloadDirectoryPath,

harmony/fs.har

748 Bytes
Binary file not shown.

harmony/fs/BuildProfile.ets

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
export default class BuildProfile {
2-
static readonly BUNDLE_NAME = 'com.rnoh.tester';
3-
static readonly VERSION_CODE = 1000000;
4-
static readonly VERSION_NAME = '1.0.0';
52
static readonly HAR_VERSION = '1.0.0';
6-
static readonly BUILD_MODE_NAME = 'Debug';
3+
static readonly BUILD_MODE_NAME = 'debug';
74
static readonly DEBUG = true;
85
}

harmony/fs/hvigorfile.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,24 @@
1+
/**
2+
* MIT License
3+
*
4+
* Copyright (C) 2024 Huawei Device Co., Ltd.
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in all
14+
* copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*/
124
export { harTasks } from '@ohos/hvigor-ohos-plugin';

harmony/fs/index.ets

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,25 @@
1+
/**
2+
* MIT License
3+
*
4+
* Copyright (C) 2024 Huawei Device Co., Ltd.
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in all
14+
* copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*/
24+
125
export * from './ts'

harmony/fs/src/main/cpp/FsPackage.h

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,27 @@
1+
/**
2+
* MIT License
3+
*
4+
* Copyright (C) 2024 Huawei Device Co., Ltd.
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in all
14+
* copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*/
24+
125
#include "RNOH/Package.h"
226
#include "FsTurboModule.h"
327

harmony/fs/src/main/cpp/FsTurboModule.cpp

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,25 @@
1-
21
/**
3-
* This code was generated by [react-native-codegen](https://www.npmjs.com/package/react-native-codegen).
2+
* MIT License
3+
*
4+
* Copyright (C) 2024 Huawei Device Co., Ltd.
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
412
*
5-
* Do not edit this file as changes may cause incorrect behavior and will be lost
6-
* once the code is regenerated.
13+
* The above copyright notice and this permission notice shall be included in all
14+
* copies or substantial portions of the Software.
715
*
8-
* @generated by codegen project: GenerateModuleJniCpp.js
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
923
*/
1024

1125
#include "FsTurboModule.h"

harmony/fs/src/main/cpp/FsTurboModule.h

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,26 @@
1+
/**
2+
* MIT License
3+
*
4+
* Copyright (C) 2024 Huawei Device Co., Ltd.
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in all
14+
* copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*/
124
// NOTE: This entire file should be codegen'ed.
225
#pragma once
326
#include "RNOH/ArkTSTurboModule.h"

harmony/fs/src/main/ets/FsPackage.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,27 @@
1+
/**
2+
* MIT License
3+
*
4+
* Copyright (C) 2024 Huawei Device Co., Ltd.
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in all
14+
* copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*/
24+
125
import { RNPackage, TurboModulesFactory } from 'rnoh/ts';
226
import type { TurboModule, TurboModuleContext } from 'rnoh/ts';
327
import { FsTurboModule } from './FsTurboModule';

harmony/fs/src/main/ets/FsTurboModule.ts

Lines changed: 48 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,51 @@
1+
/**
2+
* MIT License
3+
*
4+
* Copyright (C) 2024 Huawei Device Co., Ltd.
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in all
14+
* copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*/
24+
125
import { TurboModule, RNOHError } from 'rnoh/ts';
226
import fs from '@ohos.file.fs';
327
import { BusinessError } from '@ohos.base';
428
import common from '@ohos.app.ability.common';
529
import util from '@ohos.util';
630
import buffer from '@ohos.buffer';
7-
import HashMap from '@ohos.util.HashMap';
831

932
let context = getContext(this) as common.ApplicationContext; // ApplicationContext
10-
// 沙箱路径
11-
let pathDir = context.filesDir
12-
1333

1434
export class FsTurboModule extends TurboModule {
1535
// 常量路径
1636
getConstants(): object {
17-
// 沙箱路径
1837
return {
38+
// 沙箱路径
1939
FileSandBoxPath: context.filesDir,
40+
// 缓存路径
41+
FileCachePath: context.cacheDir,
2042
}
2143
};
2244

2345
// 读取文件内容
2446
readFile(path: string): Promise<string> {
2547
return new Promise((resolve, reject) => {
26-
fs.readText((pathDir + '/' + path), (err: BusinessError, content: string) => {
48+
fs.readText(path, (err: BusinessError, content: string) => {
2749
if (err) {
2850
reject('Failed to read the file');
2951
} else {
@@ -37,7 +59,7 @@ export class FsTurboModule extends TurboModule {
3759
// 判断文件是否存在
3860
exists(path: string): Promise<boolean> {
3961
return new Promise((resolve, reject) => {
40-
fs.access((pathDir + '/' + path), (err: BusinessError, result: boolean) => {
62+
fs.access(path, (err: BusinessError, result: boolean) => {
4163
if (err) {
4264
reject('File does not exist');
4365
} else {
@@ -50,33 +72,29 @@ export class FsTurboModule extends TurboModule {
5072
// 创建文件
5173
mkdir(path: string): Promise<void> {
5274
return new Promise(async (resolve, reject) => {
53-
let result: string[] = path.split('/');
54-
let mkdirPath = pathDir;
55-
for (let i = 0; i < result.length; i++) {
56-
mkdirPath += '/' + result[i];
57-
let access = await fs.access(mkdirPath);
58-
if (!access) {
59-
fs.mkdir(mkdirPath, (err: BusinessError) => {
60-
if (err) {
61-
reject('Directory could not be created: ' + result[i]);
62-
} else {
63-
resolve();
64-
}
65-
});
75+
fs.mkdir(path, true, (err: BusinessError) => {
76+
if (err) {
77+
if (err.code == 13900015) {
78+
// 文件夹存在
79+
resolve();
80+
} else {
81+
reject(`Directory could not be created ${err.message} ${err.code}`);
82+
}
83+
} else {
84+
resolve();
6685
}
67-
}
86+
})
6887
})
69-
};
88+
}
89+
7090

7191
// 写入文件内容
7292
writeFile(path: string, contentStr: string): Promise<void> {
7393
return new Promise((resolve, reject) => {
7494
// base64 decode 解码
7595
let result = buffer.from(contentStr, 'base64').toString('utf8');
76-
// 文件路径
77-
let filePath = pathDir + '/' + path;
7896
// 读写创建 文件不存在则创建文件
79-
let file = fs.openSync(filePath, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE);
97+
let file = fs.openSync(path, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE);
8098
fs.write(file.fd, result, (err: BusinessError, writeLen: number) => {
8199
if (err) {
82100
reject('Directory could not be created');
@@ -93,10 +111,8 @@ export class FsTurboModule extends TurboModule {
93111
return new Promise((resolve, reject) => {
94112
// base64 decode 解码
95113
let result = buffer.from(contentStr, 'base64').toString('utf8');
96-
// 文件路径
97-
let filePath = pathDir + '/' + path;
98114
// 读写创建 文件内容追加到末尾
99-
let file = fs.openSync(filePath, fs.OpenMode.READ_WRITE | fs.OpenMode.APPEND);
115+
let file = fs.openSync(path, fs.OpenMode.READ_WRITE | fs.OpenMode.APPEND);
100116
fs.write(file.fd, result, (err: BusinessError, writeLen: number) => {
101117
if (err) {
102118
reject('Directory could not be created');
@@ -131,16 +147,13 @@ export class FsTurboModule extends TurboModule {
131147
// 将位于from的文件复制到into。
132148
copyFile(from: string, into: string): Promise<void> {
133149
return new Promise((resolve, reject) => {
134-
// 文件路径
135-
let filePath = pathDir + '/' + from;
136-
let destPath = pathDir + '/' + into;
137-
let fromResult: string[] = filePath.split('/');
138-
let intoResult: string[] = destPath.split('/');
150+
let fromResult: string[] = from.split('/');
151+
let intoResult: string[] = into.split('/');
139152
if (fromResult[fromResult.length-1] === intoResult[intoResult.length-1]) {
140153
reject(new Error('The file already exists.'));
141154
return;
142155
}
143-
fs.copyFile(filePath, destPath, (err: BusinessError) => {
156+
fs.copyFile(from, into, (err: BusinessError) => {
144157
if (err) {
145158
reject(err.message);
146159
} else {
@@ -153,9 +166,7 @@ export class FsTurboModule extends TurboModule {
153166
// 删除文件
154167
unlink(path: string): Promise<void> {
155168
return new Promise((resolve, reject) => {
156-
// 文件路径
157-
let filePath = pathDir + '/' + path;
158-
fs.rmdir(filePath, (err: BusinessError) => {
169+
fs.rmdir(path, (err: BusinessError) => {
159170
if (err) {
160171
reject('FilePath does not exist');
161172
} else {

harmony/fs/ts.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,26 @@
1+
/**
2+
* MIT License
3+
*
4+
* Copyright (C) 2024 Huawei Device Co., Ltd.
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in all
14+
* copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*/
24+
125
export * from "./src/main/ets/FsPackage"
226
export * from "./src/main/ets/FsTurboModule"

0 commit comments

Comments
 (0)