-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwork.c
More file actions
289 lines (235 loc) · 7.59 KB
/
Copy pathwork.c
File metadata and controls
289 lines (235 loc) · 7.59 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
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
#include "Cello.h"
#include <sys/stat.h>
#include <ftw.h>
#include <assert.h>
#include "dscan.h"
extern var url;
extern var SHARED_DATA;
extern var shared_table;
extern var screen_mutex;
extern var tls_mutex;
extern var db_author_mutex;
extern var db_adaptor_mutex;
extern var db_album_mutex;
extern var log_mutex;
extern FILE* ds_log;
static int process_entry(const char*, const struct stat*, int, struct FTW*);
static void do_dir(int level, const char*, const char*);
static void do_adaptor(char*, char*);
static void do_album(char*, char*);
static void do_base_album(char*, char*);
static void do_author(var, var);
static void do_file(int level, const struct stat*, char*, char*);
var do_base_author(var args)
{
var thid = get(args, $I(0));
var refa = get(args, $I(1));
var authors = deref(refa);
int tid = (int) c_int(thid);
var thno = $I(c_int(current(Thread)));
SET_UP_THREAD_DATA: {
var sd = $(SHARED_DATA);
struct SHARED_DATA* ptr = (struct SHARED_DATA*) sd;
ptr->user_thread_id = tid;
ptr->author_array = new_raw(Array, String);
ptr->a_id_array = new_raw(Array, Int);
ptr->adaptor_array = new_raw(Array, String);
ptr->ad_id_array = new_raw(Array, Int);
ptr->album_id = 0;
ptr->album_name = new_raw(String);
ptr->base_album_name = new_raw(String);
lock(tls_mutex);
set(shared_table, thno, sd);
unlock(tls_mutex);
}
dbconnect(tid);
while (len(authors) > 0) {
var author = get(authors, $I(0));
var base_path = new_raw(String, url);
concat(base_path, author);
do_author(base_path, author);
lock(screen_mutex);
scr_add_author(c_str(author), tid);
unlock(screen_mutex);
int rc = nftw(c_str(base_path), process_entry, 6, 0);
assert(rc == 0);
pop_at(authors, $I(0));
del_raw(base_path);
}
disconnect(tid);
return NULL;
}
int process_entry(const char* fname, const struct stat* ftw_buf, int flag, struct FTW* ftwb)
{
if (flag == FTW_F) {
int len = strlen(fname);
char* ptr = (char*) fname + len - 4;
if (strcmp(ptr, ".mp3") == 0) {
char* bptr = (char*) fname + ftwb->base;
do_file(ftwb->level, ftw_buf, (char*) fname, bptr);
}
} else if (flag == FTW_D) {
const char* bptr = fname + ftwb->base;
do_dir(ftwb->level, fname, bptr);
}
return 0;
}
void do_author(var pname, var author)
{
var thno = $I(c_int(current(Thread)));
var sd = get(shared_table, thno);
struct SHARED_DATA* ptr = (struct SHARED_DATA*) sd;
int tid = ptr->user_thread_id;
char* bname = c_str(author);
if ((strchr(bname, '&') != NULL) || (strstr(bname, " and ") != NULL)) {
char* lptr = c_str(pname);
lptr += len(url);
lock(log_mutex);
fprintf(ds_log, "%d:%s\n", DS_INVALID_AUTHOR, lptr);
unlock(log_mutex);
}
PARSE_AUTHORS: {
resize(ptr->author_array, 0);
resize(ptr->a_id_array, 0);
var copy_bname = new_raw(String, $S(bname));
char* bptr = strtok(c_str(copy_bname), ";");
push(ptr->author_array, $S(bptr));
while ((bptr = strtok(NULL, ";")) != NULL) {
size_t a = strspn(bptr, " ");
bptr += a;
push(ptr->author_array, $S(bptr));
}
del_raw(copy_bname);
}
RECORD_AUTHORS: {
foreach (au in ptr->author_array) {
lock(db_author_mutex);
push(ptr->a_id_array, $I(get_author_id(tid, c_str(au))));
unlock(db_author_mutex);
}
}
return;
}
void do_dir(int level, const char* pname, const char* bname)
{
switch (level) {
case 0:
break;
case 1:
do_adaptor((char*) pname, (char*) bname);
break;
case 2:
do_base_album((char*) pname, (char*) bname);
break;
case 3:
do_album((char*) pname, (char*) bname);
break;
default:
lock(log_mutex);
fprintf(ds_log, "%d:%s\n", DS_MISPLACED_DIRECTORY, pname + len(url));
unlock(log_mutex);
break;
}
return;
}
void do_file(int level, const struct stat* buf, char* pname, char* bname)
{
var thno = $I(c_int(current(Thread)));
var sd = get(shared_table, thno);
struct SHARED_DATA* ptr = (struct SHARED_DATA*) sd;
int tid = ptr->user_thread_id;
if (level == 4) {
lock(log_mutex);
fprintf(ds_log, "%d:%d:%d:%s\n", DS_INSERT_TRACK, (int) len(url), (int) ptr->album_id, pname);
unlock(log_mutex);
//lock(screen_mutex);
//scr_add_track(bname, tid);
//unlock(screen_mutex);
} else {
lock(log_mutex);
fprintf(ds_log, "%d:%s\n", DS_MISPLACED_MP3, pname + len(url));
unlock(log_mutex);
}
return;
}
void do_adaptor(char* pname, char* bname)
{
var thno = $I(c_int(current(Thread)));
var sd = get(shared_table, thno);
struct SHARED_DATA* ptr = (struct SHARED_DATA*) sd;
int tid = ptr->user_thread_id;
if ((strchr(bname, '&') != NULL) || (strstr(bname, " and ") != NULL)) {
lock(log_mutex);
fprintf(ds_log, "%d:%s\n", DS_INVALID_ADAPTOR, pname + len(url));
unlock(log_mutex);
}
PARSE_ADAPTORS: {
resize(ptr->adaptor_array, 0);
resize(ptr->ad_id_array, 0);
var copy_bname = new_raw(String, $S(bname));
char* bptr = strtok(c_str(copy_bname), ";");
push(ptr->adaptor_array, $S(bptr));
while ((bptr = strtok(NULL, ";")) != NULL) {
size_t a = strspn(bptr, " ");
bptr += a;
push(ptr->adaptor_array, $S(bptr));
}
del_raw(copy_bname);
}
RECORD_ADAPTORS: {
foreach (ad in ptr->adaptor_array) {
lock(db_adaptor_mutex);
push(ptr->ad_id_array, $I(get_adaptor_id(tid, c_str(ad))));
unlock(db_adaptor_mutex);
}
}
lock(screen_mutex);
scr_add_adaptor(bname, tid);
unlock(screen_mutex);
return;
}
void do_base_album(char* pname, char* bname)
{
var thno = $I(c_int(current(Thread)));
var sd = get(shared_table, thno);
struct SHARED_DATA* ptr = (struct SHARED_DATA*) sd;
resize(ptr->base_album_name, 0);
append(ptr->base_album_name, $S(bname));
return;
}
void do_album(char* pname, char* bname)
{
int64_t rc;
var thno = $I(c_int(current(Thread)));
var sd = get(shared_table, thno);
struct SHARED_DATA* ptr = (struct SHARED_DATA*) sd;
int tid = ptr->user_thread_id;
resize(ptr->album_name, 0);
append(ptr->album_name, $S(bname));
size_t lenba = len(ptr->base_album_name);
if (strncmp(c_str(ptr->base_album_name), bname, lenba) != 0) {
lock(log_mutex);
fprintf(ds_log, "%d:%s\n", DS_INCONSISTENT_ALBUM_NAMES, pname + len(url));
unlock(log_mutex);
}
lock(db_adaptor_mutex);
rc = get_album_id(tid, bname, pname);
unlock(db_adaptor_mutex);
ptr->album_id = rc;
if (rc != 0) {
UPDATE_AUTHOR_ALBUM: {
foreach (aid in ptr->a_id_array) {
insert_author_album(tid, c_int(aid), rc);
}
}
UPDATE_ADAPTOR_ALBUM: {
foreach (adid in ptr->ad_id_array) {
insert_adaptor_album(tid, c_int(adid), rc);
}
}
}
lock(screen_mutex);
scr_add_album(bname, tid);
unlock(screen_mutex);
return;
}