<commit_before>/*
 * Copyright IBM Corp. 2007
 *
 * Authors:
 *  <NAME> <<EMAIL>>
 *  <NAME> <<EMAIL>>
 *  <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 <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>

#include "cmpidt.h"
#include "cmpift.h"
#include "cmpimacs.h"

#include <libcmpiutil/libcmpiutil.h>
#include "device_parsing.h"
#include "misc_util.h"
#include "cs_util.h"
#include <libcmpiutil/std_association.h>

#include "Virt_ComputerSystem.h"
#include "Virt_Device.h"

/* Associate an XXX_ComputerSystem to the proper XXX_LogicalDisk
 * and XXX_NetworkPort instances.
 *
 *  -- or --
 *
 * Associate an XXX_LogicalDevice to the proper XXX_ComputerSystem
 */

const static CMPIBroker *_BROKER;

#define DEV_TYPE_COUNT 4
const static int device_types[DEV_TYPE_COUNT] = 
        {VIRT_DEV_NET,
         VIRT_DEV_DISK,
         VIRT_DEV_MEM,
         VIRT_DEV_VCPU,
        };

static int get_dom_devices(const char *name,
                           struct inst_list *list,
                           int type,
                           const char *host_cn,
                           const char *ns)
{
        virConnectPtr conn = NULL;
        virDomainPtr dom = NULL;
        CMPIStatus s;
        int ret = 0;

        conn = connect_by_classname(_BROKER, host_cn, &s);
        if (conn == NULL)
                goto out;

        dom = virDomainLookupByName(conn, name);
        if (dom == NULL)
                goto out;

        ret = dom_devices(_BROKER, dom, ns, type, list);

        virDomainFree(dom);
 out:
        virConnectClose(conn);

        return ret;
}

static int get_all_devices(const char *name,
                           struct inst_list *list,
                           const char *host_cn,
                           const char *ns)
{
        int i;

        for (i = 0; i < DEV_TYPE_COUNT; i++)
                get_dom_devices(name, list, device_types[i], host_cn, ns);

        return i;
}

static CMPIInstance *host_instance(char *name,
                                   const CMPIObjectPath *ref)
{
        CMPIInstance *inst = NULL;
        virConnectPtr conn = NULL;
        CMPIStatus s;
        CMPIObjectPath *op;
        char *host_class;

        host_class = get_typed_class(CLASSNAME(ref),
                                     "ComputerSystem");
        if (host_class == NULL)
                goto out;

        op = CMNewObjectPath(_BROKER, NAMESPACE(ref), host_class, &s);
        if ((s.rc != CMPI_RC_OK) || CMIsNullObject(op))
                goto out;

        conn = connect_by_classname(_BROKER, host_class, &s);
        if (conn == NULL)
                goto out;

        inst = instance_from_name(_BROKER, conn, name, op);

 out:
        free(host_class);
        virConnectClose(conn);

        return inst;
}

static CMPIStatus sys_to_dev(const CMPIObjectPath *ref,
                             struct std_assoc_info *info,
                             struct inst_list *list)
{
        const char *host = NULL;
        CMPIStatus s = {CMPI_RC_OK, NULL};
        int ret;

        if (!match_hypervisor_prefix(ref, info))
                return s;

        if (cu_get_str_path(ref, "Name", &host) != CMPI_RC_OK) {
                cu_statusf(_BROKER, &s,
                           CMPI_RC_ERR_FAILED,
                           "Missing Name");
                goto out;
        }

        ret = get_all_devices(host,
                              list,
                              CLASSNAME(ref),
                              NAMESPACE(ref));

        if (ret >= 0) {
                CMSetStatus(&s, CMPI_RC_OK);
        } else {
                cu_statusf(_BROKER, &s,
                           CMPI_RC_ERR_FAILED,
                           "Unable to get devices");
        }

 out:
        return s;
}

static CMPIStatus dev_to_sys(const CMPIObjectPath *ref,
                             struct std_assoc_info *info,
                             struct inst_list *list)
{
        const char *devid = NULL;
        char *host = NULL;
        char *dev = NULL;
        CMPIInstance *sys;
        CMPIStatus s = {CMPI_RC_OK, NULL};

        if (!match_hypervisor_prefix(ref, info))
                return s;

        if (cu_get_str_path(ref, "DeviceID", &devid) != CMPI_RC_OK) {
                cu_statusf(_BROKER, &s,
                           CMPI_RC_ERR_FAILED,
                           "Missing DeviceID");
                goto out;
        }

        if (!parse_fq_devid(devid, &host, &dev)) {
                cu_statusf(_BROKER, &s,
                           CMPI_RC_ERR_FAILED,
                           "Invalid DeviceID");
                goto out;
        }

        sys = host_instance(host, ref);

        if (sys == NULL)
                cu_statusf(_BROKER, &s,
                           CMPI_RC_ERR_FAILED,
                           "Unable to find DeviceID `%s'", devid);
        else {
                inst_list_add(list, sys);
                CMSetStatus(&s, CMPI_RC_OK);
        }

