Back to index

nordugrid-arc-nox  1.1.0~rc6
URLMap.cpp
Go to the documentation of this file.
00001 // -*- indent-tabs-mode: nil -*-
00002 
00003 #ifdef HAVE_CONFIG_H
00004 #include <config.h>
00005 #endif
00006 
00007 #include <string>
00008 
00009 #include <fcntl.h>
00010 #include <unistd.h>
00011 
00012 #include <arc/Logger.h>
00013 #include <arc/data/URLMap.h>
00014 
00015 namespace Arc {
00016 
00017   Logger URLMap::logger(Logger::getRootLogger(), "URLMap");
00018 
00019   URLMap::URLMap() {}
00020 
00021   URLMap::~URLMap() {}
00022 
00023   bool URLMap::map(URL& url) const {
00024     for (std::list<map_entry>::const_iterator i = entries.begin();
00025          i != entries.end(); ++i)
00026       if (url.str().substr(0, i->initial.str().length()) == i->initial.str()) {
00027         std::string tmp_url = url.str();
00028         tmp_url.replace(0, i->initial.str().length(), i->replacement.str());
00029         URL newurl = tmp_url;
00030         /* must return semi-valid url */
00031         if (newurl.Protocol() == "file") { /* local file - check permissions */
00032           int h = open(newurl.Path().c_str(), O_RDONLY);
00033           if (h == -1) {
00034             logger.msg(ERROR, "file %s is not accessible", newurl.Path());
00035             return false;
00036           }
00037           close(h);
00038           if (i->access) { /* how it should be accessed on nodes */
00039             tmp_url.replace(0, i->replacement.str().length(), i->access.str());
00040             newurl = tmp_url;
00041             newurl.ChangeProtocol("link");
00042           }
00043         }
00044         logger.msg(INFO, "Mapping %s to %s", url.str(), newurl.str());
00045         url = newurl;
00046         return true;
00047       }
00048     return false;
00049   }
00050 
00051   bool URLMap::local(const URL& url) const {
00052     for (std::list<map_entry>::const_iterator i = entries.begin();
00053          i != entries.end(); ++i)
00054       if (url.str().substr(0, i->initial.str().length()) == i->initial.str())
00055         return true;
00056     return false;
00057   }
00058 
00059   void URLMap::add(const URL& templ, const URL& repl, const URL& accs) {
00060     entries.push_back(map_entry(templ, repl, accs));
00061   }
00062 
00063 } // namespace Arc