移植案例與原理 - Utils子系統之file文件操作部件
原創??https://harmonyos.51cto.com??
Utils子系統是OpenHarmony的公共基礎庫,存放OpenHarmony通用的基礎組件。這些基礎組件可被OpenHarmony各業務子系統及上層應用所使用。公共基礎庫在不同平臺上提供的能力:
- LiteOS-M內核:KV(key value)存儲、文件操作、定時器、Dump系統屬性。
- LiteOS-A內核:KV(key value)存儲、定時器、JS API(設備查詢,數據存儲)、Dump系統屬性。
本文介紹下移植開發板時如何適配utils子系統之file文件操作部件,并介紹下相關的運行機制原理。系統屬性部件syspara_lite包含系統參數特性syspara_lite和token。file文件操作部件定義在utils\native\lite\。源代碼目錄如下:
utils/native/lite/ # 公共基礎庫根目錄
├── file # 文件接口實現
├── hals # HAL目錄
│ └── file # 文件操作硬件抽象層頭文件
├── include # 公共基礎庫對外接口文件
├── js # JS API目錄
│ └── builtin
│ ├── common
│ ├── deviceinfokit # 設備信息Kit
│ ├── filekit # 文件Kit
│ └── kvstorekit # KV存儲Kit
├── kal # KAL目錄
│ └── timer # Timer的KAL實現
├── kv_store # KV存儲實現
│ ├── innerkits # KV存儲內部接口
│ └── src # KV存儲源文件
├── memory
│ └── include # 內存池管理接口
├── os_dump # Dump系統屬性
└── timer_task # Timer實現
1、file文件操作部件適配示例
1.1 配置產品解決方案config.json
utils子系統之file文件操作部件的適配示例可以參考vendor\ohemu\qemu_csky_mini_system_demo\config.json,代碼片段如下。⑴處用于配置子系統的file部件。⑵處指定在開發板目錄中適配目錄,這個適配目錄下需要創建目錄device\qemu\SmartL_E802\adapter\hals\utils\file\。為什么配置這個目錄,后文會解析。
{
"subsystem": "utils",
"components": [
⑴ { "component": "file", "features":[] },
{ "component": "kv_store", "features":[] }
]
}
],
⑵ "vendor_adapter_dir": "http://device/qemu/SmartL_E802/adapter",
1.2 適配hal_file.h文件中的接口
在文件utils\native\lite\hals\file\hal_file.h頭文件中,定義了文件操作接口,適配開發板時,如果需要使用utils子系統之file文件操作部件,就要適配這些接口。需要適配的接口如下。HalFileOpen()函數返回值是文件描述符fd,可以被其他帶int fd參數的函數使用。
int HalFileOpen(const char* path, int oflag, int mode);
int HalFileClose(int fd);
int HalFileRead(int fd, char* buf, unsigned int len);
int HalFileWrite(int fd, const char* buf, unsigned int len);
int HalFileDelete(const char* path);
int HalFileStat(const char* path, unsigned int* fileSize);
int HalFileSeek(int fd, int offset, unsigned int whence);
文件device\qemu\SmartL_E802\adapter\hals\utils\file\src\hal_file.c可以作為參考示例,演示如何適配上述接口。⑴處的HalFileOpen()函數中,先組裝文件路徑,"/littlefs"是SmartL_E802開發板設置的LFS文件默認掛載點,關于LFS的適配請參考device\qemu\SmartL_E802\liteos_m\board\fs\fs_init.c。HalFileXXX函數調用的open、close、read、write、unlink、stat、lseek等函數定義在kernel\liteos_m\kal\libc\musl\fs.c或kernel\liteos_m\kal\libc\newlib\porting\src\fs.c,這個取決于使用的是musl C庫還是newlib C庫。文件系統接口調用鏈如下所示:
UtilsFileXXX(utils\native\lite\file\src\file_impl_hal\file.c)-> HalFileXXXX(device\qemu\SmartL_E802\adapter\hals\utils\file\src\hal_file.c) -> open/read/write/…(kernel\liteos_m\kal\libc\musl\fs.c或kernel\liteos_m\kal\libc\newlib\porting\src\fs.c) -> LOS_XXXX(kernel\liteos_m\components\fs\vfs\los_fs.c) -> Lfs_XXXX(kernel\liteos_m\components\fs\littlefs\lfs_api.c) -> lfs_file_XXX (third_party\littlefs\lfs.c) -> Littlefs*(device\qemu\SmartL_E802\liteos_m\board\fs\littlefs_hal.c)。
int HalFileOpen(const char* path, int oflag, int mode)
{
char tmpPath[LITTLEFS_MAX_LFN_LEN] = {0};
⑴ (void)snprintf_s(tmpPath, LITTLEFS_MAX_LFN_LEN, LITTLEFS_MAX_LFN_LEN, "/littlefs/%s", path);
return open(tmpPath, oflag, mode);
}
int HalFileClose(int fd)
{
return close(fd);
}
int HalFileRead(int fd, char *buf, unsigned int len)
{
return read(fd, buf, len);
}
int HalFileWrite(int fd, const char *buf, unsigned int len)
{
return write(fd, buf, len);
}
int HalFileDelete(const char *path)
{
char tmpPath[LITTLEFS_MAX_LFN_LEN] = {0};
(void)snprintf_s(tmpPath, LITTLEFS_MAX_LFN_LEN, LITTLEFS_MAX_LFN_LEN, "/littlefs/%s", path);
return unlink(path);
}
int HalFileStat(const char *path, unsigned int *fileSize)
{
char tmpPath[LITTLEFS_MAX_LFN_LEN] = {0};
struct stat halStat ;
int ret = 0;
(void)snprintf_s(tmpPath, LITTLEFS_MAX_LFN_LEN, LITTLEFS_MAX_LFN_LEN, "/littlefs/%s", path);
ret = stat(tmpPath, &halStat);
*fileSize = halStat.st_size;
return ret;
}
int HalFileSeek(int fd, int offset, unsigned int whence)
{
return lseek(fd, (off_t)offset, whence);
}
2、file文件操作部件代碼分析
2.1 部件的頭文件
上文已經知道,file文件操作部件代碼的頭文件為utils\native\lite\include\utils_file.h,用戶程序可以使用該頭文件中定義的接口。
int UtilsFileOpen(const char* path, int oflag, int mode);
int UtilsFileClose(int fd);
int UtilsFileRead(int fd, char* buf, unsigned int len);
int UtilsFileWrite(int fd, const char* buf, unsigned int len);
int UtilsFileDelete(const char* path);
int UtilsFileStat(const char* path, unsigned int* fileSize);
int UtilsFileSeek(int fd, int offset, unsigned int whence);
int UtilsFileCopy(const char* src, const char* dest);
int UtilsFileMove(const char* src, const char* dest);
復制頭文件utils\native\lite\hals\file\hal_file.h中定義的接口,需要移植適配時提供實現,具體接口見上文。
2.2 部件的源代碼文件
文件utils\native\lite\file\src\file_impl_hal\file.c中實現了UtilsFileXXX接口,代碼比較簡單,調用需要開發板移植適配的HalFileXXX接口。
int UtilsFileOpen(const char* path, int oflag, int mode)
{
return HalFileOpen(path, oflag, mode);
}
int UtilsFileClose(int fd)
{
return HalFileClose(fd);
}
int UtilsFileRead(int fd, char* buf, unsigned int len)
{
return HalFileRead(fd, buf, len);
}
int UtilsFileWrite(int fd, const char* buf, unsigned int len)
{
return HalFileWrite(fd, buf, len);
}
int UtilsFileDelete(const char* path)
{
return HalFileDelete(path);
}
int UtilsFileStat(const char* path, unsigned int* fileSize)
{
return HalFileStat(path, fileSize);
}
int UtilsFileSeek(int fd, int offset, unsigned int whence)
{
return HalFileSeek(fd, offset, whence);
}
int UtilsFileCopy(const char* src, const char* dest)
{
if ((src == NULL) || (dest == NULL)) {
return EC_FAILURE;
}
int fpSrc = UtilsFileOpen(src, O_RDONLY_FS, 0);
if (fpSrc < 0) {
return fpSrc;
}
int fpDest = UtilsFileOpen(dest, O_RDWR_FS | O_CREAT_FS | O_TRUNC_FS, 0);
if (fpDest < 0) {
UtilsFileClose(fpSrc);
return fpDest;
}
bool copyFailed = true;
char* dataBuf = (char *)malloc(BUFFER_SIZE);
if (dataBuf == NULL) {
goto MALLOC_ERROR;
}
int nLen = UtilsFileRead(fpSrc, dataBuf, BUFFER_SIZE);
while (nLen > 0) {
if (UtilsFileWrite(fpDest, dataBuf, nLen) != nLen) {
goto EXIT;
}
nLen = UtilsFileRead(fpSrc, dataBuf, BUFFER_SIZE);
}
copyFailed = (nLen < 0);
EXIT:
free(dataBuf);
MALLOC_ERROR:
UtilsFileClose(fpSrc);
UtilsFileClose(fpDest);
if (copyFailed) {
UtilsFileDelete(dest);
return EC_FAILURE;
}
return EC_SUCCESS;
}
int UtilsFileMove(const char* src, const char* dest)
{
int ret = UtilsFileCopy(src, dest);
if (ret == EC_SUCCESS) {
ret = UtilsFileDelete(src);
}
return ret;
}
2.3 部件的編譯構建
編譯構建配置文件utils\native\lite\file\BUILD.gn代碼如下,⑴處的配置項ohos_board_adapter_dir為產品解決方案配置文件config.json中定義的開發板適配目錄。可以看出來:
- 開發板適配目錄必須包含目錄hals/utils/file
- 目錄hals/utils/file同級的BUILD.gn文件中,構建目標必須為hal_file_static。不能隨意命名。
import("http://build/lite/config/component/lite_component.gni")
static_library("native_file") {
sources = [ "src/file_impl_hal/file.c" ]
include_dirs = [
"http://utils/native/lite/include",
"http://utils/native/lite/hals/file",
]
⑴ deps = [ "$ohos_board_adapter_dir/hals/utils/file:hal_file_static" ]
}
lite_component("file") {
features = [ ":native_file" ]
}
小結
本文介紹了utils子系統之file文件操作部件的移植適配案例,分析了部件源代碼。
??https://harmonyos.51cto.com??