1 | /* MiniDLNA media server |
---|
2 | * Copyright (C) 2008-2010 Justin Maggard |
---|
3 | * |
---|
4 | * This file is part of MiniDLNA. |
---|
5 | * |
---|
6 | * MiniDLNA is free software; you can redistribute it and/or modify |
---|
7 | * it under the terms of the GNU General Public License version 2 as |
---|
8 | * published by the Free Software Foundation. |
---|
9 | * |
---|
10 | * MiniDLNA is distributed in the hope that it will be useful, |
---|
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
13 | * GNU General Public License for more details. |
---|
14 | * |
---|
15 | * You should have received a copy of the GNU General Public License |
---|
16 | * along with MiniDLNA. If not, see <http://www.gnu.org/licenses/>. |
---|
17 | */ |
---|
18 | #include "config.h" |
---|
19 | #include <stdio.h> |
---|
20 | #include <string.h> |
---|
21 | #include <stdlib.h> |
---|
22 | #include <errno.h> |
---|
23 | #include <unistd.h> |
---|
24 | #include <dirent.h> |
---|
25 | #include <libgen.h> |
---|
26 | #include <errno.h> |
---|
27 | #include <sys/types.h> |
---|
28 | #include <sys/stat.h> |
---|
29 | #include <sys/time.h> |
---|
30 | #include <sys/resource.h> |
---|
31 | #include <poll.h> |
---|
32 | #ifdef HAVE_INOTIFY_H |
---|
33 | #include <sys/inotify.h> |
---|
34 | #else |
---|
35 | #include "linux/inotify.h" |
---|
36 | #include "linux/inotify-syscalls.h" |
---|
37 | #endif |
---|
38 | |
---|
39 | #include "upnpglobalvars.h" |
---|
40 | #include "inotify.h" |
---|
41 | #include "utils.h" |
---|
42 | #include "sql.h" |
---|
43 | #include "scanner.h" |
---|
44 | #include "metadata.h" |
---|
45 | #include "albumart.h" |
---|
46 | #include "playlist.h" |
---|
47 | #include "log.h" |
---|
48 | |
---|
49 | #define EVENT_SIZE ( sizeof (struct inotify_event) ) |
---|
50 | #define BUF_LEN ( 1024 * ( EVENT_SIZE + 16 ) ) |
---|
51 | #define DESIRED_WATCH_LIMIT 65536 |
---|
52 | |
---|
53 | #define PATH_BUF_SIZE PATH_MAX |
---|
54 | |
---|
55 | struct watch |
---|
56 | { |
---|
57 | int wd; /* watch descriptor */ |
---|
58 | char *path; /* watched path */ |
---|
59 | struct watch *next; |
---|
60 | }; |
---|
61 | |
---|
62 | static struct watch *watches; |
---|
63 | static struct watch *lastwatch = NULL; |
---|
64 | static time_t next_pl_fill = 0; |
---|
65 | |
---|
66 | char *get_path_from_wd(int wd) |
---|
67 | { |
---|
68 | struct watch *w = watches; |
---|
69 | |
---|
70 | while( w != NULL ) |
---|
71 | { |
---|
72 | if( w->wd == wd ) |
---|
73 | return w->path; |
---|
74 | w = w->next; |
---|
75 | } |
---|
76 | |
---|
77 | return NULL; |
---|
78 | } |
---|
79 | |
---|
80 | int |
---|
81 | add_watch(int fd, const char * path) |
---|
82 | { |
---|
83 | struct watch *nw; |
---|
84 | int wd; |
---|
85 | |
---|
86 | wd = inotify_add_watch(fd, path, IN_CREATE|IN_CLOSE_WRITE|IN_DELETE|IN_MOVE); |
---|
87 | if( wd < 0 ) |
---|
88 | { |
---|
89 | DPRINTF(E_ERROR, L_INOTIFY, "inotify_add_watch(%s) [%s]\n", path, strerror(errno)); |
---|
90 | return -1; |
---|
91 | } |
---|
92 | |
---|
93 | nw = malloc(sizeof(struct watch)); |
---|
94 | if( nw == NULL ) |
---|
95 | { |
---|
96 | DPRINTF(E_ERROR, L_INOTIFY, "malloc() error\n"); |
---|
97 | return -1; |
---|
98 | } |
---|
99 | nw->wd = wd; |
---|
100 | nw->next = NULL; |
---|
101 | nw->path = strdup(path); |
---|
102 | |
---|
103 | if( watches == NULL ) |
---|
104 | { |
---|
105 | watches = nw; |
---|
106 | } |
---|
107 | |
---|
108 | if( lastwatch != NULL ) |
---|
109 | { |
---|
110 | lastwatch->next = nw; |
---|
111 | } |
---|
112 | lastwatch = nw; |
---|
113 | |
---|
114 | return wd; |
---|
115 | } |
---|
116 | |
---|
117 | int |
---|
118 | remove_watch(int fd, const char * path) |
---|
119 | { |
---|
120 | struct watch *w; |
---|
121 | |
---|
122 | for( w = watches; w; w = w->next ) |
---|
123 | { |
---|
124 | if( strcmp(path, w->path) == 0 ) |
---|
125 | return(inotify_rm_watch(fd, w->wd)); |
---|
126 | } |
---|
127 | |
---|
128 | return 1; |
---|
129 | } |
---|
130 | |
---|
131 | unsigned int |
---|
132 | next_highest(unsigned int num) |
---|
133 | { |
---|
134 | num |= num >> 1; |
---|
135 | num |= num >> 2; |
---|
136 | num |= num >> 4; |
---|
137 | num |= num >> 8; |
---|
138 | num |= num >> 16; |
---|
139 | return(++num); |
---|
140 | } |
---|
141 | |
---|
142 | int |
---|
143 | inotify_create_watches(int fd) |
---|
144 | { |
---|
145 | FILE * max_watches; |
---|
146 | unsigned int num_watches, watch_limit = 8192; |
---|
147 | char **result; |
---|
148 | int i, rows = 0; |
---|
149 | struct media_dir_s * media_path; |
---|
150 | |
---|
151 | i = sql_get_int_field(db, "SELECT count(*) from DETAILS where SIZE is NULL and PATH is not NULL"); |
---|
152 | num_watches = (i < 0) ? 0 : i; |
---|
153 | |
---|
154 | max_watches = fopen("/proc/sys/fs/inotify/max_user_watches", "r"); |
---|
155 | if( max_watches ) |
---|
156 | { |
---|
157 | fscanf(max_watches, "%10u", &watch_limit); |
---|
158 | fclose(max_watches); |
---|
159 | if( (watch_limit < DESIRED_WATCH_LIMIT) || (watch_limit < (num_watches*3/4)) ) |
---|
160 | { |
---|
161 | max_watches = fopen("/proc/sys/fs/inotify/max_user_watches", "w"); |
---|
162 | if( max_watches ) |
---|
163 | { |
---|
164 | if( DESIRED_WATCH_LIMIT >= (num_watches*3/4) ) |
---|
165 | { |
---|
166 | fprintf(max_watches, "%u", DESIRED_WATCH_LIMIT); |
---|
167 | } |
---|
168 | else if( next_highest(num_watches) >= (num_watches*3/4) ) |
---|
169 | { |
---|
170 | fprintf(max_watches, "%u", next_highest(num_watches)); |
---|
171 | } |
---|
172 | else |
---|
173 | { |
---|
174 | fprintf(max_watches, "%u", next_highest(next_highest(num_watches))); |
---|
175 | } |
---|
176 | fclose(max_watches); |
---|
177 | } |
---|
178 | else |
---|
179 | { |
---|
180 | DPRINTF(E_WARN, L_INOTIFY, "WARNING: Inotify max_user_watches [%u] is low or close to the number of used watches [%u] " |
---|
181 | "and I do not have permission to increase this limit. Please do so manually by " |
---|
182 | "writing a higher value into /proc/sys/fs/inotify/max_user_watches.\n", watch_limit, num_watches); |
---|
183 | } |
---|
184 | } |
---|
185 | } |
---|
186 | else |
---|
187 | { |
---|
188 | DPRINTF(E_WARN, L_INOTIFY, "WARNING: Could not read inotify max_user_watches! " |
---|
189 | "Hopefully it is enough to cover %u current directories plus any new ones added.\n", num_watches); |
---|
190 | } |
---|
191 | |
---|
192 | for( media_path = media_dirs; media_path != NULL; media_path = media_path->next ) |
---|
193 | { |
---|
194 | DPRINTF(E_DEBUG, L_INOTIFY, "Add watch to %s\n", media_path->path); |
---|
195 | add_watch(fd, media_path->path); |
---|
196 | } |
---|
197 | sql_get_table(db, "SELECT PATH from DETAILS where SIZE is NULL and PATH is not NULL", &result, &rows, NULL); |
---|
198 | for( i=1; i <= rows; i++ ) |
---|
199 | { |
---|
200 | DPRINTF(E_DEBUG, L_INOTIFY, "Add watch to %s\n", result[i]); |
---|
201 | add_watch(fd, result[i]); |
---|
202 | } |
---|
203 | sqlite3_free_table(result); |
---|
204 | |
---|
205 | return rows; |
---|
206 | } |
---|
207 | |
---|
208 | int |
---|
209 | inotify_remove_watches(int fd) |
---|
210 | { |
---|
211 | struct watch *w = watches; |
---|
212 | struct watch *last_w; |
---|
213 | int rm_watches = 0; |
---|
214 | |
---|
215 | while( w ) |
---|
216 | { |
---|
217 | last_w = w; |
---|
218 | inotify_rm_watch(fd, w->wd); |
---|
219 | if( w->path ) |
---|
220 | free(w->path); |
---|
221 | rm_watches++; |
---|
222 | w = w->next; |
---|
223 | free(last_w); |
---|
224 | } |
---|
225 | |
---|
226 | return rm_watches; |
---|
227 | } |
---|
228 | |
---|
229 | int add_dir_watch(int fd, char * path, char * filename) |
---|
230 | { |
---|
231 | DIR *ds; |
---|
232 | struct dirent *e; |
---|
233 | char *buf; |
---|
234 | int wd; |
---|
235 | int i = 0; |
---|
236 | |
---|
237 | if( filename ) |
---|
238 | asprintf(&buf, "%s/%s", path, filename); |
---|
239 | else |
---|
240 | buf = strdup(path); |
---|
241 | |
---|
242 | wd = add_watch(fd, buf); |
---|
243 | if( wd == -1 ) |
---|
244 | { |
---|
245 | DPRINTF(E_ERROR, L_INOTIFY, "add_watch() [%s]\n", strerror(errno)); |
---|
246 | } |
---|
247 | else |
---|
248 | { |
---|
249 | DPRINTF(E_INFO, L_INOTIFY, "Added watch to %s [%d]\n", buf, wd); |
---|
250 | } |
---|
251 | |
---|
252 | ds = opendir(buf); |
---|
253 | if( ds != NULL ) |
---|
254 | { |
---|
255 | while( (e = readdir(ds)) ) |
---|
256 | { |
---|
257 | if( strcmp(e->d_name, ".") == 0 || |
---|
258 | strcmp(e->d_name, "..") == 0 ) |
---|
259 | continue; |
---|
260 | if( (e->d_type == DT_DIR) || |
---|
261 | (e->d_type == DT_UNKNOWN && resolve_unknown_type(buf, NO_MEDIA) == TYPE_DIR) ) |
---|
262 | i += add_dir_watch(fd, buf, e->d_name); |
---|
263 | } |
---|
264 | } |
---|
265 | else |
---|
266 | { |
---|
267 | DPRINTF(E_ERROR, L_INOTIFY, "Opendir error! [%s]\n", strerror(errno)); |
---|
268 | } |
---|
269 | closedir(ds); |
---|
270 | i++; |
---|
271 | free(buf); |
---|
272 | |
---|
273 | return(i); |
---|
274 | } |
---|
275 | |
---|
276 | int |
---|
277 | inotify_insert_file(char * name, const char * path) |
---|
278 | { |
---|
279 | int len; |
---|
280 | char * last_dir; |
---|
281 | char * path_buf; |
---|
282 | char * base_name; |
---|
283 | char * base_copy; |
---|
284 | char * parent_buf = NULL; |
---|
285 | char * id = NULL; |
---|
286 | int depth = 1; |
---|
287 | int ts; |
---|
288 | enum media_types type = ALL_MEDIA; |
---|
289 | struct media_dir_s * media_path = media_dirs; |
---|
290 | struct stat st; |
---|
291 | |
---|
292 | /* Is it cover art for another file? */ |
---|
293 | if( is_image(path) ) |
---|
294 | update_if_album_art(path); |
---|
295 | else if( ends_with(path, ".srt") ) |
---|
296 | check_for_captions(path, 0); |
---|
297 | |
---|
298 | /* Check if we're supposed to be scanning for this file type in this directory */ |
---|
299 | while( media_path ) |
---|
300 | { |
---|
301 | if( strncmp(path, media_path->path, strlen(media_path->path)) == 0 ) |
---|
302 | { |
---|
303 | type = media_path->type; |
---|
304 | break; |
---|
305 | } |
---|
306 | media_path = media_path->next; |
---|
307 | } |
---|
308 | switch( type ) |
---|
309 | { |
---|
310 | case ALL_MEDIA: |
---|
311 | if( !is_image(path) && |
---|
312 | !is_audio(path) && |
---|
313 | !is_video(path) && |
---|
314 | !is_playlist(path) ) |
---|
315 | return -1; |
---|
316 | break; |
---|
317 | case AUDIO_ONLY: |
---|
318 | if( !is_audio(path) && |
---|
319 | !is_playlist(path) ) |
---|
320 | return -1; |
---|
321 | break; |
---|
322 | case VIDEO_ONLY: |
---|
323 | if( !is_video(path) ) |
---|
324 | return -1; |
---|
325 | break; |
---|
326 | case IMAGES_ONLY: |
---|
327 | if( !is_image(path) ) |
---|
328 | return -1; |
---|
329 | break; |
---|
330 | default: |
---|
331 | return -1; |
---|
332 | break; |
---|
333 | } |
---|
334 | |
---|
335 | /* If it's already in the database and hasn't been modified, skip it. */ |
---|
336 | if( stat(path, &st) != 0 ) |
---|
337 | return -1; |
---|
338 | |
---|
339 | ts = sql_get_int_field(db, "SELECT TIMESTAMP from DETAILS where PATH = '%q'", path); |
---|
340 | if( !ts && is_playlist(path) && (sql_get_int_field(db, "SELECT ID from PLAYLISTS where PATH = '%q'", path) > 0) ) |
---|
341 | { |
---|
342 | DPRINTF(E_DEBUG, L_INOTIFY, "Re-reading modified playlist.\n", path); |
---|
343 | inotify_remove_file(path); |
---|
344 | next_pl_fill = 1; |
---|
345 | } |
---|
346 | else if( ts < st.st_mtime ) |
---|
347 | { |
---|
348 | if( ts > 0 ) |
---|
349 | DPRINTF(E_DEBUG, L_INOTIFY, "%s is newer than the last db entry.\n", path); |
---|
350 | inotify_remove_file(path); |
---|
351 | } |
---|
352 | |
---|
353 | /* Find the parentID. If it's not found, create all necessary parents. */ |
---|
354 | len = strlen(path)+1; |
---|
355 | if( !(path_buf = malloc(len)) || |
---|
356 | !(last_dir = malloc(len)) || |
---|
357 | !(base_name = malloc(len)) ) |
---|
358 | return -1; |
---|
359 | base_copy = base_name; |
---|
360 | while( depth ) |
---|
361 | { |
---|
362 | depth = 0; |
---|
363 | strcpy(path_buf, path); |
---|
364 | parent_buf = dirname(path_buf); |
---|
365 | |
---|
366 | do |
---|
367 | { |
---|
368 | //DEBUG DPRINTF(E_DEBUG, L_INOTIFY, "Checking %s\n", parent_buf); |
---|
369 | id = sql_get_text_field(db, "SELECT OBJECT_ID from OBJECTS o left join DETAILS d on (d.ID = o.DETAIL_ID)" |
---|
370 | " where d.PATH = '%q' and REF_ID is NULL", parent_buf); |
---|
371 | if( id ) |
---|
372 | { |
---|
373 | if( !depth ) |
---|
374 | break; |
---|
375 | DPRINTF(E_DEBUG, L_INOTIFY, "Found first known parentID: %s [%s]\n", id, parent_buf); |
---|
376 | /* Insert newly-found directory */ |
---|
377 | strcpy(base_name, last_dir); |
---|
378 | base_copy = basename(base_name); |
---|
379 | insert_directory(base_copy, last_dir, BROWSEDIR_ID, id+2, get_next_available_id("OBJECTS", id)); |
---|
380 | sqlite3_free(id); |
---|
381 | break; |
---|
382 | } |
---|
383 | depth++; |
---|
384 | strcpy(last_dir, parent_buf); |
---|
385 | parent_buf = dirname(parent_buf); |
---|
386 | } |
---|
387 | while( strcmp(parent_buf, "/") != 0 ); |
---|
388 | |
---|
389 | if( strcmp(parent_buf, "/") == 0 ) |
---|
390 | { |
---|
391 | id = sqlite3_mprintf("%s", BROWSEDIR_ID); |
---|
392 | depth = 0; |
---|
393 | break; |
---|
394 | } |
---|
395 | strcpy(path_buf, path); |
---|
396 | } |
---|
397 | free(last_dir); |
---|
398 | free(path_buf); |
---|
399 | free(base_name); |
---|
400 | |
---|
401 | if( !depth ) |
---|
402 | { |
---|
403 | //DEBUG DPRINTF(E_DEBUG, L_INOTIFY, "Inserting %s\n", name); |
---|
404 | insert_file(name, path, id+2, get_next_available_id("OBJECTS", id)); |
---|
405 | sqlite3_free(id); |
---|
406 | if( (is_audio(path) || is_playlist(path)) && next_pl_fill != 1 ) |
---|
407 | { |
---|
408 | next_pl_fill = time(NULL) + 120; // Schedule a playlist scan for 2 minutes from now. |
---|
409 | //DEBUG DPRINTF(E_WARN, L_INOTIFY, "Playlist scan scheduled for %s", ctime(&next_pl_fill)); |
---|
410 | } |
---|
411 | } |
---|
412 | return depth; |
---|
413 | } |
---|
414 | |
---|
415 | int |
---|
416 | inotify_insert_directory(int fd, char *name, const char * path) |
---|
417 | { |
---|
418 | DIR * ds; |
---|
419 | struct dirent * e; |
---|
420 | char *id, *path_buf, *parent_buf, *esc_name; |
---|
421 | int wd; |
---|
422 | enum file_types type = TYPE_UNKNOWN; |
---|
423 | enum media_types dir_type = ALL_MEDIA; |
---|
424 | struct media_dir_s * media_path; |
---|
425 | struct stat st; |
---|
426 | |
---|
427 | if( access(path, R_OK|X_OK) != 0 ) |
---|
428 | { |
---|
429 | DPRINTF(E_WARN, L_INOTIFY, "Could not access %s [%s]\n", path, strerror(errno)); |
---|
430 | return -1; |
---|
431 | } |
---|
432 | if( sql_get_int_field(db, "SELECT ID from DETAILS where PATH = '%q'", path) > 0 ) |
---|
433 | { |
---|
434 | DPRINTF(E_DEBUG, L_INOTIFY, "%s already exists\n", path); |
---|
435 | return 0; |
---|
436 | } |
---|
437 | |
---|
438 | parent_buf = strdup(path); |
---|
439 | id = sql_get_text_field(db, "SELECT OBJECT_ID from OBJECTS o left join DETAILS d on (d.ID = o.DETAIL_ID)" |
---|
440 | " where d.PATH = '%q' and REF_ID is NULL", dirname(parent_buf)); |
---|
441 | if( !id ) |
---|
442 | id = sqlite3_mprintf("%s", BROWSEDIR_ID); |
---|
443 | insert_directory(name, path, BROWSEDIR_ID, id+2, get_next_available_id("OBJECTS", id)); |
---|
444 | sqlite3_free(id); |
---|
445 | free(parent_buf); |
---|
446 | |
---|
447 | wd = add_watch(fd, path); |
---|
448 | if( wd == -1 ) |
---|
449 | { |
---|
450 | DPRINTF(E_ERROR, L_INOTIFY, "add_watch() failed\n"); |
---|
451 | } |
---|
452 | else |
---|
453 | { |
---|
454 | DPRINTF(E_INFO, L_INOTIFY, "Added watch to %s [%d]\n", path, wd); |
---|
455 | } |
---|
456 | |
---|
457 | media_path = media_dirs; |
---|
458 | while( media_path ) |
---|
459 | { |
---|
460 | if( strncmp(path, media_path->path, strlen(media_path->path)) == 0 ) |
---|
461 | { |
---|
462 | dir_type = media_path->type; |
---|
463 | break; |
---|
464 | } |
---|
465 | media_path = media_path->next; |
---|
466 | } |
---|
467 | |
---|
468 | ds = opendir(path); |
---|
469 | if( !ds ) |
---|
470 | { |
---|
471 | DPRINTF(E_ERROR, L_INOTIFY, "opendir failed! [%s]\n", strerror(errno)); |
---|
472 | return -1; |
---|
473 | } |
---|
474 | while( (e = readdir(ds)) ) |
---|
475 | { |
---|
476 | if( e->d_name[0] == '.' ) |
---|
477 | continue; |
---|
478 | esc_name = escape_tag(e->d_name, 1); |
---|
479 | asprintf(&path_buf, "%s/%s", path, e->d_name); |
---|
480 | switch( e->d_type ) |
---|
481 | { |
---|
482 | case DT_DIR: |
---|
483 | case DT_REG: |
---|
484 | case DT_LNK: |
---|
485 | case DT_UNKNOWN: |
---|
486 | type = resolve_unknown_type(path_buf, dir_type); |
---|
487 | default: |
---|
488 | break; |
---|
489 | } |
---|
490 | if( type == TYPE_DIR ) |
---|
491 | { |
---|
492 | inotify_insert_directory(fd, esc_name, path_buf); |
---|
493 | } |
---|
494 | else if( type == TYPE_FILE ) |
---|
495 | { |
---|
496 | if( (stat(path_buf, &st) == 0) && (st.st_blocks<<9 >= st.st_size) ) |
---|
497 | { |
---|
498 | inotify_insert_file(esc_name, path_buf); |
---|
499 | } |
---|
500 | } |
---|
501 | free(esc_name); |
---|
502 | free(path_buf); |
---|
503 | } |
---|
504 | closedir(ds); |
---|
505 | |
---|
506 | return 0; |
---|
507 | } |
---|
508 | |
---|
509 | int |
---|
510 | inotify_remove_file(const char * path) |
---|
511 | { |
---|
512 | char * sql; |
---|
513 | char * art_cache; |
---|
514 | char * ptr; |
---|
515 | char **result; |
---|
516 | sqlite_int64 detailID; |
---|
517 | int rows, playlist; |
---|
518 | |
---|
519 | /* Invalidate the scanner cache so we don't insert files into non-existent containers */ |
---|
520 | valid_cache = 0; |
---|
521 | playlist = is_playlist(path); |
---|
522 | sql = sql_get_text_field(db, "SELECT ID from %s where PATH = '%q'", playlist?"PLAYLISTS":"DETAILS", path); |
---|
523 | if( !sql ) |
---|
524 | return 1; |
---|
525 | detailID = strtoll(sql, NULL, 10); |
---|
526 | sqlite3_free(sql); |
---|
527 | if( playlist ) |
---|
528 | { |
---|
529 | sql_exec(db, "DELETE from PLAYLISTS where ID = %lld", detailID); |
---|
530 | sql_exec(db, "DELETE from DETAILS where ID =" |
---|
531 | " (SELECT DETAIL_ID from OBJECTS where OBJECT_ID = '%s$%llX')", |
---|
532 | MUSIC_PLIST_ID, detailID); |
---|
533 | sql_exec(db, "DELETE from OBJECTS where OBJECT_ID = '%s$%llX' or PARENT_ID = '%s$%llX'", |
---|
534 | MUSIC_PLIST_ID, detailID, MUSIC_PLIST_ID, detailID); |
---|
535 | } |
---|
536 | else |
---|
537 | { |
---|
538 | /* Delete the parent containers if we are about to empty them. */ |
---|
539 | asprintf(&sql, "SELECT PARENT_ID from OBJECTS where DETAIL_ID = %lld", detailID); |
---|
540 | if( (sql_get_table(db, sql, &result, &rows, NULL) == SQLITE_OK) ) |
---|
541 | { |
---|
542 | int i, children; |
---|
543 | for( i=1; i <= rows; i++ ) |
---|
544 | { |
---|
545 | /* If it's a playlist item, adjust the item count of the playlist */ |
---|
546 | if( strncmp(result[i], MUSIC_PLIST_ID, strlen(MUSIC_PLIST_ID)) == 0 ) |
---|
547 | { |
---|
548 | sql_exec(db, "UPDATE PLAYLISTS set FOUND = (FOUND-1) where ID = %d", |
---|
549 | atoi(strrchr(result[i], '$') + 1)); |
---|
550 | } |
---|
551 | |
---|
552 | children = sql_get_int_field(db, "SELECT count(*) from OBJECTS where PARENT_ID = '%s'", result[i]); |
---|
553 | if( children < 0 ) |
---|
554 | continue; |
---|
555 | if( children < 2 ) |
---|
556 | { |
---|
557 | sql_exec(db, "DELETE from DETAILS where ID =" |
---|
558 | " (SELECT DETAIL_ID from OBJECTS where OBJECT_ID = '%s')", result[i]); |
---|
559 | sql_exec(db, "DELETE from OBJECTS where OBJECT_ID = '%s'", result[i]); |
---|
560 | |
---|
561 | ptr = strrchr(result[i], '$'); |
---|
562 | if( ptr ) |
---|
563 | *ptr = '\0'; |
---|
564 | if( sql_get_int_field(db, "SELECT count(*) from OBJECTS where PARENT_ID = '%s'", result[i]) == 0 ) |
---|
565 | { |
---|
566 | sql_exec(db, "DELETE from DETAILS where ID =" |
---|
567 | " (SELECT DETAIL_ID from OBJECTS where OBJECT_ID = '%s')", result[i]); |
---|
568 | sql_exec(db, "DELETE from OBJECTS where OBJECT_ID = '%s'", result[i]); |
---|
569 | } |
---|
570 | } |
---|
571 | } |
---|
572 | sqlite3_free_table(result); |
---|
573 | } |
---|
574 | free(sql); |
---|
575 | /* Now delete the actual objects */ |
---|
576 | sql_exec(db, "DELETE from DETAILS where ID = %lld", detailID); |
---|
577 | sql_exec(db, "DELETE from OBJECTS where DETAIL_ID = %lld", detailID); |
---|
578 | } |
---|
579 | asprintf(&art_cache, "%s/art_cache%s", db_path, path); |
---|
580 | remove(art_cache); |
---|
581 | free(art_cache); |
---|
582 | |
---|
583 | return 0; |
---|
584 | } |
---|
585 | |
---|
586 | int |
---|
587 | inotify_remove_directory(int fd, const char * path) |
---|
588 | { |
---|
589 | char * sql; |
---|
590 | char **result; |
---|
591 | sqlite_int64 detailID = 0; |
---|
592 | int rows, i, ret = 1; |
---|
593 | |
---|
594 | remove_watch(fd, path); |
---|
595 | sql = sqlite3_mprintf("SELECT ID from DETAILS where PATH glob '%q/*'" |
---|
596 | " UNION ALL SELECT ID from DETAILS where PATH = '%q'", path, path); |
---|
597 | if( (sql_get_table(db, sql, &result, &rows, NULL) == SQLITE_OK) ) |
---|
598 | { |
---|
599 | if( rows ) |
---|
600 | { |
---|
601 | for( i=1; i <= rows; i++ ) |
---|
602 | { |
---|
603 | detailID = strtoll(result[i], NULL, 10); |
---|
604 | sql_exec(db, "DELETE from DETAILS where ID = %lld", detailID); |
---|
605 | sql_exec(db, "DELETE from OBJECTS where DETAIL_ID = %lld", detailID); |
---|
606 | } |
---|
607 | ret = 0; |
---|
608 | } |
---|
609 | sqlite3_free_table(result); |
---|
610 | } |
---|
611 | sqlite3_free(sql); |
---|
612 | /* Clean up any album art entries in the deleted directory */ |
---|
613 | sql_exec(db, "DELETE from ALBUM_ART where PATH glob '%q/*'", path); |
---|
614 | |
---|
615 | return ret; |
---|
616 | } |
---|
617 | |
---|
618 | void * |
---|
619 | start_inotify() |
---|
620 | { |
---|
621 | struct pollfd pollfds[1]; |
---|
622 | int timeout = 1000; |
---|
623 | char buffer[BUF_LEN]; |
---|
624 | char path_buf[PATH_MAX]; |
---|
625 | int length, i = 0; |
---|
626 | char * esc_name = NULL; |
---|
627 | struct stat st; |
---|
628 | |
---|
629 | pollfds[0].fd = inotify_init(); |
---|
630 | pollfds[0].events = POLLIN; |
---|
631 | |
---|
632 | if ( pollfds[0].fd < 0 ) |
---|
633 | DPRINTF(E_ERROR, L_INOTIFY, "inotify_init() failed!\n"); |
---|
634 | |
---|
635 | while( scanning ) |
---|
636 | { |
---|
637 | if( quitting ) |
---|
638 | goto quitting; |
---|
639 | sleep(1); |
---|
640 | } |
---|
641 | inotify_create_watches(pollfds[0].fd); |
---|
642 | if (setpriority(PRIO_PROCESS, 0, 19) == -1) |
---|
643 | DPRINTF(E_WARN, L_INOTIFY, "Failed to reduce inotify thread priority\n"); |
---|
644 | |
---|
645 | while( !quitting ) |
---|
646 | { |
---|
647 | length = poll(pollfds, 1, timeout); |
---|
648 | if( !length ) |
---|
649 | { |
---|
650 | if( next_pl_fill && (time(NULL) >= next_pl_fill) ) |
---|
651 | { |
---|
652 | fill_playlists(); |
---|
653 | next_pl_fill = 0; |
---|
654 | } |
---|
655 | continue; |
---|
656 | } |
---|
657 | else if( length < 0 ) |
---|
658 | { |
---|
659 | if( (errno == EINTR) || (errno == EAGAIN) ) |
---|
660 | continue; |
---|
661 | else |
---|
662 | DPRINTF(E_ERROR, L_INOTIFY, "read failed!\n"); |
---|
663 | } |
---|
664 | else |
---|
665 | { |
---|
666 | length = read(pollfds[0].fd, buffer, BUF_LEN); |
---|
667 | } |
---|
668 | |
---|
669 | i = 0; |
---|
670 | while( i < length ) |
---|
671 | { |
---|
672 | struct inotify_event * event = (struct inotify_event *) &buffer[i]; |
---|
673 | if( event->len ) |
---|
674 | { |
---|
675 | if( *(event->name) == '.' ) |
---|
676 | { |
---|
677 | i += EVENT_SIZE + event->len; |
---|
678 | continue; |
---|
679 | } |
---|
680 | esc_name = modifyString(strdup(event->name), "&", "&amp;", 0); |
---|
681 | sprintf(path_buf, "%s/%s", get_path_from_wd(event->wd), event->name); |
---|
682 | if ( event->mask & IN_ISDIR && (event->mask & (IN_CREATE|IN_MOVED_TO)) ) |
---|
683 | { |
---|
684 | DPRINTF(E_DEBUG, L_INOTIFY, "The directory %s was %s.\n", |
---|
685 | path_buf, (event->mask & IN_MOVED_TO ? "moved here" : "created")); |
---|
686 | inotify_insert_directory(pollfds[0].fd, esc_name, path_buf); |
---|
687 | } |
---|
688 | else if ( (event->mask & (IN_CLOSE_WRITE|IN_MOVED_TO|IN_CREATE)) && |
---|
689 | (lstat(path_buf, &st) == 0) ) |
---|
690 | { |
---|
691 | if( S_ISLNK(st.st_mode) ) |
---|
692 | { |
---|
693 | DPRINTF(E_DEBUG, L_INOTIFY, "The symbolic link %s was %s.\n", |
---|
694 | path_buf, (event->mask & IN_MOVED_TO ? "moved here" : "created")); |
---|
695 | if( stat(path_buf, &st) == 0 && S_ISDIR(st.st_mode) ) |
---|
696 | inotify_insert_directory(pollfds[0].fd, esc_name, path_buf); |
---|
697 | else |
---|
698 | inotify_insert_file(esc_name, path_buf); |
---|
699 | } |
---|
700 | else if( event->mask & (IN_CLOSE_WRITE|IN_MOVED_TO) && st.st_size > 0 ) |
---|
701 | { |
---|
702 | if( (event->mask & IN_MOVED_TO) || |
---|
703 | (sql_get_int_field(db, "SELECT TIMESTAMP from DETAILS where PATH = '%q'", path_buf) != st.st_mtime) ) |
---|
704 | { |
---|
705 | DPRINTF(E_DEBUG, L_INOTIFY, "The file %s was %s.\n", |
---|
706 | path_buf, (event->mask & IN_MOVED_TO ? "moved here" : "changed")); |
---|
707 | inotify_insert_file(esc_name, path_buf); |
---|
708 | } |
---|
709 | } |
---|
710 | } |
---|
711 | else if ( event->mask & (IN_DELETE|IN_MOVED_FROM) ) |
---|
712 | { |
---|
713 | DPRINTF(E_DEBUG, L_INOTIFY, "The %s %s was %s.\n", |
---|
714 | (event->mask & IN_ISDIR ? "directory" : "file"), |
---|
715 | path_buf, (event->mask & IN_MOVED_FROM ? "moved away" : "deleted")); |
---|
716 | if ( event->mask & IN_ISDIR ) |
---|
717 | inotify_remove_directory(pollfds[0].fd, path_buf); |
---|
718 | else |
---|
719 | inotify_remove_file(path_buf); |
---|
720 | } |
---|
721 | free(esc_name); |
---|
722 | } |
---|
723 | i += EVENT_SIZE + event->len; |
---|
724 | } |
---|
725 | } |
---|
726 | inotify_remove_watches(pollfds[0].fd); |
---|
727 | quitting: |
---|
728 | close(pollfds[0].fd); |
---|
729 | |
---|
730 | return 0; |
---|
731 | } |
---|