<commit_before>/*
 * This File is part of Davix, The IO library for HTTP based protocols
 * Copyright (C) CERN 2013  
 * Author: <NAME> <<EMAIL>>
 *
 * 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) any later version.
 *
 * 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.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 *
*/

#include <davix.hpp>
#include <davix_internal.hpp>
#include <tools/davix_tool_params.hpp>
#include <tools/davix_tool_util.hpp>
#include <tools/davix_taskqueue.hpp>
#include <tools/davix_op.hpp>
#include <tools/davix_thread_pool.hpp>
#include <utils/davix_logger_internal.hpp>
#include <cstdio>


// @author : <NAME>
// main file for davix-put operation


using namespace Davix;
using namespace std;

#define READ_BLOCK_SIZE 4096


const std::string scope_put = "Davix::Tools::davix-put";

std::string  get_base_put_options(){
    return "  Put Options:\n"
           "\t-r NUMBER_OF_THREADS:     Upload directories and their contents recursively.\n"; 
}

static std::string help_msg(const std::string & cmd_path){
    std::string help_msg = fmt::format("Usage : {} ", cmd_path);
    help_msg += Tool::get_put_description_options();
    help_msg += Tool::get_common_options();
    help_msg += get_base_put_options();

    return help_msg;
}


static int execute_put(Context& c, const Tool::OptParams & opts, int fd, DavixError** err){
        const std::string &  src_file = opts.input_file_path;
        const std::string &  dst_file = opts.vec_arg[1];

        TRY_DAVIX{
            DavFile f(c, dst_file);
            struct stat st;
            if( fstat(fd, &st) != 0){
                errno_to_davix_exception(errno, scope_put, std::string("for source file ").append(src_file));
            }
            if( S_ISREG(st.st_mode) && st.st_size > 0){
                    f.put(&opts.params, fd, static_cast<dav_size_t>(st.st_size));
                    return 0;
            }
            throw DavixException(scope_put, StatusCode::SystemError, std::string(dst_file).append("is not a valid regular file"));
      }CATCH_DAVIX(err);
      return -1;
}


static int tryMakeCollection(Context& c, const Tool::OptParams & opts, std::string dst_path){
    DavixError* err=NULL;
    DavFile f(c,dst_path);
    f.makeCollection(&opts.params, &err);

    if(err){
        if(err->getStatus() == StatusCode::FileExist){
            std::cerr << std::endl << scope_put << " " << dst_path << " already exists, continuing..." << std::endl;
            DavixError::clearError(&err);
        }
        else{
            Tool::errorPrint(&err);
            return -1;
        }
    }<commit_msg>DMC-718 - Remove restriction on uploading files with 0 size in davix-put. Clarify error message if source is not regular file.<commit_after>/*
 * This File is part of Davix, The IO library for HTTP based protocols
 * Copyright (C) CERN 2013  
 * Author: <NAME> <<EMAIL>>
 *
 * 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) any later version.
 *
 * 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.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 *
*/

#include <davix.hpp>
#include <davix_internal.hpp>
#include <tools/davix_tool_params.hpp>
#include <tools/davix_tool_util.hpp>
#include <tools/davix_taskqueue.hpp>
#include <tools/davix_op.hpp>
#include <tools/davix_thread_pool.hpp>
#include <utils/davix_logger_internal.hpp>
#include <cstdio>


// @author : <NAME>
// main file for davix-put operation


using namespace Davix;
using namespace std;

#define READ_BLOCK_SIZE 4096


const std::string scope_put = "Davix::Tools::davix-put";

std::string  get_base_put_options(){
    return "  Put Options:\n"
           "\t-r NUMBER_OF_THREADS:     Upload directories and their contents recursively.\n"; 
}

static std::string help_msg(const std::string & cmd_path){
    std::string help_msg = fmt::format("Usage : {} ", cmd_path);
    help_msg += Tool::get_put_description_options();
    help_msg += Tool::get_common_options();
    help_msg += get_base_put_options();

    return help_msg;
}


static int execute_put(Context& c, const Tool::OptParams & opts, int fd, DavixError** err){
        const std::string &  src_file = opts.input_file_path;
        const std::string &  dst_file = opts.vec_arg[1];

        TRY_DAVIX{
            DavFile f(c, dst_file);
            struct stat st;
            if( fstat(fd, &st) != 0){
                errno_to_davix_exception(errno, scope_put, std::string("for source file ").append(src_file));
            }
            if( S_ISREG(st.st_mode) && (st.st_size >= 0)){
                    f.put(&opts.params, fd, static_cast<dav_size_t>(st.st_size));
                    return 0;
            }
            throw DavixException(scope_put, StatusCode::SystemError, std::string(src_file).append("is not a valid regular file"));
      }CATCH_DAVIX(err);
      return -1;
}


static int tryMakeCollection(Context& c, const Tool::OptParams & opts, std::string dst_path){
    DavixError* err=NULL;
    DavFile f(c,dst_path);
    f.makeCollection(&opts.params, &err);

    if(err){
        if(err->getStatus() == StatusCode::FileExist){
            std::cerr << std::endl << scope_put << " " << dst_path << " already exists, continuing..." << std::endl;
            DavixError::clearError(&err);
        }
        else{
            Tool::errorPrint(&err);
            return -1;
        }
    }
{'fraction_non_alphanumeric': 0.1154320987654321, 'fraction_numerical': 0.008950617283950617, 'mean_word_length': 3.181290322580645, 'pattern_counts': {'":': 0, '<': 55, '<?xml version=': 0, '>': 35, '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': '2250007', 'n_tokens_mistral': 2056, 'n_tokens_neox': 1952, 'n_words': 605}