<commit_before>/*
 * Copyright 2013 Los Alamos National Security, LLC.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * Author: <NAME> <<EMAIL>>
 */

#include <errno.h>
#include <stdio.h>
#include <string.h>

#include "common.h"
#include "log.h"
#include "stream.h"
#include "task/task.h"

/** The debug stream to write log messages to. */
extern FILE* TDB_debug_stream;

/** The log level to write messages for. */
extern TDB_loglevel TDB_debug_level;

int TDB_fdwriten(TDB_stream_ctx* stream_ctx, char* buf, int len)
{
    int rc;

    LOG(TDB_LOG_INFO, "Entered TDB_fdwriten fd=%d, buf=`%s', len=`%d'.", stream_ctx->fd, buf, len);

    while(len > 0) {
        if((rc = fdwrite(stream_ctx->fd, buf, len)) <= 0) {
            if(rc < 0 && errno == EINTR) {
                rc = 0;
            }
            else if(rc < 0 && errno == EPIPE) {
                LOG(TDB_LOG_ERR, "The client unexpectedly disconnected.");
                return TDB_ERROR;
            }
            else {
                LOG(TDB_LOG_ERR, "An error occurred while writing to the socket (errno=%d). %s", \
                    errno, strerror(errno));
                return TDB_ERROR;
            }
        }

        len -= rc;
        buf += rc;
    }

    return TDB_SUCCESS;
}

int TDB_fdreadn(TDB_stream_ctx* stream_ctx, char* buf, int len)
{
    int rc;

    while(len > 0) {
        rc = fdread(stream_ctx->fd, buf, len);

        if(rc < 0) {
            if(errno == EINTR) {
                continue;
            }

            LOG(TDB_LOG_ERR, "An error occurred while reading from the socket (errno=%d). %s", \
                errno, strerror(errno));
            return TDB_ERROR;
        }

        if(rc == 0) {
            LOG(TDB_LOG_ERR, "An error occurred while reading from the socket. Unexpected EOF.");
            return TDB_ERROR;
        }

        buf += rc;
        len -= rc;
    }

    return TDB_SUCCESS;
}

int TDB_init_xdr_stream(TDB_stream_ctx* stream_ctx)
{
    /* FIXME: Handle memory allocation failures in xdrrec_create. */
    xdrrec_create(&(stream_ctx->xdrs), 0, 0, (void*) stream_ctx, \
        (int (*)(char*,char*,int)) TDB_fdreadn, \
        (int (*)(char*,char*,int)) TDB_fdwriten);

    return TDB_SUCCESS;
}
<commit_msg>Fix return value when read/write on the fd.<commit_after>/*
 * Copyright 2013 Los Alamos National Security, LLC.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * Author: <NAME> <<EMAIL>>
 */

#include <errno.h>
#include <stdio.h>
#include <string.h>

#include "common.h"
#include "log.h"
#include "stream.h"
#include "task/task.h"

/** The debug stream to write log messages to. */
extern FILE* TDB_debug_stream;

/** The log level to write messages for. */
extern TDB_loglevel TDB_debug_level;

int TDB_fdwriten(TDB_stream_ctx* stream_ctx, char* buf, int len)
{
    int rc = -1;

    LOG(TDB_LOG_INFO, "Entered TDB_fdwriten fd=%d, buf=`%s', len=`%d'.", stream_ctx->fd, buf, len);

    while(len > 0) {
        if((rc = fdwrite(stream_ctx->fd, buf, len)) <= 0) {
            if(rc < 0 && errno == EINTR) {
                rc = 0;
            }
            else if(rc < 0 && errno == EPIPE) {
                LOG(TDB_LOG_ERR, "The client unexpectedly disconnected.");
                return -1;
            }
            else {
                LOG(TDB_LOG_ERR, "An error occurred while writing to the socket (errno=%d). %s", \
                    errno, strerror(errno));
                return -1;
            }
        }

        len -= rc;
        buf += rc;
    }

    return rc;
}

int TDB_fdreadn(TDB_stream_ctx* stream_ctx, char* buf, int len)
{
    int rc = -1;

    while(len > 0) {
        rc = fdread(stream_ctx->fd, buf, len);

        if(rc < 0) {
            if(errno == EINTR) {
                continue;
            }

            LOG(TDB_LOG_ERR, "An error occurred while reading from the socket (errno=%d). %s", \
                errno, strerror(errno));
            return -1;
        }

        if(rc == 0) {
            LOG(TDB_LOG_ERR, "An error occurred while reading from the socket. Unexpected EOF.");
            return -1;
        }

        buf += rc;
        len -= rc;
    }

    return rc;
}

int TDB_init_xdr_stream(TDB_stream_ctx* stream_ctx)
{
    /* FIXME: Handle memory allocation failures in xdrrec_create. */
    xdrrec_create(&(stream_ctx->xdrs), 0, 0, (void*) stream_ctx, \
        (int (*)(char*,char*,int)) TDB_fdreadn, \
        (int (*)(char*,char*,int)) TDB_fdwriten);

    return TDB_SUCCESS;
}

{'fraction_non_alphanumeric': 0.12255257432922408, 'fraction_numerical': 0.007614213197969543, 'mean_word_length': 2.2038327526132404, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, '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': '6783131', 'n_tokens_mistral': 1828, 'n_tokens_neox': 1731, 'n_words': 545}