Ejemplo n.º 1
0
  ////////////////////////////////////////////////////////////////////////
  // initialize the adaptor using the preferences, return false to
  // cancel adaptor loading
  bool adaptor::init(saga::impl::session *session,
          saga::ini::ini const& glob_ini, saga::ini::ini const& adap_ini)
  {
    if (adap_ini.has_section("cli")) {
      saga::ini::section s = adap_ini.get_section("cli");

      if (s.has_entry("binary_path")) {
    	  binary_path = s.get_entry("binary_path");
    	  std::cout << "binary_path = " << binary_path << std::endl;
      }

      if (s.has_section("description")) {
		saga::ini::section ss = s.get_section("description");

		std::string v = ss.get_entry(sja::description_job_contact);
		saga::url mailto(v);
		if (mailto.get_scheme() != "mailto" ||
			mailto.get_path().empty()  )
			// TODO mail address format check.
		{
		  return false;
		}
		job_contact = v;
      }
    }
    return true;
  }
 // cache ini section on init
 bool filesystem_adaptor::init (saga::impl::session  * s,
                          saga::ini::ini const & glob_ini,
                          saga::ini::ini const & adap_ini)
 {
   if ( adap_ini.has_section ("preferences") )
   {
     ini_ = adap_ini.get_section ("preferences").get_entries ();
   }
     
   return true;
 } 
dir_cpi_impl::dir_cpi_impl (proxy                * p, 
                            cpi_info       const & info,
                            saga::ini::ini const & glob_ini,
                            saga::ini::ini const & adap_ini,
                            boost::shared_ptr<saga::adaptor> adaptor)

