<commit_before>/*
 * Copyright (C) SUSE LINUX GmbH 2013-2016, 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 "lib/file/file_api.h"
#include "ccan/list/list.h"
#include "cli_common.h"
#include "cli_open.h"

struct cli_cp_args {
    char *src_path;
    char *dst_path;
};

static void
_cli_cp_args_free(struct cli_cp_args *cp_args) {
	if (cp_args == NULL) {
		return;
	}

	free(cp_args->src_path);
	free(cp_args->dst_path);
	free(cp_args);
}

static void
cli_cp_args_free(struct cli_args *cli_args)
{
	_cli_cp_args_free(cli_args->cmd_priv);
	cli_args->cmd_priv = NULL;
}

static int
cli_cp_args_parse(int argc,
		  char * const *argv,
		  struct cli_args *cli_args)
{
	int ret;
	struct cli_cp_args *cp_args = NULL;

	if ((cli_args->auth.type != ELASTO_FILE_ABB)
	 && (cli_args->auth.type != ELASTO_FILE_APB)
	 && (cli_args->auth.type != ELASTO_FILE_AFS)
	 && (cli_args->auth.type != ELASTO_FILE_S3)) {
		ret = -ENOTSUP;
		goto err_out;
	}

	cp_args = calloc(1, sizeof(*cp_args));
	if (cp_args == NULL) {
		goto err_out;
	}

	/* paths parsed by libfile on open */
	ret = cli_path_realize(cli_args->cwd, argv[1], &cp_args->src_path);
	if (ret < 0) {
		goto err_free;
	}

	ret = cli_path_realize(cli_args->cwd, argv[2], &cp_args->dst_path);
	if (ret < 0) {
		goto err_free;
	}
	cli_args->cmd_priv = cp_args;

	return 0;

err_free:
	_cli_cp_args_free(cp_args);
err_out:
	return ret;
}

static int
cli_cp_handle(struct cli_args *cli_args)
{
	struct elasto_fh *src_fh;
	struct elasto_fh *dest_fh;
	struct elasto_fstat fstat;
	struct cli_cp_args *cp_args = cli_args->cmd_priv;
	int ret;

	/* open source without create or dir flags */
	ret = cli_open_efh(cli_args, cp_args->src_path, 0, NULL, &src_fh);
	if (ret < 0) {
		printf("%s path open failed with: %s\n",
		       cp_args->src_path, strerror(-ret));
		goto err_out;
	}

	/* stat to determine size to copy */
	ret = elasto_fstat(src_fh, &fstat);
	if (ret < 0) {
		printf("stat failed with: %s\n", strerror(-ret));
		goto err_src_close;
	}

	/* open dest with create flag */
	ret = cli_open_efh(cli_args, cp_args->dst_path, ELASTO_FOPEN_CREATE,
			   NULL, &dest_fh);
	if (ret < 0) {
		printf("%s path open failed with: %s\n",
		       cp_args->dst_path, strerror(-ret));
		goto err_src_close;
	}

	printf("copying %" PRIu64 " bytes from %s to %s\n",
	       fstat.size, cp_args->src_path, cp_args->dst_path);

	ret = elasto_fsplice(src_fh, 0, dest_fh, 0, fstat.size);
	if (ret < 0) {
		printf("copy failed with: %s\n", strerror(-ret));
		goto err_dest_close;
	}

	ret = 0;
err_dest_close:
	if (elasto_fclose(dest_fh) < 0) {
		printf("dest close failed\n");
	}
err_src_close:
	if (elasto_fclose(src_fh) < 0) {
		printf("src close failed\n");
	}
err_out:
	return ret;<commit_msg>cli_cp: use src content-type for dst<commit_after>/*
 * Copyright (C) SUSE LINUX GmbH 2013-2016, 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 "lib/file/file_api.h"
#include "ccan/list/list.h"
#include "cli_common.h"
#include "cli_open.h"

struct cli_cp_args {
    char *src_path;
    char *dst_path;
};

static void
_cli_cp_args_free(struct cli_cp_args *cp_args) {
	if (cp_args == NULL) {
		return;
	}

	free(cp_args->src_path);
	free(cp_args->dst_path);
	free(cp_args);
}

static void
cli_cp_args_free(struct cli_args *cli_args)
{
	_cli_cp_args_free(cli_args->cmd_priv);
	cli_args->cmd_priv = NULL;
}

static int
cli_cp_args_parse(int argc,
		  char * const *argv,
		  struct cli_args *cli_args)
{
	int ret;
	struct cli_cp_args *cp_args = NULL;

	if ((cli_args->auth.type != ELASTO_FILE_ABB)
	 && (cli_args->auth.type != ELASTO_FILE_APB)
	 && (cli_args->auth.type != ELASTO_FILE_AFS)
	 && (cli_args->auth.type != ELASTO_FILE_S3)) {
		ret = -ENOTSUP;
		goto err_out;
	}

	cp_args = calloc(1, sizeof(*cp_args));
	if (cp_args == NULL) {
		goto err_out;
	}

	/* paths parsed by libfile on open */
	ret = cli_path_realize(cli_args->cwd, argv[1], &cp_args->src_path);
	if (ret < 0) {
		goto err_free;
	}

	ret = cli_path_realize(cli_args->cwd, argv[2], &cp_args->dst_path);
	if (ret < 0) {
		goto err_free;
	}
	cli_args->cmd_priv = cp_args;

	return 0;

err_free:
	_cli_cp_args_free(cp_args);
err_out:
	return ret;
}

