From 5dcbe6447c7213e26b13b6b4ff71a3c94be72ee0 Mon Sep 17 00:00:00 2001 From: Eric Heien Date: Fri, 14 Jun 2002 22:56:28 +0000 Subject: [PATCH] Added support for XML soft links and initialization function svn path=/trunk/boinc/; revision=113 --- api/api.C | 93 ++++++++++++++++++++++++++++++++++++++++++++++++++++++- api/api.h | 4 +++ 2 files changed, 96 insertions(+), 1 deletion(-) diff --git a/api/api.C b/api/api.C index d7c5fe36f3..b819729fed 100644 --- a/api/api.C +++ b/api/api.C @@ -21,8 +21,15 @@ #include #include #include +#ifdef WIN32 +#include +#include +#else #include +#endif +#include +#include #include "../sched/parse.h" #include "api.h" @@ -116,7 +123,7 @@ void parse_core_file(FILE* f, APP_IN& ai) { else if (parse_double(buf, "", ai.graphics.refresh_period)) continue; else if (parse_double(buf, "", ai.checkpoint_period)) continue; else if (parse_double(buf, "", ai.poll_period)) continue; - else fprintf(stderr, "read_core_file: unrecognized %s", buf); + else fprintf(stderr, "parse_core_file: unrecognized %s", buf); } } @@ -135,6 +142,60 @@ void write_app_file(FILE* f, APP_OUT& ao) { void parse_app_file(FILE* f, APP_OUT& ao) { } +void write_init_file(FILE* f, char *file_name, int fdesc, int input_file ) { + if( input_file ) { + fprintf( f, "%s\n", file_name ); + fprintf( f, "%d\n", fdesc ); + } else { + fprintf( f, "%s\n", file_name ); + fprintf( f, "%d\n", fdesc ); + } +} + +void parse_init_file(FILE* f) { + char buf[256],filename[256]; + int filedesc,fd,retval; + + while (fgets(buf, 256, f)) { + if (parse_str(buf, "", filename)) { + if (fgets(buf, 256, f)) { + if (parse_int(buf, "", filedesc)) { + fd = open(filename, O_RDONLY); + if (fd != filedesc) { + retval = dup2(fd, filedesc); + if (retval < 0) { + fprintf(stderr, "dup2 %d %d returned %d\n", fd, filedesc, retval); + exit(retval); + } + close(fd); + } + } + } + continue; + } + else if (parse_str(buf, "", filename)) { + if (fgets(buf, 256, f)) { + if (parse_int(buf, "", filedesc)) { + fd = open(filename, O_WRONLY|O_CREAT, 0660); + if (fd != filedesc) { + retval = dup2(fd, filedesc); + if (retval < 0) { + fprintf(stderr, "dup2 %d %d returned %d\n", fd, filedesc, retval); + exit(retval); + } + close(fd); + } + } + } + continue; + } + else fprintf(stderr, "parse_init_file: unrecognized %s", buf); + } +} + + +/* boinc_init reads the BOINC_INIT_FILE and CORE_TO_APP_FILE files and performs the necessary initialization */ + void boinc_init(APP_IN& ai) { FILE* f; @@ -144,6 +205,12 @@ void boinc_init(APP_IN& ai) { parse_core_file(f, ai); unlink(CORE_TO_APP_FILE); } + + f = fopen( BOINC_INIT_FILE, "r" ); + if (f) { + parse_init_file(f); + unlink(BOINC_INIT_FILE); + } } double boinc_time() { @@ -158,3 +225,27 @@ void boinc_poll(APP_IN& ai, APP_OUT& ao) { write_app_file(f, ao); rename("_app_temp", APP_TO_CORE_FILE); } + +/* boinc_resolve_link is used to resolve the XML soft links in a file. */ + +int boinc_resolve_link(char *file_name, char *resolved_name) +{ + FILE *fp; + char buf[512]; + + // Open the file and load the first line + fp = fopen( file_name, "r" ); + if (!fp) return -1; + rewind( fp ); + fgets(buf, 512, fp); + fclose( fp ); + + // If it's the XML tag, return it's value, + // otherwise, return the original file name + if( !parse_str( buf, "", resolved_name ) ) { + strcpy( resolved_name, file_name ); + } + + return 0; +} + diff --git a/api/api.h b/api/api.h index acbb768405..2402b8e0e8 100644 --- a/api/api.h +++ b/api/api.h @@ -75,12 +75,16 @@ struct APP_OUT { }; void write_core_file(FILE* f, APP_IN &ai); +void write_init_file(FILE* f, char *file_name, int fdesc, int input_file ); +void parse_init_file(FILE* f); void boinc_init(APP_IN&); double boinc_time(); void boinc_poll(APP_IN&, APP_OUT&); double boinc_cpu_time(); +int boinc_resolve_link(char *file_name, char *resolved_name); #define CORE_TO_APP_FILE "core_to_app.xml" #define APP_TO_CORE_FILE "app_to_core.xml" +#define BOINC_INIT_FILE "boinc_init.xml" #endif