Refactor enmasse() and recurse() to reflect depth

The HLP-changes to sbase have been a great addition of functionality,
but they kind of "polluted" the enmasse() and recurse() prototypes.
As this will come in handy in the future, knowing at which "depth"
you are inside a recursing function is an important functionality.

Instead of having a special HLP-flag passed to enmasse, each sub-
function needs to provide it on its own and can calculate results
based on the current depth (for instance, 'H' implies 'P' at
depth > 0).
A special case is recurse(), because it actually depends on the
follow-type. A new flag "recurse_follow" brings consistency into
what used to be spread across different naming conventions (fflag,
HLP_flag, ...).

This also fixes numerous bugs with the behaviour of HLP in the
tools using it.
This commit is contained in:
FRIGN
2015-03-02 21:43:56 +01:00
parent 274e86e1aa
commit 8dc92fbd6c
13 changed files with 71 additions and 59 deletions

View File

@@ -10,25 +10,29 @@
#include "../util.h"
int recurse_follow = 'P';
void
recurse(const char *path, void (*fn)(const char *, int), int follow)
recurse(const char *path, void (*fn)(const char *, int), int depth)
{
char buf[PATH_MAX];
struct dirent *d;
struct stat lst, st;
DIR *dp;
if (lstat(path, &lst) < 0 || stat(path, &st) < 0 ||
!(S_ISDIR(lst.st_mode) ||
(follow != 'P' && S_ISLNK(lst.st_mode) && S_ISDIR(st.st_mode))))
if (lstat(path, &lst) < 0)
eprintf("lstat %s:", path);
if (stat(path, &st) < 0)
eprintf("stat %s:", path);
if (!S_ISDIR(lst.st_mode) && !(S_ISLNK(lst.st_mode) && S_ISDIR(st.st_mode) &&
!(recurse_follow == 'P' || (recurse_follow == 'H' && depth > 0))))
return;
if (!(dp = opendir(path)))
eprintf("opendir %s:", path);
while ((d = readdir(dp))) {
if (strcmp(d->d_name, ".") == 0 ||
strcmp(d->d_name, "..") == 0)
if (!strcmp(d->d_name, ".") || !strcmp(d->d_name, ".."))
continue;
if (strlcpy(buf, path, sizeof(buf)) >= sizeof(buf))
eprintf("path too long\n");
@@ -37,7 +41,7 @@ recurse(const char *path, void (*fn)(const char *, int), int follow)
eprintf("path too long\n");
if (strlcat(buf, d->d_name, sizeof(buf)) >= sizeof(buf))
eprintf("path too long\n");
fn(buf, follow == 'H' ? 'P' : follow);
fn(buf, depth + 1);
}
closedir(dp);