replaced boost.filesystem with custom functions (improves efficiency and drops unnecessary dependencies and improves libtorrent portability)

This commit is contained in:
Arvid Norberg 2009-10-26 01:29:39 +00:00
parent 4412e2f1f6
commit c418165e07
60 changed files with 1555 additions and 1273 deletions

View File

@ -1,3 +1,5 @@
* replaced boost.filesystem with custom functions
* replaced dependency on boost.thread by asio's internal thread primitives
* added support for i2p torrents
* cleaned up usage of MAX_PATH and related macros

15
Jamfile
View File

@ -100,9 +100,7 @@ rule linking ( properties * )
if <boost>system in $(properties)
{
result += <library>boost_filesystem
<library>boost_system
;
result += <library>boost_system ;
}
if <toolset>gcc in $(properties)
@ -123,16 +121,11 @@ rule linking ( properties * )
result += <fpic>on ;
}
result += <library>/boost/filesystem//boost_filesystem/<link>static
<library>/boost/system//boost_system/<link>static
;
result += <library>/boost/system//boost_system/<link>static ;
}
else
{
result +=
<library>/boost/filesystem//boost_filesystem/<link>shared
<library>/boost/system//boost_system/<link>shared
;
result += <library>/boost/system//boost_system/<link>shared ;
}
result += <include>$(BOOST_ROOT) <define>BOOST_ALL_NO_LIB ;
}
@ -296,10 +289,8 @@ local boost-library-search-path =
<search>/sw/lib
;
lib boost_filesystem : : <target-os>darwin <name>boost_filesystem-mt $(boost-library-search-path) ;
lib boost_system : : <target-os>darwin <name>boost_system-mt $(boost-library-search-path) ;
lib boost_filesystem : : <name>boost_filesystem ;
lib boost_system : : <name>boost_system ;
# openssl on linux/bsd/macos etc.

View File

@ -43,7 +43,6 @@ python-extension libtorrent
src/session.cpp
src/entry.cpp
src/torrent_info.cpp
src/filesystem.cpp
src/torrent_handle.cpp
src/torrent_status.cpp
src/session_settings.cpp

View File

