Merge pull request #10403 from AdmiralCurtiss/iofile-seek

Minor IOFile cleanup.
This commit is contained in:
JosJuice 2022-01-29 11:59:58 +01:00 committed by GitHub
commit af5678ea75
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
26 changed files with 125 additions and 87 deletions

View File

@ -95,11 +95,10 @@ bool WaveFileWriter::Start(const std::string& filename, unsigned int HLESampleRa
void WaveFileWriter::Stop()
{
// u32 file_size = (u32)ftello(file);
file.Seek(4, SEEK_SET);
file.Seek(4, File::SeekOrigin::Begin);
Write(audio_size + 36);
file.Seek(40, SEEK_SET);
file.Seek(40, File::SeekOrigin::Begin);
Write(audio_size);
file.Close();

View File

@ -94,7 +94,7 @@ bool IOFile::Close()
void IOFile::SetHandle(std::FILE* file)
{
Close();
Clear();
ClearError();
m_file = file;
}
@ -106,9 +106,25 @@ u64 IOFile::GetSize() const
return 0;
}
bool IOFile::Seek(s64 off, int origin)
bool IOFile::Seek(s64 offset, SeekOrigin origin)
{
if (!IsOpen() || 0 != fseeko(m_file, off, origin))
int fseek_origin;
switch (origin)
{
case SeekOrigin::Begin:
fseek_origin = SEEK_SET;
break;
case SeekOrigin::Current:
fseek_origin = SEEK_CUR;
break;
case SeekOrigin::End:
fseek_origin = SEEK_END;
break;
default:
return false;
}
if (!IsOpen() || 0 != fseeko(m_file, offset, fseek_origin))
m_good = false;
return m_good;

View File

@ -13,6 +13,13 @@
namespace File
{
enum class SeekOrigin
{
Begin,
Current,
End,
};
// simple wrapper for cstdlib file functions to
// hopefully will make error checking easier
// and make forgetting an fclose() harder
@ -89,14 +96,14 @@ public:
std::FILE* GetHandle() { return m_file; }
void SetHandle(std::FILE* file);
bool Seek(s64 off, int origin);
bool Seek(s64 offset, SeekOrigin origin);
u64 Tell() const;
u64 GetSize() const;
bool Resize(u64 size);
bool Flush();
// clear error state
void Clear()
void ClearError()
{
m_good = true;
std::clearerr(m_file);

View File

@ -100,8 +100,8 @@ public:
m_num_entries++;
}
m_file.Clear();
m_file.Seek(last_valid_value_start, SEEK_SET);
m_file.ClearError();
m_file.Seek(last_valid_value_start, File::SeekOrigin::Begin);
return m_num_entries;
}

View File

@ -650,7 +650,7 @@ BootExecutableReader::BootExecutableReader(const std::string& file_name)
BootExecutableReader::BootExecutableReader(File::IOFile file)
{
file.Seek(0, SEEK_SET);
file.Seek(0, File::SeekOrigin::Begin);
m_bytes.resize(file.GetSize());
file.ReadBytes(m_bytes.data(), m_bytes.size());
}

View File

@ -167,7 +167,7 @@ bool FifoDataFile::Save(const std::string& filename)
header.mem1_size = Memory::GetRamSizeReal();
header.mem2_size = Memory::GetExRamSizeReal();
file.Seek(0, SEEK_SET);
file.Seek(0, File::SeekOrigin::Begin);
file.WriteBytes(&header, sizeof(FileHeader));
// Write frames list
@ -176,7 +176,7 @@ bool FifoDataFile::Save(const std::string& filename)
const FifoFrameInfo& srcFrame = m_Frames[i];
// Write FIFO data
file.Seek(0, SEEK_END);
file.Seek(0, File::SeekOrigin::End);
u64 dataOffset = file.Tell();
file.WriteBytes(srcFrame.fifoData.data(), srcFrame.fifoData.size());
@ -192,7 +192,7 @@ bool FifoDataFile::Save(const std::string& filename)
// Write frame info
u64 frameOffset = frameListOffset + (i * sizeof(FileFrameInfo));
file.Seek(frameOffset, SEEK_SET);
file.Seek(frameOffset, File::SeekOrigin::Begin);
file.WriteBytes(&dstFrame, sizeof(FileFrameInfo));
}
@ -284,19 +284,19 @@ std::unique_ptr<FifoDataFile> FifoDataFile::Load(const std::string& filename, bo
}
u32 size = std::min<u32>(BP_MEM_SIZE, header.bpMemSize);
file.Seek(header.bpMemOffset, SEEK_SET);
file.Seek(header.bpMemOffset, File::SeekOrigin::Begin);
file.ReadArray(&dataFile->m_BPMem);
size = std::min<u32>(CP_MEM_SIZE, header.cpMemSize);
file.Seek(header.cpMemOffset, SEEK_SET);
file.Seek(header.cpMemOffset, File::SeekOrigin::Begin);
file.ReadArray(&dataFile->m_CPMem);
size = std::min<u32>(XF_MEM_SIZE, header.xfMemSize);
file.Seek(header.xfMemOffset, SEEK_SET);
file.Seek(header.xfMemOffset, File::SeekOrigin::Begin);
file.ReadArray(&dataFile->m_XFMem);
size = std::min<u32>(XF_REGS_SIZE, header.xfRegsSize);
file.Seek(header.xfRegsOffset, SEEK_SET);
file.Seek(header.xfRegsOffset, File::SeekOrigin::Begin);
file.ReadArray(&dataFile->m_XFRegs);
// Texture memory saving was added in version 4.
@ -304,7 +304,7 @@ std::unique_ptr<FifoDataFile> FifoDataFile::Load(const std::string& filename, bo
if (dataFile->m_Version >= 4)
{
size = std::min<u32>(TEX_MEM_SIZE, header.texMemSize);
file.Seek(header.texMemOffset, SEEK_SET);
file.Seek(header.texMemOffset, File::SeekOrigin::Begin);
file.ReadArray(&dataFile->m_TexMem);
}
@ -319,7 +319,7 @@ std::unique_ptr<FifoDataFile> FifoDataFile::Load(const std::string& filename, bo
for (u32 i = 0; i < header.frameCount; ++i)
{
u64 frameOffset = header.frameListOffset + (i * sizeof(FileFrameInfo));
file.Seek(frameOffset, SEEK_SET);
file.Seek(frameOffset, File::SeekOrigin::Begin);
FileFrameInfo srcFrame;
if (!file.ReadBytes(&srcFrame, sizeof(FileFrameInfo)))
return panic_failed_to_read();
@ -329,7 +329,7 @@ std::unique_ptr<FifoDataFile> FifoDataFile::Load(const std::string& filename, bo
dstFrame.fifoStart = srcFrame.fifoStart;
dstFrame.fifoEnd = srcFrame.fifoEnd;
file.Seek(srcFrame.fifoDataOffset, SEEK_SET);
file.Seek(srcFrame.fifoDataOffset, File::SeekOrigin::Begin);
file.ReadBytes(dstFrame.fifoData.data(), srcFrame.fifoDataSize);
ReadMemoryUpdates(srcFrame.memoryUpdatesOffset, srcFrame.numMemoryUpdates,
@ -375,7 +375,7 @@ u64 FifoDataFile::WriteMemoryUpdates(const std::vector<MemoryUpdate>& memUpdates
const MemoryUpdate& srcUpdate = memUpdates[i];
// Write memory
file.Seek(0, SEEK_END);
file.Seek(0, File::SeekOrigin::End);
u64 dataOffset = file.Tell();
file.WriteBytes(srcUpdate.data.data(), srcUpdate.data.size());
@ -387,7 +387,7 @@ u64 FifoDataFile::WriteMemoryUpdates(const std::vector<MemoryUpdate>& memUpdates
dstUpdate.type = srcUpdate.type;
u64 updateOffset = updateListOffset + (i * sizeof(FileMemoryUpdate));
file.Seek(updateOffset, SEEK_SET);
file.Seek(updateOffset, File::SeekOrigin::Begin);
file.WriteBytes(&dstUpdate, sizeof(FileMemoryUpdate));
}
@ -402,7 +402,7 @@ void FifoDataFile::ReadMemoryUpdates(u64 fileOffset, u32 numUpdates,
for (u32 i = 0; i < numUpdates; ++i)
{
u64 updateOffset = fileOffset + (i * sizeof(FileMemoryUpdate));
file.Seek(updateOffset, SEEK_SET);
file.Seek(updateOffset, File::SeekOrigin::Begin);
FileMemoryUpdate srcUpdate;
file.ReadBytes(&srcUpdate, sizeof(FileMemoryUpdate));
@ -412,7 +412,7 @@ void FifoDataFile::ReadMemoryUpdates(u64 fileOffset, u32 numUpdates,
dstUpdate.data.resize(srcUpdate.dataSize);
dstUpdate.type = static_cast<MemoryUpdate::Type>(srcUpdate.type);
file.Seek(srcUpdate.dataOffset, SEEK_SET);
file.Seek(srcUpdate.dataOffset, File::SeekOrigin::Begin);
file.ReadBytes(dstUpdate.data.data(), srcUpdate.dataSize);
}
}

View File

@ -236,7 +236,7 @@ void CEXIIPL::LoadFontFile(const std::string& filename, u32 offset)
INFO_LOG_FMT(BOOT, "Found IPL dump, loading {} font from {}",
(offset == 0x1aff00) ? "Shift JIS" : "Windows-1252", ipl_rom_path);
stream.Seek(offset, 0);
stream.Seek(offset, File::SeekOrigin::Begin);
stream.ReadBytes(&m_rom[offset], fontsize);
m_fonts_loaded = true;

View File

@ -55,7 +55,7 @@ bool GCIFile::LoadSaveBlocks()
}
m_save_data.resize(num_blocks);
save_file.Seek(DENTRY_SIZE, SEEK_SET);
save_file.Seek(DENTRY_SIZE, File::SeekOrigin::Begin);
if (!save_file.ReadBytes(m_save_data.data(), size))
{
ERROR_LOG_FMT(EXPANSIONINTERFACE, "Failed to read data from GCI file {}", m_filename);

View File

@ -112,7 +112,7 @@ std::pair<GCMemcardErrorCode, std::optional<GCMemcard>> GCMemcard::Open(std::str
// read the entire card into memory
GCMemcard card;
file.Seek(0, SEEK_SET);
file.Seek(0, File::SeekOrigin::Begin);
if (!file.ReadBytes(&card.m_header_block, BLOCK_SIZE) ||
!file.ReadBytes(&card.m_directory_blocks[0], BLOCK_SIZE) ||
!file.ReadBytes(&card.m_directory_blocks[1], BLOCK_SIZE) ||
@ -310,7 +310,7 @@ bool GCMemcard::IsShiftJIS() const
bool GCMemcard::Save()
{
File::IOFile mcdFile(m_filename, "wb");
mcdFile.Seek(0, SEEK_SET);
mcdFile.Seek(0, File::SeekOrigin::Begin);
mcdFile.WriteBytes(&m_header_block, BLOCK_SIZE);
mcdFile.WriteBytes(&m_directory_blocks[0], BLOCK_SIZE);

View File

@ -278,7 +278,7 @@ public:
std::optional<Header> ReadHeader() override
{
Header header;
if (!m_file.Seek(0, SEEK_SET) || !m_file.ReadArray(&header, 1))
if (!m_file.Seek(0, File::SeekOrigin::Begin) || !m_file.ReadArray(&header, 1))
return {};
std::array<u8, 0x10> iv = s_sd_initial_iv;
@ -310,7 +310,7 @@ public:
std::optional<BkHeader> ReadBkHeader() override
{
BkHeader bk_header;
m_file.Seek(sizeof(Header), SEEK_SET);
m_file.Seek(sizeof(Header), File::SeekOrigin::Begin);
if (!m_file.ReadArray(&bk_header, 1))
return {};
if (bk_header.size != BK_LISTED_SZ || bk_header.magic != BK_HDR_MAGIC)
@ -323,7 +323,7 @@ public:
std::optional<std::vector<SaveFile>> ReadFiles() override
{
const std::optional<BkHeader> bk_header = ReadBkHeader();
if (!bk_header || !m_file.Seek(sizeof(Header) + sizeof(BkHeader), SEEK_SET))
if (!bk_header || !m_file.Seek(sizeof(Header) + sizeof(BkHeader), File::SeekOrigin::Begin))
return {};
std::vector<SaveFile> files;
@ -352,15 +352,18 @@ public:
save_file.data = [this, size, rounded_size, iv,
pos]() mutable -> std::optional<std::vector<u8>> {
std::vector<u8> file_data(rounded_size);
if (!m_file.Seek(pos, SEEK_SET) || !m_file.ReadBytes(file_data.data(), rounded_size))
if (!m_file.Seek(pos, File::SeekOrigin::Begin) ||
!m_file.ReadBytes(file_data.data(), rounded_size))
{
return {};
}
m_iosc.Decrypt(IOS::HLE::IOSC::HANDLE_SD_KEY, iv.data(), file_data.data(), rounded_size,
file_data.data(), IOS::PID_ES);
file_data.resize(size);
return file_data;
};
m_file.Seek(pos + rounded_size, SEEK_SET);
m_file.Seek(pos + rounded_size, File::SeekOrigin::Begin);
}
files.emplace_back(std::move(save_file));
}
@ -373,17 +376,17 @@ public:
std::array<u8, 0x10> iv = s_sd_initial_iv;
m_iosc.Encrypt(IOS::HLE::IOSC::HANDLE_SD_KEY, iv.data(), reinterpret_cast<const u8*>(&header),
sizeof(Header), reinterpret_cast<u8*>(&encrypted_header), IOS::PID_ES);
return m_file.Seek(0, SEEK_SET) && m_file.WriteArray(&encrypted_header, 1);
return m_file.Seek(0, File::SeekOrigin::Begin) && m_file.WriteArray(&encrypted_header, 1);
}
bool WriteBkHeader(const BkHeader& bk_header) override
{
return m_file.Seek(sizeof(Header), SEEK_SET) && m_file.WriteArray(&bk_header, 1);
return m_file.Seek(sizeof(Header), File::SeekOrigin::Begin) && m_file.WriteArray(&bk_header, 1);
}
bool WriteFiles(const std::vector<SaveFile>& files) override
{
if (!m_file.Seek(sizeof(Header) + sizeof(BkHeader), SEEK_SET))
if (!m_file.Seek(sizeof(Header) + sizeof(BkHeader), File::SeekOrigin::Begin))
return false;
for (const SaveFile& save_file : files)
@ -440,7 +443,7 @@ private:
{
const u32 data_size = bk_header->size_of_files + sizeof(BkHeader);
auto data = std::make_unique<u8[]>(data_size);
m_file.Seek(sizeof(Header), SEEK_SET);
m_file.Seek(sizeof(Header), File::SeekOrigin::Begin);
if (!m_file.ReadBytes(data.get(), data_size))
return false;
mbedtls_sha1_ret(data.get(), data_size, data_sha1.data());
@ -453,7 +456,7 @@ private:
data_sha1.data(), static_cast<u32>(data_sha1.size()));
// Write signatures.
if (!m_file.Seek(0, SEEK_END))
if (!m_file.Seek(0, File::SeekOrigin::End))
return false;
const u32 SIGNATURE_END_MAGIC = Common::swap32(0x2f536969);
const IOS::CertECC device_certificate = m_iosc.GetDeviceCertificate();

View File

@ -127,7 +127,7 @@ Result<u32> HostFileSystem::ReadBytesFromFile(Fd fd, u8* ptr, u32 count)
count = file_size - handle->file_offset;
// File might be opened twice, need to seek before we read
handle->host_file->Seek(handle->file_offset, SEEK_SET);
handle->host_file->Seek(handle->file_offset, File::SeekOrigin::Begin);
const u32 actually_read = static_cast<u32>(fread(ptr, 1, count, handle->host_file->GetHandle()));
if (actually_read != count && ferror(handle->host_file->GetHandle()))
@ -149,7 +149,7 @@ Result<u32> HostFileSystem::WriteBytesToFile(Fd fd, const u8* ptr, u32 count)
return ResultCode::AccessDenied;
// File might be opened twice, need to seek before we read
handle->host_file->Seek(handle->file_offset, SEEK_SET);
handle->host_file->Seek(handle->file_offset, File::SeekOrigin::Begin);
if (!handle->host_file->WriteBytes(ptr, count))
return ResultCode::AccessDenied;

View File

@ -251,8 +251,8 @@ s32 SDIOSlot0Device::ExecuteCommand(const Request& request, u32 buffer_in, u32 b
const u32 size = req.bsize * req.blocks;
const u64 address = GetAddressFromRequest(req.arg);
if (!m_card.Seek(address, SEEK_SET))
ERROR_LOG_FMT(IOS_SD, "Seek failed WTF");
if (!m_card.Seek(address, File::SeekOrigin::Begin))
ERROR_LOG_FMT(IOS_SD, "Seek failed");
if (m_card.ReadBytes(Memory::GetPointer(req.addr), size))
{
@ -281,8 +281,8 @@ s32 SDIOSlot0Device::ExecuteCommand(const Request& request, u32 buffer_in, u32 b
const u32 size = req.bsize * req.blocks;
const u64 address = GetAddressFromRequest(req.arg);
if (!m_card.Seek(address, SEEK_SET))
ERROR_LOG_FMT(IOS_SD, "fseeko failed WTF");
if (!m_card.Seek(address, File::SeekOrigin::Begin))
ERROR_LOG_FMT(IOS_SD, "Seek failed");
if (!m_card.WriteBytes(Memory::GetPointer(req.addr), size))
{

View File

@ -290,14 +290,14 @@ std::optional<IPCReply> WFSSRVDevice::IOCtl(const IOCtlRequest& request)
const u64 previous_position = fd_obj->file.Tell();
if (absolute)
{
fd_obj->file.Seek(position, SEEK_SET);
fd_obj->file.Seek(position, File::SeekOrigin::Begin);
}
size_t read_bytes;
fd_obj->file.ReadArray(Memory::GetPointer(addr), size, &read_bytes);
// TODO(wfs): Handle read errors.
if (absolute)
{
fd_obj->file.Seek(previous_position, SEEK_SET);
fd_obj->file.Seek(previous_position, File::SeekOrigin::Begin);
}
else
{
@ -331,13 +331,13 @@ std::optional<IPCReply> WFSSRVDevice::IOCtl(const IOCtlRequest& request)
const u64 previous_position = fd_obj->file.Tell();
if (absolute)
{
fd_obj->file.Seek(position, SEEK_SET);
fd_obj->file.Seek(position, File::SeekOrigin::Begin);
}
fd_obj->file.WriteArray(Memory::GetPointer(addr), size);
// TODO(wfs): Handle write errors.
if (absolute)
{
fd_obj->file.Seek(previous_position, SEEK_SET);
fd_obj->file.Seek(previous_position, File::SeekOrigin::Begin);
}
else
{

View File

@ -1021,7 +1021,7 @@ void LoadInput(const std::string& movie_path)
{
s_rerecords++;
tmpHeader.numRerecords = s_rerecords;
t_record.Seek(0, SEEK_SET);
t_record.Seek(0, File::SeekOrigin::Begin);
t_record.WriteArray(&tmpHeader, 1);
}

View File

@ -18,7 +18,7 @@ CISOFileReader::CISOFileReader(File::IOFile file) : m_file(std::move(file))
m_size = m_file.GetSize();
CISOHeader header;
m_file.Seek(0, SEEK_SET);
m_file.Seek(0, File::SeekOrigin::Begin);
m_file.ReadArray(&header, 1);
m_block_size = header.block_size;
@ -31,8 +31,11 @@ CISOFileReader::CISOFileReader(File::IOFile file) : m_file(std::move(file))
std::unique_ptr<CISOFileReader> CISOFileReader::Create(File::IOFile file)
{
CISOHeader header;
if (file.Seek(0, SEEK_SET) && file.ReadArray(&header, 1) && header.magic == CISO_MAGIC)
if (file.Seek(0, File::SeekOrigin::Begin) && file.ReadArray(&header, 1) &&
header.magic == CISO_MAGIC)
{
return std::unique_ptr<CISOFileReader>(new CISOFileReader(std::move(file)));
}
return nullptr;
}
@ -63,9 +66,10 @@ bool CISOFileReader::Read(u64 offset, u64 nbytes, u8* out_ptr)
// calculate the base address
u64 const file_off = CISO_HEADER_SIZE + m_ciso_map[block] * (u64)m_block_size + data_offset;
if (!(m_file.Seek(file_off, SEEK_SET) && m_file.ReadArray(out_ptr, bytes_to_read)))
if (!(m_file.Seek(file_off, File::SeekOrigin::Begin) &&
m_file.ReadArray(out_ptr, bytes_to_read)))
{
m_file.Clear();
m_file.ClearError();
return false;
}
}

View File

@ -38,7 +38,7 @@ CompressedBlobReader::CompressedBlobReader(File::IOFile file, const std::string&
: m_file(std::move(file)), m_file_name(filename)
{
m_file_size = m_file.GetSize();
m_file.Seek(0, SEEK_SET);
m_file.Seek(0, File::SeekOrigin::Begin);
m_file.ReadArray(&m_header, 1);
SetSectorSize(m_header.block_size);
@ -104,12 +104,12 @@ bool CompressedBlobReader::GetBlock(u64 block_num, u8* out_ptr)
// clear unused part of zlib buffer. maybe this can be deleted when it works fully.
memset(&m_zlib_buffer[comp_block_size], 0, m_zlib_buffer.size() - comp_block_size);
m_file.Seek(offset, SEEK_SET);
m_file.Seek(offset, File::SeekOrigin::Begin);
if (!m_file.ReadBytes(m_zlib_buffer.data(), comp_block_size))
{
ERROR_LOG_FMT(DISCIO, "The disc image \"{}\" is truncated, some of the data is missing.",
m_file_name);
m_file.Clear();
m_file.ClearError();
return false;
}
@ -301,9 +301,9 @@ bool ConvertToGCZ(BlobReader* infile, const std::string& infile_path,
std::vector<u32> hashes(header.num_blocks);
// seek past the header (we will write it at the end)
outfile.Seek(sizeof(CompressedBlobHeader), SEEK_CUR);
outfile.Seek(sizeof(CompressedBlobHeader), File::SeekOrigin::Current);
// seek past the offset and hash tables (we will write them at the end)
outfile.Seek((sizeof(u64) + sizeof(u32)) * header.num_blocks, SEEK_CUR);
outfile.Seek((sizeof(u64) + sizeof(u32)) * header.num_blocks, File::SeekOrigin::Current);
// Now we are ready to write compressed data!
u64 inpos = 0;
@ -361,7 +361,7 @@ bool ConvertToGCZ(BlobReader* infile, const std::string& infile_path,
else
{
// Okay, go back and fill in headers
outfile.Seek(0, SEEK_SET);
outfile.Seek(0, File::SeekOrigin::Begin);
outfile.WriteArray(&header, 1);
outfile.WriteArray(offsets.data(), header.num_blocks);
outfile.WriteArray(hashes.data(), header.num_blocks);
@ -385,11 +385,11 @@ bool ConvertToGCZ(BlobReader* infile, const std::string& infile_path,
bool IsGCZBlob(File::IOFile& file)
{
const u64 position = file.Tell();
if (!file.Seek(0, SEEK_SET))
if (!file.Seek(0, File::SeekOrigin::Begin))
return false;
CompressedBlobHeader header;
bool is_gcz = file.ReadArray(&header, 1) && header.magic_cookie == GCZ_MAGIC;
file.Seek(position, SEEK_SET);
file.Seek(position, File::SeekOrigin::Begin);
return is_gcz;
}

View File

@ -98,7 +98,7 @@ bool DiscContent::Read(u64* offset, u64* length, u8** buffer) const
{
const auto& content = std::get<ContentFile>(m_content_source);
File::IOFile file(content.m_filename, "rb");
if (!file.Seek(content.m_offset + offset_in_content, SEEK_SET) ||
if (!file.Seek(content.m_offset + offset_in_content, File::SeekOrigin::Begin) ||
!file.ReadBytes(*buffer, bytes_to_read))
{
return false;

View File

@ -150,10 +150,10 @@ bool DriveReader::ReadMultipleAlignedBlocks(u64 block_num, u64 num_blocks, u8* o
}
return bytes_read == GetSectorSize() * num_blocks;
#else
m_file.Seek(GetSectorSize() * block_num, SEEK_SET);
m_file.Seek(GetSectorSize() * block_num, File::SeekOrigin::Begin);
if (m_file.ReadBytes(out_ptr, num_blocks * GetSectorSize()))
return true;
m_file.Clear();
m_file.ClearError();
return false;
#endif
}

View File

@ -30,13 +30,13 @@ std::unique_ptr<PlainFileReader> PlainFileReader::Create(File::IOFile file)
bool PlainFileReader::Read(u64 offset, u64 nbytes, u8* out_ptr)
{
if (m_file.Seek(offset, SEEK_SET) && m_file.ReadBytes(out_ptr, nbytes))
if (m_file.Seek(offset, File::SeekOrigin::Begin) && m_file.ReadBytes(out_ptr, nbytes))
{
return true;
}
else
{
m_file.Clear();
m_file.ClearError();
return false;
}
}

View File

@ -71,7 +71,9 @@ bool NANDImporter::ReadNANDBin(const std::string& path_to_bin,
m_update_callback();
file.ReadBytes(&m_nand[i * NAND_BLOCK_SIZE], NAND_BLOCK_SIZE);
file.Seek(NAND_ECC_BLOCK_SIZE, SEEK_CUR); // We don't care about the ECC blocks
// We don't care about the ECC blocks
file.Seek(NAND_ECC_BLOCK_SIZE, File::SeekOrigin::Current);
}
m_nand_keys.resize(NAND_KEYS_SIZE);

View File

@ -48,15 +48,18 @@ namespace DiscIO
std::unique_ptr<TGCFileReader> TGCFileReader::Create(File::IOFile file)
{
TGCHeader header;
if (file.Seek(0, SEEK_SET) && file.ReadArray(&header, 1) && header.magic == TGC_MAGIC)
if (file.Seek(0, File::SeekOrigin::Begin) && file.ReadArray(&header, 1) &&
header.magic == TGC_MAGIC)
{
return std::unique_ptr<TGCFileReader>(new TGCFileReader(std::move(file)));
}
return nullptr;
}
TGCFileReader::TGCFileReader(File::IOFile file) : m_file(std::move(file))
{
m_file.Seek(0, SEEK_SET);
m_file.Seek(0, File::SeekOrigin::Begin);
m_file.ReadArray(&m_header, 1);
m_size = m_file.GetSize();
@ -64,8 +67,11 @@ TGCFileReader::TGCFileReader(File::IOFile file) : m_file(std::move(file))
const u32 fst_offset = Common::swap32(m_header.fst_real_offset);
const u32 fst_size = Common::swap32(m_header.fst_size);
m_fst.resize(fst_size);
if (!m_file.Seek(fst_offset, SEEK_SET) || !m_file.ReadBytes(m_fst.data(), m_fst.size()))
if (!m_file.Seek(fst_offset, File::SeekOrigin::Begin) ||
!m_file.ReadBytes(m_fst.data(), m_fst.size()))
{
m_fst.clear();
}
constexpr size_t FST_ENTRY_SIZE = 12;
if (m_fst.size() < FST_ENTRY_SIZE)
@ -101,7 +107,8 @@ bool TGCFileReader::Read(u64 offset, u64 nbytes, u8* out_ptr)
{
const u32 tgc_header_size = Common::swap32(m_header.tgc_header_size);
if (m_file.Seek(offset + tgc_header_size, SEEK_SET) && m_file.ReadBytes(out_ptr, nbytes))
if (m_file.Seek(offset + tgc_header_size, File::SeekOrigin::Begin) &&
m_file.ReadBytes(out_ptr, nbytes))
{
const u32 replacement_dol_offset = SubtractBE32(m_header.dol_real_offset, tgc_header_size);
const u32 replacement_fst_offset = SubtractBE32(m_header.fst_real_offset, tgc_header_size);
@ -114,7 +121,7 @@ bool TGCFileReader::Read(u64 offset, u64 nbytes, u8* out_ptr)
return true;
}
m_file.Clear();
m_file.ClearError();
return false;
}

View File

@ -87,7 +87,7 @@ WIARVZFileReader<RVZ>::~WIARVZFileReader() = default;
template <bool RVZ>
bool WIARVZFileReader<RVZ>::Initialize(const std::string& path)
{
if (!m_file.Seek(0, SEEK_SET) || !m_file.ReadArray(&m_header_1, 1))
if (!m_file.Seek(0, File::SeekOrigin::Begin) || !m_file.ReadArray(&m_header_1, 1))
return false;
if ((!RVZ && m_header_1.magic != WIA_MAGIC) || (RVZ && m_header_1.magic != RVZ_MAGIC))
@ -160,7 +160,7 @@ bool WIARVZFileReader<RVZ>::Initialize(const std::string& path)
const size_t number_of_partition_entries = Common::swap32(m_header_2.number_of_partition_entries);
const size_t partition_entry_size = Common::swap32(m_header_2.partition_entry_size);
std::vector<u8> partition_entries(partition_entry_size * number_of_partition_entries);
if (!m_file.Seek(Common::swap64(m_header_2.partition_entries_offset), SEEK_SET))
if (!m_file.Seek(Common::swap64(m_header_2.partition_entries_offset), File::SeekOrigin::Begin))
return false;
if (!m_file.ReadBytes(partition_entries.data(), partition_entries.size()))
return false;
@ -686,7 +686,7 @@ bool WIARVZFileReader<RVZ>::Chunk::Read(u64 offset, u64 size, u8* out_ptr)
return false;
}
if (!m_file->Seek(m_offset_in_file, SEEK_SET))
if (!m_file->Seek(m_offset_in_file, File::SeekOrigin::Begin))
return false;
if (!m_file->ReadBytes(m_in.data.data() + m_in.bytes_written, bytes_to_read))
return false;
@ -1716,7 +1716,7 @@ bool WIARVZFileReader<RVZ>::WriteHeader(File::IOFile* file, const u8* data, size
{
WARN_LOG_FMT(DISCIO,
"Headers did not fit in the allocated space. Writing to end of file instead");
if (!file->Seek(0, SEEK_END))
if (!file->Seek(0, File::SeekOrigin::End))
return false;
*bytes_written = file->Tell();
}
@ -1949,7 +1949,7 @@ WIARVZFileReader<RVZ>::Convert(BlobReader* infile, const VolumeDisc* infile_volu
return ConversionResultCode::InternalError;
bytes_written = sizeof(WIAHeader1) + sizeof(WIAHeader2);
if (!outfile->Seek(sizeof(WIAHeader1) + sizeof(WIAHeader2), SEEK_SET))
if (!outfile->Seek(sizeof(WIAHeader1) + sizeof(WIAHeader2), File::SeekOrigin::Begin))
return ConversionResultCode::WriteFailed;
u64 partition_entries_offset;
@ -2018,7 +2018,7 @@ WIARVZFileReader<RVZ>::Convert(BlobReader* infile, const VolumeDisc* infile_volu
mbedtls_sha1_ret(reinterpret_cast<const u8*>(&header_1), offsetof(WIAHeader1, header_1_hash),
header_1.header_1_hash.data());
if (!outfile->Seek(0, SEEK_SET))
if (!outfile->Seek(0, File::SeekOrigin::Begin))
return ConversionResultCode::WriteFailed;
if (!outfile->WriteArray(&header_1, 1))

View File

@ -37,7 +37,7 @@ WbfsFileReader::WbfsFileReader(File::IOFile file, const std::string& path)
// Grab disc info (assume slot 0, checked in ReadHeader())
m_wlba_table.resize(m_blocks_per_disc);
m_files[0].file.Seek(m_hd_sector_size + WII_DISC_HEADER_SIZE /*+ i * m_disc_info_size*/,
SEEK_SET);
File::SeekOrigin::Begin);
m_files[0].file.ReadBytes(m_wlba_table.data(), m_blocks_per_disc * sizeof(u16));
for (size_t i = 0; i < m_blocks_per_disc; i++)
m_wlba_table[i] = Common::swap16(m_wlba_table[i]);
@ -86,7 +86,7 @@ bool WbfsFileReader::AddFileToList(File::IOFile file)
bool WbfsFileReader::ReadHeader()
{
// Read hd size info
m_files[0].file.Seek(0, SEEK_SET);
m_files[0].file.Seek(0, File::SeekOrigin::Begin);
m_files[0].file.ReadBytes(&m_header, sizeof(WbfsHeader));
if (m_header.magic != WBFS_MAGIC)
return false;
@ -127,7 +127,7 @@ bool WbfsFileReader::Read(u64 offset, u64 nbytes, u8* out_ptr)
if (!data_file.ReadBytes(out_ptr, read_size))
{
data_file.Clear();
data_file.ClearError();
return false;
}
@ -152,7 +152,7 @@ File::IOFile& WbfsFileReader::SeekToCluster(u64 offset, u64* available)
{
if (final_address < (file_entry.base_address + file_entry.size))
{
file_entry.file.Seek(final_address - file_entry.base_address, SEEK_SET);
file_entry.file.Seek(final_address - file_entry.base_address, File::SeekOrigin::Begin);
if (available)
{
u64 till_end_of_file = file_entry.size - (final_address - file_entry.base_address);
@ -168,7 +168,7 @@ File::IOFile& WbfsFileReader::SeekToCluster(u64 offset, u64* available)
ERROR_LOG_FMT(DISCIO, "Read beyond end of disc");
if (available)
*available = 0;
m_files[0].file.Seek(0, SEEK_SET);
m_files[0].file.Seek(0, File::SeekOrigin::Begin);
return m_files[0].file;
}

View File

@ -60,7 +60,7 @@ std::vector<u32> WiiSaveBanner::GetBanner(u32* width, u32* height) const
*height = 0;
File::IOFile file(m_path, "rb");
if (!file.Seek(sizeof(Header), SEEK_SET))
if (!file.Seek(sizeof(Header), File::SeekOrigin::Begin))
return std::vector<u32>();
std::vector<u16> banner_data(BANNER_WIDTH * BANNER_HEIGHT);

View File

@ -447,7 +447,7 @@ bool HiresTexture::LoadDDSTexture(HiresTexture* tex, const std::string& filename
// Read first mip level, as it may have a custom pitch.
Level first_level;
if (!file.Seek(info.first_mip_offset, SEEK_SET) ||
if (!file.Seek(info.first_mip_offset, File::SeekOrigin::Begin) ||
!ReadMipLevel(&first_level, file, filename, 0, info, info.width, info.height,
info.first_mip_row_length, info.first_mip_size))
{

View File

@ -772,7 +772,7 @@ void ShaderCache::LoadPipelineUIDCache()
// We open the file for reading and writing, so we must seek to the end before writing.
if (uid_file_valid)
uid_file_valid = m_gx_pipeline_uid_cache_file.Seek(expected_size, SEEK_SET);
uid_file_valid = m_gx_pipeline_uid_cache_file.Seek(expected_size, File::SeekOrigin::Begin);
}
// If the file is invalid, close it. We re-open and truncate it below.