premiere-libtorrent/src/file.cpp

339 lines
7.4 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 <boost/scoped_ptr.hpp>
2004-09-24 12:50:03 +02:00
#ifdef _WIN32
// windows part
2005-08-18 13:20:17 +02:00
#include "libtorrent/utf8.hpp"
2004-03-31 01:55:52 +02:00
#include <io.h>
#include <fcntl.h>
2004-04-02 12:43:37 +02:00
#include <sys/stat.h>
#include <sys/types.h>
2004-07-19 14:38:43 +02:00
#ifndef _MODE_T_
2004-04-02 12:43:37 +02:00
typedef int mode_t;
2004-07-19 14:38:43 +02:00
#endif
2004-03-31 01:55:52 +02:00
#ifdef UNICODE
#include "libtorrent/storage.hpp"
#endif
2004-03-31 01:55:52 +02:00
#else
2004-09-24 12:50:03 +02:00
// unix 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
{
2004-01-16 16:07:01 +01:00
enum { mode_in = 1, mode_out = 2 };
2004-04-02 12:43:37 +02:00
mode_t map_open_mode(int m)
2004-01-16 03:57:45 +01:00
{
2004-12-21 13:30:09 +01:00
if (m == (mode_in | mode_out)) return O_RDWR | O_CREAT | O_BINARY | O_RANDOM;
2004-04-05 00:15:31 +02:00
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);
2004-04-14 05:01:06 +02:00
return 0;
2004-01-16 03:57:45 +01:00
}
2005-08-04 00:51:21 +02:00
#ifdef WIN32
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
std::string const& utf8_native(std::string const& s)
2005-08-04 00:51:21 +02:00
{
return s;
}
#endif
2004-01-16 03:57:45 +01:00
}
namespace libtorrent
{
2007-06-10 22:46:09 +02:00
namespace fs = boost::filesystem;
2004-01-16 16:07:01 +01:00
const file::open_mode file::in(mode_in);
const file::open_mode file::out(mode_out);
2004-01-16 03:57:45 +01:00
const file::seek_mode file::begin(1);
const file::seek_mode file::end(2);
struct file::impl
{
2004-03-31 01:55:52 +02:00
impl()
: m_fd(-1)
, m_open_mode(0)
{}
2004-01-16 03:57:45 +01:00
impl(fs::path const& path, int mode, error_code& ec)
2004-03-31 01:55:52 +02:00
: m_fd(-1)
, m_open_mode(0)
2004-01-16 03:57:45 +01:00
{
open(path, mode, ec);
2004-03-31 01:55:52 +02:00
}
2004-01-16 16:07:01 +01:00
2004-03-31 01:55:52 +02:00
~impl()
{
close();
2004-01-16 03:57:45 +01:00
}
bool open(fs::path const& path, int mode, error_code& ec)
2004-01-16 03:57:45 +01:00
{
2004-03-31 01:55:52 +02:00
close();
#ifdef TORRENT_WINDOWS
const int permissions = _S_IREAD | _S_IWRITE;
#ifdef defined UNICODE
#define open _wopen
std::wstring file_path(safe_convert(path.native_file_string()));
2005-08-17 03:57:30 +02:00
#else
#define open _open
std::string const& file_path = path.native_file_string();
#endif
#else // if not windows
// rely on default umask to filter x and w permissions
// for group and others
const mode_t permissions = S_IRWXU | S_IRWXG | S_IRWXO;
std::string const& file_path = path.native_file_string();
#endif
m_fd = ::open(file_path.c_str(), map_open_mode(mode), permissions);
#ifdef TORRENT_WINDOWS
#undef open
2005-08-17 03:57:30 +02:00
#endif
2004-03-31 01:55:52 +02:00
if (m_fd == -1)
{
ec = error_code(errno, get_posix_category());
return false;
2004-03-31 01:55:52 +02:00
}
2004-04-02 12:43:37 +02:00
m_open_mode = mode;
return true;
2004-01-16 03:57:45 +01:00
}
void close()
{
2004-03-31 01:55:52 +02:00
if (m_fd == -1) return;
2004-04-03 00:21:20 +02:00
#ifdef _WIN32
::_close(m_fd);
#else
2004-03-31 01:55:52 +02:00
::close(m_fd);
#endif
2004-03-31 01:55:52 +02:00
m_fd = -1;
2004-01-16 16:07:01 +01:00
m_open_mode = 0;
2004-01-16 03:57:45 +01:00
}
size_type read(char* buf, size_type num_bytes, error_code& ec)
2004-01-16 03:57:45 +01:00
{
TORRENT_ASSERT(m_open_mode & mode_in);
TORRENT_ASSERT(m_fd != -1);
2004-03-31 01:55:52 +02:00
#ifdef _WIN32
size_type ret = ::_read(m_fd, buf, num_bytes);
#else
2004-03-31 01:55:52 +02:00
size_type ret = ::read(m_fd, buf, num_bytes);
#endif
if (ret == -1) ec = error_code(errno, get_posix_category());
2004-03-31 01:55:52 +02:00
return ret;
2004-01-16 03:57:45 +01:00
}
size_type write(const char* buf, size_type num_bytes, error_code& ec)
2004-01-16 03:57:45 +01:00
{
TORRENT_ASSERT(m_open_mode & mode_out);
TORRENT_ASSERT(m_fd != -1);
2004-03-31 01:55:52 +02:00
// TODO: Test this a bit more, what happens with random failures in
// the files?
// if ((rand() % 100) > 80)
// throw file_error("debug");
#ifdef _WIN32
size_type ret = ::_write(m_fd, buf, num_bytes);
#else
2004-03-31 01:55:52 +02:00
size_type ret = ::write(m_fd, buf, num_bytes);
#endif
if (ret == -1) ec = error_code(errno, get_posix_category());
2004-03-31 01:55:52 +02:00
return ret;
2004-01-16 03:57:45 +01:00
}
bool set_size(size_type s, error_code& ec)
{
#ifdef _WIN32
#error file.cpp is for posix systems only. use file_win.cpp on windows
#else
if (ftruncate(m_fd, s) < 0)
{
ec = error_code(errno, get_posix_category());
return false;
}
return true;
#endif
}
size_type seek(size_type offset, int m, error_code& ec)
2004-01-16 03:57:45 +01:00
{
TORRENT_ASSERT(m_open_mode);
TORRENT_ASSERT(m_fd != -1);
2004-03-31 01:55:52 +02:00
int seekdir = (m == 1)?SEEK_SET:SEEK_END;
2004-09-24 12:50:03 +02:00
#ifdef _WIN32
2004-03-31 01:55:52 +02:00
size_type ret = _lseeki64(m_fd, offset, seekdir);
#else
size_type ret = lseek(m_fd, offset, seekdir);
#endif
2004-04-05 00:15:31 +02:00
// For some strange reason this fails
// on win32. Use windows specific file
// wrapper instead.
if (ret < 0) ec = error_code(errno, get_posix_category());
return ret;
2004-01-16 03:57:45 +01:00
}
size_type tell(error_code& ec)
2004-01-16 03:57:45 +01:00
{
TORRENT_ASSERT(m_open_mode);
TORRENT_ASSERT(m_fd != -1);
2004-01-16 16:07:01 +01:00
size_type ret;
2004-09-24 12:50:03 +02:00
#ifdef _WIN32
ret = _telli64(m_fd);
2004-02-19 17:47:12 +01:00
#else
ret = lseek(m_fd, 0, SEEK_CUR);
2004-02-19 17:47:12 +01:00
#endif
if (ret < 0) ec = error_code(errno, get_posix_category());
return ret;
}
2004-03-31 01:55:52 +02:00
int m_fd;
2004-01-16 16:07:01 +01:00
int m_open_mode;
2004-01-16 03:57:45 +01:00
};
2004-01-16 13:43:23 +01:00
// pimpl forwardings
2004-01-16 03:57:45 +01:00
file::file() : m_impl(new impl()) {}
file::file(fs::path const& p, file::open_mode m, error_code& ec)
: m_impl(new impl(p, m.m_mask, ec))
2004-01-16 03:57:45 +01:00
{}
file::~file() {}
bool file::open(fs::path const& p, file::open_mode m, error_code& ec)
2004-01-16 03:57:45 +01:00
{
return m_impl->open(p, m.m_mask, ec);
2004-01-16 03:57:45 +01:00
}
void file::close()
{
m_impl->close();
}
size_type file::write(const char* buf, size_type num_bytes, error_code& ec)
2004-01-16 03:57:45 +01:00
{
return m_impl->write(buf, num_bytes, ec);
2004-01-16 03:57:45 +01:00
}
size_type file::read(char* buf, size_type num_bytes, error_code& ec)
2004-01-16 03:57:45 +01:00
{
return m_impl->read(buf, num_bytes, ec);
2004-01-16 03:57:45 +01:00
}
bool file::set_size(size_type s, error_code& ec)
{
return m_impl->set_size(s, ec);
}
size_type file::seek(size_type pos, file::seek_mode m, error_code& ec)
2004-01-16 03:57:45 +01:00
{
return m_impl->seek(pos, m.m_val, ec);
2004-01-16 03:57:45 +01:00
}
size_type file::tell(error_code& ec)
2004-01-16 03:57:45 +01:00
{
return m_impl->tell(ec);
2004-01-16 03:57:45 +01:00
}
}