Wrapped fopen/close/read/write functions inside a simple "IOFile" class. Reading, writing, and error checking became simpler in most cases. It should be near impossible to forget to close a file now that the destructor takes care of it. (I hope this fixes Issue 3635) I have tested the functionality of most things, but it is possible I broke something. :p

git-svn-id: https://dolphin-emu.googlecode.com/svn/trunk@7328 8ced0084-cf51-0410-be5f-012b33b47a6e
This commit is contained in:
Jordan Woyak
2011-03-11 10:21:46 +00:00
parent 4f69672b2b
commit 59fd1008ca
68 changed files with 1112 additions and 1154 deletions

View File

@ -87,11 +87,10 @@ CBannerLoaderWii::CBannerLoaderWii(DiscIO::IVolume *pVolume)
if (FileSize > 0)
{
m_pBannerFile = new u8[FileSize];
FILE* pFile = fopen(Filename, "rb");
File::IOFile pFile(Filename, "rb");
if (pFile)
{
fread(m_pBannerFile, FileSize, 1, pFile);
fclose(pFile);
pFile.ReadBytes(m_pBannerFile, FileSize);
m_IsValid = true;
}
}

View File

@ -21,15 +21,13 @@
namespace DiscIO
{
CISOFileReader::CISOFileReader(FILE* file__)
CISOFileReader::CISOFileReader(std::FILE* file)
: m_file(file)
{
file_ = file__;
fseek(file_, 0, SEEK_END);
size = ftell(file_);
fseek(file_, 0, SEEK_SET);
m_size = m_file.GetSize();
memset(&header, 0, sizeof(header));
fread(&header, sizeof(header), 1, file_);
m_file.ReadArray(&header, 1);
CISO_Map_t count = 0;
int idx;
@ -41,18 +39,13 @@ CISOFileReader* CISOFileReader::Create(const char* filename)
{
if (IsCISOBlob(filename))
{
FILE* f = fopen(filename, "rb");
return new CISOFileReader(f);
File::IOFile f(filename, "rb");
return new CISOFileReader(f.ReleaseHandle());
}
else
return NULL;
}
CISOFileReader::~CISOFileReader()
{
fclose(file_);
}
bool CISOFileReader::Read(u64 offset, u64 nbytes, u8* out_ptr)
{
u64 bytesRead = 0;
@ -73,9 +66,7 @@ bool CISOFileReader::Read(u64 offset, u64 nbytes, u8* out_ptr)
// calcualte the base address
u64 file_off = CISO_HEAD_SIZE + ciso_map[block] * (u64)header.block_size + data_offset;
if (fseeko(file_, (long)file_off, SEEK_SET) != 0)
return false;
if (fread(out_ptr, 1, bytes_to_read, file_) != bytes_to_read)
if (!(m_file.Seek(file_off, SEEK_SET) && m_file.ReadBytes(out_ptr, bytes_to_read)))
return false;
out_ptr += bytes_to_read;
@ -88,15 +79,10 @@ bool CISOFileReader::Read(u64 offset, u64 nbytes, u8* out_ptr)
bool IsCISOBlob(const char* filename)
{
FILE* f = fopen(filename, "rb");
if (!f)
return false;
File::IOFile f(filename, "rb");
CISO_Head_t header;
fread(&header, sizeof(header), 1, f);
fclose(f);
return (memcmp(header.magic, CISO_MAGIC, sizeof(header.magic)) == 0);
return (f.ReadArray(&header, 1) && (memcmp(header.magic, CISO_MAGIC, sizeof(header.magic)) == 0));
}
} // namespace

View File

@ -19,8 +19,7 @@
#define _CISO_BLOB_H
#include "Blob.h"
#include <cstdio>
#include "FileUtil.h"
#define CISO_MAGIC "CISO"
#define CISO_HEAD_SIZE (0x8000)
@ -32,12 +31,12 @@ namespace DiscIO
bool IsCISOBlob(const char* filename);
// Blocks that won't compress to less than 97% of the original size are stored as-is.
typedef struct CISO_Head_t
struct CISO_Head_t
{
u8 magic[4]; // "CISO"
u32 block_size; // stored as litte endian (not network byte order)
u8 map[CISO_MAP_SIZE]; // 0=unused, 1=used, others=invalid
} CISO_Head_t;
u8 magic[4]; // "CISO"
u32 block_size; // stored as litte endian (not network byte order)
u8 map[CISO_MAP_SIZE]; // 0=unused, 1=used, others=invalid
};
typedef u16 CISO_Map_t;
@ -45,15 +44,15 @@ const CISO_Map_t CISO_UNUSED_BLOCK = (CISO_Map_t)~0;
class CISOFileReader : public IBlobReader
{
FILE* file_;
CISOFileReader(FILE* file__);
s64 size;
File::IOFile m_file;
CISOFileReader(std::FILE* file);
s64 m_size;
public:
static CISOFileReader* Create(const char* filename);
~CISOFileReader();
u64 GetDataSize() const { return size; }
u64 GetRawSize() const { return size; }
u64 GetDataSize() const { return m_size; }
u64 GetRawSize() const { return m_size; }
bool Read(u64 offset, u64 nbytes, u8* out_ptr);
private:

View File

@ -36,17 +36,17 @@ namespace DiscIO
CompressedBlobReader::CompressedBlobReader(const char *filename)
{
file_name = filename;
file = fopen(filename, "rb");
m_file.Open(filename, "rb");
file_size = File::GetSize(filename);
fread(&header, sizeof(CompressedBlobHeader), 1, file);
m_file.ReadArray(&header, 1);
SetSectorSize(header.block_size);
// cache block pointers and hashes
block_pointers = new u64[header.num_blocks];
fread(block_pointers, sizeof(u64), header.num_blocks, file);
m_file.ReadArray(block_pointers, header.num_blocks);
hashes = new u32[header.num_blocks];
fread(hashes, sizeof(u32), header.num_blocks, file);
m_file.ReadArray(hashes, header.num_blocks);
data_offset = (sizeof(CompressedBlobHeader))
+ (sizeof(u64)) * header.num_blocks // skip block pointers
@ -72,8 +72,6 @@ CompressedBlobReader::~CompressedBlobReader()
delete [] zlib_buffer;
delete [] block_pointers;
delete [] hashes;
fclose(file);
file = 0;
}
// IMPORTANT: Calling this function invalidates all earlier pointers gotten from this function.
@ -106,8 +104,8 @@ void CompressedBlobReader::GetBlock(u64 block_num, u8 *out_ptr)
// clear unused part of zlib buffer. maybe this can be deleted when it works fully.
memset(zlib_buffer + comp_block_size, 0, zlib_buffer_size - comp_block_size);
fseeko(file, offset, SEEK_SET);
fread(zlib_buffer, 1, comp_block_size, file);
m_file.Seek(offset, SEEK_SET);
m_file.ReadBytes(zlib_buffer, comp_block_size);
u8* source = zlib_buffer;
u8* dest = out_ptr;
@ -173,16 +171,11 @@ bool CompressFileToBlob(const char* infile, const char* outfile, u32 sub_type,
scrubbing = true;
}
FILE* inf = fopen(infile, "rb");
if (!inf)
return false;
File::IOFile inf(infile, "rb");
File::IOFile f(outfile, "wb");
FILE* f = fopen(outfile, "wb");
if (!f)
{
fclose(inf);
if (!f || !inf)
return false;
}
callback("Files opened, ready to compress.", 0, arg);
@ -201,9 +194,9 @@ bool CompressFileToBlob(const char* infile, const char* outfile, u32 sub_type,
u8* in_buf = new u8[block_size];
// seek past the header (we will write it at the end)
fseeko(f, sizeof(CompressedBlobHeader), SEEK_CUR);
f.Seek(sizeof(CompressedBlobHeader), SEEK_CUR);
// seek past the offset and hash tables (we will write them at the end)
fseeko(f, (sizeof(u64) + sizeof(u32)) * header.num_blocks, SEEK_CUR);
f.Seek((sizeof(u64) + sizeof(u32)) * header.num_blocks, SEEK_CUR);
// Now we are ready to write compressed data!
u64 position = 0;
@ -215,7 +208,7 @@ bool CompressFileToBlob(const char* infile, const char* outfile, u32 sub_type,
{
if (i % progress_monitor == 0)
{
u64 inpos = ftello(inf);
const u64 inpos = inf.Tell();
int ratio = 0;
if (inpos != 0)
ratio = (int)(100 * position / inpos);
@ -229,9 +222,9 @@ bool CompressFileToBlob(const char* infile, const char* outfile, u32 sub_type,
// u64 size = header.block_size;
std::fill(in_buf, in_buf + header.block_size, 0);
if (scrubbing)
DiscScrubber::GetNextBlock(inf, in_buf);
DiscScrubber::GetNextBlock(inf.GetHandle(), in_buf);
else
fread(in_buf, header.block_size, 1, inf);
inf.ReadBytes(in_buf, header.block_size);
z_stream z;
memset(&z, 0, sizeof(z));
z.zalloc = Z_NULL;
@ -256,7 +249,7 @@ bool CompressFileToBlob(const char* infile, const char* outfile, u32 sub_type,
//PanicAlert("%i %i Store %i", i*block_size, position, comp_size);
// let's store uncompressed
offsets[i] |= 0x8000000000000000ULL;
fwrite(in_buf, block_size, 1, f);
f.WriteBytes(in_buf, block_size);
hashes[i] = HashAdler32(in_buf, block_size);
position += block_size;
num_stored++;
@ -265,7 +258,7 @@ bool CompressFileToBlob(const char* infile, const char* outfile, u32 sub_type,
{
// let's store compressed
//PanicAlert("Comp %i to %i", block_size, comp_size);
fwrite(out_buf, comp_size, 1, f);
f.WriteBytes(out_buf, comp_size);
hashes[i] = HashAdler32(out_buf, comp_size);
position += comp_size;
num_compressed++;
@ -277,10 +270,10 @@ bool CompressFileToBlob(const char* infile, const char* outfile, u32 sub_type,
header.compressed_data_size = position;
// Okay, go back and fill in headers
fseeko(f, 0, SEEK_SET);
fwrite(&header, sizeof(header), 1, f);
fwrite(offsets, sizeof(u64), header.num_blocks, f);
fwrite(hashes, sizeof(u32), header.num_blocks, f);
f.Seek(0, SEEK_SET);
f.WriteArray(&header, 1);
f.WriteArray(offsets, header.num_blocks);
f.WriteArray(hashes, header.num_blocks);
cleanup:
// Cleanup
@ -288,8 +281,7 @@ cleanup:
delete[] out_buf;
delete[] offsets;
delete[] hashes;
fclose(f);
fclose(inf);
DiscScrubber::Cleanup();
callback("Done compressing disc image.", 1.0f, arg);
return true;
@ -306,7 +298,7 @@ bool DecompressBlobToFile(const char* infile, const char* outfile, CompressCB ca
CompressedBlobReader* reader = CompressedBlobReader::Create(infile);
if (!reader) return false;
FILE* f = fopen(outfile, "wb");
File::IOFile f(outfile, "wb");
const CompressedBlobHeader &header = reader->GetHeader();
u8* buffer = new u8[header.block_size];
int progress_monitor = max<int>(1, header.num_blocks / 100);
@ -318,19 +310,12 @@ bool DecompressBlobToFile(const char* infile, const char* outfile, CompressCB ca
callback("Unpacking", (float)i / (float)header.num_blocks, arg);
}
reader->Read(i * header.block_size, header.block_size, buffer);
fwrite(buffer, header.block_size, 1, f);
f.WriteBytes(buffer, header.block_size);
}
delete[] buffer;
#ifdef _WIN32
// ector: _chsize sucks, not 64-bit safe
// F|RES: changed to _chsize_s. i think it is 64-bit safe
_chsize_s(_fileno(f), header.data_size);
#else
ftruncate(fileno(f), header.data_size);
#endif
fclose(f);
f.Resize(header.data_size);
delete reader;
@ -339,15 +324,10 @@ bool DecompressBlobToFile(const char* infile, const char* outfile, CompressCB ca
bool IsCompressedBlob(const char* filename)
{
FILE* f = fopen(filename, "rb");
if (!f)
return false;
File::IOFile f(filename, "rb");
CompressedBlobHeader header;
fread(&header, sizeof(header), 1, f);
fclose(f);
return header.magic_cookie == kBlobCookie;
return f.ReadArray(&header, 1) && (header.magic_cookie == kBlobCookie);
}
} // namespace

View File

@ -28,10 +28,10 @@
#ifndef COMPRESSED_BLOB_H_
#define COMPRESSED_BLOB_H_
#include <stdio.h>
#include <string>
#include "Blob.h"
#include "FileUtil.h"
namespace DiscIO
{
@ -73,7 +73,7 @@ private:
u64 *block_pointers;
u32 *hashes;
int data_offset;
FILE *file;
File::IOFile m_file;
u64 file_size;
u8 *zlib_buffer;
int zlib_buffer_size;

View File

@ -56,7 +56,7 @@ DriveReader::DriveReader(const char *drive)
#endif
#else
SectorReader::SetSectorSize(2048);
file_ = fopen(drive, "rb");
file_.Open(drive, "rb");
if (file_)
{
#endif
@ -81,9 +81,7 @@ DriveReader::~DriveReader()
hDisc = INVALID_HANDLE_VALUE;
}
#else
if (file_)
fclose(file_);
file_ = 0;
file_.Close();
#endif
}
@ -100,7 +98,7 @@ DriveReader *DriveReader::Create(const char *drive)
void DriveReader::GetBlock(u64 block_num, u8 *out_ptr)
{
u8 * lpSector = new u8[m_blocksize];
u8* const lpSector = new u8[m_blocksize];
#ifdef _WIN32
u32 NotUsed;
u64 offset = m_blocksize * block_num;
@ -110,8 +108,8 @@ void DriveReader::GetBlock(u64 block_num, u8 *out_ptr)
if (!ReadFile(hDisc, lpSector, m_blocksize, (LPDWORD)&NotUsed, NULL))
PanicAlertT("Disc Read Error");
#else
fseeko(file_, m_blocksize*block_num, SEEK_SET);
fread(lpSector, 1, m_blocksize, file_);
file_.Seek(m_blocksize * block_num, SEEK_SET);
file_.ReadBytes(lpSector, m_blocksize);
#endif
memcpy(out_ptr, lpSector, m_blocksize);
delete[] lpSector;

View File

@ -19,6 +19,7 @@
#define _DRIVE_BLOB_H
#include "Blob.h"
#include "FileUtil.h"
#ifdef _WIN32
#include <windows.h>
@ -39,7 +40,7 @@ private:
PREVENT_MEDIA_REMOVAL pmrLockCDROM;
bool IsOK() {return hDisc != INVALID_HANDLE_VALUE;}
#else
FILE* file_;
File::IOFile file_;
bool IsOK() {return file_ != 0;}
#endif
s64 size;

View File

@ -15,40 +15,30 @@
// Official SVN repository and contact information can be found at
// http://code.google.com/p/dolphin-emu/
#include "Blob.h"
#include "FileBlob.h"
#include "FileUtil.h"
namespace DiscIO
{
PlainFileReader::PlainFileReader(FILE* file__)
PlainFileReader::PlainFileReader(std::FILE* file)
: m_file(file)
{
file_ = file__;
size = File::GetSize(file__);
m_size = m_file.GetSize();
}
PlainFileReader* PlainFileReader::Create(const char* filename)
{
FILE* f = fopen(filename, "rb");
File::IOFile f(filename, "rb");
if (f)
return new PlainFileReader(f);
return new PlainFileReader(f.ReleaseHandle());
else
return NULL;
}
PlainFileReader::~PlainFileReader()
{
fclose(file_);
}
bool PlainFileReader::Read(u64 offset, u64 nbytes, u8* out_ptr)
{
int seekStatus = fseeko(file_, offset, SEEK_SET);
if (seekStatus != 0)
return false;
size_t bytesRead = fread(out_ptr, 1, nbytes, file_);
return bytesRead == nbytes;
m_file.Seek(offset, SEEK_SET);
return m_file.ReadBytes(out_ptr, nbytes);
}
} // namespace

View File

@ -19,23 +19,23 @@
#define _FILE_BLOB_H
#include "Blob.h"
#include <cstdio>
#include "FileUtil.h"
namespace DiscIO
{
class PlainFileReader : public IBlobReader
{
FILE* file_;
PlainFileReader(FILE* file__);
s64 size;
PlainFileReader(std::FILE* file);
File::IOFile m_file;
s64 m_size;
public:
static PlainFileReader* Create(const char* filename);
~PlainFileReader();
u64 GetDataSize() const { return size; }
u64 GetRawSize() const { return size; }
u64 GetDataSize() const { return m_size; }
u64 GetRawSize() const { return m_size; }
bool Read(u64 offset, u64 nbytes, u8* out_ptr);
};

View File

@ -20,7 +20,7 @@
#include "FileHandlerARC.h"
#include "StringUtil.h"
#include "Blob.h"
#include "FileUtil.h"
#define ARC_ID 0x55aa382d
@ -144,16 +144,9 @@ CARCFile::ExportFile(const std::string& _rFullPath, const std::string& _rExportF
return(false);
}
FILE* pFile = fopen(_rExportFilename.c_str(), "wb");
File::IOFile pFile(_rExportFilename, "wb");
if (pFile == NULL)
{
return(false);
}
fwrite(&m_pBuffer[pFileInfo->m_Offset], (size_t) pFileInfo->m_FileSize, 1, pFile);
fclose(pFile);
return(true);
return pFile.WriteBytes(&m_pBuffer[pFileInfo->m_Offset], (size_t) pFileInfo->m_FileSize);
}

View File

@ -101,7 +101,7 @@ bool CFileSystemGCWii::ExportFile(const char* _rFullPath, const char* _rExportFi
u64 remainingSize = pFileInfo->m_FileSize;
u64 fileOffset = pFileInfo->m_Offset;
FILE* f = fopen(_rExportFilename, "wb");
File::IOFile f(_rExportFilename, "wb");
if (!f)
return false;
@ -122,7 +122,7 @@ bool CFileSystemGCWii::ExportFile(const char* _rFullPath, const char* _rExportFi
break;
}
fwrite(buffer, readSize, 1, f);
f.WriteBytes(buffer, readSize);
remainingSize -= readSize;
fileOffset += readSize;
@ -130,8 +130,6 @@ bool CFileSystemGCWii::ExportFile(const char* _rFullPath, const char* _rExportFi
delete[] buffer;
}
fclose(f);
return result;
}
@ -147,11 +145,11 @@ bool CFileSystemGCWii::ExportApploader(const char* _rExportFolder) const
{
char exportName[512];
sprintf(exportName, "%s/apploader.img", _rExportFolder);
FILE* AppFile = fopen(exportName, "wb");
File::IOFile AppFile(exportName, "wb");
if (AppFile)
{
fwrite(buffer, AppSize, 1, AppFile);
fclose(AppFile);
AppFile.WriteBytes(buffer, AppSize);
delete[] buffer;
return true;
}
@ -189,11 +187,10 @@ bool CFileSystemGCWii::ExportDOL(const char* _rExportFolder) const
{
char exportName[512];
sprintf(exportName, "%s/boot.dol", _rExportFolder);
FILE* DolFile = fopen(exportName, "wb");
File::IOFile DolFile(exportName, "wb");
if (DolFile)
{
fwrite(buffer, DolSize, 1, DolFile);
fclose(DolFile);
DolFile.WriteBytes(buffer, DolSize);
delete[] buffer;
return true;
}

View File

@ -36,19 +36,12 @@ CSharedContent::CSharedContent()
lastID = 0;
sprintf(contentMap, "%sshared1/content.map", File::GetUserPath(D_WIIUSER_IDX).c_str());
if (File::Exists(contentMap))
File::IOFile pFile(contentMap, "rb");
SElement Element;
while (pFile.ReadArray(&Element, 1))
{
FILE* pFile = fopen(contentMap, "rb");
while(!feof(pFile))
{
SElement Element;
if (fread(&Element, sizeof(SElement), 1, pFile) == 1)
{
m_Elements.push_back(Element);
lastID++;
}
}
fclose(pFile);
m_Elements.push_back(Element);
lastID++;
}
}
@ -84,12 +77,10 @@ std::string CSharedContent::AddSharedContent(u8* _pHash)
m_Elements.push_back(Element);
File::CreateFullPath(contentMap);
FILE* pFile = fopen(contentMap, "ab");
if (pFile)
{
fwrite(&Element, sizeof(SElement), 1, pFile);
fclose(pFile);
}
File::IOFile pFile(contentMap, "ab");
pFile.WriteArray(&Element, 1);
sprintf(tempFilename, "%sshared1/%s.app", File::GetUserPath(D_WIIUSER_IDX).c_str(), c_ID);
szFilename = tempFilename;
lastID++;
@ -205,16 +196,17 @@ bool CNANDContentLoader::CreateFromDirectory(const std::string& _rPath)
std::string TMDFileName(_rPath);
TMDFileName += "/title.tmd";
FILE* pTMDFile = fopen(TMDFileName.c_str(), "rb");
if (pTMDFile == NULL) {
File::IOFile pTMDFile(TMDFileName, "rb");
if (!pTMDFile)
{
ERROR_LOG(DISCIO, "CreateFromDirectory: error opening %s",
TMDFileName.c_str());
return false;
}
u64 Size = File::GetSize(TMDFileName);
u8* pTMD = new u8[(u32)Size];
fread(pTMD, (size_t)Size, 1, pTMDFile);
fclose(pTMDFile);
pTMDFile.ReadBytes(pTMD, (size_t)Size);
pTMDFile.Close();
memcpy(m_TicketView, pTMD + 0x180, TICKET_VIEW_SIZE);
memcpy(m_TmdHeader, pTMD, TMD_HEADER_SIZE);
@ -257,16 +249,15 @@ bool CNANDContentLoader::CreateFromDirectory(const std::string& _rPath)
INFO_LOG(DISCIO, "NANDContentLoader: load %s", szFilename);
FILE* pFile = fopen(szFilename, "rb");
if (pFile != NULL)
File::IOFile pFile(szFilename, "rb");
if (pFile)
{
u64 ContentSize = File::GetSize(szFilename);
const u64 ContentSize = File::GetSize(szFilename);
rContent.m_pData = new u8[(u32)ContentSize];
_dbg_assert_msg_(BOOT, rContent.m_Size==ContentSize, "TMDLoader: Filesize doesnt fit (%s %i)... prolly you have a bad dump", szFilename, i);
fread(rContent.m_pData, (size_t)ContentSize, 1, pFile);
fclose(pFile);
pFile.ReadBytes(rContent.m_pData, (size_t)ContentSize);
}
else
{
@ -422,34 +413,26 @@ cUIDsys::cUIDsys()
{
sprintf(uidSys, "%ssys/uid.sys", File::GetUserPath(D_WIIUSER_IDX).c_str());
lastUID = 0x00001000;
if (File::Exists(uidSys))
File::IOFile pFile(uidSys, "rb");
SElement Element;
while (pFile.ReadArray(&Element, 1))
{
FILE* pFile = fopen(uidSys, "rb");
while(!feof(pFile))
{
SElement Element;
if (fread(&Element, sizeof(SElement), 1, pFile) == 1)
{
*(u32*)&(Element.UID) = Common::swap32(lastUID++);
m_Elements.push_back(Element);
}
}
fclose(pFile);
*(u32*)&(Element.UID) = Common::swap32(lastUID++);
m_Elements.push_back(Element);
}
if(!m_Elements.size())
pFile.Close();
if (m_Elements.empty())
{
SElement Element;
*(u64*)&(Element.titleID) = Common::swap64(TITLEID_SYSMENU);
*(u32*)&(Element.UID) = Common::swap32(lastUID++);
File::CreateFullPath(uidSys);
FILE* pFile = fopen(uidSys, "wb");
if (pFile)
{
if (fwrite(&Element, sizeof(SElement), 1, pFile) != 1)
ERROR_LOG(DISCIO, "Failed to write to %s", uidSys);
fclose(pFile);
}
pFile.Open(uidSys, "wb");
if (!pFile.WriteArray(&Element, 1))
ERROR_LOG(DISCIO, "Failed to write to %s", uidSys);
}
}
@ -480,13 +463,10 @@ bool cUIDsys::AddTitle(u64 _TitleID)
m_Elements.push_back(Element);
File::CreateFullPath(uidSys);
FILE* pFile = fopen(uidSys, "ab");
if (pFile)
{
if (fwrite(&Element, sizeof(SElement), 1, pFile) != 1)
ERROR_LOG(DISCIO, "fwrite failed");
fclose(pFile);
}
File::IOFile pFile(uidSys, "ab");
if (pFile.WriteArray(&Element, 1))
ERROR_LOG(DISCIO, "fwrite failed");
return true;
}