Skip to content

Commit

Permalink
Remove unused util_opendir c functions
Browse files Browse the repository at this point in the history
  • Loading branch information
eivindjahren committed Aug 8, 2024
1 parent ae289be commit 3d9ea7d
Show file tree
Hide file tree
Showing 2 changed files with 0 additions and 201 deletions.
5 changes: 0 additions & 5 deletions lib/include/ert/util/util.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -143,11 +143,6 @@ int util_fstat(int fileno, stat_type *stat_info);
#ifdef ERT_HAVE_OPENDIR
void util_copy_directory_content(const char *src_path, const char *target_path);
void util_copy_directory(const char *, const char *);
void util_walk_directory(const char *root_path,
walk_file_callback_ftype *file_callback,
void *file_callback_arg,
walk_dir_callback_ftype *dir_callback,
void *dir_callback_arg);
#endif

#ifdef ERT_HAVE_GETUID
Expand Down
196 changes: 0 additions & 196 deletions lib/util/util_opendir.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,6 @@
#include <dirent.h>
#include <sys/types.h>

bool util_copy_file__(const char *src_file, const char *target_file,
size_t buffer_size, void *buffer, bool abort_on_error);

static DIR *util_walk_opendir__(const char *root_path) {

DIR *dirH = opendir(root_path);
if (dirH == NULL) {
if (errno == EACCES)
fprintf(stderr,
"** Warning could not open directory:%s - permission "
"denied - IGNORED.\n",
root_path);
else
util_abort("%s: failed to open directory:%s / %s \n", __func__,
root_path, strerror(errno));
}
return dirH;
}

static void util_copy_directory__(const char *src_path, const char *target_path,
int buffer_size, void *buffer) {
if (!util_is_directory(src_path))
Expand Down Expand Up @@ -100,180 +81,3 @@ void util_copy_directory(const char *src_path, const char *__target_path) {
free(target_path);
util_free_stringlist(path_parts, num_components);
}

/**
This function will evaluate all file_callbacks for all the files in
the root_path directory.
*/

static void util_walk_file_callbacks__(const char *root_path, int current_depth,
walk_file_callback_ftype *file_callback,
void *file_callback_arg) {

DIR *dirH = util_walk_opendir__(root_path);
if (dirH != NULL) {
struct dirent *dp;
do {
dp = readdir(dirH);
if (dp != NULL) {
if (dp->d_name[0] != '.') {
char *full_path =
util_alloc_filename(root_path, dp->d_name, NULL);

if (util_is_file(full_path) && file_callback != NULL)
file_callback(root_path, dp->d_name, file_callback_arg);

free(full_path);
}
}
} while (dp != NULL);
closedir(dirH);
}
}

/**
This function will start at 'root_path' and then recursively go
through all file/subdirectore located below root_path. For each
file/directory in the tree it will call the user-supplied funtions
'file_callback' and 'dir_callback'.
The arguments to file_callback will be:
file_callback(root_path, file , file_callback_arg):
For the dir_callback function the the depth in the filesystem will
also be supplied as an argument:
dir_callback(root_path , directory , depth , dir_callback_arg)
The dir_callback / file_callback arguments can be NULL. Observe
that IFF supplied the dir_callback function can be used to stop the
recursion, if the dir_callback returns false for a particular
directory the function will not descend into that directory. (If
dir_callback == NULL it will descend to the bottom irrespectively).
Example
-------
Root
Root/File1
Root/File2
Root/dir
Root/dir/fileXX
Root/dir/dir2
The call:
util_walk_directory("Root" , file_callback , file_arg , dir_callback , dir_arg);
Will result in the following calls to the callbacks:
file_callback("Root" , "File1" , file_arg);
file_callback("Root" , "File2" , file_arg);
file_callback("Root/dir" , "fileXX" , arg);
dir_callback("Root" , "dir" , 1 , dir_arg);
dir_callback("Root/dir" , "dir2" , 2 , dir_arg);
Symlinks are ignored when descending into subdirectories. The tree
is walked in a 'width-first' mode (i.e. all the callbacks in a
directory are evaluated before the function descends further down
in the tree).
If we encounter permission denied when opening a directory a
message is printed on stderr, the directory is ignored and the
function returns.
*/

static void util_walk_directory__(const char *root_path, bool depth_first,
int current_depth,
walk_file_callback_ftype *file_callback,
void *file_callback_arg,
walk_dir_callback_ftype *dir_callback,
void *dir_callback_arg);

/**
This function will evaluate the dir_callback for all the (sub)
directories in root_path, and afterwards descend recursively into
the subdireectories. It will descend into the subdirectory
immediately after completing the callback, i.e. it will not do all
the callbacks first.
Observe that if the dir_callback function returns false, this sub
directory will not be descended into. (If dir_callback == NULL that
amounts to return true.)
*/

static void util_walk_descend__(const char *root_path, bool depth_first,
int current_depth,
walk_file_callback_ftype *file_callback,
void *file_callback_arg,
walk_dir_callback_ftype *dir_callback,
void *dir_callback_arg) {

DIR *dirH = util_walk_opendir__(root_path);
if (dirH != NULL) {
struct dirent *dp;
do {
dp = readdir(dirH);
if (dp != NULL) {
if (dp->d_name[0] != '.') {
char *full_path =
util_alloc_filename(root_path, dp->d_name, NULL);

if ((util_is_directory(full_path) &&
(!util_is_link(full_path)))) {
bool descend = true;
if (dir_callback != NULL)
descend =
dir_callback(root_path, dp->d_name,
current_depth, dir_callback_arg);

if (descend &&
util_file_exists(
full_path)) /* The callback might have removed it. */
util_walk_directory__(
full_path, depth_first, current_depth + 1,
file_callback, file_callback_arg, dir_callback,
dir_callback_arg);
}
free(full_path);
}
}
} while (dp != NULL);
closedir(dirH);
}
}

static void util_walk_directory__(const char *root_path, bool depth_first,
int current_depth,
walk_file_callback_ftype *file_callback,
void *file_callback_arg,
walk_dir_callback_ftype *dir_callback,
void *dir_callback_arg) {

if (depth_first) {
util_walk_descend__(root_path, depth_first, current_depth,
file_callback, file_callback_arg, dir_callback,
dir_callback_arg);
util_walk_file_callbacks__(root_path, current_depth, file_callback,
file_callback_arg);
} else {
util_walk_file_callbacks__(root_path, current_depth, file_callback,
file_callback_arg);
util_walk_descend__(root_path, depth_first, current_depth,
file_callback, file_callback_arg, dir_callback,
dir_callback_arg);
}
}

void util_walk_directory(const char *root_path,
walk_file_callback_ftype *file_callback,
void *file_callback_arg,
walk_dir_callback_ftype *dir_callback,
void *dir_callback_arg) {

bool depth_first = false;

util_walk_directory__(root_path, depth_first, 0, file_callback,
file_callback_arg, dir_callback, dir_callback_arg);
}

0 comments on commit 3d9ea7d

Please sign in to comment.