From 153c19cca4c1bf58555fae10c5a7e1d0618b5f68 Mon Sep 17 00:00:00 2001 From: liuhaotian Date: Thu, 23 May 2024 11:54:29 +0800 Subject: [PATCH 1/5] add TS stream Signed-off-by: liuhaotian Change-Id: Iefe9918f629a81717af35876b431abb4d0c0e2da --- .../kits/ts/src/native_module_streamhash.cpp | 65 +++++++ .../kits/ts/src/native_module_streamrw.cpp | 65 +++++++ interfaces/kits/ts/src/streamhash.ts | 56 ++++++ interfaces/kits/ts/src/streamrw.ts | 182 ++++++++++++++++++ 4 files changed, 368 insertions(+) create mode 100644 interfaces/kits/ts/src/native_module_streamhash.cpp create mode 100644 interfaces/kits/ts/src/native_module_streamrw.cpp create mode 100644 interfaces/kits/ts/src/streamhash.ts create mode 100644 interfaces/kits/ts/src/streamrw.ts diff --git a/interfaces/kits/ts/src/native_module_streamhash.cpp b/interfaces/kits/ts/src/native_module_streamhash.cpp new file mode 100644 index 000000000..d34a601f8 --- /dev/null +++ b/interfaces/kits/ts/src/native_module_streamhash.cpp @@ -0,0 +1,65 @@ +/* + * Copyright (c) 2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "napi/native_api.h" +#include "napi/native_node_api.h" + +extern const char _binary_streamhash_js_start[]; +extern const char _binary_streamhash_js_end[]; +extern const char _binary_streamhash_abc_start[]; +extern const char _binary_streamhash_abc_end[]; + +static napi_value Init(napi_env env, napi_value exports) +{ + return exports; +} + +extern "C" +__attribute__((visibility("default"))) void NAPI_file_streamhash_GetJSCode(const char **buf, int *bufLen) +{ + if (buf != nullptr) { + *buf = _binary_streamhash_js_start; + } + + if (bufLen != nullptr) { + *bufLen = _binary_streamhash_js_end - _binary_streamhash_js_start; + } +} +extern "C" +__attribute__((visibility("default"))) void NAPI_file_streamhash_GetABCCode(const char** buf, int* buflen) +{ + if (buf != nullptr) { + *buf = _binary_streamhash_abc_start; + } + if (buflen != nullptr) { + *buflen = _binary_streamhash_abc_end - _binary_streamhash_abc_start; + } +} + +static napi_module_with_js streamhashModule = { + .nm_version = 1, + .nm_flags = 0, + .nm_filename = nullptr, + .nm_register_func = Init, + .nm_modname = "streamhash", + .nm_priv = ((void*)0), + .nm_get_abc_code = NAPI_file_streamhash_GetABCCode, + .nm_get_js_code = NAPI_file_streamhash_GetJSCode, +}; + +extern "C" __attribute__ ((constructor)) void StreamhashRegisterModule() +{ + napi_module_with_js_register(&streamhashModule); +} diff --git a/interfaces/kits/ts/src/native_module_streamrw.cpp b/interfaces/kits/ts/src/native_module_streamrw.cpp new file mode 100644 index 000000000..7145c378e --- /dev/null +++ b/interfaces/kits/ts/src/native_module_streamrw.cpp @@ -0,0 +1,65 @@ +/* + * Copyright (c) 2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "napi/native_api.h" +#include "napi/native_node_api.h" + +extern const char _binary_streamrw_js_start[]; +extern const char _binary_streamrw_js_end[]; +extern const char _binary_streamrw_abc_start[]; +extern const char _binary_streamrw_abc_end[]; + +static napi_value Init(napi_env env, napi_value exports) +{ + return exports; +} + +extern "C" +__attribute__((visibility("default"))) void NAPI_file_streamrw_GetJSCode(const char **buf, int *bufLen) +{ + if (buf != nullptr) { + *buf = _binary_streamrw_js_start; + } + + if (bufLen != nullptr) { + *bufLen = _binary_streamrw_js_end - _binary_streamrw_js_start; + } +} +extern "C" +__attribute__((visibility("default"))) void NAPI_file_streamrw_GetABCCode(const char** buf, int* buflen) +{ + if (buf != nullptr) { + *buf = _binary_streamrw_abc_start; + } + if (buflen != nullptr) { + *buflen = _binary_streamrw_abc_end - _binary_streamrw_abc_start; + } +} + +static napi_module_with_js streamrwModule = { + .nm_version = 1, + .nm_flags = 0, + .nm_filename = nullptr, + .nm_register_func = Init, + .nm_modname = "streamrw", + .nm_priv = ((void*)0), + .nm_get_abc_code = NAPI_file_streamrw_GetABCCode, + .nm_get_js_code = NAPI_file_streamrw_GetJSCode, +}; + +extern "C" __attribute__ ((constructor)) void StreamrwRegisterModule() +{ + napi_module_with_js_register(&streamrwModule); +} diff --git a/interfaces/kits/ts/src/streamhash.ts b/interfaces/kits/ts/src/streamhash.ts new file mode 100644 index 000000000..65a2e9d9f --- /dev/null +++ b/interfaces/kits/ts/src/streamhash.ts @@ -0,0 +1,56 @@ +/* + * Copyright (c) 2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +declare function requireNapi(napiModuleName: string): any; +const stream = requireNapi('util.stream'); +const hash = requireNapi('file.hash'); + +class HashStream extends stream.Transform { + // @ts-ignore + hs: hash.HashStream; + hashBuf?: ArrayBuffer; + + constructor(algorithm: string) { + super(); + this.hs = new hash.HashStream(algorithm); + } + + digest(): string { + return this.hs.digest(); + } + + update(data: ArrayBuffer) { + this.hs.update(data); + } + + doTransform(chunk: string, encoding: string, callback: Function) { + const buf = new Uint8Array(chunk.split('').map(x => x.charCodeAt(0))).buffer; + this.hs.update(buf); + this.push(chunk); + callback(); + } + + doWrite(chunk: string | Uint8Array, encoding: string, callback: Function) { + callback(); + } + + doFlush(callback: Function) { + callback(); + } +} + +export default { + HashStream: HashStream, +} diff --git a/interfaces/kits/ts/src/streamrw.ts b/interfaces/kits/ts/src/streamrw.ts new file mode 100644 index 000000000..9ddd5fc83 --- /dev/null +++ b/interfaces/kits/ts/src/streamrw.ts @@ -0,0 +1,182 @@ +/* + * Copyright (c) 2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +declare function requireNapi(napiModuleName: string): any; +const stream = requireNapi('util.stream'); +const fileIo = requireNapi('file.fs'); + +interface ReadStreamOptions { + start?: number; + end?: number; +} + +interface WriteStreamOptions { + start?: number; + mode?: number; +} + +class ReadStream extends stream.Readable { + private pathInner: string; + private bytesReadInner: number; + private offset: number; + private start?: number; + private end?: number; + // @ts-ignore + private stream?: fileIo.Stream; + + constructor(path: string, options?: ReadStreamOptions) { + super(); + this.pathInner = path; + this.bytesReadInner = 0; + this.start = options?.start; + this.end = options?.end; + this.stream = fileIo.createStreamSync(this.pathInner, 'r'); + this.offset = this.start ?? 0; + } + + get path() { + return this.pathInner; + } + + get bytesRead() { + return this.bytesReadInner; + } + + // @ts-ignore + seek(offset: number, whence?: fileIo.WhenceType) { + if (whence == undefined) { + this.offset = this.stream?.seek(offset); + } else { + this.offset = this.stream?.seek(offset, whence); + } + } + + close() { + this.stream?.close(); + } + + doInitialize(callback: Function) { + callback(); + } + + doRead(size: number) { + let readSize = size; + if (this.end !== undefined) { + if (this.offset > this.end) { + this.push(null); + return; + } + if (this.offset + readSize > this.end) { + readSize = this.end - this.offset; + } + } + let buffer = new ArrayBuffer(readSize); + const off = this.offset; + this.offset += readSize; + this.stream?.read(buffer, { offset: off, length: readSize }) + .then((readOut: number) => { + if (readOut > 0) { + this.bytesReadInner += readOut; + this.push(new Uint8Array(buffer.slice(0, readOut))); + } + if (readOut != readSize || readOut < size) { + this.offset = this.offset - readSize + readOut; + this.push(null); + } + }) + } +} + +class WriteStream extends stream.Writable { + private pathInner: string; + private bytesWrittenInner: number; + private offset: number; + private mode: string; + private start?: number; + // @ts-ignore + private stream?: fileIo.Stream; + + constructor(path: string, options?: WriteStreamOptions) { + super(); + this.pathInner = path; + this.bytesWrittenInner = 0; + this.start = options?.start; + this.mode = this.convertOpenMode(options?.mode); + this.stream = fileIo.createStreamSync(this.pathInner, this.mode); + this.offset = this.start ?? 0; + } + + get path() { + return this.pathInner; + } + + get bytesWritten() { + return this.bytesWrittenInner; + } + + // @ts-ignore + seek(offset: number, whence?: fileIo.WhenceType) { + if (whence == undefined) { + this.offset = this.stream?.seek(offset); + } else { + this.offset = this.stream?.seek(offset, whence); + } + } + + close() { + this.stream?.close(); + } + + doInitialize(callback: Function) { + callback(); + } + + doWrite(chunk: string | Uint8Array, encoding: string, callback: Function) { + this.stream?.write(chunk, { offset: this.offset }) + .then((writeIn: number) => { + this.offset += writeIn; + this.bytesWrittenInner += writeIn; + callback(); + }) + .finally(() => { + this.stream?.flush(); + }) + } + + convertOpenMode(mode?: number): string { + let modeStr = 'w'; + if (mode === undefined) { + return modeStr; + } + if (mode & fileIo.OpenMode.WRITE_ONLY) { + modeStr = 'w'; + } + if (mode & fileIo.OpenMode.READ_WRITE) { + modeStr = 'w+'; + } + if ((mode & fileIo.OpenMode.WRITE_ONLY) && (mode & fileIo.OpenMode.APPEND)) { + modeStr = "a" + } + if ((mode & fileIo.OpenMode.READ_WRITE) && (mode & fileIo.OpenMode.APPEND)) { + modeStr = "a+" + } + return modeStr; + } +} + +export default { + ReadStream: ReadStream, + WriteStream: WriteStream, +} -- Gitee From b13f2b4253164acef8810750b333dae5f4fc1802 Mon Sep 17 00:00:00 2001 From: liuhaotian Date: Thu, 23 May 2024 11:55:07 +0800 Subject: [PATCH 2/5] add TS build Signed-off-by: liuhaotian Change-Id: I6b38fda1bcf2e825b71413bfeb6fcf80429b8142 --- interfaces/kits/ts/BUILD.gn | 183 ++++++++++++++++++++++++++ interfaces/kits/ts/build_ts_js.py | 61 +++++++++ interfaces/kits/ts/common_gen_obj.gni | 93 +++++++++++++ interfaces/kits/ts/tsconfig.json | 13 ++ 4 files changed, 350 insertions(+) create mode 100644 interfaces/kits/ts/BUILD.gn create mode 100755 interfaces/kits/ts/build_ts_js.py create mode 100644 interfaces/kits/ts/common_gen_obj.gni create mode 100644 interfaces/kits/ts/tsconfig.json diff --git a/interfaces/kits/ts/BUILD.gn b/interfaces/kits/ts/BUILD.gn new file mode 100644 index 000000000..fbf8f8165 --- /dev/null +++ b/interfaces/kits/ts/BUILD.gn @@ -0,0 +1,183 @@ +# Copyright (c) 2024 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import("//arkcompiler/ets_frontend/es2panda/es2abc_config.gni") +import("//foundation/filemanagement/file_api/file_api.gni") +import( + "//foundation/filemanagement/file_api/interfaces/kits/ts/common_gen_obj.gni") + +# compile .ts to .js. +action("build_streamrw_js") { + script = "${file_api_path}/interfaces/kits/ts/build_ts_js.py" + + outFile_Path = target_out_dir + "/" + current_cpu + args = [ + "--dst-file", + rebase_path(target_out_dir + "/streamrw.js"), + "--module-path", + rebase_path("/foundation/filemanagement/file_api/interfaces/kits/ts"), + "--out-file", + rebase_path(outFile_Path + "/streamrw.js"), + "--out-filePath", + rebase_path(outFile_Path), + "--relative-path", + rebase_path("//", root_build_dir), + ] + outputs = [ target_out_dir + "/streamrw.js" ] +} + +# compile .js to .abc. +es2abc_gen_abc("gen_streamrw_abc") { + extra_visibility = [ ":*" ] + src_js = rebase_path(target_out_dir + "/streamrw.js") + dst_file = rebase_path(target_out_dir + "/streamrw.abc") + in_puts = [ target_out_dir + "/streamrw.js" ] + out_puts = [ target_out_dir + "/streamrw.abc" ] + extra_args = [ "--module" ] + extra_dependencies = [ ":build_streamrw_js" ] +} + +abc_output_path = get_label_info(":streamrw_abc", "target_out_dir") + +gen_obj("streamrw_js") { + input = "$target_out_dir/streamrw.js" + if (use_mac || use_mingw_win || use_ios || use_linux) { + stream_js_obj_path = abc_output_path + "/streamrw.c" + } else { + stream_js_obj_path = abc_output_path + "/streamrw.o" + } + output = stream_js_obj_path + snapshot_dep = [ ":build_streamrw_js" ] +} + +gen_obj("streamrw_abc") { + input = "$target_out_dir/streamrw.abc" + if (use_mac || use_mingw_win || use_ios || use_linux) { + stream_js_obj_path = abc_output_path + "/streamrw_abc.c" + } else { + stream_js_obj_path = abc_output_path + "/streamrw_abc.o" + } + output = stream_js_obj_path + snapshot_dep = [ ":gen_streamrw_abc" ] +} + +ohos_shared_library("streamrw") { + include_dirs = [ "include" ] + sources = [ "src/native_module_streamrw.cpp" ] + + branch_protector_ret = true + sanitize = { + cfi = true + cfi_cross_dso = true + debug = false + } + deps = [ + ":gen_obj_src_streamrw_abc", + ":gen_obj_src_streamrw_js", + ] + external_deps = [ + "hilog:libhilog", + "napi:ace_napi", + ] + subsystem_name = "filemanagement" + part_name = "file_api" + + relative_install_dir = "module/file" +} + +# hash +# compile .ts to .js. +action("build_streamhash_js") { + script = "${file_api_path}/interfaces/kits/ts/build_ts_js.py" + + outFile_Path = target_out_dir + "/" + current_cpu + args = [ + "--dst-file", + rebase_path(target_out_dir + "/streamhash.js"), + "--module-path", + rebase_path("/foundation/filemanagement/file_api/interfaces/kits/ts"), + "--out-file", + rebase_path(outFile_Path + "/streamhash.js"), + "--out-filePath", + rebase_path(outFile_Path), + "--relative-path", + rebase_path("//", root_build_dir), + ] + outputs = [ target_out_dir + "/streamhash.js" ] +} + +# compile .js to .abc. +es2abc_gen_abc("gen_streamhash_abc") { + extra_visibility = [ ":*" ] + src_js = rebase_path(target_out_dir + "/streamhash.js") + dst_file = rebase_path(target_out_dir + "/streamhash.abc") + in_puts = [ target_out_dir + "/streamhash.js" ] + out_puts = [ target_out_dir + "/streamhash.abc" ] + extra_args = [ "--module" ] + extra_dependencies = [ ":build_streamhash_js" ] +} + +abc_output_path = get_label_info(":streamhash_abc", "target_out_dir") + +gen_obj("streamhash_js") { + input = "$target_out_dir/streamhash.js" + if (use_mac || use_mingw_win || use_ios || use_linux) { + stream_js_obj_path = abc_output_path + "/streamhash.c" + } else { + stream_js_obj_path = abc_output_path + "/streamhash.o" + } + output = stream_js_obj_path + snapshot_dep = [ ":build_streamhash_js" ] +} + +gen_obj("streamhash_abc") { + input = "$target_out_dir/streamhash.abc" + if (use_mac || use_mingw_win || use_ios || use_linux) { + stream_js_obj_path = abc_output_path + "/streamhash_abc.c" + } else { + stream_js_obj_path = abc_output_path + "/streamhash_abc.o" + } + output = stream_js_obj_path + snapshot_dep = [ ":gen_streamhash_abc" ] +} + +ohos_shared_library("streamhash") { + include_dirs = [ "include" ] + sources = [ "src/native_module_streamhash.cpp" ] + + branch_protector_ret = true + sanitize = { + cfi = true + cfi_cross_dso = true + debug = false + } + deps = [ + ":gen_obj_src_streamhash_abc", + ":gen_obj_src_streamhash_js", + ] + external_deps = [ + "hilog:libhilog", + "napi:ace_napi", + ] + subsystem_name = "filemanagement" + part_name = "file_api" + + relative_install_dir = "module/file" +} + +group("build_kits_ts") { + public_deps = [ + ":streamhash", + ":streamrw", + ] +} diff --git a/interfaces/kits/ts/build_ts_js.py b/interfaces/kits/ts/build_ts_js.py new file mode 100755 index 000000000..43ffdfd70 --- /dev/null +++ b/interfaces/kits/ts/build_ts_js.py @@ -0,0 +1,61 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# Copyright (c) 2024 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import os +import shutil +import argparse +import subprocess +import sys + +def run_command(in_cmd): + print(" ".join(in_cmd)) + proc = subprocess.Popen(in_cmd, stdout=subprocess.PIPE, + stderr=subprocess.PIPE, + universal_newlines=True, + shell=False) + stdout, stderr = proc.communicate() + if stdout != "": + raise Exception(stdout) + +if __name__ == '__main__': + PARSER_INST = argparse.ArgumentParser() + PARSER_INST.add_argument('--dst-file', + help='the converted target file') + PARSER_INST.add_argument('--module-path', + help='the module path') + PARSER_INST.add_argument('--out-file', + help='js output file') + PARSER_INST.add_argument('--out-filePath', + help='js output filePath') + PARSER_INST.add_argument('--relative-path', + help='the code root path relative the root_build_dir') + INPUT_ARGUMENTS = PARSER_INST.parse_args() + + BUILD_PATH = os.path.abspath(os.path.join(os.getcwd(), INPUT_ARGUMENTS.relative_path)) + os.chdir(("%s" + INPUT_ARGUMENTS.module_path) % BUILD_PATH) + os_name = sys.platform + NODE_PATH = '../../../../../../prebuilts/build-tools/common/nodejs/current/bin/node' + if not os.path.exists(NODE_PATH): + raise Exception('error:NO such file or directory') + TSC_PATH = '../../../../../../third_party/typescript/bin/tsc' + CMD_INST = [NODE_PATH, TSC_PATH, "--outDir", INPUT_ARGUMENTS.out_filePath] + run_command(CMD_INST) + if not os.path.exists(INPUT_ARGUMENTS.out_file): + raise Exception('error:NO such file or directory') + CMD_INST = shutil.copy(INPUT_ARGUMENTS.out_file, INPUT_ARGUMENTS.dst_file) + + CMD_INST = shutil.rmtree(INPUT_ARGUMENTS.out_filePath) + + exit(0) diff --git a/interfaces/kits/ts/common_gen_obj.gni b/interfaces/kits/ts/common_gen_obj.gni new file mode 100644 index 000000000..8ea79ab3b --- /dev/null +++ b/interfaces/kits/ts/common_gen_obj.gni @@ -0,0 +1,93 @@ +# Copyright (c) 2024 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import("//build/config/clang/clang.gni") +import("//build/ohos.gni") + +ace_root = "//foundation/arkui/ace_engine" +is_ohos_standard_system = is_standard_system && !is_arkui_x +use_mingw_win = "${current_os}_${current_cpu}" == "mingw_x86_64" +use_mac = "${current_os}_${current_cpu}" == "mac_x64" || + "${current_os}_${current_cpu}" == "mac_arm64" +use_ios = "${current_os}_${current_cpu}" == "ios_x64" || + "${current_os}_${current_cpu}" == "ios_arm64" +use_linux = "${current_os}_${current_cpu}" == "linux_x64" +windows_buildtool = "//build/toolchain/mingw:mingw_x86_64" + +if (!defined(default_aosp_source_dir)) { + default_aosp_source_dir = "/" +} +objcopy_default = "${default_aosp_source_dir}/prebuilts/gcc/linux-x86/aarch64/aarch64-linux-android-4.9/aarch64-linux-android/bin/objcopy" +objcopy_mingw = "${default_aosp_source_dir}/prebuilts/gcc/linux-x86/host/x86_64-w64-mingw32-4.8/x86_64-w64-mingw32/bin/objcopy" +objcopy_x86_64 = "${default_clang_base_path}/bin/llvm-objcopy" +if ("${current_os}_${current_cpu}" == "mac_arm64") { + mac_buildtool = "//build/toolchain/mac:clang_arm64" +} else if ("${current_os}_${current_cpu}" == "mac_x64") { + mac_buildtool = "//build/toolchain/mac:clang_x64" +} +objcopy_clang = "$clang_base_path/bin/llvm-objcopy" +if (is_ohos_standard_system) { + objcopy_default = "//prebuilts/clang/ohos/linux-x86_64/llvm/bin/llvm-objcopy" +} else if (is_arkui_x) { + if (host_os == "mac") { + objcopy_default = objcopy_clang + } else if (defined(aosp_objcopy)) { + objcopy_default = aosp_objcopy + } +} + +template("gen_obj") { + name = target_name + action("gen_obj_" + name) { + visibility = [ ":*" ] # Only targets in this file can depend on this. + + if (use_mingw_win) { + objcopy_tool = objcopy_mingw + script = "$ace_root/build/tools/build_resource_to_bytecode.py" + } else if (use_mac || target_os == "ios") { + objcopy_tool = objcopy_clang + script = "$ace_root/build/tools/build_resource_to_bytecode.py" + } else if (use_linux) { + objcopy_tool = objcopy_x86_64 + script = "$ace_root/build/tools/build_resource_to_bytecode.py" + } else if (target_cpu == "x86_64") { + objcopy_tool = objcopy_x86_64 + script = "$ace_root/build/tools/run_objcopy.py" + } else { + objcopy_tool = objcopy_default + script = "$ace_root/build/tools/run_objcopy.py" + } + + args = [ + "--objcopy", + rebase_path(objcopy_tool), + "--input", + rebase_path(invoker.input), + "--output", + rebase_path(invoker.output), + "--arch", + current_cpu, + ] + + deps = [] + deps += invoker.snapshot_dep + + inputs = [ invoker.input ] + outputs = [ invoker.output ] + } + + source_set("gen_obj_src_" + name) { + sources = [ invoker.output ] + deps = [ ":gen_obj_" + name ] + } +} \ No newline at end of file diff --git a/interfaces/kits/ts/tsconfig.json b/interfaces/kits/ts/tsconfig.json new file mode 100644 index 000000000..5c9d83dc5 --- /dev/null +++ b/interfaces/kits/ts/tsconfig.json @@ -0,0 +1,13 @@ +{ + "compilerOptions": { + "target": "es6", + "module": "es6", + "rootDir": "./src", + "outDir": "./out", /* Specify an output folder for all emitted files. */ + "esModuleInterop": true, + "forceConsistentCasingInFileNames": true, + "strict": true, + "skipLibCheck": true, + "noImplicitThis": false, + } +} \ No newline at end of file -- Gitee From 914cdcd50aea978d61cb3fa470b396e92cac5451 Mon Sep 17 00:00:00 2001 From: liuhaotian Date: Thu, 23 May 2024 11:56:15 +0800 Subject: [PATCH 3/5] add mod_hash Signed-off-by: liuhaotian Change-Id: I432fe62ad0ab90c139e4abaf632567296feb4138 --- interfaces/kits/js/BUILD.gn | 10 +- .../class_hashstream/hashstream_entity.h | 37 +++ .../hashstream_n_exporter.cpp | 235 ++++++++++++++++++ .../class_hashstream/hashstream_n_exporter.h | 45 ++++ .../js/src/mod_hash/create_streamhash.cpp | 53 ++++ .../kits/js/src/mod_hash/create_streamhash.h | 31 +++ interfaces/kits/js/src/mod_hash/hash.cpp | 2 + interfaces/kits/js/src/mod_hash/module.cpp | 14 +- 8 files changed, 421 insertions(+), 6 deletions(-) create mode 100644 interfaces/kits/js/src/mod_hash/class_hashstream/hashstream_entity.h create mode 100644 interfaces/kits/js/src/mod_hash/class_hashstream/hashstream_n_exporter.cpp create mode 100644 interfaces/kits/js/src/mod_hash/class_hashstream/hashstream_n_exporter.h create mode 100644 interfaces/kits/js/src/mod_hash/create_streamhash.cpp create mode 100644 interfaces/kits/js/src/mod_hash/create_streamhash.h diff --git a/interfaces/kits/js/BUILD.gn b/interfaces/kits/js/BUILD.gn index 4d5108164..01e6a9c0e 100644 --- a/interfaces/kits/js/BUILD.gn +++ b/interfaces/kits/js/BUILD.gn @@ -252,16 +252,24 @@ ohos_shared_library("hash") { debug = false } + use_exceptions = true + subsystem_name = "filemanagement" part_name = "file_api" relative_install_dir = "module/file" - include_dirs = [ "${src_path}/common/file_helper" ] + include_dirs = [ + "${src_path}/common/file_helper", + "${src_path}/mod_hash", + "${src_path}/mod_hash/class_hashstream", + ] sources = [ "src/common/file_helper/fd_guard.cpp", "src/common/file_helper/hash_file.cpp", + "src/mod_hash/class_hashstream/hashstream_n_exporter.cpp", + "src/mod_hash/create_streamhash.cpp", "src/mod_hash/hash.cpp", "src/mod_hash/module.cpp", ] diff --git a/interfaces/kits/js/src/mod_hash/class_hashstream/hashstream_entity.h b/interfaces/kits/js/src/mod_hash/class_hashstream/hashstream_entity.h new file mode 100644 index 000000000..6783f9656 --- /dev/null +++ b/interfaces/kits/js/src/mod_hash/class_hashstream/hashstream_entity.h @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef INTERFACES_KITS_JS_SRC_MOD_HASH_CLASS_HASHSTREAM_HASHSTREAM_ENTITY_H +#define INTERFACES_KITS_JS_SRC_MOD_HASH_CLASS_HASHSTREAM_HASHSTREAM_ENTITY_H + +#include +#include +#include + +#include "hash.h" + +namespace OHOS { +namespace FileManagement { +namespace ModuleFileIO { +struct HashStreamEntity { + MD5_CTX md5Ctx; + SHA_CTX shaCtx; + SHA256_CTX sha256Ctx; + HASH_ALGORITHM_TYPE algType = HASH_ALGORITHM_TYPE_UNSUPPORTED; +}; +} // namespace ModuleFileIO +} // namespace FileManagement +} // namespace OHOS +#endif \ No newline at end of file diff --git a/interfaces/kits/js/src/mod_hash/class_hashstream/hashstream_n_exporter.cpp b/interfaces/kits/js/src/mod_hash/class_hashstream/hashstream_n_exporter.cpp new file mode 100644 index 000000000..697574dc3 --- /dev/null +++ b/interfaces/kits/js/src/mod_hash/class_hashstream/hashstream_n_exporter.cpp @@ -0,0 +1,235 @@ +/* + * Copyright (c) 2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#include "securec.h" +#include "string.h" +#include "hashstream_n_exporter.h" + +#include +#include + +#include "file_utils.h" +#include "hashstream_entity.h" + +namespace OHOS { +namespace FileManagement { +namespace ModuleFileIO { +using namespace std; +using namespace OHOS::FileManagement::LibN; + +static HASH_ALGORITHM_TYPE GetHashAlgorithm(const string &alg) +{ + return (algorithmMaps.find(alg) != algorithmMaps.end()) ? algorithmMaps.at(alg) : HASH_ALGORITHM_TYPE_UNSUPPORTED; +} + +static tuple GetHsEntity(napi_env env, napi_value hs_entity) +{ + auto hsEntity = NClass::GetEntityOf(env, hs_entity); + if (!hsEntity) { + return { false, nullptr }; + } + return { true, hsEntity }; +} + +static string HashFinal(const unique_ptr &hashBuf, size_t hashLen) +{ + stringstream ss; + for (size_t i = 0; i < hashLen; ++i) { + const int hexPerByte = 2; + ss << std::uppercase << std::setfill('0') << std::setw(hexPerByte) << std::hex + << static_cast(hashBuf[i]); + } + + return ss.str(); +} + +napi_value HashStreamNExporter::Constructor(napi_env env, napi_callback_info info) +{ + NFuncArg funcArg(env, info); + if (!funcArg.InitArgs(NARG_CNT::ONE)) { + HILOGE("Number of arguments unmatched"); + NError(EINVAL).ThrowErr(env); + return nullptr; + } + + auto [succAlg, alg, ignore] = NVal(env, funcArg[NARG_POS::FIRST]).ToUTF8String(); + if (!succAlg) { + HILOGE("Invalid algorithm"); + NError(EINVAL).ThrowErr(env); + return nullptr; + } + + HASH_ALGORITHM_TYPE algType = GetHashAlgorithm(alg.get()); + if (algType == HASH_ALGORITHM_TYPE_UNSUPPORTED) { + HILOGE("Unsupport algorithm"); + NError(EINVAL).ThrowErr(env); + return nullptr; + } + + auto hsEntity = CreateUniquePtr(); + if (hsEntity == nullptr) { + HILOGE("Failed to request heap memory."); + NError(ENOMEM).ThrowErr(env); + return nullptr; + } + hsEntity->algType = algType; + + switch (algType) { + case HASH_ALGORITHM_TYPE_MD5: { + MD5_CTX ctx; + MD5_Init(&ctx); + hsEntity->md5Ctx = ctx; + break; + } + case HASH_ALGORITHM_TYPE_SHA1: { + SHA_CTX ctx; + SHA1_Init(&ctx); + hsEntity->shaCtx = ctx; + break; + } + case HASH_ALGORITHM_TYPE_SHA256: { + SHA256_CTX ctx; + SHA256_Init(&ctx); + hsEntity->sha256Ctx = ctx; + break; + } + default: + break; + } + if (!NClass::SetEntityFor(env, funcArg.GetThisVar(), move(hsEntity))) { + HILOGE("INNER BUG. Failed to wrap entity for obj hsEntity"); + NError(EIO).ThrowErr(env); + return nullptr; + } + return funcArg.GetThisVar(); +} + +napi_value HashStreamNExporter::Digest(napi_env env, napi_callback_info info) +{ + NFuncArg funcArg(env, info); + if (!funcArg.InitArgs(NARG_CNT::ZERO)) { + HILOGE("Number of arguments unmatched"); + NError(EINVAL).ThrowErr(env); + return nullptr; + } + auto [succEntity, hsEntity] = GetHsEntity(env, funcArg.GetThisVar()); + if (!succEntity) { + HILOGE("Failed to get entity of RandomAccessFile"); + NError(EIO).ThrowErr(env); + return nullptr; + } + + string digestStr; + switch (hsEntity->algType) { + case HASH_ALGORITHM_TYPE_MD5: { + auto res = make_unique(MD5_DIGEST_LENGTH); + MD5_Final(res.get(), &hsEntity->md5Ctx); + digestStr = HashFinal(res, MD5_DIGEST_LENGTH); + break; + } + case HASH_ALGORITHM_TYPE_SHA1: { + auto res = make_unique(SHA_DIGEST_LENGTH); + SHA1_Final(res.get(), &hsEntity->shaCtx); + digestStr = HashFinal(res, SHA_DIGEST_LENGTH); + break; + } + case HASH_ALGORITHM_TYPE_SHA256: { + auto res = make_unique(SHA256_DIGEST_LENGTH); + SHA256_Final(res.get(), &hsEntity->sha256Ctx); + digestStr = HashFinal(res, SHA256_DIGEST_LENGTH); + break; + } + default: + break; + } + + return NVal::CreateUTF8String(env, digestStr).val_; +} + +napi_value HashStreamNExporter::Update(napi_env env, napi_callback_info info) +{ + NFuncArg funcArg(env, info); + if (!funcArg.InitArgs(NARG_CNT::ONE)) { + HILOGE("Number of arguments unmatched"); + NError(EINVAL).ThrowErr(env); + return nullptr; + } + + auto [succ, buf, bufLen] = NVal(env, funcArg[NARG_POS::FIRST]).ToArraybuffer(); + if (!succ) { + HILOGE("illegal array buffer"); + NError(EINVAL).ThrowErr(env); + return nullptr; + } + + auto [succEntity, hsEntity] = GetHsEntity(env, funcArg.GetThisVar()); + if (!succEntity) { + HILOGE("Failed to get entity of RandomAccessFile"); + NError(EIO).ThrowErr(env); + return nullptr; + } + + switch (hsEntity->algType) { + case HASH_ALGORITHM_TYPE_MD5: + MD5_Update(&hsEntity->md5Ctx, buf, bufLen); + break; + case HASH_ALGORITHM_TYPE_SHA1: + SHA1_Update(&hsEntity->shaCtx, buf, bufLen); + break; + case HASH_ALGORITHM_TYPE_SHA256: + SHA256_Update(&hsEntity->sha256Ctx, buf, bufLen); + break; + default: + break; + } + return NVal::CreateUndefined(env).val_; +} + +bool HashStreamNExporter::Export() +{ + vector props = { + NVal::DeclareNapiFunction("digest", Digest), + NVal::DeclareNapiFunction("update", Update), + }; + string className = GetClassName(); + bool succ = false; + napi_value classValue = nullptr; + tie(succ, classValue) = NClass::DefineClass(exports_.env_, className, + HashStreamNExporter::Constructor, move(props)); + if (!succ) { + HILOGE("INNER BUG. Failed to define class"); + NError(EIO).ThrowErr(exports_.env_); + return false; + } + succ = NClass::SaveClass(exports_.env_, className, classValue); + if (!succ) { + HILOGE("INNER BUG. Failed to define class"); + NError(EIO).ThrowErr(exports_.env_); + return false; + } + + return exports_.AddProp(className, classValue); +} + +string HashStreamNExporter::GetClassName() +{ + return HashStreamNExporter::className_; +} + +HashStreamNExporter::HashStreamNExporter(napi_env env, napi_value exports) : NExporter(env, exports) {} + +HashStreamNExporter::~HashStreamNExporter() {} +} +} +} \ No newline at end of file diff --git a/interfaces/kits/js/src/mod_hash/class_hashstream/hashstream_n_exporter.h b/interfaces/kits/js/src/mod_hash/class_hashstream/hashstream_n_exporter.h new file mode 100644 index 000000000..bbde49ac0 --- /dev/null +++ b/interfaces/kits/js/src/mod_hash/class_hashstream/hashstream_n_exporter.h @@ -0,0 +1,45 @@ +/* + * Copyright (c) 2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef INTERFACES_KITS_JS_SRC_MOD_HASH_CLASS_HASHSTREAM_HASHSTREAM_N_EXPORTER_H +#define INTERFACES_KITS_JS_SRC_MOD_HASH_CLASS_HASHSTREAM_HASHSTREAM_N_EXPORTER_H + +#include "filemgmt_libhilog.h" +#include "filemgmt_libn.h" + +namespace OHOS { +namespace FileManagement { +namespace ModuleFileIO { +using namespace OHOS::FileManagement::LibN; + +class HashStreamNExporter final : public NExporter { +public: + inline static const std::string className_ = "HashStream"; + + bool Export() override; + std::string GetClassName() override; + + static napi_value Constructor(napi_env env, napi_callback_info info); + + static napi_value Digest(napi_env env, napi_callback_info info); + static napi_value Update(napi_env env, napi_callback_info info); + + HashStreamNExporter(napi_env env, napi_value exports); + ~HashStreamNExporter() override; +}; +} // namespace ModuleFileIO +} // namespace FileManagement +} // namespace OHOS +#endif \ No newline at end of file diff --git a/interfaces/kits/js/src/mod_hash/create_streamhash.cpp b/interfaces/kits/js/src/mod_hash/create_streamhash.cpp new file mode 100644 index 000000000..57be6875f --- /dev/null +++ b/interfaces/kits/js/src/mod_hash/create_streamhash.cpp @@ -0,0 +1,53 @@ +/* + * Copyright (c) 2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "create_streamhash.h" + +#include "file_utils.h" +#include "filemgmt_libhilog.h" + +namespace OHOS { +namespace FileManagement { +namespace ModuleFileIO { +using namespace std; +using namespace OHOS::FileManagement::LibN; +const string HASH_STREAM_CLASS = "HashStream"; + +napi_value CreateStreamHash::Hash(napi_env env, napi_callback_info info) +{ + NFuncArg funcArg(env, info); + if (!funcArg.InitArgs(NARG_CNT::ONE)) { + HILOGE("Number of arguments unmatched"); + NError(EINVAL).ThrowErr(env); + return nullptr; + } + const char moduleName[] = "@ohos.file.streamhash"; + napi_value streamhash; + napi_load_module(env, moduleName, &streamhash); + napi_value constructor = nullptr; + napi_get_named_property(env, streamhash, HASH_STREAM_CLASS.c_str(), &constructor); + napi_value streamObj = nullptr; + + napi_value argv[NARG_CNT::ONE] = {funcArg[NARG_POS::FIRST]}; + napi_status status = napi_new_instance(env, constructor, NARG_CNT::ONE, argv, &streamObj); + if (status != napi_ok) { + HILOGE("create stream obj fail"); + return nullptr; + } + return NVal(env, streamObj).val_; +} +} // namespace ModuleFileIO +} // namespace FileManagement +} // namespace OHOS \ No newline at end of file diff --git a/interfaces/kits/js/src/mod_hash/create_streamhash.h b/interfaces/kits/js/src/mod_hash/create_streamhash.h new file mode 100644 index 000000000..09772e9c2 --- /dev/null +++ b/interfaces/kits/js/src/mod_hash/create_streamhash.h @@ -0,0 +1,31 @@ +/* + * Copyright (c) 2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef INTERFACES_KITS_JS_SRC_MOD_FS_PROPERTIES_CREATE_STREAMHASH_H +#define INTERFACES_KITS_JS_SRC_MOD_FS_PROPERTIES_CREATE_STREAMHASH_H + +#include "filemgmt_libn.h" + +namespace OHOS { +namespace FileManagement { +namespace ModuleFileIO { +class CreateStreamHash final { +public: + static napi_value Hash(napi_env env, napi_callback_info info); +}; +} // namespace ModuleFileIO +} // namespace FileManagement +} // namespace OHOS +#endif // INTERFACES_KITS_JS_SRC_MOD_FS_PROPERTIES_CREATE_STREAMHASH_H \ No newline at end of file diff --git a/interfaces/kits/js/src/mod_hash/hash.cpp b/interfaces/kits/js/src/mod_hash/hash.cpp index 044ccab58..b2add30b4 100644 --- a/interfaces/kits/js/src/mod_hash/hash.cpp +++ b/interfaces/kits/js/src/mod_hash/hash.cpp @@ -19,6 +19,7 @@ #include #include +#include "create_streamhash.h" #include "filemgmt_libhilog.h" #include "hash_file.h" @@ -117,6 +118,7 @@ bool HashNExporter::Export() { return exports_.AddProp({ NVal::DeclareNapiFunction("hash", Hash::Async), + NVal::DeclareNapiFunction("createHash", CreateStreamHash::Hash), }); } diff --git a/interfaces/kits/js/src/mod_hash/module.cpp b/interfaces/kits/js/src/mod_hash/module.cpp index a4170c31a..f615ba23e 100644 --- a/interfaces/kits/js/src/mod_hash/module.cpp +++ b/interfaces/kits/js/src/mod_hash/module.cpp @@ -18,6 +18,7 @@ #include "filemgmt_libhilog.h" #include "hash.h" +#include "hashstream_n_exporter.h" namespace OHOS { namespace FileManagement { @@ -25,11 +26,14 @@ namespace ModuleFileIO { using namespace std; static napi_value Export(napi_env env, napi_value exports) { - std::unique_ptr products = make_unique(env, exports); - - if (!products->Export()) { - HILOGE("INNER BUG. Failed to export class %{public}s for module fileio", products->GetClassName().c_str()); - return nullptr; + vector> products; + products.push_back(make_unique(env, exports)); + products.push_back(make_unique(env, exports)); + for (auto &&product : products) { + if (!product->Export()) { + HILOGE("INNER BUG. Failed to export class %{public}s for module fileio", product->GetClassName().c_str()); + return nullptr; + } } return exports; } -- Gitee From 289c6a92899acf9f728c3f1dc71e207a0e26a204 Mon Sep 17 00:00:00 2001 From: liuhaotian Date: Thu, 23 May 2024 11:57:00 +0800 Subject: [PATCH 4/5] add interface entry Signed-off-by: liuhaotian Change-Id: Ib04620341ab5a25b4ad5e07e7fb3b21ff4333c42 --- bundle.json | 3 +- interfaces/kits/js/BUILD.gn | 1 + .../randomaccessfile_entity.h | 6 +- .../randomaccessfile_n_exporter.cpp | 105 ++++++++++++++++++ .../randomaccessfile_n_exporter.h | 3 + .../mod_fs/class_stream/stream_n_exporter.cpp | 56 +++++++++- .../mod_fs/class_stream/stream_n_exporter.h | 3 +- .../properties/create_randomaccessfile.cpp | 70 ++++++++++-- .../src/mod_fs/properties/create_streamrw.cpp | 70 ++++++++++++ .../src/mod_fs/properties/create_streamrw.h | 32 ++++++ .../src/mod_fs/properties/prop_n_exporter.cpp | 5 +- .../hashstream_n_exporter.cpp | 51 +++++---- interfaces/kits/ts/BUILD.gn | 61 ++++++++-- .../ts/{common_gen_obj.gni => gen_obj.gni} | 2 +- 14 files changed, 418 insertions(+), 50 deletions(-) create mode 100644 interfaces/kits/js/src/mod_fs/properties/create_streamrw.cpp create mode 100644 interfaces/kits/js/src/mod_fs/properties/create_streamrw.h rename interfaces/kits/ts/{common_gen_obj.gni => gen_obj.gni} (99%) diff --git a/bundle.json b/bundle.json index 1b8a8aa05..28f55247c 100644 --- a/bundle.json +++ b/bundle.json @@ -53,7 +53,8 @@ "group_type": { "base_group": [], "fwk_group": [ - "//foundation/filemanagement/file_api/interfaces/kits/js:build_kits_js" + "//foundation/filemanagement/file_api/interfaces/kits/js:build_kits_js", + "//foundation/filemanagement/file_api/interfaces/kits/ts:build_kits_ts" ], "service_group": [] }, diff --git a/interfaces/kits/js/BUILD.gn b/interfaces/kits/js/BUILD.gn index 01e6a9c0e..994ddabe6 100644 --- a/interfaces/kits/js/BUILD.gn +++ b/interfaces/kits/js/BUILD.gn @@ -206,6 +206,7 @@ ohos_shared_library("fs") { "src/mod_fs/properties/copydir.cpp", "src/mod_fs/properties/create_randomaccessfile.cpp", "src/mod_fs/properties/create_stream.cpp", + "src/mod_fs/properties/create_streamrw.cpp", "src/mod_fs/properties/dfs_listener/file_dfs_listener_stub.cpp", "src/mod_fs/properties/disconnectdfs.cpp", "src/mod_fs/properties/dup.cpp", diff --git a/interfaces/kits/js/src/mod_fs/class_randomaccessfile/randomaccessfile_entity.h b/interfaces/kits/js/src/mod_fs/class_randomaccessfile/randomaccessfile_entity.h index be447791f..3f5b12a7f 100644 --- a/interfaces/kits/js/src/mod_fs/class_randomaccessfile/randomaccessfile_entity.h +++ b/interfaces/kits/js/src/mod_fs/class_randomaccessfile/randomaccessfile_entity.h @@ -19,13 +19,17 @@ #include #include "fd_guard.h" +#include "n_val.h" namespace OHOS { namespace FileManagement { namespace ModuleFileIO { +const int64_t INVALID_POS = -1; struct RandomAccessFileEntity { - std::unique_ptr fd = { nullptr }; + std::unique_ptr fd = {nullptr}; int64_t filePointer = 0; + int64_t start = INVALID_POS; + int64_t end = INVALID_POS; }; } // namespace ModuleFileIO } // namespace FileManagement diff --git a/interfaces/kits/js/src/mod_fs/class_randomaccessfile/randomaccessfile_n_exporter.cpp b/interfaces/kits/js/src/mod_fs/class_randomaccessfile/randomaccessfile_n_exporter.cpp index 32dc15436..5631ff605 100644 --- a/interfaces/kits/js/src/mod_fs/class_randomaccessfile/randomaccessfile_n_exporter.cpp +++ b/interfaces/kits/js/src/mod_fs/class_randomaccessfile/randomaccessfile_n_exporter.cpp @@ -15,6 +15,8 @@ #include "randomaccessfile_n_exporter.h" +#include + #include "file_utils.h" #include "randomaccessfile_entity.h" #include "../common_func.h" @@ -24,6 +26,9 @@ namespace FileManagement { namespace ModuleFileIO { using namespace std; using namespace OHOS::FileManagement::LibN; +const int BUF_SIZE = 1024; +const string READ_STREAM_CLASS = "ReadStream"; +const string WRITE_STREAM_CLASS = "WriteStream"; static tuple GetRAFEntity(napi_env env, napi_value raf_entity) { @@ -414,6 +419,104 @@ napi_value RandomAccessFileNExporter::Constructor(napi_env env, napi_callback_in return funcArg.GetThisVar(); } +static napi_value CreateStream(napi_env env, const string &streamName, RandomAccessFileEntity *rafEntity, int flags) +{ + auto dstFd = dup(rafEntity->fd.get()->GetFD()); + if (dstFd < 0) { + HILOGE("Failed to get valid fd, fail reason: %{public}s, fd: %{public}d", strerror(errno), + rafEntity->fd.get()->GetFD()); + NError(errno).ThrowErr(env); + return nullptr; + } + + string path = "/proc/self/fd/" + to_string(dstFd); + auto buf = CreateUniquePtr(BUF_SIZE); + int readLinkRes = readlink(path.c_str(), buf.get(), BUF_SIZE); + if (readLinkRes < 0) { + HILOGE("Failed to readlink uri, errno=%{public}d", errno); + NError(errno).ThrowErr(env); + return nullptr; + } + + napi_value filePath = NVal::CreateUTF8String(env, string(buf.get())).val_; + const char moduleName[] = "@ohos.file.streamrw"; + napi_value streamrw; + napi_load_module(env, moduleName, &streamrw); + napi_value constructor = nullptr; + napi_get_named_property(env, streamrw, streamName.c_str(), &constructor); + napi_value streamObj = nullptr; + + NVal options = NVal::CreateObject(env); + if (rafEntity->start >= 0) { + options.AddProp("start", NVal::CreateInt64(env, rafEntity->start).val_); + } + if (rafEntity->end >= 0 && streamName == READ_STREAM_CLASS) { + options.AddProp("end", NVal::CreateInt64(env, rafEntity->end).val_); + } + if (streamName == WRITE_STREAM_CLASS) { + options.AddProp("mode", NVal::CreateInt32(env, flags).val_); + } + + napi_value argv[NARG_CNT::TWO] = {filePath, options.val_}; + napi_status status = napi_new_instance(env, constructor, NARG_CNT::TWO, argv, &streamObj); + if (status != napi_ok) { + HILOGE("create stream obj fail"); + return nullptr; + } + + return NVal(env, streamObj).val_; +} + +napi_value RandomAccessFileNExporter::GetReadStream(napi_env env, napi_callback_info info) +{ + NFuncArg funcArg(env, info); + if (!funcArg.InitArgs(NARG_CNT::ZERO)) { + HILOGE("Number of arguments unmatched"); + NError(EINVAL).ThrowErr(env); + return nullptr; + } + auto [succEntity, rafEntity] = GetRAFEntity(env, funcArg.GetThisVar()); + if (!succEntity) { + HILOGE("Failed to get entity of RandomAccessFile"); + NError(EIO).ThrowErr(env); + return nullptr; + } + + int flags = fcntl(rafEntity->fd.get()->GetFD(), F_GETFL); + if (((flags & O_ACCMODE) != O_RDONLY) && ((flags & O_ACCMODE) != O_RDWR)) { + HILOGE("Failed to check Permission"); + NError(EACCES).ThrowErr(env); + return nullptr; + } + + return CreateStream(env, READ_STREAM_CLASS, rafEntity, flags); +} + +napi_value RandomAccessFileNExporter::GetWriteStream(napi_env env, napi_callback_info info) +{ + NFuncArg funcArg(env, info); + if (!funcArg.InitArgs(NARG_CNT::ZERO)) { + HILOGE("Number of arguments unmatched"); + NError(EINVAL).ThrowErr(env); + return nullptr; + } + auto [succEntity, rafEntity] = GetRAFEntity(env, funcArg.GetThisVar()); + if (!succEntity) { + HILOGE("Failed to get entity of RandomAccessFile"); + NError(EIO).ThrowErr(env); + return nullptr; + } + + int flags = fcntl(rafEntity->fd.get()->GetFD(), F_GETFL); + if (((flags & O_ACCMODE) != O_WRONLY) && ((flags & O_ACCMODE) != O_RDWR)) { + HILOGE("Failed to check Permission"); + NError(EACCES).ThrowErr(env); + return nullptr; + } + + return CreateStream(env, WRITE_STREAM_CLASS, rafEntity, flags); +} + bool RandomAccessFileNExporter::Export() { vector props = { @@ -423,6 +526,8 @@ bool RandomAccessFileNExporter::Export() NVal::DeclareNapiFunction("writeSync", WriteSync), NVal::DeclareNapiFunction("setFilePointer", SetFilePointerSync), NVal::DeclareNapiFunction("close", CloseSync), + NVal::DeclareNapiFunction("getReadStream", GetReadStream), + NVal::DeclareNapiFunction("getWriteStream", GetWriteStream), NVal::DeclareNapiGetter("fd", GetFD), NVal::DeclareNapiGetter("filePointer", GetFPointer), }; diff --git a/interfaces/kits/js/src/mod_fs/class_randomaccessfile/randomaccessfile_n_exporter.h b/interfaces/kits/js/src/mod_fs/class_randomaccessfile/randomaccessfile_n_exporter.h index 7d49a61f0..fbcd2836c 100644 --- a/interfaces/kits/js/src/mod_fs/class_randomaccessfile/randomaccessfile_n_exporter.h +++ b/interfaces/kits/js/src/mod_fs/class_randomaccessfile/randomaccessfile_n_exporter.h @@ -41,6 +41,9 @@ public: static napi_value Write(napi_env env, napi_callback_info info); static napi_value Read(napi_env env, napi_callback_info info); + static napi_value GetReadStream(napi_env env, napi_callback_info info); + static napi_value GetWriteStream(napi_env env, napi_callback_info info); + static napi_value GetFD(napi_env env, napi_callback_info info); static napi_value GetFPointer(napi_env env, napi_callback_info info); diff --git a/interfaces/kits/js/src/mod_fs/class_stream/stream_n_exporter.cpp b/interfaces/kits/js/src/mod_fs/class_stream/stream_n_exporter.cpp index dae4d4775..d220521a9 100644 --- a/interfaces/kits/js/src/mod_fs/class_stream/stream_n_exporter.cpp +++ b/interfaces/kits/js/src/mod_fs/class_stream/stream_n_exporter.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2023 Huawei Device Co., Ltd. + * Copyright (c) 2023-2024 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -28,6 +28,7 @@ #include "file_utils.h" #include "filemgmt_libhilog.h" #include "flush.h" +#include "rust_file.h" #include "stream_entity.h" namespace OHOS { @@ -331,6 +332,58 @@ napi_value StreamNExporter::Close(napi_env env, napi_callback_info cbInfo) } } +napi_value StreamNExporter::Seek(napi_env env, napi_callback_info cbInfo) +{ + NFuncArg funcArg(env, cbInfo); + if (!funcArg.InitArgs(NARG_CNT::ONE, NARG_CNT::TWO)) { + HILOGE("Number of arguments unmatched"); + NError(EINVAL).ThrowErr(env); + return nullptr; + } + + auto streamEntity = NClass::GetEntityOf(env, funcArg.GetThisVar()); + if (!streamEntity || !streamEntity->fp) { + HILOGE("Failed to get entity of Stream"); + NError(EIO).ThrowErr(env); + return nullptr; + } + + auto [succGetOffset, offset] = NVal(env, funcArg[NARG_POS::FIRST]).ToInt64(); + if (!succGetOffset) { + HILOGE("Invalid offset from JS first argument"); + NError(EINVAL).ThrowErr(env); + return nullptr; + } + + int whence = SEEK_SET; + if (funcArg.GetArgc() == NARG_CNT::TWO) { + auto [succGetWhence, pos] = NVal(env, funcArg[NARG_POS::SECOND]).ToInt32(SEEK_SET); + if (!succGetWhence || pos < SEEK_SET || pos > SEEK_END) { + HILOGE("Invalid whence from JS third argument"); + NError(EINVAL).ThrowErr(env); + return nullptr; + } + whence = pos; + } + + if (offset >= 0) { + int ret = fseek(streamEntity->fp.get(), static_cast(offset), whence); + if (ret < 0) { + HILOGE("Failed to set the offset location of the file stream pointer, ret: %{public}d", ret); + NError(errno).ThrowErr(env); + return nullptr; + } + } + int64_t res = ftell(streamEntity->fp.get()); + if (res < 0) { + HILOGE("Failed to tell, error:%{public}d", errno); + NError(errno).ThrowErr(env); + return nullptr; + } + + return NVal::CreateInt64(env, res).val_; +} + napi_value StreamNExporter::Constructor(napi_env env, napi_callback_info cbInfo) { NFuncArg funcArg(env, cbInfo); @@ -365,6 +418,7 @@ bool StreamNExporter::Export() NVal::DeclareNapiFunction("write", Write), NVal::DeclareNapiFunction("read", Read), NVal::DeclareNapiFunction("close", Close), + NVal::DeclareNapiFunction("seek", Seek), }; string className = GetClassName(); diff --git a/interfaces/kits/js/src/mod_fs/class_stream/stream_n_exporter.h b/interfaces/kits/js/src/mod_fs/class_stream/stream_n_exporter.h index c68c246ad..fb7f0fb3b 100644 --- a/interfaces/kits/js/src/mod_fs/class_stream/stream_n_exporter.h +++ b/interfaces/kits/js/src/mod_fs/class_stream/stream_n_exporter.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2023 Huawei Device Co., Ltd. + * Copyright (c) 2023-2024 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -38,6 +38,7 @@ public: static napi_value Write(napi_env env, napi_callback_info cbInfo); static napi_value Read(napi_env env, napi_callback_info cbInfo); static napi_value Close(napi_env env, napi_callback_info cbInfo); + static napi_value Seek(napi_env env, napi_callback_info cbInfo); StreamNExporter(napi_env env, napi_value exports); ~StreamNExporter() override; diff --git a/interfaces/kits/js/src/mod_fs/properties/create_randomaccessfile.cpp b/interfaces/kits/js/src/mod_fs/properties/create_randomaccessfile.cpp index 56f8ed6ce..e7bbd069d 100644 --- a/interfaces/kits/js/src/mod_fs/properties/create_randomaccessfile.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/create_randomaccessfile.cpp @@ -75,25 +75,67 @@ static tuple ParseJsFile(napi_env env, napi_value pathOrFil return { false, FileInfo { false, nullptr, nullptr }, EINVAL}; } -static tuple GetJsFlags(napi_env env, const NFuncArg &funcArg, FileInfo &fileInfo) +static tuple GetRafOptions(napi_env env, napi_value options) +{ + NVal op = NVal(env, options); + int64_t opStart = INVALID_POS; + int64_t opEnd = INVALID_POS; + if (op.HasProp("start")) { + auto [succ, start] = op.GetProp("start").ToInt64(); + if (!succ || start < 0) { + NError(EINVAL).ThrowErr(env, "Invalid option.start, positive integer is desired"); + return {false, opStart, opEnd}; + } + opStart = start; + } + if (op.HasProp("end")) { + auto [succ, end] = op.GetProp("end").ToInt64(); + if (!succ || end < 0) { + NError(EINVAL).ThrowErr(env, "Invalid option.end, positive integer is desired"); + return {false, opStart, opEnd}; + } + opEnd = end; + } + return {true, opStart, opEnd}; +} + +static tuple GetJsFlags(napi_env env, const NFuncArg &funcArg, + FileInfo &fileInfo) { unsigned int flags = O_RDONLY; + int64_t start = INVALID_POS; + int64_t end = INVALID_POS; if (fileInfo.isPath) { if (funcArg.GetArgc() >= NARG_CNT::TWO) { auto [succ, mode] = NVal(env, funcArg[NARG_POS::SECOND]).ToInt32(0); if (!succ || mode < 0) { HILOGE("Invalid flags"); NError(EINVAL).ThrowErr(env); - return { false, flags }; + return {false, flags, start, end}; } flags = static_cast(mode); (void)CommonFunc::ConvertJsFlags(flags); } } - return { true, flags }; + + bool succOpt; + if (funcArg.GetArgc() == NARG_CNT::THREE && !NVal(env, funcArg[NARG_POS::THIRD]).TypeIs(napi_function)) { + tie(succOpt, start, end) = GetRafOptions(env, funcArg[NARG_POS::THIRD]); + if (!succOpt) { + HILOGE("invalid RandomAccessFile options"); + NError(EINVAL).ThrowErr(env); + return {false, flags, start, end}; + } + } + + return {true, flags, start, end}; } -static NVal InstantiateRandomAccessFile(napi_env env, std::unique_ptr fdg, int64_t fp) +static NVal InstantiateRandomAccessFile(napi_env env, + std::unique_ptr fdg, + int64_t fp, + int64_t start = INVALID_POS, + int64_t end = INVALID_POS) { napi_value objRAF = NClass::InstantiateClass(env, RandomAccessFileNExporter::className_, {}); if (!objRAF) { @@ -109,13 +151,15 @@ static NVal InstantiateRandomAccessFile(napi_env env, std::unique_ptrfd.swap(fdg); rafEntity->filePointer = fp; - return { env, objRAF }; + rafEntity->start = start; + rafEntity->end = end; + return {env, objRAF}; } napi_value CreateRandomAccessFile::Sync(napi_env env, napi_callback_info info) { NFuncArg funcArg(env, info); - if (!funcArg.InitArgs(NARG_CNT::ONE, NARG_CNT::TWO)) { + if (!funcArg.InitArgs(NARG_CNT::ONE, NARG_CNT::THREE)) { HILOGE("Number of arguments unmatched"); NError(EINVAL).ThrowErr(env); return nullptr; @@ -126,7 +170,7 @@ napi_value CreateRandomAccessFile::Sync(napi_env env, napi_callback_info info) return nullptr; } if (fileInfo.isPath) { - auto [succFlags, flags] = GetJsFlags(env, funcArg, fileInfo); + auto [succFlags, flags, ignoreStart, ignoreEnd] = GetJsFlags(env, funcArg, fileInfo); if (!succFlags) { return nullptr; } @@ -146,6 +190,12 @@ napi_value CreateRandomAccessFile::Sync(napi_env env, napi_callback_info info) fileInfo.fdg->SetFD(open_req.get()->result, false); } + if (funcArg.GetArgc() == NARG_CNT::THREE) { + auto [succ, start, end] = GetRafOptions(env, funcArg[NARG_POS::THIRD]); + if (succ) { + return InstantiateRandomAccessFile(env, move(fileInfo.fdg), 0, start, end).val_; + } + } return InstantiateRandomAccessFile(env, move(fileInfo.fdg), 0).val_; } @@ -183,7 +233,7 @@ napi_value CreateRandomAccessFile::Async(napi_env env, napi_callback_info info) NError(err).ThrowErr(env); return nullptr; } - auto [succFlags, flags] = GetJsFlags(env, funcArg, fileInfo); + auto [succFlags, flags, start, end] = GetJsFlags(env, funcArg, fileInfo); if (!succFlags) { return nullptr; } @@ -203,11 +253,11 @@ napi_value CreateRandomAccessFile::Async(napi_env env, napi_callback_info info) return AsyncExec(arg, movedFileInfo, flags); }; - auto cbCompl = [arg, movedFileInfo](napi_env env, NError err) -> NVal { + auto cbCompl = [arg, movedFileInfo, start = start, end = end](napi_env env, NError err) -> NVal { if (err) { return { env, err.GetNapiErr(env) }; } - return InstantiateRandomAccessFile(env, move(movedFileInfo->fdg), 0); + return InstantiateRandomAccessFile(env, move(movedFileInfo->fdg), 0, start, end); }; NVal thisVar(env, funcArg.GetThisVar()); if (funcArg.GetArgc() == NARG_CNT::ONE || (funcArg.GetArgc() == NARG_CNT::TWO && diff --git a/interfaces/kits/js/src/mod_fs/properties/create_streamrw.cpp b/interfaces/kits/js/src/mod_fs/properties/create_streamrw.cpp new file mode 100644 index 000000000..eef138ecf --- /dev/null +++ b/interfaces/kits/js/src/mod_fs/properties/create_streamrw.cpp @@ -0,0 +1,70 @@ +/* + * Copyright (c) 2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "create_streamrw.h" + +#include "file_utils.h" +#include "filemgmt_libhilog.h" + +namespace OHOS { +namespace FileManagement { +namespace ModuleFileIO { +using namespace std; +using namespace OHOS::FileManagement::LibN; +const string READ_STREAM_CLASS = "ReadStream"; +const string WRITE_STREAM_CLASS = "WriteStream"; + +static napi_value CreateStream(napi_env env, napi_callback_info info, const string &streamName) +{ + NFuncArg funcArg(env, info); + if (!funcArg.InitArgs(NARG_CNT::ONE, NARG_CNT::TWO)) { + HILOGE("Number of arguments unmatched"); + NError(EINVAL).ThrowErr(env); + return nullptr; + } + size_t argc = funcArg.GetArgc(); + const char moduleName[] = "@ohos.file.streamrw"; + napi_value streamrw; + napi_load_module(env, moduleName, &streamrw); + napi_value constructor = nullptr; + napi_get_named_property(env, streamrw, streamName.c_str(), &constructor); + napi_value streamObj = nullptr; + napi_status status; + if (argc == NARG_CNT::ONE) { + napi_value argv[NARG_CNT::ONE] = {funcArg[NARG_POS::FIRST]}; + status = napi_new_instance(env, constructor, argc, argv, &streamObj); + } else { + napi_value argv[NARG_CNT::TWO] = {funcArg[NARG_POS::FIRST], funcArg[NARG_POS::SECOND]}; + status = napi_new_instance(env, constructor, argc, argv, &streamObj); + } + if (status != napi_ok) { + HILOGE("create stream obj fail"); + return nullptr; + } + return NVal(env, streamObj).val_; +} + +napi_value CreateStreamRw::Read(napi_env env, napi_callback_info info) +{ + return CreateStream(env, info, READ_STREAM_CLASS); +} + +napi_value CreateStreamRw::Write(napi_env env, napi_callback_info info) +{ + return CreateStream(env, info, WRITE_STREAM_CLASS); +} +} // namespace ModuleFileIO +} // namespace FileManagement +} // namespace OHOS \ No newline at end of file diff --git a/interfaces/kits/js/src/mod_fs/properties/create_streamrw.h b/interfaces/kits/js/src/mod_fs/properties/create_streamrw.h new file mode 100644 index 000000000..c94b2d9cc --- /dev/null +++ b/interfaces/kits/js/src/mod_fs/properties/create_streamrw.h @@ -0,0 +1,32 @@ +/* + * Copyright (c) 2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef INTERFACES_KITS_JS_SRC_MOD_FS_PROPERTIES_CREATE_STREAMRW_H +#define INTERFACES_KITS_JS_SRC_MOD_FS_PROPERTIES_CREATE_STREAMRW_H + +#include "filemgmt_libn.h" + +namespace OHOS { +namespace FileManagement { +namespace ModuleFileIO { +class CreateStreamRw final { +public: + static napi_value Read(napi_env env, napi_callback_info info); + static napi_value Write(napi_env env, napi_callback_info info); +}; +} // namespace ModuleFileIO +} // namespace FileManagement +} // namespace OHOS +#endif // INTERFACES_KITS_JS_SRC_MOD_FS_PROPERTIES_CREATE_STREAMRW_H \ No newline at end of file diff --git a/interfaces/kits/js/src/mod_fs/properties/prop_n_exporter.cpp b/interfaces/kits/js/src/mod_fs/properties/prop_n_exporter.cpp index 35fc82c3c..fb4b8f866 100644 --- a/interfaces/kits/js/src/mod_fs/properties/prop_n_exporter.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/prop_n_exporter.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2024 Huawei Device Co., Ltd. + * Copyright (c) 2022-2024 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -47,6 +47,7 @@ #include "copydir.h" #include "create_randomaccessfile.h" #include "create_stream.h" +#include "create_streamrw.h" #include "dup.h" #include "fdopen_stream.h" #include "listfile.h" @@ -669,6 +670,8 @@ bool PropNExporter::ExportSync() NVal::DeclareNapiFunction("copyFileSync", CopyFile::Sync), NVal::DeclareNapiFunction("createRandomAccessFileSync", CreateRandomAccessFile::Sync), NVal::DeclareNapiFunction("createStreamSync", CreateStream::Sync), + NVal::DeclareNapiFunction("createReadStream", CreateStreamRw::Read), + NVal::DeclareNapiFunction("createWriteStream", CreateStreamRw::Write), NVal::DeclareNapiFunction("dup", Dup::Sync), NVal::DeclareNapiFunction("fdopenStreamSync", FdopenStream::Sync), NVal::DeclareNapiFunction("listFileSync", ListFile::Sync), diff --git a/interfaces/kits/js/src/mod_hash/class_hashstream/hashstream_n_exporter.cpp b/interfaces/kits/js/src/mod_hash/class_hashstream/hashstream_n_exporter.cpp index 697574dc3..3684bef11 100644 --- a/interfaces/kits/js/src/mod_hash/class_hashstream/hashstream_n_exporter.cpp +++ b/interfaces/kits/js/src/mod_hash/class_hashstream/hashstream_n_exporter.cpp @@ -12,8 +12,6 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -#include "securec.h" -#include "string.h" #include "hashstream_n_exporter.h" #include @@ -54,29 +52,8 @@ static string HashFinal(const unique_ptr &hashBuf, size_t hashL return ss.str(); } -napi_value HashStreamNExporter::Constructor(napi_env env, napi_callback_info info) +static napi_value SetHsEntity(napi_env env, NFuncArg &funcArg, HASH_ALGORITHM_TYPE algType) { - NFuncArg funcArg(env, info); - if (!funcArg.InitArgs(NARG_CNT::ONE)) { - HILOGE("Number of arguments unmatched"); - NError(EINVAL).ThrowErr(env); - return nullptr; - } - - auto [succAlg, alg, ignore] = NVal(env, funcArg[NARG_POS::FIRST]).ToUTF8String(); - if (!succAlg) { - HILOGE("Invalid algorithm"); - NError(EINVAL).ThrowErr(env); - return nullptr; - } - - HASH_ALGORITHM_TYPE algType = GetHashAlgorithm(alg.get()); - if (algType == HASH_ALGORITHM_TYPE_UNSUPPORTED) { - HILOGE("Unsupport algorithm"); - NError(EINVAL).ThrowErr(env); - return nullptr; - } - auto hsEntity = CreateUniquePtr(); if (hsEntity == nullptr) { HILOGE("Failed to request heap memory."); @@ -115,6 +92,32 @@ napi_value HashStreamNExporter::Constructor(napi_env env, napi_callback_info inf return funcArg.GetThisVar(); } +napi_value HashStreamNExporter::Constructor(napi_env env, napi_callback_info info) +{ + NFuncArg funcArg(env, info); + if (!funcArg.InitArgs(NARG_CNT::ONE)) { + HILOGE("Number of arguments unmatched"); + NError(EINVAL).ThrowErr(env); + return nullptr; + } + + auto [succAlg, alg, ignore] = NVal(env, funcArg[NARG_POS::FIRST]).ToUTF8String(); + if (!succAlg) { + HILOGE("Invalid algorithm"); + NError(EINVAL).ThrowErr(env); + return nullptr; + } + + HASH_ALGORITHM_TYPE algType = GetHashAlgorithm(alg.get()); + if (algType == HASH_ALGORITHM_TYPE_UNSUPPORTED) { + HILOGE("Unsupport algorithm"); + NError(EINVAL).ThrowErr(env); + return nullptr; + } + + return SetHsEntity(env, funcArg, algType); +} + napi_value HashStreamNExporter::Digest(napi_env env, napi_callback_info info) { NFuncArg funcArg(env, info); diff --git a/interfaces/kits/ts/BUILD.gn b/interfaces/kits/ts/BUILD.gn index fbf8f8165..ef7b91b7b 100644 --- a/interfaces/kits/ts/BUILD.gn +++ b/interfaces/kits/ts/BUILD.gn @@ -13,8 +13,7 @@ import("//arkcompiler/ets_frontend/es2panda/es2abc_config.gni") import("//foundation/filemanagement/file_api/file_api.gni") -import( - "//foundation/filemanagement/file_api/interfaces/kits/ts/common_gen_obj.gni") +import("//foundation/filemanagement/file_api/interfaces/kits/ts/gen_obj.gni") # compile .ts to .js. action("build_streamrw_js") { @@ -71,16 +70,39 @@ gen_obj("streamrw_abc") { snapshot_dep = [ ":gen_streamrw_abc" ] } +streamrw_sources = [ "src/native_module_streamrw.cpp" ] + ohos_shared_library("streamrw") { + branch_protector_ret = "pac_ret" + sanitize = { + integer_overflow = true + ubsan = true + boundary_sanitize = true + cfi = true + cfi_cross_dso = true + debug = false + } + + deps = [":streamrw_static"] + subsystem_name = "filemanagement" + part_name = "file_api" + relative_install_dir = "module/file" +} + +ohos_source_set("streamrw_static") { include_dirs = [ "include" ] - sources = [ "src/native_module_streamrw.cpp" ] + sources = streamrw_sources - branch_protector_ret = true + branch_protector_ret = "pac_ret" sanitize = { + integer_overflow = true + ubsan = true + boundary_sanitize = true cfi = true cfi_cross_dso = true debug = false } + deps = [ ":gen_obj_src_streamrw_abc", ":gen_obj_src_streamrw_js", @@ -91,8 +113,6 @@ ohos_shared_library("streamrw") { ] subsystem_name = "filemanagement" part_name = "file_api" - - relative_install_dir = "module/file" } # hash @@ -151,16 +171,39 @@ gen_obj("streamhash_abc") { snapshot_dep = [ ":gen_streamhash_abc" ] } +streamhash_sources = [ "src/native_module_streamhash.cpp" ] + ohos_shared_library("streamhash") { + branch_protector_ret = "pac_ret" + sanitize = { + integer_overflow = true + ubsan = true + boundary_sanitize = true + cfi = true + cfi_cross_dso = true + debug = false + } + + deps = [":streamhash_static"] + subsystem_name = "filemanagement" + part_name = "file_api" + relative_install_dir = "module/file" +} + +ohos_source_set("streamhash_static") { include_dirs = [ "include" ] - sources = [ "src/native_module_streamhash.cpp" ] + sources = streamhash_sources - branch_protector_ret = true + branch_protector_ret = "pac_ret" sanitize = { + integer_overflow = true + ubsan = true + boundary_sanitize = true cfi = true cfi_cross_dso = true debug = false } + deps = [ ":gen_obj_src_streamhash_abc", ":gen_obj_src_streamhash_js", @@ -171,8 +214,6 @@ ohos_shared_library("streamhash") { ] subsystem_name = "filemanagement" part_name = "file_api" - - relative_install_dir = "module/file" } group("build_kits_ts") { diff --git a/interfaces/kits/ts/common_gen_obj.gni b/interfaces/kits/ts/gen_obj.gni similarity index 99% rename from interfaces/kits/ts/common_gen_obj.gni rename to interfaces/kits/ts/gen_obj.gni index 8ea79ab3b..4e9f727c9 100644 --- a/interfaces/kits/ts/common_gen_obj.gni +++ b/interfaces/kits/ts/gen_obj.gni @@ -90,4 +90,4 @@ template("gen_obj") { sources = [ invoker.output ] deps = [ ":gen_obj_" + name ] } -} \ No newline at end of file +} -- Gitee From c37e9aaa68c90b9627286b5f0892c32c730c7344 Mon Sep 17 00:00:00 2001 From: liuhaotian Date: Sat, 25 May 2024 08:55:33 +0800 Subject: [PATCH 5/5] change build method Signed-off-by: liuhaotian Change-Id: I86133b014c22b3298713651cd10228ac4e9eb31d --- bundle.json | 3 +- .../properties/create_randomaccessfile.cpp | 21 ++- interfaces/kits/ts/{ => streamhash}/BUILD.gn | 117 +---------------- .../kits/ts/{ => streamhash}/build_ts_js.py | 4 +- .../native_module_streamhash.cpp | 0 .../ts/{ => streamhash}/src/streamhash.ts | 0 .../kits/ts/{ => streamhash}/tsconfig.json | 4 +- interfaces/kits/ts/streamrw/BUILD.gn | 121 ++++++++++++++++++ interfaces/kits/ts/streamrw/build_ts_js.py | 61 +++++++++ .../native_module_streamrw.cpp | 0 .../kits/ts/{ => streamrw}/src/streamrw.ts | 0 interfaces/kits/ts/streamrw/tsconfig.json | 13 ++ 12 files changed, 217 insertions(+), 127 deletions(-) rename interfaces/kits/ts/{ => streamhash}/BUILD.gn (52%) rename interfaces/kits/ts/{ => streamhash}/build_ts_js.py (93%) rename interfaces/kits/ts/{src => streamhash}/native_module_streamhash.cpp (100%) rename interfaces/kits/ts/{ => streamhash}/src/streamhash.ts (100%) rename interfaces/kits/ts/{ => streamhash}/tsconfig.json (86%) create mode 100644 interfaces/kits/ts/streamrw/BUILD.gn create mode 100755 interfaces/kits/ts/streamrw/build_ts_js.py rename interfaces/kits/ts/{src => streamrw}/native_module_streamrw.cpp (100%) rename interfaces/kits/ts/{ => streamrw}/src/streamrw.ts (100%) create mode 100644 interfaces/kits/ts/streamrw/tsconfig.json diff --git a/bundle.json b/bundle.json index 28f55247c..7060b287c 100644 --- a/bundle.json +++ b/bundle.json @@ -54,7 +54,8 @@ "base_group": [], "fwk_group": [ "//foundation/filemanagement/file_api/interfaces/kits/js:build_kits_js", - "//foundation/filemanagement/file_api/interfaces/kits/ts:build_kits_ts" + "//foundation/filemanagement/file_api/interfaces/kits/ts/streamrw:streamrw_packages", + "//foundation/filemanagement/file_api/interfaces/kits/ts/streamhash:streamhash_packages" ], "service_group": [] }, diff --git a/interfaces/kits/js/src/mod_fs/properties/create_randomaccessfile.cpp b/interfaces/kits/js/src/mod_fs/properties/create_randomaccessfile.cpp index e7bbd069d..c63030ba7 100644 --- a/interfaces/kits/js/src/mod_fs/properties/create_randomaccessfile.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/create_randomaccessfile.cpp @@ -99,23 +99,20 @@ static tuple GetRafOptions(napi_env env, napi_value opti return {true, opStart, opEnd}; } -static tuple GetJsFlags(napi_env env, const NFuncArg &funcArg, - FileInfo &fileInfo) +static tuple GetJsFlags(napi_env env, const NFuncArg &funcArg, FileInfo &fileInfo) { unsigned int flags = O_RDONLY; int64_t start = INVALID_POS; int64_t end = INVALID_POS; - if (fileInfo.isPath) { - if (funcArg.GetArgc() >= NARG_CNT::TWO) { - auto [succ, mode] = NVal(env, funcArg[NARG_POS::SECOND]).ToInt32(0); - if (!succ || mode < 0) { - HILOGE("Invalid flags"); - NError(EINVAL).ThrowErr(env); - return {false, flags, start, end}; - } - flags = static_cast(mode); - (void)CommonFunc::ConvertJsFlags(flags); + if (fileInfo.isPath && funcArg.GetArgc() >= NARG_CNT::TWO) { + auto [succ, mode] = NVal(env, funcArg[NARG_POS::SECOND]).ToInt32(0); + if (!succ || mode < 0) { + HILOGE("Invalid flags"); + NError(EINVAL).ThrowErr(env); + return {false, flags, start, end}; } + flags = static_cast(mode); + (void)CommonFunc::ConvertJsFlags(flags); } bool succOpt; diff --git a/interfaces/kits/ts/BUILD.gn b/interfaces/kits/ts/streamhash/BUILD.gn similarity index 52% rename from interfaces/kits/ts/BUILD.gn rename to interfaces/kits/ts/streamhash/BUILD.gn index ef7b91b7b..b3d2ca526 100644 --- a/interfaces/kits/ts/BUILD.gn +++ b/interfaces/kits/ts/streamhash/BUILD.gn @@ -15,117 +15,17 @@ import("//arkcompiler/ets_frontend/es2panda/es2abc_config.gni") import("//foundation/filemanagement/file_api/file_api.gni") import("//foundation/filemanagement/file_api/interfaces/kits/ts/gen_obj.gni") -# compile .ts to .js. -action("build_streamrw_js") { - script = "${file_api_path}/interfaces/kits/ts/build_ts_js.py" - - outFile_Path = target_out_dir + "/" + current_cpu - args = [ - "--dst-file", - rebase_path(target_out_dir + "/streamrw.js"), - "--module-path", - rebase_path("/foundation/filemanagement/file_api/interfaces/kits/ts"), - "--out-file", - rebase_path(outFile_Path + "/streamrw.js"), - "--out-filePath", - rebase_path(outFile_Path), - "--relative-path", - rebase_path("//", root_build_dir), - ] - outputs = [ target_out_dir + "/streamrw.js" ] -} - -# compile .js to .abc. -es2abc_gen_abc("gen_streamrw_abc") { - extra_visibility = [ ":*" ] - src_js = rebase_path(target_out_dir + "/streamrw.js") - dst_file = rebase_path(target_out_dir + "/streamrw.abc") - in_puts = [ target_out_dir + "/streamrw.js" ] - out_puts = [ target_out_dir + "/streamrw.abc" ] - extra_args = [ "--module" ] - extra_dependencies = [ ":build_streamrw_js" ] -} - -abc_output_path = get_label_info(":streamrw_abc", "target_out_dir") - -gen_obj("streamrw_js") { - input = "$target_out_dir/streamrw.js" - if (use_mac || use_mingw_win || use_ios || use_linux) { - stream_js_obj_path = abc_output_path + "/streamrw.c" - } else { - stream_js_obj_path = abc_output_path + "/streamrw.o" - } - output = stream_js_obj_path - snapshot_dep = [ ":build_streamrw_js" ] -} - -gen_obj("streamrw_abc") { - input = "$target_out_dir/streamrw.abc" - if (use_mac || use_mingw_win || use_ios || use_linux) { - stream_js_obj_path = abc_output_path + "/streamrw_abc.c" - } else { - stream_js_obj_path = abc_output_path + "/streamrw_abc.o" - } - output = stream_js_obj_path - snapshot_dep = [ ":gen_streamrw_abc" ] -} - -streamrw_sources = [ "src/native_module_streamrw.cpp" ] - -ohos_shared_library("streamrw") { - branch_protector_ret = "pac_ret" - sanitize = { - integer_overflow = true - ubsan = true - boundary_sanitize = true - cfi = true - cfi_cross_dso = true - debug = false - } - - deps = [":streamrw_static"] - subsystem_name = "filemanagement" - part_name = "file_api" - relative_install_dir = "module/file" -} - -ohos_source_set("streamrw_static") { - include_dirs = [ "include" ] - sources = streamrw_sources - - branch_protector_ret = "pac_ret" - sanitize = { - integer_overflow = true - ubsan = true - boundary_sanitize = true - cfi = true - cfi_cross_dso = true - debug = false - } - - deps = [ - ":gen_obj_src_streamrw_abc", - ":gen_obj_src_streamrw_js", - ] - external_deps = [ - "hilog:libhilog", - "napi:ace_napi", - ] - subsystem_name = "filemanagement" - part_name = "file_api" -} - -# hash # compile .ts to .js. action("build_streamhash_js") { - script = "${file_api_path}/interfaces/kits/ts/build_ts_js.py" + script = "${file_api_path}/interfaces/kits/ts/streamhash/build_ts_js.py" outFile_Path = target_out_dir + "/" + current_cpu args = [ "--dst-file", rebase_path(target_out_dir + "/streamhash.js"), "--module-path", - rebase_path("/foundation/filemanagement/file_api/interfaces/kits/ts"), + rebase_path( + "/foundation/filemanagement/file_api/interfaces/kits/ts/streamhash"), "--out-file", rebase_path(outFile_Path + "/streamhash.js"), "--out-filePath", @@ -171,7 +71,7 @@ gen_obj("streamhash_abc") { snapshot_dep = [ ":gen_streamhash_abc" ] } -streamhash_sources = [ "src/native_module_streamhash.cpp" ] +streamhash_sources = [ "native_module_streamhash.cpp" ] ohos_shared_library("streamhash") { branch_protector_ret = "pac_ret" @@ -184,7 +84,7 @@ ohos_shared_library("streamhash") { debug = false } - deps = [":streamhash_static"] + deps = [ ":streamhash_static" ] subsystem_name = "filemanagement" part_name = "file_api" relative_install_dir = "module/file" @@ -216,9 +116,6 @@ ohos_source_set("streamhash_static") { part_name = "file_api" } -group("build_kits_ts") { - public_deps = [ - ":streamhash", - ":streamrw", - ] +group("streamhash_packages") { + public_deps = [ ":streamhash" ] } diff --git a/interfaces/kits/ts/build_ts_js.py b/interfaces/kits/ts/streamhash/build_ts_js.py similarity index 93% rename from interfaces/kits/ts/build_ts_js.py rename to interfaces/kits/ts/streamhash/build_ts_js.py index 43ffdfd70..0b6b96a7b 100755 --- a/interfaces/kits/ts/build_ts_js.py +++ b/interfaces/kits/ts/streamhash/build_ts_js.py @@ -46,10 +46,10 @@ if __name__ == '__main__': BUILD_PATH = os.path.abspath(os.path.join(os.getcwd(), INPUT_ARGUMENTS.relative_path)) os.chdir(("%s" + INPUT_ARGUMENTS.module_path) % BUILD_PATH) os_name = sys.platform - NODE_PATH = '../../../../../../prebuilts/build-tools/common/nodejs/current/bin/node' + NODE_PATH = '../../../../../../../prebuilts/build-tools/common/nodejs/current/bin/node' if not os.path.exists(NODE_PATH): raise Exception('error:NO such file or directory') - TSC_PATH = '../../../../../../third_party/typescript/bin/tsc' + TSC_PATH = '../../../../../../../third_party/typescript/bin/tsc' CMD_INST = [NODE_PATH, TSC_PATH, "--outDir", INPUT_ARGUMENTS.out_filePath] run_command(CMD_INST) if not os.path.exists(INPUT_ARGUMENTS.out_file): diff --git a/interfaces/kits/ts/src/native_module_streamhash.cpp b/interfaces/kits/ts/streamhash/native_module_streamhash.cpp similarity index 100% rename from interfaces/kits/ts/src/native_module_streamhash.cpp rename to interfaces/kits/ts/streamhash/native_module_streamhash.cpp diff --git a/interfaces/kits/ts/src/streamhash.ts b/interfaces/kits/ts/streamhash/src/streamhash.ts similarity index 100% rename from interfaces/kits/ts/src/streamhash.ts rename to interfaces/kits/ts/streamhash/src/streamhash.ts diff --git a/interfaces/kits/ts/tsconfig.json b/interfaces/kits/ts/streamhash/tsconfig.json similarity index 86% rename from interfaces/kits/ts/tsconfig.json rename to interfaces/kits/ts/streamhash/tsconfig.json index 5c9d83dc5..0bb182018 100644 --- a/interfaces/kits/ts/tsconfig.json +++ b/interfaces/kits/ts/streamhash/tsconfig.json @@ -1,7 +1,7 @@ { "compilerOptions": { - "target": "es6", - "module": "es6", + "target": "ES6", + "module": "ES6", "rootDir": "./src", "outDir": "./out", /* Specify an output folder for all emitted files. */ "esModuleInterop": true, diff --git a/interfaces/kits/ts/streamrw/BUILD.gn b/interfaces/kits/ts/streamrw/BUILD.gn new file mode 100644 index 000000000..ea257fca4 --- /dev/null +++ b/interfaces/kits/ts/streamrw/BUILD.gn @@ -0,0 +1,121 @@ +# Copyright (c) 2024 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import("//arkcompiler/ets_frontend/es2panda/es2abc_config.gni") +import("//foundation/filemanagement/file_api/file_api.gni") +import("//foundation/filemanagement/file_api/interfaces/kits/ts/gen_obj.gni") + +# compile .ts to .js. +action("build_streamrw_js") { + script = "${file_api_path}/interfaces/kits/ts/streamrw/build_ts_js.py" + + outFile_Path = target_out_dir + "/" + current_cpu + args = [ + "--dst-file", + rebase_path(target_out_dir + "/streamrw.js"), + "--module-path", + rebase_path( + "/foundation/filemanagement/file_api/interfaces/kits/ts/streamrw"), + "--out-file", + rebase_path(outFile_Path + "/streamrw.js"), + "--out-filePath", + rebase_path(outFile_Path), + "--relative-path", + rebase_path("//", root_build_dir), + ] + outputs = [ target_out_dir + "/streamrw.js" ] +} + +# compile .js to .abc. +es2abc_gen_abc("gen_streamrw_abc") { + extra_visibility = [ ":*" ] + src_js = rebase_path(target_out_dir + "/streamrw.js") + dst_file = rebase_path(target_out_dir + "/streamrw.abc") + in_puts = [ target_out_dir + "/streamrw.js" ] + out_puts = [ target_out_dir + "/streamrw.abc" ] + extra_args = [ "--module" ] + extra_dependencies = [ ":build_streamrw_js" ] +} + +abc_output_path = get_label_info(":streamrw_abc", "target_out_dir") + +gen_obj("streamrw_js") { + input = "$target_out_dir/streamrw.js" + if (use_mac || use_mingw_win || use_ios || use_linux) { + stream_js_obj_path = abc_output_path + "/streamrw.c" + } else { + stream_js_obj_path = abc_output_path + "/streamrw.o" + } + output = stream_js_obj_path + snapshot_dep = [ ":build_streamrw_js" ] +} + +gen_obj("streamrw_abc") { + input = "$target_out_dir/streamrw.abc" + if (use_mac || use_mingw_win || use_ios || use_linux) { + stream_js_obj_path = abc_output_path + "/streamrw_abc.c" + } else { + stream_js_obj_path = abc_output_path + "/streamrw_abc.o" + } + output = stream_js_obj_path + snapshot_dep = [ ":gen_streamrw_abc" ] +} + +streamrw_sources = [ "native_module_streamrw.cpp" ] + +ohos_shared_library("streamrw") { + branch_protector_ret = "pac_ret" + sanitize = { + integer_overflow = true + ubsan = true + boundary_sanitize = true + cfi = true + cfi_cross_dso = true + debug = false + } + + deps = [ ":streamrw_static" ] + subsystem_name = "filemanagement" + part_name = "file_api" + relative_install_dir = "module/file" +} + +ohos_source_set("streamrw_static") { + include_dirs = [ "include" ] + sources = streamrw_sources + + branch_protector_ret = "pac_ret" + sanitize = { + integer_overflow = true + ubsan = true + boundary_sanitize = true + cfi = true + cfi_cross_dso = true + debug = false + } + + deps = [ + ":gen_obj_src_streamrw_abc", + ":gen_obj_src_streamrw_js", + ] + external_deps = [ + "hilog:libhilog", + "napi:ace_napi", + ] + subsystem_name = "filemanagement" + part_name = "file_api" +} + +group("streamrw_packages") { + public_deps = [ ":streamrw" ] +} diff --git a/interfaces/kits/ts/streamrw/build_ts_js.py b/interfaces/kits/ts/streamrw/build_ts_js.py new file mode 100755 index 000000000..0b6b96a7b --- /dev/null +++ b/interfaces/kits/ts/streamrw/build_ts_js.py @@ -0,0 +1,61 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# Copyright (c) 2024 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import os +import shutil +import argparse +import subprocess +import sys + +def run_command(in_cmd): + print(" ".join(in_cmd)) + proc = subprocess.Popen(in_cmd, stdout=subprocess.PIPE, + stderr=subprocess.PIPE, + universal_newlines=True, + shell=False) + stdout, stderr = proc.communicate() + if stdout != "": + raise Exception(stdout) + +if __name__ == '__main__': + PARSER_INST = argparse.ArgumentParser() + PARSER_INST.add_argument('--dst-file', + help='the converted target file') + PARSER_INST.add_argument('--module-path', + help='the module path') + PARSER_INST.add_argument('--out-file', + help='js output file') + PARSER_INST.add_argument('--out-filePath', + help='js output filePath') + PARSER_INST.add_argument('--relative-path', + help='the code root path relative the root_build_dir') + INPUT_ARGUMENTS = PARSER_INST.parse_args() + + BUILD_PATH = os.path.abspath(os.path.join(os.getcwd(), INPUT_ARGUMENTS.relative_path)) + os.chdir(("%s" + INPUT_ARGUMENTS.module_path) % BUILD_PATH) + os_name = sys.platform + NODE_PATH = '../../../../../../../prebuilts/build-tools/common/nodejs/current/bin/node' + if not os.path.exists(NODE_PATH): + raise Exception('error:NO such file or directory') + TSC_PATH = '../../../../../../../third_party/typescript/bin/tsc' + CMD_INST = [NODE_PATH, TSC_PATH, "--outDir", INPUT_ARGUMENTS.out_filePath] + run_command(CMD_INST) + if not os.path.exists(INPUT_ARGUMENTS.out_file): + raise Exception('error:NO such file or directory') + CMD_INST = shutil.copy(INPUT_ARGUMENTS.out_file, INPUT_ARGUMENTS.dst_file) + + CMD_INST = shutil.rmtree(INPUT_ARGUMENTS.out_filePath) + + exit(0) diff --git a/interfaces/kits/ts/src/native_module_streamrw.cpp b/interfaces/kits/ts/streamrw/native_module_streamrw.cpp similarity index 100% rename from interfaces/kits/ts/src/native_module_streamrw.cpp rename to interfaces/kits/ts/streamrw/native_module_streamrw.cpp diff --git a/interfaces/kits/ts/src/streamrw.ts b/interfaces/kits/ts/streamrw/src/streamrw.ts similarity index 100% rename from interfaces/kits/ts/src/streamrw.ts rename to interfaces/kits/ts/streamrw/src/streamrw.ts diff --git a/interfaces/kits/ts/streamrw/tsconfig.json b/interfaces/kits/ts/streamrw/tsconfig.json new file mode 100644 index 000000000..0bb182018 --- /dev/null +++ b/interfaces/kits/ts/streamrw/tsconfig.json @@ -0,0 +1,13 @@ +{ + "compilerOptions": { + "target": "ES6", + "module": "ES6", + "rootDir": "./src", + "outDir": "./out", /* Specify an output folder for all emitted files. */ + "esModuleInterop": true, + "forceConsistentCasingInFileNames": true, + "strict": true, + "skipLibCheck": true, + "noImplicitThis": false, + } +} \ No newline at end of file -- Gitee