2004-01-16 03:57:45 +01:00
|
|
|
/*
|
|
|
|
|
2018-04-09 09:04:33 +02:00
|
|
|
Copyright (c) 2003-2018, Arvid Norberg
|
2004-01-16 03:57:45 +01:00
|
|
|
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.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
2017-04-29 06:27:55 +02:00
|
|
|
#include "libtorrent/config.hpp"
|
2016-09-14 17:28:50 +02:00
|
|
|
#include "libtorrent/aux_/disable_warnings_push.hpp"
|
2018-07-25 09:10:34 +02:00
|
|
|
#include "libtorrent/span.hpp"
|
2018-07-12 16:55:51 +02:00
|
|
|
#include <mutex> // for call_once
|
2016-09-14 17:28:50 +02:00
|
|
|
|
2015-08-16 18:17:23 +02:00
|
|
|
#ifdef __GNUC__
|
|
|
|
#pragma GCC diagnostic push
|
|
|
|
#pragma GCC diagnostic ignored "-Wunused-macros"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef __clang__
|
|
|
|
#pragma clang diagnostic push
|
2016-11-19 20:19:35 +01:00
|
|
|
#pragma clang diagnostic ignored "-Wunknown-pragmas"
|
2015-08-16 18:17:23 +02:00
|
|
|
#pragma clang diagnostic ignored "-Wunused-macros"
|
2016-11-19 20:19:35 +01:00
|
|
|
#pragma clang diagnostic ignored "-Wreserved-id-macro"
|
2015-08-16 18:17:23 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
// these defines are just in case the system we're on needs them for 64 bit file
|
|
|
|
// support
|
2013-11-08 09:10:22 +01:00
|
|
|
#define _FILE_OFFSET_BITS 64
|
|
|
|
#define _LARGE_FILES 1
|
|
|
|
|
2017-04-29 06:27:55 +02:00
|
|
|
#ifndef TORRENT_WINDOWS
|
|
|
|
#include <sys/uio.h> // for iovec
|
|
|
|
#else
|
2018-07-07 18:21:01 +02:00
|
|
|
#include <boost/scope_exit.hpp>
|
2017-04-29 06:27:55 +02:00
|
|
|
namespace {
|
|
|
|
struct iovec
|
|
|
|
{
|
|
|
|
void* iov_base;
|
|
|
|
std::size_t iov_len;
|
|
|
|
};
|
|
|
|
} // anonymous namespace
|
|
|
|
#endif
|
|
|
|
|
2014-02-15 03:45:04 +01:00
|
|
|
// on mingw this is necessary to enable 64-bit time_t, specifically used for
|
|
|
|
// the stat struct. Without this, modification times returned by stat may be
|
|
|
|
// incorrect and consistently fail resume data
|
|
|
|
#ifndef __MINGW_USE_VC2005_COMPAT
|
|
|
|
# define __MINGW_USE_VC2005_COMPAT
|
|
|
|
#endif
|
|
|
|
|
2015-08-16 18:17:23 +02:00
|
|
|
#ifdef __clang__
|
|
|
|
#pragma clang diagnostic pop
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef __GNUC__
|
|
|
|
#pragma GCC diagnostic pop
|
|
|
|
#endif
|
|
|
|
|
2016-09-14 17:28:50 +02:00
|
|
|
#include "libtorrent/aux_/disable_warnings_pop.hpp"
|
|
|
|
|
2017-01-27 18:43:34 +01:00
|
|
|
#include "libtorrent/aux_/alloca.hpp"
|
2014-07-06 21:18:00 +02:00
|
|
|
#include "libtorrent/file.hpp"
|
2017-07-02 17:39:06 +02:00
|
|
|
#include "libtorrent/aux_/path.hpp" // for convert_to_native_path_string
|
2016-08-18 23:08:40 +02:00
|
|
|
#include "libtorrent/string_util.hpp"
|
2014-07-06 21:18:00 +02:00
|
|
|
#include <cstring>
|
2015-03-15 00:10:20 +01:00
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
#include "libtorrent/assert.hpp"
|
2017-02-17 05:47:08 +01:00
|
|
|
#include "libtorrent/aux_/throw.hpp"
|
2007-03-17 18:15:16 +01:00
|
|
|
|
2016-09-14 17:28:50 +02:00
|
|
|
#include "libtorrent/aux_/disable_warnings_push.hpp"
|
|
|
|
|
2012-08-21 23:54:07 +02:00
|
|
|
#include <sys/stat.h>
|
2016-07-10 05:17:55 +02:00
|
|
|
#include <climits> // for IOV_MAX
|
2012-08-21 23:54:07 +02:00
|
|
|
|
2008-07-20 13:14:54 +02:00
|
|
|
#ifdef TORRENT_WINDOWS
|
2004-09-24 12:50:03 +02:00
|
|
|
// windows part
|
2005-08-18 13:20:17 +02:00
|
|
|
|
2011-02-02 08:41:32 +01:00
|
|
|
#ifndef PtrToPtr64
|
|
|
|
#define PtrToPtr64(x) (x)
|
|
|
|
#endif
|
|
|
|
|
2009-10-26 02:29:39 +01:00
|
|
|
#include "libtorrent/utf8.hpp"
|
2016-12-28 19:12:20 +01:00
|
|
|
#include "libtorrent/aux_/win_util.hpp"
|
2009-11-23 00:55:54 +01:00
|
|
|
|
|
|
|
#ifndef WIN32_LEAN_AND_MEAN
|
|
|
|
#define WIN32_LEAN_AND_MEAN
|
|
|
|
#endif
|
2008-07-20 13:14:54 +02:00
|
|
|
#include <windows.h>
|
|
|
|
#include <winioctl.h>
|
2009-10-26 02:29:39 +01:00
|
|
|
#include <sys/types.h>
|
2004-03-31 01:55:52 +02:00
|
|
|
#else
|
2008-07-20 13:14:54 +02:00
|
|
|
// posix part
|
2009-10-26 02:29:39 +01:00
|
|
|
|
2004-03-31 01:55:52 +02:00
|
|
|
#include <unistd.h>
|
2004-04-02 12:43:37 +02:00
|
|
|
#include <sys/types.h>
|
2016-07-10 05:17:55 +02:00
|
|
|
#include <cerrno>
|
2009-10-26 02:29:39 +01:00
|
|
|
#include <dirent.h>
|
|
|
|
|
2009-09-05 17:02:49 +02:00
|
|
|
#ifdef TORRENT_LINUX
|
2009-10-26 02:29:39 +01:00
|
|
|
// linux specifics
|
|
|
|
|
2009-09-05 17:02:49 +02:00
|
|
|
#include <sys/ioctl.h>
|
2013-11-27 17:58:02 +01:00
|
|
|
#ifdef TORRENT_ANDROID
|
|
|
|
#include <sys/syscall.h>
|
|
|
|
#define lseek lseek64
|
2016-10-19 19:00:03 +02:00
|
|
|
#define pread pread64
|
|
|
|
#define pwrite pwrite64
|
|
|
|
#define ftruncate ftruncate64
|
2013-11-27 17:58:02 +01:00
|
|
|
#endif
|
|
|
|
|
2011-02-14 05:12:26 +01:00
|
|
|
#elif defined __APPLE__ && defined __MACH__ && MAC_OS_X_VERSION_MIN_REQUIRED >= 1050
|
2009-10-26 02:29:39 +01:00
|
|
|
// mac specifics
|
|
|
|
|
|
|
|
#include <copyfile.h>
|
|
|
|
|
|
|
|
#endif
|
2004-03-31 01:55:52 +02:00
|
|
|
|
2005-08-23 11:59:56 +02:00
|
|
|
// make sure the _FILE_OFFSET_BITS define worked
|
2009-06-09 07:46:51 +02:00
|
|
|
// on this platform. It's supposed to make file
|
|
|
|
// related functions support 64-bit offsets.
|
2009-09-05 17:02:49 +02:00
|
|
|
// this test makes sure lseek() returns a type
|
2009-06-09 07:46:51 +02:00
|
|
|
// at least 64 bits wide
|
2016-05-23 14:15:39 +02:00
|
|
|
static_assert(sizeof(lseek(0, 0, 0)) >= 8, "64 bit file operations are required");
|
2005-08-23 11:59:56 +02:00
|
|
|
|
2009-10-26 02:29:39 +01:00
|
|
|
#endif // posix part
|
2004-03-31 01:55:52 +02:00
|
|
|
|
2016-09-14 17:28:50 +02:00
|
|
|
#include "libtorrent/aux_/disable_warnings_pop.hpp"
|
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
#if TORRENT_USE_PREADV
|
|
|
|
# if defined TORRENT_WINDOWS
|
2017-04-12 19:00:57 +02:00
|
|
|
namespace {
|
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
// wrap the windows function in something that looks
|
|
|
|
// like preadv() and pwritev()
|
2005-08-23 11:59:56 +02:00
|
|
|
|
2016-01-20 00:47:21 +01:00
|
|
|
// windows only lets us wait for 64 handles at a time, so this function makes
|
|
|
|
// sure we wait for all of them, partially in sequence
|
2016-12-09 14:23:54 +01:00
|
|
|
DWORD wait_for_multiple_objects(int num_handles, HANDLE* h)
|
2016-01-20 00:47:21 +01:00
|
|
|
{
|
2018-04-11 15:19:00 +02:00
|
|
|
int batch_size = std::min(num_handles, MAXIMUM_WAIT_OBJECTS);
|
2016-01-20 00:47:21 +01:00
|
|
|
while (WaitForMultipleObjects(batch_size, h, TRUE, INFINITE) != WAIT_FAILED)
|
|
|
|
{
|
|
|
|
h += batch_size;
|
|
|
|
num_handles -= batch_size;
|
2018-04-11 15:19:00 +02:00
|
|
|
batch_size = std::min(num_handles, MAXIMUM_WAIT_OBJECTS);
|
2016-01-20 00:47:21 +01:00
|
|
|
if (batch_size <= 0) return WAIT_OBJECT_0;
|
|
|
|
}
|
|
|
|
return WAIT_FAILED;
|
|
|
|
}
|
|
|
|
|
2018-07-25 09:10:34 +02:00
|
|
|
int allocate_overlapped(::iovec const* bufs, lt::span<OVERLAPPED> ol
|
|
|
|
, lt::span<HANDLE> h, std::int64_t file_offset)
|
2014-07-06 21:18:00 +02:00
|
|
|
{
|
2018-07-25 09:10:34 +02:00
|
|
|
std::memset(ol.data(), 0, sizeof(OVERLAPPED) * ol.size());
|
2018-11-01 23:05:30 +01:00
|
|
|
for (std::ptrdiff_t i = 0; i < ol.size(); ++i)
|
2014-07-06 21:18:00 +02:00
|
|
|
{
|
|
|
|
ol[i].OffsetHigh = file_offset >> 32;
|
|
|
|
ol[i].Offset = file_offset & 0xffffffff;
|
2016-06-20 17:32:06 +02:00
|
|
|
ol[i].hEvent = CreateEvent(nullptr, TRUE, FALSE, nullptr);
|
2014-07-06 21:18:00 +02:00
|
|
|
h[i] = ol[i].hEvent;
|
2016-06-20 17:32:06 +02:00
|
|
|
if (h[i] == nullptr)
|
2014-07-06 21:18:00 +02:00
|
|
|
{
|
|
|
|
// we failed to create the event, roll-back and return an error
|
2018-11-01 23:05:30 +01:00
|
|
|
for (int j = 0; j < i; ++j) CloseHandle(h[i]);
|
2014-07-06 21:18:00 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
file_offset += bufs[i].iov_len;
|
|
|
|
}
|
2018-07-25 09:10:34 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int preadv(HANDLE fd, ::iovec const* bufs, int num_bufs, std::int64_t const file_offset)
|
|
|
|
{
|
|
|
|
TORRENT_ALLOCA(ol, OVERLAPPED, num_bufs);
|
|
|
|
TORRENT_ALLOCA(h, HANDLE, num_bufs);
|
|
|
|
|
|
|
|
if (allocate_overlapped(bufs, ol, h, file_offset) < 0) return -1;
|
2014-07-06 21:18:00 +02:00
|
|
|
|
2018-07-12 23:47:45 +02:00
|
|
|
BOOST_SCOPE_EXIT_ALL(&h) {
|
|
|
|
for (auto hnd : h)
|
|
|
|
CloseHandle(hnd);
|
|
|
|
};
|
|
|
|
|
2018-07-12 23:30:50 +02:00
|
|
|
int num_waits = num_bufs;
|
2014-07-06 21:18:00 +02:00
|
|
|
for (int i = 0; i < num_bufs; ++i)
|
|
|
|
{
|
|
|
|
DWORD num_read;
|
2018-07-12 23:30:50 +02:00
|
|
|
if (ReadFile(fd, bufs[i].iov_base, DWORD(bufs[i].iov_len), &num_read, &ol[i]) == FALSE)
|
|
|
|
{
|
|
|
|
DWORD const last_error = GetLastError();
|
|
|
|
if (last_error == ERROR_HANDLE_EOF)
|
|
|
|
{
|
|
|
|
num_waits = i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else if (last_error != ERROR_IO_PENDING
|
2014-07-06 21:18:00 +02:00
|
|
|
#ifdef ERROR_CANT_WAIT
|
2018-07-12 23:30:50 +02:00
|
|
|
&& last_error != ERROR_CANT_WAIT
|
2014-07-06 21:18:00 +02:00
|
|
|
#endif
|
2018-07-12 23:30:50 +02:00
|
|
|
)
|
|
|
|
{
|
2018-07-12 23:47:45 +02:00
|
|
|
return -1;
|
2018-07-12 23:30:50 +02:00
|
|
|
}
|
2014-07-06 21:18:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-12 23:47:45 +02:00
|
|
|
if (num_waits == 0) return 0;
|
2018-07-12 23:30:50 +02:00
|
|
|
|
|
|
|
if (wait_for_multiple_objects(num_waits, h.data()) == WAIT_FAILED)
|
2018-07-12 23:47:45 +02:00
|
|
|
return -1;
|
2014-07-06 21:18:00 +02:00
|
|
|
|
2018-07-12 23:47:45 +02:00
|
|
|
int ret = 0;
|
2018-07-25 09:10:34 +02:00
|
|
|
for (auto& o : ol.first(num_waits))
|
2014-07-06 21:18:00 +02:00
|
|
|
{
|
2016-10-27 02:40:56 +02:00
|
|
|
if (WaitForSingleObject(o.hEvent, INFINITE) == WAIT_FAILED)
|
2018-07-12 23:47:45 +02:00
|
|
|
return -1;
|
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
DWORD num_read;
|
2016-10-27 02:40:56 +02:00
|
|
|
if (GetOverlappedResult(fd, &o, &num_read, FALSE) == FALSE)
|
2014-07-06 21:18:00 +02:00
|
|
|
{
|
2018-07-12 23:30:50 +02:00
|
|
|
DWORD const last_error = GetLastError();
|
|
|
|
if (last_error != ERROR_HANDLE_EOF)
|
|
|
|
{
|
2014-07-06 21:18:00 +02:00
|
|
|
#ifdef ERROR_CANT_WAIT
|
2018-07-12 23:30:50 +02:00
|
|
|
TORRENT_ASSERT(last_error != ERROR_CANT_WAIT);
|
2014-07-06 21:18:00 +02:00
|
|
|
#endif
|
2018-07-12 23:47:45 +02:00
|
|
|
return -1;
|
2018-07-12 23:30:50 +02:00
|
|
|
}
|
2014-07-06 21:18:00 +02:00
|
|
|
}
|
|
|
|
ret += num_read;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2018-07-25 09:10:34 +02:00
|
|
|
int pwritev(HANDLE fd, ::iovec const* bufs, int num_bufs, std::int64_t const file_offset)
|
2014-07-06 21:18:00 +02:00
|
|
|
{
|
2016-10-22 20:43:40 +02:00
|
|
|
TORRENT_ALLOCA(ol, OVERLAPPED, num_bufs);
|
|
|
|
TORRENT_ALLOCA(h, HANDLE, num_bufs);
|
2014-07-06 21:18:00 +02:00
|
|
|
|
2018-07-25 09:10:34 +02:00
|
|
|
if (allocate_overlapped(bufs, ol, h, file_offset) < 0) return -1;
|
2014-07-06 21:18:00 +02:00
|
|
|
|
2018-07-12 23:47:45 +02:00
|
|
|
BOOST_SCOPE_EXIT_ALL(&h) {
|
|
|
|
for (auto hnd : h)
|
|
|
|
CloseHandle(hnd);
|
|
|
|
};
|
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
for (int i = 0; i < num_bufs; ++i)
|
|
|
|
{
|
|
|
|
DWORD num_written;
|
2016-04-25 23:22:09 +02:00
|
|
|
if (WriteFile(fd, bufs[i].iov_base, DWORD(bufs[i].iov_len), &num_written, &ol[i]) == FALSE
|
2014-07-06 21:18:00 +02:00
|
|
|
&& GetLastError() != ERROR_IO_PENDING
|
|
|
|
#ifdef ERROR_CANT_WAIT
|
|
|
|
&& GetLastError() != ERROR_CANT_WAIT
|
|
|
|
#endif
|
|
|
|
)
|
|
|
|
{
|
2018-07-12 23:47:45 +02:00
|
|
|
return -1;
|
2014-07-06 21:18:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-27 02:40:56 +02:00
|
|
|
if (wait_for_multiple_objects(int(h.size()), h.data()) == WAIT_FAILED)
|
2018-07-12 23:47:45 +02:00
|
|
|
return -1;
|
2014-07-06 21:18:00 +02:00
|
|
|
|
2018-07-12 23:47:45 +02:00
|
|
|
int ret = 0;
|
2016-10-27 02:40:56 +02:00
|
|
|
for (auto& o : ol)
|
2014-07-06 21:18:00 +02:00
|
|
|
{
|
2016-10-27 02:40:56 +02:00
|
|
|
if (WaitForSingleObject(o.hEvent, INFINITE) == WAIT_FAILED)
|
2018-07-12 23:47:45 +02:00
|
|
|
return -1;
|
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
DWORD num_written;
|
2016-10-27 02:40:56 +02:00
|
|
|
if (GetOverlappedResult(fd, &o, &num_written, FALSE) == FALSE)
|
2014-07-06 21:18:00 +02:00
|
|
|
{
|
|
|
|
#ifdef ERROR_CANT_WAIT
|
|
|
|
TORRENT_ASSERT(GetLastError() != ERROR_CANT_WAIT);
|
|
|
|
#endif
|
2018-07-12 23:47:45 +02:00
|
|
|
return -1;
|
2014-07-06 21:18:00 +02:00
|
|
|
}
|
|
|
|
ret += num_written;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
2018-07-12 16:46:36 +02:00
|
|
|
} // namespace
|
2014-07-06 21:18:00 +02:00
|
|
|
# else
|
2018-10-20 12:50:53 +02:00
|
|
|
|
|
|
|
# ifdef __clang__
|
|
|
|
# pragma clang diagnostic push
|
|
|
|
# pragma clang diagnostic ignored "-Wreserved-id-macro"
|
|
|
|
# pragma clang diagnostic ignored "-Wunused-macros"
|
|
|
|
# endif
|
|
|
|
|
2015-07-14 23:50:08 +02:00
|
|
|
# undef _BSD_SOURCE
|
|
|
|
# define _BSD_SOURCE // deprecated since glibc 2.20
|
|
|
|
# undef _DEFAULT_SOURCE
|
|
|
|
# define _DEFAULT_SOURCE
|
2014-07-06 21:18:00 +02:00
|
|
|
# include <sys/uio.h>
|
2018-10-20 12:50:53 +02:00
|
|
|
|
|
|
|
# ifdef __clang__
|
|
|
|
# pragma clang diagnostic pop
|
|
|
|
# endif
|
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
# endif
|
|
|
|
#endif
|
2004-04-05 00:15:31 +02:00
|
|
|
|
2017-04-12 19:00:57 +02:00
|
|
|
namespace libtorrent {
|
|
|
|
|
2017-07-15 02:59:20 +02:00
|
|
|
static_assert(!(open_mode::rw_mask & open_mode::sparse), "internal flags error");
|
|
|
|
static_assert(!(open_mode::rw_mask & open_mode::attribute_mask), "internal flags error");
|
|
|
|
static_assert(!(open_mode::sparse & open_mode::attribute_mask), "internal flags error");
|
2017-05-26 20:49:21 +02:00
|
|
|
|
2009-10-26 02:29:39 +01:00
|
|
|
directory::directory(std::string const& path, error_code& ec)
|
|
|
|
: m_done(false)
|
|
|
|
{
|
|
|
|
ec.clear();
|
2017-02-17 05:47:08 +01:00
|
|
|
std::string p{ path };
|
|
|
|
|
2009-10-26 02:29:39 +01:00
|
|
|
#ifdef TORRENT_WINDOWS
|
|
|
|
// the path passed to FindFirstFile() must be
|
|
|
|
// a pattern
|
2017-02-17 05:47:08 +01:00
|
|
|
p.append((!p.empty() && p.back() != '\\') ? "\\*" : "*");
|
|
|
|
#else
|
|
|
|
// the path passed to opendir() may not
|
|
|
|
// end with a /
|
|
|
|
if (!p.empty() && p.back() == '/')
|
|
|
|
p.pop_back();
|
|
|
|
#endif
|
|
|
|
|
|
|
|
native_path_string f = convert_to_native_path_string(p);
|
|
|
|
|
|
|
|
#ifdef TORRENT_WINDOWS
|
2017-07-01 19:12:55 +02:00
|
|
|
m_handle = FindFirstFileW(f.c_str(), &m_fd);
|
2009-10-26 02:29:39 +01:00
|
|
|
if (m_handle == INVALID_HANDLE_VALUE)
|
|
|
|
{
|
2015-04-11 01:19:47 +02:00
|
|
|
ec.assign(GetLastError(), system_category());
|
2009-10-26 02:29:39 +01:00
|
|
|
m_done = true;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
#else
|
2017-02-17 05:47:08 +01:00
|
|
|
m_handle = ::opendir(f.c_str());
|
2016-07-09 22:26:26 +02:00
|
|
|
if (m_handle == nullptr)
|
2009-10-26 02:29:39 +01:00
|
|
|
{
|
2015-11-24 06:50:51 +01:00
|
|
|
ec.assign(errno, system_category());
|
2009-10-26 02:29:39 +01:00
|
|
|
m_done = true;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// read the first entry
|
|
|
|
next(ec);
|
2019-01-06 03:30:41 +01:00
|
|
|
#endif // TORRENT_WINDOWS
|
2009-10-26 02:29:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
directory::~directory()
|
|
|
|
{
|
|
|
|
#ifdef TORRENT_WINDOWS
|
|
|
|
if (m_handle != INVALID_HANDLE_VALUE)
|
|
|
|
FindClose(m_handle);
|
|
|
|
#else
|
2017-09-12 23:10:11 +02:00
|
|
|
if (m_handle) ::closedir(m_handle);
|
2009-10-26 02:29:39 +01:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string directory::file() const
|
|
|
|
{
|
|
|
|
#ifdef TORRENT_WINDOWS
|
2017-02-17 05:47:08 +01:00
|
|
|
return convert_from_native_path(m_fd.cFileName);
|
2009-10-26 02:29:39 +01:00
|
|
|
#else
|
2017-07-02 17:39:06 +02:00
|
|
|
return convert_from_native_path(m_name.c_str());
|
2009-10-26 02:29:39 +01:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void directory::next(error_code& ec)
|
|
|
|
{
|
|
|
|
ec.clear();
|
|
|
|
#ifdef TORRENT_WINDOWS
|
2017-07-01 19:12:55 +02:00
|
|
|
if (FindNextFileW(m_handle, &m_fd) == 0)
|
2009-10-26 02:29:39 +01:00
|
|
|
{
|
|
|
|
m_done = true;
|
|
|
|
int err = GetLastError();
|
|
|
|
if (err != ERROR_NO_MORE_FILES)
|
2015-04-11 01:19:47 +02:00
|
|
|
ec.assign(err, system_category());
|
2009-10-26 02:29:39 +01:00
|
|
|
}
|
|
|
|
#else
|
2017-07-23 04:44:44 +02:00
|
|
|
struct dirent* de;
|
|
|
|
errno = 0;
|
2017-08-04 02:25:33 +02:00
|
|
|
if ((de = ::readdir(m_handle)) != nullptr)
|
2009-10-26 02:29:39 +01:00
|
|
|
{
|
2017-07-23 04:44:44 +02:00
|
|
|
m_name = de->d_name;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (errno) ec.assign(errno, system_category());
|
2009-10-26 02:29:39 +01:00
|
|
|
m_done = true;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
2007-06-10 22:46:09 +02:00
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
#ifndef INVALID_HANDLE_VALUE
|
2016-07-10 13:34:45 +02:00
|
|
|
#define INVALID_HANDLE_VALUE (-1)
|
2014-07-06 21:18:00 +02:00
|
|
|
#endif
|
|
|
|
|
2013-02-16 09:26:55 +01:00
|
|
|
#ifdef TORRENT_WINDOWS
|
|
|
|
struct overlapped_t
|
|
|
|
{
|
|
|
|
overlapped_t()
|
|
|
|
{
|
2016-08-13 01:24:03 +02:00
|
|
|
std::memset(&ol, 0, sizeof(ol));
|
2013-02-16 09:26:55 +01:00
|
|
|
ol.hEvent = CreateEvent(0, true, false, 0);
|
|
|
|
}
|
|
|
|
~overlapped_t()
|
|
|
|
{
|
|
|
|
if (ol.hEvent != INVALID_HANDLE_VALUE)
|
|
|
|
CloseHandle(ol.hEvent);
|
|
|
|
}
|
|
|
|
int wait(HANDLE file, error_code& ec)
|
|
|
|
{
|
2013-04-11 01:37:22 +02:00
|
|
|
if (ol.hEvent != INVALID_HANDLE_VALUE
|
|
|
|
&& WaitForSingleObject(ol.hEvent, INFINITE) == WAIT_FAILED)
|
2013-02-16 09:26:55 +01:00
|
|
|
{
|
2014-07-06 21:18:00 +02:00
|
|
|
ec.assign(GetLastError(), system_category());
|
2013-02-16 09:26:55 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2016-12-13 16:30:36 +01:00
|
|
|
DWORD ret;
|
2013-02-16 09:26:55 +01:00
|
|
|
if (GetOverlappedResult(file, &ol, &ret, false) == 0)
|
|
|
|
{
|
|
|
|
DWORD last_error = GetLastError();
|
|
|
|
if (last_error != ERROR_HANDLE_EOF)
|
|
|
|
{
|
2014-03-24 08:42:55 +01:00
|
|
|
#ifdef ERROR_CANT_WAIT
|
2013-02-16 09:26:55 +01:00
|
|
|
TORRENT_ASSERT(last_error != ERROR_CANT_WAIT);
|
|
|
|
#endif
|
2014-07-06 21:18:00 +02:00
|
|
|
ec.assign(last_error, system_category());
|
2013-02-16 09:26:55 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
OVERLAPPED ol;
|
|
|
|
};
|
|
|
|
#endif // TORRENT_WINDOWS
|
|
|
|
|
2014-02-23 20:13:53 +01:00
|
|
|
|
|
|
|
#ifdef TORRENT_WINDOWS
|
2018-07-12 16:46:36 +02:00
|
|
|
void acquire_manage_volume_privs();
|
2014-02-23 20:13:53 +01:00
|
|
|
#endif
|
|
|
|
|
2017-05-26 20:49:21 +02:00
|
|
|
file::file() : m_file_handle(INVALID_HANDLE_VALUE)
|
2017-01-09 02:29:24 +01:00
|
|
|
{}
|
2008-07-18 01:41:46 +02:00
|
|
|
|
2017-05-26 20:49:21 +02:00
|
|
|
file::file(std::string const& path, open_mode_t const mode, error_code& ec)
|
2008-07-20 13:14:54 +02:00
|
|
|
: m_file_handle(INVALID_HANDLE_VALUE)
|
|
|
|
{
|
2011-02-21 06:24:41 +01:00
|
|
|
// the return value is not important, since the
|
|
|
|
// error code contains the same information
|
2008-07-20 13:14:54 +02:00
|
|
|
open(path, mode, ec);
|
|
|
|
}
|
2008-07-18 01:41:46 +02:00
|
|
|
|
2008-07-20 13:14:54 +02:00
|
|
|
file::~file()
|
|
|
|
{
|
|
|
|
close();
|
|
|
|
}
|
2004-01-16 03:57:45 +01:00
|
|
|
|
2017-05-26 20:49:21 +02:00
|
|
|
bool file::open(std::string const& path, open_mode_t mode, error_code& ec)
|
2008-07-20 13:14:54 +02:00
|
|
|
{
|
|
|
|
close();
|
2017-02-17 05:47:08 +01:00
|
|
|
native_path_string file_path = convert_to_native_path_string(path);
|
2014-07-06 21:18:00 +02:00
|
|
|
|
2008-07-20 13:14:54 +02:00
|
|
|
#ifdef TORRENT_WINDOWS
|
2004-04-03 00:21:20 +02:00
|
|
|
|
2017-05-26 20:49:21 +02:00
|
|
|
struct win_open_mode_t
|
2009-01-21 08:31:49 +01:00
|
|
|
{
|
|
|
|
DWORD rw_mode;
|
|
|
|
DWORD create_mode;
|
|
|
|
};
|
|
|
|
|
2017-05-26 20:49:21 +02:00
|
|
|
static std::array<win_open_mode_t, 3> const mode_array{
|
2009-01-21 08:31:49 +01:00
|
|
|
{
|
|
|
|
// read_only
|
2011-06-09 08:08:24 +02:00
|
|
|
{GENERIC_READ, OPEN_EXISTING},
|
2009-01-21 08:31:49 +01:00
|
|
|
// write_only
|
2011-06-09 08:08:24 +02:00
|
|
|
{GENERIC_WRITE, OPEN_ALWAYS},
|
2009-01-21 08:31:49 +01:00
|
|
|
// read_write
|
2011-06-09 08:08:24 +02:00
|
|
|
{GENERIC_WRITE | GENERIC_READ, OPEN_ALWAYS},
|
2017-05-26 20:49:21 +02:00
|
|
|
}};
|
2009-01-21 08:31:49 +01:00
|
|
|
|
2017-05-26 20:49:21 +02:00
|
|
|
static std::array<DWORD, 4> const attrib_array{
|
2009-01-21 08:31:49 +01:00
|
|
|
{
|
|
|
|
FILE_ATTRIBUTE_NORMAL, // no attrib
|
|
|
|
FILE_ATTRIBUTE_HIDDEN, // hidden
|
|
|
|
FILE_ATTRIBUTE_NORMAL, // executable
|
|
|
|
FILE_ATTRIBUTE_HIDDEN, // hidden + executable
|
2017-05-26 20:49:21 +02:00
|
|
|
}};
|
2009-01-21 08:31:49 +01:00
|
|
|
|
2017-07-15 02:59:20 +02:00
|
|
|
TORRENT_ASSERT(static_cast<std::uint32_t>(mode & open_mode::rw_mask) < mode_array.size());
|
|
|
|
win_open_mode_t const& m = mode_array[static_cast<std::uint32_t>(mode & open_mode::rw_mask)];
|
2018-05-28 09:40:53 +02:00
|
|
|
DWORD a = attrib_array[static_cast<std::uint32_t>(mode & open_mode::attribute_mask) >> 7];
|
2009-01-21 08:31:49 +01:00
|
|
|
|
2012-05-27 20:17:51 +02:00
|
|
|
// one might think it's a good idea to pass in FILE_FLAG_RANDOM_ACCESS. It
|
|
|
|
// turns out that it isn't. That flag will break your operating system:
|
|
|
|
// http://support.microsoft.com/kb/2549369
|
|
|
|
|
2017-07-15 02:59:20 +02:00
|
|
|
DWORD const flags = ((mode & open_mode::random_access) ? 0 : FILE_FLAG_SEQUENTIAL_SCAN)
|
2018-05-22 09:29:54 +02:00
|
|
|
| a
|
2014-07-06 21:18:00 +02:00
|
|
|
| FILE_FLAG_OVERLAPPED
|
2017-07-15 02:59:20 +02:00
|
|
|
| ((mode & open_mode::no_cache) ? FILE_FLAG_WRITE_THROUGH : 0);
|
2011-04-25 04:15:18 +02:00
|
|
|
|
2018-07-12 16:55:51 +02:00
|
|
|
if (!(mode & open_mode::sparse))
|
2018-07-12 16:46:36 +02:00
|
|
|
{
|
|
|
|
// Enable privilege required by SetFileValidData()
|
|
|
|
// https://docs.microsoft.com/en-us/windows/desktop/api/fileapi/nf-fileapi-setfilevaliddata
|
2018-07-12 16:55:51 +02:00
|
|
|
static std::once_flag flag;
|
|
|
|
std::call_once(flag, acquire_manage_volume_privs);
|
2018-07-12 16:46:36 +02:00
|
|
|
}
|
|
|
|
|
2017-07-01 19:12:55 +02:00
|
|
|
handle_type handle = CreateFileW(file_path.c_str(), m.rw_mode
|
2017-09-17 10:43:27 +02:00
|
|
|
, FILE_SHARE_READ | FILE_SHARE_WRITE
|
2011-06-09 08:08:24 +02:00
|
|
|
, 0, m.create_mode, flags, 0);
|
2008-07-20 13:14:54 +02:00
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
if (handle == INVALID_HANDLE_VALUE)
|
2004-01-16 03:57:45 +01:00
|
|
|
{
|
2014-07-06 21:18:00 +02:00
|
|
|
ec.assign(GetLastError(), system_category());
|
2011-02-21 06:24:41 +01:00
|
|
|
TORRENT_ASSERT(ec);
|
2008-07-20 13:14:54 +02:00
|
|
|
return false;
|
|
|
|
}
|
2004-03-31 01:55:52 +02:00
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
m_file_handle = handle;
|
|
|
|
|
2008-07-20 13:14:54 +02:00
|
|
|
// try to make the file sparse if supported
|
2010-01-09 19:40:05 +01:00
|
|
|
// only set this flag if the file is opened for writing
|
2017-07-15 02:59:20 +02:00
|
|
|
if ((mode & open_mode::sparse)
|
|
|
|
&& (mode & open_mode::rw_mask) != open_mode::read_only)
|
2008-07-20 13:14:54 +02:00
|
|
|
{
|
|
|
|
DWORD temp;
|
2013-02-16 09:26:55 +01:00
|
|
|
overlapped_t ol;
|
2014-07-06 21:18:00 +02:00
|
|
|
BOOL ret = ::DeviceIoControl(native_handle(), FSCTL_SET_SPARSE, 0, 0
|
|
|
|
, 0, 0, &temp, &ol.ol);
|
2013-02-16 09:26:55 +01:00
|
|
|
error_code error;
|
2014-07-06 21:18:00 +02:00
|
|
|
if (ret == FALSE && GetLastError() == ERROR_IO_PENDING)
|
|
|
|
ol.wait(native_handle(), error);
|
2004-01-16 03:57:45 +01:00
|
|
|
}
|
2009-10-26 02:29:39 +01:00
|
|
|
#else // TORRENT_WINDOWS
|
|
|
|
|
2008-07-20 13:14:54 +02:00
|
|
|
// rely on default umask to filter x and w permissions
|
|
|
|
// for group and others
|
2008-07-22 15:02:06 +02:00
|
|
|
int permissions = S_IRUSR | S_IWUSR
|
|
|
|
| S_IRGRP | S_IWGRP
|
|
|
|
| S_IROTH | S_IWOTH;
|
|
|
|
|
2017-07-15 02:59:20 +02:00
|
|
|
if ((mode & open_mode::attribute_executable))
|
2009-01-11 23:27:43 +01:00
|
|
|
permissions |= S_IXGRP | S_IXOTH | S_IXUSR;
|
2012-09-24 18:13:57 +02:00
|
|
|
#ifdef O_BINARY
|
|
|
|
static const int mode_array[] = {O_RDONLY | O_BINARY, O_WRONLY | O_CREAT | O_BINARY, O_RDWR | O_CREAT | O_BINARY};
|
|
|
|
#else
|
2009-01-21 08:31:49 +01:00
|
|
|
static const int mode_array[] = {O_RDONLY, O_WRONLY | O_CREAT, O_RDWR | O_CREAT};
|
2012-09-24 18:13:57 +02:00
|
|
|
#endif
|
2009-03-01 01:02:33 +01:00
|
|
|
|
2015-03-09 07:09:50 +01:00
|
|
|
int open_mode = 0
|
2010-02-02 20:44:52 +01:00
|
|
|
#ifdef O_NOATIME
|
2017-07-15 02:59:20 +02:00
|
|
|
| ((mode & open_mode::no_atime) ? O_NOATIME : 0)
|
2010-02-02 20:44:52 +01:00
|
|
|
#endif
|
2015-01-08 00:21:54 +01:00
|
|
|
#ifdef O_SYNC
|
2017-07-15 02:59:20 +02:00
|
|
|
| ((mode & open_mode::no_cache) ? O_SYNC : 0)
|
2012-07-13 06:04:56 +02:00
|
|
|
#endif
|
2015-03-09 07:09:50 +01:00
|
|
|
;
|
|
|
|
|
2017-02-17 05:47:08 +01:00
|
|
|
handle_type handle = ::open(file_path.c_str()
|
2017-07-15 02:59:20 +02:00
|
|
|
, mode_array[static_cast<std::uint32_t>(mode & open_mode::rw_mask)] | open_mode
|
2014-07-06 21:18:00 +02:00
|
|
|
, permissions);
|
2009-01-17 10:37:40 +01:00
|
|
|
|
2012-07-13 06:04:56 +02:00
|
|
|
#ifdef O_NOATIME
|
|
|
|
// O_NOATIME is not allowed for files we don't own
|
|
|
|
// so, if we get EPERM when we try to open with it
|
|
|
|
// try again without O_NOATIME
|
2017-07-15 02:59:20 +02:00
|
|
|
if (handle == -1 && (mode & open_mode::no_atime) && errno == EPERM)
|
2012-07-13 06:04:56 +02:00
|
|
|
{
|
2017-07-15 02:59:20 +02:00
|
|
|
mode &= ~open_mode::no_atime;
|
2015-03-09 07:09:50 +01:00
|
|
|
open_mode &= ~O_NOATIME;
|
2017-05-26 20:49:21 +02:00
|
|
|
handle = ::open(file_path.c_str()
|
2017-07-15 02:59:20 +02:00
|
|
|
, mode_array[static_cast<std::uint32_t>(mode & open_mode::rw_mask)] | open_mode
|
2015-03-09 07:09:50 +01:00
|
|
|
, permissions);
|
2012-07-13 06:04:56 +02:00
|
|
|
}
|
2009-01-17 10:37:40 +01:00
|
|
|
#endif
|
2014-07-06 21:18:00 +02:00
|
|
|
if (handle == -1)
|
2004-01-16 03:57:45 +01:00
|
|
|
{
|
2015-11-24 06:50:51 +01:00
|
|
|
ec.assign(errno, system_category());
|
2009-01-19 10:26:11 +01:00
|
|
|
TORRENT_ASSERT(ec);
|
2008-07-20 13:14:54 +02:00
|
|
|
return false;
|
|
|
|
}
|
2009-01-11 03:02:34 +01:00
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
m_file_handle = handle;
|
|
|
|
|
2010-03-21 18:26:21 +01:00
|
|
|
#ifdef DIRECTIO_ON
|
|
|
|
// for solaris
|
2017-07-15 02:59:20 +02:00
|
|
|
if ((mode & open_mode::no_cache))
|
2010-03-21 18:26:21 +01:00
|
|
|
{
|
|
|
|
int yes = 1;
|
2014-07-06 21:18:00 +02:00
|
|
|
directio(native_handle(), DIRECTIO_ON);
|
2010-03-21 18:26:21 +01:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2009-01-11 03:02:34 +01:00
|
|
|
#ifdef F_NOCACHE
|
2010-03-21 18:26:21 +01:00
|
|
|
// for BSD/Mac
|
2017-07-15 02:59:20 +02:00
|
|
|
if ((mode & open_mode::no_cache))
|
2009-01-11 03:02:34 +01:00
|
|
|
{
|
|
|
|
int yes = 1;
|
2017-09-12 23:10:11 +02:00
|
|
|
::fcntl(native_handle(), F_NOCACHE, &yes);
|
2014-07-06 21:18:00 +02:00
|
|
|
|
|
|
|
#ifdef F_NODIRECT
|
|
|
|
// it's OK to temporarily cache written pages
|
2017-09-12 23:10:11 +02:00
|
|
|
::fcntl(native_handle(), F_NODIRECT, &yes);
|
2014-07-06 21:18:00 +02:00
|
|
|
#endif
|
2009-01-11 03:02:34 +01:00
|
|
|
}
|
2008-07-20 13:14:54 +02:00
|
|
|
#endif
|
2009-01-11 03:02:34 +01:00
|
|
|
|
|
|
|
#ifdef POSIX_FADV_RANDOM
|
2017-07-15 02:59:20 +02:00
|
|
|
if ((mode & open_mode::random_access))
|
2011-04-25 04:15:18 +02:00
|
|
|
{
|
|
|
|
// disable read-ahead
|
2017-09-16 13:47:49 +02:00
|
|
|
// NOTE: in android this function was introduced in API 21,
|
|
|
|
// but the constant POSIX_FADV_RANDOM is there for lower
|
|
|
|
// API levels, just don't add :: to allow a macro workaround
|
|
|
|
posix_fadvise(native_handle(), 0, 0, POSIX_FADV_RANDOM);
|
2011-04-25 04:15:18 +02:00
|
|
|
}
|
2008-07-20 13:14:54 +02:00
|
|
|
#endif
|
2009-01-11 03:02:34 +01:00
|
|
|
|
|
|
|
#endif
|
|
|
|
m_open_mode = mode;
|
|
|
|
|
2008-07-20 18:34:01 +02:00
|
|
|
TORRENT_ASSERT(is_open());
|
2008-07-20 13:14:54 +02:00
|
|
|
return true;
|
|
|
|
}
|
2004-03-31 01:55:52 +02:00
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
bool file::is_open() const
|
2009-01-21 08:31:49 +01:00
|
|
|
{
|
2014-07-06 21:18:00 +02:00
|
|
|
return m_file_handle != INVALID_HANDLE_VALUE;
|
2009-01-21 08:31:49 +01:00
|
|
|
}
|
|
|
|
|
2013-02-16 09:26:55 +01:00
|
|
|
#ifdef TORRENT_WINDOWS
|
2014-07-06 21:18:00 +02:00
|
|
|
// returns true if the given file has any regions that are
|
|
|
|
// sparse, i.e. not allocated.
|
|
|
|
bool is_sparse(HANDLE file)
|
2013-02-16 09:26:55 +01:00
|
|
|
{
|
|
|
|
LARGE_INTEGER file_size;
|
|
|
|
if (!GetFileSizeEx(file, &file_size))
|
2014-05-10 05:23:05 +02:00
|
|
|
return false;
|
2013-02-16 09:26:55 +01:00
|
|
|
|
|
|
|
overlapped_t ol;
|
2016-06-20 17:32:06 +02:00
|
|
|
if (ol.ol.hEvent == nullptr) return false;
|
2013-02-16 09:26:55 +01:00
|
|
|
|
2016-05-15 19:28:22 +02:00
|
|
|
#ifndef FSCTL_QUERY_ALLOCATED_RANGES
|
2013-02-16 09:26:55 +01:00
|
|
|
typedef struct _FILE_ALLOCATED_RANGE_BUFFER {
|
|
|
|
LARGE_INTEGER FileOffset;
|
|
|
|
LARGE_INTEGER Length;
|
2016-05-15 19:28:22 +02:00
|
|
|
} FILE_ALLOCATED_RANGE_BUFFER;
|
2013-02-16 09:26:55 +01:00
|
|
|
#define FSCTL_QUERY_ALLOCATED_RANGES ((0x9 << 16) | (1 << 14) | (51 << 2) | 3)
|
|
|
|
#endif
|
|
|
|
FILE_ALLOCATED_RANGE_BUFFER in;
|
|
|
|
in.FileOffset.QuadPart = 0;
|
|
|
|
in.Length.QuadPart = file_size.QuadPart;
|
|
|
|
|
|
|
|
FILE_ALLOCATED_RANGE_BUFFER out[2];
|
|
|
|
|
|
|
|
DWORD returned_bytes = 0;
|
|
|
|
BOOL ret = DeviceIoControl(file, FSCTL_QUERY_ALLOCATED_RANGES, (void*)&in, sizeof(in)
|
2014-07-06 21:18:00 +02:00
|
|
|
, out, sizeof(out), &returned_bytes, &ol.ol);
|
2013-02-16 09:26:55 +01:00
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
if (ret == FALSE && GetLastError() == ERROR_IO_PENDING)
|
2013-02-16 09:26:55 +01:00
|
|
|
{
|
|
|
|
error_code ec;
|
|
|
|
returned_bytes = ol.wait(file, ec);
|
|
|
|
if (ec) return true;
|
|
|
|
}
|
|
|
|
else if (ret == FALSE)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-05-11 01:38:54 +02:00
|
|
|
// if we have more than one range in the file, we're sparse
|
|
|
|
if (returned_bytes != sizeof(FILE_ALLOCATED_RANGE_BUFFER)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (in.Length.QuadPart != out[0].Length.QuadPart);
|
2013-02-16 09:26:55 +01:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2008-07-20 13:14:54 +02:00
|
|
|
void file::close()
|
|
|
|
{
|
2014-07-06 21:18:00 +02:00
|
|
|
if (!is_open()) return;
|
|
|
|
|
2008-07-20 13:14:54 +02:00
|
|
|
#ifdef TORRENT_WINDOWS
|
2013-02-16 09:26:55 +01:00
|
|
|
|
|
|
|
// if this file is open for writing, has the sparse
|
|
|
|
// flag set, but there are no sparse regions, unset
|
|
|
|
// the flag
|
2017-07-15 02:59:20 +02:00
|
|
|
open_mode_t const rw_mode = m_open_mode & open_mode::rw_mask;
|
|
|
|
if ((rw_mode != open_mode::read_only)
|
|
|
|
&& (m_open_mode & open_mode::sparse)
|
2014-07-06 21:18:00 +02:00
|
|
|
&& !is_sparse(native_handle()))
|
2013-02-16 09:26:55 +01:00
|
|
|
{
|
|
|
|
overlapped_t ol;
|
|
|
|
// according to MSDN, clearing the sparse flag of a file only
|
|
|
|
// works on windows vista and later
|
|
|
|
#ifdef TORRENT_MINGW
|
2014-07-06 21:18:00 +02:00
|
|
|
typedef struct _FILE_SET_SPARSE_BUFFER {
|
|
|
|
BOOLEAN SetSparse;
|
2016-05-15 19:28:22 +02:00
|
|
|
} FILE_SET_SPARSE_BUFFER;
|
2013-02-16 09:26:55 +01:00
|
|
|
#endif
|
|
|
|
DWORD temp;
|
|
|
|
FILE_SET_SPARSE_BUFFER b;
|
|
|
|
b.SetSparse = FALSE;
|
2014-07-06 21:18:00 +02:00
|
|
|
BOOL ret = ::DeviceIoControl(native_handle(), FSCTL_SET_SPARSE, &b, sizeof(b)
|
|
|
|
, 0, 0, &temp, &ol.ol);
|
2013-02-16 09:26:55 +01:00
|
|
|
error_code ec;
|
2014-07-06 21:18:00 +02:00
|
|
|
if (ret == FALSE && GetLastError() == ERROR_IO_PENDING)
|
2013-02-16 09:26:55 +01:00
|
|
|
{
|
2014-07-06 21:18:00 +02:00
|
|
|
ol.wait(native_handle(), ec);
|
2013-02-16 09:26:55 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
CloseHandle(native_handle());
|
2006-11-14 01:08:16 +01:00
|
|
|
#else
|
2014-07-06 21:18:00 +02:00
|
|
|
if (m_file_handle != INVALID_HANDLE_VALUE)
|
|
|
|
::close(m_file_handle);
|
2006-11-14 01:08:16 +01:00
|
|
|
#endif
|
2014-07-06 21:18:00 +02:00
|
|
|
|
|
|
|
m_file_handle = INVALID_HANDLE_VALUE;
|
|
|
|
|
2017-07-15 02:59:20 +02:00
|
|
|
m_open_mode = open_mode_t{};
|
2009-01-11 03:02:34 +01:00
|
|
|
}
|
|
|
|
|
2015-04-19 00:00:27 +02:00
|
|
|
namespace {
|
|
|
|
|
2017-01-11 06:42:10 +01:00
|
|
|
void gather_copy(span<iovec_t const> bufs, char* dst)
|
2011-04-26 09:03:05 +02:00
|
|
|
{
|
2018-11-01 23:05:30 +01:00
|
|
|
std::ptrdiff_t offset = 0;
|
2016-10-27 02:40:56 +02:00
|
|
|
for (auto buf : bufs)
|
2014-07-06 21:18:00 +02:00
|
|
|
{
|
2018-11-01 23:05:30 +01:00
|
|
|
std::copy(buf.begin(), buf.end(), dst + offset);
|
2017-04-29 06:27:55 +02:00
|
|
|
offset += buf.size();
|
2014-07-06 21:18:00 +02:00
|
|
|
}
|
2011-04-26 09:03:05 +02:00
|
|
|
}
|
|
|
|
|
2017-01-11 06:42:10 +01:00
|
|
|
void scatter_copy(span<iovec_t const> bufs, char const* src)
|
2008-07-20 13:14:54 +02:00
|
|
|
{
|
2018-11-01 23:05:30 +01:00
|
|
|
std::ptrdiff_t offset = 0;
|
2016-10-27 02:40:56 +02:00
|
|
|
for (auto buf : bufs)
|
2013-01-06 19:38:33 +01:00
|
|
|
{
|
2018-11-01 23:05:30 +01:00
|
|
|
std::copy(src + offset, src + offset + buf.size(), buf.data());
|
2017-04-29 06:27:55 +02:00
|
|
|
offset += buf.size();
|
2013-01-06 19:38:33 +01:00
|
|
|
}
|
2014-07-06 21:18:00 +02:00
|
|
|
}
|
2008-07-20 13:14:54 +02:00
|
|
|
|
2017-01-11 06:42:10 +01:00
|
|
|
bool coalesce_read_buffers(span<iovec_t const>& bufs
|
|
|
|
, iovec_t& tmp)
|
2014-07-06 21:18:00 +02:00
|
|
|
{
|
2018-11-01 23:05:30 +01:00
|
|
|
auto const buf_size = bufs_size(bufs);
|
|
|
|
auto buf = new char[std::size_t(buf_size)];
|
2017-04-29 06:27:55 +02:00
|
|
|
tmp = { buf, buf_size };
|
2017-01-11 06:42:10 +01:00
|
|
|
bufs = span<iovec_t const>(tmp);
|
2014-07-06 21:18:00 +02:00
|
|
|
return true;
|
|
|
|
}
|
2009-01-11 03:02:34 +01:00
|
|
|
|
2017-01-11 06:42:10 +01:00
|
|
|
void coalesce_read_buffers_end(span<iovec_t const> bufs
|
2016-03-20 05:55:31 +01:00
|
|
|
, char* const buf, bool const copy)
|
2014-07-06 21:18:00 +02:00
|
|
|
{
|
2016-10-27 02:40:56 +02:00
|
|
|
if (copy) scatter_copy(bufs, buf);
|
2017-04-29 06:27:55 +02:00
|
|
|
delete[] buf;
|
2014-07-06 21:18:00 +02:00
|
|
|
}
|
2009-01-11 03:02:34 +01:00
|
|
|
|
2017-01-11 06:42:10 +01:00
|
|
|
bool coalesce_write_buffers(span<iovec_t const>& bufs
|
|
|
|
, iovec_t& tmp)
|
2014-07-06 21:18:00 +02:00
|
|
|
{
|
2018-11-01 23:05:30 +01:00
|
|
|
auto const buf_size = bufs_size(bufs);
|
|
|
|
auto buf = new char[std::size_t(buf_size)];
|
2016-10-27 02:40:56 +02:00
|
|
|
gather_copy(bufs, buf);
|
2017-04-29 06:27:55 +02:00
|
|
|
tmp = { buf, buf_size };
|
2017-01-11 06:42:10 +01:00
|
|
|
bufs = span<iovec_t const>(tmp);
|
2014-07-06 21:18:00 +02:00
|
|
|
return true;
|
|
|
|
}
|
2017-04-29 06:27:55 +02:00
|
|
|
|
2018-01-29 00:30:29 +01:00
|
|
|
#if TORRENT_USE_PREADV
|
2017-04-29 06:27:55 +02:00
|
|
|
namespace {
|
|
|
|
int bufs_size(span<::iovec> bufs)
|
|
|
|
{
|
|
|
|
std::size_t size = 0;
|
|
|
|
for (auto buf : bufs) size += buf.iov_len;
|
|
|
|
return int(size);
|
|
|
|
}
|
|
|
|
}
|
2015-12-12 06:15:24 +01:00
|
|
|
#endif // TORRENT_USE_PREADV
|
2009-01-11 03:02:34 +01:00
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
template <class Fun>
|
2016-10-27 02:40:56 +02:00
|
|
|
std::int64_t iov(Fun f, handle_type fd, std::int64_t file_offset
|
2017-01-11 06:42:10 +01:00
|
|
|
, span<iovec_t const> bufs, error_code& ec)
|
2014-07-06 21:18:00 +02:00
|
|
|
{
|
|
|
|
#if TORRENT_USE_PREADV
|
2009-01-11 03:02:34 +01:00
|
|
|
|
2017-04-29 06:27:55 +02:00
|
|
|
TORRENT_ALLOCA(vec, ::iovec, bufs.size());
|
|
|
|
auto it = vec.begin();
|
|
|
|
for (auto const& b : bufs)
|
|
|
|
{
|
|
|
|
it->iov_base = b.data();
|
2018-11-01 23:05:30 +01:00
|
|
|
it->iov_len = std::size_t(b.size());
|
2017-04-29 06:27:55 +02:00
|
|
|
++it;
|
|
|
|
}
|
|
|
|
|
2018-08-03 12:51:45 +02:00
|
|
|
std::int64_t ret = 0;
|
2017-04-29 06:27:55 +02:00
|
|
|
while (!vec.empty())
|
2007-04-19 05:06:15 +02:00
|
|
|
{
|
2016-05-17 15:24:06 +02:00
|
|
|
#ifdef IOV_MAX
|
2017-04-29 06:27:55 +02:00
|
|
|
auto const nbufs = vec.first(std::min(int(vec.size()), IOV_MAX));
|
2016-05-17 15:24:06 +02:00
|
|
|
#else
|
2017-04-29 06:27:55 +02:00
|
|
|
auto const nbufs = vec;
|
2016-05-17 15:24:06 +02:00
|
|
|
#endif
|
|
|
|
|
2018-08-03 12:51:45 +02:00
|
|
|
std::int64_t tmp_ret = 0;
|
2016-10-27 02:40:56 +02:00
|
|
|
tmp_ret = f(fd, nbufs.data(), int(nbufs.size()), file_offset);
|
2014-07-06 21:18:00 +02:00
|
|
|
if (tmp_ret < 0)
|
2007-08-11 17:22:06 +02:00
|
|
|
{
|
2014-07-06 21:18:00 +02:00
|
|
|
#ifdef TORRENT_WINDOWS
|
|
|
|
ec.assign(GetLastError(), system_category());
|
|
|
|
#else
|
2015-11-24 06:50:51 +01:00
|
|
|
ec.assign(errno, system_category());
|
2014-07-06 21:18:00 +02:00
|
|
|
#endif
|
2008-07-20 13:14:54 +02:00
|
|
|
return -1;
|
2007-08-11 17:22:06 +02:00
|
|
|
}
|
2014-07-06 21:18:00 +02:00
|
|
|
file_offset += tmp_ret;
|
|
|
|
ret += tmp_ret;
|
2009-01-11 03:02:34 +01:00
|
|
|
|
2015-11-14 06:21:03 +01:00
|
|
|
// we got a short read/write. It's either 0, and we're at EOF, or we
|
|
|
|
// just need to issue the read/write operation again. In either case,
|
|
|
|
// punt that to the upper layer, as reissuing the operations is
|
|
|
|
// complicated here
|
2018-01-29 00:30:29 +01:00
|
|
|
int const expected_len = bufs_size(nbufs);
|
2015-11-14 06:21:03 +01:00
|
|
|
if (tmp_ret < expected_len) break;
|
|
|
|
|
2017-04-29 06:27:55 +02:00
|
|
|
vec = vec.subspan(nbufs.size());
|
2007-04-19 05:06:15 +02:00
|
|
|
}
|
2014-07-06 21:18:00 +02:00
|
|
|
return ret;
|
2004-04-05 00:15:31 +02:00
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
#elif TORRENT_USE_PREAD
|
2009-01-01 02:47:57 +01:00
|
|
|
|
2016-12-09 14:23:54 +01:00
|
|
|
std::int64_t ret = 0;
|
2016-10-27 02:40:56 +02:00
|
|
|
for (auto i : bufs)
|
2009-01-01 02:47:57 +01:00
|
|
|
{
|
2018-11-01 23:05:30 +01:00
|
|
|
std::int64_t const tmp_ret = f(fd, i.data()
|
|
|
|
, static_cast<std::size_t>(i.size()), file_offset);
|
2014-07-06 21:18:00 +02:00
|
|
|
if (tmp_ret < 0)
|
2009-01-11 03:02:34 +01:00
|
|
|
{
|
2014-07-06 21:18:00 +02:00
|
|
|
#ifdef TORRENT_WINDOWS
|
2014-07-20 10:59:02 +02:00
|
|
|
ec.assign(GetLastError(), system_category());
|
2014-07-06 21:18:00 +02:00
|
|
|
#else
|
2015-11-24 06:50:51 +01:00
|
|
|
ec.assign(errno, system_category());
|
2013-01-11 05:49:08 +01:00
|
|
|
#endif
|
2013-02-13 19:03:59 +01:00
|
|
|
return -1;
|
|
|
|
}
|
2014-07-06 21:18:00 +02:00
|
|
|
file_offset += tmp_ret;
|
|
|
|
ret += tmp_ret;
|
2017-04-29 06:27:55 +02:00
|
|
|
if (tmp_ret < int(i.size())) break;
|
2009-01-01 02:47:57 +01:00
|
|
|
}
|
2014-07-06 21:18:00 +02:00
|
|
|
|
2009-01-11 03:02:34 +01:00
|
|
|
return ret;
|
2009-04-04 10:23:53 +02:00
|
|
|
|
2014-07-20 10:59:02 +02:00
|
|
|
#else // not PREADV nor PREAD
|
2014-07-06 21:18:00 +02:00
|
|
|
|
|
|
|
int ret = 0;
|
2009-04-04 10:23:53 +02:00
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
#ifdef TORRENT_WINDOWS
|
|
|
|
if (SetFilePointerEx(fd, offs, &offs, FILE_BEGIN) == FALSE)
|
2009-01-11 03:02:34 +01:00
|
|
|
{
|
2014-07-06 21:18:00 +02:00
|
|
|
ec.assign(GetLastError(), system_category());
|
2009-01-11 03:02:34 +01:00
|
|
|
return -1;
|
|
|
|
}
|
2014-07-06 21:18:00 +02:00
|
|
|
#else
|
|
|
|
if (lseek(fd, file_offset, SEEK_SET) < 0)
|
2009-01-11 03:02:34 +01:00
|
|
|
{
|
2015-11-24 06:50:51 +01:00
|
|
|
ec.assign(errno, system_category());
|
2014-07-06 21:18:00 +02:00
|
|
|
return -1;
|
2011-09-05 07:47:50 +02:00
|
|
|
}
|
2014-07-06 21:18:00 +02:00
|
|
|
#endif
|
2011-09-05 07:47:50 +02:00
|
|
|
|
2016-10-27 02:40:56 +02:00
|
|
|
for (auto i : bufs)
|
2009-04-04 10:23:53 +02:00
|
|
|
{
|
2018-11-01 23:05:30 +01:00
|
|
|
int tmp_ret = f(fd, i.data(), static_cast<std::size_t>(i.size()));
|
2014-07-06 21:18:00 +02:00
|
|
|
if (tmp_ret < 0)
|
2009-04-04 10:23:53 +02:00
|
|
|
{
|
2014-07-06 21:18:00 +02:00
|
|
|
#ifdef TORRENT_WINDOWS
|
|
|
|
ec.assign(GetLastError(), system_category());
|
|
|
|
#else
|
2015-11-24 06:50:51 +01:00
|
|
|
ec.assign(errno, system_category());
|
2014-07-06 21:18:00 +02:00
|
|
|
#endif
|
2009-04-04 10:23:53 +02:00
|
|
|
return -1;
|
|
|
|
}
|
2014-07-06 21:18:00 +02:00
|
|
|
file_offset += tmp_ret;
|
|
|
|
ret += tmp_ret;
|
2017-04-29 06:27:55 +02:00
|
|
|
if (tmp_ret < int(i.size())) break;
|
2009-04-04 10:23:53 +02:00
|
|
|
}
|
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
return ret;
|
2009-04-04 10:23:53 +02:00
|
|
|
|
2016-05-15 19:28:22 +02:00
|
|
|
#endif // USE_PREADV
|
2009-01-01 02:47:57 +01:00
|
|
|
}
|
|
|
|
|
2015-04-19 00:00:27 +02:00
|
|
|
} // anonymous namespace
|
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
// this has to be thread safe and atomic. i.e. on posix systems it has to be
|
|
|
|
// turned into a series of pread() calls
|
2016-10-27 02:40:56 +02:00
|
|
|
std::int64_t file::readv(std::int64_t file_offset, span<iovec_t const> bufs
|
2017-05-26 20:49:21 +02:00
|
|
|
, error_code& ec, open_mode_t flags)
|
2009-01-01 02:47:57 +01:00
|
|
|
{
|
2013-01-06 19:38:33 +01:00
|
|
|
if (m_file_handle == INVALID_HANDLE_VALUE)
|
|
|
|
{
|
2014-07-06 21:18:00 +02:00
|
|
|
#ifdef TORRENT_WINDOWS
|
|
|
|
ec = error_code(ERROR_INVALID_HANDLE, system_category());
|
2013-01-06 19:38:33 +01:00
|
|
|
#else
|
2015-11-24 06:50:51 +01:00
|
|
|
ec = error_code(boost::system::errc::bad_file_descriptor, generic_category());
|
2014-07-06 21:18:00 +02:00
|
|
|
#endif
|
2013-01-06 19:38:33 +01:00
|
|
|
return -1;
|
|
|
|
}
|
2017-07-15 02:59:20 +02:00
|
|
|
TORRENT_ASSERT((m_open_mode & open_mode::rw_mask) == open_mode::read_only
|
|
|
|
|| (m_open_mode & open_mode::rw_mask) == open_mode::read_write);
|
2016-10-27 02:40:56 +02:00
|
|
|
TORRENT_ASSERT(!bufs.empty());
|
2009-01-01 02:47:57 +01:00
|
|
|
TORRENT_ASSERT(is_open());
|
|
|
|
|
2016-03-20 16:38:55 +01:00
|
|
|
// there's no point in coalescing single buffer writes
|
2016-10-27 02:40:56 +02:00
|
|
|
if (bufs.size() == 1)
|
2016-03-20 16:38:55 +01:00
|
|
|
{
|
2017-07-15 02:59:20 +02:00
|
|
|
flags &= ~open_mode::coalesce_buffers;
|
2016-03-20 16:38:55 +01:00
|
|
|
}
|
|
|
|
|
2016-10-27 02:40:56 +02:00
|
|
|
iovec_t tmp;
|
|
|
|
span<iovec_t const> tmp_bufs = bufs;
|
2017-07-15 02:59:20 +02:00
|
|
|
if (flags & open_mode::coalesce_buffers)
|
2009-01-01 02:47:57 +01:00
|
|
|
{
|
2016-10-27 02:40:56 +02:00
|
|
|
if (!coalesce_read_buffers(tmp_bufs, tmp))
|
2014-07-06 21:18:00 +02:00
|
|
|
// ok, that failed, don't coalesce this read
|
2017-07-15 02:59:20 +02:00
|
|
|
flags &= ~open_mode::coalesce_buffers;
|
2009-01-01 02:47:57 +01:00
|
|
|
}
|
|
|
|
|
2018-01-28 01:38:24 +01:00
|
|
|
#if TORRENT_USE_PREADV
|
2018-06-23 21:46:52 +02:00
|
|
|
std::int64_t ret = iov(&::preadv, native_handle(), file_offset, tmp_bufs, ec);
|
2018-01-28 01:38:24 +01:00
|
|
|
#elif TORRENT_USE_PREAD
|
2016-12-09 14:23:54 +01:00
|
|
|
std::int64_t ret = iov(&::pread, native_handle(), file_offset, tmp_bufs, ec);
|
2014-07-06 21:18:00 +02:00
|
|
|
#else
|
2016-12-09 14:23:54 +01:00
|
|
|
std::int64_t ret = iov(&::read, native_handle(), file_offset, tmp_bufs, ec);
|
2014-05-23 04:09:27 +02:00
|
|
|
#endif
|
2009-01-11 03:02:34 +01:00
|
|
|
|
2017-07-15 02:59:20 +02:00
|
|
|
if (flags & open_mode::coalesce_buffers)
|
2016-10-27 02:40:56 +02:00
|
|
|
coalesce_read_buffers_end(bufs
|
2017-04-29 06:27:55 +02:00
|
|
|
, tmp.data(), !ec);
|
2009-01-11 03:02:34 +01:00
|
|
|
|
|
|
|
return ret;
|
2014-07-06 21:18:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// This has to be thread safe, i.e. atomic.
|
|
|
|
// that means, on posix this has to be turned into a series of
|
|
|
|
// pwrite() calls
|
2016-10-27 02:40:56 +02:00
|
|
|
std::int64_t file::writev(std::int64_t file_offset, span<iovec_t const> bufs
|
2017-05-26 20:49:21 +02:00
|
|
|
, error_code& ec, open_mode_t flags)
|
2014-07-06 21:18:00 +02:00
|
|
|
{
|
|
|
|
if (m_file_handle == INVALID_HANDLE_VALUE)
|
2009-01-11 03:02:34 +01:00
|
|
|
{
|
2014-07-06 21:18:00 +02:00
|
|
|
#ifdef TORRENT_WINDOWS
|
|
|
|
ec = error_code(ERROR_INVALID_HANDLE, system_category());
|
|
|
|
#else
|
2015-11-24 06:50:51 +01:00
|
|
|
ec = error_code(boost::system::errc::bad_file_descriptor, generic_category());
|
2014-07-06 21:18:00 +02:00
|
|
|
#endif
|
2009-01-11 03:02:34 +01:00
|
|
|
return -1;
|
|
|
|
}
|
2017-07-15 02:59:20 +02:00
|
|
|
TORRENT_ASSERT((m_open_mode & open_mode::rw_mask) == open_mode::write_only
|
|
|
|
|| (m_open_mode & open_mode::rw_mask) == open_mode::read_write);
|
2016-10-27 02:40:56 +02:00
|
|
|
TORRENT_ASSERT(!bufs.empty());
|
2014-07-06 21:18:00 +02:00
|
|
|
TORRENT_ASSERT(is_open());
|
2009-04-04 10:23:53 +02:00
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
ec.clear();
|
2009-04-04 10:23:53 +02:00
|
|
|
|
2016-03-20 16:38:55 +01:00
|
|
|
// there's no point in coalescing single buffer writes
|
2016-10-27 02:40:56 +02:00
|
|
|
if (bufs.size() == 1)
|
2016-03-20 16:38:55 +01:00
|
|
|
{
|
2017-07-15 02:59:20 +02:00
|
|
|
flags &= ~open_mode::coalesce_buffers;
|
2016-03-20 16:38:55 +01:00
|
|
|
}
|
|
|
|
|
2016-10-27 02:40:56 +02:00
|
|
|
iovec_t tmp;
|
2017-07-15 02:59:20 +02:00
|
|
|
if (flags & open_mode::coalesce_buffers)
|
2014-07-06 21:18:00 +02:00
|
|
|
{
|
2016-10-27 02:40:56 +02:00
|
|
|
if (!coalesce_write_buffers(bufs, tmp))
|
2014-07-06 21:18:00 +02:00
|
|
|
// ok, that failed, don't coalesce writes
|
2017-07-15 02:59:20 +02:00
|
|
|
flags &= ~open_mode::coalesce_buffers;
|
2011-09-05 07:47:50 +02:00
|
|
|
}
|
|
|
|
|
2018-01-28 01:38:24 +01:00
|
|
|
#if TORRENT_USE_PREADV
|
2018-01-29 00:30:29 +01:00
|
|
|
std::int64_t ret = iov(&::pwritev, native_handle(), file_offset, bufs, ec);
|
2018-01-28 01:38:24 +01:00
|
|
|
#elif TORRENT_USE_PREAD
|
2016-12-09 14:23:54 +01:00
|
|
|
std::int64_t ret = iov(&::pwrite, native_handle(), file_offset, bufs, ec);
|
2014-07-06 21:18:00 +02:00
|
|
|
#else
|
2016-12-09 14:23:54 +01:00
|
|
|
std::int64_t ret = iov(&::write, native_handle(), file_offset, bufs, ec);
|
2014-07-06 21:18:00 +02:00
|
|
|
#endif
|
2011-09-05 07:47:50 +02:00
|
|
|
|
2017-07-15 02:59:20 +02:00
|
|
|
if (flags & open_mode::coalesce_buffers)
|
2017-04-29 06:27:55 +02:00
|
|
|
delete[] tmp.data();
|
2009-04-04 10:23:53 +02:00
|
|
|
|
2015-08-09 04:53:11 +02:00
|
|
|
#if TORRENT_USE_FDATASYNC \
|
2014-07-06 21:18:00 +02:00
|
|
|
&& !defined F_NOCACHE && \
|
|
|
|
!defined DIRECTIO_ON
|
2017-07-15 02:59:20 +02:00
|
|
|
if (m_open_mode & open_mode::no_cache)
|
2009-04-04 10:23:53 +02:00
|
|
|
{
|
2017-09-12 23:10:11 +02:00
|
|
|
if (::fdatasync(native_handle()) != 0
|
2014-07-06 21:18:00 +02:00
|
|
|
&& errno != EINVAL
|
|
|
|
&& errno != ENOSYS)
|
2009-04-04 10:23:53 +02:00
|
|
|
{
|
2015-11-24 06:50:51 +01:00
|
|
|
ec.assign(errno, system_category());
|
2009-04-04 10:23:53 +02:00
|
|
|
}
|
|
|
|
}
|
2014-07-06 21:18:00 +02:00
|
|
|
#endif
|
2009-04-04 10:23:53 +02:00
|
|
|
return ret;
|
2009-09-05 09:21:10 +02:00
|
|
|
}
|
|
|
|
|
2014-02-08 10:18:09 +01:00
|
|
|
#ifdef TORRENT_WINDOWS
|
2018-07-12 16:46:36 +02:00
|
|
|
void acquire_manage_volume_privs()
|
2014-02-08 10:18:09 +01:00
|
|
|
{
|
2018-07-07 18:21:01 +02:00
|
|
|
using OpenProcessToken_t = BOOL (WINAPI*)(HANDLE, DWORD, PHANDLE);
|
2018-03-22 17:01:38 +01:00
|
|
|
|
2018-07-07 18:21:01 +02:00
|
|
|
using LookupPrivilegeValue_t = BOOL (WINAPI*)(LPCSTR, LPCSTR, PLUID);
|
2018-03-22 17:01:38 +01:00
|
|
|
|
|
|
|
using AdjustTokenPrivileges_t = BOOL (WINAPI*)(
|
|
|
|
HANDLE, BOOL, PTOKEN_PRIVILEGES, DWORD, PTOKEN_PRIVILEGES, PDWORD);
|
2014-02-08 10:18:09 +01:00
|
|
|
|
2016-12-28 19:12:20 +01:00
|
|
|
auto OpenProcessToken =
|
|
|
|
aux::get_library_procedure<aux::advapi32, OpenProcessToken_t>("OpenProcessToken");
|
|
|
|
auto LookupPrivilegeValue =
|
|
|
|
aux::get_library_procedure<aux::advapi32, LookupPrivilegeValue_t>("LookupPrivilegeValueA");
|
|
|
|
auto AdjustTokenPrivileges =
|
|
|
|
aux::get_library_procedure<aux::advapi32, AdjustTokenPrivileges_t>("AdjustTokenPrivileges");
|
|
|
|
|
2018-07-12 16:55:51 +02:00
|
|
|
if (OpenProcessToken == nullptr
|
|
|
|
|| LookupPrivilegeValue == nullptr
|
|
|
|
|| AdjustTokenPrivileges == nullptr)
|
2014-02-08 10:18:09 +01:00
|
|
|
{
|
2018-07-12 16:46:36 +02:00
|
|
|
return;
|
2014-02-08 10:18:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
HANDLE token;
|
2016-12-28 19:12:20 +01:00
|
|
|
if (!OpenProcessToken(GetCurrentProcess()
|
2014-02-08 10:18:09 +01:00
|
|
|
, TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &token))
|
2018-07-12 16:46:36 +02:00
|
|
|
return;
|
2014-02-08 10:18:09 +01:00
|
|
|
|
2018-07-07 18:21:01 +02:00
|
|
|
BOOST_SCOPE_EXIT_ALL(&token) {
|
2018-07-06 14:14:38 +02:00
|
|
|
CloseHandle(token);
|
2018-07-07 18:21:01 +02:00
|
|
|
};
|
2018-07-06 14:14:38 +02:00
|
|
|
|
2018-07-12 16:55:51 +02:00
|
|
|
TOKEN_PRIVILEGES privs{};
|
2016-12-28 19:12:20 +01:00
|
|
|
if (!LookupPrivilegeValue(nullptr, "SeManageVolumePrivilege"
|
2014-02-08 10:18:09 +01:00
|
|
|
, &privs.Privileges[0].Luid))
|
|
|
|
{
|
2018-07-12 16:46:36 +02:00
|
|
|
return;
|
2014-02-08 10:18:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
privs.PrivilegeCount = 1;
|
|
|
|
privs.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
|
|
|
|
|
2018-07-12 16:55:51 +02:00
|
|
|
AdjustTokenPrivileges(token, FALSE, &privs, 0, nullptr, nullptr);
|
2014-02-08 10:18:09 +01:00
|
|
|
}
|
|
|
|
|
2016-06-18 20:01:38 +02:00
|
|
|
void set_file_valid_data(HANDLE f, std::int64_t size)
|
2014-02-08 10:18:09 +01:00
|
|
|
{
|
2018-03-22 17:01:38 +01:00
|
|
|
using SetFileValidData_t = BOOL (WINAPI*)(HANDLE, LONGLONG);
|
2016-12-28 19:12:20 +01:00
|
|
|
auto SetFileValidData =
|
|
|
|
aux::get_library_procedure<aux::kernel32, SetFileValidData_t>("SetFileValidData");
|
2014-02-08 10:18:09 +01:00
|
|
|
|
2018-07-12 16:55:51 +02:00
|
|
|
if (SetFileValidData)
|
2018-07-06 14:14:38 +02:00
|
|
|
{
|
2018-07-07 18:21:01 +02:00
|
|
|
// we don't necessarily expect to have enough
|
|
|
|
// privilege to do this, so ignore errors.
|
|
|
|
SetFileValidData(f, size);
|
2018-07-06 14:14:38 +02:00
|
|
|
}
|
2014-02-08 10:18:09 +01:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2016-06-18 20:01:38 +02:00
|
|
|
bool file::set_size(std::int64_t s, error_code& ec)
|
2015-06-21 18:41:50 +02:00
|
|
|
{
|
|
|
|
TORRENT_ASSERT(is_open());
|
|
|
|
TORRENT_ASSERT(s >= 0);
|
2004-01-16 03:57:45 +01:00
|
|
|
|
2008-07-20 13:14:54 +02:00
|
|
|
#ifdef TORRENT_WINDOWS
|
2011-11-13 05:12:56 +01:00
|
|
|
|
2009-01-11 03:02:34 +01:00
|
|
|
LARGE_INTEGER offs;
|
2010-03-06 21:47:44 +01:00
|
|
|
LARGE_INTEGER cur_size;
|
2014-07-06 21:18:00 +02:00
|
|
|
if (GetFileSizeEx(native_handle(), &cur_size) == FALSE)
|
2009-01-11 03:02:34 +01:00
|
|
|
{
|
2014-07-06 21:18:00 +02:00
|
|
|
ec.assign(GetLastError(), system_category());
|
2009-01-11 03:02:34 +01:00
|
|
|
return false;
|
|
|
|
}
|
2010-03-06 21:47:44 +01:00
|
|
|
offs.QuadPart = s;
|
|
|
|
// only set the file size if it's not already at
|
|
|
|
// the right size. We don't want to update the
|
|
|
|
// modification time if we don't have to
|
|
|
|
if (cur_size.QuadPart != s)
|
|
|
|
{
|
2014-07-06 21:18:00 +02:00
|
|
|
if (SetFilePointerEx(native_handle(), offs, &offs, FILE_BEGIN) == FALSE)
|
2010-03-06 21:47:44 +01:00
|
|
|
{
|
2014-07-06 21:18:00 +02:00
|
|
|
ec.assign(GetLastError(), system_category());
|
2010-03-06 21:47:44 +01:00
|
|
|
return false;
|
|
|
|
}
|
2014-07-06 21:18:00 +02:00
|
|
|
if (::SetEndOfFile(native_handle()) == FALSE)
|
2010-03-06 21:47:44 +01:00
|
|
|
{
|
2014-07-06 21:18:00 +02:00
|
|
|
ec.assign(GetLastError(), system_category());
|
2010-03-06 21:47:44 +01:00
|
|
|
return false;
|
|
|
|
}
|
2017-09-28 01:14:20 +02:00
|
|
|
if (!(m_open_mode & open_mode::sparse))
|
2014-02-08 10:18:09 +01:00
|
|
|
{
|
|
|
|
// if the user has permissions, avoid filling
|
|
|
|
// the file with zeroes, but just fill it with
|
|
|
|
// garbage instead
|
|
|
|
set_file_valid_data(m_file_handle, s);
|
2010-03-20 09:18:49 +01:00
|
|
|
}
|
2009-02-14 05:18:17 +01:00
|
|
|
}
|
2010-10-18 09:15:57 +02:00
|
|
|
#else // NON-WINDOWS
|
2018-01-11 01:35:15 +01:00
|
|
|
struct stat st{};
|
2017-09-12 23:10:11 +02:00
|
|
|
if (::fstat(native_handle(), &st) != 0)
|
2008-07-20 13:14:54 +02:00
|
|
|
{
|
2015-11-24 06:50:51 +01:00
|
|
|
ec.assign(errno, system_category());
|
2008-07-20 13:14:54 +02:00
|
|
|
return false;
|
|
|
|
}
|
2010-03-06 21:09:18 +01:00
|
|
|
|
|
|
|
// only truncate the file if it doesn't already
|
|
|
|
// have the right size. We don't want to update
|
2017-09-12 23:10:11 +02:00
|
|
|
if (st.st_size != s && ::ftruncate(native_handle(), s) < 0)
|
2010-03-06 21:09:18 +01:00
|
|
|
{
|
2015-11-24 06:50:51 +01:00
|
|
|
ec.assign(errno, system_category());
|
2010-03-06 21:09:18 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// if we're not in sparse mode, allocate the storage
|
|
|
|
// but only if the number of allocated blocks for the file
|
|
|
|
// is less than the file size. Otherwise we would just
|
|
|
|
// update the modification time of the file for no good
|
|
|
|
// reason.
|
2017-07-15 02:59:20 +02:00
|
|
|
if (!(m_open_mode & open_mode::sparse)
|
2016-12-12 03:37:07 +01:00
|
|
|
&& std::int64_t(st.st_blocks) < (s + st.st_blksize - 1) / st.st_blksize)
|
2009-02-16 01:42:44 +01:00
|
|
|
{
|
2010-03-06 21:09:18 +01:00
|
|
|
// How do we know that the file is already allocated?
|
|
|
|
// if we always try to allocate the space, we'll update
|
|
|
|
// the modification time without actually changing the file
|
|
|
|
// but if we don't do anything if the file size is
|
2009-02-16 01:42:44 +01:00
|
|
|
#ifdef F_PREALLOCATE
|
|
|
|
fstore_t f = {F_ALLOCATECONTIG, F_PEOFPOSMODE, 0, s, 0};
|
2014-07-06 21:18:00 +02:00
|
|
|
if (fcntl(native_handle(), F_PREALLOCATE, &f) < 0)
|
2009-02-16 01:42:44 +01:00
|
|
|
{
|
2020-01-14 14:20:19 +01:00
|
|
|
// MacOS returns EINVAL if the file already has the space
|
|
|
|
// pre-allocated. In which case we can just move on.
|
2017-10-15 15:19:25 +02:00
|
|
|
if (errno != EINVAL)
|
2011-04-13 08:54:56 +02:00
|
|
|
{
|
2017-10-15 15:19:25 +02:00
|
|
|
if (errno != ENOSPC)
|
|
|
|
{
|
|
|
|
ec.assign(errno, system_category());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// ok, let's try to allocate non contiguous space then
|
|
|
|
f.fst_flags = F_ALLOCATEALL;
|
|
|
|
if (fcntl(native_handle(), F_PREALLOCATE, &f) < 0)
|
|
|
|
{
|
|
|
|
ec.assign(errno, system_category());
|
|
|
|
return false;
|
|
|
|
}
|
2011-04-13 08:54:56 +02:00
|
|
|
}
|
2009-02-16 01:42:44 +01:00
|
|
|
}
|
2010-04-08 07:09:11 +02:00
|
|
|
#endif // F_PREALLOCATE
|
|
|
|
|
2011-09-09 01:08:37 +02:00
|
|
|
#ifdef F_ALLOCSP64
|
|
|
|
flock64 fl64;
|
|
|
|
fl64.l_whence = SEEK_SET;
|
|
|
|
fl64.l_start = 0;
|
|
|
|
fl64.l_len = s;
|
|
|
|
if (fcntl(native_handle(), F_ALLOCSP64, &fl64) < 0)
|
|
|
|
{
|
2015-11-24 06:50:51 +01:00
|
|
|
ec.assign(errno, system_category());
|
2011-09-09 01:08:37 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // F_ALLOCSP64
|
|
|
|
|
2010-04-08 07:09:11 +02:00
|
|
|
#if TORRENT_HAS_FALLOCATE
|
2010-03-06 21:09:18 +01:00
|
|
|
// if fallocate failed, we have to use posix_fallocate
|
|
|
|
// which can be painfully slow
|
2010-04-08 07:31:00 +02:00
|
|
|
// if you get a compile error here, you might want to
|
|
|
|
// define TORRENT_HAS_FALLOCATE to 0.
|
2016-04-30 06:24:45 +02:00
|
|
|
int const ret = posix_fallocate(native_handle(), 0, s);
|
2010-10-18 09:15:57 +02:00
|
|
|
// posix_allocate fails with EINVAL in case the underlying
|
2015-09-21 23:58:20 +02:00
|
|
|
// filesystem does not support this operation
|
2018-08-19 21:55:31 +02:00
|
|
|
if (ret != 0 && ret != EINVAL && ret != ENOTSUP)
|
2010-01-18 01:51:40 +01:00
|
|
|
{
|
2015-11-24 06:50:51 +01:00
|
|
|
ec.assign(ret, system_category());
|
2010-01-18 01:51:40 +01:00
|
|
|
return false;
|
|
|
|
}
|
2010-04-08 07:09:11 +02:00
|
|
|
#endif // TORRENT_HAS_FALLOCATE
|
2009-02-16 01:42:44 +01:00
|
|
|
}
|
2010-04-08 07:09:11 +02:00
|
|
|
#endif // TORRENT_WINDOWS
|
2008-07-20 13:14:54 +02:00
|
|
|
return true;
|
2004-01-16 03:57:45 +01:00
|
|
|
}
|
|
|
|
|
2016-06-18 20:01:38 +02:00
|
|
|
std::int64_t file::get_size(error_code& ec) const
|
2004-01-16 03:57:45 +01:00
|
|
|
{
|
2008-07-20 13:14:54 +02:00
|
|
|
#ifdef TORRENT_WINDOWS
|
2009-01-11 03:02:34 +01:00
|
|
|
LARGE_INTEGER file_size;
|
2014-07-06 21:18:00 +02:00
|
|
|
if (!GetFileSizeEx(native_handle(), &file_size))
|
2008-07-20 13:14:54 +02:00
|
|
|
{
|
2014-07-06 21:18:00 +02:00
|
|
|
ec.assign(GetLastError(), system_category());
|
2008-07-20 13:14:54 +02:00
|
|
|
return -1;
|
|
|
|
}
|
2009-01-11 03:02:34 +01:00
|
|
|
return file_size.QuadPart;
|
2008-07-20 13:14:54 +02:00
|
|
|
#else
|
2018-01-11 01:35:15 +01:00
|
|
|
struct stat fs = {};
|
2017-09-12 23:10:11 +02:00
|
|
|
if (::fstat(native_handle(), &fs) != 0)
|
2008-07-20 13:14:54 +02:00
|
|
|
{
|
2015-11-24 06:50:51 +01:00
|
|
|
ec.assign(errno, system_category());
|
2008-07-20 13:14:54 +02:00
|
|
|
return -1;
|
|
|
|
}
|
2009-01-11 03:02:34 +01:00
|
|
|
return fs.st_size;
|
2009-02-17 01:11:38 +01:00
|
|
|
#endif
|
|
|
|
}
|
2004-01-16 03:57:45 +01:00
|
|
|
}
|