Updated Italian translation (FS#7389)
[Rockbox.git] / apps / tagcache.c
blob41138ddf2c1457ea923d411e773f9635102de9b3
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 "dircache.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_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 };
117 /* Numeric tags (we can use these tags with conditional clauses). */
118 static const int numeric_tags[] = { tag_year, 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", "year", "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,
142 /* Tag database structures. */
144 /* Variable-length tag entry in tag files. */
145 struct tagfile_entry {
146 short tag_length; /* Length of the data in bytes including '\0' */
147 short idx_id; /* Corresponding entry location in index file of not unique tags */
148 char tag_data[0]; /* Begin of the tag data */
151 /* Fixed-size tag entry in master db index. */
152 struct index_entry {
153 long tag_seek[TAG_COUNT]; /* Location of tag data or numeric tag data */
154 long flag; /* Status flags */
157 /* Header is the same in every file. */
158 struct tagcache_header {
159 long magic; /* Header version number */
160 long datasize; /* Data size in bytes */
161 long entry_count; /* Number of entries in this file */
164 struct master_header {
165 struct tagcache_header tch;
166 long serial; /* Increasing counting number */
167 long commitid; /* Number of commits so far */
168 long dirty;
171 /* For the endianess correction */
172 static const char *tagfile_entry_ec = "ss";
173 static const char *index_entry_ec = "llllllllllllllllll"; /* (1 + TAG_COUNT) * l */
174 static const char *tagcache_header_ec = "lll";
175 static const char *master_header_ec = "llllll";
177 static struct master_header current_tcmh;
179 #ifdef HAVE_TC_RAMCACHE
180 /* Header is created when loading database to ram. */
181 struct ramcache_header {
182 struct master_header h; /* Header from the master index */
183 struct index_entry *indices; /* Master index file content */
184 char *tags[TAG_COUNT]; /* Tag file content (not including filename tag) */
185 int entry_count[TAG_COUNT]; /* Number of entries in the indices. */
188 # ifdef HAVE_EEPROM_SETTINGS
189 struct statefile_header {
190 struct ramcache_header *hdr;
191 struct tagcache_stat tc_stat;
193 # endif
195 /* Pointer to allocated ramcache_header */
196 static struct ramcache_header *hdr;
197 #endif
199 /**
200 * Full tag entries stored in a temporary file waiting
201 * for commit to the cache. */
202 struct temp_file_entry {
203 long tag_offset[TAG_COUNT];
204 short tag_length[TAG_COUNT];
205 long flag;
207 long data_length;
210 struct tempbuf_id_list {
211 long id;
212 struct tempbuf_id_list *next;
215 struct tempbuf_searchidx {
216 long idx_id;
217 char *str;
218 int seek;
219 struct tempbuf_id_list idlist;
222 /* Lookup buffer for fixing messed up index while after sorting. */
223 static long commit_entry_count;
224 static long lookup_buffer_depth;
225 static struct tempbuf_searchidx **lookup;
227 /* Used when building the temporary file. */
228 static int cachefd = -1, filenametag_fd;
229 static int total_entry_count = 0;
230 static int data_size = 0;
231 static int processed_dir_count;
233 /* Thread safe locking */
234 static volatile int write_lock;
235 static volatile int read_lock;
237 const char* tagcache_tag_to_str(int tag)
239 return tags_str[tag];
242 bool tagcache_is_numeric_tag(int type)
244 int i;
246 for (i = 0; i < (int)(sizeof(numeric_tags)/sizeof(numeric_tags[0])); i++)
248 if (type == numeric_tags[i])
249 return true;
252 return false;
255 bool tagcache_is_unique_tag(int type)
257 int i;
259 for (i = 0; i < (int)(sizeof(unique_tags)/sizeof(unique_tags[0])); i++)
261 if (type == unique_tags[i])
262 return true;
265 return false;
268 bool tagcache_is_sorted_tag(int type)
270 int i;
272 for (i = 0; i < (int)(sizeof(sorted_tags)/sizeof(sorted_tags[0])); i++)
274 if (type == sorted_tags[i])
275 return true;
278 return false;
281 static int open_tag_fd(struct tagcache_header *hdr, int tag, bool write)
283 int fd;
284 char buf[MAX_PATH];
285 int rc;
287 if (tagcache_is_numeric_tag(tag) || tag < 0 || tag >= TAG_COUNT)
288 return -1;
290 snprintf(buf, sizeof buf, TAGCACHE_FILE_INDEX, tag);
292 fd = open(buf, write ? O_RDWR : O_RDONLY);
293 if (fd < 0)
295 logf("tag file open failed: tag=%d write=%d file=%s", tag, write, buf);
296 tc_stat.ready = false;
297 return fd;
300 /* Check the header. */
301 rc = ecread(fd, hdr, 1, tagcache_header_ec, tc_stat.econ);
302 if (hdr->magic != TAGCACHE_MAGIC || rc != sizeof(struct tagcache_header))
304 logf("header error");
305 tc_stat.ready = false;
306 close(fd);
307 return -2;
310 return fd;
313 #ifndef __PCTOOL__
314 static bool do_timed_yield(void)
316 /* Sorting can lock up for quite a while, so yield occasionally */
317 static long wakeup_tick = 0;
318 if (current_tick >= wakeup_tick)
320 wakeup_tick = current_tick + (HZ/4);
321 yield();
322 return true;
324 return false;
326 #endif
328 #if defined(HAVE_TC_RAMCACHE) && defined(HAVE_DIRCACHE)
329 static long find_entry_ram(const char *filename,
330 const struct dircache_entry *dc)
332 static long last_pos = 0;
333 int i;
335 /* Check if we tagcache is loaded into ram. */
336 if (!tc_stat.ramcache)
337 return -1;
339 if (dc == NULL)
340 dc = dircache_get_entry_ptr(filename);
342 if (dc == NULL)
344 logf("tagcache: file not found.");
345 return -1;
348 try_again:
350 if (last_pos > 0)
351 i = last_pos;
352 else
353 i = 0;
355 for (; i < hdr->h.tch.entry_count; i++)
357 if (hdr->indices[i].tag_seek[tag_filename] == (long)dc)
359 last_pos = MAX(0, i - 3);
360 return i;
363 do_timed_yield();
366 if (last_pos > 0)
368 last_pos = 0;
369 goto try_again;
372 return -1;
374 #endif
376 static long find_entry_disk(const char *filename)
378 struct tagcache_header tch;
379 static long last_pos = -1;
380 long pos_history[POS_HISTORY_COUNT];
381 long pos_history_idx = 0;
382 bool found = false;
383 struct tagfile_entry tfe;
384 int fd;
385 char buf[TAG_MAXLEN+32];
386 int i;
387 int pos = -1;
389 if (!tc_stat.ready)
390 return -2;
392 fd = filenametag_fd;
393 if (fd < 0)
395 last_pos = -1;
396 if ( (fd = open_tag_fd(&tch, tag_filename, false)) < 0)
397 return -1;
400 check_again:
402 if (last_pos > 0)
403 lseek(fd, last_pos, SEEK_SET);
404 else
405 lseek(fd, sizeof(struct tagcache_header), SEEK_SET);
407 while (true)
409 pos = lseek(fd, 0, SEEK_CUR);
410 for (i = pos_history_idx-1; i >= 0; i--)
411 pos_history[i+1] = pos_history[i];
412 pos_history[0] = pos;
414 if (ecread(fd, &tfe, 1, tagfile_entry_ec, tc_stat.econ)
415 != sizeof(struct tagfile_entry))
417 break ;
420 if (tfe.tag_length >= (long)sizeof(buf))
422 logf("too long tag #1");
423 close(fd);
424 last_pos = -1;
425 return -2;
428 if (read(fd, buf, tfe.tag_length) != tfe.tag_length)
430 logf("read error #2");
431 close(fd);
432 last_pos = -1;
433 return -3;
436 if (!strcasecmp(filename, buf))
438 last_pos = pos_history[pos_history_idx];
439 found = true;
440 break ;
443 if (pos_history_idx < POS_HISTORY_COUNT - 1)
444 pos_history_idx++;
447 /* Not found? */
448 if (!found)
450 if (last_pos > 0)
452 last_pos = -1;
453 logf("seek again");
454 goto check_again;
457 if (fd != filenametag_fd)
458 close(fd);
459 return -4;
462 if (fd != filenametag_fd)
463 close(fd);
465 return tfe.idx_id;
468 static int find_index(const char *filename)
470 long idx_id = -1;
472 #if defined(HAVE_TC_RAMCACHE) && defined(HAVE_DIRCACHE)
473 if (tc_stat.ramcache && dircache_is_enabled())
474 idx_id = find_entry_ram(filename, NULL);
475 #endif
477 if (idx_id < 0)
478 idx_id = find_entry_disk(filename);
480 return idx_id;
483 bool tagcache_find_index(struct tagcache_search *tcs, const char *filename)
485 int idx_id;
487 if (!tc_stat.ready)
488 return false;
490 idx_id = find_index(filename);
491 if (idx_id < 0)
492 return false;
494 if (!tagcache_search(tcs, tag_filename))
495 return false;
497 tcs->entry_count = 0;
498 tcs->idx_id = idx_id;
500 return true;
503 static bool get_index(int masterfd, int idxid,
504 struct index_entry *idx, bool use_ram)
506 if (idxid < 0)
508 logf("Incorrect idxid: %d", idxid);
509 return false;
512 #ifdef HAVE_TC_RAMCACHE
513 if (tc_stat.ramcache && use_ram)
515 if (hdr->indices[idxid].flag & FLAG_DELETED)
516 return false;
518 memcpy(idx, &hdr->indices[idxid], sizeof(struct index_entry));
519 return true;
521 #else
522 (void)use_ram;
523 #endif
525 lseek(masterfd, idxid * sizeof(struct index_entry)
526 + sizeof(struct master_header), SEEK_SET);
527 if (ecread(masterfd, idx, 1, index_entry_ec, tc_stat.econ)
528 != sizeof(struct index_entry))
530 logf("read error #3");
531 return false;
534 if (idx->flag & FLAG_DELETED)
535 return false;
537 return true;
540 static bool write_index(int masterfd, int idxid, struct index_entry *idx)
542 /* We need to exclude all memory only flags & tags when writing to disk. */
543 if (idx->flag & FLAG_DIRCACHE)
545 logf("memory only flags!");
546 return false;
549 #ifdef HAVE_TC_RAMCACHE
550 /* Only update numeric data. Writing the whole index to RAM by memcpy
551 * destroys dircache pointers!
553 if (tc_stat.ramcache)
555 int tag;
556 struct index_entry *idx_ram = &hdr->indices[idxid];
558 for (tag = 0; tag < TAG_COUNT; tag++)
560 if (tagcache_is_numeric_tag(tag))
562 idx_ram->tag_seek[tag] = idx->tag_seek[tag];
566 /* Don't touch the dircache flag. */
567 idx_ram->flag = idx->flag | (idx_ram->flag & FLAG_DIRCACHE);
569 #endif
571 lseek(masterfd, idxid * sizeof(struct index_entry)
572 + sizeof(struct master_header), SEEK_SET);
573 if (ecwrite(masterfd, idx, 1, index_entry_ec, tc_stat.econ)
574 != sizeof(struct index_entry))
576 logf("write error #3");
577 logf("idxid: %d", idxid);
578 return false;
581 return true;
584 static bool open_files(struct tagcache_search *tcs, int tag)
586 if (tcs->idxfd[tag] < 0)
588 char fn[MAX_PATH];
590 snprintf(fn, sizeof fn, TAGCACHE_FILE_INDEX, tag);
591 tcs->idxfd[tag] = open(fn, O_RDONLY);
594 if (tcs->idxfd[tag] < 0)
596 logf("File not open!");
597 return false;
600 return true;
603 static bool retrieve(struct tagcache_search *tcs, struct index_entry *idx,
604 int tag, char *buf, long size)
606 struct tagfile_entry tfe;
607 long seek;
609 *buf = '\0';
611 if (tagcache_is_numeric_tag(tag))
612 return false;
614 seek = idx->tag_seek[tag];
615 if (seek < 0)
617 logf("Retrieve failed");
618 return false;
621 #ifdef HAVE_TC_RAMCACHE
622 if (tcs->ramsearch)
624 struct tagfile_entry *ep;
626 # ifdef HAVE_DIRCACHE
627 if (tag == tag_filename && idx->flag & FLAG_DIRCACHE)
629 dircache_copy_path((struct dircache_entry *)seek,
630 buf, size);
631 return true;
633 else
634 # endif
635 if (tag != tag_filename)
637 ep = (struct tagfile_entry *)&hdr->tags[tag][seek];
638 strncpy(buf, ep->tag_data, size-1);
640 return true;
643 #endif
645 if (!open_files(tcs, tag))
646 return false;
648 lseek(tcs->idxfd[tag], seek, SEEK_SET);
649 if (ecread(tcs->idxfd[tag], &tfe, 1, tagfile_entry_ec, tc_stat.econ)
650 != sizeof(struct tagfile_entry))
652 logf("read error #5");
653 return false;
656 if (tfe.tag_length >= size)
658 logf("too small buffer");
659 return false;
662 if (read(tcs->idxfd[tag], buf, tfe.tag_length) !=
663 tfe.tag_length)
665 logf("read error #6");
666 return false;
669 buf[tfe.tag_length] = '\0';
671 return true;
674 static long check_virtual_tags(int tag, const struct index_entry *idx)
676 long data = 0;
678 switch (tag)
680 case tag_virt_length_sec:
681 data = (idx->tag_seek[tag_length]/1000) % 60;
682 break;
684 case tag_virt_length_min:
685 data = (idx->tag_seek[tag_length]/1000) / 60;
686 break;
688 case tag_virt_playtime_sec:
689 data = (idx->tag_seek[tag_playtime]/1000) % 60;
690 break;
692 case tag_virt_playtime_min:
693 data = (idx->tag_seek[tag_playtime]/1000) / 60;
694 break;
696 case tag_virt_autoscore:
697 if (idx->tag_seek[tag_length] == 0
698 || idx->tag_seek[tag_playcount] == 0)
700 data = 0;
702 else
704 data = 100 * idx->tag_seek[tag_playtime]
705 / idx->tag_seek[tag_length]
706 / idx->tag_seek[tag_playcount];
708 break;
710 /* How many commits before the file has been added to the DB. */
711 case tag_virt_entryage:
712 data = current_tcmh.commitid - idx->tag_seek[tag_commitid] - 1;
713 break;
715 default:
716 data = idx->tag_seek[tag];
719 return data;
722 long tagcache_get_numeric(const struct tagcache_search *tcs, int tag)
724 struct index_entry idx;
726 if (!tc_stat.ready)
727 return false;
729 if (!tagcache_is_numeric_tag(tag))
730 return -1;
732 if (!get_index(tcs->masterfd, tcs->idx_id, &idx, true))
733 return -2;
735 return check_virtual_tags(tag, &idx);
738 inline static bool str_ends_with(const char *str1, const char *str2)
740 int str_len = strlen(str1);
741 int clause_len = strlen(str2);
743 if (clause_len > str_len)
744 return false;
746 return !strcasecmp(&str1[str_len - clause_len], str2);
749 inline static bool str_oneof(const char *str, const char *list)
751 const char *sep;
752 int l, len = strlen(str);
754 while (*list)
756 sep = strchr(list, '|');
757 l = sep ? (long)sep - (long)list : (int)strlen(list);
758 if ((l==len) && !strncasecmp(str, list, len))
759 return true;
760 list += sep ? l + 1 : l;
763 return false;
766 static bool check_against_clause(long numeric, const char *str,
767 const struct tagcache_search_clause *clause)
769 if (clause->numeric)
771 switch (clause->type)
773 case clause_is:
774 return numeric == clause->numeric_data;
775 case clause_is_not:
776 return numeric != clause->numeric_data;
777 case clause_gt:
778 return numeric > clause->numeric_data;
779 case clause_gteq:
780 return numeric >= clause->numeric_data;
781 case clause_lt:
782 return numeric < clause->numeric_data;
783 case clause_lteq:
784 return numeric <= clause->numeric_data;
785 default:
786 logf("Incorrect numeric tag: %d", clause->type);
789 else
791 switch (clause->type)
793 case clause_is:
794 return !strcasecmp(clause->str, str);
795 case clause_is_not:
796 return strcasecmp(clause->str, str);
797 case clause_gt:
798 return 0>strcasecmp(clause->str, str);
799 case clause_gteq:
800 return 0>=strcasecmp(clause->str, str);
801 case clause_lt:
802 return 0<strcasecmp(clause->str, str);
803 case clause_lteq:
804 return 0<=strcasecmp(clause->str, str);
805 case clause_contains:
806 return (strcasestr(str, clause->str) != NULL);
807 case clause_not_contains:
808 return (strcasestr(str, clause->str) == NULL);
809 case clause_begins_with:
810 return (strcasestr(str, clause->str) == str);
811 case clause_not_begins_with:
812 return (strcasestr(str, clause->str) != str);
813 case clause_ends_with:
814 return str_ends_with(str, clause->str);
815 case clause_not_ends_with:
816 return !str_ends_with(str, clause->str);
817 case clause_oneof:
818 return str_oneof(str, clause->str);
820 default:
821 logf("Incorrect tag: %d", clause->type);
825 return false;
828 static bool check_clauses(struct tagcache_search *tcs,
829 struct index_entry *idx,
830 struct tagcache_search_clause **clause, int count)
832 int i;
834 #ifdef HAVE_TC_RAMCACHE
835 if (tcs->ramsearch)
837 /* Go through all conditional clauses. */
838 for (i = 0; i < count; i++)
840 struct tagfile_entry *tfe;
841 int seek;
842 char buf[256];
843 char *str = NULL;
845 seek = check_virtual_tags(clause[i]->tag, idx);
847 if (!tagcache_is_numeric_tag(clause[i]->tag))
849 if (clause[i]->tag == tag_filename)
851 retrieve(tcs, idx, tag_filename, buf, sizeof buf);
852 str = buf;
854 else
856 tfe = (struct tagfile_entry *)&hdr->tags[clause[i]->tag][seek];
857 str = tfe->tag_data;
861 if (!check_against_clause(seek, str, clause[i]))
862 return false;
865 else
866 #endif
868 /* Check for conditions. */
869 for (i = 0; i < count; i++)
871 struct tagfile_entry tfe;
872 int seek;
873 char str[256];
875 seek = check_virtual_tags(clause[i]->tag, idx);
877 memset(str, 0, sizeof str);
878 if (!tagcache_is_numeric_tag(clause[i]->tag))
880 int fd = tcs->idxfd[clause[i]->tag];
881 lseek(fd, seek, SEEK_SET);
882 ecread(fd, &tfe, 1, tagfile_entry_ec, tc_stat.econ);
883 if (tfe.tag_length >= (int)sizeof(str))
885 logf("Too long tag read!");
886 break ;
889 read(fd, str, tfe.tag_length);
891 /* Check if entry has been deleted. */
892 if (str[0] == '\0')
893 break;
896 if (!check_against_clause(seek, str, clause[i]))
897 return false;
901 return true;
904 bool tagcache_check_clauses(struct tagcache_search *tcs,
905 struct tagcache_search_clause **clause, int count)
907 struct index_entry idx;
909 if (count == 0)
910 return true;
912 if (!get_index(tcs->masterfd, tcs->idx_id, &idx, true))
913 return false;
915 return check_clauses(tcs, &idx, clause, count);
918 static bool add_uniqbuf(struct tagcache_search *tcs, unsigned long id)
920 int i;
922 /* If uniq buffer is not defined we must return true for search to work. */
923 if (tcs->unique_list == NULL
924 || (!tagcache_is_unique_tag(tcs->type)
925 && !tagcache_is_numeric_tag(tcs->type)))
927 return true;
930 for (i = 0; i < tcs->unique_list_count; i++)
932 /* Return false if entry is found. */
933 if (tcs->unique_list[i] == id)
934 return false;
937 if (tcs->unique_list_count < tcs->unique_list_capacity)
939 tcs->unique_list[i] = id;
940 tcs->unique_list_count++;
943 return true;
946 static bool build_lookup_list(struct tagcache_search *tcs)
948 struct index_entry entry;
949 int i;
951 tcs->seek_list_count = 0;
953 #ifdef HAVE_TC_RAMCACHE
954 if (tcs->ramsearch)
956 int j;
958 for (i = tcs->seek_pos; i < hdr->h.tch.entry_count; i++)
960 struct index_entry *idx = &hdr->indices[i];
961 if (tcs->seek_list_count == SEEK_LIST_SIZE)
962 break ;
964 /* Skip deleted files. */
965 if (idx->flag & FLAG_DELETED)
966 continue;
968 /* Go through all filters.. */
969 for (j = 0; j < tcs->filter_count; j++)
971 if (idx->tag_seek[tcs->filter_tag[j]] != tcs->filter_seek[j])
973 break ;
977 if (j < tcs->filter_count)
978 continue ;
980 /* Check for conditions. */
981 if (!check_clauses(tcs, idx, tcs->clause, tcs->clause_count))
982 continue;
984 /* Add to the seek list if not already in uniq buffer. */
985 if (!add_uniqbuf(tcs, idx->tag_seek[tcs->type]))
986 continue;
988 /* Lets add it. */
989 tcs->seek_list[tcs->seek_list_count] = idx->tag_seek[tcs->type];
990 tcs->seek_flags[tcs->seek_list_count] = idx->flag;
991 tcs->seek_list_count++;
994 tcs->seek_pos = i;
996 return tcs->seek_list_count > 0;
998 #endif
1000 lseek(tcs->masterfd, tcs->seek_pos * sizeof(struct index_entry) +
1001 sizeof(struct master_header), SEEK_SET);
1003 while (ecread(tcs->masterfd, &entry, 1, index_entry_ec, tc_stat.econ)
1004 == sizeof(struct index_entry))
1006 if (tcs->seek_list_count == SEEK_LIST_SIZE)
1007 break ;
1009 tcs->seek_pos++;
1011 /* Check if entry has been deleted. */
1012 if (entry.flag & FLAG_DELETED)
1013 continue;
1015 /* Go through all filters.. */
1016 for (i = 0; i < tcs->filter_count; i++)
1018 if (entry.tag_seek[tcs->filter_tag[i]] != tcs->filter_seek[i])
1019 break ;
1022 if (i < tcs->filter_count)
1023 continue ;
1025 /* Check for conditions. */
1026 if (!check_clauses(tcs, &entry, tcs->clause, tcs->clause_count))
1027 continue;
1029 /* Add to the seek list if not already in uniq buffer. */
1030 if (!add_uniqbuf(tcs, entry.tag_seek[tcs->type]))
1031 continue;
1033 /* Lets add it. */
1034 tcs->seek_list[tcs->seek_list_count] = entry.tag_seek[tcs->type];
1035 tcs->seek_flags[tcs->seek_list_count] = entry.flag;
1036 tcs->seek_list_count++;
1038 yield();
1041 return tcs->seek_list_count > 0;
1045 static void remove_files(void)
1047 int i;
1048 char buf[MAX_PATH];
1050 tc_stat.ready = false;
1051 tc_stat.ramcache = false;
1052 tc_stat.econ = false;
1053 remove(TAGCACHE_FILE_MASTER);
1054 for (i = 0; i < TAG_COUNT; i++)
1056 if (tagcache_is_numeric_tag(i))
1057 continue;
1059 snprintf(buf, sizeof buf, TAGCACHE_FILE_INDEX, i);
1060 remove(buf);
1065 static int open_master_fd(struct master_header *hdr, bool write)
1067 int fd;
1068 int rc;
1070 fd = open(TAGCACHE_FILE_MASTER, write ? O_RDWR : O_RDONLY);
1071 if (fd < 0)
1073 logf("master file open failed for R/W");
1074 tc_stat.ready = false;
1075 return fd;
1078 tc_stat.econ = false;
1080 /* Check the header. */
1081 rc = read(fd, hdr, sizeof(struct master_header));
1082 if (hdr->tch.magic == TAGCACHE_MAGIC && rc == sizeof(struct master_header))
1084 /* Success. */
1085 return fd;
1088 /* Trying to read again, this time with endianess correction enabled. */
1089 lseek(fd, 0, SEEK_SET);
1091 rc = ecread(fd, hdr, 1, master_header_ec, true);
1092 if (hdr->tch.magic != TAGCACHE_MAGIC || rc != sizeof(struct master_header))
1094 logf("header error");
1095 tc_stat.ready = false;
1096 close(fd);
1097 return -2;
1100 tc_stat.econ = true;
1102 return fd;
1105 static bool check_all_headers(void)
1107 struct master_header myhdr;
1108 struct tagcache_header tch;
1109 int tag;
1110 int fd;
1112 if ( (fd = open_master_fd(&myhdr, false)) < 0)
1113 return false;
1115 close(fd);
1116 if (myhdr.dirty)
1118 logf("tagcache is dirty!");
1119 return false;
1122 memcpy(&current_tcmh, &myhdr, sizeof(struct master_header));
1124 for (tag = 0; tag < TAG_COUNT; tag++)
1126 if (tagcache_is_numeric_tag(tag))
1127 continue;
1129 if ( (fd = open_tag_fd(&tch, tag, false)) < 0)
1130 return false;
1132 close(fd);
1135 return true;
1138 bool tagcache_search(struct tagcache_search *tcs, int tag)
1140 struct tagcache_header tag_hdr;
1141 struct master_header master_hdr;
1142 int i;
1144 if (tcs->initialized)
1145 tagcache_search_finish(tcs);
1147 while (read_lock)
1148 sleep(1);
1150 memset(tcs, 0, sizeof(struct tagcache_search));
1151 if (tc_stat.commit_step > 0 || !tc_stat.ready)
1152 return false;
1154 tcs->position = sizeof(struct tagcache_header);
1155 tcs->type = tag;
1156 tcs->seek_pos = 0;
1157 tcs->seek_list_count = 0;
1158 tcs->filter_count = 0;
1159 tcs->masterfd = -1;
1161 for (i = 0; i < TAG_COUNT; i++)
1162 tcs->idxfd[i] = -1;
1164 #ifndef HAVE_TC_RAMCACHE
1165 tcs->ramsearch = false;
1166 #else
1167 tcs->ramsearch = tc_stat.ramcache;
1168 if (tcs->ramsearch)
1170 tcs->entry_count = hdr->entry_count[tcs->type];
1172 else
1173 #endif
1175 if (!tagcache_is_numeric_tag(tcs->type))
1177 tcs->idxfd[tcs->type] = open_tag_fd(&tag_hdr, tcs->type, false);
1178 if (tcs->idxfd[tcs->type] < 0)
1179 return false;
1182 /* Always open as R/W so we can pass tcs to functions that modify data also
1183 * without failing. */
1184 tcs->masterfd = open_master_fd(&master_hdr, true);
1186 if (tcs->masterfd < 0)
1187 return false;
1190 tcs->valid = true;
1191 tcs->initialized = true;
1192 write_lock++;
1194 return true;
1197 void tagcache_search_set_uniqbuf(struct tagcache_search *tcs,
1198 void *buffer, long length)
1200 tcs->unique_list = (unsigned long *)buffer;
1201 tcs->unique_list_capacity = length / sizeof(*tcs->unique_list);
1202 tcs->unique_list_count = 0;
1205 bool tagcache_search_add_filter(struct tagcache_search *tcs,
1206 int tag, int seek)
1208 if (tcs->filter_count == TAGCACHE_MAX_FILTERS)
1209 return false;
1211 if (!tagcache_is_unique_tag(tag) || tagcache_is_numeric_tag(tag))
1212 return false;
1214 tcs->filter_tag[tcs->filter_count] = tag;
1215 tcs->filter_seek[tcs->filter_count] = seek;
1216 tcs->filter_count++;
1218 return true;
1221 bool tagcache_search_add_clause(struct tagcache_search *tcs,
1222 struct tagcache_search_clause *clause)
1224 int i;
1226 if (tcs->clause_count >= TAGCACHE_MAX_CLAUSES)
1228 logf("Too many clauses");
1229 return false;
1232 /* Check if there is already a similar filter in present (filters are
1233 * much faster than clauses).
1235 for (i = 0; i < tcs->filter_count; i++)
1237 if (tcs->filter_tag[i] == clause->tag)
1238 return true;
1241 if (!tagcache_is_numeric_tag(clause->tag) && tcs->idxfd[clause->tag] < 0)
1243 char buf[MAX_PATH];
1245 snprintf(buf, sizeof buf, TAGCACHE_FILE_INDEX, clause->tag);
1246 tcs->idxfd[clause->tag] = open(buf, O_RDONLY);
1249 tcs->clause[tcs->clause_count] = clause;
1250 tcs->clause_count++;
1252 return true;
1255 #define TAG_FILENAME_RAM(tcs) ((tcs->type == tag_filename) \
1256 ? (flag & FLAG_DIRCACHE) : 1)
1258 static bool get_next(struct tagcache_search *tcs)
1260 static char buf[TAG_MAXLEN+32];
1261 struct tagfile_entry entry;
1262 long flag = 0;
1264 if (!tcs->valid || !tc_stat.ready)
1265 return false;
1267 if (tcs->idxfd[tcs->type] < 0 && !tagcache_is_numeric_tag(tcs->type)
1268 #ifdef HAVE_TC_RAMCACHE
1269 && !tcs->ramsearch
1270 #endif
1272 return false;
1274 /* Relative fetch. */
1275 if (tcs->filter_count > 0 || tcs->clause_count > 0
1276 || tagcache_is_numeric_tag(tcs->type))
1278 /* Check for end of list. */
1279 if (tcs->seek_list_count == 0)
1281 /* Try to fetch more. */
1282 if (!build_lookup_list(tcs))
1284 tcs->valid = false;
1285 return false;
1289 tcs->seek_list_count--;
1290 flag = tcs->seek_flags[tcs->seek_list_count];
1292 /* Seek stream to the correct position and continue to direct fetch. */
1293 if ((!tcs->ramsearch || !TAG_FILENAME_RAM(tcs))
1294 && !tagcache_is_numeric_tag(tcs->type))
1296 if (!open_files(tcs, tcs->type))
1297 return false;
1299 lseek(tcs->idxfd[tcs->type], tcs->seek_list[tcs->seek_list_count], SEEK_SET);
1301 else
1302 tcs->position = tcs->seek_list[tcs->seek_list_count];
1305 if (tagcache_is_numeric_tag(tcs->type))
1307 snprintf(buf, sizeof(buf), "%d", tcs->position);
1308 tcs->result_seek = tcs->position;
1309 tcs->result = buf;
1310 tcs->result_len = strlen(buf) + 1;
1311 return true;
1314 /* Direct fetch. */
1315 #ifdef HAVE_TC_RAMCACHE
1316 if (tcs->ramsearch && TAG_FILENAME_RAM(tcs))
1318 struct tagfile_entry *ep;
1320 if (tcs->entry_count == 0)
1322 tcs->valid = false;
1323 return false;
1325 tcs->entry_count--;
1327 tcs->result_seek = tcs->position;
1329 # ifdef HAVE_DIRCACHE
1330 if (tcs->type == tag_filename)
1332 dircache_copy_path((struct dircache_entry *)tcs->position,
1333 buf, sizeof buf);
1334 tcs->result = buf;
1335 tcs->result_len = strlen(buf) + 1;
1336 tcs->idx_id = FLAG_GET_ATTR(flag);
1337 tcs->ramresult = false;
1339 return true;
1341 # endif
1343 ep = (struct tagfile_entry *)&hdr->tags[tcs->type][tcs->position];
1344 tcs->position += sizeof(struct tagfile_entry) + ep->tag_length;
1345 tcs->result = ep->tag_data;
1346 tcs->result_len = strlen(tcs->result) + 1;
1347 tcs->idx_id = ep->idx_id;
1348 tcs->ramresult = true;
1350 return true;
1352 else
1353 #endif
1355 if (!open_files(tcs, tcs->type))
1356 return false;
1358 tcs->result_seek = lseek(tcs->idxfd[tcs->type], 0, SEEK_CUR);
1359 if (ecread(tcs->idxfd[tcs->type], &entry, 1,
1360 tagfile_entry_ec, tc_stat.econ) != sizeof(struct tagfile_entry))
1362 /* End of data. */
1363 tcs->valid = false;
1364 return false;
1368 if (entry.tag_length > (long)sizeof(buf))
1370 tcs->valid = false;
1371 logf("too long tag #2");
1372 return false;
1375 if (read(tcs->idxfd[tcs->type], buf, entry.tag_length) != entry.tag_length)
1377 tcs->valid = false;
1378 logf("read error #4");
1379 return false;
1382 tcs->result = buf;
1383 tcs->result_len = strlen(tcs->result) + 1;
1384 tcs->idx_id = entry.idx_id;
1385 tcs->ramresult = false;
1387 return true;
1390 bool tagcache_get_next(struct tagcache_search *tcs)
1392 while (get_next(tcs))
1394 if (tcs->result_len > 1)
1395 return true;
1398 return false;
1401 bool tagcache_retrieve(struct tagcache_search *tcs, int idxid,
1402 int tag, char *buf, long size)
1404 struct index_entry idx;
1406 *buf = '\0';
1407 if (!get_index(tcs->masterfd, idxid, &idx, true))
1408 return false;
1410 return retrieve(tcs, &idx, tag, buf, size);
1413 static bool update_master_header(void)
1415 struct master_header myhdr;
1416 int fd;
1418 if (!tc_stat.ready)
1419 return false;
1421 if ( (fd = open_master_fd(&myhdr, true)) < 0)
1422 return false;
1424 myhdr.serial = current_tcmh.serial;
1425 myhdr.commitid = current_tcmh.commitid;
1427 /* Write it back */
1428 lseek(fd, 0, SEEK_SET);
1429 ecwrite(fd, &myhdr, 1, master_header_ec, tc_stat.econ);
1430 close(fd);
1432 #ifdef HAVE_TC_RAMCACHE
1433 if (hdr)
1435 hdr->h.serial = current_tcmh.serial;
1436 hdr->h.commitid = current_tcmh.commitid;
1438 #endif
1440 return true;
1443 #if 0
1445 void tagcache_modify(struct tagcache_search *tcs, int type, const char *text)
1447 struct tagentry *entry;
1449 if (tcs->type != tag_title)
1450 return ;
1452 /* We will need reserve buffer for this. */
1453 if (tcs->ramcache)
1455 struct tagfile_entry *ep;
1457 ep = (struct tagfile_entry *)&hdr->tags[tcs->type][tcs->result_seek];
1458 tcs->seek_list[tcs->seek_list_count];
1461 entry = find_entry_ram();
1464 #endif
1466 void tagcache_search_finish(struct tagcache_search *tcs)
1468 int i;
1470 if (!tcs->initialized)
1471 return;
1473 if (tcs->masterfd >= 0)
1475 close(tcs->masterfd);
1476 tcs->masterfd = -1;
1479 for (i = 0; i < TAG_COUNT; i++)
1481 if (tcs->idxfd[i] >= 0)
1483 close(tcs->idxfd[i]);
1484 tcs->idxfd[i] = -1;
1488 tcs->ramsearch = false;
1489 tcs->valid = false;
1490 tcs->initialized = 0;
1491 if (write_lock > 0)
1492 write_lock--;
1495 #if defined(HAVE_TC_RAMCACHE) && defined(HAVE_DIRCACHE)
1496 static struct tagfile_entry *get_tag(const struct index_entry *entry, int tag)
1498 return (struct tagfile_entry *)&hdr->tags[tag][entry->tag_seek[tag]];
1501 static long get_tag_numeric(const struct index_entry *entry, int tag)
1503 return entry->tag_seek[tag];
1506 static char* get_tag_string(const struct index_entry *entry, int tag)
1508 char* s = get_tag(entry, tag)->tag_data;
1509 return strcmp(s, UNTAGGED) ? s : NULL;
1512 bool tagcache_fill_tags(struct mp3entry *id3, const char *filename)
1514 struct index_entry *entry;
1515 int idx_id;
1517 if (!tc_stat.ready)
1518 return false;
1520 /* Find the corresponding entry in tagcache. */
1521 idx_id = find_entry_ram(filename, NULL);
1522 if (idx_id < 0 || !tc_stat.ramcache)
1523 return false;
1525 entry = &hdr->indices[idx_id];
1527 id3->title = get_tag_string(entry, tag_title);
1528 id3->artist = get_tag_string(entry, tag_artist);
1529 id3->album = get_tag_string(entry, tag_album);
1530 id3->genre_string = get_tag_string(entry, tag_genre);
1531 id3->composer = get_tag_string(entry, tag_composer);
1532 id3->comment = get_tag_string(entry, tag_comment);
1533 id3->albumartist = get_tag_string(entry, tag_albumartist);
1535 id3->playcount = get_tag_numeric(entry, tag_playcount);
1536 id3->rating = get_tag_numeric(entry, tag_rating);
1537 id3->lastplayed = get_tag_numeric(entry, tag_lastplayed);
1538 id3->score = get_tag_numeric(entry, tag_virt_autoscore) / 10;
1539 id3->year = get_tag_numeric(entry, tag_year);
1541 id3->tracknum = get_tag_numeric(entry, tag_tracknumber);
1542 id3->bitrate = get_tag_numeric(entry, tag_bitrate);
1543 if (id3->bitrate == 0)
1544 id3->bitrate = 1;
1546 return true;
1548 #endif
1550 static inline void write_item(const char *item)
1552 int len = strlen(item) + 1;
1554 data_size += len;
1555 write(cachefd, item, len);
1558 static int check_if_empty(char **tag)
1560 int length;
1562 if (*tag == NULL || *tag[0] == '\0')
1564 *tag = UNTAGGED;
1565 return sizeof(UNTAGGED); /* Tag length */
1568 length = strlen(*tag);
1569 if (length > TAG_MAXLEN)
1571 logf("over length tag: %s", *tag);
1572 length = TAG_MAXLEN;
1573 *tag[length] = '\0';
1576 return length + 1;
1579 #define ADD_TAG(entry,tag,data) \
1580 /* Adding tag */ \
1581 entry.tag_offset[tag] = offset; \
1582 entry.tag_length[tag] = check_if_empty(data); \
1583 offset += entry.tag_length[tag]
1585 #if defined(HAVE_TC_RAMCACHE) && defined(HAVE_DIRCACHE)
1586 static void add_tagcache(char *path, const struct dircache_entry *dc)
1587 #else
1588 static void add_tagcache(char *path)
1589 #endif
1591 struct track_info track;
1592 struct temp_file_entry entry;
1593 bool ret;
1594 int fd;
1595 char tracknumfix[3];
1596 int offset = 0;
1597 int path_length = strlen(path);
1598 bool has_albumartist;
1600 if (cachefd < 0)
1601 return ;
1603 /* Check for overlength file path. */
1604 if (path_length > TAG_MAXLEN)
1606 /* Path can't be shortened. */
1607 logf("Too long path: %s", path);
1608 return ;
1611 /* Check if the file is supported. */
1612 if (probe_file_format(path) == AFMT_UNKNOWN)
1613 return ;
1615 /* Check if the file is already cached. */
1616 #if defined(HAVE_TC_RAMCACHE) && defined(HAVE_DIRCACHE)
1617 if (tc_stat.ramcache && dircache_is_enabled())
1619 if (find_entry_ram(path, dc) >= 0)
1620 return ;
1622 else
1623 #endif
1625 if (filenametag_fd >= 0)
1627 if (find_entry_disk(path) >= 0)
1628 return ;
1632 fd = open(path, O_RDONLY);
1633 if (fd < 0)
1635 logf("open fail: %s", path);
1636 return ;
1639 memset(&track, 0, sizeof(struct track_info));
1640 memset(&entry, 0, sizeof(struct temp_file_entry));
1641 memset(&tracknumfix, 0, sizeof(tracknumfix));
1642 ret = get_metadata(&track, fd, path, false);
1643 close(fd);
1645 if (!ret)
1646 return ;
1648 logf("-> %s", path);
1650 /* Generate track number if missing. */
1651 if (track.id3.tracknum <= 0)
1653 const char *p = strrchr(path, '.');
1655 if (p == NULL)
1656 p = &path[strlen(path)-1];
1658 while (*p != '/')
1660 if (isdigit(*p) && isdigit(*(p-1)))
1662 tracknumfix[1] = *p--;
1663 tracknumfix[0] = *p;
1664 break;
1666 p--;
1669 if (tracknumfix[0] != '\0')
1671 track.id3.tracknum = atoi(tracknumfix);
1672 /* Set a flag to indicate track number has been generated. */
1673 entry.flag |= FLAG_TRKNUMGEN;
1675 else
1677 /* Unable to generate track number. */
1678 track.id3.tracknum = -1;
1682 /* Numeric tags */
1683 entry.tag_offset[tag_year] = track.id3.year;
1684 entry.tag_offset[tag_tracknumber] = track.id3.tracknum;
1685 entry.tag_offset[tag_length] = track.id3.length;
1686 entry.tag_offset[tag_bitrate] = track.id3.bitrate;
1688 /* String tags. */
1689 has_albumartist = track.id3.albumartist != NULL
1690 && strlen(track.id3.albumartist) > 0;
1692 ADD_TAG(entry, tag_filename, &path);
1693 ADD_TAG(entry, tag_title, &track.id3.title);
1694 ADD_TAG(entry, tag_artist, &track.id3.artist);
1695 ADD_TAG(entry, tag_album, &track.id3.album);
1696 ADD_TAG(entry, tag_genre, &track.id3.genre_string);
1697 ADD_TAG(entry, tag_composer, &track.id3.composer);
1698 ADD_TAG(entry, tag_comment, &track.id3.comment);
1699 if (has_albumartist)
1701 ADD_TAG(entry, tag_albumartist, &track.id3.albumartist);
1703 else
1705 ADD_TAG(entry, tag_albumartist, &track.id3.artist);
1707 entry.data_length = offset;
1709 /* Write the header */
1710 write(cachefd, &entry, sizeof(struct temp_file_entry));
1712 /* And tags also... Correct order is critical */
1713 write_item(path);
1714 write_item(track.id3.title);
1715 write_item(track.id3.artist);
1716 write_item(track.id3.album);
1717 write_item(track.id3.genre_string);
1718 write_item(track.id3.composer);
1719 write_item(track.id3.comment);
1720 if (has_albumartist)
1722 write_item(track.id3.albumartist);
1724 else
1726 write_item(track.id3.artist);
1728 total_entry_count++;
1731 static bool tempbuf_insert(char *str, int id, int idx_id, bool unique)
1733 struct tempbuf_searchidx *index = (struct tempbuf_searchidx *)tempbuf;
1734 int len = strlen(str)+1;
1735 int i;
1736 unsigned crc32;
1737 unsigned *crcbuf = (unsigned *)&tempbuf[tempbuf_size-4];
1738 char buf[TAG_MAXLEN+32];
1740 for (i = 0; str[i] != '\0' && i < (int)sizeof(buf)-1; i++)
1741 buf[i] = tolower(str[i]);
1742 buf[i] = '\0';
1744 crc32 = crc_32(buf, i, 0xffffffff);
1746 if (unique)
1748 /* Check if the crc does not exist -> entry does not exist for sure. */
1749 for (i = 0; i < tempbufidx; i++)
1751 if (crcbuf[-i] != crc32)
1752 continue;
1754 if (!strcasecmp(str, index[i].str))
1756 if (id < 0 || id >= lookup_buffer_depth)
1758 logf("lookup buf overf.: %d", id);
1759 return false;
1762 lookup[id] = &index[i];
1763 return true;
1768 /* Insert to CRC buffer. */
1769 crcbuf[-tempbufidx] = crc32;
1770 tempbuf_left -= 4;
1772 /* Insert it to the buffer. */
1773 tempbuf_left -= len;
1774 if (tempbuf_left - 4 < 0 || tempbufidx >= commit_entry_count-1)
1775 return false;
1777 if (id >= lookup_buffer_depth)
1779 logf("lookup buf overf. #2: %d", id);
1780 return false;
1783 if (id >= 0)
1785 lookup[id] = &index[tempbufidx];
1786 index[tempbufidx].idlist.id = id;
1788 else
1789 index[tempbufidx].idlist.id = -1;
1791 index[tempbufidx].idlist.next = NULL;
1792 index[tempbufidx].idx_id = idx_id;
1793 index[tempbufidx].seek = -1;
1794 index[tempbufidx].str = &tempbuf[tempbuf_pos];
1795 memcpy(index[tempbufidx].str, str, len);
1796 tempbuf_pos += len;
1797 tempbufidx++;
1799 return true;
1802 static int compare(const void *p1, const void *p2)
1804 do_timed_yield();
1806 struct tempbuf_searchidx *e1 = (struct tempbuf_searchidx *)p1;
1807 struct tempbuf_searchidx *e2 = (struct tempbuf_searchidx *)p2;
1809 if (strcmp(e1->str, UNTAGGED) == 0)
1811 if (strcmp(e2->str, UNTAGGED) == 0)
1812 return 0;
1813 return -1;
1815 else if (strcmp(e2->str, UNTAGGED) == 0)
1816 return 1;
1818 return strncasecmp(e1->str, e2->str, TAG_MAXLEN);
1821 static int tempbuf_sort(int fd)
1823 struct tempbuf_searchidx *index = (struct tempbuf_searchidx *)tempbuf;
1824 struct tagfile_entry fe;
1825 int i;
1826 int length;
1828 /* Generate reverse lookup entries. */
1829 for (i = 0; i < lookup_buffer_depth; i++)
1831 struct tempbuf_id_list *idlist;
1833 if (!lookup[i])
1834 continue;
1836 if (lookup[i]->idlist.id == i)
1837 continue;
1839 idlist = &lookup[i]->idlist;
1840 while (idlist->next != NULL)
1841 idlist = idlist->next;
1843 tempbuf_left -= sizeof(struct tempbuf_id_list);
1844 if (tempbuf_left - 4 < 0)
1845 return -1;
1847 idlist->next = (struct tempbuf_id_list *)&tempbuf[tempbuf_pos];
1848 if (tempbuf_pos & 0x03)
1850 tempbuf_pos = (tempbuf_pos & ~0x03) + 0x04;
1851 tempbuf_left -= 3;
1852 idlist->next = (struct tempbuf_id_list *)&tempbuf[tempbuf_pos];
1854 tempbuf_pos += sizeof(struct tempbuf_id_list);
1856 idlist = idlist->next;
1857 idlist->id = i;
1858 idlist->next = NULL;
1860 do_timed_yield();
1863 qsort(index, tempbufidx, sizeof(struct tempbuf_searchidx), compare);
1864 memset(lookup, 0, lookup_buffer_depth * sizeof(struct tempbuf_searchidx **));
1866 for (i = 0; i < tempbufidx; i++)
1868 struct tempbuf_id_list *idlist = &index[i].idlist;
1870 /* Fix the lookup list. */
1871 while (idlist != NULL)
1873 if (idlist->id >= 0)
1874 lookup[idlist->id] = &index[i];
1875 idlist = idlist->next;
1878 index[i].seek = lseek(fd, 0, SEEK_CUR);
1879 length = strlen(index[i].str) + 1;
1880 fe.tag_length = length;
1881 fe.idx_id = index[i].idx_id;
1883 /* Check the chunk alignment. */
1884 if ((fe.tag_length + sizeof(struct tagfile_entry))
1885 % TAGFILE_ENTRY_CHUNK_LENGTH)
1887 fe.tag_length += TAGFILE_ENTRY_CHUNK_LENGTH -
1888 ((fe.tag_length + sizeof(struct tagfile_entry))
1889 % TAGFILE_ENTRY_CHUNK_LENGTH);
1892 #ifdef TAGCACHE_STRICT_ALIGN
1893 /* Make sure the entry is long aligned. */
1894 if (index[i].seek & 0x03)
1896 logf("tempbuf_sort: alignment error!");
1897 return -3;
1899 #endif
1901 if (ecwrite(fd, &fe, 1, tagfile_entry_ec, tc_stat.econ) !=
1902 sizeof(struct tagfile_entry))
1904 logf("tempbuf_sort: write error #1");
1905 return -1;
1908 if (write(fd, index[i].str, length) != length)
1910 logf("tempbuf_sort: write error #2");
1911 return -2;
1914 /* Write some padding. */
1915 if (fe.tag_length - length > 0)
1916 write(fd, "XXXXXXXX", fe.tag_length - length);
1919 return i;
1922 inline static struct tempbuf_searchidx* tempbuf_locate(int id)
1924 if (id < 0 || id >= lookup_buffer_depth)
1925 return NULL;
1927 return lookup[id];
1931 inline static int tempbuf_find_location(int id)
1933 struct tempbuf_searchidx *entry;
1935 entry = tempbuf_locate(id);
1936 if (entry == NULL)
1937 return -1;
1939 return entry->seek;
1942 static bool build_numeric_indices(struct tagcache_header *h, int tmpfd)
1944 struct master_header tcmh;
1945 struct index_entry idx;
1946 int masterfd;
1947 int masterfd_pos;
1948 struct temp_file_entry *entrybuf = (struct temp_file_entry *)tempbuf;
1949 int max_entries;
1950 int entries_processed = 0;
1951 int i, j;
1953 max_entries = tempbuf_size / sizeof(struct temp_file_entry) - 1;
1955 logf("Building numeric indices...");
1956 lseek(tmpfd, sizeof(struct tagcache_header), SEEK_SET);
1958 if ( (masterfd = open_master_fd(&tcmh, true)) < 0)
1959 return false;
1961 masterfd_pos = lseek(masterfd, tcmh.tch.entry_count * sizeof(struct index_entry),
1962 SEEK_CUR);
1963 if (masterfd_pos == filesize(masterfd))
1965 logf("we can't append!");
1966 close(masterfd);
1967 return false;
1970 while (entries_processed < h->entry_count)
1972 int count = MIN(h->entry_count - entries_processed, max_entries);
1974 /* Read in as many entries as possible. */
1975 for (i = 0; i < count; i++)
1977 /* Read in numeric data. */
1978 if (read(tmpfd, &entrybuf[i], sizeof(struct temp_file_entry)) !=
1979 sizeof(struct temp_file_entry))
1981 logf("read fail #1");
1982 close(masterfd);
1983 return false;
1986 /* Skip string data. */
1987 lseek(tmpfd, entrybuf[i].data_length, SEEK_CUR);
1990 /* Commit the data to the index. */
1991 for (i = 0; i < count; i++)
1993 int loc = lseek(masterfd, 0, SEEK_CUR);
1995 if (ecread(masterfd, &idx, 1, index_entry_ec, tc_stat.econ)
1996 != sizeof(struct index_entry))
1998 logf("read fail #2");
1999 close(masterfd);
2000 return false;
2003 for (j = 0; j < TAG_COUNT; j++)
2005 if (!tagcache_is_numeric_tag(j))
2006 continue;
2008 idx.tag_seek[j] = entrybuf[i].tag_offset[j];
2010 idx.flag = entrybuf[i].flag;
2012 if (tc_stat.ready && current_tcmh.commitid > 0)
2014 idx.tag_seek[tag_commitid] = current_tcmh.commitid;
2015 idx.flag |= FLAG_DIRTYNUM;
2018 /* Write back the updated index. */
2019 lseek(masterfd, loc, SEEK_SET);
2020 if (ecwrite(masterfd, &idx, 1, index_entry_ec, tc_stat.econ)
2021 != sizeof(struct index_entry))
2023 logf("write fail");
2024 close(masterfd);
2025 return false;
2029 entries_processed += count;
2030 logf("%d/%ld entries processed", entries_processed, h->entry_count);
2033 close(masterfd);
2035 return true;
2039 * Return values:
2040 * > 0 success
2041 * == 0 temporary failure
2042 * < 0 fatal error
2044 static int build_index(int index_type, struct tagcache_header *h, int tmpfd)
2046 int i;
2047 struct tagcache_header tch;
2048 struct master_header tcmh;
2049 struct index_entry idxbuf[IDX_BUF_DEPTH];
2050 int idxbuf_pos;
2051 char buf[TAG_MAXLEN+32];
2052 int fd = -1, masterfd;
2053 bool error = false;
2054 int init;
2055 int masterfd_pos;
2057 logf("Building index: %d", index_type);
2059 /* Check the number of entries we need to allocate ram for. */
2060 commit_entry_count = h->entry_count + 1;
2062 masterfd = open_master_fd(&tcmh, false);
2063 if (masterfd >= 0)
2065 commit_entry_count += tcmh.tch.entry_count;
2066 close(masterfd);
2068 else
2069 remove_files(); /* Just to be sure we are clean. */
2071 /* Open the index file, which contains the tag names. */
2072 fd = open_tag_fd(&tch, index_type, true);
2073 if (fd >= 0)
2075 logf("tch.datasize=%ld", tch.datasize);
2076 lookup_buffer_depth = 1 +
2077 /* First part */ commit_entry_count +
2078 /* Second part */ (tch.datasize / TAGFILE_ENTRY_CHUNK_LENGTH);
2080 else
2082 lookup_buffer_depth = 1 +
2083 /* First part */ commit_entry_count +
2084 /* Second part */ 0;
2087 logf("lookup_buffer_depth=%ld", lookup_buffer_depth);
2088 logf("commit_entry_count=%ld", commit_entry_count);
2090 /* Allocate buffer for all index entries from both old and new
2091 * tag files. */
2092 tempbufidx = 0;
2093 tempbuf_pos = commit_entry_count * sizeof(struct tempbuf_searchidx);
2095 /* Allocate lookup buffer. The first portion of commit_entry_count
2096 * contains the new tags in the temporary file and the second
2097 * part for locating entries already in the db.
2099 * New tags Old tags
2100 * +---------+---------------------------+
2101 * | index | position/ENTRY_CHUNK_SIZE | lookup buffer
2102 * +---------+---------------------------+
2104 * Old tags are inserted to a temporary buffer with position:
2105 * tempbuf_insert(position/ENTRY_CHUNK_SIZE, ...);
2106 * And new tags with index:
2107 * tempbuf_insert(idx, ...);
2109 * The buffer is sorted and written into tag file:
2110 * tempbuf_sort(...);
2111 * leaving master index locations messed up.
2113 * That is fixed using the lookup buffer for old tags:
2114 * new_seek = tempbuf_find_location(old_seek, ...);
2115 * and for new tags:
2116 * new_seek = tempbuf_find_location(idx);
2118 lookup = (struct tempbuf_searchidx **)&tempbuf[tempbuf_pos];
2119 tempbuf_pos += lookup_buffer_depth * sizeof(void **);
2120 memset(lookup, 0, lookup_buffer_depth * sizeof(void **));
2122 /* And calculate the remaining data space used mainly for storing
2123 * tag data (strings). */
2124 tempbuf_left = tempbuf_size - tempbuf_pos - 8;
2125 if (tempbuf_left - TAGFILE_ENTRY_AVG_LENGTH * commit_entry_count < 0)
2127 logf("Buffer way too small!");
2128 return 0;
2131 if (fd >= 0)
2134 * If tag file contains unique tags (sorted index), we will load
2135 * it entirely into memory so we can resort it later for use with
2136 * chunked browsing.
2138 if (tagcache_is_sorted_tag(index_type))
2140 logf("loading tags...");
2141 for (i = 0; i < tch.entry_count; i++)
2143 struct tagfile_entry entry;
2144 int loc = lseek(fd, 0, SEEK_CUR);
2145 bool ret;
2147 if (ecread(fd, &entry, 1, tagfile_entry_ec, tc_stat.econ)
2148 != sizeof(struct tagfile_entry))
2150 logf("read error #7");
2151 close(fd);
2152 return -2;
2155 if (entry.tag_length >= (int)sizeof(buf))
2157 logf("too long tag #3");
2158 close(fd);
2159 return -2;
2162 if (read(fd, buf, entry.tag_length) != entry.tag_length)
2164 logf("read error #8");
2165 close(fd);
2166 return -2;
2169 /* Skip deleted entries. */
2170 if (buf[0] == '\0')
2171 continue;
2174 * Save the tag and tag id in the memory buffer. Tag id
2175 * is saved so we can later reindex the master lookup
2176 * table when the index gets resorted.
2178 ret = tempbuf_insert(buf, loc/TAGFILE_ENTRY_CHUNK_LENGTH
2179 + commit_entry_count, entry.idx_id,
2180 tagcache_is_unique_tag(index_type));
2181 if (!ret)
2183 close(fd);
2184 return -3;
2186 do_timed_yield();
2188 logf("done");
2190 else
2191 tempbufidx = tch.entry_count;
2193 else
2196 * Creating new index file to store the tags. No need to preload
2197 * anything whether the index type is sorted or not.
2199 snprintf(buf, sizeof buf, TAGCACHE_FILE_INDEX, index_type);
2200 fd = open(buf, O_WRONLY | O_CREAT | O_TRUNC);
2201 if (fd < 0)
2203 logf("%s open fail", buf);
2204 return -2;
2207 tch.magic = TAGCACHE_MAGIC;
2208 tch.entry_count = 0;
2209 tch.datasize = 0;
2211 if (ecwrite(fd, &tch, 1, tagcache_header_ec, tc_stat.econ)
2212 != sizeof(struct tagcache_header))
2214 logf("header write failed");
2215 close(fd);
2216 return -2;
2220 /* Loading the tag lookup file as "master file". */
2221 logf("Loading index file");
2222 masterfd = open(TAGCACHE_FILE_MASTER, O_RDWR);
2224 if (masterfd < 0)
2226 logf("Creating new DB");
2227 masterfd = open(TAGCACHE_FILE_MASTER, O_WRONLY | O_CREAT | O_TRUNC);
2229 if (masterfd < 0)
2231 logf("Failure to create index file (%s)", TAGCACHE_FILE_MASTER);
2232 close(fd);
2233 return -2;
2236 /* Write the header (write real values later). */
2237 memset(&tcmh, 0, sizeof(struct master_header));
2238 tcmh.tch = *h;
2239 tcmh.tch.entry_count = 0;
2240 tcmh.tch.datasize = 0;
2241 tcmh.dirty = true;
2242 ecwrite(masterfd, &tcmh, 1, master_header_ec, tc_stat.econ);
2243 init = true;
2244 masterfd_pos = lseek(masterfd, 0, SEEK_CUR);
2246 else
2249 * Master file already exists so we need to process the current
2250 * file first.
2252 init = false;
2254 if (ecread(masterfd, &tcmh, 1, master_header_ec, tc_stat.econ) !=
2255 sizeof(struct master_header) || tcmh.tch.magic != TAGCACHE_MAGIC)
2257 logf("header error");
2258 close(fd);
2259 close(masterfd);
2260 return -2;
2264 * If we reach end of the master file, we need to expand it to
2265 * hold new tags. If the current index is not sorted, we can
2266 * simply append new data to end of the file.
2267 * However, if the index is sorted, we need to update all tag
2268 * pointers in the master file for the current index.
2270 masterfd_pos = lseek(masterfd, tcmh.tch.entry_count * sizeof(struct index_entry),
2271 SEEK_CUR);
2272 if (masterfd_pos == filesize(masterfd))
2274 logf("appending...");
2275 init = true;
2280 * Load new unique tags in memory to be sorted later and added
2281 * to the master lookup file.
2283 if (tagcache_is_sorted_tag(index_type))
2285 lseek(tmpfd, sizeof(struct tagcache_header), SEEK_SET);
2286 /* h is the header of the temporary file containing new tags. */
2287 logf("inserting new tags...");
2288 for (i = 0; i < h->entry_count; i++)
2290 struct temp_file_entry entry;
2292 if (read(tmpfd, &entry, sizeof(struct temp_file_entry)) !=
2293 sizeof(struct temp_file_entry))
2295 logf("read fail #3");
2296 error = true;
2297 goto error_exit;
2300 /* Read data. */
2301 if (entry.tag_length[index_type] >= (long)sizeof(buf))
2303 logf("too long entry!");
2304 error = true;
2305 goto error_exit;
2308 lseek(tmpfd, entry.tag_offset[index_type], SEEK_CUR);
2309 if (read(tmpfd, buf, entry.tag_length[index_type]) !=
2310 entry.tag_length[index_type])
2312 logf("read fail #4");
2313 error = true;
2314 goto error_exit;
2317 if (tagcache_is_unique_tag(index_type))
2318 error = !tempbuf_insert(buf, i, -1, true);
2319 else
2320 error = !tempbuf_insert(buf, i, tcmh.tch.entry_count + i, false);
2322 if (error)
2324 logf("insert error");
2325 goto error_exit;
2328 /* Skip to next. */
2329 lseek(tmpfd, entry.data_length - entry.tag_offset[index_type] -
2330 entry.tag_length[index_type], SEEK_CUR);
2331 do_timed_yield();
2333 logf("done");
2335 /* Sort the buffer data and write it to the index file. */
2336 lseek(fd, sizeof(struct tagcache_header), SEEK_SET);
2337 i = tempbuf_sort(fd);
2338 if (i < 0)
2339 goto error_exit;
2340 logf("sorted %d tags", i);
2343 * Now update all indexes in the master lookup file.
2345 logf("updating indices...");
2346 lseek(masterfd, sizeof(struct master_header), SEEK_SET);
2347 for (i = 0; i < tcmh.tch.entry_count; i += idxbuf_pos)
2349 int j;
2350 int loc = lseek(masterfd, 0, SEEK_CUR);
2352 idxbuf_pos = MIN(tcmh.tch.entry_count - i, IDX_BUF_DEPTH);
2354 if (ecread(masterfd, idxbuf, idxbuf_pos, index_entry_ec, tc_stat.econ)
2355 != (int)sizeof(struct index_entry)*idxbuf_pos)
2357 logf("read fail #5");
2358 error = true;
2359 goto error_exit ;
2361 lseek(masterfd, loc, SEEK_SET);
2363 for (j = 0; j < idxbuf_pos; j++)
2365 if (idxbuf[j].flag & FLAG_DELETED)
2367 /* We can just ignore deleted entries. */
2368 idxbuf[j].tag_seek[index_type] = 0;
2369 continue;
2372 idxbuf[j].tag_seek[index_type] = tempbuf_find_location(
2373 idxbuf[j].tag_seek[index_type]/TAGFILE_ENTRY_CHUNK_LENGTH
2374 + commit_entry_count);
2376 if (idxbuf[j].tag_seek[index_type] < 0)
2378 logf("update error: %d/%ld", i+j, tcmh.tch.entry_count);
2379 error = true;
2380 goto error_exit;
2383 do_timed_yield();
2386 /* Write back the updated index. */
2387 if (ecwrite(masterfd, idxbuf, idxbuf_pos,
2388 index_entry_ec, tc_stat.econ) !=
2389 (int)sizeof(struct index_entry)*idxbuf_pos)
2391 logf("write fail");
2392 error = true;
2393 goto error_exit;
2396 logf("done");
2400 * Walk through the temporary file containing the new tags.
2402 // build_normal_index(h, tmpfd, masterfd, idx);
2403 logf("updating new indices...");
2404 lseek(masterfd, masterfd_pos, SEEK_SET);
2405 lseek(tmpfd, sizeof(struct tagcache_header), SEEK_SET);
2406 lseek(fd, 0, SEEK_END);
2407 for (i = 0; i < h->entry_count; i += idxbuf_pos)
2409 int j;
2411 idxbuf_pos = MIN(h->entry_count - i, IDX_BUF_DEPTH);
2412 if (init)
2414 memset(idxbuf, 0, sizeof(struct index_entry)*IDX_BUF_DEPTH);
2416 else
2418 int loc = lseek(masterfd, 0, SEEK_CUR);
2420 if (ecread(masterfd, idxbuf, idxbuf_pos, index_entry_ec, tc_stat.econ)
2421 != (int)sizeof(struct index_entry)*idxbuf_pos)
2423 logf("read fail #6");
2424 error = true;
2425 break ;
2427 lseek(masterfd, loc, SEEK_SET);
2430 /* Read entry headers. */
2431 for (j = 0; j < idxbuf_pos; j++)
2433 if (!tagcache_is_sorted_tag(index_type))
2435 struct temp_file_entry entry;
2436 struct tagfile_entry fe;
2438 if (read(tmpfd, &entry, sizeof(struct temp_file_entry)) !=
2439 sizeof(struct temp_file_entry))
2441 logf("read fail #7");
2442 error = true;
2443 break ;
2446 /* Read data. */
2447 if (entry.tag_length[index_type] >= (int)sizeof(buf))
2449 logf("too long entry!");
2450 logf("length=%d", entry.tag_length[index_type]);
2451 logf("pos=0x%02lx", lseek(tmpfd, 0, SEEK_CUR));
2452 error = true;
2453 break ;
2456 lseek(tmpfd, entry.tag_offset[index_type], SEEK_CUR);
2457 if (read(tmpfd, buf, entry.tag_length[index_type]) !=
2458 entry.tag_length[index_type])
2460 logf("read fail #8");
2461 logf("offset=0x%02lx", entry.tag_offset[index_type]);
2462 logf("length=0x%02x", entry.tag_length[index_type]);
2463 error = true;
2464 break ;
2467 /* Write to index file. */
2468 idxbuf[j].tag_seek[index_type] = lseek(fd, 0, SEEK_CUR);
2469 fe.tag_length = entry.tag_length[index_type];
2470 fe.idx_id = tcmh.tch.entry_count + i + j;
2471 ecwrite(fd, &fe, 1, tagfile_entry_ec, tc_stat.econ);
2472 write(fd, buf, fe.tag_length);
2473 tempbufidx++;
2475 /* Skip to next. */
2476 lseek(tmpfd, entry.data_length - entry.tag_offset[index_type] -
2477 entry.tag_length[index_type], SEEK_CUR);
2479 else
2481 /* Locate the correct entry from the sorted array. */
2482 idxbuf[j].tag_seek[index_type] = tempbuf_find_location(i + j);
2483 if (idxbuf[j].tag_seek[index_type] < 0)
2485 logf("entry not found (%d)", j);
2486 error = true;
2487 break ;
2492 /* Write index. */
2493 if (ecwrite(masterfd, idxbuf, idxbuf_pos,
2494 index_entry_ec, tc_stat.econ) !=
2495 (int)sizeof(struct index_entry)*idxbuf_pos)
2497 logf("tagcache: write fail #4");
2498 error = true;
2499 break ;
2502 do_timed_yield();
2504 logf("done");
2506 /* Finally write the header. */
2507 tch.magic = TAGCACHE_MAGIC;
2508 tch.entry_count = tempbufidx;
2509 tch.datasize = lseek(fd, 0, SEEK_END) - sizeof(struct tagcache_header);
2510 lseek(fd, 0, SEEK_SET);
2511 ecwrite(fd, &tch, 1, tagcache_header_ec, tc_stat.econ);
2513 if (index_type != tag_filename)
2514 h->datasize += tch.datasize;
2515 logf("s:%d/%ld/%ld", index_type, tch.datasize, h->datasize);
2516 error_exit:
2518 close(fd);
2519 close(masterfd);
2521 if (error)
2522 return -2;
2524 return 1;
2527 static bool commit(void)
2529 struct tagcache_header tch;
2530 struct master_header tcmh;
2531 int i, len, rc;
2532 int tmpfd;
2533 int masterfd;
2534 #ifdef HAVE_DIRCACHE
2535 bool dircache_buffer_stolen = false;
2536 #endif
2537 bool local_allocation = false;
2539 logf("committing tagcache");
2541 while (write_lock)
2542 sleep(1);
2544 tmpfd = open(TAGCACHE_FILE_TEMP, O_RDONLY);
2545 if (tmpfd < 0)
2547 logf("nothing to commit");
2548 return true;
2552 /* Load the header. */
2553 len = sizeof(struct tagcache_header);
2554 rc = read(tmpfd, &tch, len);
2556 if (tch.magic != TAGCACHE_MAGIC || rc != len)
2558 logf("incorrect header");
2559 close(tmpfd);
2560 remove(TAGCACHE_FILE_TEMP);
2561 return false;
2564 if (tch.entry_count == 0)
2566 logf("nothing to commit");
2567 close(tmpfd);
2568 remove(TAGCACHE_FILE_TEMP);
2569 return true;
2572 #ifdef HAVE_EEPROM_SETTINGS
2573 remove(TAGCACHE_STATEFILE);
2574 #endif
2576 /* At first be sure to unload the ramcache! */
2577 #ifdef HAVE_TC_RAMCACHE
2578 tc_stat.ramcache = false;
2579 #endif
2581 read_lock++;
2583 /* Try to steal every buffer we can :) */
2584 if (tempbuf_size == 0)
2585 local_allocation = true;
2587 #ifdef HAVE_DIRCACHE
2588 if (tempbuf_size == 0)
2590 /* Try to steal the dircache buffer. */
2591 tempbuf = dircache_steal_buffer(&tempbuf_size);
2592 tempbuf_size &= ~0x03;
2594 if (tempbuf_size > 0)
2596 dircache_buffer_stolen = true;
2599 #endif
2601 #ifdef HAVE_TC_RAMCACHE
2602 if (tempbuf_size == 0 && tc_stat.ramcache_allocated > 0)
2604 tempbuf = (char *)(hdr + 1);
2605 tempbuf_size = tc_stat.ramcache_allocated - sizeof(struct ramcache_header) - 128;
2606 tempbuf_size &= ~0x03;
2608 #endif
2610 /* And finally fail if there are no buffers available. */
2611 if (tempbuf_size == 0)
2613 logf("delaying commit until next boot");
2614 tc_stat.commit_delayed = true;
2615 close(tmpfd);
2616 read_lock--;
2617 return false;
2620 logf("commit %ld entries...", tch.entry_count);
2622 /* Mark DB dirty so it will stay disabled if commit fails. */
2623 current_tcmh.dirty = true;
2624 update_master_header();
2626 /* Now create the index files. */
2627 tc_stat.commit_step = 0;
2628 tch.datasize = 0;
2629 tc_stat.commit_delayed = false;
2631 for (i = 0; i < TAG_COUNT; i++)
2633 int ret;
2635 if (tagcache_is_numeric_tag(i))
2636 continue;
2638 tc_stat.commit_step++;
2639 ret = build_index(i, &tch, tmpfd);
2640 if (ret <= 0)
2642 close(tmpfd);
2643 logf("tagcache failed init");
2644 if (ret < 0)
2645 remove_files();
2646 else
2647 tc_stat.commit_delayed = true;
2648 tc_stat.commit_step = 0;
2649 read_lock--;
2650 return false;
2654 if (!build_numeric_indices(&tch, tmpfd))
2656 logf("Failure to commit numeric indices");
2657 close(tmpfd);
2658 remove_files();
2659 tc_stat.commit_step = 0;
2660 read_lock--;
2661 return false;
2664 close(tmpfd);
2665 tc_stat.commit_step = 0;
2667 /* Update the master index headers. */
2668 if ( (masterfd = open_master_fd(&tcmh, true)) < 0)
2670 read_lock--;
2671 return false;
2674 tcmh.tch.entry_count += tch.entry_count;
2675 tcmh.tch.datasize = sizeof(struct master_header)
2676 + sizeof(struct index_entry) * tcmh.tch.entry_count
2677 + tch.datasize;
2678 tcmh.dirty = false;
2679 tcmh.commitid++;
2681 lseek(masterfd, 0, SEEK_SET);
2682 ecwrite(masterfd, &tcmh, 1, master_header_ec, tc_stat.econ);
2683 close(masterfd);
2685 logf("tagcache committed");
2686 remove(TAGCACHE_FILE_TEMP);
2687 tc_stat.ready = check_all_headers();
2688 tc_stat.readyvalid = true;
2690 if (local_allocation)
2692 tempbuf = NULL;
2693 tempbuf_size = 0;
2696 #ifdef HAVE_DIRCACHE
2697 /* Rebuild the dircache, if we stole the buffer. */
2698 if (dircache_buffer_stolen)
2699 dircache_build(0);
2700 #endif
2702 #ifdef HAVE_TC_RAMCACHE
2703 /* Reload tagcache. */
2704 if (tc_stat.ramcache_allocated > 0)
2705 tagcache_start_scan();
2706 #endif
2708 read_lock--;
2710 return true;
2713 static void allocate_tempbuf(void)
2715 /* Yeah, malloc would be really nice now :) */
2716 #ifdef __PCTOOL__
2717 tempbuf_size = 32*1024*1024;
2718 tempbuf = malloc(tempbuf_size);
2719 #else
2720 tempbuf = (char *)(((long)audiobuf & ~0x03) + 0x04);
2721 tempbuf_size = (long)audiobufend - (long)audiobuf - 4;
2722 audiobuf += tempbuf_size;
2723 #endif
2726 static void free_tempbuf(void)
2728 if (tempbuf_size == 0)
2729 return ;
2731 #ifdef __PCTOOL__
2732 free(tempbuf);
2733 #else
2734 audiobuf -= tempbuf_size;
2735 #endif
2736 tempbuf = NULL;
2737 tempbuf_size = 0;
2740 long tagcache_increase_serial(void)
2742 long old;
2744 if (!tc_stat.ready)
2745 return -2;
2747 while (read_lock)
2748 sleep(1);
2750 old = current_tcmh.serial++;
2751 if (!update_master_header())
2752 return -1;
2754 return old;
2757 long tagcache_get_serial(void)
2759 return current_tcmh.serial;
2762 long tagcache_get_commitid(void)
2764 return current_tcmh.commitid;
2767 static bool modify_numeric_entry(int masterfd, int idx_id, int tag, long data)
2769 struct index_entry idx;
2771 if (!tc_stat.ready)
2772 return false;
2774 if (!tagcache_is_numeric_tag(tag))
2775 return false;
2777 if (!get_index(masterfd, idx_id, &idx, false))
2778 return false;
2780 idx.tag_seek[tag] = data;
2781 idx.flag |= FLAG_DIRTYNUM;
2783 return write_index(masterfd, idx_id, &idx);
2786 bool tagcache_modify_numeric_entry(struct tagcache_search *tcs,
2787 int tag, long data)
2789 struct master_header myhdr;
2791 if (tcs->masterfd < 0)
2793 if ( (tcs->masterfd = open_master_fd(&myhdr, true)) < 0)
2794 return false;
2797 return modify_numeric_entry(tcs->masterfd, tcs->idx_id, tag, data);
2800 static bool write_tag(int fd, const char *tagstr, const char *datastr)
2802 char buf[512];
2803 int i;
2805 snprintf(buf, sizeof buf, "%s=\"", tagstr);
2806 for (i = strlen(buf); i < (long)sizeof(buf)-3; i++)
2808 if (*datastr == '\0')
2809 break;
2811 if (*datastr == '"')
2813 buf[i] = '\\';
2814 datastr++;
2815 continue;
2818 buf[i] = *(datastr++);
2821 strcpy(&buf[i], "\" ");
2823 write(fd, buf, i + 2);
2825 return true;
2828 static bool read_tag(char *dest, long size,
2829 const char *src, const char *tagstr)
2831 int pos;
2832 char current_tag[32];
2834 while (*src != '\0')
2836 /* Skip all whitespace */
2837 while (*src == ' ')
2838 src++;
2840 if (*src == '\0')
2841 break;
2843 pos = 0;
2844 /* Read in tag name */
2845 while (*src != '=' && *src != ' ')
2847 current_tag[pos] = *src;
2848 src++;
2849 pos++;
2851 if (*src == '\0' || pos >= (long)sizeof(current_tag))
2852 return false;
2854 current_tag[pos] = '\0';
2856 /* Read in tag data */
2858 /* Find the start. */
2859 while (*src != '"' && *src != '\0')
2860 src++;
2862 if (*src == '\0' || *(++src) == '\0')
2863 return false;
2865 /* Read the data. */
2866 for (pos = 0; pos < size; pos++)
2868 if (*src == '\0')
2869 break;
2871 if (*src == '\\' && *(src+1) == '"')
2873 dest[pos] = '"';
2874 src += 2;
2875 continue;
2878 dest[pos] = *src;
2880 if (*src == '"')
2882 src++;
2883 break;
2886 if (*src == '\0')
2887 break;
2889 src++;
2891 dest[pos] = '\0';
2893 if (!strcasecmp(tagstr, current_tag))
2894 return true;
2897 return false;
2900 static int parse_changelog_line(int line_n, const char *buf, void *parameters)
2902 struct index_entry idx;
2903 char tag_data[TAG_MAXLEN+32];
2904 int idx_id;
2905 long masterfd = (long)parameters;
2906 const int import_tags[] = { tag_playcount, tag_rating, tag_playtime, tag_lastplayed,
2907 tag_commitid };
2908 int i;
2909 (void)line_n;
2911 if (*buf == '#')
2912 return 0;
2914 logf("%d/%s", line_n, buf);
2915 if (!read_tag(tag_data, sizeof tag_data, buf, "filename"))
2917 logf("filename missing");
2918 logf("-> %s", buf);
2919 return 0;
2922 idx_id = find_index(tag_data);
2923 if (idx_id < 0)
2925 logf("entry not found");
2926 return 0;
2929 if (!get_index(masterfd, idx_id, &idx, false))
2931 logf("failed to retrieve index entry");
2932 return 0;
2935 /* Stop if tag has already been modified. */
2936 if (idx.flag & FLAG_DIRTYNUM)
2937 return 0;
2939 logf("import: %s", tag_data);
2941 idx.flag |= FLAG_DIRTYNUM;
2942 for (i = 0; i < (long)(sizeof(import_tags)/sizeof(import_tags[0])); i++)
2944 int data;
2946 if (!read_tag(tag_data, sizeof tag_data, buf,
2947 tagcache_tag_to_str(import_tags[i])))
2949 continue;
2952 data = atoi(tag_data);
2953 if (data < 0)
2954 continue;
2956 idx.tag_seek[import_tags[i]] = data;
2958 if (import_tags[i] == tag_lastplayed && data > current_tcmh.serial)
2959 current_tcmh.serial = data;
2960 else if (import_tags[i] == tag_commitid && data >= current_tcmh.commitid)
2961 current_tcmh.commitid = data + 1;
2964 return write_index(masterfd, idx_id, &idx) ? 0 : -5;
2967 #ifndef __PCTOOL__
2968 bool tagcache_import_changelog(void)
2970 struct master_header myhdr;
2971 struct tagcache_header tch;
2972 int clfd;
2973 long masterfd;
2974 char buf[2048];
2976 if (!tc_stat.ready)
2977 return false;
2979 while (read_lock)
2980 sleep(1);
2982 clfd = open(TAGCACHE_FILE_CHANGELOG, O_RDONLY);
2983 if (clfd < 0)
2985 logf("failure to open changelog");
2986 return false;
2989 if ( (masterfd = open_master_fd(&myhdr, true)) < 0)
2991 close(clfd);
2992 return false;
2995 write_lock++;
2997 filenametag_fd = open_tag_fd(&tch, tag_filename, false);
2999 fast_readline(clfd, buf, sizeof buf, (long *)masterfd,
3000 parse_changelog_line);
3002 close(clfd);
3003 close(masterfd);
3005 if (filenametag_fd >= 0)
3006 close(filenametag_fd);
3008 write_lock--;
3010 update_master_header();
3012 return true;
3014 #endif
3016 bool tagcache_create_changelog(struct tagcache_search *tcs)
3018 struct master_header myhdr;
3019 struct index_entry idx;
3020 char buf[TAG_MAXLEN+32];
3021 char temp[32];
3022 int clfd;
3023 int i, j;
3025 if (!tc_stat.ready)
3026 return false;
3028 if (!tagcache_search(tcs, tag_filename))
3029 return false;
3031 /* Initialize the changelog */
3032 clfd = open(TAGCACHE_FILE_CHANGELOG, O_WRONLY | O_CREAT | O_TRUNC);
3033 if (clfd < 0)
3035 logf("failure to open changelog");
3036 return false;
3039 if (tcs->masterfd < 0)
3041 if ( (tcs->masterfd = open_master_fd(&myhdr, false)) < 0)
3042 return false;
3044 else
3046 lseek(tcs->masterfd, 0, SEEK_SET);
3047 ecread(tcs->masterfd, &myhdr, 1, master_header_ec, tc_stat.econ);
3050 write(clfd, "## Changelog version 1\n", 23);
3052 for (i = 0; i < myhdr.tch.entry_count; i++)
3054 if (ecread(tcs->masterfd, &idx, 1, index_entry_ec, tc_stat.econ)
3055 != sizeof(struct index_entry))
3057 logf("read error #9");
3058 tagcache_search_finish(tcs);
3059 close(clfd);
3060 return false;
3063 /* Skip until the entry found has been modified. */
3064 if (! (idx.flag & FLAG_DIRTYNUM) )
3065 continue;
3067 /* Skip deleted entries too. */
3068 if (idx.flag & FLAG_DELETED)
3069 continue;
3071 /* Now retrieve all tags. */
3072 for (j = 0; j < TAG_COUNT; j++)
3074 if (tagcache_is_numeric_tag(j))
3076 snprintf(temp, sizeof temp, "%d", idx.tag_seek[j]);
3077 write_tag(clfd, tagcache_tag_to_str(j), temp);
3078 continue;
3081 tcs->type = j;
3082 tagcache_retrieve(tcs, i, tcs->type, buf, sizeof buf);
3083 write_tag(clfd, tagcache_tag_to_str(j), buf);
3086 write(clfd, "\n", 1);
3087 do_timed_yield();
3090 close(clfd);
3092 tagcache_search_finish(tcs);
3094 return true;
3097 static bool delete_entry(long idx_id)
3099 int fd;
3100 int tag, i;
3101 struct index_entry idx, myidx;
3102 struct master_header myhdr;
3103 char buf[TAG_MAXLEN+32];
3104 int in_use[TAG_COUNT];
3106 logf("delete_entry(): %ld", idx_id);
3108 #ifdef HAVE_TC_RAMCACHE
3109 /* At first mark the entry removed from ram cache. */
3110 if (tc_stat.ramcache)
3111 hdr->indices[idx_id].flag |= FLAG_DELETED;
3112 #endif
3114 if ( (fd = open_master_fd(&myhdr, true) ) < 0)
3115 return false;
3117 lseek(fd, idx_id * sizeof(struct index_entry), SEEK_CUR);
3118 if (ecread(fd, &myidx, 1, index_entry_ec, tc_stat.econ)
3119 != sizeof(struct index_entry))
3121 logf("delete_entry(): read error");
3122 close(fd);
3123 return false;
3126 myidx.flag |= FLAG_DELETED;
3127 lseek(fd, -sizeof(struct index_entry), SEEK_CUR);
3128 if (ecwrite(fd, &myidx, 1, index_entry_ec, tc_stat.econ)
3129 != sizeof(struct index_entry))
3131 logf("delete_entry(): write_error");
3132 close(fd);
3133 return false;
3136 /* Now check which tags are no longer in use (if any) */
3137 for (tag = 0; tag < TAG_COUNT; tag++)
3138 in_use[tag] = 0;
3140 lseek(fd, sizeof(struct master_header), SEEK_SET);
3141 for (i = 0; i < myhdr.tch.entry_count; i++)
3143 struct index_entry *idxp;
3145 #ifdef HAVE_TC_RAMCACHE
3146 /* Use RAM DB if available for greater speed */
3147 if (tc_stat.ramcache)
3148 idxp = &hdr->indices[i];
3149 else
3150 #endif
3152 if (ecread(fd, &idx, 1, index_entry_ec, tc_stat.econ)
3153 != sizeof(struct index_entry))
3155 logf("delete_entry(): read error #2");
3156 close(fd);
3157 return false;
3159 idxp = &idx;
3162 if (idxp->flag & FLAG_DELETED)
3163 continue;
3165 for (tag = 0; tag < TAG_COUNT; tag++)
3167 if (tagcache_is_numeric_tag(tag))
3168 continue;
3170 if (idxp->tag_seek[tag] == myidx.tag_seek[tag])
3171 in_use[tag]++;
3175 close(fd);
3177 /* Now delete all tags no longer in use. */
3178 for (tag = 0; tag < TAG_COUNT; tag++)
3180 if (tagcache_is_numeric_tag(tag))
3181 continue;
3183 if (in_use[tag])
3185 logf("in use: %d/%d", tag, in_use[tag]);
3186 continue;
3189 #ifdef HAVE_TC_RAMCACHE
3190 /* Delete from ram. */
3191 if (tc_stat.ramcache && tag != tag_filename)
3193 struct tagfile_entry *tagentry = get_tag(&myidx, tag);
3194 tagentry->tag_data[0] = '\0';
3196 #endif
3198 /* Open the index file, which contains the tag names. */
3199 snprintf(buf, sizeof buf, TAGCACHE_FILE_INDEX, tag);
3200 fd = open(buf, O_RDWR);
3202 if (fd < 0)
3204 logf("open failed");
3205 return false;
3208 /* Skip the header block */
3209 lseek(fd, myidx.tag_seek[tag] + sizeof(struct tagfile_entry), SEEK_SET);
3211 /* Debug, print 10 first characters of the tag
3212 read(fd, buf, 10);
3213 buf[10]='\0';
3214 logf("TAG:%s", buf);
3215 lseek(fd, -10, SEEK_CUR);
3218 /* Write first data byte in tag as \0 */
3219 write(fd, "", 1);
3221 /* Now tag data has been removed */
3222 close(fd);
3225 return true;
3228 #ifndef __PCTOOL__
3230 * Returns true if there is an event waiting in the queue
3231 * that requires the current operation to be aborted.
3233 static bool check_event_queue(void)
3235 struct event ev;
3237 queue_wait_w_tmo(&tagcache_queue, &ev, 0);
3238 switch (ev.id)
3240 case Q_STOP_SCAN:
3241 case SYS_POWEROFF:
3242 case SYS_USB_CONNECTED:
3243 /* Put the event back into the queue. */
3244 queue_post(&tagcache_queue, ev.id, ev.data);
3245 return true;
3248 return false;
3250 #endif
3252 #ifdef HAVE_TC_RAMCACHE
3253 static bool allocate_tagcache(void)
3255 struct master_header tcmh;
3256 int fd;
3258 /* Load the header. */
3259 if ( (fd = open_master_fd(&tcmh, false)) < 0)
3261 hdr = NULL;
3262 return false;
3265 close(fd);
3267 /**
3268 * Now calculate the required cache size plus
3269 * some extra space for alignment fixes.
3271 tc_stat.ramcache_allocated = tcmh.tch.datasize + 128 + TAGCACHE_RESERVE +
3272 sizeof(struct ramcache_header) + TAG_COUNT*sizeof(void *);
3273 hdr = buffer_alloc(tc_stat.ramcache_allocated + 128);
3274 memset(hdr, 0, sizeof(struct ramcache_header));
3275 memcpy(&hdr->h, &tcmh, sizeof(struct master_header));
3276 hdr->indices = (struct index_entry *)(hdr + 1);
3277 logf("tagcache: %d bytes allocated.", tc_stat.ramcache_allocated);
3279 return true;
3282 # ifdef HAVE_EEPROM_SETTINGS
3283 static bool tagcache_dumpload(void)
3285 struct statefile_header shdr;
3286 int fd, rc;
3287 long offpos;
3288 int i;
3290 fd = open(TAGCACHE_STATEFILE, O_RDONLY);
3291 if (fd < 0)
3293 logf("no tagcache statedump");
3294 return false;
3297 /* Check the statefile memory placement */
3298 hdr = buffer_alloc(0);
3299 rc = read(fd, &shdr, sizeof(struct statefile_header));
3300 if (rc != sizeof(struct statefile_header)
3301 /* || (long)hdr != (long)shdr.hdr */)
3303 logf("incorrect statefile");
3304 hdr = NULL;
3305 close(fd);
3306 return false;
3309 offpos = (long)hdr - (long)shdr.hdr;
3311 /* Lets allocate real memory and load it */
3312 hdr = buffer_alloc(shdr.tc_stat.ramcache_allocated);
3313 rc = read(fd, hdr, shdr.tc_stat.ramcache_allocated);
3314 close(fd);
3316 if (rc != shdr.tc_stat.ramcache_allocated)
3318 logf("read failure!");
3319 hdr = NULL;
3320 return false;
3323 memcpy(&tc_stat, &shdr.tc_stat, sizeof(struct tagcache_stat));
3325 /* Now fix the pointers */
3326 hdr->indices = (struct index_entry *)((long)hdr->indices + offpos);
3327 for (i = 0; i < TAG_COUNT; i++)
3328 hdr->tags[i] += offpos;
3330 return true;
3333 static bool tagcache_dumpsave(void)
3335 struct statefile_header shdr;
3336 int fd;
3338 if (!tc_stat.ramcache)
3339 return false;
3341 fd = open(TAGCACHE_STATEFILE, O_WRONLY | O_CREAT | O_TRUNC);
3342 if (fd < 0)
3344 logf("failed to create a statedump");
3345 return false;
3348 /* Create the header */
3349 shdr.hdr = hdr;
3350 memcpy(&shdr.tc_stat, &tc_stat, sizeof(struct tagcache_stat));
3351 write(fd, &shdr, sizeof(struct statefile_header));
3353 /* And dump the data too */
3354 write(fd, hdr, tc_stat.ramcache_allocated);
3355 close(fd);
3357 return true;
3359 # endif
3361 static bool load_tagcache(void)
3363 struct tagcache_header *tch;
3364 long bytesleft = tc_stat.ramcache_allocated;
3365 struct index_entry *idx;
3366 int rc, fd;
3367 char *p;
3368 int i, tag;
3370 # ifdef HAVE_DIRCACHE
3371 while (dircache_is_initializing())
3372 sleep(1);
3373 # endif
3375 logf("loading tagcache to ram...");
3377 fd = open(TAGCACHE_FILE_MASTER, O_RDONLY);
3378 if (fd < 0)
3380 logf("tagcache open failed");
3381 return false;
3384 if (ecread(fd, &hdr->h, 1, master_header_ec, tc_stat.econ)
3385 != sizeof(struct master_header)
3386 || hdr->h.tch.magic != TAGCACHE_MAGIC)
3388 logf("incorrect header");
3389 return false;
3392 idx = hdr->indices;
3394 /* Load the master index table. */
3395 for (i = 0; i < hdr->h.tch.entry_count; i++)
3397 rc = ecread(fd, idx, 1, index_entry_ec, tc_stat.econ);
3398 if (rc != sizeof(struct index_entry))
3400 logf("read error #10");
3401 close(fd);
3402 return false;
3405 bytesleft -= sizeof(struct index_entry);
3406 if (bytesleft < 0 || ((long)idx - (long)hdr->indices) >= tc_stat.ramcache_allocated)
3408 logf("too big tagcache.");
3409 close(fd);
3410 return false;
3413 idx++;
3416 close(fd);
3418 /* Load the tags. */
3419 p = (char *)idx;
3420 for (tag = 0; tag < TAG_COUNT; tag++)
3422 struct tagfile_entry *fe;
3423 char buf[TAG_MAXLEN+32];
3425 if (tagcache_is_numeric_tag(tag))
3426 continue ;
3428 //p = ((void *)p+1);
3429 p = (char *)((long)p & ~0x03) + 0x04;
3430 hdr->tags[tag] = p;
3432 /* Check the header. */
3433 tch = (struct tagcache_header *)p;
3434 p += sizeof(struct tagcache_header);
3436 if ( (fd = open_tag_fd(tch, tag, false)) < 0)
3437 return false;
3439 for (hdr->entry_count[tag] = 0;
3440 hdr->entry_count[tag] < tch->entry_count;
3441 hdr->entry_count[tag]++)
3443 long pos;
3445 if (do_timed_yield())
3447 /* Abort if we got a critical event in queue */
3448 if (check_event_queue())
3449 return false;
3452 fe = (struct tagfile_entry *)p;
3453 pos = lseek(fd, 0, SEEK_CUR);
3454 rc = ecread(fd, fe, 1, tagfile_entry_ec, tc_stat.econ);
3455 if (rc != sizeof(struct tagfile_entry))
3457 /* End of lookup table. */
3458 logf("read error #11");
3459 close(fd);
3460 return false;
3463 /* We have a special handling for the filename tags. */
3464 if (tag == tag_filename)
3466 # ifdef HAVE_DIRCACHE
3467 const struct dircache_entry *dc;
3468 # endif
3470 // FIXME: This is wrong!
3471 // idx = &hdr->indices[hdr->entry_count[i]];
3472 idx = &hdr->indices[fe->idx_id];
3474 if (fe->tag_length >= (long)sizeof(buf)-1)
3476 read(fd, buf, 10);
3477 buf[10] = '\0';
3478 logf("TAG:%s", buf);
3479 logf("too long filename");
3480 close(fd);
3481 return false;
3484 rc = read(fd, buf, fe->tag_length);
3485 if (rc != fe->tag_length)
3487 logf("read error #12");
3488 close(fd);
3489 return false;
3492 /* Check if the entry has already been removed */
3493 if (idx->flag & FLAG_DELETED)
3494 continue;
3496 /* This flag must not be used yet. */
3497 if (idx->flag & FLAG_DIRCACHE)
3499 logf("internal error!");
3500 close(fd);
3501 return false;
3504 if (idx->tag_seek[tag] != pos)
3506 logf("corrupt data structures!");
3507 close(fd);
3508 return false;
3511 # ifdef HAVE_DIRCACHE
3512 if (dircache_is_enabled())
3514 dc = dircache_get_entry_ptr(buf);
3515 if (dc == NULL)
3517 logf("Entry no longer valid.");
3518 logf("-> %s", buf);
3519 delete_entry(fe->idx_id);
3520 continue ;
3523 idx->flag |= FLAG_DIRCACHE;
3524 FLAG_SET_ATTR(idx->flag, fe->idx_id);
3525 idx->tag_seek[tag_filename] = (long)dc;
3527 else
3528 # endif
3531 # if 0 /* Maybe we could enable this for flash players. Too slow otherwise. */
3532 /* Check if entry has been removed. */
3533 if (global_settings.tagcache_autoupdate)
3535 int testfd;
3537 testfd = open(buf, O_RDONLY);
3538 if (testfd < 0)
3540 logf("Entry no longer valid.");
3541 logf("-> %s", buf);
3542 delete_entry(fe->idx_id);
3543 continue;
3545 close(testfd);
3547 # endif
3550 continue ;
3553 bytesleft -= sizeof(struct tagfile_entry) + fe->tag_length;
3554 if (bytesleft < 0)
3556 logf("too big tagcache #2");
3557 logf("tl: %d", fe->tag_length);
3558 logf("bl: %ld", bytesleft);
3559 close(fd);
3560 return false;
3563 p = fe->tag_data;
3564 rc = read(fd, fe->tag_data, fe->tag_length);
3565 p += rc;
3567 if (rc != fe->tag_length)
3569 logf("read error #13");
3570 logf("rc=0x%04x", rc); // 0x431
3571 logf("len=0x%04x", fe->tag_length); // 0x4000
3572 logf("pos=0x%04lx", lseek(fd, 0, SEEK_CUR)); // 0x433
3573 logf("tag=0x%02x", tag); // 0x00
3574 close(fd);
3575 return false;
3578 close(fd);
3581 tc_stat.ramcache_used = tc_stat.ramcache_allocated - bytesleft;
3582 logf("tagcache loaded into ram!");
3584 return true;
3586 #endif /* HAVE_TC_RAMCACHE */
3588 static bool check_deleted_files(void)
3590 int fd, testfd;
3591 char buf[TAG_MAXLEN+32];
3592 struct tagfile_entry tfe;
3594 logf("reverse scan...");
3595 snprintf(buf, sizeof buf, TAGCACHE_FILE_INDEX, tag_filename);
3596 fd = open(buf, O_RDONLY);
3598 if (fd < 0)
3600 logf("%s open fail", buf);
3601 return false;
3604 lseek(fd, sizeof(struct tagcache_header), SEEK_SET);
3605 while (ecread(fd, &tfe, 1, tagfile_entry_ec, tc_stat.econ)
3606 == sizeof(struct tagfile_entry)
3607 #ifndef __PCTOOL__
3608 && !check_event_queue()
3609 #endif
3612 if (tfe.tag_length >= (long)sizeof(buf)-1)
3614 logf("too long tag");
3615 close(fd);
3616 return false;
3619 if (read(fd, buf, tfe.tag_length) != tfe.tag_length)
3621 logf("read error #14");
3622 close(fd);
3623 return false;
3626 /* Check if the file has already deleted from the db. */
3627 if (*buf == '\0')
3628 continue;
3630 /* Now check if the file exists. */
3631 testfd = open(buf, O_RDONLY);
3632 if (testfd < 0)
3634 logf("Entry no longer valid.");
3635 logf("-> %s / %d", buf, tfe.tag_length);
3636 delete_entry(tfe.idx_id);
3638 close(testfd);
3641 close(fd);
3643 logf("done");
3645 return true;
3648 static bool check_dir(const char *dirname)
3650 DIRCACHED *dir;
3651 int len;
3652 int success = false;
3654 dir = opendir_cached(dirname);
3655 if (!dir)
3657 logf("tagcache: opendir_cached() failed");
3658 return false;
3661 /* Recursively scan the dir. */
3662 #ifdef __PCTOOL__
3663 while (1)
3664 #else
3665 while (!check_event_queue())
3666 #endif
3668 struct dircache_entry *entry;
3670 entry = readdir_cached(dir);
3672 if (entry == NULL)
3674 success = true;
3675 break ;
3678 if (!strcmp((char *)entry->d_name, ".") ||
3679 !strcmp((char *)entry->d_name, ".."))
3680 continue;
3682 yield();
3684 len = strlen(curpath);
3685 snprintf(&curpath[len], curpath_size - len, "/%s",
3686 entry->d_name);
3688 processed_dir_count++;
3689 if (entry->attribute & ATTR_DIRECTORY)
3690 check_dir(curpath);
3691 else
3692 #if defined(HAVE_TC_RAMCACHE) && defined(HAVE_DIRCACHE)
3693 add_tagcache(curpath, dir->internal_entry);
3694 #else
3695 add_tagcache(curpath);
3696 #endif
3698 curpath[len] = '\0';
3701 closedir_cached(dir);
3703 return success;
3706 void build_tagcache(const char *path)
3708 struct tagcache_header header;
3709 bool ret;
3711 curpath[0] = '\0';
3712 data_size = 0;
3713 total_entry_count = 0;
3714 processed_dir_count = 0;
3716 #ifdef HAVE_DIRCACHE
3717 while (dircache_is_initializing())
3718 sleep(1);
3719 #endif
3721 logf("updating tagcache");
3723 cachefd = open(TAGCACHE_FILE_TEMP, O_RDONLY);
3724 if (cachefd >= 0)
3726 logf("skipping, cache already waiting for commit");
3727 close(cachefd);
3728 return ;
3731 cachefd = open(TAGCACHE_FILE_TEMP, O_RDWR | O_CREAT | O_TRUNC);
3732 if (cachefd < 0)
3734 logf("master file open failed: %s", TAGCACHE_FILE_TEMP);
3735 return ;
3738 filenametag_fd = open_tag_fd(&header, tag_filename, false);
3740 cpu_boost(true);
3742 logf("Scanning files...");
3743 /* Scan for new files. */
3744 memset(&header, 0, sizeof(struct tagcache_header));
3745 write(cachefd, &header, sizeof(struct tagcache_header));
3747 if (strcmp("/", path) != 0)
3748 strcpy(curpath, path);
3749 ret = check_dir(path);
3751 /* Write the header. */
3752 header.magic = TAGCACHE_MAGIC;
3753 header.datasize = data_size;
3754 header.entry_count = total_entry_count;
3755 lseek(cachefd, 0, SEEK_SET);
3756 write(cachefd, &header, sizeof(struct tagcache_header));
3757 close(cachefd);
3759 if (filenametag_fd >= 0)
3761 close(filenametag_fd);
3762 filenametag_fd = -1;
3765 if (!ret)
3767 logf("Aborted.");
3768 cpu_boost(false);
3769 return ;
3772 /* Commit changes to the database. */
3773 #ifdef __PCTOOL__
3774 allocate_tempbuf();
3775 #endif
3776 if (commit())
3778 remove(TAGCACHE_FILE_TEMP);
3779 logf("tagcache built!");
3781 #ifdef __PCTOOL__
3782 free_tempbuf();
3783 #endif
3785 #ifdef HAVE_TC_RAMCACHE
3786 if (hdr)
3788 /* Import runtime statistics if we just initialized the db. */
3789 if (hdr->h.serial == 0)
3790 queue_post(&tagcache_queue, Q_IMPORT_CHANGELOG, 0);
3792 #endif
3794 cpu_boost(false);
3797 #ifdef HAVE_TC_RAMCACHE
3798 static void load_ramcache(void)
3800 if (!hdr)
3801 return ;
3803 cpu_boost(true);
3805 /* At first we should load the cache (if exists). */
3806 tc_stat.ramcache = load_tagcache();
3808 if (!tc_stat.ramcache)
3810 /* If loading failed, it must indicate some problem with the db
3811 * so disable it entirely to prevent further issues. */
3812 tc_stat.ready = false;
3813 hdr = NULL;
3816 cpu_boost(false);
3819 void tagcache_unload_ramcache(void)
3821 tc_stat.ramcache = false;
3822 /* Just to make sure there is no statefile present. */
3823 // remove(TAGCACHE_STATEFILE);
3825 #endif
3827 #ifndef __PCTOOL__
3828 static void tagcache_thread(void)
3830 struct event ev;
3831 bool check_done = false;
3833 /* If the previous cache build/update was interrupted, commit
3834 * the changes first in foreground. */
3835 cpu_boost(true);
3836 allocate_tempbuf();
3837 commit();
3838 free_tempbuf();
3840 #ifdef HAVE_TC_RAMCACHE
3841 # ifdef HAVE_EEPROM_SETTINGS
3842 if (firmware_settings.initialized && firmware_settings.disk_clean)
3843 check_done = tagcache_dumpload();
3845 remove(TAGCACHE_STATEFILE);
3846 # endif
3848 /* Allocate space for the tagcache if found on disk. */
3849 if (global_settings.tagcache_ram && !tc_stat.ramcache)
3850 allocate_tagcache();
3851 #endif
3853 cpu_boost(false);
3854 tc_stat.initialized = true;
3856 /* Don't delay bootup with the header check but do it on background. */
3857 sleep(HZ);
3858 tc_stat.ready = check_all_headers();
3859 tc_stat.readyvalid = true;
3861 while (1)
3863 queue_wait_w_tmo(&tagcache_queue, &ev, HZ);
3865 switch (ev.id)
3867 case Q_IMPORT_CHANGELOG:
3868 tagcache_import_changelog();
3869 break;
3871 case Q_REBUILD:
3872 remove_files();
3873 build_tagcache("/");
3874 break;
3876 case Q_UPDATE:
3877 build_tagcache("/");
3878 #ifdef HAVE_TC_RAMCACHE
3879 load_ramcache();
3880 #endif
3881 check_deleted_files();
3882 break ;
3884 case Q_START_SCAN:
3885 check_done = false;
3886 case SYS_TIMEOUT:
3887 if (check_done || !tc_stat.ready)
3888 break ;
3890 #ifdef HAVE_TC_RAMCACHE
3891 if (!tc_stat.ramcache && global_settings.tagcache_ram)
3893 load_ramcache();
3894 if (tc_stat.ramcache && global_settings.tagcache_autoupdate)
3895 build_tagcache("/");
3897 else
3898 #endif
3899 if (global_settings.tagcache_autoupdate)
3901 build_tagcache("/");
3902 /* Don't do auto removal without dircache (very slow). */
3903 #ifdef HAVE_DIRCACHE
3904 if (dircache_is_enabled())
3905 check_deleted_files();
3906 #endif
3909 logf("tagcache check done");
3911 check_done = true;
3912 break ;
3914 case Q_STOP_SCAN:
3915 break ;
3917 case SYS_POWEROFF:
3918 break ;
3920 #ifndef SIMULATOR
3921 case SYS_USB_CONNECTED:
3922 logf("USB: TagCache");
3923 usb_acknowledge(SYS_USB_CONNECTED_ACK);
3924 usb_wait_for_disconnect(&tagcache_queue);
3925 break ;
3926 #endif
3931 bool tagcache_prepare_shutdown(void)
3933 if (tagcache_get_commit_step() > 0)
3934 return false;
3936 tagcache_stop_scan();
3937 while (read_lock || write_lock)
3938 sleep(1);
3940 return true;
3943 void tagcache_shutdown(void)
3945 #ifdef HAVE_EEPROM_SETTINGS
3946 if (tc_stat.ramcache)
3947 tagcache_dumpsave();
3948 #endif
3951 static int get_progress(void)
3953 int total_count = -1;
3955 #ifdef HAVE_DIRCACHE
3956 if (dircache_is_enabled())
3958 total_count = dircache_get_entry_count();
3960 else
3961 #endif
3962 #ifdef HAVE_TC_RAMCACHE
3964 if (hdr && tc_stat.ramcache)
3965 total_count = hdr->h.tch.entry_count;
3967 #endif
3969 if (total_count < 0)
3970 return -1;
3972 return processed_dir_count * 100 / total_count;
3975 struct tagcache_stat* tagcache_get_stat(void)
3977 tc_stat.progress = get_progress();
3978 tc_stat.processed_entries = processed_dir_count;
3980 return &tc_stat;
3983 void tagcache_start_scan(void)
3985 queue_post(&tagcache_queue, Q_START_SCAN, 0);
3988 bool tagcache_update(void)
3990 if (!tc_stat.ready)
3991 return false;
3993 queue_post(&tagcache_queue, Q_UPDATE, 0);
3994 return false;
3997 bool tagcache_rebuild()
3999 queue_post(&tagcache_queue, Q_REBUILD, 0);
4000 return false;
4003 void tagcache_stop_scan(void)
4005 queue_post(&tagcache_queue, Q_STOP_SCAN, 0);
4008 #ifdef HAVE_TC_RAMCACHE
4009 bool tagcache_is_ramcache(void)
4011 return tc_stat.ramcache;
4013 #endif
4015 #endif /* !__PCTOOL__ */
4018 void tagcache_init(void)
4020 memset(&tc_stat, 0, sizeof(struct tagcache_stat));
4021 memset(&current_tcmh, 0, sizeof(struct master_header));
4022 filenametag_fd = -1;
4023 write_lock = read_lock = 0;
4025 #ifndef __PCTOOL__
4026 queue_init(&tagcache_queue, true);
4027 create_thread(tagcache_thread, tagcache_stack,
4028 sizeof(tagcache_stack), tagcache_thread_name
4029 IF_PRIO(, PRIORITY_BACKGROUND)
4030 IF_COP(, CPU, false));
4031 #else
4032 tc_stat.initialized = true;
4033 allocate_tempbuf();
4034 commit();
4035 free_tempbuf();
4036 tc_stat.ready = check_all_headers();
4037 #endif
4040 #ifdef __PCTOOL__
4041 void tagcache_reverse_scan(void)
4043 logf("Checking for deleted files");
4044 check_deleted_files();
4046 #endif
4048 bool tagcache_is_initialized(void)
4050 return tc_stat.initialized;
4052 bool tagcache_is_usable(void)
4054 return tc_stat.initialized && tc_stat.ready;
4056 int tagcache_get_commit_step(void)
4058 return tc_stat.commit_step;
4060 int tagcache_get_max_commit_step(void)
4062 return (int)(sizeof(sorted_tags)/sizeof(sorted_tags[0]))+1;