<commit_before>/* -*- mode: c++; fill-column: 132; c-basic-offset: 4; indent-tabs-mode: nil -*- */

/*** Copyright (c), The Regents of the University of California            ***
 *** For more information please refer to files in the COPYRIGHT directory ***/
/* rsDataObjRename.c - rename a data object.
 */
#include "dataObjRename.h"
#include "objMetaOpr.h"
#include "dataObjOpr.h"
#include "collection.h"
#include "resource.h"
#include "specColl.h"
#include "physPath.h"
#include "subStructFileRename.h"
#include "icatHighLevelRoutines.h"
#include "dataObjUnlink.h"
#include "phyBundleColl.h"
#include "regDataObj.h"
#include "fileOpendir.h"
#include "fileReaddir.h"
#include "fileClosedir.h"
#include "rmCollOld.h"

// =-=-=-=-=-=-=-
// eirods includes
#include "eirods_resource_backport.h"
#include "eirods_stacktrace.h"
#include "eirods_hierarchy_parser.h"

int
rsDataObjRename (rsComm_t *rsComm, dataObjCopyInp_t *dataObjRenameInp)
{
    int status;
    dataObjInp_t *srcDataObjInp, *destDataObjInp;
    rodsServerHost_t *rodsServerHost = NULL;
    dataObjInfo_t *srcDataObjInfo = NULL;
    dataObjInfo_t *destDataObjInfo = NULL;
    specCollCache_t *specCollCache = NULL;
    int srcType, destType;

    srcDataObjInp = &dataObjRenameInp->srcDataObjInp;
    destDataObjInp = &dataObjRenameInp->destDataObjInp;
    
    /* don't translate the link pt. treat it as a normal collection */
    addKeyVal (&srcDataObjInp->condInput, NO_TRANSLATE_LINKPT_KW, "");
    resolveLinkedPath (rsComm, srcDataObjInp->objPath, &specCollCache,
                       &srcDataObjInp->condInput);
    rmKeyVal (&srcDataObjInp->condInput, NO_TRANSLATE_LINKPT_KW);

    resolveLinkedPath (rsComm, destDataObjInp->objPath, &specCollCache,
                       &destDataObjInp->condInput);

    if (strcmp (srcDataObjInp->objPath, destDataObjInp->objPath) == 0) {
        return (SAME_SRC_DEST_PATHS_ERR);
    }

    /* connect to rcat for cross zone */
    status = getAndConnRcatHost (rsComm, MASTER_RCAT, 
                                 srcDataObjInp->objPath, &rodsServerHost);

    if (status < 0 || NULL == rodsServerHost ) {
        return (status);
    } else if (rodsServerHost->rcatEnabled == REMOTE_ICAT) {
        status = rcDataObjRename (rodsServerHost->conn, dataObjRenameInp);
        return status;
    }

    srcType = resolvePathInSpecColl (rsComm, srcDataObjInp->objPath, 
                                     WRITE_COLL_PERM, 0, &srcDataObjInfo);

    destType = resolvePathInSpecColl (rsComm, destDataObjInp->objPath,
                                      WRITE_COLL_PERM, 0, &destDataObjInfo);

    if( srcDataObjInfo           != NULL && 
        srcDataObjInfo->specColl != NULL &&
        strcmp( srcDataObjInfo->specColl->collection, 
                srcDataObjInp->objPath ) == 0) {
	/* this must be the link pt or mount pt. treat it as normal coll */
        freeDataObjInfo (srcDataObjInfo);
	    srcDataObjInfo = NULL;
        srcType = SYS_SPEC_COLL_NOT_IN_CACHE;
    }

    if (!isSameZone (srcDataObjInp->objPath, destDataObjInp->objPath))
        return SYS_CROSS_ZONE_MV_NOT_SUPPORTED;

    if (destType >= 0) {
        rodsLog (LOG_ERROR,
                 "rsDataObjRename: dest specColl objPath %s exists",
                 destDataObjInp->objPath);
        freeDataObjInfo (srcDataObjInfo);
        freeDataObjInfo (destDataObjInfo);
        return (SYS_DEST_SPEC_COLL_SUB_EXIST);
    }

    if (srcType >= 0) { /* specColl of some sort */
        if (destType == SYS_SPEC_COLL_OBJ_NOT_EXIST) {
            status = specCollObjRename (rsComm, srcDataObjInfo,
                                        destDataObjInfo);
        } else {
            /* dest is regular obj. Allow apecial case where the src
             * is in a MOUNTED_COLL */ 
            if (srcDataObjInfo->specColl->collClass == MOUNTED_COLL) {
                /* a special case for moving obj from mounted collection to
                 * regular collection */
                status = moveMountedCollObj (rsComm, srcDataObjInfo, srcType,
                                             destDataObjInp);
            } else {
                rodsLog (LOG_ERROR,
                         "rsDataObjRename: src %s is in spec coll but dest %s is not",
                         srcDataObjInp->objPath, destDataObjInp->objPath);
                status = SYS_SRC_DEST_SPEC_COLL_CONFLICT;
            }
        }
        freeDataObjInfo (srcDataObjInfo);
        freeDataObjInfo (destDataObjInfo);
        return (status);
    } else if (srcType == SYS_SPEC_COLL_OBJ_NOT_EXIST) {
#if 0   /* don't understand this */
        /* for STRUCT_FILE_COLL to make a directory in the structFile, the
         * STRUCT_FILE_OPR_KW must be set */
        if (getSpecCollOpr (&srcDataObjInp->condInput, 
                            srcDataObjInfo->specColl) != NORMAL_OPR_ON_STRUCT_FILE_COLL) {
            return (SYS_SPEC_COLL_OBJ_NOT_EXIST);
        }
#endif
        return (SYS_SPEC_COLL_OBJ_NOT_EXIST);
    } else if (destType == SYS_SPEC_COLL_OBJ_NOT_EXIST) {
        /* source is normal object but dest is not */
        rodsLog (LOG_ERROR,
                 "rsDataObjRename: src %s is not in spec coll but dest %s is",
                 srcDataObjInp->objPath, destDataObjInp->objPath);
        return (SYS_SRC_DEST_SPEC_COLL_CONFLICT);
    }

    status = getAndConnRcatHost (rsComm, MASTER_RCAT, 
                                 dataObjRenameInp->srcDataObjInp.objPath,
                                 &rodsServerHost);
    if (status < 0) {
        return(status);
    }
    if (rodsServerHost->localFlag == LOCAL_HOST) {
#ifdef RODS_CAT
        status = _rsDataObjRename (rsComm, dataObjRenameInp);
#else
        status = SYS_NO_RCAT_SERVER_ERR;
#endif
    } else {
        status = rcDataObjRename (rodsServerHost->conn, dataObjRenameInp);
    }

    return (status);
}

