HarmonyOS ndk中如何操作文件沙箱,和正事物理路径对应不上?

如题:HarmonyOS ndk中如何操作文件沙箱,和正事物理路径对应不上?

阅读 486
1 个回答

请参考参考文档:

https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/rawfile-V5

https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/resourcemanager-V5

操作filesDir请参考:

napi\_init.cpp文件中的内容\#

#include "napi/native_api.h"
#include <iostream>
#include "reader.h"
#include "hilog/log.h"

static napi_value Add(napi_env env, napi_callback_info info)
{
size_t requireArgc = 2;
size_t argc = 2;
napi_value args[2] = {nullptr};

napi_get_cb_info(env, info, &argc, args , nullptr, nullptr);

napi_valuetype valuetype0;
napi_typeof(env, args[0], &valuetype0);

napi_valuetype valuetype1;
napi_typeof(env, args[1], &valuetype1);

double value0;
napi_get_value_double(env, args[0], &value0);

double value1;
napi_get_value_double(env, args[1], &value1);

napi_value sum;
napi_create_double(env, value0 + value1, &sum);

return sum;

}

void UnwrapStringParam(std::string &str, napi_env env, napi_value argv) {
size_t len;
napi_status status = napi_get_value_string_utf8(env, argv, nullptr, 0, &len);

std::vector<char> buf(len + 1);
status = napi_get_value_string_utf8(env, argv, buf.data(), len + 1, &len);

str = std::string(buf.data());
}

napi_value readEntry(napi_env env, napi_callback_info info) {
size_t argc = 1;
napi_value args[1] = {nullptr};

napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);

napi_valuetype valuetype0;
napi_typeof(env, args[0], &valuetype0);

std::string inputFileName;
UnwrapStringParam(inputFileName, env, args[0]);

OH_LOG_Print(LOG_APP, LOG_INFO, LOG_DOMAIN, "ndkTest", "xx inFile: %{public}s", inputFileName.c_str());
int32_t ret = readZipEntryTest(inputFileName);
OH_LOG_Print(LOG_APP, LOG_INFO, LOG_DOMAIN, "ndkTest", "xx2 inFile: %{public}s", inputFileName.c_str());

napi_value result;
napi_create_int32(env, ret, &result);

return result;
}

napi_value createFileOut(napi_env env, napi_callback_info info) {
size_t argc = 1;
napi_value args[1] = {nullptr};

napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);

napi_valuetype valuetype0;
napi_typeof(env, args[0], &valuetype0);

std::string inputFileName;
UnwrapStringParam(inputFileName, env, args[0]);

OH_LOG_Print(LOG_APP, LOG_INFO, LOG_DOMAIN, "ndkTest", "xx111 createFile: %{public}s", inputFileName.c_str());
int32_t ret = createFile(inputFileName);
OH_LOG_Print(LOG_APP, LOG_INFO, LOG_DOMAIN, "ndkTest", "xx222 createFile: %{public}s", inputFileName.c_str());

napi_value result;
napi_create_int32(env, ret, &result);

return result;
}

EXTERN_C_START
static napi_value Init(napi_env env, napi_value exports)
{
napi_property_descriptor desc[] = {
{ "add", nullptr, Add, nullptr, nullptr, nullptr, napi_default, nullptr },
{ "readEntry", nullptr, readEntry, nullptr, nullptr, nullptr, napi_default, nullptr },
{ "createFile", nullptr, createFileOut, nullptr, nullptr, nullptr, napi_default, nullptr },
};
napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
return exports;
}
EXTERN_C_END

static napi_module demoModule = {
.nm_version = 1,
.nm_flags = 0,
.nm_filename = nullptr,
.nm_register_func = Init,
.nm_modname = "entry",
.nm_priv = ((void*)0),
.reserved = { 0 },
};

extern "C" __attribute__((constructor)) void RegisterEntryModule(void)
{
napi_module_register(&demoModule);
}

reader.cpp文件中的内容\#

// Node APIs are not fully supported. To solve the compilation error of the interface cannot be found,
// please include "napi/native_api.h".

#include <iostream>
#include <cerrno>
#include "minizip/unzip.h"
#include "hilog/log.h"
#include <fstream> // 包含对文件操作的支持

