void SequenceFileReader::Initialize(const saga::url& path, long offset) {
  int mode = saga::filesystem::Read;
  // Open file.
  input_stream_ = new SagaFileInputStream(path, mode);
  if (offset > 0) {
    input_stream_->Skip(offset);
  }
  // Read header.
  char header_contents[sizeof(SequenceFile::header)];
  LOG_DEBUG << "read hdr\n";

input_stream_->Read(reinterpret_cast<void*>(&header_contents),
    sizeof(header_contents));
  if (header_contents[0] != SequenceFile::header[0] ||
    header_contents[1] != SequenceFile::header[1] ||
    header_contents[2] != SequenceFile::header[2]) {
    throw saga::exception(path.get_string() + " is not a SequenceFile");
  }
  if (header_contents[3] > SEQUENCEFILE_VERSION) {
    throw saga::exception(path.get_string() + " is unsupported");
  }
  // Create input stream.
  input_adaptor_.reset(new CopyingInputStreamAdaptor(input_stream_));
  input_adaptor_->SetOwnsCopyingStream(true);
  input_ = new CodedInputStream(input_adaptor_.get());
  input_->SetTotalBytesLimit(512 * 1024 * 1024, -1);

}
Exemple #2
0
    void logical_file_cpi_impl::sync_add_location(saga::impl::void_t&, saga::url location)
    {

//        SAGA_ADAPTOR_THROW ("Not implemented! ", saga::NotImplemented);
    	SAGA_LOG_DEBUG("file, sync_add_location()");
    	check_state();

    	instance_data   instanceData (this);
        std::string rns_path = instanceData->location_.get_path();
        std::string loc_url = location.get_string();

//    	bool exists = false;
//    	try {
//    		exists  = rns_ldir.exists(rns_path);
//    	}
//    	catch (boost::system::system_error const& e) {
//    		SAGA_ADAPTOR_THROW(e.what(), saga::NoSuccess);
//    	}
//
//    	if (exists){
////    		SAGA_ADAPTOR_THROW ("The specified target already exists. ", saga::AlreadyExists);
//    		std::cout<< "The specified target already exists." << rns_path << std::endl;
//    		std::cout<< "Adding the location : " << location << std::endl;
//    		// rns_lfile.add_location??
//    	}


    	try {
//    		std::cout<< "rns_path : " << rns_path << std::endl;
//    		std::cout<< "loc_url  : " << loc_url  << std::endl;
    		rns_lfile.add_location(rns_path, loc_url);
    	}
    	catch (boost::system::system_error const& e) {
    		SAGA_ADAPTOR_THROW(e.what(), saga::NoSuccess);
    	}


    }
Exemple #3
0
  void file_cpi_impl::sync_copy (saga::impl::void_t & ret, 
                                 saga::url            dest, 
                                 int                  flags)
  {
    adaptor_data_t       adata (this);
    file_instance_data_t idata (this);

    // check preconditions
    // We can only check coditions for local targets
    
    if ( saga::adaptors::utils::is_local_address (dest) )
    {
      saga::filesystem::directory loc ("/");

      if ( loc.exists (dest.get_path ()) )
      {
        if ( ! (flags & saga::name_space::Overwrite) )
        {
          std::stringstream ss;
          ss << "Target exists: " << dest;
          SAGA_ADAPTOR_THROW (ss.str (), saga::AlreadyExists);
        }
      }
    }

    std::string src (u_.get_string ());
    std::string tgt (dest.get_string ());


    CURLcode code;

    // get handle for input and output curl ops (see README)
    CURL * in  = adata->get_curl_handle_in  ();
    CURL * out = adata->get_curl_handle_out ();

    // create buffer file
    FILE * buf = ::fopen ("/tmp/curl-cache.dat", "w+");
    ensure (NULL != buf, "fopen() failed: ", ::strerror (errno));

    // read data into buffer
    code = curl_easy_setopt (in, CURLOPT_URL, src.c_str ());
    ensure (0 == code, "Curl Error: ", curl_easy_strerror (code));

    code = curl_easy_setopt (in, CURLOPT_WRITEDATA, buf);
    ensure (0 == code, "Curl Error: ", curl_easy_strerror (code));

    code = curl_easy_perform (in);
    ensure (0 == code, "Curl Error: ", curl_easy_strerror (code));


    // data are in buffer - now rewind buffer, and copy data to target location
    ::rewind (buf);

    code = curl_easy_setopt (out, CURLOPT_URL, tgt.c_str ());
    ensure (0 == code, "Curl Error: ", curl_easy_strerror (code));

    code = curl_easy_setopt (out, CURLOPT_VERBOSE, "true");
    ensure (0 == code, "Curl Error: ", curl_easy_strerror (code));

    code = curl_easy_setopt (out, CURLOPT_USERPWD, "merzky:Z(I)nfandel");
    ensure (0 == code, "Curl Error: ", curl_easy_strerror (code));

    code = curl_easy_setopt (out, CURLOPT_UPLOAD, "true");
    ensure (0 == code, "Curl Error: ", curl_easy_strerror (code));

    code = curl_easy_setopt (out, CURLOPT_READDATA, buf);
    ensure (0 == code, "Curl Error: ", curl_easy_strerror (code));

    code = curl_easy_perform (out);
    ensure (0 == code, "Curl Error: ", curl_easy_strerror (code));

    ::fclose (buf);

    // done :-)
  }
