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