static void process_playlist(char *file, time_t mtime) { enum file_type ft; ft = file_type_get(file); if (ft == FILE_PLAYLIST) scan_playlist(file, mtime); #ifdef ITUNES else if (ft == FILE_ITUNES) scan_itunes_itml(file); #endif }
/* * scamper_file_open * * open the file specified with the appropriate mode. * the modes that we know about are 'r' read-only, 'w' write-only on a * brand new file, and 'a' for appending. * * in 'w' mode [and conditionally for 'a'] an optional parameter may be * supplied that says what type of file should be written. * 'w' for warts * 't' for text * 'a' for arts [not implemented] * * when a file is opened for appending, this second parameter is only * used when the file is empty so that writes will be written in the * format expected. */ scamper_file_t *scamper_file_open(char *filename, char mode, char *type) { scamper_file_t *sf; mode_t mo; int ft = file_type_get(type); int flags = 0; int fd = -1; #ifndef _WIN32 mo = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH; #else mo = _S_IREAD | _S_IWRITE; #endif if(mode == 'r') { if(strcmp(filename, "-") == 0) { fd = STDIN_FILENO; } else { flags = O_RDONLY; } } else if(mode == 'w' || mode == 'a') { /* sanity check the type of file to be written */ if(ft == SCAMPER_FILE_NONE || ft == SCAMPER_FILE_ARTS) { return NULL; } if(strcmp(filename, "-") == 0) { fd = STDIN_FILENO; } else { if(mode == 'w') flags = O_WRONLY | O_TRUNC | O_CREAT; else flags = O_RDWR | O_APPEND | O_CREAT; } } else { return NULL; } #ifdef _WIN32 flags |= O_BINARY; #endif if(fd == -1) { if(mode == 'r') fd = open(filename, flags); else fd = open(filename, flags, mo); if(fd == -1) { return NULL; } } sf = file_open(fd, filename, mode, ft); return sf; }
scamper_file_t *scamper_file_openfd(int fd, char *fn, char mode, char *type) { return file_open(fd, fn, mode, file_type_get(type)); }
/* Thread: scan */ static void process_file(char *file, time_t mtime, off_t size, int type, int flags) { switch (file_type_get(file)) { case FILE_REGULAR: filescanner_process_media(file, mtime, size, type, NULL); counter++; /* When in bulk mode, split transaction in pieces of 200 */ if ((flags & F_SCAN_BULK) && (counter % 200 == 0)) { DPRINTF(E_LOG, L_SCAN, "Scanned %d files...\n", counter); db_transaction_end(); db_transaction_begin(); } break; case FILE_PLAYLIST: case FILE_ITUNES: if (flags & F_SCAN_BULK) defer_playlist(file, mtime); else process_playlist(file, mtime); break; case FILE_CTRL_REMOTE: remote_pairing_read_pin(file); break; #ifdef LASTFM case FILE_CTRL_LASTFM: lastfm_login(file); break; #endif #ifdef HAVE_SPOTIFY_H case FILE_CTRL_SPOTIFY: spotify_login(file); break; #endif case FILE_CTRL_INITSCAN: if (flags & F_SCAN_BULK) break; DPRINTF(E_LOG, L_SCAN, "Startup rescan triggered, found init-rescan file: %s\n", file); inofd_event_unset(); // Clears all inotify watches db_watch_clear(); inofd_event_set(); bulk_scan(F_SCAN_BULK | F_SCAN_RESCAN); break; case FILE_CTRL_FULLSCAN: if (flags & F_SCAN_BULK) break; DPRINTF(E_LOG, L_SCAN, "Full rescan triggered, found full-rescan file: %s\n", file); player_playback_stop(); player_queue_clear(); inofd_event_unset(); // Clears all inotify watches db_purge_all(); // Clears files, playlists, playlistitems, inotify and groups inofd_event_set(); bulk_scan(F_SCAN_BULK); break; default: DPRINTF(E_WARN, L_SCAN, "Ignoring file: %s\n", file); } }
/* Thread: scan */ static void process_inotify_file(struct watch_info *wi, char *path, struct inotify_event *ie) { struct stat sb; char *deref = NULL; char *file = path; int type; int ret; DPRINTF(E_SPAM, L_SCAN, "File event: 0x%x, cookie 0x%x, wd %d\n", ie->mask, ie->cookie, wi->wd); if (ie->mask & IN_DELETE) { DPRINTF(E_DBG, L_SCAN, "File deleted: %s\n", path); db_file_delete_bypath(path); db_pl_delete_bypath(path); } if (ie->mask & IN_MOVED_FROM) { DPRINTF(E_DBG, L_SCAN, "File moved from: %s\n", path); db_file_disable_bypath(path, path, ie->cookie); db_pl_disable_bypath(path, path, ie->cookie); } if (ie->mask & IN_ATTRIB) { DPRINTF(E_DBG, L_SCAN, "File permissions changed: %s\n", path); #ifdef HAVE_EUIDACCESS if (euidaccess(path, R_OK) < 0) #else if (access(path, R_OK) < 0) #endif { DPRINTF(E_LOG, L_SCAN, "File access to '%s' failed: %s\n", path, strerror(errno)); db_file_delete_bypath(path);; } else if ((file_type_get(path) == FILE_REGULAR) && (db_file_id_bypath(path) <= 0)) // TODO Playlists { DPRINTF(E_LOG, L_SCAN, "File access to '%s' achieved\n", path); ie->mask |= IN_CLOSE_WRITE; } } if (ie->mask & IN_MOVED_TO) { DPRINTF(E_DBG, L_SCAN, "File moved to: %s\n", path); ret = db_file_enable_bycookie(ie->cookie, path); if (ret <= 0) { /* It's not a known media file, so it's either a new file * or a playlist, known or not. * We want to scan the new file and we want to rescan the * playlist to update playlist items (relative items). */ ie->mask |= IN_CLOSE_WRITE; db_pl_enable_bycookie(ie->cookie, path); } } if (ie->mask & IN_CREATE) { DPRINTF(E_DBG, L_SCAN, "File created: %s\n", path); ret = lstat(path, &sb); if (ret < 0) { DPRINTF(E_LOG, L_SCAN, "Could not lstat() '%s': %s\n", path, strerror(errno)); return; } if (S_ISFIFO(sb.st_mode)) ie->mask |= IN_CLOSE_WRITE; } if (ie->mask & IN_CLOSE_WRITE) { DPRINTF(E_DBG, L_SCAN, "File closed: %s\n", path); ret = lstat(path, &sb); if (ret < 0) { DPRINTF(E_LOG, L_SCAN, "Could not lstat() '%s': %s\n", path, strerror(errno)); return; } if (S_ISLNK(sb.st_mode)) { deref = m_realpath(path); if (!deref) { DPRINTF(E_LOG, L_SCAN, "Could not dereference symlink '%s': %s\n", path, strerror(errno)); return; } file = deref; ret = stat(deref, &sb); if (ret < 0) { DPRINTF(E_LOG, L_SCAN, "Could not stat() '%s': %s\n", file, strerror(errno)); free(deref); return; } if (S_ISDIR(sb.st_mode)) { process_inotify_dir(wi, deref, ie); free(deref); return; } } type = 0; if (check_speciallib(path, "compilations")) type |= F_SCAN_TYPE_COMPILATION; if (check_speciallib(path, "podcasts")) type |= F_SCAN_TYPE_PODCAST; if (check_speciallib(path, "audiobooks")) type |= F_SCAN_TYPE_AUDIOBOOK; if (S_ISREG(sb.st_mode)) process_file(file, sb.st_mtime, sb.st_size, F_SCAN_TYPE_FILE | type, 0); else if (S_ISFIFO(sb.st_mode)) process_file(file, sb.st_mtime, sb.st_size, F_SCAN_TYPE_PIPE | type, 0); if (deref) free(deref); } }
/* Thread: scan */ static void process_file(char *file, time_t mtime, off_t size, int type, int flags, int dir_id) { int is_bulkscan; int ret; is_bulkscan = (flags & F_SCAN_BULK); switch (file_type_get(file)) { case FILE_REGULAR: filescanner_process_media(file, mtime, size, type, NULL, dir_id); cache_artwork_ping(file, mtime, !is_bulkscan); // TODO [artworkcache] If entry in artwork cache exists for no artwork available, delete the entry if media file has embedded artwork counter++; /* When in bulk mode, split transaction in pieces of 200 */ if ((flags & F_SCAN_BULK) && (counter % 200 == 0)) { DPRINTF(E_LOG, L_SCAN, "Scanned %d files...\n", counter); db_transaction_end(); db_transaction_begin(); } break; case FILE_PLAYLIST: case FILE_ITUNES: if (flags & F_SCAN_BULK) defer_playlist(file, mtime, dir_id); else process_playlist(file, mtime, dir_id); break; case FILE_SMARTPL: DPRINTF(E_DBG, L_SCAN, "Smart playlist file: %s\n", file); scan_smartpl(file, mtime, dir_id); break; case FILE_ARTWORK: DPRINTF(E_DBG, L_SCAN, "Artwork file: %s\n", file); cache_artwork_ping(file, mtime, !is_bulkscan); // TODO [artworkcache] If entry in artwork cache exists for no artwork available for a album with files in the same directory, delete the entry break; case FILE_CTRL_REMOTE: remote_pairing_read_pin(file); break; case FILE_CTRL_LASTFM: #ifdef LASTFM lastfm_login(file); #else DPRINTF(E_LOG, L_SCAN, "Detected LastFM file, but this version was built without LastFM support\n"); #endif break; case FILE_CTRL_SPOTIFY: #ifdef HAVE_SPOTIFY_H spotify_login(file); #else DPRINTF(E_LOG, L_SCAN, "Detected Spotify file, but this version was built without Spotify support\n"); #endif break; case FILE_CTRL_INITSCAN: if (flags & F_SCAN_BULK) break; DPRINTF(E_LOG, L_SCAN, "Startup rescan triggered, found init-rescan file: %s\n", file); filescanner_initscan(NULL, &ret); break; case FILE_CTRL_FULLSCAN: if (flags & F_SCAN_BULK) break; DPRINTF(E_LOG, L_SCAN, "Full rescan triggered, found full-rescan file: %s\n", file); filescanner_fullrescan(NULL, &ret); break; default: DPRINTF(E_WARN, L_SCAN, "Ignoring file: %s\n", file); } }
/* Thread: scan */ static void process_inotify_file(struct watch_info *wi, char *path, struct inotify_event *ie) { struct stat sb; uint32_t path_hash; char *deref = NULL; char *file = path; char *dir; char dir_vpath[PATH_MAX]; int type; int i; int dir_id; char *ptr; int ret; DPRINTF(E_DBG, L_SCAN, "File event: 0x%x, cookie 0x%x, wd %d\n", ie->mask, ie->cookie, wi->wd); path_hash = djb_hash(path, strlen(path)); if (ie->mask & IN_DELETE) { DPRINTF(E_DBG, L_SCAN, "File deleted: %s\n", path); db_file_delete_bypath(path); db_pl_delete_bypath(path); cache_artwork_delete_by_path(path); } if (ie->mask & IN_MOVED_FROM) { DPRINTF(E_DBG, L_SCAN, "File moved from: %s\n", path); db_file_disable_bypath(path, path, ie->cookie); db_pl_disable_bypath(path, path, ie->cookie); } if (ie->mask & IN_ATTRIB) { DPRINTF(E_DBG, L_SCAN, "File attributes changed: %s\n", path); // Ignore the IN_ATTRIB if we just got an IN_CREATE for (i = 0; i < INCOMINGFILES_BUFFER_SIZE; i++) { if (incomingfiles_buffer[i] == path_hash) return; } #ifdef HAVE_EUIDACCESS if (euidaccess(path, R_OK) < 0) #else if (access(path, R_OK) < 0) #endif { DPRINTF(E_LOG, L_SCAN, "File access to '%s' failed: %s\n", path, strerror(errno)); db_file_delete_bypath(path); cache_artwork_delete_by_path(path); } else if ((file_type_get(path) == FILE_REGULAR) && (db_file_id_bypath(path) <= 0)) // TODO Playlists { DPRINTF(E_LOG, L_SCAN, "File access to '%s' achieved\n", path); ie->mask |= IN_CLOSE_WRITE; } } if (ie->mask & IN_MOVED_TO) { DPRINTF(E_DBG, L_SCAN, "File moved to: %s\n", path); ret = db_file_enable_bycookie(ie->cookie, path); if (ret > 0) { // If file was successfully enabled, update the directory id dir = strdup(path); ptr = strrchr(dir, '/'); dir[(ptr - dir)] = '\0'; ret = create_virtual_path(dir, dir_vpath, sizeof(dir_vpath)); if (ret >= 0) { dir_id = db_directory_id_byvirtualpath(dir_vpath); if (dir_id > 0) { ret = db_file_update_directoryid(path, dir_id); if (ret < 0) DPRINTF(E_LOG, L_SCAN, "Error updating directory id for file: %s\n", path); } } free(dir); } else { /* It's not a known media file, so it's either a new file * or a playlist, known or not. * We want to scan the new file and we want to rescan the * playlist to update playlist items (relative items). */ ie->mask |= IN_CLOSE_WRITE; db_pl_enable_bycookie(ie->cookie, path); } } if (ie->mask & IN_CREATE) { DPRINTF(E_DBG, L_SCAN, "File created: %s\n", path); ret = lstat(path, &sb); if (ret < 0) { DPRINTF(E_LOG, L_SCAN, "Could not lstat() '%s': %s\n", path, strerror(errno)); return; } // Add to the list of files where we ignore IN_ATTRIB until the file is closed again if (S_ISREG(sb.st_mode)) { DPRINTF(E_SPAM, L_SCAN, "Incoming file created '%s' (%d), index %d\n", path, (int)path_hash, incomingfiles_idx); incomingfiles_buffer[incomingfiles_idx] = path_hash; incomingfiles_idx = (incomingfiles_idx + 1) % INCOMINGFILES_BUFFER_SIZE; } else if (S_ISFIFO(sb.st_mode)) ie->mask |= IN_CLOSE_WRITE; } if (ie->mask & IN_CLOSE_WRITE) { DPRINTF(E_DBG, L_SCAN, "File closed: %s\n", path); // File has been closed so remove from the IN_ATTRIB ignore list for (i = 0; i < INCOMINGFILES_BUFFER_SIZE; i++) if (incomingfiles_buffer[i] == path_hash) { DPRINTF(E_SPAM, L_SCAN, "Incoming file closed '%s' (%d), index %d\n", path, (int)path_hash, i); incomingfiles_buffer[i] = 0; } ret = lstat(path, &sb); if (ret < 0) { DPRINTF(E_LOG, L_SCAN, "Could not lstat() '%s': %s\n", path, strerror(errno)); return; } if (S_ISLNK(sb.st_mode)) { deref = m_realpath(path); if (!deref) { DPRINTF(E_LOG, L_SCAN, "Could not dereference symlink '%s': %s\n", path, strerror(errno)); return; } file = deref; ret = stat(deref, &sb); if (ret < 0) { DPRINTF(E_LOG, L_SCAN, "Could not stat() '%s': %s\n", file, strerror(errno)); free(deref); return; } if (S_ISDIR(sb.st_mode)) { process_inotify_dir(wi, deref, ie); free(deref); return; } } type = 0; if (check_speciallib(path, "compilations")) type |= F_SCAN_TYPE_COMPILATION; if (check_speciallib(path, "podcasts")) type |= F_SCAN_TYPE_PODCAST; if (check_speciallib(path, "audiobooks")) type |= F_SCAN_TYPE_AUDIOBOOK; dir_id = get_parent_dir_id(file); if (S_ISREG(sb.st_mode)) { process_file(file, sb.st_mtime, sb.st_size, F_SCAN_TYPE_FILE | type, 0, dir_id); } else if (S_ISFIFO(sb.st_mode)) process_file(file, sb.st_mtime, sb.st_size, F_SCAN_TYPE_PIPE | type, 0, dir_id); if (deref) free(deref); } }