premiere-libtorrent/src/file_win.cpp

346 lines
7.3 KiB
C++
Raw Normal View History

2004-01-19 20:36:55 +01:00
/*
2004-01-19 22:23:48 +01:00
Copyright (c) 2003, Magnus Jonsson & Arvid Norberg
2004-01-19 20:36:55 +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.
*/
#include "libtorrent/file.hpp"
2005-04-02 13:07:27 +02:00
#include "libtorrent/utf8.hpp"
2005-11-02 17:46:45 +01:00
#ifdef UNICODE
#include "libtorrent/storage.hpp"
#endif
2005-04-02 13:07:27 +02:00
#include <sstream>
2004-01-19 20:36:55 +01:00
#include <windows.h>
2004-01-19 20:36:55 +01:00
namespace
{
2004-01-23 00:49:48 +01:00
// must be used to not leak memory in case something would throw
2005-04-02 13:07:27 +02:00
class auto_localfree
2004-01-23 00:49:48 +01:00
{
public:
2005-04-02 13:07:27 +02:00
auto_localfree(HLOCAL memory)
2004-01-23 00:49:48 +01:00
: m_memory(memory)
{
}
2005-04-02 13:07:27 +02:00
~auto_localfree()
2004-01-23 00:49:48 +01:00
{
2004-10-16 03:10:42 +02:00
if (m_memory)
2004-01-23 00:49:48 +01:00
LocalFree(m_memory);
}
private:
HLOCAL m_memory;
};
2005-07-01 15:17:47 +02:00
std::string utf8_native(std::string const& s)
{
2005-07-02 04:11:18 +02:00
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 23:21:28 +02:00
if (size == wchar_t(-1)) return s;
2005-07-27 19:55:16 +02:00
ret.resize(size);
2005-07-02 04:11:18 +02:00
return ret;
}
catch(std::exception)
{
return s;
}
2005-07-01 15:17:47 +02:00
}
2004-01-19 20:36:55 +01:00
void throw_exception(const char* thrower)
{
2005-07-01 15:17:47 +02:00
DWORD err = GetLastError();
2004-10-13 14:55:13 +02:00
2005-04-16 12:20:50 +02:00
#ifdef UNICODE
wchar_t *wbuffer = 0;
FormatMessage(
FORMAT_MESSAGE_FROM_SYSTEM
|FORMAT_MESSAGE_ALLOCATE_BUFFER
2005-07-01 15:17:47 +02:00
, 0, err, 0, (LPWSTR)&wbuffer, 0, 0);
2005-04-16 12:20:50 +02:00
auto_localfree auto_free(wbuffer);
std::string tmp_utf8;
2005-07-01 15:17:47 +02:00
libtorrent::wchar_utf8(wbuffer, tmp_utf8);
char const* buffer = tmp_utf8.c_str();
2005-04-16 12:20:50 +02:00
#else
char* buffer = 0;
FormatMessage(
FORMAT_MESSAGE_FROM_SYSTEM
|FORMAT_MESSAGE_ALLOCATE_BUFFER
, 0, err, 0, (LPSTR)&buffer, 0, 0);
auto_localfree auto_free(buffer);
#endif
2004-01-23 00:49:48 +01:00
2004-01-19 20:36:55 +01:00
std::stringstream s;
2004-01-23 00:49:48 +01:00
s << (thrower ? thrower : "NULL") << ": " << (buffer ? buffer : "NULL");
2004-01-19 20:36:55 +01:00
throw libtorrent::file_error(s.str());
}
}
namespace libtorrent
{
struct file::impl : boost::noncopyable
{
2004-01-19 20:36:55 +01:00
enum open_flags
{
read_flag = 1,
write_flag = 2
};
enum seek_mode
{
seek_begin = FILE_BEGIN,
seek_from_here = FILE_CURRENT,
seek_end = FILE_END
};
impl()
{
2004-01-19 20:36:55 +01:00
m_file_handle = INVALID_HANDLE_VALUE;
}
void open(const char *file_name, open_flags flags)
{
assert(file_name);
2004-01-19 20:36:55 +01:00
assert(flags & (read_flag | write_flag));
2004-01-19 20:36:55 +01:00
DWORD access_mask = 0;
if (flags & read_flag)
access_mask |= GENERIC_READ;
if (flags & write_flag)
access_mask |= GENERIC_WRITE;
2004-01-19 20:36:55 +01:00
assert(access_mask & (GENERIC_READ | GENERIC_WRITE));
2005-04-02 19:43:41 +02:00
#ifdef UNICODE
2005-07-02 04:11:18 +02:00
std::wstring wfile_name(safe_convert(file_name));
2004-01-19 20:36:55 +01:00
HANDLE new_handle = CreateFile(
2005-08-16 22:42:10 +02:00
wfile_name.c_str()
2004-01-19 20:36:55 +01:00
, access_mask
, FILE_SHARE_READ
2004-06-28 22:23:42 +02:00
, 0
2005-01-14 15:05:25 +01:00
, (flags & write_flag)?OPEN_ALWAYS:OPEN_EXISTING
2004-01-19 20:36:55 +01:00
, FILE_ATTRIBUTE_NORMAL
2004-06-28 22:23:42 +02:00
, 0);
2004-10-13 14:55:13 +02:00
#else
HANDLE new_handle = CreateFile(
2005-07-01 15:17:47 +02:00
utf8_native(file_name).c_str()
2004-10-13 14:55:13 +02:00
, access_mask
, FILE_SHARE_READ
, 0
2005-01-14 15:05:25 +01:00
, (flags & write_flag)?OPEN_ALWAYS:OPEN_EXISTING
2004-10-13 14:55:13 +02:00
, FILE_ATTRIBUTE_NORMAL
, 0);
#endif
2004-01-19 20:36:55 +01:00
if (new_handle == INVALID_HANDLE_VALUE)
{
2004-01-19 20:36:55 +01:00
std::stringstream s;
2005-01-14 15:05:25 +01:00
throw_exception(file_name);
}
// will only close old file if the open succeeded
close();
2004-01-19 20:36:55 +01:00
m_file_handle = new_handle;
}
void close()
{
2004-01-19 20:36:55 +01:00
if (m_file_handle != INVALID_HANDLE_VALUE)
{
CloseHandle(m_file_handle);
2004-01-19 20:36:55 +01:00
m_file_handle = INVALID_HANDLE_VALUE;
}
}
~impl()
{
close();
}
size_type write(const char* buffer, size_type num_bytes)
{
assert(buffer);
2004-01-19 20:36:55 +01:00
assert((DWORD)num_bytes == num_bytes);
DWORD bytes_written = 0;
if (num_bytes != 0)
{
2004-01-19 20:36:55 +01:00
if (FALSE == WriteFile(
m_file_handle
, buffer
, (DWORD)num_bytes
, &bytes_written
2004-06-28 22:23:42 +02:00
, 0))
2004-01-19 20:36:55 +01:00
{
throw_exception("file::write");
}
}
return bytes_written;
}
size_type read(char* buffer, size_type num_bytes)
{
assert(buffer);
assert(num_bytes >= 0);
2004-01-19 20:36:55 +01:00
assert((DWORD)num_bytes == num_bytes);
2004-01-19 20:36:55 +01:00
DWORD bytes_read = 0;
if (num_bytes != 0)
{
2004-01-19 20:36:55 +01:00
if (FALSE == ReadFile(
m_file_handle
, buffer
, (DWORD)num_bytes
, &bytes_read
2004-06-28 22:23:42 +02:00
, 0))
2004-01-19 20:36:55 +01:00
{
throw_exception("file::read");
}
}
return bytes_read;
}
size_type seek(size_type pos, seek_mode from_where)
{
2004-01-25 23:41:55 +01:00
assert(pos >= 0 || from_where != seek_begin);
assert(pos <= 0 || from_where != seek_end);
LARGE_INTEGER offs;
2004-01-19 20:36:55 +01:00
offs.QuadPart = pos;
if (FALSE == SetFilePointerEx(
m_file_handle
, offs
, &offs
, from_where))
{
2004-10-16 03:10:42 +02:00
throw_exception("file::seek");
2004-01-19 20:36:55 +01:00
}
}
size_type tell()
{
LARGE_INTEGER offs;
2004-01-19 20:36:55 +01:00
offs.QuadPart = 0;
// is there any other way to get offset?
2004-01-19 20:36:55 +01:00
if (FALSE == SetFilePointerEx(
m_file_handle
, offs
, &offs
, FILE_CURRENT))
{
throw_exception("file::tell");
}
size_type pos=offs.QuadPart;
assert(pos>=0);
return pos;
}
2004-01-19 20:36:55 +01:00
/*
size_type size()
{
LARGE_INTEGER s;
2004-01-19 20:36:55 +01:00
if (FALSE == GetFileSizeEx(m_file_handle, &s))
{
throw_exception("file::size");
}
2004-01-19 20:36:55 +01:00
size_type size = s.QuadPart;
assert(size >= 0);
return size;
}
2004-01-19 20:36:55 +01:00
*/
private:
2004-01-19 20:36:55 +01:00
HANDLE m_file_handle;
2004-01-19 20:36:55 +01:00
};
}
2004-01-19 20:36:55 +01:00
namespace libtorrent
{
const file::seek_mode file::begin(file::impl::seek_begin);
const file::seek_mode file::end(file::impl::seek_end);
const file::open_mode file::in(file::impl::read_flag);
const file::open_mode file::out(file::impl::write_flag);
file::file()
: m_impl(new libtorrent::file::impl())
{
}
file::file(boost::filesystem::path const& p, open_mode m)
: m_impl(new libtorrent::file::impl())
{
open(p,m);
}
file::~file()
{
}
void file::open(boost::filesystem::path const& p, open_mode m)
{
2004-10-16 03:10:42 +02:00
assert(p.is_complete());
2004-01-19 20:36:55 +01:00
m_impl->open(p.native_file_string().c_str(), impl::open_flags(m.m_mask));
}
void file::close()
{
m_impl->close();
}
2004-01-19 19:20:47 +01:00
size_type file::write(const char* buffer, size_type num_bytes)
{
2004-01-19 20:36:55 +01:00
return m_impl->write(buffer, num_bytes);
}
2004-01-19 19:20:47 +01:00
size_type file::read(char* buffer, size_type num_bytes)
{
2004-01-19 20:36:55 +01:00
return m_impl->read(buffer, num_bytes);
}
size_type file::seek(size_type pos, seek_mode m)
{
return m_impl->seek(pos,impl::seek_mode(m.m_val));
}
2004-01-19 19:20:47 +01:00
size_type file::tell()
{
return m_impl->tell();
}
2004-01-19 20:36:55 +01:00
}