35 #include <sys/types.h> 47 using std::ostringstream;
50 #include "BESCatalogUtils.h" 51 #include "BESCatalogList.h" 52 #include "TheBESKeys.h" 53 #include "BESInternalError.h" 54 #include "BESSyntaxUserError.h" 55 #include "BESNotFoundError.h" 60 #include "BESContainerStorageList.h" 61 #include "BESContainerStorage.h" 62 #include "BESCatalogEntry.h" 64 map<string, BESCatalogUtils *> BESCatalogUtils::_instances;
66 BESCatalogUtils::BESCatalogUtils(
const string &n) :
67 _name(n), _follow_syms(false) {
68 string key =
"BES.Catalog." + n +
".RootDirectory";
71 if (!found || _root_dir ==
"") {
72 string s = key +
" not defined in BES configuration file";
75 DIR *dip = opendir(_root_dir.c_str());
77 string serr =
"BESCatalogDirectory - root directory " + _root_dir
84 key = (string)
"BES.Catalog." + n +
".Exclude";
87 vector<string>::iterator ei = vals.begin();
88 vector<string>::iterator ee = vals.end();
89 for (; ei != ee; ei++) {
91 if (!e_str.empty() && e_str !=
";")
95 key = (string)
"BES.Catalog." + n +
".Include";
98 vector<string>::iterator ii = vals.begin();
99 vector<string>::iterator ie = vals.end();
100 for (; ii != ie; ii++) {
101 string i_str = (*ii);
102 if (!i_str.empty() && i_str !=
";")
106 key =
"BES.Catalog." + n +
".TypeMatch";
107 list<string> match_list;
110 if (!found || vals.size() == 0) {
111 string s = key +
" not defined in key file";
114 vector<string>::iterator vi = vals.begin();
115 vector<string>::iterator ve = vals.end();
116 for (; vi != ve; vi++) {
120 list<string>::iterator mli = match_list.begin();
121 list<string>::iterator mle = match_list.end();
122 for (; mli != mle; mli++) {
123 if (!((*mli).empty()) && *(mli) !=
";") {
126 if (amatch.size() != 2) {
127 string s = (string)
"Catalog type match malformed, " 128 +
"looking for type:regexp;[type:regexp;]";
131 list<string>::iterator ami = amatch.begin();
133 newval.type = (*ami);
136 _match_list.push_back(newval);
140 key = (string)
"BES.Catalog." + n +
".FollowSymLinks";
144 if (s_str ==
"yes" || s_str ==
"on" || s_str ==
"true") {
149 bool BESCatalogUtils::include(
const string &inQuestion)
const {
150 bool toInclude =
false;
155 if (_include.size() == 0) {
158 list<string>::const_iterator i_iter = _include.begin();
159 list<string>::const_iterator i_end = _include.end();
160 for (; i_iter != i_end; i_iter++) {
161 string reg = *i_iter;
167 if (reg_expr.match(inQuestion.c_str(), inQuestion.length())
168 == static_cast<int> (inQuestion.length())) {
173 (string)
"Unable to get catalog information, " 174 +
"malformed Catalog Include parameter " 175 +
"in bes configuration file around " + reg
183 if (toInclude ==
true) {
184 if (exclude(inQuestion)) {
192 bool BESCatalogUtils::exclude(
const string &inQuestion)
const {
193 list<string>::const_iterator e_iter = _exclude.begin();
194 list<string>::const_iterator e_end = _exclude.end();
195 for (; e_iter != e_end; e_iter++) {
196 string reg = *e_iter;
200 if (reg_expr.match(inQuestion.c_str(), inQuestion.length())
201 == static_cast<int> (inQuestion.length())) {
205 string serr = (string)
"Unable to get catalog information, " 206 +
"malformed Catalog Exclude parameter " 207 +
"in bes configuration file around " + reg +
": " 216 BESCatalogUtils::match_citer BESCatalogUtils::match_list_begin()
const {
217 return _match_list.begin();
220 BESCatalogUtils::match_citer BESCatalogUtils::match_list_end()
const {
221 return _match_list.end();
224 unsigned int BESCatalogUtils::get_entries(DIR *dip,
const string &fullnode,
227 unsigned int cnt = 0;
229 int statret = stat(fullnode.c_str(), &cbuf);
230 int my_errno = errno;
236 while ((dit = readdir(dip)) != NULL) {
237 string dirEntry = dit->d_name;
238 if (dirEntry !=
"." && dirEntry !=
"..") {
239 string fullPath = fullnode +
"/" + dirEntry;
246 bool continue_checking =
true;
247 if (follow_sym_links() ==
false) {
249 int lstatret = lstat( fullPath.c_str(), &lbuf );
251 (void) lstat(fullPath.c_str(), &lbuf);
252 if (S_ISLNK( lbuf.st_mode )) {
253 continue_checking =
false;
257 if (continue_checking) {
262 statret = stat(fullPath.c_str(), &buf);
263 if (statret == 0 && S_ISDIR( buf.st_mode )) {
264 if (exclude(dirEntry) ==
false) {
266 dirEntry, entry->get_catalog());
268 bes_get_stat_info(curr_entry, buf);
270 entry->add_entry(curr_entry);
276 ".blank", entry->get_catalog());
277 curr_entry->add_entry(blank_entry);
279 }
else if (statret == 0 && S_ISREG( buf.st_mode )) {
280 if (!dirs_only && include(dirEntry)) {
282 dirEntry, entry->get_catalog());
283 bes_get_stat_info(curr_entry, buf);
285 list<string> services;
286 isData(fullPath, _name, services);
287 curr_entry->set_service_list(services);
289 bes_get_stat_info(curr_entry, buf);
291 entry->add_entry(curr_entry);
299 if (my_errno == ENOENT) {
300 string error =
"Node " + use_node +
" does not exist";
301 char *s_err = strerror(my_errno);
309 string error =
"Access denied for node " + use_node;
310 char *s_err = strerror(my_errno);
312 error = error + s_err;
324 map<string, string> props;
325 if (entry->get_catalog() == defcatname) {
326 props[
"name"] = entry->get_name();
328 string name = entry->get_catalog() +
"/";
329 if (entry->get_name() !=
"/") {
330 name = name + entry->get_name();
332 props[
"name"] = name;
334 props[
"catalog"] = entry->get_catalog();
335 props[
"size"] = entry->get_size();
336 props[
"lastModified"] = entry->get_mod_date() +
"T" + entry->get_mod_time();
337 if (entry->is_collection()) {
338 props[
"node"] =
"true";
340 strm << entry->get_count();
341 props[
"count"] = strm.str();
343 props[
"node"] =
"false";
345 info->begin_tag(
"dataset", &props);
347 list<string> services = entry->get_service_list();
348 if (services.size()) {
349 list<string>::const_iterator si = services.begin();
350 list<string>::const_iterator se = services.end();
351 for (; si != se; si++) {
352 info->add_tag(
"serviceRef", (*si));
358 const string &fullnode) {
360 int statret = stat(fullnode.c_str(), &cbuf);
362 bes_get_stat_info(entry, cbuf);
368 off_t sz = buf.st_size;
373 time_t mod = buf.st_mtime;
374 struct tm *stm = gmtime(&mod);
376 strftime(mdate, 64,
"%Y-%m-%d", stm);
378 strftime(mtime, 64,
"%T", stm);
382 entry->set_mod_date(sdt.str());
386 entry->set_mod_time(stt.str());
389 bool BESCatalogUtils::isData(
const string &inQuestion,
const string &catalog,
390 list<string> &services) {
396 return store->
isData(inQuestion, services);
400 strm << BESIndent::LMarg <<
"BESCatalogUtils::dump - (" << (
void *)
this 404 strm << BESIndent::LMarg <<
"root directory: " << _root_dir << endl;
406 if (_include.size()) {
407 strm << BESIndent::LMarg <<
"include list:" << endl;
409 list<string>::const_iterator i_iter = _include.begin();
410 list<string>::const_iterator i_end = _include.end();
411 for (; i_iter != i_end; i_iter++) {
412 if (!(*i_iter).empty()) {
413 strm << BESIndent::LMarg << *i_iter << endl;
416 BESIndent::UnIndent();
418 strm << BESIndent::LMarg <<
"include list: empty" << endl;
421 if (_exclude.size()) {
422 strm << BESIndent::LMarg <<
"exclude list:" << endl;
424 list<string>::const_iterator e_iter = _exclude.begin();
425 list<string>::const_iterator e_end = _exclude.end();
426 for (; e_iter != e_end; e_iter++) {
427 if (!(*e_iter).empty()) {
428 strm << BESIndent::LMarg << *e_iter << endl;
431 BESIndent::UnIndent();
433 strm << BESIndent::LMarg <<
"exclude list: empty" << endl;
436 if (_match_list.size()) {
437 strm << BESIndent::LMarg <<
"type matches:" << endl;
439 BESCatalogUtils::match_citer i = _match_list.begin();
440 BESCatalogUtils::match_citer ie = _match_list.end();
441 for (; i != ie; i++) {
443 strm << BESIndent::LMarg << match.type <<
" : " << match.reg
446 BESIndent::UnIndent();
448 strm << BESIndent::LMarg <<
" type matches: empty" << endl;
452 strm << BESIndent::LMarg <<
" follow symbolic links: on" << endl;
454 strm << BESIndent::LMarg <<
" follow symbolic links: off" << endl;
457 BESIndent::UnIndent();
461 BESCatalogUtils::Utils(
const string &cat_name) {
465 BESCatalogUtils::_instances[cat_name] = utils;
471 void BESCatalogUtils::delete_all_catalogs()
473 map<string, BESCatalogUtils*>::iterator i = BESCatalogUtils::_instances.begin();
474 map<string, BESCatalogUtils*>::iterator e = BESCatalogUtils::_instances.end();
476 delete (*i++).second;
error thrown if the resource requested cannot be found
provides persistent storage for data storage information represented by a container.
exception thrown if inernal error encountered
virtual BESContainerStorage * find_persistence(const string &persist_name)
find the persistence store with the given name
static string lowercase(const string &s)
virtual std::string get_message()
get the error message for this exception
void get_value(const string &s, string &val, bool &found)
Retrieve the value of a given key, if set.
error thrown if there is a user syntax error in the request or any other user error
virtual bool isData(const string &inQuestion, list< string > &provides)=0
determine if the given container is data and what services are available for it
informational response object
Abstract exception class for the BES with basic string message.
static TheBESKeys * TheKeys()
virtual void dump(ostream &strm) const
dump the contents of this object to the specified ostream
static void explode(char delim, const string &str, list< string > &values)
void get_values(const string &s, vector< string > &vals, bool &found)
Retrieve the values of a given key, if set.
static BESCatalogList * TheCatalogList()
returns the singleton BESCatalogList instance. The pthreads library insures that only one instance ca...