bool XMLValidationRelaxNG::validate(xmlTextReader * reader, std::string * error) const { int last; int valid; if (errorBuffer) { delete errorBuffer; } errorBuffer = new std::string(); xmlTextReaderSetErrorHandler(reader, (xmlTextReaderErrorFunc) XMLValidation::errorReaderFunction, 0); xmlTextReaderRelaxNGSetSchema(reader, getValidationFile < xmlRelaxNG > ()); while ((last = xmlTextReaderRead(reader)) == 1) ; valid = xmlTextReaderIsValid(reader); xmlTextReaderSetErrorHandler(reader, 0, 0); xmlFreeTextReader(reader); if (last == -1 || valid != 1) { *error = *errorBuffer; return false; } return true; }
int oval_definition_model_merge(struct oval_definition_model *model, const char *file) { __attribute__nonnull__(model); int ret; xmlTextReader *reader = xmlNewTextReaderFilename(file); if (reader == NULL) { oscap_seterr(OSCAP_EFAMILY_GLIBC, "%s '%s'", strerror(errno), file); return -1; } /* setup context */ struct oval_parser_context context; context.reader = reader; context.definition_model = model; context.user_data = NULL; xmlTextReaderSetErrorHandler(reader, &libxml_error_handler, &context); /* jump into oval_definitions */ while (xmlTextReaderRead(reader) == 1 && xmlTextReaderNodeType(reader) != XML_READER_TYPE_ELEMENT) ; /* start parsing */ ret = oval_definition_model_parse(reader, &context); xmlFreeTextReader(reader); return ret; }
//----------------------------------------- bool RngValidator::run(const std::string& xml_file_pathname, const std::string& rng_file_pathname) //----------------------------------------- { // TODO handle multiple RNG files (eg viz) // RELAX NG Parser Context xmlRelaxNGParserCtxtPtr ctxt = xmlRelaxNGNewParserCtxt(rng_file_pathname.c_str()); xmlRelaxNGSetParserErrors(ctxt, (xmlRelaxNGValidityErrorFunc)RngValidator::rngErr, (xmlRelaxNGValidityWarningFunc)RngValidator::rngWarn, NULL); xmlRelaxNGPtr schema = xmlRelaxNGParse(ctxt); xmlRelaxNGFreeParserCtxt(ctxt); xmlTextReaderPtr reader = xmlNewTextReaderFilename(xml_file_pathname.c_str()); xmlTextReaderRelaxNGSetSchema(reader, schema); xmlTextReaderSetErrorHandler(reader, (xmlTextReaderErrorFunc)RngValidator::readerErr, NULL); xmlTextReaderSetStructuredErrorHandler(reader, (xmlStructuredErrorFunc)RngValidator::structErr, NULL); while (xmlTextReaderRead(reader)); const bool valid = xmlTextReaderIsValid(reader) == 1; xmlFreeTextReader(reader); xmlRelaxNGFree(schema); return valid; }
gboolean xml_reader_load_from_stream (XmlReader *reader, GInputStream *stream, GError **error) { g_return_val_if_fail (XML_IS_READER(reader), FALSE); xml_reader_clear (reader); reader->xml = xmlReaderForIO (xml_reader_io_read_cb, xml_reader_io_close_cb, stream, reader->uri, reader->encoding, XML_PARSE_RECOVER | XML_PARSE_NOBLANKS | XML_PARSE_COMPACT); if (!reader->xml) { g_set_error (error, XML_READER_ERROR, XML_READER_ERROR_INVALID, _("Could not parse XML from stream")); return FALSE; } reader->stream = g_object_ref (stream); xmlTextReaderSetErrorHandler (reader->xml, xml_reader_error_cb, reader); return TRUE; }
xmlTextReaderPtr libabw::xmlReaderForStream(librevenge::RVNGInputStream *input) { xmlTextReaderPtr reader = xmlReaderForIO(abwxmlInputReadFunc, abwxmlInputCloseFunc, (void *)input, 0, 0, XML_PARSE_NOBLANKS|XML_PARSE_NOENT|XML_PARSE_NONET|XML_PARSE_RECOVER); xmlTextReaderSetErrorHandler(reader, abwxmlReaderErrorFunc, 0); return reader; }
char * xccdf_detect_version(const char* file) { const struct xccdf_version_info *ver_info; char *doc_version; xmlTextReaderPtr reader = xmlReaderForFile(file, NULL, 0); if (!reader) { oscap_seterr(OSCAP_EFAMILY_GLIBC, "Unable to open file: '%s'", file); return NULL; } xmlTextReaderSetErrorHandler(reader, &libxml_error_handler, NULL); while (xmlTextReaderRead(reader) == 1 && xmlTextReaderNodeType(reader) != XML_READER_TYPE_ELEMENT); ver_info = xccdf_detect_version_parser(reader); if(!ver_info) { xmlFreeTextReader(reader); return NULL; } doc_version = strdup(xccdf_version_info_get_version(ver_info)); xmlFreeTextReader(reader); return doc_version; }
static bool create_xml_reader(write_data_t *write_data) { write_data->xml_reader = log_ralloc(write_data->r, xmlReaderForMemory(write_data->response_text->elts, write_data->response_text->nelts * write_data->response_text->elt_size, NULL, NULL, 0) ); if (write_data->xml_reader == NULL) { return false; } xmlTextReaderSetErrorHandler(write_data->xml_reader, xml_reader_error, write_data); return true; }
gboolean xml_reader_load_from_path (XmlReader *reader, const gchar *path) { g_return_val_if_fail (XML_IS_READER (reader), FALSE); xml_reader_clear (reader); if ((reader->xml = xmlNewTextReaderFilename (path))) xmlTextReaderSetErrorHandler (reader->xml, xml_reader_error_cb, reader); return (reader->xml != NULL); }
static int raptor_rss_parse_chunk(raptor_parser* rdf_parser, const unsigned char *s, size_t len, int is_end) { raptor_rss_parser_context* rss_parser=(raptor_rss_parser_context*)rdf_parser->context; int ret; if(!rss_parser->reader) { unsigned char *uri=raptor_uri_as_string(rdf_parser->base_uri); rss_parser->input=xmlParserInputBufferCreateMem((const char*)s, len, XML_CHAR_ENCODING_NONE); rss_parser->reader=xmlNewTextReader(rss_parser->input, (const char*)uri); xmlTextReaderSetErrorHandler(rss_parser->reader, raptor_rss_error_handler, rdf_parser); } else if(s && len) xmlParserInputBufferPush(rss_parser->input, len, (const char*)s); if(!is_end) return 0; ret = xmlTextReaderRead(rss_parser->reader); while (ret == 1) { if(rdf_parser->failed) break; raptor_rss_parser_processNode(rdf_parser); ret = xmlTextReaderRead(rss_parser->reader); } xmlFreeTextReader(rss_parser->reader); rss_parser->reader=NULL; xmlFreeParserInputBuffer(rss_parser->input); rss_parser->input=NULL; if(rdf_parser->failed) return 1; /* turn strings into URIs, move things around if needed */ raptor_rss_insert_identifiers(rdf_parser); /* generate the triples */ raptor_rss_emit(rdf_parser); return (ret != 0); }
static xmlTextReaderPtr create_parser(const char *filename) { xmlTextReaderPtr reader = NULL; if ((reader = xmlReaderForFile(filename, NULL, 0))) { xmlTextReaderSetErrorHandler(reader, error_handler, NULL); if (xmlTextReaderRead(reader) != 1) { xmlFreeTextReader(reader); reader = NULL; } } else { tmx_err(E_UNKN, "xml parser: unable to open %s", filename); } return reader; }
XMLParser::XMLParser( intf_thread_t *pIntf, const string &rFileName ): SkinObject( pIntf ) { m_pReader = xmlNewTextReaderFilename( rFileName.c_str() ); if( !m_pReader ) { msg_Err( getIntf(), "Failed to open %s for parsing", rFileName.c_str() ); return; } // Activate DTD validation xmlTextReaderSetParserProp( m_pReader, XML_PARSER_DEFAULTATTRS, 1 ); xmlTextReaderSetParserProp( m_pReader, XML_PARSER_VALIDATE, 1 ); // Set the error handler xmlTextReaderSetErrorHandler( m_pReader, handleError, this ); }
static xml_reader_t *ReaderCreate( xml_t *p_xml, stream_t *p_stream ) { xml_reader_t *p_reader; xml_reader_sys_t *p_sys; xmlTextReaderPtr p_libxml_reader; p_libxml_reader = xmlReaderForIO( StreamRead, NULL, p_stream, NULL, NULL, 0 ); if( !p_libxml_reader ) { msg_Err( p_xml, "failed to create XML parser" ); return NULL; } p_reader = malloc( sizeof(xml_reader_t) ); if( !p_reader ) { xmlFreeTextReader( p_libxml_reader ); return NULL; } p_reader->p_sys = p_sys = malloc( sizeof(xml_reader_sys_t) ); if( !p_sys ) { xmlFreeTextReader( p_libxml_reader ); free( p_reader ); return NULL; } p_reader->p_sys->p_reader = p_libxml_reader; p_reader->p_xml = p_xml; /* Set the error handler */ xmlTextReaderSetErrorHandler( p_libxml_reader, ReaderErrorHandler, p_reader ); p_reader->pf_read = ReaderRead; p_reader->pf_node_type = ReaderNodeType; p_reader->pf_name = ReaderName; p_reader->pf_value = ReaderValue; p_reader->pf_next_attr = ReaderNextAttr; p_reader->pf_use_dtd = ReaderUseDTD; return p_reader; }
gboolean xml_reader_load_from_data (XmlReader *reader, const gchar *data, gssize length, const gchar *uri, const gchar *encoding) { g_return_val_if_fail (XML_IS_READER (reader), FALSE); xml_reader_clear (reader); if (length == -1) length = strlen (data); reader->xml = xmlReaderForMemory (data, length, uri, encoding, 0); xmlTextReaderSetErrorHandler (reader->xml, xml_reader_error_cb, reader); return (reader->xml != NULL); }
struct xccdf_benchmark *xccdf_benchmark_import(const char *file) { xmlTextReaderPtr reader = xmlReaderForFile(file, NULL, 0); if (!reader) { oscap_seterr(OSCAP_EFAMILY_GLIBC, "Unable to open file: '%s'", file); return NULL; } xmlTextReaderSetErrorHandler(reader, &libxml_error_handler, NULL); while (xmlTextReaderRead(reader) == 1 && xmlTextReaderNodeType(reader) != XML_READER_TYPE_ELEMENT) ; struct xccdf_benchmark *benchmark = xccdf_benchmark_new(); const bool parse_result = xccdf_benchmark_parse(XITEM(benchmark), reader); xmlFreeTextReader(reader); if (!parse_result) { // parsing fatal error oscap_seterr(OSCAP_EFAMILY_XML, "Failed to parse '%s'.", file); xccdf_benchmark_free(benchmark); return NULL; } // This is sadly the only place where we can pass origin file information // to the CPE1 embedded dictionary (if any). It is necessary to figure out // proper paths to OVAL files referenced from CPE1 dictionaries. // FIXME: Refactor and move this somewhere else struct cpe_dict_model* embedded_dict = xccdf_benchmark_get_cpe_list(benchmark); if (embedded_dict != NULL) { cpe_dict_model_set_origin_file(embedded_dict, file); } // same situation with embedded CPE2 lang model // FIXME: Refactor and move this somewhere else struct cpe_lang_model* embedded_lang_model = xccdf_benchmark_get_cpe_lang_model(benchmark); if (embedded_lang_model != NULL) { cpe_lang_model_set_origin_file(embedded_lang_model, file); } return benchmark; }
struct xccdf_tailoring *xccdf_tailoring_import(const char *file, struct xccdf_benchmark *benchmark) { xmlTextReaderPtr reader = xmlReaderForFile(file, NULL, 0); if (!reader) { oscap_seterr(OSCAP_EFAMILY_GLIBC, "Unable to open file: '%s'", file); return NULL; } xmlTextReaderSetErrorHandler(reader, &libxml_error_handler, NULL); while (xmlTextReaderRead(reader) == 1 && xmlTextReaderNodeType(reader) != XML_READER_TYPE_ELEMENT) ; struct xccdf_tailoring *tailoring = xccdf_tailoring_parse(reader, XITEM(benchmark)); xmlFreeTextReader(reader); if (!tailoring) { // parsing fatal error oscap_seterr(OSCAP_EFAMILY_XML, "Failed to parse '%s'.", file); xccdf_tailoring_free(tailoring); return NULL; } return tailoring; }
void readConfig( const char* filename ) { /* States: * 0 : reading new entities ( to: 1, 3, 4, 5 ) * 1 : reading machine * 2 : reading role * 3 : reading core * 4 : reading file * 5 : reading test ( to: 2 ) */ int state = 0; struct machine m; struct role r; struct file f; struct core c; struct test t; xmlTextReaderPtr xmlRead = xmlReaderForFile( filename, NULL, XML_PARSE_NONET | XML_PARSE_NOENT | XML_PARSE_NOCDATA | XML_PARSE_NOXINCNODE | XML_PARSE_COMPACT ); if( !xmlRead ) quit( "Could not open %s for reading as XML config\n", filename ); xmlTextReaderSetErrorHandler( xmlRead, xmlErrorHandler, 0 ); int nodeType, ret; xmlChar* nodeName; while( 1 ) { if( ( ret = xmlTextReaderRead( xmlRead ) ) != 1 ) { if( ret < 0 ) quit( "Error occurred\n" ); if( state ) quit( "No more nodes to read, but state is not 0. Incomplete elements.\n" ); xmlTextReaderClose( xmlRead ); return; } nodeType = xmlTextReaderNodeType( xmlRead ); nodeName = NULL; switch( nodeType ) { case XmlNodeType.Element : nodeName = xmlTextReaderLocalName( xmlRead ); switch( state ) { case 0 : if( !strcmp( nodeName, "machine" ) ) { state = 1; bzero( m, sizeof( struct machine ) ); readValidRequiredAttribute( m.name, "machine", "name" ); readValidRequiredAttribute( m.address, "machine", "address" ); if( !strcmp( m.address, "DAS4" ) ) { readValidRequiredAttribute( m.count, "machine", "DAS4 node count" ); char* end = NULL; m.icount = strtol( m.count, &end, 10 ); if( end == m.count || *end || m.icount < 1 ) quit( "Invalid DAS4 node count for machine %s\n", m.name ); } break; } if( !strcmp( nodeName, "core" ) ) { state = 3; bzero( c, sizeof( struct core ) ); readValidRequiredAttribute( c.name, "core", "name" ); break; } if( !strcmp( nodeName, "file" ) ) { state = 4; bzero( f, sizeof( struct file ) ); readValidRequiredAttribute( f.name, "file", "name" ); readValidRequiredAttribute( f.size, "file", "size" ); char* end = NULL; f.isize = strtol( f.size, &end, 10 ); if( end == f.size || f.isize < 1 ) quit( "Invalid size specifier for file %s\n", f.name ); if( *end && *(end+1) ) quit( "Invalid size specifier for file %s\n", f.name ); if( *end ) { switch( *end ) { case 'b' : case 'B' : break; case 'k' : case 'K' : f.isize *= 1024; break; case 'm' : case 'M' : f.isize *= 1024 * 1024; break; case 'g' : case 'G' : f.isize *= 1024 * 1024 * 1024; break; case 't' : case 'T' : f.isize *= 1024 * 1024 * 1024 * 1024; break; default : quit( "Invalid size specifier for file %s\n", f.name ); } } if( f.isize > 512 * 1024 * 1024 ) { int p = 512*1024*1024; while( p < f.isize ) p <<= 1; if( p != f.isize ) quit( "Invalid size specifier for file %s: sizes above 512M should be powers of 2\n", f.name ); } if( f.isize & 0x3 ) quit( "Invalid size specifier for file %s: sizes should be a multiple of 4\n", f.name ); break; } if( !strcmp( nodeName, "test" ) ) { state = 5; bzero( t, sizeof( struct test ) ); readValidRequiredAttribute( t.name, "test", "name" ); break; } break; case 1 : if( !strcmp( nodeName, "tmpDir" ) ) { onlyOne( m.tmpdir, "machine", "tmpDir" ); readValidString( m.tmpdir, "temporary directory location" ); skipToEndElement( "tmpDir" ); break; } if( !strcmp( nodeName, "params" ) ) { onlyOne( m.params, "machine", "params" ); readValidString( m.params, "params" ); skipToEndElement( "params" ); break; } printf( "Unexpected element %s in machine %s\n", nodeName, m.name ); break; case 2 : if( !strcmp( nodeName, "machine" ) ) { if( r.machineCount > 255 ) quit( "Maximum of 256 machines per role passed on line %d\n", xmlTextReaderGetParserLineNumber( xmlRead ) ); readValid( r.machine[r.machineCount], "machine name" ); r.machineCount++; skipToEndElement( "machine" ); break; } if( !strcmp( nodeName, "user" ) ) { onlyOne( r.user, "role", "user" ); readValidString( r.user, "user" ); skipToEndElement( "user" ); break; } if( !strcmp( nodeName, "core" ) ) { onlyOne( r.core, "role", "core" ); readValidString( r.core, "core name" ); skipToEndElement( "core" ); break; } if( !strcmp( nodeName, "file" ) ) { onlyOne( r.file, "role", "file" ); readValidString( r.file, "file name" ); skipToEndElement( "file" ); break; } if( !strcmp( nodeName, "params" ) ) { onlyOne( r.params, "role", "params" ); readValidString( r.params, "params" ); skipToEndElement( "params" ); break; } printf( "Unexpected element %s in role on line %d\n", nodeName, xmlTextReaderGetParserLineNumber( xmlRead ) ); break; case 3 : if( !strcmp( nodeName, "params" ) ) { onlyOne( c.params, "core", "params" ); readValidString( c.params, "params" ); skipToEndElement( "params" ); break; } if( !strcmp( nodeName, "localDir" ) ) { onlyOne( c.localdir, "core", "localDir" ); readValidString( c.localdir, "local core directory" ); skipToEndElement( "localDir" ); break; } if( !strcmp( nodeName, "compilationDir" ) ) { onlyOne( c.compdir, "core", "compilationDir" ); readValidString( c.compdir, "relative compilation directory" ); skipToEndElement( "compilationDir" ); break; } if( !strcmp( nodeName, "program" ) ) { onlyOne( c.program, "core", "program" ); readValidString( c.program, "program name" ); skipToEndElement( "program" ); break; } printf( "Unexpected element %s in core %s\n", nodeName, c.name ); skipToEndElement( nodeName ); break; case 4 : if( !strcmp( nodeName, "offset" ) ) { onlyOne( c.offset, "file", "offset" ); readValidString( c.offset, "offset" ); char* end = NULL; f.ioffset = strtol( f.offset, &end, 10 ); if( end == f.offset || *end || f.ioffset < 0 || f >= f.isize ) quit( "Invalid file offset for file %s\n", f.name ); skipToEndElement( "offset" ); break; } printf( "Unexpected element %s in file %s\n", nodeName, f.name ); skipToEndElement( nodeName ); break; case 5 : if( !strcmp( nodeName, "role" ) ) { state = 2; bzero( r, sizeof( struct role ) ); readValidRequiredAttribute( r.type, "role", "type" ); if( strcmp( r.type, "seed" ) && strcmp( r.type, "leech" ) ) quit( "Invalid value for attribute 'type' for role on line %d, expected 'seed' or 'leech'\n", xmlTextReaderGetParserLineNumber( xmlRead ) ); break; } quit( "Unexpected element %s in test on line %d\n", nodeName, xmlTextReaderGetParserLineNumber( xmlRead ) ); default : quit( "State sanity\n", nodeName ); } break; case XmlNodeType.EndElement : nodeName = xmlTextReaderLocalName( xmlRead ); switch( state ) { case 0 : quit( "End element %s found while not in entity\n", nodeName ); case 1 : if( !strcmp( nodeName, "machine" ) ) { memcpy( machines + machineCount, &m, sizeof( struct machine ) ); machineCount++; state = 0; } break; case 2 : if( !strcmp( nodeName, "role" ) ) { mempcy( t.roles + r.roleCount, &r, sizeof( struct role ) ); t.roleCount++; state = 5; } break; case 3 : if( !strcmp( nodeName, "core" ) ) { memcpy( cores + coreCount, &c, sizeof( struct core ) ); coreCount++; state = 0; } break; case 4 : if( !strcmp( nodeName, "file" ) ) { memcpy( files + fileCount, &f, sizeof( struct file ) ); fileCount++; state = 0; } break; case 5 : if( !strcmp( nodeName, "test" ) ) { memcpy( tests + testCount, &t, sizeof( struct test ) ); testCount++; state = 0; } break; default : quit( "State sanity\n" ); } default : } if( nodeName ) free( nodeName ); } } void validateConfigAndGenerateScripts( ) { int i, j, k, l; bool found; // == Validate test-role references to machines, cores and files // == Also register which machine uses which cores and which files for( i = 0; i < testCount; i++ ) { for( j = 0; j < tests[i].roleCount; j++ ) { // Check for validity of each role's core found = false; for( k = 0; k < coreCount; k++ ) { if( !strcmp( cores[k].name, tests[i].roles[j].core ) ) { found = true; break; } } if( !found ) quit( "Test %s role %i refers to core %s which does not exist\n", tests[i].name, j, tests[i].roles[j].core ); // Check for validity of each role's file found = false; for( k = 0; k < fileCount; k++ ) { if( !strcmp( files[k].name, tests[i].roles[j].file ) ) { found = true; break; } } if( !found ) quit( "Test %s role %i refers to file %s which does not exist\n", tests[i].name, j, tests[i].roles[j].file ); // Check for validity of each role's machines for( l = 0; l < tests[i].roles[j].machineCount; l++ ) { found = false; for( k = 0; k < machineCount; k++ ) { if( !strcmp( machines[k].name, tests[i].roles[j].machines[l] ) ) { // Machine found: register used core with the machine if needed for( m = 0; m < machines[k].coreCount; m++ ) { if( !strcmp( machines[k].cores[m], tests[i].roles[j].core ) ) { found = true; break; } } if( !found ) machines[k].cores[machines[k].coreCount++] = tests[i].roles[j].core; // Machine found: register used file with the machine if needed, only if seeding if( !strcmp( tests[i].roles[j].type, "seed" ) ) { found = false; for( m = 0; m < machines[k].fileCount; m++ ) { if( !strcmp( machines[k].files[m], tests[i].roles[j].file ) ) { found = true; break; } } if( !found ) machines[k].files[machines[k].fileCount++] = tests[i].roles[j].file; } // Machine found found = true; break; } } if( !found ) quit( "Test %s role %i refers to machine %s which does not exist\n", tests[i].name, j, tests[i].roles[j].machines[l] ); } } } // Create temporary script FILE* script = tmpfile(); if( !script ) quit( "Can't create temporary script\n" ); fprintf( script, "#!/bin/bash\n" ); // == Check validity of machines and users: can each machine be accessed? for( i = 0; i < machineCount; i++ ) { // Creates a function in the script for sending command to the machine using ssh // Call using // ssh_machine_%i "commands" || cleanup -1 // where %i is the index of the machine in machines. Also be sure to return non-zero from your commands on error. fprintf( script, "function ssh_machine_%i {\n", i ); if( strcmp( machines[i].address, "DAS4" ) ) fprintf( script, " ssh -T -n -o BatchMode=yes -h \"%s\"", machines[i].address ); else fprintf( script, " ssh -T -n -o BatchMode=yes -h fs3.das4.tudelft.nl" ); if( machines[i].user ) fprintf( script, " -l \"%s\"", machines[i].user ); if( machines[i].params ) fprintf( script, " %s", machines[i].params ); fprintf( script, " $1 || return -1;\n" ); fprintf( script, "}\n" ); // Creates a function in the script for sending files to the machine using scp // Call using // scp_to_machine_%i localfile remotefile fprintf( script, "function scp_to_machine_%i {\n", i ); fprintf( script, "scp -o BatchMode=yes " ); if( machines[i].params ) fprintf( script, "%s ", machines[i].params ); fprintf( script, "$1 ", l ); if( machines[i].user ) fprintf( script, "\"%s\"@", machines[i].user ); if( strcmp( machines[i].address, "DAS4" ) ) fprintf( script, "\"%s\"", machines[i].address ); else fprintf( script, "fs3.das4.tudelft.nl" ); fprintf( script, ":$2 || cleanup -1\n", i, l ) fprintf( script, "}\n" ); // Creates a function in the script for retrieving files from the machine using scp // Call using // scp_from_machine_%i remotefile localfile fprintf( script, "function scp_from_machine_%i {\n", i ); fprintf( script, "scp -o BatchMode=yes " ); if( machines[i].params ) fprintf( script, "%s ", machines[i].params ); if( machines[i].user ) fprintf( script, "\"%s\"@", machines[i].user ); if( strcmp( machines[i].address, "DAS4" ) ) fprintf( script, "\"%s\"", machines[i].address ); else fprintf( script, "fs3.das4.tudelft.nl" ); fprintf( script, ":$1 $2 || cleanup -1\n", i, l ) fprintf( script, "}\n" ); // Checks reachability of machine fprintf( script, "ssh_machine_%i || exit -1\n", i ); } // == Check validity of each core: can each core be packaged? Can it be compiled locally and does the program then exist? // Create a cleanup file. This file should have code appended to cleanup things when errors occur or testing has finished. See existing code for examples of concatenating to it. // The cleanup function is available after this as well. Call it with an exit argument to end the script cleanly. fprintf( script, "CLEANUPFILE=`mktemp`\n" ); fprintf( script, "chmod +x CLEANUPFILE\n" ); fprintf( script, "[ -x CLEANUPFILE ] || exit -1\n" ); fprintf( script, "function cleanup {\n" ); fprintf( script, " (\ncat <<EOL\nrm $CLEANUPFILE\nEOL\n) >> $CLEANUPFILE\n" ); fprintf( script, " . $CLEANUPFILE\n" ); fprintf( script " exit $1\n" ); fprintf( script, "}\n" ); // Create a local temporary directory for storage fprintf( script, "TARDIR=`mktemp -d`\n[ \"X${TARDIR}X\" == \"XX\" ] && exit -1\n" ); fprintf( script, "(\ncat <<EOL\n#!/bin/bash\nrm -rf $TARDIR\nEOL\n) >> $CLEANUPFILE\n" ); fprintf( script, "CURDIR=`pwd`\n" ); // Create a tarball for the testenvironment in the temporary local storage fprintf( script, "make clean || cleanup -1\n" ); fprintf( script, "tar cf ${TARDIR}/testenvironment.tar . || cleanup -1\n" ); fprintf( script, "bzip2 ${TARDIR}/testenvironment.tar || cleanup -1\n" ); // Check whether the needed tools of the testenvironment compile locally fprintf( script, "make genfakedata || cleanup -1\n" ); for( i = 0; i < coreCount; i++ ) { if( !cores[i].localdir ) cores[i].localdir = strdup( "../" ); // Create a tarball for the core in the temporary local storage fprintf( script, "cd %s || cleanup -1\n", cores[i].localdir ); fprintf( script, "make clean\n" ); // Not checked: SHOULD be available, but... fprintf( script, "tar cf ${TARDIR}/core_%i.tar . || cleanup -1\n", i ); fprintf( script, "bzip2 ${TARDIR}/core_%i.tar || cleanup -1\n", i ); if( !cores[i].compdir ) cores[i].compdir = strdup( "testenvironment/" ); // Check whether the core compiles locally and the program exists after fprintf( script, "cd %s || cleanup -1\n", cores[i].compdir ); fprintf( script, "make || cleanup -1\n" ); if( !cores[i].program ) cores[i].program = strdup( "swift" ); fprintf( script, "[ -x %s ] || cleanup -1\n", cores[i].program ); fprintf( script, "cd ${CURDIR}\n" ); } // For each file, precalculate the hash for( i = 0; i < fileCount; i++ ) { // Create some temporary file to write fake data to. These fake data files will be regenerated at each machine since generating is faster than copying. size_t size = files[i].isize; FILE* data = tmpfile(); if( !data ) quit( "can't create temporary data file\n" ); int datan = fileno( data ); int filesize; if( size > 512*1024*1024 ) filesize = 512*1024*1024; else filesize = size; if( generateFakeData( datan, filesize ) ) quit( "could not write fake data\n" ); MemoryHashStorage mhs; FileOffsetDataStorage fods( datan, files[i].ioffset ); if( !fods.valid() ) quit( "can't read back from temporary data file\n" ); HashTree ht( fods, Sha1Hash::ZERO, mhs ); Sha1Hash hash = ht.root_hash(); if( size > filesize ) { MemoryHashStorage mhs2; int max = size/filesize; for( j = 0; j < max; j++ ) mhs2.setHash( bin64(0,j), hash ); int lvl = 0; do { max >>= 1; lvl++; for( j = 0; j < max; j++ ) mhs2.hashLeftRight( bin64_t(lvl, j) ); } while( max > 1 ); files[i].hash = mhs.getHash( bin64_t(lvl, 0) ); } else
/*DEF*/void *XoReadXmlReader(/*xmlTextReaderPtr*/ void *preader,char *roottype,int flags,int *err) { xmlTextReaderPtr reader = (xmlTextReaderPtr)preader; int next; t_ctxt ctxt; int ret; *err = 0; memset (&ctxt,0,sizeof(ctxt)); ctxt.roottype = roottype; ctxt.flags = flags; ctxt.ns.flags = flags; _XoAllocCtxt(&ctxt); if ( C_PARSE_TARGET(&ctxt) ) { void *r; char *tag = "<dont_save_this_tag>"; r = _XoRootPush(NULL,&ctxt,NULL,tag,err); if (!r) { _XoFreeCtxt(&ctxt); return NULL; } _XoSetElementName(r,tag); *err = 0; } if ( !C_PARSE_TRACE(&ctxt) ) xmlTextReaderSetErrorHandler(reader,IgnoreTrace,NULL); ret = 1; next = xmlTextReaderRead(reader); while (next == 1 && ret > 0) { ctxt.depth = xmlTextReaderDepth(reader); ctxt.nodetype = xmlTextReaderNodeType(reader); ctxt.qname = (char *)xmlTextReaderName(reader); ctxt.value = (char *)xmlTextReaderValue(reader); switch (ctxt.nodetype) { case XML_READER_TYPE_ELEMENT : ctxt.isempty = xmlTextReaderIsEmptyElement(reader); ctxt.atcount = xmlTextReaderAttributeCount(reader); FetchAttributes(reader,&ctxt); break; case XML_READER_TYPE_TEXT : break; } ret = _XoProcessNode(reader,&ctxt); if (ctxt.atcount) _XoFreeAttributesWith(&ctxt,xmlFree); if (ctxt.qname) xmlFree(ctxt.qname); if (ctxt.value) xmlFree(ctxt.value); if (ret < 0) { ctxt.line = xmlTextReaderGetParserLineNumber(reader); goto errload; } next = xmlTextReaderRead(reader); if ( C_PARSE_INTERACTIF(&ctxt) ) { int c; switch(ctxt.prevnode) { case XML_READER_TYPE_ELEMENT : case XML_READER_TYPE_TEXT : case XML_READER_TYPE_END_ELEMENT : _XoDumpStack(&ctxt); fflush(stdin); c = getchar(); if (c == 'p') { void *o; o = ctxt.obj[ctxt.level-1]; XoSaveAscii(o,stdout); } break; } } } if (next != 0) // parsing error, get line number { *err = xmlTextReaderGetParserLineNumber(reader); } if (next != 0) { if ( C_PARSE_TRACE(&ctxt) ) XOML_TRACE("XoReadXmlReader() : failed to parse next=%d ret=%d line=%d\n", next,ret,*err); if (ctxt.obj[0]) XoFree(ctxt.obj[0],1); _XoFreeCtxt(&ctxt); return NULL; } errload : if (ret < 0) { *err = ret; if ( C_PARSE_TRACE(&ctxt) ) XOML_TRACE("XoReadXmlReader() : failed to load next=%d ret=%d line=%d\n", next,ret,ctxt.line); if (ctxt.obj[0]) XoFree(ctxt.obj[0],1); _XoFreeCtxt(&ctxt); return NULL; } _XoFreeCtxt(&ctxt); return ctxt.obj[0]; }
static GstylePalette * gstyle_palette_new_from_xml (GFile *file, GCancellable *cancellable, GError **error) { g_autoptr(GInputStream) stream = NULL; g_autofree gchar *uri = NULL; GstylePalette *palette = NULL; xmlTextReaderPtr reader; GError *tmp_error = NULL; gboolean has_colors = FALSE; gint ret = -1; g_assert (G_IS_FILE (file)); uri = g_file_get_uri (file); if (!(stream = G_INPUT_STREAM (g_file_read (file, cancellable, &tmp_error)))) goto finish; reader = xmlReaderForIO (gstyle_palette_io_read_cb, gstyle_palette_io_close_cb, stream, uri, NULL, XML_PARSE_RECOVER | XML_PARSE_NOBLANKS | XML_PARSE_COMPACT); if (reader != NULL) { GstyleColor *color; g_autofree gchar *id = NULL; g_autofree gchar *name = NULL; g_autofree gchar *domain = NULL; xmlTextReaderSetErrorHandler (reader, gstyle_palette_error_cb, NULL); if (xmlTextReaderRead(reader) && gstyle_palette_xml_get_header (reader, &id, &name, &domain)) { palette = g_object_new (GSTYLE_TYPE_PALETTE, "id", id, "domain", domain, "name", name, "file", file, NULL); ret = xmlTextReaderRead(reader); while (ret == 1) { if (xmlTextReaderNodeType (reader) == XML_READER_TYPE_END_ELEMENT) { ret = 0; break; } /* TODO: better naming */ color = gstyle_palette_xml_get_color (reader); if (color == NULL) { ret = -1; break; } gstyle_palette_add (palette, color, &tmp_error); g_object_unref (color); has_colors = TRUE; ret = xmlTextReaderRead(reader); } } if (ret != 0 || !has_colors) { g_clear_object (&palette); g_set_error (&tmp_error, GSTYLE_PALETTE_ERROR, GSTYLE_PALETTE_ERROR_PARSE, _("%s: failed to parse\n"), uri); } xmlTextReaderClose(reader); xmlFreeTextReader(reader); } else g_set_error (&tmp_error, GSTYLE_PALETTE_ERROR, GSTYLE_PALETTE_ERROR_FILE, _("Unable to open %s\n"), uri); finish: if (tmp_error) g_propagate_error (error, tmp_error); return palette; }