<commit_before>/*
 * Copyright IBM Corp. 2007
 *
 * Authors:
 *  <NAME> <<EMAIL>>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
 */
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <inttypes.h>
#include <uuid/uuid.h>

#include "xmlgen.h"

#ifndef TEST
#include "misc_util.h"
#include <libcmpiutil/libcmpiutil.h>
#include "cmpimacs.h"
#endif

static char *__tag_attr(struct kv *attrs, int count)
{
        char *result = strdup("");
        char *new = NULL;
        int i;
        int size = 0;

        for (i = 0; i < count; i++) {
                char *tmp;
                int ret;

                ret = asprintf(&new, " %s='%s'",
                               attrs[i].key, attrs[i].val);

                if (ret == -1)
                        goto err;

                size += strlen(new) + 1;
                tmp = realloc(result, size);
                if (tmp == NULL)
                        goto err;

                result = tmp;

                strcat(result, new);
                free(new);
        }

        return result;

 err:
        free(result);
        return NULL;
}

static char *tagify(char *tagname, char *content, struct kv *attrs, int count)
{
        char *result;
        int ret;
        char *opentag;

        if (count)
                opentag = __tag_attr(attrs, count);
        else
                opentag = strdup("");

        if (content)
                ret = asprintf(&result,
                               "<%s%s>%s</%s>",
                               tagname, opentag, content, tagname);
        else
                ret = asprintf(&result,
                               "<%s%s/>", tagname, opentag);
        if (ret == -1)
                result = NULL;

        free(opentag);

        return result;
}

static int astrcat(char **dest, char *source)
{
        char *tmp;
        int ret;

        if (*dest) {
                ret = asprintf(&tmp, "%s%s", *dest, source);
                if (ret == -1)
                        return 0;
        } else {
                tmp = strdup(source);
        }

        free(*dest);

        *dest = tmp;

        return 1;
}

static char *disk_block_xml(const char *path, const char *vdev)
{
        char *xml;
        int ret;

        ret = asprintf(&xml,
                       "<disk type='block' device='disk'>\n"
                       "  <source dev='%s'/>\n"
                       "  <target dev='%s'/>\n"
                       "</disk>\n",
                       path,
                       vdev);
        if (ret == -1)
                xml = NULL;

        return xml;
}

static char *disk_file_xml(const char *path, const char *vdev)
{
        char *xml;
        int ret;

        ret = asprintf(&xml,
                       "<disk type='file' device='disk'>\n"
                       "  <source file='%s'/>\n"
                       "  <target dev='%s'/>\n"
                       "</disk>\n",
                       path,
                       vdev);
        if (ret == -1)
                xml = NULL;

        return xml;
}

static bool disk_to_xml(char **xml, struct virt_device *dev)
{
        char *_xml = NULL;
        struct disk_device *disk = &dev->dev.disk;

        if (disk->disk_type == DISK_PHY)
                _xml = disk_block_xml(disk->source, disk->virtual_dev);
        else if (disk->disk_type == DISK_FILE)
                /* If it's not a block device, we assume a file,
                   which should be a reasonable fail-safe */
                _xml = disk_file_xml(disk->source, disk->virtual_dev);
        else
                return false;

        astrcat(xml, _xml);
        free(_xml);

        return true;
}

