1# Rawfile 2 3 4Provides 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. 5 6 7**Since** 8 9 108 11 12 13## Summary 14 15 16### Files 17 18| Name | Description | 19| ---------------------------------------- | ------------------ | 20| [raw_dir.h](raw__dir_8h.md) | Provides functions related to the **rawfile** directory.<br>**File to include**: \<rawfile/raw_dir.h><br>**Library**: librawfile.z.so| 21| [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><br>**Library**: librawfile.z.so| 22| [raw_file_manager.h](raw__file__manager_8h.md) | Provides file management functions for the **rawfile** directory.<br>**File to include**: \<rawfile/raw_file_manager.h><br>**Library**: librawfile.z.so| 23 24 25### Structs 26 27| Name | Description | 28| ---------------------------------------- | ----------------- | 29| [RawFileDescriptor](_raw_file_descriptor.md) | Defines the file descriptor (FD) information of a file in the **rawfile** directory. | 30 31 32### Types 33 34| Name | Description | 35| ---------------------------------------- | ------------------- | 36| [RawDir](#rawdir) | Provides access to the **rawfile** directory. | 37| [RawFile](#rawfile) | Provides access to the files in **rawfile**. | 38| [NativeResourceManager](#nativeresourcemanager) | Represents the resource manager.| 39 40 41### Functions 42 43| Name | Description | 44| ---------------------------------------- | ---------------------------------------- | 45| [OH_ResourceManager_GetRawFileName](#oh_resourcemanager_getrawfilename) ([RawDir](#rawdir) \*rawDir, int index) | Obtains the name of a file in **rawfile** based on the index. | 46| [OH_ResourceManager_GetRawFileCount](#oh_resourcemanager_getrawfilecount) ([RawDir](#rawdir) \*rawDir) | Obtains the number of files in a [RawDir](#rawdir). | 47| [OH_ResourceManager_CloseRawDir](#oh_resourcemanager_closerawdir) ([RawDir](#rawdir) \*rawDir) | Closes a [RawDir](#rawdir) and releases all associated resources. | 48| [OH_ResourceManager_ReadRawFile](#oh_resourcemanager_readrawfile) (const [RawFile](#rawfile) \*rawFile, void \*buf, size_t length) | Reads data from a file in **rawfile**. | 49| [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. | 50| [OH_ResourceManager_GetRawFileSize](#oh_resourcemanager_getrawfilesize) ([RawFile](#rawfile) \*rawFile) | Obtains the size of a file in **rawfile**. | 51| [OH_ResourceManager_GetRawFileRemainingLength](#oh_resourcemanager_getrawfileremaininglength) ([RawFile](#rawfile) \*rawFile) | Obtains the remaining size of a raw file. | 52| [OH_ResourceManager_CloseRawFile](#oh_resourcemanager_closerawfile) ([RawFile](#rawfile) \*rawFile) | Closes a [RawFile](#rawfile) and releases all associated resources. | 53| [OH_ResourceManager_GetRawFileOffset](#oh_resourcemanager_getrawfileoffset) (const [RawFile](#rawfile) \*rawFile) | Obtains the current offset of a file in **rawfile**. | 54| [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. | 55| [OH_ResourceManager_ReleaseRawFileDescriptor](#oh_resourcemanager_releaserawfiledescriptor) (const [RawFileDescriptor](_raw_file_descriptor.md) &descriptor) | Releases an FD. | 56| [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**. | 57| [OH_ResourceManager_ReleaseNativeResourceManager](#oh_resourcemanager_releasenativeresourcemanager) ([NativeResourceManager](#nativeresourcemanager) \*resMgr) | Releases a Native resource manager instance. | 58| [OH_ResourceManager_OpenRawDir](#oh_resourcemanager_openrawdir) (const [NativeResourceManager](#nativeresourcemanager) \*mgr, const char \*dirName) | Opens a directory in the **rawfile** directory. | 59| [OH_ResourceManager_OpenRawFile](#oh_resourcemanager_openrawfile) (const [NativeResourceManager](#nativeresourcemanager) \*mgr, const char \*fileName) | Opens a file in the **rawfile** directory. | 60 61 62## Description 63 64 65### Type Description 66 67 68#### NativeResourceManager 69 70 71``` 72typedef struct NativeResourceManager NativeResourceManager 73``` 74 75**Description** 76 77Represents the resource manager. 78 79This class encapsulates the native implementation of the JavaScript resource manager. The **ResourceManager** pointer can be obtained by using [OH_ResourceManager_InitNativeResourceManager](#oh_resourcemanager_initnativeresourcemanager). 80 81**Since** 82 838 84 85 86#### RawDir 87 88 89``` 90typedef struct RawDir RawDir 91``` 92 93**Description** 94 95Provides access to the **rawfile** directory. 96 97**Since** 98 998 100 101 102#### RawFile 103 104 105``` 106typedef struct RawFile RawFile 107``` 108 109**Description** 110 111Provides access to the files in **rawfile**. 112 113**Since** 114 1158 116 117 118### Function Description 119 120 121#### OH_ResourceManager_CloseRawDir() 122 123 124``` 125void OH_ResourceManager_CloseRawDir (RawDir * rawDir) 126``` 127 128**Description** 129 130Closes a [RawDir](#rawdir) opened and releases all associated resources. 131 132**Parameters** 133 134| Name | Description | 135| ------ | ------------------------- | 136| rawDir | Pointer to the [RawDir](#rawdir) to close.| 137 138**See** 139 140[OH_ResourceManager_OpenRawDir](#oh_resourcemanager_openrawdir) 141 142**Since** 143 1448 145 146 147#### OH_ResourceManager_CloseRawFile() 148 149 150``` 151void OH_ResourceManager_CloseRawFile (RawFile * rawFile) 152``` 153 154**Description** 155 156Closes a [RawFile](#rawfile) and releases all associated resources. 157 158**Parameters** 159 160| Name | Description | 161| ------- | --------------------------- | 162| rawFile | Pointer to the [RawFile](#rawfile) to close.| 163 164**See** 165 166[OH_ResourceManager_OpenRawFile](#oh_resourcemanager_openrawfile) 167 168**Since** 169 1708 171 172 173#### OH_ResourceManager_GetRawFileCount() 174 175 176``` 177int OH_ResourceManager_GetRawFileCount (RawDir * rawDir) 178``` 179 180**Description** 181 182Obtains the number of files in a [RawDir](#rawdir). 183 184You can use this function to obtain available indexes in [OH_ResourceManager_GetRawFileName](#oh_resourcemanager_getrawfilename). 185 186**Parameters** 187 188| Name | Description | 189| ------ | ------------------------- | 190| rawDir | Pointer to the target [RawDir](#rawdir).| 191 192**See** 193 194[OH_ResourceManager_GetRawFileName](#oh_resourcemanager_getrawfilename) 195 196**Since** 197 1988 199 200 201#### OH_ResourceManager_GetRawFileDescriptor() 202 203 204``` 205bool OH_ResourceManager_GetRawFileDescriptor (const RawFile * rawFile, RawFileDescriptor & descriptor ) 206``` 207 208**Description** 209 210Opens a file in the **rawfile** directory based on the offset and file length and obtains the FD. 211 212The FD obtained can be used to read the file. 213 214**Parameters** 215 216| Name | Description | 217| ---------- | ---------------------------------------------------- | 218| rawFile | Pointer to the [RawFile](#rawfile). | 219| descriptor | File descriptor of the file, start position of the file in the hAP, and length of the file.| 220 221**Returns** 222 223Returns <b>true</b> if the file is opened; returns <b>false</b> if the access to the file is rejected. 224 225**Since** 226 2278 228 229 230#### OH_ResourceManager_GetRawFileName() 231 232 233``` 234const char* OH_ResourceManager_GetRawFileName (RawDir * rawDir, int index ) 235``` 236 237**Description** 238 239Obtains the name of a file in **rawfile** based on the index. 240 241You can use this function to traverse the **rawfile** directory. 242 243**Parameters** 244 245| Name | Description | 246| ------ | ----------------------------- | 247| rawDir | Pointer to the [RawDir](#rawdir). | 248| index | Index of the file in the [RawDir](#rawdir).| 249 250**Returns** 251 252Returns 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). 253 254**See** 255 256[OH_ResourceManager_OpenRawFile](#oh_resourcemanager_openrawfile) 257 258**Since** 259 2608 261 262 263#### OH_ResourceManager_GetRawFileOffset() 264 265 266``` 267long OH_ResourceManager_GetRawFileOffset (const RawFile * rawFile) 268``` 269 270**Description** 271 272Obtains the current offset of a file in **rawfile**. 273 274The offset indicates the position of the file in the HAP. 275 276**Parameters** 277 278| Name | Description | 279| ------- | --------------------------- | 280| rawFile | Pointer to the target [RawFile](#rawfile).| 281 282**Returns** 283 284Returns the file offset obtained. 285 286**Since** 287 2888 289 290 291#### OH_ResourceManager_GetRawFileSize() 292 293 294``` 295long OH_ResourceManager_GetRawFileSize (RawFile * rawFile) 296``` 297 298**Description** 299 300Obtains the size of a file in **rawfile**. 301 302**Parameters** 303 304| Name | Description | 305| ------- | --------------------------- | 306| rawFile | Pointer to the target [RawFile](#rawfile).| 307 308**Returns** 309 310Returns the file size obtained. 311 312**Since** 313 31411 315 316 317#### OH_ResourceManager_GetRawFileRemainingLength() 318 319 320``` 321int64_t OH_ResourceManager_GetRawFileRemainingLength (RawFile * rawFile) 322``` 323 324**Description** 325 326Obtains the remaining size of a raw file. 327 328**Parameters** 329 330| Name | Description | 331| ------- | --------------------------- | 332| rawFile | Pointer to the target [RawFile](#rawfile).| 333 334**Returns** 335 336Returns the remaining size of the raw file. 337 338**Since** 339 3408 341 342 343#### OH_ResourceManager_InitNativeResourceManager() 344 345 346``` 347NativeResourceManager* OH_ResourceManager_InitNativeResourceManager (napi_env env, napi_value jsResMgr ) 348``` 349 350**Description** 351 352Initializes a Native resource manager using the JavaScript resource manager. 353 354You can use the resource manager obtained to implement **rawfile** operations. 355 356**Parameters** 357 358| Name | Description | 359| -------- | ---------------------------------------- | 360| env | Pointer to the JavaScript Native API (napi) environment.| 361| jsResMgr | JavaScript resource manager. | 362 363**Returns** 364 365Returns the pointer to the [NativeResourceManager](#nativeresourcemanager) obtained. 366 367**Since** 368 3698 370 371 372#### OH_ResourceManager_OpenRawDir() 373 374 375``` 376RawDir* OH_ResourceManager_OpenRawDir (const NativeResourceManager * mgr, const char * dirName ) 377``` 378 379**Description** 380 381Opens a directory in **rawfile**. 382 383After opening the directory, you can traverse all files in it. 384 385**Parameters** 386 387| Name | Description | 388| ------- | ---------------------------------------- | 389| mgr | Pointer to the [NativeResourceManager](#nativeresourcemanager), which is obtained by using [OH_ResourceManager_InitNativeResourceManager](#oh_resourcemanager_initnativeresourcemanager).| 390| dirName | Pointer to the name of the directory to open. If this field is left empty, the root directory will be opened.| 391 392**Returns** 393 394Returns the pointer to the [RawDir](#rawdir) opened. You can use [OH_ResourceManager_CloseRawDir](#oh_resourcemanager_closerawdir) to close the directory and release resources. 395 396**See** 397 398[OH_ResourceManager_InitNativeResourceManager](#oh_resourcemanager_initnativeresourcemanager) 399 400[OH_ResourceManager_CloseRawDir](#oh_resourcemanager_closerawdir) 401 402**Since** 403 4048 405 406 407#### OH_ResourceManager_OpenRawFile() 408 409 410``` 411RawFile* OH_ResourceManager_OpenRawFile (const NativeResourceManager * mgr, const char * fileName ) 412``` 413 414**Description** 415 416Opens a file in **rawfile**. 417 418After the file is opened, you can read data in it. 419 420**Parameters** 421 422| Name | Description | 423| -------- | ---------------------------------------- | 424| mgr | Pointer to the [NativeResourceManager](#nativeresourcemanager), which is obtained by using [OH_ResourceManager_InitNativeResourceManager](#oh_resourcemanager_initnativeresourcemanager).| 425| fileName | Pointer to the name of the file in the relative path of the **rawfile** root directory. | 426 427**Returns** 428 429Returns the pointer to the [RawFile](#rawfile) opened. You can use [OH_ResourceManager_CloseRawFile](#oh_resourcemanager_closerawfile) to close the file and release resources. 430 431**See** 432 433[OH_ResourceManager_InitNativeResourceManager](#oh_resourcemanager_initnativeresourcemanager) 434 435[OH_ResourceManager_CloseRawFile](#oh_resourcemanager_closerawfile) 436 437**Since** 438 4398 440 441 442#### OH_ResourceManager_ReadRawFile() 443 444 445``` 446int OH_ResourceManager_ReadRawFile (const RawFile * rawFile, void * buf, size_t length ) 447``` 448 449**Description** 450 451Reads data of the specified raw file. 452 453**Parameters** 454 455| Name | Description | 456| ------- | --------------------------- | 457| rawFile | Pointer to the [RawFile](#rawfile) to read.| 458| buf | Pointer to the buffer for receiving the read data. | 459| length | Length of the data to read. | 460 461**Returns** 462 463Returns the length of the data read if the operation is successful; returns **0** otherwise. 464 465**Since** 466 4678 468 469 470#### OH_ResourceManager_ReleaseNativeResourceManager() 471 472 473``` 474void OH_ResourceManager_ReleaseNativeResourceManager (NativeResourceManager * resMgr) 475``` 476 477**Description** 478 479Releases a Native resource manager instance. 480 481**Parameters** 482 483| Name | Description | 484| ------ | ---------------------------------------- | 485| resMgr | Pointer to the [NativeResourceManager](#nativeresourcemanager) instance to release.| 486 487**Since** 488 4898 490 491 492#### OH_ResourceManager_ReleaseRawFileDescriptor() 493 494 495``` 496bool OH_ResourceManager_ReleaseRawFileDescriptor (const RawFileDescriptor & descriptor) 497``` 498 499**Description** 500 501Releases the FD of a file in **rawfile**. 502 503To prevent FD leakage, you are advised to release an FD immediately after use. 504 505**Parameters** 506 507| Name | Description | 508| ---------- | ---------------------------------------------------- | 509| descriptor | File descriptor to close. It contains the FD, start position in the HAP, and file length. | 510 511**Returns** 512 513Returns <b>true</b> if the FD is released; returns <b>false</b> otherwise. 514 515**Since** 516 5178 518 519 520#### OH_ResourceManager_SeekRawFile() 521 522 523``` 524int OH_ResourceManager_SeekRawFile (const RawFile * rawFile, long offset, int whence ) 525``` 526 527**Description** 528 529Seeks for the data read/write position in a file in **rawfile** based on the specified offset. 530 531**Parameters** 532 533| Name | Description | 534| ------- | ---------------------------------------- | 535| rawFile | Pointer to the target [RawFile](#rawfile). | 536| offset | Offset. | 537| 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.| 538 539**Returns** 540 541Returns **0** if the operation is successful, and the **rawFile** pointer is directed to the new read/write position. Returns **(int) -1** if an error occurs. 542 543**Since** 544 5458 546