Add new files to the database as they are played. --- apps/tagcache.c | 144 ++++++++++++++++++++++++++++++++++++++++--------------- apps/tagcache.h | 1 + apps/tagtree.c | 17 +++++- 3 files changed, 120 insertions(+), 42 deletions(-) diff --git a/apps/tagcache.c b/apps/tagcache.c index ea45eae..4e3bc3e 100644 --- a/apps/tagcache.c +++ b/apps/tagcache.c @@ -55,7 +55,7 @@ * */ -/*#define LOGF_ENABLE*/ +#define LOGF_ENABLE #include #include @@ -4399,26 +4399,17 @@ void tagcache_screensync_enable(bool state) tc_stat.syncscreen = state; } -void tagcache_build(const char *path) +static void tagcache_build_init(struct tagcache_header *header) { - struct tagcache_header header; - bool ret; - - curpath[0] = '\0'; data_size = 0; total_entry_count = 0; - processed_dir_count = 0; - -#ifdef HAVE_DIRCACHE - while (dircache_is_initializing()) - sleep(1); -#endif - + logf("updating tagcache"); cachefd = open(TAGCACHE_FILE_TEMP, O_RDONLY); if (cachefd >= 0) { + /* XXX what if we add just single files in quick succession? */ logf("skipping, cache already waiting for commit"); close(cachefd); return ; @@ -4431,34 +4422,22 @@ void tagcache_build(const char *path) return ; } - filenametag_fd = open_tag_fd(&header, tag_filename, false); + filenametag_fd = open_tag_fd(header, tag_filename, false); cpu_boost(true); - logf("Scanning files..."); - /* Scan for new files. */ - memset(&header, 0, sizeof(struct tagcache_header)); - write(cachefd, &header, sizeof(struct tagcache_header)); - - ret = true; - roots_ll.path = path; - roots_ll.next = NULL; - struct search_roots_ll * this; - /* check_dir might add new roots */ - for(this = &roots_ll; this; this = this->next) - { - strcpy(curpath, this->path); - ret = ret && check_dir(this->path, true); - } - if (roots_ll.next) - free_search_roots(roots_ll.next); + memset(header, 0, sizeof(struct tagcache_header)); + write(cachefd, header, sizeof(struct tagcache_header)); +} +static void tagcache_build_finish(struct tagcache_header *header, bool success) +{ /* Write the header. */ - header.magic = TAGCACHE_MAGIC; - header.datasize = data_size; - header.entry_count = total_entry_count; + header->magic = TAGCACHE_MAGIC; + header->datasize = data_size; + header->entry_count = total_entry_count; lseek(cachefd, 0, SEEK_SET); - write(cachefd, &header, sizeof(struct tagcache_header)); + write(cachefd, header, sizeof(struct tagcache_header)); close(cachefd); if (filenametag_fd >= 0) @@ -4467,11 +4446,11 @@ void tagcache_build(const char *path) filenametag_fd = -1; } - if (!ret) + if (!success) { logf("Aborted."); cpu_boost(false); - return ; + return; } /* Commit changes to the database. */ @@ -4486,6 +4465,69 @@ void tagcache_build(const char *path) free_tempbuf(); #endif + cpu_boost(false); +} + +#ifndef __PCTOOL__ +static void tagcache_add_file(const struct dircache_entry *dc) +{ + struct tagcache_header header; + + tagcache_build_init(&header); + + /* XXX doesn't handle ignore/unignore files in hierarchy + above. You play it, you add it. */ + + dircache_copy_path(dc, curpath, sizeof(curpath) - 1); + + /* Add a new entry to the temporary db file. */ + add_tagcache(curpath, (dc->info.wrtdate << 16) | dc->info.wrttime +#if defined(HAVE_TC_RAMCACHE) && defined(HAVE_DIRCACHE) + , dc +#endif + ); + + tagcache_build_finish(&header, true); +} +#endif + +void tagcache_build(const char *path) +{ + struct tagcache_header header; + bool ret; + + curpath[0] = '\0'; + processed_dir_count = 0; + +#ifdef HAVE_DIRCACHE + while (dircache_is_initializing()) + sleep(1); +#endif + + tagcache_build_init(&header); + + logf("Scanning files..."); + + /* Scan for new files. */ + + ret = true; + roots_ll.path = path; + roots_ll.next = NULL; + struct search_roots_ll * this; + /* check_dir might add new roots */ + for(this = &roots_ll; this; this = this->next) + { + strcpy(curpath, this->path); + ret = ret && check_dir(this->path, true); + } + if (roots_ll.next) + free_search_roots(roots_ll.next); + + tagcache_build_finish(&header, ret); + + if (!ret) + return; + #ifdef HAVE_TC_RAMCACHE if (hdr) { @@ -4495,7 +4537,6 @@ void tagcache_build(const char *path) } #endif - cpu_boost(false); } #ifdef HAVE_TC_RAMCACHE @@ -4587,7 +4628,10 @@ static void tagcache_thread(void) break; case Q_UPDATE: - tagcache_build("/"); + if (ev.data) + tagcache_add_file((const struct dircache_entry *) ev.data); + else + tagcache_build("/"); #ifdef HAVE_TC_RAMCACHE load_ramcache(); #endif @@ -4707,6 +4751,28 @@ bool tagcache_update(void) return false; queue_post(&tagcache_queue, Q_UPDATE, 0); + return false; /* XXX should be true? or void */ +} + +bool tagcache_add(const char *path) +{ + if (!tc_stat.ready) + return false; + + /* XXX #ifdef dircache -- otherwise we cannot relay a stable + pointer identifying the file to the tagcache_thread */ + if (tc_stat.ramcache && is_dircache_intact()) + { + const struct dircache_entry *dc = dircache_get_entry_ptr(path); + + if (dc) + { + queue_post(&tagcache_queue, Q_UPDATE, (long) dc); + return true; + } + + logf("tagcache_add_file: dircache entry not found for %s", path); + } return false; } diff --git a/apps/tagcache.h b/apps/tagcache.h index 59f8b8b..8859528 100644 --- a/apps/tagcache.h +++ b/apps/tagcache.h @@ -252,6 +252,7 @@ bool tagcache_is_usable(void); void tagcache_start_scan(void); void tagcache_stop_scan(void); bool tagcache_update(void); +bool tagcache_add(const char *path); bool tagcache_rebuild(void); int tagcache_get_max_commit_step(void); #endif diff --git a/apps/tagtree.c b/apps/tagtree.c index 575ab22..4e6502f 100644 --- a/apps/tagtree.c +++ b/apps/tagtree.c @@ -24,7 +24,7 @@ * support the tag cache interface. */ -/*#define LOGF_ENABLE*/ +#define LOGF_ENABLE #include #include @@ -662,6 +662,7 @@ static void tagtree_buffer_event(void *data) if (!tagcache_find_index(&tcs, id3->path)) { logf("tc stat: not found: %s", id3->path); + tagcache_add(id3->path); return; } @@ -715,9 +716,19 @@ static void tagtree_track_finish_event(void *data) if (!tagcache_idx) { logf("No tagcache index pointer found"); - return; + + struct tagcache_search tcs; + if (!tagcache_find_index(&tcs, id3->path)) + return; + + logf("Found tagcache index in 2nd try"); + + id3->tagcache_idx = tcs.idx_id+1; + tagcache_idx = tcs.idx_id; + tagcache_search_finish(&tcs); } - tagcache_idx--; + else + tagcache_idx--; /* Don't process unplayed tracks, or tracks interrupted within the first 15 seconds. */ -- 1.7.1