-
Notifications
You must be signed in to change notification settings - Fork 1
/
conf.c
160 lines (139 loc) · 3.94 KB
/
conf.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <errno.h>
#include "cJSON.h"
#include "conf.h"
#define UPGRADECMD "upgradecmd"
#define PROGRAMS "programs"
#define NAME "name"
#define VERSION "version"
#define DOWNLOADNAME "downloadname"
#define UPDATECMD "updatecmd"
#define RUNCMD "runcmd"
#define PAGESIZE 512
void copy(char * src, char * dst){
FILE *in_fd = fopen(src,"rb");
FILE *out_fd = fopen(dst,"w");
char buf[PAGESIZE];
size_t n = 0;
while (n != EOF) {
n = fread(buf, 1, PAGESIZE, in_fd);
if (n == 0)
break;
fwrite(buf, 1, n, out_fd);
}
fclose(in_fd);
fclose(out_fd);
}
struct conf * loadconf(cJSON *json){
if(!json){
return NULL;
}
cJSON *jprograms= cJSON_GetObjectItem(json,PROGRAMS);
int programcount = cJSON_GetArraySize(jprograms);
struct program * programs = (struct program *)malloc(sizeof(struct program)*programcount);
memset(programs, 0, sizeof(struct program) * programcount);
cJSON * name;
cJSON * version;
cJSON * downloadname;
cJSON * updatecmd;
cJSON * runcmd;
int i = 0;
for(;i<programcount; i++){
cJSON * subitem = cJSON_GetArrayItem(jprograms, i);
name = cJSON_GetObjectItem(subitem, NAME);
if(name && name->type == cJSON_String){
programs[i].name = strdup(name->valuestring);
}
version = cJSON_GetObjectItem(subitem, VERSION);
if(version && version->type == cJSON_String){
programs[i].version = atoi(version->valuestring);
}
downloadname = cJSON_GetObjectItem(subitem, DOWNLOADNAME);
if(version && version->type == cJSON_String){
programs[i].downloadname = strdup(downloadname->valuestring);
}
updatecmd = cJSON_GetObjectItem(subitem, UPDATECMD);
if(updatecmd && updatecmd->type == cJSON_String){
programs[i].updatecmd = strdup(updatecmd->valuestring);
}
runcmd = cJSON_GetObjectItem(subitem, RUNCMD);
if(runcmd && runcmd->type == cJSON_String){
programs[i].runcmd = strdup(runcmd->valuestring);
}
}
cJSON * jupgradecmd = cJSON_GetObjectItem(json, UPGRADECMD);
struct conf * conf = (struct conf *) malloc(sizeof(struct conf));
memset(conf, 0, sizeof(struct conf));
if(jupgradecmd && jupgradecmd->type == cJSON_String){
conf->upgradecmd = strdup(jupgradecmd->valuestring);
}else{
return NULL;
}
conf->programcount = programcount;
conf->programs = programs;
return conf;
}
int includeprogram(struct conf * conflocal, struct program * program){
int i ;
for(i = 0; i < conflocal->programcount; i++){
if(strlen(conflocal->programs[i].name) == strlen(program->name) &&
0 == strcmp(conflocal->programs[i].name, program->name) &&
conflocal->programs[i].version == program->version){
return 1;
}
}
return 0;
}
struct list_head * getconf(struct conf * conflocal, struct conf * confurl){
if(conflocal == NULL || confurl == NULL){
return NULL;
}
struct list_head * head = (struct list_head *)malloc(sizeof(struct list_head));
INIT_LIST_HEAD(head);
int i;
for(i = 0; i < confurl->programcount; i++){
if(!includeprogram(conflocal, &confurl->programs[i])){
list_add_tail(&confurl->programs[i].list, head);
}
}
return head;
}
void destroyprogram( struct program * program ){
if(program){
free(program->name);
free(program->updatecmd);
free(program->runcmd);
}
}
void destroyconf(struct conf * conf){
if(conf){
int i = 0;
for(; i < conf->programcount; i++){
destroyprogram(&conf->programs[i]);
}
free(conf->upgradecmd);
free(conf->programs);
}
free(conf);
}
void downloadprogram(struct list_head * head){
struct program * program;
struct list_head * pos, *n;
list_for_each_safe(pos, n, head){
program = list_entry(pos, struct program, list);
remove(program->downloadname);
if (0 == system(program->updatecmd)){
copy(program->downloadname, program->name);
int rt = remove(program->downloadname);
fprintf(stdout, "%d %s\n", rt, strerror(errno));
}
}
}
void runprograms(struct conf * urlconf){
int i;
for(i = 0; i < urlconf->programcount; i++){
system(urlconf->programs[i].runcmd);
}
}