]>
Commit | Line | Data |
---|---|---|
48714f74 RN |
1 | /////////////////////////////////////////////////////////////////////////////// |
2 | // Name: tests/streams/largefile.cpp | |
3 | // Purpose: Tests for large file support | |
4 | // Author: Mike Wetherell | |
5 | // RCS-ID: $Id$ | |
6 | // Copyright: (c) 2004 Mike Wetherell | |
7 | // Licence: wxWidgets licence | |
8 | /////////////////////////////////////////////////////////////////////////////// | |
9 | ||
10 | // | |
086d7f31 | 11 | // We're interested in what happens around offsets 0x7fffffff and 0xffffffff |
48714f74 RN |
12 | // so the test writes a small chunk of test data just before and just after |
13 | // these offsets, then reads them back. | |
14 | // | |
15 | // The tests can be run with: | |
16 | // | |
17 | // test --verbose largeFile | |
18 | // | |
19 | // On systems supporting sparse files they may also be registered in the | |
20 | // Streams subsuite. | |
21 | // | |
22 | ||
23 | // For compilers that support precompilation, includes "wx/wx.h". | |
24 | #include "testprec.h" | |
25 | ||
26 | #ifdef __BORLANDC__ | |
27 | #pragma hdrstop | |
28 | #endif | |
29 | ||
30 | // for all others, include the necessary headers | |
31 | #ifndef WX_PRECOMP | |
32 | #include "wx/wx.h" | |
33 | #endif | |
34 | ||
35 | #include "wx/filename.h" | |
36 | #include "wx/wfstream.h" | |
37 | ||
38 | #ifdef __WXMSW__ | |
845e4f9b VZ |
39 | #include "wx/msw/wrapwin.h" |
40 | #ifdef __VISUALC__ | |
41 | // 'nonstandard extension used : nameless struct/union' occurs inside | |
42 | // winioctl.h | |
43 | #pragma warning(disable:4201) | |
44 | #endif | |
45 | #include <winioctl.h> | |
46 | #ifdef __VISUALC__ | |
47 | #pragma warning(default:4201) | |
48 | #endif | |
48714f74 RN |
49 | #endif |
50 | ||
d4b17832 VZ |
51 | #ifdef __VISUALC__ |
52 | #define fileno _fileno | |
53 | #endif | |
54 | ||
48714f74 RN |
55 | using std::auto_ptr; |
56 | ||
57 | ||
58 | /////////////////////////////////////////////////////////////////////////////// | |
59 | // Helpers | |
60 | ||
61 | bool IsFAT(const wxString& path); | |
62 | void MakeSparse(const wxString& path, int fd); | |
63 | ||
64 | ||
65 | /////////////////////////////////////////////////////////////////////////////// | |
66 | // Base class for the test cases - common code | |
67 | ||
68 | class LargeFileTest : public CppUnit::TestCase | |
69 | { | |
70 | public: | |
71 | LargeFileTest(std::string name) : CppUnit::TestCase(name) { } | |
72 | virtual ~LargeFileTest() { } | |
73 | ||
74 | protected: | |
75 | void runTest(); | |
76 | ||
77 | virtual wxInputStream *MakeInStream(const wxString& name) const = 0; | |
78 | virtual wxOutputStream *MakeOutStream(const wxString& name) const = 0; | |
79 | virtual bool HasLFS() const = 0; | |
80 | }; | |
81 | ||
82 | void LargeFileTest::runTest() | |
83 | { | |
84 | // self deleting temp file | |
85 | struct TmpFile { | |
9a83f860 | 86 | TmpFile() : m_name(wxFileName::CreateTempFileName(wxT("wxlfs-"))) { } |
48714f74 RN |
87 | ~TmpFile() { if (!m_name.empty()) wxRemoveFile(m_name); } |
88 | wxString m_name; | |
89 | } tmpfile; | |
90 | ||
91 | CPPUNIT_ASSERT(!tmpfile.m_name.empty()); | |
92 | ||
93 | bool haveLFS = true; | |
94 | bool fourGigLimit = false; | |
95 | ||
96 | if (!HasLFS()) { | |
97 | haveLFS = false; | |
98 | wxString n(getName().c_str(), *wxConvCurrent); | |
9a83f860 | 99 | wxLogInfo(n + wxT(": No large file support, testing up to 2GB only")); |
48714f74 RN |
100 | } |
101 | else if (IsFAT(tmpfile.m_name)) { | |
102 | fourGigLimit = true; | |
103 | wxString n(getName().c_str(), *wxConvCurrent); | |
9a83f860 | 104 | wxLogInfo(n + wxT(": FAT volumes are limited to 4GB files")); |
48714f74 RN |
105 | } |
106 | ||
107 | // size of the test blocks | |
108 | const size_t size = 0x40; | |
109 | ||
110 | // the test blocks | |
111 | char upto2Gig[size]; | |
112 | char past2Gig[size]; | |
113 | char upto4Gig[size]; | |
114 | char past4Gig[size]; | |
115 | memset(upto2Gig, 'A', size); | |
116 | memset(past2Gig, 'B', size); | |
117 | memset(upto4Gig, 'X', size); | |
118 | memset(past4Gig, 'Y', size); | |
119 | ||
120 | wxFileOffset pos; | |
121 | ||
122 | // write a large file | |
123 | { | |
124 | auto_ptr<wxOutputStream> out(MakeOutStream(tmpfile.m_name)); | |
43b2d5e7 | 125 | |
48714f74 RN |
126 | // write 'A's at [ 0x7fffffbf, 0x7fffffff [ |
127 | pos = 0x7fffffff - size; | |
128 | CPPUNIT_ASSERT(out->SeekO(pos) == pos); | |
129 | CPPUNIT_ASSERT(out->Write(upto2Gig, size).LastWrite() == size); | |
130 | pos += size; | |
131 | ||
132 | if (haveLFS) { | |
133 | // write 'B's at [ 0x7fffffff, 0x8000003f [ | |
134 | CPPUNIT_ASSERT(out->Write(past2Gig, size).LastWrite() == size); | |
135 | pos += size; | |
136 | CPPUNIT_ASSERT(out->TellO() == pos); | |
137 | ||
138 | // write 'X's at [ 0xffffffbf, 0xffffffff [ | |
139 | pos = 0xffffffff - size; | |
140 | CPPUNIT_ASSERT(out->SeekO(pos) == pos); | |
141 | CPPUNIT_ASSERT(out->Write(upto4Gig, size).LastWrite() == size); | |
142 | pos += size; | |
143 | ||
144 | if (!fourGigLimit) { | |
145 | // write 'Y's at [ 0xffffffff, 0x10000003f [ | |
146 | CPPUNIT_ASSERT(out->Write(past4Gig, size).LastWrite() == size); | |
147 | pos += size; | |
148 | } | |
149 | } | |
150 | ||
151 | // check the file seems to be the right length | |
152 | CPPUNIT_ASSERT(out->TellO() == pos); | |
153 | CPPUNIT_ASSERT(out->GetLength() == pos); | |
154 | } | |
155 | ||
156 | // read the large file back | |
157 | { | |
158 | auto_ptr<wxInputStream> in(MakeInStream(tmpfile.m_name)); | |
159 | char buf[size]; | |
160 | ||
161 | if (haveLFS) { | |
162 | CPPUNIT_ASSERT(in->GetLength() == pos); | |
163 | pos = 0xffffffff; | |
164 | ||
165 | if (!fourGigLimit) { | |
166 | CPPUNIT_ASSERT(in->GetLength() > pos); | |
167 | ||
168 | // read back the 'Y's at [ 0xffffffff, 0x10000003f [ | |
169 | CPPUNIT_ASSERT(in->SeekI(pos) == pos); | |
170 | CPPUNIT_ASSERT(in->Read(buf, size).LastRead() == size); | |
171 | CPPUNIT_ASSERT(memcmp(buf, past4Gig, size) == 0); | |
172 | ||
173 | CPPUNIT_ASSERT(in->TellI() == in->GetLength()); | |
174 | } | |
175 | ||
176 | // read back the 'X's at [ 0xffffffbf, 0xffffffff [ | |
177 | pos -= size; | |
178 | CPPUNIT_ASSERT(in->SeekI(pos) == pos); | |
179 | CPPUNIT_ASSERT(in->Read(buf, size).LastRead() == size); | |
180 | CPPUNIT_ASSERT(memcmp(buf, upto4Gig, size) == 0); | |
181 | pos += size; | |
182 | CPPUNIT_ASSERT(in->TellI() == pos); | |
183 | ||
184 | // read back the 'B's at [ 0x7fffffff, 0x8000003f [ | |
185 | pos = 0x7fffffff; | |
186 | CPPUNIT_ASSERT(in->SeekI(pos) == pos); | |
187 | CPPUNIT_ASSERT(in->Read(buf, size).LastRead() == size); | |
188 | CPPUNIT_ASSERT(memcmp(buf, past2Gig, size) == 0); | |
189 | } | |
190 | else { | |
191 | CPPUNIT_ASSERT(in->GetLength() == 0x7fffffff); | |
192 | pos = 0x7fffffff; | |
193 | } | |
194 | ||
195 | // read back the 'A's at [ 0x7fffffbf, 0x7fffffff [ | |
196 | pos -= size; | |
197 | CPPUNIT_ASSERT(in->SeekI(pos) == pos); | |
198 | CPPUNIT_ASSERT(in->Read(buf, size).LastRead() == size); | |
199 | CPPUNIT_ASSERT(memcmp(buf, upto2Gig, size) == 0); | |
200 | pos += size; | |
201 | CPPUNIT_ASSERT(in->TellI() == pos); | |
202 | } | |
203 | } | |
204 | ||
205 | ||
206 | /////////////////////////////////////////////////////////////////////////////// | |
207 | // wxFile test case | |
208 | ||
209 | class LargeFileTest_wxFile : public LargeFileTest | |
210 | { | |
211 | public: | |
212 | LargeFileTest_wxFile() : LargeFileTest("wxFile streams") { } | |
213 | ||
214 | protected: | |
215 | wxInputStream *MakeInStream(const wxString& name) const; | |
216 | wxOutputStream *MakeOutStream(const wxString& name) const; | |
217 | bool HasLFS() const { return (wxFileOffset)0xffffffff > 0; } | |
218 | }; | |
219 | ||
220 | wxInputStream *LargeFileTest_wxFile::MakeInStream(const wxString& name) const | |
221 | { | |
222 | auto_ptr<wxFileInputStream> in(new wxFileInputStream(name)); | |
223 | CPPUNIT_ASSERT(in->Ok()); | |
224 | return in.release(); | |
225 | } | |
226 | ||
227 | wxOutputStream *LargeFileTest_wxFile::MakeOutStream(const wxString& name) const | |
228 | { | |
229 | wxFile file(name, wxFile::write); | |
230 | CPPUNIT_ASSERT(file.IsOpened()); | |
231 | int fd = file.fd(); | |
232 | file.Detach(); | |
233 | MakeSparse(name, fd); | |
234 | return new wxFileOutputStream(fd); | |
235 | } | |
236 | ||
237 | ||
238 | /////////////////////////////////////////////////////////////////////////////// | |
239 | // wxFFile test case | |
240 | ||
241 | class LargeFileTest_wxFFile : public LargeFileTest | |
242 | { | |
243 | public: | |
244 | LargeFileTest_wxFFile() : LargeFileTest("wxFFile streams") { } | |
245 | ||
246 | protected: | |
247 | wxInputStream *MakeInStream(const wxString& name) const; | |
248 | wxOutputStream *MakeOutStream(const wxString& name) const; | |
249 | bool HasLFS() const; | |
250 | }; | |
251 | ||
252 | wxInputStream *LargeFileTest_wxFFile::MakeInStream(const wxString& name) const | |
253 | { | |
254 | auto_ptr<wxFFileInputStream> in(new wxFFileInputStream(name)); | |
255 | CPPUNIT_ASSERT(in->Ok()); | |
256 | return in.release(); | |
257 | } | |
258 | ||
259 | wxOutputStream *LargeFileTest_wxFFile::MakeOutStream(const wxString& name) const | |
260 | { | |
9a83f860 | 261 | wxFFile file(name, wxT("w")); |
48714f74 RN |
262 | CPPUNIT_ASSERT(file.IsOpened()); |
263 | FILE *fp = file.fp(); | |
264 | file.Detach(); | |
265 | MakeSparse(name, fileno(fp)); | |
266 | return new wxFFileOutputStream(fp); | |
267 | } | |
268 | ||
269 | bool LargeFileTest_wxFFile::HasLFS() const | |
270 | { | |
0c094518 | 271 | #ifdef HAVE_FSEEKO |
48714f74 RN |
272 | return (wxFileOffset)0xffffffff > 0; |
273 | #else | |
274 | return false; | |
275 | #endif | |
276 | } | |
277 | ||
278 | ||
279 | /////////////////////////////////////////////////////////////////////////////// | |
280 | // The suite | |
281 | ||
282 | class largeFile : public CppUnit::TestSuite | |
283 | { | |
284 | public: | |
285 | largeFile() : CppUnit::TestSuite("largeFile") { } | |
286 | ||
287 | static CppUnit::Test *suite(); | |
288 | }; | |
289 | ||
290 | CppUnit::Test *largeFile::suite() | |
291 | { | |
292 | largeFile *suite = new largeFile; | |
293 | ||
294 | suite->addTest(new LargeFileTest_wxFile); | |
295 | suite->addTest(new LargeFileTest_wxFFile); | |
296 | ||
297 | return suite; | |
298 | } | |
299 | ||
300 | ||
301 | /////////////////////////////////////////////////////////////////////////////// | |
302 | // Implement the helpers | |
303 | // | |
304 | // Ideally these tests will be part of the default suite so that regressions | |
305 | // are picked up. However this is only possible when sparse files are | |
306 | // supported otherwise the tests require too much disk space. | |
307 | // | |
086d7f31 RN |
308 | // On unix, most filesystems support sparse files, though not all. So for now |
309 | // I'm not assuming sparse file support on unix. On Windows it's possible to | |
310 | // test, and sparse files should be available on Win 5+ with NTFS. | |
48714f74 RN |
311 | |
312 | #ifdef __WXMSW__ | |
313 | ||
314 | #ifndef FILE_SUPPORTS_SPARSE_FILES | |
315 | #define FILE_SUPPORTS_SPARSE_FILES 0x00000040 | |
316 | #endif | |
317 | ||
318 | #ifndef FSCTL_SET_SPARSE | |
319 | ||
43b2d5e7 | 320 | # ifndef FILE_SPECIAL_ACCESS |
48714f74 RN |
321 | # define FILE_SPECIAL_ACCESS FILE_ANY_ACCESS |
322 | # endif | |
323 | # define FSCTL_SET_SPARSE CTL_CODE(FILE_DEVICE_FILE_SYSTEM, 49, \ | |
324 | METHOD_BUFFERED, FILE_SPECIAL_ACCESS) | |
325 | #endif | |
326 | ||
327 | static DWORD volumeFlags; | |
328 | static wxChar volumeType[64]; | |
329 | static bool volumeInfoInit; | |
330 | ||
331 | void GetVolumeInfo(const wxString& path) | |
332 | { | |
333 | // extract the volume 'C:\' or '\\tooter\share\' from the path | |
334 | wxString vol; | |
43b2d5e7 | 335 | |
9a83f860 | 336 | if (path.substr(1, 2) == wxT(":\\")) { |
48714f74 RN |
337 | vol = path.substr(0, 3); |
338 | } else { | |
9a83f860 VZ |
339 | if (path.substr(0, 2) == wxT("\\\\")) { |
340 | size_t i = path.find(wxT('\\'), 2); | |
48714f74 RN |
341 | |
342 | if (i != wxString::npos && i > 2) { | |
9a83f860 | 343 | size_t j = path.find(wxT('\\'), ++i); |
48714f74 RN |
344 | |
345 | if (j != i) | |
9a83f860 | 346 | vol = path.substr(0, j) + wxT("\\"); |
48714f74 RN |
347 | } |
348 | } | |
349 | } | |
350 | ||
351 | // NULL means the current volume | |
90adb904 | 352 | const wxChar *pVol = vol.empty() ? (const wxChar *)NULL |
48eba629 | 353 | : vol.c_str(); |
48714f74 RN |
354 | |
355 | if (!::GetVolumeInformation(pVol, NULL, 0, NULL, NULL, | |
43b2d5e7 | 356 | &volumeFlags, |
48714f74 RN |
357 | volumeType, |
358 | WXSIZEOF(volumeType))) | |
43b2d5e7 | 359 | { |
9a83f860 | 360 | wxLogSysError(wxT("GetVolumeInformation() failed")); |
43b2d5e7 | 361 | } |
48714f74 RN |
362 | |
363 | volumeInfoInit = true; | |
364 | } | |
365 | ||
366 | bool IsFAT(const wxString& path) | |
367 | { | |
368 | if (!volumeInfoInit) | |
369 | GetVolumeInfo(path); | |
9a83f860 | 370 | return wxString(volumeType).Upper().find(wxT("FAT")) != wxString::npos; |
48714f74 RN |
371 | } |
372 | ||
373 | void MakeSparse(const wxString& path, int fd) | |
374 | { | |
375 | DWORD cb; | |
376 | ||
377 | if (!volumeInfoInit) | |
378 | GetVolumeInfo(path); | |
43b2d5e7 | 379 | |
48714f74 RN |
380 | if ((volumeFlags & FILE_SUPPORTS_SPARSE_FILES) != 0) |
381 | if (!::DeviceIoControl((HANDLE)_get_osfhandle(fd), | |
382 | FSCTL_SET_SPARSE, | |
383 | NULL, 0, NULL, 0, &cb, NULL)) | |
086d7f31 | 384 | volumeFlags &= ~FILE_SUPPORTS_SPARSE_FILES; |
48714f74 RN |
385 | } |
386 | ||
387 | CppUnit::Test* GetlargeFileSuite() | |
388 | { | |
389 | if (!volumeInfoInit) { | |
390 | wxFile file; | |
9a83f860 | 391 | wxString path = wxFileName::CreateTempFileName(wxT("wxlfs-"), &file); |
48714f74 RN |
392 | MakeSparse(path, file.fd()); |
393 | wxRemoveFile(path); | |
394 | } | |
395 | ||
396 | if ((volumeFlags & FILE_SUPPORTS_SPARSE_FILES) != 0) | |
397 | return largeFile::suite(); | |
398 | else | |
399 | return NULL; | |
400 | } | |
401 | ||
402 | #else // __WXMSW__ | |
403 | ||
404 | bool IsFAT(const wxString& WXUNUSED(path)) { return false; } | |
405 | void MakeSparse(const wxString& WXUNUSED(path), int WXUNUSED(fd)) { } | |
406 | ||
48714f74 | 407 | CppUnit::Test* GetlargeFileSuite() { return NULL; } |
48714f74 RN |
408 | |
409 | #endif // __WXMSW__ | |
410 | ||
411 | CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(largeFile, "largeFile"); | |
412 | CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(largeFile, "Streams.largeFile"); |