1# Porting the File Subsystem 2 3 4The utils component can be used by service subsystems and upper-layer applications and depends on the chip file system. The chip platform needs to provide functions such as opening, closing, reading, writing, and obtaining the file size. 5 6 7## Procedure 8 9The OpenHarmony file system needs to adapt to the following HAL APIs: 10 11 **Table 1** Opening or closing a file 12 13| API| Description| 14| -------- | -------- | 15| HalFileOpen | Opens or creates a file.| 16| HalFileClose | Closes a file:| 17 18 **Table 2** File operations 19 20| API| Description| 21| -------- | -------- | 22| HalFileRead | Reads a file.| 23| HalFileWrite | Writes data to a file.| 24| HalFileDelete | Deletes a file.| 25| HalFileStat | Obtains file attributes.| 26| HalFileSeek | Searches for files.| 27 28 For details about the implementation of vendor adaptation interfaces, see the definitions of the file interface and HAL adaptation interface in OpenHarmony. 29 30``` 31//utils/native/lite/file 32├── BUILD.gn 33└── src 34 └── file_impl_hal 35 └── file.c # file interface 36``` 37 38 39``` 40//utils/native/lite/hals 41└── file 42└── hal_file.h # HAL interface header file 43``` 44 45The content of **BUILD.gn** is as follows: 46 47 48``` 49import("//build/lite/config/component/lite_component.gni") 50 51static_library("native_file") { 52 sources = [ 53 "src/file_impl_hal/file.c", 54 ] 55 include_dirs = [ 56 "//utils/native/lite/include", 57 "//utils/native/lite/hals/file", 58 ] 59 deps = ["$ohos_vendor_adapter_dir/hals/utils/file:hal_file_static"] # Vendor dependent adaptation. 60} 61 62lite_component("file") { 63 features = [ 64 ":native_file", 65 ] 66} 67``` 68 69As shown in the preceding example, the directory for storing vendor adaptation interfaces is **$ohos_vendor_adapter_dir/hals/utils/file**, where the target in the **BUILD.gn** file is **hal_file_static**. 70 71Generally, vendors can use the following methods to adapt to HAL APIs: 72 731. Directly read and write the flash memory to simulate file operations. 74 752. Use the LittleFS or FatFs file system for adaptation. For the FatFs file system, you can refer to the **//thirdparty** directory of OpenHarmony. 76 773. Use the existing file system of the vendor for adaptation. 78 79 80## Example 81 821. Add a file system to **config.json**. 83 84 Path: **vendor/MyVendorCompany/MyProduct/config.json** 85 86 The sample code is as follows: 87 88 ``` 89 { 90 "subsystem": "utils", 91 "components": [ 92 { "component": "file", "features":[] } 93 ] 94 }, 95 ``` 96 972. Add an adaptation file. 98 In the **vendor/MyVendorCompany/MyProduct/config.json** file, set **vendor_adapter_dir** as follows: 99 100 "vendor_adapter_dir": "//device/MyDeviceCompany/MyBoard/adapter". 101 102 Perform **UtilsFile** interface adaptation in this directory. 103 104 105 ``` 106 hals/utils/file 107 ├── BUILD.gn 108 └── src 109 └── hal_file.c 110 ``` 111 112 The content of **BUILD.gn** is as follows: 113 114 ``` 115 import("//build/lite/config/component/lite_component.gni") 116 static_library("hal_file_static") { # target name 117 sources = [ "src/hal_file.c" ] # Source file adapted by the vendor 118 include_dirs = [ 119 "//utils/native/lite/hals/file", 120 ] 121 } 122 ``` 123