premiere-libtorrent/src/file.cpp

367 lines
8.2 KiB
C++
Raw Normal View History

2004-01-16 03:57:45 +01:00
/*
Copyright (c) 2003, Arvid Norberg
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in
the documentation and/or other materials provided with the distribution.
* Neither the name of the author nor the names of its
contributors may be used to endorse or promote products derived
from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
*/
#include "libtorrent/pch.hpp"
#include "libtorrent/config.hpp"
#include <boost/scoped_ptr.hpp>
#ifdef TORRENT_WINDOWS
2004-09-24 12:50:03 +02:00
// windows part
2005-08-18 13:20:17 +02:00
#include "libtorrent/utf8.hpp"
#include <windows.h>
#include <winioctl.h>
2004-03-31 01:55:52 +02:00
#ifdef UNICODE
#include "libtorrent/storage.hpp"
#endif
2004-03-31 01:55:52 +02:00
#else
// posix part
2004-03-31 01:55:52 +02:00
#define _FILE_OFFSET_BITS 64
#include <unistd.h>
#include <fcntl.h>
2004-04-02 12:43:37 +02:00
#include <sys/stat.h>
#include <sys/types.h>
2004-10-31 09:20:05 +01:00
#include <errno.h>
2004-03-31 01:55:52 +02:00
2005-08-23 11:59:56 +02:00
#include <boost/static_assert.hpp>
// make sure the _FILE_OFFSET_BITS define worked
// on this platform
BOOST_STATIC_ASSERT(sizeof(lseek(0, 0, 0)) >= 8);
2004-03-31 01:55:52 +02:00
#endif
2005-08-23 11:59:56 +02:00
#include <boost/filesystem/operations.hpp>
#include "libtorrent/file.hpp"
#include <sstream>
2007-11-18 08:27:40 +01:00
#include <cstring>
#include <vector>
2005-08-23 11:59:56 +02:00
2004-03-31 01:55:52 +02:00
#ifndef O_BINARY
#define O_BINARY 0
#endif
2004-01-16 03:57:45 +01:00
2004-04-05 00:15:31 +02:00
#ifndef O_RANDOM
#define O_RANDOM 0
#endif
#ifdef UNICODE
#include "libtorrent/storage.hpp"
#endif
#include "libtorrent/assert.hpp"
2004-04-05 00:15:31 +02:00
2004-01-16 03:57:45 +01:00
namespace
{
#ifdef TORRENT_WINDOWS
2005-08-04 00:51:21 +02:00
std::string utf8_native(std::string const& s)
{
try
{
std::wstring ws;
libtorrent::utf8_wchar(s, ws);
std::size_t size = wcstombs(0, ws.c_str(), 0);
if (size == std::size_t(-1)) return s;
std::string ret;
ret.resize(size);
size = wcstombs(&ret[0], ws.c_str(), size + 1);
2005-08-17 04:38:34 +02:00
if (size == wchar_t(-1)) return s;
2005-08-04 00:51:21 +02:00
ret.resize(size);
return ret;
}
catch(std::exception)
{
return s;
}
}
#else
enum { mode_in = 1, mode_out = 2 };
mode_t map_open_mode(int m)
2005-08-04 00:51:21 +02:00
{
if (m == (mode_in | mode_out)) return O_RDWR | O_CREAT | O_BINARY | O_RANDOM;
if (m == mode_out) return O_WRONLY | O_CREAT | O_BINARY | O_RANDOM;
if (m == mode_in) return O_RDONLY | O_BINARY | O_RANDOM;
TORRENT_ASSERT(false);
return 0;
2005-08-04 00:51:21 +02:00
}
#endif
2004-01-16 03:57:45 +01:00
}
namespace libtorrent
{
2007-06-10 22:46:09 +02:00
namespace fs = boost::filesystem;
#ifdef TORRENT_WINDOWS
const file::open_mode file::in(GENERIC_READ);
const file::open_mode file::out(GENERIC_WRITE);
const file::seek_mode file::begin(FILE_BEGIN);
const file::seek_mode file::end(FILE_END);
#else
2004-01-16 16:07:01 +01:00
const file::open_mode file::in(mode_in);
const file::open_mode file::out(mode_out);
const file::seek_mode file::begin(SEEK_SET);
const file::seek_mode file::end(SEEK_END);
#endif
2004-01-16 03:57:45 +01:00
file::file()
#ifdef TORRENT_WINDOWS
: m_file_handle(INVALID_HANDLE_VALUE)
2005-08-17 03:57:30 +02:00
#else
: m_fd(-1)
#endif
#ifndef NDEBUG
, m_open_mode(0)
#endif
{}
file::file(fs::path const& path, open_mode mode, error_code& ec)
#ifdef TORRENT_WINDOWS
: m_file_handle(INVALID_HANDLE_VALUE)
#else
: m_fd(-1)
#endif
#ifndef NDEBUG
, m_open_mode(0)
2005-08-17 03:57:30 +02:00
#endif
{
open(path, mode, ec);
}
file::~file()
{
close();
}
2004-01-16 03:57:45 +01:00
bool file::open(fs::path const& path, open_mode mode, error_code& ec)
{
close();
#ifdef TORRENT_WINDOWS
2004-04-03 00:21:20 +02:00
#ifdef UNICODE
std::wstring file_path(safe_convert(path.native_file_string()));
#else
std::string file_path = utf8_native(path.native_file_string());
#endif
2004-01-16 03:57:45 +01:00
2008-07-20 18:34:01 +02:00
m_file_handle = CreateFile(
file_path.c_str()
, mode.m_mask
, FILE_SHARE_READ
, 0
, (mode & out)?OPEN_ALWAYS:OPEN_EXISTING
, FILE_ATTRIBUTE_NORMAL
, 0);
if (m_file_handle == INVALID_HANDLE_VALUE)
2004-01-16 03:57:45 +01:00
{
ec = error_code(GetLastError(), get_system_category());
return false;
}
2004-03-31 01:55:52 +02:00
// try to make the file sparse if supported
if (mode & out)
{
DWORD temp;
::DeviceIoControl(m_file_handle, FSCTL_SET_SPARSE, 0, 0
, 0, 0, &temp, 0);
2004-01-16 03:57:45 +01:00
}
#else
// rely on default umask to filter x and w permissions
// for group and others
m_fd = ::open(path.native_file_string().c_str()
, map_open_mode(mode.m_mask), S_IRWXU | S_IRWXG | S_IRWXO);
2004-01-16 03:57:45 +01:00
if (m_fd == -1)
2004-01-16 03:57:45 +01:00
{
ec = error_code(errno, get_posix_category());
return false;
}
#endif
#ifndef NDEBUG
m_open_mode = mode;
#endif
2008-07-20 18:34:01 +02:00
TORRENT_ASSERT(is_open());
return true;
}
2004-03-31 01:55:52 +02:00
bool file::is_open() const
{
#ifdef TORRENT_WINDOWS
return m_file_handle != INVALID_HANDLE_VALUE;
#else
return m_fd != -1;
#endif
}
void file::close()
{
#ifdef TORRENT_WINDOWS
if (m_file_handle == INVALID_HANDLE_VALUE) return;
CloseHandle(m_file_handle);
m_file_handle = INVALID_HANDLE_VALUE;
#else
if (m_fd == -1) return;
::close(m_fd);
m_fd = -1;
#endif
#ifndef NDEBUG
m_open_mode = 0;
#endif
}
2004-01-16 03:57:45 +01:00
size_type file::read(char* buf, size_type num_bytes, error_code& ec)
{
TORRENT_ASSERT((m_open_mode & in) == in);
TORRENT_ASSERT(buf);
TORRENT_ASSERT(num_bytes >= 0);
TORRENT_ASSERT(is_open());
#ifdef TORRENT_WINDOWS
TORRENT_ASSERT(DWORD(num_bytes) == num_bytes);
DWORD ret = 0;
if (num_bytes != 0)
{
if (ReadFile(m_file_handle, buf, (DWORD)num_bytes, &ret, 0) == FALSE)
{
ec = error_code(GetLastError(), get_system_category());
return -1;
}
}
2004-03-31 01:55:52 +02:00
#else
size_type ret = ::read(m_fd, buf, num_bytes);
if (ret == -1) ec = error_code(errno, get_posix_category());
2004-03-31 01:55:52 +02:00
#endif
return ret;
}
2004-04-05 00:15:31 +02:00
size_type file::write(const char* buf, size_type num_bytes, error_code& ec)
{
TORRENT_ASSERT((m_open_mode & out) == out);
TORRENT_ASSERT(buf);
TORRENT_ASSERT(num_bytes >= 0);
TORRENT_ASSERT(is_open());
2004-01-16 03:57:45 +01:00
#ifdef TORRENT_WINDOWS
2004-01-16 16:07:01 +01:00
DWORD ret = 0;
if (num_bytes != 0)
{
if (WriteFile(m_file_handle, buf, (DWORD)num_bytes, &ret, 0) == FALSE)
{
ec = error_code(GetLastError(), get_system_category());
return -1;
}
}
2004-02-19 17:47:12 +01:00
#else
size_type ret = ::write(m_fd, buf, num_bytes);
if (ret == -1) ec = error_code(errno, get_posix_category());
2004-02-19 17:47:12 +01:00
#endif
return ret;
}
2004-01-16 03:57:45 +01: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
#ifdef TORRENT_WINDOWS
size_type pos = tell(ec);
if (ec) return false;
seek(s, begin, ec);
if (ec) return false;
if (::SetEndOfFile(m_file_handle) == FALSE)
{
ec = error_code(GetLastError(), get_system_category());
return false;
}
#else
if (ftruncate(m_fd, s) < 0)
{
ec = error_code(errno, get_posix_category());
return false;
}
#endif
return true;
2004-01-16 03:57:45 +01:00
}
size_type file::seek(size_type offset, seek_mode m, error_code& ec)
2004-01-16 03:57:45 +01:00
{
TORRENT_ASSERT(is_open());
2004-01-16 03:57:45 +01:00
#ifdef TORRENT_WINDOWS
LARGE_INTEGER offs;
offs.QuadPart = offset;
if (SetFilePointerEx(m_file_handle, offs, &offs, m.m_val) == FALSE)
{
ec = error_code(GetLastError(), get_system_category());
return -1;
}
return offs.QuadPart;
#else
size_type ret = lseek(m_fd, offset, m.m_val);
if (ret < 0) ec = error_code(errno, get_posix_category());
return ret;
#endif
2004-01-16 03:57:45 +01:00
}
size_type file::tell(error_code& ec)
2004-01-16 03:57:45 +01:00
{
TORRENT_ASSERT(is_open());
2004-01-16 03:57:45 +01:00
#ifdef TORRENT_WINDOWS
LARGE_INTEGER offs;
offs.QuadPart = 0;
// is there any other way to get offset?
if (SetFilePointerEx(m_file_handle, offs, &offs
, FILE_CURRENT) == FALSE)
{
ec = error_code(GetLastError(), get_system_category());
return -1;
}
2004-01-16 03:57:45 +01:00
return offs.QuadPart;
#else
size_type ret;
ret = lseek(m_fd, 0, SEEK_CUR);
if (ret < 0) ec = error_code(errno, get_posix_category());
return ret;
#endif
2004-01-16 03:57:45 +01:00
}
}