62 #include <sys/types.h>
65 #include <amxc/amxc_macros.h>
66 #include <amxc/amxc.h>
75 struct dirent* ep = (
struct dirent*) priv;
78 if(strcmp(
path,
"d_ino") == 0) {
79 amxc_var_set(uint32_t, value, ep->d_ino);
80 }
else if(strcmp(
path,
"d_type") == 0) {
81 amxc_var_set(uint32_t, value, ep->d_type);
82 }
else if(strcmp(
path,
"d_name") == 0) {
83 amxc_var_set(cstring_t, value, ep->d_name);
84 }
else if(strcmp(
path,
"DT_BLK") == 0) {
85 amxc_var_set(uint32_t, value, DT_BLK);
86 }
else if(strcmp(
path,
"DT_CHR") == 0) {
87 amxc_var_set(uint32_t, value, DT_CHR);
88 }
else if(strcmp(
path,
"DT_DIR") == 0) {
89 amxc_var_set(uint32_t, value, DT_DIR);
90 }
else if(strcmp(
path,
"DT_FIFO") == 0) {
91 amxc_var_set(uint32_t, value, DT_FIFO);
92 }
else if(strcmp(
path,
"DT_LNK") == 0) {
93 amxc_var_set(uint32_t, value, DT_LNK);
94 }
else if(strcmp(
path,
"DT_REG") == 0) {
95 amxc_var_set(uint32_t, value, DT_REG);
96 }
else if(strcmp(
path,
"DT_SOCK") == 0) {
97 amxc_var_set(uint32_t, value, DT_SOCK);
98 }
else if(strcmp(
path,
"DT_UNKNOWN") == 0) {
99 amxc_var_set(uint32_t, value, DT_UNKNOWN);
118 struct dirent* ep = NULL;
119 amxc_string_t filename;
120 size_t path_len = strlen(
path);
121 amxc_string_init(&filename, 128);
127 for(ep = readdir(dp); ep; ep = readdir(dp)) {
128 if(ep->d_name[0] ==
'.') {
131 if(
path[path_len - 1] ==
'/') {
132 amxc_string_setf(&filename,
"%s%s",
path, ep->d_name);
134 amxc_string_setf(&filename,
"%s/%s",
path, ep->d_name);
138 retval = fn(amxc_string_get(&filename, 0), priv);
139 when_failed(retval, exit);
142 if(recursive && (ep->d_type == DT_DIR)) {
143 retval =
amxp_dir_scan_impl(amxc_string_get(&filename, 0), expr, recursive, fn, priv);
144 when_failed(retval, exit);
149 amxc_string_clean(&filename);
162 when_str_empty(
path, exit);
164 if(stat(
path, &sb) == 0) {
170 when_null(dir, exit);
183 if(stat(dir, &sb) == 0) {
189 retval = mkdir(dir, mode);
190 when_failed(retval, exit);
191 if((uid != 0) || (gid != 0)) {
192 retval = chown(dir, uid, gid);
193 when_failed(retval, exit);
199 if(stat(dir, &sb) != 0) {
200 retval = mkdir(dir, mode);
201 when_failed(retval, exit);
202 if((uid != 0) || (gid != 0)) {
203 retval = chown(dir, uid, gid);
223 char* real_path = NULL;
225 when_str_empty(
path, exit);
227 real_path = realpath(
path, NULL);
228 when_null(real_path, exit);
230 if((filter != NULL) && (*filter != 0)) {
245 when_str_empty(
path, exit);
260 when_str_empty(
path, exit);
262 when_false(stat(
path, &sb) == 0, exit);
263 when_false((sb.st_mode & S_IFMT) == S_IFDIR, exit);
static int amxp_dir_scan_impl(const char *path, amxp_expr_t *expr, bool recursive, amxp_dir_match_fn_t fn, void *priv)
static amxp_expr_status_t amxp_expr_dir_get_field(UNUSED amxp_expr_t *expr, amxc_var_t *value, const char *path, void *priv)
static int amxp_dir_check_is_empty(UNUSED const char *name, UNUSED void *priv)
Ambiorix directory utilities.
Ambiorix expression parser and evaluate API header file.
static struct timeval current
int amxp_dir_make(const char *path, const mode_t mode)
Creates sub-directories.
int amxp_dir_scan(const char *path, const char *filter, bool recursive, amxp_dir_match_fn_t fn, void *priv)
Scans a directory and calls a callback function for each matching entry found.
int(* amxp_dir_match_fn_t)(const char *name, void *priv)
Matching file/directory callback function signature.
bool amxp_dir_is_empty(const char *path)
Checks if a directory is empty.
int amxp_dir_owned_make(const char *path, const mode_t mode, uid_t uid, gid_t gid)
Creates sub-directories and changes ownership.
bool amxp_dir_is_directory(const char *path)
Checks if a path is a directory.
amxp_expr_status_t amxp_expr_new(amxp_expr_t **expr, const char *expression)
Allocates and initializes an expression.
bool amxp_expr_evaluate(amxp_expr_t *expr, amxp_expr_get_field_t fn, void *priv, amxp_expr_status_t *status)
Evaluates an expression.
enum _expr_status amxp_expr_status_t
Expression status/error codes.
void amxp_expr_delete(amxp_expr_t **expr)
Deletes a previously allocated expression structure.
@ amxp_expr_status_field_not_found
static amxc_string_t path