Skip to content

Commit

Permalink
Qt: more package install fixes
Browse files Browse the repository at this point in the history
- Clean directories if fill_path fails
- Fix check_target_app_version when installing multiple packages (compromise: no more optimized singular file installs for now)
  • Loading branch information
Megamouse committed Jan 11, 2023
1 parent 029cdde commit 24a309f
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 43 deletions.
94 changes: 68 additions & 26 deletions rpcs3/Crypto/unpkg.cpp
Expand Up @@ -689,20 +689,16 @@ package_error package_reader::check_target_app_version() const
return package_error::app_version;
}

bool package_reader::fill_data(std::map<std::string, install_entry*>& all_install_entries)
bool package_reader::set_install_path()
{
if (!m_is_valid)
{
return false;
}

m_install_entries.clear();
m_install_path.clear();
m_bootable_file_path.clear();
m_entry_indexer = 0;
m_written_bytes = 0;

// Get full path and create the directory
// Get full path
std::string dir = rpcs3::utils::get_hdd0_dir();

// Based on https://www.psdevwiki.com/ps3/PKG_files#ContentType
Expand Down Expand Up @@ -739,13 +735,27 @@ bool package_reader::fill_data(std::map<std::string, install_entry*>& all_instal
// If false, an existing directory is being overwritten: cannot cancel the operation
m_was_null = !fs::is_dir(dir);

if (!fs::create_path(dir))
m_install_path = dir;
return true;
}

bool package_reader::fill_data(std::map<std::string, install_entry*>& all_install_entries)
{
if (!m_is_valid)
{
pkg_log.error("Could not create the installation directory %s", dir);
return false;
}

m_install_path = dir;
if (!fs::create_path(m_install_path))
{
pkg_log.error("Could not create the installation directory %s (error=%s)", m_install_path, fs::g_tls_error);
return false;
}

m_install_entries.clear();
m_bootable_file_path.clear();
m_entry_indexer = 0;
m_written_bytes = 0;

if (!decrypt_data())
{
Expand All @@ -772,7 +782,7 @@ bool package_reader::fill_data(std::map<std::string, install_entry*>& all_instal
decrypt(entry.name_offset, entry.name_size, is_psp ? PKG_AES_KEY2 : m_dec_key.data());

const std::string name{reinterpret_cast<char*>(m_bufs.back().get()), entry.name_size};
std::string path = dir + vfs::escape(name);
std::string path = m_install_path + vfs::escape(name);

const bool log_error = entry.pad || (entry.type & ~PKG_FILE_ENTRY_KNOWN_BITS);

Expand Down Expand Up @@ -805,33 +815,34 @@ bool package_reader::fill_data(std::map<std::string, install_entry*>& all_instal
default:
{
const std::string true_path = std::filesystem::weakly_canonical(std::filesystem::u8path(path)).string();
auto map_ptr = &*all_install_entries.try_emplace(true_path).first;
auto it = all_install_entries.try_emplace(true_path).first;

m_install_entries.push_back({
.weak_reference = map_ptr,
.dominant_entry = it->second,
.path = true_path,
.name = name,
.file_offset = entry.file_offset,
.file_size = entry.file_size,
.type = entry.type,
.pad = entry.pad
});

if (map_ptr->second && !(entry.type & PKG_FILE_ENTRY_OVERWRITE))
if (it->second && !(entry.type & PKG_FILE_ENTRY_OVERWRITE))
{
// Cannot override
continue;
}

// Link
map_ptr->second = &m_install_entries.back();
it->second = m_install_entries.back().dominant_entry = &m_install_entries.back();
continue;
}
}
}

if (num_failures != 0)
{
pkg_log.error("Package installation failed: %s", dir);
pkg_log.error("Package installation failed: %s", m_install_path);
return false;
}

Expand Down Expand Up @@ -870,9 +881,11 @@ void package_reader::extract_worker(thread_key thread_data_key)
continue;
}

ensure(entry.dominant_entry);

const bool is_psp = (entry.type & PKG_FILE_ENTRY_PSP) != 0u;

const std::string& path = entry.weak_reference->first;
const std::string& path = entry.dominant_entry->path;
const std::string& name = entry.name;

const bool log_error = entry.pad || (entry.type & ~PKG_FILE_ENTRY_KNOWN_BITS);
Expand Down Expand Up @@ -988,29 +1001,53 @@ void package_reader::extract_worker(thread_key thread_data_key)
}
}

