#include "fs_helpers.h"
namespace fs = std::filesystem;
+using namespace std::string_literals;
std::string fs_helpers::get_home_dir_by_user_id(const int uid)
{
if (ret)
throw std::system_error(errno, std::system_category(),
- std::string("Couldn't stat `")
+ "Couldn't stat `"s
+ src_path.data()
+ "` file/directory");
if (chown(dest_path.data(), info.st_uid, info.st_gid) == -1)
throw std::system_error(errno, std::system_category(),
- std::string("Couldn't set owner/group of the `")
+ "Couldn't set owner/group of the `"s
+ dest_path.data()
+ "` file/directory");
}
-void fs_helpers::copy_smack_attributes(const std::string &src_path, const std::string &dest_path)
+std::string fs_helpers::get_smack_label(std::string_view src_path, smack_label_type type)
+{
+ char *label_raw = nullptr;
+ int ret = smack_getlabel(src_path.data(), &label_raw, type);
+
+ std::unique_ptr<char, decltype([] (char *p) {
+ free(p);
+ })> label { label_raw };
+
+ if (ret)
+ throw std::runtime_error(
+ "Couldn't get SMACK attributes from source directory: "s +
+ src_path.data());
+
+ std::string out_str = "";
+ if (label.get())
+ out_str = label.get();
+
+ return out_str;
+}
+
+void fs_helpers::copy_smack_attributes(std::string_view src_path, std::string_view dest_path)
{
static const enum smack_label_type label_types[] = {
SMACK_LABEL_ACCESS,
};
for (const auto type : label_types) {
- char *label = nullptr;
- int ret = smack_getlabel(src_path.c_str(), &label, type);
- if (ret) {
- if (label)
- free(label);
- throw std::runtime_error("Couldn't get SMACK attributes from source directory: " +
- src_path);
- }
+ auto label = get_smack_label(src_path, type);
+ int ret = 0;
if (type == SMACK_LABEL_TRANSMUTE) {
// N.B. Setting TRANSMUTE attribute needs special attention:
// the only correct values are: NULL, "", "0" or "1".
- if (label != nullptr && strcmp(label, "TRUE") == 0)
- ret = smack_setlabel(dest_path.c_str(), "1", type);
+ if (label == "TRUE")
+ ret = smack_setlabel(dest_path.data(), "1", type);
}
else
- ret = smack_setlabel(dest_path.c_str(), label, type);
-
- if (ret) {
- if (label)
- free(label);
- throw std::runtime_error("Couldn't set SMACK attributes of destination directory: " +
- dest_path);
- }
+ ret = smack_setlabel(dest_path.data(), label.c_str(), type);
- free(label);
+ if (ret)
+ throw std::runtime_error(
+ "Couldn't set SMACK attributes of destination directory: "s +
+ dest_path.data());
}
}
group pass_grp_buf, *pass_grp_ptr;
getgrnam_r(main_dir_group.data(), &pass_grp_buf, str_grp_buf.get(), max_grp_buf_size, &pass_grp_ptr);
if (!pass_grp_ptr)
- throw std::runtime_error(std::string("Couldn't get Unix gid for `")
+ throw std::runtime_error("Couldn't get Unix gid for `"s
+ main_dir_group.data()
- + std::string("` group"));
+ + "` group");
return pass_grp_ptr->gr_gid;
}
if (chown(main_dir.data(), session_uid, gid) == -1)
throw std::system_error(errno, std::system_category(),
- std::string("Couldn't set owner of the `")
+ "Couldn't set owner of the `"s
+ main_dir_name.data()
+ "` subdirectory");
int ret = smack_setlabel(main_dir.data(), main_dir_smack.data(), SMACK_LABEL_ACCESS);
if (ret)
- throw std::runtime_error(std::string("Couldn't set SMACK attributes for `")
+ throw std::runtime_error("Couldn't set SMACK attributes for `"s
+ main_dir_name.data()
+ "` subdirectory");
}
std::string tmp_path = s_path;
tmp_path.erase(0, source_dir_len);
- std::string d_path = apps_rw_tmp_dir + tmp_path;
+ std::string d_path = apps_rw_tmp_dir + std::move(tmp_path);
copy_ownership(s_path, d_path);
copy_smack_attributes(s_path, d_path);