static bool xen_net_to_xml(char **xml, struct virt_device *dev)
{
        int ret;
        char *_xml;
        char *script = "vif-bridge";<commit_msg>Handle LXC disk XML generation<commit_after>/*
 * Copyright IBM Corp. 2007
 *
 * Authors:
 *  <NAME> <<EMAIL>>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
 */
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <inttypes.h>
#include <uuid/uuid.h>

#include "xmlgen.h"

#ifndef TEST
#include "misc_util.h"
#include <libcmpiutil/libcmpiutil.h>
#include "cmpimacs.h"
#endif

static char *__tag_attr(struct kv *attrs, int count)
{
        char *result = strdup("");
        char *new = NULL;
        int i;
        int size = 0;

        for (i = 0; i < count; i++) {
                char *tmp;
                int ret;

                ret = asprintf(&new, " %s='%s'",
                               attrs[i].key, attrs[i].val);

                if (ret == -1)
                        goto err;

                size += strlen(new) + 1;
                tmp = realloc(result, size);
                if (tmp == NULL)
                        goto err;

                result = tmp;

                strcat(result, new);
                free(new);
        }

        return result;

 err:
        free(result);
        return NULL;
}

static char *tagify(char *tagname, char *content, struct kv *attrs, int count)
{
        char *result;
        int ret;
        char *opentag;

        if (count)
                opentag = __tag_attr(attrs, count);
        else
                opentag = strdup("");

        if (content)
                ret = asprintf(&result,
                               "<%s%s>%s</%s>",
                               tagname, opentag, content, tagname);
        else
                ret = asprintf(&result,
                               "<%s%s/>", tagname, opentag);
        if (ret == -1)
                result = NULL;

        free(opentag);

        return result;
}

static int astrcat(char **dest, char *source)
{
        char *tmp;
        int ret;

        if (*dest) {
                ret = asprintf(&tmp, "%s%s", *dest, source);
                if (ret == -1)
                        return 0;
        } else {
                tmp = strdup(source);
        }

        free(*dest);

        *dest = tmp;

        return 1;
}

static char *disk_block_xml(const char *path, const char *vdev)
{
        char *xml;
        int ret;

        ret = asprintf(&xml,
                       "<disk type='block' device='disk'>\n"
                       "  <source dev='%s'/>\n"
                       "  <target dev='%s'/>\n"
                       "</disk>\n",
                       path,
                       vdev);
        if (ret == -1)
                xml = NULL;

        return xml;
}

static char *disk_file_xml(const char *path, const char *vdev)
{
        char *xml;
        int ret;

        ret = asprintf(&xml,
                       "<disk type='file' device='disk'>\n"
                       "  <source file='%s'/>\n"
                       "  <target dev='%s'/>\n"
                       "</disk>\n",
                       path,
                       vdev);
        if (ret == -1)
                xml = NULL;

        return xml;
}

static char *disk_fs_xml(const char *path, const char *vdev)
{
        char *xml;
        int ret;

        ret = asprintf(&xml,
                       "<filesystem type='mount'>\n"
                       "  <source dir='%s'/>\n"
                       "  <target dir='%s'/>\n"
                       "</filesystem>\n",
                       path,
                       vdev);
        if (ret == -1)
                xml = NULL;

        return xml;
}

static bool disk_to_xml(char **xml, struct virt_device *dev)
{
        char *_xml = NULL;
        struct disk_device *disk = &dev->dev.disk;

        if (disk->disk_type == DISK_PHY)
                _xml = disk_block_xml(disk->source, disk->virtual_dev);
        else if (disk->disk_type == DISK_FILE)
                /* If it's not a block device, we assume a file,
                   which should be a reasonable fail-safe */
                _xml = disk_file_xml(disk->source, disk->virtual_dev);
        else if (disk->disk_type == DISK_FS)
                _xml = disk_fs_xml(disk->source, disk->virtual_dev);
        else
                return false;

        astrcat(xml, _xml);
        free(_xml);

        return true;
}

static bool xen_net_to_xml(char **xml, struct virt_device *dev)
{
        int ret;
        char *_xml;
        char *script = "vif-bridge";
{'fraction_non_alphanumeric': 0.11615584415584415, 'fraction_numerical': 0.006337662337662337, 'mean_word_length': 1.399900274245824, 'pattern_counts': {'":': 0, '<': 49, '<?xml version=': 0, '>': 64, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 55}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3173198', 'n_tokens_mistral': 2901, 'n_tokens_neox': 2731, 'n_words': 860}