strip minor bug fixes and cleanup

This commit is contained in:
Antonino Di Guardo 2026-04-03 12:01:12 +02:00
parent e88833838c
commit 501323eda8

View File

@ -23,27 +23,25 @@ struct iso_sector
bool is_file_iso(const std::string& path)
{
if (path.empty() || fs::is_dir(path))
{
return false;
}
if (path.empty()) return false;
if (fs::is_dir(path)) return false;
return is_file_iso(fs::file(path));
}
bool is_file_iso(const fs::file& file)
{
if (!file || file.size() < 32768 + 6)
{
return false;
}
if (!file) return false;
if (file.size() < 32768 + 6) return false;
file.seek(32768);
char magic[5];
file.read_at(32768 + 1, magic, 5);
return magic[0] == 'C' && magic[1] == 'D' && magic[2] == '0' && magic[3] == '0' && magic[4] == '1';
return magic[0] == 'C' && magic[1] == 'D'
&& magic[2] == '0' && magic[3] == '0'
&& magic[4] == '1';
}
// keystr_to_keyarr (&& asciischar_to_byte): Convert a hex string into a byte array
@ -390,33 +388,30 @@ inline T read_both_endian_int(fs::file& file)
return out;
}
// Assumed that directory_entry is at file head
static std::optional<iso_fs_metadata> iso_read_directory_entry(fs::file& file, bool names_in_ucs2 = false)
// assumed that directory_entry is at file head
std::optional<iso_fs_metadata> iso_read_directory_entry(fs::file& file, bool names_in_ucs2 = false)
{
const auto start_pos = file.pos();
const u8 entry_length = file.read<u8>();
if (entry_length == 0)
{
return std::nullopt;
}
if (entry_length == 0) return std::nullopt;
file.seek(1, fs::seek_cur);
const u32 start_sector = read_both_endian_int<u32>(file);
const u32 file_size = read_both_endian_int<u32>(file);
std::tm file_date = {};
std::tm file_date = {};
file_date.tm_year = file.read<u8>();
file_date.tm_mon = file.read<u8>() - 1;
file_date.tm_mday = file.read<u8>();
file_date.tm_hour = file.read<u8>();
file_date.tm_min = file.read<u8>();
file_date.tm_sec = file.read<u8>();
const s16 timezone_value = file.read<u8>();
const s16 timezone_offset = (timezone_value - 50) * 15 * 60;
const std::time_t date_time = std::mktime(&file_date) + timezone_offset;
const u8 flags = file.read<u8>();
// 2nd flag bit indicates whether a given fs node is a directory
@ -426,8 +421,8 @@ static std::optional<iso_fs_metadata> iso_read_directory_entry(fs::file& file, b
file.seek(6, fs::seek_cur);
const u8 file_name_length = file.read<u8>();
std::string file_name;
std::string file_name;
file.read(file_name, file_name_length);
if (file_name_length == 1 && file_name[0] == 0)
@ -438,14 +433,13 @@ static std::optional<iso_fs_metadata> iso_read_directory_entry(fs::file& file, b
{
file_name = "..";
}
else if (names_in_ucs2) // For strings in joliet descriptor
else if (names_in_ucs2) // for strings in joliet descriptor
{
// Characters are stored in big endian format
const u16* raw = reinterpret_cast<const u16*>(file_name.data());
// characters are stored in big endian format.
std::u16string utf16;
utf16.resize(file_name_length / 2);
const u16* raw = reinterpret_cast<const u16*>(file_name.data());
for (size_t i = 0; i < utf16.size(); ++i, raw++)
{
utf16[i] = *reinterpret_cast<const be_t<u16>*>(raw);
@ -464,7 +458,7 @@ static std::optional<iso_fs_metadata> iso_read_directory_entry(fs::file& file, b
file_name.pop_back();
}
// Skip the rest of the entry
// skip the rest of the entry.
file.seek(entry_length + start_pos);
return iso_fs_metadata
@ -555,7 +549,6 @@ static void iso_form_hierarchy(fs::file& file, iso_fs_node& node, bool use_ucs2_
u64 iso_fs_metadata::size() const
{
u64 total_size = 0;
for (const auto& extent : extents)
{
total_size += extent.size;
@ -608,27 +601,20 @@ iso_archive::iso_archive(const std::string& path)
iso_fs_node* iso_archive::retrieve(const std::string& passed_path)
{
if (passed_path.empty())
{
return nullptr;
}
if (passed_path.empty()) return nullptr;
const std::string path = std::filesystem::path(passed_path).string();
const std::string_view path_sv = path;
size_t start = 0;
size_t end = path_sv.find_first_of(fs::delim);
std::stack<iso_fs_node*> search_stack;
std::stack<iso_fs_node*> search_stack;
search_stack.push(&m_root);
do
{
if (search_stack.empty())
{
return nullptr;
}
if (search_stack.empty()) return nullptr;
const auto* top_entry = search_stack.top();
if (end == umax)
@ -636,7 +622,8 @@ iso_fs_node* iso_archive::retrieve(const std::string& passed_path)
end = path.size();
}
const std::string_view path_component = path_sv.substr(start, end - start);
const std::string_view path_component = path_sv.substr(start, end-start);
bool found = false;
if (path_component == ".")
@ -646,7 +633,6 @@ iso_fs_node* iso_archive::retrieve(const std::string& passed_path)
else if (path_component == "..")
{
search_stack.pop();
found = true;
}
else
@ -663,20 +649,14 @@ iso_fs_node* iso_archive::retrieve(const std::string& passed_path)
}
}
if (!found)
{
return nullptr;
}
if (!found) return nullptr;
start = end + 1;
end = path_sv.find_first_of(fs::delim, start);
}
while (start < path.size());
if (search_stack.empty())
{
return nullptr;
}
if (search_stack.empty()) return nullptr;
return search_stack.top();
}
@ -689,11 +669,7 @@ bool iso_archive::exists(const std::string& path)
bool iso_archive::is_file(const std::string& path)
{
const auto file_node = retrieve(path);
if (!file_node)
{
return false;
}
if (!file_node) return false;
return !file_node->metadata.is_directory;
}
@ -782,7 +758,6 @@ u64 iso_file::file_offset(u64 pos) const
u64 iso_file::read(void* buffer, u64 size)
{
const auto r = read_at(m_pos, buffer, size);
m_pos += r;
return r;
}
@ -924,10 +899,8 @@ u64 iso_file::seek(s64 offset, fs::seek_mode whence)
return -1;
}
if (m_file.seek(file_offset(new_pos)) == umax)
{
return umax;
}
const u64 result = m_file.seek(file_offset(m_pos));
if (result == umax) return umax;
m_pos = new_pos;
return m_pos;
@ -936,7 +909,6 @@ u64 iso_file::seek(s64 offset, fs::seek_mode whence)
u64 iso_file::size()
{
u64 extent_sizes = 0;
for (const auto& extent : m_meta.extents)
{
extent_sizes += extent.size;
@ -966,22 +938,18 @@ bool iso_dir::read(fs::dir_entry& entry)
entry.size = selected.size();
m_pos++;
return true;
}
return false;
}
void iso_dir::rewind()
{
m_pos = 0;
}
bool iso_device::stat(const std::string& path, fs::stat_t& info)
{
const auto relative_path = std::filesystem::relative(std::filesystem::path(path), std::filesystem::path(fs_prefix)).string();
const auto node = m_archive.retrieve(relative_path);
const auto node = m_archive.retrieve(relative_path);
if (!node)
{
fs::g_tls_error = fs::error::noent;
@ -1007,15 +975,16 @@ bool iso_device::stat(const std::string& path, fs::stat_t& info)
bool iso_device::statfs(const std::string& path, fs::device_stat& info)
{
const auto relative_path = std::filesystem::relative(std::filesystem::path(path), std::filesystem::path(fs_prefix)).string();
const auto node = m_archive.retrieve(relative_path);
const auto node = m_archive.retrieve(relative_path);
if (!node)
{
fs::g_tls_error = fs::error::noent;
return false;
}
const u64 size = node->metadata.size();
const auto& meta = node->metadata;
const u64 size = meta.size();
info = fs::device_stat
{
@ -1025,14 +994,14 @@ bool iso_device::statfs(const std::string& path, fs::device_stat& info)
.avail_free = 0
};
return true;
return false;
}
std::unique_ptr<fs::file_base> iso_device::open(const std::string& path, bs_t<fs::open_mode> mode)
{
const auto relative_path = std::filesystem::relative(std::filesystem::path(path), std::filesystem::path(fs_prefix)).string();
const auto node = m_archive.retrieve(relative_path);
const auto node = m_archive.retrieve(relative_path);
if (!node)
{
fs::g_tls_error = fs::error::noent;
@ -1051,8 +1020,8 @@ std::unique_ptr<fs::file_base> iso_device::open(const std::string& path, bs_t<fs
std::unique_ptr<fs::dir_base> iso_device::open_dir(const std::string& path)
{
const auto relative_path = std::filesystem::relative(std::filesystem::path(path), std::filesystem::path(fs_prefix)).string();
const auto node = m_archive.retrieve(relative_path);
const auto node = m_archive.retrieve(relative_path);
if (!node)
{
fs::g_tls_error = fs::error::noent;
@ -1069,6 +1038,11 @@ std::unique_ptr<fs::dir_base> iso_device::open_dir(const std::string& path)
return std::make_unique<iso_dir>(*node);
}
void iso_dir::rewind()
{
m_pos = 0;
}
void load_iso(const std::string& path)
{
sys_log.notice("Loading iso '%s'", path);