2005-02-04 04:43:47 -05:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Name: tests/streams/largefile.cpp
|
|
|
|
// Purpose: Tests for large file support
|
|
|
|
// Author: Mike Wetherell
|
|
|
|
// Copyright: (c) 2004 Mike Wetherell
|
2010-07-13 09:29:13 -04:00
|
|
|
// Licence: wxWindows licence
|
2005-02-04 04:43:47 -05:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
//
|
2005-02-04 10:15:20 -05:00
|
|
|
// We're interested in what happens around offsets 0x7fffffff and 0xffffffff
|
2005-02-04 04:43:47 -05:00
|
|
|
// so the test writes a small chunk of test data just before and just after
|
|
|
|
// these offsets, then reads them back.
|
|
|
|
//
|
|
|
|
// The tests can be run with:
|
|
|
|
//
|
|
|
|
// test --verbose largeFile
|
|
|
|
//
|
2009-12-30 12:44:09 -05:00
|
|
|
// On systems supporting sparse files they will also be registered in the
|
|
|
|
// Streams subsuite so that they run by default.
|
2005-02-04 04:43:47 -05:00
|
|
|
//
|
|
|
|
|
|
|
|
// For compilers that support precompilation, includes "wx/wx.h".
|
|
|
|
#include "testprec.h"
|
|
|
|
|
|
|
|
|
|
|
|
// for all others, include the necessary headers
|
|
|
|
#ifndef WX_PRECOMP
|
|
|
|
#include "wx/wx.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "wx/filename.h"
|
2017-10-31 16:14:05 -04:00
|
|
|
#include "wx/scopedptr.h"
|
2005-02-04 04:43:47 -05:00
|
|
|
#include "wx/wfstream.h"
|
|
|
|
|
2012-03-04 15:31:42 -05:00
|
|
|
#ifdef __WINDOWS__
|
2009-06-07 08:41:52 -04:00
|
|
|
#include "wx/msw/wrapwin.h"
|
|
|
|
#ifdef __VISUALC__
|
|
|
|
// 'nonstandard extension used : nameless struct/union' occurs inside
|
|
|
|
// winioctl.h
|
|
|
|
#pragma warning(disable:4201)
|
|
|
|
#endif
|
|
|
|
#include <winioctl.h>
|
|
|
|
#ifdef __VISUALC__
|
|
|
|
#pragma warning(default:4201)
|
|
|
|
#endif
|
2005-02-04 04:43:47 -05:00
|
|
|
#endif
|
|
|
|
|
2009-06-06 19:18:52 -04:00
|
|
|
#ifdef __VISUALC__
|
|
|
|
#define fileno _fileno
|
|
|
|
#endif
|
|
|
|
|
2005-02-04 04:43:47 -05:00
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Helpers
|
|
|
|
|
|
|
|
bool IsFAT(const wxString& path);
|
|
|
|
void MakeSparse(const wxString& path, int fd);
|
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Base class for the test cases - common code
|
|
|
|
|
|
|
|
class LargeFileTest : public CppUnit::TestCase
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
LargeFileTest(std::string name) : CppUnit::TestCase(name) { }
|
|
|
|
virtual ~LargeFileTest() { }
|
|
|
|
|
|
|
|
protected:
|
2018-09-21 13:46:49 -04:00
|
|
|
void runTest() wxOVERRIDE;
|
2005-02-04 04:43:47 -05:00
|
|
|
|
|
|
|
virtual wxInputStream *MakeInStream(const wxString& name) const = 0;
|
|
|
|
virtual wxOutputStream *MakeOutStream(const wxString& name) const = 0;
|
|
|
|
virtual bool HasLFS() const = 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
void LargeFileTest::runTest()
|
|
|
|
{
|
|
|
|
// self deleting temp file
|
|
|
|
struct TmpFile {
|
|
|
|
TmpFile() : m_name(wxFileName::CreateTempFileName(wxT("wxlfs-"))) { }
|
|
|
|
~TmpFile() { if (!m_name.empty()) wxRemoveFile(m_name); }
|
|
|
|
wxString m_name;
|
|
|
|
} tmpfile;
|
|
|
|
|
|
|
|
CPPUNIT_ASSERT(!tmpfile.m_name.empty());
|
|
|
|
|
|
|
|
bool haveLFS = true;
|
|
|
|
bool fourGigLimit = false;
|
|
|
|
|
|
|
|
if (!HasLFS()) {
|
|
|
|
haveLFS = false;
|
|
|
|
wxString n(getName().c_str(), *wxConvCurrent);
|
|
|
|
wxLogInfo(n + wxT(": No large file support, testing up to 2GB only"));
|
|
|
|
}
|
|
|
|
else if (IsFAT(tmpfile.m_name)) {
|
|
|
|
fourGigLimit = true;
|
|
|
|
wxString n(getName().c_str(), *wxConvCurrent);
|
|
|
|
wxLogInfo(n + wxT(": FAT volumes are limited to 4GB files"));
|
|
|
|
}
|
|
|
|
|
|
|
|
// size of the test blocks
|
|
|
|
const size_t size = 0x40;
|
|
|
|
|
|
|
|
// the test blocks
|
|
|
|
char upto2Gig[size];
|
|
|
|
char past2Gig[size];
|
|
|
|
char upto4Gig[size];
|
|
|
|
char past4Gig[size];
|
|
|
|
memset(upto2Gig, 'A', size);
|
|
|
|
memset(past2Gig, 'B', size);
|
|
|
|
memset(upto4Gig, 'X', size);
|
|
|
|
memset(past4Gig, 'Y', size);
|
|
|
|
|
|
|
|
wxFileOffset pos;
|
|
|
|
|
|
|
|
// write a large file
|
|
|
|
{
|
2017-10-31 16:14:05 -04:00
|
|
|
wxScopedPtr<wxOutputStream> out(MakeOutStream(tmpfile.m_name));
|
2009-07-20 12:47:54 -04:00
|
|
|
|
2005-02-04 04:43:47 -05:00
|
|
|
// write 'A's at [ 0x7fffffbf, 0x7fffffff [
|
|
|
|
pos = 0x7fffffff - size;
|
|
|
|
CPPUNIT_ASSERT(out->SeekO(pos) == pos);
|
|
|
|
CPPUNIT_ASSERT(out->Write(upto2Gig, size).LastWrite() == size);
|
|
|
|
pos += size;
|
|
|
|
|
|
|
|
if (haveLFS) {
|
|
|
|
// write 'B's at [ 0x7fffffff, 0x8000003f [
|
|
|
|
CPPUNIT_ASSERT(out->Write(past2Gig, size).LastWrite() == size);
|
|
|
|
pos += size;
|
|
|
|
CPPUNIT_ASSERT(out->TellO() == pos);
|
|
|
|
|
|
|
|
// write 'X's at [ 0xffffffbf, 0xffffffff [
|
|
|
|
pos = 0xffffffff - size;
|
|
|
|
CPPUNIT_ASSERT(out->SeekO(pos) == pos);
|
|
|
|
CPPUNIT_ASSERT(out->Write(upto4Gig, size).LastWrite() == size);
|
|
|
|
pos += size;
|
|
|
|
|
|
|
|
if (!fourGigLimit) {
|
|
|
|
// write 'Y's at [ 0xffffffff, 0x10000003f [
|
|
|
|
CPPUNIT_ASSERT(out->Write(past4Gig, size).LastWrite() == size);
|
|
|
|
pos += size;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// check the file seems to be the right length
|
|
|
|
CPPUNIT_ASSERT(out->TellO() == pos);
|
|
|
|
CPPUNIT_ASSERT(out->GetLength() == pos);
|
|
|
|
}
|
|
|
|
|
|
|
|
// read the large file back
|
|
|
|
{
|
2017-10-31 16:14:05 -04:00
|
|
|
wxScopedPtr<wxInputStream> in(MakeInStream(tmpfile.m_name));
|
2005-02-04 04:43:47 -05:00
|
|
|
char buf[size];
|
|
|
|
|
|
|
|
if (haveLFS) {
|
|
|
|
CPPUNIT_ASSERT(in->GetLength() == pos);
|
|
|
|
pos = 0xffffffff;
|
|
|
|
|
|
|
|
if (!fourGigLimit) {
|
|
|
|
CPPUNIT_ASSERT(in->GetLength() > pos);
|
|
|
|
|
|
|
|
// read back the 'Y's at [ 0xffffffff, 0x10000003f [
|
|
|
|
CPPUNIT_ASSERT(in->SeekI(pos) == pos);
|
|
|
|
CPPUNIT_ASSERT(in->Read(buf, size).LastRead() == size);
|
|
|
|
CPPUNIT_ASSERT(memcmp(buf, past4Gig, size) == 0);
|
|
|
|
|
|
|
|
CPPUNIT_ASSERT(in->TellI() == in->GetLength());
|
|
|
|
}
|
|
|
|
|
|
|
|
// read back the 'X's at [ 0xffffffbf, 0xffffffff [
|
|
|
|
pos -= size;
|
|
|
|
CPPUNIT_ASSERT(in->SeekI(pos) == pos);
|
|
|
|
CPPUNIT_ASSERT(in->Read(buf, size).LastRead() == size);
|
|
|
|
CPPUNIT_ASSERT(memcmp(buf, upto4Gig, size) == 0);
|
|
|
|
pos += size;
|
|
|
|
CPPUNIT_ASSERT(in->TellI() == pos);
|
|
|
|
|
|
|
|
// read back the 'B's at [ 0x7fffffff, 0x8000003f [
|
|
|
|
pos = 0x7fffffff;
|
|
|
|
CPPUNIT_ASSERT(in->SeekI(pos) == pos);
|
|
|
|
CPPUNIT_ASSERT(in->Read(buf, size).LastRead() == size);
|
|
|
|
CPPUNIT_ASSERT(memcmp(buf, past2Gig, size) == 0);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
CPPUNIT_ASSERT(in->GetLength() == 0x7fffffff);
|
|
|
|
pos = 0x7fffffff;
|
|
|
|
}
|
|
|
|
|
|
|
|
// read back the 'A's at [ 0x7fffffbf, 0x7fffffff [
|
|
|
|
pos -= size;
|
|
|
|
CPPUNIT_ASSERT(in->SeekI(pos) == pos);
|
|
|
|
CPPUNIT_ASSERT(in->Read(buf, size).LastRead() == size);
|
|
|
|
CPPUNIT_ASSERT(memcmp(buf, upto2Gig, size) == 0);
|
|
|
|
pos += size;
|
|
|
|
CPPUNIT_ASSERT(in->TellI() == pos);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// wxFile test case
|
|
|
|
|
|
|
|
class LargeFileTest_wxFile : public LargeFileTest
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
LargeFileTest_wxFile() : LargeFileTest("wxFile streams") { }
|
|
|
|
|
|
|
|
protected:
|
2018-09-21 13:46:49 -04:00
|
|
|
wxInputStream *MakeInStream(const wxString& name) const wxOVERRIDE;
|
|
|
|
wxOutputStream *MakeOutStream(const wxString& name) const wxOVERRIDE;
|
|
|
|
bool HasLFS() const wxOVERRIDE { return (wxFileOffset)0xffffffff > 0; }
|
2005-02-04 04:43:47 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
wxInputStream *LargeFileTest_wxFile::MakeInStream(const wxString& name) const
|
|
|
|
{
|
2017-10-31 16:14:05 -04:00
|
|
|
wxScopedPtr<wxFileInputStream> in(new wxFileInputStream(name));
|
2011-05-03 12:29:04 -04:00
|
|
|
CPPUNIT_ASSERT(in->IsOk());
|
2005-02-04 04:43:47 -05:00
|
|
|
return in.release();
|
|
|
|
}
|
|
|
|
|
|
|
|
wxOutputStream *LargeFileTest_wxFile::MakeOutStream(const wxString& name) const
|
|
|
|
{
|
|
|
|
wxFile file(name, wxFile::write);
|
|
|
|
CPPUNIT_ASSERT(file.IsOpened());
|
|
|
|
int fd = file.fd();
|
|
|
|
file.Detach();
|
|
|
|
MakeSparse(name, fd);
|
|
|
|
return new wxFileOutputStream(fd);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// wxFFile test case
|
|
|
|
|
|
|
|
class LargeFileTest_wxFFile : public LargeFileTest
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
LargeFileTest_wxFFile() : LargeFileTest("wxFFile streams") { }
|
|
|
|
|
|
|
|
protected:
|
2018-09-21 13:46:49 -04:00
|
|
|
wxInputStream *MakeInStream(const wxString& name) const wxOVERRIDE;
|
|
|
|
wxOutputStream *MakeOutStream(const wxString& name) const wxOVERRIDE;
|
|
|
|
bool HasLFS() const wxOVERRIDE;
|
2005-02-04 04:43:47 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
wxInputStream *LargeFileTest_wxFFile::MakeInStream(const wxString& name) const
|
|
|
|
{
|
2017-10-31 16:14:05 -04:00
|
|
|
wxScopedPtr<wxFFileInputStream> in(new wxFFileInputStream(name));
|
2011-05-03 12:29:04 -04:00
|
|
|
CPPUNIT_ASSERT(in->IsOk());
|
2005-02-04 04:43:47 -05:00
|
|
|
return in.release();
|
|
|
|
}
|
|
|
|
|
|
|
|
wxOutputStream *LargeFileTest_wxFFile::MakeOutStream(const wxString& name) const
|
|
|
|
{
|
|
|
|
wxFFile file(name, wxT("w"));
|
|
|
|
CPPUNIT_ASSERT(file.IsOpened());
|
|
|
|
FILE *fp = file.fp();
|
|
|
|
file.Detach();
|
|
|
|
MakeSparse(name, fileno(fp));
|
|
|
|
return new wxFFileOutputStream(fp);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool LargeFileTest_wxFFile::HasLFS() const
|
|
|
|
{
|
2009-12-22 16:22:59 -05:00
|
|
|
#ifdef wxHAS_LARGE_FFILES
|
|
|
|
return true;
|
2005-02-04 04:43:47 -05:00
|
|
|
#else
|
|
|
|
return false;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// The suite
|
|
|
|
|
|
|
|
class largeFile : public CppUnit::TestSuite
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
largeFile() : CppUnit::TestSuite("largeFile") { }
|
|
|
|
|
|
|
|
static CppUnit::Test *suite();
|
|
|
|
};
|
|
|
|
|
|
|
|
CppUnit::Test *largeFile::suite()
|
|
|
|
{
|
|
|
|
largeFile *suite = new largeFile;
|
|
|
|
|
|
|
|
suite->addTest(new LargeFileTest_wxFile);
|
|
|
|
suite->addTest(new LargeFileTest_wxFFile);
|
|
|
|
|
|
|
|
return suite;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Implement the helpers
|
|
|
|
//
|
|
|
|
// Ideally these tests will be part of the default suite so that regressions
|
|
|
|
// are picked up. However this is only possible when sparse files are
|
|
|
|
// supported otherwise the tests require too much disk space.
|
|
|
|
|
2012-03-04 15:31:42 -05:00
|
|
|
#ifdef __WINDOWS__
|
2005-02-04 04:43:47 -05:00
|
|
|
|
|
|
|
#ifndef FILE_SUPPORTS_SPARSE_FILES
|
|
|
|
#define FILE_SUPPORTS_SPARSE_FILES 0x00000040
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef FSCTL_SET_SPARSE
|
|
|
|
|
2009-07-20 12:47:54 -04:00
|
|
|
# ifndef FILE_SPECIAL_ACCESS
|
2005-02-04 04:43:47 -05:00
|
|
|
# define FILE_SPECIAL_ACCESS FILE_ANY_ACCESS
|
|
|
|
# endif
|
|
|
|
# define FSCTL_SET_SPARSE CTL_CODE(FILE_DEVICE_FILE_SYSTEM, 49, \
|
|
|
|
METHOD_BUFFERED, FILE_SPECIAL_ACCESS)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static DWORD volumeFlags;
|
|
|
|
static wxChar volumeType[64];
|
|
|
|
static bool volumeInfoInit;
|
|
|
|
|
|
|
|
void GetVolumeInfo(const wxString& path)
|
|
|
|
{
|
|
|
|
// extract the volume 'C:\' or '\\tooter\share\' from the path
|
|
|
|
wxString vol;
|
2009-07-20 12:47:54 -04:00
|
|
|
|
2005-02-04 04:43:47 -05:00
|
|
|
if (path.substr(1, 2) == wxT(":\\")) {
|
|
|
|
vol = path.substr(0, 3);
|
|
|
|
} else {
|
|
|
|
if (path.substr(0, 2) == wxT("\\\\")) {
|
|
|
|
size_t i = path.find(wxT('\\'), 2);
|
|
|
|
|
|
|
|
if (i != wxString::npos && i > 2) {
|
|
|
|
size_t j = path.find(wxT('\\'), ++i);
|
|
|
|
|
|
|
|
if (j != i)
|
|
|
|
vol = path.substr(0, j) + wxT("\\");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// NULL means the current volume
|
2007-08-22 11:19:06 -04:00
|
|
|
const wxChar *pVol = vol.empty() ? (const wxChar *)NULL
|
2007-03-18 10:29:30 -04:00
|
|
|
: vol.c_str();
|
2005-02-04 04:43:47 -05:00
|
|
|
|
|
|
|
if (!::GetVolumeInformation(pVol, NULL, 0, NULL, NULL,
|
2009-07-20 12:47:54 -04:00
|
|
|
&volumeFlags,
|
2005-02-04 04:43:47 -05:00
|
|
|
volumeType,
|
|
|
|
WXSIZEOF(volumeType)))
|
2009-07-20 12:47:54 -04:00
|
|
|
{
|
2005-02-04 04:43:47 -05:00
|
|
|
wxLogSysError(wxT("GetVolumeInformation() failed"));
|
2009-07-20 12:47:54 -04:00
|
|
|
}
|
2005-02-04 04:43:47 -05:00
|
|
|
|
|
|
|
volumeInfoInit = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool IsFAT(const wxString& path)
|
|
|
|
{
|
|
|
|
if (!volumeInfoInit)
|
|
|
|
GetVolumeInfo(path);
|
|
|
|
return wxString(volumeType).Upper().find(wxT("FAT")) != wxString::npos;
|
|
|
|
}
|
|
|
|
|
|
|
|
void MakeSparse(const wxString& path, int fd)
|
|
|
|
{
|
|
|
|
DWORD cb;
|
|
|
|
|
|
|
|
if (!volumeInfoInit)
|
|
|
|
GetVolumeInfo(path);
|
2009-07-20 12:47:54 -04:00
|
|
|
|
2005-02-04 04:43:47 -05:00
|
|
|
if ((volumeFlags & FILE_SUPPORTS_SPARSE_FILES) != 0)
|
|
|
|
if (!::DeviceIoControl((HANDLE)_get_osfhandle(fd),
|
|
|
|
FSCTL_SET_SPARSE,
|
|
|
|
NULL, 0, NULL, 0, &cb, NULL))
|
2005-02-04 10:15:20 -05:00
|
|
|
volumeFlags &= ~FILE_SUPPORTS_SPARSE_FILES;
|
2005-02-04 04:43:47 -05:00
|
|
|
}
|
|
|
|
|
2009-12-30 12:44:09 -05:00
|
|
|
// return the suite if sparse files are supported, otherwise return NULL
|
|
|
|
//
|
2005-02-04 04:43:47 -05:00
|
|
|
CppUnit::Test* GetlargeFileSuite()
|
|
|
|
{
|
|
|
|
if (!volumeInfoInit) {
|
2009-12-30 12:44:09 -05:00
|
|
|
wxString path;
|
|
|
|
{
|
|
|
|
wxFile file;
|
|
|
|
path = wxFileName::CreateTempFileName(wxT("wxlfs-"), &file);
|
|
|
|
MakeSparse(path, file.fd());
|
|
|
|
}
|
2005-02-04 04:43:47 -05:00
|
|
|
wxRemoveFile(path);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((volumeFlags & FILE_SUPPORTS_SPARSE_FILES) != 0)
|
|
|
|
return largeFile::suite();
|
|
|
|
else
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2012-03-04 15:31:42 -05:00
|
|
|
#else // __WINDOWS__
|
2005-02-04 04:43:47 -05:00
|
|
|
|
|
|
|
bool IsFAT(const wxString& WXUNUSED(path)) { return false; }
|
|
|
|
void MakeSparse(const wxString& WXUNUSED(path), int WXUNUSED(fd)) { }
|
|
|
|
|
2009-12-30 12:44:09 -05:00
|
|
|
// return the suite if sparse files are supported, otherwise return NULL
|
|
|
|
//
|
|
|
|
CppUnit::Test* GetlargeFileSuite()
|
|
|
|
{
|
|
|
|
wxString path;
|
|
|
|
struct stat st1, st2;
|
|
|
|
memset(&st1, 0, sizeof(st1));
|
|
|
|
memset(&st2, 0, sizeof(st2));
|
|
|
|
|
|
|
|
{
|
|
|
|
wxFile file;
|
|
|
|
path = wxFileName::CreateTempFileName(wxT("wxlfs-"), &file);
|
|
|
|
|
|
|
|
fstat(file.fd(), &st1);
|
|
|
|
file.Seek(st1.st_blksize);
|
|
|
|
file.Write("x", 1);
|
|
|
|
fstat(file.fd(), &st1);
|
|
|
|
|
|
|
|
file.Seek(0);
|
|
|
|
file.Write("x", 1);
|
|
|
|
fstat(file.fd(), &st2);
|
|
|
|
}
|
|
|
|
|
|
|
|
wxRemoveFile(path);
|
|
|
|
|
|
|
|
if (st1.st_blocks != st2.st_blocks)
|
|
|
|
return largeFile::suite();
|
|
|
|
else
|
|
|
|
return NULL;
|
|
|
|
}
|
2005-02-04 04:43:47 -05:00
|
|
|
|
2012-03-04 15:31:42 -05:00
|
|
|
#endif // __WINDOWS__
|
2005-02-04 04:43:47 -05:00
|
|
|
|
|
|
|
CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(largeFile, "Streams.largeFile");
|