}
// Create `$HOME/subsession` directory if it doesn't exist
-static void create_main_subdirectory(const int session_uid, std::string_view main_dir)
+static void create_main_subdirectory(const int session_uid, fs::path main_dir)
{
if (fs::exists(main_dir))
return;
int gid = OS::get_gid_from_name(system_share_group);
- if (chown(main_dir.data(), session_uid, gid) == -1)
+ if (chown(main_dir.c_str(), session_uid, gid) == -1)
throw std::system_error(errno, std::system_category(),
"Couldn't set owner of the `"s
- + main_dir_name.data()
+ + main_dir.native()
+ "` subdirectory");
- int ret = smack_setlabel(main_dir.data(), main_dir_smack.data(), SMACK_LABEL_ACCESS);
+ int ret = smack_setlabel(main_dir.c_str(), main_dir_smack.data(), SMACK_LABEL_ACCESS);
if (ret)
throw std::runtime_error("Couldn't set SMACK attributes for `"s
- + main_dir_name.data()
+ + main_dir.native()
+ "` subdirectory");
}
fs::create_directory(tmp_subsession_dir);
int system_share_gid = OS::get_gid_from_name(system_share_group);
- OS::change_owner_and_group(tmp_subsession_dir, session_uid, system_share_gid);
+ OS::change_owner_and_group(tmp_subsession_path, session_uid, system_share_gid);
std::string apps_rw_dir = tmp_subsession_dir + "/apps_rw";
fs::path apps_rw_path { apps_rw_dir };
| group_read | group_exec;
for (auto const& entry : fs::recursive_directory_iterator(source_dir)) {
- std::string s_path = entry.path();
+ const auto s_path = entry.path();
- std::string_view tmp_path = s_path;
+ std::string_view tmp_path = s_path.native();
tmp_path.remove_prefix(source_dir_len + 1);
- std::string d_path = apps_rw_dir + "/" + tmp_path.data();
+ const auto d_path = apps_rw_path / tmp_path;
int gid;
fs::perms permissions;
}
// Last but not least - the `apps_rw` directory itself
- OS::change_owner_and_group(apps_rw_dir, session_uid, system_share_gid);
- fs::permissions(apps_rw_dir, apps_rw_dir_perms);
- OS::copy_smack_attributes(source_dir, apps_rw_dir);
+ OS::change_owner_and_group(apps_rw_path, session_uid, system_share_gid);
+ fs::permissions(apps_rw_path, apps_rw_dir_perms);
+ OS::copy_smack_attributes(source_dir, apps_rw_path);
// Copy + rename so that the replacement is atomic
fs::rename(tmp_subsession_path, subsession_path);
return pass_grp_ptr->gr_gid;
}
-void OS::change_owner_and_group(std::string_view path, const int session_uid, const int group_id)
+void OS::change_owner_and_group(fs::path path, const int session_uid, const int group_id)
{
- if (lchown(path.data(), session_uid, group_id) == -1)
+ if (lchown(path.c_str(), session_uid, group_id) == -1)
throw std::system_error(errno, std::system_category(),
"Couldn't set owner/group of the `"s
- + path.data()
+ + path.native()
+ "` file/directory");
}
-std::string OS::get_smack_label(std::string_view src_path, smack_label_type type)
+std::string OS::get_smack_label(fs::path src_path, smack_label_type type)
{
char *label_raw = nullptr;
- int ret = smack_lgetlabel(src_path.data(), &label_raw, type);
+ int ret = smack_lgetlabel(src_path.c_str(), &label_raw, type);
std::unique_ptr<char, decltype([] (char *p) {
free(p);
if (ret)
throw std::runtime_error(
"Couldn't get SMACK attributes from source directory: "s +
- src_path.data());
+ src_path.native());
std::string out_str = "";
if (label.get())
return out_str;
}
-static int copy_label(std::string label, std::string_view dest_path, smack_label_type type)
+static int copy_label(std::string label, fs::path dest_path, smack_label_type type)
{
if (type != SMACK_LABEL_TRANSMUTE)
- return smack_lsetlabel(dest_path.data(), label.c_str(), type);
+ return smack_lsetlabel(dest_path.c_str(), label.c_str(), type);
/* Setting TRANSMUTE attribute needs special attention:
* the only correct values are: NULL, "", "0" or "1" */
if (label == "TRUE")
- return smack_lsetlabel(dest_path.data(), "1", type);
+ return smack_lsetlabel(dest_path.c_str(), "1", type);
/* N.B. This is a bit tricky. Since TRANSMUTE attribute is inheritable,
* it is possible that it was set to TRUE while copying the files from
* /etc/skel, but originally it wasn't there in the source directory for
* some subdirectories/files. Therefore it must be removed explicitly. */
- int ret = smack_lsetlabel(dest_path.data(), "0", type);
+ int ret = smack_lsetlabel(dest_path.c_str(), "0", type);
if (ret == -1 && errno == ENODATA) {
/* We tried to drop the label, but it already didn't exist,
* so the "error" is expected and not a problem. */
return ret;
}
-void OS::copy_smack_attributes(std::string_view src_path, std::string_view dest_path)
+void OS::copy_smack_attributes(fs::path src_path, fs::path dest_path)
{
static const enum smack_label_type label_types[] = {
SMACK_LABEL_ACCESS,
if (copy_label(label, dest_path, type))
throw std::runtime_error(
"Couldn't set SMACK attributes of destination directory: "s +
- dest_path.data());
+ dest_path.native());
}
}
-void OS::set_ownership_and_perms(std::string_view src_path, std::string_view dest_path, fs::perms permissions,
+void OS::set_ownership_and_perms(fs::path src_path, fs::path dest_path, fs::perms permissions,
const int session_uid, const int gid, bool copy_perms_from_skel)
{
const auto info = fs::symlink_status (src_path);
#include <string>
#include <string_view>
+namespace fs = std::filesystem;
+
namespace OS {
std::string get_home_dir_by_user_id(const int uid);
int get_gid_from_name(std::string_view group_name);
- void change_owner_and_group(std::string_view path, const int session_uid, const int group_id);
+ void change_owner_and_group(fs::path path, const int session_uid, const int group_id);
- std::string get_smack_label(std::string_view src_path, smack_label_type type);
+ std::string get_smack_label(fs::path src_path, smack_label_type type);
- void copy_smack_attributes(std::string_view src_path, std::string_view dest_path);
+ void copy_smack_attributes(fs::path src_path, fs::path dest_path);
- void set_ownership_and_perms(std::string_view src_path, std::string_view dest_path, std::filesystem::perms permissions,
+ void set_ownership_and_perms(fs::path src_path, fs::path dest_path, fs::perms permissions,
const int session_uid, const int gid, bool copy_perms_from_skel);
}