-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrcontrol.c
More file actions
96 lines (72 loc) · 1.7 KB
/
rcontrol.c
File metadata and controls
96 lines (72 loc) · 1.7 KB
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
#include "rcontrol.h"
static struct list_t filename_list;
static struct list_t file_list;
static struct rt_args_t * args;
static int init_check = 0;
static pthread_t** rthreads;
struct list_t * rc_startup() {
assert(!init_check);
init_check = 1;
init(&filename_list);
init(&file_list);
args = (struct rt_args_t *) malloc(sizeof(struct rt_args_t));
args->filename_list = &filename_list;
args->file_list = &file_list;
args->done = 0;
{
rthreads = (pthread_t**) malloc(sizeof(pthread_t)*MAX_READ_THREADS);
int i;
for(i = 0; i < MAX_READ_THREADS; ++i) {
rthreads[i] = (pthread_t *) malloc(sizeof(pthread_t));
assert(rthreads[i]);
pthread_create(rthreads[i], NULL, rt_thread, (void*) args);
}
}
return &file_list;
}
void rc_stop() {
assert(init_check);
//join
args->done = 1;
int i;
for(i = 0; i < MAX_READ_THREADS; ++i) {
pthread_join(*(rthreads[i]), NULL);
}
free(args);
destroy(&filename_list);
destroy(&file_list);
for(i = 0; i < MAX_READ_THREADS; ++i) {
free(rthreads[i]);
}
free(rthreads);
}
void rc_readdir(char* restrict dir) {
assert(init_check);
DIR * dp;
struct dirent *entries;
size_t count = 0;
dp = opendir(dir);
if(dp) {
while( (entries = readdir(dp)) ) {
// We don't want . files.
if(entries->d_name[0] == '.')
continue;
rc_readfile(entries->d_name);
++count;
}
(void) closedir(dp);
}
else {
perror("Could not open root directory!");
}
if(!count) {
fprintf(stderr, "No files in root directory, nothing to do.\n");
exit(1);
}
// Wait until all files are read
while(file_list.size != count) usleep(1000);
build_cache(&file_list);
}
void rc_readfile(char* restrict name) {
push_back(&filename_list, name, strlen(name)+1, NULL, 0);
}