mirror of
https://github.com/Ascend/ascend-docker-runtime.git
synced 2025-10-25 00:50:37 +08:00
Match-id-58ded49300187904b332c0e336002c8cc6bd1dbf
This commit is contained in:
@@ -14,7 +14,13 @@
|
|||||||
#define CMD_INDEX 1
|
#define CMD_INDEX 1
|
||||||
#define FINAL_FILE_INDEX 2
|
#define FINAL_FILE_INDEX 2
|
||||||
#define TEMP_FILE_INDEX 3
|
#define TEMP_FILE_INDEX 3
|
||||||
#define PATH_VALUE "/usr/local/bin/ascend-docker-runtime"
|
#define ASCEND_RUNTIME_PATH_VALUE "/usr/local/bin/ascend-docker-runtime"
|
||||||
|
#define ASCEND_RUNTIME_PATH_KEY "path"
|
||||||
|
#define ASCEND_RUNTIME_ARGS_KEY "runtimeArgs"
|
||||||
|
#define RUNTIME_KEY "runtimes"
|
||||||
|
#define ASCEND_RUNTIME_NAME "ascend"
|
||||||
|
#define DEFALUT_KEY "default-runtime"
|
||||||
|
#define DEFAULT_VALUE "ascend"
|
||||||
|
|
||||||
void ReadJsonFile(const FILE *pf, char *text, int maxBufferSize)
|
void ReadJsonFile(const FILE *pf, char *text, int maxBufferSize)
|
||||||
{
|
{
|
||||||
@@ -31,80 +37,200 @@ void ReadJsonFile(const FILE *pf, char *text, int maxBufferSize)
|
|||||||
text[size] = '\0';
|
text[size] = '\0';
|
||||||
}
|
}
|
||||||
|
|
||||||
int CreateNode(cJSON *runtimes)
|
cJSON *CreateAscendRuntimeInfo()
|
||||||
{
|
|
||||||
cJSON *node = cJSON_GetObjectItem(runtimes, "ascend");
|
|
||||||
if (node != NULL) {
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
cJSON *newItem = NULL;
|
|
||||||
newItem = cJSON_CreateObject();
|
|
||||||
if (newItem == NULL) {
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
cJSON_AddItemToObject(runtimes, "ascend", newItem);
|
|
||||||
|
|
||||||
cJSON *paraArray = NULL;
|
|
||||||
paraArray = cJSON_CreateArray();
|
|
||||||
if (paraArray == NULL) {
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
cJSON_AddItemToObject(newItem, "path", cJSON_CreateString(PATH_VALUE));
|
|
||||||
cJSON_AddItemToObject(newItem, "runtimeArgs", paraArray);
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
int CreateRuntimes(cJSON *root)
|
|
||||||
{
|
|
||||||
cJSON *runtimes = cJSON_CreateObject();
|
|
||||||
if (runtimes == NULL) {
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
cJSON_AddItemToObject(root, "runtimes", runtimes);
|
|
||||||
return CreateNode(runtimes);
|
|
||||||
}
|
|
||||||
|
|
||||||
cJSON *CreateContent()
|
|
||||||
{
|
{
|
||||||
cJSON *root = NULL;
|
cJSON *root = NULL;
|
||||||
root = cJSON_CreateObject();
|
root = cJSON_CreateObject();
|
||||||
if (root == NULL) {
|
if (root == NULL) {
|
||||||
|
fprintf(stderr, "create ascend runtime info root err\n");
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
int ret = CreateRuntimes(root);
|
cJSON *newString = NULL;
|
||||||
if (ret != 0) {
|
newString = cJSON_CreateString(ASCEND_RUNTIME_PATH_VALUE);
|
||||||
|
if (newString == NULL) {
|
||||||
|
fprintf(stderr, "create ascend runtime info path value err\n");
|
||||||
|
cJSON_Delete(root);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
cJSON *paraArray = NULL;
|
||||||
|
paraArray = cJSON_CreateArray();
|
||||||
|
if (paraArray == NULL) {
|
||||||
|
fprintf(stderr, "create ascend runtime info args err\n");
|
||||||
|
cJSON_Delete(root);
|
||||||
|
cJSON_Delete(newString);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
cJSON_AddItemToObject(root, ASCEND_RUNTIME_PATH_KEY, newString);
|
||||||
|
cJSON_AddItemToObject(root, ASCEND_RUNTIME_ARGS_KEY, paraArray);
|
||||||
|
|
||||||
return root;
|
return root;
|
||||||
}
|
}
|
||||||
|
|
||||||
cJSON *InsertContent(const FILE *pf)
|
cJSON *CreateRuntimes()
|
||||||
{
|
{
|
||||||
char jsonStr[MAX_JSON_FILE_SIZE] = {0x0};
|
cJSON *ascendRuntime = NULL;
|
||||||
ReadJsonFile(pf, &jsonStr[0], MAX_JSON_FILE_SIZE);
|
ascendRuntime = CreateAscendRuntimeInfo();
|
||||||
cJSON *root = NULL;
|
if (ascendRuntime == NULL) {
|
||||||
root = cJSON_Parse(jsonStr);
|
fprintf(stderr, "create ascendruntime err\n");
|
||||||
if (!root) {
|
|
||||||
fprintf(stderr, "Error before: [%s]\n", cJSON_GetErrorPtr());
|
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
cJSON *runtimes = NULL;
|
cJSON *runtimes = NULL;
|
||||||
runtimes = cJSON_GetObjectItem(root, "runtimes");
|
runtimes = cJSON_CreateObject();
|
||||||
int ret;
|
|
||||||
if (runtimes == NULL) {
|
if (runtimes == NULL) {
|
||||||
ret = CreateRuntimes(root);
|
fprintf(stderr, "create runtimes err\n");
|
||||||
} else {
|
cJSON_Delete(ascendRuntime);
|
||||||
ret = CreateNode(runtimes);
|
|
||||||
}
|
|
||||||
if (ret != 0) {
|
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
cJSON_AddItemToObject(runtimes, ASCEND_RUNTIME_NAME, ascendRuntime);
|
||||||
|
|
||||||
|
return runtimes;
|
||||||
|
}
|
||||||
|
|
||||||
|
int DelJsonContent(cJSON *root, const char *key)
|
||||||
|
{
|
||||||
|
cJSON *existItem = NULL;
|
||||||
|
existItem = cJSON_GetObjectItem(root, key);
|
||||||
|
if (existItem == NULL) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
cJSON *removedItem = NULL;
|
||||||
|
removedItem = cJSON_DetachItemViaPointer(root, existItem);
|
||||||
|
if (removedItem == NULL) {
|
||||||
|
fprintf(stderr, "remove %s failed\n", key);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
cJSON_Delete(removedItem);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
cJSON *CreateContent()
|
||||||
|
{
|
||||||
|
/* 插入ascend runtime */
|
||||||
|
cJSON *runtimes = NULL;
|
||||||
|
runtimes = CreateRuntimes();
|
||||||
|
if (runtimes == NULL) {
|
||||||
|
fprintf(stderr, "create runtimes err\n");
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
cJSON *defaultRuntime = NULL;
|
||||||
|
defaultRuntime = cJSON_CreateString(DEFAULT_VALUE);
|
||||||
|
if (defaultRuntime == NULL) {
|
||||||
|
cJSON_Delete(runtimes);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
cJSON *root = NULL;
|
||||||
|
root = cJSON_CreateObject();
|
||||||
|
if (root == NULL) {
|
||||||
|
/* ascendRuntime已经挂载到runtimes上了,再释放会coredump */
|
||||||
|
fprintf(stderr, "create root err\n");
|
||||||
|
cJSON_Delete(runtimes);
|
||||||
|
cJSON_Delete(defaultRuntime);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
cJSON_AddItemToObject(root, RUNTIME_KEY, runtimes);
|
||||||
|
|
||||||
|
cJSON_AddItemToObject(root, DEFALUT_KEY, defaultRuntime);
|
||||||
|
|
||||||
|
return root;
|
||||||
|
}
|
||||||
|
|
||||||
|
cJSON *ModifyContent(const FILE *pf)
|
||||||
|
{
|
||||||
|
char jsonStr[MAX_JSON_FILE_SIZE] = {0x0};
|
||||||
|
ReadJsonFile(pf, &jsonStr[0], MAX_JSON_FILE_SIZE);
|
||||||
|
|
||||||
|
cJSON *root = NULL;
|
||||||
|
root = cJSON_Parse(jsonStr);
|
||||||
|
if (root == NULL) {
|
||||||
|
fprintf(stderr, "Error before: [%s]\n", cJSON_GetErrorPtr());
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* 插入ascend runtime */
|
||||||
|
cJSON *runtimes = NULL;
|
||||||
|
runtimes = cJSON_GetObjectItem(root, "runtimes");
|
||||||
|
if (runtimes == NULL) {
|
||||||
|
runtimes = CreateRuntimes();
|
||||||
|
if (runtimes == NULL) {
|
||||||
|
cJSON_Delete(root);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
cJSON_AddItemToObject(root, RUNTIME_KEY, runtimes);
|
||||||
|
} else {
|
||||||
|
int ret = DelJsonContent(runtimes, ASCEND_RUNTIME_NAME);
|
||||||
|
if (ret != 0) {
|
||||||
|
cJSON_Delete(root);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
cJSON *ascendRuntime = NULL;
|
||||||
|
ascendRuntime = CreateAscendRuntimeInfo();
|
||||||
|
if (ascendRuntime == NULL) {
|
||||||
|
cJSON_Delete(root);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
cJSON_AddItemToObject(runtimes, ASCEND_RUNTIME_NAME, ascendRuntime);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* 插入defaul runtime */
|
||||||
|
int ret = DelJsonContent(root, DEFALUT_KEY);
|
||||||
|
if (ret != 0) {
|
||||||
|
cJSON_Delete(root);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
cJSON *defaultRuntime = cJSON_CreateString(DEFAULT_VALUE);
|
||||||
|
if (defaultRuntime == NULL) {
|
||||||
|
cJSON_Delete(root);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
cJSON_AddItemToObject(root, DEFALUT_KEY, defaultRuntime);
|
||||||
|
|
||||||
|
return root;
|
||||||
|
}
|
||||||
|
|
||||||
|
cJSON *RemoveContent(const FILE *pf)
|
||||||
|
{
|
||||||
|
char jsonStr[MAX_JSON_FILE_SIZE] = {0x0};
|
||||||
|
ReadJsonFile(pf, &jsonStr[0], MAX_JSON_FILE_SIZE);
|
||||||
|
|
||||||
|
cJSON *root = NULL;
|
||||||
|
root = cJSON_Parse(jsonStr);
|
||||||
|
if (root == NULL) {
|
||||||
|
fprintf(stderr, "Error before: [%s]\n", cJSON_GetErrorPtr());
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* 去除default runtimes */
|
||||||
|
int ret = DelJsonContent(root, DEFALUT_KEY);
|
||||||
|
if (ret != 0) {
|
||||||
|
cJSON_Delete(root);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* 去除runtimes */
|
||||||
|
cJSON *runtimes = NULL;
|
||||||
|
runtimes = cJSON_GetObjectItem(root, RUNTIME_KEY);
|
||||||
|
if (runtimes == NULL) {
|
||||||
|
fprintf(stderr, "no runtime key found\n");
|
||||||
|
cJSON_Delete(root);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
ret = DelJsonContent(runtimes, DEFAULT_VALUE);
|
||||||
|
if (ret != 0) {
|
||||||
|
cJSON_Delete(root);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
return root;
|
return root;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -117,7 +243,7 @@ int DetectAndCreateJsonFile(const char *filePath, const char *tempPath)
|
|||||||
if (pf == NULL) {
|
if (pf == NULL) {
|
||||||
root = CreateContent();
|
root = CreateContent();
|
||||||
} else {
|
} else {
|
||||||
root = InsertContent(pf);
|
root = ModifyContent(pf);
|
||||||
fclose(pf);
|
fclose(pf);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -140,46 +266,6 @@ int DetectAndCreateJsonFile(const char *filePath, const char *tempPath)
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
cJSON *GetNewContent(const FILE *pf)
|
|
||||||
{
|
|
||||||
char jsonStr[MAX_JSON_FILE_SIZE] = {0x0};
|
|
||||||
ReadJsonFile(pf, &jsonStr[0], MAX_JSON_FILE_SIZE);
|
|
||||||
|
|
||||||
cJSON *root = NULL;
|
|
||||||
root = cJSON_Parse(jsonStr);
|
|
||||||
if (!root) {
|
|
||||||
fprintf(stderr, "Error before: [%s]\n", cJSON_GetErrorPtr());
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
cJSON *runtimes = NULL;
|
|
||||||
runtimes = cJSON_GetObjectItem(root, "runtimes");
|
|
||||||
if (runtimes == NULL) {
|
|
||||||
fprintf(stderr, "no runtime key found\n");
|
|
||||||
cJSON_Delete(root);
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
cJSON *ascend = NULL;
|
|
||||||
ascend = cJSON_GetObjectItem(runtimes, "ascend");
|
|
||||||
if (ascend == NULL) {
|
|
||||||
fprintf(stderr, "no ascend key found\n");
|
|
||||||
cJSON_Delete(root);
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
cJSON *removedItem = NULL;
|
|
||||||
removedItem = cJSON_DetachItemViaPointer(runtimes, ascend);
|
|
||||||
if (removedItem == NULL) {
|
|
||||||
fprintf(stderr, "remove runtime failed\n");
|
|
||||||
cJSON_Delete(root);
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
cJSON_Delete(removedItem);
|
|
||||||
return root;
|
|
||||||
}
|
|
||||||
|
|
||||||
int CreateRevisedJsonFile(const char *filePath, const char *tempPath)
|
int CreateRevisedJsonFile(const char *filePath, const char *tempPath)
|
||||||
{
|
{
|
||||||
FILE *pf = NULL;
|
FILE *pf = NULL;
|
||||||
@@ -189,7 +275,7 @@ int CreateRevisedJsonFile(const char *filePath, const char *tempPath)
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
cJSON *newContent = NULL;
|
cJSON *newContent = NULL;
|
||||||
newContent = GetNewContent(pf);
|
newContent = RemoveContent(pf);
|
||||||
fclose(pf);
|
fclose(pf);
|
||||||
|
|
||||||
if (newContent == NULL) {
|
if (newContent == NULL) {
|
||||||
@@ -225,5 +311,4 @@ int main(int argc, char *argv[])
|
|||||||
return DetectAndCreateJsonFile(argv[FINAL_FILE_INDEX], argv[TEMP_FILE_INDEX]);
|
return DetectAndCreateJsonFile(argv[FINAL_FILE_INDEX], argv[TEMP_FILE_INDEX]);
|
||||||
}
|
}
|
||||||
return CreateRevisedJsonFile(argv[FINAL_FILE_INDEX], argv[TEMP_FILE_INDEX]);
|
return CreateRevisedJsonFile(argv[FINAL_FILE_INDEX], argv[TEMP_FILE_INDEX]);
|
||||||
|
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user