<commit_before>/*
 * Copyright (C) SUSE LINUX GmbH 2012-2015, all rights reserved.
 *
 * 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) version 3.
 *
 * 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.
 */
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdbool.h>
#include <errno.h>
#include <string.h>
#include <assert.h>
#include <time.h>
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <inttypes.h>

#include "ccan/list/list.h"
#include "lib/exml.h"
#include "lib/data_api.h"
#include "lib/file/file_api.h"
#include "lib/op.h"
#include "lib/azure_blob_req.h"
#include "lib/s3_req.h"
#include "lib/conn.h"
#include "lib/azure_ssl.h"
#include "cli_common.h"
#include "cli_sign.h"
#include "cli_get.h"

void
cli_get_args_free(struct cli_args *cli_args)
{
	free(cli_args->path);
	free(cli_args->get.local_path);
}

int
cli_get_args_parse(int argc,
		   char * const *argv,
		   struct cli_args *cli_args)
{
	int ret;

	/* path is parsed by libfile on open */
	cli_args->path = strdup(argv[1]);
	if (cli_args->path == NULL) {
		ret = -ENOMEM;
		goto err_out;
	}

	cli_args->get.local_path = strdup(argv[2]);
	if (cli_args->get.local_path == NULL) {
		ret = -ENOMEM;
		goto err_path_free;
	}

	cli_args->cmd = CLI_CMD_GET;

	return 0;

err_path_free:
	free(cli_args->path);
err_out:
	return ret;
}

struct cli_get_data_ctx {
	int fd;
	char *path;
	uint64_t len;
};

static int
cli_get_data_in_cb(uint64_t stream_off,
		   uint64_t got,
		   uint8_t *in_buf,
		   uint64_t buf_len,
		   void *priv)
{
	struct cli_get_data_ctx *data_ctx = priv;
	size_t wrote;
	int ret;

	wrote = pwrite(data_ctx->fd, in_buf, got, stream_off);
	if ((wrote == -1) || (wrote != got)) {
		printf("write callback failed: %s\n", strerror(errno));
		ret = -EBADF;
		goto err_out;
	}

	free(in_buf);

	ret = 0;
err_out:
	return ret;
}

static int
cli_get_data_setup(const char *path,
		   uint64_t len,
		   struct elasto_data **_data)
{
	struct elasto_data *data;
	struct cli_get_data_ctx *data_ctx;
	int ret;

	data_ctx = malloc(sizeof(*data_ctx));
	if (data_ctx == NULL) {
		ret = -ENOMEM;
		goto err_out;
	}

	data_ctx->fd = open(path, (O_CREAT | O_WRONLY),
			    (S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH));
	if (data_ctx->fd == -1) {
		ret = -errno;
		goto err_ctx_free;
	}

	/* TODO fallocate file */<commit_msg>cli/get: fallocate file before download<commit_after>/*
 * Copyright (C) SUSE LINUX GmbH 2012-2015, all rights reserved.
 *
 * 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) version 3.
 *
 * 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.
 */
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdbool.h>
#include <errno.h>
#include <string.h>
#include <assert.h>
#include <time.h>
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <inttypes.h>

#include "ccan/list/list.h"
#include "lib/exml.h"
#include "lib/data_api.h"
#include "lib/file/file_api.h"
#include "lib/op.h"
#include "lib/azure_blob_req.h"
#include "lib/s3_req.h"
#include "lib/conn.h"
#include "lib/azure_ssl.h"
#include "cli_common.h"
#include "cli_sign.h"
#include "cli_get.h"

void
cli_get_args_free(struct cli_args *cli_args)
{
	free(cli_args->path);
	free(cli_args->get.local_path);
}

int
cli_get_args_parse(int argc,
		   char * const *argv,
		   struct cli_args *cli_args)
{
	int ret;

	/* path is parsed by libfile on open */
	cli_args->path = strdup(argv[1]);
	if (cli_args->path == NULL) {
		ret = -ENOMEM;
		goto err_out;
	}

	cli_args->get.local_path = strdup(argv[2]);
	if (cli_args->get.local_path == NULL) {
		ret = -ENOMEM;
		goto err_path_free;
	}

	cli_args->cmd = CLI_CMD_GET;

	return 0;

err_path_free:
	free(cli_args->path);
err_out:
	return ret;
}

struct cli_get_data_ctx {
	int fd;
	char *path;
	uint64_t len;
};

static int
cli_get_data_in_cb(uint64_t stream_off,
		   uint64_t got,
		   uint8_t *in_buf,
		   uint64_t buf_len,
		   void *priv)
{
	struct cli_get_data_ctx *data_ctx = priv;
	size_t wrote;
	int ret;

	wrote = pwrite(data_ctx->fd, in_buf, got, stream_off);
	if ((wrote == -1) || (wrote != got)) {
		printf("write callback failed: %s\n", strerror(errno));
		ret = -EBADF;
		goto err_out;
	}

	free(in_buf);

	ret = 0;
err_out:
	return ret;
}

static int
cli_get_data_setup(const char *path,
		   uint64_t len,
		   struct elasto_data **_data)
{
	struct elasto_data *data;
	struct cli_get_data_ctx *data_ctx;
	int ret;

	data_ctx = malloc(sizeof(*data_ctx));
	if (data_ctx == NULL) {
		ret = -ENOMEM;
		goto err_out;
	}

	data_ctx->fd = open(path, (O_CREAT | O_WRONLY),
			    (S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH));
	if (data_ctx->fd == -1) {
		ret = -errno;
		goto err_ctx_free;
	}

	ret = fallocate(data_ctx->fd, 0, 0, len);
	if (ret < 0) {
		printf("fallocate failed: %s\n", strerror(errno));
		ret = -EBADF;
		goto err_fd_close;
	}
{'fraction_non_alphanumeric': 0.13301196214961614, 'fraction_numerical': 0.010890912337082663, 'mean_word_length': 3.93568281938326, 'pattern_counts': {'":': 0, '<': 28, '<?xml version=': 0, '>': 50, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1305047', 'n_tokens_mistral': 2351, 'n_tokens_neox': 2213, 'n_words': 591}