@@ -49,7 +49,7 @@ limitations under the License.
4949 #define LSTAT _stat64
5050#else
5151 #if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__) || defined(__APPLE__) || defined(__MINGW32__)
52- #define LSTAT stat
52+ #define LSTAT lstat
5353 #else
5454 #define LSTAT lstat64
5555 #endif
@@ -100,6 +100,17 @@ namespace kanzi
100100 };
101101
102102
103+ #define HANDLE_DOT_FILES (f, ignore )\
104+ do {\
105+ if (ignore) {\
106+ size_t idx = f.rfind (PATH_SEPARATOR);\
107+ if ((idx != std::string::npos) && (idx < f.length () - 1 ) && (f[idx + 1 ] == ' .' ))\
108+ return ;\
109+ }\
110+ } while (false );
111+
112+
113+
103114 static inline void createFileList (std::string& target, std::vector<FileData>& files, const FileListConfig& cfg,
104115 std::vector<std::string>& errors)
105116 {
@@ -113,8 +124,9 @@ namespace kanzi
113124 target.resize (target.size () - 1 );
114125 #endif
115126
127+ HANDLE_DOT_FILES (target, cfg._ignoreDotFiles );
116128 struct STAT buffer;
117- int res = cfg. _ignoreLinks == true ? LSTAT (target. c_str (), &buffer) : STAT (target.c_str (), &buffer);
129+ int res = LSTAT (target.c_str (), &buffer);
118130
119131 if (res != 0 ) {
120132 std::stringstream ss;
@@ -125,26 +137,17 @@ namespace kanzi
125137 return ;
126138 }
127139
128- if ((buffer.st_mode & S_IFMT) == S_IFREG) {
129- // Target is regular file
130- if (cfg._ignoreDotFiles == true ) {
131- size_t idx = target.rfind (PATH_SEPARATOR);
132-
133- if ((idx != std::string::npos) && (idx < target.length () - 1 ) && (target[idx + 1 ] == ' .' ))
134- return ;
135- }
136-
137- if ((cfg._ignoreLinks == false ) || (buffer.st_mode & S_IFMT) != S_IFLNK)
140+ if (S_ISREG (buffer.st_mode ) || (!cfg._ignoreLinks && S_ISLNK (buffer.st_mode ))) {
138141 #if __cplusplus >= 201103L
139- files.emplace_back (target, buffer.st_size , buffer.st_mtime );
142+ files.emplace_back (target, buffer.st_size , buffer.st_mtime );
140143 #else
141- files.push_back (FileData (target, buffer.st_size , buffer.st_mtime ));
144+ files.push_back (FileData (target, buffer.st_size , buffer.st_mtime ));
142145 #endif
143146
144147 return ;
145148 }
146149
147- if ((buffer.st_mode & S_IFMT) != S_IFDIR ) {
150+ if (! S_ISDIR (buffer.st_mode ) ) {
148151 // Target is neither regular file nor directory, ignore
149152 return ;
150153 }
@@ -169,8 +172,7 @@ namespace kanzi
169172 continue ;
170173
171174 std::string fullpath = target + dirName;
172- res = cfg._ignoreLinks == true ? LSTAT (fullpath.c_str (), &buffer) :
173- STAT (fullpath.c_str (), &buffer);
175+ res = LSTAT (fullpath.c_str (), &buffer);
174176
175177 if (res != 0 ) {
176178 std::stringstream ss;
@@ -183,30 +185,19 @@ namespace kanzi
183185 }
184186 }
185187
186- if ((buffer.st_mode & S_IFMT) == S_IFREG ) {
188+ if (S_ISREG (buffer.st_mode ) ) {
187189 // Target is regular file
188- if (cfg._ignoreDotFiles == true ) {
189- size_t idx = fullpath.rfind (PATH_SEPARATOR);
190-
191- if ((idx != std::string::npos) && (idx < fullpath.length () - 1 ) && (fullpath[idx + 1 ] == ' .' ))
192- continue ;
193- }
190+ HANDLE_DOT_FILES (fullpath, cfg._ignoreDotFiles );
194191
195- if (( cfg._ignoreLinks == false ) || (buffer.st_mode & S_IFMT) != S_IFLNK )
192+ if (! cfg._ignoreLinks || ! S_ISLNK (buffer.st_mode ) )
196193 #if __cplusplus >= 201103L
197194 files.emplace_back (fullpath, buffer.st_size , buffer.st_mtime );
198195 #else
199196 files.push_back (FileData (fullpath, buffer.st_size , buffer.st_mtime ));
200197 #endif
201198 }
202- else if ((cfg._recursive ) && ((buffer.st_mode & S_IFMT) == S_IFDIR)) {
203- if (cfg._ignoreDotFiles == true ) {
204- size_t idx = fullpath.rfind (PATH_SEPARATOR);
205-
206- if ((idx != std::string::npos) && (idx < fullpath.length () - 1 ) && (fullpath[idx + 1 ] == ' .' ))
207- continue ;
208- }
209-
199+ else if (cfg._recursive && S_ISDIR (buffer.st_mode )) {
200+ HANDLE_DOT_FILES (fullpath, cfg._ignoreDotFiles );
210201 createFileList (fullpath, files, cfg, errors);
211202 }
212203 }
0 commit comments