 out:
        free(dev);
        free(host);<commit_msg>SD: does not check client given object path<commit_after>/*
 * Copyright IBM Corp. 2007
 *
 * Authors:
 *  <NAME> <<EMAIL>>
 *  <NAME> <<EMAIL>>
 *  <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 <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>

#include "cmpidt.h"
#include "cmpift.h"
#include "cmpimacs.h"

#include <libcmpiutil/libcmpiutil.h>
#include "device_parsing.h"
#include "misc_util.h"
#include "cs_util.h"
#include <libcmpiutil/std_association.h>

#include "Virt_ComputerSystem.h"
#include "Virt_Device.h"

/* Associate an XXX_ComputerSystem to the proper XXX_LogicalDisk
 * and XXX_NetworkPort instances.
 *
 *  -- or --
 *
 * Associate an XXX_LogicalDevice to the proper XXX_ComputerSystem
 */

const static CMPIBroker *_BROKER;

#define DEV_TYPE_COUNT 4
const static int device_types[DEV_TYPE_COUNT] = 
        {VIRT_DEV_NET,
         VIRT_DEV_DISK,
         VIRT_DEV_MEM,
         VIRT_DEV_VCPU,
        };

static int get_dom_devices(const char *name,
                           struct inst_list *list,
                           int type,
                           const char *host_cn,
                           const char *ns)
{
        virConnectPtr conn = NULL;
        virDomainPtr dom = NULL;
        CMPIStatus s;
        int ret = 0;

        conn = connect_by_classname(_BROKER, host_cn, &s);
        if (conn == NULL)
                goto out;

        dom = virDomainLookupByName(conn, name);
        if (dom == NULL)
                goto out;

        ret = dom_devices(_BROKER, dom, ns, type, list);

        virDomainFree(dom);
 out:
        virConnectClose(conn);

        return ret;
}

static int get_all_devices(const char *name,
                           struct inst_list *list,
                           const char *host_cn,
                           const char *ns)
{
        int i;

        for (i = 0; i < DEV_TYPE_COUNT; i++)
                get_dom_devices(name, list, device_types[i], host_cn, ns);

        return i;
}

static CMPIInstance *host_instance(char *name,
                                   const CMPIObjectPath *ref)
{
        CMPIInstance *inst = NULL;
        virConnectPtr conn = NULL;
        CMPIStatus s;
        CMPIObjectPath *op;
        char *host_class;

        host_class = get_typed_class(CLASSNAME(ref),
                                     "ComputerSystem");
        if (host_class == NULL)
                goto out;

        op = CMNewObjectPath(_BROKER, NAMESPACE(ref), host_class, &s);
        if ((s.rc != CMPI_RC_OK) || CMIsNullObject(op))
                goto out;

        conn = connect_by_classname(_BROKER, host_class, &s);
        if (conn == NULL)
                goto out;

        inst = instance_from_name(_BROKER, conn, name, op);

 out:
        free(host_class);
        virConnectClose(conn);

        return inst;
}

static CMPIStatus sys_to_dev(const CMPIObjectPath *ref,
                             struct std_assoc_info *info,
                             struct inst_list *list)
{
        const char *host = NULL;
        CMPIStatus s = {CMPI_RC_OK, NULL};
        CMPIInstance *inst = NULL;
        int ret;

        if (!match_hypervisor_prefix(ref, info))
                return s;

        s = get_domain(_BROKER, ref, &inst);
        if (s.rc != CMPI_RC_OK)
                goto out;

        if (cu_get_str_path(ref, "Name", &host) != CMPI_RC_OK) {
                cu_statusf(_BROKER, &s,
                           CMPI_RC_ERR_FAILED,
                           "Missing Name");
                goto out;
        }

        ret = get_all_devices(host,
                              list,
                              CLASSNAME(ref),
                              NAMESPACE(ref));

        if (ret >= 0) {
                CMSetStatus(&s, CMPI_RC_OK);
        } else {
                cu_statusf(_BROKER, &s,
                           CMPI_RC_ERR_FAILED,
                           "Unable to get devices");
        }

 out:
        return s;
}

static CMPIStatus dev_to_sys(const CMPIObjectPath *ref,
                             struct std_assoc_info *info,
                             struct inst_list *list)
{
        const char *devid = NULL;
        char *host = NULL;
        char *dev = NULL;
        CMPIInstance *sys;
        CMPIInstance *inst = NULL;
        CMPIStatus s = {CMPI_RC_OK, NULL};

        if (!match_hypervisor_prefix(ref, info))
                return s;

        s = get_device(_BROKER, ref, &inst);
        if (s.rc != CMPI_RC_OK)
                goto out;

        if (cu_get_str_path(ref, "DeviceID", &devid) != CMPI_RC_OK) {
                cu_statusf(_BROKER, &s,
                           CMPI_RC_ERR_FAILED,
                           "Missing DeviceID");
                goto out;
        }

        if (!parse_fq_devid(devid, &host, &dev)) {
                cu_statusf(_BROKER, &s,
                           CMPI_RC_ERR_FAILED,
                           "Invalid DeviceID");
                goto out;
        }

        sys = host_instance(host, ref);

        if (sys == NULL)
                cu_statusf(_BROKER, &s,
                           CMPI_RC_ERR_FAILED,
                           "Unable to find DeviceID `%s'", devid);
        else {
                inst_list_add(list, sys);
                CMSetStatus(&s, CMPI_RC_OK);
        }

 out:
        free(dev);
        free(host);
{'fraction_non_alphanumeric': 0.09034924919858275, 'fraction_numerical': 0.004049266070524717, 'mean_word_length': 1.6239486498450642, 'pattern_counts': {'":': 0, '<': 35, '<?xml version=': 0, '>': 35, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2064164', 'n_tokens_mistral': 3727, 'n_tokens_neox': 3548, 'n_words': 964}