@ -22,7 +22,7 @@ namespace
obj(i);
}
void set_piece_hashes_callback(create_torrent& c, boost::filesystem::path const& p
void set_piece_hashes_callback(create_torrent& c, std::string const& p
, boost::python::object cb)
{
set_piece_hashes(c, p, boost::bind(call_python_object, cb, _1));
@ -32,22 +32,22 @@ namespace
void bind_create_torrent()
{
void (file_storage::*add_file0)(file_entry const&) = &file_storage::add_file;
void (file_storage::*add_file1)(fs::path const&, size_type, int, std::time_t, fs::path const&) = &file_storage::add_file;
#ifndef BOOST_FILESYSTEM_NARROW_ONLY
void (file_storage::*add_file2)(fs::wpath const&, size_type, int, std::time_t, fs::path const&) = &file_storage::add_file;
void (file_storage::*add_file1)(std::string const&, size_type, int, std::time_t, std::string const&) = &file_storage::add_file;
#if TORRENT_USE_WSTRING
void (file_storage::*add_file2)(std::wstring const&, size_type, int, std::time_t, std::string const&) = &file_storage::add_file;
#endif
void (file_storage::*set_name0)(std::string const&) = &file_storage::set_name;
void (file_storage::*set_name1)(std::wstring const&) = &file_storage::set_name;
void (*set_piece_hashes0)(create_torrent&, boost::filesystem::path const&) = &set_piece_hashes;
void (*add_files0)(file_storage&, boost::filesystem::path const&) = add_files;
void (*set_piece_hashes0)(create_torrent&, std::string const&) = &set_piece_hashes;
void (*add_files0)(file_storage&, std::string const&) = add_files;
class_<file_storage>("file_storage")
.def("is_valid", &file_storage::is_valid)
.def("add_file", add_file0)
.def("add_file", add_file1, (arg("path"), arg("size"), arg("flags") = 0, arg("mtime") = 0, arg("linkpath") = ""))
#ifndef BOOST_FILESYSTEM_NARROW_ONLY
#if TORRENT_USE_WSTRING
.def("add_file", add_file2, (arg("path"), arg("size"), arg("flags") = 0, arg("mtime") = 0, arg("linkpath") = ""))
#endif
.def("num_files", &file_storage::num_files)

View File

@ -1,72 +0,0 @@
// Copyright Daniel Wallin 2006. Use, modification and distribution is
// subject to the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
#include <boost/python.hpp>
#include <boost/filesystem/path.hpp>
#include "libtorrent/utf8.hpp"
using namespace boost::python;
struct path_to_python
{
static PyObject* convert(boost::filesystem::path const& p)
{
return incref(object(p.string()).ptr());
}
};
struct path_from_python
{
path_from_python()
{
converter::registry::push_back(
&convertible, &construct, type_id<boost::filesystem::path>()
);
}
static void* convertible(PyObject* x)
{
return PyString_Check(x) ? x : PyUnicode_Check(x) ? x : 0;
}
static void construct(PyObject* x, converter::rvalue_from_python_stage1_data* data)
{
using libtorrent::wchar_utf8;
void* storage = ((converter::rvalue_from_python_storage<
boost::filesystem::path
>*)data)->storage.bytes;
if (PyUnicode_Check(x))
{
std::wstring str;
str.resize(PyUnicode_GetSize(x) + 1, 0);
int len = PyUnicode_AsWideChar((PyUnicodeObject*)x, &str[0], str.size());
if (len > -1)
{
assert(len < str.size());
str[len] = 0;
}
else str[str.size()-1] = 0;
std::string utf8;
int ret = wchar_utf8(str, utf8);
new (storage) boost::filesystem::path(utf8);
}
else
{
new (storage) boost::filesystem::path(PyString_AsString(x));
}
data->convertible = storage;
}
};
void bind_filesystem()
{
to_python_converter<boost::filesystem::path, path_to_python>();
path_from_python();
using namespace boost::filesystem;
if (path::default_name_check_writable())
path::default_name_check(no_check);
}

View File

@ -29,7 +29,7 @@ namespace {
name = extract<std::string>(params["name"]);
p.name = name.c_str();
}
p.save_path = fs::path(extract<std::string>(params["save_path"]));
p.save_path = extract<std::string>(params["save_path"]);
std::vector<char> resume_buf;
if (params.has_key("resume_data"))

View File

@ -10,7 +10,6 @@ void bind_big_number();
void bind_session();
void bind_entry();
void bind_torrent_info();
void bind_filesystem();
void bind_torrent_handle();
void bind_torrent_status();
void bind_session_settings();
@ -37,7 +36,6 @@ BOOST_PYTHON_MODULE(libtorrent)
bind_entry();
bind_session();
bind_torrent_info();
bind_filesystem();
bind_torrent_handle();
bind_torrent_status();
bind_session_settings();

View File

@ -60,7 +60,7 @@ namespace
#ifndef TORRENT_NO_DEPRECATE
torrent_handle add_torrent_depr(session& s, torrent_info const& ti
, boost::filesystem::path const& save, entry const& resume
, std::string const& save, entry const& resume
, storage_mode_t storage_mode, bool paused)
{
allow_threading_guard guard;
@ -89,7 +89,7 @@ namespace
name = extract<std::string>(params["name"]);
p.name = name.c_str();
}
p.save_path = fs::path(extract<std::string>(params["save_path"]));
p.save_path = extract<std::string>(params["save_path"]);
std::vector<char> resume_buf;
if (params.has_key("resume_data"))

View File

@ -286,11 +286,13 @@ void bind_torrent_handle()
int (torrent_handle::*piece_priority0)(int) const = &torrent_handle::piece_priority;
void (torrent_handle::*piece_priority1)(int, int) const = &torrent_handle::piece_priority;
void (torrent_handle::*move_storage0)(fs::path const&) const = &torrent_handle::move_storage;
void (torrent_handle::*move_storage1)(fs::wpath const&) const = &torrent_handle::move_storage;
void (torrent_handle::*move_storage0)(std::string const&) const = &torrent_handle::move_storage;
void (torrent_handle::*rename_file0)(int, std::string const&) const = &torrent_handle::rename_file;
void (torrent_handle::*rename_file0)(int, fs::path const&) const = &torrent_handle::rename_file;
void (torrent_handle::*rename_file1)(int, fs::wpath const&) const = &torrent_handle::rename_file;
#if TORRENT_USE_WSTRING
void (torrent_handle::*move_storage1)(std::wstring const&) const = &torrent_handle::move_storage;
void (torrent_handle::*rename_file1)(int, std::wstring const&) const = &torrent_handle::rename_file;
#endif
#ifndef TORRENT_DISABLE_RESOLVE_COUNTRIES
bool (torrent_handle::*resolve_countries0)() const = &torrent_handle::resolve_countries;
@ -370,11 +372,13 @@ void bind_torrent_handle()
.def("set_max_connections", _(&torrent_handle::set_max_connections))
.def("set_tracker_login", _(&torrent_handle::set_tracker_login))
.def("move_storage", _(move_storage0))
.def("move_storage", _(move_storage1))
.def("info_hash", _(&torrent_handle::info_hash))
.def("force_recheck", _(&torrent_handle::force_recheck))
.def("rename_file", _(rename_file0))
#if TORRENT_USE_WSTRING
.def("move_storage", _(move_storage1))
.def("rename_file", _(rename_file1))
#endif
;
enum_<torrent_handle::deadline_flags>("deadline_flags")

View File

@ -69,7 +69,7 @@ namespace
}
torrent_info construct0(std::string path) {
return torrent_info(fs::path(path));
return torrent_info(path);
}
list map_block(torrent_info& ti, int piece, size_type offset, int size)
@ -106,8 +106,8 @@ void bind_torrent_info()
#endif
.def(init<sha1_hash const&>())
.def(init<char const*, int>())
.def(init<boost::filesystem::path>())
.def(init<boost::filesystem::wpath>())
.def(init<std::string>())
.def(init<std::wstring>())
.def("add_tracker", &torrent_info::add_tracker, (arg("url"), arg("tier")=0))
.def("add_url_seed", &torrent_info::add_url_seed)

View File

@ -38,8 +38,6 @@ POSSIBILITY OF SUCH DAMAGE.
#pragma warning(push, 1)
#endif
#include <boost/filesystem/operations.hpp>
#include <boost/filesystem/convenience.hpp>
#include <boost/bind.hpp>
#ifdef _MSC_VER
@ -502,11 +500,6 @@ void print_peer_info(std::string& out, std::vector<libtorrent::peer_info> const&
typedef std::multimap<std::string, libtorrent::torrent_handle> handles_t;
using boost::bind;
using boost::filesystem::path;
using boost::filesystem::exists;
using boost::filesystem::directory_iterator;
using boost::filesystem::extension;
// monitored_dir is true if this torrent is added because
// it was found in the directory that is monitored. If it
@ -517,7 +510,7 @@ void add_torrent(libtorrent::session& ses
, std::string const& torrent
, float preferred_ratio
, bool compact_mode
, path const& save_path
, std::string const& save_path
, bool monitored_dir
, int torrent_upload_limit
, int torrent_download_limit)
@ -538,7 +531,7 @@ void add_torrent(libtorrent::session& ses
add_torrent_params p;
lazy_entry resume_data;
std::string filename = (save_path / (t->name() + ".resume")).string();
std::string filename = combine_path(save_path, t->name() + ".resume");
std::vector<char> buf;
if (load_file(filename.c_str(), buf) == 0)
@ -565,12 +558,12 @@ void add_torrent(libtorrent::session& ses
#endif
}
void scan_dir(path const& dir_path
void scan_dir(std::string const& dir_path
, libtorrent::session& ses
, handles_t& handles
, float preferred_ratio
, bool compact_mode
, path const& save_path
, std::string const& save_path
, int torrent_upload_limit
, int torrent_download_limit)
{
@ -578,10 +571,11 @@ void scan_dir(path const& dir_path
using namespace libtorrent;
for (directory_iterator i(dir_path), end; i != end; ++i)
error_code ec;
for (directory i(dir_path, ec); !i.done(); i.next(ec))
{
if (extension(*i) != ".torrent") continue;
std::string file = i->path().string();
std::string file = i.file();
if (extension(file) != ".torrent") continue;
handles_t::iterator k = handles.find(file);
if (k != handles.end())
@ -659,7 +653,7 @@ void print_alert(libtorrent::alert const* a, std::string& str)
fprintf(g_log_file, "[%s] %s\n", time_now_string(), a->message().c_str());
}
int save_file(boost::filesystem::path const& filename, std::vector<char>& v)
int save_file(std::string const& filename, std::vector<char>& v)
{
using namespace libtorrent;
@ -697,7 +691,7 @@ void handle_alert(libtorrent::session& ses, libtorrent::alert* a
{
std::vector<char> out;
bencode(std::back_inserter(out), *p->resume_data);
save_file(h.save_path() / (h.name() + ".resume"), out);
save_file(combine_path(h.save_path(), h.name() + ".resume"), out);
if (std::find_if(handles.begin(), handles.end()
, bind(&handles_t::value_type::second, _1) == h) == handles.end())
ses.remove_torrent(h);
@ -718,11 +712,6 @@ static char const* state_str[] =
int main(int argc, char* argv[])
{
#if BOOST_VERSION < 103400
using boost::filesystem::no_check;
path::default_name_check(no_check);
#endif
if (argc == 1)
{
fprintf(stderr, "usage: client_test [OPTIONS] [TORRENT|MAGNETURL]\n\n"
@ -818,10 +807,10 @@ int main(int argc, char* argv[])
int listen_port = 6881;
float preferred_ratio = 0.f;
std::string allocation_mode = "sparse";
boost::filesystem::path save_path(".");
std::string save_path(".");
int torrent_upload_limit = 0;
int torrent_download_limit = 0;
boost::filesystem::path monitor_dir;
std::string monitor_dir;
std::string bind_to_interface = "";
int poll_interval = 5;
@ -1097,7 +1086,7 @@ int main(int argc, char* argv[])
torrent_handle h = rd->handle;
std::vector<char> out;
bencode(std::back_inserter(out), *rd->resume_data);
save_file(h.save_path() / (h.name() + ".resume"), out);
save_file(combine_path(h.save_path(), h.name() + ".resume"), out);
}
break;
}
@ -1554,7 +1543,7 @@ int main(int argc, char* argv[])
, pad_file?esc("34"):""
, progress / 10.f
, add_suffix(file_progress[i]).c_str()
, info.file_at(i).path.leaf().c_str()
, filename(info.file_at(i).path).c_str()
, pad_file?esc("0"):"");
out += str;
}

View File

@ -40,22 +40,17 @@ POSSIBILITY OF SUCH DAMAGE.
#include "libtorrent/torrent_info.hpp"
#include "libtorrent/lazy_entry.hpp"
#include "libtorrent/magnet_uri.hpp"
#include <boost/filesystem/operations.hpp>
int main(int argc, char* argv[])
{
using namespace libtorrent;
using namespace boost::filesystem;
if (argc != 2)
{
std::cerr << "usage: dump_torrent torrent-file\n";
return 1;
}
#if BOOST_VERSION < 103400
boost::filesystem::path::default_name_check(boost::filesystem::no_check);
#endif
int size = file_size(argv[1]);
if (size > 10 * 1000000)
@ -126,9 +121,9 @@ int main(int argc, char* argv[])
<< (i->symlink_attribute?'l':'-')
<< " "
<< "[ " << std::setw(4) << first << ", " << std::setw(4) << last << " ]\t"
<< i->path.string() ;
<< i->path;
if (i->symlink_attribute)
std::cout << " -> " << i->symlink_path.string();
std::cout << " -> " << i->symlink_path;
std::cout << std::endl;
}

View File

@ -37,20 +37,18 @@ POSSIBILITY OF SUCH DAMAGE.
#include "libtorrent/storage.hpp"
#include "libtorrent/hasher.hpp"
#include "libtorrent/create_torrent.hpp"
#include "libtorrent/file.hpp"
#include <boost/filesystem/operations.hpp>
#include <boost/filesystem/path.hpp>
#include <boost/bind.hpp>
using namespace boost::filesystem;
using namespace libtorrent;
// do not include files and folders whose
// name starts with a .
bool file_filter(boost::filesystem::path const& filename)
bool file_filter(std::string const& f)
{
if (filename.leaf()[0] == '.') return false;
fprintf(stderr, "%s\n", filename.string().c_str());
if (filename(f)[0] == '.') return false;
fprintf(stderr, "%s\n", f.c_str());
return true;
}
@ -82,7 +80,6 @@ void print_usage()
int main(int argc, char* argv[])
{
using namespace libtorrent;
using namespace boost::filesystem;
char const* creator_str = "libtorrent";
@ -140,7 +137,7 @@ int main(int argc, char* argv[])
file_storage fs;
file_pool fp;
path full_path = complete(path(argv[1]));
std::string full_path = libtorrent::complete(argv[1]);
add_files(fs, full_path, file_filter);
@ -154,7 +151,7 @@ int main(int argc, char* argv[])
t.add_url_seed(*i);
error_code ec;
set_piece_hashes(t, full_path.branch_path()
set_piece_hashes(t, parent_path(full_path)
, boost::bind(&print_progress, _1, t.num_pieces()), ec);
if (ec)
{

View File

@ -42,10 +42,6 @@ POSSIBILITY OF SUCH DAMAGE.
int main(int argc, char* argv[])
{
using namespace libtorrent;
#if BOOST_VERSION < 103400
namespace fs = boost::filesystem;
fs::path::default_name_check(fs::no_check);
#endif
if (argc != 2)
{

View File

@ -50,7 +50,6 @@ POSSIBILITY OF SUCH DAMAGE.
#pragma warning(push, 1)
#endif
#include <boost/filesystem/path.hpp>
#include <boost/pool/object_pool.hpp>
#ifdef _MSC_VER
@ -87,7 +86,6 @@ POSSIBILITY OF SUCH DAMAGE.
namespace libtorrent
{
namespace fs = boost::filesystem;
class upnp;
class natpmp;
class lsd;
@ -129,7 +127,7 @@ namespace libtorrent
, fingerprint const& cl_fprint
, char const* listen_interface
#if defined TORRENT_VERBOSE_LOGGING || defined TORRENT_LOGGING || defined TORRENT_ERROR_LOGGING
, fs::path const& logpath
, std::string const& logpath
#endif
);
~session_impl();
@ -714,7 +712,7 @@ namespace libtorrent
// whe shutting down process
std::list<boost::shared_ptr<tracker_logger> > m_tracker_loggers;
fs::path m_logpath;
std::string m_logpath;
public:
boost::shared_ptr<logger> m_logger;
private:

View File

@ -120,7 +120,7 @@ POSSIBILITY OF SUCH DAMAGE.
#endif
// set up defines for target environments
#if (defined __APPLE__ && __MACH__) || defined __FreeBSD__ || defined __NetBSD__ \
#if (defined __APPLE__ && defined __MACH__) || defined __FreeBSD__ || defined __NetBSD__ \
|| defined __OpenBSD__ || defined __bsdi__ || defined __DragonFly__ \
|| defined __FreeBSD_kernel__
#define TORRENT_BSD
@ -145,14 +145,6 @@ POSSIBILITY OF SUCH DAMAGE.
#define TORRENT_USE_I2P 1
// should wpath or path be used?
#if defined UNICODE && !defined BOOST_FILESYSTEM_NARROW_ONLY \
&& BOOST_VERSION >= 103400 && !defined __APPLE__
#define TORRENT_USE_WPATH 1
#else
#define TORRENT_USE_WPATH 0
#endif
// set this to 1 to disable all floating point operations
// (disables some float-dependent APIs)
#define TORRENT_NO_FPU 0
@ -209,13 +201,22 @@ inline int snprintf(char* buf, int len, char const* fmt, ...)
#define TORRENT_UPNP_LOGGING
#endif
#if !TORRENT_USE_WPATH && defined TORRENT_LINUX
// libiconv presnce, not implemented yet
#define TORRENT_USE_LOCALE_FILENAMES 1
// windows has its own functions to convert
// apple uses utf-8 as its locale, so no conversion
// is necessary
#if !defined TORRENT_WINDOWS && !defined __APPLE__
// libiconv presence, not implemented yet
#define TORRENT_USE_ICONV 1
#else
#define TORRENT_USE_LOCALE_FILENAMES 0
#define TORRENT_ISE_ICONV 0
#endif
#if defined UNICODE
#define TORRENT_USE_WSTRING 1
#else
#define TORRENT_USE_WSTRING 0
#endif // TORRENT_WINDOWS
#if !defined(TORRENT_READ_HANDLER_MAX_SIZE)
# define TORRENT_READ_HANDLER_MAX_SIZE 256
#endif

View File

@ -42,6 +42,7 @@ POSSIBILITY OF SUCH DAMAGE.
#include "libtorrent/hasher.hpp"
#include "libtorrent/utf8.hpp"
#include "libtorrent/allocator.hpp"
#include "libtorrent/file.hpp" // for combine_path etc.
#include <vector>
#include <string>
@ -51,8 +52,6 @@ POSSIBILITY OF SUCH DAMAGE.
#pragma warning(push, 1)
#endif
#include <boost/filesystem/path.hpp>
#include <boost/filesystem/operations.hpp>
#include <boost/optional.hpp>
#include <boost/date_time/posix_time/posix_time_types.hpp>
#include <boost/scoped_ptr.hpp>
@ -64,7 +63,6 @@ POSSIBILITY OF SUCH DAMAGE.
namespace libtorrent
{
namespace fs = boost::filesystem;
namespace pt = boost::posix_time;
class torrent_info;
@ -147,94 +145,64 @@ namespace libtorrent
namespace detail
{
inline bool default_pred(boost::filesystem::path const&) { return true; }
#if TORRENT_USE_WPATH
inline bool wdefault_pred(boost::filesystem::wpath const&) { return true; }
#endif
inline bool default_pred(std::string const&) { return true; }
inline bool ignore_subdir(std::string const& leaf)
{ return leaf == ".." || leaf == "."; }
inline bool ignore_subdir(std::wstring const& leaf)
{ return leaf == L".." || leaf == L"."; }
inline void nop(int i) {}
int TORRENT_EXPORT get_file_attributes(boost::filesystem::path const& p);
#if TORRENT_USE_WPATH
int TORRENT_EXPORT get_file_attributes(boost::filesystem::wpath const& p);
#endif
int TORRENT_EXPORT get_file_attributes(std::string const& p);
std::string TORRENT_EXPORT get_symlink_path(std::string const& p);
std::time_t TORRENT_EXPORT get_file_mtime(boost::filesystem::path const& p);
#if TORRENT_USE_WPATH
std::time_t TORRENT_EXPORT get_file_mtime(boost::filesystem::wpath const& p);
#endif
fs::path TORRENT_EXPORT get_symlink_path(boost::filesystem::path const& p);
#if TORRENT_USE_WPATH
fs::path TORRENT_EXPORT get_symlink_path(boost::filesystem::wpath const& p);
#endif
template <class Pred, class Str, class PathTraits>
void add_files_impl(file_storage& fs, boost::filesystem::basic_path<Str, PathTraits> const& p
, boost::filesystem::basic_path<Str, PathTraits> const& l, Pred pred)
template <class Pred>
void add_files_impl(file_storage& fs, std::string const& p
, std::string const& l, Pred pred)
{
using boost::filesystem::basic_path;
using boost::filesystem::basic_directory_iterator;
if (!pred(l)) return;
basic_path<Str, PathTraits> f(p / l);
if (is_directory(f))
std::string f = combine_path(p, l);
error_code ec;
file_status s;
stat_file(f, &s, ec);
if (ec) return;
if (s.mode & file_status::directory)
{
for (basic_directory_iterator<basic_path<Str, PathTraits> > i(f), end; i != end; ++i)
for (directory i(f, ec); !i.done(); i.next(ec))
{
#if BOOST_VERSION < 103600
Str const& leaf = i->path().leaf();
#else
Str const& leaf = i->path().filename();
#endif
std::string leaf = i.file();
if (ignore_subdir(leaf)) continue;
add_files_impl(fs, p, l / leaf, pred);
add_files_impl(fs, p, combine_path(l, leaf), pred);
}
}
else
{
// #error use the fields from s
int file_flags = get_file_attributes(f);
std::time_t mtime = get_file_mtime(f);
//Masking all bits to check if the file is a symlink
if(file_flags & file_storage::attribute_symlink)
// mask all bits to check if the file is a symlink
if (file_flags & file_storage::attribute_symlink)
{
fs::path sym_path = get_symlink_path(f);
fs.add_file(l, 0 ,file_flags, mtime, sym_path);
std::string sym_path = get_symlink_path(f);
fs.add_file(l, 0, file_flags, s.mtime, sym_path);
}
else
{
fs.add_file(l, file_size(f), file_flags, mtime);
fs.add_file(l, s.file_size, file_flags, s.mtime);
}
}
}
}
// path versions
template <class Pred>
void add_files(file_storage& fs, boost::filesystem::path const& file, Pred p)
void add_files(file_storage& fs, std::string const& file, Pred p)
{
using boost::filesystem::path;
#if BOOST_VERSION < 103600
detail::add_files_impl(fs, complete(file).branch_path(), path(file.leaf()), p);
#else
detail::add_files_impl(fs, complete(file).parent_path(), path(file.filename()), p);
#endif
detail::add_files_impl(fs, parent_path(complete(file)), filename(file), p);
}
inline void add_files(file_storage& fs, boost::filesystem::path const& file)
inline void add_files(file_storage& fs, std::string const& file)
{
using boost::filesystem::path;
#if BOOST_VERSION < 103600
detail::add_files_impl(fs, complete(file).branch_path(), path(file.leaf()), detail::default_pred);
#else
detail::add_files_impl(fs, complete(file).parent_path(), path(file.filename()), detail::default_pred);
#endif
detail::add_files_impl(fs, parent_path(complete(file)), filename(file), detail::default_pred);
}
struct piece_holder
@ -247,7 +215,7 @@ namespace libtorrent
};
template <class Fun>
void set_piece_hashes(create_torrent& t, boost::filesystem::path const& p, Fun f
void set_piece_hashes(create_torrent& t, std::string const& p, Fun f
, error_code& ec)
{
file_pool fp;
@ -275,14 +243,14 @@ namespace libtorrent
#ifndef BOOST_NO_EXCEPTIONS
template <class Fun>
void set_piece_hashes(create_torrent& t, boost::filesystem::path const& p, Fun f)
void set_piece_hashes(create_torrent& t, std::string const& p, Fun f)
{
error_code ec;
set_piece_hashes(t, p, f, ec);
if (ec) throw libtorrent_exception(ec);
}
inline void set_piece_hashes(create_torrent& t, boost::filesystem::path const& p)
inline void set_piece_hashes(create_torrent& t, std::string const& p)
{
error_code ec;
set_piece_hashes(t, p, detail::nop, ec);
@ -290,42 +258,36 @@ namespace libtorrent
}
#endif
inline void set_piece_hashes(create_torrent& t, boost::filesystem::path const& p, error_code& ec)
inline void set_piece_hashes(create_torrent& t, std::string const& p, error_code& ec)
{
set_piece_hashes(t, p, detail::nop, ec);
}
#if TORRENT_USE_WPATH
// wpath versions
#if TORRENT_USE_WSTRING
// wstring versions
template <class Pred>
void add_files(file_storage& fs, boost::filesystem::wpath const& file, Pred p)
void add_files(file_storage& fs, std::wstring const& wfile, Pred p)
{
using boost::filesystem::wpath;
#if BOOST_VERSION < 103600
detail::add_files_impl(fs, complete(file).branch_path(), wpath(file.leaf()), p);
#else
detail::add_files_impl(fs, complete(file).parent_path(), wpath(file.filename()), p);
#endif
std::string utf8;
wchar_utf8(wfile, utf8);
detail::add_files_impl(fs, parent_path(complete(utf8)), filename(utf8), p);
}
inline void add_files(file_storage& fs, boost::filesystem::wpath const& file)
inline void add_files(file_storage& fs, std::wstring const& wfile)
{
using boost::filesystem::wpath;
#if BOOST_VERSION < 103600
detail::add_files_impl(fs, complete(file).branch_path(), wpath(file.leaf()), detail::wdefault_pred);
#else
detail::add_files_impl(fs, complete(file).parent_path(), wpath(file.filename()), detail::wdefault_pred);
#endif
std::string utf8;
wchar_utf8(wfile, utf8);
detail::add_files_impl(fs, parent_path(complete(utf8)), filename(utf8), detail::default_pred);
}
template <class Fun>
void set_piece_hashes(create_torrent& t, boost::filesystem::wpath const& p, Fun f
void set_piece_hashes(create_torrent& t, std::wstring const& p, Fun f
, error_code& ec)
{
file_pool fp;
std::string utf8;
wchar_utf8(p.string(), utf8);
wchar_utf8(p, utf8);
boost::scoped_ptr<storage_interface> st(
default_storage_constructor(const_cast<file_storage&>(t.files()), 0, utf8, fp));
@ -350,14 +312,14 @@ namespace libtorrent
#ifndef BOOST_NO_EXCEPTIONS
template <class Fun>
void set_piece_hashes(create_torrent& t, boost::filesystem::wpath const& p, Fun f)
void set_piece_hashes(create_torrent& t, std::wstring const& p, Fun f)
{
error_code ec;
set_piece_hashes(t, p, f, ec);
if (ec) throw libtorrent_exception(ec);
}
inline void set_piece_hashes(create_torrent& t, boost::filesystem::wpath const& p)
inline void set_piece_hashes(create_torrent& t, std::wstring const& p)
{
error_code ec;
set_piece_hashes(t, p, detail::nop, ec);
@ -365,7 +327,7 @@ namespace libtorrent
}
#endif
inline void set_piece_hashes(create_torrent& t, boost::filesystem::wpath const& p, error_code& ec)
inline void set_piece_hashes(create_torrent& t, std::wstring const& p, error_code& ec)
{
set_piece_hashes(t, p, detail::nop, ec);
}
@ -375,3 +337,4 @@ namespace libtorrent
}
#endif

View File

@ -35,33 +35,21 @@ POSSIBILITY OF SUCH DAMAGE.
#include <string>
#include "libtorrent/config.hpp"
#include "libtorrent/file.hpp"
#if TORRENT_USE_IOSTREAM
#include <fstream>
#include <iostream>
#endif
#ifdef _MSC_VER
#pragma warning(push, 1)
#endif
#include <boost/filesystem/fstream.hpp>
#include <boost/filesystem/convenience.hpp>
#ifdef _MSC_VER
#pragma warning(pop)
#endif
namespace libtorrent
{
// DEBUG API
namespace fs = boost::filesystem;
struct logger
{
logger(fs::path const& logpath, fs::path const& filename, int instance, bool append = true)
logger(std::string const& logpath, std::string const& filename
, int instance, bool append = true)
{
#if TORRENT_USE_IOSTREAM
@ -71,15 +59,17 @@ namespace libtorrent
#endif
char log_name[256];
snprintf(log_name, sizeof(log_name), "libtorrent_logs%d", instance);
fs::path dir(fs::complete(logpath / log_name));
if (!fs::exists(dir)) fs::create_directories(dir);
m_file.open((dir / filename).string().c_str(), std::ios_base::out | (append ? std::ios_base::app : std::ios_base::out));
std::string dir(complete(combine_path(logpath, log_name)));
error_code ec;
if (!exists(dir)) create_directories(dir, ec);
m_file.open(combine_path(dir, filename).c_str()
, std::ios_base::out | (append ? std::ios_base::app : std::ios_base::out));
*this << "\n\n\n*** starting log ***\n";
#ifndef BOOST_NO_EXCEPTIONS
}
catch (std::exception& e)
{
std::cerr << "failed to create log '" << filename.string() << "': " << e.what() << std::endl;
std::cerr << "failed to create log '" << filename << "': " << e.what() << std::endl;
}
#endif
#endif

View File

@ -44,6 +44,7 @@ POSSIBILITY OF SUCH DAMAGE.
namespace libtorrent
{
boost::array<char, 3 + std::numeric_limits<size_type>::digits10> TORRENT_EXPORT to_string(size_type n);
bool TORRENT_EXPORT is_alpha(char c);
bool TORRENT_EXPORT is_digit(char c);
bool TORRENT_EXPORT is_print(char c);
bool TORRENT_EXPORT is_space(char c);
@ -80,16 +81,18 @@ namespace libtorrent
TORRENT_EXPORT void to_hex(char const *in, int len, char* out);
TORRENT_EXPORT bool from_hex(char const *in, int len, char* out);
#if TORRENT_USE_WPATH
#if defined TORRENT_WINDOWS && defined UNICODE
TORRENT_EXPORT std::wstring convert_to_wstring(std::string const& s);
TORRENT_EXPORT std::string convert_from_wstring(std::wstring const& s);
#endif
#if defined TORRENT_WINDOWS || TORRENT_USE_LOCALE_FILENAMES
#if defined TORRENT_WINDOWS || TORRENT_USE_ICONV
TORRENT_EXPORT std::string convert_to_native(std::string const& s);
TORRENT_EXPORT std::string convert_from_native(std::string const& s);
#else
inline std::string const& convert_to_native(std::string const& s) { return s; }
inline std::string const& convert_from_native(std::string const& s) { return s; }
#endif
}
#endif // TORRENT_ESCAPE_STRING_HPP_INCLUDED

View File

@ -42,7 +42,6 @@ POSSIBILITY OF SUCH DAMAGE.
#endif
#include <boost/noncopyable.hpp>
#include <boost/filesystem/path.hpp>
#ifdef _MSC_VER
#pragma warning(pop)
@ -59,6 +58,8 @@ POSSIBILITY OF SUCH DAMAGE.
#endif
#include <windows.h>
#include <winioctl.h>
#include <sys/types.h>
#include <sys/stat.h>
#else
// posix part
#define _FILE_OFFSET_BITS 64
@ -76,10 +77,88 @@ POSSIBILITY OF SUCH DAMAGE.
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <dirent.h> // for DIR
#endif
namespace libtorrent
{
namespace fs = boost::filesystem;
struct file_status
{
size_type file_size;
time_t atime;
time_t mtime;
time_t ctime;
enum {
#if defined TORRENT_WINDOWS
directory = _S_IFDIR,
regular_file = _S_IFREG
#else
fifo = S_IFIFO,
character_special = S_IFCHR,
directory = S_IFDIR,
block_special = S_IFBLK,
regular_file = S_IFREG,
link = S_IFLNK,
socket = S_IFSOCK
#endif
} modes_t;
int mode;
};
TORRENT_EXPORT void stat_file(std::string const& f, file_status* s
, error_code& ec);
TORRENT_EXPORT void rename(std::string const& f
, std::string const& newf, error_code& ec);
TORRENT_EXPORT void create_directories(std::string const& f
, error_code& ec);
TORRENT_EXPORT void create_directory(std::string const& f
, error_code& ec);
TORRENT_EXPORT void remove_all(std::string const& f
, error_code& ec);
TORRENT_EXPORT void remove(std::string const& f, error_code& ec);
TORRENT_EXPORT bool exists(std::string const& f);
TORRENT_EXPORT size_type file_size(std::string const& f);
TORRENT_EXPORT bool is_directory(std::string const& f
, error_code& ec);
TORRENT_EXPORT void copy_file(std::string const& f
, std::string const& newf, error_code& ec);
TORRENT_EXPORT std::string split_path(std::string const& f);
TORRENT_EXPORT char const* next_path_element(char const* p);
TORRENT_EXPORT std::string extension(std::string const& f);
TORRENT_EXPORT void replace_extension(std::string& f, std::string const& ext);
TORRENT_EXPORT bool is_root_path(std::string const& f);
TORRENT_EXPORT std::string parent_path(std::string const& f);
TORRENT_EXPORT bool has_parent_path(std::string const& f);
TORRENT_EXPORT std::string filename(std::string const& f);
TORRENT_EXPORT std::string combine_path(std::string const& lhs
, std::string const& rhs);
TORRENT_EXPORT std::string complete(std::string const& f);
TORRENT_EXPORT bool is_complete(std::string const& f);
TORRENT_EXPORT std::string current_working_directory();
class TORRENT_EXPORT directory : public boost::noncopyable
{
public:
directory(std::string const& path, error_code& ec);
~directory();
void next(error_code& ec);
std::string file() const;
bool done() const { return m_done; }
private:
#ifdef TORRENT_WINDOWS
HANDLE m_handle;
WIN32_FIND_DATA m_fd;
#else
DIR* m_handle;
// the dirent struct contains a zero-sized
// array at the end, it will end up referring
// to the m_name field
struct dirent m_dirent;
char m_name[NAME_MAX + 1]; // +1 to make room for null
#endif
bool m_done;
};
class TORRENT_EXPORT file: public boost::noncopyable
{
@ -124,10 +203,10 @@ namespace libtorrent
#endif
file();
file(fs::path const& p, int m, error_code& ec);
file(std::string const& p, int m, error_code& ec);
~file();
bool open(fs::path const& p, int m, error_code& ec);
bool open(std::string const& p, int m, error_code& ec);
bool is_open() const;
void close();
bool set_size(size_type size, error_code& ec);
@ -163,14 +242,15 @@ namespace libtorrent
#ifdef TORRENT_WINDOWS
HANDLE m_file_handle;
#if TORRENT_USE_WPATH
#ifdef UNICODE
std::wstring m_path;
#else
std::string m_path;
#endif
#else
#endif // UNICODE
#else // TORRENT_WINDOWS
int m_fd;
#endif
#endif // TORRENT_WINDOWS
#if defined TORRENT_WINDOWS || defined TORRENT_LINUX || defined TORRENT_DEBUG
static void init_file();
static int m_page_size;

View File

@ -37,7 +37,6 @@ POSSIBILITY OF SUCH DAMAGE.
#pragma warning(push, 1)
#endif
#include <boost/filesystem/path.hpp>
#include <boost/shared_ptr.hpp>
#ifdef _MSC_VER
@ -51,16 +50,14 @@ POSSIBILITY OF SUCH DAMAGE.
namespace libtorrent
{
namespace fs = boost::filesystem;
struct TORRENT_EXPORT file_pool : boost::noncopyable
{
file_pool(int size = 40): m_size(size) {}
boost::shared_ptr<file> open_file(void* st, fs::path const& p
boost::shared_ptr<file> open_file(void* st, std::string const& p
, int m, error_code& ec);
void release(void* st);
void release(fs::path const& p);
void release(std::string const& p);
void resize(int size);
int size_limit() const { return m_size; }

View File

@ -37,24 +37,12 @@ POSSIBILITY OF SUCH DAMAGE.
#include <vector>
#include <ctime>
#ifdef _MSC_VER
#pragma warning(push, 1)
#endif
#include <boost/filesystem/path.hpp>
#ifdef _MSC_VER
#pragma warning(pop)
#endif
#include "libtorrent/size_type.hpp"
#include "libtorrent/assert.hpp"
#include "libtorrent/peer_request.hpp"
namespace libtorrent
{
namespace fs = boost::filesystem;
struct TORRENT_EXPORT file_entry
{
file_entry(): offset(0), size(0), file_base(0)
@ -63,7 +51,7 @@ namespace libtorrent
, symlink_attribute(false)
{}
fs::path path;
std::string path;
size_type offset; // the offset of this file inside the torrent
size_type size; // the size of this file
// the offset in the file where the storage starts.
@ -75,7 +63,7 @@ namespace libtorrent
bool hidden_attribute:1;
bool executable_attribute:1;
bool symlink_attribute:1;
fs::path symlink_path;
std::string symlink_path;
};
struct TORRENT_EXPORT file_slice
@ -103,11 +91,13 @@ namespace libtorrent
};
void add_file(file_entry const& e);
void add_file(fs::path const& p, size_type size, int flags = 0, std::time_t mtime = 0, fs::path const& s_p = "");
void add_file(std::string const& p, size_type size, int flags = 0
, std::time_t mtime = 0, std::string const& s_p = "");
void rename_file(int index, std::string const& new_filename);
#ifndef BOOST_FILESYSTEM_NARROW_ONLY
void add_file(fs::wpath const& p, size_type size, int flags = 0, std::time_t mtime = 0, fs::path const& s_p = "");
#if TORRENT_USE_WSTRING
void add_file(std::wstring const& p, size_type size, int flags = 0
, std::time_t mtime = 0, std::string const& s_p = "");
void rename_file(int index, std::wstring const& new_filename);
void set_name(std::wstring const& n);
#endif

View File

@ -41,7 +41,6 @@ POSSIBILITY OF SUCH DAMAGE.
#include <boost/bind.hpp>
#include <boost/ref.hpp>
#include <boost/optional.hpp>
#include <boost/filesystem/operations.hpp>
#include <boost/intrusive_ptr.hpp>
#include <boost/detail/atomic_count.hpp>

View File

@ -37,12 +37,9 @@ POSSIBILITY OF SUCH DAMAGE.
#include "libtorrent/config.hpp"
#include "libtorrent/torrent_handle.hpp"
#include "libtorrent/session.hpp"
#include <boost/filesystem/path.hpp>
namespace libtorrent
{
namespace fs = boost::filesystem;
struct torrent_handle;
std::string TORRENT_EXPORT make_magnet_uri(torrent_handle const& handle);
@ -52,7 +49,7 @@ namespace libtorrent
#ifndef TORRENT_NO_DEPRECATE
// deprecated in 0.14
torrent_handle TORRENT_EXPORT add_magnet_uri(session& ses, std::string const& uri
, fs::path const& save_path
, std::string const& save_path
, storage_mode_t storage_mode = storage_mode_sparse
, bool paused = false
, storage_constructor_type sc = default_storage_constructor

View File

@ -46,11 +46,6 @@ POSSIBILITY OF SUCH DAMAGE.
#include <boost/date_time/posix_time/ptime.hpp>
#include <boost/detail/atomic_count.hpp>
#include <boost/enable_shared_from_this.hpp>
#include <boost/filesystem/convenience.hpp>
#include <boost/filesystem/exception.hpp>
#include <boost/filesystem/fstream.hpp>
#include <boost/filesystem/operations.hpp>
#include <boost/filesystem/path.hpp>
#include <boost/function.hpp>
#include <boost/integer_traits.hpp>
#include <boost/intrusive_ptr.hpp>

View File

@ -41,7 +41,6 @@ POSSIBILITY OF SUCH DAMAGE.
#endif
#include <boost/limits.hpp>
#include <boost/filesystem/path.hpp>
#ifdef _MSC_VER
#pragma warning(pop)
@ -115,8 +114,6 @@ namespace libtorrent
TORRENT_LINK_TEST_NAME();
}
namespace fs = boost::filesystem;
session_settings min_memory_usage();
session_settings high_performance_seed();
@ -140,12 +137,6 @@ namespace libtorrent
struct eh_initializer {};
#endif
struct session_impl;
struct filesystem_init
{
filesystem_init();
};
}
class TORRENT_EXPORT session_proxy
@ -180,7 +171,7 @@ namespace libtorrent
char const* tracker_url;
sha1_hash info_hash;
char const* name;
fs::path save_path;
std::string save_path;
std::vector<char>* resume_data;
storage_mode_t storage_mode;
bool paused;
@ -202,7 +193,7 @@ namespace libtorrent
, int flags = start_default_features | add_default_plugins
, int alert_mask = alert::error_notification
#if defined TORRENT_VERBOSE_LOGGING || defined TORRENT_LOGGING || defined TORRENT_ERROR_LOGGING
, fs::path logpath = "."
, std::string logpath = "."
#endif
);
session(
@ -212,7 +203,7 @@ namespace libtorrent
, int flags = start_default_features | add_default_plugins
, int alert_mask = alert::error_notification
#if defined TORRENT_VERBOSE_LOGGING || defined TORRENT_LOGGING || defined TORRENT_ERROR_LOGGING
, fs::path logpath = "."
, std::string logpath = "."
#endif
);
@ -235,7 +226,7 @@ namespace libtorrent
TORRENT_DEPRECATED_PREFIX
torrent_handle add_torrent(
torrent_info const& ti
, fs::path const& save_path
, std::string const& save_path
, entry const& resume_data = entry()
, storage_mode_t storage_mode = storage_mode_sparse
, bool paused = false
@ -245,7 +236,7 @@ namespace libtorrent
TORRENT_DEPRECATED_PREFIX
torrent_handle add_torrent(
boost::intrusive_ptr<torrent_info> ti
, fs::path const& save_path
, std::string const& save_path
, entry const& resume_data = entry()
, storage_mode_t storage_mode = storage_mode_sparse
, bool paused = false
@ -258,7 +249,7 @@ namespace libtorrent
char const* tracker_url
, sha1_hash const& info_hash
, char const* name
, fs::path const& save_path
, std::string const& save_path
, entry const& resume_data = entry()
, storage_mode_t storage_mode = storage_mode_sparse
, bool paused = false
@ -422,10 +413,6 @@ namespace libtorrent
private:
// just a way to initialize boost.filesystem
// before the session_impl is created
aux::filesystem_init m_dummy;
// data shared between the main thread
// and the working thread
boost::shared_ptr<aux::session_impl> m_impl;

View File

@ -34,6 +34,8 @@ POSSIBILITY OF SUCH DAMAGE.
#define TORRENT_STORAGE_HPP_INCLUDE
#include <vector>
#include <sys/types.h>
#include <sys/stat.h>
#ifdef _MSC_VER
#pragma warning(push, 1)
@ -43,7 +45,6 @@ POSSIBILITY OF SUCH DAMAGE.
#include <boost/limits.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/intrusive_ptr.hpp>
#include <boost/filesystem/path.hpp>
#ifdef _MSC_VER
#pragma warning(pop)
@ -67,8 +68,6 @@ namespace libtorrent
struct piece_checker_data;
}
namespace fs = boost::filesystem;
class session;
struct file_pool;
struct disk_io_job;
@ -83,11 +82,11 @@ namespace libtorrent
TORRENT_EXPORT std::vector<std::pair<size_type, std::time_t> > get_filesizes(
file_storage const& t
, fs::path p);
, std::string const& p);
TORRENT_EXPORT bool match_filesizes(
file_storage const& t
, fs::path p
, std::string const& p
, std::vector<std::pair<size_type, std::time_t> > const& sizes
, bool compact_mode
, std::string* error = 0);
@ -138,7 +137,7 @@ namespace libtorrent
virtual int sparse_end(int start) const { return start; }
// non-zero return value indicates an error
virtual bool move_storage(fs::path save_path) = 0;
virtual bool move_storage(std::string const& save_path) = 0;
// verify storage dependent fast resume entries
virtual bool verify_resume_data(lazy_entry const& rd, error_code& error) = 0;
@ -172,9 +171,9 @@ namespace libtorrent
disk_buffer_pool* disk_pool() { return m_disk_pool; }
session_settings const& settings() const { return *m_settings; }
void set_error(boost::filesystem::path const& file, error_code const& ec) const
void set_error(std::string const& file, error_code const& ec) const
{
m_error_file = file.string();
m_error_file = file;
m_error = ec;
}
@ -192,13 +191,13 @@ namespace libtorrent
};
typedef storage_interface* (*storage_constructor_type)(
file_storage const&, file_storage const*, fs::path const&, file_pool&);
file_storage const&, file_storage const*, std::string const&, file_pool&);
TORRENT_EXPORT storage_interface* default_storage_constructor(
file_storage const&, file_storage const* mapped, fs::path const&, file_pool&);
file_storage const&, file_storage const* mapped, std::string const&, file_pool&);
TORRENT_EXPORT storage_interface* disabled_storage_constructor(
file_storage const&, file_storage const* mapped, fs::path const&, file_pool&);
file_storage const&, file_storage const* mapped, std::string const&, file_pool&);
struct disk_io_thread;
@ -213,7 +212,7 @@ namespace libtorrent
piece_manager(
boost::shared_ptr<void> const& torrent
, boost::intrusive_ptr<torrent_info const> info
, fs::path const& path
, std::string const& path
, file_pool& fp
, disk_io_thread& io
, storage_constructor_type sc
@ -265,7 +264,7 @@ namespace libtorrent
boost::function<void(int, disk_io_job const&)> const& handler
= boost::function<void(int, disk_io_job const&)>());
void async_move_storage(fs::path const& p
void async_move_storage(std::string const& p
, boost::function<void(int, disk_io_job const&)> const& handler);
void async_save_resume_data(
@ -284,7 +283,7 @@ namespace libtorrent
private:
fs::path save_path() const;
std::string save_path() const;
bool verify_resume_data(lazy_entry const& rd, error_code& error)
{ return m_storage->verify_resume_data(rd, error); }
@ -363,7 +362,7 @@ namespace libtorrent
int rename_file_impl(int index, std::string const& new_filename)
{ return m_storage->rename_file(index, new_filename); }
int move_storage_impl(fs::path const& save_path);
int move_storage_impl(std::string const& save_path);
int allocate_slot_for_piece(int piece_index);
#ifdef TORRENT_DEBUG
@ -403,7 +402,7 @@ namespace libtorrent
// it can either be 'unassigned' or 'unallocated'
std::vector<int> m_slot_to_piece;
fs::path m_save_path;
std::string m_save_path;
mutable mutex m_mutex;

View File

@ -43,7 +43,6 @@ POSSIBILITY OF SUCH DAMAGE.
#endif
#include <boost/limits.hpp>
#include <boost/filesystem/path.hpp>
#include <boost/tuple/tuple.hpp>
#include <boost/enable_shared_from_this.hpp>
#include <boost/scoped_ptr.hpp>
@ -110,8 +109,6 @@ namespace libtorrent
{ return url < e.url ? true : type < e.type; }
};
namespace fs = boost::filesystem;
// a torrent is a class that holds information
// for a specific download. It updates itself against
// the tracker
@ -599,7 +596,7 @@ namespace libtorrent
- m_picker->num_have() - m_picker->num_filtered() == 0;
}
fs::path save_path() const;
std::string save_path() const;
alert_manager& alerts() const;
piece_picker& picker()
{
@ -654,7 +651,7 @@ namespace libtorrent
void set_max_connections(int limit);
int max_connections() const { return m_max_connections; }
void move_storage(fs::path const& save_path);
void move_storage(std::string const& save_path);
// renames the file with the given index to the new name
// the name may include a directory path
@ -923,7 +920,7 @@ namespace libtorrent
// are opened through
tcp::endpoint m_net_interface;
fs::path m_save_path;
std::string m_save_path;
// each bit represents a piece. a set bit means
// the piece has had its hash verified. This

View File

@ -56,8 +56,6 @@ POSSIBILITY OF SUCH DAMAGE.
namespace libtorrent
{
namespace fs = boost::filesystem;
namespace aux
{
struct session_impl;
@ -552,7 +550,7 @@ namespace libtorrent
// the ratio is uploaded / downloaded. less than 1 is not allowed
void set_ratio(float up_down_ratio) const;
fs::path save_path() const;
std::string save_path() const;
// -1 means unlimited unchokes
void set_max_uploads(int max_uploads) const;
@ -566,12 +564,12 @@ namespace libtorrent
, std::string const& password) const;
// post condition: save_path() == save_path if true is returned
void move_storage(fs::path const& save_path) const;
void rename_file(int index, fs::path const& new_name) const;
void move_storage(std::string const& save_path) const;
void rename_file(int index, std::string const& new_name) const;
#ifndef BOOST_FILESYSTEM_NARROW_ONLY
void move_storage(fs::wpath const& save_path) const;
void rename_file(int index, fs::wpath const& new_name) const;
#if TORRENT_USE_WSTRING
void move_storage(std::wstring const& save_path) const;
void rename_file(int index, std::wstring const& new_name) const;
#endif
bool super_seeding() const;

View File

@ -42,7 +42,6 @@ POSSIBILITY OF SUCH DAMAGE.
#endif
#include <boost/optional.hpp>
#include <boost/filesystem/path.hpp>
#include <boost/shared_array.hpp>
#ifdef _MSC_VER
@ -64,7 +63,6 @@ namespace libtorrent
{
namespace pt = boost::posix_time;
namespace gr = boost::gregorian;
namespace fs = boost::filesystem;
enum
{
@ -169,7 +167,7 @@ namespace libtorrent
typedef libtorrent_exception invalid_torrent_file;
#endif
int TORRENT_EXPORT load_file(fs::path const& filename, std::vector<char>& v);
int TORRENT_EXPORT load_file(std::string const& filename, std::vector<char>& v);
class TORRENT_EXPORT torrent_info : public intrusive_ptr_base<torrent_info>
{
@ -177,19 +175,19 @@ namespace libtorrent
#ifndef BOOST_NO_EXCEPTIONS
torrent_info(lazy_entry const& torrent_file);
torrent_info(char const* buffer, int size);
torrent_info(fs::path const& filename);
#ifndef BOOST_FILESYSTEM_NARROW_ONLY
torrent_info(fs::wpath const& filename);
#endif
torrent_info(std::string const& filename);
#if TORRENT_USE_WSTRING
torrent_info(std::wstring const& filename);
#endif // TORRENT_USE_WSTRING
#endif
torrent_info(sha1_hash const& info_hash);
torrent_info(lazy_entry const& torrent_file, error_code& ec);
torrent_info(char const* buffer, int size, error_code& ec);
torrent_info(fs::path const& filename, error_code& ec);
#ifndef BOOST_FILESYSTEM_NARROW_ONLY
torrent_info(fs::wpath const& filename, error_code& ec);
#endif
torrent_info(std::string const& filename, error_code& ec);
#if TORRENT_USE_WSTRING
torrent_info(std::wstring const& filename, error_code& ec);
#endif // TORRENT_USE_WSTRING
~torrent_info();
@ -202,13 +200,13 @@ namespace libtorrent
m_files.rename_file(index, new_filename);
}
#ifndef BOOST_FILESYSTEM_NARROW_ONLY
#if TORRENT_USE_WSTRING
void rename_file(int index, std::wstring const& new_filename)
{
copy_on_write();
m_files.rename_file(index, new_filename);
}
#endif
#endif // TORRENT_USE_WSTRING
void add_tracker(std::string const& url, int tier = 0);
std::vector<announce_entry> const& trackers() const { return m_urls; }

View File

@ -33,7 +33,7 @@ POSSIBILITY OF SUCH DAMAGE.
#ifndef TORRENT_UTF8_HPP_INCLUDED
#define TORRENT_UTF8_HPP_INCLUDED
#if !defined BOOST_FILESYSTEM_NARROW_ONLY && !defined BOOST_NO_STD_WSTRING
#if !defined BOOST_NO_STD_WSTRING
#include <string>
#include <cwchar>
@ -100,7 +100,7 @@ namespace libtorrent
}
}
}
#endif // !FILESYSTEM_NARROW_ONLY && !BOOST_NO_STD_WSTRING
#endif // !BOOST_NO_STD_WSTRING
#endif

View File

@ -57,46 +57,22 @@ namespace libtorrent
namespace detail
{
int TORRENT_EXPORT get_file_attributes(boost::filesystem::path const& p)
int TORRENT_EXPORT get_file_attributes(std::string const& p)
{
#ifdef TORRENT_WINDOWS
#if TORRENT_USE_WPATH
std::wstring path = convert_to_wstring(p.external_file_string());
#ifdef UNICODE
std::wstring path = convert_to_wstring(p);
DWORD attr = GetFileAttributesW(path.c_str());
#else
std::string path = convert_to_native(p.external_file_string());
std::string path = convert_to_native(p);
DWORD attr = GetFileAttributesA(path.c_str());
#endif
#endif // UNICODE
if (attr & FILE_ATTRIBUTE_HIDDEN) return file_storage::attribute_hidden;
return 0;
#else
struct stat s;
if (lstat(convert_to_native(p.external_file_string()).c_str(), &s) < 0) return 0;
int file_attr = 0;
if (s.st_mode & S_IXUSR)
file_attr += file_storage::attribute_executable;
if(S_ISLNK(s.st_mode))
file_attr += file_storage::attribute_symlink;
return file_attr;
#endif
}
#if TORRENT_USE_WPATH
int TORRENT_EXPORT get_file_attributes(boost::filesystem::wpath const& p)
{
#ifdef TORRENT_WINDOWS
std::wstring const& path = p.external_file_string();
DWORD attr = GetFileAttributesW(path.c_str());
if (attr & FILE_ATTRIBUTE_HIDDEN) return file_storage::attribute_hidden;
return 0;
#else
std::string native;
wchar_utf8(p.string(), native);
native = convert_to_native(native);
struct stat s;
if (lstat(native.c_str(), &s) < 0) return 0;
if (lstat(convert_to_native(p).c_str(), &s) < 0) return 0;
int file_attr = 0;
if (s.st_mode & S_IXUSR)
file_attr += file_storage::attribute_executable;
@ -105,85 +81,30 @@ namespace libtorrent
return file_attr;
#endif
}
#endif // TORRENT_USE_WPATH
std::time_t get_file_mtime(char const* path)
{
#ifdef TORRENT_WINDOWS
struct _stat s;
if (::_stat(path, &s) < 0) return 0;
#else
struct stat s;
if (lstat(path, &s) < 0) return 0;
#endif
return s.st_mtime;
}
std::time_t TORRENT_EXPORT get_file_mtime(boost::filesystem::path const& p)
{
#if defined TORRENT_WINDOWS && TORRENT_USE_WPATH
std::wstring path = convert_to_wstring(p.external_file_string());
struct _stat s;
if (::_wstat(path.c_str(), &s) < 0) return 0;
return s.st_mtime;
#else
std::string path = convert_to_native(p.external_file_string());
return get_file_mtime(p.string().c_str());
#endif
}
#if TORRENT_USE_WPATH
std::time_t TORRENT_EXPORT get_file_mtime(boost::filesystem::wpath const& p)
{
#ifdef TORRENT_WINDOWS
struct _stat s;
if (::_wstat(p.string().c_str(), &s) < 0) return 0;
return s.st_mtime;
#else
std::string utf8;
wchar_utf8(p.string(), utf8);
utf8 = convert_to_native(utf8);
return get_file_mtime(utf8.c_str());
#endif
}
#endif // TORRENT_USE_WPATH
#ifndef TORRENT_WINDOWS
boost::filesystem::path get_symlink_path_impl(char const* path)
std::string get_symlink_path_impl(char const* path)
{
char buf[MAX_SYMLINK_PATH];
int char_read = readlink(path,buf,MAX_SYMLINK_PATH);
std::string f = convert_to_native(path);
int char_read = readlink(f.c_str(),buf,MAX_SYMLINK_PATH);
if (char_read < 0) return "";
if (char_read < MAX_SYMLINK_PATH) buf[char_read] = 0;
else buf[0] = 0;
return buf;
return convert_from_native(buf);
}
#endif
boost::filesystem::path TORRENT_EXPORT get_symlink_path(boost::filesystem::path const& p)
std::string TORRENT_EXPORT get_symlink_path(std::string const& p)
{
#if defined TORRENT_WINDOWS && TORRENT_USE_WPATH
#if defined TORRENT_WINDOWS
return "";
#else
std::string path = convert_to_native(p.external_file_string());
return get_symlink_path_impl(p.string().c_str());
std::string path = convert_to_native(p);
return get_symlink_path_impl(p.c_str());
#endif
}
#if TORRENT_USE_WPATH
boost::filesystem::path TORRENT_EXPORT get_symlink_path(boost::filesystem::wpath const& p)
{
#ifdef TORRENT_WINDOWS
return "";
#else
std::string utf8;
wchar_utf8(p.string(), utf8);
utf8 = convert_to_native(utf8);
return get_symlink_path_impl(utf8.c_str());
#endif
}
#endif // TORRENT_USE_WPATH
}
create_torrent::create_torrent(file_storage& fs, int piece_size, int pad_file_limit, int flags)
@ -194,11 +115,7 @@ namespace libtorrent
, m_merkle_torrent(flags & merkle)
{
TORRENT_ASSERT(fs.num_files() > 0);
#if BOOST_VERSION < 103600
if (!m_multifile && m_files.at(0).path.has_branch_path()) m_multifile = true;
#else
if (!m_multifile && m_files.at(0).path.has_parent_path()) m_multifile = true;
#endif
if (!m_multifile && has_parent_path(m_files.at(0).path)) m_multifile = true;
// a piece_size of 0 means automatic
if (piece_size == 0 && !m_merkle_torrent)
@ -359,7 +276,10 @@ namespace libtorrent
{
info["mtime"] = m_files.at(0).mtime;
info["length"] = m_files.at(0).size;
if (m_files.at(0).pad_file || m_files.at(0).hidden_attribute || m_files.at(0).executable_attribute || m_files.at(0).symlink_attribute)
if (m_files.at(0).pad_file
|| m_files.at(0).hidden_attribute
|| m_files.at(0).executable_attribute
|| m_files.at(0).symlink_attribute)
{
std::string& attr = info["attr"].string();
if (m_files.at(0).pad_file) attr += 'p';
@ -371,11 +291,9 @@ namespace libtorrent
{
entry& sympath_e = info["symlink path"];
for (fs::path::iterator j = (m_files.at(0).symlink_path.begin());
j != m_files.at(0).symlink_path.end(); ++j)
{
sympath_e.list().push_back(entry(*j));
}
std::string split = split_path(m_files.at(0).symlink_path);
for (char const* e = split.c_str(); e != 0; e = next_path_element(e))
sympath_e.list().push_back(entry(e));
}
}
else
@ -393,19 +311,19 @@ namespace libtorrent
file_e["length"] = i->size;
entry& path_e = file_e["path"];
#if BOOST_VERSION < 103600
TORRENT_ASSERT(i->path.has_branch_path());
#else
TORRENT_ASSERT(i->path.has_parent_path());
#endif
TORRENT_ASSERT(*i->path.begin() == m_files.name());
TORRENT_ASSERT(has_parent_path(i->path));
for (fs::path::iterator j = boost::next(i->path.begin());
j != i->path.end(); ++j)
{
path_e.list().push_back(entry(*j));
}
if (i->pad_file || i->hidden_attribute || i->executable_attribute || i->symlink_attribute)
std::string split = split_path(i->path);
TORRENT_ASSERT(split.c_str() == m_files.name());
for (char const* e = next_path_element(split.c_str());
e != 0; e = next_path_element(e))
path_e.list().push_back(entry(e));
if (i->pad_file
|| i->hidden_attribute
|| i->executable_attribute
|| i->symlink_attribute)
{
std::string& attr = file_e["attr"].string();
if (i->pad_file) attr += 'p';
@ -417,11 +335,9 @@ namespace libtorrent
{
entry& sympath_e = file_e["symlink path"];
for (fs::path::iterator j = (i->symlink_path.begin());
j != i->symlink_path.end(); ++j)
{
sympath_e.list().push_back(entry(*j));
}
std::string split = split_path(i->symlink_path);
for (char const* e = split.c_str(); e != 0; e = next_path_element(e))
sympath_e.list().push_back(entry(e));
}
}
}

View File

@ -1771,7 +1771,7 @@ namespace libtorrent
test_error(j);
break;
}
j.str = j.storage->save_path().string();
j.str = j.storage->save_path();
break;
}
case disk_io_job::release_files:

View File

@ -47,14 +47,12 @@ POSSIBILITY OF SUCH DAMAGE.
#include "libtorrent/parse_url.hpp"
#ifdef TORRENT_WINDOWS
#if TORRENT_USE_WPATH
#include <windows.h>
#endif
#endif
#include "libtorrent/utf8.hpp"
#if TORRENT_USE_LOCALE_FILENAMES
#if TORRENT_USE_ICONV
#include <iconv.h>
#include <locale.h>
#endif
@ -80,6 +78,11 @@ namespace libtorrent
return ret;
}
bool is_alpha(char c)
{
return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z');
}
bool is_digit(char c)
{
return c >= '0' && c <= '9';
@ -532,14 +535,11 @@ namespace libtorrent
return true;
}
#if TORRENT_USE_WPATH
#if defined TORRENT_WINDOWS && defined UNICODE
std::wstring convert_to_wstring(std::string const& s)
{
std::wstring ret;
int result = libtorrent::utf8_wchar(s, ret);
#ifndef BOOST_WINDOWS
return ret;
#else
if (result == 0) return ret;
ret.clear();
@ -553,42 +553,65 @@ namespace libtorrent
ret += c;
}
return ret;
#endif
}
std::string convert_from_wstring(std::wstring const& s)
{
std::string ret;
int result = libtorrent::wchar_utf8(s, ret);
if (result == 0) return ret;
ret.clear();
const wchar_t* end = &s[0] + s.size();
for (const wchar_t* i = &s[0]; i < end;)
{
char c[10];
TORRENT_ASSERT(sizeof(c) >= MB_CUR_MAX);
int result = std::wctomb(c, *i);
if (result > 0)
{
i += result;
ret.append(c, result);
}
else
{
++i;
ret += ".";
}
}
return ret;
}
#endif
#ifdef TORRENT_WINDOWS
std::string convert_to_native(std::string const& s)
{
#ifndef BOOST_NO_EXCEPTIONS
try
{
#endif
std::wstring ws;
libtorrent::utf8_wchar(s, ws);
std::size_t size = wcstombs(0, ws.c_str(), 0);
if (size == std::size_t(-1)) return s;
std::string ret;
ret.resize(size);
size = wcstombs(&ret[0], ws.c_str(), size + 1);
if (size == std::size_t(-1)) return s;
ret.resize(size);
return ret;
#ifndef BOOST_NO_EXCEPTIONS
}
catch(std::exception)
{
return s;
}
#endif
std::wstring ws;
libtorrent::utf8_wchar(s, ws);
std::size_t size = wcstombs(0, ws.c_str(), 0);
if (size == std::size_t(-1)) return s;
std::string ret;
ret.resize(size);
size = wcstombs(&ret[0], ws.c_str(), size + 1);
if (size == std::size_t(-1)) return s;
ret.resize(size);
return ret;
}
#elif TORRENT_USE_LOCALE_FILENAMES
std::string convert_to_native(std::string const& s)
std::string convert_from_native(std::string const& s)
{
std::wstring ws;
ws.resize(s.size());
std::size_t size = mbstowcs(&ws[0], s.c_str(), s.size());
if (size == std::size_t(-1)) return s;
std::string ret;
libtorrent::wchar_utf8(ws, ret);
return ret;
}
#elif TORRENT_USE_ICONV
std::string iconv_convert_impl(std::string const& s, iconv_t h)
{
// the empty string represents the local dependent encoding
static iconv_t iconv_handle = iconv_open("", "UTF-8");
if (iconv_handle == iconv_t(-1)) return s;
std::string ret;
size_t insize = s.size();
size_t outsize = insize * 4;
@ -601,6 +624,22 @@ namespace libtorrent
ret.resize(outsize);
return ret;
}
std::string convert_to_native(std::string const& s)
{
// the empty string represents the local dependent encoding
static iconv_t iconv_handle = iconv_open("", "UTF-8");
if (iconv_handle == iconv_t(-1)) return s;
return iconv_convert_impl(s, iconv_handle);
}
std::string convert_from_native(std::string const& s)
{
// the empty string represents the local dependent encoding
static iconv_t iconv_handle = iconv_open("UTF-8", "");
if (iconv_handle == iconv_t(-1)) return s;
return iconv_convert_impl(s, iconv_handle);
}
#endif
}

View File

@ -37,17 +37,22 @@ POSSIBILITY OF SUCH DAMAGE.
#include "libtorrent/pch.hpp"
#include "libtorrent/config.hpp"
#include "libtorrent/alloca.hpp"
#include "libtorrent/escape_string.hpp" // for string conversion
#include <boost/scoped_ptr.hpp>
#ifdef TORRENT_WINDOWS
// windows part
#include "libtorrent/utf8.hpp"
#include "libtorrent/utf8.hpp"
#include <windows.h>
#include <winioctl.h>
#include <direct.h> // for _getcwd, _mkdir
#include <sys/types.h>
#include <sys/stat.h>
#else
// posix part
#define _FILE_OFFSET_BITS 64
#include <unistd.h>
#include <fcntl.h> // for F_LOG2PHYS
@ -55,12 +60,22 @@ POSSIBILITY OF SUCH DAMAGE.
#include <sys/types.h>
#include <sys/statvfs.h>
#include <errno.h>
#include <dirent.h>
#ifdef TORRENT_LINUX
// linux specifics
#include <sys/ioctl.h>
#ifdef FS_IOC_FIEMAP
#include <linux/fiemap.h>
#endif
#endif // TORRENT_LINUX
#elif defined __APPLE__ && defined __MACH__
// mac specifics
#include <copyfile.h>
#endif
#include <boost/static_assert.hpp>
// make sure the _FILE_OFFSET_BITS define worked
@ -70,9 +85,8 @@ POSSIBILITY OF SUCH DAMAGE.
// at least 64 bits wide
BOOST_STATIC_ASSERT(sizeof(lseek(0, 0, 0)) >= 8);
#endif
#endif // posix part
#include <boost/filesystem/operations.hpp>
#include "libtorrent/file.hpp"
#include <cstring>
#include <vector>
@ -90,7 +104,501 @@ BOOST_STATIC_ASSERT((libtorrent::file::no_buffer & libtorrent::file::attribute_m
namespace libtorrent
{
namespace fs = boost::filesystem;
void stat_file(std::string const& inf, file_status* s, error_code& ec)
{
ec.clear();
#if defined UNICODE && defined TORRENT_WINDOWS
std::wstring f = convert_to_wstring(inf);
#else
std::string f = convert_to_native(inf);
#endif
#ifdef TORRENT_WINDOWS
struct _stati64 ret;
#ifdef UNICODE
if (_wstati64(f.c_str(), &ret) < 0)
#else
if (_stati64(f.c_str(), &ret) < 0)
#endif
{
ec.assign(errno, boost::system::get_generic_category());
return;
}
#else
struct stat ret;
if (::stat(f.c_str(), &ret) < 0)
{
ec.assign(errno, boost::system::get_generic_category());
return;
}
#endif // TORRENT_WINDOWS
s->file_size = ret.st_size;
s->atime = ret.st_atime;
s->mtime = ret.st_mtime;
s->ctime = ret.st_ctime;
s->mode = ret.st_mode;
}
void rename(std::string const& inf, std::string const& newf, error_code& ec)
{
ec.clear();
#if defined UNICODE && defined TORRENT_WINDOWS
std::wstring f1 = convert_to_wstring(inf);
std::wstring f2 = convert_to_wstring(newf);
if (_wrename(f1.c_str(), f2.c_str()) < 0)
#else
std::string f1 = convert_to_native(inf);
std::string f2 = convert_to_native(newf);
if (std::rename(f1.c_str(), f2.c_str()) < 0)
#endif
{
ec.assign(errno, boost::system::get_generic_category());
return;
}
}
void create_directories(std::string const& f, error_code& ec)
{
ec.clear();
if (is_root_path(f)) return;
if (has_parent_path(f))
{
create_directories(parent_path(f), ec);
if (ec) return;
}
create_directory(f, ec);
}
void create_directory(std::string const& f, error_code& ec)
{
ec.clear();
#if defined TORRENT_WINDOWS && defined UNICODE
std::wstring n = convert_to_wstring(f);
#else
std::string n = convert_to_native(f);
#endif
#ifdef TORRENT_WINDOWS
if (CreateDirectory(n.c_str(), 0) == 0
&& GetLastError() != ERROR_ALREADY_EXISTS)
ec.assign(GetLastError(), boost::system::get_system_category());
#else
int ret = mkdir(n.c_str(), 0777);
if (ret < 0 && errno != EEXIST)
ec.assign(errno, boost::system::get_generic_category());
#endif
}
bool is_directory(std::string const& f, error_code& ec)
{
ec.clear();
error_code e;
file_status s;
stat_file(f, &s, e);
if (!e && s.mode & file_status::directory) return true;
ec = e;
return false;
}
void copy_file(std::string const& inf, std::string const& newf, error_code& ec)
{
ec.clear();
#if defined UNICODE && defined TORRENT_WINDOWS
std::wstring f1 = convert_to_wstring(inf);
std::wstring f2 = convert_to_wstring(newf);
#else
std::string f1 = convert_to_native(inf);
std::string f2 = convert_to_native(newf);
#endif
#ifdef TORRENT_WINDOWS
if (CopyFile(f1.c_str(), f2.c_str(), false) == 0)
ec.assign(GetLastError(), boost::system::get_system_category());
#elif defined __APPLE__ && defined __MACH__ && MAC_OS_X_VERSION_MIN_REQUIRED >= 1050
// this only works on 10.5
copyfile_state_t state = copyfile_state_alloc();
if (copyfile(f1.c_str(), f2.c_str(), state, COPYFILE_ALL) < 0)
ec.assign(errno, boost::system::get_generic_category());
copyfile_state_free(state);
#else
int infd = open(inf.c_str(), O_RDONLY);
if (infd < 0)
{
ec.assign(errno, boost::system::get_generic_category());
return;
}
int outfd = open(newf.c_str(), O_WRONLY | O_CREAT);
if (outfd < 0)
{
close(infd);
ec.assign(errno, boost::system::get_generic_category());
return;
}
char buffer[4096];
for (;;)
{
int num_read = read(infd, buffer, sizeof(buffer));
if (num_read == 0) break;
int num_written = write(outfd, buffer, num_read);
if (num_written < num_read)
{
ec.assign(errno, boost::system::get_generic_category());
break;
}
if (num_read < sizeof(buffer)) break;
}
close(infd);
close(outfd);
#endif // TORRENT_WINDOWS
}
std::string split_path(std::string const& f)
{
if (f.empty()) return f;
std::string ret;
char const* start = f.c_str();
char const* p = start;
while (*start != 0)
{
while (*p != '/'
&& *p != '\0'
#ifdef TORRENT_WINDOWS
&& *p != '\\'
#endif
) ++p;
if (p - start > 0)
{
ret.append(start, p - start);
ret.append(1, '\0');
}
if (*p != 0) ++p;
start = p;
}
ret.append(1, '\0');
return ret;
}
char const* next_path_element(char const* p)
{
p += strlen(p) + 1;
if (*p == 0) return 0;
return p;
}
std::string extension(std::string const& f)
{
char const* ext = strrchr(f.c_str(), '.');
if (ext == 0) return "";
return ext;
}
void replace_extension(std::string& f, std::string const& ext)
{
char* e = strrchr(f.c_str(), '.');
if (e == 0) f += '.';
else f.resize(e - f.c_str() + 1);
f += ext;
}
bool is_root_path(std::string const& f)
{
if (f.empty()) return false;
#ifdef TORRENT_WINDOWS
if (f == "\\\\") return true;
int i = 0;
// match the xx:\ or xx:/ form
while (f[i] && is_alpha(f[i])) ++i;
if (i == int(f.size()-2) && f[i] == ':' && (f[i+1] == '\\' || f[i+1] == '/'))
return true;
#else
// as well as parent_path("/") should be "/".
if (f == "/") return true;
#endif
return false;
}
bool has_parent_path(std::string const& f)
{
if (f.empty()) return false;
#ifdef TORRENT_WINDOWS
if (f == "\\\\") return false;
#else
// as well as parent_path("/") should be "/".
if (f == "/") return false;
#endif
int len = f.size() - 1;
// if the last character is / or \ ignore it
if (f[len] == '/' || f[len] == '\\') --len;
while (len >= 0)
{
if (f[len] == '/' || f[len] == '\\')
break;
--len;
}
return len >= 0;
}
std::string parent_path(std::string const& f)
{
if (f.empty()) return f;
#ifdef TORRENT_WINDOWS
if (f == "\\\\") return "";
#endif
if (f == "/") return "";
int len = f.size();
// if the last character is / or \ ignore it
if (f[len-1] == '/' || f[len-1] == '\\') --len;
while (len > 0)
{
--len;
if (f[len] == '/' || f[len] == '\\')
break;
}
if (f[len] == '/' || f[len] == '\\') ++len;
return std::string(f.c_str(), len);
}
std::string filename(std::string const& f)
{
char* sep = strrchr(f.c_str(), '/');
#ifdef TORRENT_WINDOWS
char* altsep = strrchr(f.c_str(), '\\');
if (sep == 0 || altsep > sep) sep = altsep;
#endif
if (sep == 0) return f;
return std::string(sep + 1);
}
std::string combine_path(std::string const& lhs, std::string const& rhs)
{
TORRENT_ASSERT(!is_complete(rhs));
if (lhs.empty()) return rhs;
if (rhs.empty()) return lhs;
#ifdef TORRENT_WINDOWS
bool need_sep = lhs[lhs.size()-1] != '\\' && lhs[lhs.size()-1] != '/';
return lhs + (need_sep?"\\":"") + rhs;
#else
bool need_sep = lhs[lhs.size()-1] != '/';
return lhs + (need_sep?"/":"") + rhs;
#endif
}
std::string current_working_directory()
{
#ifdef TORRENT_WINDOWS
#ifdef UNICODE
wchar_t cwd[TORRENT_MAX_PATH];
_wgetcwd(cwd, sizeof(cwd) / sizeof(wchar_t));
#else
char cwd[TORRENT_MAX_PATH];
_getcwd(cwd, sizeof(cwd));
#endif // UNICODE
#else
char cwd[TORRENT_MAX_PATH];
getcwd(cwd, sizeof(cwd));
#endif
#if defined TORRENT_WINDOWS && defined UNICODE
return convert_from_wstring(cwd);
#else
return convert_from_native(cwd);
#endif
}
size_type file_size(std::string const& f)
{
error_code ec;
file_status s;
stat_file(f, &s, ec);
if (ec) return 0;
return s.file_size;
}
bool exists(std::string const& f)
{
error_code ec;
file_status s;
stat_file(f, &s, ec);
if (ec) return false;
return true;
}
void remove(std::string const& inf, error_code& ec)
{
ec.clear();
#ifdef TORRENT_WINDOWS
#ifdef UNICODE
std::wstring f = convert_to_wstring(inf);
#else
std::string f = convert_to_native(inf);
#endif
if (DeleteFile(f.c_str()) == 0)
{
if (GetLastError() == ERROR_ACCESS_DENIED)
{
if (RemoveDirectory(f.c_str()) != 0)
return;
}
ec.assign(GetLastError(), boost::system::get_system_category());
return;
}
#else // TORRENT_WINDOWS
std::string f = convert_to_native(inf);
if (std::remove(f.c_str()) < 0)
{
ec.assign(errno, boost::system::get_generic_category());
return;
}
#endif // TORRENT_WINDOWS
}
void remove_all(std::string const& f, error_code& ec)
{
ec.clear();
file_status s;
stat_file(f, &s, ec);
if (ec) return;
if (s.mode & file_status::directory)
{
for (directory i(f, ec); !i.done(); i.next(ec))
{
if (ec) return;
std::string p = i.file();
if (p == "." || p == "..") continue;
remove_all(combine_path(f, p), ec);
if (ec) return;
}
}
remove(f, ec);
}
std::string complete(std::string const& f)
{
if (is_complete(f)) return f;
return combine_path(current_working_directory(), f);
}
bool is_complete(std::string const& f)
{
if (f.empty()) return false;
#ifdef TORRENT_WINDOWS
int i = 0;
// match the xx:\ or xx:/ form
while (f[i] && is_alpha(f[i])) ++i;
if (i < int(f.size()-2) && f[i] == ':' && (f[i+1] == '\\' || f[i+1] == '/'))
return true;
// match the \\ form
if (int(f.size()) >= 2 && f[0] == '\\' && f[1] == '\\')
return true;
return false;
#else
if (f[0] == '/') return true;
return false;
#endif
}
directory::directory(std::string const& path, error_code& ec)
: m_done(false)
{
ec.clear();
#ifdef TORRENT_WINDOWS
// the path passed to FindFirstFile() must be
// a pattern
std::string f = path;
if (!f.empty() && (f[f.size()-1] != '/' || f[f.size()-1] != '\\')) f += "\\*";
else f += "*";
#ifdef UNICODE
std::wstring p = convert_to_wstring(f);
#else
std::string p = convert_to_native(f);
#endif
m_handle = FindFirstFile(p.c_str(), &m_fd);
if (m_handle == INVALID_HANDLE_VALUE)
{
ec.assign(GetLastError(), boost::system::get_system_category());
m_done = true;
return;
}
#else
// the path passed to opendir() may not
// end with a /
std::string p = path;
if (!path.empty() && path[path.size()-1] == '/')
p.resize(path.size()-1);
p = convert_to_native(p);
m_handle = opendir(p.c_str());
if (m_handle == 0)
{
ec.assign(errno, boost::system::get_generic_category());
m_done = true;
return;
}
// read the first entry
next(ec);
#endif
}
directory::~directory()
{
#ifdef TORRENT_WINDOWS
if (m_handle != INVALID_HANDLE_VALUE)
FindClose(m_handle);
#else
if (m_handle) closedir(m_handle);
#endif
}
std::string directory::file() const
{
#ifdef TORRENT_WINDOWS
#ifdef UNICODE
return convert_from_wstring(m_fd.cFileName);
#else
return convert_from_native(m_fd.cFileName);
#endif
#else
return convert_from_native(m_dirent.d_name);
#endif
}
void directory::next(error_code& ec)
{
ec.clear();
#ifdef TORRENT_WINDOWS
if (FindNextFile(m_handle, &m_fd) == 0)
{
m_done = true;
int err = GetLastError();
if (err != ERROR_NO_MORE_FILES)
ec.assign(err, boost::system::get_system_category());
}
#else
dirent* dummy;
if (readdir_r(m_handle, &m_dirent, &dummy) != 0)
{
ec.assign(errno, boost::system::get_generic_category());
m_done = true;
}
if (dummy == 0) m_done = true;
#endif
}
file::file()
#ifdef TORRENT_WINDOWS
@ -104,7 +612,7 @@ namespace libtorrent
#endif
{}
file::file(fs::path const& path, int mode, error_code& ec)
file::file(std::string const& path, int mode, error_code& ec)
#ifdef TORRENT_WINDOWS
: m_file_handle(INVALID_HANDLE_VALUE)
#else
@ -120,7 +628,7 @@ namespace libtorrent
close();
}
bool file::open(fs::path const& path, int mode, error_code& ec)
bool file::open(std::string const& path, int mode, error_code& ec)
{
close();
#ifdef TORRENT_WINDOWS
@ -161,10 +669,10 @@ namespace libtorrent
FILE_ATTRIBUTE_HIDDEN, // hidden + executable
};
#if TORRENT_USE_WPATH
m_path = convert_to_wstring(path.external_file_string());
#ifdef UNICODE
m_path = convert_to_wstring(path);
#else
m_path = convert_to_native(path.external_file_string());
m_path = convert_to_native(path);
#endif
TORRENT_ASSERT((mode & mode_mask) < sizeof(mode_array)/sizeof(mode_array[0]));
@ -176,7 +684,7 @@ namespace libtorrent
if (m_file_handle == INVALID_HANDLE_VALUE)
{
ec = error_code(GetLastError(), get_system_category());
ec.assign(GetLastError(), get_system_category());
return false;
}
@ -187,7 +695,8 @@ namespace libtorrent
::DeviceIoControl(m_file_handle, FSCTL_SET_SPARSE, 0, 0
, 0, 0, &temp, 0);
}
#else
#else // TORRENT_WINDOWS
// rely on default umask to filter x and w permissions
// for group and others
int permissions = S_IRUSR | S_IWUSR
@ -204,7 +713,7 @@ namespace libtorrent
static const int no_buffer_flag[] = {0, 0};
#endif
m_fd = ::open(convert_to_native(path.external_file_string()).c_str()
m_fd = ::open(convert_to_native(path).c_str()
, mode_array[mode & rw_mask] | no_buffer_flag[(mode & no_buffer) >> 2], permissions);
#ifdef TORRENT_LINUX
@ -213,14 +722,14 @@ namespace libtorrent
if (m_fd == -1 && (mode & no_buffer) && errno == EINVAL)
{
mode &= ~no_buffer;
m_fd = ::open(path.external_file_string().c_str()
m_fd = ::open(path.c_str()
, mode & (rw_mask | no_buffer), permissions);
}
#endif
if (m_fd == -1)
{
ec = error_code(errno, get_posix_category());
ec.assign(errno, get_posix_category());
TORRENT_ASSERT(ec);
return false;
}
@ -275,7 +784,7 @@ namespace libtorrent
DWORD bytes_per_sector;
DWORD free_clusters;
DWORD total_clusters;
#if TORRENT_USE_WPATH
#ifdef UNICODE
wchar_t backslash = L'\\';
#else
char backslash = '\\';
@ -414,7 +923,7 @@ namespace libtorrent
offs.QuadPart = file_offset;
if (SetFilePointerEx(m_file_handle, offs, &offs, FILE_BEGIN) == FALSE)
{
ec = error_code(GetLastError(), get_system_category());
ec.assign(GetLastError(), get_system_category());
return -1;
}
@ -424,7 +933,7 @@ namespace libtorrent
if (ReadFile(m_file_handle, (char*)i->iov_base
, (DWORD)i->iov_len, &intermediate, 0) == FALSE)
{
ec = error_code(GetLastError(), get_system_category());
ec.assign(GetLastError(), get_system_category());
return -1;
}
ret += intermediate;
@ -441,7 +950,7 @@ namespace libtorrent
for (file::iovec_t const* i = bufs, *end(bufs + num_bufs); i < end; ++i)
{
for (int k = 0; k < i->iov_len; k += m_page_size)
for (int k = 0; k < int(i->iov_len); k += m_page_size)
{
cur_seg->Buffer = ((char*)i->iov_base) + k;
++cur_seg;
@ -453,8 +962,8 @@ namespace libtorrent
OVERLAPPED ol;
ol.Internal = 0;
ol.InternalHigh = 0;
ol.OffsetHigh = file_offset >> 32;
ol.Offset = file_offset & 0xffffffff;
ol.OffsetHigh = DWORD(file_offset >> 32);
ol.Offset = DWORD(file_offset & 0xffffffff);
ol.hEvent = CreateEvent(0, true, false, 0);
ret += size;
@ -464,13 +973,13 @@ namespace libtorrent
DWORD last_error = GetLastError();
if (last_error != ERROR_IO_PENDING)
{
ec = error_code(GetLastError(), get_system_category());
ec.assign(GetLastError(), get_system_category());
CloseHandle(ol.hEvent);
return -1;
}
if (GetOverlappedResult(m_file_handle, &ol, &ret, true) == 0)
{
ec = error_code(GetLastError(), get_system_category());
ec.assign(GetLastError(), get_system_category());
CloseHandle(ol.hEvent);
return -1;
}
@ -483,7 +992,7 @@ namespace libtorrent
size_type ret = lseek(m_fd, file_offset, SEEK_SET);
if (ret < 0)
{
ec = error_code(errno, get_posix_category());
ec.assign(errno, get_posix_category());
return -1;
}
#if TORRENT_USE_READV
@ -504,7 +1013,7 @@ namespace libtorrent
ret = ::readv(m_fd, bufs, num_bufs);
if (ret < 0)
{
ec = error_code(errno, get_posix_category());
ec.assign(errno, get_posix_category());
return -1;
}
return ret;
@ -517,7 +1026,7 @@ namespace libtorrent
ret = ::readv(m_fd, temp_bufs, num_bufs);
if (ret < 0)
{
ec = error_code(errno, get_posix_category());
ec.assign(errno, get_posix_category());
return -1;
}
return (std::min)(ret, size_type(size));
@ -531,7 +1040,7 @@ namespace libtorrent
int tmp = read(m_fd, i->iov_base, i->iov_len);
if (tmp < 0)
{
ec = error_code(errno, get_posix_category());
ec.assign(errno, get_posix_category());
return -1;
}
ret += tmp;
@ -597,7 +1106,7 @@ namespace libtorrent
offs.QuadPart = file_offset;
if (SetFilePointerEx(m_file_handle, offs, &offs, FILE_BEGIN) == FALSE)
{
ec = error_code(GetLastError(), get_system_category());
ec.assign(GetLastError(), get_system_category());
return -1;
}
@ -607,7 +1116,7 @@ namespace libtorrent
if (WriteFile(m_file_handle, (char const*)i->iov_base
, (DWORD)i->iov_len, &intermediate, 0) == FALSE)
{
ec = error_code(GetLastError(), get_system_category());
ec.assign(GetLastError(), get_system_category());
return -1;
}
ret += intermediate;
@ -624,7 +1133,7 @@ namespace libtorrent
for (file::iovec_t const* i = bufs, *end(bufs + num_bufs); i < end; ++i)
{
for (int k = 0; k < i->iov_len; k += m_page_size)
for (int k = 0; k < int(i->iov_len); k += m_page_size)
{
cur_seg->Buffer = ((char*)i->iov_base) + k;
++cur_seg;
@ -636,8 +1145,8 @@ namespace libtorrent
OVERLAPPED ol;
ol.Internal = 0;
ol.InternalHigh = 0;
ol.OffsetHigh = file_offset >> 32;
ol.Offset = file_offset & 0xffffffff;
ol.OffsetHigh = DWORD(file_offset >> 32);
ol.Offset = DWORD(file_offset & 0xffffffff);
ol.hEvent = CreateEvent(0, true, false, 0);
ret += size;
@ -661,14 +1170,14 @@ namespace libtorrent
{
if (GetLastError() != ERROR_IO_PENDING)
{
ec = error_code(GetLastError(), get_system_category());
ec.assign(GetLastError(), get_system_category());
CloseHandle(ol.hEvent);
return -1;
}
DWORD tmp;
if (GetOverlappedResult(m_file_handle, &ol, &tmp, true) == 0)
{
ec = error_code(GetLastError(), get_system_category());
ec.assign(GetLastError(), get_system_category());
CloseHandle(ol.hEvent);
return -1;
}
@ -684,7 +1193,7 @@ namespace libtorrent
if (f == INVALID_HANDLE_VALUE)
{
ec = error_code(GetLastError(), get_system_category());
ec.assign(GetLastError(), get_system_category());
return -1;
}
@ -693,12 +1202,12 @@ namespace libtorrent
if (SetFilePointerEx(f, offs, &offs, FILE_BEGIN) == FALSE)
{
CloseHandle(f);
ec = error_code(GetLastError(), get_system_category());
ec.assign(GetLastError(), get_system_category());
return -1;
}
if (::SetEndOfFile(f) == FALSE)
{
ec = error_code(GetLastError(), get_system_category());
ec.assign(GetLastError(), get_system_category());
CloseHandle(f);
return -1;
}
@ -710,7 +1219,7 @@ namespace libtorrent
size_type ret = lseek(m_fd, file_offset, SEEK_SET);
if (ret < 0)
{
ec = error_code(errno, get_posix_category());
ec.assign(errno, get_posix_category());
return -1;
}
@ -732,7 +1241,7 @@ namespace libtorrent
ret = ::writev(m_fd, bufs, num_bufs);
if (ret < 0)
{
ec = error_code(errno, get_posix_category());
ec.assign(errno, get_posix_category());
return -1;
}
return ret;
@ -745,12 +1254,12 @@ namespace libtorrent
ret = ::writev(m_fd, temp_bufs, num_bufs);
if (ret < 0)
{
ec = error_code(errno, get_posix_category());
ec.assign(errno, get_posix_category());
return -1;
}
if (ftruncate(m_fd, file_offset + size) < 0)
{
ec = error_code(errno, get_posix_category());
ec.assign(errno, get_posix_category());
return -1;
}
return (std::min)(ret, size_type(size));
@ -764,7 +1273,7 @@ namespace libtorrent
int tmp = write(m_fd, i->iov_base, i->iov_len);
if (tmp < 0)
{
ec = error_code(errno, get_posix_category());
ec.assign(errno, get_posix_category());
return -1;
}
ret += tmp;
@ -826,7 +1335,7 @@ namespace libtorrent
// query cluster size
pos_alignment();
in.StartingVcn.QuadPart = offset / m_cluster_size;
int cluster_offset = in.StartingVcn.QuadPart % m_cluster_size;
int cluster_offset = int(in.StartingVcn.QuadPart % m_cluster_size);
if (DeviceIoControl(m_file_handle, FSCTL_GET_RETRIEVAL_POINTERS, &in
, sizeof(in), &out, sizeof(out), &out_bytes, 0) == 0)
@ -859,7 +1368,7 @@ namespace libtorrent
offs.QuadPart = s;
if (SetFilePointerEx(m_file_handle, offs, &offs, FILE_BEGIN) == FALSE)
{
ec = error_code(GetLastError(), get_system_category());
ec.assign(GetLastError(), get_system_category());
return false;
}
#if _WIN32_WINNT >= 0x501
@ -873,13 +1382,13 @@ namespace libtorrent
#endif
if (::SetEndOfFile(m_file_handle) == FALSE)
{
ec = error_code(GetLastError(), get_system_category());
ec.assign(GetLastError(), get_system_category());
return false;
}
#else
if (ftruncate(m_fd, s) < 0)
{
ec = error_code(errno, get_posix_category());
ec.assign(errno, get_posix_category());
return false;
}
if ((m_open_mode & sparse) == 0)
@ -889,14 +1398,14 @@ namespace libtorrent
fstore_t f = {F_ALLOCATECONTIG, F_PEOFPOSMODE, 0, s, 0};
if (fcntl(m_fd, F_PREALLOCATE, &f) < 0)
{
ec = error_code(errno, get_posix_category());
ec.assign(errno, get_posix_category());
return false;
}
#else
int ret = posix_fallocate(m_fd, 0, s);
if (ret != 0)
{
ec = error_code(ret, get_posix_category());
ec.assign(ret, get_posix_category());
return false;
}
#endif
@ -911,7 +1420,7 @@ namespace libtorrent
LARGE_INTEGER file_size;
if (!GetFileSizeEx(m_file_handle, &file_size))
{
ec = error_code(GetLastError(), get_system_category());
ec.assign(GetLastError(), get_system_category());
return -1;
}
return file_size.QuadPart;
@ -919,7 +1428,7 @@ namespace libtorrent
struct stat fs;
if (fstat(m_fd, &fs) != 0)
{
ec = error_code(errno, get_posix_category());
ec.assign(errno, get_posix_category());
return -1;
}
return fs.st_size;

View File

@ -38,15 +38,15 @@ POSSIBILITY OF SUCH DAMAGE.
namespace libtorrent
{
boost::shared_ptr<file> file_pool::open_file(void* st, fs::path const& p
boost::shared_ptr<file> file_pool::open_file(void* st, std::string const& p
, int m, error_code& ec)
{
TORRENT_ASSERT(st != 0);
TORRENT_ASSERT(p.is_complete());
TORRENT_ASSERT(is_complete(p));
TORRENT_ASSERT((m & file::rw_mask) == file::read_only
|| (m & file::rw_mask) == file::read_write);
mutex::scoped_lock l(m_mutex);
file_set::iterator i = m_files.find(p.string());
file_set::iterator i = m_files.find(p);
if (i != m_files.end())
{
lru_file_entry& e = i->second;
@ -103,7 +103,7 @@ namespace libtorrent
return boost::shared_ptr<file>();
e.mode = m;
e.key = st;
m_files.insert(std::make_pair(p.string(), e));
m_files.insert(std::make_pair(p, e));
TORRENT_ASSERT(e.file_ptr->is_open());
return e.file_ptr;
}
@ -117,11 +117,11 @@ namespace libtorrent
m_files.erase(i);
}
void file_pool::release(fs::path const& p)
void file_pool::release(std::string const& p)
{
mutex::scoped_lock l(m_mutex);
file_set::iterator i = m_files.find(p.string());
file_set::iterator i = m_files.find(p);
if (i != m_files.end()) m_files.erase(i);
}

View File

@ -33,6 +33,7 @@ POSSIBILITY OF SUCH DAMAGE.
#include "libtorrent/pch.hpp"
#include "libtorrent/file_storage.hpp"
#include "libtorrent/file.hpp"
#include "libtorrent/utf8.hpp"
#include <boost/bind.hpp>
#include <cstdio>
@ -60,7 +61,7 @@ namespace libtorrent
return piece_length();
}
#ifndef BOOST_FILESYSTEM_NARROW_ONLY
#if TORRENT_USE_WSTRING
void file_storage::set_name(std::wstring const& n)
{
std::string utf8;
@ -76,14 +77,14 @@ namespace libtorrent
m_files[index].path = utf8;
}
void file_storage::add_file(fs::wpath const& file, size_type size, int flags
, std::time_t mtime, fs::path const& symlink_path)
void file_storage::add_file(std::wstring const& file, size_type size, int flags
, std::time_t mtime, std::string const& symlink_path)
{
std::string utf8;
wchar_utf8(file.string(), utf8);
wchar_utf8(file, utf8);
add_file(utf8, size, flags, mtime, symlink_path);
}
#endif
#endif // TORRENT_USE_WSTRING
void file_storage::rename_file(int index, std::string const& new_filename)
{
@ -168,29 +169,25 @@ namespace libtorrent
return ret;
}
void file_storage::add_file(fs::path const& file, size_type size, int flags
, std::time_t mtime, fs::path const& symlink_path)
void file_storage::add_file(std::string const& file, size_type size, int flags
, std::time_t mtime, std::string const& symlink_path)
{
TORRENT_ASSERT(size >= 0);
#if BOOST_VERSION < 103600
if (!file.has_branch_path())
#else
if (!file.has_parent_path())
#endif
if (!has_parent_path(file))
{
// you have already added at least one file with a
// path to the file (branch_path), which means that
// all the other files need to be in the same top
// directory as the first file.
TORRENT_ASSERT(m_files.empty());
m_name = file.string();
m_name = file;
}
else
{
if (m_files.empty())
m_name = *file.begin();
m_name = split_path(file).c_str();
}
TORRENT_ASSERT(m_name == *file.begin());
TORRENT_ASSERT(m_name == split_path(file).c_str());
m_files.push_back(file_entry());
file_entry& e = m_files.back();
e.size = size;
@ -200,25 +197,21 @@ namespace libtorrent
e.hidden_attribute = bool(flags & attribute_hidden);
e.executable_attribute = bool(flags & attribute_executable);
e.symlink_attribute = bool(flags & attribute_symlink);
if (e.symlink_attribute) e.symlink_path = symlink_path.string();
if (e.symlink_attribute) e.symlink_path = symlink_path;
e.mtime = mtime;
m_total_size += size;
}
void file_storage::add_file(file_entry const& ent)
{
#if BOOST_VERSION < 103600
if (!ent.path.has_branch_path())
#else
if (!ent.path.has_parent_path())
#endif
if (!has_parent_path(ent.path))
{
// you have already added at least one file with a
// path to the file (branch_path), which means that
// all the other files need to be in the same top
// directory as the first file.
TORRENT_ASSERT(m_files.empty());
m_name = ent.path.string();
m_name = ent.path;
}
else
{
@ -296,9 +289,7 @@ namespace libtorrent
i->file_base = 0;
char name[10];
std::sprintf(name, "%d", padding_file);
i->path = *(i+1)->path.begin();
i->path /= "_____padding_file_";
i->path /= name;
i->path = combine_path("_____padding_file", name);
i->pad_file = true;
off += pad_size;
++padding_file;

View File

@ -429,7 +429,7 @@ void node_impl::add_node(udp::endpoint node)
// create a dummy traversal_algorithm
boost::intrusive_ptr<traversal_algorithm> algo(
new traversal_algorithm(*this, node_id::min()));
new traversal_algorithm(*this, (node_id::min)()));
observer_ptr o(new (ptr) null_observer(algo));
#ifdef TORRENT_DEBUG
o->m_in_constructor = false;

View File

@ -37,8 +37,6 @@ POSSIBILITY OF SUCH DAMAGE.
#endif
#include <boost/shared_ptr.hpp>
#include <boost/filesystem/fstream.hpp>
#include <boost/filesystem/convenience.hpp>
#ifdef _MSC_VER
#pragma warning(pop)
@ -56,8 +54,6 @@ POSSIBILITY OF SUCH DAMAGE.
namespace libtorrent {
namespace fs = boost::filesystem;
namespace
{
@ -65,9 +61,11 @@ namespace
{
logger_peer_plugin(std::string const& filename)
{
fs::path dir(fs::complete("libtorrent_ext_logs"));
if (!fs::exists(dir)) fs::create_directories(dir);
m_file.open((dir / filename).string().c_str(), std::ios_base::out | std::ios_base::out);
error_code ec;
std::string dir = complete("libtorrent_ext_logs");
if (!exists(dir)) create_directories(dir, ec);
m_file.open(combine_path(dir, filename).c_str()
, std::ios_base::out | std::ios_base::out);
m_file << "\n\n\n";
log_timestamp();
m_file << "*** starting log ***\n";

View File

@ -96,7 +96,7 @@ namespace libtorrent
#ifndef BOOST_NO_EXCEPTIONS
#ifndef TORRENT_NO_DEPRECATE
torrent_handle add_magnet_uri(session& ses, std::string const& uri
, fs::path const& save_path
, std::string const& save_path
, storage_mode_t storage_mode
, bool paused
, storage_constructor_type sc

View File

@ -43,8 +43,6 @@ POSSIBILITY OF SUCH DAMAGE.
#pragma warning(push, 1)
#endif
#include <boost/filesystem/convenience.hpp>
#include <boost/filesystem/exception.hpp>
#include <boost/limits.hpp>
#include <boost/bind.hpp>
@ -99,18 +97,6 @@ namespace libtorrent
return ret;
}
namespace aux
{
filesystem_init::filesystem_init()
{
#if BOOST_VERSION < 103400
using namespace boost::filesystem;
if (path::default_name_check_writable())
path::default_name_check(no_check);
#endif
}
}
// this function returns a session_settings object
// which will optimize libtorrent for minimum memory
// usage, with no consideration of performance.
@ -238,7 +224,7 @@ namespace libtorrent
, int flags
, int alert_mask
#if defined TORRENT_VERBOSE_LOGGING || defined TORRENT_LOGGING || defined TORRENT_ERROR_LOGGING
, fs::path logpath
, std::string logpath
#endif
)
: m_impl(new session_impl(listen_port_range, id, listen_interface
@ -250,7 +236,6 @@ namespace libtorrent
#ifdef TORRENT_MEMDEBUG
start_malloc_debug();
#endif
// turn off the filename checking in boost.filesystem
TORRENT_ASSERT(listen_port_range.first > 0);
TORRENT_ASSERT(listen_port_range.first < listen_port_range.second);
set_alert_mask(alert_mask);
@ -278,7 +263,7 @@ namespace libtorrent
, int flags
, int alert_mask
#if defined TORRENT_VERBOSE_LOGGING || defined TORRENT_LOGGING || defined TORRENT_ERROR_LOGGING
, fs::path logpath
, std::string logpath
#endif
)
#if defined TORRENT_VERBOSE_LOGGING || defined TORRENT_LOGGING || defined TORRENT_ERROR_LOGGING
@ -456,7 +441,7 @@ namespace libtorrent
// if the torrent already exists, this will throw duplicate_torrent
torrent_handle session::add_torrent(
torrent_info const& ti
, fs::path const& save_path
, std::string const& save_path
, entry const& resume_data
, storage_mode_t storage_mode
, bool paused
@ -479,7 +464,7 @@ namespace libtorrent
torrent_handle session::add_torrent(
boost::intrusive_ptr<torrent_info> ti
, fs::path const& save_path
, std::string const& save_path
, entry const& resume_data
, storage_mode_t storage_mode
, bool paused
@ -505,7 +490,7 @@ namespace libtorrent
char const* tracker_url
, sha1_hash const& info_hash
, char const* name
, fs::path const& save_path
, std::string const& save_path
, entry const& e
, storage_mode_t storage_mode
, bool paused

View File

@ -43,8 +43,6 @@ POSSIBILITY OF SUCH DAMAGE.
#pragma warning(push, 1)
#endif
#include <boost/filesystem/convenience.hpp>
#include <boost/filesystem/exception.hpp>
#include <boost/limits.hpp>
#include <boost/bind.hpp>
#include <boost/function_equal.hpp>
@ -127,8 +125,6 @@ namespace boost {
namespace libtorrent {
namespace fs = boost::filesystem;
namespace detail
{
@ -167,7 +163,7 @@ namespace aux {
, fingerprint const& cl_fprint
, char const* listen_interface
#if defined TORRENT_VERBOSE_LOGGING || defined TORRENT_LOGGING || defined TORRENT_ERROR_LOGGING
, fs::path const& logpath
, std::string const& logpath
#endif
)
: m_ipv4_peer_pool(500)

View File

@ -42,8 +42,6 @@ POSSIBILITY OF SUCH DAMAGE.
#pragma warning(push, 1)
#endif
#include <boost/filesystem/convenience.hpp>
#include <boost/filesystem/operations.hpp>
#include <boost/ref.hpp>
#include <boost/bind.hpp>
#include <boost/version.hpp>
@ -74,7 +72,6 @@ POSSIBILITY OF SUCH DAMAGE.
//#define TORRENT_PARTIAL_HASH_LOG
#if TORRENT_USE_IOSTREAM
#include <boost/filesystem/fstream.hpp>
#include <ios>
#include <iostream>
#include <iomanip>
@ -102,7 +99,6 @@ POSSIBILITY OF SUCH DAMAGE.
// for convert_to_wstring and convert_to_native
#include "libtorrent/escape_string.hpp"
namespace fs = boost::filesystem;
using boost::bind;
#if defined TORRENT_DEBUG && defined TORRENT_STORAGE_DEBUG && TORRENT_USE_IOSTREAM
@ -121,100 +117,65 @@ namespace
namespace libtorrent
{
template <class Path>
void recursive_copy(Path const& old_path, Path const& new_path, error_code& ec)
void recursive_copy(std::string const& old_path, std::string const& new_path, error_code& ec)
{
using boost::filesystem::basic_directory_iterator;
#ifndef BOOST_NO_EXCEPTIONS
try {
#endif
TORRENT_ASSERT(!ec);
if (is_directory(old_path))
if (is_directory(old_path, ec))
{
create_directory(new_path);
for (basic_directory_iterator<Path> i(old_path), end; i != end; ++i)
create_directory(new_path, ec);
if (ec) return;
for (directory i(old_path, ec); !i.done(); i.next(ec))
{
#if BOOST_VERSION < 103600
recursive_copy(i->path(), new_path / i->path().leaf(), ec);
#else
recursive_copy(i->path(), new_path / i->path().filename(), ec);
#endif
std::string f = i.file();
recursive_copy(f, combine_path(new_path, f), ec);
if (ec) return;
}
}
else
else if (!ec)
{
copy_file(old_path, new_path);
copy_file(old_path, new_path, ec);
}
#ifndef BOOST_NO_EXCEPTIONS
}
#if BOOST_VERSION >= 103500
catch (boost::system::system_error& e)
{
ec = e.code();
}
#else
catch (boost::filesystem::filesystem_error& e)
{
ec = error_code(e.system_error(), get_system_category());
}
#endif // BOOST_VERSION
#endif // BOOST_NO_EXCEPTIONS
}
template <class Path>
void recursive_remove(Path const& old_path)
void recursive_remove(std::string const& old_path)
{
using boost::filesystem::basic_directory_iterator;
#ifndef BOOST_NO_EXCEPTIONS
try {
#endif
if (is_directory(old_path))
error_code ec;
if (is_directory(old_path, ec))
{
for (basic_directory_iterator<Path> i(old_path), end; i != end; ++i)
recursive_remove(i->path());
remove(old_path);
for (directory i(old_path, ec); !i.done(); i.next(ec))
recursive_remove(combine_path(old_path, i.file()));
remove(old_path, ec);
}
else
{
remove(old_path);
remove(old_path, ec);
}
#ifndef BOOST_NO_EXCEPTIONS
} catch (std::exception& e) {}
#endif
}
std::vector<std::pair<size_type, std::time_t> > get_filesizes(
file_storage const& s, fs::path p)
file_storage const& storage, std::string const& p)
{
p = complete(p);
std::string save_path = complete(p);
std::vector<std::pair<size_type, std::time_t> > sizes;
for (file_storage::iterator i = s.begin()
, end(s.end());i != end; ++i)
for (file_storage::iterator i = storage.begin()
, end(storage.end()); i != end; ++i)
{
size_type size = 0;
std::time_t time = 0;
if (i->pad_file)
if (!i->pad_file)
{
sizes.push_back(std::make_pair(i->size, time));
continue;
file_status s;
error_code ec;
stat_file(combine_path(save_path, i->path), &s, ec);
if (!ec)
{
size = s.file_size;
time = s.mtime;
}
}
#if TORRENT_USE_WPATH
fs::wpath f = convert_to_wstring((p / i->path).string());
#else
fs::path f = convert_to_native((p / i->path).string());
#endif
// TODO: optimize
if (exists(f))
#ifndef BOOST_NO_EXCEPTIONS
try
#endif
{
size = file_size(f);
time = last_write_time(f);
}
#ifndef BOOST_NO_EXCEPTIONS
catch (std::exception&) {}
#endif
sizes.push_back(std::make_pair(size, time));
}
return sizes;
@ -228,7 +189,7 @@ namespace libtorrent
// still be a correct subset of the actual data on disk.
bool match_filesizes(
file_storage const& fs
, fs::path p
, std::string p
, std::vector<std::pair<size_type, std::time_t> > const& sizes
, bool compact_mode
, error_code& error)
@ -240,34 +201,27 @@ namespace libtorrent
}
p = complete(p);
std::vector<std::pair<size_type, std::time_t> >::const_iterator s
std::vector<std::pair<size_type, std::time_t> >::const_iterator size_iter
= sizes.begin();
for (file_storage::iterator i = fs.begin()
, end(fs.end());i != end; ++i, ++s)
, end(fs.end());i != end; ++i, ++size_iter)
{
size_type size = 0;
std::time_t time = 0;
if (i->pad_file) continue;
#if TORRENT_USE_WPATH
fs::wpath f = convert_to_wstring((p / i->path).string());
#else
fs::path f = convert_to_native((p / i->path).string());
#endif
// TODO: Optimize this! This will result in 3 stat calls per file!
if (exists(f))
#ifndef BOOST_NO_EXCEPTIONS
try
#endif
file_status s;
error_code ec;
stat_file(combine_path(p, i->path), &s, ec);
if (!ec)
{
size = file_size(f);
time = last_write_time(f);
size = s.file_size;
time = s.mtime;
}
#ifndef BOOST_NO_EXCEPTIONS
catch (std::exception&) {}
#endif
if ((compact_mode && size != s->first)
|| (!compact_mode && size < s->first))
if ((compact_mode && size != size_iter->first)
|| (!compact_mode && size < size_iter->first))
{
error = error_code(errors::mismatching_file_size, libtorrent_category);
return false;
@ -275,8 +229,8 @@ namespace libtorrent
// allow one second 'slack', because of FAT volumes
// in sparse mode, allow the files to be more recent
// than the resume data, but only by 5 minutes
if ((compact_mode && (time > s->second + 1 || time < s->second - 1)) ||
(!compact_mode && (time > s->second + 5 * 60 || time < s->second - 1)))
if ((compact_mode && (time > size_iter->second + 1 || time < size_iter->second - 1)) ||
(!compact_mode && (time > size_iter->second + 5 * 60 || time < size_iter->second - 1)))
{
error = error_code(errors::mismatching_file_timestamp, libtorrent_category);
return false;
@ -383,7 +337,7 @@ namespace libtorrent
class storage : public storage_interface, boost::noncopyable
{
public:
storage(file_storage const& fs, file_storage const* mapped, fs::path const& path, file_pool& fp)
storage(file_storage const& fs, file_storage const* mapped, std::string const& path, file_pool& fp)
: m_files(fs)
, m_pool(fp)
, m_page_size(4096)
@ -392,8 +346,7 @@ namespace libtorrent
if (mapped) m_mapped_files.reset(new file_storage(*mapped));
TORRENT_ASSERT(m_files.begin() != m_files.end());
m_save_path = fs::complete(path);
TORRENT_ASSERT(m_save_path.is_complete());
m_save_path = complete(path);
#ifdef TORRENT_WINDOWS
SYSTEM_INFO si;
GetSystemInfo(&si);
@ -408,7 +361,7 @@ namespace libtorrent
bool release_files();
bool delete_files();
bool initialize(bool allocate_files);
bool move_storage(fs::path save_path);
bool move_storage(std::string const& save_path);
int read(char* buf, int slot, int offset, int size);
int write(char const* buf, int slot, int offset, int size);
int sparse_end(int start) const;
@ -453,7 +406,7 @@ namespace libtorrent
file_storage const& m_files;
std::vector<boost::uint8_t> m_file_priority;
fs::path m_save_path;
std::string m_save_path;
// the file pool is typically stored in
// the session, to make all storage
// instances use the same pool
@ -553,25 +506,19 @@ namespace libtorrent
m_allocate_files = allocate_files;
error_code ec;
// first, create all missing directories
fs::path last_path;
std::string last_path;
for (file_storage::iterator file_iter = files().begin(),
end_iter = files().end(); file_iter != end_iter; ++file_iter)
{
fs::path dir = (m_save_path / file_iter->path).branch_path();
std::string file_path = combine_path(m_save_path, file_iter->path);
std::string dir = parent_path(file_path);
if (dir != last_path)
{
last_path = dir;
#if TORRENT_USE_WPATH
fs::wpath wp = convert_to_wstring(last_path.string());
if (!exists(wp))
create_directories(wp);
#else
fs::path p = convert_to_native(last_path.string());
if (!exists(p))
create_directories(p);
#endif
if (!is_directory(last_path, ec))
create_directories(last_path, ec);
}
int file_index = file_iter - files().begin();
@ -583,23 +530,23 @@ namespace libtorrent
// ignore pad files
if (file_iter->pad_file) continue;
#ifndef BOOST_NO_EXCEPTIONS
try {
#endif
#if TORRENT_USE_WPATH
fs::wpath file_path = convert_to_wstring((m_save_path / file_iter->path).string());
#else
fs::path file_path = convert_to_native((m_save_path / file_iter->path).string());
#endif
// if the file is empty, just create it either way.
// if the file already exists, but is larger than what
// it's supposed to be, also truncate it
if (allocate_files
|| file_iter->size == 0
|| (exists(file_path) && file_size(file_path) > file_iter->size))
if (!allocate_files && file_iter->size > 0) continue;
file_status s;
stat_file(file_path, &s, ec);
if (ec && ec != boost::system::errc::no_such_file_or_directory)
{
error_code ec;
set_error(file_path, ec);
break;
}
// ec is either ENOENT or the file existed and s is valid
if (ec || s.file_size > file_iter->size || file_iter->size == 0)
{
ec.clear();
int mode = file::read_write;
if (m_settings
&& (settings().disk_io_read_mode == session_settings::disable_os_cache
@ -608,32 +555,18 @@ namespace libtorrent
mode |= file::no_buffer;
if (!m_allocate_files) mode |= file::sparse;
boost::shared_ptr<file> f = m_pool.open_file(this
, m_save_path / file_iter->path, mode, ec);
if (ec) set_error(m_save_path / file_iter->path, ec);
, combine_path(m_save_path, file_iter->path), mode, ec);
if (ec) set_error(combine_path(m_save_path, file_iter->path), ec);
else if (f)
{
f->set_size(file_iter->size, ec);
if (ec) set_error(m_save_path / file_iter->path, ec);
if (ec) set_error(combine_path(m_save_path, file_iter->path), ec);
}
}
#ifndef BOOST_NO_EXCEPTIONS
}
#if BOOST_VERSION >= 103500
catch (boost::system::system_error& e)
{
set_error(m_save_path / file_iter->path, e.code());
return true;
}
#else
catch (boost::filesystem::filesystem_error& e)
{
set_error(m_save_path / file_iter->path
, error_code(e.system_error(), get_system_category()));
return true;
}
#endif // BOOST_VERSION
#endif // BOOST_NO_EXCEPTIONS
if (ec) break;
}
std::vector<boost::uint8_t>().swap(m_file_priority);
// close files that were opened in write mode
m_pool.release(this);
@ -647,34 +580,11 @@ namespace libtorrent
for (; i != end; ++i)
{
bool file_exists = false;
#if TORRENT_USE_WPATH
fs::wpath f = convert_to_wstring((m_save_path / i->path).string());
#else
fs::path f = convert_to_native((m_save_path / i->path).string());
#endif
#ifndef BOOST_NO_EXCEPTIONS
try
{
#endif
file_exists = exists(f);
#ifndef BOOST_NO_EXCEPTIONS
}
#if BOOST_VERSION >= 103500
catch (boost::system::system_error& e)
{
set_error(m_save_path / i->path, e.code());
return false;
}
#else
catch (boost::filesystem::filesystem_error& e)
{
set_error(m_save_path / i->path, error_code(e.system_error(), get_system_category()));
return false;
}
#endif // BOOST_VERSION
#endif // BOOST_NO_EXCEPTIONS
if (file_exists && i->size > 0)
error_code ec;
file_status s;
stat_file(combine_path(m_save_path, i->path), &s, ec);
if (ec) continue;
if (s.mode & file_status::regular_file && i->size > 0)
return true;
}
return false;
@ -683,55 +593,24 @@ namespace libtorrent
bool storage::rename_file(int index, std::string const& new_filename)
{
if (index < 0 || index >= m_files.num_files()) return true;
fs::path old_name = m_save_path / files().at(index).path;
std::string old_name = combine_path(m_save_path, files().at(index).path);
m_pool.release(old_name);
#if TORRENT_USE_WPATH
fs::wpath old_path = convert_to_wstring(old_name.string());
fs::wpath new_path = convert_to_wstring((m_save_path / new_filename).string());
#else
fs::path const& old_path = convert_to_native(old_name.string());
fs::path new_path = convert_to_native((m_save_path / new_filename).string());
#endif
error_code ec;
rename(old_name, combine_path(m_save_path, new_filename), ec);
if (ec && ec != boost::system::errc::no_such_file_or_directory)
{
set_error(old_name, ec);
return true;
}
#ifndef BOOST_NO_EXCEPTIONS
try
{
#endif
// if old path doesn't exist, just rename the file
// in our file_storage, so that when it is created
// it will get the new name
create_directories(new_path.branch_path());
if (exists(old_path)) rename(old_path, new_path);
/*
error_code ec;
rename(old_path, new_path, ec);
if (ec)
{
set_error(old_path, ec);
return;
}
*/
if (!m_mapped_files)
{ m_mapped_files.reset(new file_storage(m_files)); }
m_mapped_files->rename_file(index, new_filename);
#ifndef BOOST_NO_EXCEPTIONS
}
#if BOOST_VERSION >= 103500
catch (boost::system::system_error& e)
{
set_error(old_name, e.code());
return true;
}
#else
catch (boost::filesystem::filesystem_error& e)
{
set_error(old_name, error_code(e.system_error()
, get_system_category()));
return true;
}
#endif // BOOST_VERSION
#endif
// if old path doesn't exist, just rename the file
// in our file_storage, so that when it is created
// it will get the new name
if (!m_mapped_files)
{ m_mapped_files.reset(new file_storage(m_files)); }
m_mapped_files->rename_file(index, new_filename);
return false;
}
@ -743,33 +622,11 @@ namespace libtorrent
void storage::delete_one_file(std::string const& p)
{
#if TORRENT_USE_WPATH
#ifndef BOOST_NO_EXCEPTIONS
try
#endif
{ fs::remove(convert_to_wstring(p)); }
#ifndef BOOST_NO_EXCEPTIONS
#if BOOST_VERSION >= 103500
catch (boost::system::system_error& e)
{
// no such file or directory is not an error
if (e.code() != make_error_code(boost::system::errc::no_such_file_or_directory))
set_error(p, e.code());
}
#else
catch (boost::filesystem::filesystem_error& e)
{
set_error(p, error_code(e.system_error(), get_system_category()));
}
#endif // BOOST_VERSION
#endif // BOOST_NO_EXCEPTIONS
#else
// no such file or directory is not an error
if (std::remove(convert_to_native(p).c_str()) != 0 && errno != ENOENT)
{
set_error(p, error_code(errno, get_posix_category()));
}
#endif
error_code ec;
remove(p, ec);
if (ec && ec != boost::system::errc::no_such_file_or_directory)
set_error(p, ec);
}
bool storage::delete_files()
@ -783,14 +640,14 @@ namespace libtorrent
for (file_storage::iterator i = files().begin()
, end(files().end()); i != end; ++i)
{
std::string p = (m_save_path / i->path).string();
fs::path bp = i->path.branch_path();
std::string p = combine_path(m_save_path, i->path);
std::string bp = parent_path(i->path);
std::pair<iter_t, bool> ret;
ret.second = true;
while (ret.second && !bp.empty())
{
std::pair<iter_t, bool> ret = directories.insert((m_save_path / bp).string());
bp = bp.branch_path();
std::pair<iter_t, bool> ret = directories.insert(combine_path(m_save_path, bp));
bp = parent_path(bp);
}
delete_one_file(p);
}
@ -846,7 +703,7 @@ namespace libtorrent
TORRENT_ASSERT(file_iter != files().end());
}
fs::path path = m_save_path / file_iter->path;
std::string path = combine_path(m_save_path, file_iter->path);
error_code ec;
int mode = file::read_only;
@ -982,31 +839,17 @@ namespace libtorrent
}
// returns true on success
bool storage::move_storage(fs::path save_path)
bool storage::move_storage(std::string const& sp)
{
#if TORRENT_USE_WPATH
fs::wpath old_path;
fs::wpath new_path;
#else
fs::path old_path;
fs::path new_path;
#endif
std::string save_path = complete(sp);
save_path = complete(save_path);
#if TORRENT_USE_WPATH
fs::wpath wp = convert_to_wstring(save_path.string());
if (!exists(wp))
create_directory(wp);
else if (!is_directory(wp))
error_code ec;
file_status s;
stat_file(save_path, &s, ec);
if (ec == boost::system::errc::no_such_file_or_directory)
create_directories(save_path, ec);
else if (ec)
return false;
#else
fs::path p = convert_to_native(save_path.string());
if (!exists(p))
create_directory(p);
else if (!is_directory(p))
return false;
#endif
m_pool.release(this);
@ -1017,43 +860,32 @@ namespace libtorrent
for (file_storage::iterator i = f.begin()
, end(f.end()); i != end; ++i)
{
to_move.insert(to_move.begin(), *i->path.begin());
std::string split = split_path(i->path);
to_move.insert(to_move.begin(), split);
}
for (std::set<std::string>::const_iterator i = to_move.begin()
, end(to_move.end()); i != end; ++i)
{
#if TORRENT_USE_WPATH
old_path = convert_to_wstring((m_save_path / *i).string());
new_path = convert_to_wstring((save_path / *i).string());
#else
old_path = convert_to_native((m_save_path / *i).string());
new_path = convert_to_native((save_path / *i).string());
#endif
std::string old_path = combine_path(m_save_path, *i);
std::string new_path = combine_path(save_path, *i);
#ifndef BOOST_NO_EXCEPTIONS
try
{
#endif
rename(old_path, new_path);
#ifndef BOOST_NO_EXCEPTIONS
}
catch (std::exception& e)
rename(old_path, new_path, ec);
if (ec && ec != boost::system::errc::no_such_file_or_directory)
{
error_code ec;
recursive_copy(old_path, new_path, ec);
if (ec)
{
set_error(m_save_path / files().name(), ec);
set_error(old_path, ec);
ret = false;
}
else
{
recursive_remove(old_path);
}
break;
}
#endif
}
if (ret) m_save_path = save_path;
@ -1219,7 +1051,7 @@ ret:
size_type file_offset = tor_off - file_iter->offset;
TORRENT_ASSERT(file_offset >= 0);
fs::path p(m_save_path / file_iter->path);
std::string p = combine_path(m_save_path, file_iter->path);
error_code ec;
// open the file read only to avoid re-opening
@ -1366,7 +1198,7 @@ ret:
continue;
}
fs::path path = m_save_path / file_iter->path;
std::string path = combine_path(m_save_path, file_iter->path);
error_code ec;
int mode = op.mode;
@ -1409,7 +1241,7 @@ ret:
if (ec)
{
set_error(m_save_path / file_iter->path, ec);
set_error(combine_path(m_save_path, file_iter->path), ec);
return -1;
}
@ -1486,7 +1318,7 @@ ret:
}
storage_interface* default_storage_constructor(file_storage const& fs
, file_storage const* mapped, fs::path const& path, file_pool& fp)
, file_storage const* mapped, std::string const& path, file_pool& fp)
{
return new storage(fs, mapped, path, fp);
}
@ -1508,7 +1340,7 @@ ret:
bool release_files() { return false; }
bool delete_files() { return false; }
bool initialize(bool allocate_files) { return false; }
bool move_storage(fs::path save_path) { return true; }
bool move_storage(std::string const& save_path) { return true; }
int read(char* buf, int slot, int offset, int size) { return size; }
int write(char const* buf, int slot, int offset, int size) { return size; }
size_type physical_offset(int slot, int offset) { return 0; }
@ -1566,7 +1398,7 @@ ret:
};
storage_interface* disabled_storage_constructor(file_storage const& fs
, file_storage const* mapped, fs::path const& path, file_pool& fp)
, file_storage const* mapped, std::string const& path, file_pool& fp)
{
return new disabled_storage(fs.piece_length());
}
@ -1576,7 +1408,7 @@ ret:
piece_manager::piece_manager(
boost::shared_ptr<void> const& torrent
, boost::intrusive_ptr<torrent_info const> info
, fs::path const& save_path
, std::string const& save_path
, file_pool& fp
, disk_io_thread& io
, storage_constructor_type sc
@ -1646,13 +1478,13 @@ ret:
m_io_thread.add_job(j, handler);
}
void piece_manager::async_move_storage(fs::path const& p
void piece_manager::async_move_storage(std::string const& p
, boost::function<void(int, disk_io_job const&)> const& handler)
{
disk_io_job j;
j.storage = this;
j.action = disk_io_job::move_storage;
j.str = p.string();
j.str = p;
m_io_thread.add_job(j, handler);
}
@ -1771,7 +1603,7 @@ ret:
m_io_thread.add_job(j, handler);
}
fs::path piece_manager::save_path() const
std::string piece_manager::save_path() const
{
mutex::scoped_lock l(m_mutex);
return m_save_path;
@ -1795,11 +1627,11 @@ ret:
return ph.h.final();
}
int piece_manager::move_storage_impl(fs::path const& save_path)
int piece_manager::move_storage_impl(std::string const& save_path)
{
if (m_storage->move_storage(save_path))
{
m_save_path = fs::complete(save_path);
m_save_path = complete(save_path);
return 0;
}
return -1;

View File

@ -47,7 +47,6 @@ POSSIBILITY OF SUCH DAMAGE.
#pragma warning(push, 1)
#endif
#include <boost/filesystem/convenience.hpp>
#include <boost/bind.hpp>
#ifdef _MSC_VER
@ -3688,7 +3687,7 @@ namespace libtorrent
for (torrent_info::file_iterator i = m_torrent_file->begin_files()
, end(m_torrent_file->end_files()); i != end; ++i)
{
fl.push_back(i->path.string());
fl.push_back(i->path);
}
}
@ -4447,7 +4446,7 @@ namespace libtorrent
return m_ses.m_alerts;
}
fs::path torrent::save_path() const
std::string torrent::save_path() const
{
return m_save_path;
}
@ -4466,7 +4465,7 @@ namespace libtorrent
return true;
}
void torrent::move_storage(fs::path const& save_path)
void torrent::move_storage(std::string const& save_path)
{
INVARIANT_CHECK;
@ -4480,7 +4479,7 @@ namespace libtorrent
m_save_path = save_path;
if (alerts().should_post<storage_moved_alert>())
{
alerts().post_alert(storage_moved_alert(get_handle(), m_save_path.string()));
alerts().post_alert(storage_moved_alert(get_handle(), m_save_path));
}
}
}

View File

@ -43,7 +43,6 @@ POSSIBILITY OF SUCH DAMAGE.
#pragma warning(push, 1)
#endif
#include <boost/filesystem/convenience.hpp>
#include <boost/optional.hpp>
#include <boost/bind.hpp>
@ -118,8 +117,6 @@ using libtorrent::aux::session_impl;
namespace libtorrent
{
namespace fs = boost::filesystem;
#ifndef BOOST_NO_EXCEPTIONS
void throw_invalid_handle()
{
@ -215,35 +212,35 @@ namespace libtorrent
}
void torrent_handle::move_storage(
fs::path const& save_path) const
std::string const& save_path) const
{
INVARIANT_CHECK;
TORRENT_FORWARD(move_storage(save_path));
}
#ifndef BOOST_FILESYSTEM_NARROW_ONLY
#if TORRENT_USE_WSTRING
void torrent_handle::move_storage(
fs::wpath const& save_path) const
std::wstring const& save_path) const
{
INVARIANT_CHECK;
std::string utf8;
wchar_utf8(save_path.string(), utf8);
wchar_utf8(save_path, utf8);
TORRENT_FORWARD(move_storage(utf8));
}
void torrent_handle::rename_file(int index, fs::wpath const& new_name) const
void torrent_handle::rename_file(int index, std::wstring const& new_name) const
{
INVARIANT_CHECK;
std::string utf8;
wchar_utf8(new_name.string(), utf8);
wchar_utf8(new_name, utf8);
TORRENT_FORWARD(rename_file(index, utf8));
}
#endif
#endif // TORRENT_USE_WSTRING
void torrent_handle::rename_file(int index, fs::path const& new_name) const
void torrent_handle::rename_file(int index, std::string const& new_name) const
{
INVARIANT_CHECK;
TORRENT_FORWARD(rename_file(index, new_name.string()));
TORRENT_FORWARD(rename_file(index, new_name));
}
void torrent_handle::add_extension(
@ -630,10 +627,10 @@ namespace libtorrent
}
#endif
fs::path torrent_handle::save_path() const
std::string torrent_handle::save_path() const
{
INVARIANT_CHECK;
TORRENT_FORWARD_RETURN(save_path(), fs::path());
TORRENT_FORWARD_RETURN(save_path(), std::string());
}
void torrent_handle::connect_peer(tcp::endpoint const& adr, int source) const

View File

@ -47,8 +47,6 @@ POSSIBILITY OF SUCH DAMAGE.
#pragma warning(push, 1)
#endif
#include <boost/filesystem/path.hpp>
#include <boost/filesystem.hpp>
#include <boost/bind.hpp>
#ifdef _MSC_VER
@ -72,8 +70,6 @@ namespace gr = boost::gregorian;
namespace libtorrent
{
namespace fs = boost::filesystem;
void convert_to_utf8(std::string& str, unsigned char chr)
{
str += 0xc0 | ((chr & 0xff) >> 6);
@ -187,10 +183,11 @@ namespace libtorrent
void verify_encoding(file_entry& target)
{
std::string p = target.path.string();
std::string p = target.path;
if (!verify_encoding(p)) target.path = p;
}
// TODO: should this take a char const*?
bool valid_path_element(std::string const& element)
{
if (element.empty()
@ -207,7 +204,7 @@ namespace libtorrent
if (path_element.size() > max_path_len)
{
// truncate filenames that are too long. But keep extensions!
std::string ext = fs::extension(path_element);
std::string ext = extension(path_element);
if (ext.size() > 15)
{
path_element.resize(max_path_len);
@ -220,17 +217,17 @@ namespace libtorrent
}
}
fs::path sanitize_path(fs::path const& p)
std::string sanitize_path(std::string const& p)
{
fs::path new_path;
for (fs::path::const_iterator i = p.begin(); i != p.end(); ++i)
std::string new_path;
std::string split = split_path(p);
for (char const* e = split.c_str(); e != 0; e = next_path_element(e))
{
if (!valid_path_element(*i)) continue;
std::string pe = *i;
std::string pe = e;
if (!valid_path_element(pe)) continue;
trim_path_element(pe);
new_path /= pe;
new_path = combine_path(new_path, pe);
}
TORRENT_ASSERT(!new_path.is_complete());
return new_path;
}
@ -263,14 +260,13 @@ namespace libtorrent
return false;
std::string path_element = p->list_at(i)->string_value();
trim_path_element(path_element);
target.path /= path_element;
target.path = combine_path(target.path, path_element);
}
target.path = sanitize_path(target.path);
verify_encoding(target);
TORRENT_ASSERT(!target.path.is_complete());
// bitcomet pad file
if (target.path.string().find("_____padding_file_") != std::string::npos)
if (target.path.find("_____padding_file_") != std::string::npos)
target.pad_file = true;
lazy_entry const* attr = dict.dict_find_string("attr");
@ -295,7 +291,7 @@ namespace libtorrent
{
std::string path_element = s_p->list_at(i)->string_value();
trim_path_element(path_element);
target.symlink_path /= path_element;
target.symlink_path = combine_path(target.symlink_path, path_element);
}
}
@ -311,24 +307,22 @@ namespace libtorrent
file_entry e;
if (!extract_single_file(*list.list_at(i), e, root_dir))
return false;
#if BOOST_VERSON > 103600
int cnt = 0;
for (file_storage::iterator k = target.begin()
, end(target.end()); k != end; ++k)
{
if (string_equal_no_case(e.path.string().c_str()
, k->path.string().c_str())) ++cnt;
if (string_equal_no_case(e.path.c_str()
, k->path.c_str())) ++cnt;
}
if (cnt)
{
char suffix[15];
snprintf(suffix, sizeof(suffix), ".%d", cnt);
e.path.replace_extension(suffix + e.path.extension());
replace_extension(e.path, suffix + extension(e.path));
// TODO: we should really make sure that this new name
// doesn't already exist as well, otherwise we might
// just create another collision
}
#endif
target.add_file(e);
}
return true;
@ -364,7 +358,7 @@ namespace libtorrent
return 1 << i;
}
int load_file(fs::path const& filename, std::vector<char>& v)
int load_file(std::string const& filename, std::vector<char>& v)
{
file f;
error_code ec;
@ -449,7 +443,7 @@ namespace libtorrent
throw invalid_torrent_file(ec);
}
torrent_info::torrent_info(fs::path const& filename)
torrent_info::torrent_info(std::string const& filename)
: m_creation_date(pt::ptime(pt::not_a_date_time))
, m_multifile(false)
, m_private(false)
@ -470,8 +464,8 @@ namespace libtorrent
throw invalid_torrent_file(ec);
}
#ifndef BOOST_FILESYSTEM_NARROW_ONLY
torrent_info::torrent_info(fs::wpath const& filename)
#if TORRENT_USE_WSTRING
torrent_info::torrent_info(std::wstring const& filename)
: m_creation_date(pt::ptime(pt::not_a_date_time))
, m_multifile(false)
, m_private(false)
@ -482,7 +476,7 @@ namespace libtorrent
{
std::vector<char> buf;
std::string utf8;
wchar_utf8(filename.string(), utf8);
wchar_utf8(filename, utf8);
int ret = load_file(utf8, buf);
if (ret < 0) return;
@ -527,7 +521,7 @@ namespace libtorrent
parse_torrent_file(e, ec);
}
torrent_info::torrent_info(fs::path const& filename, error_code& ec)
torrent_info::torrent_info(std::string const& filename, error_code& ec)
: m_creation_date(pt::ptime(pt::not_a_date_time))
, m_multifile(false)
, m_private(false)
@ -548,8 +542,8 @@ namespace libtorrent
parse_torrent_file(e, ec);
}
#ifndef BOOST_FILESYSTEM_NARROW_ONLY
torrent_info::torrent_info(fs::wpath const& filename, error_code& ec)
#if TORRENT_USE_WSTRING
torrent_info::torrent_info(std::wstring const& filename, error_code& ec)
: m_creation_date(pt::ptime(pt::not_a_date_time))
, m_multifile(false)
, m_private(false)
@ -559,7 +553,7 @@ namespace libtorrent
{
std::vector<char> buf;
std::string utf8;
wchar_utf8(filename.string(), utf8);
wchar_utf8(filename, utf8);
int ret = load_file(utf8, buf);
if (ret < 0) return;
@ -656,7 +650,7 @@ namespace libtorrent
return false;
}
name = sanitize_path(name).string();
name = sanitize_path(name);
if (!valid_path_element(name))
{
@ -702,11 +696,11 @@ namespace libtorrent
{
std::string path_element = s_p->list_at(i)->string_value();
trim_path_element(path_element);
e.symlink_path /= path_element;
e.symlink_path = combine_path(e.symlink_path, path_element);
}
}
// bitcomet pad file
if (e.path.string().find("_____padding_file_") != std::string::npos)
if (e.path.find("_____padding_file_") != std::string::npos)
e.pad_file = true;
if (e.size < 0)
{
@ -1038,7 +1032,7 @@ namespace libtorrent
os << "piece length: " << piece_length() << "\n";
os << "files:\n";
for (file_storage::iterator i = m_files.begin(); i != m_files.end(); ++i)
os << " " << std::setw(11) << i->size << " " << i->path.string() << "\n";
os << " " << std::setw(11) << i->size << " " << i->path << "\n";
}
// ------- end deprecation -------

View File

@ -260,13 +260,13 @@ namespace libtorrent
if (using_proxy)
{
request += m_url;
std::string path = info.orig_files().at(f.file_index).path.string();
std::string path = info.orig_files().at(f.file_index).path;
request += escape_path(path.c_str(), path.length());
}
else
{
std::string path = m_path;
path += info.orig_files().at(f.file_index).path.string();
path += info.orig_files().at(f.file_index).path;
request += escape_path(path.c_str(), path.length());
}
request += " HTTP/1.1\r\n";
@ -452,7 +452,7 @@ namespace libtorrent
int file_index = m_file_requests.front();
torrent_info const& info = t->torrent_file();
std::string path = info.orig_files().at(file_index).path.string();
std::string path = info.orig_files().at(file_index).path;
path = escape_path(path.c_str(), path.length());
size_t i = location.rfind(path);
if (i == std::string::npos)

View File

@ -38,24 +38,26 @@ POSSIBILITY OF SUCH DAMAGE.
#include <boost/thread.hpp>
#include <boost/tuple/tuple.hpp>
#include <boost/filesystem/operations.hpp>
#include <boost/filesystem/convenience.hpp>
#include "test.hpp"
#include "libtorrent/assert.hpp"
#include "libtorrent/alert_types.hpp"
#include "libtorrent/create_torrent.hpp"
using boost::filesystem::remove_all;
using boost::filesystem::create_directory;
using namespace libtorrent;
namespace sf = boost::filesystem;
bool tests_failure = false;
void report_failure(char const* err, char const* file, int line)
{
#ifdef TORRENT_WINDOWS
HANDLE console = CreateConsoleScreenBuffer(GENERIC_READ | GENERIC_WRITE, 0, 0, CONSOLE_TEXTMODE_BUFFER, 0);
SetConsoleTextAttribute(console, FOREGROUND_RED);
std::cerr << "\n**** " << file << ":" << line << " \"" << err << " ****\n\n";
CloseHandle(console);
#else
std::cerr << "\033[31m" << file << ":" << line << " \"" << err << "\"\033[0m\n";
#endif
tests_failure = true;
}
@ -106,9 +108,9 @@ void test_sleep(int millisec)
void stop_web_server(int port)
{
std::stringstream cmd;
cmd << "kill `cat ./lighty" << port << ".pid` >/dev/null";
system(cmd.str().c_str());
char buf[100];
snprintf(buf, sizeof(buf), "kill `cat ./lighty%d.pid` >/dev/null", port);
system(buf);
}
void start_web_server(int port, bool ssl)
@ -117,6 +119,7 @@ void start_web_server(int port, bool ssl)
if (ssl)
{
fprintf(stderr, "generating SSL key\n");
system("echo . > tmp");
system("echo test province >>tmp");
system("echo test city >> tmp");
@ -128,38 +131,56 @@ void start_web_server(int port, bool ssl)
"-days 365 -nodes <tmp");
}
std::ofstream f("lighty_config");
f << "server.modules = (\"mod_access\", \"mod_redirect\", \"mod_setenv\")\n"
"server.document-root = \"" << fs::initial_path<fs::path>().string() << "\"\n"
error_code ec;
file f("lighty_config", file::write_only, ec);
if (ec)
{
fprintf(stderr, "error writing lighty config file: %s\n", ec.message().c_str());
return;
}
// this requires lighttpd to be built with ssl support.
// The port distribution for mac is not built with ssl
// support by default.
char buf[1024];
int buf_size = snprintf(buf, sizeof(buf),
"server.modules = (\"mod_access\", \"mod_redirect\", \"mod_setenv\")\n"
"server.document-root = \"%s\"\n"
"server.range-requests = \"enable\"\n"
"server.port = " << port << "\n"
"server.pid-file = \"./lighty" << port << ".pid\"\n"
"server.port = %d\n"
"server.pid-file = \"./lighty%d.pid\"\n"
"url.redirect = ("
"\"^/redirect$\" => \"" << (ssl?"https":"http") << "://127.0.0.1:" << port << "/test_file\""
", \"^/infinite_redirect$\" => \"" << (ssl?"https":"http") << "://127.0.0.1:" << port << "/infinite_redirect\""
"\"^/redirect$\" => \"%s://127.0.0.1:%d/test_file\""
", \"^/infinite_redirect$\" => \"%s://127.0.0.1:%d/infinite_redirect\""
", \"^/relative/redirect$\" => \"../test_file\""
")\n"
"$HTTP[\"url\"] == \"/test_file.gz\" {\n"
" setenv.add-response-header = ( \"Content-Encoding\" => \"gzip\" )\n"
"# mimetype.assign = ()\n"
"}\n";
// this requires lighttpd to be built with ssl support.
// The port distribution for mac is not built with ssl
// support by default.
if (ssl)
f << "ssl.engine = \"enable\"\n"
"ssl.pemfile = \"server.pem\"\n";
"}\n"
"ssl.engine = \"%s\"\n"
"ssl.pemfile = \"server.pem\"\n"
, current_working_directory().c_str(), port, port
, (ssl?"https":"http"), port, (ssl?"https":"http"), port
, (ssl?"enable":"disable"));
file::iovec_t b = { buf, buf_size };
f.writev(0, &b, 1, ec);
if (ec)
{
fprintf(stderr, "error writing lighty config file: %s\n", ec.message().c_str());
return;
}
f.close();
fprintf(stderr, "starting lighty\n\n%s\n\n", buf);
system("lighttpd -f lighty_config 2> lighty.err >lighty.log &");
test_sleep(1000);
}
void stop_proxy(int port)
{
std::stringstream cmd;
cmd << "delegated -P" << port << " -Fkill";
system(cmd.str().c_str());
char buf[100];
snprintf(buf, sizeof(buf), "delegated -P%d -Fkill", port);
system(buf);
}
void start_proxy(int port, int proxy_type)
@ -167,29 +188,39 @@ void start_proxy(int port, int proxy_type)
using namespace libtorrent;
stop_proxy(port);
std::stringstream cmd;
// we need to echo n since dg will ask us to configure it
cmd << "echo n | delegated -P" << port << " ADMIN=test@test.com "
"PERMIT=\"*:*:localhost\" REMITTABLE=+,https RELAY=proxy,delegate";
char const* type = "";
char const* auth = "";
switch (proxy_type)
{
case proxy_settings::socks4:
cmd << " SERVER=socks4";
type = "socks4";
break;
case proxy_settings::socks5:
cmd << " SERVER=socks5";
type = "socks5";
break;
case proxy_settings::socks5_pw:
cmd << " SERVER=socks5 AUTHORIZER=-list{testuser:testpass}";
type = "socks5";
auth = "AUTHORIZER=-list{testuser:testpass}";
break;
case proxy_settings::http:
cmd << " SERVER=http";
type = "http";
break;
case proxy_settings::http_pw:
cmd << " SERVER=http AUTHORIZER=-list{testuser:testpass}";
type = "http";
auth = "AUTHORIZER=-list{testuser:testpass}";
break;
}
system(cmd.str().c_str());
char buf[512];
// we need to echo n since dg will ask us to configure it
snprintf(buf, sizeof(buf), "echo n | delegated -P%d ADMIN=test@test.com "
"PERMIT=\"*:*:localhost\" REMITTABLE=+,https RELAY=proxy,delegate "
"SERVER=%s %s"
, port, type, auth);
system(buf);
test_sleep(1000);
}
@ -208,11 +239,9 @@ boost::intrusive_ptr<torrent_info> create_torrent(std::ostream* file, int piece_
char const* invalid_tracker_url = "http:";
char const* invalid_tracker_protocol = "foo://non/existent-name.com/announce";
using namespace boost::filesystem;
file_storage fs;
int total_size = piece_size * num_pieces;
fs.add_file(path("temporary"), total_size);
fs.add_file("temporary", total_size);
libtorrent::create_torrent t(fs, piece_size);
t.add_tracker(tracker_url);
t.add_tracker(invalid_tracker_url);
@ -239,6 +268,7 @@ boost::intrusive_ptr<torrent_info> create_torrent(std::ostream* file, int piece_
std::vector<char> tmp;
std::back_insert_iterator<std::vector<char> > out(tmp);
bencode(out, t.generate());
return boost::intrusive_ptr<torrent_info>(new torrent_info(&tmp[0], tmp.size()));
}
@ -250,8 +280,6 @@ setup_transfer(session* ses1, session* ses2, session* ses3
, boost::intrusive_ptr<torrent_info>* torrent, bool super_seeding
, add_torrent_params const* p)
{
using namespace boost::filesystem;
assert(ses1);
assert(ses2);
@ -282,14 +310,15 @@ setup_transfer(session* ses1, session* ses2, session* ses3
boost::intrusive_ptr<torrent_info> t;
if (torrent == 0)
{
create_directory("./tmp1" + suffix);
error_code ec;
create_directory("./tmp1" + suffix, ec);
std::ofstream file(("./tmp1" + suffix + "/temporary").c_str());
t = ::create_torrent(&file, piece_size, 19);
file.close();
if (clear_files)
{
remove_all("./tmp2" + suffix + "/temporary");
remove_all("./tmp3" + suffix + "/temporary");
remove_all("./tmp2" + suffix + "/temporary", ec);
remove_all("./tmp3" + suffix + "/temporary", ec);
}
char ih_hex[41];
to_hex((char const*)&t->info_hash()[0], 20, ih_hex);

View File

@ -66,6 +66,23 @@ void report_failure(char const* str, char const* file, int line);
TEST_ERROR("Exception thrown: " #x); \
}
#define TEST_EQUAL(x, y) \
try { \
if (x != y) { \
std::stringstream s; \
s << "TEST_EQUAL_ERROR: " << #x << ": " << x << " expected: " << y << std::endl; \
TEST_REPORT_AUX(s.str().c_str(), __FILE__, __LINE__); \
} \
} \
catch (std::exception& e) \
{ \
TEST_ERROR("Exception thrown: " #x " :" + std::string(e.what())); \
} \
catch (...) \
{ \
TEST_ERROR("Exception thrown: " #x); \
}
#define TEST_ERROR(x) \
TEST_REPORT_AUX((std::string("ERROR: \"") + x + "\"").c_str(), __FILE__, __LINE__)

View File

@ -4,14 +4,10 @@
#include "libtorrent/alert_types.hpp"
#include <boost/thread.hpp>
#include <boost/tuple/tuple.hpp>
#include <boost/filesystem/operations.hpp>
#include "test.hpp"
#include "setup_transfer.hpp"
using boost::filesystem::remove_all;
using boost::filesystem::exists;
void test_swarm()
{
using namespace libtorrent;
@ -102,12 +98,12 @@ void test_swarm()
int test_main()
{
using namespace libtorrent;
using namespace boost::filesystem;
// in case the previous run was terminated
try { remove_all("./tmp1_unchoke"); } catch (std::exception&) {}
try { remove_all("./tmp2_unchoke"); } catch (std::exception&) {}
try { remove_all("./tmp3_unchoke"); } catch (std::exception&) {}
// in case the previous run was t r catch (std::exception&) {}erminated
error_code ec;
remove_all("./tmp1_unchoke", ec);
remove_all("./tmp2_unchoke", ec);
remove_all("./tmp3_unchoke", ec);
test_swarm();
@ -116,9 +112,9 @@ int test_main()
TEST_CHECK(!exists("./tmp2_unchoke/temporary"));
TEST_CHECK(!exists("./tmp3_unchoke/temporary"));
remove_all("./tmp1_unchoke");
remove_all("./tmp2_unchoke");
remove_all("./tmp3_unchoke");
remove_all("./tmp1_unchoke", ec);
remove_all("./tmp2_unchoke", ec);
remove_all("./tmp3_unchoke", ec);
return 0;
}

View File

@ -35,13 +35,10 @@ POSSIBILITY OF SUCH DAMAGE.
#include "libtorrent/hasher.hpp"
#include <boost/thread.hpp>
#include <boost/tuple/tuple.hpp>
#include <boost/filesystem/operations.hpp>
#include "test.hpp"
#include "setup_transfer.hpp"
using boost::filesystem::remove_all;
void test_lsd()
{
using namespace libtorrent;
@ -122,18 +119,18 @@ void test_lsd()
int test_main()
{
using namespace libtorrent;
using namespace boost::filesystem;
// in case the previous run was terminated
try { remove_all("./tmp1_lsd"); } catch (std::exception&) {}
try { remove_all("./tmp2_lsd"); } catch (std::exception&) {}
try { remove_all("./tmp3_lsd"); } catch (std::exception&) {}
error_code ec;
remove_all("./tmp1_lsd", ec);
remove_all("./tmp2_lsd", ec);
remove_all("./tmp3_lsd", ec);
test_lsd();
remove_all("./tmp1_lsd");
remove_all("./tmp2_lsd");
remove_all("./tmp3_lsd");
remove_all("./tmp1_lsd", ec);
remove_all("./tmp2_lsd", ec);
remove_all("./tmp3_lsd", ec);
return 0;
}

View File

@ -34,15 +34,12 @@ POSSIBILITY OF SUCH DAMAGE.
#include "libtorrent/hasher.hpp"
#include <boost/thread.hpp>
#include <boost/tuple/tuple.hpp>
#include <boost/filesystem/operations.hpp>
#include <boost/filesystem/convenience.hpp>
#include "test.hpp"
#include "setup_transfer.hpp"
#include "libtorrent/extensions/metadata_transfer.hpp"
#include "libtorrent/extensions/ut_metadata.hpp"
using boost::filesystem::remove_all;
using boost::tuples::ignore;
void test_transfer(bool clear_files, bool disconnect
@ -104,16 +101,15 @@ void test_transfer(bool clear_files, bool disconnect
TEST_CHECK(tor2.is_seed());
if (tor2.is_seed()) std::cerr << "done\n";
using boost::filesystem::remove_all;
remove_all("./tmp1_meta");
remove_all("./tmp2_meta");
remove_all("./tmp3_meta");
error_code ec;
remove_all("./tmp1_meta", ec);
remove_all("./tmp2_meta", ec);
remove_all("./tmp3_meta", ec);
}
int test_main()
{
using namespace libtorrent;
using namespace boost::filesystem;
// test to disconnect one client prematurely
test_transfer(true, true, &create_metadata_plugin);
@ -129,8 +125,9 @@ int test_main()
// test where both have data (to trigger the file check)
test_transfer(false, false, &create_ut_metadata_plugin);
remove_all("./tmp1");
remove_all("./tmp2");
error_code ec;
remove_all("./tmp1", ec);
remove_all("./tmp2", ec);
return 0;
}

View File

@ -36,7 +36,6 @@ POSSIBILITY OF SUCH DAMAGE.
#include "libtorrent/hasher.hpp"
#include "libtorrent/pe_crypto.hpp"
#include "libtorrent/session.hpp"
#include <boost/filesystem/convenience.hpp>
#include "setup_transfer.hpp"
#include "test.hpp"
@ -126,10 +125,10 @@ void test_transfer(libtorrent::pe_settings::enc_policy policy,
ses1.remove_torrent(tor1);
ses2.remove_torrent(tor2);
using boost::filesystem::remove_all;
remove_all("./tmp1_pe");
remove_all("./tmp2_pe");
remove_all("./tmp3_pe");
error_code ec;
remove_all("./tmp1_pe", ec);
remove_all("./tmp2_pe", ec);
remove_all("./tmp3_pe", ec);
}

View File

@ -36,13 +36,10 @@ POSSIBILITY OF SUCH DAMAGE.
#include "libtorrent/extensions/ut_pex.hpp"
#include <boost/thread.hpp>
#include <boost/tuple/tuple.hpp>
#include <boost/filesystem/operations.hpp>
#include "test.hpp"
#include "setup_transfer.hpp"
using boost::filesystem::remove_all;
void test_pex()
{
using namespace libtorrent;
@ -135,18 +132,18 @@ void test_pex()
int test_main()
{
using namespace libtorrent;
using namespace boost::filesystem;
// in case the previous run was terminated
try { remove_all("./tmp1_pex"); } catch (std::exception&) {}
try { remove_all("./tmp2_pex"); } catch (std::exception&) {}
try { remove_all("./tmp3_pex"); } catch (std::exception&) {}
error_code ec;
remove_all("./tmp1_pex", ec);
remove_all("./tmp2_pex", ec);
remove_all("./tmp3_pex", ec);
test_pex();
remove_all("./tmp1_pex");
remove_all("./tmp2_pex");
remove_all("./tmp3_pex");
remove_all("./tmp1_pex", ec);
remove_all("./tmp2_pex", ec);
remove_all("./tmp3_pex", ec);
return 0;
}

View File

@ -41,6 +41,7 @@ POSSIBILITY OF SUCH DAMAGE.
#include "libtorrent/escape_string.hpp"
#include "libtorrent/broadcast_socket.hpp"
#include "libtorrent/identify_client.hpp"
#include "libtorrent/file.hpp"
#ifndef TORRENT_DISABLE_DHT
#include "libtorrent/kademlia/node_id.hpp"
#include "libtorrent/kademlia/routing_table.hpp"
@ -57,7 +58,7 @@ using namespace boost::tuples;
using boost::bind;
namespace libtorrent {
fs::path sanitize_path(fs::path const& p);
std::string sanitize_path(std::string const& p);
}
sha1_hash to_hash(char const* s)
@ -365,6 +366,69 @@ void find_control_url(int type, char const* string, parse_state& state);
int test_main()
{
using namespace libtorrent;
error_code ec;
// test path functions
TEST_EQUAL(combine_path("test1/", "test2"), "test1/test2");
#ifdef TORRENT_WINDOWS
TEST_EQUAL(combine_path("test1\\", "test2"), "test1\\test2");
TEST_EQUAL(combine_path("test1", "test2"), "test1\\test2");
#else
TEST_EQUAL(combine_path("test1", "test2"), "test1/test2");
#endif
TEST_EQUAL(extension("blah"), "");
TEST_EQUAL(extension("blah.exe"), ".exe");
TEST_EQUAL(extension("blah.foo.bar"), ".bar");
TEST_EQUAL(extension("blah.foo."), ".");
TEST_EQUAL(filename("blah"), "blah");
TEST_EQUAL(filename("/blah/foo/bar"), "bar");
#ifdef TORRENT_WINDOWS
TEST_EQUAL(is_root_path("c:\\blah"), false);
TEST_EQUAL(is_root_path("c:\\"), true);
TEST_EQUAL(is_root_path("\\\\"), true);
TEST_EQUAL(is_root_path("\\\\foobar"), false);
#else
TEST_EQUAL(is_root_path("/blah"), false);
TEST_EQUAL(is_root_path("/"), true);
#endif
// if has_parent_path() returns false
// parent_path() should return the empty string
TEST_EQUAL(parent_path("blah"), "");
TEST_EQUAL(has_parent_path("blah"), false);
TEST_EQUAL(parent_path("/blah/foo/bar"), "/blah/foo/");
TEST_EQUAL(has_parent_path("/blah/foo/bar"), true);
TEST_EQUAL(parent_path("/blah/foo/bar/"), "/blah/foo/");
TEST_EQUAL(has_parent_path("/blah/foo/bar/"), true);
TEST_EQUAL(parent_path("/a"), "/");
TEST_EQUAL(has_parent_path("/a"), true);
TEST_EQUAL(parent_path("/"), "");
TEST_EQUAL(has_parent_path("/"), false);
TEST_EQUAL(parent_path(""), "");
TEST_EQUAL(has_parent_path(""), false);
#ifdef TORRENT_WINDOWS
TEST_EQUAL(parent_path("\\\\"), "");
TEST_EQUAL(has_parent_path("\\\\"), false);
TEST_EQUAL(parent_path("c:\\"), "");
TEST_EQUAL(has_parent_path("c:\\"), false);
TEST_EQUAL(parent_path("c:\\a"), "c:\\");
TEST_EQUAL(has_parent_path("c:\\a"), true);
#endif
#ifdef TORRENT_WINDOWS
TEST_EQUAL(is_complete("c:\\foo\\bar"), true);
TEST_EQUAL(is_complete("\\\\foo\\bar"), true);
TEST_EQUAL(is_complete("foo/bar"), false);
TEST_EQUAL(is_complete("\\\\"), true);
#else
TEST_EQUAL(is_complete("/foo/bar"), true);
TEST_EQUAL(is_complete("foo/bar"), false);
TEST_EQUAL(is_complete("/"), true);
TEST_EQUAL(is_complete(""), false);
#endif
#ifndef TORRENT_DISABLE_DHT
// test search_torrent_entry
@ -417,43 +481,52 @@ int test_main()
// test maybe_url_encode
TEST_CHECK(maybe_url_encode("http://test:test@abc.com/abc<>abc") == "http://test:test@abc.com:80/abc%3c%3eabc");
TEST_CHECK(maybe_url_encode("http://abc.com/foo bar") == "http://abc.com:80/foo%20bar");
TEST_CHECK(maybe_url_encode("abc") == "abc");
TEST_CHECK(maybe_url_encode("http://abc.com/abc") == "http://abc.com/abc");
TEST_EQUAL(maybe_url_encode("http://test:test@abc.com/abc<>abc"), "http://test:test@abc.com:80/abc%3c%3eabc");
TEST_EQUAL(maybe_url_encode("http://abc.com/foo bar"), "http://abc.com:80/foo%20bar");
TEST_EQUAL(maybe_url_encode("abc"), "abc");
TEST_EQUAL(maybe_url_encode("http://abc.com/abc"), "http://abc.com/abc");
// test sanitize_path
TEST_CHECK(sanitize_path("/a/b/c").string() == "a/b/c");
TEST_CHECK(sanitize_path("a/../c").string() == "a/c");
TEST_CHECK(sanitize_path("/.././c").string() == "c");
TEST_CHECK(sanitize_path("dev:").string() == "");
TEST_CHECK(sanitize_path("c:/b").string() == "b");
#ifdef TORRENT_WINDOWS
TEST_CHECK(sanitize_path("c:\\.\\c").string() == "c");
TEST_EQUAL(sanitize_path("/a/b/c"), "a\\b\\c");
TEST_EQUAL(sanitize_path("a/../c"), "a\\c");
#else
TEST_CHECK(sanitize_path("//./c").string() == "c");
TEST_EQUAL(sanitize_path("/a/b/c"), "a/b/c");
TEST_EQUAL(sanitize_path("a/../c"), "a/c");
#endif
TEST_EQUAL(sanitize_path("/.././c"), "c");
TEST_EQUAL(sanitize_path("dev:"), "");
TEST_EQUAL(sanitize_path("c:/b"), "b");
#ifdef TORRENT_WINDOWS
TEST_EQUAL(sanitize_path("c:\\.\\c"), "c");
TEST_EQUAL(sanitize_path("\\c"), "c");
#else
TEST_EQUAL(sanitize_path("//./c"), "c");
#endif
// make sure the time classes have correct semantics
TEST_CHECK(total_milliseconds(milliseconds(100)) == 100);
TEST_CHECK(total_milliseconds(milliseconds(1)) == 1);
TEST_CHECK(total_milliseconds(seconds(1)) == 1000);
TEST_EQUAL(total_milliseconds(milliseconds(100)), 100);
TEST_EQUAL(total_milliseconds(milliseconds(1)), 1);
TEST_EQUAL(total_milliseconds(seconds(1)), 1000);
// make sure the assumption we use in policy's peer list hold
std::multimap<address, int> peers;
std::multimap<address, int>::iterator i;
peers.insert(std::make_pair(address::from_string("::1"), 0));
peers.insert(std::make_pair(address::from_string("::2"), 3));
peers.insert(std::make_pair(address::from_string("::3"), 5));
i = peers.find(address::from_string("::2"));
TEST_CHECK(i != peers.end());
if (i != peers.end())
if (supports_ipv6())
{
TEST_CHECK(i->first == address::from_string("::2"));
TEST_CHECK(i->second == 3);
// make sure the assumption we use in policy's peer list hold
std::multimap<address, int> peers;
std::multimap<address, int>::iterator i;
peers.insert(std::make_pair(address::from_string("::1", ec), 0));
peers.insert(std::make_pair(address::from_string("::2", ec), 3));
peers.insert(std::make_pair(address::from_string("::3", ec), 5));
i = peers.find(address::from_string("::2", ec));
TEST_CHECK(i != peers.end());
if (i != peers.end())
{
TEST_CHECK(i->first == address::from_string("::2", ec));
TEST_CHECK(i->second == 3);
}
}
// test identify_client
@ -500,7 +573,6 @@ int test_main()
// test url parsing
error_code ec;
TEST_CHECK(parse_url_components("http://foo:bar@host.com:80/path/to/file", ec)
== make_tuple("http", "foo:bar", "host.com", 80, "/path/to/file"));
@ -817,13 +889,21 @@ int test_main()
torrent["info"] = info;
torrent_info ti2(torrent);
std::cerr << ti2.name() << std::endl;
#ifdef TORRENT_WINDOWS
TEST_CHECK(ti2.name() == "test1\\test2\\test3");
#else
TEST_CHECK(ti2.name() == "test1/test2/test3");
#endif
info["name.utf-8"] = "test2/../test3/.././../../test4";
torrent["info"] = info;
torrent_info ti3(torrent);
std::cerr << ti3.name() << std::endl;
#ifdef TORRENT_WINDOWS
TEST_CHECK(ti3.name() == "test2\\test3\\test4");
#else
TEST_CHECK(ti3.name() == "test2/test3/test4");
#endif
#ifndef TORRENT_DISABLE_DHT
// test kademlia functions

View File

@ -40,14 +40,11 @@ POSSIBILITY OF SUCH DAMAGE.
#include "libtorrent/thread.hpp"
#include <boost/utility.hpp>
#include <boost/filesystem/operations.hpp>
#include <boost/filesystem/convenience.hpp>
#include "test.hpp"
#include "setup_transfer.hpp"
using namespace libtorrent;
using namespace boost::filesystem;
const int piece_size = 16 * 1024 * 16;
const int block_size = 16 * 1024;
@ -61,7 +58,7 @@ char* piece2 = page_aligned_allocator::malloc(piece_size);
void on_read_piece(int ret, disk_io_job const& j, char const* data, int size)
{
std::cerr << "on_read_piece piece: " << j.piece << std::endl;
TEST_CHECK(ret == size);
TEST_EQUAL(ret, size);
if (ret > 0) TEST_CHECK(std::equal(j.buffer, j.buffer + ret, data));
}
@ -95,7 +92,7 @@ void on_check_files(int ret, disk_io_job const& j, bool* done)
void on_read(int ret, disk_io_job const& j, bool* done)
{
std::cerr << "on_read ret: " << ret;
std::cerr << "on_read ret: " << ret << std::endl;
*done = true;
if (ret < 0)
@ -109,8 +106,8 @@ void on_read(int ret, disk_io_job const& j, bool* done)
void on_move_storage(int ret, disk_io_job const& j, std::string path)
{
std::cerr << "on_move_storage ret: " << ret << " path: " << j.str << std::endl;
TEST_CHECK(ret == 0);
TEST_CHECK(j.str == path);
TEST_EQUAL(ret, 0);
TEST_EQUAL(j.str, path);
}
void print_error(int ret, boost::scoped_ptr<storage_interface> const& s)
@ -160,7 +157,7 @@ struct test_storage : storage_interface
virtual int sparse_end(int start) const
{ return start; }
virtual bool move_storage(fs::path save_path)
virtual bool move_storage(std::string const& save_path)
{ return false; }
virtual bool verify_resume_data(lazy_entry const& rd, error_code& error)
@ -189,7 +186,7 @@ struct test_storage : storage_interface
};
storage_interface* create_test_storage(file_storage const& fs
, file_storage const* mapped, fs::path const& path, file_pool& fp)
, file_storage const* mapped, std::string const& path, file_pool& fp)
{
return new test_storage;
}
@ -243,6 +240,7 @@ void run_elevator_test()
boost::intrusive_ptr<torrent_info> ti = ::create_torrent(0, 16, 6000);
{
error_code ec;
disk_io_thread dio(ios, &nop);
boost::intrusive_ptr<piece_manager> pm(new piece_manager(boost::shared_ptr<void>(), ti, ""
, fp, dio, &create_test_storage, storage_mode_sparse));
@ -260,9 +258,12 @@ void run_elevator_test()
}
for (int i = 0; i < 101; ++i)
ios.run_one();
{
ios.run_one(ec);
if (ec) std::cerr << "run_one: " << ec.message() << std::endl;
}
TEST_CHECK(job_counter == 0);
TEST_EQUAL(job_counter, 0);
// test the elevator going down
add_job_down(dio, 5999, pm);
@ -276,9 +277,12 @@ void run_elevator_test()
}
for (int i = 0; i < 101; ++i)
ios.run_one();
{
ios.run_one(ec);
if (ec) std::cerr << "run_one: " << ec.message() << std::endl;
}
TEST_CHECK(job_counter == 0);
TEST_EQUAL(job_counter, 0);
dio.join();
}
@ -286,14 +290,18 @@ void run_elevator_test()
void run_storage_tests(boost::intrusive_ptr<torrent_info> info
, file_storage& fs
, path const& test_path
, std::string const& test_path
, libtorrent::storage_mode_t storage_mode
, bool unbuffered)
{
TORRENT_ASSERT(fs.num_files() > 0);
create_directory(test_path / "temp_storage");
remove_all(test_path / "temp_storage2");
remove_all(test_path / "part0");
error_code ec;
create_directory(combine_path(test_path, "temp_storage"), ec);
if (ec) std::cerr << "create_directory: " << ec.message() << std::endl;
remove_all(combine_path(test_path, "temp_storage2"), ec);
if (ec) std::cerr << "remove_all: " << ec.message() << std::endl;
remove_all(combine_path(test_path, "part0"), ec);
if (ec) std::cerr << "remove_all: " << ec.message() << std::endl;
int num_pieces = fs.num_pieces();
TEST_CHECK(info->num_pieces() == num_pieces);
@ -373,6 +381,7 @@ void run_storage_tests(boost::intrusive_ptr<torrent_info> info
{
ios.reset();
ios.run_one(ec);
if (ec) std::cerr << "run_one: " << ec.message() << std::endl;
}
done = false;
@ -381,6 +390,7 @@ void run_storage_tests(boost::intrusive_ptr<torrent_info> info
{
ios.reset();
ios.run_one(ec);
if (ec) std::cerr << "run_one: " << ec.message() << std::endl;
}
done = false;
@ -393,47 +403,53 @@ void run_storage_tests(boost::intrusive_ptr<torrent_info> info
{
ios.reset();
ios.run_one(ec);
if (ec) std::cerr << "run_one: " << ec.message() << std::endl;
}
// test rename_file
remove(test_path / "part0");
TEST_CHECK(exists(test_path / "temp_storage/test1.tmp"));
TEST_CHECK(!exists(test_path / "part0"));
remove(combine_path(test_path, "part0"), ec);
if (ec) std::cerr << "remove: " << ec.message() << std::endl;
TEST_CHECK(exists(combine_path(test_path, "temp_storage/test1.tmp")));
TEST_CHECK(!exists(combine_path(test_path, "part0")));
boost::function<void(int, disk_io_job const&)> none;
pm->async_rename_file(0, "part0", none);
test_sleep(1000);
ios.reset();
ios.poll(ec);
if (ec) std::cerr << "poll: " << ec.message() << std::endl;
TEST_CHECK(!exists(test_path / "temp_storage/test1.tmp"));
TEST_CHECK(!exists(test_path / "temp_storage2"));
TEST_CHECK(exists(test_path / "part0"));
TEST_CHECK(!exists(combine_path(test_path, "temp_storage/test1.tmp")));
TEST_CHECK(!exists(combine_path(test_path, "temp_storage2")));
TEST_CHECK(exists(combine_path(test_path, "part0")));
// test move_storage with two files in the root directory
TEST_CHECK(exists(test_path / "temp_storage"));
pm->async_move_storage(test_path / "temp_storage2", bind(on_move_storage, _1, _2, (test_path / "temp_storage2").string()));
TEST_CHECK(exists(combine_path(test_path, "temp_storage")));
pm->async_move_storage(combine_path(test_path, "temp_storage2")
, bind(on_move_storage, _1, _2, combine_path(test_path, "temp_storage2")));
test_sleep(2000);
ios.reset();
ios.poll(ec);
if (ec) std::cerr << "poll: " << ec.message() << std::endl;
if (fs.num_files() > 1)
{
TEST_CHECK(!exists(test_path / "temp_storage"));
TEST_CHECK(exists(test_path / "temp_storage2/temp_storage"));
TEST_CHECK(!exists(combine_path(test_path, "temp_storage")));
TEST_CHECK(exists(combine_path(test_path, "temp_storage2/temp_storage")));
}
TEST_CHECK(exists(test_path / "temp_storage2/part0"));
TEST_CHECK(exists(combine_path(test_path, "temp_storage2/part0")));
pm->async_move_storage(test_path, bind(on_move_storage, _1, _2, test_path.string()));
pm->async_move_storage(test_path, bind(on_move_storage, _1, _2, test_path));
test_sleep(2000);
ios.reset();
ios.poll(ec);
if (ec) std::cerr << "poll: " << ec.message() << std::endl;
TEST_CHECK(exists(test_path / "part0"));
TEST_CHECK(!exists(test_path / "temp_storage2/temp_storage"));
TEST_CHECK(!exists(test_path / "temp_storage2/part0"));
TEST_CHECK(exists(combine_path(test_path, "part0")));
TEST_CHECK(!exists(combine_path(test_path, "temp_storage2/temp_storage")));
TEST_CHECK(!exists(combine_path(test_path, "temp_storage2/part0")));
r.piece = 0;
r.start = 0;
@ -449,23 +465,31 @@ void run_storage_tests(boost::intrusive_ptr<torrent_info> info
test_sleep(2000);
ios.reset();
ios.poll(ec);
if (ec) std::cerr << "poll: " << ec.message() << std::endl;
TEST_CHECK(!exists(test_path / "part0"));
TEST_CHECK(exists(test_path / "temp_storage/test1.tmp"));
TEST_CHECK(!exists(combine_path(test_path, "part0")));
TEST_CHECK(exists(combine_path(test_path, "temp_storage/test1.tmp")));
ios.reset();
ios.poll(ec);
if (ec) std::cerr << "poll: " << ec.message() << std::endl;
io.join();
remove_all(test_path / "temp_storage2");
remove_all(test_path / "part0");
remove_all(combine_path(test_path, "temp_storage2"), ec);
if (ec) std::cerr << "remove_all: " << ec.message() << std::endl;
remove_all(combine_path(test_path, "part0"), ec);
if (ec) std::cerr << "remove_all: " << ec.message() << std::endl;
}
page_aligned_allocator::free(piece);
}
void test_remove(path const& test_path, bool unbuffered)
void test_remove(std::string const& test_path, bool unbuffered)
{
file_storage fs;
error_code ec;
remove_all(combine_path(test_path, "temp_storage"), ec);
if (ec) std::cerr << "remove_all: " << ec.message() << std::endl;
TEST_CHECK(!exists(combine_path(test_path, "temp_storage")));
fs.add_file("temp_storage/test1.tmp", 8);
fs.add_file("temp_storage/folder1/test2.tmp", 8);
fs.add_file("temp_storage/folder2/test3.tmp", 0);
@ -494,12 +518,12 @@ void test_remove(path const& test_path, bool unbuffered)
// allocate the files and create the directories
s->initialize(true);
TEST_CHECK(exists(test_path / "temp_storage/_folder3/subfolder/test5.tmp"));
TEST_CHECK(exists(test_path / "temp_storage/folder2/test3.tmp"));
TEST_CHECK(exists(combine_path(test_path, "temp_storage/_folder3/subfolder/test5.tmp")));
TEST_CHECK(exists(combine_path(test_path, "temp_storage/folder2/test3.tmp")));
s->delete_files();
TEST_CHECK(!exists(test_path / "temp_storage"));
TEST_CHECK(!exists(combine_path(test_path, "temp_storage")));
}
namespace
@ -520,14 +544,16 @@ namespace
}
}
void test_check_files(path const& test_path
void test_check_files(std::string const& test_path
, libtorrent::storage_mode_t storage_mode
, bool unbuffered)
{
boost::intrusive_ptr<torrent_info> info;
error_code ec;
const int piece_size = 16 * 1024;
remove_all(test_path / "temp_storage");
remove_all(combine_path(test_path, "temp_storage"), ec);
if (ec) std::cerr << "remove_all: " << ec.message() << std::endl;
file_storage fs;
fs.add_file("temp_storage/test1.tmp", piece_size);
fs.add_file("temp_storage/test2.tmp", piece_size * 2);
@ -545,13 +571,16 @@ void test_check_files(path const& test_path
t.set_hash(2, sha1_hash(0));
t.set_hash(3, hasher(piece2, piece_size).final());
create_directory(test_path / "temp_storage");
create_directory(combine_path(test_path, "temp_storage"), ec);
if (ec) std::cerr << "create_directory: " << ec.message() << std::endl;
std::ofstream f;
f.open((test_path / "temp_storage/test1.tmp").string().c_str(), std::ios::trunc | std::ios::binary);
f.open(combine_path(test_path, "temp_storage/test1.tmp").c_str()
, std::ios::trunc | std::ios::binary);
f.write(piece0, sizeof(piece0));
f.close();
f.open((test_path / "temp_storage/test3.tmp").string().c_str(), std::ios::trunc | std::ios::binary);
f.open(combine_path(test_path, "temp_storage/test3.tmp").c_str()
, std::ios::trunc | std::ios::binary);
f.write(piece2, sizeof(piece2));
f.close();
@ -565,7 +594,6 @@ void test_check_files(path const& test_path
, test_path, fp, io, default_storage_constructor, storage_mode);
mutex lock;
error_code ec;
bool done = false;
lazy_entry frd;
pm->async_check_fastresume(&frd, boost::bind(&on_check_resume_data, _1, _2, &done));
@ -574,6 +602,7 @@ void test_check_files(path const& test_path
{
ios.reset();
ios.run_one(ec);
if (ec) std::cerr << "run_one: " << ec.message() << std::endl;
}
bool pieces[4] = {false, false, false, false};
@ -584,22 +613,25 @@ void test_check_files(path const& test_path
{
ios.reset();
ios.run_one(ec);
if (ec) std::cerr << "run_one: " << ec.message() << std::endl;
}
TEST_CHECK(pieces[0] == true);
TEST_CHECK(pieces[1] == false);
TEST_CHECK(pieces[2] == false);
TEST_CHECK(pieces[3] == true);
TEST_EQUAL(pieces[0], true);
TEST_EQUAL(pieces[1], false);
TEST_EQUAL(pieces[2], false);
TEST_EQUAL(pieces[3], true);
io.join();
}
void run_test(path const& test_path, bool unbuffered)
void run_test(std::string const& test_path, bool unbuffered)
{
std::cerr << "\n=== " << test_path.string() << " ===\n" << std::endl;
std::cerr << "\n=== " << test_path << " ===\n" << std::endl;
boost::intrusive_ptr<torrent_info> info;
{
remove_all(test_path / "temp_storage");
error_code ec;
remove_all(combine_path(test_path, "temp_storage"), ec);
if (ec) std::cerr << "remove_all: " << ec.message() << std::endl;
file_storage fs;
fs.add_file("temp_storage/test1.tmp", 17);
fs.add_file("temp_storage/test2.tmp", 612);
@ -621,25 +653,23 @@ void run_test(path const& test_path, bool unbuffered)
run_storage_tests(info, fs, test_path, storage_mode_compact, unbuffered);
// make sure the files have the correct size
TEST_CHECK(file_size(test_path / "temp_storage" / "test1.tmp") == 17);
TEST_CHECK(file_size(test_path / "temp_storage" / "test2.tmp") == 612);
TEST_CHECK(exists(test_path / "temp_storage/test3.tmp"));
TEST_CHECK(exists(test_path / "temp_storage/test4.tmp"));
TEST_CHECK(file_size(test_path / "temp_storage" / "test5.tmp") == 3253);
TEST_CHECK(file_size(test_path / "temp_storage" / "test6.tmp") == 841);
TEST_CHECK(file_size(test_path / "temp_storage" / "test7.tmp") == last_file_size - piece_size);
std::cerr << file_size(test_path / "temp_storage" / "test1.tmp") << std::endl;
std::cerr << file_size(test_path / "temp_storage" / "test2.tmp") << std::endl;
std::cerr << file_size(test_path / "temp_storage" / "test3.tmp") << std::endl;
std::cerr << file_size(test_path / "temp_storage" / "test4.tmp") << std::endl;
std::cerr << file_size(test_path / "temp_storage" / "test5.tmp") << std::endl;
std::cerr << file_size(test_path / "temp_storage" / "test6.tmp") << std::endl;
remove_all(test_path / "temp_storage");
std::string base = combine_path(test_path, "temp_storage");
TEST_EQUAL(file_size(combine_path(base, "test1.tmp")), 17);
TEST_EQUAL(file_size(combine_path(base, "test2.tmp")), 612);
// these files should have been allocated since they are 0 sized
TEST_CHECK(exists(combine_path(base, "test3.tmp")));
TEST_CHECK(exists(combine_path(base, "test4.tmp")));
TEST_EQUAL(file_size(combine_path(base, "test5.tmp")), 3253);
TEST_EQUAL(file_size(combine_path(base, "test6.tmp")), 841);
TEST_EQUAL(file_size(combine_path(base, "test7.tmp")), last_file_size - piece_size);
remove_all(combine_path(test_path, "temp_storage"), ec);
if (ec) std::cerr << "remove_all: " << ec.message() << std::endl;
}
// ==============================================
{
error_code ec;
file_storage fs;
fs.add_file("temp_storage/test1.tmp", 3 * piece_size);
libtorrent::create_torrent t(fs, piece_size, -1, 0);
@ -654,8 +684,9 @@ void run_test(path const& test_path, bool unbuffered)
run_storage_tests(info, fs, test_path, storage_mode_compact, unbuffered);
TEST_CHECK(file_size(test_path / "temp_storage" / "test1.tmp") == piece_size * 3);
remove_all(test_path / "temp_storage");
TEST_EQUAL(file_size(combine_path(test_path, "temp_storage/test1.tmp")), piece_size * 3);
remove_all(combine_path(test_path, "temp_storage"), ec);
if (ec) std::cerr << "remove_all: " << ec.message() << std::endl;
// ==============================================
@ -663,10 +694,11 @@ void run_test(path const& test_path, bool unbuffered)
run_storage_tests(info, fs, test_path, storage_mode_allocate, unbuffered);
std::cerr << file_size(test_path / "temp_storage" / "test1.tmp") << std::endl;
TEST_CHECK(file_size(test_path / "temp_storage" / "test1.tmp") == 3 * piece_size);
std::cerr << file_size(combine_path(test_path, "temp_storage/test1.tmp")) << std::endl;
TEST_EQUAL(file_size(combine_path(test_path, "temp_storage/test1.tmp")), 3 * piece_size);
remove_all(test_path / "temp_storage");
remove_all(combine_path(test_path, "temp_storage"), ec);
if (ec) std::cerr << "remove_all: " << ec.message() << std::endl;
}
@ -682,15 +714,18 @@ void run_test(path const& test_path, bool unbuffered)
test_check_files(test_path, storage_mode_compact, unbuffered);
}
void test_fastresume(path const& test_path)
void test_fastresume(std::string const& test_path)
{
error_code ec;
std::cout << "\n\n=== test fastresume ===" << std::endl;
remove_all(test_path / "tmp1");
create_directory(test_path / "tmp1");
std::ofstream file((test_path / "tmp1/temporary").external_file_string().c_str());
remove_all(combine_path(test_path, "tmp1"), ec);
if (ec) std::cerr << "remove_all: " << ec.message() << std::endl;
create_directory(combine_path(test_path, "tmp1"), ec);
if (ec) std::cerr << "create_directory: " << ec.message() << std::endl;
std::ofstream file(combine_path(test_path, "tmp1/temporary").c_str());
boost::intrusive_ptr<torrent_info> t = ::create_torrent(&file);
file.close();
TEST_CHECK(exists(test_path / "tmp1/temporary"));
TEST_CHECK(exists(combine_path(test_path, "tmp1/temporary")));
entry resume;
{
@ -698,8 +733,7 @@ void test_fastresume(path const& test_path)
ses.set_alert_mask(alert::all_categories);
torrent_handle h = ses.add_torrent(boost::intrusive_ptr<torrent_info>(new torrent_info(*t))
, test_path / "tmp1", entry()
, storage_mode_compact);
, combine_path(test_path, "tmp1"), entry(), storage_mode_compact);
for (int i = 0; i < 10; ++i)
{
@ -715,7 +749,7 @@ void test_fastresume(path const& test_path)
resume = h.write_resume_data();
ses.remove_torrent(h, session::delete_files);
}
TEST_CHECK(!exists(test_path / "tmp1/temporary"));
TEST_CHECK(!exists(combine_path(test_path, "tmp1/temporary")));
#if defined TORRENT_DEBUG && TORRENT_USE_IOSTREAM
resume.print(std::cout);
#endif
@ -724,7 +758,7 @@ void test_fastresume(path const& test_path)
{
session ses(fingerprint(" ", 0,0,0,0), 0);
ses.set_alert_mask(alert::all_categories);
torrent_handle h = ses.add_torrent(t, test_path / "tmp1", resume
torrent_handle h = ses.add_torrent(t, combine_path(test_path, "tmp1"), resume
, storage_mode_compact);
std::auto_ptr<alert> a = ses.pop_alert();
@ -742,7 +776,8 @@ void test_fastresume(path const& test_path)
}
TEST_CHECK(dynamic_cast<fastresume_rejected_alert*>(a.get()) != 0);
}
remove_all(test_path / "tmp1");
remove_all(combine_path(test_path, "tmp1"), ec);
if (ec) std::cerr << "remove_all: " << ec.message() << std::endl;
}
bool got_file_rename_alert(alert* a)
@ -751,15 +786,18 @@ bool got_file_rename_alert(alert* a)
|| dynamic_cast<libtorrent::file_rename_failed_alert*>(a);
}
void test_rename_file_in_fastresume(path const& test_path)
void test_rename_file_in_fastresume(std::string const& test_path)
{
error_code ec;
std::cout << "\n\n=== test rename file in fastresume ===" << std::endl;
remove_all(test_path / "tmp2");
create_directory(test_path / "tmp2");
std::ofstream file((test_path / "tmp2/temporary").external_file_string().c_str());
remove_all(combine_path(test_path, "tmp2"), ec);
if (ec) std::cerr << "remove_all: " << ec.message() << std::endl;
create_directory(combine_path(test_path, "tmp2"), ec);
if (ec) std::cerr << "create_directory: " << ec.message() << std::endl;
std::ofstream file(combine_path(test_path, "tmp2/temporary").c_str());
boost::intrusive_ptr<torrent_info> t = ::create_torrent(&file);
file.close();
TEST_CHECK(exists(test_path / "tmp2/temporary"));
TEST_CHECK(exists(combine_path(test_path, "tmp2/temporary")));
entry resume;
{
@ -767,7 +805,7 @@ void test_rename_file_in_fastresume(path const& test_path)
ses.set_alert_mask(alert::all_categories);
torrent_handle h = ses.add_torrent(boost::intrusive_ptr<torrent_info>(new torrent_info(*t))
, test_path / "tmp2", entry()
, combine_path(test_path, "tmp2"), entry()
, storage_mode_compact);
h.rename_file(0, "testing_renamed_files");
@ -786,8 +824,8 @@ void test_rename_file_in_fastresume(path const& test_path)
resume = h.write_resume_data();
ses.remove_torrent(h);
}
TEST_CHECK(!exists(test_path / "tmp2/temporary"));
TEST_CHECK(exists(test_path / "tmp2/testing_renamed_files"));
TEST_CHECK(!exists(combine_path(test_path, "tmp2/temporary")));
TEST_CHECK(exists(combine_path(test_path, "tmp2/testing_renamed_files")));
TEST_CHECK(resume.dict().find("mapped_files") != resume.dict().end());
#if defined TORRENT_DEBUG && TORRENT_USE_IOSTREAM
resume.print(std::cout);
@ -797,7 +835,7 @@ void test_rename_file_in_fastresume(path const& test_path)
{
session ses(fingerprint(" ", 0,0,0,0), 0);
ses.set_alert_mask(alert::all_categories);
torrent_handle h = ses.add_torrent(t, test_path / "tmp2", resume
torrent_handle h = ses.add_torrent(t, combine_path(test_path, "tmp2"), resume
, storage_mode_compact);
for (int i = 0; i < 5; ++i)
@ -815,7 +853,8 @@ void test_rename_file_in_fastresume(path const& test_path)
#if defined TORRENT_DEBUG && TORRENT_USE_IOSTREAM
resume.print(std::cout);
#endif
remove_all(test_path / "tmp2");
remove_all(combine_path(test_path, "tmp2"), ec);
if (ec) std::cerr << "remove_all: " << ec.message() << std::endl;
}
int test_main()
@ -831,11 +870,11 @@ int test_main()
for (char* p = piece2, *end(piece2 + piece_size); p < end; ++p)
*p = rand();
std::vector<path> test_paths;
std::vector<std::string> test_paths;
char* env = std::getenv("TORRENT_TEST_PATHS");
if (env == 0)
{
test_paths.push_back(initial_path<path>());
test_paths.push_back(current_working_directory());
}
else
{

View File

@ -36,22 +36,19 @@ POSSIBILITY OF SUCH DAMAGE.
#include "libtorrent/alert_types.hpp"
#include <boost/thread.hpp>
#include <boost/tuple/tuple.hpp>
#include <boost/filesystem/operations.hpp>
#include "test.hpp"
#include "setup_transfer.hpp"
using boost::filesystem::remove_all;
using boost::filesystem::exists;
void test_swarm(bool super_seeding = false, bool strict = false, bool seed_mode = false, bool time_critical = false)
{
using namespace libtorrent;
// in case the previous run was terminated
try { remove_all("./tmp1_swarm"); } catch (std::exception&) {}
try { remove_all("./tmp2_swarm"); } catch (std::exception&) {}
try { remove_all("./tmp3_swarm"); } catch (std::exception&) {}
error_code ec;
remove_all("./tmp1_swarm", ec);
remove_all("./tmp2_swarm", ec);
remove_all("./tmp3_swarm", ec);
session ses1(fingerprint("LT", 0, 1, 0, 0), std::make_pair(48000, 49000), "0.0.0.0", 0);
session ses2(fingerprint("LT", 0, 1, 0, 0), std::make_pair(49000, 50000), "0.0.0.0", 0);
@ -201,15 +198,14 @@ void test_swarm(bool super_seeding = false, bool strict = false, bool seed_mode
TEST_CHECK(!exists("./tmp2_swarm/temporary"));
TEST_CHECK(!exists("./tmp3_swarm/temporary"));
remove_all("./tmp1_swarm");
remove_all("./tmp2_swarm");
remove_all("./tmp3_swarm");
remove_all("./tmp1_swarm", ec);
remove_all("./tmp2_swarm", ec);
remove_all("./tmp3_swarm", ec);
}
int test_main()
{
using namespace libtorrent;
using namespace boost::filesystem;
// with time critical pieces
test_swarm(false, false, false, true);

View File

@ -34,8 +34,6 @@ POSSIBILITY OF SUCH DAMAGE.
#include "libtorrent/hasher.hpp"
#include <boost/thread.hpp>
#include <boost/tuple/tuple.hpp>
#include <boost/filesystem/operations.hpp>
#include <boost/filesystem/convenience.hpp>
#include "test.hpp"
#include "setup_transfer.hpp"
@ -43,7 +41,6 @@ POSSIBILITY OF SUCH DAMAGE.
#include "libtorrent/extensions/ut_metadata.hpp"
#include "libtorrent/extensions/lt_trackers.hpp"
using boost::filesystem::remove_all;
using boost::tuples::ignore;
int test_main()

View File

@ -37,14 +37,10 @@ POSSIBILITY OF SUCH DAMAGE.
#include "libtorrent/bencode.hpp"
#include <boost/thread.hpp>
#include <boost/tuple/tuple.hpp>
#include <boost/filesystem/operations.hpp>
#include "test.hpp"
#include "setup_transfer.hpp"
using boost::filesystem::remove_all;
using boost::filesystem::exists;
using boost::filesystem::create_directory;
using namespace libtorrent;
using boost::tuples::ignore;
@ -52,10 +48,11 @@ using boost::tuples::ignore;
void test_rate()
{
// in case the previous run was terminated
try { remove_all("./tmp1_transfer"); } catch (std::exception&) {}
try { remove_all("./tmp2_transfer"); } catch (std::exception&) {}
try { remove_all("./tmp1_transfer_moved"); } catch (std::exception&) {}
try { remove_all("./tmp2_transfer_moved"); } catch (std::exception&) {}
error_code ec;
remove_all("./tmp1_transfer", ec);
remove_all("./tmp2_transfer", ec);
remove_all("./tmp1_transfer_moved", ec);
remove_all("./tmp2_transfer_moved", ec);
session ses1(fingerprint("LT", 0, 1, 0, 0), std::make_pair(48575, 49000), "0.0.0.0", 0);
session ses2(fingerprint("LT", 0, 1, 0, 0), std::make_pair(49575, 50000), "0.0.0.0", 0);
@ -63,7 +60,7 @@ void test_rate()
torrent_handle tor1;
torrent_handle tor2;
create_directory("./tmp1_transfer");
create_directory("./tmp1_transfer", ec);
std::ofstream file("./tmp1_transfer/temporary");
boost::intrusive_ptr<torrent_info> t = ::create_torrent(&file, 4 * 1024 * 1024, 7);
file.close();
@ -114,7 +111,7 @@ void print_alert(alert const& a)
// simulate a full disk
struct test_storage : storage_interface
{
test_storage(file_storage const& fs, fs::path const& p, file_pool& fp)
test_storage(file_storage const& fs, std::string const& p, file_pool& fp)
: m_lower_layer(default_storage_constructor(fs, 0, p, fp))
, m_written(0)
, m_limit(16 * 1024 * 2)
@ -174,7 +171,7 @@ struct test_storage : storage_interface
virtual int sparse_end(int start) const
{ return m_lower_layer->sparse_end(start); }
virtual bool move_storage(fs::path save_path)
virtual bool move_storage(std::string const& save_path)
{ return m_lower_layer->move_storage(save_path); }
virtual bool verify_resume_data(lazy_entry const& rd, error_code& error)
@ -207,7 +204,7 @@ struct test_storage : storage_interface
};
storage_interface* test_storage_constructor(file_storage const& fs
, file_storage const*, fs::path const& path, file_pool& fp)
, file_storage const*, std::string const& path, file_pool& fp)
{
return new test_storage(fs, path, fp);
}
@ -215,10 +212,11 @@ storage_interface* test_storage_constructor(file_storage const& fs
void test_transfer(bool test_disk_full = false, bool test_allowed_fast = false)
{
// in case the previous run was terminated
try { remove_all("./tmp1_transfer"); } catch (std::exception&) {}
try { remove_all("./tmp2_transfer"); } catch (std::exception&) {}
try { remove_all("./tmp1_transfer_moved"); } catch (std::exception&) {}
try { remove_all("./tmp2_transfer_moved"); } catch (std::exception&) {}
error_code ec;
remove_all("./tmp1_transfer", ec);
remove_all("./tmp2_transfer", ec);
remove_all("./tmp1_transfer_moved", ec);
remove_all("./tmp2_transfer_moved", ec);
session ses1(fingerprint("LT", 0, 1, 0, 0), std::make_pair(48075, 49000), "0.0.0.0", 0);
session ses2(fingerprint("LT", 0, 1, 0, 0), std::make_pair(49075, 50000), "0.0.0.0", 0);
@ -242,7 +240,7 @@ void test_transfer(bool test_disk_full = false, bool test_allowed_fast = false)
torrent_handle tor1;
torrent_handle tor2;
create_directory("./tmp1_transfer");
create_directory("./tmp1_transfer", ec);
std::ofstream file("./tmp1_transfer/temporary");
boost::intrusive_ptr<torrent_info> t = ::create_torrent(&file, 16 * 1024);
file.close();
@ -456,7 +454,6 @@ void test_transfer(bool test_disk_full = false, bool test_allowed_fast = false)
int test_main()
{
using namespace libtorrent;
using namespace boost::filesystem;
#ifdef NDEBUG
// test rate only makes sense in release mode
@ -471,10 +468,11 @@ int test_main()
// test allowed fast
test_transfer(false, true);
try { remove_all("./tmp1_transfer"); } catch (std::exception&) {}
try { remove_all("./tmp2_transfer"); } catch (std::exception&) {}
try { remove_all("./tmp1_transfer_moved"); } catch (std::exception&) {}
try { remove_all("./tmp2_transfer_moved"); } catch (std::exception&) {}
error_code ec;
remove_all("./tmp1_transfer", ec);
remove_all("./tmp2_transfer", ec);
remove_all("./tmp1_transfer_moved", ec);
remove_all("./tmp2_transfer_moved", ec);
return 0;
}

View File

@ -38,13 +38,11 @@ POSSIBILITY OF SUCH DAMAGE.
#include "libtorrent/create_torrent.hpp"
#include <boost/thread.hpp>
#include <boost/tuple/tuple.hpp>
#include <boost/filesystem/operations.hpp>
#include <fstream>
#include "test.hpp"
#include "setup_transfer.hpp"
using namespace boost::filesystem;
using namespace libtorrent;
// proxy: 0=none, 1=socks4, 2=socks5, 3=socks5_pw 4=http 5=http_pw
@ -60,7 +58,8 @@ void test_transfer(boost::intrusive_ptr<torrent_info> torrent_file, int proxy)
ses.set_alert_mask(~alert::progress_notification);
ses.listen_on(std::make_pair(51000, 52000));
ses.set_download_rate_limit(torrent_file->total_size() / 10);
remove_all("./tmp2_web_seed");
error_code ec;
remove_all("./tmp2_web_seed", ec);
char const* test_name[] = {"no", "SOCKS4", "SOCKS5", "SOCKS5 password", "HTTP", "HTTP password"};
@ -139,22 +138,16 @@ void test_transfer(boost::intrusive_ptr<torrent_info> torrent_file, int proxy)
if (proxy) stop_proxy(8002);
TEST_CHECK(exists("./tmp2_web_seed" / torrent_file->file_at(0).path));
remove_all("./tmp2_web_seed");
TEST_CHECK(exists(combine_path("./tmp2_web_seed", torrent_file->file_at(0).path)));
remove_all("./tmp2_web_seed", ec);
}
int test_main()
{
using namespace libtorrent;
using namespace boost::filesystem;
try {
create_directory("./tmp1_web_seed");
} catch (std::exception&) {}
try {
create_directory("./tmp1_web_seed/test_torrent_dir");
} catch (std::exception&) {}
error_code ec;
create_directories("./tmp1_web_seed/test_torrent_dir", ec);
char random_data[300000];
std::srand(10);
@ -169,7 +162,7 @@ int test_main()
std::ofstream("./tmp1_web_seed/test_torrent_dir/test7").write(random_data, 300000);
file_storage fs;
add_files(fs, path("./tmp1_web_seed/test_torrent_dir"));
add_files(fs, "./tmp1_web_seed/test_torrent_dir");
libtorrent::create_torrent t(fs, 16 * 1024);
t.add_url_seed("http://127.0.0.1:8000/tmp1_web_seed");
@ -187,7 +180,7 @@ int test_main()
test_transfer(torrent_file, 0);
stop_web_server(8000);
remove_all("./tmp1_web_seed");
remove_all("./tmp1_web_seed", ec);
return 0;
}