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 tmpDataObjInfo = *dataObjInfoHead;
00215 while (tmpDataObjInfo != NULL) {
00216 status = dataObjUnlinkS (rsComm, dataObjUnlinkInp, tmpDataObjInfo);
00217 if (status < 0) {
00218 if (retVal == 0) {
00219 retVal = status;
00220 }
00221 }
00222 if (dataObjUnlinkInp->specColl != NULL)
00223 break;
00224 tmpDataObjInfo = tmpDataObjInfo->next;
00225 }
00226
00227 if ((*dataObjInfoHead)->specColl == NULL)
00228 resolveDataObjReplStatus (rsComm, dataObjUnlinkInp);
00229
00230 return (retVal);
00231 }
00232
00233
00234
00235
00236 int
00237 resolveDataObjReplStatus (rsComm_t *rsComm, dataObjInp_t *dataObjUnlinkInp)
00238 {
00239 int status;
00240 dataObjInfo_t *dataObjInfoHead = NULL;
00241 dataObjInfo_t *newestDataObjInfo = NULL;
00242 dataObjInfo_t *tmpDataObjInfo;
00243
00244 if (getValByKey (&dataObjUnlinkInp->condInput, RESC_NAME_KW) == NULL &&
00245 getValByKey (&dataObjUnlinkInp->condInput, REPL_NUM_KW) == NULL) {
00246 return 0;
00247 }
00248 status = getDataObjInfo (rsComm, dataObjUnlinkInp,
00249 &dataObjInfoHead, ACCESS_DELETE_OBJECT, 1);
00250
00251 if (status < 0) return status;
00252
00253 tmpDataObjInfo = dataObjInfoHead;
00254 while (tmpDataObjInfo != NULL) {
00255 if (tmpDataObjInfo->replStatus == 0) {
00256 if (newestDataObjInfo == NULL) {
00257 newestDataObjInfo = tmpDataObjInfo;
00258 } else if (atoi (tmpDataObjInfo->dataModify) >
00259 atoi (newestDataObjInfo->dataModify)) {
00260 newestDataObjInfo = tmpDataObjInfo;
00261 }
00262 } else {
00263 newestDataObjInfo = NULL;
00264 break;
00265 }
00266 tmpDataObjInfo = tmpDataObjInfo->next;
00267 }
00268
00269
00270 if (newestDataObjInfo != NULL) {
00271 keyValPair_t regParam;
00272 char tmpStr[MAX_NAME_LEN];
00273 modDataObjMeta_t modDataObjMetaInp;
00274
00275 memset (®Param, 0, sizeof (regParam));
00276 memset (&modDataObjMetaInp, 0, sizeof (modDataObjMetaInp));
00277 snprintf (tmpStr, MAX_NAME_LEN, "%d", NEWLY_CREATED_COPY);
00278 addKeyVal (®Param, REPL_STATUS_KW, tmpStr);
00279 modDataObjMetaInp.dataObjInfo = newestDataObjInfo;
00280 modDataObjMetaInp.regParam = ®Param;
00281
00282 status = rsModDataObjMeta (rsComm, &modDataObjMetaInp);
00283
00284 clearKeyVal (®Param);
00285 }
00286 freeAllDataObjInfo (dataObjInfoHead);
00287 return (status);
00288 }
00289
00290 int
00291 dataObjUnlinkS (rsComm_t *rsComm, dataObjInp_t *dataObjUnlinkInp,
00292 dataObjInfo_t *dataObjInfo)
00293 {
00294 int status;
00295 unregDataObj_t unregDataObjInp;
00296
00297 if( dataObjInfo->specColl == NULL ) {
00298 if( dataObjUnlinkInp->oprType == UNREG_OPR &&
00299 rsComm->clientUser.authInfo.authFlag != LOCAL_PRIV_USER_AUTH ) {
00300 ruleExecInfo_t rei;
00301
00302 initReiWithDataObjInp (&rei, rsComm, dataObjUnlinkInp);
00303 rei.doi = dataObjInfo;
00304 rei.status = DO_CHK_PATH_PERM;
00305
00306 applyRule ("acSetChkFilePathPerm", NULL, &rei, NO_SAVE_REI);
00307 if (rei.status != NO_CHK_PATH_PERM) {
00308 rodsServerHost_t *rodsServerHost;
00309 status = resolveHostByRescInfo (dataObjInfo->rescInfo,
00310 &rodsServerHost);
00311 if (status < 0) {
00312 return status;
00313 }
00314
00315
00316 std::string out_path;
00317 eirods::error ret = resc_mgr.validate_vault_path( dataObjInfo->filePath, rodsServerHost, out_path );
00318 if( !ret.ok() ) {
00319
00320 std::stringstream msg;
00321 msg << "unregistering a data object which is in a vault [";
00322 msg << dataObjInfo->filePath << "]";
00323 eirods::log( PASSMSG( msg.str(), ret ) );
00324 return CANT_UNREG_IN_VAULT_FILE;
00325 }
00326 }
00327 }
00328
00329 unregDataObjInp.dataObjInfo = dataObjInfo;
00330 unregDataObjInp.condInput = &dataObjUnlinkInp->condInput;
00331 status = rsUnregDataObj (rsComm, &unregDataObjInp);
00332
00333 if (status < 0) {
00334 rodsLog (LOG_NOTICE,
00335 "dataObjUnlinkS: rsUnregDataObj error for %s. status = %d",
00336 dataObjUnlinkInp->objPath, status);
00337 return status;
00338 }
00339 }
00340
00341 if (dataObjUnlinkInp->oprType != UNREG_OPR) {
00342
00343
00344 char* in_pdmo = getValByKey(&dataObjUnlinkInp->condInput, IN_PDMO_KW);
00345 if(in_pdmo != NULL) {
00346 rstrcpy(dataObjInfo->in_pdmo, in_pdmo, MAX_NAME_LEN);
00347 } else {
00348 dataObjInfo->in_pdmo[0] = '\0';
00349 }
00350
00351 status = l3Unlink (rsComm, dataObjInfo);
00352 if (status < 0) {
00353 int myError = getErrno (status);
00354 rodsLog (LOG_NOTICE,
00355 "dataObjUnlinkS: l3Unlink error for %s. status = %d",
00356 dataObjUnlinkInp->objPath, status);
00357
00358 if (myError != ENOENT && myError != EACCES) {
00359 char orphanPath[MAX_NAME_LEN];
00360 int status1 = 0;
00361 rodsLog (LOG_NOTICE,
00362 "dataObjUnlinkS: orphan file %s", dataObjInfo->filePath);
00363 while (1) {
00364 if (isOrphanPath (dataObjUnlinkInp->objPath) ==
00365 NOT_ORPHAN_PATH) {
00366
00367 status1 = rsMkOrphanPath (rsComm, dataObjInfo->objPath,
00368 orphanPath);
00369 if (status1 < 0) break;
00370
00371 rstrcpy (dataObjInfo->objPath, orphanPath,MAX_NAME_LEN);
00372 }
00373 status1 = svrRegDataObj (rsComm, dataObjInfo);
00374 if (status1 == CAT_NAME_EXISTS_AS_DATAOBJ ||
00375 status1 == CATALOG_ALREADY_HAS_ITEM_BY_THAT_NAME) {
00376 continue;
00377 } else if (status1 < 0) {
00378 rodsLogError (LOG_ERROR, status1,
00379 "dataObjUnlinkS: svrRegDataObj of orphan %s error",
00380 dataObjInfo->objPath);
00381 }
00382 break;
00383 }
00384 return (status);
00385 } else {
00386 status = 0;
00387 }
00388 }
00389 }
00390
00391 return (status);
00392 }
00393
00394 int
00395 l3Unlink (rsComm_t *rsComm, dataObjInfo_t *dataObjInfo)
00396 {
00397 fileUnlinkInp_t fileUnlinkInp;
00398 int status;
00399
00400
00401
00402 std::string resc_class;
00403 eirods::error prop_err = eirods::get_resource_property<std::string>(
00404 dataObjInfo->rescInfo->rescName,
00405 eirods::RESOURCE_CLASS,
00406 resc_class );
00407 if( prop_err.ok() ) {
00408 if( resc_class == eirods::RESOURCE_CLASS_BUNDLE ) {
00409 return 0;
00410 }
00411 } else {
00412 std::stringstream msg;
00413 msg << "failed to get proprty [class] for resource [";
00414 msg << dataObjInfo->rescInfo->rescName;
00415 msg << "]";
00416 eirods::log( PASSMSG( msg.str(), prop_err ) );
00417 return -1;
00418 }
00419
00420
00421
00422
00423 std::string location;
00424 eirods::error ret = eirods::get_loc_for_hier_string( dataObjInfo->rescHier, location );
00425 if( !ret.ok() ) {
00426 eirods::log( PASSMSG( "failed in get_loc_for_hier_String", ret ) );
00427 return -1;
00428 }
00429
00430
00431 if (dataObjInfo->rescInfo->rescStatus == INT_RESC_STATUS_DOWN)
00432 return SYS_RESC_IS_DOWN;
00433
00434 if (getStructFileType (dataObjInfo->specColl) >= 0) {
00435 subFile_t subFile;
00436 memset (&subFile, 0, sizeof (subFile));
00437 rstrcpy ( subFile.subFilePath, dataObjInfo->subPath,MAX_NAME_LEN);
00438 rstrcpy ( subFile.addr.hostAddr, location.c_str(), NAME_LEN );
00439 subFile.specColl = dataObjInfo->specColl;
00440 status = rsSubStructFileUnlink (rsComm, &subFile);
00441 } else {
00442 #if 0 // JMC - legacy resource
00443 rescTypeInx = dataObjInfo->rescInfo->rescTypeInx;
00444 switch (RescTypeDef[rescTypeInx].rescCat) {
00445 case FILE_CAT:
00446 #endif // JMC - legacy resource
00447 memset (&fileUnlinkInp, 0, sizeof (fileUnlinkInp));
00448 fileUnlinkInp.fileType = static_cast< fileDriverType_t >( -1 );
00449 rstrcpy (fileUnlinkInp.fileName, dataObjInfo->filePath, MAX_NAME_LEN);
00450 rstrcpy (fileUnlinkInp.rescHier, dataObjInfo->rescHier, MAX_NAME_LEN);
00451 rstrcpy (fileUnlinkInp.addr.hostAddr, location.c_str(), NAME_LEN);
00452 rstrcpy (fileUnlinkInp.objPath, dataObjInfo->objPath, MAX_NAME_LEN);
00453 rstrcpy (fileUnlinkInp.in_pdmo, dataObjInfo->in_pdmo, MAX_NAME_LEN);
00454 status = rsFileUnlink (rsComm, &fileUnlinkInp);
00455 #if 0 // JMC - legacy resource
00456 break;
00457
00458 default:
00459 rodsLog (LOG_NOTICE,
00460 "l3Unlink: rescCat type %d is not recognized",
00461 RescTypeDef[rescTypeInx].rescCat);
00462 status = SYS_INVALID_RESC_TYPE;
00463 break;
00464 }
00465 #endif // JMC - legacy resource
00466 }
00467 return (status);
00468 }
00469
00470 int
00471 rsMvDataObjToTrash (rsComm_t *rsComm, dataObjInp_t *dataObjInp,
00472 dataObjInfo_t **dataObjInfoHead)
00473 {
00474 int status;
00475 char trashPath[MAX_NAME_LEN];
00476 dataObjCopyInp_t dataObjRenameInp;
00477
00478 if (strstr ((*dataObjInfoHead)->dataType, BUNDLE_STR) != NULL) {
00479 return SYS_CANT_MV_BUNDLE_DATA_TO_TRASH;
00480 }
00481
00482 if (getValByKey (&dataObjInp->condInput, DATA_ACCESS_KW) == NULL) {
00483 addKeyVal (&dataObjInp->condInput, DATA_ACCESS_KW,
00484 ACCESS_DELETE_OBJECT);
00485 }
00486
00487 status = getDataObjInfo (rsComm, dataObjInp, dataObjInfoHead,
00488 ACCESS_DELETE_OBJECT, 0);
00489 if (status < 0) {
00490 rodsLog (LOG_NOTICE,
00491 "rsMvDataObjToTrash: getDataObjInfo error for %s. status = %d",
00492 dataObjInp->objPath, status);
00493 return (status);
00494 }
00495
00496 status = chkPreProcDeleteRule (rsComm, dataObjInp, *dataObjInfoHead);
00497 if (status < 0) return status;
00498
00499
00500 status = rsMkTrashPath (rsComm, dataObjInp->objPath, trashPath);
00501
00502 if (status < 0) {
00503 return (status);
00504 }
00505
00506 memset (&dataObjRenameInp, 0, sizeof (dataObjRenameInp));
00507
00508 dataObjRenameInp.srcDataObjInp.oprType =
00509 dataObjRenameInp.destDataObjInp.oprType = RENAME_DATA_OBJ;
00510
00511 rstrcpy (dataObjRenameInp.destDataObjInp.objPath, trashPath, MAX_NAME_LEN);
00512 rstrcpy (dataObjRenameInp.srcDataObjInp.objPath, dataObjInp->objPath,
00513 MAX_NAME_LEN);
00514
00515 status = rsDataObjRename (rsComm, &dataObjRenameInp);
00516
00517 while (status == CAT_NAME_EXISTS_AS_DATAOBJ ||
00518 status == CAT_NAME_EXISTS_AS_COLLECTION ||
00519 status == SYS_PHY_PATH_INUSE ||
00520 getErrno (status) == EISDIR) {
00521 appendRandomToPath (dataObjRenameInp.destDataObjInp.objPath);
00522 status = rsDataObjRename (rsComm, &dataObjRenameInp);
00523 }
00524 if (status < 0) {
00525 rodsLog (LOG_ERROR,
00526 "rsMvDataObjToTrash: rcDataObjRename error for %s, status = %d",
00527 dataObjRenameInp.destDataObjInp.objPath, status);
00528 return (status);
00529 }
00530 return (status);
00531 }
00532
00533 int
00534 chkPreProcDeleteRule (rsComm_t *rsComm, dataObjInp_t *dataObjUnlinkInp,
00535 dataObjInfo_t *dataObjInfoHead)
00536 {
00537 dataObjInfo_t *tmpDataObjInfo;
00538 ruleExecInfo_t rei;
00539 int status = 0;
00540
00541 initReiWithDataObjInp (&rei, rsComm, dataObjUnlinkInp);
00542 tmpDataObjInfo = dataObjInfoHead;
00543 while (tmpDataObjInfo != NULL) {
00544
00545 rei.doi = tmpDataObjInfo;
00546
00547 status = applyRule ("acDataDeletePolicy", NULL, &rei, NO_SAVE_REI);
00548
00549 if (status < 0 && status != NO_MORE_RULES_ERR &&
00550 status != SYS_DELETE_DISALLOWED) {
00551 rodsLog (LOG_ERROR,
00552 "chkPreProcDeleteRule: acDataDeletePolicy err for %s. stat = %d",
00553 dataObjUnlinkInp->objPath, status);
00554 return (status);
00555 }
00556
00557 if (rei.status == SYS_DELETE_DISALLOWED) {
00558 rodsLog (LOG_ERROR,
00559 "chkPreProcDeleteRule:acDataDeletePolicy disallowed delete of %s",
00560 dataObjUnlinkInp->objPath);
00561 return (rei.status);
00562 }
00563 tmpDataObjInfo = tmpDataObjInfo->next;
00564 }
00565 return status;
00566 }