From 4e1547b3b271abcabce1326d84ec0d5d3cb06ec5 Mon Sep 17 00:00:00 2001 From: Lioncash Date: Fri, 11 May 2018 09:12:28 -0400 Subject: [PATCH] SDCardUtil: Remove the use of IOFile's GetHandle() function GetHandle() should really not even be part of IOFile's interface, but since it is (for the time being), we can cull unnecessary usages of it. In this case, the WriteBytes() function does what we need without using the underlying handle directly. --- Source/Core/Common/SDCardUtil.cpp | 35 +++++++++++++++---------------- 1 file changed, 17 insertions(+), 18 deletions(-) diff --git a/Source/Core/Common/SDCardUtil.cpp b/Source/Core/Common/SDCardUtil.cpp index 3a0ecedf40..8a06a118d9 100644 --- a/Source/Core/Common/SDCardUtil.cpp +++ b/Source/Core/Common/SDCardUtil.cpp @@ -175,12 +175,12 @@ static void fat_init(u8* fat) POKEW(fat + 8, 0x0fffffff); /* End of cluster chain for root dir */ } -static bool write_sector(FILE* file, const u8* sector) +static bool write_sector(File::IOFile& file, const u8* sector) { - return fwrite(sector, 1, BYTES_PER_SECTOR, file) == BYTES_PER_SECTOR; + return file.WriteBytes(sector, BYTES_PER_SECTOR); } -static bool write_empty(FILE* file, std::size_t count) +static bool write_empty(File::IOFile& file, std::size_t count) { static constexpr u8 empty[64 * 1024] = {}; @@ -189,7 +189,7 @@ static bool write_empty(FILE* file, std::size_t count) { const std::size_t len = std::min(sizeof(empty), count); - if (fwrite(empty, 1, len, file) != len) + if (!file.WriteBytes(empty, len)) return false; count -= len; @@ -218,8 +218,7 @@ bool SDCardCreate(u64 disk_size /*in MB*/, const std::string& filename) fat_init(s_fat_head); File::IOFile file(filename, "wb"); - FILE* const f = file.GetHandle(); - if (!f) + if (!file) { ERROR_LOG(COMMON, "Could not create file '%s', aborting...", filename.c_str()); return false; @@ -238,45 +237,45 @@ bool SDCardCreate(u64 disk_size /*in MB*/, const std::string& filename) * zero sectors */ - if (!write_sector(f, s_boot_sector)) + if (!write_sector(file, s_boot_sector)) goto FailWrite; - if (!write_sector(f, s_fsinfo_sector)) + if (!write_sector(file, s_fsinfo_sector)) goto FailWrite; if (BACKUP_BOOT_SECTOR > 0) { - if (!write_empty(f, BACKUP_BOOT_SECTOR - 2)) + if (!write_empty(file, BACKUP_BOOT_SECTOR - 2)) goto FailWrite; - if (!write_sector(f, s_boot_sector)) + if (!write_sector(file, s_boot_sector)) goto FailWrite; - if (!write_sector(f, s_fsinfo_sector)) + if (!write_sector(file, s_fsinfo_sector)) goto FailWrite; - if (!write_empty(f, RESERVED_SECTORS - 2 - BACKUP_BOOT_SECTOR)) + if (!write_empty(file, RESERVED_SECTORS - 2 - BACKUP_BOOT_SECTOR)) goto FailWrite; } else { - if (!write_empty(f, RESERVED_SECTORS - 2)) + if (!write_empty(file, RESERVED_SECTORS - 2)) goto FailWrite; } - if (!write_sector(f, s_fat_head)) + if (!write_sector(file, s_fat_head)) goto FailWrite; - if (!write_empty(f, sectors_per_fat - 1)) + if (!write_empty(file, sectors_per_fat - 1)) goto FailWrite; - if (!write_sector(f, s_fat_head)) + if (!write_sector(file, s_fat_head)) goto FailWrite; - if (!write_empty(f, sectors_per_fat - 1)) + if (!write_empty(file, sectors_per_fat - 1)) goto FailWrite; - if (!write_empty(f, sectors_per_disk - RESERVED_SECTORS - 2 * sectors_per_fat)) + if (!write_empty(file, sectors_per_disk - RESERVED_SECTORS - 2 * sectors_per_fat)) goto FailWrite; return true;