[修改]获取系统版本号代码格式

This commit is contained in:
shaozhimin 2022-08-15 08:41:46 +08:00
parent 9f49556352
commit f2ad3834fd
7 changed files with 243 additions and 273 deletions

View File

@ -1,4 +1,3 @@
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/

View File

@ -20,6 +20,3 @@ install(FILES libkysysinfo.hpp
install(FILES libkysysinfo.h
DESTINATION include/kysdk/kysdk-system)
install(FILES getSystemVersion.h
DESTINATION include/kysdk/kysdk-system)

View File

@ -1,52 +0,0 @@
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <cJSON.h>
/* 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 */

View File

@ -1,215 +0,0 @@
#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;
}

View File

@ -10,6 +10,14 @@
#define KYLIN_ACTIVATION_DBUS_ADDRESS "org.freedesktop.activation"
#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
static char* get_val_from_file(FILE *fp, const char *key)
{
@ -615,3 +623,220 @@ bool kdk_system_is_zyj(void)
SAFE_FREE(project_codename);
return bool_value;
}
char *readReleaseFile()
{
char buf[MAX_LINE];
FILE *fp; int len;
static char * os_version = NULL;
if((fp = fopen(OS_RELEASE_PATH, "r")) == NULL) {
printf("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);
printf("Get os version --- %s \n",os_version);
}
}
return os_version;
}
char *parseJsonText(char *text)
{
char *out;
cJSON *json;
cJSON *childJson = NULL;
char * version;
// printf("%s \n",text);
//获取补丁版本
json=cJSON_Parse(text);
if (!json) {
printf("Error before: [%s]\n",cJSON_GetErrorPtr());
version=NULL;
} else {
childJson = cJSON_GetObjectItem(json, "version");
if (!childJson) {
printf("Error before: [%s]\n",cJSON_GetErrorPtr());
version=NULL;
} else {
printf("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);
printf("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);
printf("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);
printf("Get update_version: %s\n",ver->update_version);
} else {
printf("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);
printf("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);
printf("Get update_version: %s\n",ver->update_version);
}
}
version_t kdk_system_get_version_detaile()
{
version_t version = { 0 };
if ( access (SYSTEM_VERSION_PATH, F_OK) == 0) {
printf("System version file is exist..\n");
//get current version
getCurrentVersion(&version);
} else {
printf("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", "");
}
printf("os_version :%d\n", strlen(version.os_version));
printf("update_version :%d\n", strlen(version.update_version));
return version;
}

View File

@ -17,11 +17,20 @@
#include <string.h>
#include <stdbool.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <cJSON.h>
#ifdef __cplusplus
extern "C" {
#endif
typedef struct {
char *os_version; //系统版本
char *update_version; //补丁版本
} version_t;
/**
* @brief
*
@ -128,6 +137,14 @@ extern char* kdk_system_get_hostCloudPlatform();
extern bool kdk_system_is_zyj(void);
/**
* @brief
*
* @return version_t
*/
extern version_t kdk_system_get_version_detaile();
#ifdef __cplusplus
}
#endif

View File

@ -1,5 +1,4 @@
#include <libkysysinfo.h>
#include <getSystemVersion.h>
#include <stdio.h>
#include <stdlib.h>
@ -48,7 +47,7 @@ int main()
printf("云平台类型:%s\n", res);
free(res);
version_t test = getSystemVersion( );
version_t test = kdk_system_get_version_detaile( );
printf("test.os_version = %s\n",test.os_version);
printf("test.update_version = %s\n",test.update_version);