fix target name for MPIO HD200 in tools/configure
[maemo-rb.git] / apps / tagcache.c
bloba6cfb4c5902ae25181470a128df3c3f341a839af
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2005 by Miika Pekkarinen
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
23 * TagCache API
25 * ----------x---------x------------------x-----
26 * | | | External
27 * +---------------x-------+ | TagCache | Libraries
28 * | Modification routines | | Core |
29 * +-x---------x-----------+ | |
30 * | (R/W) | | | |
31 * | +------x-------------x-+ +-------------x-----+ |
32 * | | x==x Filters & clauses | |
33 * | | Search routines | +-------------------+ |
34 * | | x============================x DirCache
35 * | +-x--------------------+ | (optional)
36 * | | (R) |
37 * | | +-------------------------------+ +---------+ |
38 * | | | DB Commit (sort,unique,index) | | | |
39 * | | +-x--------------------------x--+ | Control | |
40 * | | | (R/W) | (R) | Thread | |
41 * | | | +----------------------+ | | | |
42 * | | | | TagCache DB Builder | | +---------+ |
43 * | | | +-x-------------x------+ | |
44 * | | | | (R) | (W) | |
45 * | | | | +--x--------x---------+ |
46 * | | | | | Temporary Commit DB | |
47 * | | | | +---------------------+ |
48 * +-x----x-------x--+ |
49 * | TagCache RAM DB x==\(W) +-----------------+ |
50 * +-----------------+ \===x | |
51 * | | | | (R) | Ram DB Loader x============x DirCache
52 * +-x----x---x---x---+ /==x | | (optional)
53 * | Tagcache Disk DB x==/ +-----------------+ |
54 * +------------------+ |
58 /*#define LOGF_ENABLE*/
60 #include <stdio.h>
61 #include <stdlib.h>
62 #include <ctype.h>
63 #include "config.h"
64 #include "ata_idle_notify.h"
65 #include "thread.h"
66 #include "kernel.h"
67 #include "system.h"
68 #include "logf.h"
69 #include "string-extra.h"
70 #include "usb.h"
71 #include "metadata.h"
72 #include "tagcache.h"
73 #include "buffer.h"
74 #include "crc32.h"
75 #include "misc.h"
76 #include "filefuncs.h"
77 #include "settings.h"
78 #include "dir.h"
79 #include "structec.h"
81 #ifndef __PCTOOL__
82 #include "lang.h"
83 #include "eeprom_settings.h"
84 #endif
86 #ifdef __PCTOOL__
87 #define yield() do { } while(0)
88 #define sim_sleep(timeout) do { } while(0)
89 #define do_timed_yield() do { } while(0)
90 #endif
92 #ifndef __PCTOOL__
93 /* Tag Cache thread. */
94 static struct event_queue tagcache_queue;
95 static long tagcache_stack[(DEFAULT_STACK_SIZE + 0x4000)/sizeof(long)];
96 static const char tagcache_thread_name[] = "tagcache";
97 #endif
99 /* Previous path when scanning directory tree recursively. */
100 static char curpath[TAG_MAXLEN+32];
102 /* Used when removing duplicates. */
103 static char *tempbuf; /* Allocated when needed. */
104 static long tempbufidx; /* Current location in buffer. */
105 static long tempbuf_size; /* Buffer size (TEMPBUF_SIZE). */
106 static long tempbuf_left; /* Buffer space left. */
107 static long tempbuf_pos;
109 #define SORTED_TAGS_COUNT 8
110 #define TAGCACHE_IS_UNIQUE(tag) (BIT_N(tag) & TAGCACHE_UNIQUE_TAGS)
111 #define TAGCACHE_IS_SORTED(tag) (BIT_N(tag) & TAGCACHE_SORTED_TAGS)
112 #define TAGCACHE_IS_NUMERIC_OR_NONUNIQUE(tag) \
113 (BIT_N(tag) & (TAGCACHE_NUMERIC_TAGS | ~TAGCACHE_UNIQUE_TAGS))
114 /* Tags we want to get sorted (loaded to the tempbuf). */
115 #define TAGCACHE_SORTED_TAGS ((1LU << tag_artist) | (1LU << tag_album) | \
116 (1LU << tag_genre) | (1LU << tag_composer) | (1LU << tag_comment) | \
117 (1LU << tag_albumartist) | (1LU << tag_grouping) | (1LU << tag_title))
119 /* Uniqued tags (we can use these tags with filters and conditional clauses). */
120 #define TAGCACHE_UNIQUE_TAGS ((1LU << tag_artist) | (1LU << tag_album) | \
121 (1LU << tag_genre) | (1LU << tag_composer) | (1LU << tag_comment) | \
122 (1LU << tag_albumartist) | (1LU << tag_grouping))
124 /* String presentation of the tags defined in tagcache.h. Must be in correct order! */
125 static const char *tags_str[] = { "artist", "album", "genre", "title",
126 "filename", "composer", "comment", "albumartist", "grouping", "year",
127 "discnumber", "tracknumber", "bitrate", "length", "playcount", "rating",
128 "playtime", "lastplayed", "commitid", "mtime" };
130 /* Status information of the tagcache. */
131 static struct tagcache_stat tc_stat;
133 /* Queue commands. */
134 enum tagcache_queue {
135 Q_STOP_SCAN = 0,
136 Q_START_SCAN,
137 Q_IMPORT_CHANGELOG,
138 Q_UPDATE,
139 Q_REBUILD,
141 /* Internal tagcache command queue. */
142 CMD_UPDATE_MASTER_HEADER,
143 CMD_UPDATE_NUMERIC,
146 struct tagcache_command_entry {
147 int32_t command;
148 int32_t idx_id;
149 int32_t tag;
150 int32_t data;
153 #ifndef __PCTOOL__
154 static struct tagcache_command_entry command_queue[TAGCACHE_COMMAND_QUEUE_LENGTH];
155 static volatile int command_queue_widx = 0;
156 static volatile int command_queue_ridx = 0;
157 static struct mutex command_queue_mutex;
158 #endif
160 /* Tag database structures. */
162 /* Variable-length tag entry in tag files. */
163 struct tagfile_entry {
164 int32_t tag_length; /* Length of the data in bytes including '\0' */
165 int32_t idx_id; /* Corresponding entry location in index file of not unique tags */
166 char tag_data[0]; /* Begin of the tag data */
169 /* Fixed-size tag entry in master db index. */
170 struct index_entry {
171 int32_t tag_seek[TAG_COUNT]; /* Location of tag data or numeric tag data */
172 int32_t flag; /* Status flags */
175 /* Header is the same in every file. */
176 struct tagcache_header {
177 int32_t magic; /* Header version number */
178 int32_t datasize; /* Data size in bytes */
179 int32_t entry_count; /* Number of entries in this file */
182 struct master_header {
183 struct tagcache_header tch;
184 int32_t serial; /* Increasing counting number */
185 int32_t commitid; /* Number of commits so far */
186 int32_t dirty;
189 /* For the endianess correction */
190 static const char *tagfile_entry_ec = "ll";
192 Note: This should be (1 + TAG_COUNT) amount of l's.
194 static const char *index_entry_ec = "lllllllllllllllllllll";
196 static const char *tagcache_header_ec = "lll";
197 static const char *master_header_ec = "llllll";
199 static struct master_header current_tcmh;
201 #ifdef HAVE_TC_RAMCACHE
202 /* Header is created when loading database to ram. */
203 struct ramcache_header {
204 struct master_header h; /* Header from the master index */
205 struct index_entry *indices; /* Master index file content */
206 char *tags[TAG_COUNT]; /* Tag file content (not including filename tag) */
207 int entry_count[TAG_COUNT]; /* Number of entries in the indices. */
210 # ifdef HAVE_EEPROM_SETTINGS
211 struct statefile_header {
212 struct ramcache_header *hdr;
213 struct tagcache_stat tc_stat;
215 # endif
217 /* Pointer to allocated ramcache_header */
218 static struct ramcache_header *hdr;
219 #endif
221 /**
222 * Full tag entries stored in a temporary file waiting
223 * for commit to the cache. */
224 struct temp_file_entry {
225 long tag_offset[TAG_COUNT];
226 short tag_length[TAG_COUNT];
227 long flag;
229 long data_length;
232 struct tempbuf_id_list {
233 long id;
234 struct tempbuf_id_list *next;
237 struct tempbuf_searchidx {
238 long idx_id;
239 char *str;
240 int seek;
241 struct tempbuf_id_list idlist;
244 /* Lookup buffer for fixing messed up index while after sorting. */
245 static long commit_entry_count;
246 static long lookup_buffer_depth;
247 static struct tempbuf_searchidx **lookup;
249 /* Used when building the temporary file. */
250 static int cachefd = -1, filenametag_fd;
251 static int total_entry_count = 0;
252 static int data_size = 0;
253 static int processed_dir_count;
255 /* Thread safe locking */
256 static volatile int write_lock;
257 static volatile int read_lock;
259 static bool delete_entry(long idx_id);
261 const char* tagcache_tag_to_str(int tag)
263 return tags_str[tag];
266 /* Helper functions for the two most read/write data structure: tagfile_entry and index_entry */
267 static ssize_t ecread_tagfile_entry(int fd, struct tagfile_entry *buf)
269 return ecread(fd, buf, 1, tagfile_entry_ec, tc_stat.econ);
272 static ssize_t ecread_index_entry(int fd, struct index_entry *buf)
274 return ecread(fd, buf, 1, index_entry_ec, tc_stat.econ);
277 static ssize_t ecwrite_index_entry(int fd, struct index_entry *buf)
279 return ecwrite(fd, buf, 1, index_entry_ec, tc_stat.econ);
282 #ifdef HAVE_DIRCACHE
284 * Returns true if specified flag is still present, i.e., dircache
285 * has not been reloaded.
287 static bool is_dircache_intact(void)
289 return dircache_get_appflag(DIRCACHE_APPFLAG_TAGCACHE);
291 #endif
293 static int open_tag_fd(struct tagcache_header *hdr, int tag, bool write)
295 int fd;
296 char buf[MAX_PATH], path[MAX_PATH];
297 const char * file;
298 int rc;
300 if (TAGCACHE_IS_NUMERIC(tag) || tag < 0 || tag >= TAG_COUNT)
301 return -1;
303 snprintf(buf, sizeof buf, TAGCACHE_FILE_INDEX, tag);
304 file = get_user_file_path(buf, IS_FILE | NEED_WRITE, path, sizeof(path));
306 fd = open(file, write ? O_RDWR : O_RDONLY);
307 if (fd < 0)
309 logf("tag file open failed: tag=%d write=%d file=%s", tag, write, buf);
310 tc_stat.ready = false;
311 return fd;
314 /* Check the header. */
315 rc = ecread(fd, hdr, 1, tagcache_header_ec, tc_stat.econ);
316 if (hdr->magic != TAGCACHE_MAGIC || rc != sizeof(struct tagcache_header))
318 logf("header error");
319 tc_stat.ready = false;
320 close(fd);
321 return -2;
324 return fd;
327 static int open_master_fd(struct master_header *hdr, bool write)
329 int fd;
330 int rc;
331 char path[MAX_PATH];
333 fd = open(get_user_file_path(TAGCACHE_FILE_MASTER,
334 IS_FILE|NEED_WRITE,
335 path, sizeof(path)),
336 write ? O_RDWR : O_RDONLY);
337 if (fd < 0)
339 logf("master file open failed for R/W");
340 tc_stat.ready = false;
341 return fd;
344 tc_stat.econ = false;
346 /* Check the header. */
347 rc = read(fd, hdr, sizeof(struct master_header));
348 if (hdr->tch.magic == TAGCACHE_MAGIC && rc == sizeof(struct master_header))
350 /* Success. */
351 return fd;
354 /* Trying to read again, this time with endianess correction enabled. */
355 lseek(fd, 0, SEEK_SET);
357 rc = ecread(fd, hdr, 1, master_header_ec, true);
358 if (hdr->tch.magic != TAGCACHE_MAGIC || rc != sizeof(struct master_header))
360 logf("header error");
361 tc_stat.ready = false;
362 close(fd);
363 return -2;
366 tc_stat.econ = true;
368 return fd;
371 #ifndef __PCTOOL__
372 static bool do_timed_yield(void)
374 /* Sorting can lock up for quite a while, so yield occasionally */
375 static long wakeup_tick = 0;
376 if (TIME_AFTER(current_tick, wakeup_tick))
378 wakeup_tick = current_tick + (HZ/4);
379 yield();
380 return true;
382 return false;
384 #endif
386 #if defined(HAVE_TC_RAMCACHE) && defined(HAVE_DIRCACHE)
387 static long find_entry_ram(const char *filename,
388 const struct dircache_entry *dc)
390 static long last_pos = 0;
391 int i;
393 /* Check if we tagcache is loaded into ram. */
394 if (!tc_stat.ramcache)
395 return -1;
397 if (dc == NULL)
398 dc = dircache_get_entry_ptr(filename);
400 if (dc == NULL)
402 logf("tagcache: file not found.");
403 return -1;
406 try_again:
408 if (last_pos > 0)
409 i = last_pos;
410 else
411 i = 0;
413 for (; i < hdr->h.tch.entry_count; i++)
415 if (hdr->indices[i].tag_seek[tag_filename] == (long)dc)
417 last_pos = MAX(0, i - 3);
418 return i;
421 do_timed_yield();
424 if (last_pos > 0)
426 last_pos = 0;
427 goto try_again;
430 return -1;
432 #endif
434 static long find_entry_disk(const char *filename, bool localfd)
436 struct tagcache_header tch;
437 static long last_pos = -1;
438 long pos_history[POS_HISTORY_COUNT];
439 long pos_history_idx = 0;
440 bool found = false;
441 struct tagfile_entry tfe;
442 int fd;
443 char buf[TAG_MAXLEN+32];
444 int i;
445 int pos = -1;
447 if (!tc_stat.ready)
448 return -2;
450 fd = filenametag_fd;
451 if (fd < 0 || localfd)
453 last_pos = -1;
454 if ( (fd = open_tag_fd(&tch, tag_filename, false)) < 0)
455 return -1;
458 check_again:
460 if (last_pos > 0)
461 lseek(fd, last_pos, SEEK_SET);
462 else
463 lseek(fd, sizeof(struct tagcache_header), SEEK_SET);
465 while (true)
467 pos = lseek(fd, 0, SEEK_CUR);
468 for (i = pos_history_idx-1; i >= 0; i--)
469 pos_history[i+1] = pos_history[i];
470 pos_history[0] = pos;
472 if (ecread_tagfile_entry(fd, &tfe)
473 != sizeof(struct tagfile_entry))
475 break ;
478 if (tfe.tag_length >= (long)sizeof(buf))
480 logf("too long tag #1");
481 close(fd);
482 if (!localfd)
483 filenametag_fd = -1;
484 last_pos = -1;
485 return -2;
488 if (read(fd, buf, tfe.tag_length) != tfe.tag_length)
490 logf("read error #2");
491 close(fd);
492 if (!localfd)
493 filenametag_fd = -1;
494 last_pos = -1;
495 return -3;
498 if (!strcasecmp(filename, buf))
500 last_pos = pos_history[pos_history_idx];
501 found = true;
502 break ;
505 if (pos_history_idx < POS_HISTORY_COUNT - 1)
506 pos_history_idx++;
509 /* Not found? */
510 if (!found)
512 if (last_pos > 0)
514 last_pos = -1;
515 logf("seek again");
516 goto check_again;
519 if (fd != filenametag_fd || localfd)
520 close(fd);
521 return -4;
524 if (fd != filenametag_fd || localfd)
525 close(fd);
527 return tfe.idx_id;
530 static int find_index(const char *filename)
532 long idx_id = -1;
534 #if defined(HAVE_TC_RAMCACHE) && defined(HAVE_DIRCACHE)
535 if (tc_stat.ramcache && is_dircache_intact())
536 idx_id = find_entry_ram(filename, NULL);
537 #endif
539 if (idx_id < 0)
540 idx_id = find_entry_disk(filename, true);
542 return idx_id;
545 bool tagcache_find_index(struct tagcache_search *tcs, const char *filename)
547 int idx_id;
549 if (!tc_stat.ready)
550 return false;
552 idx_id = find_index(filename);
553 if (idx_id < 0)
554 return false;
556 if (!tagcache_search(tcs, tag_filename))
557 return false;
559 tcs->entry_count = 0;
560 tcs->idx_id = idx_id;
562 return true;
565 static bool get_index(int masterfd, int idxid,
566 struct index_entry *idx, bool use_ram)
568 bool localfd = false;
570 if (idxid < 0)
572 logf("Incorrect idxid: %d", idxid);
573 return false;
576 #ifdef HAVE_TC_RAMCACHE
577 if (tc_stat.ramcache && use_ram)
579 if (hdr->indices[idxid].flag & FLAG_DELETED)
580 return false;
582 # ifdef HAVE_DIRCACHE
583 if (!(hdr->indices[idxid].flag & FLAG_DIRCACHE)
584 || is_dircache_intact())
585 #endif
587 memcpy(idx, &hdr->indices[idxid], sizeof(struct index_entry));
588 return true;
591 #else
592 (void)use_ram;
593 #endif
595 if (masterfd < 0)
597 struct master_header tcmh;
599 localfd = true;
600 masterfd = open_master_fd(&tcmh, false);
601 if (masterfd < 0)
602 return false;
605 lseek(masterfd, idxid * sizeof(struct index_entry)
606 + sizeof(struct master_header), SEEK_SET);
607 if (ecread_index_entry(masterfd, idx)
608 != sizeof(struct index_entry))
610 logf("read error #3");
611 if (localfd)
612 close(masterfd);
614 return false;
617 if (localfd)
618 close(masterfd);
620 if (idx->flag & FLAG_DELETED)
621 return false;
623 return true;
626 #ifndef __PCTOOL__
628 static bool write_index(int masterfd, int idxid, struct index_entry *idx)
630 /* We need to exclude all memory only flags & tags when writing to disk. */
631 if (idx->flag & FLAG_DIRCACHE)
633 logf("memory only flags!");
634 return false;
637 #ifdef HAVE_TC_RAMCACHE
638 /* Only update numeric data. Writing the whole index to RAM by memcpy
639 * destroys dircache pointers!
641 if (tc_stat.ramcache)
643 int tag;
644 struct index_entry *idx_ram = &hdr->indices[idxid];
646 for (tag = 0; tag < TAG_COUNT; tag++)
648 if (TAGCACHE_IS_NUMERIC(tag))
650 idx_ram->tag_seek[tag] = idx->tag_seek[tag];
654 /* Don't touch the dircache flag or attributes. */
655 idx_ram->flag = (idx->flag & 0x0000ffff)
656 | (idx_ram->flag & (0xffff0000 | FLAG_DIRCACHE));
658 #endif
660 lseek(masterfd, idxid * sizeof(struct index_entry)
661 + sizeof(struct master_header), SEEK_SET);
662 if (ecwrite_index_entry(masterfd, idx) != sizeof(struct index_entry))
664 logf("write error #3");
665 logf("idxid: %d", idxid);
666 return false;
669 return true;
672 #endif /* !__PCTOOL__ */
674 static bool open_files(struct tagcache_search *tcs, int tag)
676 if (tcs->idxfd[tag] < 0)
678 char fn[MAX_PATH], path[MAX_PATH];
679 const char *file;
681 snprintf(fn, sizeof fn, TAGCACHE_FILE_INDEX, tag);
682 file = get_user_file_path(fn, IS_FILE | NEED_WRITE, path, sizeof(path));
683 tcs->idxfd[tag] = open(fn, O_RDONLY);
686 if (tcs->idxfd[tag] < 0)
688 logf("File not open!");
689 return false;
692 return true;
695 static bool retrieve(struct tagcache_search *tcs, struct index_entry *idx,
696 int tag, char *buf, long size)
698 struct tagfile_entry tfe;
699 long seek;
701 *buf = '\0';
703 if (TAGCACHE_IS_NUMERIC(tag))
704 return false;
706 seek = idx->tag_seek[tag];
707 if (seek < 0)
709 logf("Retrieve failed");
710 return false;
713 #ifdef HAVE_TC_RAMCACHE
714 if (tcs->ramsearch)
716 struct tagfile_entry *ep;
718 # ifdef HAVE_DIRCACHE
719 if (tag == tag_filename && (idx->flag & FLAG_DIRCACHE)
720 && is_dircache_intact())
722 dircache_copy_path((struct dircache_entry *)seek,
723 buf, size);
724 return true;
726 else
727 # endif
728 if (tag != tag_filename)
730 ep = (struct tagfile_entry *)&hdr->tags[tag][seek];
731 strlcpy(buf, ep->tag_data, size);
733 return true;
736 #endif
738 if (!open_files(tcs, tag))
739 return false;
741 lseek(tcs->idxfd[tag], seek, SEEK_SET);
742 if (ecread_tagfile_entry(tcs->idxfd[tag], &tfe)
743 != sizeof(struct tagfile_entry))
745 logf("read error #5");
746 return false;
749 if (tfe.tag_length >= size)
751 logf("too small buffer");
752 return false;
755 if (read(tcs->idxfd[tag], buf, tfe.tag_length) !=
756 tfe.tag_length)
758 logf("read error #6");
759 return false;
762 buf[tfe.tag_length] = '\0';
764 return true;
767 static long check_virtual_tags(int tag, const struct index_entry *idx)
769 long data = 0;
771 switch (tag)
773 case tag_virt_length_sec:
774 data = (idx->tag_seek[tag_length]/1000) % 60;
775 break;
777 case tag_virt_length_min:
778 data = (idx->tag_seek[tag_length]/1000) / 60;
779 break;
781 case tag_virt_playtime_sec:
782 data = (idx->tag_seek[tag_playtime]/1000) % 60;
783 break;
785 case tag_virt_playtime_min:
786 data = (idx->tag_seek[tag_playtime]/1000) / 60;
787 break;
789 case tag_virt_autoscore:
790 if (idx->tag_seek[tag_length] == 0
791 || idx->tag_seek[tag_playcount] == 0)
793 data = 0;
795 else
797 /* A straight calculus gives:
798 autoscore = 100 * playtime / length / playcout (1)
799 Now, consider the euclidian division of playtime by length:
800 playtime = alpha * length + beta
801 With:
802 0 <= beta < length
803 Now, (1) becomes:
804 autoscore = 100 * (alpha / playcout + beta / length / playcount)
805 Both terms should be small enough to avoid any overflow
807 data = 100 * (idx->tag_seek[tag_playtime] / idx->tag_seek[tag_length])
808 + (100 * (idx->tag_seek[tag_playtime] % idx->tag_seek[tag_length])) / idx->tag_seek[tag_length];
809 data /= idx->tag_seek[tag_playcount];
811 break;
813 /* How many commits before the file has been added to the DB. */
814 case tag_virt_entryage:
815 data = current_tcmh.commitid - idx->tag_seek[tag_commitid] - 1;
816 break;
818 default:
819 data = idx->tag_seek[tag];
822 return data;
825 long tagcache_get_numeric(const struct tagcache_search *tcs, int tag)
827 struct index_entry idx;
829 if (!tc_stat.ready)
830 return false;
832 if (!TAGCACHE_IS_NUMERIC(tag))
833 return -1;
835 if (!get_index(tcs->masterfd, tcs->idx_id, &idx, true))
836 return -2;
838 return check_virtual_tags(tag, &idx);
841 inline static bool str_ends_with(const char *str1, const char *str2)
843 int str_len = strlen(str1);
844 int clause_len = strlen(str2);
846 if (clause_len > str_len)
847 return false;
849 return !strcasecmp(&str1[str_len - clause_len], str2);
852 inline static bool str_oneof(const char *str, const char *list)
854 const char *sep;
855 int l, len = strlen(str);
857 while (*list)
859 sep = strchr(list, '|');
860 l = sep ? (long)sep - (long)list : (int)strlen(list);
861 if ((l==len) && !strncasecmp(str, list, len))
862 return true;
863 list += sep ? l + 1 : l;
866 return false;
869 static bool check_against_clause(long numeric, const char *str,
870 const struct tagcache_search_clause *clause)
872 if (clause->numeric)
874 switch (clause->type)
876 case clause_is:
877 return numeric == clause->numeric_data;
878 case clause_is_not:
879 return numeric != clause->numeric_data;
880 case clause_gt:
881 return numeric > clause->numeric_data;
882 case clause_gteq:
883 return numeric >= clause->numeric_data;
884 case clause_lt:
885 return numeric < clause->numeric_data;
886 case clause_lteq:
887 return numeric <= clause->numeric_data;
888 default:
889 logf("Incorrect numeric tag: %d", clause->type);
892 else
894 switch (clause->type)
896 case clause_is:
897 return !strcasecmp(clause->str, str);
898 case clause_is_not:
899 return strcasecmp(clause->str, str);
900 case clause_gt:
901 return 0>strcasecmp(clause->str, str);
902 case clause_gteq:
903 return 0>=strcasecmp(clause->str, str);
904 case clause_lt:
905 return 0<strcasecmp(clause->str, str);
906 case clause_lteq:
907 return 0<=strcasecmp(clause->str, str);
908 case clause_contains:
909 return (strcasestr(str, clause->str) != NULL);
910 case clause_not_contains:
911 return (strcasestr(str, clause->str) == NULL);
912 case clause_begins_with:
913 return (strcasestr(str, clause->str) == str);
914 case clause_not_begins_with:
915 return (strcasestr(str, clause->str) != str);
916 case clause_ends_with:
917 return str_ends_with(str, clause->str);
918 case clause_not_ends_with:
919 return !str_ends_with(str, clause->str);
920 case clause_oneof:
921 return str_oneof(str, clause->str);
923 default:
924 logf("Incorrect tag: %d", clause->type);
928 return false;
931 static bool check_clauses(struct tagcache_search *tcs,
932 struct index_entry *idx,
933 struct tagcache_search_clause **clause, int count)
935 int i;
937 #ifdef HAVE_TC_RAMCACHE
938 if (tcs->ramsearch)
940 /* Go through all conditional clauses. */
941 for (i = 0; i < count; i++)
943 struct tagfile_entry *tfe;
944 int seek;
945 char buf[256];
946 char *str = NULL;
948 seek = check_virtual_tags(clause[i]->tag, idx);
950 if (!TAGCACHE_IS_NUMERIC(clause[i]->tag))
952 if (clause[i]->tag == tag_filename)
954 retrieve(tcs, idx, tag_filename, buf, sizeof buf);
955 str = buf;
957 else
959 tfe = (struct tagfile_entry *)&hdr->tags[clause[i]->tag][seek];
960 str = tfe->tag_data;
964 if (!check_against_clause(seek, str, clause[i]))
965 return false;
968 else
969 #endif
971 /* Check for conditions. */
972 for (i = 0; i < count; i++)
974 struct tagfile_entry tfe;
975 int seek;
976 char str[256];
978 seek = check_virtual_tags(clause[i]->tag, idx);
980 memset(str, 0, sizeof str);
981 if (!TAGCACHE_IS_NUMERIC(clause[i]->tag))
983 int fd = tcs->idxfd[clause[i]->tag];
984 lseek(fd, seek, SEEK_SET);
985 ecread_tagfile_entry(fd, &tfe);
986 if (tfe.tag_length >= (int)sizeof(str))
988 logf("Too long tag read!");
989 break ;
992 read(fd, str, tfe.tag_length);
994 /* Check if entry has been deleted. */
995 if (str[0] == '\0')
996 break;
999 if (!check_against_clause(seek, str, clause[i]))
1000 return false;
1004 return true;
1007 bool tagcache_check_clauses(struct tagcache_search *tcs,
1008 struct tagcache_search_clause **clause, int count)
1010 struct index_entry idx;
1012 if (count == 0)
1013 return true;
1015 if (!get_index(tcs->masterfd, tcs->idx_id, &idx, true))
1016 return false;
1018 return check_clauses(tcs, &idx, clause, count);
1021 static bool add_uniqbuf(struct tagcache_search *tcs, unsigned long id)
1023 int i;
1025 /* If uniq buffer is not defined we must return true for search to work. */
1026 if (tcs->unique_list == NULL || (!TAGCACHE_IS_UNIQUE(tcs->type)
1027 && !TAGCACHE_IS_NUMERIC(tcs->type)))
1029 return true;
1032 for (i = 0; i < tcs->unique_list_count; i++)
1034 /* Return false if entry is found. */
1035 if (tcs->unique_list[i] == id)
1036 return false;
1039 if (tcs->unique_list_count < tcs->unique_list_capacity)
1041 tcs->unique_list[i] = id;
1042 tcs->unique_list_count++;
1045 return true;
1048 static bool build_lookup_list(struct tagcache_search *tcs)
1050 struct index_entry entry;
1051 int i, j;
1053 tcs->seek_list_count = 0;
1055 #ifdef HAVE_TC_RAMCACHE
1056 if (tcs->ramsearch
1057 # ifdef HAVE_DIRCACHE
1058 && (tcs->type != tag_filename || is_dircache_intact())
1059 # endif
1062 for (i = tcs->seek_pos; i < hdr->h.tch.entry_count; i++)
1064 struct tagcache_seeklist_entry *seeklist;
1065 struct index_entry *idx = &hdr->indices[i];
1066 if (tcs->seek_list_count == SEEK_LIST_SIZE)
1067 break ;
1069 /* Skip deleted files. */
1070 if (idx->flag & FLAG_DELETED)
1071 continue;
1073 /* Go through all filters.. */
1074 for (j = 0; j < tcs->filter_count; j++)
1076 if (idx->tag_seek[tcs->filter_tag[j]] != tcs->filter_seek[j])
1078 break ;
1082 if (j < tcs->filter_count)
1083 continue ;
1085 /* Check for conditions. */
1086 if (!check_clauses(tcs, idx, tcs->clause, tcs->clause_count))
1087 continue;
1089 /* Add to the seek list if not already in uniq buffer. */
1090 if (!add_uniqbuf(tcs, idx->tag_seek[tcs->type]))
1091 continue;
1093 /* Lets add it. */
1094 seeklist = &tcs->seeklist[tcs->seek_list_count];
1095 seeklist->seek = idx->tag_seek[tcs->type];
1096 seeklist->flag = idx->flag;
1097 seeklist->idx_id = i;
1098 tcs->seek_list_count++;
1101 tcs->seek_pos = i;
1103 return tcs->seek_list_count > 0;
1105 #endif
1107 if (tcs->masterfd < 0)
1109 struct master_header tcmh;
1110 tcs->masterfd = open_master_fd(&tcmh, false);
1113 lseek(tcs->masterfd, tcs->seek_pos * sizeof(struct index_entry) +
1114 sizeof(struct master_header), SEEK_SET);
1116 while (ecread_index_entry(tcs->masterfd, &entry)
1117 == sizeof(struct index_entry))
1119 struct tagcache_seeklist_entry *seeklist;
1121 if (tcs->seek_list_count == SEEK_LIST_SIZE)
1122 break ;
1124 i = tcs->seek_pos;
1125 tcs->seek_pos++;
1127 /* Check if entry has been deleted. */
1128 if (entry.flag & FLAG_DELETED)
1129 continue;
1131 /* Go through all filters.. */
1132 for (j = 0; j < tcs->filter_count; j++)
1134 if (entry.tag_seek[tcs->filter_tag[j]] != tcs->filter_seek[j])
1135 break ;
1138 if (j < tcs->filter_count)
1139 continue ;
1141 /* Check for conditions. */
1142 if (!check_clauses(tcs, &entry, tcs->clause, tcs->clause_count))
1143 continue;
1145 /* Add to the seek list if not already in uniq buffer. */
1146 if (!add_uniqbuf(tcs, entry.tag_seek[tcs->type]))
1147 continue;
1149 /* Lets add it. */
1150 seeklist = &tcs->seeklist[tcs->seek_list_count];
1151 seeklist->seek = entry.tag_seek[tcs->type];
1152 seeklist->flag = entry.flag;
1153 seeklist->idx_id = i;
1154 tcs->seek_list_count++;
1156 yield();
1159 return tcs->seek_list_count > 0;
1163 static void remove_files(void)
1165 int i;
1166 char buf[MAX_PATH];
1168 tc_stat.ready = false;
1169 tc_stat.ramcache = false;
1170 tc_stat.econ = false;
1171 remove(get_user_file_path(TAGCACHE_FILE_MASTER, NEED_WRITE|IS_FILE,
1172 buf, sizeof(buf)));
1173 for (i = 0; i < TAG_COUNT; i++)
1175 char buf2[MAX_PATH];
1176 if (TAGCACHE_IS_NUMERIC(i))
1177 continue;
1179 /* database_%d.tcd -> database_0.tcd */
1180 snprintf(buf, sizeof buf, TAGCACHE_FILE_INDEX, i);
1181 remove(get_user_file_path(buf, NEED_WRITE | IS_FILE, buf2, sizeof(buf2)));
1186 static bool check_all_headers(void)
1188 struct master_header myhdr;
1189 struct tagcache_header tch;
1190 int tag;
1191 int fd;
1193 if ( (fd = open_master_fd(&myhdr, false)) < 0)
1194 return false;
1196 close(fd);
1197 if (myhdr.dirty)
1199 logf("tagcache is dirty!");
1200 return false;
1203 memcpy(&current_tcmh, &myhdr, sizeof(struct master_header));
1205 for (tag = 0; tag < TAG_COUNT; tag++)
1207 if (TAGCACHE_IS_NUMERIC(tag))
1208 continue;
1210 if ( (fd = open_tag_fd(&tch, tag, false)) < 0)
1211 return false;
1213 close(fd);
1216 return true;
1219 bool tagcache_is_busy(void)
1221 return read_lock || write_lock;
1224 bool tagcache_search(struct tagcache_search *tcs, int tag)
1226 struct tagcache_header tag_hdr;
1227 struct master_header master_hdr;
1228 int i;
1230 while (read_lock)
1231 sleep(1);
1233 memset(tcs, 0, sizeof(struct tagcache_search));
1234 if (tc_stat.commit_step > 0 || !tc_stat.ready)
1235 return false;
1237 tcs->position = sizeof(struct tagcache_header);
1238 tcs->type = tag;
1239 tcs->seek_pos = 0;
1240 tcs->list_position = 0;
1241 tcs->seek_list_count = 0;
1242 tcs->filter_count = 0;
1243 tcs->masterfd = -1;
1245 for (i = 0; i < TAG_COUNT; i++)
1246 tcs->idxfd[i] = -1;
1248 #ifndef HAVE_TC_RAMCACHE
1249 tcs->ramsearch = false;
1250 #else
1251 tcs->ramsearch = tc_stat.ramcache;
1252 if (tcs->ramsearch)
1254 tcs->entry_count = hdr->entry_count[tcs->type];
1256 else
1257 #endif
1259 /* Always open as R/W so we can pass tcs to functions that modify data also
1260 * without failing. */
1261 tcs->masterfd = open_master_fd(&master_hdr, true);
1262 if (tcs->masterfd < 0)
1263 return false;
1265 if (!TAGCACHE_IS_NUMERIC(tcs->type))
1267 tcs->idxfd[tcs->type] = open_tag_fd(&tag_hdr, tcs->type, false);
1268 if (tcs->idxfd[tcs->type] < 0)
1269 return false;
1271 tcs->entry_count = tag_hdr.entry_count;
1273 else
1275 tcs->entry_count = master_hdr.tch.entry_count;
1279 tcs->valid = true;
1280 tcs->initialized = true;
1281 write_lock++;
1283 return true;
1286 void tagcache_search_set_uniqbuf(struct tagcache_search *tcs,
1287 void *buffer, long length)
1289 tcs->unique_list = (unsigned long *)buffer;
1290 tcs->unique_list_capacity = length / sizeof(*tcs->unique_list);
1291 tcs->unique_list_count = 0;
1294 bool tagcache_search_add_filter(struct tagcache_search *tcs,
1295 int tag, int seek)
1297 if (tcs->filter_count == TAGCACHE_MAX_FILTERS)
1298 return false;
1300 if (TAGCACHE_IS_NUMERIC_OR_NONUNIQUE(tag))
1301 return false;
1303 tcs->filter_tag[tcs->filter_count] = tag;
1304 tcs->filter_seek[tcs->filter_count] = seek;
1305 tcs->filter_count++;
1307 return true;
1310 bool tagcache_search_add_clause(struct tagcache_search *tcs,
1311 struct tagcache_search_clause *clause)
1313 int i;
1315 if (tcs->clause_count >= TAGCACHE_MAX_CLAUSES)
1317 logf("Too many clauses");
1318 return false;
1321 /* Check if there is already a similar filter in present (filters are
1322 * much faster than clauses).
1324 for (i = 0; i < tcs->filter_count; i++)
1326 if (tcs->filter_tag[i] == clause->tag)
1327 return true;
1330 if (!TAGCACHE_IS_NUMERIC(clause->tag) && tcs->idxfd[clause->tag] < 0)
1332 char buf[MAX_PATH], path[MAX_PATH];
1333 const char *file;
1334 snprintf(buf, sizeof buf, TAGCACHE_FILE_INDEX, clause->tag);
1335 file = get_user_file_path(buf, IS_FILE | NEED_WRITE, path, sizeof(path));
1336 tcs->idxfd[clause->tag] = open(file, O_RDONLY);
1339 tcs->clause[tcs->clause_count] = clause;
1340 tcs->clause_count++;
1342 return true;
1345 static bool get_next(struct tagcache_search *tcs)
1347 static char buf[TAG_MAXLEN+32];
1348 struct tagfile_entry entry;
1349 long flag = 0;
1351 if (!tcs->valid || !tc_stat.ready)
1352 return false;
1354 if (tcs->idxfd[tcs->type] < 0 && !TAGCACHE_IS_NUMERIC(tcs->type)
1355 #ifdef HAVE_TC_RAMCACHE
1356 && !tcs->ramsearch
1357 #endif
1359 return false;
1361 /* Relative fetch. */
1362 if (tcs->filter_count > 0 || tcs->clause_count > 0
1363 || TAGCACHE_IS_NUMERIC(tcs->type)
1364 #if defined(HAVE_TC_RAMCACHE) && defined(HAVE_DIRCACHE)
1365 /* We need to retrieve flag status for dircache. */
1366 || (tcs->ramsearch && tcs->type == tag_filename)
1367 #endif
1370 struct tagcache_seeklist_entry *seeklist;
1372 /* Check for end of list. */
1373 if (tcs->list_position == tcs->seek_list_count)
1375 tcs->list_position = 0;
1377 /* Try to fetch more. */
1378 if (!build_lookup_list(tcs))
1380 tcs->valid = false;
1381 return false;
1385 seeklist = &tcs->seeklist[tcs->list_position];
1386 flag = seeklist->flag;
1387 tcs->position = seeklist->seek;
1388 tcs->idx_id = seeklist->idx_id;
1389 tcs->list_position++;
1391 else
1393 if (tcs->entry_count == 0)
1395 tcs->valid = false;
1396 return false;
1399 tcs->entry_count--;
1402 tcs->result_seek = tcs->position;
1404 if (TAGCACHE_IS_NUMERIC(tcs->type))
1406 snprintf(buf, sizeof(buf), "%ld", tcs->position);
1407 tcs->result = buf;
1408 tcs->result_len = strlen(buf) + 1;
1409 return true;
1412 /* Direct fetch. */
1413 #ifdef HAVE_TC_RAMCACHE
1414 if (tcs->ramsearch)
1417 #if defined(HAVE_TC_RAMCACHE) && defined(HAVE_DIRCACHE)
1418 if (tcs->type == tag_filename && (flag & FLAG_DIRCACHE)
1419 && is_dircache_intact())
1421 dircache_copy_path((struct dircache_entry *)tcs->position,
1422 buf, sizeof buf);
1423 tcs->result = buf;
1424 tcs->result_len = strlen(buf) + 1;
1425 tcs->ramresult = false;
1427 return true;
1429 else
1430 #endif
1431 if (tcs->type != tag_filename)
1433 struct tagfile_entry *ep;
1435 ep = (struct tagfile_entry *)&hdr->tags[tcs->type][tcs->position];
1436 tcs->result = ep->tag_data;
1437 tcs->result_len = strlen(tcs->result) + 1;
1438 tcs->idx_id = ep->idx_id;
1439 tcs->ramresult = true;
1441 /* Increase position for the next run. This may get overwritten. */
1442 tcs->position += sizeof(struct tagfile_entry) + ep->tag_length;
1444 return true;
1447 #endif
1449 if (!open_files(tcs, tcs->type))
1451 tcs->valid = false;
1452 return false;
1455 /* Seek stream to the correct position and continue to direct fetch. */
1456 lseek(tcs->idxfd[tcs->type], tcs->position, SEEK_SET);
1458 if (ecread_tagfile_entry(tcs->idxfd[tcs->type], &entry) != sizeof(struct tagfile_entry))
1460 logf("read error #5");
1461 tcs->valid = false;
1462 return false;
1465 if (entry.tag_length > (long)sizeof(buf))
1467 tcs->valid = false;
1468 logf("too long tag #2");
1469 logf("P:%lX/%lX", tcs->position, entry.tag_length);
1470 return false;
1473 if (read(tcs->idxfd[tcs->type], buf, entry.tag_length) != entry.tag_length)
1475 tcs->valid = false;
1476 logf("read error #4");
1477 return false;
1481 Update the position for the next read (this may be overridden
1482 if filters or clauses are being used).
1484 tcs->position += sizeof(struct tagfile_entry) + entry.tag_length;
1485 tcs->result = buf;
1486 tcs->result_len = strlen(tcs->result) + 1;
1487 tcs->idx_id = entry.idx_id;
1488 tcs->ramresult = false;
1490 return true;
1493 bool tagcache_get_next(struct tagcache_search *tcs)
1495 while (get_next(tcs))
1497 if (tcs->result_len > 1)
1498 return true;
1501 return false;
1504 bool tagcache_retrieve(struct tagcache_search *tcs, int idxid,
1505 int tag, char *buf, long size)
1507 struct index_entry idx;
1509 *buf = '\0';
1510 if (!get_index(tcs->masterfd, idxid, &idx, true))
1511 return false;
1513 return retrieve(tcs, &idx, tag, buf, size);
1516 static bool update_master_header(void)
1518 struct master_header myhdr;
1519 int fd;
1521 if (!tc_stat.ready)
1522 return false;
1524 if ( (fd = open_master_fd(&myhdr, true)) < 0)
1525 return false;
1527 myhdr.serial = current_tcmh.serial;
1528 myhdr.commitid = current_tcmh.commitid;
1529 myhdr.dirty = current_tcmh.dirty;
1531 /* Write it back */
1532 lseek(fd, 0, SEEK_SET);
1533 ecwrite(fd, &myhdr, 1, master_header_ec, tc_stat.econ);
1534 close(fd);
1536 #ifdef HAVE_TC_RAMCACHE
1537 if (hdr)
1539 hdr->h.serial = current_tcmh.serial;
1540 hdr->h.commitid = current_tcmh.commitid;
1541 hdr->h.dirty = current_tcmh.dirty;
1543 #endif
1545 return true;
1548 #if 0
1550 void tagcache_modify(struct tagcache_search *tcs, int type, const char *text)
1552 struct tagentry *entry;
1554 if (tcs->type != tag_title)
1555 return ;
1557 /* We will need reserve buffer for this. */
1558 if (tcs->ramcache)
1560 struct tagfile_entry *ep;
1562 ep = (struct tagfile_entry *)&hdr->tags[tcs->type][tcs->result_seek];
1563 tcs->seek_list[tcs->seek_list_count];
1566 entry = find_entry_ram();
1569 #endif
1571 void tagcache_search_finish(struct tagcache_search *tcs)
1573 int i;
1575 if (!tcs->initialized)
1576 return;
1578 if (tcs->masterfd >= 0)
1580 close(tcs->masterfd);
1581 tcs->masterfd = -1;
1584 for (i = 0; i < TAG_COUNT; i++)
1586 if (tcs->idxfd[i] >= 0)
1588 close(tcs->idxfd[i]);
1589 tcs->idxfd[i] = -1;
1593 tcs->ramsearch = false;
1594 tcs->valid = false;
1595 tcs->initialized = 0;
1596 if (write_lock > 0)
1597 write_lock--;
1600 #if defined(HAVE_TC_RAMCACHE) && defined(HAVE_DIRCACHE)
1601 static struct tagfile_entry *get_tag(const struct index_entry *entry, int tag)
1603 return (struct tagfile_entry *)&hdr->tags[tag][entry->tag_seek[tag]];
1606 static long get_tag_numeric(const struct index_entry *entry, int tag)
1608 return check_virtual_tags(tag, entry);
1611 static char* get_tag_string(const struct index_entry *entry, int tag)
1613 char* s = get_tag(entry, tag)->tag_data;
1614 return strcmp(s, UNTAGGED) ? s : NULL;
1617 bool tagcache_fill_tags(struct mp3entry *id3, const char *filename)
1619 struct index_entry *entry;
1620 int idx_id;
1622 if (!tc_stat.ready || !tc_stat.ramcache)
1623 return false;
1625 /* Find the corresponding entry in tagcache. */
1626 idx_id = find_entry_ram(filename, NULL);
1627 if (idx_id < 0)
1628 return false;
1630 entry = &hdr->indices[idx_id];
1632 memset(id3, 0, sizeof(struct mp3entry));
1634 id3->title = get_tag_string(entry, tag_title);
1635 id3->artist = get_tag_string(entry, tag_artist);
1636 id3->album = get_tag_string(entry, tag_album);
1637 id3->genre_string = get_tag_string(entry, tag_genre);
1638 id3->composer = get_tag_string(entry, tag_composer);
1639 id3->comment = get_tag_string(entry, tag_comment);
1640 id3->albumartist = get_tag_string(entry, tag_albumartist);
1641 id3->grouping = get_tag_string(entry, tag_grouping);
1643 id3->length = get_tag_numeric(entry, tag_length);
1644 id3->playcount = get_tag_numeric(entry, tag_playcount);
1645 id3->rating = get_tag_numeric(entry, tag_rating);
1646 id3->lastplayed = get_tag_numeric(entry, tag_lastplayed);
1647 id3->score = get_tag_numeric(entry, tag_virt_autoscore) / 10;
1648 id3->year = get_tag_numeric(entry, tag_year);
1650 id3->discnum = get_tag_numeric(entry, tag_discnumber);
1651 id3->tracknum = get_tag_numeric(entry, tag_tracknumber);
1652 id3->bitrate = get_tag_numeric(entry, tag_bitrate);
1653 if (id3->bitrate == 0)
1654 id3->bitrate = 1;
1656 return true;
1658 #endif
1660 static inline void write_item(const char *item)
1662 int len = strlen(item) + 1;
1664 data_size += len;
1665 write(cachefd, item, len);
1668 static int check_if_empty(char **tag)
1670 int length;
1672 if (*tag == NULL || **tag == '\0')
1674 *tag = UNTAGGED;
1675 return sizeof(UNTAGGED); /* Tag length */
1678 length = strlen(*tag);
1679 if (length > TAG_MAXLEN)
1681 logf("over length tag: %s", *tag);
1682 length = TAG_MAXLEN;
1683 (*tag)[length] = '\0';
1686 return length + 1;
1689 #define ADD_TAG(entry,tag,data) \
1690 /* Adding tag */ \
1691 entry.tag_offset[tag] = offset; \
1692 entry.tag_length[tag] = check_if_empty(data); \
1693 offset += entry.tag_length[tag]
1694 /* GCC 3.4.6 for Coldfire can choose to inline this function. Not a good
1695 * idea, as it uses lots of stack and is called from a recursive function
1696 * (check_dir).
1698 static void __attribute__ ((noinline)) add_tagcache(char *path,
1699 unsigned long mtime
1700 #if defined(HAVE_TC_RAMCACHE) && defined(HAVE_DIRCACHE)
1701 ,const struct dircache_entry *dc
1702 #endif
1705 struct mp3entry id3;
1706 struct temp_file_entry entry;
1707 bool ret;
1708 int fd;
1709 int idx_id = -1;
1710 char tracknumfix[3];
1711 int offset = 0;
1712 int path_length = strlen(path);
1713 bool has_albumartist;
1714 bool has_grouping;
1716 #ifdef SIMULATOR
1717 /* Crude logging for the sim - to aid in debugging */
1718 int logfd = open(ROCKBOX_DIR "/database.log",
1719 O_WRONLY | O_APPEND | O_CREAT, 0666);
1720 if (logfd >= 0) {
1721 write(logfd, path, strlen(path));
1722 write(logfd, "\n", 1);
1723 close(logfd);
1725 #endif
1727 if (cachefd < 0)
1728 return ;
1730 /* Check for overlength file path. */
1731 if (path_length > TAG_MAXLEN)
1733 /* Path can't be shortened. */
1734 logf("Too long path: %s", path);
1735 return ;
1738 /* Check if the file is supported. */
1739 if (probe_file_format(path) == AFMT_UNKNOWN)
1740 return ;
1742 /* Check if the file is already cached. */
1743 #if defined(HAVE_TC_RAMCACHE) && defined(HAVE_DIRCACHE)
1744 if (tc_stat.ramcache && is_dircache_intact())
1746 idx_id = find_entry_ram(path, dc);
1748 #endif
1750 /* Be sure the entry doesn't exist. */
1751 if (filenametag_fd >= 0 && idx_id < 0)
1752 idx_id = find_entry_disk(path, false);
1754 /* Check if file has been modified. */
1755 if (idx_id >= 0)
1757 struct index_entry idx;
1759 /* TODO: Mark that the index exists (for fast reverse scan) */
1760 //found_idx[idx_id/8] |= idx_id%8;
1762 if (!get_index(-1, idx_id, &idx, true))
1764 logf("failed to retrieve index entry");
1765 return ;
1768 if ((unsigned long)idx.tag_seek[tag_mtime] == mtime)
1770 /* No changes to file. */
1771 return ;
1774 /* Metadata might have been changed. Delete the entry. */
1775 logf("Re-adding: %s", path);
1776 if (!delete_entry(idx_id))
1778 logf("delete_entry failed: %d", idx_id);
1779 return ;
1783 fd = open(path, O_RDONLY);
1784 if (fd < 0)
1786 logf("open fail: %s", path);
1787 return ;
1790 memset(&id3, 0, sizeof(struct mp3entry));
1791 memset(&entry, 0, sizeof(struct temp_file_entry));
1792 memset(&tracknumfix, 0, sizeof(tracknumfix));
1793 ret = get_metadata(&id3, fd, path);
1794 close(fd);
1796 if (!ret)
1797 return ;
1799 logf("-> %s", path);
1801 /* Generate track number if missing. */
1802 if (id3.tracknum <= 0)
1804 const char *p = strrchr(path, '.');
1806 if (p == NULL)
1807 p = &path[strlen(path)-1];
1809 while (*p != '/')
1811 if (isdigit(*p) && isdigit(*(p-1)))
1813 tracknumfix[1] = *p--;
1814 tracknumfix[0] = *p;
1815 break;
1817 p--;
1820 if (tracknumfix[0] != '\0')
1822 id3.tracknum = atoi(tracknumfix);
1823 /* Set a flag to indicate track number has been generated. */
1824 entry.flag |= FLAG_TRKNUMGEN;
1826 else
1828 /* Unable to generate track number. */
1829 id3.tracknum = -1;
1833 /* Numeric tags */
1834 entry.tag_offset[tag_year] = id3.year;
1835 entry.tag_offset[tag_discnumber] = id3.discnum;
1836 entry.tag_offset[tag_tracknumber] = id3.tracknum;
1837 entry.tag_offset[tag_length] = id3.length;
1838 entry.tag_offset[tag_bitrate] = id3.bitrate;
1839 entry.tag_offset[tag_mtime] = mtime;
1841 /* String tags. */
1842 has_albumartist = id3.albumartist != NULL
1843 && strlen(id3.albumartist) > 0;
1844 has_grouping = id3.grouping != NULL
1845 && strlen(id3.grouping) > 0;
1847 ADD_TAG(entry, tag_filename, &path);
1848 ADD_TAG(entry, tag_title, &id3.title);
1849 ADD_TAG(entry, tag_artist, &id3.artist);
1850 ADD_TAG(entry, tag_album, &id3.album);
1851 ADD_TAG(entry, tag_genre, &id3.genre_string);
1852 ADD_TAG(entry, tag_composer, &id3.composer);
1853 ADD_TAG(entry, tag_comment, &id3.comment);
1854 if (has_albumartist)
1856 ADD_TAG(entry, tag_albumartist, &id3.albumartist);
1858 else
1860 ADD_TAG(entry, tag_albumartist, &id3.artist);
1862 if (has_grouping)
1864 ADD_TAG(entry, tag_grouping, &id3.grouping);
1866 else
1868 ADD_TAG(entry, tag_grouping, &id3.title);
1870 entry.data_length = offset;
1872 /* Write the header */
1873 write(cachefd, &entry, sizeof(struct temp_file_entry));
1875 /* And tags also... Correct order is critical */
1876 write_item(path);
1877 write_item(id3.title);
1878 write_item(id3.artist);
1879 write_item(id3.album);
1880 write_item(id3.genre_string);
1881 write_item(id3.composer);
1882 write_item(id3.comment);
1883 if (has_albumartist)
1885 write_item(id3.albumartist);
1887 else
1889 write_item(id3.artist);
1891 if (has_grouping)
1893 write_item(id3.grouping);
1895 else
1897 write_item(id3.title);
1899 total_entry_count++;
1902 static bool tempbuf_insert(char *str, int id, int idx_id, bool unique)
1904 struct tempbuf_searchidx *index = (struct tempbuf_searchidx *)tempbuf;
1905 int len = strlen(str)+1;
1906 int i;
1907 unsigned crc32;
1908 unsigned *crcbuf = (unsigned *)&tempbuf[tempbuf_size-4];
1909 char buf[TAG_MAXLEN+32];
1911 for (i = 0; str[i] != '\0' && i < (int)sizeof(buf)-1; i++)
1912 buf[i] = tolower(str[i]);
1913 buf[i] = '\0';
1915 crc32 = crc_32(buf, i, 0xffffffff);
1917 if (unique)
1919 /* Check if the crc does not exist -> entry does not exist for sure. */
1920 for (i = 0; i < tempbufidx; i++)
1922 if (crcbuf[-i] != crc32)
1923 continue;
1925 if (!strcasecmp(str, index[i].str))
1927 if (id < 0 || id >= lookup_buffer_depth)
1929 logf("lookup buf overf.: %d", id);
1930 return false;
1933 lookup[id] = &index[i];
1934 return true;
1939 /* Insert to CRC buffer. */
1940 crcbuf[-tempbufidx] = crc32;
1941 tempbuf_left -= 4;
1943 /* Insert it to the buffer. */
1944 tempbuf_left -= len;
1945 if (tempbuf_left - 4 < 0 || tempbufidx >= commit_entry_count-1)
1946 return false;
1948 if (id >= lookup_buffer_depth)
1950 logf("lookup buf overf. #2: %d", id);
1951 return false;
1954 if (id >= 0)
1956 lookup[id] = &index[tempbufidx];
1957 index[tempbufidx].idlist.id = id;
1959 else
1960 index[tempbufidx].idlist.id = -1;
1962 index[tempbufidx].idlist.next = NULL;
1963 index[tempbufidx].idx_id = idx_id;
1964 index[tempbufidx].seek = -1;
1965 index[tempbufidx].str = &tempbuf[tempbuf_pos];
1966 memcpy(index[tempbufidx].str, str, len);
1967 tempbuf_pos += len;
1968 tempbufidx++;
1970 return true;
1973 static int compare(const void *p1, const void *p2)
1975 do_timed_yield();
1977 struct tempbuf_searchidx *e1 = (struct tempbuf_searchidx *)p1;
1978 struct tempbuf_searchidx *e2 = (struct tempbuf_searchidx *)p2;
1980 if (strcmp(e1->str, UNTAGGED) == 0)
1982 if (strcmp(e2->str, UNTAGGED) == 0)
1983 return 0;
1984 return -1;
1986 else if (strcmp(e2->str, UNTAGGED) == 0)
1987 return 1;
1989 return strncasecmp(e1->str, e2->str, TAG_MAXLEN);
1992 static int tempbuf_sort(int fd)
1994 struct tempbuf_searchidx *index = (struct tempbuf_searchidx *)tempbuf;
1995 struct tagfile_entry fe;
1996 int i;
1997 int length;
1999 /* Generate reverse lookup entries. */
2000 for (i = 0; i < lookup_buffer_depth; i++)
2002 struct tempbuf_id_list *idlist;
2004 if (!lookup[i])
2005 continue;
2007 if (lookup[i]->idlist.id == i)
2008 continue;
2010 idlist = &lookup[i]->idlist;
2011 while (idlist->next != NULL)
2012 idlist = idlist->next;
2014 tempbuf_left -= sizeof(struct tempbuf_id_list);
2015 if (tempbuf_left - 4 < 0)
2016 return -1;
2018 idlist->next = (struct tempbuf_id_list *)&tempbuf[tempbuf_pos];
2019 if (tempbuf_pos & 0x03)
2021 tempbuf_pos = (tempbuf_pos & ~0x03) + 0x04;
2022 tempbuf_left -= 3;
2023 idlist->next = (struct tempbuf_id_list *)&tempbuf[tempbuf_pos];
2025 tempbuf_pos += sizeof(struct tempbuf_id_list);
2027 idlist = idlist->next;
2028 idlist->id = i;
2029 idlist->next = NULL;
2031 do_timed_yield();
2034 qsort(index, tempbufidx, sizeof(struct tempbuf_searchidx), compare);
2035 memset(lookup, 0, lookup_buffer_depth * sizeof(struct tempbuf_searchidx **));
2037 for (i = 0; i < tempbufidx; i++)
2039 struct tempbuf_id_list *idlist = &index[i].idlist;
2041 /* Fix the lookup list. */
2042 while (idlist != NULL)
2044 if (idlist->id >= 0)
2045 lookup[idlist->id] = &index[i];
2046 idlist = idlist->next;
2049 index[i].seek = lseek(fd, 0, SEEK_CUR);
2050 length = strlen(index[i].str) + 1;
2051 fe.tag_length = length;
2052 fe.idx_id = index[i].idx_id;
2054 /* Check the chunk alignment. */
2055 if ((fe.tag_length + sizeof(struct tagfile_entry))
2056 % TAGFILE_ENTRY_CHUNK_LENGTH)
2058 fe.tag_length += TAGFILE_ENTRY_CHUNK_LENGTH -
2059 ((fe.tag_length + sizeof(struct tagfile_entry))
2060 % TAGFILE_ENTRY_CHUNK_LENGTH);
2063 #ifdef TAGCACHE_STRICT_ALIGN
2064 /* Make sure the entry is long aligned. */
2065 if (index[i].seek & 0x03)
2067 logf("tempbuf_sort: alignment error!");
2068 return -3;
2070 #endif
2072 if (ecwrite(fd, &fe, 1, tagfile_entry_ec, tc_stat.econ) !=
2073 sizeof(struct tagfile_entry))
2075 logf("tempbuf_sort: write error #1");
2076 return -1;
2079 if (write(fd, index[i].str, length) != length)
2081 logf("tempbuf_sort: write error #2");
2082 return -2;
2085 /* Write some padding. */
2086 if (fe.tag_length - length > 0)
2087 write(fd, "XXXXXXXX", fe.tag_length - length);
2090 return i;
2093 inline static struct tempbuf_searchidx* tempbuf_locate(int id)
2095 if (id < 0 || id >= lookup_buffer_depth)
2096 return NULL;
2098 return lookup[id];
2102 inline static int tempbuf_find_location(int id)
2104 struct tempbuf_searchidx *entry;
2106 entry = tempbuf_locate(id);
2107 if (entry == NULL)
2108 return -1;
2110 return entry->seek;
2113 static bool build_numeric_indices(struct tagcache_header *h, int tmpfd)
2115 struct master_header tcmh;
2116 struct index_entry idx;
2117 int masterfd;
2118 int masterfd_pos;
2119 struct temp_file_entry *entrybuf = (struct temp_file_entry *)tempbuf;
2120 int max_entries;
2121 int entries_processed = 0;
2122 int i, j;
2123 char buf[TAG_MAXLEN];
2125 max_entries = tempbuf_size / sizeof(struct temp_file_entry) - 1;
2127 logf("Building numeric indices...");
2128 lseek(tmpfd, sizeof(struct tagcache_header), SEEK_SET);
2130 if ( (masterfd = open_master_fd(&tcmh, true)) < 0)
2131 return false;
2133 masterfd_pos = lseek(masterfd, tcmh.tch.entry_count * sizeof(struct index_entry),
2134 SEEK_CUR);
2135 if (masterfd_pos == filesize(masterfd))
2137 logf("we can't append!");
2138 close(masterfd);
2139 return false;
2142 while (entries_processed < h->entry_count)
2144 int count = MIN(h->entry_count - entries_processed, max_entries);
2146 /* Read in as many entries as possible. */
2147 for (i = 0; i < count; i++)
2149 struct temp_file_entry *tfe = &entrybuf[i];
2150 int datastart;
2152 /* Read in numeric data. */
2153 if (read(tmpfd, tfe, sizeof(struct temp_file_entry)) !=
2154 sizeof(struct temp_file_entry))
2156 logf("read fail #1");
2157 close(masterfd);
2158 return false;
2161 datastart = lseek(tmpfd, 0, SEEK_CUR);
2164 * Read string data from the following tags:
2165 * - tag_filename
2166 * - tag_artist
2167 * - tag_album
2168 * - tag_title
2170 * A crc32 hash is calculated from the read data
2171 * and stored back to the data offset field kept in memory.
2173 #define tmpdb_read_string_tag(tag) \
2174 lseek(tmpfd, tfe->tag_offset[tag], SEEK_CUR); \
2175 if ((unsigned long)tfe->tag_length[tag] > sizeof buf) \
2177 logf("read fail: buffer overflow"); \
2178 close(masterfd); \
2179 return false; \
2182 if (read(tmpfd, buf, tfe->tag_length[tag]) != \
2183 tfe->tag_length[tag]) \
2185 logf("read fail #2"); \
2186 close(masterfd); \
2187 return false; \
2190 tfe->tag_offset[tag] = crc_32(buf, strlen(buf), 0xffffffff); \
2191 lseek(tmpfd, datastart, SEEK_SET)
2193 tmpdb_read_string_tag(tag_filename);
2194 tmpdb_read_string_tag(tag_artist);
2195 tmpdb_read_string_tag(tag_album);
2196 tmpdb_read_string_tag(tag_title);
2198 /* Seek to the end of the string data. */
2199 lseek(tmpfd, tfe->data_length, SEEK_CUR);
2202 /* Backup the master index position. */
2203 masterfd_pos = lseek(masterfd, 0, SEEK_CUR);
2204 lseek(masterfd, sizeof(struct master_header), SEEK_SET);
2206 /* Check if we can resurrect some deleted runtime statistics data. */
2207 for (i = 0; i < tcmh.tch.entry_count; i++)
2209 /* Read the index entry. */
2210 if (ecread_index_entry(masterfd, &idx)
2211 != sizeof(struct index_entry))
2213 logf("read fail #3");
2214 close(masterfd);
2215 return false;
2219 * Skip unless the entry is marked as being deleted
2220 * or the data has already been resurrected.
2222 if (!(idx.flag & FLAG_DELETED) || idx.flag & FLAG_RESURRECTED)
2223 continue;
2225 /* Now try to match the entry. */
2227 * To succesfully match a song, the following conditions
2228 * must apply:
2230 * For numeric fields: tag_length
2231 * - Full identical match is required
2233 * If tag_filename matches, no further checking necessary.
2235 * For string hashes: tag_artist, tag_album, tag_title
2236 * - Two of these must match
2238 for (j = 0; j < count; j++)
2240 struct temp_file_entry *tfe = &entrybuf[j];
2242 /* Try to match numeric fields first. */
2243 if (tfe->tag_offset[tag_length] != idx.tag_seek[tag_length])
2244 continue;
2246 /* Now it's time to do the hash matching. */
2247 if (tfe->tag_offset[tag_filename] != idx.tag_seek[tag_filename])
2249 int match_count = 0;
2251 /* No filename match, check if we can match two other tags. */
2252 #define tmpdb_match(tag) \
2253 if (tfe->tag_offset[tag] == idx.tag_seek[tag]) \
2254 match_count++
2256 tmpdb_match(tag_artist);
2257 tmpdb_match(tag_album);
2258 tmpdb_match(tag_title);
2260 if (match_count < 2)
2262 /* Still no match found, give up. */
2263 continue;
2267 /* A match found, now copy & resurrect the statistical data. */
2268 #define tmpdb_copy_tag(tag) \
2269 tfe->tag_offset[tag] = idx.tag_seek[tag]
2271 tmpdb_copy_tag(tag_playcount);
2272 tmpdb_copy_tag(tag_rating);
2273 tmpdb_copy_tag(tag_playtime);
2274 tmpdb_copy_tag(tag_lastplayed);
2275 tmpdb_copy_tag(tag_commitid);
2277 /* Avoid processing this entry again. */
2278 idx.flag |= FLAG_RESURRECTED;
2280 lseek(masterfd, -sizeof(struct index_entry), SEEK_CUR);
2281 if (ecwrite_index_entry(masterfd, &idx) != sizeof(struct index_entry))
2283 logf("masterfd writeback fail #1");
2284 close(masterfd);
2285 return false;
2288 logf("Entry resurrected");
2293 /* Restore the master index position. */
2294 lseek(masterfd, masterfd_pos, SEEK_SET);
2296 /* Commit the data to the index. */
2297 for (i = 0; i < count; i++)
2299 int loc = lseek(masterfd, 0, SEEK_CUR);
2301 if (ecread_index_entry(masterfd, &idx) != sizeof(struct index_entry))
2303 logf("read fail #3");
2304 close(masterfd);
2305 return false;
2308 for (j = 0; j < TAG_COUNT; j++)
2310 if (!TAGCACHE_IS_NUMERIC(j))
2311 continue;
2313 idx.tag_seek[j] = entrybuf[i].tag_offset[j];
2315 idx.flag = entrybuf[i].flag;
2317 if (idx.tag_seek[tag_commitid])
2319 /* Data has been resurrected. */
2320 idx.flag |= FLAG_DIRTYNUM;
2322 else if (tc_stat.ready && current_tcmh.commitid > 0)
2324 idx.tag_seek[tag_commitid] = current_tcmh.commitid;
2325 idx.flag |= FLAG_DIRTYNUM;
2328 /* Write back the updated index. */
2329 lseek(masterfd, loc, SEEK_SET);
2330 if (ecwrite_index_entry(masterfd, &idx) != sizeof(struct index_entry))
2332 logf("write fail");
2333 close(masterfd);
2334 return false;
2338 entries_processed += count;
2339 logf("%d/%ld entries processed", entries_processed, h->entry_count);
2342 close(masterfd);
2344 return true;
2348 * Return values:
2349 * > 0 success
2350 * == 0 temporary failure
2351 * < 0 fatal error
2353 static int build_index(int index_type, struct tagcache_header *h, int tmpfd)
2355 int i;
2356 struct tagcache_header tch;
2357 struct master_header tcmh;
2358 struct index_entry idxbuf[IDX_BUF_DEPTH];
2359 int idxbuf_pos;
2360 char buf[TAG_MAXLEN+32], path[MAX_PATH];
2361 const char *file;
2362 int fd = -1, masterfd;
2363 bool error = false;
2364 int init;
2365 int masterfd_pos;
2367 logf("Building index: %d", index_type);
2369 /* Check the number of entries we need to allocate ram for. */
2370 commit_entry_count = h->entry_count + 1;
2372 masterfd = open_master_fd(&tcmh, false);
2373 if (masterfd >= 0)
2375 commit_entry_count += tcmh.tch.entry_count;
2376 close(masterfd);
2378 else
2379 remove_files(); /* Just to be sure we are clean. */
2381 /* Open the index file, which contains the tag names. */
2382 fd = open_tag_fd(&tch, index_type, true);
2383 if (fd >= 0)
2385 logf("tch.datasize=%ld", tch.datasize);
2386 lookup_buffer_depth = 1 +
2387 /* First part */ commit_entry_count +
2388 /* Second part */ (tch.datasize / TAGFILE_ENTRY_CHUNK_LENGTH);
2390 else
2392 lookup_buffer_depth = 1 +
2393 /* First part */ commit_entry_count +
2394 /* Second part */ 0;
2397 logf("lookup_buffer_depth=%ld", lookup_buffer_depth);
2398 logf("commit_entry_count=%ld", commit_entry_count);
2400 /* Allocate buffer for all index entries from both old and new
2401 * tag files. */
2402 tempbufidx = 0;
2403 tempbuf_pos = commit_entry_count * sizeof(struct tempbuf_searchidx);
2405 /* Allocate lookup buffer. The first portion of commit_entry_count
2406 * contains the new tags in the temporary file and the second
2407 * part for locating entries already in the db.
2409 * New tags Old tags
2410 * +---------+---------------------------+
2411 * | index | position/ENTRY_CHUNK_SIZE | lookup buffer
2412 * +---------+---------------------------+
2414 * Old tags are inserted to a temporary buffer with position:
2415 * tempbuf_insert(position/ENTRY_CHUNK_SIZE, ...);
2416 * And new tags with index:
2417 * tempbuf_insert(idx, ...);
2419 * The buffer is sorted and written into tag file:
2420 * tempbuf_sort(...);
2421 * leaving master index locations messed up.
2423 * That is fixed using the lookup buffer for old tags:
2424 * new_seek = tempbuf_find_location(old_seek, ...);
2425 * and for new tags:
2426 * new_seek = tempbuf_find_location(idx);
2428 lookup = (struct tempbuf_searchidx **)&tempbuf[tempbuf_pos];
2429 tempbuf_pos += lookup_buffer_depth * sizeof(void **);
2430 memset(lookup, 0, lookup_buffer_depth * sizeof(void **));
2432 /* And calculate the remaining data space used mainly for storing
2433 * tag data (strings). */
2434 tempbuf_left = tempbuf_size - tempbuf_pos - 8;
2435 if (tempbuf_left - TAGFILE_ENTRY_AVG_LENGTH * commit_entry_count < 0)
2437 logf("Buffer way too small!");
2438 return 0;
2441 if (fd >= 0)
2444 * If tag file contains unique tags (sorted index), we will load
2445 * it entirely into memory so we can resort it later for use with
2446 * chunked browsing.
2448 if (TAGCACHE_IS_SORTED(index_type))
2450 logf("loading tags...");
2451 for (i = 0; i < tch.entry_count; i++)
2453 struct tagfile_entry entry;
2454 int loc = lseek(fd, 0, SEEK_CUR);
2455 bool ret;
2457 if (ecread_tagfile_entry(fd, &entry) != sizeof(struct tagfile_entry))
2459 logf("read error #7");
2460 close(fd);
2461 return -2;
2464 if (entry.tag_length >= (int)sizeof(buf))
2466 logf("too long tag #3");
2467 close(fd);
2468 return -2;
2471 if (read(fd, buf, entry.tag_length) != entry.tag_length)
2473 logf("read error #8");
2474 close(fd);
2475 return -2;
2478 /* Skip deleted entries. */
2479 if (buf[0] == '\0')
2480 continue;
2483 * Save the tag and tag id in the memory buffer. Tag id
2484 * is saved so we can later reindex the master lookup
2485 * table when the index gets resorted.
2487 ret = tempbuf_insert(buf, loc/TAGFILE_ENTRY_CHUNK_LENGTH
2488 + commit_entry_count, entry.idx_id,
2489 TAGCACHE_IS_UNIQUE(index_type));
2490 if (!ret)
2492 close(fd);
2493 return -3;
2495 do_timed_yield();
2497 logf("done");
2499 else
2500 tempbufidx = tch.entry_count;
2502 else
2505 * Creating new index file to store the tags. No need to preload
2506 * anything whether the index type is sorted or not.
2508 snprintf(buf, sizeof buf, TAGCACHE_FILE_INDEX, index_type);
2509 file = get_user_file_path(buf, IS_FILE | NEED_WRITE, path, sizeof(path));
2510 fd = open(file, O_WRONLY | O_CREAT | O_TRUNC, 0666);
2511 if (fd < 0)
2513 logf("%s open fail", buf);
2514 return -2;
2517 tch.magic = TAGCACHE_MAGIC;
2518 tch.entry_count = 0;
2519 tch.datasize = 0;
2521 if (ecwrite(fd, &tch, 1, tagcache_header_ec, tc_stat.econ)
2522 != sizeof(struct tagcache_header))
2524 logf("header write failed");
2525 close(fd);
2526 return -2;
2530 file = get_user_file_path(TAGCACHE_FILE_MASTER,
2531 IS_FILE|NEED_WRITE,
2532 buf, sizeof(buf));
2533 /* Loading the tag lookup file as "master file". */
2534 logf("Loading index file");
2535 masterfd = open(file, O_RDWR);
2537 if (masterfd < 0)
2539 logf("Creating new DB");
2540 masterfd = open(file, O_WRONLY | O_CREAT | O_TRUNC, 0666);
2542 if (masterfd < 0)
2544 logf("Failure to create index file (%s)", file);
2545 close(fd);
2546 return -2;
2549 /* Write the header (write real values later). */
2550 memset(&tcmh, 0, sizeof(struct master_header));
2551 tcmh.tch = *h;
2552 tcmh.tch.entry_count = 0;
2553 tcmh.tch.datasize = 0;
2554 tcmh.dirty = true;
2555 ecwrite(masterfd, &tcmh, 1, master_header_ec, tc_stat.econ);
2556 init = true;
2557 masterfd_pos = lseek(masterfd, 0, SEEK_CUR);
2559 else
2562 * Master file already exists so we need to process the current
2563 * file first.
2565 init = false;
2567 if (ecread(masterfd, &tcmh, 1, master_header_ec, tc_stat.econ) !=
2568 sizeof(struct master_header) || tcmh.tch.magic != TAGCACHE_MAGIC)
2570 logf("header error");
2571 close(fd);
2572 close(masterfd);
2573 return -2;
2577 * If we reach end of the master file, we need to expand it to
2578 * hold new tags. If the current index is not sorted, we can
2579 * simply append new data to end of the file.
2580 * However, if the index is sorted, we need to update all tag
2581 * pointers in the master file for the current index.
2583 masterfd_pos = lseek(masterfd, tcmh.tch.entry_count * sizeof(struct index_entry),
2584 SEEK_CUR);
2585 if (masterfd_pos == filesize(masterfd))
2587 logf("appending...");
2588 init = true;
2593 * Load new unique tags in memory to be sorted later and added
2594 * to the master lookup file.
2596 if (TAGCACHE_IS_SORTED(index_type))
2598 lseek(tmpfd, sizeof(struct tagcache_header), SEEK_SET);
2599 /* h is the header of the temporary file containing new tags. */
2600 logf("inserting new tags...");
2601 for (i = 0; i < h->entry_count; i++)
2603 struct temp_file_entry entry;
2605 if (read(tmpfd, &entry, sizeof(struct temp_file_entry)) !=
2606 sizeof(struct temp_file_entry))
2608 logf("read fail #3");
2609 error = true;
2610 goto error_exit;
2613 /* Read data. */
2614 if (entry.tag_length[index_type] >= (long)sizeof(buf))
2616 logf("too long entry!");
2617 error = true;
2618 goto error_exit;
2621 lseek(tmpfd, entry.tag_offset[index_type], SEEK_CUR);
2622 if (read(tmpfd, buf, entry.tag_length[index_type]) !=
2623 entry.tag_length[index_type])
2625 logf("read fail #4");
2626 error = true;
2627 goto error_exit;
2630 if (TAGCACHE_IS_UNIQUE(index_type))
2631 error = !tempbuf_insert(buf, i, -1, true);
2632 else
2633 error = !tempbuf_insert(buf, i, tcmh.tch.entry_count + i, false);
2635 if (error)
2637 logf("insert error");
2638 goto error_exit;
2641 /* Skip to next. */
2642 lseek(tmpfd, entry.data_length - entry.tag_offset[index_type] -
2643 entry.tag_length[index_type], SEEK_CUR);
2644 do_timed_yield();
2646 logf("done");
2648 /* Sort the buffer data and write it to the index file. */
2649 lseek(fd, sizeof(struct tagcache_header), SEEK_SET);
2651 * We need to truncate the index file now. There can be junk left
2652 * at the end of file (however, we _should_ always follow the
2653 * entry_count and don't crash with that).
2655 ftruncate(fd, lseek(fd, 0, SEEK_CUR));
2657 i = tempbuf_sort(fd);
2658 if (i < 0)
2659 goto error_exit;
2660 logf("sorted %d tags", i);
2663 * Now update all indexes in the master lookup file.
2665 logf("updating indices...");
2666 lseek(masterfd, sizeof(struct master_header), SEEK_SET);
2667 for (i = 0; i < tcmh.tch.entry_count; i += idxbuf_pos)
2669 int j;
2670 int loc = lseek(masterfd, 0, SEEK_CUR);
2672 idxbuf_pos = MIN(tcmh.tch.entry_count - i, IDX_BUF_DEPTH);
2674 if (ecread(masterfd, idxbuf, idxbuf_pos, index_entry_ec, tc_stat.econ)
2675 != (int)sizeof(struct index_entry)*idxbuf_pos)
2677 logf("read fail #5");
2678 error = true;
2679 goto error_exit ;
2681 lseek(masterfd, loc, SEEK_SET);
2683 for (j = 0; j < idxbuf_pos; j++)
2685 if (idxbuf[j].flag & FLAG_DELETED)
2687 /* We can just ignore deleted entries. */
2688 // idxbuf[j].tag_seek[index_type] = 0;
2689 continue;
2692 idxbuf[j].tag_seek[index_type] = tempbuf_find_location(
2693 idxbuf[j].tag_seek[index_type]/TAGFILE_ENTRY_CHUNK_LENGTH
2694 + commit_entry_count);
2696 if (idxbuf[j].tag_seek[index_type] < 0)
2698 logf("update error: %ld/%d/%ld",
2699 idxbuf[j].flag, i+j, tcmh.tch.entry_count);
2700 error = true;
2701 goto error_exit;
2704 do_timed_yield();
2707 /* Write back the updated index. */
2708 if (ecwrite(masterfd, idxbuf, idxbuf_pos,
2709 index_entry_ec, tc_stat.econ) !=
2710 (int)sizeof(struct index_entry)*idxbuf_pos)
2712 logf("write fail");
2713 error = true;
2714 goto error_exit;
2717 logf("done");
2721 * Walk through the temporary file containing the new tags.
2723 // build_normal_index(h, tmpfd, masterfd, idx);
2724 logf("updating new indices...");
2725 lseek(masterfd, masterfd_pos, SEEK_SET);
2726 lseek(tmpfd, sizeof(struct tagcache_header), SEEK_SET);
2727 lseek(fd, 0, SEEK_END);
2728 for (i = 0; i < h->entry_count; i += idxbuf_pos)
2730 int j;
2732 idxbuf_pos = MIN(h->entry_count - i, IDX_BUF_DEPTH);
2733 if (init)
2735 memset(idxbuf, 0, sizeof(struct index_entry)*IDX_BUF_DEPTH);
2737 else
2739 int loc = lseek(masterfd, 0, SEEK_CUR);
2741 if (ecread(masterfd, idxbuf, idxbuf_pos, index_entry_ec, tc_stat.econ)
2742 != (int)sizeof(struct index_entry)*idxbuf_pos)
2744 logf("read fail #6");
2745 error = true;
2746 break ;
2748 lseek(masterfd, loc, SEEK_SET);
2751 /* Read entry headers. */
2752 for (j = 0; j < idxbuf_pos; j++)
2754 if (!TAGCACHE_IS_SORTED(index_type))
2756 struct temp_file_entry entry;
2757 struct tagfile_entry fe;
2759 if (read(tmpfd, &entry, sizeof(struct temp_file_entry)) !=
2760 sizeof(struct temp_file_entry))
2762 logf("read fail #7");
2763 error = true;
2764 break ;
2767 /* Read data. */
2768 if (entry.tag_length[index_type] >= (int)sizeof(buf))
2770 logf("too long entry!");
2771 logf("length=%d", entry.tag_length[index_type]);
2772 logf("pos=0x%02lx", lseek(tmpfd, 0, SEEK_CUR));
2773 error = true;
2774 break ;
2777 lseek(tmpfd, entry.tag_offset[index_type], SEEK_CUR);
2778 if (read(tmpfd, buf, entry.tag_length[index_type]) !=
2779 entry.tag_length[index_type])
2781 logf("read fail #8");
2782 logf("offset=0x%02lx", entry.tag_offset[index_type]);
2783 logf("length=0x%02x", entry.tag_length[index_type]);
2784 error = true;
2785 break ;
2788 /* Write to index file. */
2789 idxbuf[j].tag_seek[index_type] = lseek(fd, 0, SEEK_CUR);
2790 fe.tag_length = entry.tag_length[index_type];
2791 fe.idx_id = tcmh.tch.entry_count + i + j;
2792 ecwrite(fd, &fe, 1, tagfile_entry_ec, tc_stat.econ);
2793 write(fd, buf, fe.tag_length);
2794 tempbufidx++;
2796 /* Skip to next. */
2797 lseek(tmpfd, entry.data_length - entry.tag_offset[index_type] -
2798 entry.tag_length[index_type], SEEK_CUR);
2800 else
2802 /* Locate the correct entry from the sorted array. */
2803 idxbuf[j].tag_seek[index_type] = tempbuf_find_location(i + j);
2804 if (idxbuf[j].tag_seek[index_type] < 0)
2806 logf("entry not found (%d)", j);
2807 error = true;
2808 break ;
2813 /* Write index. */
2814 if (ecwrite(masterfd, idxbuf, idxbuf_pos,
2815 index_entry_ec, tc_stat.econ) !=
2816 (int)sizeof(struct index_entry)*idxbuf_pos)
2818 logf("tagcache: write fail #4");
2819 error = true;
2820 break ;
2823 do_timed_yield();
2825 logf("done");
2827 /* Finally write the header. */
2828 tch.magic = TAGCACHE_MAGIC;
2829 tch.entry_count = tempbufidx;
2830 tch.datasize = lseek(fd, 0, SEEK_END) - sizeof(struct tagcache_header);
2831 lseek(fd, 0, SEEK_SET);
2832 ecwrite(fd, &tch, 1, tagcache_header_ec, tc_stat.econ);
2834 if (index_type != tag_filename)
2835 h->datasize += tch.datasize;
2836 logf("s:%d/%ld/%ld", index_type, tch.datasize, h->datasize);
2837 error_exit:
2839 close(fd);
2840 close(masterfd);
2842 if (error)
2843 return -2;
2845 return 1;
2848 static bool commit(void)
2850 struct tagcache_header tch;
2851 struct master_header tcmh;
2852 char path[MAX_PATH];
2853 const char *file;
2854 int i, len, rc;
2855 int tmpfd;
2856 int masterfd;
2857 #ifdef HAVE_DIRCACHE
2858 bool dircache_buffer_stolen = false;
2859 #endif
2860 bool local_allocation = false;
2862 logf("committing tagcache");
2864 while (write_lock)
2865 sleep(1);
2867 file = get_user_file_path(TAGCACHE_FILE_TEMP,
2868 IS_FILE|NEED_WRITE, path, sizeof(path));
2870 tmpfd = open(file, O_RDONLY);
2871 if (tmpfd < 0)
2873 logf("nothing to commit");
2874 return true;
2878 /* Load the header. */
2879 len = sizeof(struct tagcache_header);
2880 rc = read(tmpfd, &tch, len);
2882 if (tch.magic != TAGCACHE_MAGIC || rc != len)
2884 logf("incorrect tmpheader");
2885 close(tmpfd);
2886 remove(file);
2887 return false;
2890 if (tch.entry_count == 0)
2892 logf("nothing to commit");
2893 close(tmpfd);
2894 remove(file);
2895 return true;
2898 /* Fully initialize existing headers (if any) before going further. */
2899 tc_stat.ready = check_all_headers();
2901 #ifdef HAVE_EEPROM_SETTINGS
2902 remove(get_user_file_path(TAGCACHE_STATEFILE, IS_FILE | NEED_WRITE,
2903 path, sizeof(path)));
2904 #endif
2906 /* At first be sure to unload the ramcache! */
2907 #ifdef HAVE_TC_RAMCACHE
2908 tc_stat.ramcache = false;
2909 #endif
2911 read_lock++;
2913 /* Try to steal every buffer we can :) */
2914 if (tempbuf_size == 0)
2915 local_allocation = true;
2917 #ifdef HAVE_DIRCACHE
2918 if (tempbuf_size == 0)
2920 /* Try to steal the dircache buffer. */
2921 tempbuf = dircache_steal_buffer(&tempbuf_size);
2922 tempbuf_size &= ~0x03;
2924 if (tempbuf_size > 0)
2926 dircache_buffer_stolen = true;
2929 #endif
2931 #ifdef HAVE_TC_RAMCACHE
2932 if (tempbuf_size == 0 && tc_stat.ramcache_allocated > 0)
2934 tempbuf = (char *)(hdr + 1);
2935 tempbuf_size = tc_stat.ramcache_allocated - sizeof(struct ramcache_header) - 128;
2936 tempbuf_size &= ~0x03;
2938 #endif
2940 /* And finally fail if there are no buffers available. */
2941 if (tempbuf_size == 0)
2943 logf("delaying commit until next boot");
2944 tc_stat.commit_delayed = true;
2945 close(tmpfd);
2946 read_lock--;
2947 return false;
2950 logf("commit %ld entries...", tch.entry_count);
2952 /* Mark DB dirty so it will stay disabled if commit fails. */
2953 current_tcmh.dirty = true;
2954 update_master_header();
2956 /* Now create the index files. */
2957 tc_stat.commit_step = 0;
2958 tch.datasize = 0;
2959 tc_stat.commit_delayed = false;
2961 for (i = 0; i < TAG_COUNT; i++)
2963 int ret;
2965 if (TAGCACHE_IS_NUMERIC(i))
2966 continue;
2968 tc_stat.commit_step++;
2969 ret = build_index(i, &tch, tmpfd);
2970 if (ret <= 0)
2972 close(tmpfd);
2973 logf("tagcache failed init");
2974 if (ret == 0)
2975 tc_stat.commit_delayed = true;
2977 tc_stat.commit_step = 0;
2978 read_lock--;
2979 return false;
2983 if (!build_numeric_indices(&tch, tmpfd))
2985 logf("Failure to commit numeric indices");
2986 close(tmpfd);
2987 tc_stat.commit_step = 0;
2988 read_lock--;
2989 return false;
2992 close(tmpfd);
2993 remove(file);
2995 tc_stat.commit_step = 0;
2997 /* Update the master index headers. */
2998 if ( (masterfd = open_master_fd(&tcmh, true)) < 0)
3000 read_lock--;
3001 return false;
3004 tcmh.tch.entry_count += tch.entry_count;
3005 tcmh.tch.datasize = sizeof(struct master_header)
3006 + sizeof(struct index_entry) * tcmh.tch.entry_count
3007 + tch.datasize;
3008 tcmh.dirty = false;
3009 tcmh.commitid++;
3011 lseek(masterfd, 0, SEEK_SET);
3012 ecwrite(masterfd, &tcmh, 1, master_header_ec, tc_stat.econ);
3013 close(masterfd);
3015 logf("tagcache committed");
3016 tc_stat.ready = check_all_headers();
3017 tc_stat.readyvalid = true;
3019 if (local_allocation)
3021 tempbuf = NULL;
3022 tempbuf_size = 0;
3025 #ifdef HAVE_DIRCACHE
3026 /* Rebuild the dircache, if we stole the buffer. */
3027 if (dircache_buffer_stolen)
3028 dircache_build(0);
3029 #endif
3031 #ifdef HAVE_TC_RAMCACHE
3032 /* Reload tagcache. */
3033 if (tc_stat.ramcache_allocated > 0)
3034 tagcache_start_scan();
3035 #endif
3037 read_lock--;
3039 return true;
3042 static void allocate_tempbuf(void)
3044 /* Yeah, malloc would be really nice now :) */
3045 #ifdef __PCTOOL__
3046 tempbuf_size = 32*1024*1024;
3047 tempbuf = malloc(tempbuf_size);
3048 #else
3049 tempbuf = (char *)(((long)audiobuf & ~0x03) + 0x04);
3050 tempbuf_size = (long)audiobufend - (long)audiobuf - 4;
3051 audiobuf += tempbuf_size;
3052 #endif
3055 static void free_tempbuf(void)
3057 if (tempbuf_size == 0)
3058 return ;
3060 #ifdef __PCTOOL__
3061 free(tempbuf);
3062 #else
3063 audiobuf -= tempbuf_size;
3064 #endif
3065 tempbuf = NULL;
3066 tempbuf_size = 0;
3069 #ifndef __PCTOOL__
3071 static bool modify_numeric_entry(int masterfd, int idx_id, int tag, long data)
3073 struct index_entry idx;
3075 if (!tc_stat.ready)
3076 return false;
3078 if (!TAGCACHE_IS_NUMERIC(tag))
3079 return false;
3081 if (!get_index(masterfd, idx_id, &idx, false))
3082 return false;
3084 idx.tag_seek[tag] = data;
3085 idx.flag |= FLAG_DIRTYNUM;
3087 return write_index(masterfd, idx_id, &idx);
3090 #if 0
3091 bool tagcache_modify_numeric_entry(struct tagcache_search *tcs,
3092 int tag, long data)
3094 struct master_header myhdr;
3096 if (tcs->masterfd < 0)
3098 if ( (tcs->masterfd = open_master_fd(&myhdr, true)) < 0)
3099 return false;
3102 return modify_numeric_entry(tcs->masterfd, tcs->idx_id, tag, data);
3104 #endif
3106 #define COMMAND_QUEUE_IS_EMPTY (command_queue_ridx == command_queue_widx)
3108 static bool command_queue_is_full(void)
3110 int next;
3112 next = command_queue_widx + 1;
3113 if (next >= TAGCACHE_COMMAND_QUEUE_LENGTH)
3114 next = 0;
3116 return (next == command_queue_ridx);
3119 static void command_queue_sync_callback(void *data)
3121 (void)data;
3122 struct master_header myhdr;
3123 int masterfd;
3125 mutex_lock(&command_queue_mutex);
3127 if ( (masterfd = open_master_fd(&myhdr, true)) < 0)
3128 return;
3130 while (command_queue_ridx != command_queue_widx)
3132 struct tagcache_command_entry *ce = &command_queue[command_queue_ridx];
3134 switch (ce->command)
3136 case CMD_UPDATE_MASTER_HEADER:
3138 close(masterfd);
3139 update_master_header();
3141 /* Re-open the masterfd. */
3142 if ( (masterfd = open_master_fd(&myhdr, true)) < 0)
3143 return;
3145 break;
3147 case CMD_UPDATE_NUMERIC:
3149 modify_numeric_entry(masterfd, ce->idx_id, ce->tag, ce->data);
3150 break;
3154 if (++command_queue_ridx >= TAGCACHE_COMMAND_QUEUE_LENGTH)
3155 command_queue_ridx = 0;
3158 close(masterfd);
3160 tc_stat.queue_length = 0;
3161 mutex_unlock(&command_queue_mutex);
3164 static void run_command_queue(bool force)
3166 if (COMMAND_QUEUE_IS_EMPTY)
3167 return;
3169 if (force || command_queue_is_full())
3170 command_queue_sync_callback(NULL);
3171 else
3172 register_storage_idle_func(command_queue_sync_callback);
3175 static void queue_command(int cmd, long idx_id, int tag, long data)
3177 while (1)
3179 int next;
3181 mutex_lock(&command_queue_mutex);
3182 next = command_queue_widx + 1;
3183 if (next >= TAGCACHE_COMMAND_QUEUE_LENGTH)
3184 next = 0;
3186 /* Make sure queue is not full. */
3187 if (next != command_queue_ridx)
3189 struct tagcache_command_entry *ce = &command_queue[command_queue_widx];
3191 ce->command = cmd;
3192 ce->idx_id = idx_id;
3193 ce->tag = tag;
3194 ce->data = data;
3196 command_queue_widx = next;
3198 tc_stat.queue_length++;
3200 mutex_unlock(&command_queue_mutex);
3201 break;
3204 /* Queue is full, try again later... */
3205 mutex_unlock(&command_queue_mutex);
3206 sleep(1);
3210 long tagcache_increase_serial(void)
3212 long old;
3214 if (!tc_stat.ready)
3215 return -2;
3217 while (read_lock)
3218 sleep(1);
3220 old = current_tcmh.serial++;
3221 queue_command(CMD_UPDATE_MASTER_HEADER, 0, 0, 0);
3223 return old;
3226 void tagcache_update_numeric(int idx_id, int tag, long data)
3228 queue_command(CMD_UPDATE_NUMERIC, idx_id, tag, data);
3230 #endif /* !__PCTOOL__ */
3232 long tagcache_get_serial(void)
3234 return current_tcmh.serial;
3237 long tagcache_get_commitid(void)
3239 return current_tcmh.commitid;
3242 static bool write_tag(int fd, const char *tagstr, const char *datastr)
3244 char buf[512];
3245 int i;
3247 snprintf(buf, sizeof buf, "%s=\"", tagstr);
3248 for (i = strlen(buf); i < (long)sizeof(buf)-4; i++)
3250 if (*datastr == '\0')
3251 break;
3253 if (*datastr == '"' || *datastr == '\\')
3254 buf[i++] = '\\';
3256 buf[i] = *(datastr++);
3259 strcpy(&buf[i], "\" ");
3261 write(fd, buf, i + 2);
3263 return true;
3266 #ifndef __PCTOOL__
3268 static bool read_tag(char *dest, long size,
3269 const char *src, const char *tagstr)
3271 int pos;
3272 char current_tag[32];
3274 while (*src != '\0')
3276 /* Skip all whitespace */
3277 while (*src == ' ')
3278 src++;
3280 if (*src == '\0')
3281 break;
3283 pos = 0;
3284 /* Read in tag name */
3285 while (*src != '=' && *src != ' ')
3287 current_tag[pos] = *src;
3288 src++;
3289 pos++;
3291 if (*src == '\0' || pos >= (long)sizeof(current_tag))
3292 return false;
3294 current_tag[pos] = '\0';
3296 /* Read in tag data */
3298 /* Find the start. */
3299 while (*src != '"' && *src != '\0')
3300 src++;
3302 if (*src == '\0' || *(++src) == '\0')
3303 return false;
3305 /* Read the data. */
3306 for (pos = 0; pos < size; pos++)
3308 if (*src == '\0')
3309 break;
3311 if (*src == '\\')
3313 dest[pos] = *(src+1);
3314 src += 2;
3315 continue;
3318 dest[pos] = *src;
3320 if (*src == '"')
3322 src++;
3323 break;
3326 if (*src == '\0')
3327 break;
3329 src++;
3331 dest[pos] = '\0';
3333 if (!strcasecmp(tagstr, current_tag))
3334 return true;
3337 return false;
3340 static int parse_changelog_line(int line_n, const char *buf, void *parameters)
3342 struct index_entry idx;
3343 char tag_data[TAG_MAXLEN+32];
3344 int idx_id;
3345 long masterfd = (long)parameters;
3346 const int import_tags[] = { tag_playcount, tag_rating, tag_playtime, tag_lastplayed,
3347 tag_commitid };
3348 int i;
3349 (void)line_n;
3351 if (*buf == '#')
3352 return 0;
3354 logf("%d/%s", line_n, buf);
3355 if (!read_tag(tag_data, sizeof tag_data, buf, "filename"))
3357 logf("filename missing");
3358 logf("-> %s", buf);
3359 return 0;
3362 idx_id = find_index(tag_data);
3363 if (idx_id < 0)
3365 logf("entry not found");
3366 return 0;
3369 if (!get_index(masterfd, idx_id, &idx, false))
3371 logf("failed to retrieve index entry");
3372 return 0;
3375 /* Stop if tag has already been modified. */
3376 if (idx.flag & FLAG_DIRTYNUM)
3377 return 0;
3379 logf("import: %s", tag_data);
3381 idx.flag |= FLAG_DIRTYNUM;
3382 for (i = 0; i < (long)(sizeof(import_tags)/sizeof(import_tags[0])); i++)
3384 int data;
3386 if (!read_tag(tag_data, sizeof tag_data, buf,
3387 tagcache_tag_to_str(import_tags[i])))
3389 continue;
3392 data = atoi(tag_data);
3393 if (data < 0)
3394 continue;
3396 idx.tag_seek[import_tags[i]] = data;
3398 if (import_tags[i] == tag_lastplayed && data >= current_tcmh.serial)
3399 current_tcmh.serial = data + 1;
3400 else if (import_tags[i] == tag_commitid && data >= current_tcmh.commitid)
3401 current_tcmh.commitid = data + 1;
3404 return write_index(masterfd, idx_id, &idx) ? 0 : -5;
3407 bool tagcache_import_changelog(void)
3409 struct master_header myhdr;
3410 struct tagcache_header tch;
3411 int clfd;
3412 long masterfd;
3413 char buf[MAX(MAX_PATH, 2048)];
3414 const char *file;
3416 if (!tc_stat.ready)
3417 return false;
3419 while (read_lock)
3420 sleep(1);
3422 file = get_user_file_path(TAGCACHE_FILE_CHANGELOG,
3423 IS_FILE|NEED_WRITE, buf, sizeof(buf));
3424 clfd = open(file, O_RDONLY);
3425 if (clfd < 0)
3427 logf("failure to open changelog");
3428 return false;
3431 if ( (masterfd = open_master_fd(&myhdr, true)) < 0)
3433 close(clfd);
3434 return false;
3437 write_lock++;
3439 filenametag_fd = open_tag_fd(&tch, tag_filename, false);
3441 fast_readline(clfd, buf, sizeof buf, (long *)masterfd,
3442 parse_changelog_line);
3444 close(clfd);
3445 close(masterfd);
3447 if (filenametag_fd >= 0)
3449 close(filenametag_fd);
3450 filenametag_fd = -1;
3453 write_lock--;
3455 update_master_header();
3457 return true;
3460 #endif /* !__PCTOOL__ */
3462 bool tagcache_create_changelog(struct tagcache_search *tcs)
3464 struct master_header myhdr;
3465 struct index_entry idx;
3466 const char *file;
3467 char buf[MAX(TAG_MAXLEN+32, MAX_PATH)];
3468 char temp[32];
3469 int clfd;
3470 int i, j;
3472 if (!tc_stat.ready)
3473 return false;
3475 if (!tagcache_search(tcs, tag_filename))
3476 return false;
3478 /* Initialize the changelog */
3479 file = get_user_file_path(TAGCACHE_FILE_CHANGELOG, IS_FILE | NEED_WRITE,
3480 buf, sizeof(buf));
3481 clfd = open(file, O_WRONLY | O_CREAT | O_TRUNC, 0666);
3482 if (clfd < 0)
3484 logf("failure to open changelog");
3485 return false;
3488 if (tcs->masterfd < 0)
3490 if ( (tcs->masterfd = open_master_fd(&myhdr, false)) < 0)
3491 return false;
3493 else
3495 lseek(tcs->masterfd, 0, SEEK_SET);
3496 ecread(tcs->masterfd, &myhdr, 1, master_header_ec, tc_stat.econ);
3499 write(clfd, "## Changelog version 1\n", 23);
3501 for (i = 0; i < myhdr.tch.entry_count; i++)
3503 if (ecread_index_entry(tcs->masterfd, &idx) != sizeof(struct index_entry))
3505 logf("read error #9");
3506 tagcache_search_finish(tcs);
3507 close(clfd);
3508 return false;
3511 /* Skip until the entry found has been modified. */
3512 if (! (idx.flag & FLAG_DIRTYNUM) )
3513 continue;
3515 /* Skip deleted entries too. */
3516 if (idx.flag & FLAG_DELETED)
3517 continue;
3519 /* Now retrieve all tags. */
3520 for (j = 0; j < TAG_COUNT; j++)
3522 if (TAGCACHE_IS_NUMERIC(j))
3524 snprintf(temp, sizeof temp, "%d", (int)idx.tag_seek[j]);
3525 write_tag(clfd, tagcache_tag_to_str(j), temp);
3526 continue;
3529 tcs->type = j;
3530 tagcache_retrieve(tcs, i, tcs->type, buf, sizeof buf);
3531 write_tag(clfd, tagcache_tag_to_str(j), buf);
3534 write(clfd, "\n", 1);
3535 do_timed_yield();
3538 close(clfd);
3540 tagcache_search_finish(tcs);
3542 return true;
3545 static bool delete_entry(long idx_id)
3547 int fd = -1;
3548 int masterfd = -1;
3549 int tag, i;
3550 struct index_entry idx, myidx;
3551 struct master_header myhdr;
3552 char buf[TAG_MAXLEN+32];
3553 int in_use[TAG_COUNT];
3555 logf("delete_entry(): %ld", idx_id);
3557 #ifdef HAVE_TC_RAMCACHE
3558 /* At first mark the entry removed from ram cache. */
3559 if (tc_stat.ramcache)
3560 hdr->indices[idx_id].flag |= FLAG_DELETED;
3561 #endif
3563 if ( (masterfd = open_master_fd(&myhdr, true) ) < 0)
3564 return false;
3566 lseek(masterfd, idx_id * sizeof(struct index_entry), SEEK_CUR);
3567 if (ecread_index_entry(masterfd, &myidx) != sizeof(struct index_entry))
3569 logf("delete_entry(): read error");
3570 goto cleanup;
3573 if (myidx.flag & FLAG_DELETED)
3575 logf("delete_entry(): already deleted!");
3576 goto cleanup;
3579 myidx.flag |= FLAG_DELETED;
3580 lseek(masterfd, -sizeof(struct index_entry), SEEK_CUR);
3581 if (ecwrite_index_entry(masterfd, &myidx) != sizeof(struct index_entry))
3583 logf("delete_entry(): write_error #1");
3584 goto cleanup;
3587 /* Now check which tags are no longer in use (if any) */
3588 for (tag = 0; tag < TAG_COUNT; tag++)
3589 in_use[tag] = 0;
3591 lseek(masterfd, sizeof(struct master_header), SEEK_SET);
3592 for (i = 0; i < myhdr.tch.entry_count; i++)
3594 struct index_entry *idxp;
3596 #ifdef HAVE_TC_RAMCACHE
3597 /* Use RAM DB if available for greater speed */
3598 if (tc_stat.ramcache)
3599 idxp = &hdr->indices[i];
3600 else
3601 #endif
3603 if (ecread_index_entry(masterfd, &idx) != sizeof(struct index_entry))
3605 logf("delete_entry(): read error #2");
3606 goto cleanup;
3608 idxp = &idx;
3611 if (idxp->flag & FLAG_DELETED)
3612 continue;
3614 for (tag = 0; tag < TAG_COUNT; tag++)
3616 if (TAGCACHE_IS_NUMERIC(tag))
3617 continue;
3619 if (idxp->tag_seek[tag] == myidx.tag_seek[tag])
3620 in_use[tag]++;
3624 /* Now delete all tags no longer in use. */
3625 for (tag = 0; tag < TAG_COUNT; tag++)
3627 struct tagcache_header tch;
3628 int oldseek = myidx.tag_seek[tag];
3630 if (TAGCACHE_IS_NUMERIC(tag))
3631 continue;
3633 /**
3634 * Replace tag seek with a hash value of the field string data.
3635 * That way runtime statistics of moved or altered files can be
3636 * resurrected.
3638 #ifdef HAVE_TC_RAMCACHE
3639 if (tc_stat.ramcache && tag != tag_filename)
3641 struct tagfile_entry *tfe;
3642 int32_t *seek = &hdr->indices[idx_id].tag_seek[tag];
3644 tfe = (struct tagfile_entry *)&hdr->tags[tag][*seek];
3645 *seek = crc_32(tfe->tag_data, strlen(tfe->tag_data), 0xffffffff);
3646 myidx.tag_seek[tag] = *seek;
3648 else
3649 #endif
3651 struct tagfile_entry tfe;
3653 /* Open the index file, which contains the tag names. */
3654 if ((fd = open_tag_fd(&tch, tag, true)) < 0)
3655 goto cleanup;
3657 /* Skip the header block */
3658 lseek(fd, myidx.tag_seek[tag], SEEK_SET);
3659 if (ecread_tagfile_entry(fd, &tfe) != sizeof(struct tagfile_entry))
3661 logf("delete_entry(): read error #3");
3662 goto cleanup;
3665 if (read(fd, buf, tfe.tag_length) != tfe.tag_length)
3667 logf("delete_entry(): read error #4");
3668 goto cleanup;
3671 myidx.tag_seek[tag] = crc_32(buf, strlen(buf), 0xffffffff);
3674 if (in_use[tag])
3676 logf("in use: %d/%d", tag, in_use[tag]);
3677 if (fd >= 0)
3679 close(fd);
3680 fd = -1;
3682 continue;
3685 #ifdef HAVE_TC_RAMCACHE
3686 /* Delete from ram. */
3687 if (tc_stat.ramcache && tag != tag_filename)
3689 struct tagfile_entry *tagentry = (struct tagfile_entry *)&hdr->tags[tag][oldseek];
3690 tagentry->tag_data[0] = '\0';
3692 #endif
3694 /* Open the index file, which contains the tag names. */
3695 if (fd < 0)
3697 if ((fd = open_tag_fd(&tch, tag, true)) < 0)
3698 goto cleanup;
3701 /* Skip the header block */
3702 lseek(fd, oldseek + sizeof(struct tagfile_entry), SEEK_SET);
3704 /* Debug, print 10 first characters of the tag
3705 read(fd, buf, 10);
3706 buf[10]='\0';
3707 logf("TAG:%s", buf);
3708 lseek(fd, -10, SEEK_CUR);
3711 /* Write first data byte in tag as \0 */
3712 write(fd, "", 1);
3714 /* Now tag data has been removed */
3715 close(fd);
3716 fd = -1;
3719 /* Write index entry back into master index. */
3720 lseek(masterfd, sizeof(struct master_header) +
3721 (idx_id * sizeof(struct index_entry)), SEEK_SET);
3722 if (ecwrite_index_entry(masterfd, &myidx) != sizeof(struct index_entry))
3724 logf("delete_entry(): write_error #2");
3725 goto cleanup;
3728 close(masterfd);
3730 return true;
3732 cleanup:
3733 if (fd >= 0)
3734 close(fd);
3735 if (masterfd >= 0)
3736 close(masterfd);
3738 return false;
3741 #ifndef __PCTOOL__
3743 * Returns true if there is an event waiting in the queue
3744 * that requires the current operation to be aborted.
3746 static bool check_event_queue(void)
3748 struct queue_event ev;
3750 if(!queue_peek(&tagcache_queue, &ev))
3751 return false;
3753 switch (ev.id)
3755 case Q_STOP_SCAN:
3756 case SYS_POWEROFF:
3757 case SYS_USB_CONNECTED:
3758 return true;
3761 return false;
3763 #endif
3765 #ifdef HAVE_TC_RAMCACHE
3766 static bool allocate_tagcache(void)
3768 struct master_header tcmh;
3769 int fd;
3771 /* Load the header. */
3772 if ( (fd = open_master_fd(&tcmh, false)) < 0)
3774 hdr = NULL;
3775 return false;
3778 close(fd);
3780 /**
3781 * Now calculate the required cache size plus
3782 * some extra space for alignment fixes.
3784 tc_stat.ramcache_allocated = tcmh.tch.datasize + 128 + TAGCACHE_RESERVE +
3785 sizeof(struct ramcache_header) + TAG_COUNT*sizeof(void *);
3786 hdr = buffer_alloc(tc_stat.ramcache_allocated + 128);
3787 memset(hdr, 0, sizeof(struct ramcache_header));
3788 memcpy(&hdr->h, &tcmh, sizeof(struct master_header));
3789 hdr->indices = (struct index_entry *)(hdr + 1);
3790 logf("tagcache: %d bytes allocated.", tc_stat.ramcache_allocated);
3792 return true;
3795 # ifdef HAVE_EEPROM_SETTINGS
3796 static bool tagcache_dumpload(void)
3798 struct statefile_header shdr;
3799 char path[MAX_PATH];
3800 const char *file;
3801 int fd, rc;
3802 long offpos;
3803 int i;
3805 file = get_user_file_path(TAGCACHE_STATEFILE, IS_FILE | NEED_WRITE,
3806 path, sizeof(path));
3807 fd = open(file, O_RDONLY);
3808 if (fd < 0)
3810 logf("no tagcache statedump");
3811 return false;
3814 /* Check the statefile memory placement */
3815 hdr = buffer_alloc(0);
3816 rc = read(fd, &shdr, sizeof(struct statefile_header));
3817 if (rc != sizeof(struct statefile_header)
3818 /* || (long)hdr != (long)shdr.hdr */)
3820 logf("incorrect statefile");
3821 hdr = NULL;
3822 close(fd);
3823 return false;
3826 offpos = (long)hdr - (long)shdr.hdr;
3828 /* Lets allocate real memory and load it */
3829 hdr = buffer_alloc(shdr.tc_stat.ramcache_allocated);
3830 rc = read(fd, hdr, shdr.tc_stat.ramcache_allocated);
3831 close(fd);
3833 if (rc != shdr.tc_stat.ramcache_allocated)
3835 logf("read failure!");
3836 hdr = NULL;
3837 return false;
3840 memcpy(&tc_stat, &shdr.tc_stat, sizeof(struct tagcache_stat));
3842 /* Now fix the pointers */
3843 hdr->indices = (struct index_entry *)((long)hdr->indices + offpos);
3844 for (i = 0; i < TAG_COUNT; i++)
3845 hdr->tags[i] += offpos;
3847 return true;
3850 static bool tagcache_dumpsave(void)
3852 struct statefile_header shdr;
3853 char path[MAX_PATH];
3854 const char *file;
3855 int fd;
3857 if (!tc_stat.ramcache)
3858 return false;
3860 file = get_user_file_path(TAGCACHE_STATEFILE, IS_FILE | NEED_WRITE,
3861 path, sizeof(path));
3862 fd = open(file, O_WRONLY | O_CREAT | O_TRUNC, 0666);
3863 if (fd < 0)
3865 logf("failed to create a statedump");
3866 return false;
3869 /* Create the header */
3870 shdr.hdr = hdr;
3871 memcpy(&shdr.tc_stat, &tc_stat, sizeof(struct tagcache_stat));
3872 write(fd, &shdr, sizeof(struct statefile_header));
3874 /* And dump the data too */
3875 write(fd, hdr, tc_stat.ramcache_allocated);
3876 close(fd);
3878 return true;
3880 # endif
3882 static bool load_tagcache(void)
3884 struct tagcache_header *tch;
3885 long bytesleft = tc_stat.ramcache_allocated;
3886 struct index_entry *idx;
3887 int rc, fd;
3888 char *p, path[MAX_PATH];
3889 const char *file;
3890 int i, tag;
3892 # ifdef HAVE_DIRCACHE
3893 while (dircache_is_initializing())
3894 sleep(1);
3896 dircache_set_appflag(DIRCACHE_APPFLAG_TAGCACHE);
3897 # endif
3899 logf("loading tagcache to ram...");
3901 file = get_user_file_path(TAGCACHE_FILE_MASTER,
3902 IS_FILE|NEED_WRITE,
3903 path, sizeof(path));
3904 fd = open(file, O_RDONLY);
3905 if (fd < 0)
3907 logf("tagcache open failed");
3908 return false;
3911 if (ecread(fd, &hdr->h, 1, master_header_ec, tc_stat.econ)
3912 != sizeof(struct master_header)
3913 || hdr->h.tch.magic != TAGCACHE_MAGIC)
3915 logf("incorrect header");
3916 return false;
3919 idx = hdr->indices;
3921 /* Load the master index table. */
3922 for (i = 0; i < hdr->h.tch.entry_count; i++)
3924 rc = ecread_index_entry(fd, idx);
3925 if (rc != sizeof(struct index_entry))
3927 logf("read error #10");
3928 close(fd);
3929 return false;
3932 bytesleft -= sizeof(struct index_entry);
3933 if (bytesleft < 0 || ((long)idx - (long)hdr->indices) >= tc_stat.ramcache_allocated)
3935 logf("too big tagcache.");
3936 close(fd);
3937 return false;
3940 idx++;
3943 close(fd);
3945 /* Load the tags. */
3946 p = (char *)idx;
3947 for (tag = 0; tag < TAG_COUNT; tag++)
3949 struct tagfile_entry *fe;
3950 char buf[TAG_MAXLEN+32];
3952 if (TAGCACHE_IS_NUMERIC(tag))
3953 continue ;
3955 //p = ((void *)p+1);
3956 p = (char *)((long)p & ~0x03) + 0x04;
3957 hdr->tags[tag] = p;
3959 /* Check the header. */
3960 tch = (struct tagcache_header *)p;
3961 p += sizeof(struct tagcache_header);
3963 if ( (fd = open_tag_fd(tch, tag, false)) < 0)
3964 return false;
3966 for (hdr->entry_count[tag] = 0;
3967 hdr->entry_count[tag] < tch->entry_count;
3968 hdr->entry_count[tag]++)
3970 long pos;
3972 if (do_timed_yield())
3974 /* Abort if we got a critical event in queue */
3975 if (check_event_queue())
3976 return false;
3979 fe = (struct tagfile_entry *)p;
3980 pos = lseek(fd, 0, SEEK_CUR);
3981 rc = ecread_tagfile_entry(fd, fe);
3982 if (rc != sizeof(struct tagfile_entry))
3984 /* End of lookup table. */
3985 logf("read error #11");
3986 close(fd);
3987 return false;
3990 /* We have a special handling for the filename tags. */
3991 if (tag == tag_filename)
3993 # ifdef HAVE_DIRCACHE
3994 const struct dircache_entry *dc;
3995 # endif
3997 // FIXME: This is wrong!
3998 // idx = &hdr->indices[hdr->entry_count[i]];
3999 idx = &hdr->indices[fe->idx_id];
4001 if (fe->tag_length >= (long)sizeof(buf)-1)
4003 read(fd, buf, 10);
4004 buf[10] = '\0';
4005 logf("TAG:%s", buf);
4006 logf("too long filename");
4007 close(fd);
4008 return false;
4011 rc = read(fd, buf, fe->tag_length);
4012 if (rc != fe->tag_length)
4014 logf("read error #12");
4015 close(fd);
4016 return false;
4019 /* Check if the entry has already been removed */
4020 if (idx->flag & FLAG_DELETED)
4021 continue;
4023 /* This flag must not be used yet. */
4024 if (idx->flag & FLAG_DIRCACHE)
4026 logf("internal error!");
4027 close(fd);
4028 return false;
4031 if (idx->tag_seek[tag] != pos)
4033 logf("corrupt data structures!");
4034 close(fd);
4035 return false;
4038 # ifdef HAVE_DIRCACHE
4039 if (dircache_is_enabled())
4041 dc = dircache_get_entry_ptr(buf);
4042 if (dc == NULL)
4044 logf("Entry no longer valid.");
4045 logf("-> %s", buf);
4046 if (global_settings.tagcache_autoupdate)
4047 delete_entry(fe->idx_id);
4048 continue ;
4051 idx->flag |= FLAG_DIRCACHE;
4052 idx->tag_seek[tag_filename] = (long)dc;
4054 else
4055 # endif
4057 /* This will be very slow unless dircache is enabled
4058 or target is flash based, but do it anyway for
4059 consistency. */
4060 /* Check if entry has been removed. */
4061 if (global_settings.tagcache_autoupdate)
4063 if (!file_exists(buf))
4065 logf("Entry no longer valid.");
4066 logf("-> %s", buf);
4067 delete_entry(fe->idx_id);
4068 continue;
4073 continue ;
4076 bytesleft -= sizeof(struct tagfile_entry) + fe->tag_length;
4077 if (bytesleft < 0)
4079 logf("too big tagcache #2");
4080 logf("tl: %ld", fe->tag_length);
4081 logf("bl: %ld", bytesleft);
4082 close(fd);
4083 return false;
4086 p = fe->tag_data;
4087 rc = read(fd, fe->tag_data, fe->tag_length);
4088 p += rc;
4090 if (rc != fe->tag_length)
4092 logf("read error #13");
4093 logf("rc=0x%04x", rc); // 0x431
4094 logf("len=0x%04lx", fe->tag_length); // 0x4000
4095 logf("pos=0x%04lx", lseek(fd, 0, SEEK_CUR)); // 0x433
4096 logf("tag=0x%02x", tag); // 0x00
4097 close(fd);
4098 return false;
4101 close(fd);
4104 tc_stat.ramcache_used = tc_stat.ramcache_allocated - bytesleft;
4105 logf("tagcache loaded into ram!");
4107 return true;
4109 #endif /* HAVE_TC_RAMCACHE */
4111 static bool check_deleted_files(void)
4113 int fd;
4114 char buf[TAG_MAXLEN+32], path[MAX_PATH];
4115 const char *file;
4116 struct tagfile_entry tfe;
4118 logf("reverse scan...");
4119 snprintf(buf, sizeof buf, TAGCACHE_FILE_INDEX, tag_filename);
4120 file = get_user_file_path(buf, IS_FILE | NEED_WRITE, path, sizeof(path));
4121 fd = open(file, O_RDONLY);
4123 if (fd < 0)
4125 logf("%s open fail", buf);
4126 return false;
4129 lseek(fd, sizeof(struct tagcache_header), SEEK_SET);
4130 while (ecread_tagfile_entry(fd, &tfe) == sizeof(struct tagfile_entry)
4131 #ifndef __PCTOOL__
4132 && !check_event_queue()
4133 #endif
4136 if (tfe.tag_length >= (long)sizeof(buf)-1)
4138 logf("too long tag");
4139 close(fd);
4140 return false;
4143 if (read(fd, buf, tfe.tag_length) != tfe.tag_length)
4145 logf("read error #14");
4146 close(fd);
4147 return false;
4150 /* Check if the file has already deleted from the db. */
4151 if (*buf == '\0')
4152 continue;
4154 /* Now check if the file exists. */
4155 if (!file_exists(buf))
4157 logf("Entry no longer valid.");
4158 logf("-> %s / %ld", buf, tfe.tag_length);
4159 delete_entry(tfe.idx_id);
4163 close(fd);
4165 logf("done");
4167 return true;
4171 /* Note that this function must not be inlined, otherwise the whole point
4172 * of having the code in a separate function is lost.
4174 static void __attribute__ ((noinline)) check_ignore(const char *dirname,
4175 int *ignore, int *unignore)
4177 char newpath[MAX_PATH];
4179 /* check for a database.ignore file */
4180 snprintf(newpath, MAX_PATH, "%s/database.ignore", dirname);
4181 *ignore = file_exists(newpath);
4182 /* check for a database.unignore file */
4183 snprintf(newpath, MAX_PATH, "%s/database.unignore", dirname);
4184 *unignore = file_exists(newpath);
4188 static bool check_dir(const char *dirname, int add_files)
4190 DIR *dir;
4191 int len;
4192 int success = false;
4193 int ignore, unignore;
4195 dir = opendir(dirname);
4196 if (!dir)
4198 logf("tagcache: opendir(%s) failed", dirname);
4199 return false;
4202 /* check for a database.ignore and database.unignore */
4203 check_ignore(dirname, &ignore, &unignore);
4205 /* don't do anything if both ignore and unignore are there */
4206 if (ignore != unignore)
4207 add_files = unignore;
4209 /* Recursively scan the dir. */
4210 #ifdef __PCTOOL__
4211 while (1)
4212 #else
4213 while (!check_event_queue())
4214 #endif
4216 struct dirent *entry;
4218 entry = readdir(dir);
4220 if (entry == NULL)
4222 success = true;
4223 break ;
4226 struct dirinfo info = dir_get_info(dir, entry);
4228 if (!strcmp((char *)entry->d_name, ".") ||
4229 !strcmp((char *)entry->d_name, ".."))
4230 continue;
4232 yield();
4234 len = strlen(curpath);
4235 snprintf(&curpath[len], sizeof(curpath) - len, "/%s",
4236 entry->d_name);
4238 processed_dir_count++;
4239 if (info.attribute & ATTR_DIRECTORY)
4240 check_dir(curpath, add_files);
4241 else if (add_files)
4243 tc_stat.curentry = curpath;
4245 /* Add a new entry to the temporary db file. */
4246 add_tagcache(curpath, (info.wrtdate << 16) | info.wrttime
4247 #if defined(HAVE_TC_RAMCACHE) && defined(HAVE_DIRCACHE)
4248 , dir->internal_entry
4249 #endif
4252 /* Wait until current path for debug screen is read and unset. */
4253 while (tc_stat.syncscreen && tc_stat.curentry != NULL)
4254 yield();
4256 tc_stat.curentry = NULL;
4259 curpath[len] = '\0';
4262 closedir(dir);
4264 return success;
4267 void tagcache_screensync_event(void)
4269 tc_stat.curentry = NULL;
4272 void tagcache_screensync_enable(bool state)
4274 tc_stat.syncscreen = state;
4277 void tagcache_build(const char *path)
4279 struct tagcache_header header;
4280 bool ret;
4281 char buf[MAX_PATH];
4282 const char *file;
4284 curpath[0] = '\0';
4285 data_size = 0;
4286 total_entry_count = 0;
4287 processed_dir_count = 0;
4289 #ifdef HAVE_DIRCACHE
4290 while (dircache_is_initializing())
4291 sleep(1);
4292 #endif
4294 logf("updating tagcache");
4296 file = get_user_file_path(TAGCACHE_FILE_TEMP,
4297 IS_FILE|NEED_WRITE, buf, sizeof(buf));
4300 if (file_exists(file))
4302 logf("skipping, cache already waiting for commit");
4303 return ;
4306 cachefd = open(file, O_RDWR | O_CREAT | O_TRUNC, 0666);
4307 if (cachefd < 0)
4309 logf("master file open failed: %s", file);
4310 return ;
4313 filenametag_fd = open_tag_fd(&header, tag_filename, false);
4315 cpu_boost(true);
4317 logf("Scanning files...");
4318 /* Scan for new files. */
4319 memset(&header, 0, sizeof(struct tagcache_header));
4320 write(cachefd, &header, sizeof(struct tagcache_header));
4322 if (strcmp("/", path) != 0)
4323 strcpy(curpath, path);
4324 ret = check_dir(path, true);
4326 /* Write the header. */
4327 header.magic = TAGCACHE_MAGIC;
4328 header.datasize = data_size;
4329 header.entry_count = total_entry_count;
4330 lseek(cachefd, 0, SEEK_SET);
4331 write(cachefd, &header, sizeof(struct tagcache_header));
4332 close(cachefd);
4334 if (filenametag_fd >= 0)
4336 close(filenametag_fd);
4337 filenametag_fd = -1;
4340 if (!ret)
4342 logf("Aborted.");
4343 cpu_boost(false);
4344 return ;
4347 /* Commit changes to the database. */
4348 #ifdef __PCTOOL__
4349 allocate_tempbuf();
4350 #endif
4351 if (commit())
4353 remove(file);
4354 logf("tagcache built!");
4356 #ifdef __PCTOOL__
4357 free_tempbuf();
4358 #endif
4360 #ifdef HAVE_TC_RAMCACHE
4361 if (hdr)
4363 /* Import runtime statistics if we just initialized the db. */
4364 if (hdr->h.serial == 0)
4365 queue_post(&tagcache_queue, Q_IMPORT_CHANGELOG, 0);
4367 #endif
4369 cpu_boost(false);
4372 #ifdef HAVE_TC_RAMCACHE
4373 static void load_ramcache(void)
4375 if (!hdr)
4376 return ;
4378 cpu_boost(true);
4380 /* At first we should load the cache (if exists). */
4381 tc_stat.ramcache = load_tagcache();
4383 if (!tc_stat.ramcache)
4385 /* If loading failed, it must indicate some problem with the db
4386 * so disable it entirely to prevent further issues. */
4387 tc_stat.ready = false;
4388 hdr = NULL;
4391 cpu_boost(false);
4394 void tagcache_unload_ramcache(void)
4396 tc_stat.ramcache = false;
4397 /* Just to make sure there is no statefile present. */
4399 #if 0
4400 char path[MAX_PATH];
4401 remove(get_user_file_path(TAGCACHE_STATEFILE, IS_FILE | NEED_WRITE,
4402 path, sizeof(path)));
4403 #endif
4405 #endif
4407 #ifndef __PCTOOL__
4408 static void tagcache_thread(void)
4410 struct queue_event ev;
4411 bool check_done = false;
4412 char path[MAX_PATH];
4414 /* If the previous cache build/update was interrupted, commit
4415 * the changes first in foreground. */
4416 cpu_boost(true);
4417 allocate_tempbuf();
4418 commit();
4419 free_tempbuf();
4421 #ifdef HAVE_TC_RAMCACHE
4422 # ifdef HAVE_EEPROM_SETTINGS
4423 if (firmware_settings.initialized && firmware_settings.disk_clean
4424 && global_settings.tagcache_ram)
4426 check_done = tagcache_dumpload();
4429 remove(get_user_file_path(TAGCACHE_STATEFILE, IS_FILE | NEED_WRITE,
4430 path, sizeof(path)));
4431 # endif
4433 /* Allocate space for the tagcache if found on disk. */
4434 if (global_settings.tagcache_ram && !tc_stat.ramcache)
4435 allocate_tagcache();
4436 #endif
4438 cpu_boost(false);
4439 tc_stat.initialized = true;
4441 /* Don't delay bootup with the header check but do it on background. */
4442 if (!tc_stat.ready)
4444 sleep(HZ);
4445 tc_stat.ready = check_all_headers();
4446 tc_stat.readyvalid = true;
4449 while (1)
4451 run_command_queue(false);
4453 queue_wait_w_tmo(&tagcache_queue, &ev, HZ);
4455 switch (ev.id)
4457 case Q_IMPORT_CHANGELOG:
4458 tagcache_import_changelog();
4459 break;
4461 case Q_REBUILD:
4462 remove_files();
4463 remove(get_user_file_path(TAGCACHE_FILE_TEMP,
4464 IS_FILE|NEED_WRITE, path, sizeof(path)));
4465 tagcache_build("/");
4466 break;
4468 case Q_UPDATE:
4469 tagcache_build("/");
4470 #ifdef HAVE_TC_RAMCACHE
4471 load_ramcache();
4472 #endif
4473 check_deleted_files();
4474 break ;
4476 case Q_START_SCAN:
4477 check_done = false;
4478 case SYS_TIMEOUT:
4479 if (check_done || !tc_stat.ready)
4480 break ;
4482 #ifdef HAVE_TC_RAMCACHE
4483 if (!tc_stat.ramcache && global_settings.tagcache_ram)
4485 load_ramcache();
4486 if (tc_stat.ramcache && global_settings.tagcache_autoupdate)
4487 tagcache_build("/");
4489 else
4490 #endif
4491 if (global_settings.tagcache_autoupdate)
4493 tagcache_build("/");
4495 /* This will be very slow unless dircache is enabled
4496 or target is flash based, but do it anyway for
4497 consistency. */
4498 check_deleted_files();
4501 logf("tagcache check done");
4503 check_done = true;
4504 break ;
4506 case Q_STOP_SCAN:
4507 break ;
4509 case SYS_POWEROFF:
4510 break ;
4512 #if (CONFIG_PLATFORM & PLATFORM_NATIVE)
4513 case SYS_USB_CONNECTED:
4514 logf("USB: TagCache");
4515 usb_acknowledge(SYS_USB_CONNECTED_ACK);
4516 usb_wait_for_disconnect(&tagcache_queue);
4517 break ;
4518 #endif
4523 bool tagcache_prepare_shutdown(void)
4525 if (tagcache_get_commit_step() > 0)
4526 return false;
4528 tagcache_stop_scan();
4529 while (read_lock || write_lock)
4530 sleep(1);
4532 return true;
4535 void tagcache_shutdown(void)
4537 /* Flush the command queue. */
4538 run_command_queue(true);
4540 #ifdef HAVE_EEPROM_SETTINGS
4541 if (tc_stat.ramcache)
4542 tagcache_dumpsave();
4543 #endif
4546 static int get_progress(void)
4548 int total_count = -1;
4550 #ifdef HAVE_DIRCACHE
4551 if (dircache_is_enabled())
4553 total_count = dircache_get_entry_count();
4555 else
4556 #endif
4557 #ifdef HAVE_TC_RAMCACHE
4559 if (hdr && tc_stat.ramcache)
4560 total_count = hdr->h.tch.entry_count;
4562 #endif
4564 if (total_count < 0)
4565 return -1;
4567 return processed_dir_count * 100 / total_count;
4570 struct tagcache_stat* tagcache_get_stat(void)
4572 tc_stat.progress = get_progress();
4573 tc_stat.processed_entries = processed_dir_count;
4575 return &tc_stat;
4578 void tagcache_start_scan(void)
4580 queue_post(&tagcache_queue, Q_START_SCAN, 0);
4583 bool tagcache_update(void)
4585 if (!tc_stat.ready)
4586 return false;
4588 queue_post(&tagcache_queue, Q_UPDATE, 0);
4589 return false;
4592 bool tagcache_rebuild()
4594 queue_post(&tagcache_queue, Q_REBUILD, 0);
4595 return false;
4598 void tagcache_stop_scan(void)
4600 queue_post(&tagcache_queue, Q_STOP_SCAN, 0);
4603 #ifdef HAVE_TC_RAMCACHE
4604 bool tagcache_is_ramcache(void)
4606 return tc_stat.ramcache;
4608 #endif
4610 #endif /* !__PCTOOL__ */
4613 void tagcache_init(void)
4615 memset(&tc_stat, 0, sizeof(struct tagcache_stat));
4616 memset(&current_tcmh, 0, sizeof(struct master_header));
4617 filenametag_fd = -1;
4618 write_lock = read_lock = 0;
4620 #ifndef __PCTOOL__
4621 mutex_init(&command_queue_mutex);
4622 queue_init(&tagcache_queue, true);
4623 create_thread(tagcache_thread, tagcache_stack,
4624 sizeof(tagcache_stack), 0, tagcache_thread_name
4625 IF_PRIO(, PRIORITY_BACKGROUND)
4626 IF_COP(, CPU));
4627 #else
4628 tc_stat.initialized = true;
4629 allocate_tempbuf();
4630 commit();
4631 free_tempbuf();
4632 tc_stat.ready = check_all_headers();
4633 #endif
4636 #ifdef __PCTOOL__
4637 void tagcache_reverse_scan(void)
4639 logf("Checking for deleted files");
4640 check_deleted_files();
4642 #endif
4644 bool tagcache_is_initialized(void)
4646 return tc_stat.initialized;
4648 bool tagcache_is_usable(void)
4650 return tc_stat.initialized && tc_stat.ready;
4652 int tagcache_get_commit_step(void)
4654 return tc_stat.commit_step;
4656 int tagcache_get_max_commit_step(void)
4658 return (int)(SORTED_TAGS_COUNT)+1;