41#include "ShowPathInfoResponseHandler.h"
45#include "BESInfoList.h"
48#include "BESRequestHandlerList.h"
49#include "BESRequestHandler.h"
51#include "BESDapNames.h"
52#include "BESDataNames.h"
53#include "BESCatalogList.h"
54#include "BESCatalog.h"
55#include "BESCatalogEntry.h"
56#include "BESCatalogUtils.h"
57#include "BESSyntaxUserError.h"
58#include "BESForbiddenError.h"
59#include "BESNotFoundError.h"
60#include "BESStopWatch.h"
68#define PATH_INFO_RESPONSE "PathInfo"
70#define VALID_PATH "validPath"
71#define REMAINDER "remainder"
72#define IS_DATA "isData"
73#define IS_FILE "isFile"
75#define IS_ACCESSIBLE "access"
77#define LMT "lastModified"
79#define SPI_DEBUG_KEY "show-path-info"
80#define SHOW_PATH_INFO_RESPONSE_STR "showPathInfo"
82const auto MODULE=
"dap";
83#define prolog string("ShowPathInfoResponseHandler::").append(__func__).append("() - ")
85ShowPathInfoResponseHandler::ShowPathInfoResponseHandler(
const string &name) :
90ShowPathInfoResponseHandler::~ShowPathInfoResponseHandler()
107 BES_STOPWATCH_START_DHI(MODULE, prolog +
"Timing", &dhi);
109 BESDEBUG(SPI_DEBUG_KEY,
110 "ShowPathInfoResponseHandler::execute() - BEGIN ############################################################## BEGIN" << endl);
112 BESInfo *info = BESInfoList::TheList()->build_info();
113 d_response_object = info;
115 string container = dhi.
data[CONTAINER];
118 string defcatname = BESCatalogList::TheCatalogList()->default_catalog_name();
121 BESCatalog *defcat = BESCatalogList::TheCatalogList()->default_catalog();
123 throw BESInternalError(
"Not able to find the default catalog.", __FILE__, __LINE__);
126 string::size_type notslash = container.find_first_not_of(
"/", 0);
127 if (notslash != string::npos) {
128 container = container.substr(notslash);
133 string::size_type slash = container.find_first_of(
"/", 0);
134 if (slash != string::npos) {
135 catname = container.substr(0, slash);
143 BESCatalog *catobj = BESCatalogList::TheCatalogList()->find_catalog(catname);
145 if (slash != string::npos) {
146 container = container.substr(slash + 1);
149 notslash = container.find_first_not_of(
"/", 0);
150 if (notslash != string::npos) {
151 container = container.substr(notslash);
159 if (container.empty()) container =
"/";
161 if (container[0] !=
'/') container =
"/" + container;
163 BESDEBUG(SPI_DEBUG_KEY,
"ShowPathInfoResponseHandler::execute() - container: " << container << endl);
168 map<string, string, std::less<>> pathInfoAttrs;
169 pathInfoAttrs[PATH] = container;
171 info->begin_tag(PATH_INFO_RESPONSE, &pathInfoAttrs);
173 string validPath, remainder;
174 bool isFile, isDir, canRead;
175 long long size, time;
177 BESCatalogUtils *utils = BESCatalogList::TheCatalogList()->default_catalog()->get_catalog_utils();
179 eval_resource_path(container, utils->
get_root_dir(), utils->follow_sym_links(), validPath, isFile, isDir, size,
180 time, canRead, remainder);
187 if (validPath.size() != 0) {
194 string err = (string)
"Failed to find the validPath node " + validPath
195 +
" this should not be possible. Some thing BAD is happening.";
200 list<string> services = entry->get_service_list();
203 if (services.size()) {
206 for (; si != se; si++) {
207 if ((*si) == OPENDAP_SERVICE) isData =
true;
212 map<string, string, std::less<>> validPathAttrs;
213 validPathAttrs[IS_DATA] = isData ?
"true" :
"false";
214 validPathAttrs[IS_FILE] = isFile ?
"true" :
"false";
215 validPathAttrs[IS_DIR] = isDir ?
"true" :
"false";
216 validPathAttrs[IS_ACCESSIBLE] = canRead ?
"true" :
"false";
219 std::ostringstream os_size;
221 validPathAttrs[SIZE] = os_size.str();
224 std::ostringstream os_time;
226 validPathAttrs[LMT] = os_time.str();
228 info->add_tag(VALID_PATH, validPath, &validPathAttrs);
229 info->add_tag(REMAINDER, remainder);
231 info->end_tag(PATH_INFO_RESPONSE);
234 info->end_response();
236 BESDEBUG(SPI_DEBUG_KEY,
"ShowPathInfoResponseHandler::execute() - END" << endl);
252 if (d_response_object) {
267 strm << BESIndent::LMarg <<
"ShowPathInfoResponseHandler::dump - (" << (
void *)
this <<
")" << std::endl;
270 BESIndent::UnIndent();
274ShowPathInfoResponseHandler::ShowPathInfoResponseBuilder(
const string &name)
282void ShowPathInfoResponseHandler::eval_resource_path(
const string &resource_path,
const string &catalog_root,
283 const bool follow_sym_links,
string &validPath,
bool &isFile,
bool &isDir,
long long &size,
284 long long &lastModifiedTime,
bool &canRead,
string &remainder)
287 BESDEBUG(SPI_DEBUG_KEY,
288 "ShowPathInfoResponseHandler::"<<__func__ <<
"() - " <<
"CatalogRoot: "<< catalog_root << endl);
290 BESDEBUG(SPI_DEBUG_KEY,
291 "ShowPathInfoResponseHandler::"<<__func__ <<
"() - " <<
"resourceID: "<< resource_path << endl);
296 lastModifiedTime = -1;
299 string rem = resource_path;
305 int (*ye_old_stat_function)(
const char *pathname,
struct stat *buf);
306 if (follow_sym_links) {
307 BESDEBUG(SPI_DEBUG_KEY,
308 "ShowPathInfoResponseHandler::"<<__func__ <<
"() - " <<
"Using 'stat' function (follow_sym_links = true)" << endl);
309 ye_old_stat_function = &stat;
312 BESDEBUG(SPI_DEBUG_KEY,
313 "ShowPathInfoResponseHandler::"<<__func__ <<
"() - " <<
"Using 'lstat' function (follow_sym_links = false)" << endl);
314 ye_old_stat_function = &lstat;
319 if (resource_path ==
"") {
320 BESDEBUG(SPI_DEBUG_KEY,
"ShowPathInfoResponseHandler::"<<__func__ <<
"() - The resourceID is empty" << endl);
326 string::size_type dotdot = resource_path.find(
"..");
327 if (dotdot != string::npos) {
328 BESDEBUG(SPI_DEBUG_KEY,
329 "ShowPathInfoResponseHandler::"<<__func__ <<
"() - " <<
" ERROR: The resourceID '" << resource_path <<
"' contains the substring '..' This is Forbidden." << endl);
330 string s = (string)
"Invalid node name '" + resource_path +
"' ACCESS IS FORBIDDEN";
332 throw BESForbiddenError(s, __FILE__, __LINE__);
341 string fullpath = catalog_root;
350 size_t slash = rem.find(
'/');
351 if (slash == string::npos) {
352 BESDEBUG(SPI_DEBUG_KEY,
353 "ShowPathInfoResponseHandler::"<<__func__ <<
"() - " <<
"Checking final path component: " << rem << endl);
362 rem = rem.substr(slash + 1, rem.size() - slash);
365 BESDEBUG(SPI_DEBUG_KEY,
366 "ShowPathInfoResponseHandler::"<<__func__ <<
"() - " <<
"validPath: "<< validPath << endl);
367 BESDEBUG(SPI_DEBUG_KEY,
368 "ShowPathInfoResponseHandler::"<<__func__ <<
"() - " <<
"checking: "<< checking << endl);
369 BESDEBUG(SPI_DEBUG_KEY,
370 "ShowPathInfoResponseHandler::"<<__func__ <<
"() - " <<
"fullpath: "<< fullpath << endl);
372 BESDEBUG(SPI_DEBUG_KEY,
"ShowPathInfoResponseHandler::"<<__func__ <<
"() - " <<
"rem: "<< rem << endl);
374 BESDEBUG(SPI_DEBUG_KEY,
375 "ShowPathInfoResponseHandler::"<<__func__ <<
"() - " <<
"remainder: "<< remainder << endl);
378 int statret = ye_old_stat_function(fullpath.c_str(), &sb);
382 validPath = checking;
389 char *s_err = strerror(errsv);
390 string error =
"Unable to access node " + checking +
": ";
392 error = error + s_err;
395 error = error +
"unknown access error";
397 BESDEBUG(SPI_DEBUG_KEY,
398 "ShowPathInfoResponseHandler::"<<__func__ <<
"() - " <<
"error: "<< error <<
" errno: " << errno << endl);
400 BESDEBUG(SPI_DEBUG_KEY,
401 "ShowPathInfoResponseHandler::"<<__func__ <<
"() - " <<
"remainder: '" << remainder <<
"'" << endl);
405 if (errsv != ENOENT && errsv != ENOTDIR) {
406 throw BESForbiddenError(error, __FILE__, __LINE__);
410 size_t s_loc = remainder.find(
'/');
411 if (s_loc == string::npos) {
413 string basename = remainder;
414 bool moreDots =
true;
417 size_t d_loc = basename.find_last_of(
".");
418 if (d_loc != string::npos) {
419 basename = basename.substr(0, d_loc);
420 BESDEBUG(SPI_DEBUG_KEY,
421 "ShowPathInfoResponseHandler::" << __func__ <<
"() - basename: "<< basename << endl);
423 string candidate_remainder = remainder.substr(basename.size());
424 BESDEBUG(SPI_DEBUG_KEY,
425 "ShowPathInfoResponseHandler::" << __func__ <<
"() - candidate_remainder: "<< candidate_remainder << endl);
428 BESDEBUG(SPI_DEBUG_KEY,
429 "ShowPathInfoResponseHandler::" << __func__ <<
"() - candidate_path: "<< candidate_path << endl);
432 BESDEBUG(SPI_DEBUG_KEY,
433 "ShowPathInfoResponseHandler::" << __func__ <<
"() - full_candidate_path: "<< full_candidate_path << endl);
436 int statret1 = ye_old_stat_function(full_candidate_path.c_str(), &sb1);
437 if (statret1 != -1) {
438 validPath = candidate_path;
439 remainder = candidate_remainder;
444 BESDEBUG(SPI_DEBUG_KEY,
445 "ShowPathInfoResponseHandler::"<<__func__ <<
"() - " <<
"No dots in remainder: "<< remainder << endl);
451 BESDEBUG(SPI_DEBUG_KEY,
452 "ShowPathInfoResponseHandler::"<<__func__ <<
"() - " <<
"Remainder has slash pollution: "<< remainder << endl);
458 statret = ye_old_stat_function(fullpath.c_str(), &sb);
459 if (S_ISREG(sb.st_mode)) {
460 BESDEBUG(SPI_DEBUG_KEY,
461 "ShowPathInfoResponseHandler::"<<__func__ <<
"() - " <<
"'"<< fullpath <<
"' Is regular file." << endl);
465 else if (S_ISDIR(sb.st_mode)) {
466 BESDEBUG(SPI_DEBUG_KEY,
467 "ShowPathInfoResponseHandler::"<<__func__ <<
"() - " <<
"'"<< fullpath <<
"' Is directory." << endl);
471 else if (S_ISLNK(sb.st_mode)) {
472 BESDEBUG(SPI_DEBUG_KEY,
473 "ShowPathInfoResponseHandler::"<<__func__ <<
"() - " <<
"'"<< fullpath <<
"' Is symbolic Link." << endl);
474 string error =
"Service not configured to traverse symbolic links as embodied by the node '" + checking
475 +
"' ACCESS IS FORBIDDEN";
476 throw BESForbiddenError(error, __FILE__, __LINE__);
482 std::ifstream ifile(fullpath.c_str());
483 canRead = ifile.good();
496#if !defined(_POSIX_C_SOURCE) || defined(_DARWIN_C_SOURCE)
498 lastModifiedTime = (sb.st_mtimespec.tv_sec * 1000) + (sb.st_mtimespec.tv_nsec / 1000000);
500 lastModifiedTime = sb.st_mtime;
504 BESDEBUG(SPI_DEBUG_KEY,
"ShowPathInfoResponseHandler::" << __func__ <<
"() - fullpath: " << fullpath << endl);
505 BESDEBUG(SPI_DEBUG_KEY,
"ShowPathInfoResponseHandler::" << __func__ <<
"() - validPath: " << validPath << endl);
506 BESDEBUG(SPI_DEBUG_KEY,
"ShowPathInfoResponseHandler::" << __func__ <<
"() - remainder: " << remainder << endl);
507 BESDEBUG(SPI_DEBUG_KEY,
"ShowPathInfoResponseHandler::" << __func__ <<
"() - rem: " << rem << endl);
508 BESDEBUG(SPI_DEBUG_KEY,
509 "ShowPathInfoResponseHandler::" << __func__ <<
"() - isFile: " << (isFile?
"true":
"false") << endl);
510 BESDEBUG(SPI_DEBUG_KEY,
511 "ShowPathInfoResponseHandler::" << __func__ <<
"() - isDir: " << (isDir?
"true":
"false") << endl);
512 BESDEBUG(SPI_DEBUG_KEY,
513 "ShowPathInfoResponseHandler::" << __func__ <<
"() - access: " << (canRead?
"true":
"false") << endl);
514 BESDEBUG(SPI_DEBUG_KEY,
"ShowPathInfoResponseHandler::" << __func__ <<
"() - size: " << size << endl);
515 BESDEBUG(SPI_DEBUG_KEY,
516 "ShowPathInfoResponseHandler::" << __func__ <<
"() - LMT: " << lastModifiedTime << endl);
const std::string & get_root_dir() const
Get the root directory of the catalog.
Catalogs provide a hierarchical organization for data.
virtual BESCatalogEntry * show_catalog(const std::string &container, BESCatalogEntry *entry)=0
Structure storing information used by the BES to handle the request.
std::map< std::string, std::string > data
the map of string data that will be required for the current request.
informational response object
virtual void transmit(BESTransmitter *transmitter, BESDataHandlerInterface &dhi)=0
transmit the informational object
virtual void begin_response(const std::string &response_name, BESDataHandlerInterface &dhi)
begin the informational response
exception thrown if internal error encountered
handler object that knows how to create a specific response object
void dump(std::ostream &strm) const override
dumps information about this object
static std::string assemblePath(const std::string &firstPart, const std::string &secondPart, bool leadingSlash=false, bool trailingSlash=false)
Assemble path fragments making sure that they are separated by a single '/' character.
response handler that returns nodes or leaves within the catalog either at the root or at a specified...
virtual void transmit(BESTransmitter *transmitter, BESDataHandlerInterface &dhi)
transmit the response object built by the execute command using the specified transmitter object
virtual void execute(BESDataHandlerInterface &dhi)
executes the command 'show catalog|leaves [for <node>];' by returning nodes or leaves at the top leve...
virtual void dump(std::ostream &strm) const
dumps information about this object