static int
cli_cp_handle(struct cli_args *cli_args)
{
	struct elasto_fh *src_fh;
	struct elasto_fh *dest_fh;
	struct elasto_fstat fstat;
	struct cli_cp_args *cp_args = cli_args->cmd_priv;
	int ret;
	struct elasto_ftoken_list *toks = NULL;

	/* open source without create or dir flags */
	ret = cli_open_efh(cli_args, cp_args->src_path, 0, NULL, &src_fh);
	if (ret < 0) {
		printf("%s path open failed with: %s\n",
		       cp_args->src_path, strerror(-ret));
		goto err_out;
	}

	/* stat to determine size to copy and content-type for dest */
	ret = elasto_fstat(src_fh, &fstat);
	if (ret < 0) {
		printf("stat failed with: %s\n", strerror(-ret));
		goto err_src_close;
	}

	if (strlen(fstat.content_type) != 0) {
		ret = elasto_ftoken_add(ELASTO_FOPEN_TOK_CREATE_CONTENT_TYPE,
					fstat.content_type, &toks);
		if (ret < 0) {
			goto err_src_close;
		}
	}

	/* open dest with create flag */
	ret = cli_open_efh(cli_args, cp_args->dst_path, ELASTO_FOPEN_CREATE,
			   toks, &dest_fh);
	if (ret < 0) {
		printf("%s path open failed with: %s\n",
		       cp_args->dst_path, strerror(-ret));
		goto err_src_close;
	}

	printf("copying %" PRIu64 " bytes from %s to %s\n",
	       fstat.size, cp_args->src_path, cp_args->dst_path);

	ret = elasto_fsplice(src_fh, 0, dest_fh, 0, fstat.size);
	if (ret < 0) {
		printf("copy failed with: %s\n", strerror(-ret));
		goto err_dest_close;
	}

	ret = 0;
err_dest_close:
	if (elasto_fclose(dest_fh) < 0) {
		printf("dest close failed\n");
	}
err_src_close:
	if (elasto_fclose(src_fh) < 0) {
		printf("src close failed\n");
	}
err_out:
	return ret;
{'fraction_non_alphanumeric': 0.13397195612939053, 'fraction_numerical': 0.008607524642510066, 'mean_word_length': 4.080394922425952, 'pattern_counts': {'":': 0, '<': 44, '<?xml version=': 0, '>': 67, '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': '7270665', 'n_tokens_mistral': 3053, 'n_tokens_neox': 2882, 'n_words': 728}