2 * Copyright (c) 2008-2018 by Andreas Schneider <asn@samba.org>
4 * Adopted from the csync source code
17 #include "system/locale.h"
18 #include "lib/util/asn1.h"
19 #include "lib/util/debug.h"
20 #include "lib/util/samba_util.h"
25 int tftw(TALLOC_CTX *mem_ctx, const char *fpath, tftw_walker_fn fn, size_t depth, void *userdata)
27 char *filename = NULL;
30 struct dirent *dirent = NULL;
34 if (fpath[0] == '\0') {
39 if ((dh = opendir(fpath)) == NULL) {
40 /* permission denied */
41 if (errno == EACCES) {
44 DBG_ERR("opendir failed for: [%s]\n", strerror(errno));
49 while ((dirent = readdir(dh))) {
52 d_name = dirent->d_name;
57 /* skip "." and ".." */
58 if (d_name[0] == '.' && (d_name[1] == '\0'
59 || (d_name[1] == '.' && d_name[2] == '\0'))) {
64 filename = talloc_asprintf(mem_ctx, "%s/%s", fpath, d_name);
65 if (filename == NULL) {
69 rc = lstat(filename, &sb);
75 switch (sb.st_mode & S_IFMT) {
77 flag = TFTW_FLAG_SLINK;
86 flag = TFTW_FLAG_SPEC;
89 flag = TFTW_FLAG_FILE;
93 DBG_INFO("walk: [%s]\n", filename);
95 /* Call walker function for each file */
96 rc = fn(mem_ctx, filename, &sb, flag, userdata);
99 DBG_ERR("provided callback fn() failed: [%s]\n",
105 if (flag == TFTW_FLAG_DIR && depth) {
106 rc = tftw(mem_ctx, filename, fn, depth - 1, userdata);
112 TALLOC_FREE(filename);
118 TALLOC_FREE(filename);
124 TALLOC_FREE(filename);