00001
00002
00003
00004
00005 #include "eirods_resource_backport.h"
00006 #include "eirods_string_tokenize.h"
00007 #include "eirods_hierarchy_parser.h"
00008 #include "eirods_stacktrace.h"
00009
00010 namespace eirods {
00011
00012
00013
00014
00015 error resource_to_resc_info( rescInfo_t& _info, resource_ptr& _resc ) {
00016 error err;
00017 std::string prop_name;
00018
00019
00020 prop_name = RESOURCE_HOST;
00021 rodsServerHost_t* host = 0;
00022 err = _resc->get_property< rodsServerHost_t* >( prop_name, host );
00023 if( !err.ok() ) {
00024 std::stringstream msg;
00025 msg << "failed to get property [";
00026 msg << prop_name;
00027 msg << "]";
00028 return ERROR( UNMATCHED_KEY_OR_INDEX, msg.str() );
00029 }
00030
00031
00032
00033 prop_name = RESOURCE_ID;
00034 long id = 0;
00035 err = _resc->get_property< long >( prop_name, id );
00036 if( !err.ok() ) {
00037 std::stringstream msg;
00038 msg << "failed to get property [";
00039 msg << prop_name;
00040 msg << "]";
00041 return ERROR( UNMATCHED_KEY_OR_INDEX, msg.str() );
00042 }
00043
00044
00045
00046 prop_name = RESOURCE_FREESPACE;
00047 long freespace = 0;
00048 err = _resc->get_property< long >( prop_name, freespace );
00049 if( !err.ok() ) {
00050 std::stringstream msg;
00051 msg << "failed to get property [";
00052 msg << prop_name;
00053 msg << "]";
00054 return ERROR( UNMATCHED_KEY_OR_INDEX, msg.str() );
00055 }
00056
00057
00058
00059 prop_name = RESOURCE_QUOTA;
00060 long quota = 0;
00061 err = _resc->get_property< long >( prop_name, quota );
00062 if( !err.ok() ) {
00063 std::stringstream msg;
00064 msg << "failed to get property [";
00065 msg << prop_name;
00066 msg << "]";
00067 return ERROR( UNMATCHED_KEY_OR_INDEX, msg.str() );
00068 }
00069
00070
00071
00072 prop_name = RESOURCE_ZONE;
00073 std::string zone;
00074 err = _resc->get_property< std::string >( prop_name, zone );
00075 if( !err.ok() ) {
00076 std::stringstream msg;
00077 msg << "failed to get property [";
00078 msg << prop_name;
00079 msg << "]";
00080 return ERROR( UNMATCHED_KEY_OR_INDEX, msg.str() );
00081 }
00082
00083
00084
00085 prop_name = RESOURCE_NAME;
00086 std::string name;
00087 err = _resc->get_property< std::string >( prop_name, name );
00088 if( !err.ok() ) {
00089 std::stringstream msg;
00090 msg << "failed to get property [";
00091 msg << prop_name;
00092 msg << "]";
00093 return ERROR( UNMATCHED_KEY_OR_INDEX, msg.str() );
00094 }
00095
00096
00097
00098 prop_name = RESOURCE_LOCATION;
00099 std::string location;
00100 err = _resc->get_property< std::string >( prop_name, location );
00101 if( !err.ok() ) {
00102 std::stringstream msg;
00103 msg << "failed to get property [";
00104 msg << prop_name;
00105 msg << "]";
00106 return ERROR( UNMATCHED_KEY_OR_INDEX, msg.str() );
00107 }
00108
00109
00110
00111 prop_name = RESOURCE_TYPE;
00112 std::string type;
00113 err = _resc->get_property< std::string >( prop_name, type );
00114 if( !err.ok() ) {
00115 std::stringstream msg;
00116 msg << "failed to get property [";
00117 msg << prop_name;
00118 msg << "]";
00119 return ERROR( UNMATCHED_KEY_OR_INDEX, msg.str() );
00120 }
00121
00122
00123
00124 prop_name = RESOURCE_CLASS;
00125 std::string rclass;
00126 err = _resc->get_property< std::string >( prop_name, rclass );
00127 if( !err.ok() ) {
00128 std::stringstream msg;
00129 msg << "failed to get property";
00130 msg << prop_name;
00131 msg << "]";
00132 return ERROR( UNMATCHED_KEY_OR_INDEX, msg.str() );
00133 }
00134
00135
00136
00137 prop_name = RESOURCE_PATH;
00138 std::string path;
00139 err = _resc->get_property< std::string >( prop_name, path );
00140 if( !err.ok() ) {
00141 std::stringstream msg;
00142 msg << "failed to get property";
00143 msg << prop_name;
00144 msg << "]";
00145 return ERROR( UNMATCHED_KEY_OR_INDEX, msg.str() );
00146 }
00147
00148
00149
00150 prop_name = RESOURCE_INFO;
00151 std::string info;
00152 err = _resc->get_property< std::string >( prop_name, info );
00153 if( !err.ok() ) {
00154 std::stringstream msg;
00155 msg << "failed to get property";
00156 msg << prop_name;
00157 msg << "]";
00158 return ERROR( UNMATCHED_KEY_OR_INDEX, msg.str() );
00159 }
00160
00161
00162
00163 prop_name = RESOURCE_COMMENTS;
00164 std::string comments;
00165 err = _resc->get_property< std::string >( prop_name, comments );
00166 if( !err.ok() ) {
00167 std::stringstream msg;
00168 msg << "failed to get property";
00169 msg << prop_name;
00170 msg << "]";
00171 return ERROR( UNMATCHED_KEY_OR_INDEX, msg.str() );
00172 }
00173
00174
00175
00176 prop_name = RESOURCE_CREATE_TS;
00177 std::string create;
00178 err = _resc->get_property< std::string >( prop_name, create );
00179 if( !err.ok() ) {
00180 std::stringstream msg;
00181 msg << "failed to get property [";
00182 msg << prop_name;
00183 msg << "]";
00184 return ERROR( UNMATCHED_KEY_OR_INDEX, msg.str() );
00185 }
00186
00187
00188
00189 prop_name = RESOURCE_MODIFY_TS;
00190 std::string modify;
00191 err = _resc->get_property< std::string >( prop_name, modify );
00192 if( !err.ok() ) {
00193 std::stringstream msg;
00194 msg << "failed to get property [";
00195 msg << prop_name;
00196 msg << "]";
00197 return ERROR( UNMATCHED_KEY_OR_INDEX, msg.str() );
00198 }
00199
00200
00201
00202 prop_name = RESOURCE_STATUS;
00203 int status = 0;
00204 err = _resc->get_property< int >( prop_name, status );
00205 if( !err.ok() ) {
00206 std::stringstream msg;
00207 msg << "failed to get property [";
00208 msg << prop_name;
00209 msg << "]";
00210 return ERROR( UNMATCHED_KEY_OR_INDEX, msg.str() );
00211 }
00212
00213 _info.rodsServerHost = host;
00214 _info.rescId = id;
00215 _info.freeSpace = freespace;
00216 _info.quotaLimit = quota;
00217 _info.rescStatus = status;
00218 strncpy( _info.zoneName, zone.c_str(), NAME_LEN );
00219 strncpy( _info.rescName, name.c_str(), NAME_LEN );
00220 strncpy( _info.rescLoc, location.c_str(), NAME_LEN );
00221 strncpy( _info.rescType, type.c_str(), NAME_LEN );
00222 strncpy( _info.rescClass, rclass.c_str(), NAME_LEN );
00223 strncpy( _info.rescVaultPath, path.c_str(), NAME_LEN );
00224 strncpy( _info.rescInfo, info.c_str(), NAME_LEN );
00225 strncpy( _info.rescComments, comments.c_str(), NAME_LEN );
00226 strncpy( _info.rescCreate, create.c_str(), TIME_LEN );
00227 strncpy( _info.rescModify, modify.c_str(), TIME_LEN );
00228
00229 return SUCCESS();
00230
00231 }
00232
00233
00234
00235
00236 error resource_to_resc_grp_info( rescGrpInfo_t& _grp_info, resource_ptr& _resc ) {
00237
00238
00239 _grp_info.next = NULL;
00240 _grp_info.cacheNext = NULL;
00241 _grp_info.status = 0;
00242 _grp_info.dummy = 0;
00243
00244
00245
00246 if( !_grp_info.rescInfo ) {
00247 _grp_info.rescInfo = new rescInfo_t;
00248 } else {
00249
00250 }
00251
00252
00253
00254 error err = resource_to_resc_info( *_grp_info.rescInfo, _resc );
00255 if( !err.ok() ) {
00256 return PASS( err );
00257
00258 }
00259
00260
00261
00262
00263 rstrcpy( _grp_info.rescGroupName, _grp_info.rescInfo->rescName, NAME_LEN );
00264
00265 return SUCCESS();
00266
00267 }
00268
00269
00270
00271
00272
00273
00274
00275 error set_default_resource( rsComm_t* _comm, std::string _resc_list,
00276 std::string _option, keyValPair_t* _cond_input,
00277 rescGrpInfo_t& _resc_grp ) {
00278
00279
00280 if( _resc_list.empty() && NULL == _cond_input ) {
00281 return ERROR( USER_NO_RESC_INPUT_ERR, "no user input" );
00282 }
00283
00284
00285
00286 std::string cond_input_resc;
00287
00288
00289
00290
00291 if( "null" != _resc_list &&
00292 "forced" == _option &&
00293 _comm->proxyUser.authInfo.authFlag < LOCAL_PRIV_USER_AUTH ) {
00294 _cond_input = NULL;
00295 } else if( _cond_input ) {
00296 char* name = NULL;
00297 if( ( name = getValByKey( _cond_input, BACKUP_RESC_NAME_KW ) ) == NULL &&
00298 ( name = getValByKey( _cond_input, DEST_RESC_NAME_KW ) ) == NULL &&
00299 ( name = getValByKey( _cond_input, DEF_RESC_NAME_KW ) ) == NULL &&
00300 ( name = getValByKey( _cond_input, RESC_NAME_KW ) ) == NULL ) {
00301
00302
00303 } else {
00304 cond_input_resc = name;
00305
00306 }
00307
00308 }
00309
00310
00311
00312 std::vector< std::string > resources;
00313 string_tokenize( _resc_list, "%", resources );
00314
00315
00316
00317
00318
00319
00320 std::string default_resc_name;
00321
00322 std::vector< std::string >::iterator itr = resources.begin();
00323 for( ; itr != resources.end(); ++itr ) {
00324
00325
00326 error grp_err = get_resc_grp_info( *itr, _resc_grp );
00327 if( grp_err.ok() ) {
00328 default_resc_name = *itr;
00329
00330
00331 break;
00332
00333 } else {
00334 std::stringstream msg;
00335 msg << "failed to get group info for [";
00336 msg << *itr;
00337 msg << "]";
00338 eirods::log( PASSMSG( msg.str(), grp_err ) );
00339
00340 }
00341
00342 }
00343
00344
00345
00346
00347 if( "preferred" == _option && !cond_input_resc.empty() ) {
00348
00349
00350 error grp_err = get_resc_grp_info( cond_input_resc, _resc_grp );
00351 if( grp_err.ok() ) {
00352 if( _resc_grp.rescInfo->rescStatus != INT_RESC_STATUS_DOWN ) {
00353
00354
00355 return SUCCESS();
00356 }
00357
00358 }
00359
00360 } else if( "forced" == _option && _comm->clientUser.authInfo.authFlag < LOCAL_PRIV_USER_AUTH ) {
00361
00362 return SUCCESS();
00363
00364 } else {
00365
00366
00367 error grp_err = get_resc_grp_info( cond_input_resc, _resc_grp );
00368 if( grp_err.ok() ) {
00369 if( _resc_grp.rescInfo->rescStatus != INT_RESC_STATUS_DOWN ) {
00370
00371
00372 return SUCCESS();
00373 } else {
00374
00375 }
00376
00377 } else {
00378
00379
00380 error grp_err = get_resc_grp_info( default_resc_name, _resc_grp );
00381 if( grp_err.ok() ) {
00382 return SUCCESS();
00383
00384 } else {
00385 std::stringstream msg;
00386 msg << "set_default_resource - failed to find default resource for list [";
00387 msg << _resc_list;
00388 msg << "] and option [";
00389 msg << _option;
00390 msg << "]";
00391 return PASSMSG( msg.str(), grp_err );
00392 }
00393
00394 }
00395
00396 }
00397
00398
00399
00400
00401 std::stringstream msg;
00402 msg << "should not reach here for list [";
00403 msg << _resc_list;
00404 msg << "] and option [";
00405 msg << _option;
00406 msg << "]";
00407 return ERROR( CAT_NO_ROWS_FOUND, msg.str() );
00408
00409 }
00410
00411
00412
00413
00414 error resolve_resource_name( std::string _resc_name, keyValPair_t* _cond_input, std::string& _out ) {
00415 if ( _resc_name.empty() ) {
00416 char* name = 0;
00417 name = getValByKey( _cond_input, BACKUP_RESC_NAME_KW );
00418 if( name ) {
00419 _out = std::string( name );
00420 return SUCCESS();
00421 }
00422
00423 name = getValByKey( _cond_input, DEST_RESC_NAME_KW );
00424 if( name ) {
00425 _out = std::string( name );
00426 return SUCCESS();
00427 }
00428
00429 name = getValByKey( _cond_input, DEF_RESC_NAME_KW );
00430 if( name ) {
00431 _out = std::string( name );
00432 return SUCCESS();
00433 }
00434
00435 return ERROR( INT_RESC_STATUS_DOWN, "failed to resolve resource name" );
00436
00437 } else {
00438 _out = _resc_name;
00439 return SUCCESS();
00440 }
00441
00442 }
00443
00444
00445
00446
00447 error get_host_status_by_host_info( rodsServerHost_t* _info ) {
00448
00449
00450 if( !_info ) {
00451 return ERROR( SYS_INVALID_INPUT_PARAM, "null pointer" );
00452 }
00453
00454
00455
00456 resource_ptr resc;
00457 error err = resc_mgr.resolve_from_property< rodsServerHost_t* >( RESOURCE_HOST, _info, resc );
00458 if( !err.ok() ) {
00459 return PASSMSG( "failed to resolve resource", err );
00460 }
00461
00462
00463
00464 int status = -1;
00465 err = resc->get_property< int >( RESOURCE_STATUS, status );
00466 if( !err.ok() ) {
00467 return PASSMSG( "failed to get resource property", err );
00468 }
00469
00470 return CODE( status );
00471
00472 }
00473
00474
00475
00476
00477 error get_resc_info( std::string _name, rescInfo_t& _info ) {
00478
00479 resource_ptr resc;
00480 error res_err = resc_mgr.resolve( _name, resc );
00481 if( res_err.ok() ) {
00482
00483
00484 int status = 0;
00485 get_resource_property< int >( _name, RESOURCE_STATUS, status );
00486 if( status == INT_RESC_STATUS_DOWN ) {
00487 return ERROR( SYS_RESC_IS_DOWN, "The Resource is Down" );
00488 }
00489
00490 error info_err = resource_to_resc_info( _info, resc );
00491 if( info_err.ok() ) {
00492 return SUCCESS();
00493
00494 } else {
00495 return PASS( info_err );
00496
00497 }
00498
00499 } else {
00500 return PASS( res_err );
00501
00502 }
00503
00504 }
00505
00506
00507
00508
00509 error get_resc_grp_info( std::string _name, rescGrpInfo_t& _info ) {
00510 if( _name.empty() ) {
00511 return ERROR( SYS_INVALID_INPUT_PARAM, "empty key" );
00512 }
00513
00514 resource_ptr resc;
00515 error res_err = resc_mgr.resolve( _name, resc );
00516 if( res_err.ok() ) {
00517
00518
00519 int status = 0;
00520 get_resource_property< int >( _name, RESOURCE_STATUS, status );
00521 if( status == INT_RESC_STATUS_DOWN ) {
00522 return ERROR( SYS_RESC_IS_DOWN, "The Resource is Down" );
00523 }
00524
00525 error info_err = resource_to_resc_grp_info( _info, resc );
00526 if( info_err.ok() ) {
00527 return SUCCESS();
00528
00529 } else {
00530 return PASS( info_err );
00531
00532 }
00533
00534 } else {
00535 return PASS( res_err );
00536
00537 }
00538
00539
00540 }
00541
00542 error get_host_for_hier_string(
00543 const std::string& _hier_str,
00544 int& _local_flag,
00545 rodsServerHost_t*& _server_host) {
00546
00547
00548
00549 if( _hier_str.empty() ) {
00550 return ERROR( SYS_INVALID_INPUT_PARAM, "hier string is empty" );
00551 }
00552
00553
00554
00555 std::string resc_name;
00556 hierarchy_parser parse;
00557 parse.set_string( _hier_str );
00558 parse.last_resc( resc_name );
00559
00560
00561
00562 if( resc_name.empty() ) {
00563 return ERROR( SYS_INVALID_INPUT_PARAM, "resc_name string is empty" );
00564 }
00565
00566
00567
00568 rodsServerHost_t* host = NULL;
00569 error ret = get_resource_property< rodsServerHost_t* >( resc_name, RESOURCE_HOST, host );
00570 if( !ret.ok() ) {
00571 std::stringstream msg;
00572 msg << "get_host_for_hier_string - failed to get host property for [";
00573 msg << resc_name;
00574 msg << "]";
00575 return PASSMSG( msg.str(), ret );
00576 }
00577
00578
00579 if(host == NULL) {
00580 std::stringstream msg;
00581 msg << __FUNCTION__;
00582 msg << " - Host from hierarchy string: \"";
00583 msg << _hier_str;
00584 msg << "\" is NULL";
00585 return ERROR(EIRODS_INVALID_LOCATION, msg.str());
00586 }
00587
00588
00589
00590 _server_host = host;
00591 _local_flag = host->localFlag;
00592
00593 return SUCCESS();
00594
00595 }
00596
00597
00598
00599 error get_loc_for_hier_string(
00600 const std::string& _hier,
00601 std::string& _loc ) {
00602
00603
00604 hierarchy_parser parser;
00605 parser.set_string( _hier );
00606
00607 std::string last_resc;
00608 parser.last_resc( last_resc );
00609
00610 std::string location;
00611 error ret = get_resource_property< std::string >( last_resc, RESOURCE_LOCATION, location );
00612 if( !ret.ok() ) {
00613 location = "";
00614 return PASSMSG( "get_loc_for_hier_string - failed in get_resource_property", ret );
00615 }
00616
00617
00618
00619 _loc = location;
00620
00621 return SUCCESS();
00622
00623 }
00624
00625
00626 error get_vault_path_for_hier_string(
00627 const std::string& _hier_string,
00628 std::string& _rtn_vault_path)
00629 {
00630 error result = SUCCESS();
00631 error ret;
00632 hierarchy_parser parser;
00633 ret = parser.set_string(_hier_string);
00634 if(!ret.ok()) {
00635 std::stringstream msg;
00636 msg << __FUNCTION__;
00637 msg << " - Failed to parse the hierarchy string \"" << _hier_string << "\"";
00638 result = PASSMSG(msg.str(), ret);
00639 } else {
00640 std::string last_resc;
00641 ret = parser.last_resc(last_resc);
00642 if(!ret.ok()) {
00643 std::stringstream msg;
00644 msg << __FUNCTION__;
00645 msg << " - Failed to get the last resource in the hierarchy: \"" << _hier_string << "\"";
00646 result = PASSMSG(msg.str(), ret);
00647 } else {
00648 ret = get_resource_property<std::string>(last_resc, RESOURCE_PATH, _rtn_vault_path);
00649 if(!ret.ok()) {
00650 std::stringstream msg;
00651 msg << __FUNCTION__;
00652 msg << " - Failed to get the fault path property from the resource: \"" << last_resc << "\"";
00653 result = PASSMSG(msg.str(), ret);
00654 }
00655 }
00656 }
00657
00658 return result;
00659 }
00660
00661 };
00662
00663
00664
00665
00666
00667
00668
00669
00670
00671
00672
00673
00674
00675