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
00242
00243
00244 if( !_grp_info.rescInfo ) {
00245 _grp_info.rescInfo = new rescInfo_t;
00246 } else {
00247
00248 }
00249
00250
00251
00252 error err = resource_to_resc_info( *_grp_info.rescInfo, _resc );
00253 if( !err.ok() ) {
00254 return PASS( err );
00255
00256 }
00257
00258
00259
00260
00261 rstrcpy( _grp_info.rescGroupName, _grp_info.rescInfo->rescName, NAME_LEN );
00262
00263 return SUCCESS();
00264
00265 }
00266
00267
00268
00269
00270
00271
00272
00273 error set_default_resource( rsComm_t* _comm, std::string _resc_list,
00274 std::string _option, keyValPair_t* _cond_input,
00275 rescGrpInfo_t& _resc_grp ) {
00276
00277
00278 if( _resc_list.empty() && NULL == _cond_input ) {
00279 return ERROR( USER_NO_RESC_INPUT_ERR, "no user input" );
00280 }
00281
00282
00283
00284 std::string cond_input_resc;
00285
00286
00287
00288
00289 if( "null" != _resc_list &&
00290 "forced" == _option &&
00291 _comm->proxyUser.authInfo.authFlag < LOCAL_PRIV_USER_AUTH ) {
00292 _cond_input = NULL;
00293 } else if( _cond_input ) {
00294 char* name = NULL;
00295 if( ( name = getValByKey( _cond_input, BACKUP_RESC_NAME_KW ) ) == NULL &&
00296 ( name = getValByKey( _cond_input, DEST_RESC_NAME_KW ) ) == NULL &&
00297 ( name = getValByKey( _cond_input, DEF_RESC_NAME_KW ) ) == NULL &&
00298 ( name = getValByKey( _cond_input, RESC_NAME_KW ) ) == NULL ) {
00299
00300
00301 } else {
00302 cond_input_resc = name;
00303
00304 }
00305
00306 }
00307
00308
00309
00310 std::vector< std::string > resources;
00311 string_tokenize( _resc_list, "%", resources );
00312
00313
00314
00315
00316
00317
00318 std::string default_resc_name;
00319
00320 std::vector< std::string >::iterator itr = resources.begin();
00321 for( ; itr != resources.end(); ++itr ) {
00322
00323
00324 error grp_err = get_resc_grp_info( *itr, _resc_grp );
00325 if( grp_err.ok() ) {
00326 default_resc_name = *itr;
00327
00328
00329 break;
00330
00331 } else {
00332 std::stringstream msg;
00333 msg << "failed to get group info for [";
00334 msg << *itr;
00335 msg << "]";
00336 eirods::log( PASSMSG( msg.str(), grp_err ) );
00337
00338 }
00339
00340 }
00341
00342
00343
00344
00345 if( "preferred" == _option && !cond_input_resc.empty() ) {
00346
00347
00348 error grp_err = get_resc_grp_info( cond_input_resc, _resc_grp );
00349 if( grp_err.ok() ) {
00350 if( _resc_grp.rescInfo->rescStatus != INT_RESC_STATUS_DOWN ) {
00351
00352
00353 return SUCCESS();
00354 }
00355
00356 }
00357
00358 } else if( "forced" == _option && _comm->clientUser.authInfo.authFlag < LOCAL_PRIV_USER_AUTH ) {
00359
00360 return SUCCESS();
00361
00362 } else {
00363
00364
00365 error grp_err = get_resc_grp_info( cond_input_resc, _resc_grp );
00366 if( grp_err.ok() ) {
00367 if( _resc_grp.rescInfo->rescStatus != INT_RESC_STATUS_DOWN ) {
00368
00369
00370 return SUCCESS();
00371 } else {
00372
00373 }
00374
00375 } else {
00376
00377
00378 error grp_err = get_resc_grp_info( default_resc_name, _resc_grp );
00379 if( grp_err.ok() ) {
00380 return SUCCESS();
00381
00382 } else {
00383 std::stringstream msg;
00384 msg << "set_default_resource - failed to find default resource for list [";
00385 msg << _resc_list;
00386 msg << "] and option [";
00387 msg << _option;
00388 msg << "]";
00389 return PASSMSG( msg.str(), grp_err );
00390 }
00391
00392 }
00393
00394 }
00395
00396
00397
00398
00399 std::stringstream msg;
00400 msg << "should not reach here for list [";
00401 msg << _resc_list;
00402 msg << "] and option [";
00403 msg << _option;
00404 msg << "]";
00405 return ERROR( CAT_NO_ROWS_FOUND, msg.str() );
00406
00407 }
00408
00409
00410
00411
00412 error resolve_resource_name( std::string _resc_name, keyValPair_t* _cond_input, std::string& _out ) {
00413 if ( _resc_name.empty() ) {
00414 char* name = 0;
00415 name = getValByKey( _cond_input, BACKUP_RESC_NAME_KW );
00416 if( name ) {
00417 _out = std::string( name );
00418 return SUCCESS();
00419 }
00420
00421 name = getValByKey( _cond_input, DEST_RESC_NAME_KW );
00422 if( name ) {
00423 _out = std::string( name );
00424 return SUCCESS();
00425 }
00426
00427 name = getValByKey( _cond_input, DEF_RESC_NAME_KW );
00428 if( name ) {
00429 _out = std::string( name );
00430 return SUCCESS();
00431 }
00432
00433 return ERROR( INT_RESC_STATUS_DOWN, "failed to resolve resource name" );
00434
00435 } else {
00436 _out = _resc_name;
00437 return SUCCESS();
00438 }
00439
00440 }
00441
00442
00443
00444
00445 error get_host_status_by_host_info( rodsServerHost_t* _info ) {
00446
00447
00448 if( !_info ) {
00449 return ERROR( SYS_INVALID_INPUT_PARAM, "null pointer" );
00450 }
00451
00452
00453
00454 resource_ptr resc;
00455 error err = resc_mgr.resolve_from_property< rodsServerHost_t* >( RESOURCE_HOST, _info, resc );
00456 if( !err.ok() ) {
00457 return PASSMSG( "failed to resolve resource", err );
00458 }
00459
00460
00461
00462 int status = -1;
00463 err = resc->get_property< int >( RESOURCE_STATUS, status );
00464 if( !err.ok() ) {
00465 return PASSMSG( "failed to get resource property", err );
00466 }
00467
00468 return CODE( status );
00469
00470 }
00471
00472
00473
00474
00475 error get_resc_info( std::string _name, rescInfo_t& _info ) {
00476
00477 resource_ptr resc;
00478 error res_err = resc_mgr.resolve( _name, resc );
00479 if( res_err.ok() ) {
00480
00481
00482 int status = 0;
00483 get_resource_property< int >( _name, RESOURCE_STATUS, status );
00484 if( status == INT_RESC_STATUS_DOWN ) {
00485 return ERROR( SYS_RESC_IS_DOWN, "The Resource is Down" );
00486 }
00487
00488 error info_err = resource_to_resc_info( _info, resc );
00489 if( info_err.ok() ) {
00490 return SUCCESS();
00491
00492 } else {
00493 return PASS( info_err );
00494
00495 }
00496
00497 } else {
00498 return PASS( res_err );
00499
00500 }
00501
00502 }
00503
00504
00505
00506
00507 error get_resc_grp_info( std::string _name, rescGrpInfo_t& _info ) {
00508 if( _name.empty() ) {
00509 return ERROR( SYS_INVALID_INPUT_PARAM, "empty key" );
00510 }
00511
00512 resource_ptr resc;
00513 error res_err = resc_mgr.resolve( _name, resc );
00514 if( res_err.ok() ) {
00515
00516
00517 int status = 0;
00518 get_resource_property< int >( _name, RESOURCE_STATUS, status );
00519 if( status == INT_RESC_STATUS_DOWN ) {
00520 return ERROR( SYS_RESC_IS_DOWN, "The Resource is Down" );
00521 }
00522
00523 error info_err = resource_to_resc_grp_info( _info, resc );
00524 if( info_err.ok() ) {
00525 return SUCCESS();
00526
00527 } else {
00528 return PASS( info_err );
00529
00530 }
00531
00532 } else {
00533 return PASS( res_err );
00534
00535 }
00536
00537
00538 }
00539
00540 error get_host_for_hier_string(
00541 const std::string& _hier_str,
00542 int& _local_flag,
00543 rodsServerHost_t*& _server_host) {
00544
00545
00546
00547 if( _hier_str.empty() ) {
00548 return ERROR( SYS_INVALID_INPUT_PARAM, "hier string is empty" );
00549 }
00550
00551
00552
00553 std::string resc_name;
00554 hierarchy_parser parse;
00555 parse.set_string( _hier_str );
00556 parse.last_resc( resc_name );
00557
00558
00559
00560 if( resc_name.empty() ) {
00561 return ERROR( SYS_INVALID_INPUT_PARAM, "resc_name string is empty" );
00562 }
00563
00564
00565
00566 rodsServerHost_t* host = NULL;
00567 error ret = get_resource_property< rodsServerHost_t* >( resc_name, RESOURCE_HOST, host );
00568 if( !ret.ok() ) {
00569 std::stringstream msg;
00570 msg << "get_host_for_hier_string - failed to get host property for [";
00571 msg << resc_name;
00572 msg << "]";
00573 return PASSMSG( msg.str(), ret );
00574 }
00575
00576
00577 if(host == NULL) {
00578 std::stringstream msg;
00579 msg << __FUNCTION__;
00580 msg << " - Host from hierarchy string: \"";
00581 msg << _hier_str;
00582 msg << "\" is NULL";
00583 return ERROR(EIRODS_INVALID_LOCATION, msg.str());
00584 }
00585
00586
00587
00588 _server_host = host;
00589 _local_flag = host->localFlag;
00590
00591 return SUCCESS();
00592
00593 }
00594
00595
00596
00597 error get_loc_for_hier_string(
00598 const std::string& _hier,
00599 std::string& _loc ) {
00600
00601
00602 hierarchy_parser parser;
00603 parser.set_string( _hier );
00604
00605 std::string last_resc;
00606 parser.last_resc( last_resc );
00607
00608 std::string location;
00609 error ret = get_resource_property< std::string >( last_resc, RESOURCE_LOCATION, location );
00610 if( !ret.ok() ) {
00611 location = "";
00612 return PASSMSG( "get_loc_for_hier_string - failed in get_resource_property", ret );
00613 }
00614
00615
00616
00617 _loc = location;
00618
00619 return SUCCESS();
00620
00621 }
00622
00623
00624 error get_vault_path_for_hier_string(
00625 const std::string& _hier_string,
00626 std::string& _rtn_vault_path)
00627 {
00628 error result = SUCCESS();
00629 error ret;
00630 hierarchy_parser parser;
00631 ret = parser.set_string(_hier_string);
00632 if(!ret.ok()) {
00633 std::stringstream msg;
00634 msg << __FUNCTION__;
00635 msg << " - Failed to parse the hierarchy string \"" << _hier_string << "\"";
00636 result = PASSMSG(msg.str(), ret);
00637 } else {
00638 std::string last_resc;
00639 ret = parser.last_resc(last_resc);
00640 if(!ret.ok()) {
00641 std::stringstream msg;
00642 msg << __FUNCTION__;
00643 msg << " - Failed to get the last resource in the hierarchy: \"" << _hier_string << "\"";
00644 result = PASSMSG(msg.str(), ret);
00645 } else {
00646 ret = get_resource_property<std::string>(last_resc, RESOURCE_PATH, _rtn_vault_path);
00647 if(!ret.ok()) {
00648 std::stringstream msg;
00649 msg << __FUNCTION__;
00650 msg << " - Failed to get the fault path property from the resource: \"" << last_resc << "\"";
00651 result = PASSMSG(msg.str(), ret);
00652 }
00653 }
00654 }
00655
00656 return result;
00657 }
00658
00659 };
00660
00661
00662
00663
00664
00665
00666
00667
00668
00669
00670
00671
00672
00673