28 #include <sys/types.h>
39 #include "ShowPathInfoResponseHandler.h"
43 #include "BESInfoList.h"
46 #include "BESRequestHandlerList.h"
47 #include "BESRequestHandler.h"
49 #include "BESDapNames.h"
50 #include "BESDataNames.h"
51 #include "BESCatalogList.h"
52 #include "BESCatalog.h"
53 #include "BESCatalogEntry.h"
54 #include "BESCatalogUtils.h"
55 #include "BESSyntaxUserError.h"
56 #include "BESForbiddenError.h"
57 #include "BESNotFoundError.h"
58 #include "BESStopWatch.h"
60 #define PATH_INFO_RESPONSE "PathInfo"
62 #define VALID_PATH "validPath"
63 #define REMAINDER "remainder"
64 #define IS_DATA "isData"
65 #define IS_FILE "isFile"
66 #define IS_DIR "isDir"
67 #define IS_ACCESSIBLE "access"
69 #define LMT "lastModified"
71 #define SPI_DEBUG_KEY "show-path-info"
72 #define SHOW_PATH_INFO_RESPONSE_STR "showPathInfo"
74 ShowPathInfoResponseHandler::ShowPathInfoResponseHandler(
const string &name) :
79 ShowPathInfoResponseHandler::~ShowPathInfoResponseHandler()
97 if (BESISDEBUG(TIMING_LOG)) sw.
start(
"ShowPathInfoResponseHandler::execute", dhi.
data[REQUEST_ID]);
99 BESDEBUG(SPI_DEBUG_KEY,
100 "ShowPathInfoResponseHandler::execute() - BEGIN ############################################################## BEGIN" << endl);
102 BESInfo *info = BESInfoList::TheList()->build_info();
103 d_response_object = info;
105 string container = dhi.
data[CONTAINER];
113 throw BESInternalError(
"Not able to find the default catalog.", __FILE__, __LINE__);
116 string::size_type notslash = container.find_first_not_of(
"/", 0);
117 if (notslash != string::npos) {
118 container = container.substr(notslash);
123 string::size_type slash = container.find_first_of(
"/", 0);
124 if (slash != string::npos) {
125 catname = container.substr(0, slash);
135 if (slash != string::npos) {
136 container = container.substr(slash + 1);
139 notslash = container.find_first_not_of(
"/", 0);
140 if (notslash != string::npos) {
141 container = container.substr(notslash);
149 if (container.empty()) container =
"/";
151 if (container[0] !=
'/') container =
"/" + container;
153 BESDEBUG(SPI_DEBUG_KEY,
"ShowPathInfoResponseHandler::execute() - container: " << container << endl);
158 map<string, string> pathInfoAttrs;
159 pathInfoAttrs[PATH] = container;
161 info->begin_tag(PATH_INFO_RESPONSE, &pathInfoAttrs);
163 string validPath, remainder;
164 bool isFile, isDir, canRead;
165 long long size, time;
169 eval_resource_path(container, utils->
get_root_dir(), utils->follow_sym_links(), validPath, isFile, isDir, size,
170 time, canRead, remainder);
177 if (validPath.length() != 0) {
184 string err = (string)
"Failed to find the validPath node " + validPath
185 +
" this should not be possible. Some thing BAD is happening.";
190 list<string> services = entry->get_service_list();
193 if (services.size()) {
194 list<string>::const_iterator si = services.begin();
195 list<string>::const_iterator se = services.end();
196 for (; si != se; si++) {
197 if ((*si) == OPENDAP_SERVICE) isData =
true;
202 map<string, string> validPathAttrs;
203 validPathAttrs[IS_DATA] = isData ?
"true" :
"false";
204 validPathAttrs[IS_FILE] = isFile ?
"true" :
"false";
205 validPathAttrs[IS_DIR] = isDir ?
"true" :
"false";
206 validPathAttrs[IS_ACCESSIBLE] = canRead ?
"true" :
"false";
209 std::ostringstream os_size;
211 validPathAttrs[SIZE] = os_size.str();
214 std::ostringstream os_time;
216 validPathAttrs[LMT] = os_time.str();
218 info->add_tag(VALID_PATH, validPath, &validPathAttrs);
219 info->add_tag(REMAINDER, remainder);
221 info->end_tag(PATH_INFO_RESPONSE);
224 info->end_response();
226 BESDEBUG(SPI_DEBUG_KEY,
"ShowPathInfoResponseHandler::execute() - END" << endl);
242 if (d_response_object) {
243 BESInfo *info = dynamic_cast<BESInfo *>(d_response_object);
257 strm << BESIndent::LMarg <<
"ShowPathInfoResponseHandler::dump - (" << (
void *)
this <<
")" << std::endl;
260 BESIndent::UnIndent();
264 ShowPathInfoResponseHandler::ShowPathInfoResponseBuilder(
const string &name)
272 void ShowPathInfoResponseHandler::eval_resource_path(
const string &resource_path,
const string &catalog_root,
273 const bool follow_sym_links,
string &validPath,
bool &isFile,
bool &isDir,
long long &size,
274 long long &lastModifiedTime,
bool &canRead,
string &remainder)
277 BESDEBUG(SPI_DEBUG_KEY,
278 "ShowPathInfoResponseHandler::"<<__func__ <<
"() - " <<
"CatalogRoot: "<< catalog_root << endl);
280 BESDEBUG(SPI_DEBUG_KEY,
281 "ShowPathInfoResponseHandler::"<<__func__ <<
"() - " <<
"resourceID: "<< resource_path << endl);
286 lastModifiedTime = -1;
289 string rem = resource_path;
295 int (*ye_old_stat_function)(
const char *pathname,
struct stat *buf);
296 if (follow_sym_links) {
297 BESDEBUG(SPI_DEBUG_KEY,
298 "ShowPathInfoResponseHandler::"<<__func__ <<
"() - " <<
"Using 'stat' function (follow_sym_links = true)" << endl);
299 ye_old_stat_function = &stat;
302 BESDEBUG(SPI_DEBUG_KEY,
303 "ShowPathInfoResponseHandler::"<<__func__ <<
"() - " <<
"Using 'lstat' function (follow_sym_links = false)" << endl);
304 ye_old_stat_function = &lstat;
309 if (resource_path ==
"") {
310 BESDEBUG(SPI_DEBUG_KEY,
"ShowPathInfoResponseHandler::"<<__func__ <<
"() - The resourceID is empty" << endl);
316 string::size_type dotdot = resource_path.find(
"..");
317 if (dotdot != string::npos) {
318 BESDEBUG(SPI_DEBUG_KEY,
319 "ShowPathInfoResponseHandler::"<<__func__ <<
"() - " <<
" ERROR: The resourceID '" << resource_path <<
"' contains the substring '..' This is Forbidden." << endl);
320 string s = (string)
"Invalid node name '" + resource_path +
"' ACCESS IS FORBIDDEN";
331 string fullpath = catalog_root;
340 size_t slash = rem.find(
'/');
341 if (slash == string::npos) {
342 BESDEBUG(SPI_DEBUG_KEY,
343 "ShowPathInfoResponseHandler::"<<__func__ <<
"() - " <<
"Checking final path component: " << rem << endl);
352 rem = rem.substr(slash + 1, rem.length() - slash);
355 BESDEBUG(SPI_DEBUG_KEY,
356 "ShowPathInfoResponseHandler::"<<__func__ <<
"() - " <<
"validPath: "<< validPath << endl);
357 BESDEBUG(SPI_DEBUG_KEY,
358 "ShowPathInfoResponseHandler::"<<__func__ <<
"() - " <<
"checking: "<< checking << endl);
359 BESDEBUG(SPI_DEBUG_KEY,
360 "ShowPathInfoResponseHandler::"<<__func__ <<
"() - " <<
"fullpath: "<< fullpath << endl);
362 BESDEBUG(SPI_DEBUG_KEY,
"ShowPathInfoResponseHandler::"<<__func__ <<
"() - " <<
"rem: "<< rem << endl);
364 BESDEBUG(SPI_DEBUG_KEY,
365 "ShowPathInfoResponseHandler::"<<__func__ <<
"() - " <<
"remainder: "<< remainder << endl);
368 int statret = ye_old_stat_function(fullpath.c_str(), &sb);
372 validPath = checking;
379 char *s_err = strerror(errsv);
380 string error =
"Unable to access node " + checking +
": ";
382 error = error + s_err;
385 error = error +
"unknown access error";
387 BESDEBUG(SPI_DEBUG_KEY,
388 "ShowPathInfoResponseHandler::"<<__func__ <<
"() - " <<
"error: "<< error <<
" errno: " << errno << endl);
390 BESDEBUG(SPI_DEBUG_KEY,
391 "ShowPathInfoResponseHandler::"<<__func__ <<
"() - " <<
"remainder: '" << remainder <<
"'" << endl);
395 if (errsv != ENOENT && errsv != ENOTDIR) {
400 size_t s_loc = remainder.find(
'/');
401 if (s_loc == string::npos) {
403 string basename = remainder;
404 bool moreDots =
true;
407 size_t d_loc = basename.find_last_of(
".");
408 if (d_loc != string::npos) {
409 basename = basename.substr(0, d_loc);
410 BESDEBUG(SPI_DEBUG_KEY,
411 "ShowPathInfoResponseHandler::" << __func__ <<
"() - basename: "<< basename << endl);
413 string candidate_remainder = remainder.substr(basename.length());
414 BESDEBUG(SPI_DEBUG_KEY,
415 "ShowPathInfoResponseHandler::" << __func__ <<
"() - candidate_remainder: "<< candidate_remainder << endl);
418 BESDEBUG(SPI_DEBUG_KEY,
419 "ShowPathInfoResponseHandler::" << __func__ <<
"() - candidate_path: "<< candidate_path << endl);
422 BESDEBUG(SPI_DEBUG_KEY,
423 "ShowPathInfoResponseHandler::" << __func__ <<
"() - full_candidate_path: "<< full_candidate_path << endl);
426 int statret1 = ye_old_stat_function(full_candidate_path.c_str(), &sb1);
427 if (statret1 != -1) {
428 validPath = candidate_path;
429 remainder = candidate_remainder;
434 BESDEBUG(SPI_DEBUG_KEY,
435 "ShowPathInfoResponseHandler::"<<__func__ <<
"() - " <<
"No dots in remainder: "<< remainder << endl);
441 BESDEBUG(SPI_DEBUG_KEY,
442 "ShowPathInfoResponseHandler::"<<__func__ <<
"() - " <<
"Remainder has slash pollution: "<< remainder << endl);
448 statret = ye_old_stat_function(fullpath.c_str(), &sb);
449 if (S_ISREG(sb.st_mode)) {
450 BESDEBUG(SPI_DEBUG_KEY,
451 "ShowPathInfoResponseHandler::"<<__func__ <<
"() - " <<
"'"<< fullpath <<
"' Is regular file." << endl);
455 else if (S_ISDIR(sb.st_mode)) {
456 BESDEBUG(SPI_DEBUG_KEY,
457 "ShowPathInfoResponseHandler::"<<__func__ <<
"() - " <<
"'"<< fullpath <<
"' Is directory." << endl);
461 else if (S_ISLNK(sb.st_mode)) {
462 BESDEBUG(SPI_DEBUG_KEY,
463 "ShowPathInfoResponseHandler::"<<__func__ <<
"() - " <<
"'"<< fullpath <<
"' Is symbolic Link." << endl);
464 string error =
"Service not configured to traverse symbolic links as embodied by the node '" + checking
465 +
"' ACCESS IS FORBIDDEN";
472 std::ifstream ifile(fullpath.c_str());
473 canRead = ifile.good();
486 #if !defined(_POSIX_C_SOURCE) || defined(_DARWIN_C_SOURCE)
488 lastModifiedTime = (sb.st_mtimespec.tv_sec * 1000) + (sb.st_mtimespec.tv_nsec / 1000000);
490 lastModifiedTime = sb.st_mtime;
494 BESDEBUG(SPI_DEBUG_KEY,
"ShowPathInfoResponseHandler::" << __func__ <<
"() - fullpath: " << fullpath << endl);
495 BESDEBUG(SPI_DEBUG_KEY,
"ShowPathInfoResponseHandler::" << __func__ <<
"() - validPath: " << validPath << endl);
496 BESDEBUG(SPI_DEBUG_KEY,
"ShowPathInfoResponseHandler::" << __func__ <<
"() - remainder: " << remainder << endl);
497 BESDEBUG(SPI_DEBUG_KEY,
"ShowPathInfoResponseHandler::" << __func__ <<
"() - rem: " << rem << endl);
498 BESDEBUG(SPI_DEBUG_KEY,
499 "ShowPathInfoResponseHandler::" << __func__ <<
"() - isFile: " << (isFile?
"true":
"false") << endl);
500 BESDEBUG(SPI_DEBUG_KEY,
501 "ShowPathInfoResponseHandler::" << __func__ <<
"() - isDir: " << (isDir?
"true":
"false") << endl);
502 BESDEBUG(SPI_DEBUG_KEY,
503 "ShowPathInfoResponseHandler::" << __func__ <<
"() - access: " << (canRead?
"true":
"false") << endl);
504 BESDEBUG(SPI_DEBUG_KEY,
"ShowPathInfoResponseHandler::" << __func__ <<
"() - size: " << size << endl);
505 BESDEBUG(SPI_DEBUG_KEY,
506 "ShowPathInfoResponseHandler::" << __func__ <<
"() - LMT: " << lastModifiedTime << endl);