未验证 提交 693efa8b 编写于 作者: O openharmony_ci 提交者: Gitee

!20540 [翻译完成】#I7C19F

Merge pull request !20540 from Annie_wang/PR19346
# Raw File Development
# RawFile Development
## When to Use
This document describes how to use the native Rawfile APIs to manage raw file directories and files in OpenHarmony. You can use Rawfile APIs to perform operations such as traversing the file list, opening, searching for, reading, and closing raw files.
This document describes how to use Native Rawfile APIs to manage the directories and files in the **rawfile** directory in OpenHarmony. You can use the APIs to perform operations, such as traversing a directory and opening, searching for, reading, and closing a file in the the **rawfile** directory.
## Available APIs
| Name | Description |
| :----------------------------------------------------------- | :--------------------------------------- |
| NativeResourceManager *OH_ResourceManager_InitNativeResourceManager(napi_env env, napi_value jsResMgr) | Initializes the native resource manager. |
| RawDir *OH_ResourceManager_OpenRawDir(const NativeResourceManager *mgr, const char *dirName) | Opens a raw file directory. |
| int OH_ResourceManager_GetRawFileCount(RawDir *rawDir) | Obtains the number of raw files in the specified directory.|
| const char *OH_ResourceManager_GetRawFileName(RawDir *rawDir, int index) | Obtains the name of a raw file. |
| RawFile *OH_ResourceManager_OpenRawFile(const NativeResourceManager *mgr, const char *fileName) | Opens a raw file. |
| long OH_ResourceManager_GetRawFileSize(RawFile *rawFile) | Obtains the size of a raw file. |
| int OH_ResourceManager_SeekRawFile(const RawFile *rawFile, long offset, int whence) | Seeks a read/write position in a raw file based on the specified offset. |
| long OH_ResourceManager_GetRawFileOffset(const RawFile *rawFile) | Obtains the offset. |
| int OH_ResourceManager_ReadRawFile(const RawFile *rawFile, void *buf, size_t length) | Reads a raw file. |
| void OH_ResourceManager_CloseRawFile(RawFile *rawFile) | Closes a raw file to release resources. |
| void OH_ResourceManager_CloseRawDir(RawDir *rawDir) | Closes a raw file directory to release resources. |
| bool OH_ResourceManager_GetRawFileDescriptor(const RawFile *rawFile, RawFileDescriptor &descriptor) | Obtains the file descriptor (FD) of a raw file. |
| bool OH_ResourceManager_ReleaseRawFileDescriptor(const RawFileDescriptor &descriptor) | Releases the FD of a raw file. |
| void OH_ResourceManager_ReleaseNativeResourceManager(NativeResourceManager *resMgr) | Releases the native resource manager. |
## How to Develop
The following describes how to obtain the raw file list, raw file content, and raw file descriptor on the JavaScript side as an example.
1. Create a project.
![Creating a C++ application](figures/rawfile1.png)
2. Add dependencies.
After a project is created, the **cpp** directory is created under the project. The directory contains files such as **libentry/index.d.ts**, **hello.cpp**, and **CMakeLists.txt**.
1. Open the **src/main/cpp/CMakeLists.txt** file, and add **librawfile.z.so** and **libhilog_ndk.z.so** to **target_link_libraries**.
```c++
target_link_libraries(entry PUBLIC libace_napi.z.so libhilog_ndk.z.so librawfile.z.so)
```
2. Open the **src/main/cpp/types/libentry/index.d.ts** file, and declare the application functions **getFileList**, **getRawFileContent**, and **getRawFileDescriptor**.
```c++
import resourceManager from '@ohos.resourceManager';
export const getFileList: (resmgr: resourceManager.ResourceManager, path: string) => Array<String>;
export const getRawFileContent: (resmgr: resourceManager.ResourceManager, path: string) => Uint8Array;
export const getRawFileDescriptor: (resmgr: resourceManager.ResourceManager, path: string) => resourceManager.RawFileDescriptor;
```
3. Modify the source file.
1. Open the **src/main/cpp/hello.cpp** file. During initialization, the file maps the external JavaScript APIs **getFileList**, **getRawFileContent**, and **getRawFileDescriptor** to C++ native APIs **GetFileList**, **GetRawFileContent**, and **GetRawFileDescriptor**.
```c++
EXTERN_C_START
static napi_value Init(napi_env env, napi_value exports)
{
napi_property_descriptor desc[] = {
{ "getFileList", nullptr, GetFileList, nullptr, nullptr, nullptr, napi_default, nullptr },
{ "getRawFileContent", nullptr, GetRawFileContent, nullptr, nullptr, nullptr, napi_default, nullptr },
{ "getRawFileDescriptor", nullptr, GetRawFileDescriptor, nullptr, nullptr, nullptr, napi_default, nullptr }
};
napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
return exports;
}
EXTERN_C_END
```
2. Add the three functions to the **src/main/cpp/hello.cpp** file.
```c++
static napi_value GetFileList(napi_env env, napi_callback_info info)
static napi_value GetRawFileContent(napi_env env, napi_callback_info info)
static napi_value GetRawFileDescriptor(napi_env env, napi_callback_info info)
```
3. Obtain JavaScript resource objects from the **hello.cpp** file, and convert them to native resource objects. Then, call the native APIs to obtain the raw file list, raw file content, and raw file descriptor {fd, offset, length}. The sample code is as follows:
```c++
// Example 1: Use GetFileList to obtain the raw file list.
static napi_value GetFileList(napi_env env, napi_callback_info info)
{
OH_LOG_Print(LOG_APP, LOG_ERROR, GLOBAL_RESMGR, tag, "NDKTest Begin");
size_t requireArgc = 3;
size_t argc = 2;
napi_value argv[2] = { nullptr };
// Obtain arguments of the native API.
napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
// Obtain argv[0], which specifies conversion of the JavaScript resource object (that is, OH_ResourceManager_InitNativeResourceManager) to a native object.
NativeResourceManager *mNativeResMgr = OH_ResourceManager_InitNativeResourceManager(env, argv[0]);
// Obtain argv[1], which specifies the relative path of the raw file.
size_t strSize;
char strBuf[256];
napi_get_value_string_utf8(env, argv[1], strBuf, sizeof(strBuf), &strSize);
std::string dirName(strBuf, strSize);
// Obtain the corresponding rawDir pointer object.
RawDir* rawDir = OH_ResourceManager_OpenRawDir(mNativeResMgr, dirName.c_str());
// Obtain the number of files and folders in rawDir.
int count = OH_ResourceManager_GetRawFileCount(rawDir);
// Traverse rawDir to obtain the list of file names and save it.
std::vector<std::string> tempArray;
for(int i = 0; i < count; i++) {
std::string filename = OH_ResourceManager_GetRawFileName(rawDir, i);
tempArray.emplace_back(filename);
}
napi_value fileList;
napi_create_array(env, &fileList);
for (size_t i = 0; i < tempArray.size(); i++) {
napi_value jsString;
napi_create_string_utf8(env, tempArray[i].c_str(), NAPI_AUTO_LENGTH, &jsString);
napi_set_element(env, fileList, i, jsString);
}
// Close the rawDir pointer object.
OH_ResourceManager_CloseRawDir(rawDir);
OH_ResourceManager_ReleaseNativeResourceManager(mNativeResMgr);
return fileList;
}
// Example 2: Use rawDir pointer object to obtain the content of the raw file.
napi_value CreateJsArrayValue(napi_env env, std::unique_ptr<uint8_t[]> &data, long length)
{
napi_value buffer;
napi_status status = napi_create_external_arraybuffer(env, data.get(), length,
[](napi_env env, void *data, void *hint) {
delete[] static_cast<char*>(data);
}, nullptr, &buffer);
if (status != napi_ok) {
OH_LOG_Print(LOG_APP, LOG_ERROR, GLOBAL_RESMGR, tag, "Failed to create external array buffer");
return nullptr;
}
napi_value result = nullptr;
status = napi_create_typedarray(env, napi_uint8_array, length, buffer, 0, &result);
if (status != napi_ok) {
OH_LOG_Print(LOG_APP, LOG_ERROR, GLOBAL_RESMGR, tag, "Failed to create media typed array");
return nullptr;
}
data.release();
return result;
}
static napi_value GetRawFileContent(napi_env env, napi_callback_info info)
{
OH_LOG_Print(LOG_APP, LOG_ERROR, GLOBAL_RESMGR, tag, "GetFileContent Begin");
size_t requireArgc = 3;
size_t argc = 2;
napi_value argv[2] = { nullptr };
// Obtain arguments of the native API.
napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
// Obtain argv[0], which specifies conversion of the JavaScript resource object (that is, OH_ResourceManager_InitNativeResourceManager) to a native object.
NativeResourceManager *mNativeResMgr = OH_ResourceManager_InitNativeResourceManager(env, argv[0]);
size_t strSize;
char strBuf[256];
napi_get_value_string_utf8(env, argv[1], strBuf, sizeof(strBuf), &strSize);
std::string filename(strBuf, strSize);
// Obtain the raw file pointer object.
RawFile *rawFile = OH_ResourceManager_OpenRawFile(mNativeResMgr, filename.c_str());
if (rawFile != nullptr) {
OH_LOG_Print(LOG_APP, LOG_ERROR, GLOBAL_RESMGR, tag, "OH_ResourceManager_OpenRawFile success");
}
// Obtain the size of the raw file and apply for memory.
long len = OH_ResourceManager_GetRawFileSize(rawFile);
std::unique_ptr<uint8_t[]> data= std::make_unique<uint8_t[]>(len);
// Read the raw file.
int res = OH_ResourceManager_ReadRawFile(rawFile, data.get(), len);
// Close the raw file pointer object.
OH_ResourceManager_CloseRawFile(rawFile);
OH_ResourceManager_ReleaseNativeResourceManager(mNativeResMgr);
// Convert the native object to a JavaScript object.
return CreateJsArrayValue(env, data, len);
}
// Example 3: Use GetRawFileDescriptor to obtain the FD of the raw file.
napi_value createJsFileDescriptor(napi_env env, RawFileDescriptor &descriptor)
{
napi_value result;
napi_status status = napi_create_object(env, &result);
if (status != napi_ok) {
return result;
}
napi_value fd;
status = napi_create_int32(env, descriptor.fd, &fd);
if (status != napi_ok) {
return result;
}
status = napi_set_named_property(env, result, "fd", fd);
if (status != napi_ok) {
return result;
}
napi_value offset;
status = napi_create_int64(env, descriptor.start, &offset);
if (status != napi_ok) {
return result;
}
status = napi_set_named_property(env, result, "offset", offset);
if (status != napi_ok) {
return result;
}
napi_value length;
status = napi_create_int64(env, descriptor.length, &length);
if (status != napi_ok) {
return result;
}
status = napi_set_named_property(env, result, "length", length);
if (status != napi_ok) {
return result;
}
return result;
}
static napi_value GetRawFileDescriptor(napi_env env, napi_callback_info info)
{
OH_LOG_Print(LOG_APP, LOG_ERROR, GLOBAL_RESMGR, tag, "NDKTest GetRawFileDescriptor Begin");
size_t requireArgc = 3;
size_t argc = 2;
napi_value argv[2] = { nullptr };
// Obtain arguments of the native API.
napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
napi_valuetype valueType;
napi_typeof(env, argv[0], &valueType);
// Obtain the native resourceManager object.
NativeResourceManager *mNativeResMgr = OH_ResourceManager_InitNativeResourceManager(env, argv[0]);
size_t strSize;
char strBuf[256];
napi_get_value_string_utf8(env, argv[1], strBuf, sizeof(strBuf), &strSize);
std::string filename(strBuf, strSize);
// Obtain the raw file pointer object.
RawFile *rawFile = OH_ResourceManager_OpenRawFile(mNativeResMgr, filename.c_str());
if (rawFile != nullptr) {
OH_LOG_Print(LOG_APP, LOG_ERROR, GLOBAL_RESMGR, tag, "OH_ResourceManager_OpenRawFile success");
}
// Obtain the FD of the raw file, that is, RawFileDescriptor {fd, offset, length}.
RawFileDescriptor descriptor;
OH_ResourceManager_GetRawFileDescriptor(rawFile, descriptor);
// Close the raw file pointer object.
OH_ResourceManager_CloseRawFile(rawFile);
OH_ResourceManager_ReleaseNativeResourceManager(mNativeResMgr);
// Convert the native object to a JavaScript object.
return createJsFileDescriptor(env,descriptor);
}
```
4. Call APIs on the JavaScript side.
1. Open **src\main\ets\pages\index.ets**, and import **libentry.so**.
2. Obtain the JavaScript resource object, that is, **resourceManager**.
3. Call **getFileList**, that is, the native API declared in **src/main/cpp/types/libentry/index.d.ts**. When calling the API, pass the JavaScript resource object and the relative path of the raw file. The sample code is as follows:
```js
import hilog from '@ohos.hilog';
import testNapi from 'libentry.so' // Import the libentry.so file.
@Entry
@Component
struct Index {
@State message: string = 'Hello World'
private resmgr = getContext().resourceManager; // Obtain the JavaScript resource object.
build() {
Row() {
Column() {
Text(this.message)
.fontSize(50)
.fontWeight(FontWeight.Bold)
.onClick(() => {
hilog.isLoggable(0x0000, 'testTag', hilog.LogLevel.INFO);
let rawfilelist = testNapi.getFileList(this.resmgr, ""); // Pass the JavaScript resource object and the relative path of the raw file.
console.log("rawfilelist" + rawfilelist);
let rawfileContet = testNapi.getRawFileContent(this.resmgr, "rawfile1.txt");
console.log("rawfileContet" + rawfileContet);
let rawfileDescriptor = testNapi.getRawFileDescriptor(this.resmgr, "rawfile1.txt");
console.log("getRawFileDescriptor" + rawfileDescriptor.fd, rawfileDescriptor.offset, rawfileDescriptor.length);
})
}
.width('100%')
}
.height('100%')
}
}
```
## Using C++ Functions
| API | Description |
| :----------------------------------------------------------- | :----------------------------------------------------------- |
| NativeResourceManager *OH_ResourceManager_InitNativeResourceManager(napi_env env, napi_value jsResMgr) | Initializes a Native resource manager instance. |
| RawDir *OH_ResourceManager_OpenRawDir(const NativeResourceManager *mgr, const char *dirName) | Opens a directory in the **rawfile** directory. |
| int OH_ResourceManager_GetRawFileCount(RawDir *rawDir) | Obtains the number of files in the specified directory. |
| const char *OH_ResourceManager_GetRawFileName(RawDir *rawDir, int index) | Obtains the name of a file in the **rawfile** directory. |
| RawFile *OH_ResourceManager_OpenRawFile(const NativeResourceManager *mgr, const char *fileName) | Opens a file in the **rawfile** directory. |
| long OH_ResourceManager_GetRawFileSize(RawFile *rawFile) | Obtains the size of a file in the **rawfile** directory. |
| int OH_ResourceManager_SeekRawFile(const RawFile *rawFile, long offset, int whence) | Seeks for the read/write position in a file in the **rawfile** directory based on the specified offset. |
| long OH_ResourceManager_GetRawFileOffset(const RawFile *rawFile) | Obtains the offset of a file. |
| int OH_ResourceManager_ReadRawFile(const RawFile *rawFile, void *buf, size_t length) | Reads data from a file in the **rawfile** directory. |
| void OH_ResourceManager_CloseRawFile(RawFile *rawFile) | Closes a file in the **rawfile** directory to release resources. |
| void OH_ResourceManager_CloseRawDir(RawDir *rawDir) | Closes a directory in the **rawfile** directory to release resources. |
| bool OH_ResourceManager_GetRawFileDescriptor(const RawFile *rawFile, RawFileDescriptor &descriptor) | Obtains the file descriptor (FD) of a file in the **rawfile** directory. |
| bool OH_ResourceManager_ReleaseRawFileDescriptor(const RawFileDescriptor &descriptor) | Releases the FD of a file in the **rawfile** directory. |
| void OH_ResourceManager_ReleaseNativeResourceManager(NativeResourceManager *resMgr) | Releases a Native resource manager instance. |
## Functions
1. Call **OH_ResourceManager_OpenRawDir** to obtain a **RawDir** instance based on the **NativeResourceManager** instance.
```c++
RawDir* rawDir = OH_ResourceManager_OpenRawDir(nativeResourceManager, path.c_str());
```
2. Call **OH_ResourceManager_GetRawFileCount** to obtain the total number of raw files in the directory based on the **RawDir** instance.
2. Call **OH_ResourceManager_GetRawFileCount** to obtain the total number of files in the directory based on the **RawDir** instance.
```c++
int count = OH_ResourceManager_GetRawFileCount(rawDir);
```
3. Call **OH_ResourceManager_GetRawFileName** to obtain the name of the raw file with the specified index.
3. Call **OH_ResourceManager_GetRawFileName** to obtain the name of a file based on the specified index.
```c++
for (int index = 0; index < count; index++) {
......@@ -315,19 +45,19 @@ After a project is created, the **cpp** directory is created under the project.
}
```
4. Call **OH_ResourceManager_OpenRawFile** to obtain a **RawFile** instance with the specified file name.
4. Call **OH_ResourceManager_OpenRawFile** to open a **RawFile** instance with the specified file name.
```c++
RawFile* rawFile = OH_ResourceManager_OpenRawFile(nativeResourceManager, fileName.c_str());
```
5. Call **OH_ResourceManager_GetRawFileSize** to obtain the size of the raw file.
5. Call **OH_ResourceManager_GetRawFileSize** to obtain the size of the file.
```c++
long rawFileSize = OH_ResourceManager_GetRawFileSize(rawFile);
```
6. Call **OH_ResourceManager_SeekRawFile** to seek a read/write position in the raw file based on the specified offset.
6. Call **OH_ResourceManager_SeekRawFile** to seek for the read/write position in the file based on the specified offset.
```c++
int position = OH_ResourceManager_SeekRawFile(rawFile, 10, 0);
......@@ -335,13 +65,13 @@ After a project is created, the **cpp** directory is created under the project.
int position = OH_ResourceManager_SeekRawFile(rawFile, -10, 2);
```
7. Call **OH_ResourceManager_GetRawFileOffset** to obtain the raw file offset.
7. Call **OH_ResourceManager_GetRawFileOffset** to obtain the file offset.
```c++
long rawFileOffset = OH_ResourceManager_GetRawFileOffset(rawFile)
```
8. Call **OH_ResourceManager_ReadRawFile** to read the raw file.
8. Call **OH_ResourceManager_ReadRawFile** to read a file.
```c++
std::unique_ptr<char[]> mediaData = std::make_unique<char[]>(rawFileSize);
......@@ -354,27 +84,314 @@ After a project is created, the **cpp** directory is created under the project.
OH_ResourceManager_CloseRawFile(rawFile);
```
10. Call **OH_ResourceManager_CloseRawDir** to close the raw file directory.
10. Call **OH_ResourceManager_CloseRawDir** to close the file directory.
```c++
OH_ResourceManager_CloseRawDir(rawDir);
```
11. Call **OH_ResourceManager_GetRawFileDescriptor** to obtain the FD of the raw file.
11. Call **OH_ResourceManager_GetRawFileDescriptor** to obtain the FD of the file.
```c++
RawFileDescriptor descriptor;
bool result = OH_ResourceManager_GetRawFileDescriptor(rawFile, descriptor);
```
12. Call **OH_ResourceManager_ReleaseRawFileDescriptor** to release the FD of the raw file.
12. Call **OH_ResourceManager_ReleaseRawFileDescriptor** to release the FD of the file.
```c++
OH_ResourceManager_ReleaseRawFileDescriptor(descriptor);
```
13. Call **OH_ResourceManager_ReleaseNativeResourceManager** to release the native resource manager.
13. Call **OH_ResourceManager_ReleaseNativeResourceManager** to release the Native resource manager.
```c++
OH_ResourceManager_ReleaseNativeResourceManager(nativeResourceManager);
```
## How to Develop
The following describes how to obtain the file list in the **rawfile** directory, file content, and FD {fd, offset, length} on the JavaScript side as an example.
1. Create a project.
![Creating a C++ application](figures/rawfile1.png)
2. Add dependencies.
After the project is created, the **cpp** directory is created under the project. The directory contains files such as **libentry/index.d.ts**, **hello.cpp**, and **CMakeLists.txt**.
1. Open the **src/main/cpp/CMakeLists.txt** file, and add **librawfile.z.so** and **libhilog_ndk.z.so** to **target_link_libraries**.
```
target_link_libraries(entry PUBLIC libace_napi.z.so libhilog_ndk.z.so librawfile.z.so)
```
2. Open the **src/main/cpp/types/libentry/index.d.ts** file, and declare the application functions **getFileList**, **getRawFileContent**, and **getRawFileDescriptor**.
```c++
import resourceManager from '@ohos.resourceManager';
export const getFileList: (resmgr: resourceManager.ResourceManager, path: string) => Array<String>;
export const getRawFileContent: (resmgr: resourceManager.ResourceManager, path: string) => Uint8Array;
export const getRawFileDescriptor: (resmgr: resourceManager.ResourceManager, path: string) => resourceManager.RawFileDescriptor;
```
3. Modify the source file.
1. Open the **src/main/cpp/hello.cpp** file. During initialization, the file maps the external JavaScript APIs **getFileList**, **getRawFileContent**, and **getRawFileDescriptor** to C++ native APIs **GetFileList**, **GetRawFileContent**, and **GetRawFileDescriptor**.
```c++
EXTERN_C_START
static napi_value Init(napi_env env, napi_value exports)
{
napi_property_descriptor desc[] = {
{ "getFileList", nullptr, GetFileList, nullptr, nullptr, nullptr, napi_default, nullptr },
{ "getRawFileContent", nullptr, GetRawFileContent, nullptr, nullptr, nullptr, napi_default, nullptr },
{ "getRawFileDescriptor", nullptr, GetRawFileDescriptor, nullptr, nullptr, nullptr, napi_default, nullptr }
};
napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
return exports;
}
EXTERN_C_END
```
2. Add the three functions to the **src/main/cpp/hello.cpp** file.
```c++
static napi_value GetFileList(napi_env env, napi_callback_info info)
static napi_value GetRawFileContent(napi_env env, napi_callback_info info)
static napi_value GetRawFileDescriptor(napi_env env, napi_callback_info info)
```
3. Obtain JavaScript resource objects from the **hello.cpp** file, and convert them to Native resource objects. Then, call the Native APIs to obtain the file list, file content, and FD {fd, offset, length}.
The sample code is as follows:
```c++
// Example 1: Use GetFileList to obtain the raw file list.
static napi_value GetFileList(napi_env env, napi_callback_info info)
{
OH_LOG_Print(LOG_APP, LOG_ERROR, GLOBAL_RESMGR, tag, "NDKTest Begin");
size_t requireArgc = 3;
size_t argc = 2;
napi_value argv[2] = { nullptr };
// Obtain arguments of the native API.
napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
// Obtain argv[0], which specifies conversion of the JavaScript resource object (that is, OH_ResourceManager_InitNativeResourceManager) to a native object.
NativeResourceManager *mNativeResMgr = OH_ResourceManager_InitNativeResourceManager(env, argv[0]);
// Obtain argv[1], which specifies the relative path of the raw file.
size_t strSize;
char strBuf[256];
napi_get_value_string_utf8(env, argv[1], strBuf, sizeof(strBuf), &strSize);
std::string dirName(strBuf, strSize);
// Obtain the corresponding rawDir pointer object.
RawDir* rawDir = OH_ResourceManager_OpenRawDir(mNativeResMgr, dirName.c_str());
// Obtain the number of files and folders in rawDir.
int count = OH_ResourceManager_GetRawFileCount(rawDir);
// Traverse rawDir to obtain the list of file names and save it.
std::vector<std::string> tempArray;
for(int i = 0; i < count; i++) {
std::string filename = OH_ResourceManager_GetRawFileName(rawDir, i);
tempArray.emplace_back(filename);
}
napi_value fileList;
napi_create_array(env, &fileList);
for (size_t i = 0; i < tempArray.size(); i++) {
napi_value jsString;
napi_create_string_utf8(env, tempArray[i].c_str(), NAPI_AUTO_LENGTH, &jsString);
napi_set_element(env, fileList, i, jsString);
}
// Close the rawDir pointer object.
OH_ResourceManager_CloseRawDir(rawDir);
OH_ResourceManager_ReleaseNativeResourceManager(mNativeResMgr);
return fileList;
}
// Example 2: Use rawDir pointer object to obtain the content of the raw file.
napi_value CreateJsArrayValue(napi_env env, std::unique_ptr<uint8_t[]> &data, long length)
{
napi_value buffer;
napi_status status = napi_create_external_arraybuffer(env, data.get(), length,
[](napi_env env, void *data, void *hint) {
delete[] static_cast<char*>(data);
}, nullptr, &buffer);
if (status != napi_ok) {
OH_LOG_Print(LOG_APP, LOG_ERROR, GLOBAL_RESMGR, tag, "Failed to create external array buffer");
return nullptr;
}
napi_value result = nullptr;
status = napi_create_typedarray(env, napi_uint8_array, length, buffer, 0, &result);
if (status != napi_ok) {
OH_LOG_Print(LOG_APP, LOG_ERROR, GLOBAL_RESMGR, tag, "Failed to create media typed array");
return nullptr;
}
data.release();
return result;
}
static napi_value GetRawFileContent(napi_env env, napi_callback_info info)
{
OH_LOG_Print(LOG_APP, LOG_ERROR, GLOBAL_RESMGR, tag, "GetFileContent Begin");
size_t requireArgc = 3;
size_t argc = 2;
napi_value argv[2] = { nullptr };
// Obtain arguments of the native API.
napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
// Obtain argv[0], which specifies conversion of the JavaScript resource object (that is, OH_ResourceManager_InitNativeResourceManager) to a native object.
NativeResourceManager *mNativeResMgr = OH_ResourceManager_InitNativeResourceManager(env, argv[0]);
size_t strSize;
char strBuf[256];
napi_get_value_string_utf8(env, argv[1], strBuf, sizeof(strBuf), &strSize);
std::string filename(strBuf, strSize);
// Obtain the raw file pointer object.
RawFile *rawFile = OH_ResourceManager_OpenRawFile(mNativeResMgr, filename.c_str());
if (rawFile != nullptr) {
OH_LOG_Print(LOG_APP, LOG_ERROR, GLOBAL_RESMGR, tag, "OH_ResourceManager_OpenRawFile success");
}
// Obtain the size of the raw file and apply for memory.
long len = OH_ResourceManager_GetRawFileSize(rawFile);
std::unique_ptr<uint8_t[]> data= std::make_unique<uint8_t[]>(len);
// Read the raw file.
int res = OH_ResourceManager_ReadRawFile(rawFile, data.get(), len);
// Close the rawDir pointer object.
OH_ResourceManager_CloseRawFile(rawFile);
OH_ResourceManager_ReleaseNativeResourceManager(mNativeResMgr);
// Convert the native object to a JavaScript object.
return CreateJsArrayValue(env, data, len);
}
// Example 3: Use GetRawFileDescriptor to obtain the FD of the raw file.
napi_value createJsFileDescriptor(napi_env env, RawFileDescriptor &descriptor)
{
napi_value result;
napi_status status = napi_create_object(env, &result);
if (status != napi_ok) {
return result;
}
napi_value fd;
status = napi_create_int32(env, descriptor.fd, &fd);
if (status != napi_ok) {
return result;
}
status = napi_set_named_property(env, result, "fd", fd);
if (status != napi_ok) {
return result;
}
napi_value offset;
status = napi_create_int64(env, descriptor.start, &offset);
if (status != napi_ok) {
return result;
}
status = napi_set_named_property(env, result, "offset", offset);
if (status != napi_ok) {
return result;
}
napi_value length;
status = napi_create_int64(env, descriptor.length, &length);
if (status != napi_ok) {
return result;
}
status = napi_set_named_property(env, result, "length", length);
if (status != napi_ok) {
return result;
}
return result;
}
static napi_value GetRawFileDescriptor(napi_env env, napi_callback_info info)
{
OH_LOG_Print(LOG_APP, LOG_ERROR, GLOBAL_RESMGR, tag, "NDKTest GetRawFileDescriptor Begin");
size_t requireArgc = 3;
size_t argc = 2;
napi_value argv[2] = { nullptr };
// Obtain arguments of the native API.
napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
napi_valuetype valueType;
napi_typeof(env, argv[0], &valueType);
// Obtain the native resourceManager object.
NativeResourceManager *mNativeResMgr = OH_ResourceManager_InitNativeResourceManager(env, argv[0]);
size_t strSize;
char strBuf[256];
napi_get_value_string_utf8(env, argv[1], strBuf, sizeof(strBuf), &strSize);
std::string filename(strBuf, strSize);
// Obtain the raw file pointer object.
RawFile *rawFile = OH_ResourceManager_OpenRawFile(mNativeResMgr, filename.c_str());
if (rawFile != nullptr) {
OH_LOG_Print(LOG_APP, LOG_ERROR, GLOBAL_RESMGR, tag, "OH_ResourceManager_OpenRawFile success");
}
// Obtain the FD of the raw file, that is, RawFileDescriptor {fd, offset, length}.
RawFileDescriptor descriptor;
OH_ResourceManager_GetRawFileDescriptor(rawFile, descriptor);
// Close the rawDir pointer object.
OH_ResourceManager_CloseRawFile(rawFile);
OH_ResourceManager_ReleaseNativeResourceManager(mNativeResMgr);
// Convert the native object to a JavaScript object.
return createJsFileDescriptor(env,descriptor);
}
```
4. Call JavaScript APIs.
1. Open **src\main\ets\pages\index.ets**, and import **libentry.so**.
2. Obtain the JavaScript resource object, that is, **resourceManager**.
3. Call **getFileList**, that is, the Native API declared in **src/main/cpp/types/libentry/index.d.ts**. When calling the API, pass in the JavaScript resource object and the relative path of the file.
The sample code is as follows:
```js
import hilog from '@ohos.hilog';
import testNapi from 'libentry.so' // Import the libentry.so file.
@Entry
@Component
struct Index {
@State message: string = 'Hello World'
private resmgr = getContext().resourceManager; // Obtain the JavaScript resource object.
build() {
Row() {
Column() {
Text(this.message)
.fontSize(50)
.fontWeight(FontWeight.Bold)
.onClick(() => {
hilog.isLoggable(0x0000, 'testTag', hilog.LogLevel.INFO);
let rawfilelist = testNapi.getFileList(this.resmgr, ""); // Pass the JavaScript resource object and the relative path of the raw file.
console.log("rawfilelist" + rawfilelist);
let rawfileContet = testNapi.getRawFileContent(this.resmgr, "rawfile1.txt");
console.log("rawfileContet" + rawfileContet);
let rawfileDescriptor = testNapi.getRawFileDescriptor(this.resmgr, "rawfile1.txt");
console.log("getRawFileDescriptor" + rawfileDescriptor.fd, rawfileDescriptor.offset, rawfileDescriptor.length);
})
}
.width('100%')
}
.height('100%')
}
}
```
# Rawfile
## Overview
Provides APIs for operating the **rawfile** directory and its files, including traversing the **rawfile** directory and opening, searching for, reading, and closing a file in it.
Provides the function of operating rawfile directories and rawfiles.
**Since**
These functions include traversing, opening, searching, reading, and closing rawfiles.
**Since:**
8
......@@ -16,399 +14,506 @@ These functions include traversing, opening, searching, reading, and closing raw
### Files
| Name | Description |
| -------- | -------- |
| [raw_dir.h](raw__dir_8h.md) | Provides functions for operating rawfile directories. <br>File to Include: <rawfile/raw_dir.h> |
| [raw_file.h](raw__file_8h.md) | Provides functions for operating rawfiles. <br>File to Include: <rawfile/raw_file.h>|
| [raw_file_manager.h](raw__file__manager_8h.md) | Provides functions for managing rawfile resources. <br>File to Include: <rawfile/raw_file_manager.h>|
| Name | Description |
| ---------------------------------------- | ------------------ |
| [raw_dir.h](raw__dir_8h.md) | Provides functions related to the **rawfile** directory.<br>File to include: \<rawfile/raw_dir.h> |
| [raw_file.h](raw__file_8h.md) | Provides functions related to the files in the **rawfile** directory.<br>File to include: \<rawfile/raw_file.h> |
| [raw_file_manager.h](raw__file__manager_8h.md) | Provides file management functions for the **rawfile** directory.<br>File to import: \<rawfile/raw_file_manager.h>|
### Structs
| Name | Description |
| -------- | -------- |
| [RawFileDescriptor](_raw_file_descriptor.md) | Provides rawfile descriptor information. |
| Name | Description |
| ---------------------------------------- | ----------------- |
| [RawFileDescriptor](_raw_file_descriptor.md) | Defines the file descriptor (FD) information of a file in the **rawfile** directory. |
### Types
| Name | Description |
| -------- | -------- |
| [RawDir](#rawdir) | Provides the function of accessing rawfile directories. |
| [RawFile](#rawfile) | Provides the function of accessing rawfiles. |
| [NativeResourceManager](#nativeresourcemanager) | Implements the resource manager. |
| Name | Description |
| ---------------------------------------- | ------------------- |
| [RawDir](#rawdir) | Provides access to the **rawfile** directory. |
| [RawFile](#rawfile) | Provides access to the files in **rawfile**. |
| [NativeResourceManager](#nativeresourcemanager) | Represents the resource manager.|
### Functions
| Name | Description |
| -------- | -------- |
| [OH_ResourceManager_GetRawFileName](#oh_resourcemanager_getrawfilename) ([RawDir](#rawdir) \*rawDir, int index) | Obtains the rawfile name via an index. |
| [OH_ResourceManager_GetRawFileCount](#oh_resourcemanager_getrawfilecount) ([RawDir](#rawdir) \*rawDir) |Obtains the number of rawfiles in [RawDir](#rawdir). |
| [OH_ResourceManager_CloseRawDir](#oh_resourcemanager_closerawdir) ([RawDir](#rawdir) \*rawDir) | Closes an opened [RawDir](#rawdir) and releases all associated resources. |
| [OH_ResourceManager_ReadRawFile](#oh_resourcemanager_readrawfile) (const [RawFile](#rawfile) \*rawFile, void \*buf, size_t length) |Reads a rawfile. |
| [OH_ResourceManager_SeekRawFile](#oh_resourcemanager_seekrawfile) (const [RawFile](#rawfile) \*rawFile, long offset, int whence) |Seeks for the data read/write position in the rawfile based on the specified offset. |
| [OH_ResourceManager_GetRawFileSize](#oh_resourcemanager_getrawfilesize) ([RawFile](#rawfile) \*rawFile) | Obtains the length of a rawfile in int32_t. |
| [OH_ResourceManager_CloseRawFile](#oh_resourcemanager_closerawfile) ([RawFile](#rawfile) \*rawFile) | Closes an opened [RawFile](#rawfile) and releases all associated resources. |
| [OH_ResourceManager_GetRawFileOffset](#oh_resourcemanager_getrawfileoffset) (const [RawFile](#rawfile) \*rawFile) | Obtains the current offset of the rawfile in int32_t. |
| [OH_ResourceManager_GetRawFileDescriptor](#oh_resourcemanager_getrawfiledescriptor) (const [RawFile](#rawfile) \*rawFile, [RawFileDescriptor](_raw_file_descriptor.md) &amp;descriptor) | Opens a rawfile descriptor. |
| [OH_ResourceManager_ReleaseRawFileDescriptor](#oh_resourcemanager_releaserawfiledescriptor) (const [RawFileDescriptor](_raw_file_descriptor.md) &amp;descriptor) | Closes a rawfile descriptor. |
| [OH_ResourceManager_InitNativeResourceManager](#oh_resourcemanager_initnativeresourcemanager) (napi_env env, napi_value jsResMgr) | Obtains the native resource manager based on JavaScipt resource manager. |
| [OH_ResourceManager_ReleaseNativeResourceManager](#oh_resourcemanager_releasenativeresourcemanager) ([NativeResourceManager](#nativeresourcemanager) \*resMgr) | Releases a native resource manager. |
| [OH_ResourceManager_OpenRawDir](#oh_resourcemanager_openrawdir) (const [NativeResourceManager](#nativeresourcemanager) \*mgr, const char \*dirName) | Opens a rawfile directory. |
| [OH_ResourceManager_OpenRawFile](#oh_resourcemanager_openrawfile) (const [NativeResourceManager](#nativeresourcemanager) \*mgr, const char \*fileName) | Opens a rawfile. |
| Name | Description |
| ------------------------------------------------------------ | ------------------------------------------------------------ |
| [OH_ResourceManager_GetRawFileName](#oh_resourcemanager_getrawfilename) ([RawDir](#rawdir) \*rawDir, int index) | Obtains the name of a file in **rawfile** based on the index. |
| [OH_ResourceManager_GetRawFileCount](#oh_resourcemanager_getrawfilecount) ([RawDir](#rawdir) \*rawDir) | Obtains the number of files in a [RawDir](#rawdir). |
| [OH_ResourceManager_CloseRawDir](#oh_resourcemanager_closerawdir) ([RawDir](#rawdir) \*rawDir) | Closes a [RawDir](#rawdir) and releases all associated resources. |
| [OH_ResourceManager_ReadRawFile](#oh_resourcemanager_readrawfile) (const [RawFile](#rawfile) \*rawFile, void \*buf, size_t length) | Reads data from a file in **rawfile**. |
| [OH_ResourceManager_SeekRawFile](#oh_resourcemanager_seekrawfile) (const [RawFile](#rawfile) \*rawFile, long offset, int whence) | Seeks for the data read/write position in a file in **rawfile** based on the specified offset. |
| [OH_ResourceManager_GetRawFileSize](#oh_resourcemanager_getrawfilesize) ([RawFile](#rawfile) \*rawFile) | Obtains the size of a file in **rawfile**. |
| [OH_ResourceManager_CloseRawFile](#oh_resourcemanager_closerawfile) ([RawFile](#rawfile) \*rawFile) | Closes a [RawFile](#rawfile) and releases all associated resources. |
| [OH_ResourceManager_GetRawFileOffset](#oh_resourcemanager_getrawfileoffset) (const [RawFile](#rawfile) \*rawFile) | Obtains the current offset of a file in **rawfile**. |
| [OH_ResourceManager_GetRawFileDescriptor](#oh_resourcemanager_getrawfiledescriptor) (const [RawFile](#rawfile) \*rawFile, [RawFileDescriptor](_raw_file_descriptor.md) &amp;descriptor) | Opens a file in **rawfile** based on the offset and file length and obtains the FD. |
| [OH_ResourceManager_ReleaseRawFileDescriptor](#oh_resourcemanager_releaserawfiledescriptor) (const [RawFileDescriptor](_raw_file_descriptor.md) &amp;descriptor) | Releases an FD. |
| [OH_ResourceManager_InitNativeResourceManager](#oh_resourcemanager_initnativeresourcemanager) (napi_env env, napi_value jsResMgr) | Initializes a Native resource manager using the JavaScript resource manager. You can use the Native resource manager obtained to implement operations related to **rawfile**. |
| [OH_ResourceManager_ReleaseNativeResourceManager](#oh_resourcemanager_releasenativeresourcemanager) ([NativeResourceManager](#nativeresourcemanager) \*resMgr) | Releases a Native resource manager instance. |
| [OH_ResourceManager_OpenRawDir](#oh_resourcemanager_openrawdir) (const [NativeResourceManager](#nativeresourcemanager) \*mgr, const char \*dirName) | Opens a directory in the **rawfile** directory. |
| [OH_ResourceManager_OpenRawFile](#oh_resourcemanager_openrawfile) (const [NativeResourceManager](#nativeresourcemanager) \*mgr, const char \*fileName) | Opens a file in the **rawfile** directory. |
## Type Description
## Description
### NativeResourceManager
### Type Description
#### NativeResourceManager
```
typedef struct NativeResourceManagerNativeResourceManager
typedef struct NativeResourceManager NativeResourceManager
```
**Description**<br>
Implements the resource manager.
This class encapsulates the native implementation of the JavaScript resource manager. You can obtain the pointer to **ResourceManager** by calling [OH_ResourceManager_InitNativeResourceManager](#oh_resourcemanager_initnativeresourcemanager).
**Description**
Represents the resource manager.
This class encapsulates the native implementation of the JavaScript resource manager. The **ResourceManager** pointer can be obtained by using [OH_ResourceManager_InitNativeResourceManager](#oh_resourcemanager_initnativeresourcemanager).
**Since**
8
#### RawDir
### RawDir
```
typedef struct RawDirRawDir
typedef struct RawDir RawDir
```
**Description**<br>
Provides the function of accessing rawfile directories.
**Description**
Provides access to the **rawfile** directory.
**Since**
8
### RawFile
#### RawFile
```
typedef struct RawFileRawFile
typedef struct RawFile RawFile
```
**Description**<br>
Provides the function of accessing rawfiles.
**Description**
Provides access to the files in **rawfile**.
**Since**
8
### Function Description
## Function Description
#### OH_ResourceManager_CloseRawDir()
### OH_ResourceManager_CloseRawDir()
```
void OH_ResourceManager_CloseRawDir (RawDir * rawDir)
```
**Description**<br>
Closes an opened [RawDir](#rawdir) and releases all associated resources.
**Parameters**
**Description**
| Name | Description |
| -------- | -------- |
| rawDir | Indicates the pointer to [RawDir](#rawdir). |
Closes a [RawDir](#rawdir) opened and releases all associated resources.
**See**
**Parameters**
| Name | Description |
| ------ | ------------------------- |
| rawDir | Pointer to the [RawDir](#rawdir) to close.|
**See**
[OH_ResourceManager_OpenRawDir](#oh_resourcemanager_openrawdir)
**Since**
8
#### OH_ResourceManager_CloseRawFile()
### OH_ResourceManager_CloseRawFile()
```
void OH_ResourceManager_CloseRawFile (RawFile * rawFile)
```
**Description**<br>
Closes an opened [RawFile](#rawfile) and releases all associated resources.
**Parameters**
**Description**
| Name | Description |
| -------- | -------- |
| rawFile | Indicates the pointer to [RawFile](#rawfile). |
Closes a [RawFile](#rawfile) and releases all associated resources.
**See**
**Parameters**
| Name | Description |
| ------- | --------------------------- |
| rawFile | Pointer to the [RawFile](#rawfile) to close.|
**See**
[OH_ResourceManager_OpenRawFile](#oh_resourcemanager_openrawfile)
**Since**
8
#### OH_ResourceManager_GetRawFileCount()
### OH_ResourceManager_GetRawFileCount()
```
int OH_ResourceManager_GetRawFileCount (RawDir * rawDir)
```
**Description**<br>
Obtains the number of rawfiles in [RawDir](#rawdir).
**Description**
Obtains the number of files in a [RawDir](#rawdir).
You can use this function to obtain available indexes in [OH_ResourceManager_GetRawFileName](#oh_resourcemanager_getrawfilename).
**Parameters**
**Parameters**
| Name | Description |
| -------- | -------- |
| rawDir | Indicates the pointer to [RawDir](#rawdir). |
| Name | Description |
| ------ | ------------------------- |
| rawDir | Pointer to the target [RawDir](#rawdir).|
**See**
**See**
[OH_ResourceManager_GetRawFileName](#oh_resourcemanager_getrawfilename)
**Since**
8
#### OH_ResourceManager_GetRawFileDescriptor()
### OH_ResourceManager_GetRawFileDescriptor()
```
bool OH_ResourceManager_GetRawFileDescriptor (const RawFile * rawFile, RawFileDescriptor & descriptor )
```
**Description**<br>
Opens a rawfile descriptor.
After the descriptor is opened, you can use it to read the rawfile based on the offset (in int32_t) and file length.
**Description**
Opens a file in the **rawfile** directory based on the offset and file length and obtains the FD.
**Parameters**
The FD obtained can be used to read the file.
| Name | Description |
| -------- | -------- |
| rawFile | Indicates the pointer to [RawFile](#rawfile). |
| descriptor | Indicates the rawfile descriptor, and the start position and length of the rawfile file in the HAP package. |
**Parameters**
| Name | Description |
| ---------- | ---------------------------------------------------- |
| rawFile | Pointer to the [RawFile](#rawfile). |
| descriptor | File descriptor of the file, start position of the file in the hAP, and length of the file.|
**Returns**
Returns **true** if the rawfile descriptor is opened successfully; returns **false** if the rawfile cannot be accessed.
Returns <b>true</b> if the file is opened; returns <b>false</b> if the access to the file is rejected.
**Since**
8
### OH_ResourceManager_GetRawFileName()
#### OH_ResourceManager_GetRawFileName()
```
const char* OH_ResourceManager_GetRawFileName (RawDir * rawDir, int index )
```
**Description**<br>
Obtains the rawfile name via an index.
You can use this function to traverse a rawfile directory.
**Description**
Obtains the name of a file in **rawfile** based on the index.
You can use this function to traverse the **rawfile** directory.
**Parameters**
**Parameters**
| Name | Description |
| -------- | -------- |
| rawDir | Indicates the pointer to [RawDir](#rawdir). |
| index | Indicates the index of the file in [RawDir](#rawdir). |
| Name | Description |
| ------ | ----------------------------- |
| rawDir | Pointer to the [RawDir](#rawdir). |
| index | Index of the file in the [RawDir](#rawdir).|
**Returns**
Returns the rawfile name via an index. The return value can be used as the input parameter of [OH_ResourceManager_OpenRawFile](#oh_resourcemanager_openrawfile). If no rawfile is found after all rawfiles are traversed, **NULL** will be returned.
Returns the file name obtained if the file exists in the directory; returns **null** otherwise. The file name returned can be used as the input parameter of [OH_ResourceManager_OpenRawFile](#oh_resourcemanager_openrawfile).
**See**
**See**
[OH_ResourceManager_OpenRawFile](#oh_resourcemanager_openrawfile)
**Since**
8
#### OH_ResourceManager_GetRawFileOffset()
### OH_ResourceManager_GetRawFileOffset()
```
long OH_ResourceManager_GetRawFileOffset (const RawFile * rawFile)
```
**Description**<br>
Obtains the current offset of the rawfile in int32_t.
**Parameters**
**Description**
Obtains the current offset of a file in **rawfile**.
The offset indicates the position of the file in the HAP.
| Name | Description |
| -------- | -------- |
| rawFile | Indicates the pointer to [RawFile](#rawfile). |
**Parameters**
| Name | Description |
| ------- | --------------------------- |
| rawFile | Pointer to the target [RawFile](#rawfile).|
**Returns**
Returns the current offset of the rawfile.
Returns the file offset obtained.
**Since**
8
### OH_ResourceManager_GetRawFileSize()
#### OH_ResourceManager_GetRawFileSize()
```
long OH_ResourceManager_GetRawFileSize (RawFile * rawFile)
```
**Description**<br>
Obtains the length of a rawfile in int32_t.
**Parameters**
**Description**
Obtains the size of a file in **rawfile**.
**Parameters**
| Name | Description |
| -------- | -------- |
| rawFile | Indicates the pointer to [RawFile](#rawfile). |
| Name | Description |
| ------- | --------------------------- |
| rawFile | Pointer to the target [RawFile](#rawfile).|
**Returns**
Returns the total length of the rawfile.
Returns the file size obtained.
**Since**
8
#### OH_ResourceManager_InitNativeResourceManager()
### OH_ResourceManager_InitNativeResourceManager()
```
NativeResourceManager* OH_ResourceManager_InitNativeResourceManager (napi_env env, napi_value jsResMgr )
```
**Description**<br>
Obtains the native resource manager based on JavaScipt resource manager.
After obtaining a resource manager, you can use it complete various rawfile operations.
**Description**
Initializes a Native resource manager using the JavaScript resource manager.
You can use the resource manager obtained to implement **rawfile** operations.
**Parameters**
**Parameters**
| Name | Description |
| -------- | -------- |
| env | Indicates the pointer to the JavaScipt Native Interface (napi) environment. |
| jsResMgr | Indicates the JavaScipt resource manager. |
| Name | Description |
| -------- | ---------------------------------------- |
| env | Pointer to the JavaScript Native API (napi) environment.|
| jsResMgr | JavaScript resource manager. |
**Returns**
Returns the pointer to [NativeResourceManager](#nativeresourcemanager).
Returns the pointer to the [NativeResourceManager](#nativeresourcemanager) obtained.
**Since**
8
#### OH_ResourceManager_OpenRawDir()
### OH_ResourceManager_OpenRawDir()
```
RawDir* OH_ResourceManager_OpenRawDir (const NativeResourceManager * mgr, const char * dirName )
```
**Description**<br>
Opens a rawfile directory.
After opening a rawfile directory, you can traverse all the rawfile files in it.
**Description**
**Parameters**
Opens a directory in **rawfile**.
| Name | Description |
| -------- | -------- |
| mgr | Indicates the pointer to [NativeResourceManager](#nativeresourcemanager). You can obtain this pointer by calling [OH_ResourceManager_InitNativeResourceManager](#oh_resourcemanager_initnativeresourcemanager). |
| dirName | Indicates the name of the rawfile directory to open. If this field is left empty, the root directory of rawfile will be opened. |
After opening the directory, you can traverse all files in it.
**Parameters**
| Name | Description |
| ------- | ---------------------------------------- |
| mgr | Pointer to the [NativeResourceManager](#nativeresourcemanager), which is obtained by using [OH_ResourceManager_InitNativeResourceManager](#oh_resourcemanager_initnativeresourcemanager).|
| dirName | Pointer to the name of the directory to open. If this field is left empty, the root directory will be opened.|
**Returns**
Returns the pointer to [RawDir](#rawdir). If this pointer is no longer needed after use, call [OH_ResourceManager_CloseRawDir](#oh_resourcemanager_closerawdir) to release it.
Returns the pointer to the [RawDir](#rawdir) opened. You can use [OH_ResourceManager_CloseRawDir](#oh_resourcemanager_closerawdir) to close the directory and release resources.
**See**
**See**
[OH_ResourceManager_InitNativeResourceManager](#oh_resourcemanager_initnativeresourcemanager)
[OH_ResourceManager_CloseRawDir](#oh_resourcemanager_closerawdir)
**Since**
8
#### OH_ResourceManager_OpenRawFile()
### OH_ResourceManager_OpenRawFile()
```
RawFile* OH_ResourceManager_OpenRawFile (const NativeResourceManager * mgr, const char * fileName )
```
**Description**<br>
Opens a rawfile.
After a rawfile is opened, you can read the data in it.
**Description**
**Parameters**
Opens a file in **rawfile**.
| Name | Description |
| -------- | -------- |
| mgr | Indicates the pointer to [NativeResourceManager](#nativeresourcemanager). You can obtain this pointer by calling [OH_ResourceManager_InitNativeResourceManager](#oh_resourcemanager_initnativeresourcemanager). |
| fileName | Indicates the file name in the relative path of the rawfile root directory. |
After the file is opened, you can read data in it.
**Parameters**
| Name | Description |
| -------- | ---------------------------------------- |
| mgr | Pointer to the [NativeResourceManager](#nativeresourcemanager), which is obtained by using [OH_ResourceManager_InitNativeResourceManager](#oh_resourcemanager_initnativeresourcemanager).|
| fileName | Pointer to the name of the file in the relative path of the **rawfile** root directory. |
**Returns**
Returns the pointer to [RawFile](#rawfile). If this pointer is no longer needed after use, call [OH_ResourceManager_CloseRawFile](#oh_resourcemanager_closerawfile) to release it.
Returns the pointer to the [RawFile](#rawfile) opened. You can use [OH_ResourceManager_CloseRawFile](#oh_resourcemanager_closerawfile) to close the file and release resources.
**See**
**See**
[OH_ResourceManager_InitNativeResourceManager](#oh_resourcemanager_initnativeresourcemanager)
[OH_ResourceManager_CloseRawFile](#oh_resourcemanager_closerawfile)
**Since**
8
#### OH_ResourceManager_ReadRawFile()
### OH_ResourceManager_ReadRawFile()
```
int OH_ResourceManager_ReadRawFile (const RawFile * rawFile, void * buf, size_t length )
```
**Description**<br>
Reads a rawfile.
**Description**
Reads a file in **rawfile**.
You can use this function to read data of the specified length from the current position.
**Parameters**
**Parameters**
| Name | Description |
| -------- | -------- |
| rawFile | Indicates the pointer to [RawFile](#rawfile). |
| buf | Indicates the pointer to the buffer for storing the read data. |
| length | Indicates the length of the read data, in bytes. |
| Name | Description |
| ------- | --------------------------- |
| rawFile | Pointer to the [RawFile](#rawfile) to read.|
| buf | Pointer to the buffer for receiving the read data. |
| length | Length of the data to read. |
**Returns**
Returns the length of the read data in bytes. If the length is beyond the end of the rawfile, **0** will be returned.
Returns the number of bytes read. If the read length exceeds the length of the file end, **0** will be returned.
**Since**
8
#### OH_ResourceManager_ReleaseNativeResourceManager()
### OH_ResourceManager_ReleaseNativeResourceManager()
```
void OH_ResourceManager_ReleaseNativeResourceManager (NativeResourceManager * resMgr)
```
**Description**<br>
Releases a native resource manager.
**Parameters**
**Description**
Releases a Native resource manager instance.
**Parameters**
| Name | Description |
| ------ | ---------------------------------------- |
| resMgr | Pointer to the [NativeResourceManager](#nativeresourcemanager) instance to release.|
**Since**
8
| Name | Description |
| -------- | -------- |
| resMgr | Indicates the pointer to [NativeResourceManager](#nativeresourcemanager). |
#### OH_ResourceManager_ReleaseRawFileDescriptor()
### OH_ResourceManager_ReleaseRawFileDescriptor()
```
bool OH_ResourceManager_ReleaseRawFileDescriptor (const RawFileDescriptor & descriptor)
```
**Description**<br>
Closes a rawfile descriptor.
To prevent file descriptor leakage, you are advised to release a rawfile descriptor after use.
**Description**
Releases the FD of a file in **rawfile**.
To prevent FD leakage, you are advised to release an FD immediately after use.
**Parameters**
**Parameters**
| Name | Description |
| -------- | -------- |
| descriptor | Indicates the rawfile descriptor, and the start position and length of the rawfile file in the HAP package. |
| Name | Description |
| ---------- | ------------------------------------------------------------ |
| descriptor | File descriptor to close. It contains the FD, start position in the HAP, and file length. |
**Returns**
Returns **true** if the rawfile descriptor is closed successfully; returns **false** otherwise.
Returns <b>true</b> if the FD is released; returns <b>false</b> otherwise.
**Since**
8
### OH_ResourceManager_SeekRawFile()
#### OH_ResourceManager_SeekRawFile()
```
int OH_ResourceManager_SeekRawFile (const RawFile * rawFile, long offset, int whence )
```
**Description**<br>
Seeks for the data read/write position in the rawfile based on the specified offset.
**Parameters**
**Description**
Seeks for the data read/write position in a file in **rawfile** based on the specified offset.
**Parameters**
| Name | Description |
| -------- | -------- |
| rawFile | Indicates the pointer to [RawFile](#rawfile). |
| offset | Indicates the specified offset. |
| whence | Indicates the data read/write position. The options are as follows:<br/>**0**: The read/write position is **offset**.<br/>**1**: The read/write position is the current position plus **offset**.<br/>**2**: The read/write position is the end of the file (EOF) plus **offset**. |
| Name | Description |
| ------- | ---------------------------------------- |
| rawFile | Pointer to the target [RawFile](#rawfile). |
| offset | Offset. |
| whence | Read/Write position. The options are as follows:<br>**0**: The read/write position is the offset.<br>**1**: The read/write position is the current position plus the offset.<br>**2**: The read/write position is the end of the file (EOF) plus the offset.|
**Returns**
Returns the new data read/write position if the operation is successful; returns **(long) -1** otherwise.
Returns the read/write position if the operation is successful; returns **(long) -1** otherwise.
**Since**
8
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册