3 Copyright (c) 2003, Magnus Jonsson & Arvid Norberg
6 Redistribution and use in source and binary forms, with or without
7 modification, are permitted provided that the following conditions
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"
37 #include "libtorrent/storage.hpp"
46 // must be used to not leak memory in case something would throw
50 auto_localfree(HLOCAL memory
)
63 std::string
utf8_native(std::string
const& s
)
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
;
73 size
= wcstombs(&ret
[0], ws
.c_str(), size
+ 1);
74 if (size
== wchar_t(-1)) return s
;
84 void throw_exception(const char* thrower
)
86 DWORD err
= GetLastError();
91 FORMAT_MESSAGE_FROM_SYSTEM
92 |FORMAT_MESSAGE_ALLOCATE_BUFFER
93 , 0, err
, 0, (LPWSTR
)&wbuffer
, 0, 0);
94 auto_localfree
auto_free(wbuffer
);
96 libtorrent::wchar_utf8(wbuffer
, tmp_utf8
);
97 char const* buffer
= tmp_utf8
.c_str();
101 FORMAT_MESSAGE_FROM_SYSTEM
102 |FORMAT_MESSAGE_ALLOCATE_BUFFER
103 , 0, err
, 0, (LPSTR
)&buffer
, 0, 0);
104 auto_localfree
auto_free(buffer
);
108 s
<< (thrower
? thrower
: "NULL") << ": " << (buffer
? buffer
: "NULL");
110 throw libtorrent::file_error(s
.str());
117 struct file::impl
: boost::noncopyable
127 seek_begin
= FILE_BEGIN
,
128 seek_from_here
= FILE_CURRENT
,
134 m_file_handle
= INVALID_HANDLE_VALUE
;
137 void open(const char *file_name
, open_flags flags
)
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
));
151 std::wstring
wfile_name(safe_convert(file_name
));
152 HANDLE new_handle
= CreateFile(
157 , (flags
& write_flag
)?OPEN_ALWAYS
:OPEN_EXISTING
158 , FILE_ATTRIBUTE_NORMAL
161 HANDLE new_handle
= CreateFile(
162 utf8_native(file_name
).c_str()
166 , (flags
& write_flag
)?OPEN_ALWAYS
:OPEN_EXISTING
167 , FILE_ATTRIBUTE_NORMAL
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
)
177 ::DeviceIoControl(new_handle
, FSCTL_SET_SPARSE
, 0, 0
180 // will only close old file if the open succeeded
182 m_file_handle
= new_handle
;
187 if (m_file_handle
!= INVALID_HANDLE_VALUE
)
189 CloseHandle(m_file_handle
);
190 m_file_handle
= INVALID_HANDLE_VALUE
;
199 size_type
write(const char* buffer
, size_type num_bytes
)
202 assert((DWORD
)num_bytes
== num_bytes
);
203 DWORD bytes_written
= 0;
206 if (FALSE
== WriteFile(
213 throw_exception("file::write");
216 return bytes_written
;
219 size_type
read(char* buffer
, size_type num_bytes
)
222 assert(num_bytes
>= 0);
223 assert((DWORD
)num_bytes
== num_bytes
);
225 DWORD bytes_read
= 0;
228 if (FALSE
== ReadFile(
235 throw_exception("file::read");
241 void set_size(size_type s
)
243 size_type pos
= tell();
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
);
257 if (FALSE
== SetFilePointerEx(
263 throw_exception("file::seek");
265 return offs
.QuadPart
;
273 // is there any other way to get offset?
274 if (FALSE
== SetFilePointerEx(
280 throw_exception("file::tell");
283 size_type pos
= offs
.QuadPart
;
291 if (FALSE == GetFileSizeEx(m_file_handle, &s))
293 throw_exception("file::size");
296 size_type size = s.QuadPart;
303 HANDLE m_file_handle
;
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
);
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())
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
));
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
)
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();