40 static void rm_file(
struct ast_str *filename)
43 ast_log(LOG_ERROR,
"Unable to remove file: %s\n",
ast_str_buffer(filename));
49 static int test_files_destroy(
struct ast_test *
test,
char *dir_name,
59 if ((res = rmdir(dir_name)) < 0) {
60 ast_test_status_update(test,
"Failed to remove directory: %s\n", dir_name);
66 static int test_files_create(
struct ast_test *test,
char *dir_name,
71 if (!(mkdtemp(dir_name))) {
72 ast_test_status_update(test,
"Failed to create directory: %s\n", dir_name);
82 for (i = 0; i < num; ++i) {
94 ast_test_status_update(test,
"Failed to create file: %s\n",
105 test_files_destroy(test, dir_name, filenames);
112 static char *test_files_get_one(
struct _filenames *filenames,
int num)
119 static int handle_find_file(
const char *dir_name,
const char *filename,
void *obj)
122 char *full_path =
ast_alloca(strlen(dir_name) + strlen(filename) + 2);
124 sprintf(full_path,
"%s/%s", dir_name, filename);
127 if (stat(full_path, &statbuf)) {
128 ast_log(LOG_ERROR,
"Error reading path stats - %s: %s\n",
129 full_path, strerror(errno));
133 return strcmp(obj, filename) ? 0 : FOUND;
138 char tmp_dir[] =
"/tmp/tmpdir.XXXXXX";
141 enum ast_test_result_state res;
142 const int num_files = 10 + (ast_random() % 10);
146 info->name =
"read_dir_test";
147 info->category =
"/main/file/";
148 info->summary =
"Read a directory's content";
149 info->description =
"Iterate over directories looking for a file.";
150 return AST_TEST_NOT_RUN;
159 if (!(mkdtemp(tmp_dir))) {
160 ast_test_status_update(test,
"Failed to create directory: %s\n", tmp_dir);
161 return AST_TEST_FAIL;
164 tmp_sub_dir = ast_str_alloca(32);
165 ast_str_set(&tmp_sub_dir, 0,
"%s/XXXXXX", tmp_dir);
167 if (test_files_create(test,
ast_str_buffer(tmp_sub_dir), &filenames, num_files)) {
168 test_files_destroy(test, tmp_dir, NULL);
169 return AST_TEST_FAIL;
173 &filenames, num_files), 2) == FOUND ? AST_TEST_PASS : AST_TEST_FAIL;
175 if (test_files_destroy(test,
ast_str_buffer(tmp_sub_dir), &filenames) ||
176 test_files_destroy(test, tmp_dir, NULL)) {
183 static int unload_module(
void)
185 AST_TEST_UNREGISTER(read_dirs_test);
189 static int load_module(
void)
191 AST_TEST_REGISTER(read_dirs_test);
#define AST_VECTOR_FREE(vec)
Deallocates this vector.
Asterisk main include file. File version handling, generic pbx functions.
String manipulation functions.
char * ast_str_buffer(const struct ast_str *buf)
Returns the string buffer within the ast_str buf.
#define AST_VECTOR_APPEND(vec, elem)
Append an element to a vector, growing the vector if needed.
Generic File Format Support. Should be included by clients of the file handling routines. File service providers should instead include mod_format.h.
int ast_file_read_dirs(const char *dir_name, ast_file_on_file on_file, void *obj, int max_depth)
Recursively iterate through files and directories up to max_depth.
int ast_str_set(struct ast_str **buf, ssize_t max_len, const char *fmt,...)
Set a dynamic string using variable arguments.
#define AST_VECTOR_INIT(vec, size)
Initialize a vector.
Asterisk file paths, configured in asterisk.conf.
#define AST_VECTOR(name, type)
Define a vector structure.
#define ast_alloca(size)
call __builtin_alloca to ensure we get gcc builtin semantics
Support for dynamic strings.
Vector container support.
#define AST_VECTOR_GET(vec, idx)
Get an element from a vector.
#define AST_TEST_DEFINE(hdr)
#define ASTERISK_GPL_KEY
The text the key() function should return.
Asterisk module definitions.
#define AST_VECTOR_CALLBACK_VOID(vec, callback,...)
Execute a callback on every element in a vector disregarding callback return.
#define ast_str_create(init_len)
Create a malloc'ed dynamic length string.