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/>.
21 #include "system/filesys.h"
22 #include "system/dir.h"
23 #include "lib/util/debug.h"
28 int tftw(TALLOC_CTX
*mem_ctx
, const char *fpath
, tftw_walker_fn fn
, size_t depth
, void *userdata
)
30 char *filename
= NULL
;
33 struct dirent
*dirent
= NULL
;
37 if (fpath
[0] == '\0') {
42 if ((dh
= opendir(fpath
)) == NULL
) {
43 /* permission denied */
44 if (errno
== EACCES
) {
47 DBG_ERR("opendir failed for: [%s]\n", strerror(errno
));
52 while ((dirent
= readdir(dh
))) {
55 d_name
= dirent
->d_name
;
60 /* skip "." and ".." */
61 if (d_name
[0] == '.' && (d_name
[1] == '\0'
62 || (d_name
[1] == '.' && d_name
[2] == '\0'))) {
67 filename
= talloc_asprintf(mem_ctx
, "%s/%s", fpath
, d_name
);
68 if (filename
== NULL
) {
72 rc
= lstat(filename
, &sb
);
78 switch (sb
.st_mode
& S_IFMT
) {
80 flag
= TFTW_FLAG_SLINK
;
89 flag
= TFTW_FLAG_SPEC
;
92 flag
= TFTW_FLAG_FILE
;
96 DBG_INFO("walk: [%s]\n", filename
);
98 /* Call walker function for each file */
99 rc
= fn(mem_ctx
, filename
, &sb
, flag
, userdata
);
102 DBG_ERR("provided callback fn() failed: [%s]\n",
108 if (flag
== TFTW_FLAG_DIR
&& depth
) {
109 rc
= tftw(mem_ctx
, filename
, fn
, depth
- 1, userdata
);
115 TALLOC_FREE(filename
);
121 TALLOC_FREE(filename
);
127 TALLOC_FREE(filename
);