00001
00002
00003
00004
00005
00006
00007
00008 #include "dataObjUnlink.h"
00009 #include "rodsLog.h"
00010 #include "icatDefines.h"
00011 #include "fileUnlink.h"
00012 #include "unregDataObj.h"
00013 #include "objMetaOpr.h"
00014 #include "dataObjOpr.h"
00015 #include "specColl.h"
00016 #include "resource.h"
00017 #include "rsGlobalExtern.h"
00018 #include "rcGlobalExtern.h"
00019 #include "reGlobalsExtern.h"
00020 #include "reDefines.h"
00021 #include "rmCollOld.h"
00022 #include "rmColl.h"
00023 #include "dataObjRename.h"
00024 #include "subStructFileUnlink.h"
00025 #include "modDataObjMeta.h"
00026 #include "phyBundleColl.h"
00027 #include "dataObjRepl.h"
00028 #include "regDataObj.h"
00029 #include "physPath.h"
00030
00031
00032
00033 #include "eirods_resource_backport.h"
00034 #include "eirods_resource_redirect.h"
00035 #include "eirods_hierarchy_parser.h"
00036
00037 int
00038 rsDataObjUnlink (rsComm_t *rsComm, dataObjInp_t *dataObjUnlinkInp)
00039 {
00040 int status;
00041 ruleExecInfo_t rei;
00042 int trashPolicy;
00043 dataObjInfo_t *dataObjInfoHead = NULL;
00044 rodsServerHost_t *rodsServerHost = NULL;
00045 int rmTrashFlag = 0;
00046 specCollCache_t *specCollCache = NULL;
00047
00048 resolveLinkedPath (rsComm, dataObjUnlinkInp->objPath, &specCollCache,
00049 &dataObjUnlinkInp->condInput);
00050 status = getAndConnRcatHost (rsComm, MASTER_RCAT,
00051 dataObjUnlinkInp->objPath, &rodsServerHost);
00052
00053 if (status < 0 || NULL == rodsServerHost ) {
00054 return (status);
00055 } else if (rodsServerHost->rcatEnabled == REMOTE_ICAT) {
00056 rcDataObjUnlink (rodsServerHost->conn, dataObjUnlinkInp);
00057 return status;
00058 }
00059
00060
00061
00062
00063
00064
00065
00066
00067 if( getValByKey( &dataObjUnlinkInp->condInput, RESC_HIER_STR_KW ) == NULL ) {
00068 std::string hier;
00069 eirods::error ret = eirods::resolve_resource_hierarchy( eirods::EIRODS_OPEN_OPERATION,
00070 rsComm, dataObjUnlinkInp, hier );
00071 if( !ret.ok() ) {
00072 std::stringstream msg;
00073 msg << "failed in eirods::resolve_resource_hierarchy for [";
00074 msg << dataObjUnlinkInp->objPath << "]";
00075 eirods::log( PASSMSG( msg.str(), ret ) );
00076 return ret.code();
00077 }
00078
00079
00080
00081
00082 addKeyVal( &dataObjUnlinkInp->condInput, RESC_HIER_STR_KW, hier.c_str() );
00083
00084 }
00085
00086 if (getValByKey (
00087 &dataObjUnlinkInp->condInput, IRODS_ADMIN_RMTRASH_KW) != NULL ||
00088 getValByKey (
00089 &dataObjUnlinkInp->condInput, IRODS_RMTRASH_KW) != NULL) {
00090 if (isTrashPath (dataObjUnlinkInp->objPath) == False) {
00091 return (SYS_INVALID_FILE_PATH);
00092 }
00093 rmTrashFlag = 1;
00094 }
00095
00096 dataObjUnlinkInp->openFlags = O_WRONLY;
00097 status = getDataObjInfoIncSpecColl (rsComm, dataObjUnlinkInp,
00098 &dataObjInfoHead);
00099
00100 if (status < 0) {
00101 char* sys_error;
00102 char* rods_error = rodsErrorName(status, &sys_error);
00103 std::stringstream msg;
00104 msg << __FUNCTION__;
00105 msg << " - Failed to get data objects.";
00106 msg << " - " << rods_error << " " << sys_error;
00107 eirods::error result = ERROR(status, msg.str());
00108 eirods::log(result);
00109 return (status);
00110 }
00111
00112 if (rmTrashFlag == 1) {
00113 char *tmpAge;
00114 int ageLimit;
00115 if ((tmpAge = getValByKey (&dataObjUnlinkInp->condInput, AGE_KW))
00116 != NULL) {
00117 ageLimit = atoi (tmpAge) * 60;
00118 if ((time (0) - atoi (dataObjInfoHead->dataModify)) < ageLimit) {
00119
00120 freeAllDataObjInfo (dataObjInfoHead);
00121 return 0;
00122 }
00123 }
00124 }
00125
00126 if (dataObjUnlinkInp->oprType == UNREG_OPR ||
00127 getValByKey (&dataObjUnlinkInp->condInput, FORCE_FLAG_KW) != NULL ||
00128 getValByKey (&dataObjUnlinkInp->condInput, REPL_NUM_KW) != NULL ||
00129 dataObjInfoHead->specColl != NULL || rmTrashFlag == 1) {
00130 status = _rsDataObjUnlink (rsComm, dataObjUnlinkInp, &dataObjInfoHead);
00131 } else {
00132 initReiWithDataObjInp (&rei, rsComm, dataObjUnlinkInp);
00133 status = applyRule ("acTrashPolicy", NULL, &rei, NO_SAVE_REI);
00134 trashPolicy = rei.status;
00135
00136 if (trashPolicy != NO_TRASH_CAN) {
00137 status = rsMvDataObjToTrash (rsComm, dataObjUnlinkInp,
00138 &dataObjInfoHead);
00139 freeAllDataObjInfo (dataObjInfoHead);
00140 return status;
00141 } else {
00142 status = _rsDataObjUnlink (rsComm, dataObjUnlinkInp,
00143 &dataObjInfoHead);
00144 }
00145 }
00146
00147 initReiWithDataObjInp (&rei, rsComm, dataObjUnlinkInp);
00148 rei.doi = dataObjInfoHead;
00149 rei.status = status;
00150 rei.status = applyRule ("acPostProcForDelete", NULL, &rei, NO_SAVE_REI);
00151
00152 if (rei.status < 0) {
00153 rodsLog (LOG_NOTICE,
00154 "rsDataObjUnlink: acPostProcForDelete error for %s. status = %d",
00155 dataObjUnlinkInp->objPath, rei.status);
00156 }
00157
00158
00159 freeAllDataObjInfo (dataObjInfoHead);
00160
00161 return (status);
00162 }
00163
00164 int
00165 _rsDataObjUnlink (rsComm_t *rsComm, dataObjInp_t *dataObjUnlinkInp,
00166 dataObjInfo_t **dataObjInfoHead)
00167 {
00168 int status;
00169 int retVal = 0;
00170 dataObjInfo_t *tmpDataObjInfo, *myDataObjInfoHead;
00171
00172 status = chkPreProcDeleteRule (rsComm, dataObjUnlinkInp, *dataObjInfoHead);
00173 if (status < 0) return status;
00174
00175
00176 myDataObjInfoHead = *dataObjInfoHead;
00177 if (strstr (myDataObjInfoHead->dataType, BUNDLE_STR) != NULL) {
00178 int numSubfiles;
00179 if (rsComm->proxyUser.authInfo.authFlag < LOCAL_PRIV_USER_AUTH) {
00180 return CAT_INSUFFICIENT_PRIVILEGE_LEVEL;
00181 }
00182 if (getValByKey (&dataObjUnlinkInp->condInput, REPL_NUM_KW) != NULL) {
00183 return SYS_CANT_MV_BUNDLE_DATA_BY_COPY;
00184 }
00185
00186
00187
00188 numSubfiles = getNumSubfilesInBunfileObj (rsComm, myDataObjInfoHead->objPath);
00189 if (numSubfiles > 0) {
00190 if (getValByKey (&dataObjUnlinkInp->condInput, EMPTY_BUNDLE_ONLY_KW) != NULL) {
00191
00192 return 0;
00193 } else {
00194 status = _unbunAndStageBunfileObj (rsComm, dataObjInfoHead, &dataObjUnlinkInp->condInput, NULL, 1);
00195 if (status < 0) {
00196
00197
00198 if (getErrno (status) != EEXIST && getIrodsErrno (status) != SYS_TAR_STRUCT_FILE_EXTRACT_ERR) {
00199 rodsLogError( LOG_ERROR, status, "_rsDataObjUnlink:_unbunAndStageBunfileObj err for %s",
00200 myDataObjInfoHead->objPath);
00201 return (status);
00202 }
00203 }
00204
00205 *dataObjInfoHead = NULL;
00206 status = getDataObjInfoIncSpecColl (rsComm, dataObjUnlinkInp, dataObjInfoHead );
00207
00208 if (status < 0) return (status);
00209 }
00210 }
00211 }
00212
00213
00214 char* rescHier = getValByKey(&dataObjUnlinkInp->condInput, RESC_HIER_STR_KW);
00215
00216 tmpDataObjInfo = *dataObjInfoHead;
00217 while (tmpDataObjInfo != NULL) {
00218 status = dataObjUnlinkS (rsComm, dataObjUnlinkInp, tmpDataObjInfo);
00219 if (status < 0) {
00220 if (retVal == 0) {
00221 retVal = status;
00222 }
00223 }
00224 if (dataObjUnlinkInp->specColl != NULL)
00225 break;
00226 tmpDataObjInfo = tmpDataObjInfo->next;
00227 }
00228
00229 if ((*dataObjInfoHead)->specColl == NULL)
00230 resolveDataObjReplStatus (rsComm, dataObjUnlinkInp);
00231
00232 return (retVal);
00233 }
00234
00235
00236
00237
00238 int
00239 resolveDataObjReplStatus (rsComm_t *rsComm, dataObjInp_t *dataObjUnlinkInp)
00240 {
00241 int status;
00242 dataObjInfo_t *dataObjInfoHead = NULL;
00243 dataObjInfo_t *newestDataObjInfo = NULL;
00244 dataObjInfo_t *tmpDataObjInfo;
00245
00246 if (getValByKey (&dataObjUnlinkInp->condInput, RESC_NAME_KW) == NULL &&
00247 getValByKey (&dataObjUnlinkInp->condInput, REPL_NUM_KW) == NULL) {
00248 return 0;
00249 }
00250 status = getDataObjInfo (rsComm, dataObjUnlinkInp,
00251 &dataObjInfoHead, ACCESS_DELETE_OBJECT, 1);
00252
00253 if (status < 0) return status;
00254
00255 tmpDataObjInfo = dataObjInfoHead;
00256 while (tmpDataObjInfo != NULL) {
00257 if (tmpDataObjInfo->replStatus == 0) {
00258 if (newestDataObjInfo == NULL) {
00259 newestDataObjInfo = tmpDataObjInfo;
00260 } else if (atoi (tmpDataObjInfo->dataModify) >
00261 atoi (newestDataObjInfo->dataModify)) {
00262 newestDataObjInfo = tmpDataObjInfo;
00263 }
00264 } else {
00265 newestDataObjInfo = NULL;
00266 break;
00267 }
00268 tmpDataObjInfo = tmpDataObjInfo->next;
00269 }
00270
00271
00272 if (newestDataObjInfo != NULL) {
00273 keyValPair_t regParam;
00274 char tmpStr[MAX_NAME_LEN];
00275 modDataObjMeta_t modDataObjMetaInp;
00276
00277 memset (®Param, 0, sizeof (regParam));
00278 memset (&modDataObjMetaInp, 0, sizeof (modDataObjMetaInp));
00279 snprintf (tmpStr, MAX_NAME_LEN, "%d", NEWLY_CREATED_COPY);
00280 addKeyVal (®Param, REPL_STATUS_KW, tmpStr);
00281 modDataObjMetaInp.dataObjInfo = newestDataObjInfo;
00282 modDataObjMetaInp.regParam = ®Param;
00283
00284 status = rsModDataObjMeta (rsComm, &modDataObjMetaInp);
00285
00286 clearKeyVal (®Param);
00287 }
00288 freeAllDataObjInfo (dataObjInfoHead);
00289 return (status);
00290 }
00291
00292 int
00293 dataObjUnlinkS (rsComm_t *rsComm, dataObjInp_t *dataObjUnlinkInp,
00294 dataObjInfo_t *dataObjInfo)
00295 {
00296 int status;
00297 unregDataObj_t unregDataObjInp;
00298
00299 if( dataObjInfo->specColl == NULL ) {
00300 if( dataObjUnlinkInp->oprType == UNREG_OPR &&
00301 rsComm->clientUser.authInfo.authFlag != LOCAL_PRIV_USER_AUTH ) {
00302 ruleExecInfo_t rei;
00303
00304 initReiWithDataObjInp (&rei, rsComm, dataObjUnlinkInp);
00305 rei.doi = dataObjInfo;
00306 rei.status = DO_CHK_PATH_PERM;
00307
00308 applyRule ("acSetChkFilePathPerm", NULL, &rei, NO_SAVE_REI);
00309 if (rei.status != NO_CHK_PATH_PERM) {
00310 rodsServerHost_t *rodsServerHost;
00311 status = resolveHostByRescInfo (dataObjInfo->rescInfo,
00312 &rodsServerHost);
00313 if (status < 0) {
00314 return status;
00315 }
00316
00317
00318 std::string out_path;
00319 eirods::error ret = resc_mgr.validate_vault_path( dataObjInfo->filePath, rodsServerHost, out_path );
00320 if( !ret.ok() ) {
00321
00322 std::stringstream msg;
00323 msg << "unregistering a data object which is in a vault [";
00324 msg << dataObjInfo->filePath << "]";
00325 eirods::log( PASSMSG( msg.str(), ret ) );
00326 return CANT_UNREG_IN_VAULT_FILE;
00327 }
00328 }
00329 }
00330
00331 unregDataObjInp.dataObjInfo = dataObjInfo;
00332 unregDataObjInp.condInput = &dataObjUnlinkInp->condInput;
00333 status = rsUnregDataObj (rsComm, &unregDataObjInp);
00334
00335 if (status < 0) {
00336 rodsLog (LOG_NOTICE,
00337 "dataObjUnlinkS: rsUnregDataObj error for %s. status = %d",
00338 dataObjUnlinkInp->objPath, status);
00339 return status;
00340 }
00341 }
00342
00343 if (dataObjUnlinkInp->oprType != UNREG_OPR) {
00344
00345
00346 char* in_pdmo = getValByKey(&dataObjUnlinkInp->condInput, IN_PDMO_KW);
00347 if(in_pdmo != NULL) {
00348 dataObjInfo->in_pdmo = 1;
00349 } else {
00350 dataObjInfo->in_pdmo = 0;
00351 }
00352
00353 status = l3Unlink (rsComm, dataObjInfo);
00354 if (status < 0) {
00355 int myError = getErrno (status);
00356 rodsLog (LOG_NOTICE,
00357 "dataObjUnlinkS: l3Unlink error for %s. status = %d",
00358 dataObjUnlinkInp->objPath, status);
00359
00360 if (myError != ENOENT && myError != EACCES) {
00361 char orphanPath[MAX_NAME_LEN];
00362 int status1 = 0;
00363 rodsLog (LOG_NOTICE,
00364 "dataObjUnlinkS: orphan file %s", dataObjInfo->filePath);
00365 while (1) {
00366 if (isOrphanPath (dataObjUnlinkInp->objPath) ==
00367 NOT_ORPHAN_PATH) {
00368
00369 status1 = rsMkOrphanPath (rsComm, dataObjInfo->objPath,
00370 orphanPath);
00371 if (status1 < 0) break;
00372
00373 rstrcpy (dataObjInfo->objPath, orphanPath,MAX_NAME_LEN);
00374 }
00375 status1 = svrRegDataObj (rsComm, dataObjInfo);
00376 if (status1 == CAT_NAME_EXISTS_AS_DATAOBJ ||
00377 status1 == CATALOG_ALREADY_HAS_ITEM_BY_THAT_NAME) {
00378 continue;
00379 } else if (status1 < 0) {
00380 rodsLogError (LOG_ERROR, status1,
00381 "dataObjUnlinkS: svrRegDataObj of orphan %s error",
00382 dataObjInfo->objPath);
00383 }
00384 break;
00385 }
00386 return (status);
00387 } else {
00388 status = 0;
00389 }
00390 }
00391 }
00392
00393 return (status);
00394 }
00395
00396 int
00397 l3Unlink (rsComm_t *rsComm, dataObjInfo_t *dataObjInfo)
00398 {
00399 fileUnlinkInp_t fileUnlinkInp;
00400 int status;
00401
00402
00403
00404 std::string resc_class;
00405 eirods::error prop_err = eirods::get_resource_property<std::string>(
00406 dataObjInfo->rescInfo->rescName,
00407 eirods::RESOURCE_CLASS,
00408 resc_class );
00409 if( prop_err.ok() ) {
00410 if( resc_class == eirods::RESOURCE_CLASS_BUNDLE ) {
00411 return 0;
00412 }
00413 } else {
00414 std::stringstream msg;
00415 msg << "failed to get proprty [class] for resource [";
00416 msg << dataObjInfo->rescInfo->rescName;
00417 msg << "]";
00418 eirods::log( PASSMSG( msg.str(), prop_err ) );
00419 return -1;
00420 }
00421
00422
00423
00424
00425 std::string location;
00426 eirods::error ret = eirods::get_loc_for_hier_string( dataObjInfo->rescHier, location );
00427 if( !ret.ok() ) {
00428 eirods::log( PASSMSG( "failed in get_loc_for_hier_String", ret ) );
00429 return -1;
00430 }
00431
00432
00433 if (dataObjInfo->rescInfo->rescStatus == INT_RESC_STATUS_DOWN)
00434 return SYS_RESC_IS_DOWN;
00435
00436 if (getStructFileType (dataObjInfo->specColl) >= 0) {
00437 subFile_t subFile;
00438 memset (&subFile, 0, sizeof (subFile));
00439 rstrcpy ( subFile.subFilePath, dataObjInfo->subPath,MAX_NAME_LEN);
00440 rstrcpy ( subFile.addr.hostAddr, location.c_str(), NAME_LEN );
00441 subFile.specColl = dataObjInfo->specColl;
00442 status = rsSubStructFileUnlink (rsComm, &subFile);
00443 } else {
00444 #if 0 // JMC - legacy resource
00445 rescTypeInx = dataObjInfo->rescInfo->rescTypeInx;
00446 switch (RescTypeDef[rescTypeInx].rescCat) {
00447 case FILE_CAT:
00448 #endif // JMC - legacy resource
00449 memset (&fileUnlinkInp, 0, sizeof (fileUnlinkInp));
00450 fileUnlinkInp.fileType = static_cast< fileDriverType_t >( -1 );
00451 rstrcpy (fileUnlinkInp.fileName, dataObjInfo->filePath, MAX_NAME_LEN);
00452 rstrcpy (fileUnlinkInp.rescHier, dataObjInfo->rescHier, MAX_NAME_LEN);
00453 rstrcpy (fileUnlinkInp.addr.hostAddr, location.c_str(), NAME_LEN);
00454 rstrcpy (fileUnlinkInp.objPath, dataObjInfo->objPath, MAX_NAME_LEN);
00455 fileUnlinkInp.in_pdmo = dataObjInfo->in_pdmo;
00456 status = rsFileUnlink (rsComm, &fileUnlinkInp);
00457 #if 0 // JMC - legacy resource
00458 break;
00459
00460 default:
00461 rodsLog (LOG_NOTICE,
00462 "l3Unlink: rescCat type %d is not recognized",
00463 RescTypeDef[rescTypeInx].rescCat);
00464 status = SYS_INVALID_RESC_TYPE;
00465 break;
00466 }
00467 #endif // JMC - legacy resource
00468 }
00469 return (status);
00470 }
00471
00472 int
00473 rsMvDataObjToTrash (rsComm_t *rsComm, dataObjInp_t *dataObjInp,
00474 dataObjInfo_t **dataObjInfoHead)
00475 {
00476 int status;
00477 char trashPath[MAX_NAME_LEN];
00478 dataObjCopyInp_t dataObjRenameInp;
00479
00480 if (strstr ((*dataObjInfoHead)->dataType, BUNDLE_STR) != NULL) {
00481 return SYS_CANT_MV_BUNDLE_DATA_TO_TRASH;
00482 }
00483
00484 if (getValByKey (&dataObjInp->condInput, DATA_ACCESS_KW) == NULL) {
00485 addKeyVal (&dataObjInp->condInput, DATA_ACCESS_KW,
00486 ACCESS_DELETE_OBJECT);
00487 }
00488
00489 status = getDataObjInfo (rsComm, dataObjInp, dataObjInfoHead,
00490 ACCESS_DELETE_OBJECT, 0);
00491 if (status < 0) {
00492 rodsLog (LOG_NOTICE,
00493 "rsMvDataObjToTrash: getDataObjInfo error for %s. status = %d",
00494 dataObjInp->objPath, status);
00495 return (status);
00496 }
00497
00498 status = chkPreProcDeleteRule (rsComm, dataObjInp, *dataObjInfoHead);
00499 if (status < 0) return status;
00500
00501
00502 status = rsMkTrashPath (rsComm, dataObjInp->objPath, trashPath);
00503
00504 if (status < 0) {
00505 return (status);
00506 }
00507
00508 memset (&dataObjRenameInp, 0, sizeof (dataObjRenameInp));
00509
00510 dataObjRenameInp.srcDataObjInp.oprType =
00511 dataObjRenameInp.destDataObjInp.oprType = RENAME_DATA_OBJ;
00512
00513 rstrcpy (dataObjRenameInp.destDataObjInp.objPath, trashPath, MAX_NAME_LEN);
00514 rstrcpy (dataObjRenameInp.srcDataObjInp.objPath, dataObjInp->objPath,
00515 MAX_NAME_LEN);
00516
00517 status = rsDataObjRename (rsComm, &dataObjRenameInp);
00518
00519 while (status == CAT_NAME_EXISTS_AS_DATAOBJ ||
00520 status == CAT_NAME_EXISTS_AS_COLLECTION ||
00521 status == SYS_PHY_PATH_INUSE ||
00522 getErrno (status) == EISDIR) {
00523 appendRandomToPath (dataObjRenameInp.destDataObjInp.objPath);
00524 status = rsDataObjRename (rsComm, &dataObjRenameInp);
00525 }
00526 if (status < 0) {
00527 rodsLog (LOG_ERROR,
00528 "rsMvDataObjToTrash: rcDataObjRename error for %s, status = %d",
00529 dataObjRenameInp.destDataObjInp.objPath, status);
00530 return (status);
00531 }
00532 return (status);
00533 }
00534
00535 int
00536 chkPreProcDeleteRule (rsComm_t *rsComm, dataObjInp_t *dataObjUnlinkInp,
00537 dataObjInfo_t *dataObjInfoHead)
00538 {
00539 dataObjInfo_t *tmpDataObjInfo;
00540 ruleExecInfo_t rei;
00541 int status = 0;
00542
00543 initReiWithDataObjInp (&rei, rsComm, dataObjUnlinkInp);
00544 tmpDataObjInfo = dataObjInfoHead;
00545 while (tmpDataObjInfo != NULL) {
00546
00547 rei.doi = tmpDataObjInfo;
00548
00549 status = applyRule ("acDataDeletePolicy", NULL, &rei, NO_SAVE_REI);
00550
00551 if (status < 0 && status != NO_MORE_RULES_ERR &&
00552 status != SYS_DELETE_DISALLOWED) {
00553 rodsLog (LOG_ERROR,
00554 "chkPreProcDeleteRule: acDataDeletePolicy err for %s. stat = %d",
00555 dataObjUnlinkInp->objPath, status);
00556 return (status);
00557 }
00558
00559 if (rei.status == SYS_DELETE_DISALLOWED) {
00560 rodsLog (LOG_ERROR,
00561 "chkPreProcDeleteRule:acDataDeletePolicy disallowed delete of %s",
00562 dataObjUnlinkInp->objPath);
00563 return (rei.status);
00564 }
00565 tmpDataObjInfo = tmpDataObjInfo->next;
00566 }
00567 return status;
00568 }