double compare(saga::url testUrl, saga::url baseUrl) {
   // Need to create tmp names, since I need ImageMagick to understand the names
   // therefore I need to copy the images to those files
   // If I can get imagemagick to read directly from the url, I woudn't waste
   // the time in copying files...  
   char tName[] = "/tmp/testImgXXXXXX";
   char bName[] = "/tmp/baseImgXXXXXX";
   std::cerr << "about to generate names!" << std::endl;
   int testFile = mkstemp(tName);
   std::cerr << "got one (" << tName << ")" << std::endl;
   if(testFile == -1) 
      perror(NULL);
   int baseFile = mkstemp(bName);
   std::cerr << "got two (" << bName << ")" << std::endl;
   if(baseFile == -1) 
      perror(NULL);

   std::cerr << "Before copying the files... " << std::endl; 
   
   saga::size_t const KB64 = 1024*64; //64KB
   saga::size_t bytesRead;
   std::cerr << "about to open files (" << testUrl.get_string() << ", " << baseUrl.get_string() << ")" << std::endl;
   saga::filesystem::file test(testUrl, saga::filesystem::Read);
   std::cerr << "1opened!" << std::endl;
   saga::filesystem::file base    (baseUrl    , saga::filesystem::Read);
   std::cerr << "2opened!" << std::endl;
   
   char data[KB64+1];
   std::cerr << "about to read" << std::endl;
   try {
      while((bytesRead = test.read(saga::buffer(data,KB64)))!=0) {
         std::cerr << "read from testFile" << std::endl;
         int retval = write(testFile, data, bytesRead);
         if(retval < 0)
            perror(NULL);
      }
   }
   catch(saga::exception const & e) {
      std::cerr << "Exception caught: " << e.what() << std::endl;
   }
   test.close();
   fsync(testFile);
   close(testFile);
   try {
      while((bytesRead = base.read(saga::buffer(data,KB64)))!=0) {
         std::cerr << "read from baseFile" << std::endl;
         std::cerr << "size = " << base.get_size() << std::endl;
         int retval = write(baseFile, data, bytesRead);
         if(retval < 0)
            perror(NULL);
      }
   }
   catch(saga::exception const & e) {
      std::cerr << "Exception caught: " << e.what() << std::endl;
   }
   base.close();
   fsync(baseFile);
   close(baseFile);
   
   std::cerr << "Just before Image magick" << std::endl;
   
   MagickWand *magick_wand_1;
   MagickWand *magick_wand_2;
   MagickWand *compare_wand;
   double difference;
   MagickBooleanType status_1, status_2;
   
   MagickWandGenesis();
   magick_wand_1=NewMagickWand();
   status_1=MagickReadImage(magick_wand_1,bName);
   if (status_1 == MagickFalse)
	   ThrowWandException(magick_wand_1);
   MagickWandGenesis();
   magick_wand_2=NewMagickWand();
   status_2=MagickReadImage(magick_wand_2,tName);
   if (status_2 == MagickFalse)
	   ThrowWandException(magick_wand_2);
   compare_wand = NewMagickWand();
   compare_wand=MagickCompareImages(magick_wand_1,magick_wand_2, AbsoluteErrorMetric, &difference);
   std::cerr << "Difference is: " << difference << std::endl;
   
   DestroyMagickWand(magick_wand_1);
   DestroyMagickWand(magick_wand_2);

   MagickWandTerminus();
   remove(bName);
   remove(tName);
   return difference;
}