移植案例與原理 - Utils子系統(tǒng)之file文件操作部件
原創(chuàng)??51CTO和華為官方合作共建的鴻蒙技術(shù)社區(qū)??
??https://harmonyos.51cto.com??
Utils子系統(tǒng)是OpenHarmony的公共基礎(chǔ)庫,存放OpenHarmony通用的基礎(chǔ)組件。這些基礎(chǔ)組件可被OpenHarmony各業(yè)務(wù)子系統(tǒng)及上層應(yīng)用所使用。公共基礎(chǔ)庫在不同平臺上提供的能力:
- LiteOS-M內(nèi)核:KV(key value)存儲(chǔ)、文件操作、定時(shí)器、Dump系統(tǒng)屬性。
- LiteOS-A內(nèi)核:KV(key value)存儲(chǔ)、定時(shí)器、JS API(設(shè)備查詢,數(shù)據(jù)存儲(chǔ))、Dump系統(tǒng)屬性。
本文介紹下移植開發(fā)板時(shí)如何適配utils子系統(tǒng)之file文件操作部件,并介紹下相關(guān)的運(yùn)行機(jī)制原理。系統(tǒng)屬性部件syspara_lite包含系統(tǒng)參數(shù)特性syspara_lite和token。file文件操作部件定義在utils\native\lite\。源代碼目錄如下:
utils/native/lite/ # 公共基礎(chǔ)庫根目錄
├── file # 文件接口實(shí)現(xiàn)
├── hals # HAL目錄
│ └── file # 文件操作硬件抽象層頭文件
├── include # 公共基礎(chǔ)庫對外接口文件
├── js # JS API目錄
│ └── builtin
│ ├── common
│ ├── deviceinfokit # 設(shè)備信息Kit
│ ├── filekit # 文件Kit
│ └── kvstorekit # KV存儲(chǔ)Kit
├── kal # KAL目錄
│ └── timer # Timer的KAL實(shí)現(xiàn)
├── kv_store # KV存儲(chǔ)實(shí)現(xiàn)
│ ├── innerkits # KV存儲(chǔ)內(nèi)部接口
│ └── src # KV存儲(chǔ)源文件
├── memory
│ └── include # 內(nèi)存池管理接口
├── os_dump # Dump系統(tǒng)屬性
└── timer_task # Timer實(shí)現(xiàn)
1、file文件操作部件適配示例
1.1 配置產(chǎn)品解決方案config.json
utils子系統(tǒng)之file文件操作部件的適配示例可以參考vendor\ohemu\qemu_csky_mini_system_demo\config.json,代碼片段如下。⑴處用于配置子系統(tǒng)的file部件。⑵處指定在開發(fā)板目錄中適配目錄,這個(gè)適配目錄下需要?jiǎng)?chuàng)建目錄device\qemu\SmartL_E802\adapter\hals\utils\file\。為什么配置這個(gè)目錄,后文會(huì)解析。
{
"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頭文件中,定義了文件操作接口,適配開發(fā)板時(shí),如果需要使用utils子系統(tǒng)之file文件操作部件,就要適配這些接口。需要適配的接口如下。HalFileOpen()函數(shù)返回值是文件描述符fd,可以被其他帶int fd參數(shù)的函數(shù)使用。
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()函數(shù)中,先組裝文件路徑,"/littlefs"是SmartL_E802開發(fā)板設(shè)置的LFS文件默認(rèn)掛載點(diǎn),關(guān)于LFS的適配請參考device\qemu\SmartL_E802\liteos_m\board\fs\fs_init.c。HalFileXXX函數(shù)調(diào)用的open、close、read、write、unlink、stat、lseek等函數(shù)定義在kernel\liteos_m\kal\libc\musl\fs.c或kernel\liteos_m\kal\libc\newlib\porting\src\fs.c,這個(gè)取決于使用的是musl C庫還是newlib C庫。文件系統(tǒng)接口調(diào)用鏈如下所示:
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 部件的頭文件
上文已經(jīng)知道,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);
復(fù)制頭文件utils\native\lite\hals\file\hal_file.h中定義的接口,需要移植適配時(shí)提供實(shí)現(xiàn),具體接口見上文。
2.2 部件的源代碼文件
文件utils\native\lite\file\src\file_impl_hal\file.c中實(shí)現(xiàn)了UtilsFileXXX接口,代碼比較簡單,調(diào)用需要開發(fā)板移植適配的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 部件的編譯構(gòu)建
編譯構(gòu)建配置文件utils\native\lite\file\BUILD.gn代碼如下,⑴處的配置項(xiàng)ohos_board_adapter_dir為產(chǎn)品解決方案配置文件config.json中定義的開發(fā)板適配目錄??梢钥闯鰜恚?/p>
- 開發(fā)板適配目錄必須包含目錄hals/utils/file
- 目錄hals/utils/file同級的BUILD.gn文件中,構(gòu)建目標(biāo)必須為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" ]
}
小結(jié)
本文介紹了utils子系統(tǒng)之file文件操作部件的移植適配案例,分析了部件源代碼。
??51CTO和華為官方合作共建的鴻蒙技術(shù)社區(qū)??
??https://harmonyos.51cto.com??