2012-06-30 01:10:28 +02:00
|
|
|
/*
|
|
|
|
|
|
|
|
Copyright (c) 2012, Arvid Norberg
|
|
|
|
All rights reserved.
|
|
|
|
|
|
|
|
Redistribution and use in source and binary forms, with or without
|
|
|
|
modification, are permitted provided that the following conditions
|
|
|
|
are met:
|
|
|
|
|
|
|
|
* Redistributions of source code must retain the above copyright
|
|
|
|
notice, this list of conditions and the following disclaimer.
|
|
|
|
* Redistributions in binary form must reproduce the above copyright
|
|
|
|
notice, this list of conditions and the following disclaimer in
|
|
|
|
the documentation and/or other materials provided with the distribution.
|
|
|
|
* Neither the name of the author nor the names of its
|
|
|
|
contributors may be used to endorse or promote products derived
|
|
|
|
from this software without specific prior written permission.
|
|
|
|
|
|
|
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
|
|
|
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "libtorrent/file.hpp"
|
2017-04-11 06:52:46 +02:00
|
|
|
#include "libtorrent/aux_/path.hpp"
|
2016-08-18 23:08:40 +02:00
|
|
|
#include "libtorrent/string_util.hpp" // for split_string
|
2017-03-18 17:19:49 +01:00
|
|
|
#include "libtorrent/string_view.hpp"
|
2012-06-30 01:10:28 +02:00
|
|
|
#include "test.hpp"
|
|
|
|
#include <vector>
|
|
|
|
#include <set>
|
2016-06-26 15:24:06 +02:00
|
|
|
#include <thread>
|
2012-06-30 01:10:28 +02:00
|
|
|
|
2017-04-12 20:05:53 +02:00
|
|
|
using namespace lt;
|
2012-06-30 01:10:28 +02:00
|
|
|
|
|
|
|
int touch_file(std::string const& filename, int size)
|
|
|
|
{
|
2017-04-12 20:05:53 +02:00
|
|
|
using namespace lt;
|
2012-06-30 01:10:28 +02:00
|
|
|
|
|
|
|
std::vector<char> v;
|
|
|
|
v.resize(size);
|
|
|
|
for (int i = 0; i < size; ++i)
|
|
|
|
v[i] = i & 255;
|
|
|
|
|
|
|
|
file f;
|
|
|
|
error_code ec;
|
|
|
|
if (!f.open(filename, file::write_only, ec)) return -1;
|
|
|
|
if (ec) return -1;
|
2017-01-11 06:42:10 +01:00
|
|
|
iovec_t b = {&v[0], v.size()};
|
2016-10-27 02:40:56 +02:00
|
|
|
std::int64_t written = f.writev(0, b, ec);
|
2012-06-30 01:10:28 +02:00
|
|
|
if (written != int(v.size())) return -3;
|
|
|
|
if (ec) return -3;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-05-30 23:10:34 +02:00
|
|
|
TORRENT_TEST(create_directory)
|
2014-04-04 09:30:54 +02:00
|
|
|
{
|
|
|
|
error_code ec;
|
|
|
|
create_directory("__foobar__", ec);
|
2016-12-21 07:00:49 +01:00
|
|
|
if (ec) std::printf("ERROR: create_directory: (%d) %s\n"
|
|
|
|
, ec.value(), ec.message().c_str());
|
2014-04-04 09:30:54 +02:00
|
|
|
TEST_CHECK(!ec);
|
|
|
|
|
|
|
|
file_status st;
|
|
|
|
stat_file("__foobar__", &st, ec);
|
2016-12-21 07:00:49 +01:00
|
|
|
if (ec) std::printf("ERROR: stat_file: (%d) %s\n"
|
|
|
|
, ec.value(), ec.message().c_str());
|
2014-04-04 09:30:54 +02:00
|
|
|
TEST_CHECK(!ec);
|
|
|
|
|
|
|
|
TEST_CHECK(st.mode & file_status::directory);
|
|
|
|
|
|
|
|
remove("__foobar__", ec);
|
2016-12-21 07:00:49 +01:00
|
|
|
if (ec) std::printf("ERROR: remove: (%d) %s\n"
|
|
|
|
, ec.value(), ec.message().c_str());
|
2014-04-04 09:30:54 +02:00
|
|
|
TEST_CHECK(!ec);
|
|
|
|
}
|
|
|
|
|
2015-05-30 23:10:34 +02:00
|
|
|
TORRENT_TEST(file_status)
|
2014-04-04 09:30:54 +02:00
|
|
|
{
|
|
|
|
error_code ec;
|
|
|
|
|
|
|
|
// test that the modification timestamps
|
|
|
|
touch_file("__test_timestamp__", 10);
|
|
|
|
|
|
|
|
file_status st1;
|
|
|
|
stat_file("__test_timestamp__", &st1, ec);
|
|
|
|
TEST_CHECK(!ec);
|
|
|
|
|
|
|
|
// sleep for 3 seconds and then make sure the difference in timestamp is
|
|
|
|
// between 2-4 seconds after touching it again
|
2016-06-26 15:24:06 +02:00
|
|
|
std::this_thread::sleep_for(lt::milliseconds(3000));
|
2014-04-04 09:30:54 +02:00
|
|
|
|
|
|
|
touch_file("__test_timestamp__", 10);
|
|
|
|
|
|
|
|
file_status st2;
|
|
|
|
stat_file("__test_timestamp__", &st2, ec);
|
|
|
|
TEST_CHECK(!ec);
|
|
|
|
|
|
|
|
int diff = int(st2.mtime - st1.mtime);
|
2016-10-10 02:23:45 +02:00
|
|
|
std::printf("timestamp difference: %d seconds. expected approx. 3 seconds\n"
|
2014-04-04 09:30:54 +02:00
|
|
|
, diff);
|
|
|
|
|
|
|
|
TEST_CHECK(diff >= 2 && diff <= 4);
|
|
|
|
}
|
|
|
|
|
2015-05-30 23:10:34 +02:00
|
|
|
TORRENT_TEST(directory)
|
2012-06-30 01:10:28 +02:00
|
|
|
{
|
|
|
|
error_code ec;
|
|
|
|
|
|
|
|
create_directory("file_test_dir", ec);
|
2016-10-10 02:23:45 +02:00
|
|
|
if (ec) std::printf("create_directory: %s\n", ec.message().c_str());
|
2012-06-30 01:10:28 +02:00
|
|
|
TEST_CHECK(!ec);
|
|
|
|
|
|
|
|
std::string cwd = current_working_directory();
|
|
|
|
|
|
|
|
touch_file(combine_path("file_test_dir", "abc"), 10);
|
|
|
|
touch_file(combine_path("file_test_dir", "def"), 100);
|
|
|
|
touch_file(combine_path("file_test_dir", "ghi"), 1000);
|
|
|
|
|
|
|
|
std::set<std::string> files;
|
|
|
|
for (directory i("file_test_dir", ec); !i.done(); i.next(ec))
|
|
|
|
{
|
|
|
|
std::string f = i.file();
|
|
|
|
TEST_CHECK(files.count(f) == 0);
|
|
|
|
files.insert(f);
|
2016-10-10 02:23:45 +02:00
|
|
|
std::printf(" %s\n", f.c_str());
|
2012-06-30 01:10:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CHECK(files.count("abc") == 1);
|
|
|
|
TEST_CHECK(files.count("def") == 1);
|
|
|
|
TEST_CHECK(files.count("ghi") == 1);
|
|
|
|
TEST_CHECK(files.count("..") == 1);
|
|
|
|
TEST_CHECK(files.count(".") == 1);
|
2012-06-30 04:35:38 +02:00
|
|
|
files.clear();
|
|
|
|
|
|
|
|
recursive_copy("file_test_dir", "file_test_dir2", ec);
|
|
|
|
|
|
|
|
for (directory i("file_test_dir2", ec); !i.done(); i.next(ec))
|
|
|
|
{
|
|
|
|
std::string f = i.file();
|
|
|
|
TEST_CHECK(files.count(f) == 0);
|
|
|
|
files.insert(f);
|
2016-10-10 02:23:45 +02:00
|
|
|
std::printf(" %s\n", f.c_str());
|
2012-06-30 04:35:38 +02:00
|
|
|
}
|
2012-06-30 01:10:28 +02:00
|
|
|
|
|
|
|
remove_all("file_test_dir", ec);
|
2016-10-10 02:23:45 +02:00
|
|
|
if (ec) std::printf("remove_all: %s\n", ec.message().c_str());
|
2012-06-30 04:35:38 +02:00
|
|
|
remove_all("file_test_dir2", ec);
|
2016-10-10 02:23:45 +02:00
|
|
|
if (ec) std::printf("remove_all: %s\n", ec.message().c_str());
|
2015-05-30 23:10:34 +02:00
|
|
|
}
|
2012-06-30 01:10:28 +02:00
|
|
|
|
2015-05-30 23:10:34 +02:00
|
|
|
// test path functions
|
|
|
|
TORRENT_TEST(paths)
|
|
|
|
{
|
2013-09-01 03:10:50 +02:00
|
|
|
TEST_EQUAL(combine_path("test1/", "test2"), "test1/test2");
|
2014-12-17 15:33:52 +01:00
|
|
|
TEST_EQUAL(combine_path("test1", "."), "test1");
|
|
|
|
TEST_EQUAL(combine_path(".", "test1"), "test1");
|
2013-09-01 03:10:50 +02:00
|
|
|
#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."), ".");
|
2013-10-24 09:22:44 +02:00
|
|
|
TEST_EQUAL(extension("blah.foo/bar"), "");
|
2013-09-01 03:10:50 +02:00
|
|
|
|
2013-11-18 07:59:47 +01:00
|
|
|
TEST_EQUAL(remove_extension("blah"), "blah");
|
|
|
|
TEST_EQUAL(remove_extension("blah.exe"), "blah");
|
|
|
|
TEST_EQUAL(remove_extension("blah.foo.bar"), "blah.foo");
|
|
|
|
TEST_EQUAL(remove_extension("blah.foo."), "blah.foo");
|
|
|
|
|
2013-09-01 03:10:50 +02:00
|
|
|
TEST_EQUAL(filename("blah"), "blah");
|
|
|
|
TEST_EQUAL(filename("/blah/foo/bar"), "bar");
|
|
|
|
TEST_EQUAL(filename("/blah/foo/bar/"), "bar");
|
|
|
|
TEST_EQUAL(filename("blah/"), "blah");
|
|
|
|
|
|
|
|
#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"), true);
|
|
|
|
TEST_EQUAL(is_root_path("\\\\foobar\\"), true);
|
|
|
|
TEST_EQUAL(is_root_path("\\\\foobar/"), true);
|
|
|
|
TEST_EQUAL(is_root_path("\\\\foo/bar"), false);
|
|
|
|
TEST_EQUAL(is_root_path("\\\\foo\\bar\\"), 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);
|
|
|
|
TEST_EQUAL(has_parent_path("\\\\a"), false);
|
|
|
|
TEST_EQUAL(has_parent_path("\\\\foobar/"), false);
|
|
|
|
TEST_EQUAL(has_parent_path("\\\\foobar\\"), false);
|
|
|
|
TEST_EQUAL(has_parent_path("\\\\foo/bar\\"), true);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef TORRENT_WINDOWS
|
|
|
|
TEST_EQUAL(is_complete("c:\\"), true);
|
|
|
|
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
|
|
|
|
|
2014-12-17 15:33:52 +01:00
|
|
|
TEST_EQUAL(complete("."), current_working_directory());
|
2015-05-30 23:10:34 +02:00
|
|
|
}
|
2014-12-17 15:33:52 +01:00
|
|
|
|
2015-05-30 23:10:34 +02:00
|
|
|
// test split_string
|
|
|
|
TORRENT_TEST(split_string)
|
|
|
|
{
|
2013-09-01 03:10:50 +02:00
|
|
|
char const* tags[10];
|
|
|
|
char tags_str[] = " this is\ta test\t string\x01to be split and it cannot "
|
|
|
|
"extend over the limit of elements \t";
|
|
|
|
int ret = split_string(tags, 10, tags_str);
|
|
|
|
|
|
|
|
TEST_CHECK(ret == 10);
|
2017-03-18 17:19:49 +01:00
|
|
|
TEST_CHECK(tags[0] == "this"_sv);
|
|
|
|
TEST_CHECK(tags[1] == "is"_sv);
|
|
|
|
TEST_CHECK(tags[2] == "a"_sv);
|
|
|
|
TEST_CHECK(tags[3] == "test"_sv);
|
|
|
|
TEST_CHECK(tags[4] == "string"_sv);
|
|
|
|
TEST_CHECK(tags[5] == "to"_sv);
|
|
|
|
TEST_CHECK(tags[6] == "be"_sv);
|
|
|
|
TEST_CHECK(tags[7] == "split"_sv);
|
|
|
|
TEST_CHECK(tags[8] == "and"_sv);
|
|
|
|
TEST_CHECK(tags[9] == "it"_sv);
|
2013-09-01 03:10:50 +02:00
|
|
|
|
|
|
|
// replace_extension
|
|
|
|
std::string test = "foo.bar";
|
|
|
|
replace_extension(test, "txt");
|
|
|
|
TEST_EQUAL(test, "foo.txt");
|
|
|
|
|
2013-10-24 09:22:44 +02:00
|
|
|
test = "_";
|
|
|
|
replace_extension(test, "txt");
|
|
|
|
TEST_EQUAL(test, "_.txt");
|
|
|
|
|
|
|
|
test = "1.2.3/_";
|
|
|
|
replace_extension(test, "txt");
|
|
|
|
TEST_EQUAL(test, "1.2.3/_.txt");
|
2015-05-30 23:10:34 +02:00
|
|
|
}
|
2013-10-24 09:22:44 +02:00
|
|
|
|
2015-05-30 23:10:34 +02:00
|
|
|
// file class
|
|
|
|
TORRENT_TEST(file)
|
|
|
|
{
|
|
|
|
error_code ec;
|
2013-09-01 03:10:50 +02:00
|
|
|
file f;
|
2016-05-27 21:35:53 +02:00
|
|
|
#if TORRENT_USE_UNC_PATHS || !defined _WIN32
|
2013-09-01 03:10:50 +02:00
|
|
|
TEST_CHECK(f.open("con", file::read_write, ec));
|
|
|
|
#else
|
|
|
|
TEST_CHECK(f.open("test_file", file::read_write, ec));
|
|
|
|
#endif
|
2014-07-06 21:18:00 +02:00
|
|
|
if (ec)
|
2016-10-10 02:23:45 +02:00
|
|
|
std::printf("open failed: [%s] %s\n", ec.category().name(), ec.message().c_str());
|
2015-08-31 02:14:55 +02:00
|
|
|
TEST_EQUAL(ec, error_code());
|
2016-10-10 02:23:45 +02:00
|
|
|
if (ec) std::printf("%s\n", ec.message().c_str());
|
2017-04-29 06:27:55 +02:00
|
|
|
char test[] = "test";
|
|
|
|
iovec_t b = {test, 4};
|
2016-10-27 02:40:56 +02:00
|
|
|
TEST_EQUAL(f.writev(0, b, ec), 4);
|
2014-07-06 21:18:00 +02:00
|
|
|
if (ec)
|
2016-10-10 02:23:45 +02:00
|
|
|
std::printf("writev failed: [%s] %s\n", ec.category().name(), ec.message().c_str());
|
2013-09-01 03:10:50 +02:00
|
|
|
TEST_CHECK(!ec);
|
|
|
|
char test_buf[5] = {0};
|
2017-04-29 06:27:55 +02:00
|
|
|
b = { test_buf, 4 };
|
2016-10-27 02:40:56 +02:00
|
|
|
TEST_EQUAL(f.readv(0, b, ec), 4);
|
2014-07-06 21:18:00 +02:00
|
|
|
if (ec)
|
2016-10-10 02:23:45 +02:00
|
|
|
std::printf("readv failed: [%s] %s\n", ec.category().name(), ec.message().c_str());
|
2015-08-31 02:14:55 +02:00
|
|
|
TEST_EQUAL(ec, error_code());
|
2017-03-18 17:19:49 +01:00
|
|
|
TEST_CHECK(test_buf == "test"_sv);
|
2013-09-01 03:10:50 +02:00
|
|
|
f.close();
|
2012-06-30 01:10:28 +02:00
|
|
|
}
|
|
|
|
|
2015-08-31 02:14:55 +02:00
|
|
|
TORRENT_TEST(hard_link)
|
|
|
|
{
|
|
|
|
// try to create a hard link to see what happens
|
|
|
|
// first create a regular file to then add another link to.
|
|
|
|
|
|
|
|
// create a file, write some stuff to it, create a hard link to that file,
|
|
|
|
// read that file and assert we get the same stuff we wrote to the first file
|
|
|
|
error_code ec;
|
|
|
|
file f;
|
|
|
|
TEST_CHECK(f.open("original_file", file::read_write, ec));
|
|
|
|
if (ec)
|
2016-10-10 02:23:45 +02:00
|
|
|
std::printf("open failed: [%s] %s\n", ec.category().name(), ec.message().c_str());
|
2015-08-31 02:14:55 +02:00
|
|
|
TEST_EQUAL(ec, error_code());
|
|
|
|
|
2017-04-29 06:27:55 +02:00
|
|
|
char str[] = "abcdefghijklmnopqrstuvwxyz";
|
|
|
|
iovec_t b = { str, 26 };
|
2016-10-27 02:40:56 +02:00
|
|
|
TEST_EQUAL(f.writev(0, b, ec), 26);
|
2015-08-31 02:14:55 +02:00
|
|
|
if (ec)
|
2016-10-10 02:23:45 +02:00
|
|
|
std::printf("writev failed: [%s] %s\n", ec.category().name(), ec.message().c_str());
|
2015-08-31 02:14:55 +02:00
|
|
|
TEST_EQUAL(ec, error_code());
|
|
|
|
f.close();
|
|
|
|
|
|
|
|
hard_link("original_file", "second_link", ec);
|
|
|
|
|
|
|
|
if (ec)
|
2016-10-10 02:23:45 +02:00
|
|
|
std::printf("hard_link failed: [%s] %s\n", ec.category().name(), ec.message().c_str());
|
2015-08-31 02:14:55 +02:00
|
|
|
TEST_EQUAL(ec, error_code());
|
|
|
|
|
|
|
|
|
|
|
|
TEST_CHECK(f.open("second_link", file::read_write, ec));
|
|
|
|
if (ec)
|
2016-10-10 02:23:45 +02:00
|
|
|
std::printf("open failed: [%s] %s\n", ec.category().name(), ec.message().c_str());
|
2015-08-31 02:14:55 +02:00
|
|
|
TEST_EQUAL(ec, error_code());
|
|
|
|
|
2017-04-29 06:27:55 +02:00
|
|
|
char test_buf[27] = {};
|
|
|
|
b = { test_buf, 27 };
|
2016-10-27 02:40:56 +02:00
|
|
|
TEST_EQUAL(f.readv(0, b, ec), 26);
|
2015-08-31 02:14:55 +02:00
|
|
|
if (ec)
|
2016-10-10 02:23:45 +02:00
|
|
|
std::printf("readv failed: [%s] %s\n", ec.category().name(), ec.message().c_str());
|
2015-08-31 02:14:55 +02:00
|
|
|
TEST_EQUAL(ec, error_code());
|
2017-03-18 17:19:49 +01:00
|
|
|
TEST_CHECK(test_buf == "abcdefghijklmnopqrstuvwxyz"_sv);
|
2015-08-31 02:14:55 +02:00
|
|
|
f.close();
|
|
|
|
|
|
|
|
remove("original_file", ec);
|
|
|
|
if (ec)
|
2016-10-10 02:23:45 +02:00
|
|
|
std::printf("remove failed: [%s] %s\n", ec.category().name(), ec.message().c_str());
|
2015-08-31 02:14:55 +02:00
|
|
|
|
|
|
|
remove("second_link", ec);
|
|
|
|
if (ec)
|
2016-10-10 02:23:45 +02:00
|
|
|
std::printf("remove failed: [%s] %s\n", ec.category().name(), ec.message().c_str());
|
2015-08-31 02:14:55 +02:00
|
|
|
}
|
|
|
|
|
2016-03-20 16:38:55 +01:00
|
|
|
TORRENT_TEST(coalesce_buffer)
|
|
|
|
{
|
|
|
|
error_code ec;
|
|
|
|
file f;
|
|
|
|
TEST_CHECK(f.open("test_file", file::read_write, ec));
|
|
|
|
if (ec)
|
2016-10-10 02:23:45 +02:00
|
|
|
std::printf("open failed: [%s] %s\n", ec.category().name(), ec.message().c_str());
|
2016-03-20 16:38:55 +01:00
|
|
|
TEST_EQUAL(ec, error_code());
|
2016-10-10 02:23:45 +02:00
|
|
|
if (ec) std::printf("%s\n", ec.message().c_str());
|
2017-04-29 06:27:55 +02:00
|
|
|
char test[] = "test";
|
|
|
|
char foobar[] = "foobar";
|
|
|
|
iovec_t b[2] = {{test, 4}, {foobar, 6}};
|
|
|
|
TEST_EQUAL(f.writev(0, b, ec, file::coalesce_buffers), 4 + 6);
|
2016-03-20 16:38:55 +01:00
|
|
|
if (ec)
|
2016-10-10 02:23:45 +02:00
|
|
|
std::printf("writev failed: [%s] %s\n", ec.category().name(), ec.message().c_str());
|
2016-03-20 16:38:55 +01:00
|
|
|
TEST_CHECK(!ec);
|
|
|
|
char test_buf1[5] = {0};
|
|
|
|
char test_buf2[7] = {0};
|
2017-04-29 06:27:55 +02:00
|
|
|
b[0] = { test_buf1, 4 };
|
|
|
|
b[1] = { test_buf2, 6 };
|
|
|
|
TEST_EQUAL(f.readv(0, b, ec), 4 + 6);
|
2016-03-20 16:38:55 +01:00
|
|
|
if (ec)
|
|
|
|
{
|
2016-10-10 02:23:45 +02:00
|
|
|
std::printf("readv failed: [%s] %s\n"
|
2016-03-20 16:38:55 +01:00
|
|
|
, ec.category().name(), ec.message().c_str());
|
|
|
|
}
|
|
|
|
TEST_EQUAL(ec, error_code());
|
2017-03-18 17:19:49 +01:00
|
|
|
TEST_CHECK(test_buf1 == "test"_sv);
|
|
|
|
TEST_CHECK(test_buf2 == "foobar"_sv);
|
2016-03-20 16:38:55 +01:00
|
|
|
f.close();
|
|
|
|
}
|
2016-09-23 22:49:39 +02:00
|
|
|
|
|
|
|
TORRENT_TEST(stat_file)
|
|
|
|
{
|
|
|
|
file_status st;
|
|
|
|
error_code ec;
|
|
|
|
stat_file("no_such_file_or_directory.file", &st, ec);
|
|
|
|
TEST_CHECK(ec);
|
|
|
|
TEST_EQUAL(ec, boost::system::errc::no_such_file_or_directory);
|
|
|
|
}
|
2017-02-17 05:47:08 +01:00
|
|
|
|
|
|
|
// specificaly UNC tests
|
|
|
|
#if TORRENT_USE_UNC_PATHS
|
|
|
|
|
|
|
|
std::tuple<int, bool> fill_current_directory_caps()
|
|
|
|
{
|
|
|
|
#ifdef TORRENT_WINDOWS
|
|
|
|
error_code ec;
|
|
|
|
DWORD dw_maximum_component_length;
|
|
|
|
DWORD dw_file_system_flags;
|
|
|
|
if (!GetVolumeInformationA(nullptr, nullptr, 0, nullptr, &dw_maximum_component_length, &dw_file_system_flags, nullptr, 0))
|
|
|
|
{
|
|
|
|
ec.assign(GetLastError(), system_category());
|
|
|
|
std::printf("GetVolumeInformation: [%s] %s\n"
|
|
|
|
, ec.category().name(), ec.message().c_str());
|
|
|
|
return std::make_tuple(0, false);
|
|
|
|
}
|
|
|
|
int maximum_component_length = int(dw_maximum_component_length);
|
|
|
|
bool support_hard_links = ((dw_file_system_flags & FILE_SUPPORTS_HARD_LINKS) != 0);
|
|
|
|
return std::make_tuple(maximum_component_length, support_hard_links);
|
|
|
|
#else
|
|
|
|
return std::make_tuple(TORRENT_MAX_PATH, true);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
TORRENT_TEST(unc_tests)
|
|
|
|
{
|
|
|
|
TEST_EQUAL(canonicalize_path("c:\\a\\..\\b"), "c:\\b");
|
|
|
|
TEST_EQUAL(canonicalize_path("a\\..\\b"), "b");
|
|
|
|
TEST_EQUAL(canonicalize_path("a\\..\\.\\b"), "b");
|
|
|
|
TEST_EQUAL(canonicalize_path("\\.\\a"), "\\a");
|
|
|
|
TEST_EQUAL(canonicalize_path("\\\\bla\\.\\a"), "\\\\bla\\a");
|
|
|
|
TEST_EQUAL(canonicalize_path("c:\\bla\\a"), "c:\\bla\\a");
|
|
|
|
|
|
|
|
error_code ec;
|
|
|
|
|
|
|
|
std::vector<std::string> special_names
|
|
|
|
{
|
|
|
|
"CON", "PRN", "AUX", "NUL",
|
|
|
|
"COM1", "COM2", "COM3", "COM4",
|
|
|
|
"COM5", "COM6", "COM7", "COM8",
|
|
|
|
"COM9", "LPT1", "LPT2", "LPT3",
|
|
|
|
"LPT4", "LPT5", "LPT6", "LPT7",
|
|
|
|
"LPT8", "LPT9"
|
|
|
|
};
|
|
|
|
|
|
|
|
for (std::string special_name : special_names)
|
|
|
|
{
|
|
|
|
TEST_EQUAL(touch_file(special_name, 10), 0);
|
|
|
|
TEST_CHECK(lt::exists(special_name));
|
|
|
|
lt::remove(special_name, ec);
|
|
|
|
TEST_EQUAL(ec, error_code());
|
|
|
|
TEST_CHECK(!lt::exists(special_name));
|
|
|
|
}
|
|
|
|
|
|
|
|
int maximum_component_length;
|
|
|
|
bool support_hard_links;
|
|
|
|
std::tie(maximum_component_length, support_hard_links) = fill_current_directory_caps();
|
|
|
|
|
|
|
|
if (maximum_component_length > 0)
|
|
|
|
{
|
|
|
|
std::string long_component_name;
|
|
|
|
long_component_name.resize(maximum_component_length);
|
|
|
|
for (int i = 0; i < maximum_component_length; ++i)
|
|
|
|
long_component_name[i] = static_cast<char>((i % 26) + 'A');
|
|
|
|
|
|
|
|
std::string long_file_name1 = combine_path(long_component_name, long_component_name);
|
|
|
|
long_file_name1.back() = '1';
|
|
|
|
std::string long_file_name2 { long_file_name1 };
|
|
|
|
long_file_name2.back() = '2';
|
|
|
|
|
|
|
|
error_code ec;
|
|
|
|
|
|
|
|
lt::create_directory(long_component_name, ec);
|
|
|
|
TEST_EQUAL(ec, error_code());
|
|
|
|
TEST_CHECK(lt::exists(long_component_name));
|
|
|
|
TEST_CHECK(lt::is_directory(long_component_name, ec));
|
|
|
|
|
|
|
|
TEST_EQUAL(touch_file(long_file_name1, 10), 0);
|
|
|
|
TEST_CHECK(lt::exists(long_file_name1));
|
|
|
|
|
|
|
|
lt::rename(long_file_name1, long_file_name2, ec);
|
|
|
|
TEST_EQUAL(ec, error_code());
|
|
|
|
TEST_CHECK(!lt::exists(long_file_name1));
|
|
|
|
TEST_CHECK(lt::exists(long_file_name2));
|
|
|
|
|
|
|
|
lt::copy_file(long_file_name2, long_file_name1, ec);
|
|
|
|
TEST_EQUAL(ec, error_code());
|
|
|
|
TEST_CHECK(lt::exists(long_file_name1));
|
|
|
|
|
|
|
|
std::set<std::string> files;
|
|
|
|
|
|
|
|
for (lt::directory i(long_component_name, ec); !i.done(); i.next(ec))
|
|
|
|
{
|
|
|
|
std::string f = i.file();
|
|
|
|
files.insert(f);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_EQUAL(files.size(), 4);
|
|
|
|
|
|
|
|
lt::remove(long_file_name1, ec);
|
|
|
|
TEST_EQUAL(ec, error_code());
|
|
|
|
TEST_CHECK(!lt::exists(long_file_name1));
|
|
|
|
|
|
|
|
if (support_hard_links)
|
|
|
|
{
|
|
|
|
lt::hard_link(long_file_name2, long_file_name1, ec);
|
|
|
|
TEST_EQUAL(ec, error_code());
|
|
|
|
TEST_CHECK(lt::exists(long_file_name1));
|
|
|
|
|
|
|
|
lt::remove(long_file_name1, ec);
|
|
|
|
TEST_EQUAL(ec, error_code());
|
|
|
|
TEST_CHECK(!lt::exists(long_file_name1));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|