<commit_before>// =-=-=-=-=-=-=-
// irods includes
#include "msParam.hpp"
#include "reGlobalsExtern.hpp"
#include "rcConnect.hpp"
#include "readServerConfig.hpp"
#include "miscServerFunct.hpp"
#include "generalAdmin.hpp"

// =-=-=-=-=-=-=-
#include "irods_resource_plugin.hpp"
#include "irods_file_object.hpp"
#include "irods_physical_object.hpp"
#include "irods_collection_object.hpp"
#include "irods_string_tokenize.hpp"
#include "irods_hierarchy_parser.hpp"
#include "irods_resource_redirect.hpp"
#include "irods_stacktrace.hpp"
#include "irods_server_properties.hpp"
#include "irods_hierarchy_parser.hpp"

// =-=-=-=-=-=-=-
// stl includes
#include <iostream>
#include <sstream>
#include <vector>
#include <string>

// =-=-=-=-=-=-=-
// boost includes
#include <boost/function.hpp>
#include <boost/any.hpp>
#include <boost/lexical_cast.hpp>

// =-=-=-=-=-=-=-
// system includes
#ifndef _WIN32
#include <sys/file.h>
#include <sys/param.h>
#endif
#include <errno.h>
#include <sys/stat.h>
#include <string.h>
#ifndef _WIN32
#include <unistd.h>
#endif
#include <sys/types.h>
#if defined(osx_platform)
#include <sys/malloc.h>
#else
#include <malloc.h>
#endif
#include <fcntl.h>
#ifndef _WIN32
#include <sys/file.h>
#include <unistd.h>
#endif
#include <dirent.h>

#if defined(solaris_platform)
#include <sys/statvfs.h>
#endif

#if defined(osx_platform)
#include <sys/param.h>
#include <sys/mount.h>
#endif

#if defined(linux_platform)
#include <sys/vfs.h>
#endif
#include <sys/stat.h>

#include <string.h>
#include <sstream>


// =-=-=-=-=-=-=-
// 1. Define utility functions that the operations might need

// =-=-=-=-=-=-=-
// NOTE: All storage resources must do this on the physical path stored in the file object and then update
//       the file object's physical path with the full path

static irods::error unix_file_copy_plugin(
    int mode,
    const char* srcFileName,
    const char* destFileName ) {

    irods::error result = SUCCESS();

    struct stat statbuf;
    int status = stat( srcFileName, &statbuf );
    int err_status = UNIX_FILE_STAT_ERR - errno;
    if ( ( result = ASSERT_ERROR( status >= 0, err_status, "Stat of \"%s\" error, status = %d",
                                  srcFileName, err_status ) ).ok() ) {

        int inFd = open( srcFileName, O_RDONLY, 0 );
        err_status = UNIX_FILE_OPEN_ERR - errno;
        if ( inFd < 0 ) {
            std::stringstream msg_stream;
            msg_stream << "Open error for srcFileName \"" << srcFileName << "\", status = " << status;
            result = ERROR( err_status, msg_stream.str() );
        }
        else if ( statbuf.st_mode & S_IFREG == 0 ) {
            close( inFd ); // JMC cppcheck - resource
            std::stringstream msg_stream;
            msg_stream << "srcFileName \"" << srcFileName << "\" is not a regular file.";
            result = ERROR( UNIX_FILE_STAT_ERR, msg_stream.str() );
        }
        else {
            int outFd = open( destFileName, O_WRONLY | O_CREAT | O_TRUNC, mode );
            err_status = UNIX_FILE_OPEN_ERR - errno;
            if ( outFd < 0 ) {
                std::stringstream msg_stream;
                msg_stream << "Open error for destFileName \"" << destFileName << "\", status = " << status;
                result = ERROR( err_status, msg_stream.str() );
            }
            else if ( statbuf.st_mode & S_IFREG == 0 ) {
                close( outFd ); // JMC cppcheck - resource
                std::stringstream msg_stream;
                msg_stream << "destFileName \"" << destFileName << "\" is not a regular file.";
                result = ERROR( UNIX_FILE_STAT_ERR, msg_stream.str() );
            }
            else {
                char myBuf[TRANS_BUF_SZ];
                int bytesRead;
                rodsLong_t bytesCopied = 0;
                while ( result.ok() && ( bytesRead = read( inFd, ( void * ) myBuf, TRANS_BUF_SZ ) ) > 0 ) {
                    int bytesWritten = write( outFd, ( void * ) myBuf, bytesRead );
                    err_status = UNIX_FILE_WRITE_ERR - errno;
                    if ( ( result = ASSERT_ERROR( bytesWritten > 0, err_status, "Write error for srcFileName %s, status = %d",
                                                  destFileName, status ) ).ok() ) {
                        bytesCopied += bytesWritten;
                    }
                }

                close( inFd );
                close( outFd );

                if ( result.ok() ) {
                    result = ASSERT_ERROR( bytesCopied == statbuf.st_size, SYS_COPY_LEN_ERR, "Copied size %lld does not match source size %lld of %s",
                                           bytesCopied, statbuf.st_size, srcFileName );
                }
            }
        }
    }
    return result;
}



