/** * @brief remove directory * * @param pathname [in] path of directory * * @return 0, if succ; -1, if failed. */ int remove_dir(const char *pathname) { struct dirent *dir = NULL; struct stat st = {0}; DIR *d = NULL; char buf[512] = {0}; if (pathname == NULL) return -1; if (lstat(pathname, &st) < 0) return -1; if (!S_ISDIR(st.st_mode)) return -1; if ((d = opendir(pathname)) == NULL) return -1; while ((dir = readdir(d)) != NULL) { if (!strcmp(dir->d_name, ".") || !strcmp(dir->d_name, "..")) continue; sprintf(buf, "%s/%s", pathname, dir->d_name); if (lstat(buf, &st) >= 0) { if (S_ISDIR(st.st_mode)) { remove_dir(buf); rmdir(buf); } else remove(buf); } } if (file_is_exist(pathname) == 1) rmdir(pathname); closedir(d); if (file_is_exist(pathname) == 1) return -1; return 0; }
/******************************************* *func name: *function: *parameters: *call: *called: *return: */ static int open_sq_list_file(char *file_path,int *fd_ptr,unsigned long *file_size_ptr) { int fd; unsigned long file_size; if (NULL == file_path) return(CTL_PAR_ERR); if (NOT_EXIST == file_is_exist(file_path)) { error("[Err]Protect rules file don't exist."); return(CTL_FILE_NOT_EXIST); } if ((fd = open(file_path,O_RDONLY | O_CREAT)) < 0) { error("[Err]Open protect rules file fail."); return(CTL_FILE_OPEN_FAIL); } if (0 == (file_size = get_file_size(file_path))) { error("[Err]Protect rules file no content."); close(fd); return(CTL_FILE_IS_NULL); } *fd_ptr = fd; *file_size_ptr = file_size; return(SAIL_OK); }
/******************************************* *func name: *function: *parameters: *call: *called: *return: */ int get_read_cfg_mode(char *file_path,int *fd_ptr,unsigned long *file_size_ptr) { int fd; unsigned long file_size; if (NULL == file_path) return(DEF_MODE); if (NOT_EXIST == file_is_exist(file_path)) return(DEF_MODE); if ((fd = open(file_path,O_RDONLY | O_CREAT)) < 0) return(DEF_MODE); if (0 == (file_size = get_file_size(file_path))) { close(fd); return(DEF_MODE); } *fd_ptr = fd; *file_size_ptr = file_size; return(READ_FILE); }
/** * @brief create a multi directoy * * @param pathname [in] path of directory * @param mode [in] mode of dir * * @return 0, if succ; -1, if failed. */ int make_dir(char *pathname, mode_t mode) { int i; int size = (pathname != NULL) ? strlen(pathname) : -1; if (size <= 0) return -1; for (i = 1; i < size; i++) { if (pathname[i] == '/') { pathname[i] = '\0'; if (file_is_exist(pathname) != 1) mkdir(pathname, mode); pathname[i] = '/'; } } if (file_is_exist(pathname) != 1) mkdir(pathname, mode); if (file_is_exist(pathname) == 1) return 0; return -1; }