You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
ubl-settings-diskquota/source/system_quotas.cc

263 lines
8.4 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

#include "system_quotas.h"
#include "my_type.h"
namespace Quotas_sys {
Quotas_sys::Quotas_sys() {
map_hw_or_sw["ext2"] = "hw";
map_hw_or_sw["ext3"] = "hw";
map_hw_or_sw["nfs"] = "sw";
map_hw_or_sw["NFS"] = "sw";
}
void Quotas_sys::set_map_device(map_str_str &map_device) {
this->map_device = &map_device;
this->check_partition_mount();
}
void Quotas_sys::set_map_gui(map_str_str &map_gui) {
map_gui_cfg = &map_gui;
}
void Quotas_sys::check_partition_mount() {
string key = "";
string response = Utils::file_read("/proc/mounts");
for (const auto& iter: (*map_device)) {
key = iter.first;
if (response.find(key) != string::npos) {
disk_mount[key] = true;
}
}
}
string Quotas_sys::wrapper_hw_sw(string partition) {
if ((*map_device).find(partition) != (*map_device).end()) {
string fsystem = (*map_device)[partition];
if ((*map_device).find(fsystem) != (*map_device).end()) {
return map_hw_or_sw[fsystem];
}
}
return "";
}
bool Quotas_sys::check_on_quotas_system(string cmd) {
string response = obj_process_call.call_all_sections(cmd);
if (response.length() > 0) {
return true;
}
else {
return false;
}
}
struct status_quotas Quotas_sys::check_on_quotas_system_2_hw(string params) {
string cmd = "quotaon -ugPvp " + params;
string response = obj_process_call.call_all_sections(cmd);
struct status_quotas status;
if (response.length() > 0 && response.find("Cannot find mountpoint for device ") == string::npos) {
status.status = true;
vector<string> vec_str = Utils::split(response, '\n');
string array_str[3] = {"group", "user", "project"};
for (int index = 0; index < 3; index++) {
string u_g_p = array_str[index];
for (const string& vec_u_g_p : vec_str) {
if (vec_u_g_p.find(u_g_p) != string::npos && vec_u_g_p.find("accounting") != string::npos ) {
if (index == 0) {
status.group = true;
}
else if (index == 1) {
status.user = true;
}
else if (index == 2) {
status.project = true;
}
}
else if (vec_u_g_p.find(u_g_p) != string::npos && vec_u_g_p.find("enforced") != string::npos){
if (index == 0) {
status.group = false;
}
else if (index == 1) {
status.user = false;
}
else if (index == 2) {
status.project = false;
}
}
}
}
}
else {
status.group = false;
status.user = false;
status.project = false;
status.status = false;
}
return status;
}
bool Quotas_sys::wrapper_on_quotas(string partition, string flag_mode) {
// Todo
// Добавить выключение квот в зависимости от диска
cout << partition << "" << flag_mode << endl;
if (disk_mount.find(partition) == disk_mount.end()) {
}
else {
}
return true;
}
bool Quotas_sys::on_quota_hw(string partition, string cmd_hw, string cmd_sw) {
/*
Включить квоты, только для HW ext2,3,4:
Если ФС, уже существует и без HW атрибут квот ext2,3,4
Раздел должен быть отмонтирован
*/
if (this->wrapper_hw_sw(partition) == "hw") {
obj_process_system.call(cmd_hw, "");
obj_process_system.call("ubconfig set config SERVICESSTART+=,quotaon.service", "");
return true;
}
else if (this->wrapper_hw_sw(partition) == "sw") {
obj_process_system.call(cmd_sw, "");
obj_process_system.call("ubconfig set config SERVICESSTART+=,quotaon.service", "");
return true;
}
else{
return false;
}
return false;
}
bool Quotas_sys::on_quota_sw(string partition, int mode) {
/*
Включить квоты, только для SW, другие ФС и сетевые NFS:
У этого исполнения квот отсутствуют проектные квоты prjquota
Раздел должен быть примонтирован
*/
if (map_hw_or_sw.find(partition) != map_hw_or_sw.end()) {
string cmd = "";
if (mode == 0) {
cmd += "mount -vo remount,usrquota " + partition;
cmd += "; quotacheck -ucm " + partition;
cmd += "; quotaon -uv " + partition;
}
else if (mode == 1) {
cmd += "; mount -vo remount,grpquota " + partition;
cmd += "; quotacheck -gcm " + partition;
cmd += "; quotaon -gv " + partition;
}
else if (mode == 2) {
cmd += "; quotaon -ugPv " + partition;
}
else if (mode == 3) {
cmd += "; quotaon -augPv";
}
else {
return false;
}
cmd += "; ubconfig set config SERVICESSTART+=,quotaon.service";
obj_process_system.call(cmd, "");
return true;
}
return false;
}
bool Quotas_sys::off_quota(string partition, string quota_hw, string quota_sw) {
string cmd = "";
if (map_hw_or_sw.find(partition) != map_hw_or_sw.end()) {
if (this->wrapper_hw_sw(partition) == "hw") {
cmd = quota_hw + " " + partition;
obj_process_system.call(cmd, "");
return true;
}
else if (this->wrapper_hw_sw(partition) == "sw") {
cmd = quota_sw + " " + partition;
obj_process_system.call(cmd, "");
return true;
}
}
return false;
}
vector<data_all_quotas> Quotas_sys::pars_data(string cmd) {
string start_pars = "------\n";
string response = obj_process_call.call_all_sections(cmd);
size_t index_start = response.find(start_pars);
string line_pars = "";
struct data_all_quotas struct_data;
vector<data_all_quotas> vec_struct_data;
int index = 0;
int old_index = index;
if (index_start != string::npos) {
response = response.substr(index_start+start_pars.length(), response.length());
vector<int> vec_index_sep = Utils::find_all(response, "\n");
for (size_t i = 0; i < vec_index_sep.size(); i++) {
old_index = index;
index = vec_index_sep[i];
if (index-1 == old_index) {
break;
}
line_pars = response.substr(old_index, index);
struct_data = part_quotas_line(line_pars);
vec_struct_data.push_back(struct_data);
}
}
return vec_struct_data;
}
struct data_all_quotas Quotas_sys::part_quotas_line(string line) {
struct data_all_quotas struct_data;
vector<int> vec_index_sep = Utils::find_all(line, " ");
int old_index = 0;
string buff = "";
int index_data = 0;
for (int& index: vec_index_sep) {
if ((index - old_index) >= 2) {
buff = line.substr(old_index, index - old_index);
cout << buff << endl;
if (index_data == 0) {
struct_data.username = buff;
}
else if (index_data == 1) {
struct_data.status = buff;
}
else if (index_data == 2) {
struct_data.block_used = buff;
}
else if (index_data == 3) {
struct_data.block_soft = buff;
}
else if (index_data == 4) {
struct_data.block_hard = buff;
}
else if (index_data == 5) {
struct_data.block_grace = buff;
}
else if (index_data == 6) {
struct_data.file_used = buff;
}
else if (index_data == 7) {
struct_data.file_soft = buff;
}
else if (index_data == 8) {
struct_data.file_hard = buff;
}
else if (index == 9) {
struct_data.file_grace = buff;
}
index_data += 1;
}
old_index = index;
}
if (index_data != 0) {struct_data.is_validate = true; }
else {struct_data.is_validate = false; }
return struct_data;
}
}