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/model/save.c

219 lines
8.5 KiB

#ifndef SAVE_H
#define SAVE_H
#include "save.h"
char* save_str_users(config_u_g_p* config, int index, char* start_split) {
char* cmd = yon_char_new("");
char split_simvol[2] = {0};
split_simvol[0] = 0;
split_simvol[1] = 0;
vector* vec_actors = &config->actors;
char* str_actors = NULL;
if (vec_actors->vectorList.total> 0) {
for (int i = 0; i < vec_actors->vectorList.total; i++) {
if (i==0) {
cmd = yon_char_unite(cmd, start_split, NULL);
}
str_actors = (char*)vec_actors->pfVectorGet(vec_actors, i);
cmd = yon_char_unite(cmd, split_simvol, str_actors, NULL);
split_simvol[0] = ',';
split_simvol[1] = '\0';
}
}
return cmd;
}
char* template_format_save(vector* vec_temp) {
char* cmd = yon_char_new("");
if (vec_temp->vectorList.total> 0) {
for (int i = 0; i < vec_temp->pfVectorTotal(vec_temp); i++) {
config_u_g_p* _config = (config_u_g_p*)vec_temp->pfVectorGet(vec_temp, i);
char* key = save_format_key(_config, i);
char* value = save_format_str_value(_config, i);
if (key!= NULL) {
cmd = yon_char_unite(cmd, key, "=" , value, " ", NULL);
free(key);
free(value);
}
}
}
return cmd;
}
char* save_format_status_cmd_all(vector* vec_temp, vector* vec_status_disk) {
char* cmd = yon_char_new("");
char* str_cmd_format = NULL;
if (vec_temp->vectorList.total > 0 && vec_status_disk->vectorList.total > 0) {
for (int i=0; i < vec_temp->vectorList.total; i++) {
config_u_g_p* _config = (config_u_g_p*)vec_temp->pfVectorGet(vec_temp, i);
for (int j=0; j < vec_status_disk->vectorList.total;j++) {
disk_status* config_disk_status = (disk_status*)vec_status_disk->pfVectorGet(vec_status_disk, j);
if (strstr(config_disk_status->device, _config->UNIX_file) ) {
str_cmd_format = save_format_status_cmd(config_disk_status, _config);
if (str_cmd_format) {
cmd = yon_char_unite(cmd, " ", str_cmd_format, NULL);
free(str_cmd_format);
}
}
}
}
}
return cmd;
}
char* wrapper_save_format_status_cmd_all(config_all* _all_config) {
char* cmd = save_format_status_cmd_all(&_all_config->v_user, &_all_config->v_disk_status);
cmd = yon_char_unite(cmd, " ", save_format_status_cmd_all(&_all_config->v_group ,&_all_config->v_disk_status), NULL);
cmd = yon_char_unite(cmd, " ", save_format_status_cmd_all(&_all_config->v_project, &_all_config->v_disk_status), NULL);
return cmd;
}
char* save_format_status_cmd(disk_status* config_disk_status, config_u_g_p* config) {
char* cmd = yon_char_unite("DISK_QUOTA[", config->type_arr, ":", config->UNIX_file,"]=", NULL);;
if (config_disk_status->status == 2) {
cmd = yon_char_unite(cmd,"enable", NULL);
return cmd;
}
else if (config_disk_status->status == 1) {
return yon_char_unite(cmd,"disable", NULL);
}
else {
return NULL;
}
}
char* wrapper_template_format_save(config_all* _all_config, char* source_set_cmd) {
char* cmd_user = template_format_save(&_all_config->v_user);
char* cmd_group = template_format_save(&_all_config->v_group);
char* cmd_project = template_format_save(&_all_config->v_project);
char* cmd_status_disk = wrapper_save_format_status_cmd_all(_all_config);
if ((strlen(cmd_status_disk) > 3 || strlen(cmd_user) > 3 || strlen(cmd_group) > 3 || strlen(cmd_project) > 3) && !save_check_save(_all_config)) {
return yon_char_unite(source_set_cmd," ", cmd_status_disk, " ", cmd_user, " ", cmd_group, " ", cmd_project, NULL);
}
return NULL;
}
char* template_format_remove_save(config_all* _all_config, char* source_remove_cmd) {
if (_all_config->v_remove.vectorList.total > 0) {
char* cmd = yon_char_new(source_remove_cmd);
for (int i = 0; i < _all_config->v_remove.vectorList.total; i++) {
char* str_remove = (char*)_all_config->v_remove.pfVectorGet(&_all_config->v_remove, i);
cmd = yon_char_unite(cmd, " DISK_QUOTA[" , str_remove, "] ", NULL);
}
if (strlen(cmd) > strlen(source_remove_cmd) && save_check_save(_all_config)== 0) {
return cmd;
}
}
return NULL;
}
char* template_format_remove_save_device(config_all* _all_config, char* source_remove_cmd) {
if (_all_config->v_disk_status.vectorList.total > 0) {
char* cmd = yon_char_new("");
for (int i=0; i < _all_config->v_disk_status.vectorList.total; i++) {
disk_status* _config = (disk_status*)_all_config->v_disk_status.pfVectorGet(&_all_config->v_disk_status, i);
if (_config->status == 0) {
cmd = yon_char_unite(cmd, " DISK_QUOTA[usrquota" , _config->device, "] ", NULL);
cmd = yon_char_unite(cmd, " DISK_QUOTA[grpquota" , _config->device, "] ", NULL);
cmd = yon_char_unite(cmd, " DISK_QUOTA[prjquota" , _config->device, "] ", NULL);
}
}
if (strlen(cmd)>2) {
return cmd;
}
}
return NULL;
}
int wrapper_template_save(config_all* _all_config, char* source_set_cmd, char* source_remove_cmd) {
char* str_cmd_remove = template_format_remove_save(_all_config,source_remove_cmd);
char* str_cmd_remove_device = template_format_remove_save_device(_all_config,source_remove_cmd);
char* str_cmd_set = wrapper_template_format_save(_all_config,source_set_cmd);
if (str_cmd_set != NULL && str_cmd_remove != NULL && str_cmd_remove_device != NULL) {
philos_config_save(yon_char_unite(str_cmd_remove, " " ,str_cmd_remove_device, "; " , str_cmd_set, NULL));
}
else {
if (str_cmd_remove != NULL) {
philos_config_save(str_cmd_remove);
}
if (str_cmd_set != NULL) {
philos_config_save(str_cmd_set);
}
if (str_cmd_remove_device != NULL) {
philos_config_save(yon_char_unite(source_remove_cmd, " " , str_cmd_remove_device, NULL));
}
}
if (str_cmd_remove != NULL) {
free(str_cmd_remove);
}
if (str_cmd_set != NULL) {
free(str_cmd_set);
}
if (str_cmd_remove_device != NULL) {
free(str_cmd_remove_device);
}
_all_config->flag_set_data = 0;
return 1;
}
char* save_format_key(config_u_g_p* config, int index) {
char* value = yon_char_new("");
char* key = yon_char_new("");
int number = config->id;
key = yon_char_unite("DISK_QUOTA[", config->type_arr, ":", NULL);
if (strstr(config->type_arr, "prjquota")) {
if (number!=-1) {
key = yon_char_unite(key, config->UNIX_file,":",yon_char_from_int(number), NULL);
}
else {
key = yon_char_unite(key, config->UNIX_file,":AUTO", NULL);
}
key = yon_char_unite(key, save_str_users(config, index, ",") , "]", NULL);
}
else {
key = yon_char_unite(key, config->UNIX_file, NULL);
key = yon_char_unite(key, save_str_users(config, index, ":") , "]", NULL);
}
return key;
}
char* save_format_str_value(config_u_g_p* config, int index) {
char* value = NULL;
if (config->soft_restriction_size) {
value = yon_char_unite(philos_format_cfg_str_size_memory("", config->soft_restriction_size,config->soft_restriction_size_pow), NULL);
}
else {
value = yon_char_new("0");
}
if (config->severe_limitation_size) {
value = yon_char_unite(value, ":", philos_format_cfg_str_size_memory("", config->severe_limitation_size,config->severe_limitation_size_pow), NULL);
}
else {
value = yon_char_unite(value,":0", NULL);
}
value = yon_char_unite(value, ":", yon_char_from_int(config->soft_restriction_file), NULL);
value = yon_char_unite(value, ":", NULL);
value = yon_char_unite(value, yon_char_from_int(config->severe_limitation_file), NULL);
value = yon_char_unite(value, ":", NULL);
value = yon_char_unite(value, philos_char_from_size_t(config->deferring_size), ":", NULL);
value = yon_char_unite(value, philos_char_from_size_t(config->deferring_file), NULL);
return value;
}
int save_check_save(config_all* _all_config) {
if (_all_config->flag_set_data == 1 || _all_config->flag_save>0) {
return 0;
}
else if (_all_config->flag_set_data == 0 || _all_config->flag_save<=0) {
return 1;
}
return 0;
}
#endif