diff --git a/debian/libkysdk-sysinfo-dev.install b/debian/libkysdk-sysinfo-dev.install index 20c050b..fd6575c 100644 --- a/debian/libkysdk-sysinfo-dev.install +++ b/debian/libkysdk-sysinfo-dev.install @@ -1,3 +1,4 @@ usr/include/kysdk/kysdk-system/libkysysinfo.hpp usr/include/kysdk/kysdk-system/libkysysinfo.h +usr/include/kysdk/kysdk-system/getSystemVersion.h development-files/kysdk-sysinfo.pc usr/share/pkgconfig/ \ No newline at end of file diff --git a/src/systeminfo/CMakeLists.txt b/src/systeminfo/CMakeLists.txt index d623f59..8dd7da6 100644 --- a/src/systeminfo/CMakeLists.txt +++ b/src/systeminfo/CMakeLists.txt @@ -1,13 +1,15 @@ aux_source_directory(. SOURCESCODE) include_directories(.) +include_directories(/usr/include/cjson/) find_library(GLIBC_LIB glib-2.0) find_library(DBUS_LIB dbus-1) find_library(DBUS_GLIB_LIB dbus-glib-1) +find_package(PkgConfig) add_library(kysysinfo SHARED ${SOURCESCODE}) set_target_properties(kysysinfo PROPERTIES VERSION 1.2.0 SOVERSION 1) add_executable(kysysinfo-test test/kysysinfo_test.c) -target_link_libraries(kysysinfo dl kylog systemd kyconf ${GLIBC_LIB} ${DBUS_LIB} ${DBUS_GLIB_LIB}) -target_link_libraries(kysysinfo-test kysysinfo) +target_link_libraries(kysysinfo dl kylog systemd kyconf libcjson.so ${GLIBC_LIB} ${DBUS_LIB} ${DBUS_GLIB_LIB}) +target_link_libraries(kysysinfo-test kysysinfo libcjson.so) # target_link_libraries(kysysinfo-test kysysinfo kylin-activation kylog systemd kyconf ${GLIBC_LIB} ${DBUS_LIB} ${DBUS_GLIB_LIB}) install(TARGETS kysysinfo @@ -17,4 +19,7 @@ install(FILES libkysysinfo.hpp DESTINATION include/kysdk/kysdk-system) install(FILES libkysysinfo.h + DESTINATION include/kysdk/kysdk-system) + +install(FILES getSystemVersion.h DESTINATION include/kysdk/kysdk-system) \ No newline at end of file diff --git a/src/systeminfo/README.md b/src/systeminfo/README.md new file mode 100644 index 0000000..375da2b --- /dev/null +++ b/src/systeminfo/README.md @@ -0,0 +1,15 @@ +需要的依赖: +cmake +libcjson1 +libcjson-dev + +源码编译执行: +cmake . && make && echo "" && echo "" && ./getSystemVersion + + +获取系统版本号接口: version_t getSystemVersion(); + +typedef struct { + char *os_version; //系统版本 + char *update_version; //补丁版本 +} version_t; diff --git a/src/systeminfo/getSystemVersion.h b/src/systeminfo/getSystemVersion.h new file mode 100644 index 0000000..55a673b --- /dev/null +++ b/src/systeminfo/getSystemVersion.h @@ -0,0 +1,52 @@ +#include +#include +#include +#include + + +/* getSystemVersion.h + */ +#ifndef GETSYSTEMVERSION_H +#define GETSYSTEMVERSION_H + +#define DEBUG + +#ifdef DEBUG +#define DBGprint(...) printf(__VA_ARGS__) +#else +#define DBGprint(...) +#endif + +#define SYSTEM_VERSION_PATH "/etc/kylin-version/kylin-system-version.conf" +#define UPDATE_VERSION_PATH "/usr/share/kylin-update-desktop-config/data/" +#define UPDATE_VERSION_PATH_REPLACE "/usr/share/kylin-update-desktop-config/config/" +#define VERSION_FILE_PATH "/etc/kylin-version/kylin-system-version.conf" +#define OS_RELEASE_PATH "/etc/os-release" +#define KYLIN_RELEASE_ID "KYLIN_RELEASE_ID" +#define MAX_LINE 1024 + +typedef struct { + char *os_version; //系统版本 + char *update_version; //补丁版本 +} version_t; + +/* Json解析 */ +char * parseJsonText(char *); + +char * readJsonFile(char *); + +char * readReleaseFile(); + +/* 获取版本号 */ +version_t getSystemVersion( ); + +void getDefaultVersion( version_t * ); + +void getCurrentVersion( version_t * ); + +/* 读取配置 */ +int GetIniKeyString(char *section,char *key,char *filename,char *buf); + +int PutIniKeyString(char *section,char *key,char *val,char *filename); + +#endif /* GETSYSTEMVERSION_H */ diff --git a/src/systeminfo/getSystemVsersion.c b/src/systeminfo/getSystemVsersion.c new file mode 100644 index 0000000..f53c818 --- /dev/null +++ b/src/systeminfo/getSystemVsersion.c @@ -0,0 +1,215 @@ +#include "getSystemVersion.h" + +char * +readReleaseFile() { + char buf[MAX_LINE]; + FILE *fp; int len; + static char * os_version = NULL; + + if((fp = fopen(OS_RELEASE_PATH, "r")) == NULL) { + DBGprint("fail to read.\n"); + return os_version; + } + + while(fgets(buf,MAX_LINE,fp) != NULL) { + len = strlen(buf); + buf[len-1] = '\0'; + if ((strstr (buf, KYLIN_RELEASE_ID) != NULL )) { + os_version = strdup (buf); + DBGprint("Get os version --- %s \n",os_version); + } + } + return os_version; +} + +char * +parseJsonText(char *text) { + char *out; + cJSON *json; + cJSON *childJson = NULL; + char * version; + + DBGprint("%s \n",text); + + //获取补丁版本 + json=cJSON_Parse(text); + if (!json) { + DBGprint("Error before: [%s]\n",cJSON_GetErrorPtr()); + version=NULL; + } else { + childJson = cJSON_GetObjectItem(json, "version"); + if (!childJson) { + DBGprint("Error before: [%s]\n",cJSON_GetErrorPtr()); + version=NULL; + } else { + DBGprint("Get update version --- %s \r\n",childJson->valuestring); + version=strdup(childJson->valuestring); + } + + out=cJSON_Print(json); + cJSON_Delete(childJson); + free(out); + } + return version; +} + +char * +readJsonFile(char *json_file) { + FILE *fp; long len; + char *data; + static char *update_version; + + fp=fopen(json_file,"rb");fseek(fp,0,SEEK_END);len=ftell(fp);fseek(fp,0,SEEK_SET); + DBGprint("File [ %s ] size: %ld. \n", json_file, len); + data=(char*)malloc(len+1);fread(data,1,len,fp);fclose(fp); + update_version = parseJsonText(data); + free(data); + return update_version; +} + +void +getDefaultVersion( version_t *ver ) { + char *update_version, *os_version; + os_version = readReleaseFile(); //os-version + asprintf (&ver->os_version, "%s", os_version); + DBGprint("Get os_version: %s\n",ver->os_version); + if ( (access (UPDATE_VERSION_PATH_REPLACE "kylin-update-desktop-system.json", F_OK) == 0) ||\ + (access (UPDATE_VERSION_PATH "kylin-update-desktop-system.json", F_OK) == 0) ){ + if ( access (UPDATE_VERSION_PATH_REPLACE "kylin-update-desktop-system.json", F_OK) == 0) { + update_version = readJsonFile(UPDATE_VERSION_PATH_REPLACE "kylin-update-desktop-system.json"); //config update version + } else if ( access (UPDATE_VERSION_PATH "kylin-update-desktop-system.json", F_OK) == 0) { + update_version = readJsonFile(UPDATE_VERSION_PATH "kylin-update-desktop-system.json"); //data update version + } + asprintf (&ver->update_version, "%s", update_version); + DBGprint("Get update_version: %s\n",ver->update_version); + } else { + DBGprint("update version file(config|data) doesn't exist..\n"); + } +} + +/** 函数名: GetIniKeyString +*** 入口参数: section :配置文件中一组数据的标识 +*** key :这组数据中要读出的值的标识 +*** filename :要读取的文件路径 +*** 返回值: 找到需要查的值则返回正确结果 0 +*** 否则返回-1 */ +int GetIniKeyString(char *section,char *key,char *filename,char *buf) { + FILE *fp; + int flag = 0; + char sTitle[64], *wTmp; + char sLine[1024]; + sprintf(sTitle, "[%s]", section); + + if(NULL == (fp = fopen(filename, "r"))) { + perror("fopen"); + return -1; + } + while (NULL != fgets(sLine, 1024, fp)) { + // 这是注释行 + if (0 == strncmp("//", sLine, 2)) continue; + if ('#' == sLine[0]) continue; + wTmp = strchr(sLine, '='); + if ((NULL != wTmp) && (1 == flag)) { + if (0 == strncmp(key, sLine, strlen(key))) { // 长度依文件读取的为准 + sLine[strlen(sLine) - 1] = '\0'; + fclose(fp); + while(*(wTmp + 1) == ' '){ + wTmp++; + } + strcpy(buf,wTmp + 1); + return 0; + } + } else { + if (0 == strncmp(sTitle, sLine, strlen(sTitle))) { // 长度依文件读取的为准 + flag = 1; // 找到标题位置 + } + } + } + fclose(fp); + return -1; +} + +/** 函数名: PutIniKeyString +*** 入口参数: section:配置文件中一组数据的标识 +*** key:这组数据中要读出的值的标识 +*** val:更改后的值 +*** filename:要读取的文件路径 +*** 返回值: 成功返回 0 +*** 否则返回 -1 */ +int PutIniKeyString(char *section,char *key,char *val,char *filename) { + FILE *fpr, *fpw; + int flag = 0; + char sLine[1024], sTitle[32], *wTmp; + sprintf(sTitle, "[%s]", section); + if (NULL == (fpr = fopen(filename, "r"))) + return -1;// 读取原文件 + sprintf(sLine, "%s.tmp", filename); + if (NULL == (fpw = fopen(sLine, "w"))) + return -1;// 写入临时文件 + while (NULL != fgets(sLine, 1024, fpr)) { + if (2 != flag) { // 如果找到要修改的那一行,则不会执行内部的操作 + wTmp = strchr(sLine, '='); + if ((NULL != wTmp) && (1 == flag)) { + if (0 == strncmp(key, sLine, strlen(key))) { // 长度依文件读取的为准 + flag = 2;// 更改值,方便写入文件 + sprintf(wTmp + 1, " %s\n", val); + } + } else { + if (0 == strncmp(sTitle, sLine, strlen(sTitle))) { // 长度依文件读取的为准 + flag = 1; // 找到标题位置 + } + } + } + fputs(sLine, fpw); // 写入临时文件 + } + fclose(fpr); + fclose(fpw); + sprintf(sLine, "%s.tmp", filename); + return rename(sLine, filename);// 将临时文件更新到原文件 +} + +void +getCurrentVersion( version_t *ver ) { + char buff[100]; + + if ( 0 == GetIniKeyString("SYSTEM", "os_version", VERSION_FILE_PATH, buff)) { + if (ver->os_version == NULL) + asprintf (&ver->os_version, "%s", buff); + DBGprint("Get os_version: %s\n",ver->os_version); + } + + if (0 == GetIniKeyString("SYSTEM", "update_version", VERSION_FILE_PATH, buff)) { + if (ver->update_version == NULL) + asprintf (&ver->update_version, "%s", buff); + DBGprint("Get update_version: %s\n",ver->update_version); + } +} + +version_t +getSystemVersion() { + version_t version = { 0 }; + + if ( access (SYSTEM_VERSION_PATH, F_OK) == 0) { + DBGprint("System version file is exist..\n"); + //get current version + getCurrentVersion(&version); + } else { + DBGprint("System version file doesn't exist..\n"); + //get default version + getDefaultVersion(&version); + } + + if (((version.os_version == NULL) || (strstr (version.os_version, "null") != NULL )) && version.update_version != NULL) { + asprintf (&version.os_version, "%s", version.update_version); + } else if (( (version.update_version == NULL) || (strstr (version.update_version, "null") != NULL )) && version.os_version != NULL) { + asprintf (&version.update_version, "%s", version.os_version); + } else if ( ((version.os_version == NULL) || (strstr (version.os_version, "null") != NULL )) && ( (version.update_version == NULL) || (strstr (version.update_version, "null") != NULL )) ) { + asprintf (&version.os_version, "%s", ""); + asprintf (&version.update_version, "%s", ""); + } + + DBGprint("os_version :%d\n", strlen(version.os_version)); + DBGprint("update_version :%d\n", strlen(version.update_version)); + + return version; +} \ No newline at end of file diff --git a/src/systeminfo/test/kysysinfo_test.c b/src/systeminfo/test/kysysinfo_test.c index e045534..1769e55 100644 --- a/src/systeminfo/test/kysysinfo_test.c +++ b/src/systeminfo/test/kysysinfo_test.c @@ -1,4 +1,5 @@ #include +#include #include #include @@ -46,5 +47,10 @@ int main() res = kdk_system_get_hostCloudPlatform(); printf("云平台类型:%s\n", res); free(res); + + version_t test = getSystemVersion( ); + printf("test.os_version = %s\n",test.os_version); + printf("test.update_version = %s\n",test.update_version); + return 0; } \ No newline at end of file