<commit_before>/*
 * Copyright (C) 2011-2014 Red Hat, Inc.
 * 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, see <http://www.gnu.org/licenses/>.
 *
 * Author: tasleson
 */

#include "lsm_ipc.hpp"

#include "libstoragemgmt/libstoragemgmt_plug_interface.h"

#include <unistd.h>
#include <errno.h>
#include <sys/un.h>
#include <sys/socket.h>
#include <string.h>
#include <iomanip>
#include <sstream>
#include <iostream>
#include <algorithm>
#include <stdio.h>
#include <list>

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#ifdef HAVE_YAJL_YAJL_VERSION_H
#include <yajl/yajl_version.h>
#endif


#if defined(HAVE_YAJL_YAJL_VERSION_H) && YAJL_MAJOR > 1
#define LSM_NEW_YAJL
#endif

static std::string zero_pad_num(unsigned int num)
{
    std::ostringstream ss;
    ss << std::setw(Transport::HDR_LEN) << std::setfill('0') << num;
    return ss.str();
}

Transport::Transport():s(-1)
{
}

Transport::Transport(int socket_desc):s(socket_desc)
{
}

int Transport::msg_send(const std::string & msg, int &error_code)
{
    int rc = -1;
    error_code = 0;

    if (msg.size() > 0) {
        ssize_t written = 0;
        //fprintf(stderr, ">>> %s\n", msg.c_str());
        std::string data = zero_pad_num(msg.size()) + msg;
        ssize_t msg_size = data.size();

        while (written < msg_size) {
            int wrote = send(s, data.c_str() + written, (msg_size - written),
                             MSG_NOSIGNAL); //Prevent SIGPIPE on write
            if (wrote != -1) {
                written += wrote;
            } else {
                error_code = errno;
                break;
            }
        }

        if ((written == msg_size) && error_code == 0) {
            rc = 0;
        }
    }
    return rc;
}

static std::string string_read(int fd, size_t count, int &error_code)
{
    char buff[4096];
    size_t amount_read = 0;
    std::string rc = "";

    error_code = 0;

    while (amount_read < count) {
        ssize_t rd = recv(fd, buff, std::min(sizeof(buff),
                                             (count - amount_read)),
                          MSG_WAITALL);
        if (rd > 0) {
            amount_read += rd;
            rc += std::string(buff, rd);
        } else {
            error_code = errno;
            break;
        }
    }

    if ((amount_read == count) && (error_code == 0))
        return rc;
    else
        throw EOFException("");
}

std::string Transport::msg_recv(int &error_code)
{
    std::string msg;
    error_code = 0;
    unsigned long int payload_len = 0;
    std::string len = string_read(s, HDR_LEN, error_code);  //Read the length
    if (len.size() && error_code == 0) {
        payload_len = strtoul(len.c_str(), NULL, 10);
        if (payload_len < 0x80000000) { /* Should be big enough */
            msg = string_read(s, payload_len, error_code);
        }
        //fprintf(stderr, "<<< %s\n", msg.c_str());
    }
    return msg;
}

int Transport::socket_get(const std::string & path, int &error_code)
{
    int sfd = socket(AF_UNIX, SOCK_STREAM, 0);
    int rc = -1;
    error_code = 0;

    if (sfd != -1) {
        struct sockaddr_un addr;

        memset(&addr, 0, sizeof(addr));
        addr.sun_family = AF_UNIX;
        strncpy(addr.sun_path, path.c_str(), sizeof(addr.sun_path) - 1);

        // Connect
        rc = connect(sfd, (struct sockaddr *) &addr, sizeof(addr));
        if (rc != 0) {
            error_code = errno;
            rc = -1;            //Redundant, connect should set to -1 on error
            ::close(sfd);
        } else {
            rc = sfd;           //We are good to go.
        }
    }
    return rc;
}

Transport::~Transport()
{
    close();
}

int Transport::close()
{
    int rc = EBADF;

    if (s >= 0) {
        int rc =::close(s);
        if (rc != 0) {
            rc = errno;
        }
        // Regardless, clear out socket
        s = -1;
    }
    return rc;
}

EOFException::EOFException(std::string m):std::runtime_error(m)
{
}

ValueException::ValueException(std::string m):std::runtime_error(m)
{
}

LsmException::LsmException(int code, std::string & msg):<commit_msg>Fix compile warning: declaration of 'rc' shadows a previous local<commit_after>/*
 * Copyright (C) 2011-2014 Red Hat, Inc.
 * 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, see <http://www.gnu.org/licenses/>.
 *
 * Author: tasleson
 */

