bool diff_rename(project_t::ref oldproj, project_t *nproj) { const std::string &diff_name = project_diff_name(oldproj.get()); if(unlikely(diff_name.empty())) return false; fdguard difffd(oldproj->dirfd, diff_name.c_str(), O_RDONLY); /* parse the file and get the DOM */ xmlDocGuard doc(xmlReadFd(difffd, nullptr, nullptr, XML_PARSE_NONET)); if(unlikely(!doc)) { error_dlg(trstring("Error: could not parse file %1\n").arg(diff_name)); return false; } for (xmlNode *cur_node = xmlDocGetRootElement(doc.get()); cur_node != nullptr; cur_node = cur_node->next) { if (cur_node->type == XML_ELEMENT_NODE) { if(likely(strcmp(reinterpret_cast<const char *>(cur_node->name), "diff") == 0)) { xmlSetProp(cur_node, BAD_CAST "name", BAD_CAST nproj->name.c_str()); break; } } } xmlSaveFormatFileEnc((nproj->path + diff_filename(nproj)).c_str(), doc.get(), "UTF-8", 1); return true; }
/** * \brief Initiate internal configuration file - open, get xmlDoc and prepare * XPathContext. Also register namespace "urn:cesnet:params:xml:ns:yang:ipfixcol-internals" * with given namespace name. * * @param[in] ns_name Name for the "urn:cesnet:params:xml:ns:yang:ipfixcol-internals" * namespace in xpath queries. * @return XPath Context for the internal XML configuration. Caller will need to * free it including separated free of return->doc. */ static xmlXPathContextPtr ic_init(xmlChar* ns_name, char *internal_cfg) { int fd; xmlDocPtr doc = NULL; xmlXPathContextPtr ctxt = NULL; /* open and prepare internal XML configuration file */ if ((fd = open(internal_cfg, O_RDONLY)) == -1) { MSG_ERROR(msg_module, "Unable to open internal configuration file %s (%s)", internal_cfg, strerror(errno)); return (NULL); } if ((doc = xmlReadFd(fd, NULL, NULL, XML_PARSE_NOERROR | XML_PARSE_NOWARNING | XML_PARSE_NOBLANKS)) == NULL) { MSG_ERROR(msg_module, "Unable to parse internal configuration file %s", internal_cfg); close(fd); return (NULL); } close(fd); /* create xpath evaluation context of internal configuration file */ if ((ctxt = xmlXPathNewContext(doc)) == NULL) { MSG_ERROR(msg_module, "Unable to create XPath context for internal configuration (%s:%d)", __FILE__, __LINE__); xmlFreeDoc(doc); return (NULL); } /* register namespace for the context of internal configuration file */ if (xmlXPathRegisterNs (ctxt, ns_name, BAD_CAST "urn:cesnet:params:xml:ns:yang:ipfixcol-internals") != 0) { MSG_ERROR(msg_module, "Unable to register namespace for internal configuration file (%s:%d)", __FILE__, __LINE__); xmlXPathFreeContext(ctxt); xmlFreeDoc(doc); return (NULL); } return (ctxt); }
/** * \brief Initialize the iterator of IPFIX elements over a XML document * * After success intilialization call #elem_iter_next to get first element. * \param[in] fd An opened file descriptor of XML document * \return On success returns pointer to the initialized iterator. Otherwise * returns NULL. */ static struct elem_xml_iter *elem_iter_init(int fd) { struct elem_xml_iter *iter; iter = (struct elem_xml_iter *) calloc(1, sizeof(struct elem_xml_iter)); if (!iter) { MSG_ERROR(msg_module, "CALLOC FAILED! (%s:%d)", __FILE__, __LINE__); return NULL; } // Load XML document iter->doc = xmlReadFd(fd, NULL, NULL, XML_PARSE_NOERROR | XML_PARSE_NOWARNING | XML_PARSE_NOBLANKS | XML_PARSE_BIG_LINES); if (!iter->doc) { MSG_ERROR(msg_module, "Unable to parse XML document with IPFIX " " elements."); free(iter); return NULL; } // Get XPath context and evaluate expression iter->xpath_ctx = xmlXPathNewContext(iter->doc); if (!iter->xpath_ctx) { MSG_ERROR(msg_module, "Unable to get XPath context of XML document " "with IPFIX elements."); xmlFreeDoc(iter->doc); free(iter); return NULL; } iter->xpath_obj = xmlXPathEvalExpression(BAD_CAST ELEM_XML_XPATH, iter->xpath_ctx); if (!iter->xpath_obj) { MSG_ERROR(msg_module, "Unable to evaluate XPath expression."); xmlXPathFreeContext(iter->xpath_ctx); xmlFreeDoc(iter->doc); free(iter); return NULL; } if (xmlXPathNodeSetIsEmpty(iter->xpath_obj->nodesetval)) { MSG_ERROR(msg_module, "No IPFIX elements in XML document."); xmlXPathFreeObject(iter->xpath_obj); xmlXPathFreeContext(iter->xpath_ctx); xmlFreeDoc(iter->doc); free(iter); return NULL; } return iter; }
int xml2lpc_set_xsd_fd(xml2lpc_context* xmlCtx, int fd) { xml2lpc_context_clear_logs(xmlCtx); xmlSetGenericErrorFunc(xmlCtx, xml2lpc_genericxml_error); if(xmlCtx->xsd != NULL) { xmlFreeDoc(xmlCtx->xsd); xmlCtx->xsd = NULL; } xmlCtx->xsd = xmlReadFd(fd, 0, NULL, 0); if(xmlCtx->xsd == NULL) { xml2lpc_log(xmlCtx, XML2LPC_ERROR, "Can't open/parse fd \"%d\"", fd); xml2lpc_log(xmlCtx, XML2LPC_ERROR, "%s", xmlCtx->errorBuffer); return -1; } return 0; }
JBoolean JGetSVNEntryType ( const JCharacter* url, JString* type, JString* error ) { type->Clear(); error->Clear(); JString cmd = "svn info --xml " + JPrepArgForExec(url); JProcess* p; int fromFD, errFD; JError err = JProcess::Create(&p, cmd, kJIgnoreConnection, NULL, kJCreatePipe, &fromFD, kJCreatePipe, &errFD); if (!err.OK()) { err.ReportIfError(); return kJFalse; } p->WaitUntilFinished(); if (p->SuccessfulFinish()) { xmlDoc* doc = xmlReadFd(fromFD, NULL, NULL, XML_PARSE_NOBLANKS | XML_PARSE_NOCDATA); close(fromFD); if (doc != NULL) { xmlNode* root = xmlDocGetRootElement(doc); if (root != NULL && strcmp((char*) root->name, "info") == 0 && strcmp((char*) root->children->name, "entry") == 0) { *type = JGetXMLNodeAttr(root->children, "kind"); return kJTrue; } } } JReadAll(errFD, error, kJTrue); return kJFalse; }
/* Run an XPath query on XML on stdin, print results to stdout. */ static void do_xpath (const char *query) { CLEANUP_XMLFREEDOC xmlDocPtr doc = NULL; CLEANUP_XMLXPATHFREECONTEXT xmlXPathContextPtr xpathCtx = NULL; CLEANUP_XMLXPATHFREEOBJECT xmlXPathObjectPtr xpathObj = NULL; xmlNodeSetPtr nodes; char *r; size_t i; xmlSaveCtxtPtr saveCtx; xmlNodePtr wrnode; doc = xmlReadFd (STDIN_FILENO, NULL, "utf8", XML_PARSE_NOBLANKS); if (doc == NULL) { fprintf (stderr, _("%s: unable to parse XML from stdin\n"), guestfs_int_program_name); exit (EXIT_FAILURE); } xpathCtx = xmlXPathNewContext (doc); if (xpathCtx == NULL) { fprintf (stderr, _("%s: unable to create new XPath context\n"), guestfs_int_program_name); exit (EXIT_FAILURE); } xpathObj = xmlXPathEvalExpression (BAD_CAST query, xpathCtx); if (xpathObj == NULL) { fprintf (stderr, _("%s: unable to evaluate XPath expression\n"), guestfs_int_program_name); exit (EXIT_FAILURE); } switch (xpathObj->type) { case XPATH_NODESET: nodes = xpathObj->nodesetval; if (nodes == NULL) break; saveCtx = xmlSaveToFd (STDOUT_FILENO, NULL, XML_SAVE_NO_DECL | XML_SAVE_FORMAT); if (saveCtx == NULL) { fprintf (stderr, _("%s: xmlSaveToFd failed\n"), guestfs_int_program_name); exit (EXIT_FAILURE); } for (i = 0; i < (size_t) nodes->nodeNr; ++i) { CLEANUP_XMLFREEDOC xmlDocPtr wrdoc = xmlNewDoc (BAD_CAST "1.0"); if (wrdoc == NULL) { fprintf (stderr, _("%s: xmlNewDoc failed\n"), guestfs_int_program_name); exit (EXIT_FAILURE); } wrnode = xmlCopyNode (nodes->nodeTab[i], 1); if (wrnode == NULL) { fprintf (stderr, _("%s: xmlCopyNode failed\n"), guestfs_int_program_name); exit (EXIT_FAILURE); } xmlDocSetRootElement (wrdoc, wrnode); if (xmlSaveDoc (saveCtx, wrdoc) == -1) { fprintf (stderr, _("%s: xmlSaveDoc failed\n"), guestfs_int_program_name); exit (EXIT_FAILURE); } } xmlSaveClose (saveCtx); break; case XPATH_STRING: r = (char *) xpathObj->stringval; printf ("%s", r); i = strlen (r); if (i > 0 && r[i-1] != '\n') printf ("\n"); break; case XPATH_UNDEFINED: /* grrrrr ... switch-enum is a useless warning */ case XPATH_BOOLEAN: case XPATH_NUMBER: case XPATH_POINT: case XPATH_RANGE: case XPATH_LOCATIONSET: case XPATH_USERS: case XPATH_XSLT_TREE: default: r = (char *) xmlXPathCastToString (xpathObj); printf ("%s\n", r); free (r); } }
int main (void) { char *formName, *pid, *username, *newPid; boolean found_patient; FILE *document; /* libcgi declarations */ formvars *first_input, *input; /* libxml2 declarations */ xmlChar *strUTF = NULL; xmlDocPtr doc; xmlNodePtr root_element, cur_node, new_form, new_node, edited_patient, old_patient; /* * this initialize the library and check potential ABI mismatches * between the version it was compiled for and the actual shared * library used. */ LIBXML_TEST_VERSION /****************************************************************************** * READ CONTENT STRING FROM SERVER. * * PROCESS DATA. * * CREATE A LINKED LIST. * ******************************************************************************/ cgi_init(); first_input = cgi_process_form(); newPid = cgi_param("numeroGeral"); pid = cgi_param("antigoNumeroGeral"); formName = cgi_param("form"); //username = cgi_param("uid"); if(!(username= getenv("REMOTE_USER"))) //verifica se string lida é null { printf("Content-type: text/html\n\n"); printf("<html>\n"); printf("<head>\n"); printf("<title>Resultado</title>\n"); printf("</head>\n"); printf("<body>\n"); printf("Erro ao verificar o usuário."); printf("</body>\n"); printf("</html>\n"); exit(0); } if ((!pid) || (!formName)) { printError("ID do paciente e nome do formulário não foram enviados"); usualFreeMemory(NULL); exit(0); } /****************************************************************************** * OPENING AND PARSING AN XML FILE TO A TREE * ******************************************************************************/ document = fopen(XML_FILE_PATH, "r+"); if (document == NULL) { printError("O arquivo de pacientes não pode ser aberto"); exit(0); } if(flock(fileno(document), LOCK_EX)) { printError("Erro ao trancar o arquivo"); fclose(document); exit(0); } //printWait("Trancado!"); //sleep(20); doc = xmlReadFd(fileno(document), XML_FILE_PATH, NULL, XML_PARSE_NOBLANKS); if (doc == NULL) { printError("Failed to parse doc"); usualFreeMemory(NULL); flock(fileno(document), LOCK_EX); fclose(document); exit(0); } root_element = xmlDocGetRootElement(doc); /****************************************************************************** * SEARCH THE SAME <numeroGeral> * ******************************************************************************/ cur_node = root_element->children; /* Get the node <paciente> if file isn't empty */ if (!cur_node) //test!!!! printf!! pode-se tb na remocao exigir a remocao do arquivo se o paciente excluido for o unico do arquivo found_patient = false; else { /* looping through patients looking for the right patient */ for (found_patient = false; ((!found_patient) && (cur_node)); cur_node = cur_node->next) { cur_node = cur_node->children; /* <triagem> */ cur_node = cur_node->children; /* <numeroGeral> ? */ while ((!xmlStrEqual(cur_node->name, BAD_CAST "numeroGeral")) && (cur_node)) cur_node = cur_node->next; if (xmlStrEqual(cur_node->name, BAD_CAST "numeroGeral")) { if (xmlStrEqual(cur_node->children->content, BAD_CAST pid)) { found_patient = true; old_patient = cur_node->parent; /*old_paciente recebe o noh <triagem> do paciente que possui o numeroGeral procurado */ } else { cur_node = cur_node->parent; /* <triagem> */ cur_node = cur_node->parent; /* <paciente> */ } } else { printError("Erro de formação do XML. Tem paciente sem número geral."); usualFreeMemory(doc); flock(fileno(document), LOCK_EX); fclose(document); exit(0); } } } /****************************************************************************** * CHECK IF PATIENT WAS FOUND * * IF TRUE, EXIT * ******************************************************************************/ if (!found_patient) { printError("Este paciente não existe."); usualFreeMemory(doc); flock(fileno(document), LOCK_EX); fclose(document); exit(0); } /****************************************************************************** * Verificando se o novo numero geral jah existe * *******************************************************************************/ cur_node = root_element->children; /* Get the node <paciente> if file isn't empty */ if (!cur_node) //test!!!! printf!! pode-se tb na remocao exigir a remocao do arquivo se o paciente excluido for o unico do arquivo found_patient = false; else { /* looping through patients looking for the right patient */ for (found_patient = false; ((!found_patient) && (cur_node)); cur_node = cur_node->next) { cur_node = cur_node->children; /* <triagem> */ cur_node = cur_node->children; /* <numeroGeral> ? */ while ((!xmlStrEqual(cur_node->name, BAD_CAST "numeroGeral")) && (cur_node)) cur_node = cur_node->next; if (xmlStrEqual(cur_node->name, BAD_CAST "numeroGeral")) { if (xmlStrEqual(cur_node->children->content, BAD_CAST newPid)) { found_patient = true; old_patient = cur_node->parent; /*old_paciente recebe o noh <triagem> do paciente que possui o numeroGeral procurado */ } else { cur_node = cur_node->parent; /* <triagem> */ cur_node = cur_node->parent; /* <paciente> */ } } else { printError("Erro de formação do XML. Tem paciente sem número geral."); usualFreeMemory(doc); flock(fileno(document), LOCK_EX); fclose(document); exit(0); } } } /****************************************************************************** * Comfirmando se novo numero geral jah existe * *******************************************************************************/ if((found_patient) && (strcmp(newPid,pid) != 0)) { printError("O novo número geral escolhido já existe."); usualFreeMemory(doc); flock(fileno(document), LOCK_EX); fclose(document); exit(0); } /****************************************************************************** * CRIANDO NOVO FORMULARIO DE TRIAGEM * ******************************************************************************/ edited_patient = xmlNewNode (NULL,BAD_CAST "triagem"); /****************************************************************************** * ADD NEW FORM * ******************************************************************************/ strUTF = fixCgiStr(BAD_CAST formName); if (!strUTF) { cgi_init_headers(); printError("Erro na conversão de formName para UTF-8"); usualFreeMemory(doc); flock(fileno(document), LOCK_EX); fclose(document); exit(0); } new_form = xmlNewChild (edited_patient, NULL, BAD_CAST strUTF, NULL); free(strUTF);//frees formName for (input = first_input; input; input = input->next) { if (!strcmp(input->name,"form")) input = input->next; if (!strcmp(input->name,"uid")) input = input->next; if (!strcmp(input->name,"antigoNumeroGeral")) input = input->next; /* Validate tag name input against UTF-8 */ strUTF = fixCgiStr((unsigned char *)input->name); if (!strUTF) { printError("Erro na conversão de input->name para UTF-8"); usualFreeMemory(doc); flock(fileno(document), LOCK_EX); fclose(document); exit(0); } new_node = xmlNewNode (NULL, strUTF); free(strUTF);//frees input->name /* Validate tag value input against UTF-8 */ strUTF = fixCgiStr((unsigned char *)input->value); if (!strUTF) { printError("Erro na conversão de input->value para UTF-8"); usualFreeMemory(doc); flock(fileno(document), LOCK_EX); fclose(document); exit(0); } xmlNodeAddContent (new_node, strUTF); free(strUTF);//frees input->value xmlAddChild (new_form, new_node); } /****************************************************************************** * Substituindo o noh antigo pelo novo * ******************************************************************************/ xmlReplaceNode (old_patient, edited_patient->children); xmlFreeNode (old_patient); /****************************************************************************** * DUMPING DOCUMENT TO FILE * ******************************************************************************/ if ((xmlSaveFormatFileEnc(XML_FILE_PATH, doc, "ISO-8859-1", 1)) < 0) { printError("Erro ao salvar arquivo"); usualFreeMemory(doc); xmlFreeNode(edited_patient); flock(fileno(document), LOCK_EX); fclose(document); exit(0); } /*remove(XML_FILE_PATH); if (rename(XML_TEMP_FILE, XML_FILE_PATH)) { printError("Erro ao renomear o arquivo atualizado"); usualFreeMemory(doc); exit(0); }*/ /****************************************************************************** * FREE MEMORY AND EXIT * ******************************************************************************/ printSuccess(username); usualFreeMemory(doc); xmlFreeNode(edited_patient); flock(fileno(document), LOCK_EX); fclose(document); autoBackup(); return 0; }
unsigned int project_t::diff_restore() { const std::string &diff_name = project_diff_name(this); if(diff_name.empty()) { printf("no diff present!\n"); return DIFF_NONE_PRESENT; } fdguard difffd(dirfd, diff_name.c_str(), O_RDONLY); /* parse the file and get the DOM */ xmlDocGuard doc(xmlReadFd(difffd, nullptr, nullptr, XML_PARSE_NONET)); if(unlikely(!doc)) { error_dlg(trstring("Error: could not parse file %1\n").arg(diff_name)); return DIFF_INVALID; } printf("diff %s found, applying ...\n", diff_name.c_str()); unsigned int res = DIFF_RESTORED; for (xmlNode *cur_node = xmlDocGetRootElement(doc.get()); cur_node != nullptr; cur_node = cur_node->next) { if (cur_node->type == XML_ELEMENT_NODE) { if(strcmp(reinterpret_cast<const char *>(cur_node->name), "diff") == 0) { xmlString str(xmlGetProp(cur_node, BAD_CAST "name")); if(!str.empty()) { const char *cstr = str; printf("diff for project %s\n", cstr); if(unlikely(name != cstr)) { warning_dlg(trstring("Diff name (%1) does not match project name (%2)").arg(cstr).arg(name)); res |= DIFF_PROJECT_MISMATCH; } } for(xmlNodePtr node_node = cur_node->children; node_node != nullptr; node_node = node_node->next) { if(node_node->type == XML_ELEMENT_NODE) { if(strcmp(reinterpret_cast<const char *>(node_node->name), node_t::api_string()) == 0) diff_restore_node(node_node, osm); else if(strcmp(reinterpret_cast<const char *>(node_node->name), way_t::api_string()) == 0) diff_restore_way(node_node, osm); else if(likely(strcmp(reinterpret_cast<const char *>(node_node->name), relation_t::api_string()) == 0)) diff_restore_relation(node_node, osm); else { printf("WARNING: item %s not restored\n", node_node->name); res |= DIFF_ELEMENTS_IGNORED; } } } } } } /* check for hidden ways and update menu accordingly */ if(osm->hasHiddenWays()) res |= DIFF_HAS_HIDDEN; return res; }