2004-01-16 03:57:45 +01:00
|
|
|
/*
|
|
|
|
|
2012-10-02 05:16:33 +02:00
|
|
|
Copyright (c) 2003-2012, 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.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
2009-09-05 09:21:10 +02:00
|
|
|
/*
|
|
|
|
Physical file offset patch by Morten Husveit
|
|
|
|
*/
|
|
|
|
|
2007-03-17 18:15:16 +01:00
|
|
|
#include "libtorrent/pch.hpp"
|
2008-07-20 13:14:54 +02:00
|
|
|
#include "libtorrent/config.hpp"
|
2009-01-11 03:02:34 +01:00
|
|
|
#include "libtorrent/alloca.hpp"
|
2009-12-09 02:55:30 +01:00
|
|
|
#include "libtorrent/allocator.hpp" // page_size
|
2009-10-26 02:29:39 +01:00
|
|
|
#include "libtorrent/escape_string.hpp" // for string conversion
|
2007-03-17 18:15:16 +01:00
|
|
|
|
2008-02-14 04:48:20 +01:00
|
|
|
#include <boost/scoped_ptr.hpp>
|
2009-11-26 22:05:57 +01:00
|
|
|
#include <boost/static_assert.hpp>
|
2009-10-26 02:29:39 +01:00
|
|
|
|
2012-08-21 23:54:07 +02:00
|
|
|
#include <sys/stat.h>
|
|
|
|
|
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"
|
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>
|
2012-08-21 23:54:07 +02:00
|
|
|
#ifndef TORRENT_MINGW
|
2009-10-26 02:29:39 +01:00
|
|
|
#include <direct.h> // for _getcwd, _mkdir
|
2012-08-21 23:54:07 +02:00
|
|
|
#else
|
|
|
|
#include <dirent.h>
|
|
|
|
#endif
|
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
|
|
|
#define _FILE_OFFSET_BITS 64
|
|
|
|
#include <unistd.h>
|
2009-09-05 17:02:49 +02:00
|
|
|
#include <fcntl.h> // for F_LOG2PHYS
|
2004-04-02 12:43:37 +02:00
|
|
|
#include <sys/types.h>
|
2009-01-17 10:37:40 +01:00
|
|
|
#include <sys/statvfs.h>
|
2004-10-31 09:20:05 +01:00
|
|
|
#include <errno.h>
|
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>
|
2010-01-17 22:56:24 +01:00
|
|
|
#ifdef HAVE_LINUX_FIEMAP_H
|
2010-04-11 02:48:48 +02:00
|
|
|
#include <linux/fiemap.h> // FIEMAP_*
|
|
|
|
#include <linux/fs.h> // FS_IOC_FIEMAP
|
2009-09-05 09:21:10 +02:00
|
|
|
#endif
|
2009-10-26 02:29:39 +01:00
|
|
|
|
2010-01-31 22:52:45 +01:00
|
|
|
#include <asm/unistd.h> // For __NR_fallocate
|
2010-01-18 01:51:40 +01:00
|
|
|
|
|
|
|
// circumvent the lack of support in glibc
|
2010-01-18 02:08:19 +01:00
|
|
|
static int my_fallocate(int fd, int mode, loff_t offset, loff_t len)
|
2010-01-18 01:51:40 +01:00
|
|
|
{
|
2010-01-31 22:52:45 +01:00
|
|
|
#ifdef __NR_fallocate
|
2010-03-06 21:09:18 +01:00
|
|
|
// the man page on fallocate differes between versions of linux.
|
|
|
|
// it appears that fallocate in fact sets errno and returns -1
|
|
|
|
// on failure.
|
2010-01-18 01:51:40 +01:00
|
|
|
return syscall(__NR_fallocate, fd, mode, offset, len);
|
2010-01-31 22:52:45 +01:00
|
|
|
#else
|
2010-03-06 21:09:18 +01:00
|
|
|
// pretend that the system call doesn't exist
|
|
|
|
errno = ENOSYS;
|
|
|
|
return -1;
|
2010-01-31 22:52:45 +01:00
|
|
|
#endif
|
2010-01-18 01:51:40 +01:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2010-09-07 05:37:33 +02:00
|
|
|
#undef _FILE_OFFSET_BITS
|
|
|
|
|
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
|
2005-08-23 11:59:56 +02:00
|
|
|
BOOST_STATIC_ASSERT(sizeof(lseek(0, 0, 0)) >= 8);
|
|
|
|
|
2009-10-26 02:29:39 +01:00
|
|
|
#endif // posix part
|
2004-03-31 01:55:52 +02:00
|
|
|
|
2005-08-23 11:59:56 +02:00
|
|
|
#include "libtorrent/file.hpp"
|
2007-11-18 08:27:40 +01:00
|
|
|
#include <cstring>
|
2008-07-01 01:14:31 +02:00
|
|
|
#include <vector>
|
2005-08-23 11:59:56 +02:00
|
|
|
|
2009-03-01 01:02:33 +01:00
|
|
|
// for convert_to_wstring and convert_to_native
|
|
|
|
#include "libtorrent/escape_string.hpp"
|
2009-10-27 02:47:54 +01:00
|
|
|
#include <stdio.h>
|
2007-10-06 19:27:53 +02:00
|
|
|
#include "libtorrent/assert.hpp"
|
2004-04-05 00:15:31 +02:00
|
|
|
|
2009-01-11 23:27:43 +01:00
|
|
|
#ifdef TORRENT_DEBUG
|
2009-01-11 03:02:34 +01:00
|
|
|
BOOST_STATIC_ASSERT((libtorrent::file::rw_mask & libtorrent::file::no_buffer) == 0);
|
2009-01-11 23:27:43 +01:00
|
|
|
BOOST_STATIC_ASSERT((libtorrent::file::rw_mask & libtorrent::file::attribute_mask) == 0);
|
|
|
|
BOOST_STATIC_ASSERT((libtorrent::file::no_buffer & libtorrent::file::attribute_mask) == 0);
|
|
|
|
#endif
|
2009-01-11 03:02:34 +01:00
|
|
|
|
2010-06-06 03:18:58 +02:00
|
|
|
#ifdef TORRENT_WINDOWS
|
|
|
|
#if defined UNICODE && !TORRENT_USE_WSTRING
|
|
|
|
#warning wide character support not available. Files will be saved using narrow string names
|
|
|
|
#endif
|
|
|
|
#endif // TORRENT_WINDOWS
|
|
|
|
|
2004-01-16 03:57:45 +01:00
|
|
|
namespace libtorrent
|
|
|
|
{
|
2012-03-17 20:06:11 +01:00
|
|
|
|
|
|
|
#ifdef TORRENT_WINDOWS
|
|
|
|
std::string convert_separators(std::string p)
|
|
|
|
{
|
|
|
|
for (int i = 0; i < p.size(); ++i)
|
|
|
|
if (p[i] == '/') p[i] = '\\';
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2011-01-25 09:21:01 +01:00
|
|
|
void stat_file(std::string inf, file_status* s
|
2010-04-01 02:44:29 +02:00
|
|
|
, error_code& ec, int flags)
|
2009-10-26 02:29:39 +01:00
|
|
|
{
|
|
|
|
ec.clear();
|
2011-01-25 09:21:01 +01:00
|
|
|
#ifdef TORRENT_WINDOWS
|
|
|
|
// apparently windows doesn't expect paths
|
|
|
|
// to directories to ever end with a \ or /
|
|
|
|
if (!inf.empty() && (inf[inf.size() - 1] == '\\'
|
|
|
|
|| inf[inf.size() - 1] == '/'))
|
|
|
|
inf.resize(inf.size() - 1);
|
|
|
|
#endif
|
2009-10-26 02:29:39 +01:00
|
|
|
|
2010-02-15 06:49:10 +01:00
|
|
|
#if TORRENT_USE_WSTRING && defined TORRENT_WINDOWS
|
2009-10-26 02:29:39 +01:00
|
|
|
std::wstring f = convert_to_wstring(inf);
|
|
|
|
#else
|
|
|
|
std::string f = convert_to_native(inf);
|
|
|
|
#endif
|
|
|
|
|
2012-08-21 23:54:07 +02:00
|
|
|
#if defined TORRENT_WINDOWS
|
2009-10-26 02:29:39 +01:00
|
|
|
struct _stati64 ret;
|
2010-02-15 06:49:10 +01:00
|
|
|
#if TORRENT_USE_WSTRING
|
2009-10-26 02:29:39 +01:00
|
|
|
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;
|
2010-04-01 02:44:29 +02:00
|
|
|
int retval;
|
|
|
|
if (flags & dont_follow_links)
|
|
|
|
retval = ::lstat(f.c_str(), &ret);
|
|
|
|
else
|
|
|
|
retval = ::stat(f.c_str(), &ret);
|
|
|
|
if (retval < 0)
|
2009-10-26 02:29:39 +01:00
|
|
|
{
|
|
|
|
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;
|
2012-08-21 23:54:07 +02:00
|
|
|
#if defined TORRENT_WINDOWS
|
|
|
|
s->mode = ((ret.st_mode & _S_IFREG) ? file_status::regular_file : 0)
|
|
|
|
| ((ret.st_mode & _S_IFDIR) ? file_status::directory : 0)
|
|
|
|
| ((ret.st_mode & _S_IFCHR) ? file_status::character_special : 0)
|
|
|
|
| ((ret.st_mode & _S_IFIFO) ? file_status::fifo : 0);
|
|
|
|
#else
|
|
|
|
s->mode = (S_ISREG(ret.st_mode) ? file_status::regular_file : 0)
|
|
|
|
| (S_ISDIR(ret.st_mode) ? file_status::directory : 0)
|
|
|
|
| (S_ISLNK(ret.st_mode) ? file_status::link : 0)
|
|
|
|
| (S_ISFIFO(ret.st_mode) ? file_status::fifo : 0)
|
|
|
|
| (S_ISCHR(ret.st_mode) ? file_status::character_special : 0)
|
|
|
|
| (S_ISBLK(ret.st_mode) ? file_status::block_special : 0)
|
|
|
|
| (S_ISSOCK(ret.st_mode) ? file_status::socket : 0);
|
|
|
|
#endif
|
2009-10-26 02:29:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void rename(std::string const& inf, std::string const& newf, error_code& ec)
|
|
|
|
{
|
|
|
|
ec.clear();
|
|
|
|
|
2010-02-15 06:49:10 +01:00
|
|
|
#if TORRENT_USE_WSTRING && defined TORRENT_WINDOWS
|
2009-10-26 02:29:39 +01:00
|
|
|
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);
|
2009-10-27 02:47:54 +01:00
|
|
|
if (::rename(f1.c_str(), f2.c_str()) < 0)
|
2009-10-26 02:29:39 +01:00
|
|
|
#endif
|
|
|
|
{
|
|
|
|
ec.assign(errno, boost::system::get_generic_category());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void create_directories(std::string const& f, error_code& ec)
|
|
|
|
{
|
|
|
|
ec.clear();
|
2011-09-05 01:29:47 +02:00
|
|
|
if (is_directory(f, ec)) return;
|
|
|
|
if (ec != boost::system::errc::no_such_file_or_directory)
|
|
|
|
return;
|
|
|
|
ec.clear();
|
2009-10-26 02:29:39 +01:00
|
|
|
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();
|
|
|
|
|
2010-02-15 06:49:10 +01:00
|
|
|
#if defined TORRENT_WINDOWS && TORRENT_USE_WSTRING
|
|
|
|
#define CreateDirectory_ CreateDirectoryW
|
2009-10-26 02:29:39 +01:00
|
|
|
std::wstring n = convert_to_wstring(f);
|
|
|
|
#else
|
2010-02-15 06:49:10 +01:00
|
|
|
#define CreateDirectory_ CreateDirectoryA
|
2009-10-26 02:29:39 +01:00
|
|
|
std::string n = convert_to_native(f);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef TORRENT_WINDOWS
|
2010-02-15 06:49:10 +01:00
|
|
|
if (CreateDirectory_(n.c_str(), 0) == 0
|
2009-10-26 02:29:39 +01:00
|
|
|
&& 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;
|
|
|
|
}
|
|
|
|
|
2012-06-30 01:35:11 +02:00
|
|
|
void recursive_copy(std::string const& old_path, std::string const& new_path, error_code& ec)
|
|
|
|
{
|
|
|
|
TORRENT_ASSERT(!ec);
|
|
|
|
if (is_directory(old_path, ec))
|
|
|
|
{
|
|
|
|
create_directory(new_path, ec);
|
|
|
|
if (ec) return;
|
|
|
|
for (directory i(old_path, ec); !i.done(); i.next(ec))
|
|
|
|
{
|
|
|
|
std::string f = i.file();
|
|
|
|
if (f == ".." || f == ".") continue;
|
|
|
|
recursive_copy(combine_path(old_path, f), combine_path(new_path, f), ec);
|
|
|
|
if (ec) return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (!ec)
|
|
|
|
{
|
|
|
|
copy_file(old_path, new_path, ec);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-10-26 02:29:39 +01:00
|
|
|
void copy_file(std::string const& inf, std::string const& newf, error_code& ec)
|
|
|
|
{
|
|
|
|
ec.clear();
|
2010-02-15 06:49:10 +01:00
|
|
|
#if TORRENT_USE_WSTRING && defined TORRENT_WINDOWS
|
|
|
|
#define CopyFile_ CopyFileW
|
2009-10-26 02:29:39 +01:00
|
|
|
std::wstring f1 = convert_to_wstring(inf);
|
|
|
|
std::wstring f2 = convert_to_wstring(newf);
|
|
|
|
#else
|
2010-02-15 06:49:10 +01:00
|
|
|
#define CopyFile_ CopyFileA
|
2009-10-26 02:29:39 +01:00
|
|
|
std::string f1 = convert_to_native(inf);
|
|
|
|
std::string f2 = convert_to_native(newf);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef TORRENT_WINDOWS
|
2010-02-15 06:49:10 +01:00
|
|
|
if (CopyFile_(f1.c_str(), f2.c_str(), false) == 0)
|
2009-10-26 02:29:39 +01:00
|
|
|
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
|
2009-10-28 20:52:58 +01:00
|
|
|
int infd = ::open(inf.c_str(), O_RDONLY);
|
2009-10-26 02:29:39 +01:00
|
|
|
if (infd < 0)
|
|
|
|
{
|
|
|
|
ec.assign(errno, boost::system::get_generic_category());
|
|
|
|
return;
|
|
|
|
}
|
2009-10-28 20:52:58 +01:00
|
|
|
|
|
|
|
// rely on default umask to filter x and w permissions
|
|
|
|
// for group and others
|
|
|
|
int permissions = S_IRUSR | S_IWUSR
|
|
|
|
| S_IRGRP | S_IWGRP
|
|
|
|
| S_IROTH | S_IWOTH;
|
|
|
|
|
|
|
|
int outfd = ::open(newf.c_str(), O_WRONLY | O_CREAT, permissions);
|
2009-10-26 02:29:39 +01:00
|
|
|
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;
|
2011-02-21 06:24:41 +01:00
|
|
|
if (num_read < 0)
|
|
|
|
{
|
|
|
|
ec.assign(errno, boost::system::get_generic_category());
|
|
|
|
break;
|
|
|
|
}
|
2009-10-26 02:29:39 +01:00
|
|
|
int num_written = write(outfd, buffer, num_read);
|
|
|
|
if (num_written < num_read)
|
|
|
|
{
|
|
|
|
ec.assign(errno, boost::system::get_generic_category());
|
|
|
|
break;
|
|
|
|
}
|
2011-02-14 02:59:01 +01:00
|
|
|
if (num_read < int(sizeof(buffer))) break;
|
2009-10-26 02:29:39 +01:00
|
|
|
}
|
|
|
|
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'
|
2012-09-24 18:13:57 +02:00
|
|
|
#if defined(TORRENT_WINDOWS) || defined(TORRENT_OS2)
|
2009-10-26 02:29:39 +01:00
|
|
|
&& *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)
|
|
|
|
{
|
2009-10-30 04:32:24 +01:00
|
|
|
char const* e = strrchr(f.c_str(), '.');
|
2009-10-26 02:29:39 +01:00
|
|
|
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;
|
|
|
|
|
2012-09-24 18:13:57 +02:00
|
|
|
#if defined(TORRENT_WINDOWS) || defined(TORRENT_OS2)
|
2011-11-27 11:23:50 +01:00
|
|
|
// match \\ form
|
2009-10-26 02:29:39 +01:00
|
|
|
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;
|
2011-11-27 11:23:50 +01:00
|
|
|
// match network paths \\computer_name\ form
|
|
|
|
if (f.size() > 2 && f[0] == '\\' && f[1] == '\\')
|
|
|
|
{
|
|
|
|
// we don't care about the last character, since it's OK for it
|
|
|
|
// to be a slash or a back slash
|
|
|
|
bool found = false;
|
|
|
|
for (int i = 2; i < f.size() - 1; ++i)
|
|
|
|
{
|
|
|
|
if (f[i] != '\\' && f[i] != '/') continue;
|
|
|
|
// there is a directory separator in here,
|
|
|
|
// i.e. this is not the root
|
|
|
|
found = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (!found) return true;
|
|
|
|
}
|
2009-10-26 02:29:39 +01:00
|
|
|
#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;
|
2011-11-27 11:23:50 +01:00
|
|
|
if (is_root_path(f)) return false;
|
2009-10-26 02:29:39 +01:00
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
2009-11-02 05:08:37 +01:00
|
|
|
if (f.empty()) return "";
|
|
|
|
char const* first = f.c_str();
|
|
|
|
char const* sep = strrchr(first, '/');
|
2012-09-24 18:13:57 +02:00
|
|
|
#if defined(TORRENT_WINDOWS) || defined(TORRENT_OS2)
|
2009-11-02 05:08:37 +01:00
|
|
|
char const* altsep = strrchr(first, '\\');
|
2009-10-26 02:29:39 +01:00
|
|
|
if (sep == 0 || altsep > sep) sep = altsep;
|
|
|
|
#endif
|
|
|
|
if (sep == 0) return f;
|
2009-11-02 05:08:37 +01:00
|
|
|
|
2011-02-14 02:59:01 +01:00
|
|
|
if (sep - first == int(f.size()) - 1)
|
2009-11-02 05:08:37 +01:00
|
|
|
{
|
|
|
|
// if the last character is a / (or \)
|
|
|
|
// ignore it
|
|
|
|
int len = 0;
|
|
|
|
while (sep > first)
|
|
|
|
{
|
|
|
|
--sep;
|
|
|
|
if (*sep == '/'
|
2012-09-24 18:13:57 +02:00
|
|
|
#if defined(TORRENT_WINDOWS) || defined(TORRENT_OS2)
|
2009-11-02 05:08:37 +01:00
|
|
|
|| *sep == '\\'
|
|
|
|
#endif
|
|
|
|
)
|
|
|
|
return std::string(sep + 1, len);
|
|
|
|
++len;
|
|
|
|
}
|
|
|
|
return std::string(first, len);
|
|
|
|
|
|
|
|
}
|
2009-10-26 02:29:39 +01:00
|
|
|
return std::string(sep + 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string combine_path(std::string const& lhs, std::string const& rhs)
|
|
|
|
{
|
|
|
|
TORRENT_ASSERT(!is_complete(rhs));
|
2012-03-13 05:50:12 +01:00
|
|
|
if (lhs.empty() || lhs == ".") return rhs;
|
|
|
|
if (rhs.empty() || rhs == ".") return lhs;
|
2009-10-26 02:29:39 +01:00
|
|
|
|
2012-09-24 18:13:57 +02:00
|
|
|
#if defined(TORRENT_WINDOWS) || defined(TORRENT_OS2)
|
2010-03-07 22:59:02 +01:00
|
|
|
#define TORRENT_SEPARATOR "\\"
|
2009-10-26 02:29:39 +01:00
|
|
|
bool need_sep = lhs[lhs.size()-1] != '\\' && lhs[lhs.size()-1] != '/';
|
|
|
|
#else
|
2010-03-07 22:59:02 +01:00
|
|
|
#define TORRENT_SEPARATOR "/"
|
2009-10-26 02:29:39 +01:00
|
|
|
bool need_sep = lhs[lhs.size()-1] != '/';
|
|
|
|
#endif
|
2010-03-07 22:59:02 +01:00
|
|
|
std::string ret;
|
|
|
|
int target_size = lhs.size() + rhs.size() + 2;
|
|
|
|
ret.resize(target_size);
|
2010-03-08 03:55:16 +01:00
|
|
|
target_size = snprintf(&ret[0], target_size, "%s%s%s", lhs.c_str()
|
2010-03-07 22:59:02 +01:00
|
|
|
, (need_sep?TORRENT_SEPARATOR:""), rhs.c_str());
|
2010-03-08 03:55:16 +01:00
|
|
|
ret.resize(target_size);
|
2010-03-07 22:59:02 +01:00
|
|
|
return ret;
|
2009-10-26 02:29:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string current_working_directory()
|
|
|
|
{
|
2012-08-21 23:54:07 +02:00
|
|
|
#if defined TORRENT_WINDOWS && !defined TORRENT_MINGW
|
2010-02-15 06:49:10 +01:00
|
|
|
#if TORRENT_USE_WSTRING
|
2009-10-26 02:29:39 +01:00
|
|
|
wchar_t cwd[TORRENT_MAX_PATH];
|
|
|
|
_wgetcwd(cwd, sizeof(cwd) / sizeof(wchar_t));
|
|
|
|
#else
|
|
|
|
char cwd[TORRENT_MAX_PATH];
|
|
|
|
_getcwd(cwd, sizeof(cwd));
|
2010-02-15 06:49:10 +01:00
|
|
|
#endif // TORRENT_USE_WSTRING
|
2009-10-26 02:29:39 +01:00
|
|
|
#else
|
|
|
|
char cwd[TORRENT_MAX_PATH];
|
2009-10-28 20:52:58 +01:00
|
|
|
if (getcwd(cwd, sizeof(cwd)) == 0) return "/";
|
2009-10-26 02:29:39 +01:00
|
|
|
#endif
|
2012-09-08 21:21:31 +02:00
|
|
|
#if defined TORRENT_WINDOWS && !defined TORRENT_MINGW && TORRENT_USE_WSTRING
|
2009-10-26 02:29:39 +01:00
|
|
|
return convert_from_wstring(cwd);
|
|
|
|
#else
|
|
|
|
return convert_from_native(cwd);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2012-04-28 23:13:55 +02:00
|
|
|
#if TORRENT_USE_UNC_PATHS
|
|
|
|
std::string canonicalize_path(std::string const& f)
|
|
|
|
{
|
|
|
|
std::string ret;
|
|
|
|
ret.resize(f.size());
|
|
|
|
char* write_cur = &ret[0];
|
|
|
|
char* last_write_sep = write_cur;
|
|
|
|
|
|
|
|
char const* read_cur = f.c_str();
|
|
|
|
char const* last_read_sep = read_cur;
|
|
|
|
|
|
|
|
// the last_*_sep pointers point to one past
|
|
|
|
// the last path separator encountered and is
|
|
|
|
// initializes to the first character in the path
|
|
|
|
while (*read_cur)
|
|
|
|
{
|
|
|
|
if (*read_cur != '\\')
|
|
|
|
{
|
|
|
|
*write_cur++ = *read_cur++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
int element_len = read_cur - last_read_sep;
|
|
|
|
if (element_len == 1 && memcmp(last_read_sep, ".", 1) == 0)
|
|
|
|
{
|
|
|
|
--write_cur;
|
|
|
|
++read_cur;
|
|
|
|
last_read_sep = read_cur;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (element_len == 2 && memcmp(last_read_sep, "..", 2) == 0)
|
|
|
|
{
|
|
|
|
// find the previous path separator
|
|
|
|
if (last_write_sep > &ret[0])
|
|
|
|
{
|
|
|
|
--last_write_sep;
|
|
|
|
while (last_write_sep > &ret[0]
|
|
|
|
&& last_write_sep[-1] != '\\')
|
|
|
|
--last_write_sep;
|
|
|
|
}
|
|
|
|
write_cur = last_write_sep;
|
|
|
|
// find the previous path separator
|
|
|
|
if (last_write_sep > &ret[0])
|
|
|
|
{
|
|
|
|
--last_write_sep;
|
|
|
|
while (last_write_sep > &ret[0]
|
|
|
|
&& last_write_sep[-1] != '\\')
|
|
|
|
--last_write_sep;
|
|
|
|
}
|
|
|
|
++read_cur;
|
|
|
|
last_read_sep = read_cur;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
*write_cur++ = *read_cur++;
|
|
|
|
last_write_sep = write_cur;
|
|
|
|
last_read_sep = read_cur;
|
|
|
|
}
|
|
|
|
// terminate destination string
|
|
|
|
*write_cur = 0;
|
|
|
|
ret.resize(write_cur - &ret[0]);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2009-10-26 02:29:39 +01:00
|
|
|
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
|
2009-11-28 09:58:07 +01:00
|
|
|
// windows does not allow trailing / or \ in
|
|
|
|
// the path when removing files
|
|
|
|
std::string pruned;
|
|
|
|
if (inf[inf.size() - 1] == '/'
|
|
|
|
|| inf[inf.size() - 1] == '\\')
|
|
|
|
pruned = inf.substr(0, inf.size() - 1);
|
|
|
|
else
|
|
|
|
pruned = inf;
|
2010-02-15 06:49:10 +01:00
|
|
|
#if TORRENT_USE_WSTRING
|
|
|
|
#define DeleteFile_ DeleteFileW
|
|
|
|
#define RemoveDirectory_ RemoveDirectoryW
|
2009-11-28 09:58:07 +01:00
|
|
|
std::wstring f = convert_to_wstring(pruned);
|
2009-10-26 02:29:39 +01:00
|
|
|
#else
|
2010-02-15 06:49:10 +01:00
|
|
|
#define DeleteFile_ DeleteFileA
|
|
|
|
#define RemoveDirectory_ RemoveDirectoryA
|
2009-11-28 09:58:07 +01:00
|
|
|
std::string f = convert_to_native(pruned);
|
2009-10-26 02:29:39 +01:00
|
|
|
#endif
|
2010-02-15 06:49:10 +01:00
|
|
|
if (DeleteFile_(f.c_str()) == 0)
|
2009-10-26 02:29:39 +01:00
|
|
|
{
|
|
|
|
if (GetLastError() == ERROR_ACCESS_DENIED)
|
|
|
|
{
|
2010-02-15 06:49:10 +01:00
|
|
|
if (RemoveDirectory_(f.c_str()) != 0)
|
2009-10-26 02:29:39 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
ec.assign(GetLastError(), boost::system::get_system_category());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
#else // TORRENT_WINDOWS
|
|
|
|
std::string f = convert_to_native(inf);
|
2009-10-27 02:47:54 +01:00
|
|
|
if (::remove(f.c_str()) < 0)
|
2009-10-26 02:29:39 +01:00
|
|
|
{
|
|
|
|
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;
|
2012-09-24 18:13:57 +02:00
|
|
|
#if defined(TORRENT_WINDOWS) || defined(TORRENT_OS2)
|
2009-10-26 02:29:39 +01:00
|
|
|
int i = 0;
|
|
|
|
// match the xx:\ or xx:/ form
|
|
|
|
while (f[i] && is_alpha(f[i])) ++i;
|
2010-03-21 18:40:15 +01:00
|
|
|
if (i < int(f.size()-1) && f[i] == ':' && (f[i+1] == '\\' || f[i+1] == '/'))
|
2009-10-26 02:29:39 +01:00
|
|
|
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
|
2012-03-17 20:06:11 +01:00
|
|
|
std::string f = convert_separators(path);
|
|
|
|
if (!f.empty() && f[f.size()-1] != '\\') f += "\\*";
|
2009-10-26 02:29:39 +01:00
|
|
|
else f += "*";
|
2010-02-15 06:49:10 +01:00
|
|
|
#if TORRENT_USE_WSTRING
|
|
|
|
#define FindFirstFile_ FindFirstFileW
|
2009-10-26 02:29:39 +01:00
|
|
|
std::wstring p = convert_to_wstring(f);
|
|
|
|
#else
|
2010-02-15 06:49:10 +01:00
|
|
|
#define FindFirstFile_ FindFirstFileA
|
2009-10-26 02:29:39 +01:00
|
|
|
std::string p = convert_to_native(f);
|
|
|
|
#endif
|
2010-02-15 06:49:10 +01:00
|
|
|
m_handle = FindFirstFile_(p.c_str(), &m_fd);
|
2009-10-26 02:29:39 +01:00
|
|
|
if (m_handle == INVALID_HANDLE_VALUE)
|
|
|
|
{
|
|
|
|
ec.assign(GetLastError(), boost::system::get_system_category());
|
|
|
|
m_done = true;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
#else
|
2011-02-28 01:35:58 +01:00
|
|
|
|
|
|
|
memset(&m_dirent, 0, sizeof(dirent));
|
|
|
|
m_name[0] = 0;
|
|
|
|
|
2009-10-26 02:29:39 +01:00
|
|
|
// 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
|
2010-02-15 06:49:10 +01:00
|
|
|
#if TORRENT_USE_WSTRING
|
2009-10-26 02:29:39 +01:00
|
|
|
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
|
2010-02-15 06:49:10 +01:00
|
|
|
#if TORRENT_USE_WSTRING
|
|
|
|
#define FindNextFile_ FindNextFileW
|
|
|
|
#else
|
|
|
|
#define FindNextFile_ FindNextFileA
|
|
|
|
#endif
|
|
|
|
if (FindNextFile_(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)
|
|
|
|
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
|
|
|
|
}
|
2007-06-10 22:46:09 +02:00
|
|
|
|
2008-07-20 13:14:54 +02:00
|
|
|
file::file()
|
2008-07-18 01:41:46 +02:00
|
|
|
#ifdef TORRENT_WINDOWS
|
2008-07-20 13:14:54 +02:00
|
|
|
: m_file_handle(INVALID_HANDLE_VALUE)
|
2005-08-17 03:57:30 +02:00
|
|
|
#else
|
2008-07-20 13:14:54 +02:00
|
|
|
: m_fd(-1)
|
2008-07-18 01:41:46 +02:00
|
|
|
#endif
|
2008-07-20 13:14:54 +02:00
|
|
|
, m_open_mode(0)
|
2009-01-17 09:35:48 +01:00
|
|
|
#if defined TORRENT_WINDOWS || defined TORRENT_LINUX
|
|
|
|
, m_sector_size(0)
|
|
|
|
#endif
|
2008-07-20 13:14:54 +02:00
|
|
|
{}
|
2008-07-18 01:41:46 +02:00
|
|
|
|
2009-10-26 02:29:39 +01:00
|
|
|
file::file(std::string const& path, int mode, error_code& ec)
|
2008-07-18 01:41:46 +02:00
|
|
|
#ifdef TORRENT_WINDOWS
|
2008-07-20 13:14:54 +02:00
|
|
|
: m_file_handle(INVALID_HANDLE_VALUE)
|
|
|
|
#else
|
|
|
|
: m_fd(-1)
|
|
|
|
#endif
|
|
|
|
, m_open_mode(0)
|
|
|
|
{
|
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
|
|
|
|
2009-10-26 02:29:39 +01:00
|
|
|
bool file::open(std::string const& path, int mode, error_code& ec)
|
2008-07-20 13:14:54 +02:00
|
|
|
{
|
|
|
|
close();
|
|
|
|
#ifdef TORRENT_WINDOWS
|
2004-04-03 00:21:20 +02:00
|
|
|
|
2009-01-21 08:31:49 +01:00
|
|
|
struct open_mode_t
|
|
|
|
{
|
|
|
|
DWORD rw_mode;
|
|
|
|
DWORD create_mode;
|
|
|
|
};
|
|
|
|
|
|
|
|
const static open_mode_t mode_array[] =
|
|
|
|
{
|
|
|
|
// 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},
|
2009-01-21 08:31:49 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
const static DWORD attrib_array[] =
|
|
|
|
{
|
|
|
|
FILE_ATTRIBUTE_NORMAL, // no attrib
|
|
|
|
FILE_ATTRIBUTE_HIDDEN, // hidden
|
|
|
|
FILE_ATTRIBUTE_NORMAL, // executable
|
|
|
|
FILE_ATTRIBUTE_HIDDEN, // hidden + executable
|
|
|
|
};
|
|
|
|
|
2011-06-09 08:08:24 +02:00
|
|
|
const static DWORD share_array[] =
|
|
|
|
{
|
|
|
|
// read only (no locking)
|
|
|
|
FILE_SHARE_READ | FILE_SHARE_WRITE,
|
|
|
|
// write only (no locking)
|
|
|
|
FILE_SHARE_READ,
|
|
|
|
// read/write (no locking)
|
|
|
|
FILE_SHARE_READ,
|
|
|
|
};
|
|
|
|
|
2012-04-28 23:13:55 +02:00
|
|
|
std::string p = convert_separators(path);
|
2012-03-03 22:51:15 +01:00
|
|
|
#if TORRENT_USE_UNC_PATHS
|
|
|
|
// UNC paths must be absolute
|
2012-04-28 23:13:55 +02:00
|
|
|
// network paths are already UNC paths
|
2012-03-04 11:54:10 +01:00
|
|
|
if (path.substr(0,2) == "\\\\") p = path;
|
2012-03-17 20:06:11 +01:00
|
|
|
else p = "\\\\?\\" + (is_complete(p) ? p : combine_path(current_working_directory(), p));
|
2006-11-14 01:08:16 +01:00
|
|
|
#endif
|
2004-01-16 03:57:45 +01:00
|
|
|
|
2012-02-23 10:13:18 +01:00
|
|
|
#if TORRENT_USE_WSTRING
|
2012-03-03 22:51:15 +01:00
|
|
|
#define CreateFile_ CreateFileW
|
|
|
|
m_path = convert_to_wstring(p);
|
2012-02-23 10:13:18 +01:00
|
|
|
#else
|
2012-03-03 22:51:15 +01:00
|
|
|
#define CreateFile_ CreateFileA
|
|
|
|
m_path = convert_to_native(p);
|
2012-02-23 07:46:23 +01:00
|
|
|
#endif
|
|
|
|
|
2011-06-17 08:55:40 +02:00
|
|
|
TORRENT_ASSERT((mode & rw_mask) < sizeof(mode_array)/sizeof(mode_array[0]));
|
2011-06-09 08:08:24 +02:00
|
|
|
open_mode_t const& m = mode_array[mode & rw_mask];
|
2009-01-21 08:31:49 +01:00
|
|
|
DWORD a = attrib_array[(mode & attribute_mask) >> 12];
|
|
|
|
|
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
|
|
|
|
|
2011-06-09 08:08:24 +02:00
|
|
|
DWORD flags
|
2012-05-27 20:17:51 +02:00
|
|
|
= ((mode & random_access) ? 0 : FILE_FLAG_SEQUENTIAL_SCAN)
|
2011-06-09 08:08:24 +02:00
|
|
|
| (a ? a : FILE_ATTRIBUTE_NORMAL)
|
|
|
|
| ((mode & no_buffer) ? FILE_FLAG_OVERLAPPED | FILE_FLAG_NO_BUFFERING : 0);
|
2011-04-25 04:15:18 +02:00
|
|
|
|
2011-06-09 08:08:24 +02:00
|
|
|
m_file_handle = CreateFile_(m_path.c_str(), m.rw_mode
|
|
|
|
, (mode & lock_file) ? 0 : share_array[mode & rw_mask]
|
|
|
|
, 0, m.create_mode, flags, 0);
|
2008-07-20 13:14:54 +02:00
|
|
|
|
|
|
|
if (m_file_handle == INVALID_HANDLE_VALUE)
|
2004-01-16 03:57:45 +01:00
|
|
|
{
|
2009-10-26 02:29:39 +01:00
|
|
|
ec.assign(GetLastError(), get_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
|
|
|
|
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
|
|
|
|
if ((mode & file::sparse) && (mode & rw_mask) != read_only)
|
2008-07-20 13:14:54 +02:00
|
|
|
{
|
|
|
|
DWORD temp;
|
|
|
|
::DeviceIoControl(m_file_handle, FSCTL_SET_SPARSE, 0, 0
|
|
|
|
, 0, 0, &temp, 0);
|
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;
|
|
|
|
|
2009-01-11 23:27:43 +01:00
|
|
|
if (mode & attribute_executable)
|
|
|
|
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-01-21 08:31:49 +01:00
|
|
|
#ifdef O_DIRECT
|
|
|
|
static const int no_buffer_flag[] = {0, O_DIRECT};
|
|
|
|
#else
|
|
|
|
static const int no_buffer_flag[] = {0, 0};
|
|
|
|
#endif
|
2009-03-01 01:02:33 +01:00
|
|
|
|
2010-02-02 20:44:52 +01:00
|
|
|
#ifdef O_NOATIME
|
|
|
|
static const int no_atime_flag[] = {0, O_NOATIME};
|
|
|
|
#endif
|
|
|
|
|
2009-10-26 02:29:39 +01:00
|
|
|
m_fd = ::open(convert_to_native(path).c_str()
|
2010-02-02 20:44:52 +01:00
|
|
|
, mode_array[mode & rw_mask]
|
|
|
|
| no_buffer_flag[(mode & no_buffer) >> 2]
|
|
|
|
#ifdef O_NOATIME
|
|
|
|
| no_atime_flag[(mode & no_atime) >> 4]
|
|
|
|
#endif
|
|
|
|
, permissions);
|
2004-01-16 03:57:45 +01:00
|
|
|
|
2009-01-17 10:37:40 +01:00
|
|
|
#ifdef TORRENT_LINUX
|
|
|
|
// workaround for linux bug
|
|
|
|
// https://bugs.launchpad.net/ubuntu/+source/linux/+bug/269946
|
|
|
|
if (m_fd == -1 && (mode & no_buffer) && errno == EINVAL)
|
|
|
|
{
|
|
|
|
mode &= ~no_buffer;
|
2009-10-26 02:29:39 +01:00
|
|
|
m_fd = ::open(path.c_str()
|
2012-07-13 06:04:56 +02:00
|
|
|
, mode_array[mode & rw_mask]
|
|
|
|
#ifdef O_NOATIME
|
|
|
|
| no_atime_flag[(mode & no_atime) >> 4]
|
|
|
|
#endif
|
|
|
|
|
|
|
|
, permissions);
|
2009-01-17 10:37:40 +01:00
|
|
|
}
|
2012-07-13 06:04:56 +02:00
|
|
|
#endif
|
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
|
|
|
|
if (m_fd == -1 && (mode & no_atime) && errno == EPERM)
|
|
|
|
{
|
|
|
|
mode &= ~no_atime;
|
|
|
|
m_fd = ::open(path.c_str()
|
|
|
|
, mode_array[mode & rw_mask]
|
|
|
|
| no_buffer_flag[(mode & no_buffer) >> 2]
|
|
|
|
, permissions);
|
|
|
|
}
|
2009-01-17 10:37:40 +01:00
|
|
|
#endif
|
2008-07-20 13:14:54 +02:00
|
|
|
if (m_fd == -1)
|
2004-01-16 03:57:45 +01:00
|
|
|
{
|
2009-10-26 02:29:39 +01:00
|
|
|
ec.assign(errno, get_posix_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
|
|
|
|
2011-06-09 08:08:24 +02:00
|
|
|
#ifdef F_SETLK
|
|
|
|
if (mode & lock_file)
|
|
|
|
{
|
|
|
|
struct flock l =
|
|
|
|
{
|
|
|
|
0, // start offset
|
|
|
|
0, // length (0 = until EOF)
|
|
|
|
getpid(), // owner
|
|
|
|
(mode & write_only) ? F_WRLCK : F_RDLCK, // lock type
|
|
|
|
SEEK_SET // whence
|
|
|
|
};
|
|
|
|
if (fcntl(m_fd, F_SETLK, &l) != 0)
|
|
|
|
{
|
|
|
|
ec.assign(errno, get_posix_category());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2010-03-21 18:26:21 +01:00
|
|
|
#ifdef DIRECTIO_ON
|
|
|
|
// for solaris
|
|
|
|
if (mode & no_buffer)
|
|
|
|
{
|
|
|
|
int yes = 1;
|
|
|
|
directio(m_fd, DIRECTIO_ON);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2009-01-11 03:02:34 +01:00
|
|
|
#ifdef F_NOCACHE
|
2010-03-21 18:26:21 +01:00
|
|
|
// for BSD/Mac
|
2009-01-11 03:02:34 +01:00
|
|
|
if (mode & no_buffer)
|
|
|
|
{
|
|
|
|
int yes = 1;
|
|
|
|
fcntl(m_fd, F_NOCACHE, &yes);
|
|
|
|
}
|
2008-07-20 13:14:54 +02:00
|
|
|
#endif
|
2009-01-11 03:02:34 +01:00
|
|
|
|
|
|
|
#ifdef POSIX_FADV_RANDOM
|
2011-04-25 04:15:18 +02:00
|
|
|
if (mode & random_access)
|
|
|
|
{
|
|
|
|
// disable read-ahead
|
|
|
|
posix_fadvise(m_fd, 0, 0, POSIX_FADV_RANDOM);
|
|
|
|
}
|
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
|
|
|
|
2008-07-20 13:14:54 +02:00
|
|
|
bool file::is_open() const
|
|
|
|
{
|
|
|
|
#ifdef TORRENT_WINDOWS
|
|
|
|
return m_file_handle != INVALID_HANDLE_VALUE;
|
|
|
|
#else
|
|
|
|
return m_fd != -1;
|
|
|
|
#endif
|
|
|
|
}
|
2006-07-08 21:41:39 +02:00
|
|
|
|
2009-01-17 09:35:48 +01:00
|
|
|
int file::pos_alignment() const
|
|
|
|
{
|
|
|
|
// on linux and windows, file offsets needs
|
|
|
|
// to be aligned to the disk sector size
|
|
|
|
#if defined TORRENT_LINUX
|
|
|
|
if (m_sector_size == 0)
|
|
|
|
{
|
|
|
|
struct statvfs fs;
|
|
|
|
if (fstatvfs(m_fd, &fs) == 0)
|
|
|
|
m_sector_size = fs.f_bsize;
|
|
|
|
else
|
|
|
|
m_sector_size = 4096;
|
|
|
|
}
|
|
|
|
return m_sector_size;
|
|
|
|
#elif defined TORRENT_WINDOWS
|
|
|
|
if (m_sector_size == 0)
|
|
|
|
{
|
|
|
|
DWORD sectors_per_cluster;
|
|
|
|
DWORD bytes_per_sector;
|
|
|
|
DWORD free_clusters;
|
|
|
|
DWORD total_clusters;
|
2010-02-15 06:49:10 +01:00
|
|
|
#if TORRENT_USE_WSTRING
|
|
|
|
#define GetDiskFreeSpace_ GetDiskFreeSpaceW
|
2009-01-17 09:35:48 +01:00
|
|
|
wchar_t backslash = L'\\';
|
|
|
|
#else
|
2010-02-15 06:49:10 +01:00
|
|
|
#define GetDiskFreeSpace_ GetDiskFreeSpaceA
|
2009-01-17 09:35:48 +01:00
|
|
|
char backslash = '\\';
|
|
|
|
#endif
|
2010-02-15 06:49:10 +01:00
|
|
|
if (GetDiskFreeSpace_(m_path.substr(0, m_path.find_first_of(backslash)+1).c_str()
|
2009-01-17 09:35:48 +01:00
|
|
|
, §ors_per_cluster, &bytes_per_sector
|
|
|
|
, &free_clusters, &total_clusters))
|
2009-09-06 09:44:45 +02:00
|
|
|
{
|
2009-01-17 09:35:48 +01:00
|
|
|
m_sector_size = bytes_per_sector;
|
2009-09-06 09:44:45 +02:00
|
|
|
m_cluster_size = sectors_per_cluster * bytes_per_sector;
|
|
|
|
}
|
2009-01-17 09:35:48 +01:00
|
|
|
else
|
2009-09-06 09:44:45 +02:00
|
|
|
{
|
|
|
|
// make a conservative guess
|
|
|
|
m_sector_size = 512;
|
|
|
|
m_cluster_size = 4096;
|
|
|
|
}
|
2009-01-17 09:35:48 +01:00
|
|
|
}
|
|
|
|
return m_sector_size;
|
|
|
|
#else
|
|
|
|
return 1;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2009-01-19 10:21:21 +01:00
|
|
|
int file::buf_alignment() const
|
|
|
|
{
|
|
|
|
#if defined TORRENT_WINDOWS
|
|
|
|
init_file();
|
|
|
|
return m_page_size;
|
|
|
|
#else
|
|
|
|
return pos_alignment();
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2009-01-21 08:31:49 +01:00
|
|
|
int file::size_alignment() const
|
|
|
|
{
|
|
|
|
#if defined TORRENT_WINDOWS
|
|
|
|
init_file();
|
|
|
|
return m_page_size;
|
|
|
|
#else
|
|
|
|
return pos_alignment();
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2008-07-20 13:14:54 +02:00
|
|
|
void file::close()
|
|
|
|
{
|
2009-01-17 09:35:48 +01:00
|
|
|
#if defined TORRENT_WINDOWS || defined TORRENT_LINUX
|
|
|
|
m_sector_size = 0;
|
|
|
|
#endif
|
|
|
|
|
2008-07-20 13:14:54 +02:00
|
|
|
#ifdef TORRENT_WINDOWS
|
|
|
|
if (m_file_handle == INVALID_HANDLE_VALUE) return;
|
|
|
|
CloseHandle(m_file_handle);
|
|
|
|
m_file_handle = INVALID_HANDLE_VALUE;
|
2009-01-11 03:02:34 +01:00
|
|
|
m_path.clear();
|
2006-11-14 01:08:16 +01:00
|
|
|
#else
|
2008-07-20 13:14:54 +02:00
|
|
|
if (m_fd == -1) return;
|
|
|
|
::close(m_fd);
|
|
|
|
m_fd = -1;
|
2006-11-14 01:08:16 +01:00
|
|
|
#endif
|
2008-07-20 13:14:54 +02:00
|
|
|
m_open_mode = 0;
|
2009-01-11 03:02:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// defined in storage.cpp
|
|
|
|
int bufs_size(file::iovec_t const* bufs, int num_bufs);
|
|
|
|
|
2009-01-17 01:09:44 +01:00
|
|
|
#if defined TORRENT_WINDOWS || defined TORRENT_LINUX || defined TORRENT_DEBUG
|
2009-01-11 03:02:34 +01:00
|
|
|
|
|
|
|
int file::m_page_size = 0;
|
|
|
|
|
|
|
|
void file::init_file()
|
|
|
|
{
|
|
|
|
if (m_page_size != 0) return;
|
|
|
|
|
2009-12-09 02:55:30 +01:00
|
|
|
m_page_size = page_size();
|
2008-07-20 13:14:54 +02:00
|
|
|
}
|
2004-01-16 03:57:45 +01:00
|
|
|
|
2009-01-11 03:02:34 +01:00
|
|
|
#endif
|
|
|
|
|
2011-04-26 09:03:05 +02:00
|
|
|
void file::hint_read(size_type file_offset, int len)
|
|
|
|
{
|
|
|
|
#if defined POSIX_FADV_WILLNEED
|
|
|
|
posix_fadvise(m_fd, file_offset, len, POSIX_FADV_WILLNEED);
|
|
|
|
#elif defined F_RDADVISE
|
|
|
|
radvisory r;
|
|
|
|
r.ra_offset = file_offset;
|
|
|
|
r.ra_count = len;
|
|
|
|
fcntl(m_fd, F_RDADVISE, &r);
|
|
|
|
#else
|
|
|
|
// TODO: is there any way to pre-fetch data from a file on windows?
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2009-01-11 03:02:34 +01:00
|
|
|
size_type file::readv(size_type file_offset, iovec_t const* bufs, int num_bufs, error_code& ec)
|
2008-07-20 13:14:54 +02:00
|
|
|
{
|
2013-01-06 19:38:33 +01:00
|
|
|
#ifdef TORRENT_WINDOWS
|
|
|
|
if (m_file_handle == INVALID_HANDLE_VALUE)
|
|
|
|
{
|
|
|
|
ec = error_code(ERROR_INVALID_HANDLE, get_system_category());
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
#else
|
2013-01-07 05:13:45 +01:00
|
|
|
if (m_fd == -1)
|
2013-01-06 19:38:33 +01:00
|
|
|
{
|
|
|
|
ec = error_code(EBADF, get_system_category());
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
#endif
|
2009-01-11 03:02:34 +01:00
|
|
|
TORRENT_ASSERT((m_open_mode & rw_mask) == read_only || (m_open_mode & rw_mask) == read_write);
|
|
|
|
TORRENT_ASSERT(bufs);
|
|
|
|
TORRENT_ASSERT(num_bufs > 0);
|
2008-07-20 13:14:54 +02:00
|
|
|
TORRENT_ASSERT(is_open());
|
|
|
|
|
2009-01-17 01:09:44 +01:00
|
|
|
#if defined TORRENT_WINDOWS || defined TORRENT_LINUX || defined TORRENT_DEBUG
|
2009-01-11 03:02:34 +01:00
|
|
|
// make sure m_page_size is initialized
|
|
|
|
init_file();
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef TORRENT_DEBUG
|
|
|
|
if (m_open_mode & no_buffer)
|
|
|
|
{
|
|
|
|
bool eof = false;
|
|
|
|
int size = 0;
|
2009-01-17 09:35:48 +01:00
|
|
|
// when opened in no_buffer mode, the file_offset must
|
|
|
|
// be aligned to pos_alignment()
|
|
|
|
TORRENT_ASSERT((file_offset & (pos_alignment()-1)) == 0);
|
2009-01-11 03:02:34 +01:00
|
|
|
for (file::iovec_t const* i = bufs, *end(bufs + num_bufs); i < end; ++i)
|
|
|
|
{
|
2009-02-22 21:18:42 +01:00
|
|
|
TORRENT_ASSERT((uintptr_t(i->iov_base) & (buf_alignment()-1)) == 0);
|
2009-01-11 03:02:34 +01:00
|
|
|
// every buffer must be a multiple of the page size
|
|
|
|
// except for the last one
|
2009-01-21 08:31:49 +01:00
|
|
|
TORRENT_ASSERT((i->iov_len & (size_alignment()-1)) == 0 || i == end-1);
|
|
|
|
if ((i->iov_len & (size_alignment()-1)) != 0) eof = true;
|
2009-01-11 03:02:34 +01:00
|
|
|
size += i->iov_len;
|
|
|
|
}
|
|
|
|
error_code code;
|
2011-11-02 10:02:28 +01:00
|
|
|
if (eof)
|
|
|
|
{
|
|
|
|
size_type fsize = get_size(code);
|
|
|
|
if (code) printf("get_size: %s\n", code.message().c_str());
|
|
|
|
if (file_offset + size < fsize)
|
|
|
|
{
|
|
|
|
printf("offset: %d size: %d get_size: %d\n", int(file_offset), int(size), int(fsize));
|
|
|
|
TORRENT_ASSERT(false);
|
|
|
|
}
|
|
|
|
}
|
2009-01-11 03:02:34 +01:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2008-07-20 13:14:54 +02:00
|
|
|
#ifdef TORRENT_WINDOWS
|
2009-01-11 03:02:34 +01:00
|
|
|
|
2008-07-20 13:14:54 +02:00
|
|
|
DWORD ret = 0;
|
2009-01-11 03:02:34 +01:00
|
|
|
|
|
|
|
// since the ReadFileScatter requires the file to be opened
|
|
|
|
// with no buffering, and no buffering requires page aligned
|
|
|
|
// buffers, open the file in non-buffered mode in case the
|
|
|
|
// buffer is not aligned. Most of the times the buffer should
|
|
|
|
// be aligned though
|
|
|
|
|
|
|
|
if ((m_open_mode & no_buffer) == 0)
|
2007-04-19 05:06:15 +02:00
|
|
|
{
|
2009-01-11 03:02:34 +01:00
|
|
|
// this means the buffer base or the buffer size is not aligned
|
|
|
|
// to the page size. Use a regular file for this operation.
|
|
|
|
|
|
|
|
LARGE_INTEGER offs;
|
|
|
|
offs.QuadPart = file_offset;
|
|
|
|
if (SetFilePointerEx(m_file_handle, offs, &offs, FILE_BEGIN) == FALSE)
|
2007-08-11 17:22:06 +02:00
|
|
|
{
|
2009-10-26 02:29:39 +01:00
|
|
|
ec.assign(GetLastError(), get_system_category());
|
2008-07-20 13:14:54 +02:00
|
|
|
return -1;
|
2007-08-11 17:22:06 +02:00
|
|
|
}
|
2009-01-11 03:02:34 +01:00
|
|
|
|
|
|
|
for (file::iovec_t const* i = bufs, *end(bufs + num_bufs); i < end; ++i)
|
|
|
|
{
|
|
|
|
DWORD intermediate = 0;
|
2009-01-13 20:40:14 +01:00
|
|
|
if (ReadFile(m_file_handle, (char*)i->iov_base
|
|
|
|
, (DWORD)i->iov_len, &intermediate, 0) == FALSE)
|
2009-01-11 03:02:34 +01:00
|
|
|
{
|
2009-10-26 02:29:39 +01:00
|
|
|
ec.assign(GetLastError(), get_system_category());
|
2009-01-11 03:02:34 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
ret += intermediate;
|
|
|
|
}
|
|
|
|
return ret;
|
2007-04-19 05:06:15 +02:00
|
|
|
}
|
2004-04-05 00:15:31 +02:00
|
|
|
|
2009-01-11 03:02:34 +01:00
|
|
|
int size = bufs_size(bufs, num_bufs);
|
|
|
|
// number of pages for the read. round up
|
|
|
|
int num_pages = (size + m_page_size - 1) / m_page_size;
|
|
|
|
// allocate array of FILE_SEGMENT_ELEMENT for ReadFileScatter
|
|
|
|
FILE_SEGMENT_ELEMENT* segment_array = TORRENT_ALLOCA(FILE_SEGMENT_ELEMENT, num_pages + 1);
|
|
|
|
FILE_SEGMENT_ELEMENT* cur_seg = segment_array;
|
2009-01-01 02:47:57 +01:00
|
|
|
|
2009-01-11 03:02:34 +01:00
|
|
|
for (file::iovec_t const* i = bufs, *end(bufs + num_bufs); i < end; ++i)
|
2009-01-01 02:47:57 +01:00
|
|
|
{
|
2009-10-26 02:29:39 +01:00
|
|
|
for (int k = 0; k < int(i->iov_len); k += m_page_size)
|
2009-01-11 03:02:34 +01:00
|
|
|
{
|
2010-12-19 09:12:31 +01:00
|
|
|
cur_seg->Buffer = PtrToPtr64((((char*)i->iov_base) + k));
|
2009-01-11 03:02:34 +01:00
|
|
|
++cur_seg;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// terminate the array
|
|
|
|
cur_seg->Buffer = 0;
|
|
|
|
|
|
|
|
OVERLAPPED ol;
|
|
|
|
ol.Internal = 0;
|
|
|
|
ol.InternalHigh = 0;
|
2009-10-26 02:29:39 +01:00
|
|
|
ol.OffsetHigh = DWORD(file_offset >> 32);
|
|
|
|
ol.Offset = DWORD(file_offset & 0xffffffff);
|
2009-01-11 03:02:34 +01:00
|
|
|
ol.hEvent = CreateEvent(0, true, false, 0);
|
2013-01-11 05:49:08 +01:00
|
|
|
if (ol.hEvent == NULL)
|
2013-01-10 04:22:49 +01:00
|
|
|
{
|
2013-01-11 05:49:08 +01:00
|
|
|
ec.assign(GetLastError(), get_system_category());
|
2013-01-10 04:22:49 +01:00
|
|
|
return -1;
|
|
|
|
}
|
2009-01-11 03:02:34 +01:00
|
|
|
|
|
|
|
ret += size;
|
|
|
|
size = num_pages * m_page_size;
|
|
|
|
if (ReadFileScatter(m_file_handle, segment_array, size, 0, &ol) == 0)
|
|
|
|
{
|
|
|
|
DWORD last_error = GetLastError();
|
2013-01-10 04:22:49 +01:00
|
|
|
if (last_error != ERROR_IO_PENDING
|
2013-01-11 05:49:08 +01:00
|
|
|
#ifndef TORRENT_MINGW
|
|
|
|
&& last_error != ERROR_CANT_WAIT
|
|
|
|
#endif
|
|
|
|
)
|
2009-01-01 02:47:57 +01:00
|
|
|
{
|
2013-01-10 04:22:49 +01:00
|
|
|
ec.assign(last_error, get_system_category());
|
2009-01-11 03:02:34 +01:00
|
|
|
CloseHandle(ol.hEvent);
|
|
|
|
return -1;
|
|
|
|
}
|
2011-11-13 05:12:56 +01:00
|
|
|
DWORD num_read;
|
|
|
|
if (GetOverlappedResult(m_file_handle, &ol, &num_read, true) == 0)
|
2009-01-11 03:02:34 +01:00
|
|
|
{
|
2013-01-10 04:22:49 +01:00
|
|
|
DWORD last_error = GetLastError();
|
|
|
|
if (last_error != ERROR_HANDLE_EOF)
|
2011-06-17 17:10:40 +02:00
|
|
|
{
|
2013-01-11 05:49:08 +01:00
|
|
|
#ifndef TORRENT_MINGW
|
2013-01-10 04:22:49 +01:00
|
|
|
TORRENT_ASSERT(last_error != ERROR_CANT_WAIT);
|
2013-01-11 05:49:08 +01:00
|
|
|
#endif
|
2013-01-10 04:22:49 +01:00
|
|
|
ec.assign(last_error, get_system_category());
|
2011-06-17 17:10:40 +02:00
|
|
|
CloseHandle(ol.hEvent);
|
|
|
|
return -1;
|
|
|
|
}
|
2009-01-01 02:47:57 +01:00
|
|
|
}
|
2011-11-13 05:12:56 +01:00
|
|
|
if (num_read < ret) ret = num_read;
|
2009-01-01 02:47:57 +01:00
|
|
|
}
|
2009-01-17 19:25:27 +01:00
|
|
|
CloseHandle(ol.hEvent);
|
2009-01-11 03:02:34 +01:00
|
|
|
return ret;
|
2009-04-04 10:23:53 +02:00
|
|
|
|
|
|
|
#else // TORRENT_WINDOWS
|
|
|
|
|
2009-01-11 03:02:34 +01:00
|
|
|
size_type ret = lseek(m_fd, file_offset, SEEK_SET);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2009-10-26 02:29:39 +01:00
|
|
|
ec.assign(errno, get_posix_category());
|
2009-01-11 03:02:34 +01:00
|
|
|
return -1;
|
|
|
|
}
|
2009-04-04 10:23:53 +02:00
|
|
|
#if TORRENT_USE_READV
|
|
|
|
|
2011-09-05 07:47:50 +02:00
|
|
|
ret = 0;
|
|
|
|
while (num_bufs > 0)
|
2009-01-11 03:02:34 +01:00
|
|
|
{
|
2011-09-05 07:47:50 +02:00
|
|
|
int nbufs = (std::min)(num_bufs, TORRENT_IOV_MAX);
|
|
|
|
int tmp_ret = 0;
|
|
|
|
#ifdef TORRENT_LINUX
|
|
|
|
bool aligned = false;
|
|
|
|
int size = 0;
|
|
|
|
// if we're not opened in no-buffer mode, we don't need alignment
|
|
|
|
if ((m_open_mode & no_buffer) == 0) aligned = true;
|
|
|
|
if (!aligned)
|
|
|
|
{
|
|
|
|
size = bufs_size(bufs, nbufs);
|
|
|
|
if ((size & (size_alignment()-1)) == 0) aligned = true;
|
|
|
|
}
|
|
|
|
if (aligned)
|
2009-04-04 10:23:53 +02:00
|
|
|
#endif // TORRENT_LINUX
|
2009-01-11 03:02:34 +01:00
|
|
|
{
|
2011-09-05 07:47:50 +02:00
|
|
|
tmp_ret = ::readv(m_fd, bufs, nbufs);
|
|
|
|
if (tmp_ret < 0)
|
|
|
|
{
|
|
|
|
ec.assign(errno, get_posix_category());
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
ret += tmp_ret;
|
2009-01-11 03:02:34 +01:00
|
|
|
}
|
|
|
|
#ifdef TORRENT_LINUX
|
2011-09-05 07:47:50 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
file::iovec_t* temp_bufs = TORRENT_ALLOCA(file::iovec_t, nbufs);
|
|
|
|
memcpy(temp_bufs, bufs, sizeof(file::iovec_t) * nbufs);
|
|
|
|
iovec_t& last = temp_bufs[nbufs-1];
|
|
|
|
last.iov_len = (last.iov_len & ~(size_alignment()-1)) + m_page_size;
|
|
|
|
tmp_ret = ::readv(m_fd, temp_bufs, nbufs);
|
|
|
|
if (tmp_ret < 0)
|
|
|
|
{
|
|
|
|
ec.assign(errno, get_posix_category());
|
|
|
|
return -1;
|
|
|
|
}
|
2011-09-13 09:02:14 +02:00
|
|
|
ret += (std::min)(tmp_ret, size);
|
2011-09-05 07:47:50 +02:00
|
|
|
}
|
2009-04-04 10:23:53 +02:00
|
|
|
#endif // TORRENT_LINUX
|
|
|
|
|
2011-09-05 07:47:50 +02:00
|
|
|
num_bufs -= nbufs;
|
|
|
|
bufs += nbufs;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
|
2009-04-04 10:23:53 +02:00
|
|
|
#else // TORRENT_USE_READV
|
|
|
|
|
|
|
|
ret = 0;
|
|
|
|
for (file::iovec_t const* i = bufs, *end(bufs + num_bufs); i < end; ++i)
|
|
|
|
{
|
|
|
|
int tmp = read(m_fd, i->iov_base, i->iov_len);
|
|
|
|
if (tmp < 0)
|
|
|
|
{
|
2009-10-26 02:29:39 +01:00
|
|
|
ec.assign(errno, get_posix_category());
|
2009-04-04 10:23:53 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
ret += tmp;
|
|
|
|
if (tmp < i->iov_len) break;
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
#endif // TORRENT_USE_READV
|
|
|
|
|
|
|
|
#endif // TORRENT_WINDOWS
|
2009-01-01 02:47:57 +01:00
|
|
|
}
|
|
|
|
|
2009-01-11 03:02:34 +01:00
|
|
|
size_type file::writev(size_type file_offset, iovec_t const* bufs, int num_bufs, error_code& ec)
|
2009-01-01 02:47:57 +01:00
|
|
|
{
|
2013-01-06 19:38:33 +01:00
|
|
|
#ifdef TORRENT_WINDOWS
|
|
|
|
if (m_file_handle == INVALID_HANDLE_VALUE)
|
|
|
|
{
|
|
|
|
ec = error_code(ERROR_INVALID_HANDLE, get_system_category());
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
#else
|
2013-01-07 05:13:45 +01:00
|
|
|
if (m_fd == -1)
|
2013-01-06 19:38:33 +01:00
|
|
|
{
|
|
|
|
ec = error_code(EBADF, get_system_category());
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
#endif
|
2009-01-11 03:02:34 +01:00
|
|
|
TORRENT_ASSERT((m_open_mode & rw_mask) == write_only || (m_open_mode & rw_mask) == read_write);
|
2009-01-01 02:47:57 +01:00
|
|
|
TORRENT_ASSERT(bufs);
|
2009-01-11 03:02:34 +01:00
|
|
|
TORRENT_ASSERT(num_bufs > 0);
|
2009-01-01 02:47:57 +01:00
|
|
|
TORRENT_ASSERT(is_open());
|
|
|
|
|
2009-01-17 01:09:44 +01:00
|
|
|
#if defined TORRENT_WINDOWS || defined TORRENT_LINUX || defined TORRENT_DEBUG
|
2009-01-11 03:02:34 +01:00
|
|
|
// make sure m_page_size is initialized
|
|
|
|
init_file();
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef TORRENT_DEBUG
|
|
|
|
if (m_open_mode & no_buffer)
|
|
|
|
{
|
|
|
|
bool eof = false;
|
|
|
|
int size = 0;
|
2009-01-17 09:35:48 +01:00
|
|
|
// when opened in no_buffer mode, the file_offset must
|
|
|
|
// be aligned to pos_alignment()
|
|
|
|
TORRENT_ASSERT((file_offset & (pos_alignment()-1)) == 0);
|
2009-01-11 03:02:34 +01:00
|
|
|
for (file::iovec_t const* i = bufs, *end(bufs + num_bufs); i < end; ++i)
|
|
|
|
{
|
2009-02-22 21:18:42 +01:00
|
|
|
TORRENT_ASSERT((uintptr_t(i->iov_base) & (buf_alignment()-1)) == 0);
|
2009-01-11 03:02:34 +01:00
|
|
|
// every buffer must be a multiple of the page size
|
|
|
|
// except for the last one
|
2009-01-21 08:31:49 +01:00
|
|
|
TORRENT_ASSERT((i->iov_len & (size_alignment()-1)) == 0 || i == end-1);
|
|
|
|
if ((i->iov_len & (size_alignment()-1)) != 0) eof = true;
|
2009-01-11 03:02:34 +01:00
|
|
|
size += i->iov_len;
|
|
|
|
}
|
|
|
|
error_code code;
|
|
|
|
if (eof) TORRENT_ASSERT(file_offset + size >= get_size(code));
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2009-01-01 02:47:57 +01:00
|
|
|
#ifdef TORRENT_WINDOWS
|
2009-01-11 03:02:34 +01:00
|
|
|
|
|
|
|
DWORD ret = 0;
|
|
|
|
|
|
|
|
// since the ReadFileScatter requires the file to be opened
|
|
|
|
// with no buffering, and no buffering requires page aligned
|
|
|
|
// buffers, open the file in non-buffered mode in case the
|
|
|
|
// buffer is not aligned. Most of the times the buffer should
|
|
|
|
// be aligned though
|
|
|
|
|
|
|
|
if ((m_open_mode & no_buffer) == 0)
|
2009-01-01 02:47:57 +01:00
|
|
|
{
|
2009-01-11 03:02:34 +01:00
|
|
|
// this means the buffer base or the buffer size is not aligned
|
|
|
|
// to the page size. Use a regular file for this operation.
|
|
|
|
|
|
|
|
LARGE_INTEGER offs;
|
|
|
|
offs.QuadPart = file_offset;
|
2009-03-31 10:20:34 +02:00
|
|
|
if (SetFilePointerEx(m_file_handle, offs, &offs, FILE_BEGIN) == FALSE)
|
2009-01-01 02:47:57 +01:00
|
|
|
{
|
2009-10-26 02:29:39 +01:00
|
|
|
ec.assign(GetLastError(), get_system_category());
|
2009-01-01 02:47:57 +01:00
|
|
|
return -1;
|
|
|
|
}
|
2009-01-11 03:02:34 +01:00
|
|
|
|
|
|
|
for (file::iovec_t const* i = bufs, *end(bufs + num_bufs); i < end; ++i)
|
|
|
|
{
|
|
|
|
DWORD intermediate = 0;
|
2009-01-13 20:40:14 +01:00
|
|
|
if (WriteFile(m_file_handle, (char const*)i->iov_base
|
|
|
|
, (DWORD)i->iov_len, &intermediate, 0) == FALSE)
|
2009-01-11 03:02:34 +01:00
|
|
|
{
|
2009-10-26 02:29:39 +01:00
|
|
|
ec.assign(GetLastError(), get_system_category());
|
2009-01-11 03:02:34 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
ret += intermediate;
|
|
|
|
}
|
|
|
|
return ret;
|
2009-01-01 02:47:57 +01:00
|
|
|
}
|
|
|
|
|
2009-01-11 03:02:34 +01:00
|
|
|
int size = bufs_size(bufs, num_bufs);
|
|
|
|
// number of pages for the write. round up
|
|
|
|
int num_pages = (size + m_page_size - 1) / m_page_size;
|
|
|
|
// allocate array of FILE_SEGMENT_ELEMENT for WriteFileGather
|
|
|
|
FILE_SEGMENT_ELEMENT* segment_array = TORRENT_ALLOCA(FILE_SEGMENT_ELEMENT, num_pages + 1);
|
|
|
|
FILE_SEGMENT_ELEMENT* cur_seg = segment_array;
|
2004-01-16 03:57:45 +01:00
|
|
|
|
2009-01-11 03:02:34 +01:00
|
|
|
for (file::iovec_t const* i = bufs, *end(bufs + num_bufs); i < end; ++i)
|
|
|
|
{
|
2009-10-26 02:29:39 +01:00
|
|
|
for (int k = 0; k < int(i->iov_len); k += m_page_size)
|
2009-01-11 03:02:34 +01:00
|
|
|
{
|
2010-12-19 09:12:31 +01:00
|
|
|
cur_seg->Buffer = PtrToPtr64((((char*)i->iov_base) + k));
|
2009-01-11 03:02:34 +01:00
|
|
|
++cur_seg;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// terminate the array
|
|
|
|
cur_seg->Buffer = 0;
|
|
|
|
|
|
|
|
OVERLAPPED ol;
|
|
|
|
ol.Internal = 0;
|
|
|
|
ol.InternalHigh = 0;
|
2009-10-26 02:29:39 +01:00
|
|
|
ol.OffsetHigh = DWORD(file_offset >> 32);
|
|
|
|
ol.Offset = DWORD(file_offset & 0xffffffff);
|
2009-01-11 03:02:34 +01:00
|
|
|
ol.hEvent = CreateEvent(0, true, false, 0);
|
2013-01-11 05:49:08 +01:00
|
|
|
if (ol.hEvent == NULL)
|
2013-01-10 04:22:49 +01:00
|
|
|
{
|
2013-01-11 05:49:08 +01:00
|
|
|
ec.assign(GetLastError(), get_system_category());
|
2013-01-10 04:22:49 +01:00
|
|
|
return -1;
|
|
|
|
}
|
2009-01-11 03:02:34 +01:00
|
|
|
|
|
|
|
ret += size;
|
|
|
|
size_type file_size = 0;
|
|
|
|
|
2009-01-11 10:59:05 +01:00
|
|
|
if ((size & (m_page_size-1)) != 0)
|
2009-01-11 03:02:34 +01:00
|
|
|
{
|
|
|
|
// if size is not an even multiple, this must be the tail
|
2011-11-13 05:12:56 +01:00
|
|
|
// of the file.
|
2009-01-11 03:02:34 +01:00
|
|
|
|
|
|
|
file_size = file_offset + size;
|
|
|
|
size = num_pages * m_page_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (WriteFileGather(m_file_handle, segment_array, size, 0, &ol) == 0)
|
2008-07-20 13:14:54 +02:00
|
|
|
{
|
2013-01-10 04:22:49 +01:00
|
|
|
DWORD last_error = GetLastError();
|
|
|
|
if (last_error != ERROR_IO_PENDING
|
2013-01-11 05:49:08 +01:00
|
|
|
#ifndef TORRENT_MINGW
|
|
|
|
&& last_error != ERROR_CANT_WAIT
|
|
|
|
#endif
|
|
|
|
)
|
2008-07-20 13:14:54 +02:00
|
|
|
{
|
2013-01-10 04:22:49 +01:00
|
|
|
TORRENT_ASSERT(last_error != ERROR_BAD_ARGUMENTS);
|
|
|
|
ec.assign(last_error, get_system_category());
|
2009-01-11 03:02:34 +01:00
|
|
|
CloseHandle(ol.hEvent);
|
2008-07-20 13:14:54 +02:00
|
|
|
return -1;
|
|
|
|
}
|
2011-11-13 05:12:56 +01:00
|
|
|
DWORD num_written;
|
|
|
|
if (GetOverlappedResult(m_file_handle, &ol, &num_written, true) == 0)
|
2009-01-11 03:02:34 +01:00
|
|
|
{
|
2013-01-10 04:22:49 +01:00
|
|
|
DWORD last_error = GetLastError();
|
2013-01-11 05:49:08 +01:00
|
|
|
#ifndef TORRENT_MINGW
|
2013-01-10 04:22:49 +01:00
|
|
|
TORRENT_ASSERT(last_error != ERROR_CANT_WAIT);
|
2013-01-11 05:49:08 +01:00
|
|
|
#endif
|
2013-01-10 04:22:49 +01:00
|
|
|
ec.assign(last_error, get_system_category());
|
2009-01-11 03:02:34 +01:00
|
|
|
CloseHandle(ol.hEvent);
|
|
|
|
return -1;
|
|
|
|
}
|
2011-11-13 05:12:56 +01:00
|
|
|
if (num_written < ret) ret = num_written;
|
2009-01-11 03:02:34 +01:00
|
|
|
}
|
|
|
|
CloseHandle(ol.hEvent);
|
2011-11-13 05:12:56 +01:00
|
|
|
if (file_size > 0) set_size(file_size, ec);
|
2009-01-11 03:02:34 +01:00
|
|
|
return ret;
|
2004-02-19 17:47:12 +01:00
|
|
|
#else
|
2009-01-11 03:02:34 +01:00
|
|
|
size_type ret = lseek(m_fd, file_offset, SEEK_SET);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2009-10-26 02:29:39 +01:00
|
|
|
ec.assign(errno, get_posix_category());
|
2009-01-11 03:02:34 +01:00
|
|
|
return -1;
|
|
|
|
}
|
2009-04-04 10:23:53 +02:00
|
|
|
|
|
|
|
#if TORRENT_USE_WRITEV
|
|
|
|
|
2011-09-05 07:47:50 +02:00
|
|
|
ret = 0;
|
|
|
|
while (num_bufs > 0)
|
2009-01-11 03:02:34 +01:00
|
|
|
{
|
2011-09-05 07:47:50 +02:00
|
|
|
int nbufs = (std::min)(num_bufs, TORRENT_IOV_MAX);
|
|
|
|
int tmp_ret = 0;
|
|
|
|
#ifdef TORRENT_LINUX
|
|
|
|
bool aligned = false;
|
|
|
|
int size = 0;
|
|
|
|
// if we're not opened in no-buffer mode, we don't need alignment
|
|
|
|
if ((m_open_mode & no_buffer) == 0) aligned = true;
|
|
|
|
if (!aligned)
|
|
|
|
{
|
|
|
|
size = bufs_size(bufs, nbufs);
|
|
|
|
if ((size & (size_alignment()-1)) == 0) aligned = true;
|
|
|
|
}
|
|
|
|
if (aligned)
|
2009-01-11 03:02:34 +01:00
|
|
|
#endif
|
|
|
|
{
|
2011-09-05 07:47:50 +02:00
|
|
|
tmp_ret = ::writev(m_fd, bufs, nbufs);
|
|
|
|
if (tmp_ret < 0)
|
|
|
|
{
|
|
|
|
ec.assign(errno, get_posix_category());
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
ret += tmp_ret;
|
2009-01-11 03:02:34 +01:00
|
|
|
}
|
|
|
|
#ifdef TORRENT_LINUX
|
2011-09-05 07:47:50 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
file::iovec_t* temp_bufs = TORRENT_ALLOCA(file::iovec_t, nbufs);
|
|
|
|
memcpy(temp_bufs, bufs, sizeof(file::iovec_t) * nbufs);
|
|
|
|
iovec_t& last = temp_bufs[nbufs-1];
|
|
|
|
last.iov_len = (last.iov_len & ~(size_alignment()-1)) + size_alignment();
|
|
|
|
tmp_ret = ::writev(m_fd, temp_bufs, nbufs);
|
|
|
|
if (tmp_ret < 0)
|
|
|
|
{
|
|
|
|
ec.assign(errno, get_posix_category());
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (ftruncate(m_fd, file_offset + size) < 0)
|
|
|
|
{
|
|
|
|
ec.assign(errno, get_posix_category());
|
|
|
|
return -1;
|
|
|
|
}
|
2011-09-13 09:02:14 +02:00
|
|
|
ret += (std::min)(tmp_ret, size);
|
2011-09-05 07:47:50 +02:00
|
|
|
}
|
2009-04-04 10:23:53 +02:00
|
|
|
#endif // TORRENT_LINUX
|
|
|
|
|
2011-09-05 07:47:50 +02:00
|
|
|
num_bufs -= nbufs;
|
|
|
|
bufs += nbufs;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
|
2009-04-04 10:23:53 +02:00
|
|
|
#else // TORRENT_USE_WRITEV
|
|
|
|
|
|
|
|
ret = 0;
|
|
|
|
for (file::iovec_t const* i = bufs, *end(bufs + num_bufs); i < end; ++i)
|
|
|
|
{
|
|
|
|
int tmp = write(m_fd, i->iov_base, i->iov_len);
|
|
|
|
if (tmp < 0)
|
|
|
|
{
|
2009-10-26 02:29:39 +01:00
|
|
|
ec.assign(errno, get_posix_category());
|
2009-04-04 10:23:53 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
ret += tmp;
|
|
|
|
if (tmp < i->iov_len) break;
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
#endif // TORRENT_USE_WRITEV
|
|
|
|
|
|
|
|
#endif // TORRENT_WINDOWS
|
2008-07-20 13:14:54 +02:00
|
|
|
}
|
2004-01-16 03:57:45 +01:00
|
|
|
|
2009-09-05 09:21:10 +02:00
|
|
|
size_type file::phys_offset(size_type offset)
|
|
|
|
{
|
2010-04-11 02:48:48 +02:00
|
|
|
#ifdef FIEMAP_EXTENT_UNKNOWN
|
2009-09-05 09:21:10 +02:00
|
|
|
// for documentation of this feature
|
|
|
|
// http://lwn.net/Articles/297696/
|
|
|
|
struct
|
|
|
|
{
|
|
|
|
struct fiemap fiemap;
|
|
|
|
struct fiemap_extent extent;
|
|
|
|
} fm;
|
|
|
|
|
|
|
|
memset(&fm, 0, sizeof(fm));
|
|
|
|
fm.fiemap.fm_start = offset;
|
|
|
|
fm.fiemap.fm_length = size_alignment();
|
|
|
|
// this sounds expensive
|
|
|
|
fm.fiemap.fm_flags = FIEMAP_FLAG_SYNC;
|
|
|
|
fm.fiemap.fm_extent_count = 1;
|
|
|
|
|
|
|
|
if (ioctl(m_fd, FS_IOC_FIEMAP, &fm) == -1)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (fm.fiemap.fm_extents[0].fe_flags & FIEMAP_EXTENT_UNKNOWN)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
// the returned extent is not guaranteed to start
|
|
|
|
// at the requested offset, adjust for that in
|
|
|
|
// case they differ
|
2009-09-06 09:44:45 +02:00
|
|
|
TORRENT_ASSERT(offset >= fm.fiemap.fm_extents[0].fe_logical);
|
2009-09-05 09:21:10 +02:00
|
|
|
return fm.fiemap.fm_extents[0].fe_physical + (offset - fm.fiemap.fm_extents[0].fe_logical);
|
|
|
|
|
|
|
|
#elif defined F_LOG2PHYS
|
|
|
|
// for documentation of this feature
|
|
|
|
// http://developer.apple.com/mac/library/documentation/Darwin/Reference/ManPages/man2/fcntl.2.html
|
|
|
|
|
|
|
|
log2phys l;
|
|
|
|
size_type ret = lseek(m_fd, offset, SEEK_SET);
|
|
|
|
if (ret < 0) return 0;
|
2009-09-06 09:44:45 +02:00
|
|
|
if (fcntl(m_fd, F_LOG2PHYS, &l) == -1) return 0;
|
|
|
|
return l.l2p_devoffset;
|
|
|
|
#elif defined TORRENT_WINDOWS
|
|
|
|
// for documentation of this feature
|
|
|
|
// http://msdn.microsoft.com/en-us/library/aa364572(VS.85).aspx
|
|
|
|
STARTING_VCN_INPUT_BUFFER in;
|
|
|
|
RETRIEVAL_POINTERS_BUFFER out;
|
|
|
|
DWORD out_bytes;
|
|
|
|
|
|
|
|
// query cluster size
|
|
|
|
pos_alignment();
|
|
|
|
in.StartingVcn.QuadPart = offset / m_cluster_size;
|
2009-10-26 02:29:39 +01:00
|
|
|
int cluster_offset = int(in.StartingVcn.QuadPart % m_cluster_size);
|
2009-09-06 09:44:45 +02:00
|
|
|
|
|
|
|
if (DeviceIoControl(m_file_handle, FSCTL_GET_RETRIEVAL_POINTERS, &in
|
|
|
|
, sizeof(in), &out, sizeof(out), &out_bytes, 0) == 0)
|
2009-09-07 07:31:29 +02:00
|
|
|
{
|
|
|
|
DWORD error = GetLastError();
|
|
|
|
TORRENT_ASSERT(error != ERROR_INVALID_PARAMETER);
|
|
|
|
|
|
|
|
// insufficient buffer error is expected, but we're
|
|
|
|
// only interested in the first extent anyway
|
|
|
|
if (error != ERROR_MORE_DATA) return 0;
|
|
|
|
}
|
2009-09-06 09:44:45 +02:00
|
|
|
if (out_bytes < sizeof(out)) return 0;
|
|
|
|
if (out.ExtentCount == 0) return 0;
|
|
|
|
if (out.Extents[0].Lcn.QuadPart == (LONGLONG)-1) return 0;
|
|
|
|
TORRENT_ASSERT(in.StartingVcn.QuadPart >= out.StartingVcn.QuadPart);
|
|
|
|
return (out.Extents[0].Lcn.QuadPart
|
|
|
|
+ (in.StartingVcn.QuadPart - out.StartingVcn.QuadPart))
|
|
|
|
* m_cluster_size + cluster_offset;
|
2009-09-05 09:21:10 +02:00
|
|
|
#endif
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-07-20 13:14:54 +02:00
|
|
|
bool file::set_size(size_type s, error_code& ec)
|
|
|
|
{
|
|
|
|
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
|
|
|
|
|
|
|
if ((m_open_mode & no_buffer) && (s & (size_alignment()-1)) != 0)
|
|
|
|
{
|
|
|
|
// the file is opened in unbuffered mode, and the size is not
|
|
|
|
// aligned to the required cluster size. Use NtSetInformationFile
|
|
|
|
|
|
|
|
#define FileEndOfFileInformation 20
|
|
|
|
#ifndef NT_SUCCESS
|
|
|
|
#define NT_SUCCESS(x) (!((x) & 0x80000000))
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// for NtSetInformationFile, see:
|
|
|
|
// http://undocumented.ntinternals.net/UserMode/Undocumented%20Functions/NT%20Objects/File/NtSetInformationFile.html
|
|
|
|
|
|
|
|
typedef DWORD _NTSTATUS;
|
|
|
|
typedef _NTSTATUS (NTAPI * NtSetInformationFile_t)(HANDLE file, PULONG_PTR iosb, PVOID data, ULONG len, ULONG file_info_class);
|
|
|
|
|
|
|
|
static NtSetInformationFile_t NtSetInformationFile = 0;
|
|
|
|
static bool failed_ntdll = false;
|
|
|
|
|
|
|
|
if (NtSetInformationFile == 0 && !failed_ntdll)
|
|
|
|
{
|
|
|
|
HMODULE nt = LoadLibraryA("ntdll");
|
|
|
|
if (nt)
|
|
|
|
{
|
|
|
|
NtSetInformationFile = (NtSetInformationFile_t)GetProcAddress(nt, "NtSetInformationFile");
|
|
|
|
if (NtSetInformationFile == 0) failed_ntdll = true;
|
|
|
|
}
|
|
|
|
else failed_ntdll = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!failed_ntdll && NtSetInformationFile)
|
|
|
|
{
|
|
|
|
ULONG_PTR Iosb[2];
|
|
|
|
LARGE_INTEGER fsize;
|
|
|
|
fsize.QuadPart = s;
|
|
|
|
_NTSTATUS st = NtSetInformationFile(m_file_handle
|
|
|
|
, Iosb, &fsize, sizeof(fsize), FileEndOfFileInformation);
|
|
|
|
if (!NT_SUCCESS(st))
|
|
|
|
{
|
|
|
|
ec.assign(INVALID_SET_FILE_POINTER, get_system_category());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2012-02-22 10:29:07 +01:00
|
|
|
|
|
|
|
// couldn't find ntdll or NtSetFileInformation function
|
|
|
|
// and the file is opened in unbuffered mode! There's
|
|
|
|
// nothing we can do! (short of re-opening the file, but
|
|
|
|
// that introduces all sorts of nasty race conditions)
|
|
|
|
return false;
|
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;
|
2010-03-06 23:47:25 +01:00
|
|
|
if (GetFileSizeEx(m_file_handle, &cur_size) == FALSE)
|
2009-01-11 03:02:34 +01:00
|
|
|
{
|
2009-10-26 02:29:39 +01:00
|
|
|
ec.assign(GetLastError(), get_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)
|
|
|
|
{
|
|
|
|
if (SetFilePointerEx(m_file_handle, offs, &offs, FILE_BEGIN) == FALSE)
|
|
|
|
{
|
|
|
|
ec.assign(GetLastError(), get_system_category());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (::SetEndOfFile(m_file_handle) == FALSE)
|
|
|
|
{
|
|
|
|
ec.assign(GetLastError(), get_system_category());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2013-01-07 02:56:40 +01:00
|
|
|
|
2009-02-16 12:04:54 +01:00
|
|
|
if ((m_open_mode & sparse) == 0)
|
2009-02-14 05:18:17 +01:00
|
|
|
{
|
2013-01-07 02:56:40 +01:00
|
|
|
typedef DWORD (WINAPI *GetCompressedFileSizeW_t)(LPCWSTR lpFileName, LPDWORD lpFileSizeHigh);
|
|
|
|
typedef BOOL (WINAPI *SetFileValidData_t)(HANDLE hFile, LONGLONG ValidDataLength);
|
|
|
|
|
|
|
|
static GetCompressedFileSizeW_t GetCompressedFileSizeW = NULL;
|
|
|
|
static SetFileValidData_t SetFileValidData = NULL;
|
|
|
|
|
|
|
|
static bool failed_kernel32 = false;
|
|
|
|
|
|
|
|
if ((GetCompressedFileSizeW == NULL) && !failed_kernel32)
|
2010-03-20 09:18:49 +01:00
|
|
|
{
|
2013-01-07 02:56:40 +01:00
|
|
|
HMODULE kernel32 = LoadLibraryA("kernel32.dll");
|
|
|
|
if (kernel32)
|
|
|
|
{
|
|
|
|
GetCompressedFileSizeW = (GetCompressedFileSizeW_t)GetProcAddress(kernel32, "GetCompressedFileSizeW");
|
|
|
|
SetFileValidData = (SetFileValidData_t)GetProcAddress(kernel32, "SetFileValidData");
|
|
|
|
if ((GetCompressedFileSizeW == NULL) || (SetFileValidData == NULL))
|
|
|
|
{
|
|
|
|
failed_kernel32 = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
failed_kernel32 = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!failed_kernel32 && GetCompressedFileSizeW && SetFileValidData)
|
|
|
|
{
|
|
|
|
// only allocate the space if the file
|
|
|
|
// is not fully allocated
|
|
|
|
DWORD high_dword = 0;
|
|
|
|
offs.LowPart = GetCompressedFileSize(m_path.c_str(), &high_dword);
|
|
|
|
offs.HighPart = high_dword;
|
|
|
|
ec.assign(GetLastError(), get_system_category());
|
|
|
|
if (ec) return false;
|
|
|
|
if (offs.QuadPart != s)
|
|
|
|
{
|
|
|
|
// if the user has permissions, avoid filling
|
|
|
|
// the file with zeroes, but just fill it with
|
|
|
|
// garbage instead
|
|
|
|
SetFileValidData(m_file_handle, offs.QuadPart);
|
|
|
|
}
|
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
|
2010-03-06 21:09:18 +01:00
|
|
|
struct stat st;
|
|
|
|
if (fstat(m_fd, &st) != 0)
|
2008-07-20 13:14:54 +02:00
|
|
|
{
|
2009-10-26 02:29:39 +01:00
|
|
|
ec.assign(errno, get_posix_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
|
|
|
|
if (st.st_size != s && ftruncate(m_fd, s) < 0)
|
|
|
|
{
|
|
|
|
ec.assign(errno, get_posix_category());
|
|
|
|
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.
|
|
|
|
if ((m_open_mode & sparse) == 0
|
|
|
|
&& 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};
|
|
|
|
if (fcntl(m_fd, F_PREALLOCATE, &f) < 0)
|
|
|
|
{
|
2011-04-13 08:54:56 +02:00
|
|
|
if (errno != ENOSPC)
|
|
|
|
{
|
|
|
|
ec.assign(errno, get_posix_category());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// ok, let's try to allocate non contiguous space then
|
|
|
|
fstore_t f = {F_ALLOCATEALL, F_PEOFPOSMODE, 0, s, 0};
|
|
|
|
if (fcntl(m_fd, F_PREALLOCATE, &f) < 0)
|
|
|
|
{
|
|
|
|
ec.assign(errno, get_posix_category());
|
|
|
|
return false;
|
|
|
|
}
|
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)
|
|
|
|
{
|
|
|
|
ec.assign(errno, get_posix_category());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // F_ALLOCSP64
|
|
|
|
|
2011-02-21 06:24:41 +01:00
|
|
|
#if defined TORRENT_LINUX || TORRENT_HAS_FALLOCATE
|
2010-05-03 00:26:43 +02:00
|
|
|
int ret;
|
2011-02-15 11:05:25 +01:00
|
|
|
#endif
|
|
|
|
|
2010-04-08 07:09:11 +02:00
|
|
|
#if defined TORRENT_LINUX
|
2010-05-03 00:26:43 +02:00
|
|
|
ret = my_fallocate(m_fd, 0, 0, s);
|
2010-03-06 21:09:18 +01:00
|
|
|
// if we return 0, everything went fine
|
|
|
|
// the fallocate call succeeded
|
|
|
|
if (ret == 0) return true;
|
|
|
|
// otherwise, something went wrong. If the error
|
|
|
|
// is ENOSYS, just keep going and do it the old-fashioned
|
|
|
|
// way. If fallocate failed with some other error, it
|
|
|
|
// probably means the user should know about it, error out
|
|
|
|
// and report it.
|
2010-10-18 09:15:57 +02:00
|
|
|
if (errno != ENOSYS && errno != EOPNOTSUPP)
|
2010-01-18 04:08:38 +01:00
|
|
|
{
|
2010-10-14 02:58:08 +02:00
|
|
|
ec.assign(errno, get_posix_category());
|
2010-01-18 04:08:38 +01:00
|
|
|
return false;
|
|
|
|
}
|
2010-04-08 07:09:11 +02:00
|
|
|
#endif // TORRENT_LINUX
|
|
|
|
|
|
|
|
#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.
|
2010-01-18 04:08:38 +01:00
|
|
|
ret = posix_fallocate(m_fd, 0, s);
|
2010-10-18 09:15:57 +02:00
|
|
|
// posix_allocate fails with EINVAL in case the underlying
|
|
|
|
// filesystem does bot support this operation
|
|
|
|
if (ret != 0 && ret != EINVAL)
|
2010-01-18 01:51:40 +01:00
|
|
|
{
|
|
|
|
ec.assign(ret, get_posix_category());
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2010-01-09 19:40:05 +01:00
|
|
|
void file::finalize()
|
|
|
|
{
|
|
|
|
#ifdef TORRENT_WINDOWS
|
|
|
|
// according to MSDN, clearing the sparse flag of a file only
|
|
|
|
// works on windows vista and later
|
2010-02-14 05:05:18 +01:00
|
|
|
#ifdef TORRENT_MINGW
|
|
|
|
typedef struct _FILE_SET_SPARSE_BUFFER {
|
|
|
|
BOOLEAN SetSparse;
|
|
|
|
} FILE_SET_SPARSE_BUFFER, *PFILE_SET_SPARSE_BUFFER;
|
|
|
|
#endif
|
2010-01-09 19:40:05 +01:00
|
|
|
DWORD temp;
|
|
|
|
FILE_SET_SPARSE_BUFFER b;
|
|
|
|
b.SetSparse = FALSE;
|
|
|
|
::DeviceIoControl(m_file_handle, FSCTL_SET_SPARSE, &b, sizeof(b)
|
|
|
|
, 0, 0, &temp, 0);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2009-02-17 01:11:38 +01:00
|
|
|
size_type 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;
|
|
|
|
if (!GetFileSizeEx(m_file_handle, &file_size))
|
2008-07-20 13:14:54 +02:00
|
|
|
{
|
2009-10-26 02:29:39 +01:00
|
|
|
ec.assign(GetLastError(), get_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
|
2009-01-11 03:02:34 +01:00
|
|
|
struct stat fs;
|
|
|
|
if (fstat(m_fd, &fs) != 0)
|
2008-07-20 13:14:54 +02:00
|
|
|
{
|
2009-10-26 02:29:39 +01:00
|
|
|
ec.assign(errno, get_posix_category());
|
2008-07-20 13:14:54 +02:00
|
|
|
return -1;
|
|
|
|
}
|
2009-01-11 03:02:34 +01:00
|
|
|
return fs.st_size;
|
2008-07-20 13:14:54 +02:00
|
|
|
#endif
|
2004-01-16 03:57:45 +01:00
|
|
|
}
|
2009-02-17 01:11:38 +01:00
|
|
|
|
|
|
|
size_type file::sparse_end(size_type start) const
|
|
|
|
{
|
|
|
|
#ifdef TORRENT_WINDOWS
|
2010-02-14 05:05:18 +01:00
|
|
|
#ifdef TORRENT_MINGW
|
|
|
|
typedef struct _FILE_ALLOCATED_RANGE_BUFFER {
|
|
|
|
LARGE_INTEGER FileOffset;
|
|
|
|
LARGE_INTEGER Length;
|
|
|
|
} FILE_ALLOCATED_RANGE_BUFFER, *PFILE_ALLOCATED_RANGE_BUFFER;
|
|
|
|
#define FSCTL_QUERY_ALLOCATED_RANGES ((0x9 << 16) | (1 << 14) | (51 << 2) | 3)
|
|
|
|
#endif
|
2009-02-17 01:11:38 +01:00
|
|
|
FILE_ALLOCATED_RANGE_BUFFER buffer;
|
|
|
|
DWORD bytes_returned = 0;
|
|
|
|
FILE_ALLOCATED_RANGE_BUFFER in;
|
|
|
|
error_code ec;
|
|
|
|
size_type file_size = get_size(ec);
|
|
|
|
if (ec) return start;
|
2012-01-22 00:24:24 +01:00
|
|
|
|
|
|
|
if (m_open_mode & no_buffer)
|
|
|
|
{
|
|
|
|
boost::uint64_t mask = size_alignment()-1;
|
|
|
|
in.FileOffset.QuadPart = start & (~mask);
|
|
|
|
in.Length.QuadPart = ((file_size + mask) & ~mask) - in.FileOffset.QuadPart;
|
|
|
|
TORRENT_ASSERT((in.Length.QuadPart & mask) == 0);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
in.FileOffset.QuadPart = start;
|
|
|
|
in.Length.QuadPart = file_size - start;
|
|
|
|
}
|
|
|
|
|
2009-02-17 01:11:38 +01:00
|
|
|
if (!DeviceIoControl(m_file_handle, FSCTL_QUERY_ALLOCATED_RANGES
|
|
|
|
, &in, sizeof(FILE_ALLOCATED_RANGE_BUFFER)
|
|
|
|
, &buffer, sizeof(FILE_ALLOCATED_RANGE_BUFFER), &bytes_returned, 0))
|
|
|
|
{
|
|
|
|
if (GetLastError() != ERROR_INSUFFICIENT_BUFFER) return start;
|
|
|
|
}
|
|
|
|
|
|
|
|
// if there are no allocated regions within the rest
|
|
|
|
// of the file, return the end of the file
|
|
|
|
if (bytes_returned == 0) return file_size;
|
|
|
|
|
|
|
|
// assume that this range overlaps the start of the
|
|
|
|
// region we were interested in, and that start actually
|
|
|
|
// resides in an allocated region.
|
|
|
|
if (buffer.FileOffset.QuadPart < start) return start;
|
|
|
|
|
|
|
|
// return the offset to the next allocated region
|
|
|
|
return buffer.FileOffset.QuadPart;
|
|
|
|
|
|
|
|
#elif defined SEEK_DATA
|
|
|
|
// this is supported on solaris
|
|
|
|
size_type ret = lseek(m_fd, start, SEEK_DATA);
|
|
|
|
if (ret < 0) return start;
|
2011-09-05 01:29:47 +02:00
|
|
|
return start;
|
2009-02-17 01:11:38 +01:00
|
|
|
#else
|
|
|
|
return start;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2004-01-16 03:57:45 +01:00
|
|
|
}
|
2008-07-20 13:14:54 +02:00
|
|
|
|