rawfile-guidelines.md 17.9 KB
Newer Older
A
Annie_wang 已提交
1
# RawFile Development
A
Annie_wang 已提交
2 3 4

## When to Use

A
Annie_wang 已提交
5
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.
A
Annie_wang 已提交
6 7 8

## Available APIs

A
Annie_wang 已提交
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
| 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
A
Annie_wang 已提交
27

S
shawn_he 已提交
28
1. Call **OH_ResourceManager_OpenRawDir** to obtain a **RawDir** instance based on the **NativeResourceManager** instance.
A
Annie_wang 已提交
29 30 31 32

    ```c++
    RawDir* rawDir = OH_ResourceManager_OpenRawDir(nativeResourceManager, path.c_str());
    ```
A
Annie_wang 已提交
33 34

2. Call **OH_ResourceManager_GetRawFileCount** to obtain the total number of files in the directory based on the **RawDir** instance.
A
Annie_wang 已提交
35 36 37 38

    ```c++
    int count = OH_ResourceManager_GetRawFileCount(rawDir);
    ```
A
Annie_wang 已提交
39 40

3. Call **OH_ResourceManager_GetRawFileName** to obtain the name of a file based on the specified index.
A
Annie_wang 已提交
41 42 43 44 45 46 47

    ```c++
    for (int index = 0; index < count; index++) {
        std::string fileName = OH_ResourceManager_GetRawFileName(rawDir, index);
    }
    ```

A
Annie_wang 已提交
48
4. Call **OH_ResourceManager_OpenRawFile** to open a **RawFile** instance with the specified file name.
A
Annie_wang 已提交
49 50 51 52

    ```c++
    RawFile* rawFile = OH_ResourceManager_OpenRawFile(nativeResourceManager, fileName.c_str());
    ```
A
Annie_wang 已提交
53 54

5. Call **OH_ResourceManager_GetRawFileSize** to obtain the size of the file.
A
Annie_wang 已提交
55 56 57 58 59

    ```c++
    long rawFileSize = OH_ResourceManager_GetRawFileSize(rawFile);
    ```

A
Annie_wang 已提交
60
6. Call **OH_ResourceManager_SeekRawFile** to seek for the read/write position in the file based on the specified offset.
A
Annie_wang 已提交
61 62 63 64 65 66 67

    ```c++
    int position = OH_ResourceManager_SeekRawFile(rawFile, 10, 0);
    int position = OH_ResourceManager_SeekRawFile(rawFile, 0 , 1);
    int position = OH_ResourceManager_SeekRawFile(rawFile, -10, 2);
    ```

A
Annie_wang 已提交
68
7. Call **OH_ResourceManager_GetRawFileOffset** to obtain the file offset.
A
Annie_wang 已提交
69 70 71 72 73

    ```c++
    long rawFileOffset = OH_ResourceManager_GetRawFileOffset(rawFile)
    ```

A
Annie_wang 已提交
74
8. Call **OH_ResourceManager_ReadRawFile** to read a file.
A
Annie_wang 已提交
75 76 77 78 79 80

    ```c++
    std::unique_ptr<char[]> mediaData = std::make_unique<char[]>(rawFileSize);
    long rawFileOffset = OH_ResourceManager_ReadRawFile(rawFile, mediaData.get(), rawFileSize);
    ```

S
shawn_he 已提交
81
9. Call **OH_ResourceManager_CloseRawFile** to close the file to release resources.
A
Annie_wang 已提交
82 83 84 85 86

    ```c++
    OH_ResourceManager_CloseRawFile(rawFile);
    ```

A
Annie_wang 已提交
87
10. Call **OH_ResourceManager_CloseRawDir** to close the file directory.
A
Annie_wang 已提交
88 89 90 91 92

    ```c++
    OH_ResourceManager_CloseRawDir(rawDir);
    ```

A
Annie_wang 已提交
93
11. Call **OH_ResourceManager_GetRawFileDescriptor** to obtain the FD of the file.
A
Annie_wang 已提交
94 95 96 97 98 99

    ```c++
    RawFileDescriptor descriptor;
    bool result = OH_ResourceManager_GetRawFileDescriptor(rawFile, descriptor);
    ```

A
Annie_wang 已提交
100
12. Call **OH_ResourceManager_ReleaseRawFileDescriptor** to release the FD of the file.
A
Annie_wang 已提交
101 102 103 104 105

    ```c++
    OH_ResourceManager_ReleaseRawFileDescriptor(descriptor);
    ```

A
Annie_wang 已提交
106
13. Call **OH_ResourceManager_ReleaseNativeResourceManager** to release the Native resource manager.
A
Annie_wang 已提交
107 108 109 110

    ```c++
    OH_ResourceManager_ReleaseNativeResourceManager(nativeResourceManager);
    ```
A
Annie_wang 已提交
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397

## 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%')
          }
      }
      ```