: directory_cpi (p, info, adaptor, cpi::Noflags)
{
    adaptor_data_t adata(this);
    instance_data idata(this);
        
    // Read some stuff from the .ini file
    saga::ini::ini prefs = adap_ini.get_section ("preferences");
    
    if (prefs.has_entry("write_ftp_log")) 
    {
        std::string wfl = prefs.get_entry("write_ftp_log");
        if(wfl == "true" || wfl == "True" || wfl == "TRUE")
        {
            write_log_ = true;
        }
        else 
        {
            write_log_ = false;
        }
        
    }
    
    if (prefs.has_entry("logilfe_location"))
        logfile_loc_ = prefs.get_entry("logilfe_location");
    else 
        logfile_loc_ = "saga_gridftp.log";
    
    saga::url location(idata->location_);
    std::string host(location.get_host());
    std::string scheme(location.get_scheme());
	
    // check if we can handle url scheme
    if (scheme != "file" && scheme != "gridftp" && scheme != "gsiftp")
    {
       SAGA_OSSTREAM strm;
       strm << "Could not initialize file object for [" << idata->location_ << "]. " 
            << "Only griftp:// and gsiftp:// schemes are supported.";
       SAGA_ADAPTOR_THROW(SAGA_OSSTREAM_GETSTRING(strm), saga::adaptors::AdaptorDeclined); 
    }
    
    
    this->is_local_dir_ = false;
    
    if (scheme == "file") 
    {
        this->is_local_dir_ = true;
        // make sure directory exist
        namespace fs = boost::filesystem;
        try 
        {
            std::string url = saga::url::unescape(location.get_path());
            
            #if BOOST_FILESYSTEM_VERSION > 2
            fs::path fpath (url);
            #else
            fs::path fpath (url, fs::native);
            #endif
            
            if ( ! fs::exists (fpath) )
            {
                SAGA_OSSTREAM strm;
                strm << "Local directory doesn't exist: [" << location << "].";
                SAGA_ADAPTOR_THROW(SAGA_OSSTREAM_GETSTRING(strm), saga::adaptors::AdaptorDeclined); 
            }
            else 
            {
                is_open_ = true; // otherwise check in copy() will fail
                return;
            }
        }
        catch (boost::system::system_error const& e) 
        {
            SAGA_ADAPTOR_THROW(
                               location.get_string() + ": caught filesystem exception: " + e.what(),
                               saga::NoSuccess);
        }
        
    }
    else
    {
        if (host.empty())
        {
            SAGA_OSSTREAM strm;
            strm << "Could not initialize file object for [" << idata->location_ << "]. " 
            << "URL doesn't define a hostname.";
            SAGA_ADAPTOR_THROW(SAGA_OSSTREAM_GETSTRING(strm), saga::BadParameter); 
        }
    }
    
    // check if we have x.509 contexts available and if they are usable
    // with this adaptor. if no context is usable, the constructor fails with
    // an authorization failed exception.
    std::vector <saga::context> contexts = p->get_session ().list_contexts ();
    std::vector <saga::context> context_list;
    // holds a list of reasons why a context can't be used. if no context
    // can be used, the list will be appended to the exception message otherwise
    // it will be discarded. 
    std::vector <std::string> context_error_list;
    
    for (unsigned int i = 0; i < contexts.size (); i++)
    {
      globus_adaptors_shared::check_x509_globus_cert(contexts[i], 
                                                     context_list, 
                                                     context_error_list);
    } 
    
    if(context_list.size() <1) {
        SAGA_OSSTREAM strm;
        strm << "Could not initialize directory object for " << idata->location_ << ". "
             << "No valid and/or usable x.509 context could be found:\n";
        for(unsigned int i=0; i<context_error_list.size(); ++i) {
          strm << "    - " << context_error_list[i] << "\n";
        }
        SAGA_ADAPTOR_THROW(SAGA_OSSTREAM_GETSTRING(strm),
                           saga::AuthorizationFailed);
    }
    
    //If we've made it here, it should be safe to load
    // the GRAM modules now. The loader employs a sigleton mechanism,
    // so ut doesn't matter if we call this method multiple times.
    globus_module_loader::globus_init ();
    
    GridFTPConnection * ConnectionHandle = 
    adata->getConnectionHandleForURL(saga::url(idata->location_.get_url()), write_log_, logfile_loc_);
	
    // check if file exists AND is a dir (not a file)
    bool exists  = true;
    bool is_dir  = false;
    
    try 
    {
        is_dir = ConnectionHandle->is_dir(idata->location_.get_url());
    }
    catch( globus_gridftp_file_adaptor::exception const & e )
    {
        if( e.get_error() == DoesNotExist ) {
            exists = false;
        }    
        else {
            error_package ep = globus_gridftp_file_adaptor
            ::error_default_redirect(e, idata->location_.get_url());
            std::string e_text("Could not opend directory. " + ep.error_text);
            SAGA_ADAPTOR_THROW(e_text, ep.saga_error);
        }
    }

    // check for openmode //
    saga::filesystem::flags OpenMode = (saga::filesystem::flags)idata->mode_;
    
    if(exists)
    {
        if(!is_dir)
        {
            SAGA_ADAPTOR_THROW ("Could not open directory. URL doesn't point to a directory: " +
                                idata->location_.get_url(), saga::BadParameter);
        }
        else
        {
            if((OpenMode & saga::filesystem::Create) && (OpenMode & saga::filesystem::Exclusive))
            {
                SAGA_ADAPTOR_THROW ("Could not open directory with 'Exclusive' flag set. The directory already exists: " +
                                    idata->location_.get_url(), saga::AlreadyExists);
            }
        }
    }
    else // !exists
    {
        if(!(OpenMode & saga::filesystem::Create))
        {
            SAGA_ADAPTOR_THROW ("Could not open directory. The directory doesn't exist and 'Create' flag is not set: " +
                                idata->location_.get_url(), saga::DoesNotExist);
        }
        else
        {
            try
            {
                ConnectionHandle->make_directory( idata->location_.get_url() );
            }
            catch( globus_gridftp_file_adaptor::exception const & e )
            {
                error_package ep = globus_gridftp_file_adaptor
                ::error_default_redirect(e, idata->location_.get_url());
                std::string e_text("Could not opend directory. " + ep.error_text);
                SAGA_ADAPTOR_THROW(e_text, ep.saga_error);
            }  
        }
        
    }
    
    this->is_open_ = true;
}