<commit_before>#if HAVE_CONFIG_H
#include "config.h"
#endif

#if HAVE_MALLOC_H
#include <malloc.h>
#endif

#if HAVE_STDLIB_H
#include <stdlib.h>
#endif

#if HAVE_MNTENT_H
#include <mntent.h>
#endif

#include <stdio.h>
#include <unistd.h>

#if HAVE_LINUX_CDROM_H
#include <linux/cdrom.h>
#endif

#include <sys/fcntl.h>
#include <sys/ioctl.h>

#include "mmc.h"
#include "crypto.h"
#include "../file/file.h"
#include "../util/macro.h"
#include "../util/logging.h"


static int _mmc_send_cmd(MMC *mmc, const uint8_t *cmd, uint8_t *buf, size_t tx, size_t rx)
{
#if HAVE_LINUX_CDROM_H
    if (mmc->fd >= 0) {
        struct cdrom_generic_command cgc;
        struct request_sense sense;

        memset(&cgc, 0, sizeof(cgc));
        memcpy(cgc.cmd, cmd, CDROM_PACKET_SIZE);
        cgc.sense = &sense;
        cgc.timeout = 5000;

        if (buf) {
            if (tx) {
                cgc.data_direction = CGC_DATA_WRITE;
                cgc.buflen = tx;
                cgc.buffer = buf;
            } else if (rx) {
                cgc.data_direction = CGC_DATA_READ;
                cgc.buflen = rx;
                cgc.buffer = buf;
            }
        } else {
            cgc.data_direction = CGC_DATA_NONE;
            cgc.buflen = 0;
            cgc.buffer = NULL;
        }

        int a = ioctl(mmc->fd, CDROM_SEND_PACKET, &cgc);

        DEBUG(DBG_MMC, "Send LINUX MMC cmd %s: (0x%08x)\n", print_hex((uint8_t *)cmd, 16), mmc);
        if (tx) {
            DEBUG(DBG_MMC, "  Buffer: %s -> (0x%08x)\n", print_hex(buf, tx), mmc);
        } else {
            DEBUG(DBG_MMC, "  Buffer: %s <- (0x%08x)\n", print_hex(buf, rx), mmc);
        }

        mmc->sk = sense.sense_key & 0x0f;
        mmc->asc = sense.asc;
        mmc->ascq = sense.ascq;

        if (a == 0) {
            DEBUG(DBG_MMC, "  Send succeeded! (0x%08x)\n", mmc);
            return 1;
        } else {
            DEBUG(DBG_MMC, "  Send failed! [%d] (0x%08x)\n", a, mmc);
        }
    }
#endif

    return 0;
}
<commit_msg>_mmc_send_cmd(): - fixed checking ioctl() return value: some ioctls return > 0 on success (number of bytes etc.) - added verbose error messages when ioctl fails<commit_after>#if HAVE_CONFIG_H
#include "config.h"
#endif

#if HAVE_MALLOC_H
#include <malloc.h>
#endif

#if HAVE_STDLIB_H
#include <stdlib.h>
#endif

#if HAVE_MNTENT_H
#include <mntent.h>
#endif

#include <stdio.h>
#include <unistd.h>

#include <errno.h>  /* errno */
#include <string.h> /* strerror() */

#if HAVE_LINUX_CDROM_H
#include <linux/cdrom.h>
#endif

#include <sys/fcntl.h>
#include <sys/ioctl.h>

#include "mmc.h"
#include "crypto.h"
#include "../file/file.h"
#include "../util/macro.h"
#include "../util/logging.h"


static int _mmc_send_cmd(MMC *mmc, const uint8_t *cmd, uint8_t *buf, size_t tx, size_t rx)
{
#if HAVE_LINUX_CDROM_H
    if (mmc->fd >= 0) {
        struct cdrom_generic_command cgc;
        struct request_sense sense;

        memset(&cgc, 0, sizeof(cgc));
        memcpy(cgc.cmd, cmd, CDROM_PACKET_SIZE);
        cgc.sense = &sense;
        cgc.timeout = 5000;

        if (buf) {
            if (tx) {
                cgc.data_direction = CGC_DATA_WRITE;
                cgc.buflen = tx;
                cgc.buffer = buf;
            } else if (rx) {
                cgc.data_direction = CGC_DATA_READ;
                cgc.buflen = rx;
                cgc.buffer = buf;
            }
        } else {
            cgc.data_direction = CGC_DATA_NONE;
            cgc.buflen = 0;
            cgc.buffer = NULL;
        }

        int a = ioctl(mmc->fd, CDROM_SEND_PACKET, &cgc);

        DEBUG(DBG_MMC, "Send LINUX MMC cmd %s: (0x%08x)\n", print_hex((uint8_t *)cmd, 16), mmc);
        if (tx) {
            DEBUG(DBG_MMC, "  Buffer: %s -> (0x%08x)\n", print_hex(buf, tx), mmc);
        } else {
            DEBUG(DBG_MMC, "  Buffer: %s <- (0x%08x)\n", print_hex(buf, rx), mmc);
        }

        mmc->sk = sense.sense_key & 0x0f;
        mmc->asc = sense.asc;
        mmc->ascq = sense.ascq;

        if (a >= 0) {
            DEBUG(DBG_MMC, "  Send succeeded! [%d] (0x%08x)\n", mmc, a);
            return 1;
        }

        DEBUG(DBG_MMC, "  Send failed! [%d] %s (0x%08x)\n", a, strerror(errno), mmc);
    }
#endif

    return 0;
}

{'fraction_non_alphanumeric': 0.14835813843609733, 'fraction_numerical': 0.01535553980628396, 'mean_word_length': 2.013523131672598, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 37, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '4466530', 'n_tokens_mistral': 1724, 'n_tokens_neox': 1633, 'n_words': 359}