forked from BOINC/boinc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcs_statefile.C
359 lines (334 loc) · 11 KB
/
cs_statefile.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
// The contents of this file are subject to the BOINC Public License
// Version 1.0 (the "License"); you may not use this file except in
// compliance with the License. You may obtain a copy of the License at
// http://boinc.berkeley.edu/license_1.0.txt
//
// Software distributed under the License is distributed on an "AS IS"
// basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
// License for the specific language governing rights and limitations
// under the License.
//
// The Original Code is the Berkeley Open Infrastructure for Network Computing.
//
// The Initial Developer of the Original Code is the SETI@home project.
// Portions created by the SETI@home project are Copyright (C) 2002
// University of California at Berkeley. All Rights Reserved.
//
// Contributor(s):
//
#ifdef _WIN32
#include "stdafx.h"
#endif
#include "parse.h"
#include "error_numbers.h"
#include "filesys.h"
#include "file_names.h"
#include "client_msgs.h"
#include "client_state.h"
void CLIENT_STATE::set_client_state_dirty(char* source) {
log_messages.printf(CLIENT_MSG_LOG::DEBUG_STATE, "set dirty: %s\n", source);
client_state_dirty = true;
}
// Parse the client_state.xml file
//
int CLIENT_STATE::parse_state_file() {
char buf[256];
PROJECT temp_project, *project=NULL;
int retval=0;
int failnum;
SCOPE_MSG_LOG scope_messages(log_messages, CLIENT_MSG_LOG::DEBUG_STATE);
if (!boinc_file_exists(STATE_FILE_NAME)) {
scope_messages.printf("CLIENT_STATE::parse_state_file(): No state file; will create one\n");
// avoid warning messages about version
//
old_major_version = MAJOR_VERSION;
old_minor_version = MINOR_VERSION;
return ERR_FOPEN;
}
FILE* f = fopen(STATE_FILE_NAME, "r");
fgets(buf, 256, f);
if (!match_tag(buf, "<client_state>")) {
retval = ERR_XML_PARSE;
goto done;
}
while (fgets(buf, 256, f)) {
if (match_tag(buf, "</client_state>")) {
retval = 0;
break;
} else if (match_tag(buf, "<project>")) {
temp_project.parse_state(f);
project = lookup_project(temp_project.master_url);
if (project) {
project->copy_state_fields(temp_project);
} else {
msg_printf(NULL, MSG_ERROR, "Project %s found in state file but not prefs.\n",
temp_project.master_url);
}
} else if (match_tag(buf, "<app>")) {
APP* app = new APP;
app->parse(f);
if (project) {
retval = link_app(project, app);
if (!retval) apps.push_back(app);
} else {
delete app;
}
} else if (match_tag(buf, "<file_info>")) {
FILE_INFO* fip = new FILE_INFO;
fip->parse(f, false);
if (project) {
retval = link_file_info(project, fip);
if (!retval) file_infos.push_back(fip);
// If the file had a failure before, there's no reason
// to start another file transfer
if (fip->had_failure(failnum)) {
if (fip->pers_file_xfer) delete fip->pers_file_xfer;
fip->pers_file_xfer = NULL;
}
// Init PERS_FILE_XFER and push it onto pers_file_xfer stack
if (fip->pers_file_xfer) {
fip->pers_file_xfer->init(fip, fip->upload_when_present);
retval = pers_file_xfers->insert( fip->pers_file_xfer );
}
} else {
delete fip;
}
} else if (match_tag(buf, "<app_version>")) {
APP_VERSION* avp = new APP_VERSION;
avp->parse(f);
if (project) {
retval = link_app_version(project, avp);
if (!retval) app_versions.push_back(avp);
} else {
delete avp;
}
} else if (match_tag(buf, "<workunit>")) {
WORKUNIT* wup = new WORKUNIT;
wup->parse(f);
if (project) {
retval = link_workunit(project, wup);
if (!retval) workunits.push_back(wup);
} else {
delete wup;
}
} else if (match_tag(buf, "<result>")) {
RESULT* rp = new RESULT;
rp->parse_state(f);
if (project) {
retval = link_result(project, rp);
if (!retval) results.push_back(rp);
} else {
msg_printf(NULL, MSG_ERROR,
"<result> found before any project\n"
);
delete rp;
}
} else if (match_tag(buf, "<host_info>")) {
retval = host_info.parse(f);
if (retval) goto done;
} else if (match_tag(buf, "<time_stats>")) {
retval = time_stats.parse(f);
if (retval) goto done;
} else if (match_tag(buf, "<net_stats>")) {
retval = net_stats.parse(f);
if (retval) goto done;
} else if (match_tag(buf, "<active_task_set>")) {
retval = active_tasks.parse(f, this);
if (retval) goto done;
} else if (match_tag(buf, "<platform_name>")) {
// should match our current platform name
} else if (match_tag(buf, "<version>")) {
// could put logic here to detect incompatible state files
// after core client update
} else if (parse_int(buf, "<core_client_major_version>", old_major_version)) {
} else if (parse_int(buf, "<core_client_minor_version>", old_minor_version)) {
} else if (match_tag(buf, "<proxy_info>")) {
retval = pi.parse(f);
if (retval) goto done;
// } else if (parse_int(buf, "<user_run_request/>")) {
} else if (parse_str(buf, "<host_venue>", host_venue, sizeof(host_venue))) {
} else scope_messages.printf("CLIENT_STATE::parse_state_file: unrecognized: %s\n", buf);
}
done:
fclose(f);
return retval;
}
// read just the venue from the state file.
//
int CLIENT_STATE::parse_venue() {
char buf[256];
if (!boinc_file_exists(STATE_FILE_NAME)) return 0;
FILE* f = fopen(STATE_FILE_NAME, "r");
while (fgets(buf, 256, f)) {
if (match_tag(buf, "</client_state>")) {
break;
}
if (parse_str(buf, "<host_venue>", host_venue, sizeof(host_venue))) {
break;
}
}
fclose(f);
return 0;
}
// Write the client_state.xml file
//
int CLIENT_STATE::write_state_file() {
FILE* f = boinc_fopen(STATE_FILE_TEMP, "w");
int retval;
SCOPE_MSG_LOG scope_messages(log_messages, CLIENT_MSG_LOG::DEBUG_STATE);
scope_messages.printf("CLIENT_STATE::write_state_file(): Writing state file\n");
if (!f) {
msg_printf(0, MSG_ERROR, "Can't open temp state file: %s\n", STATE_FILE_TEMP);
return ERR_FOPEN;
}
retval = write_state(f);
fclose(f);
if (retval) return retval;
retval = boinc_rename(STATE_FILE_TEMP, STATE_FILE_NAME);
scope_messages.printf("CLIENT_STATE::write_state_file(): Done writing state file\n");
if (retval) return ERR_RENAME;
return 0;
}
int CLIENT_STATE::write_state(FILE* f) {
unsigned int i, j;
int retval;
fprintf(f, "<client_state>\n");
retval = host_info.write(f);
if (retval) return retval;
retval = time_stats.write(f, false);
if (retval) return retval;
retval = net_stats.write(f, false);
if (retval) return retval;
for (j=0; j<projects.size(); j++) {
PROJECT* p = projects[j];
retval = p->write_state(f);
if (retval) return retval;
for (i=0; i<apps.size(); i++) {
if (apps[i]->project == p) {
retval = apps[i]->write(f);
if (retval) return retval;
}
}
for (i=0; i<file_infos.size(); i++) {
if (file_infos[i]->project == p) {
retval = file_infos[i]->write(f, false);
if (retval) return retval;
}
}
for (i=0; i<app_versions.size(); i++) {
if (app_versions[i]->project == p) app_versions[i]->write(f);
}
for (i=0; i<workunits.size(); i++) {
if (workunits[i]->project == p) workunits[i]->write(f);
}
for (i=0; i<results.size(); i++) {
if (results[i]->project == p) results[i]->write(f, false);
}
}
active_tasks.write(f);
fprintf(f,
"<platform_name>%s</platform_name>\n"
"<core_client_major_version>%d</core_client_major_version>\n"
"<core_client_minor_version>%d</core_client_minor_version>\n",
platform_name,
core_client_major_version,
core_client_minor_version
);
// save proxy info
//
pi.write(f);
#if 0
fprintf(f, "<user_run_request>%d</user_run_request>\n", user_run_request);
#endif
if (strlen(host_venue)) {
fprintf(f, "<host_venue>%s</host_venue>\n", host_venue);
}
fprintf(f, "</client_state>\n");
return 0;
}
// Write the client_state.xml file if necessary
// TODO: write no more often than X seconds
//
int CLIENT_STATE::write_state_file_if_needed() {
int retval;
if (client_state_dirty) {
client_state_dirty = false;
retval = write_state_file();
if (retval) return retval;
}
return 0;
}
// look for app_versions.xml file in project dir.
// If find, get app versions from there,
// and use "anonymous platform" mechanism for this project
//
void CLIENT_STATE::check_anonymous() {
unsigned int i;
char dir[256], path[256];
FILE* f;
int retval;
for (i=0; i<projects.size(); i++) {
PROJECT* p = projects[i];
get_project_dir(p, dir);
sprintf(path, "%s%s%s", dir, PATH_SEPARATOR, APP_INFO_FILE_NAME);
f = fopen(path, "r");
if (!f) continue;
msg_printf(p, MSG_INFO,
"Found %s; using anonymous platform\n", APP_INFO_FILE_NAME
);
p->anonymous_platform = true;
// flag as anonymous even if can't parse file
retval = parse_app_info(p, f);
fclose(f);
}
}
int CLIENT_STATE::parse_app_info(PROJECT* p, FILE* in) {
char buf[256];
while (fgets(buf, 256, in)) {
if (match_tag(buf, "<app_info>")) continue;
if (match_tag(buf, "</app_info>")) return 0;
if (match_tag(buf, "<file_info>")) {
FILE_INFO* fip = new FILE_INFO;
if (fip->parse(in, false)) {
delete fip;
continue;
}
if (link_file_info(p, fip)) {
delete fip;
continue;
}
fip->status = FILE_PRESENT;
file_infos.push_back(fip);
continue;
}
if (match_tag(buf, "<app>")) {
APP* app = new APP;
if (app->parse(in)) {
delete app;
continue;
}
if (lookup_app(p, app->name)) {
delete app;
continue;
}
link_app(p, app);
apps.push_back(app);
continue;
}
if (match_tag(buf, "<app_version>")) {
APP_VERSION* avp = new APP_VERSION;
if (avp->parse(in)) {
delete avp;
continue;
}
if (gstate.link_app_version(p, avp)) {
delete avp;
continue;
}
link_app_version(p, avp);
app_versions.push_back(avp);
continue;
}
}
return ERR_XML_PARSE;
}