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 free(rs);
00214 return(0);
00215 }
00216
00217 int
00218 copyUserInfo(userInfo_t *from, userInfo_t *to)
00219 {
00220 *to = *from;
00221 to->authInfo = from->authInfo;
00222 to->userOtherInfo = from->userOtherInfo;
00223 return(0);
00224 }
00225
00226
00227 int
00228 freeUserInfo(userInfo_t *rs)
00229 {
00230 free(rs);
00231 return(0);
00232 }
00233
00234
00235
00236 int
00237 copyRescInfo(rescInfo_t *from, rescInfo_t *to)
00238 {
00239 *to = *from;
00240 return(0);
00241 }
00242
00243
00244 int
00245 freeRescInfo(rescInfo_t *rs)
00246 {
00247 free(rs);
00248 return(0);
00249 }
00250
00251
00252
00253 int
00254 copyKeyValPairStruct(keyValPair_t *from, keyValPair_t *to)
00255 {
00256 *to = *from;
00257 return(0);
00258 }
00259
00260 int
00261 freeKeyValPairStruct(keyValPair_t *rs)
00262 {
00263 free(rs);
00264 return(0);
00265 }
00266
00267
00268
00269 int
00270 zeroRuleExecInfoStruct(ruleExecInfo_t *rei)
00271 {
00272
00273 memset (rei, 0, sizeof (ruleExecInfo_t));
00274 return(0);
00275 }
00276
00277 int
00278 initReiWithDataObjInp (ruleExecInfo_t *rei, rsComm_t *rsComm,
00279 dataObjInp_t *dataObjInp)
00280 {
00281 memset (rei, 0, sizeof (ruleExecInfo_t));
00282 rei->doinp = dataObjInp;
00283 rei->rsComm = rsComm;
00284 if (rsComm != NULL) {
00285 rei->uoic = &rsComm->clientUser;
00286 rei->uoip = &rsComm->proxyUser;
00287 }
00288
00289 return (0);
00290 }
00291
00292 int
00293 initReiWithCollInp (ruleExecInfo_t *rei, rsComm_t *rsComm,
00294 collInp_t *collCreateInp, collInfo_t *collInfo)
00295 {
00296 int status;
00297
00298 memset (rei, 0, sizeof (ruleExecInfo_t));
00299 memset (collInfo, 0, sizeof (collInfo_t));
00300 rei->coi = collInfo;
00301
00302 if ((status = splitPathByKey (collCreateInp->collName,
00303 collInfo->collParentName, collInfo->collName, '/')) < 0) {
00304 rodsLog (LOG_ERROR,
00305 "initReiWithCollInp: splitPathByKey for %s error, status = %d",
00306 collCreateInp->collName, status);
00307 return status;
00308 } else {
00309 rstrcpy (collInfo->collName, collCreateInp->collName, MAX_NAME_LEN);
00310 }
00311 rei->rsComm = rsComm;
00312 if (rsComm != NULL) {
00313 rei->uoic = &rsComm->clientUser;
00314 rei->uoip = &rsComm->proxyUser;
00315 }
00316
00317 return (0);
00318 }
00319
00320 int
00321 packRei (rsComm_t *rsComm, ruleExecInfo_t *rei,
00322 bytesBuf_t **packedReiBBuf)
00323 {
00324 int status;
00325
00326 if (packedReiBBuf == NULL) {
00327 return (SYS_INTERNAL_NULL_INPUT_ERR);
00328 }
00329
00330
00331
00332 status = packStruct ((void *) rei, packedReiBBuf,
00333 "Rei_PI", RodsPackTable, 0, NATIVE_PROT);
00334
00335 if (status < 0) {
00336 rodsLog (LOG_ERROR,
00337 "packRei: packStruct error. status = %d", status);
00338 return (status);
00339 }
00340
00341 return (status);
00342 }
00343
00344 int
00345 unpackRei (rsComm_t *rsComm, ruleExecInfo_t **rei,
00346 bytesBuf_t *packedReiBBuf)
00347 {
00348 int status;
00349
00350 if (packedReiBBuf == NULL || rei == NULL) {
00351 return (SYS_INTERNAL_NULL_INPUT_ERR);
00352 }
00353
00354
00355
00356
00357 status = unpackStruct (packedReiBBuf->buf, (void **) rei,
00358 "Rei_PI", RodsPackTable, NATIVE_PROT);
00359
00360 if (status < 0) {
00361 rodsLog (LOG_ERROR,
00362 "unpackRei: unpackStruct error. status = %d", status);
00363 return (status);
00364 }
00365
00366
00367
00368 status = touchupPackedRei (rsComm, *rei);
00369
00370 return (status);
00371 }
00372
00373 int
00374 packReiAndArg (rsComm_t *rsComm, ruleExecInfo_t *rei, char *myArgv[],
00375 int myArgc, bytesBuf_t **packedReiAndArgBBuf)
00376 {
00377 int status;
00378 ruleExecInfoAndArg_t reiAndArg;
00379
00380 if (packedReiAndArgBBuf == NULL) {
00381 rodsLog (LOG_ERROR,
00382 "packReiAndArg: NULL packedReiAndArgBBuf input");
00383 return (SYS_INTERNAL_NULL_INPUT_ERR);
00384 }
00385
00386 if (myArgc > 0 && (myArgv == NULL || *myArgv == NULL)) {
00387 rodsLog (LOG_ERROR,
00388 "packReiAndArg: NULL myArgv input");
00389 return (SYS_INTERNAL_NULL_INPUT_ERR);
00390 }
00391
00392 memset (&reiAndArg, 0, sizeof (reiAndArg));
00393
00394 reiAndArg.rei = rei;
00395
00396 reiAndArg.reArg.myArgc = myArgc;
00397 reiAndArg.reArg.myArgv = myArgv;
00398
00399
00400
00401 status = packStruct ((void *) &reiAndArg, packedReiAndArgBBuf,
00402 "ReiAndArg_PI", RodsPackTable, 0, NATIVE_PROT);
00403
00404 if (status < 0) {
00405 rodsLog (LOG_ERROR,
00406 "packReiAndArg: packStruct error. status = %d", status);
00407 return (status);
00408 }
00409
00410 return (status);
00411 }
00412
00413 int
00414 unpackReiAndArg (rsComm_t *rsComm, ruleExecInfoAndArg_t **reiAndArg,
00415 bytesBuf_t *packedReiAndArgBBuf)
00416 {
00417 int status;
00418
00419
00420 if (packedReiAndArgBBuf == NULL || reiAndArg == NULL) {
00421 return (SYS_INTERNAL_NULL_INPUT_ERR);
00422 }
00423
00424
00425
00426 status = unpackStruct (packedReiAndArgBBuf->buf, (void **) reiAndArg,
00427 "ReiAndArg_PI", RodsPackTable, NATIVE_PROT);
00428
00429 if (status < 0) {
00430 rodsLog (LOG_ERROR,
00431 "unpackReiAndArg: unpackStruct error. status = %d", status);
00432 return (status);
00433 }
00434
00435
00436
00437 status = touchupPackedRei (rsComm, (*reiAndArg)->rei);
00438
00439 return (status);
00440 }
00441
00442 int
00443 touchupPackedRei (rsComm_t *rsComm, ruleExecInfo_t *myRei)
00444 {
00445 int savedStatus = 0;
00446 rescInfo_t *rescInfo = NULL;
00447
00448 if (myRei == NULL || rsComm == NULL) {
00449 return (SYS_INTERNAL_NULL_INPUT_ERR);
00450 }
00451
00452 if (myRei->rsComm != NULL) {
00453 free (myRei->rsComm);
00454 }
00455
00456 myRei->rsComm = rsComm;
00457
00458 rsComm->clientUser = *(myRei->uoic);
00459
00460 if (myRei->doi != NULL) {
00461 if (myRei->doi->next != NULL) {
00462 free (myRei->doi->next);
00463 myRei->doi->next = NULL;
00464 }
00465 rescInfo = new rescInfo_t;
00466 eirods::resource_ptr resc;
00467 std::string rescName = myRei->doi->rescInfo->rescName;
00468 free (myRei->doi->rescInfo);
00469 eirods::error err = eirods::get_resc_info(rescName, *rescInfo );
00470 if( !err.ok() ) {
00471 delete rescInfo;
00472 savedStatus = err.code();
00473 myRei->doi->rescInfo = NULL;
00474 std::stringstream msg;
00475 msg << "touchupPackedRei - failed to resolve resource \"";
00476 msg << rescName;
00477 msg << "\"";
00478 eirods::log( PASS( false, -1, msg.str(), err ) );
00479 } else {
00480 myRei->doi->rescInfo = rescInfo;
00481 }
00482
00483 }
00484
00485 if (myRei->rgi != NULL) {
00486 rescGrpInfo_t *rescGrpInfo = myRei->rgi;
00487 while (rescGrpInfo != NULL) {
00488
00489 rescInfo = new rescInfo_t;
00490 eirods::resource_ptr resc;
00491 std::string rescName = rescGrpInfo->rescInfo->rescName;
00492 eirods::error err = eirods::get_resc_info( rescName, *rescInfo );
00493 free(rescGrpInfo->rescInfo);
00494 if( !err.ok() ) {
00495 delete rescInfo;
00496 savedStatus = err.code();
00497 rescGrpInfo->rescInfo = NULL;
00498 std::stringstream msg;
00499 msg << "touchupPackedRei - failed to resolve resource \"";
00500 msg << rescName;
00501 msg << "\"";
00502 eirods::log( PASS( false, -1, msg.str(), err ) );
00503 } else {
00504 rescGrpInfo->rescInfo = rescInfo;
00505 }
00506
00507 rescGrpInfo = rescGrpInfo->next;
00508 }
00509
00510 if (myRei->next != NULL) {
00511 free (myRei->next);
00512 myRei->next = NULL;
00513 }
00514 }
00515 return (savedStatus);
00516 }
00517
00518
00519 int
00520 copyTaggedValue(char *str, char *tag, char *buf, int bufLen)
00521 {
00522
00523 char tVal[NAME_LEN];
00524 char *t,*s, *u;
00525
00526 snprintf(tVal, NAME_LEN, "<%s>", tag );
00527 if ((t = strstr(str,tVal)) == NULL) {
00528 if (strcmp(tag,"KVALPR"))
00529 return(UNMATCHED_KEY_OR_INDEX);
00530 else {
00531 while ((t = strstr(str,"<_____X>")) != NULL) {
00532 memcpy(t+1,tag,6);
00533 }
00534 }
00535 return(-1);
00536 }
00537 s = t + strlen(tVal);
00538 snprintf(tVal, NAME_LEN,"</%s>", tag);
00539 if ((u = strstr(str,tVal)) == NULL) {
00540 return(INPUT_ARG_NOT_WELL_FORMED_ERR);
00541 }
00542 *u = '\0';
00543 strncpy(buf,s,bufLen);
00544 *u = '<';
00545 if (!strcmp(tag,"KVALPR"))
00546 memcpy(t+1,"_____X",6);
00547 return(0);
00548
00549 }
00550 int
00551 fillSubmitConditions (char *action, char *inDelayCondition,
00552 bytesBuf_t *packedReiAndArgBBuf, ruleExecSubmitInp_t *ruleSubmitInfo,
00553 ruleExecInfo_t *rei )
00554 {
00555 int i;
00556 int j = 0;
00557 char kwp[NAME_LEN * 2];
00558 char *t, *s;
00559 char *delayCondition;
00560
00561 delayCondition = strdup(inDelayCondition);
00562 strncpy(ruleSubmitInfo->ruleName, action, META_STR_LEN);
00563
00564
00565
00566
00567 i= copyTaggedValue(delayCondition,"EA", ruleSubmitInfo->exeAddress,NAME_LEN);
00568 if (i != 0 && i != UNMATCHED_KEY_OR_INDEX) {
00569 free( delayCondition );
00570 return(i);
00571 }
00572 i= copyTaggedValue(delayCondition,"ET", ruleSubmitInfo->exeTime,NAME_LEN);
00573 if (i != 0 && i != UNMATCHED_KEY_OR_INDEX) {
00574 free( delayCondition );
00575 return(i);
00576 }
00577 else if (i == 0) {
00578 i = checkDateFormat(ruleSubmitInfo->exeTime);
00579 if (i != 0) {
00580 free( delayCondition );
00581 return(i);
00582 }
00583
00584 }
00585 i= copyTaggedValue(delayCondition,"EF", ruleSubmitInfo->exeFrequency,NAME_LEN);
00586 if (i != 0 && i != UNMATCHED_KEY_OR_INDEX) {
00587 free( delayCondition );
00588 return(i);
00589 }
00590 i= copyTaggedValue(delayCondition,"PRI",ruleSubmitInfo->priority,NAME_LEN);
00591 if (i != 0 && i != UNMATCHED_KEY_OR_INDEX) {
00592 free( delayCondition );
00593 return(i);
00594 }
00595 i= copyTaggedValue(delayCondition,"EET",ruleSubmitInfo->estimateExeTime,NAME_LEN);
00596 if (i != 0 && i != UNMATCHED_KEY_OR_INDEX) {
00597 free( delayCondition );
00598 return(i);
00599 }
00600 i= copyTaggedValue(delayCondition,"NA", ruleSubmitInfo->notificationAddr,NAME_LEN);
00601 if (i != 0 && i != UNMATCHED_KEY_OR_INDEX) {
00602 free( delayCondition );
00603 return(i);
00604 }
00605 i= copyTaggedValue(delayCondition,"PLUSET", kwp,NAME_LEN * 2);
00606 if (i != 0 && i != UNMATCHED_KEY_OR_INDEX) {
00607 free( delayCondition );
00608 return(i);
00609 }
00610 else if (i == 0) {
00611 i = checkDateFormat(kwp);
00612 if (i != 0)
00613 return(i);
00614 getOffsetTimeStr(ruleSubmitInfo->exeTime,kwp);
00615 }
00616 if (i != 0 && i != UNMATCHED_KEY_OR_INDEX) return(i);
00617 i= copyTaggedValue(delayCondition,"KVALPR",kwp,NAME_LEN * 2);
00618 while (i >= 0) {
00619 if ((t = strstr(kwp,"=")) == NULL) {
00620 free(delayCondition);
00621 return(INPUT_ARG_NOT_WELL_FORMED_ERR);
00622 }
00623 *t = '\0';
00624 s = t-1;
00625 while (*s == ' ') s--;
00626 *(s+1) = '\0';
00627 ruleSubmitInfo->condInput.keyWord[j] = strdup(kwp);
00628 t++;
00629 while (*t == ' ') t++;
00630 ruleSubmitInfo->condInput.value[j] = t;
00631 j++;
00632 i= copyTaggedValue(delayCondition,"KWVAL",kwp,NAME_LEN * 2);
00633 }
00634 ruleSubmitInfo->condInput.len = j;
00635 ruleSubmitInfo->packedReiAndArgBBuf = packedReiAndArgBBuf;
00636 if (ruleSubmitInfo->userName == NULL || strlen(ruleSubmitInfo->userName) == 0) {
00637 if (rei->uoic->userName != NULL)
00638 strncpy(ruleSubmitInfo->userName, rei->uoic->userName,NAME_LEN);
00639 else if (rei->rsComm->clientUser.userName != NULL)
00640 strncpy(rei->rsComm->clientUser.userName,rei->uoic->userName,NAME_LEN);
00641 }
00642 free(delayCondition);
00643 return(0);
00644 }
00645
00646
00647 int
00648 pushStack(strArray_t *strArray, char *value)
00649 {
00650
00651 return(addStrArray (strArray, value));
00652
00653 }
00654
00655 int
00656 popStack(strArray_t *strArray, char *value)
00657 {
00658
00659 if (strArray->len <= 0 || strArray->size == 0) {
00660 rodsLog (LOG_ERROR,
00661 "popStack: Stack is empty: invalid size %d, len %d",
00662 strArray->size, strArray->len);
00663 return(SYS_INTERNAL_NULL_INPUT_ERR);
00664 }
00665 rstrcpy (value, &strArray->value[(strArray->len - 1) * strArray->size], strArray->size);
00666 strArray->len--;
00667 return(0);
00668
00669 }
00670
00671 int
00672 clearMsparamInRei (ruleExecInfo_t *rei)
00673 {
00674 if (rei == NULL || rei->msParamArray == NULL) {
00675 return (0);
00676 }
00677
00678
00679 clearMsParamArray (rei->msParamArray, 0);
00680 free (rei->msParamArray);
00681
00682 rei->msParamArray = NULL;
00683
00684 return (0);
00685 }
00686