-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfind.c
128 lines (116 loc) · 3.49 KB
/
find.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#include <sys/stat.h>
#include <dirent.h>
#include <unistd.h>
#include <string.h>
#include "commands.h"
#include "utils.h"
#include "colors.h"
#include "pattern.h"
#include "threads.h"
typedef struct
{
char *search_pattern;
Arena *arena;
int id;
bool no_recurse;
int is_stdout;
} WorkerArgs;
void *work(void *arg)
{
WorkerArgs *worker_args = (WorkerArgs *)arg;
char *path;
while ((path = (char *)dequeue()) != NULL)
{
struct dirent *d;
DIR *dir;
dir = opendir(path);
if (dir == NULL)
{
pthread_mutex_lock(get_queue_mutex());
set_active_tasks(get_active_tasks() - 1); // Decrement active tasks if directory open fails
pthread_mutex_unlock(get_queue_mutex());
continue;
}
while ((d = readdir(dir)) != NULL)
{
bool is_dir = d->d_type == DT_DIR;
char *dir_name = d->d_name;
if (match_pattern(worker_args->search_pattern, dir_name))
{
set_flag(true);
if (is_dir)
{
print_cyan(worker_args->is_stdout);
}
printf("%s/%s\n", path, dir_name);
if (is_dir)
{
print_reset(worker_args->is_stdout);
}
}
if (is_dir && !worker_args->no_recurse && !check_if_parent_dir(dir_name))
{
char *new_path = join_paths(path, dir_name, worker_args->arena);
enqueue(new_path);
}
}
closedir(dir);
pthread_mutex_lock(get_queue_mutex());
set_active_tasks(get_active_tasks() - 1);
// If no tasks are left, signal all threads to exit
if (get_active_tasks() == 0 && get_queue_size() == 0)
{
set_done(true);
pthread_cond_broadcast(get_not_empty_condition()); // Notify all waiting threads
}
pthread_mutex_unlock(get_queue_mutex());
}
return NULL;
}
int command_find(Cli_args *args, Arena *arena)
{
long number_of_processors = sysconf(_SC_NPROCESSORS_ONLN);
pthread_t threads[number_of_processors];
struct stat input_dir_stat;
if (lstat(args->path, &input_dir_stat) == -1)
{
perror("stat");
return EXIT_FAILURE;
}
if (!S_ISDIR(input_dir_stat.st_mode))
{
fprintf(stderr, "Not a directory\n");
return EXIT_FAILURE;
}
if (!args->search_pattern)
{
fprintf(stderr, "No search pattern provided\n");
return EXIT_FAILURE;
}
int should_print_color = isatty(fileno(stdout));
enqueue(args->path);
WorkerArgs worker_args = {.arena = arena, .search_pattern = args->search_pattern, .no_recurse = args->no_recurse, .is_stdout = should_print_color};
worker_args.arena = arena;
worker_args.search_pattern = args->search_pattern;
for (int i = 0; i < number_of_processors; i++)
{
worker_args.id = i;
if (pthread_create(&threads[i], NULL, work, &worker_args) != 0)
{
fprintf(stderr, "Failed to create thread %d\n", i);
return EXIT_FAILURE;
}
}
for (int i = 0; i < number_of_processors; i++)
{
pthread_join(threads[i], NULL);
}
pthread_mutex_destroy(get_queue_mutex());
pthread_cond_destroy(get_not_empty_condition());
pthread_cond_destroy(get_not_full_condition());
if (!get_flag())
{
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}