int
_rsDataObjRename (rsComm_t *rsComm, dataObjCopyInp_t *dataObjRenameInp)
{
#ifdef RODS_CAT
    int status;
    char srcColl[MAX_NAME_LEN], srcObj[MAX_NAME_LEN];
    char destColl[MAX_NAME_LEN], destObj[MAX_NAME_LEN];
    dataObjInp_t *srcDataObjInp, *destDataObjInp;
    dataObjInfo_t *dataObjInfoHead = NULL;
    rodsLong_t srcId, destId;
    int multiCopyFlag;
    int acPreProcFromRenameFlag = 0;

    char *args[MAX_NUM_OF_ARGS_IN_ACTION];<commit_msg>[#1233] moved getMultiCopyPerResc to rsDataObjRename<commit_after>/* -*- mode: c++; fill-column: 132; c-basic-offset: 4; indent-tabs-mode: nil -*- */

/*** Copyright (c), The Regents of the University of California            ***
 *** For more information please refer to files in the COPYRIGHT directory ***/
/* rsDataObjRename.c - rename a data object.
 */
#include "dataObjRename.h"
#include "objMetaOpr.h"
#include "dataObjOpr.h"
#include "collection.h"
#include "resource.h"
#include "specColl.h"
#include "physPath.h"
#include "subStructFileRename.h"
#include "icatHighLevelRoutines.h"
#include "dataObjUnlink.h"
#include "phyBundleColl.h"
#include "regDataObj.h"
#include "fileOpendir.h"
#include "fileReaddir.h"
#include "fileClosedir.h"
#include "rmCollOld.h"

// =-=-=-=-=-=-=-
// eirods includes
#include "eirods_resource_backport.h"
#include "eirods_stacktrace.h"
#include "eirods_hierarchy_parser.h"

int
rsDataObjRename (rsComm_t *rsComm, dataObjCopyInp_t *dataObjRenameInp)
{
    int status;
    dataObjInp_t *srcDataObjInp, *destDataObjInp;
    rodsServerHost_t *rodsServerHost = NULL;
    dataObjInfo_t *srcDataObjInfo = NULL;
    dataObjInfo_t *destDataObjInfo = NULL;
    specCollCache_t *specCollCache = NULL;
    int srcType, destType;

    srcDataObjInp = &dataObjRenameInp->srcDataObjInp;
    destDataObjInp = &dataObjRenameInp->destDataObjInp;
    
    /* don't translate the link pt. treat it as a normal collection */
    addKeyVal (&srcDataObjInp->condInput, NO_TRANSLATE_LINKPT_KW, "");
    resolveLinkedPath (rsComm, srcDataObjInp->objPath, &specCollCache,
                       &srcDataObjInp->condInput);
    rmKeyVal (&srcDataObjInp->condInput, NO_TRANSLATE_LINKPT_KW);

    resolveLinkedPath (rsComm, destDataObjInp->objPath, &specCollCache,
                       &destDataObjInp->condInput);

    if (strcmp (srcDataObjInp->objPath, destDataObjInp->objPath) == 0) {
        return (SAME_SRC_DEST_PATHS_ERR);
    }

    /* connect to rcat for cross zone */
    status = getAndConnRcatHost (rsComm, MASTER_RCAT, 
                                 srcDataObjInp->objPath, &rodsServerHost);

    if (status < 0 || NULL == rodsServerHost ) {
        return (status);
    } else if (rodsServerHost->rcatEnabled == REMOTE_ICAT) {
        status = rcDataObjRename (rodsServerHost->conn, dataObjRenameInp);
        return status;
    }

    srcType = resolvePathInSpecColl (rsComm, srcDataObjInp->objPath, 
                                     WRITE_COLL_PERM, 0, &srcDataObjInfo);

    destType = resolvePathInSpecColl (rsComm, destDataObjInp->objPath,
                                      WRITE_COLL_PERM, 0, &destDataObjInfo);

    if( srcDataObjInfo           != NULL && 
        srcDataObjInfo->specColl != NULL &&
        strcmp( srcDataObjInfo->specColl->collection, 
                srcDataObjInp->objPath ) == 0) {
	/* this must be the link pt or mount pt. treat it as normal coll */
        freeDataObjInfo (srcDataObjInfo);
	    srcDataObjInfo = NULL;
        srcType = SYS_SPEC_COLL_NOT_IN_CACHE;
    }

    if (!isSameZone (srcDataObjInp->objPath, destDataObjInp->objPath))
        return SYS_CROSS_ZONE_MV_NOT_SUPPORTED;

    if (destType >= 0) {
        rodsLog (LOG_ERROR,
                 "rsDataObjRename: dest specColl objPath %s exists",
                 destDataObjInp->objPath);
        freeDataObjInfo (srcDataObjInfo);
        freeDataObjInfo (destDataObjInfo);
        return (SYS_DEST_SPEC_COLL_SUB_EXIST);
    }

    if (srcType >= 0) { /* specColl of some sort */
        if (destType == SYS_SPEC_COLL_OBJ_NOT_EXIST) {
            status = specCollObjRename (rsComm, srcDataObjInfo,
                                        destDataObjInfo);
        } else {
            /* dest is regular obj. Allow apecial case where the src
             * is in a MOUNTED_COLL */ 
            if (srcDataObjInfo->specColl->collClass == MOUNTED_COLL) {
                /* a special case for moving obj from mounted collection to
                 * regular collection */
                status = moveMountedCollObj (rsComm, srcDataObjInfo, srcType,
                                             destDataObjInp);
            } else {
                rodsLog (LOG_ERROR,
                         "rsDataObjRename: src %s is in spec coll but dest %s is not",
                         srcDataObjInp->objPath, destDataObjInp->objPath);
                status = SYS_SRC_DEST_SPEC_COLL_CONFLICT;
            }
        }
        freeDataObjInfo (srcDataObjInfo);
        freeDataObjInfo (destDataObjInfo);
        return (status);
    } else if (srcType == SYS_SPEC_COLL_OBJ_NOT_EXIST) {
#if 0   /* don't understand this */
        /* for STRUCT_FILE_COLL to make a directory in the structFile, the
         * STRUCT_FILE_OPR_KW must be set */
        if (getSpecCollOpr (&srcDataObjInp->condInput, 
                            srcDataObjInfo->specColl) != NORMAL_OPR_ON_STRUCT_FILE_COLL) {
            return (SYS_SPEC_COLL_OBJ_NOT_EXIST);
        }
#endif
        return (SYS_SPEC_COLL_OBJ_NOT_EXIST);
    } else if (destType == SYS_SPEC_COLL_OBJ_NOT_EXIST) {
        /* source is normal object but dest is not */
        rodsLog (LOG_ERROR,
                 "rsDataObjRename: src %s is not in spec coll but dest %s is",
                 srcDataObjInp->objPath, destDataObjInp->objPath);
        return (SYS_SRC_DEST_SPEC_COLL_CONFLICT);
    }

    status = getAndConnRcatHost (rsComm, MASTER_RCAT, 
                                 dataObjRenameInp->srcDataObjInp.objPath,
                                 &rodsServerHost);
    if (status < 0) {
        return(status);
    }
    if (rodsServerHost->localFlag == LOCAL_HOST) {
#ifdef RODS_CAT
        status = _rsDataObjRename (rsComm, dataObjRenameInp);
#else
        status = SYS_NO_RCAT_SERVER_ERR;
#endif
    } else {
        status = rcDataObjRename (rodsServerHost->conn, dataObjRenameInp);
    }

    return (status);
}

int
getMultiCopyPerResc ( rsComm_t *rsComm ) // JMC - backport 4556
{
    ruleExecInfo_t rei;

    memset (&rei, 0, sizeof (rei));
    rei.rsComm = rsComm; // JMC - backport 4556
    applyRule ("acSetMultiReplPerResc", NULL, &rei, NO_SAVE_REI);
    if (strcmp (rei.statusStr, MULTI_COPIES_PER_RESC) == 0) {
        return 1;
    } else {
        return 0;
    }
}


int
_rsDataObjRename (rsComm_t *rsComm, dataObjCopyInp_t *dataObjRenameInp)
{
#ifdef RODS_CAT
    int status;
    char srcColl[MAX_NAME_LEN], srcObj[MAX_NAME_LEN];
    char destColl[MAX_NAME_LEN], destObj[MAX_NAME_LEN];
    dataObjInp_t *srcDataObjInp, *destDataObjInp;
    dataObjInfo_t *dataObjInfoHead = NULL;
    rodsLong_t srcId, destId;
    int multiCopyFlag;
    int acPreProcFromRenameFlag = 0;

    char *args[MAX_NUM_OF_ARGS_IN_ACTION];
{'fraction_non_alphanumeric': 0.09792193850164865, 'fraction_numerical': 0.003373974388467142, 'mean_word_length': 2.547878128400435, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 73, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 16, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3938179', 'n_tokens_mistral': 4446, 'n_tokens_neox': 4279, 'n_words': 907}