Improve:Ipc config.

This commit is contained in:
Fancy code 2024-05-24 09:01:38 +08:00
parent 8511e2947a
commit 21e6d4fd09
3 changed files with 82 additions and 10 deletions

View File

@ -13,4 +13,54 @@
### 1.2.3. 其它定制版本
   基于公版,继承派生出来的特殊功能的商业化版本。
   基于公版,继承派生出来的特殊功能的商业化版本。
```
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int is_mounted(const char *dirpath) {
FILE *fp;
char line[1024];
char mount_point[1024];
// 尝试打开 /proc/mounts 文件
fp = fopen("/proc/mounts", "r");
if (fp == NULL) {
perror("Error opening /proc/mounts");
return -1;
}
// 逐行读取并查找挂载点
while (fgets(line, sizeof(line), fp) != NULL) {
if (sscanf(line, "%*s %1023s %*s %*s %*d %*d\n", mount_point) == 1) {
// 检查当前行中的挂载点是否与所查找的目录匹配
if (strcmp(dirpath, mount_point) == 0) {
fclose(fp);
return 1; // 已挂载
}
}
}
fclose(fp);
return 0; // 未挂载
}
int main(int argc, char *argv[]) {
if (argc != 2) {
fprintf(stderr, "Usage: %s <directory>\n", argv[0]);
return 1;
}
const char *dirpath = argv[1];
if (is_mounted(dirpath)) {
printf("Directory '%s' is mounted.\n", dirpath);
} else {
printf("Directory '%s' is not mounted.\n", dirpath);
}
return 0;
}
```

View File

@ -466,12 +466,9 @@ void IpcConfigImpl::ReadingConfigThread(void)
*lastSlash = '\0';
}
mThreadRuning = true;
DIR *dir = nullptr;
LogInfo("Reading config thread is running, dirPath = %s\n", dirPath);
while (mThreadRuning) {
dir = opendir(dirPath);
if (nullptr != dir) {
closedir(dir);
if (CheckConfigPathMounted() == true) {
memset(&mAllData, 0, sizeof(Config_s));
mCfg = OpenConfigFile(IPC_CONFIG_FILE_PATH);
if (nullptr == mCfg) {
@ -875,9 +872,33 @@ void IpcConfigImpl::ReadAllConfigParameters(void)
// ConfigSetFloat(mCfg, "test_float", mAllData.testFloat);
// }
// if (CONFIG_HAS_CHANGED == mCfgChanged) {
// LogInfo("Save the config file.\n");
// mCfgChanged = CONFIG_HAS_NOT_CHANGED;
// ConfigSaveFile(mCfg);
// }
if (CONFIG_HAS_CHANGED == mCfgChanged) {
LogInfo("Save the config file.\n");
mCfgChanged = CONFIG_HAS_NOT_CHANGED;
ConfigSaveFile(mCfg);
}
}
bool IpcConfigImpl::CheckConfigPathMounted(void)
{
FILE *fp;
char line[1024];
char mount_point[1024] = {0};
fp = fopen("/proc/mounts", "r");
if (fp == NULL) {
perror("Error opening /proc/mounts");
return false;
}
while (fgets(line, sizeof(line), fp) != NULL) {
if (sscanf(line, "%*s %1023s %*s %*s %*d %*d\n", mount_point) == 1) {
// LogInfo("mount_point: %s\n", mount_point);
if (strcmp("/userdata", mount_point) == 0) {
fclose(fp);
return true; // mounted
}
}
}
fclose(fp);
return false; // unmounted
}

View File

@ -112,6 +112,7 @@ private:
void ReadingConfigThread(void);
void InitConfigMap(void);
void ReadAllConfigParameters(void);
virtual bool CheckConfigPathMounted(void);
private:
std::mutex mMutex;