<commit_before>#include <errno.h>
#include <stdio.h>
#include <unistd.h>
#include <getopt.h>

#include "storj.h"

extern int errno;

#define HELP_TEXT "usage: storj [<options>] <command> [<args>]\n\n"     \
    "These are common Storj commands for various situations:\n\n"       \
    "working with buckets and files\n"                                  \
    "  get-info\n"                                                      \
    "  list-buckets\n"                                                  \
    "  list-files <bucket-id>\n"                                        \
    "  remove-file <bucket-id> <file-id>\n"                             \
    "  add-bucket <name> \n"                                            \
    "  remove-bucket <bucket-id>\n\n"                                   \
    "downloading and uploading files\n"                                 \
    "  upload-file <bucket-id> <path>\n"                                \
    "  download-file <bucket-id> <file-id> <path>\n\n"                  \
    "options:\n\n"                                                      \
    "  -h, --help                output usage information\n"            \
    "  -V, --version             output the version number\n"           \
    "  -u, --url <url>           set the base url for the api\n\n"      \

static void upload_file_progress(double progress)
{
    // TODO assersions
}

static void upload_file_complete(int status)
{
    if (status != 0) {
        printf("Upload failure: %s\n", storj_strerror(status));
        exit(status);
    }

    printf("Upload Success!\n");
    exit(0);
}

static int upload_file(storj_env_t *env, char *bucket_id, char *file_path)
{
    char *mnemonic = getenv("STORJ_MNEMONIC");
    if (!mnemonic) {
        printf("Set your STORJ_MNEMONIC\n");
        exit(1);
        // "abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about"
    }

    storj_upload_opts_t upload_opts = {
        .file_concurrency = 1,
        .shard_concurrency = 3,
        .bucket_id = bucket_id,
        .file_path = file_path,
        .key_pass = "password",
        .mnemonic = mnemonic
    };

    int status = storj_bridge_store_file(env, &upload_opts,
                                     upload_file_progress,
                                     upload_file_complete);

    return status;
}


static void download_file_progress(double progress)
{
    // TODO
}

static void download_file_complete(int status, FILE *fd)
{
    fclose(fd);
    if (status) {
        printf("Download failure: %s\n", storj_strerror(status));
        exit(status);
    }
    exit(0);
}

static int download_file(storj_env_t *env, char *bucket_id,
                         char *file_id, char *path)
{
    FILE *fd = fopen(path, "w+");

    if (fd == NULL) {
        printf("Unable to open %s: %s\n", path, strerror(errno));
        return 1;
    }

    int status = storj_bridge_resolve_file(env, bucket_id, file_id, fd,
                                           download_file_progress,
                                           download_file_complete);

    return status;

}

static void list_files_callback(uv_work_t *work_req, int status)
{
    assert(status == 0);
    json_request_t *req = work_req->data;

    if (req->response == NULL) {
        free(req);
        free(work_req);
        printf("Failed to list files.\n");
        exit(1);
    }
    int num_files = json_object_array_length(req->response);
    struct json_object *file;
    struct json_object *filename;
    struct json_object *mimetype;
    struct json_object *size;
    struct json_object *id;

    for (int i = 0; i < num_files; i++) {<commit_msg>Report status code in list files, and print if no files found.<commit_after>#include <errno.h>
#include <stdio.h>
#include <unistd.h>
#include <getopt.h>

#include "storj.h"

extern int errno;

#define HELP_TEXT "usage: storj [<options>] <command> [<args>]\n\n"     \
    "These are common Storj commands for various situations:\n\n"       \
    "working with buckets and files\n"                                  \
    "  get-info\n"                                                      \
    "  list-buckets\n"                                                  \
    "  list-files <bucket-id>\n"                                        \
    "  remove-file <bucket-id> <file-id>\n"                             \
    "  add-bucket <name> \n"                                            \
    "  remove-bucket <bucket-id>\n\n"                                   \
    "downloading and uploading files\n"                                 \
    "  upload-file <bucket-id> <path>\n"                                \
    "  download-file <bucket-id> <file-id> <path>\n\n"                  \
    "options:\n\n"                                                      \
    "  -h, --help                output usage information\n"            \
    "  -V, --version             output the version number\n"           \
    "  -u, --url <url>           set the base url for the api\n\n"      \

static void upload_file_progress(double progress)
{
    // TODO assersions
}

static void upload_file_complete(int status)
{
    if (status != 0) {
        printf("Upload failure: %s\n", storj_strerror(status));
        exit(status);
    }

    printf("Upload Success!\n");
    exit(0);
}

static int upload_file(storj_env_t *env, char *bucket_id, char *file_path)
{
    char *mnemonic = getenv("STORJ_MNEMONIC");
    if (!mnemonic) {
        printf("Set your STORJ_MNEMONIC\n");
        exit(1);
        // "abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about"
    }

    storj_upload_opts_t upload_opts = {
        .file_concurrency = 1,
        .shard_concurrency = 3,
        .bucket_id = bucket_id,
        .file_path = file_path,
        .key_pass = "password",
        .mnemonic = mnemonic
    };

    int status = storj_bridge_store_file(env, &upload_opts,
                                     upload_file_progress,
                                     upload_file_complete);

    return status;
}


static void download_file_progress(double progress)
{
    // TODO
}

static void download_file_complete(int status, FILE *fd)
{
    fclose(fd);
    if (status) {
        printf("Download failure: %s\n", storj_strerror(status));
        exit(status);
    }
    exit(0);
}

static int download_file(storj_env_t *env, char *bucket_id,
                         char *file_id, char *path)
{
    FILE *fd = fopen(path, "w+");

    if (fd == NULL) {
        printf("Unable to open %s: %s\n", path, strerror(errno));
        return 1;
    }

    int status = storj_bridge_resolve_file(env, bucket_id, file_id, fd,
                                           download_file_progress,
                                           download_file_complete);

    return status;

}

static void list_files_callback(uv_work_t *work_req, int status)
{
    assert(status == 0);
    json_request_t *req = work_req->data;

    if (req->status_code != 200) {
        printf("Request failed with status code %i", req->status_code);
    }

    if (req->response == NULL) {
        free(req);
        free(work_req);
        printf("Failed to list files.\n");
        exit(1);
    }
    int num_files = json_object_array_length(req->response);

    if (num_files == 0) {
        printf("No files for bucket");
    }

    struct json_object *file;
    struct json_object *filename;
    struct json_object *mimetype;
    struct json_object *size;
    struct json_object *id;

    for (int i = 0; i < num_files; i++) {
{'fraction_non_alphanumeric': 0.1126612266385891, 'fraction_numerical': 0.0031587259805211897, 'mean_word_length': 1.537228714524207, 'pattern_counts': {'":': 0, '<': 41, '<?xml version=': 0, '>': 47, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 48, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5720576', 'n_tokens_mistral': 2315, 'n_tokens_neox': 2239, 'n_words': 532}