bool package_reader::extract_data(std::deque<package_reader>& readers, std::deque<std::string>& bootable_paths)
package_error package_reader::extract_data(std::deque<package_reader>& readers, std::deque<std::string>& bootable_paths)
{
std::map<std::string, install_entry*> all_install_entries;
package_error error = package_error::no_error;
usz num_failures = 0;

// Set paths first in order to know if the install dir was empty before starting any installations.
// This will also allow us to remove all the new packages in one path at once if any of them fail.
for (package_reader& reader : readers)
{
if (!reader.fill_data(all_install_entries))
reader.m_result = result::not_started;

if (!reader.set_install_path())
{
return false;
error = package_error::other;
reader.m_result = result::error;
break;
}
}

usz num_failures = 0;

for (package_reader& reader : readers)
{
// Use a seperate map for each reader. We need to check if the target app version exists for each package in sequence.
std::map<std::string, install_entry*> all_install_entries;

if (error == package_error::no_error)
{
// Check if this package is allowed to be installed on top of the existing data
error = reader.check_target_app_version();
}

if (error == package_error::no_error)
{
reader.m_result = result::started;

// Parse the files to be installed and create all paths.
if (!reader.fill_data(all_install_entries))
{
error = package_error::other;
}
}

reader.m_bufs.resize(std::min<usz>(utils::get_thread_count(), reader.m_install_entries.size()));
reader.m_num_failures = 0;
reader.m_result = result::not_started;
reader.m_num_failures = error == package_error::no_error ? 0 : 1;

atomic_t<usz> thread_indexer = 0;

named_thread_group workers("PKG Installer "sv, std::max<usz>(reader.m_bufs.size(), 1) - 1, [&]()
named_thread_group workers("PKG Installer "sv, std::max<u32>(::narrow<u32>(reader.m_bufs.size()), 1) - 1, [&]()
{
reader.extract_worker(thread_key{thread_indexer++});
});
Expand Down Expand Up @@ -1067,7 +1104,12 @@ bool package_reader::extract_data(std::deque<package_reader>& readers, std::dequ
bootable_paths.emplace_back(std::move(reader.m_bootable_file_path));
}

return num_failures == 0;
if (num_failures > 0)
{
error = package_error::other;
}

return error;
}

void package_reader::archive_seek(const s64 new_offset, const fs::seek_mode damode)
Expand Down
9 changes: 6 additions & 3 deletions rpcs3/Crypto/unpkg.h
Expand Up @@ -313,7 +313,8 @@ class package_reader

struct install_entry
{
typename std::map<std::string, install_entry*>::value_type* weak_reference{};
install_entry* dominant_entry = nullptr;
std::string path;
std::string name;

u64 file_offset{};
Expand All @@ -324,7 +325,7 @@ class package_reader
// Check if the entry is the same one registered in entries to install
bool is_dominating() const
{
return weak_reference->second == this;
return dominant_entry == this;
}
};

Expand All @@ -335,6 +336,7 @@ class package_reader
enum result
{
not_started,
started,
success,
aborted,
aborted_cleaned,
Expand All @@ -344,7 +346,7 @@ class package_reader

bool is_valid() const { return m_is_valid; }
package_error check_target_app_version() const;
static bool extract_data(std::deque<package_reader>& readers, std::deque<std::string>& bootable_paths);
static package_error extract_data(std::deque<package_reader>& readers, std::deque<std::string>& bootable_paths);
psf::registry get_psf() const { return m_psf; }
result get_result() const { return m_result; };

Expand All @@ -359,6 +361,7 @@ class package_reader
bool decrypt_data();
void archive_seek(s64 new_offset, const fs::seek_mode damode = fs::seek_set);
u64 archive_read(void* data_ptr, u64 num_bytes);
bool set_install_path();
bool fill_data(std::map<std::string, install_entry*>& all_install_entries);
std::span<const char> archive_read_block(u64 offset, void* data_ptr, u64 num_bytes);
std::span<const char> decrypt(u64 offset, u64 size, const uchar* key, thread_key thread_data_key = {0});
Expand Down
4 changes: 2 additions & 2 deletions rpcs3/Emu/system_utils.cpp
Expand Up @@ -92,8 +92,8 @@ namespace rpcs3::utils
named_thread worker("PKG Installer", [&]
{
std::deque<std::string> bootables;

return package_reader::extract_data(reader, bootables);
const package_error error = package_reader::extract_data(reader, bootables);
return error == package_error::no_error;
});

// Wait for the completion
Expand Down
18 changes: 6 additions & 12 deletions rpcs3/rpcs3qt/main_window.cpp
Expand Up @@ -944,26 +944,18 @@ void main_window::HandlePackageInstallation(QStringList file_paths)

std::deque<package_reader> readers;

for (usz i = 0; error == package_error::no_error && i < packages.size(); i++)
for (const compat::package_info& info : packages)
{
readers.emplace_back(sstr(packages[i].path));
error = readers.back().check_target_app_version();
readers.emplace_back(sstr(info.path));
}

std::deque<std::string> bootable_paths;

// Run PKG unpacking asynchronously
named_thread worker("PKG Installer", [&readers, &error, &bootable_paths]
{
if (error == package_error::no_error)
{
if (package_reader::extract_data(readers, bootable_paths))
{
return true;
}
}

return false;
error = package_reader::extract_data(readers, bootable_paths);
return error == package_error::no_error;
});

pdlg.show();
Expand Down Expand Up @@ -1021,6 +1013,7 @@ void main_window::HandlePackageInstallation(QStringList file_paths)
break;
}
case package_reader::result::not_started:
case package_reader::result::started:
case package_reader::result::aborted_cleaned:
{
gui_log.notice("Aborted installation of %s (title_id=%s, title=%s, version=%s).", sstr(package.path), sstr(package.title_id), sstr(package.title), sstr(package.version));
Expand Down Expand Up @@ -1146,6 +1139,7 @@ void main_window::HandlePackageInstallation(QStringList file_paths)
{
case package_reader::result::success:
case package_reader::result::not_started:
case package_reader::result::started:
case package_reader::result::aborted:
case package_reader::result::aborted_cleaned:
break;
Expand Down

0 comments on commit 24a309f

Please sign in to comment.