vfs: restore stat fields in vfs_stat_fsp()
[Samba.git] / lib / util / tftw.c
blobb731a2ef03af045a0d42586ea9730b9c3b2760d6
1 /*
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/>.
20 #include "replace.h"
21 #include <errno.h>
22 #include <sys/types.h>
23 #include <dirent.h>
24 #include <stdio.h>
25 #include <string.h>
26 #include <stdlib.h>
27 #include <sys/stat.h>
28 #include "memory.h"
29 #include "debug.h"
30 #include "replace.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"
36 #include "tftw.h"
39 int tftw(TALLOC_CTX *mem_ctx, const char *fpath, tftw_walker_fn fn, size_t depth, void *userdata)
41 char *filename = NULL;
42 char *d_name = NULL;
43 DIR *dh = NULL;
44 struct dirent *dirent = NULL;
45 struct stat sb = {0};
46 int rc = 0;
48 if (fpath[0] == '\0') {
49 errno = ENOENT;
50 goto error;
53 if ((dh = opendir(fpath)) == NULL) {
54 /* permission denied */
55 if (errno == EACCES) {
56 return 0;
57 } else {
58 DBG_ERR("opendir failed for: [%s]\n", strerror(errno));
59 goto error;
63 while ((dirent = readdir(dh))) {
64 int flag;
66 d_name = dirent->d_name;
67 if (d_name == NULL) {
68 goto error;
71 /* skip "." and ".." */
72 if (d_name[0] == '.' && (d_name[1] == '\0'
73 || (d_name[1] == '.' && d_name[2] == '\0'))) {
74 dirent = NULL;
75 continue;
78 filename = talloc_asprintf(mem_ctx, "%s/%s", fpath, d_name);
79 if (filename == NULL) {
80 goto error;
83 rc = lstat(filename, &sb);
84 if (rc < 0) {
85 dirent = NULL;
86 goto error;
89 switch (sb.st_mode & S_IFMT) {
90 case S_IFLNK:
91 flag = TFTW_FLAG_SLINK;
92 break;
93 case S_IFDIR:
94 flag = TFTW_FLAG_DIR;
95 break;
96 case S_IFBLK:
97 case S_IFCHR:
98 case S_IFSOCK:
99 case S_IFIFO:
100 flag = TFTW_FLAG_SPEC;
101 break;
102 default:
103 flag = TFTW_FLAG_FILE;
104 break;
107 DBG_INFO("walk: [%s]\n", filename);
109 /* Call walker function for each file */
110 rc = fn(mem_ctx, filename, &sb, flag, userdata);
112 if (rc < 0) {
113 DBG_ERR("provided callback fn() failed: [%s]\n",
114 strerror(errno));
115 closedir(dh);
116 goto done;
119 if (flag == TFTW_FLAG_DIR && depth) {
120 rc = tftw(mem_ctx, filename, fn, depth - 1, userdata);
121 if (rc < 0) {
122 closedir(dh);
123 goto done;
126 TALLOC_FREE(filename);
127 dirent = NULL;
129 closedir(dh);
131 done:
132 TALLOC_FREE(filename);
133 return rc;
134 error:
135 if (dh != NULL) {
136 closedir(dh);
138 TALLOC_FREE(filename);
139 return -1;