diff options
Diffstat (limited to 'apexd/apexd_session.cpp')
-rw-r--r-- | apexd/apexd_session.cpp | 94 |
1 files changed, 48 insertions, 46 deletions
diff --git a/apexd/apexd_session.cpp b/apexd/apexd_session.cpp index 3d916c4..b8dd87b 100644 --- a/apexd/apexd_session.cpp +++ b/apexd/apexd_session.cpp @@ -22,6 +22,7 @@ #include "session_state.pb.h" #include <android-base/logging.h> +#include <android-base/stringprintf.h> #include <dirent.h> #include <sys/stat.h> @@ -32,6 +33,7 @@ using android::base::Error; using android::base::Result; +using android::base::StringPrintf; using apex::proto::SessionState; namespace android { @@ -39,60 +41,49 @@ namespace apex { namespace { -static constexpr const char* kStateFileName = "state"; +// Starting from R, apexd prefers /metadata partition (kNewApexSessionsDir) as +// location for sessions-related information. For devices that don't have +// /metadata partition, apexd will fallback to the /data one +// (kOldApexSessionsDir). +static constexpr const char* kOldApexSessionsDir = "/data/apex/sessions"; +static constexpr const char* kNewApexSessionsDir = "/metadata/apex/sessions"; -std::string getSessionDir(int session_id) { - return kApexSessionsDir + "/" + std::to_string(session_id); -} +static constexpr const char* kStateFileName = "state"; -std::string getSessionStateFilePath(int session_id) { - return getSessionDir(session_id) + "/" + kStateFileName; -} +} // namespace -Result<std::string> createSessionDirIfNeeded(int session_id) { - // create /data/sessions - auto res = createDirIfNeeded(kApexSessionsDir, 0700); - if (!res.ok()) { - return res.error(); - } - // create /data/sessions/session_id - std::string sessionDir = getSessionDir(session_id); - res = createDirIfNeeded(sessionDir, 0700); - if (!res.ok()) { - return res.error(); - } +ApexSession::ApexSession(SessionState state) : state_(std::move(state)) {} - return sessionDir; +std::string ApexSession::GetSessionsDir() { + static std::string result; + static std::once_flag once_flag; + std::call_once(once_flag, [&]() { + auto status = + FindFirstExistingDirectory(kNewApexSessionsDir, kOldApexSessionsDir); + if (!status.ok()) { + LOG(FATAL) << status.error(); + } + result = std::move(*status); + }); + return result; } -Result<void> deleteSessionDir(int session_id) { - std::string session_dir = getSessionDir(session_id); - LOG(DEBUG) << "Deleting " << session_dir; - auto path = std::filesystem::path(session_dir); - std::error_code error_code; - std::filesystem::remove_all(path, error_code); - if (error_code) { - return Error() << "Failed to delete " << session_dir << " : " - << error_code.message(); - } - return {}; +Result<void> ApexSession::MigrateToMetadataSessionsDir() { + return MoveDir(kOldApexSessionsDir, kNewApexSessionsDir); } -} // namespace - -ApexSession::ApexSession(SessionState state) : state_(std::move(state)) {} - Result<ApexSession> ApexSession::CreateSession(int session_id) { SessionState state; // Create session directory - auto sessionPath = createSessionDirIfNeeded(session_id); - if (!sessionPath.ok()) { - return sessionPath.error(); + std::string session_dir = GetSessionsDir() + "/" + std::to_string(session_id); + if (auto status = createDirIfNeeded(session_dir, 0700); !status.ok()) { + return status.error(); } state.set_id(session_id); return ApexSession(state); } + Result<ApexSession> ApexSession::GetSessionFromFile(const std::string& path) { SessionState state; std::fstream stateFile(path, std::ios::in | std::ios::binary); @@ -108,7 +99,8 @@ Result<ApexSession> ApexSession::GetSessionFromFile(const std::string& path) { } Result<ApexSession> ApexSession::GetSession(int session_id) { - auto path = getSessionStateFilePath(session_id); + auto path = StringPrintf("%s/%d/%s", GetSessionsDir().c_str(), session_id, + kStateFileName); return GetSessionFromFile(path); } @@ -117,7 +109,7 @@ std::vector<ApexSession> ApexSession::GetSessions() { std::vector<ApexSession> sessions; Result<std::vector<std::string>> sessionPaths = ReadDir( - kApexSessionsDir, [](const std::filesystem::directory_entry& entry) { + GetSessionsDir(), [](const std::filesystem::directory_entry& entry) { std::error_code ec; return entry.is_directory(ec); }); @@ -238,19 +230,29 @@ Result<void> ApexSession::UpdateStateAndCommit( const SessionState::State& session_state) { state_.set_state(session_state); - auto stateFilePath = getSessionStateFilePath(state_.id()); + auto state_file_path = StringPrintf("%s/%d/%s", GetSessionsDir().c_str(), + state_.id(), kStateFileName); - std::fstream stateFile(stateFilePath, - std::ios::out | std::ios::trunc | std::ios::binary); - if (!state_.SerializeToOstream(&stateFile)) { - return Error() << "Failed to write state file " << stateFilePath; + std::fstream state_file(state_file_path, + std::ios::out | std::ios::trunc | std::ios::binary); + if (!state_.SerializeToOstream(&state_file)) { + return Error() << "Failed to write state file " << state_file_path; } return {}; } Result<void> ApexSession::DeleteSession() const { - return deleteSessionDir(GetId()); + std::string session_dir = GetSessionsDir() + "/" + std::to_string(GetId()); + LOG(INFO) << "Deleting " << session_dir; + auto path = std::filesystem::path(session_dir); + std::error_code error_code; + std::filesystem::remove_all(path, error_code); + if (error_code) { + return Error() << "Failed to delete " << session_dir << " : " + << error_code.message(); + } + return {}; } std::ostream& operator<<(std::ostream& out, const ApexSession& session) { |