Add fs-helper function for `get_user_list()` API call 83/274283/5
authorAdam Michalski <a.michalski2@partner.samsung.com>
Mon, 25 Apr 2022 15:04:46 +0000 (17:04 +0200)
committerAdam Michalski <a.michalski2@partner.samsung.com>
Wed, 27 Apr 2022 10:29:06 +0000 (12:29 +0200)
Change-Id: I6781895f195591737ad1a1f60149d91e86d3c317

sessiond/src/fs_helpers.cpp
sessiond/src/fs_helpers.h

index 8efeddaf0ca220703f4247ebeb81772986ccd405..ccf7522e7418a028c21ee6870d04bb0add182524 100644 (file)
@@ -209,3 +209,51 @@ void fs_helpers::remove_user_subsession(const int session_uid, const int subsess
        }
 }
 
+fs::path fs_helpers::get_subsession_dir_by_uid(const int session_uid)
+{
+       std::string home_dir = get_home_dir_by_user_id(session_uid);
+
+       return fs::path {
+               std::move(home_dir) + "/" + main_dir_name.data()
+       };
+}
+
+std::vector<int> fs_helpers::get_user_list(const int session_uid) try
+{
+       auto const subsession_path = get_subsession_dir_by_uid(session_uid);
+       std::vector<int> subsessions;
+
+       /* NB: the `subsession` folder may not exist if no
+        * subsessions have been created before. */
+       if (!fs::exists(subsession_path))
+               return subsessions;
+
+       for (auto const& entry : fs::directory_iterator(subsession_path)) {
+               if (!fs::is_directory(entry.status()))
+                       continue;
+
+               std::string_view s_path = entry.path().filename().string();
+
+               /* NB: the std::stoi check below is insufficient,
+                * since you can create folders named +123 or 0x123. */
+               if (s_path.find_first_not_of("0123456789") != std::string::npos)
+                       continue;
+
+               try {
+                       const int subsession_id { std::stoi(s_path.data()) };
+                       if (subsession_id > 0)
+                               subsessions.emplace_back(subsession_id);
+               }
+               catch (std::exception const &ex) {
+                       continue;
+               }
+       }
+
+       return subsessions;
+} catch (std::exception const &ex) {
+       std::cerr << "Exception " << ex.what() << std::endl
+               << "while enumerating user subsessions [session_uid="
+               << session_uid << "]" << std::endl;
+       throw std::runtime_error("Couldn't enumerate user subsessions");
+}
+
index f9eccbd7bb21d939692a4d8975088645c92d04c9..e806ed6b44684b434f0087ec7f7485c818924a64 100644 (file)
@@ -1,15 +1,22 @@
 #pragma once
+#include <filesystem>
 #include <string>
 #include <string_view>
+#include <vector>
+
+namespace fs = std::filesystem;
 
 namespace fs_helpers
 {
        std::string get_home_dir_by_user_id(const int uid);
        int get_gid_from_name(std::string_view group_name);
+       fs::path get_subsession_dir_by_uid(const int session_uid);
+
        void create_main_subdirectory(const int session_uid, std::string_view main_dir);
-       void  copy_smack_attributes(const std::string &src_path, const std::string &dest_path);
-       void    add_user_subsession(const int session_uid, const int subsession_id);
+       void copy_smack_attributes(const std::string &src_path, const std::string &dest_path);
+       void add_user_subsession(const int session_uid, const int subsession_id);
        void remove_user_subsession(const int session_uid, const int subsession_id);
+       std::vector<int> get_user_list(const int session_uid);
 
        constexpr static std::string_view main_dir_name  = "subsession";
        constexpr static std::string_view main_dir_group = "system_share";