libamxp  1.4.0
Patterns C Implementation
amxp_dir.c File Reference
#include <stdlib.h>
#include <string.h>
#include <dirent.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <amxc/amxc_macros.h>
#include <amxc/amxc.h>
#include "amxp/amxp_expression.h"
#include "amxp/amxp_dir.h"

Go to the source code of this file.

Macros

#define _GNU_SOURCE
 

Functions

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)
 
static int amxp_dir_scan_impl (const char *path, amxp_expr_t *expr, bool recursive, amxp_dir_match_fn_t fn, void *priv)
 
int amxp_dir_owned_make (const char *path, const mode_t mode, uid_t uid, gid_t gid)
 Creates sub-directories and changes ownership. More...
 
int amxp_dir_make (const char *path, const mode_t mode)
 Creates sub-directories. More...
 
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. More...
 
bool amxp_dir_is_empty (const char *path)
 Checks if a directory is empty. More...
 
bool amxp_dir_is_directory (const char *path)
 Checks if a path is a directory. More...
 

Macro Definition Documentation

◆ _GNU_SOURCE

#define _GNU_SOURCE

Definition at line 56 of file amxp_dir.c.

Function Documentation

◆ amxp_dir_check_is_empty()

static int amxp_dir_check_is_empty ( UNUSED const char *  name,
UNUSED void *  priv 
)
static

Definition at line 107 of file amxp_dir.c.

107  {
108  return -1;
109 }

◆ amxp_dir_scan_impl()

static int amxp_dir_scan_impl ( const char *  path,
amxp_expr_t expr,
bool  recursive,
amxp_dir_match_fn_t  fn,
void *  priv 
)
static

Definition at line 111 of file amxp_dir.c.

115  {
116  int retval = -1;
117  DIR* dp = NULL;
118  struct dirent* ep = NULL;
119  amxc_string_t filename;
120  size_t path_len = strlen(path);
121  amxc_string_init(&filename, 128);
122  dp = opendir(path);
123  when_null(dp, exit);
124 
125  retval = 0;
126 
127  for(ep = readdir(dp); ep; ep = readdir(dp)) {
128  if(ep->d_name[0] == '.') {
129  continue;
130  }
131  if(path[path_len - 1] == '/') {
132  amxc_string_setf(&filename, "%s%s", path, ep->d_name);
133  } else {
134  amxc_string_setf(&filename, "%s/%s", path, ep->d_name);
135  }
136  if((expr == NULL) || amxp_expr_evaluate(expr, amxp_expr_dir_get_field, ep, NULL)) {
137  if(fn != NULL) {
138  retval = fn(amxc_string_get(&filename, 0), priv);
139  when_failed(retval, exit);
140  }
141  }
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);
145  }
146  }
147 
148 exit:
149  amxc_string_clean(&filename);
150  if(dp != NULL) {
151  closedir(dp);
152  }
153  return retval;
154 }
static int amxp_dir_scan_impl(const char *path, amxp_expr_t *expr, bool recursive, amxp_dir_match_fn_t fn, void *priv)
Definition: amxp_dir.c:111
static amxp_expr_status_t amxp_expr_dir_get_field(UNUSED amxp_expr_t *expr, amxc_var_t *value, const char *path, void *priv)
Definition: amxp_dir.c:71
bool amxp_expr_evaluate(amxp_expr_t *expr, amxp_expr_get_field_t fn, void *priv, amxp_expr_status_t *status)
Evaluates an expression.
static amxc_string_t path

◆ amxp_expr_dir_get_field()

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

Definition at line 71 of file amxp_dir.c.

74  {
75  struct dirent* ep = (struct dirent*) priv;
77 
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);
100  } else {
102  }
103 
104  return status;
105 }
enum _expr_status amxp_expr_status_t
Expression status/error codes.
@ amxp_expr_status_field_not_found
@ amxp_expr_status_ok