-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlib.c
676 lines (535 loc) · 17.8 KB
/
lib.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
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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
//
// Created by victor on 22.01.2022.
//
//need for nftw
//#define DEBUG
#define _LARGEFILE64_SOURCE
#define _FILE_OFFSET_BITS 64
#define _XOPEN_SOURCE 700
#include <deadbeef/deadbeef.h>
#include "utils.h"
#include <unistd.h>
#include <glib.h>
#include <sqlite3.h>
#include <sys/inotify.h>
#include <ftw.h>
#include <libgen.h>
#include <poll.h>
#include "cJSON/cJSON.h"
//need for nftw
#ifndef USE_FDS
#define USE_FDS 15
#endif
//need for inotify
#define MAX_EVENTS 1024 //The maximum number of events to process at one time.
#define LEN_NAME 256 //max length of file name
#define EVENT_SIZE ( sizeof (struct inotify_event) ) //the size of the event structure
#define BUF_LEN ( MAX_EVENTS * ( EVENT_SIZE + LEN_NAME )) //event buffer
static DB_functions_t *deadbeef;
static sqlite3* db;
static char *err_msg = 0;
static int in_fd; //inotify fd
static GHashTable* wd_table = NULL;
static GHashTable* wait_table = NULL;
static intptr_t watch_thread_ptr = NULL;
static int shutdown = 0;
static const int notify_mask = IN_CREATE | IN_CLOSE_WRITE | IN_DELETE | IN_MOVE;
static char* root = NULL;
static const char* ext_list = "mp3,flac,ape,m4a,wav,cue";
static char* pl_name = NULL;
static FILE* log_file = NULL;
//#define log(x,...) fprintf(log_file,x,...)
static char* escaping(const char* str){
size_t str_len = strlen(str);
size_t quotes = 0;
for (int i = 0; i < str_len; ++i) {
if(str[i] == '\''){
quotes++;
}
}
if(str_len == 0){
return NULL;
}
char* new_str = malloc(str_len + quotes + 1);
int j = 0;
for (int i = 0; i < str_len; ++i) {
if(str[i] == '\''){
new_str[j++] = '\'';
new_str[j++] = '\'';
}else{
new_str[j++] = str[i];
}
}
new_str[j] = '\0';
return new_str;
}
int file_callback(const char *filepath, const struct stat *info,
int typeflag, struct FTW *pathinfo){
if(typeflag == FTW_D){
int* wd = malloc(sizeof(int));
*wd = inotify_add_watch(in_fd, filepath, notify_mask);
g_hash_table_insert(wd_table,wd,strdup(filepath));
}else{
char* dir_name_tmp = strdup(filepath);
char* basename_tmp = strdup(filepath);
char* file = basename(basename_tmp);
if(strstr(ext_list, get_filename_ext(file)) == NULL){
free(dir_name_tmp);
free(basename_tmp);
return 0;
}
char* file_path_esq = escaping(filepath);
const char* sql_form = "insert into files_tmp(path) values('%s');";
char *sql;
if(file_path_esq == NULL){
sql = malloc(strlen(sql_form) + strlen(filepath) + 2);
sprintf(sql,sql_form,filepath);
}else{
sql = malloc(strlen(sql_form) + strlen(file_path_esq) + 2);
sprintf(sql,sql_form,file_path_esq);
free(file_path_esq);
}
sqlite3_str* test;
int rc = sqlite3_exec(db, sql, 0, 0, &err_msg);
if (rc != SQLITE_OK ) {
fprintf(stderr, "SQL error2: %s\n", err_msg);
}
//printf("%s\n", filepath);
free(sql);
free(dir_name_tmp);
free(basename_tmp);
}
return 0;
}
static ddb_playlist_t* find_playlist(const char* find_title){
int pl_count = deadbeef->plt_get_count();
fprintf(log_file,"pl count: %d\n", pl_count);
for(int i = 0; i < pl_count; i++){
ddb_playlist_t* pl = deadbeef->plt_get_for_idx(i);
char title[1024];
deadbeef->plt_get_title(pl,title,sizeof(title));
if(strcmp(title,find_title) == 0){
return pl;
}
}
return NULL;
}
void free_wd(gpointer data){
#ifdef DEGUG
fprintf(log_file,"wd free\n");
#endif
inotify_rm_watch(in_fd, *((int*) data));
free(data);
}
static int read_conf(const char* path){
FILE* file = fopen(path, "rt");
if(file == NULL){
return 0;
}
fseek(file, 0, SEEK_END);
int l = ftell(file);
char *data = malloc(l);
if(data == NULL){
fclose(file);
return 0;
}
fseek(file, 0, SEEK_SET);
l = (int) fread(data, sizeof(char), l, file);
fclose(file);
const cJSON* conf_json = cJSON_Parse(data);
if(conf_json == NULL){
return 0;
}
cJSON* folder_json = cJSON_GetObjectItem(conf_json,"folder");
if(folder_json == NULL){
return 0;
}
char* folder = cJSON_GetStringValue(folder_json);
if(folder_json == NULL){
return 0;
}
root = folder;
cJSON* pl_name_json = cJSON_GetObjectItem(conf_json,"pl name");
if(pl_name_json == NULL){
char* folder_name_tmp = strdup(folder);
pl_name = basename(folder_name_tmp);
}else{
pl_name = cJSON_GetStringValue(pl_name_json);
}
return 1;
}
static int start(){
//get path to database file
const char* db_dir = deadbeef->get_system_dir(DDB_SYS_DIR_CONFIG);
char* db_file = full_path(db_dir,"data.db");
log_file = fopen(full_path(db_dir,"log.txt"),"wt");
char* conf_file = full_path(db_dir,"watch_dir_conf.json");
fprintf(log_file,"%s\n", conf_file);
int conf_read_result = read_conf(conf_file);
if(!conf_read_result){
free(db_file);
free(conf_file);
return 1;
}
#ifdef DEGUG
fprintf(log_file,"data_base file: %s\n",db_file);
#endif
int init_needed = access(db_file,R_OK);
#ifdef DEGUG
fprintf(log_file,"init_needed: %d\n", init_needed);
#endif
int rc = sqlite3_open(db_file, &db);
if (rc != SQLITE_OK) {
fprintf(log_file,"Cannot open database: %s\n", sqlite3_errmsg(db));
sqlite3_close(db);
return 1;
}
if(init_needed == -1){
fprintf(log_file,"init bd\n");
sqlite3_exec(db,"CREATE TABLE \"files\" (\n"
"\t\"path\"\tTEXT NOT NULL CHECK(path <> \"\")\n"
");",NULL,NULL,&err_msg);
sqlite3_exec(db,"CREATE INDEX \"path_index\" ON \"files\" (\n"
"\t\"path\"\n"
");", NULL,NULL,&err_msg);
}
#ifdef DEGUG
else{
fprintf(log_file,"work with exist db\n");
}
#endif
sqlite3_exec(db, "CREATE temporary TABLE \"files_tmp\" (\n"
"\t\"path\"\tTEXT NOT NULL CHECK(path <> \"\")\n"
");",NULL,NULL, &err_msg);
in_fd = inotify_init();
if ( in_fd < 0 ) {
perror( "Couldn't initialize inotify");
}
wd_table = g_hash_table_new_full(g_int_hash,g_int_equal,free_wd,g_free);
wait_table = g_hash_table_new(g_str_hash,g_str_equal);
nftw(root, file_callback, USE_FDS, FTW_PHYS);
free(db_file);
free(conf_file);
return 0;
}
static int add_callback(void *pl_raw, int argc, char **argv, char **azColName){
ddb_playlist_t* pl = pl_raw;
if(argc == 1){
const char* sql_form = "insert into files(path) values('%s');";
const char* path = argv[0];
char* path_esq = escaping(path);
char *sql;
if(path_esq == NULL){
sql = malloc(strlen(sql_form) + strlen(path) + 2);
sprintf(sql,sql_form,path);
}else {
sql = malloc(strlen(sql_form) + strlen(path_esq) + 2);
sprintf(sql, sql_form, path_esq);
free(path_esq);
}
fprintf(log_file,"%s\n", path);
if(deadbeef->plt_add_file2(1,pl,path,NULL,NULL)){
fprintf(log_file,"add failed: %s\n",path);
return 0;
}
fprintf(log_file, "add_callback, write to bd: %s\n", sql);
int rc = sqlite3_exec(db, sql, 0, 0, &err_msg);
if (rc != SQLITE_OK ) {
fprintf(stderr, "SQL error: %s\n", err_msg);
}
//deadbeef->plt_add_file2(1,pl,path,NULL,NULL);
fprintf(log_file,"add: %s\n",path);
free(sql);
}
return 0;
}
static ddb_playItem_t* find_by_path(ddb_playlist_t* pl, const char* path){
int count = deadbeef->plt_get_item_count(pl,0);
ddb_playItem_t* current = deadbeef->plt_get_first(pl,0);
deadbeef->pl_lock();
while (current){
const char* path_ = deadbeef->pl_find_meta(current,":URI");
if(strcmp(path, path_) == 0){
deadbeef->pl_item_unref(current);
deadbeef->pl_unlock();
return current;
}
ddb_playItem_t* tmp = current;
current = deadbeef->pl_get_next(current,0);
deadbeef->pl_item_unref(tmp);
}
deadbeef->pl_unlock();
return NULL;
}
static int remove_callback(void *pl, int argc, char **argv, char **azColName){
if(argc == 1){
const char* sql_form = "delete from files where path = '%s';";
const char* path = argv[0];
char* sql = malloc(strlen(sql_form) + strlen(path) + 2);
sprintf(sql,sql_form,path);
int rc = sqlite3_exec(db, sql, 0, 0, &err_msg);
if (rc != SQLITE_OK ) {
fprintf(log_file, "SQL error: %s\n", err_msg);
}
ddb_playItem_t* playItem = find_by_path(pl,path);
if(playItem == NULL){
free(sql);
return 0;
}
deadbeef->pl_lock();
deadbeef->pl_item_ref(playItem);
deadbeef->plt_remove_item(pl,playItem);
deadbeef->pl_item_unref(playItem);
deadbeef->pl_unlock();
fprintf(log_file,"removed: %s\n", path);
free(sql);
}
return 0;
}
void add_dir(struct inotify_event *event){
const char* parent_name = g_hash_table_lookup(wd_table,&event->wd);
char* path = malloc(strlen(parent_name) + 1 + strlen(event->name) + 2);
sprintf(path,"%s/%s",parent_name,event->name);
int* wd = malloc(sizeof(int));
*wd = inotify_add_watch(in_fd, path, notify_mask);
if (*wd == -1)
{
fprintf(log_file,"Couldn't add watch to %s\n", path);
return;
}
g_hash_table_insert(wd_table,wd,path);
}
void add_file(struct inotify_event *event, ddb_playlist_t* pl){
fprintf(log_file,"file created: %s\n",event->name);
char* basename_tmp = strdup(event->name);
char* file = basename(basename_tmp);
//check file format
if(strstr(ext_list, get_filename_ext(file)) == NULL){
free(basename_tmp);
return;
}
free(basename_tmp);
const char* parent_name = g_hash_table_lookup(wd_table,&event->wd);
char* path = malloc(strlen(parent_name) + 1 + strlen(event->name) + 2);
sprintf(path,"%s/%s",parent_name,event->name);
g_hash_table_insert(wait_table,path,NULL);
}
void file_close(struct inotify_event *event, ddb_playlist_t* pl){
const char* parent_name = g_hash_table_lookup(wd_table,&event->wd);
char* path = malloc(strlen(parent_name) + 1 + strlen(event->name) + 2);
sprintf(path,"%s/%s",parent_name,event->name);
if(g_hash_table_contains(wait_table,path)){
deadbeef->pl_lock();
fprintf(log_file,"%s\n", path);
if(deadbeef->plt_add_file2(1,pl,path,NULL,NULL)){
fprintf(log_file,"add failed: %s\n",path);
deadbeef->pl_unlock();
return;
}
deadbeef->pl_unlock();
#ifdef DEBUG
fprintf(log_file,"file write end %s\n", path);
#endif
g_hash_table_remove(wait_table,path);
const char* sql_form = "insert into files(path) values('%s');";
char* path_esq = escaping(path);
char *sql;
if(path_esq == NULL){
sql = malloc(strlen(sql_form) + strlen(path) + 2);
sprintf(sql,sql_form,path);
}else{
sql = malloc(strlen(sql_form) + strlen(path_esq) + 2);
sprintf(sql,sql_form,path_esq);
free(path_esq);
}
fprintf(log_file, "file_close, write to bd: %s\n", sql);
int rc = sqlite3_exec(db, sql, 0, 0, &err_msg);
if (rc != SQLITE_OK ) {
fprintf(stderr, "SQL error: %s\n", err_msg);
}
free(path);
free(sql);
}
}
void remove_file(struct inotify_event *event, ddb_playlist_t* pl){
const char* sql_form = "delete from files where path = '%s';";
const char* parent_name = g_hash_table_lookup(wd_table,&event->wd);
char* path = malloc(strlen(parent_name) + 1 + strlen(event->name) + 2);
sprintf(path,"%s/%s",parent_name,event->name);
char* sql = malloc(strlen(sql_form) + strlen(path) + 2);
sprintf(sql,sql_form,path);
int rc = sqlite3_exec(db, sql, 0, 0, &err_msg);
if (rc != SQLITE_OK ) {
fprintf(stderr, "SQL error: %s\n", err_msg);
}
ddb_playItem_t* playItem = find_by_path(pl,path);
fprintf(log_file,"found: %p\n", playItem);
if(playItem == NULL){
free(sql);
free(path);
return;
}
deadbeef->pl_lock();
deadbeef->pl_item_ref(playItem);
deadbeef->plt_remove_item(pl,playItem);
deadbeef->pl_item_unref(playItem);
deadbeef->pl_unlock();
free(sql);
free(path);
}
static void watch_thread(ddb_playlist_t* pl){
int length, i;
char buffer[BUF_LEN];
struct pollfd fds[1];
fds[0].fd = in_fd;
fds[0].events = POLLIN;
while(!shutdown)
{
i = 0;
fflush(log_file);
sqlite3_db_cacheflush(db);
int ret = poll( fds, 1, 10000);
if(ret == -1){
fprintf(log_file,"error\n");
break;
}else if(ret == 0){
continue;
}else{
fds[0].revents = 0;
}
length = read( in_fd, buffer, BUF_LEN );
if ( length < 0 ) {
perror( "read" );
}
while ( i < length ) {
struct inotify_event *event = ( struct inotify_event * ) &buffer[ i ];
if ( event->len ) {
if (event->mask & IN_CREATE) {
if (event->mask & IN_ISDIR) {
#ifdef DEBUG
fprintf(log_file,"The directory %s was Created.\n", event->name);
#endif
add_dir(event);
}else{
#ifdef DEBUG
fprintf(log_file,"The file %s was Created with WD %d - cookie %d\n", event->name, event->wd,
event->cookie);
#endif
add_file(event,pl);
}
}else if ( event->mask & IN_DELETE){
if (event->mask & IN_ISDIR) {
//TODO удалять wd у далёной дериктории
#ifdef DEBUG
fprintf(log_file,"STAB The directory %s was deleted.\n", event->name);
#endif
}else {
#ifdef DEBUG
fprintf(log_file,"The file %s was deleted with WD %d - cookie %d\n", event->name, event->wd,
event->cookie);
#endif
remove_file(event,pl);
}
}else if (event->mask & IN_MOVED_FROM){
if (event->mask & IN_ISDIR) {
//TODO удалять wd у далёной дериктории
#ifdef DEBUG
fprintf(log_file,"STAB The directory %s was deleted.\n", event->name);
#endif
}else {
#ifdef DEBUG
fprintf(log_file,"The file %s was deleted with WD %d - cookie %d\n", event->name, event->wd,
event->cookie);
#endif
remove_file(event,pl);
}
}else if(event->mask & IN_MOVED_TO){
if (event->mask & IN_ISDIR) {
#ifdef DEBUG
fprintf(log_file,"The directory %s was Moved.\n", event->name);
#endif
add_dir(event);
}else{
#ifdef DEBUG
fprintf(log_file,"The file %s was Created with WD %d - cookie %d\n", event->name, event->wd,
event->cookie);
#endif
add_file(event,pl);
}
}else if(event->mask & IN_CLOSE){
if(!(event->mask & IN_ISDIR)){
file_close(event,pl);
}
}
i += EVENT_SIZE + event->len;
}
}
}
}
static int connect(){
if(root == NULL){
return 1;
}
ddb_playlist_t *pl = deadbeef->plt_find_by_name(pl_name);
if(pl == NULL){
pl = deadbeef->plt_append(pl_name);
}
#ifdef DEBUG
fprintf(log_file,"play list found\n");
#endif
if(pl == NULL){
return 1;
}
deadbeef->plt_add_files_begin(pl,1);
fprintf(log_file,"новые файлы\n");
//find new files
sqlite3_exec(db,"SELECT path FROM files\n"
" EXCEPT\n"
" SELECT path FROM files_tmp;",remove_callback,pl,&err_msg);
fprintf(log_file,"удалёные файлы\n");
//find deleted files
sqlite3_exec(db,"SELECT path FROM files_tmp\n"
" EXCEPT\n"
" SELECT path FROM files;",add_callback,pl,&err_msg);
fflush(log_file);
deadbeef->plt_add_files_end(pl,1);
deadbeef->plt_modified(pl);
watch_thread_ptr = deadbeef->thread_start((void (*)(void *)) watch_thread, pl);
sqlite3_exec(db,"drop table files_tmp;",NULL,NULL,&err_msg);
return 0;
}
static int stop(){
fclose(log_file);
shutdown = TRUE;
if(watch_thread_ptr)
deadbeef->thread_join(watch_thread_ptr);
if(wd_table)
g_hash_table_destroy(wd_table);
if (db)
sqlite3_close(db);
return 0;
}
static DB_misc_t plugin = {
.plugin = {
.api_vmajor = 1,
.api_vminor = 14,
.id = NULL,
.name = "watch dir",
.descr = "auto refresh play list",
.copyright = "Murloc Knight",
.website = "https://github.com/KnightMurloc/DeadBeef-watch-dir-plugin",
.command = NULL,
.start = start,
.stop = stop,
.connect = connect,
.disconnect = NULL,
.exec_cmdline = NULL,
.get_actions = NULL,
.message = NULL,
.configdialog = NULL
}
};
extern DB_plugin_t *watch_dir_load(DB_functions_t *api) {
deadbeef = api;
return &plugin.plugin;
}