Exemple #1
0
bool Quests::parseQuestNode(xmlNodePtr p, bool checkDuplicate)
{
	if(xmlStrcmp(p->name, (const xmlChar*)"quest"))
		return false;

	int32_t intValue;
	std::string strValue;

	uint32_t id = m_lastId;
	if(readXMLInteger(p, "id", intValue) && id > 0)
	{
		id = intValue;
		if(id > m_lastId)
			m_lastId = id;
	}

	std::string name;
	if(readXMLString(p, "name", strValue))
		name = strValue;

	uint32_t startStorageId = 0;
	if(readXMLInteger(p, "startstorageid", intValue) || readXMLInteger(p, "storageId", intValue))
		startStorageId = intValue;

	int32_t startStorageValue = 0;
	if(readXMLInteger(p, "startstoragevalue", intValue) || readXMLInteger(p, "storageValue", intValue))
		startStorageValue = intValue;

	Quest* quest = new Quest(name, id, startStorageId, startStorageValue);
	if(!quest)
		return false;

	for(xmlNodePtr missionNode = p->children; missionNode; missionNode = missionNode->next)
	{
		if(xmlStrcmp(missionNode->name, (const xmlChar*)"mission"))
			continue;

		std::string missionName, missionState;
		if(readXMLString(missionNode, "name", strValue))
			missionName = strValue;

		if(readXMLString(missionNode, "state", strValue) || readXMLString(missionNode, "description", strValue))
			missionState = strValue;

		uint32_t storageId = 0;
		if(readXMLInteger(missionNode, "storageid", intValue) || readXMLInteger(p, "storageId", intValue))
			storageId = intValue;

		int32_t startValue = 0, endValue = 0;
		if(readXMLInteger(missionNode, "startvalue", intValue) || readXMLInteger(p, "startValue", intValue))
			startValue = intValue;

		if(readXMLInteger(missionNode, "endvalue", intValue) || readXMLInteger(p, "endValue", intValue))
			endValue = intValue;

		if(Mission* mission = new Mission(missionName, missionState, storageId, startValue, endValue))
		{
			if(missionState.empty())
			{
				// parse sub-states only if main is not set
				for(xmlNodePtr stateNode = missionNode->children; stateNode; stateNode = stateNode->next)
				{
					if(xmlStrcmp(stateNode->name, (const xmlChar*)"missionstate"))
						continue;

					uint32_t missionId;
					if(!readXMLInteger(stateNode, "id", intValue))
					{
						std::clog << "[Warning - Quests::parseQuestNode] Missing missionId for mission state" << std::endl;
						continue;
					}

					missionId = intValue;
					std::string description;
					if(readXMLString(stateNode, "description", strValue))
						description = strValue;

					mission->newState(missionId, description);
				}
			}

			quest->newMission(mission);
		}
	}

	if(checkDuplicate)
	{
		for(QuestList::iterator it = quests.begin(); it != quests.end(); ++it)
		{
			if((*it)->getName() == name)
				delete *it;
		}
	}

	m_lastId++;
	quests.push_back(quest);
	return true;
}
Exemple #2
0
static int ooxml_basic_json(int fd, cli_ctx *ctx, const char *key)
{
    int ret = CL_SUCCESS;
    const xmlChar *stack[OOXML_JSON_RECLEVEL];
    json_object *summary, *wrkptr;
    int type, rlvl = 0;
    int32_t val2;
    const xmlChar *name, *value;
    xmlTextReaderPtr reader = NULL;

    cli_dbgmsg("in ooxml_basic_json\n");

    reader = xmlReaderForFd(fd, "properties.xml", NULL, 0);
    if (reader == NULL) {
        cli_dbgmsg("ooxml_basic_json: xmlReaderForFd error for %s\n", key);
        return CL_SUCCESS; // libxml2 failed!
    }

    summary = json_object_new_object();
    if (NULL == summary) {
        cli_errmsg("ooxml_basic_json: no memory for json object.\n");
        ret = CL_EFORMAT;
        goto ooxml_basic_exit;
    }

    while (xmlTextReaderRead(reader) == 1) {
        name = xmlTextReaderConstLocalName(reader);
        value = xmlTextReaderConstValue(reader);
        type = xmlTextReaderNodeType(reader);

        cli_dbgmsg("%s [%i]: %s\n", name, type, value);

        switch (type) {
        case XML_READER_TYPE_ELEMENT:
            stack[rlvl] = name;
            rlvl++;
            break;
        case XML_READER_TYPE_TEXT:
            {
                wrkptr = summary;
                if (rlvl > 2) { /* 0 is root xml object */
                    int i;
                    for (i = 1; i < rlvl-1; ++i) {
                        json_object *newptr = json_object_object_get(wrkptr, stack[i]);
                        if (!newptr) {
                            newptr = json_object_new_object();
                            if (NULL == newptr) {
                                cli_errmsg("ooxml_basic_json: no memory for json object.\n");
                                ret = CL_EMEM;
                                goto ooxml_basic_exit;
                            }
                            json_object_object_add(wrkptr, stack[i], newptr);
                        }
                        else {
                            /* object already exists */
                            if (!json_object_is_type(newptr, json_type_object)) {
                                cli_warnmsg("ooxml_content_cb: json object already exists as not an object\n");
                                ret = CL_EFORMAT;
                                goto ooxml_basic_exit;
                            } 
                        }
                        wrkptr = newptr;
                        cli_dbgmsg("stack %d: %s\n", i, stack[i]);
                    }
                }
                
                if (ooxml_is_int(value, xmlStrlen(value), &val2)) {
                    ret = cli_jsonint(wrkptr, stack[rlvl-1], val2);
                }
                else if (!xmlStrcmp(value, "true")) {
                    ret = cli_jsonbool(wrkptr, stack[rlvl-1], 1);
                }
                else if (!xmlStrcmp(value, "false")) {
                    ret = cli_jsonbool(wrkptr, stack[rlvl-1], 0);
                }
                else {
                    ret = cli_jsonstr(wrkptr, stack[rlvl-1], value);
                }

                if (ret != CL_SUCCESS)
                    goto ooxml_basic_exit;
            }
            break;
        case XML_READER_TYPE_END_ELEMENT:
            rlvl--;
            break;
        default:
            cli_dbgmsg("ooxml_content_cb: unhandled xml node %s [%i]: %s\n", name, type, value);
            ret = CL_EFORMAT;
            goto ooxml_basic_exit;
        }
    }

    json_object_object_add(ctx->wrkproperty, key, summary);

    if (rlvl != 0) {
        cli_warnmsg("ooxml_basic_json: office property file has unbalanced tags\n");
        /* FAIL */
    }

 ooxml_basic_exit:
    xmlTextReaderClose(reader);
    xmlFreeTextReader(reader);
    return ret;
}
Exemple #3
0
static int ooxml_content_cb(int fd, cli_ctx *ctx)
{
    int ret = CL_SUCCESS;
    int core=0, extn=0, cust=0, dsig=0;
    const xmlChar *name, *value, *CT, *PN;
    xmlTextReaderPtr reader = NULL;
    uint32_t loff;

    cli_dbgmsg("in ooxml_content_cb\n");

    reader = xmlReaderForFd(fd, "[Content_Types].xml", NULL, 0);
    if (reader == NULL) {
        cli_dbgmsg("ooxml_content_cb: xmlReaderForFd error for ""[Content_Types].xml""\n");
        return CL_SUCCESS; // libxml2 failed!
    }

    /* locate core-properties, extended-properties, and custom-properties (optional)  */
    while (xmlTextReaderRead(reader) == 1) {
        name = xmlTextReaderConstLocalName(reader);
        if (name == NULL) continue;

        if (strcmp(name, "Override")) continue;

        if (!xmlTextReaderHasAttributes(reader)) continue;

        CT = NULL; PN = NULL;
        while (xmlTextReaderMoveToNextAttribute(reader) == 1) {
            name = xmlTextReaderConstLocalName(reader);
            value = xmlTextReaderConstValue(reader);
            if (name == NULL || value == NULL) continue;

            if (!xmlStrcmp(name, "ContentType")) {
                CT = value;
            }
            else if (!xmlStrcmp(name, "PartName")) {
                PN = value;
            }

            cli_dbgmsg("%s: %s\n", name, value);
        }

        if (!CT && !PN) continue;

        if (!core && !xmlStrcmp(CT, "application/vnd.openxmlformats-package.core-properties+xml")) {
            /* default: /docProps/core.xml*/
            if (unzip_search(ctx, PN+1, xmlStrlen(PN)-1, &loff) != CL_VIRUS) {
                cli_dbgmsg("cli_process_ooxml: failed to find core properties file \"%s\"!\n", PN);
            }
            else {
                cli_dbgmsg("ooxml_content_cb: found core properties file \"%s\" @ %x\n", PN, loff);
                ret = unzip_single_internal(ctx, loff, ooxml_core_cb);
            }
            core = 1;
        }
        else if (!extn && !xmlStrcmp(CT, "application/vnd.openxmlformats-officedocument.extended-properties+xml")) {
            /* default: /docProps/app.xml */
            if (unzip_search(ctx, PN+1, xmlStrlen(PN)-1, &loff) != CL_VIRUS) {
                cli_dbgmsg("cli_process_ooxml: failed to find extended properties file \"%s\"!\n", PN);
            }
            else {
                cli_dbgmsg("ooxml_content_cb: found extended properties file \"%s\" @ %x\n", PN, loff);
                ret = unzip_single_internal(ctx, loff, ooxml_extn_cb);
            }
            extn = 1;
        }
        else if (!cust && !xmlStrcmp(CT, "application/vnd.openxmlformats-officedocument.custom-properties+xml")) {
            /* default: /docProps/custom.xml */
            if (unzip_search(ctx, PN+1, xmlStrlen(PN)-1, &loff) != CL_VIRUS) {
                cli_dbgmsg("cli_process_ooxml: failed to find custom properties file \"%s\"!\n", PN);
            }
            else {
                cli_dbgmsg("ooxml_content_cb: found custom properties file \"%s\" @ %x\n", PN, loff);
                /* custom properties ignored for now */
                cli_jsonbool(ctx->wrkproperty, "CustomProperties", 1);
                //ret = unzip_single_internal(ctx, loff, ooxml_cust_cb);
            }
            cust = 1;
        }
        else if (!dsig && !xmlStrcmp(CT, "application/vnd.openxmlformats-package.digital-signature-xmlsignature+xml")) {
            if (unzip_search(ctx, PN+1, xmlStrlen(PN)-1, &loff) != CL_VIRUS) {
                cli_dbgmsg("cli_process_ooxml: failed to find digital signature file \"%s\"!\n", PN);
            }
            else {
                cli_dbgmsg("ooxml_content_cb: found digital signature file \"%s\" @ %x\n", PN, loff);
                /* digital signatures ignored for now */
                cli_jsonbool(ctx->wrkproperty, "DigitalSignatures", 1);
                //ret = unzip_single_internal(ctx, loff, ooxml_dsig_cb);
            }
            dsig = 1;
        }

        if (ret != CL_SUCCESS)
            goto ooxml_content_exit;
    }

    if (!core) {
        cli_dbgmsg("cli_process_ooxml: file does not contain core properties file\n");
    }
    if (!extn) {
        cli_dbgmsg("cli_process_ooxml: file does not contain extended properties file\n");
    }
    if (!cust) {
        cli_dbgmsg("cli_process_ooxml: file does not contain custom properties file\n");
    }

 ooxml_content_exit:
    xmlTextReaderClose(reader);
    xmlFreeTextReader(reader);
    return ret;
}
Exemple #4
0
static void
initListValue(CompDisplay           *d,
              CompOptionValue       *v,
              CompOptionRestriction *r,
              CompActionState        state,
              Bool                   helper,
              xmlDocPtr              doc,
              xmlNodePtr             node)
{
   xmlNodePtr child;

   v->list.value = NULL;
   v->list.nValue = 0;

   if (!doc)
     return;

   for (child = node->xmlChildrenNode; child; child = child->next)
     {
        CompOptionValue *value;

        if (xmlStrcmp (child->name, BAD_CAST "value"))
          continue;

        value = realloc (v->list.value,
                         sizeof (CompOptionValue) * (v->list.nValue + 1));
        if (value)
          {
             switch (v->list.type) {
                case CompOptionTypeBool:
                  initBoolValue (&value[v->list.nValue], doc, child);
                  break;

                case CompOptionTypeInt:
                  initIntValue (&value[v->list.nValue], r, doc, child);
                  break;

                case CompOptionTypeFloat:
                  initFloatValue (&value[v->list.nValue], r, doc, child);
                  break;

                case CompOptionTypeString:
                  initStringValue (&value[v->list.nValue], r, doc, child);
                  break;

                case CompOptionTypeColor:
                  initColorValue (&value[v->list.nValue], doc, child);
                  break;

     /* case CompOptionTypeAction:
      *   initActionValue (d, &value[v->list.nValue], state, doc, child);
      *   break; */
                case CompOptionTypeMatch:
                  initMatchValue (d, &value[v->list.nValue], helper, doc, child);

                default:
                  break;
               }

             v->list.value = value;
             v->list.nValue++;
          }
     }
}
Exemple #5
0
static int ooxml_parse_element(xmlTextReaderPtr reader, json_object *wrkptr, int rlvl, int skip)
{
    const char *element_tag = NULL, *end_tag = NULL;
    const xmlChar *node_name = NULL, *node_value = NULL;
    json_object *njptr;
    int node_type, ret = CL_SUCCESS;
    int32_t val2;

    cli_dbgmsg("in ooxml_parse_element @ layer %d\n", rlvl);

    /* check recursion level */
    if (rlvl >= OOXML_JSON_RECLEVEL_MAX) {
        return CL_EMAXREC;
    }

    if (wrkptr == NULL) {
        skip = 1;
    }

    /* acquire element type */
    node_type = xmlTextReaderNodeType(reader);
    if (node_type != XML_READER_TYPE_ELEMENT) {
        cli_dbgmsg("ooxml_parse_element: first node typed %d, not %d\n", node_type, XML_READER_TYPE_ELEMENT);
        return CL_EPARSE; /* first type is not an element */
    }

    /* acquire element tag */
    node_name = xmlTextReaderConstLocalName(reader);
    if (!node_name) {
        cli_dbgmsg("ooxml_parse_element: element tag node nameless\n");
        return CL_EPARSE; /* no name, nameless */
    }
    element_tag = ooxml_check_key(node_name, xmlStrlen(node_name));
    if (!element_tag) {
        cli_dbgmsg("ooxml_parse_element: invalid element tag [%s]\n", node_name);
        skip = 1; /* skipping element */
        //return CL_EFORMAT; /* REMOVE */
    }

    /* handle attributes if you want */

    /* loop across all element contents */
    while (xmlTextReaderRead(reader) == 1) {
        node_type = xmlTextReaderNodeType(reader);
        switch (node_type) {
        case XML_READER_TYPE_ELEMENT:
            if (!skip) {
                njptr = json_object_object_get(wrkptr, element_tag);
                if (!njptr) {
                    njptr = json_object_new_object();
                    if (NULL == njptr) {
                        cli_errmsg("ooxml_basic_json: no memory for json object.\n");
                        return CL_EMEM;
                    }
                    cli_dbgmsg("ooxml_basic_json: added json object [%s]\n", element_tag);
                    json_object_object_add(wrkptr, element_tag, njptr);
                }
                else {
                    if (!json_object_is_type(njptr, json_type_object)) {
                        cli_warnmsg("ooxml_content_cb: json object [%s] already exists as not an object\n", element_tag);
                        return CL_EFORMAT;
                    }
                }
            }
            else {
                njptr = NULL;
            } 

            ret = ooxml_parse_element(reader, njptr, rlvl+1, skip);
            if (ret != CL_SUCCESS) {
                return ret;
            }
            break;
        case XML_READER_TYPE_END_ELEMENT:
            cli_dbgmsg("in ooxml_parse_element @ layer %d closed\n", rlvl);
            node_name = xmlTextReaderConstLocalName(reader);
            if (!node_name) {
                cli_dbgmsg("ooxml_parse_element: element end tag node nameless\n");
                return CL_EPARSE; /* no name, nameless */
            }
            if (!skip) {
                end_tag = ooxml_check_key(node_name, xmlStrlen(node_name));
                if (!end_tag) {
                    cli_dbgmsg("ooxml_parse_element: invalid element end tag [%s]\n", node_name);
                    return CL_EFORMAT; /* unrecognized element tag */
                }
                if (strncmp(element_tag, end_tag, strlen(element_tag))) {
                    cli_dbgmsg("ooxml_parse_element: element tag does not match end tag\n");
                    return CL_EFORMAT;
                }
            }
            return CL_SUCCESS;
        case XML_READER_TYPE_TEXT:
            if (!skip) {
                node_value = xmlTextReaderConstValue(reader);
                njptr = json_object_object_get(wrkptr, element_tag);
                if (njptr) {
                    cli_warnmsg("ooxml_parse_element: json object [%s] already exists\n", element_tag);
                }

                if (ooxml_is_int(node_value, xmlStrlen(node_value), &val2)) {
                    ret = cli_jsonint(wrkptr, element_tag, val2);
                }
                else if (!xmlStrcmp(node_value, "true")) {
                    ret = cli_jsonbool(wrkptr, element_tag, 1);
                }
                else if (!xmlStrcmp(node_value, "false")) {
                    ret = cli_jsonbool(wrkptr, element_tag, 0);
                }
                else {
                    ret = cli_jsonstr(wrkptr, element_tag, node_value);
                }

                if (ret != CL_SUCCESS)
                    return ret;

                cli_dbgmsg("ooxml_basic_json: added json value [%s: %s]\n", element_tag, node_value);
            }
            else {
                node_name = xmlTextReaderConstLocalName(reader);
                node_value = xmlTextReaderConstValue(reader);

                cli_dbgmsg("ooxml_parse_element: not adding xml node %s [%d]: %s\n", node_name, node_type, node_value);
            }
            break;
        default:
            node_name = xmlTextReaderConstLocalName(reader);
            node_value = xmlTextReaderConstValue(reader);

            cli_dbgmsg("ooxml_parse_element: unhandled xml node %s [%d]: %s\n", node_name, node_type, node_value);
            return CL_EPARSE;
        }
    }

    return CL_SUCCESS;
}
int main(int argc, char **argv)
{
    int opt, r = 0;
    char *alt_config = NULL, *pub = NULL, *ver = NULL, *winfile = NULL;
    char prefix[2048];
    enum { REBUILD, WINZONES, NONE } op = NONE;

    if ((geteuid()) == 0 && (become_cyrus(/*ismaster*/0) != 0)) {
        fatal("must run as the Cyrus user", EC_USAGE);
    }

    while ((opt = getopt(argc, argv, "C:r:vw:")) != EOF) {
        switch (opt) {
        case 'C': /* alt config file */
            alt_config = optarg;
            break;

        case 'r':
            if (op == NONE) {
                op = REBUILD;
                pub = optarg;
                ver = strchr(optarg, ':');
                if (ver) *ver++ = '\0';
                else usage();
            }
            else usage();
            break;

        case 'v':
            verbose = 1;
            break;

        case 'w':
            if (op == NONE) {
                op = WINZONES;
                winfile = optarg;
            }
            else usage();
            break;

        default:
            usage();
        }
    }

    cyrus_init(alt_config, "ctl_zoneinfo", 0, 0);

    signals_set_shutdown(&shut_down);
    signals_add_handlers(0);

    snprintf(prefix, sizeof(prefix), "%s%s", config_dir, FNAME_ZONEINFODIR);

    switch (op) {
    case REBUILD: {
        struct hash_table tzentries;
        struct zoneinfo *info;
        struct txn *tid = NULL;
        char buf[1024];
        FILE *fp;

        construct_hash_table(&tzentries, 500, 1);

        /* Add INFO record (overall lastmod and TZ DB source version) */
        info = xzmalloc(sizeof(struct zoneinfo));
        info->type = ZI_INFO;
        appendstrlist(&info->data, pub);
        appendstrlist(&info->data, ver);
        hash_insert(INFO_TZID, info, &tzentries);

        /* Add LEAP record (last updated and hash) */
        snprintf(buf, sizeof(buf), "%s%s", prefix, FNAME_LEAPSECFILE);
        if (verbose) printf("Processing leap seconds file %s\n", buf);
        if (!(fp = fopen(buf, "r"))) {
            fprintf(stderr, "Could not open leap seconds file %s\n", buf);
        }
        else {
            struct zoneinfo *leap = xzmalloc(sizeof(struct zoneinfo));
            leap->type = ZI_INFO;

            while(fgets(buf, sizeof(buf), fp)) {
                if (buf[0] == '#') {
                    /* comment line */

                    if (buf[1] == '$') {
                        /* last updated */
                        unsigned long last;

                        sscanf(buf+2, "\t%lu", &last);
                        leap->dtstamp = last - NIST_EPOCH_OFFSET;
                    }
                    else if (buf[1] == 'h') {
                        /* hash */
                        char *p, *hash = buf+3 /* skip "#h\t" */;

                        /* trim trailing whitespace */
                        for (p = hash + strlen(hash); isspace(*--p); *p = '\0');
                        appendstrlist(&leap->data, hash);
                    }
                }
            }
            fclose(fp);

            hash_insert(LEAP_TZID, leap, &tzentries);
            info->dtstamp = leap->dtstamp;
        }

        /* Add ZONE/LINK records */
        do_zonedir(prefix, &tzentries, info);

        zoneinfo_open(NULL);

        /* Store records */
        hash_enumerate(&tzentries, &store_zoneinfo, &tid);

        zoneinfo_close(tid);

        free_hash_table(&tzentries, &free_zoneinfo);
        break;
    }

    case WINZONES: {
        xmlParserCtxtPtr ctxt;
        xmlDocPtr doc;
        xmlNodePtr node;
        struct buf tzidbuf = BUF_INITIALIZER;
        struct buf aliasbuf = BUF_INITIALIZER;

        if (verbose) printf("Processing Windows Zone file %s\n", winfile);

        /* Parse the XML file */
        ctxt = xmlNewParserCtxt();
        if (!ctxt) {
            fprintf(stderr, "Failed to create XML parser context\n");
            break;
        }

        doc = xmlCtxtReadFile(ctxt, winfile, NULL, 0);
        xmlFreeParserCtxt(ctxt);
        if (!doc) {
            fprintf(stderr, "Failed to parse XML document\n");
            break;
        }

        node = xmlDocGetRootElement(doc);
        if (!node || xmlStrcmp(node->name, BAD_CAST "supplementalData")) {
            fprintf(stderr, "Incorrect root node\n");
            goto done;
        }

        for (node = xmlFirstElementChild(node);
             node && xmlStrcmp(node->name, BAD_CAST "windowsZones");
             node = xmlNextElementSibling(node));
        if (!node) {
            fprintf(stderr, "Missing windowsZones node\n");
            goto done;
        }

        node = xmlFirstElementChild(node);
        if (!node || xmlStrcmp(node->name, BAD_CAST "mapTimezones")) {
            fprintf(stderr, "Missing mapTimezones node\n");
            goto done;
        }

        if (chdir(prefix)) {
            fprintf(stderr, "chdir(%s) failed\n", prefix);
            goto done;
        }

        for (node = xmlFirstElementChild(node);
             node;
             node = xmlNextElementSibling(node)) {
            if (!xmlStrcmp(node->name, BAD_CAST "mapZone") &&
                !xmlStrcmp(xmlGetProp(node, BAD_CAST "territory"),
                           BAD_CAST "001")) {
                const char *tzid, *alias;

                buf_setcstr(&tzidbuf,
                            (const char *) xmlGetProp(node, BAD_CAST "type"));
                buf_appendcstr(&tzidbuf, ".ics");
                tzid = buf_cstring(&tzidbuf);
                buf_setcstr(&aliasbuf,
                            (const char *) xmlGetProp(node, BAD_CAST "other"));
                buf_appendcstr(&aliasbuf, ".ics");
                alias = buf_cstring(&aliasbuf);

                if (verbose) printf("\tLINK: %s -> %s\n", alias, tzid);

                if (symlink(tzid, alias)) {
                    if (errno == EEXIST) {
                        struct stat sbuf;

                        if (stat(alias, &sbuf)) {
                            fprintf(stderr, "stat(%s) failed: %s\n",
                                    alias, strerror(errno));
                            errno = EEXIST;
                        }
                        else if (sbuf.st_mode & S_IFLNK) {
                            char link[MAX_MAILBOX_PATH+1];
                            int n = readlink(alias, link, MAX_MAILBOX_PATH);

                            if (n == -1) {
                                fprintf(stderr, "readlink(%s) failed: %s\n",
                                        alias, strerror(errno));
                                errno = EEXIST;
                            }
                            else if (n == (int) strlen(tzid) &&
                                     !strncmp(tzid, link, n)) {
                                errno = 0;
                            }
                        }
                    }

                    if (errno) {
                        fprintf(stderr, "symlink(%s, %s) failed: %s\n",
                                tzid, alias, strerror(errno));
                    }
                }
            }
        }

  done:
        buf_free(&aliasbuf);
        buf_free(&tzidbuf);
        xmlFreeDoc(doc);
        break;
    }

    case NONE:
        r = 2;
        usage();
        break;
    }

    cyrus_done();

    return r;
}
Exemple #7
0
int importDatabase( const char * filename, struct database_t * databaseList, int * databaseListTotal, struct userAttr_t * userAttr ){
    int subscript;
    int subscriptCount;
    int attrSubscript;
    xmlChar * longStr;
    xmlNodePtr node;
    xmlNodePtr content;
    xmlNodePtr sonNode;
    xmlNodePtr grandsonNode;

    *databaseListTotal = 0;
    userAttr->userStatus = NULL;
    userAttr->mobileNumber = NULL;

    xmlKeepBlanksDefault(0);

    // create the doc and root node
    xmlDocPtr doc = xmlReadFile( filename, "UTF-8", XML_PARSE_RECOVER );

    if ( doc == NULL ){
        // printf("Error: xml Data file \"%s\" import failed.\n", filename);
        return 0;
    }

    xmlNodePtr root_node = xmlDocGetRootElement( doc );

    if ( root_node == NULL ){
        // printf("Error: failed to get the root of xml doc.\n");
        return 0;
    }

    if ( xmlStrcmp( root_node->name, BAD_CAST "databaseList") != 0 ){
        // printf("Error: name of root node of xml doc not matched.\n");
        return 0;
    }

    if ( xmlHasProp( root_node, BAD_CAST "databaseListTotal") && xmlHasProp( root_node, BAD_CAST "userStatus") ){
        longStr = xmlGetProp( root_node, BAD_CAST "databaseListTotal" );
        *databaseListTotal = xatoi( (char *) longStr );
        if ( *databaseListTotal > MAX_DATABASE_SAVE ) *databaseListTotal = MAX_DATABASE_SAVE;

        longStr = xmlGetProp( root_node, BAD_CAST "userStatus" );
        userAttr->userStatus = malloc( strlen(longStr)+1 );
        strcpy( userAttr->userStatus, longStr );
        // printf("itemListTotal: %d\n", *itemListTotal );
    }else{
        // printf("Error: failed to get the itemListTotal.\n");
        return 0;
    }

    if ( xmlHasProp( root_node, BAD_CAST "mobileNumber") ){
        longStr = xmlGetProp( root_node, BAD_CAST "mobileNumber" );
        userAttr->mobileNumber = malloc( strlen(longStr)+1 );
        strcpy( userAttr->mobileNumber, longStr );
    }

    node = root_node->xmlChildrenNode;

    for (subscriptCount=1; subscriptCount <= *databaseListTotal; subscriptCount++){
        // printf("subscriptCount: %d\n", subscriptCount);
        if ( node == NULL ){
            // printf("Error: not enough item node.\n");
            *databaseListTotal = 0;
            return 0;
        }

        subscript = subscriptCount;

        databaseList[subscript].url = NULL;
        databaseList[subscript].urlstatus = NULL;
        databaseList[subscript].filenamePrefix = NULL;

        sonNode = node->xmlChildrenNode;
        while( sonNode != NULL ){

            if ( xmlStrcmp( sonNode->name, BAD_CAST "url" ) == 0){
                longStr = xmlNodeGetContent( sonNode );
                databaseList[subscript].url = malloc( strlen(longStr) +1 );
                strcpy( databaseList[subscript].url, (char *) longStr );
            }else if ( xmlStrcmp( sonNode->name, BAD_CAST "urlstatus" ) == 0){
                longStr = xmlNodeGetContent( sonNode );
                databaseList[subscript].urlstatus = malloc( strlen(longStr) +1 );
                strcpy( databaseList[subscript].urlstatus, (char *) longStr );
            }else if ( xmlStrcmp( sonNode->name, BAD_CAST "filenamePrefix" ) == 0){
                longStr = xmlNodeGetContent( sonNode );
                databaseList[subscript].filenamePrefix = malloc( strlen(longStr) +1 );
                strcpy( databaseList[subscript].filenamePrefix, (char *) longStr );
            }else{
                // printf("Error: unknown sonNode of xml doc item, %s", (char *) sonNode->name );
                *databaseListTotal = 0;
                return 0;
            }

            sonNode = sonNode->next;
        }

        node = node->next;
    }
    printf("databaseList import success.\n");
    xmlFreeDoc(doc);
    xmlCleanupParser();
    return 1;
}
int
tdsOpendir (rsComm_t *rsComm, char *dirUrl, void **outDirPtr)
{
    CURLcode res;
    CURL *easyhandle;
    tdsDirStruct_t *tdsDirStruct = NULL;

    if (dirUrl == NULL || outDirPtr == NULL) return USER__NULL_INPUT_ERR;

    *outDirPtr = NULL;
    easyhandle = curl_easy_init();
    if(!easyhandle) {
        rodsLog (LOG_ERROR,
          "tdsOpendir: curl_easy_init error");
        return OOI_CURL_EASY_INIT_ERR;
    }
    curl_easy_setopt(easyhandle, CURLOPT_URL, dirUrl);
    curl_easy_setopt(easyhandle, CURLOPT_WRITEFUNCTION, tdsDirRespHandler);
    tdsDirStruct = (tdsDirStruct_t *) calloc (1, sizeof (tdsDirStruct_t));
    tdsDirStruct->easyhandle = easyhandle;
    curl_easy_setopt(easyhandle, CURLOPT_WRITEDATA, tdsDirStruct);
    /* this is needed for ERDDAP site */
    curl_easy_setopt(easyhandle, CURLOPT_FOLLOWLOCATION, 1);

    res = curl_easy_perform (easyhandle);

    if (res != CURLE_OK) {
        /* res is +ive for error */
        rodsLog (LOG_ERROR,
          "tdsOpendir: curl_easy_perform error: %d", res);
        freeTdsDirStruct (&tdsDirStruct);
        curl_easy_cleanup (easyhandle);
        return OOI_CURL_EASY_PERFORM_ERR - res;
    }
    tdsDirStruct->doc = xmlParseMemory (tdsDirStruct->httpResponse,
      tdsDirStruct->len);
    if (tdsDirStruct->doc == NULL) {
        freeTdsDirStruct (&tdsDirStruct);
        rodsLog (LOG_ERROR,
          "tdsOpendir: xmlParseMemory error for %s", dirUrl);
        return XML_PARSING_ERR;
    }
    free (tdsDirStruct->httpResponse);          /* don't need this anymore */
    tdsDirStruct->httpResponse = NULL;
    tdsDirStruct->rootnode = xmlDocGetRootElement (tdsDirStruct->doc);

    if (tdsDirStruct->rootnode == NULL) {
        rodsLog (LOG_ERROR,
          "tdsOpendir: xmlDocGetRootElement error for %s", dirUrl);
        freeTdsDirStruct (&tdsDirStruct);
        return XML_PARSING_ERR;
    }
    if (xmlStrcmp(tdsDirStruct->rootnode->name, (const xmlChar *) "catalog")) {
        rodsLog (LOG_ERROR,
          "tdsOpendir: root node name %s is not 'catalog' for %s",
          tdsDirStruct->rootnode, dirUrl);
        freeTdsDirStruct (&tdsDirStruct);
        return XML_PARSING_ERR;
    }
    tdsDirStruct->curnode = tdsDirStruct->rootnode;
    rstrcpy (tdsDirStruct->dirUrl, dirUrl, MAX_NAME_LEN);
    *outDirPtr = tdsDirStruct;
    return 0;
}
int
tdsReaddir (rsComm_t *rsComm, void *dirPtr, struct dirent *direntPtr)
{
    int status = -1;
    tdsDirStruct_t *tdsDirStruct = (tdsDirStruct_t *) dirPtr;
    xmlAttrPtr myprop;
    const xmlChar *myname, *myurlPath, *mytitle, *myhref, *tmpname;
    xmlNodePtr mynode;

    while (getNextNode (tdsDirStruct)) {
        mynode = tdsDirStruct->curnode;
        if (xmlIsBlankNode (tdsDirStruct->curnode)) {
            continue;
        } else if (xmlStrcmp (mynode->name, (const xmlChar *) "dataset") == 0) {
            myprop = mynode->properties;
            myname = myurlPath = mytitle = NULL;
            while (myprop) {
                if (xmlStrcmp (myprop->name, (const xmlChar *) "name") == 0) {
                    myname = myprop->children->content;
                } else if (xmlStrcmp (myprop->name,
                  (const xmlChar *) "urlPath") == 0) {
                    myurlPath = myprop->children->content;
               } else if (xmlStrcmp (myprop->name, (const xmlChar *) "title")
                  == 0) {
                    mytitle = myprop->children->content;
                }
                myprop = myprop->next;
            }
            if (myurlPath == NULL) {
                /* drill down */
                setTdsCurdir (tdsDirStruct, (char *) myname);
                continue;
            } else {
                if ((char *)myname != NULL && strlen ((char *)myname) > 0) {
                    tmpname = myname;
                } else if ((char *)mytitle != NULL &&
                  strlen ((char *)mytitle) > 0) {
                    tmpname = mytitle;
                } else {
                    rodsLog (LOG_ERROR,
                      "tdsReaddir: dataset %s has no name nor title",
                      myurlPath);
                    continue;
                }
                rstrcpy (direntPtr->d_name, (char *) tmpname, NAME_MAX);
                status = setTDSUrl (tdsDirStruct, (char *)myurlPath, False);
                if (status >= 0) direntPtr->d_ino = status;
                return status;
            }
        } else if (xmlStrcmp (mynode->name, (const xmlChar *) "catalogRef")
          == 0) {
            /* this is a link */
            myprop = mynode->properties;
            myname = myhref = mytitle = NULL;
            while (myprop) {
                if (xmlStrcmp (myprop->name, (const xmlChar *) "name") == 0) {
                    myname = myprop->children->content;
                } else if (xmlStrcmp (myprop->name, (const xmlChar *) "href")
                  == 0) {
                    myhref = myprop->children->content;
               } else if (xmlStrcmp (myprop->name, (const xmlChar *) "title")
                  == 0) {
                    mytitle = myprop->children->content;
                }
                myprop = myprop->next;
            }
            if (myhref == NULL) continue;
            if ((char *)myname != NULL && strlen ((char *)myname) > 0) {
                tmpname = myname;
            } else if ((char *)mytitle != NULL &&
              strlen ((char *)mytitle) > 0) {
                tmpname = mytitle;
            } else {
                rodsLog (LOG_ERROR,
                  "tdsReaddir: dataset %s has no name nor title",
                  myurlPath);
                continue;
            }
            snprintf (direntPtr->d_name, NAME_MAX, "%s", (char *) tmpname);
            status = setTDSUrl (tdsDirStruct, (char *)myhref, True);
            if (status >= 0) direntPtr->d_ino = status;
            return status;
        }
    }
    return status;
}
Exemple #10
0
static int XMLNode2Value(ValueStruct *val, xmlNodePtr root) {
  int i;
  char *buff1;
  xmlNodePtr node;

  if (val == NULL || root == NULL) {
    return MCP_BAD_OTHER;
  }
  switch (ValueType(val)) {
  case GL_TYPE_INT:
    if (xmlStrcmp(root->name, "int") != 0) {
      break;
    }
    buff1 = XMLGetString(root, "0");
    SetValueStringWithLength(val, buff1, strlen(buff1), NULL);
    free(buff1);
    break;
  case GL_TYPE_FLOAT:
    if (xmlStrcmp(root->name, "float") != 0) {
      break;
    }
    buff1 = XMLGetString(root, "0");
    SetValueStringWithLength(val, buff1, strlen(buff1), NULL);
    free(buff1);
    break;
  case GL_TYPE_NUMBER:
    if (xmlStrcmp(root->name, "number") != 0) {
      break;
    }
    buff1 = XMLGetString(root, "0.0");
    SetValueStringWithLength(val, buff1, strlen(buff1), NULL);
    free(buff1);
    break;
  case GL_TYPE_BOOL:
    if (xmlStrcmp(root->name, "bool") != 0) {
      break;
    }
    buff1 = XMLGetString(root, "FALSE");
    SetValueStringWithLength(val, buff1, strlen(buff1), NULL);
    free(buff1);
    break;
  case GL_TYPE_CHAR:
  case GL_TYPE_VARCHAR:
  case GL_TYPE_SYMBOL:
  case GL_TYPE_DBCODE:
  case GL_TYPE_TEXT:
    if (xmlStrcmp(root->name, "string") != 0) {
      break;
    }
    buff1 = XMLGetString(root, "");
    SetValueStringWithLength(val, buff1, strlen(buff1), NULL);
    free(buff1);
    break;
  case GL_TYPE_ARRAY:
    if (xmlStrcmp(root->name, "array") != 0) {
      break;
    }
    node = root->children;
    for (i = 0; i < ValueArraySize(val); i++) {
      if (node == NULL) {
        break;
      }
      XMLNode2Value(ValueArrayItem(val, i), node);
      node = node->next;
    }
    break;
  case GL_TYPE_RECORD:
    if (xmlStrcmp(root->name, "record") != 0) {
      break;
    }
    for (i = 0; i < ValueRecordSize(val); i++) {
      for (node = root->children; node != NULL; node = node->next) {
        if ((buff1 = xmlGetProp(node, "name")) == NULL) {
          continue;
        }
        if (!xmlStrcmp(buff1, ValueRecordName(val, i))) {
          XMLNode2Value(ValueRecordItem(val, i), node);
          free(buff1);
          break;
        }
        free(buff1);
      }
    }
    break;
  case GL_TYPE_OBJECT:
    break;
  default:
    return MCP_BAD_ARG;
    break;
  }
  return MCP_OK;
}