+bool FileUtil::file_exist(const char *file_name_with_path, bool writable) {
+ bool ret_val;
+ int acl_mode;
+
+ ret_val = false;
+ if (writable == true) {
+ acl_mode = W_OK;
+ }
+ else {
+ acl_mode = R_OK;
+ }
+ if (file_name_with_path != NULL) {
+ if (access(file_name_with_path, acl_mode) == 0) {
+ ret_val = true;
+ }
+ }
+ return ret_val;
+}
+
+bool FileUtil::mkfile(const char *file_name_with_path, bool writable) {
+ bool ret_val;
+ string path_name;
+ const char *acl;
+ int acl_mode;
+ FILE *fp;
+ bool succ_bool;
+
+ ret_val = false;
+ acl = "a";
+ if (writable == true) {
+ acl_mode = W_OK;
+ }
+ else {
+ acl_mode = R_OK;
+ }
+ if (file_name_with_path != NULL) {
+ if (access(file_name_with_path, F_OK) != 0) {
+ // file does not exist, create it
+ path_name = parse_directory_path(file_name_with_path);
+ succ_bool = true;
+ if (access(path_name.c_str(), F_OK) != 0) {
+ // path to file does not exist, create it
+ succ_bool = mkdirs(path_name.c_str());
+ }
+ if (succ_bool == true) {
+ // create file with read or write acl rights
+ fp = fopen(file_name_with_path, acl);
+ if (fp != NULL) {
+ ret_val = true;
+ fclose(fp);
+ }
+ }
+ }
+ else {
+ // file already exist, check access rights and change if needed
+ ret_val = true;
+ if (access(file_name_with_path, acl_mode) != 0) {
+ // file does not have specified acl righs, try to grant them
+ fp = fopen(file_name_with_path, acl);
+ if (fp != NULL) {
+ ret_val = true;
+ fclose(fp);
+ }
+ }
+ }
+ }
+ return ret_val;
+
+/*
+ path_name = parse_directory_path(file_name_with_path)
+ if (cfg_dir.empty() == false) {
+ if (access(path_name.c_str(), F_OK) != 0) {
+ // config dir does not exist, create it
+ FileUtil::mkdirs(cfg_dir.c_str());
+ }
+ if (access(cfg_dir.c_str(), R_OK) != 0) {
+ // config dir does not have read access, add it
+ fp = fopen(cfg_dir.c_str(), "r+");
+ if (fp != NULL)
+ fclose(fp);
+ }
+ fname_full = get_config_file_name();
+ if (access(fname_full.c_str(), F_OK) != 0) {
+ // config file does not exist, create empty one
+ fp = fopen(fname_full.c_str(), "w+");
+ if (fp != NULL)
+ fclose(fp);
+ }
+ if (access(fname_full.c_str(), R_OK) != 0) {
+ // config file does not have read access, add it
+ fp = fopen(fname_full.c_str(), "r+");
+ if (fp != NULL)
+ fclose(fp);
+ }
+ if (access(fname_full.c_str(), R_OK) == 0) {
+
+ }
+*/
+}
+