2 * Copyright (c) 2008-2018 by Andreas Schneider <asn@samba.org>
4 * Adopted from the csync source code
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #include <sys/types.h>
31 #include "system/locale.h"
32 #include "lib/util/asn1.h"
33 #include "lib/util/debug.h"
34 #include "lib/util/samba_util.h"
39 int tftw(TALLOC_CTX *mem_ctx, const char *fpath, tftw_walker_fn fn, size_t depth, void *userdata)
41 char *filename = NULL;
44 struct dirent *dirent = NULL;
48 if (fpath[0] == '\0') {
53 if ((dh = opendir(fpath)) == NULL) {
54 /* permission denied */
55 if (errno == EACCES) {
58 DBG_ERR("opendir failed for: [%s]\n", strerror(errno));
63 while ((dirent = readdir(dh))) {
66 d_name = dirent->d_name;
71 /* skip "." and ".." */
72 if (d_name[0] == '.' && (d_name[1] == '\0'
73 || (d_name[1] == '.' && d_name[2] == '\0'))) {
78 filename = talloc_asprintf(mem_ctx, "%s/%s", fpath, d_name);
79 if (filename == NULL) {
83 rc = lstat(filename, &sb);
89 switch (sb.st_mode & S_IFMT) {
91 flag = TFTW_FLAG_SLINK;
100 flag = TFTW_FLAG_SPEC;
103 flag = TFTW_FLAG_FILE;
107 DBG_INFO("walk: [%s]\n", filename);
109 /* Call walker function for each file */
110 rc = fn(mem_ctx, filename, &sb, flag, userdata);
113 DBG_ERR("provided callback fn() failed: [%s]\n",
119 if (flag == TFTW_FLAG_DIR && depth) {
120 rc = tftw(mem_ctx, filename, fn, depth - 1, userdata);
126 TALLOC_FREE(filename);
132 TALLOC_FREE(filename);
138 TALLOC_FREE(filename);