Beispiel #1
0
void session::save_torrent(const lt::torrent_info &ti)
{
    fs::path data = environment::get_data_path();
    fs::directory dir = data.combine(L"Torrents");

    if (!dir.path().exists())
    {
        dir.create();
    }

    lt::create_torrent ct(ti);
    lt::entry e = ct.generate();

    std::vector<char> buf;
    lt::bencode(std::back_inserter(buf), e);

    std::wstring hash = lt::convert_to_wstring(lt::to_hex(ti.info_hash().to_string()));
    fs::file torrentFile(dir.path().combine((hash + L".torrent")));

    try
    {
        torrentFile.write_all(buf);
    }
    catch (const std::exception &e)
    {
        LOG(error) << "Error when writing torrent file: " << e.what();
    }
}
      bool Context::dataPresentInSeedDir(libtorrent::torrent_info const& _torrent_info)
      {
         BTG_MENTER(logWrapper(), "dataPresentInSeedDir(torrent_info)", "");

         bool status = false;

         libtorrent::torrent_info::file_iterator iter;

         // Try to find any file.
         for (iter = _torrent_info.begin_files();
              iter != _torrent_info.end_files();
              iter++)
            {
               libtorrent::file_entry const& fe = *iter;
         
               // The full path.
               boost::filesystem::path const& file_path = fe.path;
               
               if (boost::filesystem::exists(seedDir_ / file_path))
                  {
                     // Check whether we have the same directory as file name in torrent.
                     // If so, we can't use the seedDir anyway.
                     if (boost::filesystem::is_directory(seedDir_ / file_path))
                        {
                           BTG_MNOTICE(logWrapper(), "dataPresentInSeedDir: torrent file '"
                                       << file_path.string() << "' is a directory, can't use seedDir.");
                        }
                     else
                        {
                           status = true;
                           
                           BTG_MNOTICE(logWrapper(), "dataPresentInSeedDir: found torrent file '"
                                       << file_path.string() << "'.");
                        }
                     break;
                  }
            }

         BTG_MEXIT(logWrapper(), "dataPresentInSeedDir(torrent_info)", status);
         return status;
      }
void generate_files(libtorrent::torrent_info const& ti, std::string const& path
	, bool alternate_data)
{
	file_pool fp;

	storage_params params;
	params.files = &ti.files();
	params.path = path;
	params.pool = &fp;

	default_storage st(params);

	int const num_pieces = ti.num_pieces();

	std::vector<char> buffer;
	for (int i = 0; i < num_pieces; ++i)
	{
		int const piece_size = ti.piece_size(i);
		buffer.resize(ti.piece_length());

		std::uint8_t const data = std::uint8_t((alternate_data ? 255 - i : i) & 0xff);
		for (int o = 0; o < piece_size; ++o)
		{
			memcpy(&buffer[o], &data, 1);
		}

		file::iovec_t b = { &buffer[0], size_t(piece_size) };
		storage_error ec;
		int ret = st.writev(&b, 1, i, 0, 0, ec);
		if (ret != piece_size || ec)
		{
			std::fprintf(stderr, "ERROR writing files: (%d expected %d) %s\n"
				, ret, piece_size, ec.ec.message().c_str());
		}
	}
}
      bool Context::torrentInfoToFiles(libtorrent::torrent_info const& _tinfo,
                                       std::vector<std::string> & _output) const
      {
         BTG_MENTER(logWrapper(), "torrentInfoToFiles", "");

         bool status = true;
         try
            {
               libtorrent::torrent_info::file_iterator iter;

               for (iter = _tinfo.begin_files();
                    iter != _tinfo.end_files();
                    iter++)
                  {
                     libtorrent::file_entry const& fe = *iter;
                     BTG_MNOTICE(logWrapper(), fe.path.string());

                     _output.push_back(fe.path.string());
                  }
            }
         catch (std::exception& e)
            {
               BTG_ERROR_LOG(logWrapper(), "libtorrent exception (torrentInfoToFiles): " << e.what() );
               status = false;
            }

         if (_output.size() == 0)
         {
            // Torrents with no files are not ok.
            BTG_ERROR_LOG(logWrapper(), "torrentInfoToFiles, attempt to load torrent with no files in it.");
            status = false;
         }
         
         BTG_MEXIT(logWrapper(), "torrentInfoToFiles", status);
         return status;
      }
QString QTorrentHandle::filepath_at(const libtorrent::torrent_info &info, unsigned int index)
{
    return fsutils::fromNativePath(misc::toQStringU(info.files().file_path(index)));

}
      bool Context::dataPresentInSeedDir(libtorrent::torrent_info const& _torrent_info)
      {
         BTG_MENTER(logWrapper(), "dataPresentInSeedDir(torrent_info)", "");

         bool status = true;
         
         // Fix of bug #10064
         // Try to find ANY file in seed dir before give up
         bool found = false;

         libtorrent::torrent_info::file_iterator iter;

         // Find and count the files.
         for (iter = _torrent_info.begin_files();
              iter != _torrent_info.end_files();
              iter++)
            {
               libtorrent::file_entry const& fe = *iter;
	      
               // The full path.
               boost::filesystem::path file_path = fe.path;
	      
               // Only the file name.
               std::string filename = file_path.leaf();
	      
               // Get the first directory name, if it exists.
               if (!file_path.branch_path().empty())
                  {
                     file_path = file_path.branch_path();
                  }

               // Fix of bug #11575 / #12055
               // Check if the found file path is a directory.
               // If its not, then its just files in the seed
               // directory.
               
               
               try
                  {
                     if (boost::filesystem::exists(seedDir_ / file_path) &&
                         boost::filesystem::is_directory(seedDir_ / file_path))
                        {
                           file_path = seedDir_ / file_path;
                        }
                     else
                        {
                           file_path = seedDir_;
                        }
                  }
               catch(std::exception& e)
                  {
                     BTG_ERROR_LOG(logWrapper(), 
                                   "Got exception from boost::filesystem, is_directory: " << 
                                   e.what());
                     status = false;
                     break;
                  }

               BTG_MNOTICE(logWrapper(), "Using directory '" << file_path.string() << "'");
               BTG_MNOTICE(logWrapper(), "Checking file '" << filename << "'");
	      
               try
                  {
                     if (find_file(boost::filesystem::path(file_path), filename))
                        {
                           found = true;
                           break;
                        }
                  }
               catch(std::exception& e)
                  {
                     BTG_ERROR_LOG(logWrapper(), 
                                   "Got exception from find_file: " << e.what());
                     status = false;
                     break;
                  }
            }

         BTG_MEXIT(logWrapper(), "dataPresentInSeedDir(torrent_info)", (status && found) );
         return status && found;
      }