int readZipEntryTest(std::string inFile) {
OH_LOG_Print(LOG_APP, LOG_INFO, LOG_DOMAIN, "ndkTest", "-- inFile: %{public}s", inFile.c_str());
zlib_filefunc_def *zipFuncPtrs = nullptr;
unzFile zipFile = unzOpen2(inFile.c_str(), zipFuncPtrs);
if (zipFile == nullptr) {
OH_LOG_Print(LOG_APP, LOG_INFO, LOG_DOMAIN, "ndkTest", "-- errno: %{public}d - %{public}s", errno, strerror(errno));
return -1;
}

unz_global_info unzInfo = {}; // Zero-clear.
if (unzGetGlobalInfo(zipFile, &unzInfo) != UNZ_OK) {
OH_LOG_Print(LOG_APP, LOG_INFO, LOG_DOMAIN, "ndkTest", "-- errno: %{public}d - %{public}s", errno,
strerror(errno));
return -2;
}

do {
unz_file_info raw_file_info = {};
char raw_file_name_in_zip[256] = {};

int result = unzGetCurrentFileInfo(zipFile, &raw_file_info, raw_file_name_in_zip,
sizeof(raw_file_name_in_zip) - 1, NULL, 0, NULL, 0);
if (result != UNZ_OK) {
OH_LOG_Print(LOG_APP, LOG_INFO, LOG_DOMAIN, "ndkTest", "-- errno: %{public}d - %{public}s", errno,
strerror(errno));
return -3;
}
if (raw_file_name_in_zip[0] == '\0') {
OH_LOG_Print(LOG_APP, LOG_INFO, LOG_DOMAIN, "ndkTest", "-- errno: %{public}d - %{public}s", errno,
strerror(errno));
return -4;
}
OH_LOG_Print(LOG_APP, LOG_INFO, LOG_DOMAIN, "ndkTest", "-- zipName: %{public}s", raw_file_name_in_zip);
OH_LOG_Print(LOG_APP, LOG_INFO, LOG_DOMAIN, "ndkTest", " compressed_size: %{public}d", (int)raw_file_info.compressed_size);
OH_LOG_Print(LOG_APP, LOG_INFO, LOG_DOMAIN, "ndkTest", " uncompressed_size: %{public}d", (int)raw_file_info.uncompressed_size);

unz_file_pos position = {};
if (unzGetFilePos(zipFile, &position) != UNZ_OK) {
OH_LOG_Print(LOG_APP, LOG_INFO, LOG_DOMAIN, "ndkTest", "-- errno: %{public}d - %{public}s", errno,
strerror(errno));
return -5;
}

if (position.num_of_file + 1 == unzInfo.number_entry) {
break;
} else {
if (unzGoToNextFile(zipFile) != UNZ_OK) {
OH_LOG_Print(LOG_APP, LOG_INFO, LOG_DOMAIN, "ndkTest", "-- errno: %{public}d - %{public}s", errno,
strerror(errno));
return -6;
}
}
} while (true);

return 0;
}

int createFile(std::string inFile) {
OH_LOG_Print(LOG_APP, LOG_INFO, LOG_DOMAIN, "ndkTest", "-- createFile: %{public}s", inFile.c_str());
std::ofstream myfile(inFile);
// 检查文件是否成功打开
if (myfile.is_open()) {
// 向文件写入内容
myfile << "Hello, this is a test.\n";
myfile << "Writing to a file in C++ is easy!\n";

// 关闭文件
myfile.close();

std::cout << "File successfully created and written to." << std::endl;
} else {
std::cout << "Unable to open file";
}

return 0;
}

reader.h头文件中的内容\#

//
// Created on 2024/2/24.
//
// Node APIs are not fully supported. To solve the compilation error of the interface cannot be found,
// please include "napi/native_api.h".

#ifndef MYAPPNDK_READER_H
#define MYAPPNDK_READER_H

#include <string>

int readZipEntryTest(std::string inFile);
int createFile(std::string inFile);

#endif //MYAPPNDK_READER_H

libentry目录中index.d.ts文件中的内容

export const add: (a: number, b: number) => number;
export const readEntry: (inputFilePath: String) => number;
export const createFile: (inputFilePath: String) => number;

CMakeLists.txt文件中的内容

# the minimum version of CMake.
cmake_minimum_required(VERSION 3.4.1)
project(MyAppNdk)

set(NATIVERENDER_ROOT_PATH ${CMAKE_CURRENT_SOURCE_DIR})

set(SRC_PATH ${NATIVERENDER_ROOT_PATH}/../../../src/main/cpp)

if(DEFINED PACKAGE_FIND_FILE)
include(${PACKAGE_FIND_FILE})
endif()

include_directories(${NATIVERENDER_ROOT_PATH}
${NATIVERENDER_ROOT_PATH}/include
${SRC_PATH}
${SRC_PATH}/minizip)

add_library(entry SHARED napi_init.cpp reader.h reader.cpp
${SRC_PATH}/minizip/unzip.h ${SRC_PATH}/minizip/unzip.c
${SRC_PATH}/minizip/crypt.h
# ${SRC_PATH}/minizip/iowin32.h ${SRC_PATH}/minizip/iowin32.c
${SRC_PATH}/minizip/ioapi.h ${SRC_PATH}/minizip/ioapi.c
# ${SRC_PATH}/minizip/miniunz.c
${SRC_PATH}/minizip/minizip.c
${SRC_PATH}/minizip/mztools.h ${SRC_PATH}/minizip/mztools.c
${SRC_PATH}/minizip/zip.h ${SRC_PATH}/minizip/zip.c )
target_link_libraries(entry PUBLIC libace_napi.z.so libz.so libhilog_ndk.z.so)

ArkTS页面处调用的内容

import { hilog } from '@kit.PerformanceAnalysisKit';
import testNapi from 'libentry.so';

const TAG: string = 'testTag'

@Entry
@Component
struct Index {
@State message: string = 'Hello World';

build() {
Row() {
Column() {
Text(this.message)
.fontSize(50)
.fontWeight(FontWeight.Bold)
.onClick(() => {
hilog.info(0x0000, 'testTag', 'Test NAPI 2 + 3 = %{public}d', testNapi.add(2, 3));
})

Button("readZip test.zip").onClick(() => {

let inFile = getContext().filesDir + '/result.zip';

hilog.info(0x0000, TAG, 'inFile内容为:%{public}s', inFile)

let ret = testNapi.readEntry(inFile);
hilog.info(0x0000, 'testTag', "xx ret: " + ret)

})

Button('创建文件').onClick(() => {
hilog.info(0x00000, TAG, '创建文件');
let inFile = getContext().filesDir + '/abc.txt';
hilog.info(0x0000, TAG, '创建的文件:%{public}s', inFile)

let ret = testNapi.createFile(inFile);
hilog.info(0x0000, 'testTag', "创建结果: " + ret)
}).margin({
bottom: '6%'
})
}
.width('100%')
}
.height('100%')
}
}
logo
HarmonyOS
子站问答
访问
宣传栏