FileUtil::~FileUtil() {
}
-char *FileUtil::parse_directory_path(const char *file_path) {
+/*
+ * Parses directory path from the full filename and
+ * returns it's copy.
+ */
+string FileUtil::parse_directory_path(const char *file_name_with_path) {
char *p;
size_t b_count;
size_t f_size;
- char *ret_val;
+ char *ret;
+ string ret_val;
- ret_val = NULL;
- if (file_path != NULL) {
+ ret = NULL;
+ if (file_name_with_path != NULL) {
f_size = 0;
- b_count = strlen(file_path);
- for (p = &((char *)file_path)[b_count]; p != (char *)file_path; p--) {
+ b_count = strlen(file_name_with_path);
+ for (p = &((char *)file_name_with_path)[b_count]; p != (char *)file_name_with_path; p--) {
f_size++;
if ((*p == '/') ||
(*p == '\\')) {
b_count = (b_count - f_size) + 1;
- ret_val = strndup(file_path, b_count);
- log_debug("dir: %s\n", ret_val);
+ ret = strndup(file_name_with_path, b_count);
+ //log_debug("path ret_val: %s\n", ret);
break;
}
}
}
+ if (ret != NULL) {
+ ret_val = ret;
+ free(ret);
+ }
return ret_val;
}
ret_val = true;
if (path != NULL) {
- // go through each directory one by and and create if not exist
- for (p = (char *)path; *p; p++) {
- if ((p != path) &&
- ((*p == '/') ||
- (*p == '\\'))) {
- *p = '\0';
+ if (access(path, F_OK) != 0) {
+ // path does not exist, need to create it
+ // go through each directory one by one and create them if they do not yet exist
+ for (p = (char *)path; *p; p++) {
+ if ((p != path) &&
+ ((*p == '/') ||
+ (*p == '\\'))) {
+ *p = '\0';
+ // if dir does not exist, create it
+ if (access(path, F_OK) != 0) {
+ //log_debug("trying to create directory: %s\n", path);
+ err_flg = mkdir(path, S_IRWXU);
+ if (err_flg != 0) {
+ log_error("Could not create directory: %s\n", path);
+ ret_val = false;
+ break;
+ }
+ }
+ *p = '/';
+ }
+ }
+ if (ret_val == true) {
// if dir does not exist, create it
if (access(path, F_OK) != 0) {
//log_debug("trying to create directory: %s\n", path);
if (err_flg != 0) {
log_error("Could not create directory: %s\n", path);
ret_val = false;
- break;
}
}
- *p = '/';
}
}
- if (ret_val == true) {
- // if dir does not exist, create it
- if (access(path, F_OK) != 0) {
- //log_debug("trying to create directory: %s\n", path);
- err_flg = mkdir(path, S_IRWXU);
- if (err_flg != 0) {
- log_error("Could not create directory: %s\n", path);
- ret_val = false;
- }
- }
+ else {
+ ret_val = true;
}
}
else {
return ret_val;
}
+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) {
+
+ }
+*/
+}
+
std::ofstream *FileUtil::open_for_writing(const char *f_path) {
- char *d_path;
+ string path_name;
size_t b_count;
ofstream *ret_val;
bool b_flg;
ret_val = new ofstream();
ret_val->open(f_path, ios::app);
if (ret_val->is_open() == false) {
- d_path = parse_directory_path(f_path);
- if (d_path != NULL) {
- b_flg = mkdirs(d_path);
- free(d_path);
+ path_name = parse_directory_path(f_path);
+ if (path_name.empty() == false) {
+ b_flg = mkdirs(path_name.c_str());
}
if (b_flg == true) {
ret_val->open(f_path, ios::app);