From 6b2c4315db05cc1bfcfbdbbefe1046486bd3cfd6 Mon Sep 17 00:00:00 2001 From: WangLin305 Date: Tue, 9 Sep 2025 10:35:43 +0800 Subject: [PATCH] =?UTF-8?q?=E8=A7=84=E8=8C=83=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- entry/src/main/cpp/FileAccessMethods.cpp | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/entry/src/main/cpp/FileAccessMethods.cpp b/entry/src/main/cpp/FileAccessMethods.cpp index de62f69..c63a591 100644 --- a/entry/src/main/cpp/FileAccessMethods.cpp +++ b/entry/src/main/cpp/FileAccessMethods.cpp @@ -25,6 +25,9 @@ const int DOMAIN = 0xFF00; const char *TAG = "[FileAccessMethods]"; +const int BUFFER_SIZE = 256; +const int READ_SIZE = 1000; +const char *FILE_PATH = "/data/storage/el2/base/haps/entry/files/SplicePath.txt"; // [Start napi_value] // entry/src/main/cpp/FileAccessMethods.cpp static napi_value TransferSandboxPath(napi_env env, napi_callback_info info) { @@ -33,7 +36,7 @@ static napi_value TransferSandboxPath(napi_env env, napi_callback_info info) { napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr); //Convert the sandbox path and the contents of the text to be written into C-side variables through the Node-API interface. size_t pathSize, contentsSize; - char pathBuf[256], contentsBuf[256]; + char pathBuf[BUFFER_SIZE], contentsBuf[BUFFER_SIZE]; // [Start get_path] napi_get_value_string_utf8(env, argv[0], pathBuf, sizeof(pathBuf), &pathSize); napi_get_value_string_utf8(env, argv[1], contentsBuf, sizeof(contentsBuf), &contentsSize); @@ -64,11 +67,12 @@ static napi_value SplicePath(napi_env env, napi_callback_info info) { napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr); //Splice the sandbox path according to the actual file location. size_t contentsSize; - // [Start path_buf] - char pathBuf[256] = "/data/storage/el2/base/haps/entry/files/SplicePath.txt"; + // [Start path_buf] + char pathBuf[READ_SIZE] = {0}; + strncpy(pathBuf,FILE_PATH,READ_SIZE); // [End path_buf] //Convert the contents of the text to be written into C-side variables through the Node-API interface. - char contentsBuf[256]; + char contentsBuf[BUFFER_SIZE]; // [Start value_utf8] // [Start value_string] napi_get_value_string_utf8(env, argv[0], contentsBuf, sizeof(contentsBuf), &contentsSize); @@ -102,7 +106,7 @@ static napi_value TransferResourceMgr(napi_env env, napi_callback_info info) { NativeResourceManager *mNativeResMgr = OH_ResourceManager_InitNativeResourceManager(env, argv[0]); // [End native_manage] size_t fileNameSize; - char fileNameBuf[256]; + char fileNameBuf[BUFFER_SIZE]; //Convert the passed-in file name into a C-side variable through the Node-API interface. napi_get_value_string_utf8(env, argv[1], fileNameBuf, sizeof(fileNameBuf), &fileNameSize); // [Start open_raw_file] @@ -133,7 +137,7 @@ static napi_value WriteFileUsingPickerFd(napi_env env, napi_callback_info info) unsigned int fd = -1; size_t contentsSize; - char contentsBuf[256]; + char contentsBuf[BUFFER_SIZE]; // [Start uint_get] //Convert the incoming file descriptor and the contents to be written into the file into C-side variables. napi_get_value_uint32(env, argv[0], &fd); @@ -174,7 +178,7 @@ static napi_value ReadFileUsingPickerFd(napi_env env, napi_callback_info info) { // [End uint32_env] // [Start buff_char] //Use the file operation function of the C standard library to read the file. - char buff[1000]; + char buff[READ_SIZE]; size_t buffSize = read(fd, buff, sizeof(buff)); // [End buff_char] // [Start content_value] -- Gitee