/* * FileUtil.cc * * Created on: Mar 5, 2011 * Author: lamikr */ #include #include #include #include #include #include #include #include #include #include "log.h" #include "FileUtil.hh" #include "DeviceConfig.hh" using namespace std; using namespace plp; template bool string_to_number(NumberDataType& result, const std::string& string_param, std::ios_base& (*format)(std::ios_base&)) { std::istringstream iss(string_param); return !(iss >> format >> result).fail(); } FileUtil::FileUtil() { } FileUtil::~FileUtil() { } /* * 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; string ret_val; ret = NULL; if (file_name_with_path != NULL) { f_size = 0; 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 = 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; } bool FileUtil::mkdirs(const char *path) { bool ret_val; char *p; int err_flg; ret_val = true; if (path != NULL) { 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); 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 { ret_val = false; log_error("Could not create NULL directory\n"); } 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) { string path_name; size_t b_count; ofstream *ret_val; bool b_flg; ret_val = NULL; b_flg = true; if (f_path != NULL) { b_count = strlen(f_path); if ((f_path[b_count - 1] != '/') && (f_path[b_count - 1] != '\\')) { ret_val = new ofstream(); ret_val->open(f_path, ios::app); if (ret_val->is_open() == false) { 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); } } } else { log_error("Could not open file, invalid file name. (%s)\n", f_path); } } else { log_error("Could not open file, invalid file name. (= NULL)\n"); } return ret_val; } string FileUtil::concat_paths(string path_start, string path_end) { return concat_paths(path_start.c_str(), path_end.c_str()); } string FileUtil::concat_paths(const char *path_start, const char *path_end) { string ret_val; string end_str; int pos; int b_count; if (path_start != NULL) { ret_val = path_start; if (path_end != NULL) { end_str = path_end; b_count = ret_val.length(); pos = ret_val.find_last_of("/"); if (pos == (b_count -1)) { ret_val.append(end_str); } else { pos = end_str.find_first_of("/"); if (pos == 0) { ret_val.append(end_str); } else { ret_val.append("/"); ret_val.append(end_str); } } } } else { if (path_end != NULL) { ret_val = path_end; } } return ret_val; } bool FileUtil::is_subdirectory(const char *path, dirent *direntry) { bool ret_val; struct stat stat_info; string fname; ret_val = false; if (direntry != NULL) { if ((strcmp(direntry->d_name, ".") == 0) || (strcmp(direntry->d_name, "..") == 0)) { ret_val = false; } else { fname = concat_paths(path, direntry->d_name); stat(fname.c_str(), &stat_info); ret_val = S_ISDIR(stat_info.st_mode); //log_debug("stat for: %s: %d\n", fname.c_str(), ret_val); } } return ret_val; } bool FileUtil::is_datafile(const char *path, dirent *direntry) { bool ret_val; struct stat stat_info; string name; int pos; string fname; ret_val = false; if (direntry != NULL) { name = direntry->d_name; pos = name.find(DATAFILE_SUFFIX); if (pos > 0) { fname = concat_paths(path, direntry->d_name); stat(fname.c_str(), &stat_info); ret_val = S_ISREG(stat_info.st_mode); } } return ret_val; } /** * get sub-directories sorted in alphabetical order. */ vector FileUtil::get_subdirectories(const string& path) { dirent *direntry; DIR *dir; bool bool_flg; vector ret_val; //log_debug("scanning path: %s\n", path.c_str()); errno = 0; if (path.empty() == false) { dir = opendir(path.c_str()); if (dir) { while (true) { errno = 0; direntry = readdir(dir); if (direntry != NULL) { bool_flg = is_subdirectory(path.c_str(), direntry); if (bool_flg == true) { ret_val.push_back(string(direntry->d_name)); //log_debug("added dir: %s\n", direntry->d_name); } } else { break; } } closedir(dir); sort(ret_val.begin(), ret_val.end()); } } return ret_val; } /** * get sub-directories sorted in alphabetical order. */ vector FileUtil::get_data_files(const string& path) { dirent *direntry; DIR *dir; vector ret_val; errno = 0; if (path.empty() == false) { dir = opendir(path.c_str()); if (dir) { while (true) { errno = 0; direntry = readdir(dir); if (direntry != NULL) { if (is_datafile(path.c_str(), direntry) == true) { ret_val.push_back(string(direntry->d_name)); } } else { break; } } closedir(dir); sort(ret_val.begin(), ret_val.end()); } } return ret_val; }