#include "lsm_ipc.hpp"

#include "libstoragemgmt/libstoragemgmt_plug_interface.h"

#include <unistd.h>
#include <errno.h>
#include <sys/un.h>
#include <sys/socket.h>
#include <string.h>
#include <iomanip>
#include <sstream>
#include <iostream>
#include <algorithm>
#include <stdio.h>
#include <list>

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#ifdef HAVE_YAJL_YAJL_VERSION_H
#include <yajl/yajl_version.h>
#endif


#if defined(HAVE_YAJL_YAJL_VERSION_H) && YAJL_MAJOR > 1
#define LSM_NEW_YAJL
#endif

static std::string zero_pad_num(unsigned int num)
{
    std::ostringstream ss;
    ss << std::setw(Transport::HDR_LEN) << std::setfill('0') << num;
    return ss.str();
}

Transport::Transport():s(-1)
{
}

Transport::Transport(int socket_desc):s(socket_desc)
{
}

int Transport::msg_send(const std::string & msg, int &error_code)
{
    int rc = -1;
    error_code = 0;

    if (msg.size() > 0) {
        ssize_t written = 0;
        //fprintf(stderr, ">>> %s\n", msg.c_str());
        std::string data = zero_pad_num(msg.size()) + msg;
        ssize_t msg_size = data.size();

        while (written < msg_size) {
            int wrote = send(s, data.c_str() + written, (msg_size - written),
                             MSG_NOSIGNAL); //Prevent SIGPIPE on write
            if (wrote != -1) {
                written += wrote;
            } else {
                error_code = errno;
                break;
            }
        }

        if ((written == msg_size) && error_code == 0) {
            rc = 0;
        }
    }
    return rc;
}

static std::string string_read(int fd, size_t count, int &error_code)
{
    char buff[4096];
    size_t amount_read = 0;
    std::string rc = "";

    error_code = 0;

    while (amount_read < count) {
        ssize_t rd = recv(fd, buff, std::min(sizeof(buff),
                                             (count - amount_read)),
                          MSG_WAITALL);
        if (rd > 0) {
            amount_read += rd;
            rc += std::string(buff, rd);
        } else {
            error_code = errno;
            break;
        }
    }

    if ((amount_read == count) && (error_code == 0))
        return rc;
    else
        throw EOFException("");
}

std::string Transport::msg_recv(int &error_code)
{
    std::string msg;
    error_code = 0;
    unsigned long int payload_len = 0;
    std::string len = string_read(s, HDR_LEN, error_code);  //Read the length
    if (len.size() && error_code == 0) {
        payload_len = strtoul(len.c_str(), NULL, 10);
        if (payload_len < 0x80000000) { /* Should be big enough */
            msg = string_read(s, payload_len, error_code);
        }
        //fprintf(stderr, "<<< %s\n", msg.c_str());
    }
    return msg;
}

int Transport::socket_get(const std::string & path, int &error_code)
{
    int sfd = socket(AF_UNIX, SOCK_STREAM, 0);
    int rc = -1;
    error_code = 0;

    if (sfd != -1) {
        struct sockaddr_un addr;

        memset(&addr, 0, sizeof(addr));
        addr.sun_family = AF_UNIX;
        strncpy(addr.sun_path, path.c_str(), sizeof(addr.sun_path) - 1);

        // Connect
        rc = connect(sfd, (struct sockaddr *) &addr, sizeof(addr));
        if (rc != 0) {
            error_code = errno;
            rc = -1;            //Redundant, connect should set to -1 on error
            ::close(sfd);
        } else {
            rc = sfd;           //We are good to go.
        }
    }
    return rc;
}

Transport::~Transport()
{
    close();
}

int Transport::close()
{
    int rc = EBADF;

    if (s >= 0) {
        rc =::close(s);
        if (rc != 0) {
            rc = errno;
        }
        // Regardless, clear out socket
        s = -1;
    }
    return rc;
}

EOFException::EOFException(std::string m):std::runtime_error(m)
{
}

ValueException::ValueException(std::string m):std::runtime_error(m)
{
}

LsmException::LsmException(int code, std::string & msg):
{'fraction_non_alphanumeric': 0.13236232192217734, 'fraction_numerical': 0.011482032745056348, 'mean_word_length': 2.2949211908931697, 'pattern_counts': {'":': 0, '<': 53, '<?xml version=': 0, '>': 43, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, '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': '4699699', 'n_tokens_mistral': 3236, 'n_tokens_neox': 3095, 'n_words': 851}