00001
00002
00003
00004
00005
00006 #include "reGlobalsExtern.h"
00007 #include "rcMisc.h"
00008 #include "objMetaOpr.h"
00009 #include "resource.h"
00010
00011
00012
00013 #include "eirods_resource_backport.h"
00014
00015
00016 void *mallocAndZero(int s)
00017 {
00018 void *t;
00019 t = malloc(s);
00020 memset(t,0,s);
00021 return(t);
00022 }
00023
00024 int
00025 copyRuleExecInfo(ruleExecInfo_t *from, ruleExecInfo_t *to)
00026 {
00027
00028 (*to) = (*from);
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042 if( strlen( from->pluginInstanceName ) != 0 ) {
00043 rstrcpy( to->pluginInstanceName, from->pluginInstanceName, MAX_NAME_LEN );
00044 }
00045
00046 if (from->doi != NULL) {
00047 to->doi = (dataObjInfo_t *) mallocAndZero(sizeof(dataObjInfo_t));
00048 copyDataObjInfo(from->doi, to->doi);
00049 }
00050 else
00051 to->doi = NULL;
00052
00053 if (from->rgi != NULL) {
00054 to->rgi = (rescGrpInfo_t*)mallocAndZero(sizeof(rescGrpInfo_t));
00055 copyRescGrpInfo(from->rgi, to->rgi);
00056 }
00057 else
00058 to->rgi = NULL;
00059
00060 if (from->uoic != NULL) {
00061 to->uoic = (userInfo_t*)mallocAndZero(sizeof(userInfo_t));
00062 copyUserInfo(from->uoic, to->uoic);
00063 }
00064 else
00065 to->uoic = NULL;
00066
00067 if (from->uoip != NULL) {
00068 to->uoip = (userInfo_t*)mallocAndZero(sizeof(userInfo_t));
00069 copyUserInfo(from->uoip, to->uoip);
00070 }
00071 else
00072 to->uoip = NULL;
00073
00074 if (from->coi != NULL) {
00075 to->coi = (collInfo_t*)mallocAndZero(sizeof(collInfo_t));
00076 copyCollInfo(from->coi, to->coi);
00077 }
00078 else
00079 to->coi = NULL;
00080
00081 #if 0
00082 if (from->rgio != NULL) {
00083 to->rgio = mallocAndZero(sizeof(rescGrpInfo_t));
00084 copyRescGrpInfo(from->rgio, to->rgio);
00085 }
00086 else
00087 to->rgi = NULL;
00088 #endif
00089
00090 if (from->uoio != NULL) {
00091 to->uoio= (userInfo_t*)mallocAndZero(sizeof(userInfo_t));
00092 copyUserInfo(from->uoio, to->uoio);
00093 }
00094 else
00095 to->uoio = NULL;
00096
00097 if (from->condInputData != NULL) {
00098 to->condInputData = (keyValPair_t*)mallocAndZero(sizeof(keyValPair_t));
00099 copyKeyValPairStruct(from->condInputData, to->condInputData);
00100 }
00101 else
00102 to->condInputData = NULL;
00103
00104 if (from->next != NULL) {
00105 to->next = (ruleExecInfo_t*)mallocAndZero(sizeof(ruleExecInfo_t));
00106 copyRuleExecInfo(from->next,to->next);
00107 }
00108 else
00109 to->next = NULL;
00110 return(0);
00111 }
00112
00113 int
00114 freeRuleExecInfoStruct(ruleExecInfo_t *rs, int freeSpeialStructFlag)
00115 {
00116 freeRuleExecInfoInternals(rs, freeSpeialStructFlag);
00117 free(rs);
00118 return(0);
00119 }
00120 int
00121 freeRuleExecInfoInternals(ruleExecInfo_t *rs, int freeSpeialStructFlag)
00122 {
00123 if (rs->msParamArray != NULL && (freeSpeialStructFlag & FREE_MS_PARAM) > 0) {
00124 clearMsParamArray (rs->msParamArray, 1);
00125 free (rs->msParamArray);
00126 }
00127
00128 if (rs->doinp != NULL && (freeSpeialStructFlag & FREE_DOINP) > 0) {
00129 clearDataObjInp (rs->doinp);
00130 free (rs->doinp);
00131 }
00132
00133 if (rs->doi != NULL)
00134 freeAllDataObjInfo(rs->doi);
00135 if (rs->rgi != NULL)
00136 freeRescGrpInfo(rs->rgi);
00137 if (rs->uoic != NULL)
00138 freeUserInfo(rs->uoic);
00139 if (rs->uoip != NULL)
00140 freeUserInfo(rs->uoip);
00141 if (rs->coi != NULL)
00142 freeCollInfo(rs->coi);
00143 #if 0
00144 if (rs->rgio != NULL)
00145 freeRescGrpInfo(rs->rgio);
00146 #endif
00147 if (rs->uoio != NULL)
00148 freeUserInfo(rs->uoio);
00149 if (rs->condInputData != NULL)
00150 freeKeyValPairStruct(rs->condInputData);
00151 if (rs->next != NULL)
00152 freeRuleExecInfoStruct(rs->next, freeSpeialStructFlag);
00153 return(0);
00154 }
00155
00156 int
00157 copyDataObjInfo(dataObjInfo_t *from, dataObjInfo_t *to)
00158 {
00159 *to = *from;
00160 if (from->next != NULL) {
00161 to->next = (dataObjInfo_t*)mallocAndZero(sizeof(dataObjInfo_t));
00162 copyDataObjInfo(from->next,to->next);
00163 }
00164 else
00165 to->next = NULL;
00166 return(0);
00167 }
00168
00169
00170 int
00171 copyCollInfo(collInfo_t *from, collInfo_t *to)
00172 {
00173 *to = *from;
00174 if (from->next != NULL) {
00175 to->next = (collInfo_t*)mallocAndZero(sizeof(collInfo_t));
00176 copyCollInfo(from->next,to->next);
00177 }
00178 else
00179 to->next = NULL;
00180 return(0);
00181 }
00182
00183
00184 int
00185 copyRescGrpInfo(rescGrpInfo_t *from, rescGrpInfo_t *to)
00186 {
00187 *to = *from;
00188 if (from->next != NULL) {
00189 to->next = (rescGrpInfo_t*)mallocAndZero(sizeof(rescGrpInfo_t));
00190 copyRescGrpInfo(from->next,to->next);
00191 }
00192 else
00193 to->next = NULL;
00194 return(0);
00195 }
00196
00197
00198
00199 int
00200 freeCollInfo(collInfo_t *rs)
00201 {
00202 if (rs->next != NULL)
00203 freeCollInfo(rs->next);
00204 free(rs);
00205 return(0);
00206 }
00207
00208 int
00209 freeRescGrpInfo( rescGrpInfo_t *rs)
00210 {
00211 if (rs->next != NULL)
00212 freeRescGrpInfo(rs->next);
00213 if( rs->rescInfo ) {
00214 delete rs->rescInfo;
00215 }
00216 free(rs);
00217 return(0);
00218 }
00219
00220 int
00221 copyUserInfo(userInfo_t *from, userInfo_t *to)
00222 {
00223 *to = *from;
00224 to->authInfo = from->authInfo;
00225 to->userOtherInfo = from->userOtherInfo;
00226 return(0);
00227 }
00228
00229
00230 int
00231 freeUserInfo(userInfo_t *rs)
00232 {
00233 free(rs);
00234 return(0);
00235 }
00236
00237
00238
00239 int
00240 copyRescInfo(rescInfo_t *from, rescInfo_t *to)
00241 {
00242 *to = *from;
00243 return(0);
00244 }
00245
00246
00247 int
00248 freeRescInfo(rescInfo_t *rs)
00249 {
00250 free(rs);
00251 return(0);
00252 }
00253
00254
00255
00256 int
00257 copyKeyValPairStruct(keyValPair_t *from, keyValPair_t *to)
00258 {
00259 *to = *from;
00260 return(0);
00261 }
00262
00263 int
00264 freeKeyValPairStruct(keyValPair_t *rs)
00265 {
00266 free(rs);
00267 return(0);
00268 }
00269
00270
00271
00272 int
00273 zeroRuleExecInfoStruct(ruleExecInfo_t *rei)
00274 {
00275
00276 memset (rei, 0, sizeof (ruleExecInfo_t));
00277 return(0);
00278 }
00279
00280 int
00281 initReiWithDataObjInp (ruleExecInfo_t *rei, rsComm_t *rsComm,
00282 dataObjInp_t *dataObjInp)
00283 {
00284 memset (rei, 0, sizeof (ruleExecInfo_t));
00285 rei->doinp = dataObjInp;
00286 rei->rsComm = rsComm;
00287 if (rsComm != NULL) {
00288 rei->uoic = &rsComm->clientUser;
00289 rei->uoip = &rsComm->proxyUser;
00290 }
00291
00292 return (0);
00293 }
00294
00295 int
00296 initReiWithCollInp (ruleExecInfo_t *rei, rsComm_t *rsComm,
00297 collInp_t *collCreateInp, collInfo_t *collInfo)
00298 {
00299 int status;
00300
00301 memset (rei, 0, sizeof (ruleExecInfo_t));
00302 memset (collInfo, 0, sizeof (collInfo_t));
00303 rei->coi = collInfo;
00304
00305 if ((status = splitPathByKey (collCreateInp->collName,
00306 collInfo->collParentName, collInfo->collName, '/')) < 0) {
00307 rodsLog (LOG_ERROR,
00308 "initReiWithCollInp: splitPathByKey for %s error, status = %d",
00309 collCreateInp->collName, status);
00310 return status;
00311 } else {
00312 rstrcpy (collInfo->collName, collCreateInp->collName, MAX_NAME_LEN);
00313 }
00314 rei->rsComm = rsComm;
00315 if (rsComm != NULL) {
00316 rei->uoic = &rsComm->clientUser;
00317 rei->uoip = &rsComm->proxyUser;
00318 }
00319
00320 return (0);
00321 }
00322
00323 int
00324 packRei (rsComm_t *rsComm, ruleExecInfo_t *rei,
00325 bytesBuf_t **packedReiBBuf)
00326 {
00327 int status;
00328
00329 if (packedReiBBuf == NULL) {
00330 return (SYS_INTERNAL_NULL_INPUT_ERR);
00331 }
00332
00333
00334
00335 status = packStruct ((void *) rei, packedReiBBuf,
00336 "Rei_PI", RodsPackTable, 0, NATIVE_PROT);
00337
00338 if (status < 0) {
00339 rodsLog (LOG_ERROR,
00340 "packRei: packStruct error. status = %d", status);
00341 return (status);
00342 }
00343
00344 return (status);
00345 }
00346
00347 int
00348 unpackRei (rsComm_t *rsComm, ruleExecInfo_t **rei,
00349 bytesBuf_t *packedReiBBuf)
00350 {
00351 int status;
00352
00353 if (packedReiBBuf == NULL || rei == NULL) {
00354 return (SYS_INTERNAL_NULL_INPUT_ERR);
00355 }
00356
00357
00358
00359
00360 status = unpackStruct (packedReiBBuf->buf, (void **) rei,
00361 "Rei_PI", RodsPackTable, NATIVE_PROT);
00362
00363 if (status < 0) {
00364 rodsLog (LOG_ERROR,
00365 "unpackRei: unpackStruct error. status = %d", status);
00366 return (status);
00367 }
00368
00369
00370
00371 status = touchupPackedRei (rsComm, *rei);
00372
00373 return (status);
00374 }
00375
00376 int
00377 packReiAndArg (rsComm_t *rsComm, ruleExecInfo_t *rei, char *myArgv[],
00378 int myArgc, bytesBuf_t **packedReiAndArgBBuf)
00379 {
00380 int status;
00381 ruleExecInfoAndArg_t reiAndArg;
00382
00383 if (packedReiAndArgBBuf == NULL) {
00384 rodsLog (LOG_ERROR,
00385 "packReiAndArg: NULL packedReiAndArgBBuf input");
00386 return (SYS_INTERNAL_NULL_INPUT_ERR);
00387 }
00388
00389 if (myArgc > 0 && (myArgv == NULL || *myArgv == NULL)) {
00390 rodsLog (LOG_ERROR,
00391 "packReiAndArg: NULL myArgv input");
00392 return (SYS_INTERNAL_NULL_INPUT_ERR);
00393 }
00394
00395 memset (&reiAndArg, 0, sizeof (reiAndArg));
00396
00397 reiAndArg.rei = rei;
00398
00399 reiAndArg.reArg.myArgc = myArgc;
00400 reiAndArg.reArg.myArgv = myArgv;
00401
00402
00403
00404 status = packStruct ((void *) &reiAndArg, packedReiAndArgBBuf,
00405 "ReiAndArg_PI", RodsPackTable, 0, NATIVE_PROT);
00406
00407 if (status < 0) {
00408 rodsLog (LOG_ERROR,
00409 "packReiAndArg: packStruct error. status = %d", status);
00410 return (status);
00411 }
00412
00413 return (status);
00414 }
00415
00416 int
00417 unpackReiAndArg (rsComm_t *rsComm, ruleExecInfoAndArg_t **reiAndArg,
00418 bytesBuf_t *packedReiAndArgBBuf)
00419 {
00420 int status;
00421
00422
00423 if (packedReiAndArgBBuf == NULL || reiAndArg == NULL) {
00424 return (SYS_INTERNAL_NULL_INPUT_ERR);
00425 }
00426
00427
00428
00429 status = unpackStruct (packedReiAndArgBBuf->buf, (void **) reiAndArg,
00430 "ReiAndArg_PI", RodsPackTable, NATIVE_PROT);
00431
00432 if (status < 0) {
00433 rodsLog (LOG_ERROR,
00434 "unpackReiAndArg: unpackStruct error. status = %d", status);
00435 return (status);
00436 }
00437
00438
00439
00440 status = touchupPackedRei (rsComm, (*reiAndArg)->rei);
00441
00442 return (status);
00443 }
00444
00445 int
00446 touchupPackedRei (rsComm_t *rsComm, ruleExecInfo_t *myRei)
00447 {
00448 int savedStatus = 0;
00449 rescInfo_t *rescInfo = NULL;
00450
00451 if (myRei == NULL || rsComm == NULL) {
00452 return (SYS_INTERNAL_NULL_INPUT_ERR);
00453 }
00454
00455 if (myRei->rsComm != NULL) {
00456 free (myRei->rsComm);
00457 }
00458
00459 myRei->rsComm = rsComm;
00460
00461 rsComm->clientUser = *(myRei->uoic);
00462
00463 if (myRei->doi != NULL) {
00464 if (myRei->doi->next != NULL) {
00465 free (myRei->doi->next);
00466 myRei->doi->next = NULL;
00467 }
00468 rescInfo = new rescInfo_t;
00469 eirods::resource_ptr resc;
00470 std::string rescName = myRei->doi->rescInfo->rescName;
00471 free (myRei->doi->rescInfo);
00472 eirods::error err = eirods::get_resc_info(rescName, *rescInfo );
00473 if( !err.ok() ) {
00474 delete rescInfo;
00475 savedStatus = err.code();
00476 myRei->doi->rescInfo = NULL;
00477 std::stringstream msg;
00478 msg << "failed to resolve resource [";
00479 msg << rescName;
00480 msg << "]";
00481 eirods::log( PASSMSG( msg.str(), err ) );
00482 } else {
00483 myRei->doi->rescInfo = rescInfo;
00484 }
00485
00486 }
00487
00488 if (myRei->rgi != NULL) {
00489 rescGrpInfo_t *rescGrpInfo = myRei->rgi;
00490 while (rescGrpInfo != NULL) {
00491
00492 rescInfo = new rescInfo_t;
00493 eirods::resource_ptr resc;
00494 std::string rescName = rescGrpInfo->rescInfo->rescName;
00495 eirods::error err = eirods::get_resc_info( rescName, *rescInfo );
00496 free(rescGrpInfo->rescInfo);
00497 if( !err.ok() ) {
00498 delete rescInfo;
00499 savedStatus = err.code();
00500 rescGrpInfo->rescInfo = NULL;
00501 std::stringstream msg;
00502 msg << "failed to resolve resource [";
00503 msg << rescName;
00504 msg << "]";
00505 eirods::log( PASSMSG( msg.str(), err ) );
00506 } else {
00507 rescGrpInfo->rescInfo = rescInfo;
00508 }
00509
00510 rescGrpInfo = rescGrpInfo->next;
00511 }
00512
00513 if (myRei->next != NULL) {
00514 free (myRei->next);
00515 myRei->next = NULL;
00516 }
00517 }
00518 return (savedStatus);
00519 }
00520
00521
00522 int
00523 copyTaggedValue(char *str, char *tag, char *buf, int bufLen)
00524 {
00525
00526 char tVal[NAME_LEN];
00527 char *t,*s, *u;
00528
00529 snprintf(tVal, NAME_LEN, "<%s>", tag );
00530 if ((t = strstr(str,tVal)) == NULL) {
00531 if (strcmp(tag,"KVALPR"))
00532 return(UNMATCHED_KEY_OR_INDEX);
00533 else {
00534 while ((t = strstr(str,"<_____X>")) != NULL) {
00535 memcpy(t+1,tag,6);
00536 }
00537 }
00538 return(-1);
00539 }
00540 s = t + strlen(tVal);
00541 snprintf(tVal, NAME_LEN,"</%s>", tag);
00542 if ((u = strstr(str,tVal)) == NULL) {
00543 return(INPUT_ARG_NOT_WELL_FORMED_ERR);
00544 }
00545 *u = '\0';
00546 strncpy(buf,s,bufLen);
00547 *u = '<';
00548 if (!strcmp(tag,"KVALPR"))
00549 memcpy(t+1,"_____X",6);
00550 return(0);
00551
00552 }
00553 int
00554 fillSubmitConditions (char *action, char *inDelayCondition,
00555 bytesBuf_t *packedReiAndArgBBuf, ruleExecSubmitInp_t *ruleSubmitInfo,
00556 ruleExecInfo_t *rei )
00557 {
00558 int i;
00559 int j = 0;
00560 char kwp[NAME_LEN * 2];
00561 char *t, *s;
00562 char *delayCondition;
00563
00564 delayCondition = strdup(inDelayCondition);
00565 strncpy(ruleSubmitInfo->ruleName, action, META_STR_LEN);
00566
00567
00568
00569
00570 i= copyTaggedValue(delayCondition,"EA", ruleSubmitInfo->exeAddress,NAME_LEN);
00571 if (i != 0 && i != UNMATCHED_KEY_OR_INDEX) {
00572 free( delayCondition );
00573 return(i);
00574 }
00575 i= copyTaggedValue(delayCondition,"ET", ruleSubmitInfo->exeTime,NAME_LEN);
00576 if (i != 0 && i != UNMATCHED_KEY_OR_INDEX) {
00577 free( delayCondition );
00578 return(i);
00579 }
00580 else if (i == 0) {
00581 i = checkDateFormat(ruleSubmitInfo->exeTime);
00582 if (i != 0) {
00583 free( delayCondition );
00584 return(i);
00585 }
00586
00587 }
00588 i= copyTaggedValue(delayCondition,"EF", ruleSubmitInfo->exeFrequency,NAME_LEN);
00589 if (i != 0 && i != UNMATCHED_KEY_OR_INDEX) {
00590 free( delayCondition );
00591 return(i);
00592 }
00593 i= copyTaggedValue(delayCondition,"PRI",ruleSubmitInfo->priority,NAME_LEN);
00594 if (i != 0 && i != UNMATCHED_KEY_OR_INDEX) {
00595 free( delayCondition );
00596 return(i);
00597 }
00598 i= copyTaggedValue(delayCondition,"EET",ruleSubmitInfo->estimateExeTime,NAME_LEN);
00599 if (i != 0 && i != UNMATCHED_KEY_OR_INDEX) {
00600 free( delayCondition );
00601 return(i);
00602 }
00603 i= copyTaggedValue(delayCondition,"NA", ruleSubmitInfo->notificationAddr,NAME_LEN);
00604 if (i != 0 && i != UNMATCHED_KEY_OR_INDEX) {
00605 free( delayCondition );
00606 return(i);
00607 }
00608 i= copyTaggedValue(delayCondition,"PLUSET", kwp,NAME_LEN * 2);
00609 if (i != 0 && i != UNMATCHED_KEY_OR_INDEX) {
00610 free( delayCondition );
00611 return(i);
00612 }
00613 else if (i == 0) {
00614 i = checkDateFormat(kwp);
00615 if (i != 0)
00616 return(i);
00617 getOffsetTimeStr(ruleSubmitInfo->exeTime,kwp);
00618 }
00619 if (i != 0 && i != UNMATCHED_KEY_OR_INDEX) return(i);
00620 i= copyTaggedValue(delayCondition,"KVALPR",kwp,NAME_LEN * 2);
00621 while (i >= 0) {
00622 if ((t = strstr(kwp,"=")) == NULL) {
00623 free(delayCondition);
00624 return(INPUT_ARG_NOT_WELL_FORMED_ERR);
00625 }
00626 *t = '\0';
00627 s = t-1;
00628 while (*s == ' ') s--;
00629 *(s+1) = '\0';
00630 ruleSubmitInfo->condInput.keyWord[j] = strdup(kwp);
00631 t++;
00632 while (*t == ' ') t++;
00633 ruleSubmitInfo->condInput.value[j] = t;
00634 j++;
00635 i= copyTaggedValue(delayCondition,"KWVAL",kwp,NAME_LEN * 2);
00636 }
00637 ruleSubmitInfo->condInput.len = j;
00638 ruleSubmitInfo->packedReiAndArgBBuf = packedReiAndArgBBuf;
00639 if (ruleSubmitInfo->userName == NULL || strlen(ruleSubmitInfo->userName) == 0) {
00640 if (rei->uoic->userName != NULL)
00641 strncpy(ruleSubmitInfo->userName, rei->uoic->userName,NAME_LEN);
00642 else if (rei->rsComm->clientUser.userName != NULL)
00643 strncpy(rei->rsComm->clientUser.userName,rei->uoic->userName,NAME_LEN);
00644 }
00645 free(delayCondition);
00646 return(0);
00647 }
00648
00649
00650 int
00651 pushStack(strArray_t *strArray, char *value)
00652 {
00653
00654 return(addStrArray (strArray, value));
00655
00656 }
00657
00658 int
00659 popStack(strArray_t *strArray, char *value)
00660 {
00661
00662 if (strArray->len <= 0 || strArray->size == 0) {
00663 rodsLog (LOG_ERROR,
00664 "popStack: Stack is empty: invalid size %d, len %d",
00665 strArray->size, strArray->len);
00666 return(SYS_INTERNAL_NULL_INPUT_ERR);
00667 }
00668 rstrcpy (value, &strArray->value[(strArray->len - 1) * strArray->size], strArray->size);
00669 strArray->len--;
00670 return(0);
00671
00672 }
00673
00674 int
00675 clearMsparamInRei (ruleExecInfo_t *rei)
00676 {
00677 if (rei == NULL || rei->msParamArray == NULL) {
00678 return (0);
00679 }
00680
00681
00682 clearMsParamArray (rei->msParamArray, 0);
00683 free (rei->msParamArray);
00684
00685 rei->msParamArray = NULL;
00686
00687 return (0);
00688 }
00689