From e2f51b80a1aa7a9a97214e5b061cac99158f0755 Mon Sep 17 00:00:00 2001 From: Simon Fels Date: Thu, 2 Feb 2017 12:37:49 +0100 Subject: [PATCH] Rework stream usage for desktop/icon file creation --- src/anbox/application/launcher_storage.cpp | 34 +++++++++++----------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/src/anbox/application/launcher_storage.cpp b/src/anbox/application/launcher_storage.cpp index 9aab375..ae036ef 100644 --- a/src/anbox/application/launcher_storage.cpp +++ b/src/anbox/application/launcher_storage.cpp @@ -48,10 +48,8 @@ void LauncherStorage::add(const Item &item) { auto package_name = item.package; std::replace(package_name.begin(), package_name.end(), '.', '-'); - const auto item_path = - path_ / utils::string_format("anbox-%s.desktop", package_name); - const auto item_icon_path = - icon_path_ / utils::string_format("anbox-%s.png", package_name); + const auto item_path = path_ / utils::string_format("anbox-%s.desktop", package_name); + const auto item_icon_path = icon_path_ / utils::string_format("anbox-%s.png", package_name); std::string exec = utils::string_format("%s launch ", utils::process_get_exe_path(getpid())); @@ -68,21 +66,23 @@ void LauncherStorage::add(const Item &item) { exec += utils::string_format("--package=%s ", item.launch_intent.package); if (!item.launch_intent.component.empty()) - exec += - utils::string_format("--component=%s ", item.launch_intent.component); + exec += utils::string_format("--component=%s ", item.launch_intent.component); - std::ofstream desktop_item(item_path.string()); - desktop_item << "[Desktop Entry]" << std::endl - << "Name=" << item.package << std::endl - << "Exec=" << exec << std::endl - << "Terminal=false" << std::endl - << "Type=Application" << std::endl - << "Icon=" << item_icon_path.string() << std::endl; - desktop_item.close(); + if (auto desktop_item = std::ofstream(item_path.string())) { + desktop_item << "[Desktop Entry]" << std::endl + << "Name=" << item.package << std::endl + << "Exec=" << exec << std::endl + << "Terminal=false" << std::endl + << "Type=Application" << std::endl + << "Icon=" << item_icon_path.string() << std::endl; + } else { + BOOST_THROW_EXCEPTION(std::runtime_error("Failed to create desktop item")); + } - std::ofstream icon(item_icon_path.string()); - icon.write(item.icon.data(), item.icon.size()); - icon.close(); + if (auto icon = std::ofstream(item_icon_path.string())) + icon.write(item.icon.data(), item.icon.size()); + else + BOOST_THROW_EXCEPTION(std::runtime_error("Failed to write icon")); } } // namespace application } // namespace anbox