formatting fixes in client test, and made the test build when resolve countries is...
[libtorrent-kjk.git] / src / file_win.cpp
blob9d2c2f4bf80a92177eefc0973bf43dfc5fbc97de
1 /*
3 Copyright (c) 2003, Magnus Jonsson & Arvid Norberg
4 All rights reserved.
6 Redistribution and use in source and binary forms, with or without
7 modification, are permitted provided that the following conditions
8 are met:
10 * Redistributions of source code must retain the above copyright
11 notice, this list of conditions and the following disclaimer.
12 * Redistributions in binary form must reproduce the above copyright
13 notice, this list of conditions and the following disclaimer in
14 the documentation and/or other materials provided with the distribution.
15 * Neither the name of the author nor the names of its
16 contributors may be used to endorse or promote products derived
17 from this software without specific prior written permission.
19 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
23 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 POSSIBILITY OF SUCH DAMAGE.
33 #include "libtorrent/file.hpp"
34 #include "libtorrent/utf8.hpp"
36 #ifdef UNICODE
37 #include "libtorrent/storage.hpp"
38 #endif
40 #include <sstream>
41 #include <windows.h>
42 #include <winioctl.h>
44 namespace
46 // must be used to not leak memory in case something would throw
47 class auto_localfree
49 public:
50 auto_localfree(HLOCAL memory)
51 : m_memory(memory)
54 ~auto_localfree()
56 if (m_memory)
57 LocalFree(m_memory);
59 private:
60 HLOCAL m_memory;
63 std::string utf8_native(std::string const& s)
65 try
67 std::wstring ws;
68 libtorrent::utf8_wchar(s, ws);
69 std::size_t size = wcstombs(0, ws.c_str(), 0);
70 if (size == std::size_t(-1)) return s;
71 std::string ret;
72 ret.resize(size);
73 size = wcstombs(&ret[0], ws.c_str(), size + 1);
74 if (size == wchar_t(-1)) return s;
75 ret.resize(size);
76 return ret;
78 catch(std::exception)
80 return s;
84 void throw_exception(const char* thrower)
86 DWORD err = GetLastError();
88 #ifdef UNICODE
89 wchar_t *wbuffer = 0;
90 FormatMessage(
91 FORMAT_MESSAGE_FROM_SYSTEM
92 |FORMAT_MESSAGE_ALLOCATE_BUFFER
93 , 0, err, 0, (LPWSTR)&wbuffer, 0, 0);
94 auto_localfree auto_free(wbuffer);
95 std::string tmp_utf8;
96 libtorrent::wchar_utf8(wbuffer, tmp_utf8);
97 char const* buffer = tmp_utf8.c_str();
98 #else
99 char* buffer = 0;
100 FormatMessage(
101 FORMAT_MESSAGE_FROM_SYSTEM
102 |FORMAT_MESSAGE_ALLOCATE_BUFFER
103 , 0, err, 0, (LPSTR)&buffer, 0, 0);
104 auto_localfree auto_free(buffer);
105 #endif
107 std::stringstream s;
108 s << (thrower ? thrower : "NULL") << ": " << (buffer ? buffer : "NULL");
110 throw libtorrent::file_error(s.str());
114 namespace libtorrent
117 struct file::impl : boost::noncopyable
119 enum open_flags
121 read_flag = 1,
122 write_flag = 2
125 enum seek_mode
127 seek_begin = FILE_BEGIN,
128 seek_from_here = FILE_CURRENT,
129 seek_end = FILE_END
132 impl()
134 m_file_handle = INVALID_HANDLE_VALUE;
137 void open(const char *file_name, open_flags flags)
139 assert(file_name);
140 assert(flags & (read_flag | write_flag));
142 DWORD access_mask = 0;
143 if (flags & read_flag)
144 access_mask |= GENERIC_READ;
145 if (flags & write_flag)
146 access_mask |= GENERIC_WRITE;
148 assert(access_mask & (GENERIC_READ | GENERIC_WRITE));
150 #ifdef UNICODE
151 std::wstring wfile_name(safe_convert(file_name));
152 HANDLE new_handle = CreateFile(
153 wfile_name.c_str()
154 , access_mask
155 , FILE_SHARE_READ
157 , (flags & write_flag)?OPEN_ALWAYS:OPEN_EXISTING
158 , FILE_ATTRIBUTE_NORMAL
159 , 0);
160 #else
161 HANDLE new_handle = CreateFile(
162 utf8_native(file_name).c_str()
163 , access_mask
164 , FILE_SHARE_READ
166 , (flags & write_flag)?OPEN_ALWAYS:OPEN_EXISTING
167 , FILE_ATTRIBUTE_NORMAL
168 , 0);
169 #endif
171 if (new_handle == INVALID_HANDLE_VALUE)
172 throw_exception(file_name);
173 // try to make the file sparse if supported
174 if (access_mask & GENERIC_WRITE)
176 DWORD temp;
177 ::DeviceIoControl(new_handle, FSCTL_SET_SPARSE, 0, 0
178 , 0, 0, &temp, 0);
180 // will only close old file if the open succeeded
181 close();
182 m_file_handle = new_handle;
185 void close()
187 if (m_file_handle != INVALID_HANDLE_VALUE)
189 CloseHandle(m_file_handle);
190 m_file_handle = INVALID_HANDLE_VALUE;
194 ~impl()
196 close();
199 size_type write(const char* buffer, size_type num_bytes)
201 assert(buffer);
202 assert((DWORD)num_bytes == num_bytes);
203 DWORD bytes_written = 0;
204 if (num_bytes != 0)
206 if (FALSE == WriteFile(
207 m_file_handle
208 , buffer
209 , (DWORD)num_bytes
210 , &bytes_written
211 , 0))
213 throw_exception("file::write");
216 return bytes_written;
219 size_type read(char* buffer, size_type num_bytes)
221 assert(buffer);
222 assert(num_bytes >= 0);
223 assert((DWORD)num_bytes == num_bytes);
225 DWORD bytes_read = 0;
226 if (num_bytes != 0)
228 if (FALSE == ReadFile(
229 m_file_handle
230 , buffer
231 , (DWORD)num_bytes
232 , &bytes_read
233 , 0))
235 throw_exception("file::read");
238 return bytes_read;
241 void set_size(size_type s)
243 size_type pos = tell();
244 seek(s, seek_begin);
245 if (FALSE == ::SetEndOfFile(m_file_handle))
246 throw_exception("file::set_size");
248 seek(pos, seek_begin);
251 size_type seek(size_type pos, seek_mode from_where)
253 assert(pos >= 0 || from_where != seek_begin);
254 assert(pos <= 0 || from_where != seek_end);
255 LARGE_INTEGER offs;
256 offs.QuadPart = pos;
257 if (FALSE == SetFilePointerEx(
258 m_file_handle
259 , offs
260 , &offs
261 , from_where))
263 throw_exception("file::seek");
265 return offs.QuadPart;
268 size_type tell()
270 LARGE_INTEGER offs;
271 offs.QuadPart = 0;
273 // is there any other way to get offset?
274 if (FALSE == SetFilePointerEx(
275 m_file_handle
276 , offs
277 , &offs
278 , FILE_CURRENT))
280 throw_exception("file::tell");
283 size_type pos = offs.QuadPart;
284 assert(pos >= 0);
285 return pos;
288 size_type size()
290 LARGE_INTEGER s;
291 if (FALSE == GetFileSizeEx(m_file_handle, &s))
293 throw_exception("file::size");
296 size_type size = s.QuadPart;
297 assert(size >= 0);
298 return size;
301 private:
303 HANDLE m_file_handle;
308 namespace libtorrent
311 const file::seek_mode file::begin(file::impl::seek_begin);
312 const file::seek_mode file::end(file::impl::seek_end);
314 const file::open_mode file::in(file::impl::read_flag);
315 const file::open_mode file::out(file::impl::write_flag);
317 file::file()
318 : m_impl(new libtorrent::file::impl())
321 file::file(boost::filesystem::path const& p, open_mode m)
322 : m_impl(new libtorrent::file::impl())
324 open(p,m);
327 file::~file()
331 void file::open(boost::filesystem::path const& p, open_mode m)
333 assert(p.is_complete());
334 m_impl->open(p.native_file_string().c_str(), impl::open_flags(m.m_mask));
337 void file::close()
339 m_impl->close();
342 size_type file::write(const char* buffer, size_type num_bytes)
344 return m_impl->write(buffer, num_bytes);
347 size_type file::read(char* buffer, size_type num_bytes)
349 return m_impl->read(buffer, num_bytes);
352 void file::set_size(size_type s)
354 m_impl->set_size(s);
357 size_type file::seek(size_type pos, seek_mode m)
359 return m_impl->seek(pos,impl::seek_mode(m.m_val));
362 size_type file::tell()
364 return m_impl->tell();