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.
| RawDir *OH_ResourceManager_OpenRawDir(const NativeResourceManager *mgr, const char *dirName) | Opens a raw file directory. |
| 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 raw files in the specified 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 raw file. |
| 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 raw file. |
| 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 raw file. |
| 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 a read/write position in a raw file based on the specified offset. |
| 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. |
| 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 a raw 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 raw file to release resources. |
| void OH_ResourceManager_CloseRawFile(RawFile *rawFile) | Closes a file in the **rawfile** directory to release resources. |
| void OH_ResourceManager_CloseRawDir(RawDir *rawDir) | Closes a raw file 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 raw file. |
| 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 raw file. |
| bool OH_ResourceManager_ReleaseRawFileDescriptor(const RawFileDescriptor &descriptor) | Releases the FD of a file in the **rawfile** directory. |
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';
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**.
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.
// Obtain argv[0], which specifies conversion of the JavaScript resource object (that is, OH_ResourceManager_InitNativeResourceManager) to a native object.
// Obtain argv[0], which specifies conversion of the JavaScript resource object (that is, OH_ResourceManager_InitNativeResourceManager) to a native object.
// 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.
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';
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**.
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.
// Obtain argv[0], which specifies conversion of the JavaScript resource object (that is, OH_ResourceManager_InitNativeResourceManager) to a native object.
// Obtain argv[0], which specifies conversion of the JavaScript resource object (that is, OH_ResourceManager_InitNativeResourceManager) to a native object.
// 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.
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
8
...
@@ -16,399 +14,506 @@ These functions include traversing, opening, searching, reading, and closing raw
...
@@ -16,399 +14,506 @@ These functions include traversing, opening, searching, reading, and closing raw
| [raw_dir.h](raw__dir_8h.md)| Provides functions for operating rawfile directories. <br>File to Include: <rawfile/raw_dir.h> |
| [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 for operating rawfiles. <br>File to Include: <rawfile/raw_file.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 functions for managing rawfile resources. <br>File to Include: <rawfile/raw_file_manager.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>|
| [OH_ResourceManager_GetRawFileName](#oh_resourcemanager_getrawfilename)([RawDir](#rawdir)\*rawDir, int index) | Obtains the rawfile name via an index. |
| [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 rawfiles in [RawDir](#rawdir). |
| [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 an opened [RawDir](#rawdir) and releases all associated resources. |
| [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 the rawfile based on the specified offset. |
| [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 length of a rawfile in int32_t. |
| [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 an opened [RawFile](#rawfile) and releases all associated resources. |
| [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 the rawfile in int32_t. |
| [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)&descriptor) | Opens a rawfile descriptor. |
| [OH_ResourceManager_GetRawFileDescriptor](#oh_resourcemanager_getrawfiledescriptor)(const[RawFile](#rawfile) \*rawFile, [RawFileDescriptor](_raw_file_descriptor.md)&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) &descriptor) | Closes a rawfile descriptor. |
| [OH_ResourceManager_ReleaseRawFileDescriptor](#oh_resourcemanager_releaserawfiledescriptor)(const[RawFileDescriptor](_raw_file_descriptor.md) &descriptor) | Releases an FD. |
| [OH_ResourceManager_InitNativeResourceManager](#oh_resourcemanager_initnativeresourcemanager)(napi_env env, napi_value jsResMgr) | Obtains the native resource manager based on JavaScipt resource manager. |
| [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. |
| [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. |
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 RawDirRawDir
```
```
**Description**<br>
Provides the function of accessing rawfile directories.
| descriptor | File descriptor of the file, start position of the file in the hAP, and length of the file.|
**Returns**
**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 )
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 |
| Name | Description |
| -------- | -------- |
| ------ | ----------------------------- |
| rawDir | Indicates the pointer to [RawDir](#rawdir). |
| rawDir | Pointer to the [RawDir](#rawdir). |
| index | Indicates the index of the file in [RawDir](#rawdir). |
| index | Index of the file in the [RawDir](#rawdir).|
**Returns**
**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).
After opening a rawfile directory, you can traverse all the rawfile files in it.
**Description**
**Parameters**
Opens a directory in **rawfile**.
| Name | Description |
After opening the directory, you can traverse all files in it.
| -------- | -------- |
| mgr | Indicates the pointer to [NativeResourceManager](#nativeresourcemanager). You can obtain this pointer by calling [OH_ResourceManager_InitNativeResourceManager](#oh_resourcemanager_initnativeresourcemanager). |
**Parameters**
| dirName | Indicates the name of the rawfile directory to open. If this field is left empty, the root directory of rawfile will be opened. |
| 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**
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.
After a rawfile is opened, you can read the data in it.
**Description**
**Parameters**
Opens a file in **rawfile**.
| Name | Description |
After the file is opened, you can read data in it.
| -------- | -------- |
| mgr | Indicates the pointer to [NativeResourceManager](#nativeresourcemanager). You can obtain this pointer by calling [OH_ResourceManager_InitNativeResourceManager](#oh_resourcemanager_initnativeresourcemanager). |
**Parameters**
| fileName | Indicates the file name in the relative path of the rawfile root directory. |
| 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**
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.
| rawFile | Indicates the pointer to [RawFile](#rawfile). |
| rawFile | Pointer to the target [RawFile](#rawfile). |
| offset | Indicates the specified offset. |
| offset | 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**. |
| 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**
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.