#include <sys/mman.h>
#include <fnmatch.h>
#include <sys/shm.h>
+#include <dirent.h>
#include <osl.h>
#include <lopsub.h>
return send_ret;
}
+/*
+ * Call back once for each regular file below a directory.
+ *
+ * Traverse the given directory recursively and call the supplied callback for
+ * each regular file encountered. The first argument to the callback will be
+ * the path to the regular file and the second argument will be the data
+ * pointer. All file types except regular files and directories are ignored. In
+ * particular, symlinks are not followed. Subdirectories are ignored silently
+ * if the calling process has insufficient access permissions.
+ */
+static int for_each_file_in_dir(const char *dirname,
+ int (*func)(const char *, void *), void *data)
+{
+ int ret;
+ DIR *dir;
+ struct dirent *entry;
+
+ dir = opendir(dirname);
+ if (!dir)
+ return errno == EACCES? 1 : -ERRNO_TO_PARA_ERROR(errno);
+ /* scan cwd recursively */
+ while ((entry = readdir(dir))) {
+ char *tmp;
+ struct stat s;
+
+ if (!strcmp(entry->d_name, "."))
+ continue;
+ if (!strcmp(entry->d_name, ".."))
+ continue;
+ tmp = make_message("%s/%s", dirname, entry->d_name);
+ ret = 0;
+ if (lstat(tmp, &s) != -1) {
+ if (S_ISREG(s.st_mode))
+ ret = func(tmp, data);
+ else if (S_ISDIR(s.st_mode))
+ ret = for_each_file_in_dir(tmp, func, data);
+ }
+ free(tmp);
+ if (ret < 0)
+ goto out;
+ }
+ ret = 1;
+out:
+ closedir(dir);
+ return ret;
+}
+
static int com_add(struct command_context *cc, struct lls_parse_result *lpr)
{
int i, ret;
return -ERRNO_TO_PARA_ERROR(errno);
}
-/**
- * Save the cwd and open a given directory.
- *
- * \param dirname Path to the directory to open.
- * \param dir Result pointer.
- * \param cwd File descriptor of the current working directory.
- *
- * \return Standard.
- *
- * Opening the current directory (".") and calling fchdir() to return is
- * usually faster and more reliable than saving cwd in some buffer and calling
- * chdir() afterwards.
- *
- * If \a cwd is not \p NULL "." is opened and the resulting file descriptor is
- * stored in \a cwd. If the function returns success, and \a cwd is not \p
- * NULL, the caller must close this file descriptor (probably after calling
- * fchdir(*cwd)).
- *
- * On errors, the function undos everything, so the caller needs neither close
- * any files, nor change back to the original working directory.
- *
- * \sa getcwd(3).
- *
- */
-static int para_opendir(const char *dirname, DIR **dir, int *cwd)
-{
- int ret;
-
- *dir = NULL;
- if (cwd) {
- ret = para_open(".", O_RDONLY, 0);
- if (ret < 0)
- return ret;
- *cwd = ret;
- }
- if (chdir(dirname) != 0) {
- ret = -ERRNO_TO_PARA_ERROR(errno);
- goto close_cwd;
- }
- *dir = opendir(".");
- if (*dir)
- return 1;
- ret = -ERRNO_TO_PARA_ERROR(errno);
- /* Ignore return value of fchdir() and close(). We're busted anyway. */
- if (cwd) {
- int __a_unused ret2 = fchdir(*cwd); /* STFU, gcc */
- }
-close_cwd:
- if (cwd)
- close(*cwd);
- return ret;
-}
-
/**
* Create a directory, don't fail if it already exists.
*
}
}
}
-
-/**
- * Traverse the given directory recursively.
- *
- * \param dirname The directory to traverse.
- * \param func The function to call for each entry.
- * \param private_data Pointer to an arbitrary data structure.
- *
- * For each regular file under \a dirname, the supplied function \a func is
- * called. The full path of the regular file and the \a private_data pointer
- * are passed to \a func. Directories for which the calling process has no
- * permissions to change to are silently ignored.
- *
- * \return Standard.
- */
-int for_each_file_in_dir(const char *dirname,
- int (*func)(const char *, void *), void *private_data)
-{
- DIR *dir;
- struct dirent *entry;
- int cwd_fd, ret = para_opendir(dirname, &dir, &cwd_fd);
-
- if (ret < 0)
- return ret == -ERRNO_TO_PARA_ERROR(EACCES)? 1 : ret;
- /* scan cwd recursively */
- while ((entry = readdir(dir))) {
- mode_t m;
- char *tmp;
- struct stat s;
-
- if (!strcmp(entry->d_name, "."))
- continue;
- if (!strcmp(entry->d_name, ".."))
- continue;
- if (lstat(entry->d_name, &s) == -1)
- continue;
- m = s.st_mode;
- if (!S_ISREG(m) && !S_ISDIR(m))
- continue;
- tmp = make_message("%s/%s", dirname, entry->d_name);
- if (!S_ISDIR(m)) {
- ret = func(tmp, private_data);
- free(tmp);
- if (ret < 0)
- goto out;
- continue;
- }
- /* directory */
- ret = for_each_file_in_dir(tmp, func, private_data);
- free(tmp);
- if (ret < 0)
- goto out;
- }
- ret = 1;
-out:
- closedir(dir);
- if (fchdir(cwd_fd) < 0 && ret >= 0)
- ret = -ERRNO_TO_PARA_ERROR(errno);
- close(cwd_fd);
- return ret;
-}