make sure closing the application aborts the remaining HttpGet objects. Should fix...
[Rockbox.git] / apps / tagcache.c
blobb1811061b2360c6febc2f9d59018302c241dbb2c
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2005 by Miika Pekkarinen
12 * All files in this archive are subject to the GNU General Public License.
13 * See the file COPYING in the source tree root for full license agreement.
15 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
16 * KIND, either express or implied.
18 ****************************************************************************/
21 * TagCache API
23 * ----------x---------x------------------x-----
24 * | | | External
25 * +---------------x-------+ | TagCache | Libraries
26 * | Modification routines | | Core |
27 * +-x---------x-----------+ | |
28 * | (R/W) | | | |
29 * | +------x-------------x-+ +-------------x-----+ |
30 * | | x==x Filters & clauses | |
31 * | | Search routines | +-------------------+ |
32 * | | x============================x DirCache
33 * | +-x--------------------+ | (optional)
34 * | | (R) |
35 * | | +-------------------------------+ +---------+ |
36 * | | | DB Commit (sort,unique,index) | | | |
37 * | | +-x--------------------------x--+ | Control | |
38 * | | | (R/W) | (R) | Thread | |
39 * | | | +----------------------+ | | | |
40 * | | | | TagCache DB Builder | | +---------+ |
41 * | | | +-x-------------x------+ | |
42 * | | | | (R) | (W) | |
43 * | | | | +--x--------x---------+ |
44 * | | | | | Temporary Commit DB | |
45 * | | | | +---------------------+ |
46 * +-x----x-------x--+ |
47 * | TagCache RAM DB x==\(W) +-----------------+ |
48 * +-----------------+ \===x | |
49 * | | | | (R) | Ram DB Loader x============x DirCache
50 * +-x----x---x---x---+ /==x | | (optional)
51 * | Tagcache Disk DB x==/ +-----------------+ |
52 * +------------------+ |
56 #include <stdio.h>
57 #include <stdlib.h>
58 #include <ctype.h>
59 #include "config.h"
60 #include "thread.h"
61 #include "kernel.h"
62 #include "system.h"
63 #include "logf.h"
64 #include "string.h"
65 #include "usb.h"
66 #include "metadata.h"
67 #include "id3.h"
68 #include "tagcache.h"
69 #include "buffer.h"
70 #include "crc32.h"
71 #include "misc.h"
72 #include "settings.h"
73 #include "dir.h"
74 #include "structec.h"
75 #ifndef __PCTOOL__
76 #include "atoi.h"
77 #include "splash.h"
78 #include "lang.h"
79 #include "eeprom_settings.h"
80 #endif
82 #ifdef __PCTOOL__
83 #define yield() do { } while(0)
84 #define sim_sleep(timeout) do { } while(0)
85 #define do_timed_yield() do { } while(0)
86 #endif
89 #ifndef __PCTOOL__
90 /* Tag Cache thread. */
91 static struct event_queue tagcache_queue;
92 static long tagcache_stack[(DEFAULT_STACK_SIZE + 0x4000)/sizeof(long)];
93 static const char tagcache_thread_name[] = "tagcache";
94 #endif
96 #define UNTAGGED "<Untagged>"
98 /* Previous path when scanning directory tree recursively. */
99 static char curpath[TAG_MAXLEN+32];
100 static long curpath_size = sizeof(curpath);
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 /* Tags we want to get sorted (loaded to the tempbuf). */
110 static const int sorted_tags[] = { tag_artist, tag_album, tag_genre,
111 tag_composer, tag_comment, tag_albumartist, tag_grouping, tag_title };
113 /* Uniqued tags (we can use these tags with filters and conditional clauses). */
114 static const int unique_tags[] = { tag_artist, tag_album, tag_genre,
115 tag_composer, tag_comment, tag_albumartist, tag_grouping };
117 /* Numeric tags (we can use these tags with conditional clauses). */
118 static const int numeric_tags[] = { tag_year, tag_discnumber, tag_tracknumber, tag_length,
119 tag_bitrate, tag_playcount, tag_rating, tag_playtime, tag_lastplayed, tag_commitid,
120 tag_virt_length_min, tag_virt_length_sec,
121 tag_virt_playtime_min, tag_virt_playtime_sec,
122 tag_virt_entryage, tag_virt_autoscore };
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", "discnumber", "tracknumber",
127 "bitrate", "length", "playcount", "rating", "playtime", "lastplayed", "commitid" };
129 /* Status information of the tagcache. */
130 static struct tagcache_stat tc_stat;
132 /* Queue commands. */
133 enum tagcache_queue {
134 Q_STOP_SCAN = 0,
135 Q_START_SCAN,
136 Q_IMPORT_CHANGELOG,
137 Q_UPDATE,
138 Q_REBUILD,
140 /* Internal tagcache command queue. */
141 CMD_UPDATE_MASTER_HEADER,
142 CMD_UPDATE_NUMERIC,
145 struct tagcache_command_entry {
146 long command;
147 long idx_id;
148 long tag;
149 long data;
152 static struct tagcache_command_entry command_queue[TAGCACHE_COMMAND_QUEUE_LENGTH];
153 static volatile int command_queue_widx = 0;
154 static volatile int command_queue_ridx = 0;
155 static struct mutex command_queue_mutex;
156 /* Timestamp of the last added event, so we can wait a bit before committing the
157 * whole queue at once. */
158 static long command_queue_timestamp = 0;
160 /* Tag database structures. */
162 /* Variable-length tag entry in tag files. */
163 struct tagfile_entry {
164 short tag_length; /* Length of the data in bytes including '\0' */
165 short 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 long tag_seek[TAG_COUNT]; /* Location of tag data or numeric tag data */
172 long flag; /* Status flags */
175 /* Header is the same in every file. */
176 struct tagcache_header {
177 long magic; /* Header version number */
178 long datasize; /* Data size in bytes */
179 long entry_count; /* Number of entries in this file */
182 struct master_header {
183 struct tagcache_header tch;
184 long serial; /* Increasing counting number */
185 long commitid; /* Number of commits so far */
186 long dirty;
189 /* For the endianess correction */
190 static const char *tagfile_entry_ec = "ss";
191 static const char *index_entry_ec = "llllllllllllllllllll"; /* (1 + TAG_COUNT) * l */
192 static const char *tagcache_header_ec = "lll";
193 static const char *master_header_ec = "llllll";
195 static struct master_header current_tcmh;
197 #ifdef HAVE_TC_RAMCACHE
198 /* Header is created when loading database to ram. */
199 struct ramcache_header {
200 struct master_header h; /* Header from the master index */
201 struct index_entry *indices; /* Master index file content */
202 char *tags[TAG_COUNT]; /* Tag file content (not including filename tag) */
203 int entry_count[TAG_COUNT]; /* Number of entries in the indices. */
206 # ifdef HAVE_EEPROM_SETTINGS
207 struct statefile_header {
208 struct ramcache_header *hdr;
209 struct tagcache_stat tc_stat;
211 # endif
213 /* Pointer to allocated ramcache_header */
214 static struct ramcache_header *hdr;
215 #endif
217 /**
218 * Full tag entries stored in a temporary file waiting
219 * for commit to the cache. */
220 struct temp_file_entry {
221 long tag_offset[TAG_COUNT];
222 short tag_length[TAG_COUNT];
223 long flag;
225 long data_length;
228 struct tempbuf_id_list {
229 long id;
230 struct tempbuf_id_list *next;
233 struct tempbuf_searchidx {
234 long idx_id;
235 char *str;
236 int seek;
237 struct tempbuf_id_list idlist;
240 /* Lookup buffer for fixing messed up index while after sorting. */
241 static long commit_entry_count;
242 static long lookup_buffer_depth;
243 static struct tempbuf_searchidx **lookup;
245 /* Used when building the temporary file. */
246 static int cachefd = -1, filenametag_fd;
247 static int total_entry_count = 0;
248 static int data_size = 0;
249 static int processed_dir_count;
251 /* Thread safe locking */
252 static volatile int write_lock;
253 static volatile int read_lock;
255 const char* tagcache_tag_to_str(int tag)
257 return tags_str[tag];
260 bool tagcache_is_numeric_tag(int type)
262 int i;
264 for (i = 0; i < (int)(sizeof(numeric_tags)/sizeof(numeric_tags[0])); i++)
266 if (type == numeric_tags[i])
267 return true;
270 return false;
273 bool tagcache_is_unique_tag(int type)
275 int i;
277 for (i = 0; i < (int)(sizeof(unique_tags)/sizeof(unique_tags[0])); i++)
279 if (type == unique_tags[i])
280 return true;
283 return false;
286 bool tagcache_is_sorted_tag(int type)
288 int i;
290 for (i = 0; i < (int)(sizeof(sorted_tags)/sizeof(sorted_tags[0])); i++)
292 if (type == sorted_tags[i])
293 return true;
296 return false;
299 static int open_tag_fd(struct tagcache_header *hdr, int tag, bool write)
301 int fd;
302 char buf[MAX_PATH];
303 int rc;
305 if (tagcache_is_numeric_tag(tag) || tag < 0 || tag >= TAG_COUNT)
306 return -1;
308 snprintf(buf, sizeof buf, TAGCACHE_FILE_INDEX, tag);
310 fd = open(buf, write ? O_RDWR : O_RDONLY);
311 if (fd < 0)
313 logf("tag file open failed: tag=%d write=%d file=%s", tag, write, buf);
314 tc_stat.ready = false;
315 return fd;
318 /* Check the header. */
319 rc = ecread(fd, hdr, 1, tagcache_header_ec, tc_stat.econ);
320 if (hdr->magic != TAGCACHE_MAGIC || rc != sizeof(struct tagcache_header))
322 logf("header error");
323 tc_stat.ready = false;
324 close(fd);
325 return -2;
328 return fd;
331 #ifndef __PCTOOL__
332 static bool do_timed_yield(void)
334 /* Sorting can lock up for quite a while, so yield occasionally */
335 static long wakeup_tick = 0;
336 if (current_tick >= wakeup_tick)
338 wakeup_tick = current_tick + (HZ/4);
339 yield();
340 return true;
342 return false;
344 #endif
346 #if defined(HAVE_TC_RAMCACHE) && defined(HAVE_DIRCACHE)
347 static long find_entry_ram(const char *filename,
348 const struct dirent *dc)
350 static long last_pos = 0;
351 int i;
353 /* Check if we tagcache is loaded into ram. */
354 if (!tc_stat.ramcache)
355 return -1;
357 if (dc == NULL)
358 dc = dircache_get_entry_ptr(filename);
360 if (dc == NULL)
362 logf("tagcache: file not found.");
363 return -1;
366 try_again:
368 if (last_pos > 0)
369 i = last_pos;
370 else
371 i = 0;
373 for (; i < hdr->h.tch.entry_count; i++)
375 if (hdr->indices[i].tag_seek[tag_filename] == (long)dc)
377 last_pos = MAX(0, i - 3);
378 return i;
381 do_timed_yield();
384 if (last_pos > 0)
386 last_pos = 0;
387 goto try_again;
390 return -1;
392 #endif
394 static long find_entry_disk(const char *filename)
396 struct tagcache_header tch;
397 static long last_pos = -1;
398 long pos_history[POS_HISTORY_COUNT];
399 long pos_history_idx = 0;
400 bool found = false;
401 struct tagfile_entry tfe;
402 int fd;
403 char buf[TAG_MAXLEN+32];
404 int i;
405 int pos = -1;
407 if (!tc_stat.ready)
408 return -2;
410 fd = filenametag_fd;
411 if (fd < 0)
413 last_pos = -1;
414 if ( (fd = open_tag_fd(&tch, tag_filename, false)) < 0)
415 return -1;
418 check_again:
420 if (last_pos > 0)
421 lseek(fd, last_pos, SEEK_SET);
422 else
423 lseek(fd, sizeof(struct tagcache_header), SEEK_SET);
425 while (true)
427 pos = lseek(fd, 0, SEEK_CUR);
428 for (i = pos_history_idx-1; i >= 0; i--)
429 pos_history[i+1] = pos_history[i];
430 pos_history[0] = pos;
432 if (ecread(fd, &tfe, 1, tagfile_entry_ec, tc_stat.econ)
433 != sizeof(struct tagfile_entry))
435 break ;
438 if (tfe.tag_length >= (long)sizeof(buf))
440 logf("too long tag #1");
441 close(fd);
442 last_pos = -1;
443 return -2;
446 if (read(fd, buf, tfe.tag_length) != tfe.tag_length)
448 logf("read error #2");
449 close(fd);
450 last_pos = -1;
451 return -3;
454 if (!strcasecmp(filename, buf))
456 last_pos = pos_history[pos_history_idx];
457 found = true;
458 break ;
461 if (pos_history_idx < POS_HISTORY_COUNT - 1)
462 pos_history_idx++;
465 /* Not found? */
466 if (!found)
468 if (last_pos > 0)
470 last_pos = -1;
471 logf("seek again");
472 goto check_again;
475 if (fd != filenametag_fd)
476 close(fd);
477 return -4;
480 if (fd != filenametag_fd)
481 close(fd);
483 return tfe.idx_id;
486 static int find_index(const char *filename)
488 long idx_id = -1;
490 #if defined(HAVE_TC_RAMCACHE) && defined(HAVE_DIRCACHE)
491 if (tc_stat.ramcache && dircache_is_enabled())
492 idx_id = find_entry_ram(filename, NULL);
493 #endif
495 if (idx_id < 0)
496 idx_id = find_entry_disk(filename);
498 return idx_id;
501 bool tagcache_find_index(struct tagcache_search *tcs, const char *filename)
503 int idx_id;
505 if (!tc_stat.ready)
506 return false;
508 idx_id = find_index(filename);
509 if (idx_id < 0)
510 return false;
512 if (!tagcache_search(tcs, tag_filename))
513 return false;
515 tcs->entry_count = 0;
516 tcs->idx_id = idx_id;
518 return true;
521 static bool get_index(int masterfd, int idxid,
522 struct index_entry *idx, bool use_ram)
524 if (idxid < 0)
526 logf("Incorrect idxid: %d", idxid);
527 return false;
530 #ifdef HAVE_TC_RAMCACHE
531 if (tc_stat.ramcache && use_ram)
533 if (hdr->indices[idxid].flag & FLAG_DELETED)
534 return false;
536 memcpy(idx, &hdr->indices[idxid], sizeof(struct index_entry));
537 return true;
539 #else
540 (void)use_ram;
541 #endif
543 lseek(masterfd, idxid * sizeof(struct index_entry)
544 + sizeof(struct master_header), SEEK_SET);
545 if (ecread(masterfd, idx, 1, index_entry_ec, tc_stat.econ)
546 != sizeof(struct index_entry))
548 logf("read error #3");
549 return false;
552 if (idx->flag & FLAG_DELETED)
553 return false;
555 return true;
558 static bool write_index(int masterfd, int idxid, struct index_entry *idx)
560 /* We need to exclude all memory only flags & tags when writing to disk. */
561 if (idx->flag & FLAG_DIRCACHE)
563 logf("memory only flags!");
564 return false;
567 #ifdef HAVE_TC_RAMCACHE
568 /* Only update numeric data. Writing the whole index to RAM by memcpy
569 * destroys dircache pointers!
571 if (tc_stat.ramcache)
573 int tag;
574 struct index_entry *idx_ram = &hdr->indices[idxid];
576 for (tag = 0; tag < TAG_COUNT; tag++)
578 if (tagcache_is_numeric_tag(tag))
580 idx_ram->tag_seek[tag] = idx->tag_seek[tag];
584 /* Don't touch the dircache flag. */
585 idx_ram->flag = idx->flag | (idx_ram->flag & FLAG_DIRCACHE);
587 #endif
589 lseek(masterfd, idxid * sizeof(struct index_entry)
590 + sizeof(struct master_header), SEEK_SET);
591 if (ecwrite(masterfd, idx, 1, index_entry_ec, tc_stat.econ)
592 != sizeof(struct index_entry))
594 logf("write error #3");
595 logf("idxid: %d", idxid);
596 return false;
599 return true;
602 static bool open_files(struct tagcache_search *tcs, int tag)
604 if (tcs->idxfd[tag] < 0)
606 char fn[MAX_PATH];
608 snprintf(fn, sizeof fn, TAGCACHE_FILE_INDEX, tag);
609 tcs->idxfd[tag] = open(fn, O_RDONLY);
612 if (tcs->idxfd[tag] < 0)
614 logf("File not open!");
615 return false;
618 return true;
621 static bool retrieve(struct tagcache_search *tcs, struct index_entry *idx,
622 int tag, char *buf, long size)
624 struct tagfile_entry tfe;
625 long seek;
627 *buf = '\0';
629 if (tagcache_is_numeric_tag(tag))
630 return false;
632 seek = idx->tag_seek[tag];
633 if (seek < 0)
635 logf("Retrieve failed");
636 return false;
639 #ifdef HAVE_TC_RAMCACHE
640 if (tcs->ramsearch)
642 struct tagfile_entry *ep;
644 # ifdef HAVE_DIRCACHE
645 if (tag == tag_filename && idx->flag & FLAG_DIRCACHE)
647 dircache_copy_path((struct dirent *)seek,
648 buf, size);
649 return true;
651 else
652 # endif
653 if (tag != tag_filename)
655 ep = (struct tagfile_entry *)&hdr->tags[tag][seek];
656 strncpy(buf, ep->tag_data, size-1);
658 return true;
661 #endif
663 if (!open_files(tcs, tag))
664 return false;
666 lseek(tcs->idxfd[tag], seek, SEEK_SET);
667 if (ecread(tcs->idxfd[tag], &tfe, 1, tagfile_entry_ec, tc_stat.econ)
668 != sizeof(struct tagfile_entry))
670 logf("read error #5");
671 return false;
674 if (tfe.tag_length >= size)
676 logf("too small buffer");
677 return false;
680 if (read(tcs->idxfd[tag], buf, tfe.tag_length) !=
681 tfe.tag_length)
683 logf("read error #6");
684 return false;
687 buf[tfe.tag_length] = '\0';
689 return true;
692 static long check_virtual_tags(int tag, const struct index_entry *idx)
694 long data = 0;
696 switch (tag)
698 case tag_virt_length_sec:
699 data = (idx->tag_seek[tag_length]/1000) % 60;
700 break;
702 case tag_virt_length_min:
703 data = (idx->tag_seek[tag_length]/1000) / 60;
704 break;
706 case tag_virt_playtime_sec:
707 data = (idx->tag_seek[tag_playtime]/1000) % 60;
708 break;
710 case tag_virt_playtime_min:
711 data = (idx->tag_seek[tag_playtime]/1000) / 60;
712 break;
714 case tag_virt_autoscore:
715 if (idx->tag_seek[tag_length] == 0
716 || idx->tag_seek[tag_playcount] == 0)
718 data = 0;
720 else
722 data = 100 * idx->tag_seek[tag_playtime]
723 / idx->tag_seek[tag_length]
724 / idx->tag_seek[tag_playcount];
726 break;
728 /* How many commits before the file has been added to the DB. */
729 case tag_virt_entryage:
730 data = current_tcmh.commitid - idx->tag_seek[tag_commitid] - 1;
731 break;
733 default:
734 data = idx->tag_seek[tag];
737 return data;
740 long tagcache_get_numeric(const struct tagcache_search *tcs, int tag)
742 struct index_entry idx;
744 if (!tc_stat.ready)
745 return false;
747 if (!tagcache_is_numeric_tag(tag))
748 return -1;
750 if (!get_index(tcs->masterfd, tcs->idx_id, &idx, true))
751 return -2;
753 return check_virtual_tags(tag, &idx);
756 inline static bool str_ends_with(const char *str1, const char *str2)
758 int str_len = strlen(str1);
759 int clause_len = strlen(str2);
761 if (clause_len > str_len)
762 return false;
764 return !strcasecmp(&str1[str_len - clause_len], str2);
767 inline static bool str_oneof(const char *str, const char *list)
769 const char *sep;
770 int l, len = strlen(str);
772 while (*list)
774 sep = strchr(list, '|');
775 l = sep ? (long)sep - (long)list : (int)strlen(list);
776 if ((l==len) && !strncasecmp(str, list, len))
777 return true;
778 list += sep ? l + 1 : l;
781 return false;
784 static bool check_against_clause(long numeric, const char *str,
785 const struct tagcache_search_clause *clause)
787 if (clause->numeric)
789 switch (clause->type)
791 case clause_is:
792 return numeric == clause->numeric_data;
793 case clause_is_not:
794 return numeric != clause->numeric_data;
795 case clause_gt:
796 return numeric > clause->numeric_data;
797 case clause_gteq:
798 return numeric >= clause->numeric_data;
799 case clause_lt:
800 return numeric < clause->numeric_data;
801 case clause_lteq:
802 return numeric <= clause->numeric_data;
803 default:
804 logf("Incorrect numeric tag: %d", clause->type);
807 else
809 switch (clause->type)
811 case clause_is:
812 return !strcasecmp(clause->str, str);
813 case clause_is_not:
814 return strcasecmp(clause->str, str);
815 case clause_gt:
816 return 0>strcasecmp(clause->str, str);
817 case clause_gteq:
818 return 0>=strcasecmp(clause->str, str);
819 case clause_lt:
820 return 0<strcasecmp(clause->str, str);
821 case clause_lteq:
822 return 0<=strcasecmp(clause->str, str);
823 case clause_contains:
824 return (strcasestr(str, clause->str) != NULL);
825 case clause_not_contains:
826 return (strcasestr(str, clause->str) == NULL);
827 case clause_begins_with:
828 return (strcasestr(str, clause->str) == str);
829 case clause_not_begins_with:
830 return (strcasestr(str, clause->str) != str);
831 case clause_ends_with:
832 return str_ends_with(str, clause->str);
833 case clause_not_ends_with:
834 return !str_ends_with(str, clause->str);
835 case clause_oneof:
836 return str_oneof(str, clause->str);
838 default:
839 logf("Incorrect tag: %d", clause->type);
843 return false;
846 static bool check_clauses(struct tagcache_search *tcs,
847 struct index_entry *idx,
848 struct tagcache_search_clause **clause, int count)
850 int i;
852 #ifdef HAVE_TC_RAMCACHE
853 if (tcs->ramsearch)
855 /* Go through all conditional clauses. */
856 for (i = 0; i < count; i++)
858 struct tagfile_entry *tfe;
859 int seek;
860 char buf[256];
861 char *str = NULL;
863 seek = check_virtual_tags(clause[i]->tag, idx);
865 if (!tagcache_is_numeric_tag(clause[i]->tag))
867 if (clause[i]->tag == tag_filename)
869 retrieve(tcs, idx, tag_filename, buf, sizeof buf);
870 str = buf;
872 else
874 tfe = (struct tagfile_entry *)&hdr->tags[clause[i]->tag][seek];
875 str = tfe->tag_data;
879 if (!check_against_clause(seek, str, clause[i]))
880 return false;
883 else
884 #endif
886 /* Check for conditions. */
887 for (i = 0; i < count; i++)
889 struct tagfile_entry tfe;
890 int seek;
891 char str[256];
893 seek = check_virtual_tags(clause[i]->tag, idx);
895 memset(str, 0, sizeof str);
896 if (!tagcache_is_numeric_tag(clause[i]->tag))
898 int fd = tcs->idxfd[clause[i]->tag];
899 lseek(fd, seek, SEEK_SET);
900 ecread(fd, &tfe, 1, tagfile_entry_ec, tc_stat.econ);
901 if (tfe.tag_length >= (int)sizeof(str))
903 logf("Too long tag read!");
904 break ;
907 read(fd, str, tfe.tag_length);
909 /* Check if entry has been deleted. */
910 if (str[0] == '\0')
911 break;
914 if (!check_against_clause(seek, str, clause[i]))
915 return false;
919 return true;
922 bool tagcache_check_clauses(struct tagcache_search *tcs,
923 struct tagcache_search_clause **clause, int count)
925 struct index_entry idx;
927 if (count == 0)
928 return true;
930 if (!get_index(tcs->masterfd, tcs->idx_id, &idx, true))
931 return false;
933 return check_clauses(tcs, &idx, clause, count);
936 static bool add_uniqbuf(struct tagcache_search *tcs, unsigned long id)
938 int i;
940 /* If uniq buffer is not defined we must return true for search to work. */
941 if (tcs->unique_list == NULL
942 || (!tagcache_is_unique_tag(tcs->type)
943 && !tagcache_is_numeric_tag(tcs->type)))
945 return true;
948 for (i = 0; i < tcs->unique_list_count; i++)
950 /* Return false if entry is found. */
951 if (tcs->unique_list[i] == id)
952 return false;
955 if (tcs->unique_list_count < tcs->unique_list_capacity)
957 tcs->unique_list[i] = id;
958 tcs->unique_list_count++;
961 return true;
964 static bool build_lookup_list(struct tagcache_search *tcs)
966 struct index_entry entry;
967 int i;
969 tcs->seek_list_count = 0;
971 #ifdef HAVE_TC_RAMCACHE
972 if (tcs->ramsearch)
974 int j;
976 for (i = tcs->seek_pos; i < hdr->h.tch.entry_count; i++)
978 struct index_entry *idx = &hdr->indices[i];
979 if (tcs->seek_list_count == SEEK_LIST_SIZE)
980 break ;
982 /* Skip deleted files. */
983 if (idx->flag & FLAG_DELETED)
984 continue;
986 /* Go through all filters.. */
987 for (j = 0; j < tcs->filter_count; j++)
989 if (idx->tag_seek[tcs->filter_tag[j]] != tcs->filter_seek[j])
991 break ;
995 if (j < tcs->filter_count)
996 continue ;
998 /* Check for conditions. */
999 if (!check_clauses(tcs, idx, tcs->clause, tcs->clause_count))
1000 continue;
1002 /* Add to the seek list if not already in uniq buffer. */
1003 if (!add_uniqbuf(tcs, idx->tag_seek[tcs->type]))
1004 continue;
1006 /* Lets add it. */
1007 tcs->seek_list[tcs->seek_list_count] = idx->tag_seek[tcs->type];
1008 tcs->seek_flags[tcs->seek_list_count] = idx->flag;
1009 tcs->seek_list_count++;
1012 tcs->seek_pos = i;
1014 return tcs->seek_list_count > 0;
1016 #endif
1018 lseek(tcs->masterfd, tcs->seek_pos * sizeof(struct index_entry) +
1019 sizeof(struct master_header), SEEK_SET);
1021 while (ecread(tcs->masterfd, &entry, 1, index_entry_ec, tc_stat.econ)
1022 == sizeof(struct index_entry))
1024 if (tcs->seek_list_count == SEEK_LIST_SIZE)
1025 break ;
1027 tcs->seek_pos++;
1029 /* Check if entry has been deleted. */
1030 if (entry.flag & FLAG_DELETED)
1031 continue;
1033 /* Go through all filters.. */
1034 for (i = 0; i < tcs->filter_count; i++)
1036 if (entry.tag_seek[tcs->filter_tag[i]] != tcs->filter_seek[i])
1037 break ;
1040 if (i < tcs->filter_count)
1041 continue ;
1043 /* Check for conditions. */
1044 if (!check_clauses(tcs, &entry, tcs->clause, tcs->clause_count))
1045 continue;
1047 /* Add to the seek list if not already in uniq buffer. */
1048 if (!add_uniqbuf(tcs, entry.tag_seek[tcs->type]))
1049 continue;
1051 /* Lets add it. */
1052 tcs->seek_list[tcs->seek_list_count] = entry.tag_seek[tcs->type];
1053 tcs->seek_flags[tcs->seek_list_count] = entry.flag;
1054 tcs->seek_list_count++;
1056 yield();
1059 return tcs->seek_list_count > 0;
1063 static void remove_files(void)
1065 int i;
1066 char buf[MAX_PATH];
1068 tc_stat.ready = false;
1069 tc_stat.ramcache = false;
1070 tc_stat.econ = false;
1071 remove(TAGCACHE_FILE_MASTER);
1072 for (i = 0; i < TAG_COUNT; i++)
1074 if (tagcache_is_numeric_tag(i))
1075 continue;
1077 snprintf(buf, sizeof buf, TAGCACHE_FILE_INDEX, i);
1078 remove(buf);
1083 static int open_master_fd(struct master_header *hdr, bool write)
1085 int fd;
1086 int rc;
1088 fd = open(TAGCACHE_FILE_MASTER, write ? O_RDWR : O_RDONLY);
1089 if (fd < 0)
1091 logf("master file open failed for R/W");
1092 tc_stat.ready = false;
1093 return fd;
1096 tc_stat.econ = false;
1098 /* Check the header. */
1099 rc = read(fd, hdr, sizeof(struct master_header));
1100 if (hdr->tch.magic == TAGCACHE_MAGIC && rc == sizeof(struct master_header))
1102 /* Success. */
1103 return fd;
1106 /* Trying to read again, this time with endianess correction enabled. */
1107 lseek(fd, 0, SEEK_SET);
1109 rc = ecread(fd, hdr, 1, master_header_ec, true);
1110 if (hdr->tch.magic != TAGCACHE_MAGIC || rc != sizeof(struct master_header))
1112 logf("header error");
1113 tc_stat.ready = false;
1114 close(fd);
1115 return -2;
1118 tc_stat.econ = true;
1120 return fd;
1123 static bool check_all_headers(void)
1125 struct master_header myhdr;
1126 struct tagcache_header tch;
1127 int tag;
1128 int fd;
1130 if ( (fd = open_master_fd(&myhdr, false)) < 0)
1131 return false;
1133 close(fd);
1134 if (myhdr.dirty)
1136 logf("tagcache is dirty!");
1137 return false;
1140 memcpy(&current_tcmh, &myhdr, sizeof(struct master_header));
1142 for (tag = 0; tag < TAG_COUNT; tag++)
1144 if (tagcache_is_numeric_tag(tag))
1145 continue;
1147 if ( (fd = open_tag_fd(&tch, tag, false)) < 0)
1148 return false;
1150 close(fd);
1153 return true;
1156 bool tagcache_search(struct tagcache_search *tcs, int tag)
1158 struct tagcache_header tag_hdr;
1159 struct master_header master_hdr;
1160 int i;
1162 if (tcs->initialized)
1163 tagcache_search_finish(tcs);
1165 while (read_lock)
1166 sleep(1);
1168 memset(tcs, 0, sizeof(struct tagcache_search));
1169 if (tc_stat.commit_step > 0 || !tc_stat.ready)
1170 return false;
1172 tcs->position = sizeof(struct tagcache_header);
1173 tcs->type = tag;
1174 tcs->seek_pos = 0;
1175 tcs->seek_list_count = 0;
1176 tcs->filter_count = 0;
1177 tcs->masterfd = -1;
1179 for (i = 0; i < TAG_COUNT; i++)
1180 tcs->idxfd[i] = -1;
1182 #ifndef HAVE_TC_RAMCACHE
1183 tcs->ramsearch = false;
1184 #else
1185 tcs->ramsearch = tc_stat.ramcache;
1186 if (tcs->ramsearch)
1188 tcs->entry_count = hdr->entry_count[tcs->type];
1190 else
1191 #endif
1193 if (!tagcache_is_numeric_tag(tcs->type))
1195 tcs->idxfd[tcs->type] = open_tag_fd(&tag_hdr, tcs->type, false);
1196 if (tcs->idxfd[tcs->type] < 0)
1197 return false;
1200 /* Always open as R/W so we can pass tcs to functions that modify data also
1201 * without failing. */
1202 tcs->masterfd = open_master_fd(&master_hdr, true);
1204 if (tcs->masterfd < 0)
1205 return false;
1208 tcs->valid = true;
1209 tcs->initialized = true;
1210 write_lock++;
1212 return true;
1215 void tagcache_search_set_uniqbuf(struct tagcache_search *tcs,
1216 void *buffer, long length)
1218 tcs->unique_list = (unsigned long *)buffer;
1219 tcs->unique_list_capacity = length / sizeof(*tcs->unique_list);
1220 tcs->unique_list_count = 0;
1223 bool tagcache_search_add_filter(struct tagcache_search *tcs,
1224 int tag, int seek)
1226 if (tcs->filter_count == TAGCACHE_MAX_FILTERS)
1227 return false;
1229 if (!tagcache_is_unique_tag(tag) || tagcache_is_numeric_tag(tag))
1230 return false;
1232 tcs->filter_tag[tcs->filter_count] = tag;
1233 tcs->filter_seek[tcs->filter_count] = seek;
1234 tcs->filter_count++;
1236 return true;
1239 bool tagcache_search_add_clause(struct tagcache_search *tcs,
1240 struct tagcache_search_clause *clause)
1242 int i;
1244 if (tcs->clause_count >= TAGCACHE_MAX_CLAUSES)
1246 logf("Too many clauses");
1247 return false;
1250 /* Check if there is already a similar filter in present (filters are
1251 * much faster than clauses).
1253 for (i = 0; i < tcs->filter_count; i++)
1255 if (tcs->filter_tag[i] == clause->tag)
1256 return true;
1259 if (!tagcache_is_numeric_tag(clause->tag) && tcs->idxfd[clause->tag] < 0)
1261 char buf[MAX_PATH];
1263 snprintf(buf, sizeof buf, TAGCACHE_FILE_INDEX, clause->tag);
1264 tcs->idxfd[clause->tag] = open(buf, O_RDONLY);
1267 tcs->clause[tcs->clause_count] = clause;
1268 tcs->clause_count++;
1270 return true;
1273 #define TAG_FILENAME_RAM(tcs) ((tcs->type == tag_filename) \
1274 ? (flag & FLAG_DIRCACHE) : 1)
1276 static bool get_next(struct tagcache_search *tcs)
1278 static char buf[TAG_MAXLEN+32];
1279 struct tagfile_entry entry;
1280 long flag = 0;
1282 if (!tcs->valid || !tc_stat.ready)
1283 return false;
1285 if (tcs->idxfd[tcs->type] < 0 && !tagcache_is_numeric_tag(tcs->type)
1286 #ifdef HAVE_TC_RAMCACHE
1287 && !tcs->ramsearch
1288 #endif
1290 return false;
1292 /* Relative fetch. */
1293 if (tcs->filter_count > 0 || tcs->clause_count > 0
1294 || tagcache_is_numeric_tag(tcs->type))
1296 /* Check for end of list. */
1297 if (tcs->seek_list_count == 0)
1299 /* Try to fetch more. */
1300 if (!build_lookup_list(tcs))
1302 tcs->valid = false;
1303 return false;
1307 tcs->seek_list_count--;
1308 flag = tcs->seek_flags[tcs->seek_list_count];
1310 /* Seek stream to the correct position and continue to direct fetch. */
1311 if ((!tcs->ramsearch || !TAG_FILENAME_RAM(tcs))
1312 && !tagcache_is_numeric_tag(tcs->type))
1314 if (!open_files(tcs, tcs->type))
1315 return false;
1317 lseek(tcs->idxfd[tcs->type], tcs->seek_list[tcs->seek_list_count], SEEK_SET);
1319 else
1320 tcs->position = tcs->seek_list[tcs->seek_list_count];
1323 if (tagcache_is_numeric_tag(tcs->type))
1325 snprintf(buf, sizeof(buf), "%d", tcs->position);
1326 tcs->result_seek = tcs->position;
1327 tcs->result = buf;
1328 tcs->result_len = strlen(buf) + 1;
1329 return true;
1332 /* Direct fetch. */
1333 #ifdef HAVE_TC_RAMCACHE
1334 if (tcs->ramsearch && TAG_FILENAME_RAM(tcs))
1336 struct tagfile_entry *ep;
1338 if (tcs->entry_count == 0)
1340 tcs->valid = false;
1341 return false;
1343 tcs->entry_count--;
1345 tcs->result_seek = tcs->position;
1347 # ifdef HAVE_DIRCACHE
1348 if (tcs->type == tag_filename)
1350 dircache_copy_path((struct dirent *)tcs->position,
1351 buf, sizeof buf);
1352 tcs->result = buf;
1353 tcs->result_len = strlen(buf) + 1;
1354 tcs->idx_id = FLAG_GET_ATTR(flag);
1355 tcs->ramresult = false;
1357 return true;
1359 # endif
1361 ep = (struct tagfile_entry *)&hdr->tags[tcs->type][tcs->position];
1362 tcs->position += sizeof(struct tagfile_entry) + ep->tag_length;
1363 tcs->result = ep->tag_data;
1364 tcs->result_len = strlen(tcs->result) + 1;
1365 tcs->idx_id = ep->idx_id;
1366 tcs->ramresult = true;
1368 return true;
1370 else
1371 #endif
1373 if (!open_files(tcs, tcs->type))
1374 return false;
1376 tcs->result_seek = lseek(tcs->idxfd[tcs->type], 0, SEEK_CUR);
1377 if (ecread(tcs->idxfd[tcs->type], &entry, 1,
1378 tagfile_entry_ec, tc_stat.econ) != sizeof(struct tagfile_entry))
1380 /* End of data. */
1381 tcs->valid = false;
1382 return false;
1386 if (entry.tag_length > (long)sizeof(buf))
1388 tcs->valid = false;
1389 logf("too long tag #2");
1390 return false;
1393 if (read(tcs->idxfd[tcs->type], buf, entry.tag_length) != entry.tag_length)
1395 tcs->valid = false;
1396 logf("read error #4");
1397 return false;
1400 tcs->result = buf;
1401 tcs->result_len = strlen(tcs->result) + 1;
1402 tcs->idx_id = entry.idx_id;
1403 tcs->ramresult = false;
1405 return true;
1408 bool tagcache_get_next(struct tagcache_search *tcs)
1410 while (get_next(tcs))
1412 if (tcs->result_len > 1)
1413 return true;
1416 return false;
1419 bool tagcache_retrieve(struct tagcache_search *tcs, int idxid,
1420 int tag, char *buf, long size)
1422 struct index_entry idx;
1424 *buf = '\0';
1425 if (!get_index(tcs->masterfd, idxid, &idx, true))
1426 return false;
1428 return retrieve(tcs, &idx, tag, buf, size);
1431 static bool update_master_header(void)
1433 struct master_header myhdr;
1434 int fd;
1436 if (!tc_stat.ready)
1437 return false;
1439 if ( (fd = open_master_fd(&myhdr, true)) < 0)
1440 return false;
1442 myhdr.serial = current_tcmh.serial;
1443 myhdr.commitid = current_tcmh.commitid;
1445 /* Write it back */
1446 lseek(fd, 0, SEEK_SET);
1447 ecwrite(fd, &myhdr, 1, master_header_ec, tc_stat.econ);
1448 close(fd);
1450 #ifdef HAVE_TC_RAMCACHE
1451 if (hdr)
1453 hdr->h.serial = current_tcmh.serial;
1454 hdr->h.commitid = current_tcmh.commitid;
1456 #endif
1458 return true;
1461 #if 0
1463 void tagcache_modify(struct tagcache_search *tcs, int type, const char *text)
1465 struct tagentry *entry;
1467 if (tcs->type != tag_title)
1468 return ;
1470 /* We will need reserve buffer for this. */
1471 if (tcs->ramcache)
1473 struct tagfile_entry *ep;
1475 ep = (struct tagfile_entry *)&hdr->tags[tcs->type][tcs->result_seek];
1476 tcs->seek_list[tcs->seek_list_count];
1479 entry = find_entry_ram();
1482 #endif
1484 void tagcache_search_finish(struct tagcache_search *tcs)
1486 int i;
1488 if (!tcs->initialized)
1489 return;
1491 if (tcs->masterfd >= 0)
1493 close(tcs->masterfd);
1494 tcs->masterfd = -1;
1497 for (i = 0; i < TAG_COUNT; i++)
1499 if (tcs->idxfd[i] >= 0)
1501 close(tcs->idxfd[i]);
1502 tcs->idxfd[i] = -1;
1506 tcs->ramsearch = false;
1507 tcs->valid = false;
1508 tcs->initialized = 0;
1509 if (write_lock > 0)
1510 write_lock--;
1513 #if defined(HAVE_TC_RAMCACHE) && defined(HAVE_DIRCACHE)
1514 static struct tagfile_entry *get_tag(const struct index_entry *entry, int tag)
1516 return (struct tagfile_entry *)&hdr->tags[tag][entry->tag_seek[tag]];
1519 static long get_tag_numeric(const struct index_entry *entry, int tag)
1521 return entry->tag_seek[tag];
1524 static char* get_tag_string(const struct index_entry *entry, int tag)
1526 char* s = get_tag(entry, tag)->tag_data;
1527 return strcmp(s, UNTAGGED) ? s : NULL;
1530 bool tagcache_fill_tags(struct mp3entry *id3, const char *filename)
1532 struct index_entry *entry;
1533 int idx_id;
1535 if (!tc_stat.ready)
1536 return false;
1538 /* Find the corresponding entry in tagcache. */
1539 idx_id = find_entry_ram(filename, NULL);
1540 if (idx_id < 0 || !tc_stat.ramcache)
1541 return false;
1543 entry = &hdr->indices[idx_id];
1545 id3->title = get_tag_string(entry, tag_title);
1546 id3->artist = get_tag_string(entry, tag_artist);
1547 id3->album = get_tag_string(entry, tag_album);
1548 id3->genre_string = get_tag_string(entry, tag_genre);
1549 id3->composer = get_tag_string(entry, tag_composer);
1550 id3->comment = get_tag_string(entry, tag_comment);
1551 id3->albumartist = get_tag_string(entry, tag_albumartist);
1552 id3->grouping = get_tag_string(entry, tag_grouping);
1554 id3->playcount = get_tag_numeric(entry, tag_playcount);
1555 id3->rating = get_tag_numeric(entry, tag_rating);
1556 id3->lastplayed = get_tag_numeric(entry, tag_lastplayed);
1557 id3->score = get_tag_numeric(entry, tag_virt_autoscore) / 10;
1558 id3->year = get_tag_numeric(entry, tag_year);
1560 id3->discnum = get_tag_numeric(entry, tag_discnumber);
1561 id3->tracknum = get_tag_numeric(entry, tag_tracknumber);
1562 id3->bitrate = get_tag_numeric(entry, tag_bitrate);
1563 if (id3->bitrate == 0)
1564 id3->bitrate = 1;
1566 return true;
1568 #endif
1570 static inline void write_item(const char *item)
1572 int len = strlen(item) + 1;
1574 data_size += len;
1575 write(cachefd, item, len);
1578 static int check_if_empty(char **tag)
1580 int length;
1582 if (*tag == NULL || *tag[0] == '\0')
1584 *tag = UNTAGGED;
1585 return sizeof(UNTAGGED); /* Tag length */
1588 length = strlen(*tag);
1589 if (length > TAG_MAXLEN)
1591 logf("over length tag: %s", *tag);
1592 length = TAG_MAXLEN;
1593 *tag[length] = '\0';
1596 return length + 1;
1599 #define ADD_TAG(entry,tag,data) \
1600 /* Adding tag */ \
1601 entry.tag_offset[tag] = offset; \
1602 entry.tag_length[tag] = check_if_empty(data); \
1603 offset += entry.tag_length[tag]
1605 #if defined(HAVE_TC_RAMCACHE) && defined(HAVE_DIRCACHE)
1606 static void add_tagcache(char *path, const struct dirent *dc)
1607 #else
1608 static void add_tagcache(char *path)
1609 #endif
1611 struct track_info track;
1612 struct temp_file_entry entry;
1613 bool ret;
1614 int fd;
1615 char tracknumfix[3];
1616 int offset = 0;
1617 int path_length = strlen(path);
1618 bool has_albumartist;
1619 bool has_grouping;
1621 if (cachefd < 0)
1622 return ;
1624 /* Check for overlength file path. */
1625 if (path_length > TAG_MAXLEN)
1627 /* Path can't be shortened. */
1628 logf("Too long path: %s", path);
1629 return ;
1632 /* Check if the file is supported. */
1633 if (probe_file_format(path) == AFMT_UNKNOWN)
1634 return ;
1636 /* Check if the file is already cached. */
1637 #if defined(HAVE_TC_RAMCACHE) && defined(HAVE_DIRCACHE)
1638 if (tc_stat.ramcache && dircache_is_enabled())
1640 if (find_entry_ram(path, dc) >= 0)
1641 return ;
1643 else
1644 #endif
1646 if (filenametag_fd >= 0)
1648 if (find_entry_disk(path) >= 0)
1649 return ;
1653 fd = open(path, O_RDONLY);
1654 if (fd < 0)
1656 logf("open fail: %s", path);
1657 return ;
1660 memset(&track, 0, sizeof(struct track_info));
1661 memset(&entry, 0, sizeof(struct temp_file_entry));
1662 memset(&tracknumfix, 0, sizeof(tracknumfix));
1663 ret = get_metadata(&(track.id3), fd, path, false);
1664 close(fd);
1666 if (!ret)
1667 return ;
1669 logf("-> %s", path);
1671 /* Generate track number if missing. */
1672 if (track.id3.tracknum <= 0)
1674 const char *p = strrchr(path, '.');
1676 if (p == NULL)
1677 p = &path[strlen(path)-1];
1679 while (*p != '/')
1681 if (isdigit(*p) && isdigit(*(p-1)))
1683 tracknumfix[1] = *p--;
1684 tracknumfix[0] = *p;
1685 break;
1687 p--;
1690 if (tracknumfix[0] != '\0')
1692 track.id3.tracknum = atoi(tracknumfix);
1693 /* Set a flag to indicate track number has been generated. */
1694 entry.flag |= FLAG_TRKNUMGEN;
1696 else
1698 /* Unable to generate track number. */
1699 track.id3.tracknum = -1;
1703 /* Numeric tags */
1704 entry.tag_offset[tag_year] = track.id3.year;
1705 entry.tag_offset[tag_discnumber] = track.id3.discnum;
1706 entry.tag_offset[tag_tracknumber] = track.id3.tracknum;
1707 entry.tag_offset[tag_length] = track.id3.length;
1708 entry.tag_offset[tag_bitrate] = track.id3.bitrate;
1710 /* String tags. */
1711 has_albumartist = track.id3.albumartist != NULL
1712 && strlen(track.id3.albumartist) > 0;
1713 has_grouping = track.id3.grouping != NULL
1714 && strlen(track.id3.grouping) > 0;
1716 ADD_TAG(entry, tag_filename, &path);
1717 ADD_TAG(entry, tag_title, &track.id3.title);
1718 ADD_TAG(entry, tag_artist, &track.id3.artist);
1719 ADD_TAG(entry, tag_album, &track.id3.album);
1720 ADD_TAG(entry, tag_genre, &track.id3.genre_string);
1721 ADD_TAG(entry, tag_composer, &track.id3.composer);
1722 ADD_TAG(entry, tag_comment, &track.id3.comment);
1723 if (has_albumartist)
1725 ADD_TAG(entry, tag_albumartist, &track.id3.albumartist);
1727 else
1729 ADD_TAG(entry, tag_albumartist, &track.id3.artist);
1731 if (has_grouping)
1733 ADD_TAG(entry, tag_grouping, &track.id3.grouping);
1735 else
1737 ADD_TAG(entry, tag_grouping, &track.id3.title);
1739 entry.data_length = offset;
1741 /* Write the header */
1742 write(cachefd, &entry, sizeof(struct temp_file_entry));
1744 /* And tags also... Correct order is critical */
1745 write_item(path);
1746 write_item(track.id3.title);
1747 write_item(track.id3.artist);
1748 write_item(track.id3.album);
1749 write_item(track.id3.genre_string);
1750 write_item(track.id3.composer);
1751 write_item(track.id3.comment);
1752 if (has_albumartist)
1754 write_item(track.id3.albumartist);
1756 else
1758 write_item(track.id3.artist);
1760 if (has_grouping)
1762 write_item(track.id3.grouping);
1764 else
1766 write_item(track.id3.title);
1768 total_entry_count++;
1771 static bool tempbuf_insert(char *str, int id, int idx_id, bool unique)
1773 struct tempbuf_searchidx *index = (struct tempbuf_searchidx *)tempbuf;
1774 int len = strlen(str)+1;
1775 int i;
1776 unsigned crc32;
1777 unsigned *crcbuf = (unsigned *)&tempbuf[tempbuf_size-4];
1778 char buf[TAG_MAXLEN+32];
1780 for (i = 0; str[i] != '\0' && i < (int)sizeof(buf)-1; i++)
1781 buf[i] = tolower(str[i]);
1782 buf[i] = '\0';
1784 crc32 = crc_32(buf, i, 0xffffffff);
1786 if (unique)
1788 /* Check if the crc does not exist -> entry does not exist for sure. */
1789 for (i = 0; i < tempbufidx; i++)
1791 if (crcbuf[-i] != crc32)
1792 continue;
1794 if (!strcasecmp(str, index[i].str))
1796 if (id < 0 || id >= lookup_buffer_depth)
1798 logf("lookup buf overf.: %d", id);
1799 return false;
1802 lookup[id] = &index[i];
1803 return true;
1808 /* Insert to CRC buffer. */
1809 crcbuf[-tempbufidx] = crc32;
1810 tempbuf_left -= 4;
1812 /* Insert it to the buffer. */
1813 tempbuf_left -= len;
1814 if (tempbuf_left - 4 < 0 || tempbufidx >= commit_entry_count-1)
1815 return false;
1817 if (id >= lookup_buffer_depth)
1819 logf("lookup buf overf. #2: %d", id);
1820 return false;
1823 if (id >= 0)
1825 lookup[id] = &index[tempbufidx];
1826 index[tempbufidx].idlist.id = id;
1828 else
1829 index[tempbufidx].idlist.id = -1;
1831 index[tempbufidx].idlist.next = NULL;
1832 index[tempbufidx].idx_id = idx_id;
1833 index[tempbufidx].seek = -1;
1834 index[tempbufidx].str = &tempbuf[tempbuf_pos];
1835 memcpy(index[tempbufidx].str, str, len);
1836 tempbuf_pos += len;
1837 tempbufidx++;
1839 return true;
1842 static int compare(const void *p1, const void *p2)
1844 do_timed_yield();
1846 struct tempbuf_searchidx *e1 = (struct tempbuf_searchidx *)p1;
1847 struct tempbuf_searchidx *e2 = (struct tempbuf_searchidx *)p2;
1849 if (strcmp(e1->str, UNTAGGED) == 0)
1851 if (strcmp(e2->str, UNTAGGED) == 0)
1852 return 0;
1853 return -1;
1855 else if (strcmp(e2->str, UNTAGGED) == 0)
1856 return 1;
1858 return strncasecmp(e1->str, e2->str, TAG_MAXLEN);
1861 static int tempbuf_sort(int fd)
1863 struct tempbuf_searchidx *index = (struct tempbuf_searchidx *)tempbuf;
1864 struct tagfile_entry fe;
1865 int i;
1866 int length;
1868 /* Generate reverse lookup entries. */
1869 for (i = 0; i < lookup_buffer_depth; i++)
1871 struct tempbuf_id_list *idlist;
1873 if (!lookup[i])
1874 continue;
1876 if (lookup[i]->idlist.id == i)
1877 continue;
1879 idlist = &lookup[i]->idlist;
1880 while (idlist->next != NULL)
1881 idlist = idlist->next;
1883 tempbuf_left -= sizeof(struct tempbuf_id_list);
1884 if (tempbuf_left - 4 < 0)
1885 return -1;
1887 idlist->next = (struct tempbuf_id_list *)&tempbuf[tempbuf_pos];
1888 if (tempbuf_pos & 0x03)
1890 tempbuf_pos = (tempbuf_pos & ~0x03) + 0x04;
1891 tempbuf_left -= 3;
1892 idlist->next = (struct tempbuf_id_list *)&tempbuf[tempbuf_pos];
1894 tempbuf_pos += sizeof(struct tempbuf_id_list);
1896 idlist = idlist->next;
1897 idlist->id = i;
1898 idlist->next = NULL;
1900 do_timed_yield();
1903 qsort(index, tempbufidx, sizeof(struct tempbuf_searchidx), compare);
1904 memset(lookup, 0, lookup_buffer_depth * sizeof(struct tempbuf_searchidx **));
1906 for (i = 0; i < tempbufidx; i++)
1908 struct tempbuf_id_list *idlist = &index[i].idlist;
1910 /* Fix the lookup list. */
1911 while (idlist != NULL)
1913 if (idlist->id >= 0)
1914 lookup[idlist->id] = &index[i];
1915 idlist = idlist->next;
1918 index[i].seek = lseek(fd, 0, SEEK_CUR);
1919 length = strlen(index[i].str) + 1;
1920 fe.tag_length = length;
1921 fe.idx_id = index[i].idx_id;
1923 /* Check the chunk alignment. */
1924 if ((fe.tag_length + sizeof(struct tagfile_entry))
1925 % TAGFILE_ENTRY_CHUNK_LENGTH)
1927 fe.tag_length += TAGFILE_ENTRY_CHUNK_LENGTH -
1928 ((fe.tag_length + sizeof(struct tagfile_entry))
1929 % TAGFILE_ENTRY_CHUNK_LENGTH);
1932 #ifdef TAGCACHE_STRICT_ALIGN
1933 /* Make sure the entry is long aligned. */
1934 if (index[i].seek & 0x03)
1936 logf("tempbuf_sort: alignment error!");
1937 return -3;
1939 #endif
1941 if (ecwrite(fd, &fe, 1, tagfile_entry_ec, tc_stat.econ) !=
1942 sizeof(struct tagfile_entry))
1944 logf("tempbuf_sort: write error #1");
1945 return -1;
1948 if (write(fd, index[i].str, length) != length)
1950 logf("tempbuf_sort: write error #2");
1951 return -2;
1954 /* Write some padding. */
1955 if (fe.tag_length - length > 0)
1956 write(fd, "XXXXXXXX", fe.tag_length - length);
1959 return i;
1962 inline static struct tempbuf_searchidx* tempbuf_locate(int id)
1964 if (id < 0 || id >= lookup_buffer_depth)
1965 return NULL;
1967 return lookup[id];
1971 inline static int tempbuf_find_location(int id)
1973 struct tempbuf_searchidx *entry;
1975 entry = tempbuf_locate(id);
1976 if (entry == NULL)
1977 return -1;
1979 return entry->seek;
1982 static bool build_numeric_indices(struct tagcache_header *h, int tmpfd)
1984 struct master_header tcmh;
1985 struct index_entry idx;
1986 int masterfd;
1987 int masterfd_pos;
1988 struct temp_file_entry *entrybuf = (struct temp_file_entry *)tempbuf;
1989 int max_entries;
1990 int entries_processed = 0;
1991 int i, j;
1993 max_entries = tempbuf_size / sizeof(struct temp_file_entry) - 1;
1995 logf("Building numeric indices...");
1996 lseek(tmpfd, sizeof(struct tagcache_header), SEEK_SET);
1998 if ( (masterfd = open_master_fd(&tcmh, true)) < 0)
1999 return false;
2001 masterfd_pos = lseek(masterfd, tcmh.tch.entry_count * sizeof(struct index_entry),
2002 SEEK_CUR);
2003 if (masterfd_pos == filesize(masterfd))
2005 logf("we can't append!");
2006 close(masterfd);
2007 return false;
2010 while (entries_processed < h->entry_count)
2012 int count = MIN(h->entry_count - entries_processed, max_entries);
2014 /* Read in as many entries as possible. */
2015 for (i = 0; i < count; i++)
2017 /* Read in numeric data. */
2018 if (read(tmpfd, &entrybuf[i], sizeof(struct temp_file_entry)) !=
2019 sizeof(struct temp_file_entry))
2021 logf("read fail #1");
2022 close(masterfd);
2023 return false;
2026 /* Skip string data. */
2027 lseek(tmpfd, entrybuf[i].data_length, SEEK_CUR);
2030 /* Commit the data to the index. */
2031 for (i = 0; i < count; i++)
2033 int loc = lseek(masterfd, 0, SEEK_CUR);
2035 if (ecread(masterfd, &idx, 1, index_entry_ec, tc_stat.econ)
2036 != sizeof(struct index_entry))
2038 logf("read fail #2");
2039 close(masterfd);
2040 return false;
2043 for (j = 0; j < TAG_COUNT; j++)
2045 if (!tagcache_is_numeric_tag(j))
2046 continue;
2048 idx.tag_seek[j] = entrybuf[i].tag_offset[j];
2050 idx.flag = entrybuf[i].flag;
2052 if (tc_stat.ready && current_tcmh.commitid > 0)
2054 idx.tag_seek[tag_commitid] = current_tcmh.commitid;
2055 idx.flag |= FLAG_DIRTYNUM;
2058 /* Write back the updated index. */
2059 lseek(masterfd, loc, SEEK_SET);
2060 if (ecwrite(masterfd, &idx, 1, index_entry_ec, tc_stat.econ)
2061 != sizeof(struct index_entry))
2063 logf("write fail");
2064 close(masterfd);
2065 return false;
2069 entries_processed += count;
2070 logf("%d/%ld entries processed", entries_processed, h->entry_count);
2073 close(masterfd);
2075 return true;
2079 * Return values:
2080 * > 0 success
2081 * == 0 temporary failure
2082 * < 0 fatal error
2084 static int build_index(int index_type, struct tagcache_header *h, int tmpfd)
2086 int i;
2087 struct tagcache_header tch;
2088 struct master_header tcmh;
2089 struct index_entry idxbuf[IDX_BUF_DEPTH];
2090 int idxbuf_pos;
2091 char buf[TAG_MAXLEN+32];
2092 int fd = -1, masterfd;
2093 bool error = false;
2094 int init;
2095 int masterfd_pos;
2097 logf("Building index: %d", index_type);
2099 /* Check the number of entries we need to allocate ram for. */
2100 commit_entry_count = h->entry_count + 1;
2102 masterfd = open_master_fd(&tcmh, false);
2103 if (masterfd >= 0)
2105 commit_entry_count += tcmh.tch.entry_count;
2106 close(masterfd);
2108 else
2109 remove_files(); /* Just to be sure we are clean. */
2111 /* Open the index file, which contains the tag names. */
2112 fd = open_tag_fd(&tch, index_type, true);
2113 if (fd >= 0)
2115 logf("tch.datasize=%ld", tch.datasize);
2116 lookup_buffer_depth = 1 +
2117 /* First part */ commit_entry_count +
2118 /* Second part */ (tch.datasize / TAGFILE_ENTRY_CHUNK_LENGTH);
2120 else
2122 lookup_buffer_depth = 1 +
2123 /* First part */ commit_entry_count +
2124 /* Second part */ 0;
2127 logf("lookup_buffer_depth=%ld", lookup_buffer_depth);
2128 logf("commit_entry_count=%ld", commit_entry_count);
2130 /* Allocate buffer for all index entries from both old and new
2131 * tag files. */
2132 tempbufidx = 0;
2133 tempbuf_pos = commit_entry_count * sizeof(struct tempbuf_searchidx);
2135 /* Allocate lookup buffer. The first portion of commit_entry_count
2136 * contains the new tags in the temporary file and the second
2137 * part for locating entries already in the db.
2139 * New tags Old tags
2140 * +---------+---------------------------+
2141 * | index | position/ENTRY_CHUNK_SIZE | lookup buffer
2142 * +---------+---------------------------+
2144 * Old tags are inserted to a temporary buffer with position:
2145 * tempbuf_insert(position/ENTRY_CHUNK_SIZE, ...);
2146 * And new tags with index:
2147 * tempbuf_insert(idx, ...);
2149 * The buffer is sorted and written into tag file:
2150 * tempbuf_sort(...);
2151 * leaving master index locations messed up.
2153 * That is fixed using the lookup buffer for old tags:
2154 * new_seek = tempbuf_find_location(old_seek, ...);
2155 * and for new tags:
2156 * new_seek = tempbuf_find_location(idx);
2158 lookup = (struct tempbuf_searchidx **)&tempbuf[tempbuf_pos];
2159 tempbuf_pos += lookup_buffer_depth * sizeof(void **);
2160 memset(lookup, 0, lookup_buffer_depth * sizeof(void **));
2162 /* And calculate the remaining data space used mainly for storing
2163 * tag data (strings). */
2164 tempbuf_left = tempbuf_size - tempbuf_pos - 8;
2165 if (tempbuf_left - TAGFILE_ENTRY_AVG_LENGTH * commit_entry_count < 0)
2167 logf("Buffer way too small!");
2168 return 0;
2171 if (fd >= 0)
2174 * If tag file contains unique tags (sorted index), we will load
2175 * it entirely into memory so we can resort it later for use with
2176 * chunked browsing.
2178 if (tagcache_is_sorted_tag(index_type))
2180 logf("loading tags...");
2181 for (i = 0; i < tch.entry_count; i++)
2183 struct tagfile_entry entry;
2184 int loc = lseek(fd, 0, SEEK_CUR);
2185 bool ret;
2187 if (ecread(fd, &entry, 1, tagfile_entry_ec, tc_stat.econ)
2188 != sizeof(struct tagfile_entry))
2190 logf("read error #7");
2191 close(fd);
2192 return -2;
2195 if (entry.tag_length >= (int)sizeof(buf))
2197 logf("too long tag #3");
2198 close(fd);
2199 return -2;
2202 if (read(fd, buf, entry.tag_length) != entry.tag_length)
2204 logf("read error #8");
2205 close(fd);
2206 return -2;
2209 /* Skip deleted entries. */
2210 if (buf[0] == '\0')
2211 continue;
2214 * Save the tag and tag id in the memory buffer. Tag id
2215 * is saved so we can later reindex the master lookup
2216 * table when the index gets resorted.
2218 ret = tempbuf_insert(buf, loc/TAGFILE_ENTRY_CHUNK_LENGTH
2219 + commit_entry_count, entry.idx_id,
2220 tagcache_is_unique_tag(index_type));
2221 if (!ret)
2223 close(fd);
2224 return -3;
2226 do_timed_yield();
2228 logf("done");
2230 else
2231 tempbufidx = tch.entry_count;
2233 else
2236 * Creating new index file to store the tags. No need to preload
2237 * anything whether the index type is sorted or not.
2239 snprintf(buf, sizeof buf, TAGCACHE_FILE_INDEX, index_type);
2240 fd = open(buf, O_WRONLY | O_CREAT | O_TRUNC);
2241 if (fd < 0)
2243 logf("%s open fail", buf);
2244 return -2;
2247 tch.magic = TAGCACHE_MAGIC;
2248 tch.entry_count = 0;
2249 tch.datasize = 0;
2251 if (ecwrite(fd, &tch, 1, tagcache_header_ec, tc_stat.econ)
2252 != sizeof(struct tagcache_header))
2254 logf("header write failed");
2255 close(fd);
2256 return -2;
2260 /* Loading the tag lookup file as "master file". */
2261 logf("Loading index file");
2262 masterfd = open(TAGCACHE_FILE_MASTER, O_RDWR);
2264 if (masterfd < 0)
2266 logf("Creating new DB");
2267 masterfd = open(TAGCACHE_FILE_MASTER, O_WRONLY | O_CREAT | O_TRUNC);
2269 if (masterfd < 0)
2271 logf("Failure to create index file (%s)", TAGCACHE_FILE_MASTER);
2272 close(fd);
2273 return -2;
2276 /* Write the header (write real values later). */
2277 memset(&tcmh, 0, sizeof(struct master_header));
2278 tcmh.tch = *h;
2279 tcmh.tch.entry_count = 0;
2280 tcmh.tch.datasize = 0;
2281 tcmh.dirty = true;
2282 ecwrite(masterfd, &tcmh, 1, master_header_ec, tc_stat.econ);
2283 init = true;
2284 masterfd_pos = lseek(masterfd, 0, SEEK_CUR);
2286 else
2289 * Master file already exists so we need to process the current
2290 * file first.
2292 init = false;
2294 if (ecread(masterfd, &tcmh, 1, master_header_ec, tc_stat.econ) !=
2295 sizeof(struct master_header) || tcmh.tch.magic != TAGCACHE_MAGIC)
2297 logf("header error");
2298 close(fd);
2299 close(masterfd);
2300 return -2;
2304 * If we reach end of the master file, we need to expand it to
2305 * hold new tags. If the current index is not sorted, we can
2306 * simply append new data to end of the file.
2307 * However, if the index is sorted, we need to update all tag
2308 * pointers in the master file for the current index.
2310 masterfd_pos = lseek(masterfd, tcmh.tch.entry_count * sizeof(struct index_entry),
2311 SEEK_CUR);
2312 if (masterfd_pos == filesize(masterfd))
2314 logf("appending...");
2315 init = true;
2320 * Load new unique tags in memory to be sorted later and added
2321 * to the master lookup file.
2323 if (tagcache_is_sorted_tag(index_type))
2325 lseek(tmpfd, sizeof(struct tagcache_header), SEEK_SET);
2326 /* h is the header of the temporary file containing new tags. */
2327 logf("inserting new tags...");
2328 for (i = 0; i < h->entry_count; i++)
2330 struct temp_file_entry entry;
2332 if (read(tmpfd, &entry, sizeof(struct temp_file_entry)) !=
2333 sizeof(struct temp_file_entry))
2335 logf("read fail #3");
2336 error = true;
2337 goto error_exit;
2340 /* Read data. */
2341 if (entry.tag_length[index_type] >= (long)sizeof(buf))
2343 logf("too long entry!");
2344 error = true;
2345 goto error_exit;
2348 lseek(tmpfd, entry.tag_offset[index_type], SEEK_CUR);
2349 if (read(tmpfd, buf, entry.tag_length[index_type]) !=
2350 entry.tag_length[index_type])
2352 logf("read fail #4");
2353 error = true;
2354 goto error_exit;
2357 if (tagcache_is_unique_tag(index_type))
2358 error = !tempbuf_insert(buf, i, -1, true);
2359 else
2360 error = !tempbuf_insert(buf, i, tcmh.tch.entry_count + i, false);
2362 if (error)
2364 logf("insert error");
2365 goto error_exit;
2368 /* Skip to next. */
2369 lseek(tmpfd, entry.data_length - entry.tag_offset[index_type] -
2370 entry.tag_length[index_type], SEEK_CUR);
2371 do_timed_yield();
2373 logf("done");
2375 /* Sort the buffer data and write it to the index file. */
2376 lseek(fd, sizeof(struct tagcache_header), SEEK_SET);
2377 i = tempbuf_sort(fd);
2378 if (i < 0)
2379 goto error_exit;
2380 logf("sorted %d tags", i);
2383 * Now update all indexes in the master lookup file.
2385 logf("updating indices...");
2386 lseek(masterfd, sizeof(struct master_header), SEEK_SET);
2387 for (i = 0; i < tcmh.tch.entry_count; i += idxbuf_pos)
2389 int j;
2390 int loc = lseek(masterfd, 0, SEEK_CUR);
2392 idxbuf_pos = MIN(tcmh.tch.entry_count - i, IDX_BUF_DEPTH);
2394 if (ecread(masterfd, idxbuf, idxbuf_pos, index_entry_ec, tc_stat.econ)
2395 != (int)sizeof(struct index_entry)*idxbuf_pos)
2397 logf("read fail #5");
2398 error = true;
2399 goto error_exit ;
2401 lseek(masterfd, loc, SEEK_SET);
2403 for (j = 0; j < idxbuf_pos; j++)
2405 if (idxbuf[j].flag & FLAG_DELETED)
2407 /* We can just ignore deleted entries. */
2408 idxbuf[j].tag_seek[index_type] = 0;
2409 continue;
2412 idxbuf[j].tag_seek[index_type] = tempbuf_find_location(
2413 idxbuf[j].tag_seek[index_type]/TAGFILE_ENTRY_CHUNK_LENGTH
2414 + commit_entry_count);
2416 if (idxbuf[j].tag_seek[index_type] < 0)
2418 logf("update error: %d/%ld", i+j, tcmh.tch.entry_count);
2419 error = true;
2420 goto error_exit;
2423 do_timed_yield();
2426 /* Write back the updated index. */
2427 if (ecwrite(masterfd, idxbuf, idxbuf_pos,
2428 index_entry_ec, tc_stat.econ) !=
2429 (int)sizeof(struct index_entry)*idxbuf_pos)
2431 logf("write fail");
2432 error = true;
2433 goto error_exit;
2436 logf("done");
2440 * Walk through the temporary file containing the new tags.
2442 // build_normal_index(h, tmpfd, masterfd, idx);
2443 logf("updating new indices...");
2444 lseek(masterfd, masterfd_pos, SEEK_SET);
2445 lseek(tmpfd, sizeof(struct tagcache_header), SEEK_SET);
2446 lseek(fd, 0, SEEK_END);
2447 for (i = 0; i < h->entry_count; i += idxbuf_pos)
2449 int j;
2451 idxbuf_pos = MIN(h->entry_count - i, IDX_BUF_DEPTH);
2452 if (init)
2454 memset(idxbuf, 0, sizeof(struct index_entry)*IDX_BUF_DEPTH);
2456 else
2458 int loc = lseek(masterfd, 0, SEEK_CUR);
2460 if (ecread(masterfd, idxbuf, idxbuf_pos, index_entry_ec, tc_stat.econ)
2461 != (int)sizeof(struct index_entry)*idxbuf_pos)
2463 logf("read fail #6");
2464 error = true;
2465 break ;
2467 lseek(masterfd, loc, SEEK_SET);
2470 /* Read entry headers. */
2471 for (j = 0; j < idxbuf_pos; j++)
2473 if (!tagcache_is_sorted_tag(index_type))
2475 struct temp_file_entry entry;
2476 struct tagfile_entry fe;
2478 if (read(tmpfd, &entry, sizeof(struct temp_file_entry)) !=
2479 sizeof(struct temp_file_entry))
2481 logf("read fail #7");
2482 error = true;
2483 break ;
2486 /* Read data. */
2487 if (entry.tag_length[index_type] >= (int)sizeof(buf))
2489 logf("too long entry!");
2490 logf("length=%d", entry.tag_length[index_type]);
2491 logf("pos=0x%02lx", lseek(tmpfd, 0, SEEK_CUR));
2492 error = true;
2493 break ;
2496 lseek(tmpfd, entry.tag_offset[index_type], SEEK_CUR);
2497 if (read(tmpfd, buf, entry.tag_length[index_type]) !=
2498 entry.tag_length[index_type])
2500 logf("read fail #8");
2501 logf("offset=0x%02lx", entry.tag_offset[index_type]);
2502 logf("length=0x%02x", entry.tag_length[index_type]);
2503 error = true;
2504 break ;
2507 /* Write to index file. */
2508 idxbuf[j].tag_seek[index_type] = lseek(fd, 0, SEEK_CUR);
2509 fe.tag_length = entry.tag_length[index_type];
2510 fe.idx_id = tcmh.tch.entry_count + i + j;
2511 ecwrite(fd, &fe, 1, tagfile_entry_ec, tc_stat.econ);
2512 write(fd, buf, fe.tag_length);
2513 tempbufidx++;
2515 /* Skip to next. */
2516 lseek(tmpfd, entry.data_length - entry.tag_offset[index_type] -
2517 entry.tag_length[index_type], SEEK_CUR);
2519 else
2521 /* Locate the correct entry from the sorted array. */
2522 idxbuf[j].tag_seek[index_type] = tempbuf_find_location(i + j);
2523 if (idxbuf[j].tag_seek[index_type] < 0)
2525 logf("entry not found (%d)", j);
2526 error = true;
2527 break ;
2532 /* Write index. */
2533 if (ecwrite(masterfd, idxbuf, idxbuf_pos,
2534 index_entry_ec, tc_stat.econ) !=
2535 (int)sizeof(struct index_entry)*idxbuf_pos)
2537 logf("tagcache: write fail #4");
2538 error = true;
2539 break ;
2542 do_timed_yield();
2544 logf("done");
2546 /* Finally write the header. */
2547 tch.magic = TAGCACHE_MAGIC;
2548 tch.entry_count = tempbufidx;
2549 tch.datasize = lseek(fd, 0, SEEK_END) - sizeof(struct tagcache_header);
2550 lseek(fd, 0, SEEK_SET);
2551 ecwrite(fd, &tch, 1, tagcache_header_ec, tc_stat.econ);
2553 if (index_type != tag_filename)
2554 h->datasize += tch.datasize;
2555 logf("s:%d/%ld/%ld", index_type, tch.datasize, h->datasize);
2556 error_exit:
2558 close(fd);
2559 close(masterfd);
2561 if (error)
2562 return -2;
2564 return 1;
2567 static bool commit(void)
2569 struct tagcache_header tch;
2570 struct master_header tcmh;
2571 int i, len, rc;
2572 int tmpfd;
2573 int masterfd;
2574 #ifdef HAVE_DIRCACHE
2575 bool dircache_buffer_stolen = false;
2576 #endif
2577 bool local_allocation = false;
2579 logf("committing tagcache");
2581 while (write_lock)
2582 sleep(1);
2584 tmpfd = open(TAGCACHE_FILE_TEMP, O_RDONLY);
2585 if (tmpfd < 0)
2587 logf("nothing to commit");
2588 return true;
2592 /* Load the header. */
2593 len = sizeof(struct tagcache_header);
2594 rc = read(tmpfd, &tch, len);
2596 if (tch.magic != TAGCACHE_MAGIC || rc != len)
2598 logf("incorrect header");
2599 close(tmpfd);
2600 remove(TAGCACHE_FILE_TEMP);
2601 return false;
2604 if (tch.entry_count == 0)
2606 logf("nothing to commit");
2607 close(tmpfd);
2608 remove(TAGCACHE_FILE_TEMP);
2609 return true;
2612 #ifdef HAVE_EEPROM_SETTINGS
2613 remove(TAGCACHE_STATEFILE);
2614 #endif
2616 /* At first be sure to unload the ramcache! */
2617 #ifdef HAVE_TC_RAMCACHE
2618 tc_stat.ramcache = false;
2619 #endif
2621 read_lock++;
2623 /* Try to steal every buffer we can :) */
2624 if (tempbuf_size == 0)
2625 local_allocation = true;
2627 #ifdef HAVE_DIRCACHE
2628 if (tempbuf_size == 0)
2630 /* Try to steal the dircache buffer. */
2631 tempbuf = dircache_steal_buffer(&tempbuf_size);
2632 tempbuf_size &= ~0x03;
2634 if (tempbuf_size > 0)
2636 dircache_buffer_stolen = true;
2639 #endif
2641 #ifdef HAVE_TC_RAMCACHE
2642 if (tempbuf_size == 0 && tc_stat.ramcache_allocated > 0)
2644 tempbuf = (char *)(hdr + 1);
2645 tempbuf_size = tc_stat.ramcache_allocated - sizeof(struct ramcache_header) - 128;
2646 tempbuf_size &= ~0x03;
2648 #endif
2650 /* And finally fail if there are no buffers available. */
2651 if (tempbuf_size == 0)
2653 logf("delaying commit until next boot");
2654 tc_stat.commit_delayed = true;
2655 close(tmpfd);
2656 read_lock--;
2657 return false;
2660 logf("commit %ld entries...", tch.entry_count);
2662 /* Mark DB dirty so it will stay disabled if commit fails. */
2663 current_tcmh.dirty = true;
2664 update_master_header();
2666 /* Now create the index files. */
2667 tc_stat.commit_step = 0;
2668 tch.datasize = 0;
2669 tc_stat.commit_delayed = false;
2671 for (i = 0; i < TAG_COUNT; i++)
2673 int ret;
2675 if (tagcache_is_numeric_tag(i))
2676 continue;
2678 tc_stat.commit_step++;
2679 ret = build_index(i, &tch, tmpfd);
2680 if (ret <= 0)
2682 close(tmpfd);
2683 logf("tagcache failed init");
2684 if (ret < 0)
2685 remove_files();
2686 else
2687 tc_stat.commit_delayed = true;
2688 tc_stat.commit_step = 0;
2689 read_lock--;
2690 return false;
2694 if (!build_numeric_indices(&tch, tmpfd))
2696 logf("Failure to commit numeric indices");
2697 close(tmpfd);
2698 remove_files();
2699 tc_stat.commit_step = 0;
2700 read_lock--;
2701 return false;
2704 close(tmpfd);
2705 tc_stat.commit_step = 0;
2707 /* Update the master index headers. */
2708 if ( (masterfd = open_master_fd(&tcmh, true)) < 0)
2710 read_lock--;
2711 return false;
2714 tcmh.tch.entry_count += tch.entry_count;
2715 tcmh.tch.datasize = sizeof(struct master_header)
2716 + sizeof(struct index_entry) * tcmh.tch.entry_count
2717 + tch.datasize;
2718 tcmh.dirty = false;
2719 tcmh.commitid++;
2721 lseek(masterfd, 0, SEEK_SET);
2722 ecwrite(masterfd, &tcmh, 1, master_header_ec, tc_stat.econ);
2723 close(masterfd);
2725 logf("tagcache committed");
2726 remove(TAGCACHE_FILE_TEMP);
2727 tc_stat.ready = check_all_headers();
2728 tc_stat.readyvalid = true;
2730 if (local_allocation)
2732 tempbuf = NULL;
2733 tempbuf_size = 0;
2736 #ifdef HAVE_DIRCACHE
2737 /* Rebuild the dircache, if we stole the buffer. */
2738 if (dircache_buffer_stolen)
2739 dircache_build(0);
2740 #endif
2742 #ifdef HAVE_TC_RAMCACHE
2743 /* Reload tagcache. */
2744 if (tc_stat.ramcache_allocated > 0)
2745 tagcache_start_scan();
2746 #endif
2748 read_lock--;
2750 return true;
2753 static void allocate_tempbuf(void)
2755 /* Yeah, malloc would be really nice now :) */
2756 #ifdef __PCTOOL__
2757 tempbuf_size = 32*1024*1024;
2758 tempbuf = malloc(tempbuf_size);
2759 #else
2760 tempbuf = (char *)(((long)audiobuf & ~0x03) + 0x04);
2761 tempbuf_size = (long)audiobufend - (long)audiobuf - 4;
2762 audiobuf += tempbuf_size;
2763 #endif
2766 static void free_tempbuf(void)
2768 if (tempbuf_size == 0)
2769 return ;
2771 #ifdef __PCTOOL__
2772 free(tempbuf);
2773 #else
2774 audiobuf -= tempbuf_size;
2775 #endif
2776 tempbuf = NULL;
2777 tempbuf_size = 0;
2780 static bool modify_numeric_entry(int masterfd, int idx_id, int tag, long data)
2782 struct index_entry idx;
2784 if (!tc_stat.ready)
2785 return false;
2787 if (!tagcache_is_numeric_tag(tag))
2788 return false;
2790 if (!get_index(masterfd, idx_id, &idx, false))
2791 return false;
2793 idx.tag_seek[tag] = data;
2794 idx.flag |= FLAG_DIRTYNUM;
2796 return write_index(masterfd, idx_id, &idx);
2799 #if 0
2800 bool tagcache_modify_numeric_entry(struct tagcache_search *tcs,
2801 int tag, long data)
2803 struct master_header myhdr;
2805 if (tcs->masterfd < 0)
2807 if ( (tcs->masterfd = open_master_fd(&myhdr, true)) < 0)
2808 return false;
2811 return modify_numeric_entry(tcs->masterfd, tcs->idx_id, tag, data);
2813 #endif
2815 #define COMMAND_QUEUE_IS_EMPTY (command_queue_ridx == command_queue_widx)
2817 static bool command_queue_is_full(void)
2819 int next;
2821 next = command_queue_widx + 1;
2822 if (next >= TAGCACHE_COMMAND_QUEUE_LENGTH)
2823 next = 0;
2825 return (next == command_queue_ridx);
2828 void run_command_queue(bool force)
2830 struct master_header myhdr;
2831 int masterfd;
2833 if (COMMAND_QUEUE_IS_EMPTY)
2834 return;
2836 if (!force && !command_queue_is_full()
2837 && current_tick - TAGCACHE_COMMAND_QUEUE_COMMIT_DELAY
2838 < command_queue_timestamp)
2840 return;
2843 mutex_lock(&command_queue_mutex);
2845 if ( (masterfd = open_master_fd(&myhdr, true)) < 0)
2846 return;
2848 while (command_queue_ridx != command_queue_widx)
2850 struct tagcache_command_entry *ce = &command_queue[command_queue_ridx];
2852 switch (ce->command)
2854 case CMD_UPDATE_MASTER_HEADER:
2856 close(masterfd);
2857 update_master_header();
2859 /* Re-open the masterfd. */
2860 if ( (masterfd = open_master_fd(&myhdr, true)) < 0)
2861 return;
2863 break;
2865 case CMD_UPDATE_NUMERIC:
2867 modify_numeric_entry(masterfd, ce->idx_id, ce->tag, ce->data);
2868 break;
2872 if (++command_queue_ridx >= TAGCACHE_COMMAND_QUEUE_LENGTH)
2873 command_queue_ridx = 0;
2876 close(masterfd);
2878 mutex_unlock(&command_queue_mutex);
2881 static void queue_command(int cmd, long idx_id, int tag, long data)
2883 while (1)
2885 int next;
2887 mutex_lock(&command_queue_mutex);
2888 next = command_queue_widx + 1;
2889 if (next >= TAGCACHE_COMMAND_QUEUE_LENGTH)
2890 next = 0;
2892 /* Make sure queue is not full. */
2893 if (next != command_queue_ridx)
2895 struct tagcache_command_entry *ce = &command_queue[command_queue_widx];
2897 ce->command = cmd;
2898 ce->idx_id = idx_id;
2899 ce->tag = tag;
2900 ce->data = data;
2902 command_queue_widx = next;
2903 command_queue_timestamp = current_tick;
2904 mutex_unlock(&command_queue_mutex);
2905 break;
2908 /* Queue is full, try again later... */
2909 mutex_unlock(&command_queue_mutex);
2910 sleep(1);
2914 long tagcache_increase_serial(void)
2916 long old;
2918 if (!tc_stat.ready)
2919 return -2;
2921 while (read_lock)
2922 sleep(1);
2924 old = current_tcmh.serial++;
2925 queue_command(CMD_UPDATE_MASTER_HEADER, 0, 0, 0);
2927 return old;
2930 void tagcache_update_numeric(int idx_id, int tag, long data)
2932 queue_command(CMD_UPDATE_NUMERIC, idx_id, tag, data);
2935 long tagcache_get_serial(void)
2937 return current_tcmh.serial;
2940 long tagcache_get_commitid(void)
2942 return current_tcmh.commitid;
2945 static bool write_tag(int fd, const char *tagstr, const char *datastr)
2947 char buf[512];
2948 int i;
2950 snprintf(buf, sizeof buf, "%s=\"", tagstr);
2951 for (i = strlen(buf); i < (long)sizeof(buf)-3; i++)
2953 if (*datastr == '\0')
2954 break;
2956 if (*datastr == '"')
2958 buf[i] = '\\';
2959 datastr++;
2960 continue;
2963 buf[i] = *(datastr++);
2966 strcpy(&buf[i], "\" ");
2968 write(fd, buf, i + 2);
2970 return true;
2973 static bool read_tag(char *dest, long size,
2974 const char *src, const char *tagstr)
2976 int pos;
2977 char current_tag[32];
2979 while (*src != '\0')
2981 /* Skip all whitespace */
2982 while (*src == ' ')
2983 src++;
2985 if (*src == '\0')
2986 break;
2988 pos = 0;
2989 /* Read in tag name */
2990 while (*src != '=' && *src != ' ')
2992 current_tag[pos] = *src;
2993 src++;
2994 pos++;
2996 if (*src == '\0' || pos >= (long)sizeof(current_tag))
2997 return false;
2999 current_tag[pos] = '\0';
3001 /* Read in tag data */
3003 /* Find the start. */
3004 while (*src != '"' && *src != '\0')
3005 src++;
3007 if (*src == '\0' || *(++src) == '\0')
3008 return false;
3010 /* Read the data. */
3011 for (pos = 0; pos < size; pos++)
3013 if (*src == '\0')
3014 break;
3016 if (*src == '\\' && *(src+1) == '"')
3018 dest[pos] = '"';
3019 src += 2;
3020 continue;
3023 dest[pos] = *src;
3025 if (*src == '"')
3027 src++;
3028 break;
3031 if (*src == '\0')
3032 break;
3034 src++;
3036 dest[pos] = '\0';
3038 if (!strcasecmp(tagstr, current_tag))
3039 return true;
3042 return false;
3045 static int parse_changelog_line(int line_n, const char *buf, void *parameters)
3047 struct index_entry idx;
3048 char tag_data[TAG_MAXLEN+32];
3049 int idx_id;
3050 long masterfd = (long)parameters;
3051 const int import_tags[] = { tag_playcount, tag_rating, tag_playtime, tag_lastplayed,
3052 tag_commitid };
3053 int i;
3054 (void)line_n;
3056 if (*buf == '#')
3057 return 0;
3059 logf("%d/%s", line_n, buf);
3060 if (!read_tag(tag_data, sizeof tag_data, buf, "filename"))
3062 logf("filename missing");
3063 logf("-> %s", buf);
3064 return 0;
3067 idx_id = find_index(tag_data);
3068 if (idx_id < 0)
3070 logf("entry not found");
3071 return 0;
3074 if (!get_index(masterfd, idx_id, &idx, false))
3076 logf("failed to retrieve index entry");
3077 return 0;
3080 /* Stop if tag has already been modified. */
3081 if (idx.flag & FLAG_DIRTYNUM)
3082 return 0;
3084 logf("import: %s", tag_data);
3086 idx.flag |= FLAG_DIRTYNUM;
3087 for (i = 0; i < (long)(sizeof(import_tags)/sizeof(import_tags[0])); i++)
3089 int data;
3091 if (!read_tag(tag_data, sizeof tag_data, buf,
3092 tagcache_tag_to_str(import_tags[i])))
3094 continue;
3097 data = atoi(tag_data);
3098 if (data < 0)
3099 continue;
3101 idx.tag_seek[import_tags[i]] = data;
3103 if (import_tags[i] == tag_lastplayed && data > current_tcmh.serial)
3104 current_tcmh.serial = data;
3105 else if (import_tags[i] == tag_commitid && data >= current_tcmh.commitid)
3106 current_tcmh.commitid = data + 1;
3109 return write_index(masterfd, idx_id, &idx) ? 0 : -5;
3112 #ifndef __PCTOOL__
3113 bool tagcache_import_changelog(void)
3115 struct master_header myhdr;
3116 struct tagcache_header tch;
3117 int clfd;
3118 long masterfd;
3119 char buf[2048];
3121 if (!tc_stat.ready)
3122 return false;
3124 while (read_lock)
3125 sleep(1);
3127 clfd = open(TAGCACHE_FILE_CHANGELOG, O_RDONLY);
3128 if (clfd < 0)
3130 logf("failure to open changelog");
3131 return false;
3134 if ( (masterfd = open_master_fd(&myhdr, true)) < 0)
3136 close(clfd);
3137 return false;
3140 write_lock++;
3142 filenametag_fd = open_tag_fd(&tch, tag_filename, false);
3144 fast_readline(clfd, buf, sizeof buf, (long *)masterfd,
3145 parse_changelog_line);
3147 close(clfd);
3148 close(masterfd);
3150 if (filenametag_fd >= 0)
3151 close(filenametag_fd);
3153 write_lock--;
3155 update_master_header();
3157 return true;
3159 #endif
3161 bool tagcache_create_changelog(struct tagcache_search *tcs)
3163 struct master_header myhdr;
3164 struct index_entry idx;
3165 char buf[TAG_MAXLEN+32];
3166 char temp[32];
3167 int clfd;
3168 int i, j;
3170 if (!tc_stat.ready)
3171 return false;
3173 if (!tagcache_search(tcs, tag_filename))
3174 return false;
3176 /* Initialize the changelog */
3177 clfd = open(TAGCACHE_FILE_CHANGELOG, O_WRONLY | O_CREAT | O_TRUNC);
3178 if (clfd < 0)
3180 logf("failure to open changelog");
3181 return false;
3184 if (tcs->masterfd < 0)
3186 if ( (tcs->masterfd = open_master_fd(&myhdr, false)) < 0)
3187 return false;
3189 else
3191 lseek(tcs->masterfd, 0, SEEK_SET);
3192 ecread(tcs->masterfd, &myhdr, 1, master_header_ec, tc_stat.econ);
3195 write(clfd, "## Changelog version 1\n", 23);
3197 for (i = 0; i < myhdr.tch.entry_count; i++)
3199 if (ecread(tcs->masterfd, &idx, 1, index_entry_ec, tc_stat.econ)
3200 != sizeof(struct index_entry))
3202 logf("read error #9");
3203 tagcache_search_finish(tcs);
3204 close(clfd);
3205 return false;
3208 /* Skip until the entry found has been modified. */
3209 if (! (idx.flag & FLAG_DIRTYNUM) )
3210 continue;
3212 /* Skip deleted entries too. */
3213 if (idx.flag & FLAG_DELETED)
3214 continue;
3216 /* Now retrieve all tags. */
3217 for (j = 0; j < TAG_COUNT; j++)
3219 if (tagcache_is_numeric_tag(j))
3221 snprintf(temp, sizeof temp, "%d", idx.tag_seek[j]);
3222 write_tag(clfd, tagcache_tag_to_str(j), temp);
3223 continue;
3226 tcs->type = j;
3227 tagcache_retrieve(tcs, i, tcs->type, buf, sizeof buf);
3228 write_tag(clfd, tagcache_tag_to_str(j), buf);
3231 write(clfd, "\n", 1);
3232 do_timed_yield();
3235 close(clfd);
3237 tagcache_search_finish(tcs);
3239 return true;
3242 static bool delete_entry(long idx_id)
3244 int fd;
3245 int tag, i;
3246 struct index_entry idx, myidx;
3247 struct master_header myhdr;
3248 char buf[TAG_MAXLEN+32];
3249 int in_use[TAG_COUNT];
3251 logf("delete_entry(): %ld", idx_id);
3253 #ifdef HAVE_TC_RAMCACHE
3254 /* At first mark the entry removed from ram cache. */
3255 if (tc_stat.ramcache)
3256 hdr->indices[idx_id].flag |= FLAG_DELETED;
3257 #endif
3259 if ( (fd = open_master_fd(&myhdr, true) ) < 0)
3260 return false;
3262 lseek(fd, idx_id * sizeof(struct index_entry), SEEK_CUR);
3263 if (ecread(fd, &myidx, 1, index_entry_ec, tc_stat.econ)
3264 != sizeof(struct index_entry))
3266 logf("delete_entry(): read error");
3267 close(fd);
3268 return false;
3271 myidx.flag |= FLAG_DELETED;
3272 lseek(fd, -sizeof(struct index_entry), SEEK_CUR);
3273 if (ecwrite(fd, &myidx, 1, index_entry_ec, tc_stat.econ)
3274 != sizeof(struct index_entry))
3276 logf("delete_entry(): write_error");
3277 close(fd);
3278 return false;
3281 /* Now check which tags are no longer in use (if any) */
3282 for (tag = 0; tag < TAG_COUNT; tag++)
3283 in_use[tag] = 0;
3285 lseek(fd, sizeof(struct master_header), SEEK_SET);
3286 for (i = 0; i < myhdr.tch.entry_count; i++)
3288 struct index_entry *idxp;
3290 #ifdef HAVE_TC_RAMCACHE
3291 /* Use RAM DB if available for greater speed */
3292 if (tc_stat.ramcache)
3293 idxp = &hdr->indices[i];
3294 else
3295 #endif
3297 if (ecread(fd, &idx, 1, index_entry_ec, tc_stat.econ)
3298 != sizeof(struct index_entry))
3300 logf("delete_entry(): read error #2");
3301 close(fd);
3302 return false;
3304 idxp = &idx;
3307 if (idxp->flag & FLAG_DELETED)
3308 continue;
3310 for (tag = 0; tag < TAG_COUNT; tag++)
3312 if (tagcache_is_numeric_tag(tag))
3313 continue;
3315 if (idxp->tag_seek[tag] == myidx.tag_seek[tag])
3316 in_use[tag]++;
3320 close(fd);
3322 /* Now delete all tags no longer in use. */
3323 for (tag = 0; tag < TAG_COUNT; tag++)
3325 if (tagcache_is_numeric_tag(tag))
3326 continue;
3328 if (in_use[tag])
3330 logf("in use: %d/%d", tag, in_use[tag]);
3331 continue;
3334 #ifdef HAVE_TC_RAMCACHE
3335 /* Delete from ram. */
3336 if (tc_stat.ramcache && tag != tag_filename)
3338 struct tagfile_entry *tagentry = get_tag(&myidx, tag);
3339 tagentry->tag_data[0] = '\0';
3341 #endif
3343 /* Open the index file, which contains the tag names. */
3344 snprintf(buf, sizeof buf, TAGCACHE_FILE_INDEX, tag);
3345 fd = open(buf, O_RDWR);
3347 if (fd < 0)
3349 logf("open failed");
3350 return false;
3353 /* Skip the header block */
3354 lseek(fd, myidx.tag_seek[tag] + sizeof(struct tagfile_entry), SEEK_SET);
3356 /* Debug, print 10 first characters of the tag
3357 read(fd, buf, 10);
3358 buf[10]='\0';
3359 logf("TAG:%s", buf);
3360 lseek(fd, -10, SEEK_CUR);
3363 /* Write first data byte in tag as \0 */
3364 write(fd, "", 1);
3366 /* Now tag data has been removed */
3367 close(fd);
3370 return true;
3373 #ifndef __PCTOOL__
3375 * Returns true if there is an event waiting in the queue
3376 * that requires the current operation to be aborted.
3378 static bool check_event_queue(void)
3380 struct event ev;
3382 queue_wait_w_tmo(&tagcache_queue, &ev, 0);
3383 switch (ev.id)
3385 case Q_STOP_SCAN:
3386 case SYS_POWEROFF:
3387 case SYS_USB_CONNECTED:
3388 /* Put the event back into the queue. */
3389 queue_post(&tagcache_queue, ev.id, ev.data);
3390 return true;
3393 return false;
3395 #endif
3397 #ifdef HAVE_TC_RAMCACHE
3398 static bool allocate_tagcache(void)
3400 struct master_header tcmh;
3401 int fd;
3403 /* Load the header. */
3404 if ( (fd = open_master_fd(&tcmh, false)) < 0)
3406 hdr = NULL;
3407 return false;
3410 close(fd);
3412 /**
3413 * Now calculate the required cache size plus
3414 * some extra space for alignment fixes.
3416 tc_stat.ramcache_allocated = tcmh.tch.datasize + 128 + TAGCACHE_RESERVE +
3417 sizeof(struct ramcache_header) + TAG_COUNT*sizeof(void *);
3418 hdr = buffer_alloc(tc_stat.ramcache_allocated + 128);
3419 memset(hdr, 0, sizeof(struct ramcache_header));
3420 memcpy(&hdr->h, &tcmh, sizeof(struct master_header));
3421 hdr->indices = (struct index_entry *)(hdr + 1);
3422 logf("tagcache: %d bytes allocated.", tc_stat.ramcache_allocated);
3424 return true;
3427 # ifdef HAVE_EEPROM_SETTINGS
3428 static bool tagcache_dumpload(void)
3430 struct statefile_header shdr;
3431 int fd, rc;
3432 long offpos;
3433 int i;
3435 fd = open(TAGCACHE_STATEFILE, O_RDONLY);
3436 if (fd < 0)
3438 logf("no tagcache statedump");
3439 return false;
3442 /* Check the statefile memory placement */
3443 hdr = buffer_alloc(0);
3444 rc = read(fd, &shdr, sizeof(struct statefile_header));
3445 if (rc != sizeof(struct statefile_header)
3446 /* || (long)hdr != (long)shdr.hdr */)
3448 logf("incorrect statefile");
3449 hdr = NULL;
3450 close(fd);
3451 return false;
3454 offpos = (long)hdr - (long)shdr.hdr;
3456 /* Lets allocate real memory and load it */
3457 hdr = buffer_alloc(shdr.tc_stat.ramcache_allocated);
3458 rc = read(fd, hdr, shdr.tc_stat.ramcache_allocated);
3459 close(fd);
3461 if (rc != shdr.tc_stat.ramcache_allocated)
3463 logf("read failure!");
3464 hdr = NULL;
3465 return false;
3468 memcpy(&tc_stat, &shdr.tc_stat, sizeof(struct tagcache_stat));
3470 /* Now fix the pointers */
3471 hdr->indices = (struct index_entry *)((long)hdr->indices + offpos);
3472 for (i = 0; i < TAG_COUNT; i++)
3473 hdr->tags[i] += offpos;
3475 return true;
3478 static bool tagcache_dumpsave(void)
3480 struct statefile_header shdr;
3481 int fd;
3483 if (!tc_stat.ramcache)
3484 return false;
3486 fd = open(TAGCACHE_STATEFILE, O_WRONLY | O_CREAT | O_TRUNC);
3487 if (fd < 0)
3489 logf("failed to create a statedump");
3490 return false;
3493 /* Create the header */
3494 shdr.hdr = hdr;
3495 memcpy(&shdr.tc_stat, &tc_stat, sizeof(struct tagcache_stat));
3496 write(fd, &shdr, sizeof(struct statefile_header));
3498 /* And dump the data too */
3499 write(fd, hdr, tc_stat.ramcache_allocated);
3500 close(fd);
3502 return true;
3504 # endif
3506 static bool load_tagcache(void)
3508 struct tagcache_header *tch;
3509 long bytesleft = tc_stat.ramcache_allocated;
3510 struct index_entry *idx;
3511 int rc, fd;
3512 char *p;
3513 int i, tag;
3515 # ifdef HAVE_DIRCACHE
3516 while (dircache_is_initializing())
3517 sleep(1);
3518 # endif
3520 logf("loading tagcache to ram...");
3522 fd = open(TAGCACHE_FILE_MASTER, O_RDONLY);
3523 if (fd < 0)
3525 logf("tagcache open failed");
3526 return false;
3529 if (ecread(fd, &hdr->h, 1, master_header_ec, tc_stat.econ)
3530 != sizeof(struct master_header)
3531 || hdr->h.tch.magic != TAGCACHE_MAGIC)
3533 logf("incorrect header");
3534 return false;
3537 idx = hdr->indices;
3539 /* Load the master index table. */
3540 for (i = 0; i < hdr->h.tch.entry_count; i++)
3542 rc = ecread(fd, idx, 1, index_entry_ec, tc_stat.econ);
3543 if (rc != sizeof(struct index_entry))
3545 logf("read error #10");
3546 close(fd);
3547 return false;
3550 bytesleft -= sizeof(struct index_entry);
3551 if (bytesleft < 0 || ((long)idx - (long)hdr->indices) >= tc_stat.ramcache_allocated)
3553 logf("too big tagcache.");
3554 close(fd);
3555 return false;
3558 idx++;
3561 close(fd);
3563 /* Load the tags. */
3564 p = (char *)idx;
3565 for (tag = 0; tag < TAG_COUNT; tag++)
3567 struct tagfile_entry *fe;
3568 char buf[TAG_MAXLEN+32];
3570 if (tagcache_is_numeric_tag(tag))
3571 continue ;
3573 //p = ((void *)p+1);
3574 p = (char *)((long)p & ~0x03) + 0x04;
3575 hdr->tags[tag] = p;
3577 /* Check the header. */
3578 tch = (struct tagcache_header *)p;
3579 p += sizeof(struct tagcache_header);
3581 if ( (fd = open_tag_fd(tch, tag, false)) < 0)
3582 return false;
3584 for (hdr->entry_count[tag] = 0;
3585 hdr->entry_count[tag] < tch->entry_count;
3586 hdr->entry_count[tag]++)
3588 long pos;
3590 if (do_timed_yield())
3592 /* Abort if we got a critical event in queue */
3593 if (check_event_queue())
3594 return false;
3597 fe = (struct tagfile_entry *)p;
3598 pos = lseek(fd, 0, SEEK_CUR);
3599 rc = ecread(fd, fe, 1, tagfile_entry_ec, tc_stat.econ);
3600 if (rc != sizeof(struct tagfile_entry))
3602 /* End of lookup table. */
3603 logf("read error #11");
3604 close(fd);
3605 return false;
3608 /* We have a special handling for the filename tags. */
3609 if (tag == tag_filename)
3611 # ifdef HAVE_DIRCACHE
3612 const struct dirent *dc;
3613 # endif
3615 // FIXME: This is wrong!
3616 // idx = &hdr->indices[hdr->entry_count[i]];
3617 idx = &hdr->indices[fe->idx_id];
3619 if (fe->tag_length >= (long)sizeof(buf)-1)
3621 read(fd, buf, 10);
3622 buf[10] = '\0';
3623 logf("TAG:%s", buf);
3624 logf("too long filename");
3625 close(fd);
3626 return false;
3629 rc = read(fd, buf, fe->tag_length);
3630 if (rc != fe->tag_length)
3632 logf("read error #12");
3633 close(fd);
3634 return false;
3637 /* Check if the entry has already been removed */
3638 if (idx->flag & FLAG_DELETED)
3639 continue;
3641 /* This flag must not be used yet. */
3642 if (idx->flag & FLAG_DIRCACHE)
3644 logf("internal error!");
3645 close(fd);
3646 return false;
3649 if (idx->tag_seek[tag] != pos)
3651 logf("corrupt data structures!");
3652 close(fd);
3653 return false;
3656 # ifdef HAVE_DIRCACHE
3657 if (dircache_is_enabled())
3659 dc = dircache_get_entry_ptr(buf);
3660 if (dc == NULL)
3662 logf("Entry no longer valid.");
3663 logf("-> %s", buf);
3664 delete_entry(fe->idx_id);
3665 continue ;
3668 idx->flag |= FLAG_DIRCACHE;
3669 FLAG_SET_ATTR(idx->flag, fe->idx_id);
3670 idx->tag_seek[tag_filename] = (long)dc;
3672 else
3673 # endif
3676 # if 0 /* Maybe we could enable this for flash players. Too slow otherwise. */
3677 /* Check if entry has been removed. */
3678 if (global_settings.tagcache_autoupdate)
3680 int testfd;
3682 testfd = open(buf, O_RDONLY);
3683 if (testfd < 0)
3685 logf("Entry no longer valid.");
3686 logf("-> %s", buf);
3687 delete_entry(fe->idx_id);
3688 continue;
3690 close(testfd);
3692 # endif
3695 continue ;
3698 bytesleft -= sizeof(struct tagfile_entry) + fe->tag_length;
3699 if (bytesleft < 0)
3701 logf("too big tagcache #2");
3702 logf("tl: %d", fe->tag_length);
3703 logf("bl: %ld", bytesleft);
3704 close(fd);
3705 return false;
3708 p = fe->tag_data;
3709 rc = read(fd, fe->tag_data, fe->tag_length);
3710 p += rc;
3712 if (rc != fe->tag_length)
3714 logf("read error #13");
3715 logf("rc=0x%04x", rc); // 0x431
3716 logf("len=0x%04x", fe->tag_length); // 0x4000
3717 logf("pos=0x%04lx", lseek(fd, 0, SEEK_CUR)); // 0x433
3718 logf("tag=0x%02x", tag); // 0x00
3719 close(fd);
3720 return false;
3723 close(fd);
3726 tc_stat.ramcache_used = tc_stat.ramcache_allocated - bytesleft;
3727 logf("tagcache loaded into ram!");
3729 return true;
3731 #endif /* HAVE_TC_RAMCACHE */
3733 static bool check_deleted_files(void)
3735 int fd, testfd;
3736 char buf[TAG_MAXLEN+32];
3737 struct tagfile_entry tfe;
3739 logf("reverse scan...");
3740 snprintf(buf, sizeof buf, TAGCACHE_FILE_INDEX, tag_filename);
3741 fd = open(buf, O_RDONLY);
3743 if (fd < 0)
3745 logf("%s open fail", buf);
3746 return false;
3749 lseek(fd, sizeof(struct tagcache_header), SEEK_SET);
3750 while (ecread(fd, &tfe, 1, tagfile_entry_ec, tc_stat.econ)
3751 == sizeof(struct tagfile_entry)
3752 #ifndef __PCTOOL__
3753 && !check_event_queue()
3754 #endif
3757 if (tfe.tag_length >= (long)sizeof(buf)-1)
3759 logf("too long tag");
3760 close(fd);
3761 return false;
3764 if (read(fd, buf, tfe.tag_length) != tfe.tag_length)
3766 logf("read error #14");
3767 close(fd);
3768 return false;
3771 /* Check if the file has already deleted from the db. */
3772 if (*buf == '\0')
3773 continue;
3775 /* Now check if the file exists. */
3776 testfd = open(buf, O_RDONLY);
3777 if (testfd < 0)
3779 logf("Entry no longer valid.");
3780 logf("-> %s / %d", buf, tfe.tag_length);
3781 delete_entry(tfe.idx_id);
3783 close(testfd);
3786 close(fd);
3788 logf("done");
3790 return true;
3793 static bool check_dir(const char *dirname)
3795 DIR *dir;
3796 int len;
3797 int success = false;
3799 dir = opendir(dirname);
3800 if (!dir)
3802 logf("tagcache: opendir() failed");
3803 return false;
3806 /* Recursively scan the dir. */
3807 #ifdef __PCTOOL__
3808 while (1)
3809 #else
3810 while (!check_event_queue())
3811 #endif
3813 struct dirent *entry;
3815 entry = readdir(dir);
3817 if (entry == NULL)
3819 success = true;
3820 break ;
3823 if (!strcmp((char *)entry->d_name, ".") ||
3824 !strcmp((char *)entry->d_name, ".."))
3825 continue;
3827 yield();
3829 len = strlen(curpath);
3830 snprintf(&curpath[len], curpath_size - len, "/%s",
3831 entry->d_name);
3833 processed_dir_count++;
3834 if (entry->attribute & ATTR_DIRECTORY)
3835 check_dir(curpath);
3836 else
3837 #if defined(HAVE_TC_RAMCACHE) && defined(HAVE_DIRCACHE)
3838 add_tagcache(curpath, dir->internal_entry);
3839 #else
3840 add_tagcache(curpath);
3841 #endif
3843 curpath[len] = '\0';
3846 closedir(dir);
3848 return success;
3851 void build_tagcache(const char *path)
3853 struct tagcache_header header;
3854 bool ret;
3856 curpath[0] = '\0';
3857 data_size = 0;
3858 total_entry_count = 0;
3859 processed_dir_count = 0;
3861 #ifdef HAVE_DIRCACHE
3862 while (dircache_is_initializing())
3863 sleep(1);
3864 #endif
3866 logf("updating tagcache");
3868 cachefd = open(TAGCACHE_FILE_TEMP, O_RDONLY);
3869 if (cachefd >= 0)
3871 logf("skipping, cache already waiting for commit");
3872 close(cachefd);
3873 return ;
3876 cachefd = open(TAGCACHE_FILE_TEMP, O_RDWR | O_CREAT | O_TRUNC);
3877 if (cachefd < 0)
3879 logf("master file open failed: %s", TAGCACHE_FILE_TEMP);
3880 return ;
3883 filenametag_fd = open_tag_fd(&header, tag_filename, false);
3885 cpu_boost(true);
3887 logf("Scanning files...");
3888 /* Scan for new files. */
3889 memset(&header, 0, sizeof(struct tagcache_header));
3890 write(cachefd, &header, sizeof(struct tagcache_header));
3892 if (strcmp("/", path) != 0)
3893 strcpy(curpath, path);
3894 ret = check_dir(path);
3896 /* Write the header. */
3897 header.magic = TAGCACHE_MAGIC;
3898 header.datasize = data_size;
3899 header.entry_count = total_entry_count;
3900 lseek(cachefd, 0, SEEK_SET);
3901 write(cachefd, &header, sizeof(struct tagcache_header));
3902 close(cachefd);
3904 if (filenametag_fd >= 0)
3906 close(filenametag_fd);
3907 filenametag_fd = -1;
3910 if (!ret)
3912 logf("Aborted.");
3913 cpu_boost(false);
3914 return ;
3917 /* Commit changes to the database. */
3918 #ifdef __PCTOOL__
3919 allocate_tempbuf();
3920 #endif
3921 if (commit())
3923 remove(TAGCACHE_FILE_TEMP);
3924 logf("tagcache built!");
3926 #ifdef __PCTOOL__
3927 free_tempbuf();
3928 #endif
3930 #ifdef HAVE_TC_RAMCACHE
3931 if (hdr)
3933 /* Import runtime statistics if we just initialized the db. */
3934 if (hdr->h.serial == 0)
3935 queue_post(&tagcache_queue, Q_IMPORT_CHANGELOG, 0);
3937 #endif
3939 cpu_boost(false);
3942 #ifdef HAVE_TC_RAMCACHE
3943 static void load_ramcache(void)
3945 if (!hdr)
3946 return ;
3948 cpu_boost(true);
3950 /* At first we should load the cache (if exists). */
3951 tc_stat.ramcache = load_tagcache();
3953 if (!tc_stat.ramcache)
3955 /* If loading failed, it must indicate some problem with the db
3956 * so disable it entirely to prevent further issues. */
3957 tc_stat.ready = false;
3958 hdr = NULL;
3961 cpu_boost(false);
3964 void tagcache_unload_ramcache(void)
3966 tc_stat.ramcache = false;
3967 /* Just to make sure there is no statefile present. */
3968 // remove(TAGCACHE_STATEFILE);
3970 #endif
3972 #ifndef __PCTOOL__
3973 static void tagcache_thread(void)
3975 struct event ev;
3976 bool check_done = false;
3978 /* If the previous cache build/update was interrupted, commit
3979 * the changes first in foreground. */
3980 cpu_boost(true);
3981 allocate_tempbuf();
3982 commit();
3983 free_tempbuf();
3985 #ifdef HAVE_TC_RAMCACHE
3986 # ifdef HAVE_EEPROM_SETTINGS
3987 if (firmware_settings.initialized && firmware_settings.disk_clean)
3988 check_done = tagcache_dumpload();
3990 remove(TAGCACHE_STATEFILE);
3991 # endif
3993 /* Allocate space for the tagcache if found on disk. */
3994 if (global_settings.tagcache_ram && !tc_stat.ramcache)
3995 allocate_tagcache();
3996 #endif
3998 cpu_boost(false);
3999 tc_stat.initialized = true;
4001 /* Don't delay bootup with the header check but do it on background. */
4002 sleep(HZ);
4003 tc_stat.ready = check_all_headers();
4004 tc_stat.readyvalid = true;
4006 while (1)
4008 run_command_queue(false);
4010 queue_wait_w_tmo(&tagcache_queue, &ev, HZ);
4012 switch (ev.id)
4014 case Q_IMPORT_CHANGELOG:
4015 tagcache_import_changelog();
4016 break;
4018 case Q_REBUILD:
4019 remove_files();
4020 build_tagcache("/");
4021 break;
4023 case Q_UPDATE:
4024 build_tagcache("/");
4025 #ifdef HAVE_TC_RAMCACHE
4026 load_ramcache();
4027 #endif
4028 check_deleted_files();
4029 break ;
4031 case Q_START_SCAN:
4032 check_done = false;
4033 case SYS_TIMEOUT:
4034 if (check_done || !tc_stat.ready)
4035 break ;
4037 #ifdef HAVE_TC_RAMCACHE
4038 if (!tc_stat.ramcache && global_settings.tagcache_ram)
4040 load_ramcache();
4041 if (tc_stat.ramcache && global_settings.tagcache_autoupdate)
4042 build_tagcache("/");
4044 else
4045 #endif
4046 if (global_settings.tagcache_autoupdate)
4048 build_tagcache("/");
4049 /* Don't do auto removal without dircache (very slow). */
4050 #ifdef HAVE_DIRCACHE
4051 if (dircache_is_enabled())
4052 check_deleted_files();
4053 #endif
4056 logf("tagcache check done");
4058 check_done = true;
4059 break ;
4061 case Q_STOP_SCAN:
4062 break ;
4064 case SYS_POWEROFF:
4065 break ;
4067 #ifndef SIMULATOR
4068 case SYS_USB_CONNECTED:
4069 logf("USB: TagCache");
4070 usb_acknowledge(SYS_USB_CONNECTED_ACK);
4071 usb_wait_for_disconnect(&tagcache_queue);
4072 break ;
4073 #endif
4078 bool tagcache_prepare_shutdown(void)
4080 if (tagcache_get_commit_step() > 0)
4081 return false;
4083 tagcache_stop_scan();
4084 while (read_lock || write_lock)
4085 sleep(1);
4087 return true;
4090 void tagcache_shutdown(void)
4092 /* Flush the command queue. */
4093 run_command_queue(true);
4095 #ifdef HAVE_EEPROM_SETTINGS
4096 if (tc_stat.ramcache)
4097 tagcache_dumpsave();
4098 #endif
4101 static int get_progress(void)
4103 int total_count = -1;
4105 #ifdef HAVE_DIRCACHE
4106 if (dircache_is_enabled())
4108 total_count = dircache_get_entry_count();
4110 else
4111 #endif
4112 #ifdef HAVE_TC_RAMCACHE
4114 if (hdr && tc_stat.ramcache)
4115 total_count = hdr->h.tch.entry_count;
4117 #endif
4119 if (total_count < 0)
4120 return -1;
4122 return processed_dir_count * 100 / total_count;
4125 struct tagcache_stat* tagcache_get_stat(void)
4127 tc_stat.progress = get_progress();
4128 tc_stat.processed_entries = processed_dir_count;
4130 return &tc_stat;
4133 void tagcache_start_scan(void)
4135 queue_post(&tagcache_queue, Q_START_SCAN, 0);
4138 bool tagcache_update(void)
4140 if (!tc_stat.ready)
4141 return false;
4143 queue_post(&tagcache_queue, Q_UPDATE, 0);
4144 return false;
4147 bool tagcache_rebuild()
4149 queue_post(&tagcache_queue, Q_REBUILD, 0);
4150 return false;
4153 void tagcache_stop_scan(void)
4155 queue_post(&tagcache_queue, Q_STOP_SCAN, 0);
4158 #ifdef HAVE_TC_RAMCACHE
4159 bool tagcache_is_ramcache(void)
4161 return tc_stat.ramcache;
4163 #endif
4165 #endif /* !__PCTOOL__ */
4168 void tagcache_init(void)
4170 memset(&tc_stat, 0, sizeof(struct tagcache_stat));
4171 memset(&current_tcmh, 0, sizeof(struct master_header));
4172 filenametag_fd = -1;
4173 write_lock = read_lock = 0;
4175 #ifndef __PCTOOL__
4176 mutex_init(&command_queue_mutex);
4177 queue_init(&tagcache_queue, true);
4178 create_thread(tagcache_thread, tagcache_stack,
4179 sizeof(tagcache_stack), tagcache_thread_name
4180 IF_PRIO(, PRIORITY_BACKGROUND)
4181 IF_COP(, CPU, false));
4182 #else
4183 tc_stat.initialized = true;
4184 allocate_tempbuf();
4185 commit();
4186 free_tempbuf();
4187 tc_stat.ready = check_all_headers();
4188 #endif
4191 #ifdef __PCTOOL__
4192 void tagcache_reverse_scan(void)
4194 logf("Checking for deleted files");
4195 check_deleted_files();
4197 #endif
4199 bool tagcache_is_initialized(void)
4201 return tc_stat.initialized;
4203 bool tagcache_is_usable(void)
4205 return tc_stat.initialized && tc_stat.ready;
4207 int tagcache_get_commit_step(void)
4209 return tc_stat.commit_step;
4211 int tagcache_get_max_commit_step(void)
4213 return (int)(sizeof(sorted_tags)/sizeof(sorted_tags[0]))+1;