// =-=-=-=-=-=-=-
/// @brief Generates a full path name from the partial physical path and the specified resource's vault path
irods::error unix_generate_full_path(
    irods::plugin_property_map& _prop_map,
    const std::string&           _phy_path,
    std::string&                 _ret_string ) {
    irods::error result = SUCCESS();
    irods::error ret;
    std::string vault_path;
    // TODO - getting vault path by property will not likely work for coordinating nodes
    ret = _prop_map.get<std::string>( irods::RESOURCE_PATH, vault_path );
    if ( ( result = ASSERT_ERROR( ret.ok(), SYS_INVALID_INPUT_PARAM, "resource has no vault path." ) ).ok() ) {
        if ( _phy_path.compare( 0, 1, "/" ) != 0 &&
                _phy_path.compare( 0, vault_path.size(), vault_path ) != 0 ) {
            _ret_string  = vault_path;
            _ret_string += "/";
            _ret_string += _phy_path;
        }<commit_msg>[#2212] CID37580:<commit_after>// =-=-=-=-=-=-=-
// irods includes
#include "msParam.hpp"
#include "reGlobalsExtern.hpp"
#include "rcConnect.hpp"
#include "readServerConfig.hpp"
#include "miscServerFunct.hpp"
#include "generalAdmin.hpp"

// =-=-=-=-=-=-=-
#include "irods_resource_plugin.hpp"
#include "irods_file_object.hpp"
#include "irods_physical_object.hpp"
#include "irods_collection_object.hpp"
#include "irods_string_tokenize.hpp"
#include "irods_hierarchy_parser.hpp"
#include "irods_resource_redirect.hpp"
#include "irods_stacktrace.hpp"
#include "irods_server_properties.hpp"
#include "irods_hierarchy_parser.hpp"

// =-=-=-=-=-=-=-
// stl includes
#include <iostream>
#include <sstream>
#include <vector>
#include <string>

// =-=-=-=-=-=-=-
// boost includes
#include <boost/function.hpp>
#include <boost/any.hpp>
#include <boost/lexical_cast.hpp>

// =-=-=-=-=-=-=-
// system includes
#ifndef _WIN32
#include <sys/file.h>
#include <sys/param.h>
#endif
#include <errno.h>
#include <sys/stat.h>
#include <string.h>
#ifndef _WIN32
#include <unistd.h>
#endif
#include <sys/types.h>
#if defined(osx_platform)
#include <sys/malloc.h>
#else
#include <malloc.h>
#endif
#include <fcntl.h>
#ifndef _WIN32
#include <sys/file.h>
#include <unistd.h>
#endif
#include <dirent.h>

#if defined(solaris_platform)
#include <sys/statvfs.h>
#endif

#if defined(osx_platform)
#include <sys/param.h>
#include <sys/mount.h>
#endif

#if defined(linux_platform)
#include <sys/vfs.h>
#endif
#include <sys/stat.h>

#include <string.h>
#include <sstream>


// =-=-=-=-=-=-=-
// 1. Define utility functions that the operations might need

// =-=-=-=-=-=-=-
// NOTE: All storage resources must do this on the physical path stored in the file object and then update
//       the file object's physical path with the full path

static irods::error unix_file_copy_plugin(
    int mode,
    const char* srcFileName,
    const char* destFileName ) {

    irods::error result = SUCCESS();

    struct stat statbuf;
    int status = stat( srcFileName, &statbuf );
    int err_status = UNIX_FILE_STAT_ERR - errno;
    if ( ( result = ASSERT_ERROR( status >= 0, err_status, "Stat of \"%s\" error, status = %d",
                                  srcFileName, err_status ) ).ok() ) {

        int inFd = open( srcFileName, O_RDONLY, 0 );
        err_status = UNIX_FILE_OPEN_ERR - errno;
        if ( inFd < 0 ) {
            std::stringstream msg_stream;
            msg_stream << "Open error for srcFileName \"" << srcFileName << "\", status = " << status;
            result = ERROR( err_status, msg_stream.str() );
        }
        else if ( statbuf.st_mode & S_IFREG == 0 ) {
            close( inFd ); // JMC cppcheck - resource
            std::stringstream msg_stream;
            msg_stream << "srcFileName \"" << srcFileName << "\" is not a regular file.";
            result = ERROR( UNIX_FILE_STAT_ERR, msg_stream.str() );
        }
        else {
            int outFd = open( destFileName, O_WRONLY | O_CREAT | O_TRUNC, mode );
            err_status = UNIX_FILE_OPEN_ERR - errno;
            if ( outFd < 0 ) {
                std::stringstream msg_stream;
                msg_stream << "Open error for destFileName \"" << destFileName << "\", status = " << status;
                result = ERROR( err_status, msg_stream.str() );
            }
            else if ( statbuf.st_mode & S_IFREG == 0 ) {
                close( outFd ); // JMC cppcheck - resource
                std::stringstream msg_stream;
                msg_stream << "destFileName \"" << destFileName << "\" is not a regular file.";
                result = ERROR( UNIX_FILE_STAT_ERR, msg_stream.str() );
            }
            else {
                char myBuf[TRANS_BUF_SZ];
                int bytesRead;
                rodsLong_t bytesCopied = 0;
                while ( result.ok() && ( bytesRead = read( inFd, ( void * ) myBuf, TRANS_BUF_SZ ) ) > 0 ) {
                    int bytesWritten = write( outFd, ( void * ) myBuf, bytesRead );
                    err_status = UNIX_FILE_WRITE_ERR - errno;
                    if ( ( result = ASSERT_ERROR( bytesWritten > 0, err_status, "Write error for srcFileName %s, status = %d",
                                                  destFileName, status ) ).ok() ) {
                        bytesCopied += bytesWritten;
                    }
                }

                close( outFd );

                if ( result.ok() ) {
                    result = ASSERT_ERROR( bytesCopied == statbuf.st_size, SYS_COPY_LEN_ERR, "Copied size %lld does not match source size %lld of %s",
                                           bytesCopied, statbuf.st_size, srcFileName );
                }
            }
            close( inFd );
        }
    }
    return result;
}



// =-=-=-=-=-=-=-
/// @brief Generates a full path name from the partial physical path and the specified resource's vault path
irods::error unix_generate_full_path(
    irods::plugin_property_map& _prop_map,
    const std::string&           _phy_path,
    std::string&                 _ret_string ) {
    irods::error result = SUCCESS();
    irods::error ret;
    std::string vault_path;
    // TODO - getting vault path by property will not likely work for coordinating nodes
    ret = _prop_map.get<std::string>( irods::RESOURCE_PATH, vault_path );
    if ( ( result = ASSERT_ERROR( ret.ok(), SYS_INVALID_INPUT_PARAM, "resource has no vault path." ) ).ok() ) {
        if ( _phy_path.compare( 0, 1, "/" ) != 0 &&
                _phy_path.compare( 0, vault_path.size(), vault_path ) != 0 ) {
            _ret_string  = vault_path;
            _ret_string += "/";
            _ret_string += _phy_path;
        }
{'fraction_non_alphanumeric': 0.13778558875219685, 'fraction_numerical': 0.004481546572934973, 'mean_word_length': 2.2277368122518433, 'pattern_counts': {'":': 0, '<': 119, '<?xml version=': 0, '>': 65, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 10, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6716138', 'n_tokens_mistral': 3726, 'n_tokens_neox': 3531, 'n_words': 893}