Pull yesno_pop out of the radio code as it is a nice simple resuasble yesno api worth...
[kugel-rb.git] / apps / tagcache.c
blob089bba13aacda9dc236ad600601277b7d28afec7
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2005 by Miika Pekkarinen
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
23 * TagCache API
25 * ----------x---------x------------------x-----
26 * | | | External
27 * +---------------x-------+ | TagCache | Libraries
28 * | Modification routines | | Core |
29 * +-x---------x-----------+ | |
30 * | (R/W) | | | |
31 * | +------x-------------x-+ +-------------x-----+ |
32 * | | x==x Filters & clauses | |
33 * | | Search routines | +-------------------+ |
34 * | | x============================x DirCache
35 * | +-x--------------------+ | (optional)
36 * | | (R) |
37 * | | +-------------------------------+ +---------+ |
38 * | | | DB Commit (sort,unique,index) | | | |
39 * | | +-x--------------------------x--+ | Control | |
40 * | | | (R/W) | (R) | Thread | |
41 * | | | +----------------------+ | | | |
42 * | | | | TagCache DB Builder | | +---------+ |
43 * | | | +-x-------------x------+ | |
44 * | | | | (R) | (W) | |
45 * | | | | +--x--------x---------+ |
46 * | | | | | Temporary Commit DB | |
47 * | | | | +---------------------+ |
48 * +-x----x-------x--+ |
49 * | TagCache RAM DB x==\(W) +-----------------+ |
50 * +-----------------+ \===x | |
51 * | | | | (R) | Ram DB Loader x============x DirCache
52 * +-x----x---x---x---+ /==x | | (optional)
53 * | Tagcache Disk DB x==/ +-----------------+ |
54 * +------------------+ |
58 /*#define LOGF_ENABLE*/
60 #include <stdio.h>
61 #include <stdlib.h>
62 #include <ctype.h>
63 #ifdef APPLICATION
64 #include <unistd.h> /* readlink() */
65 #include <limits.h> /* PATH_MAX */
66 #endif
67 #include "config.h"
68 #include "ata_idle_notify.h"
69 #include "thread.h"
70 #include "kernel.h"
71 #include "system.h"
72 #include "logf.h"
73 #include "string-extra.h"
74 #include "usb.h"
75 #include "metadata.h"
76 #include "tagcache.h"
77 #include "buffer.h"
78 #include "crc32.h"
79 #include "misc.h"
80 #include "settings.h"
81 #include "dir.h"
82 #include "filefuncs.h"
83 #include "structec.h"
84 #include "debug.h"
86 #ifndef __PCTOOL__
87 #include "lang.h"
88 #include "eeprom_settings.h"
89 #endif
91 #ifdef __PCTOOL__
92 #define yield() do { } while(0)
93 #define sim_sleep(timeout) do { } while(0)
94 #define do_timed_yield() do { } while(0)
95 #endif
97 #ifndef __PCTOOL__
98 /* Tag Cache thread. */
99 static struct event_queue tagcache_queue SHAREDBSS_ATTR;
100 static long tagcache_stack[(DEFAULT_STACK_SIZE + 0x4000)/sizeof(long)];
101 static const char tagcache_thread_name[] = "tagcache";
102 #endif
104 /* Previous path when scanning directory tree recursively. */
105 static char curpath[TAG_MAXLEN+32];
107 /* Used when removing duplicates. */
108 static char *tempbuf; /* Allocated when needed. */
109 static long tempbufidx; /* Current location in buffer. */
110 static long tempbuf_size; /* Buffer size (TEMPBUF_SIZE). */
111 static long tempbuf_left; /* Buffer space left. */
112 static long tempbuf_pos;
114 #define SORTED_TAGS_COUNT 8
115 #define TAGCACHE_IS_UNIQUE(tag) (BIT_N(tag) & TAGCACHE_UNIQUE_TAGS)
116 #define TAGCACHE_IS_SORTED(tag) (BIT_N(tag) & TAGCACHE_SORTED_TAGS)
117 #define TAGCACHE_IS_NUMERIC_OR_NONUNIQUE(tag) \
118 (BIT_N(tag) & (TAGCACHE_NUMERIC_TAGS | ~TAGCACHE_UNIQUE_TAGS))
119 /* Tags we want to get sorted (loaded to the tempbuf). */
120 #define TAGCACHE_SORTED_TAGS ((1LU << tag_artist) | (1LU << tag_album) | \
121 (1LU << tag_genre) | (1LU << tag_composer) | (1LU << tag_comment) | \
122 (1LU << tag_albumartist) | (1LU << tag_grouping) | (1LU << tag_title))
124 /* Uniqued tags (we can use these tags with filters and conditional clauses). */
125 #define TAGCACHE_UNIQUE_TAGS ((1LU << tag_artist) | (1LU << tag_album) | \
126 (1LU << tag_genre) | (1LU << tag_composer) | (1LU << tag_comment) | \
127 (1LU << tag_albumartist) | (1LU << tag_grouping))
129 /* String presentation of the tags defined in tagcache.h. Must be in correct order! */
130 static const char *tags_str[] = { "artist", "album", "genre", "title",
131 "filename", "composer", "comment", "albumartist", "grouping", "year",
132 "discnumber", "tracknumber", "bitrate", "length", "playcount", "rating",
133 "playtime", "lastplayed", "commitid", "mtime", "lastoffset" };
135 /* Status information of the tagcache. */
136 static struct tagcache_stat tc_stat;
138 /* Queue commands. */
139 enum tagcache_queue {
140 Q_STOP_SCAN = 0,
141 Q_START_SCAN,
142 Q_IMPORT_CHANGELOG,
143 Q_UPDATE,
144 Q_REBUILD,
146 /* Internal tagcache command queue. */
147 CMD_UPDATE_MASTER_HEADER,
148 CMD_UPDATE_NUMERIC,
151 struct tagcache_command_entry {
152 int32_t command;
153 int32_t idx_id;
154 int32_t tag;
155 int32_t data;
158 #ifndef __PCTOOL__
159 static struct tagcache_command_entry command_queue[TAGCACHE_COMMAND_QUEUE_LENGTH];
160 static volatile int command_queue_widx = 0;
161 static volatile int command_queue_ridx = 0;
162 static struct mutex command_queue_mutex SHAREDBSS_ATTR;
163 #endif
165 /* Tag database structures. */
167 /* Variable-length tag entry in tag files. */
168 struct tagfile_entry {
169 int32_t tag_length; /* Length of the data in bytes including '\0' */
170 int32_t idx_id; /* Corresponding entry location in index file of not unique tags */
171 char tag_data[0]; /* Begin of the tag data */
174 /* Fixed-size tag entry in master db index. */
175 struct index_entry {
176 int32_t tag_seek[TAG_COUNT]; /* Location of tag data or numeric tag data */
177 int32_t flag; /* Status flags */
180 /* Header is the same in every file. */
181 struct tagcache_header {
182 int32_t magic; /* Header version number */
183 int32_t datasize; /* Data size in bytes */
184 int32_t entry_count; /* Number of entries in this file */
187 struct master_header {
188 struct tagcache_header tch;
189 int32_t serial; /* Increasing counting number */
190 int32_t commitid; /* Number of commits so far */
191 int32_t dirty;
194 /* For the endianess correction */
195 static const char *tagfile_entry_ec = "ll";
197 Note: This should be (1 + TAG_COUNT) amount of l's.
199 static const char *index_entry_ec = "llllllllllllllllllllll";
201 static const char *tagcache_header_ec = "lll";
202 static const char *master_header_ec = "llllll";
204 static struct master_header current_tcmh;
206 #ifdef HAVE_TC_RAMCACHE
207 /* Header is created when loading database to ram. */
208 struct ramcache_header {
209 char *tags[TAG_COUNT]; /* Tag file content (not including filename tag) */
210 int entry_count[TAG_COUNT]; /* Number of entries in the indices. */
211 struct index_entry indices[0]; /* Master index file content */
214 # ifdef HAVE_EEPROM_SETTINGS
215 struct statefile_header {
216 int32_t magic; /* Statefile version number */
217 struct master_header mh; /* Header from the master index */
218 struct ramcache_header *hdr; /* Old load address of hdr for relocation */
219 struct tagcache_stat tc_stat;
221 # endif
223 /* Pointer to allocated ramcache_header */
224 static struct ramcache_header *hdr;
225 #endif
227 /**
228 * Full tag entries stored in a temporary file waiting
229 * for commit to the cache. */
230 struct temp_file_entry {
231 long tag_offset[TAG_COUNT];
232 short tag_length[TAG_COUNT];
233 long flag;
235 long data_length;
238 struct tempbuf_id_list {
239 long id;
240 struct tempbuf_id_list *next;
243 struct tempbuf_searchidx {
244 long idx_id;
245 char *str;
246 int seek;
247 struct tempbuf_id_list idlist;
250 /* Lookup buffer for fixing messed up index while after sorting. */
251 static long commit_entry_count;
252 static long lookup_buffer_depth;
253 static struct tempbuf_searchidx **lookup;
255 /* Used when building the temporary file. */
256 static int cachefd = -1, filenametag_fd;
257 static int total_entry_count = 0;
258 static int data_size = 0;
259 static int processed_dir_count;
261 /* Thread safe locking */
262 static volatile int write_lock;
263 static volatile int read_lock;
265 static bool delete_entry(long idx_id);
267 const char* tagcache_tag_to_str(int tag)
269 return tags_str[tag];
272 /* Helper functions for the two most read/write data structure: tagfile_entry and index_entry */
273 static ssize_t ecread_tagfile_entry(int fd, struct tagfile_entry *buf)
275 return ecread(fd, buf, 1, tagfile_entry_ec, tc_stat.econ);
278 static ssize_t ecread_index_entry(int fd, struct index_entry *buf)
280 return ecread(fd, buf, 1, index_entry_ec, tc_stat.econ);
283 static ssize_t ecwrite_index_entry(int fd, struct index_entry *buf)
285 return ecwrite(fd, buf, 1, index_entry_ec, tc_stat.econ);
288 #ifdef HAVE_DIRCACHE
290 * Returns true if specified flag is still present, i.e., dircache
291 * has not been reloaded.
293 static bool is_dircache_intact(void)
295 return dircache_get_appflag(DIRCACHE_APPFLAG_TAGCACHE);
297 #endif
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 < 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 static int open_master_fd(struct master_header *hdr, bool write)
333 int fd;
334 int rc;
336 fd = open(TAGCACHE_FILE_MASTER, write ? O_RDWR : O_RDONLY);
337 if (fd < 0)
339 logf("master file open failed for R/W");
340 tc_stat.ready = false;
341 return fd;
344 tc_stat.econ = false;
346 /* Check the header. */
347 rc = read(fd, hdr, sizeof(struct master_header));
348 if (hdr->tch.magic == TAGCACHE_MAGIC && rc == sizeof(struct master_header))
350 /* Success. */
351 return fd;
354 /* Trying to read again, this time with endianess correction enabled. */
355 lseek(fd, 0, SEEK_SET);
357 rc = ecread(fd, hdr, 1, master_header_ec, true);
358 if (hdr->tch.magic != TAGCACHE_MAGIC || rc != sizeof(struct master_header))
360 logf("header error");
361 tc_stat.ready = false;
362 close(fd);
363 return -2;
366 tc_stat.econ = true;
368 return fd;
371 #ifndef __PCTOOL__
372 static bool do_timed_yield(void)
374 /* Sorting can lock up for quite a while, so yield occasionally */
375 static long wakeup_tick = 0;
376 if (TIME_AFTER(current_tick, wakeup_tick))
378 wakeup_tick = current_tick + (HZ/4);
379 yield();
380 return true;
382 return false;
384 #endif
386 #if defined(HAVE_TC_RAMCACHE) && defined(HAVE_DIRCACHE)
387 /* find the ramcache entry corresponding to the file indicated by
388 * filename and dc (it's corresponding dircache id). */
389 static long find_entry_ram(const char *filename, int dc)
391 static long last_pos = 0;
392 int i;
394 /* Check if tagcache is loaded into ram. */
395 if (!tc_stat.ramcache)
396 return -1;
398 if (dc < 0)
399 dc = dircache_get_entry_id(filename);
401 if (dc < 0)
403 logf("tagcache: file not found.");
404 return -1;
407 try_again:
409 if (last_pos > 0)
410 i = last_pos;
411 else
412 i = 0;
414 for (; i < current_tcmh.tch.entry_count; i++)
416 if (hdr->indices[i].tag_seek[tag_filename] == dc)
418 last_pos = MAX(0, i - 3);
419 return i;
422 do_timed_yield();
425 if (last_pos > 0)
427 last_pos = 0;
428 goto try_again;
431 return -1;
433 #endif
435 static long find_entry_disk(const char *filename_raw, bool localfd)
437 struct tagcache_header tch;
438 static long last_pos = -1;
439 long pos_history[POS_HISTORY_COUNT];
440 long pos_history_idx = 0;
441 bool found = false;
442 struct tagfile_entry tfe;
443 int fd;
444 char buf[TAG_MAXLEN+32];
445 int i;
446 int pos = -1;
448 const char *filename = filename_raw;
449 #ifdef APPLICATION
450 char pathbuf[PATH_MAX]; /* Note: Don't use MAX_PATH here, it's too small */
451 if (realpath(filename, pathbuf) == pathbuf)
452 filename = pathbuf;
453 #endif
455 if (!tc_stat.ready)
456 return -2;
458 fd = filenametag_fd;
459 if (fd < 0 || localfd)
461 last_pos = -1;
462 if ( (fd = open_tag_fd(&tch, tag_filename, false)) < 0)
463 return -1;
466 check_again:
468 if (last_pos > 0)
469 lseek(fd, last_pos, SEEK_SET);
470 else
471 lseek(fd, sizeof(struct tagcache_header), SEEK_SET);
473 while (true)
475 pos = lseek(fd, 0, SEEK_CUR);
476 for (i = pos_history_idx-1; i >= 0; i--)
477 pos_history[i+1] = pos_history[i];
478 pos_history[0] = pos;
480 if (ecread_tagfile_entry(fd, &tfe)
481 != sizeof(struct tagfile_entry))
483 break ;
486 if (tfe.tag_length >= (long)sizeof(buf))
488 logf("too long tag #1");
489 close(fd);
490 if (!localfd)
491 filenametag_fd = -1;
492 last_pos = -1;
493 return -2;
496 if (read(fd, buf, tfe.tag_length) != tfe.tag_length)
498 logf("read error #2");
499 close(fd);
500 if (!localfd)
501 filenametag_fd = -1;
502 last_pos = -1;
503 return -3;
506 if (!strcasecmp(filename, buf))
508 last_pos = pos_history[pos_history_idx];
509 found = true;
510 break ;
513 if (pos_history_idx < POS_HISTORY_COUNT - 1)
514 pos_history_idx++;
517 /* Not found? */
518 if (!found)
520 if (last_pos > 0)
522 last_pos = -1;
523 logf("seek again");
524 goto check_again;
527 if (fd != filenametag_fd || localfd)
528 close(fd);
529 return -4;
532 if (fd != filenametag_fd || localfd)
533 close(fd);
535 return tfe.idx_id;
538 static int find_index(const char *filename)
540 long idx_id = -1;
542 #if defined(HAVE_TC_RAMCACHE) && defined(HAVE_DIRCACHE)
543 if (tc_stat.ramcache && is_dircache_intact())
544 idx_id = find_entry_ram(filename, -1);
545 #endif
547 if (idx_id < 0)
548 idx_id = find_entry_disk(filename, true);
550 return idx_id;
553 bool tagcache_find_index(struct tagcache_search *tcs, const char *filename)
555 int idx_id;
557 if (!tc_stat.ready)
558 return false;
560 idx_id = find_index(filename);
561 if (idx_id < 0)
562 return false;
564 if (!tagcache_search(tcs, tag_filename))
565 return false;
567 tcs->entry_count = 0;
568 tcs->idx_id = idx_id;
570 return true;
573 static bool get_index(int masterfd, int idxid,
574 struct index_entry *idx, bool use_ram)
576 bool localfd = false;
578 if (idxid < 0)
580 logf("Incorrect idxid: %d", idxid);
581 return false;
584 #ifdef HAVE_TC_RAMCACHE
585 if (tc_stat.ramcache && use_ram)
587 if (hdr->indices[idxid].flag & FLAG_DELETED)
588 return false;
590 # ifdef HAVE_DIRCACHE
591 if (!(hdr->indices[idxid].flag & FLAG_DIRCACHE)
592 || is_dircache_intact())
593 #endif
595 memcpy(idx, &hdr->indices[idxid], sizeof(struct index_entry));
596 return true;
599 #else
600 (void)use_ram;
601 #endif
603 if (masterfd < 0)
605 struct master_header tcmh;
607 localfd = true;
608 masterfd = open_master_fd(&tcmh, false);
609 if (masterfd < 0)
610 return false;
613 lseek(masterfd, idxid * sizeof(struct index_entry)
614 + sizeof(struct master_header), SEEK_SET);
615 if (ecread_index_entry(masterfd, idx)
616 != sizeof(struct index_entry))
618 logf("read error #3");
619 if (localfd)
620 close(masterfd);
622 return false;
625 if (localfd)
626 close(masterfd);
628 if (idx->flag & FLAG_DELETED)
629 return false;
631 return true;
634 #ifndef __PCTOOL__
636 static bool write_index(int masterfd, int idxid, struct index_entry *idx)
638 /* We need to exclude all memory only flags & tags when writing to disk. */
639 if (idx->flag & FLAG_DIRCACHE)
641 logf("memory only flags!");
642 return false;
645 #ifdef HAVE_TC_RAMCACHE
646 /* Only update numeric data. Writing the whole index to RAM by memcpy
647 * destroys dircache pointers!
649 if (tc_stat.ramcache)
651 int tag;
652 struct index_entry *idx_ram = &hdr->indices[idxid];
654 for (tag = 0; tag < TAG_COUNT; tag++)
656 if (TAGCACHE_IS_NUMERIC(tag))
658 idx_ram->tag_seek[tag] = idx->tag_seek[tag];
662 /* Don't touch the dircache flag or attributes. */
663 idx_ram->flag = (idx->flag & 0x0000ffff)
664 | (idx_ram->flag & (0xffff0000 | FLAG_DIRCACHE));
666 #endif
668 lseek(masterfd, idxid * sizeof(struct index_entry)
669 + sizeof(struct master_header), SEEK_SET);
670 if (ecwrite_index_entry(masterfd, idx) != sizeof(struct index_entry))
672 logf("write error #3");
673 logf("idxid: %d", idxid);
674 return false;
677 return true;
680 #endif /* !__PCTOOL__ */
682 static bool open_files(struct tagcache_search *tcs, int tag)
684 if (tcs->idxfd[tag] < 0)
686 char fn[MAX_PATH];
688 snprintf(fn, sizeof fn, TAGCACHE_FILE_INDEX, tag);
689 tcs->idxfd[tag] = open(fn, O_RDONLY);
692 if (tcs->idxfd[tag] < 0)
694 logf("File not open!");
695 return false;
698 return true;
701 static bool retrieve(struct tagcache_search *tcs, struct index_entry *idx,
702 int tag, char *buf, long size)
704 struct tagfile_entry tfe;
705 long seek;
707 *buf = '\0';
709 if (TAGCACHE_IS_NUMERIC(tag))
710 return false;
712 seek = idx->tag_seek[tag];
713 if (seek < 0)
715 logf("Retrieve failed");
716 return false;
719 #ifdef HAVE_TC_RAMCACHE
720 if (tcs->ramsearch)
722 struct tagfile_entry *ep;
724 # ifdef HAVE_DIRCACHE
725 if (tag == tag_filename && (idx->flag & FLAG_DIRCACHE)
726 && is_dircache_intact())
728 /* for tag_filename, seek is a dircache index */
729 dircache_copy_path(seek, buf, size);
730 return true;
732 else
733 # endif
734 if (tag != tag_filename)
736 ep = (struct tagfile_entry *)&hdr->tags[tag][seek];
737 strlcpy(buf, ep->tag_data, size);
739 return true;
742 #endif
744 if (!open_files(tcs, tag))
745 return false;
747 lseek(tcs->idxfd[tag], seek, SEEK_SET);
748 if (ecread_tagfile_entry(tcs->idxfd[tag], &tfe)
749 != sizeof(struct tagfile_entry))
751 logf("read error #5");
752 return false;
755 if (tfe.tag_length >= size)
757 logf("too small buffer");
758 return false;
761 if (read(tcs->idxfd[tag], buf, tfe.tag_length) !=
762 tfe.tag_length)
764 logf("read error #6");
765 return false;
768 buf[tfe.tag_length] = '\0';
770 return true;
773 #define COMMAND_QUEUE_IS_EMPTY (command_queue_ridx == command_queue_widx)
775 static long find_tag(int tag, int idx_id, const struct index_entry *idx)
777 #ifndef __PCTOOL__
778 if (! COMMAND_QUEUE_IS_EMPTY && TAGCACHE_IS_NUMERIC(tag))
780 /* Attempt to find tag data through store-to-load forwarding in
781 command queue */
782 long result = -1;
784 mutex_lock(&command_queue_mutex);
786 int ridx = command_queue_widx;
788 while (ridx != command_queue_ridx)
790 if (--ridx < 0)
791 ridx = TAGCACHE_COMMAND_QUEUE_LENGTH - 1;
793 if (command_queue[ridx].command == CMD_UPDATE_NUMERIC
794 && command_queue[ridx].idx_id == idx_id
795 && command_queue[ridx].tag == tag)
797 result = command_queue[ridx].data;
798 break;
802 mutex_unlock(&command_queue_mutex);
804 if (result >= 0)
806 logf("find_tag: "
807 "Recovered tag %d value %lX from write queue",
808 tag, result);
809 return result;
812 #endif
814 return idx->tag_seek[tag];
818 static long check_virtual_tags(int tag, int idx_id,
819 const struct index_entry *idx)
821 long data = 0;
823 switch (tag)
825 case tag_virt_length_sec:
826 data = (find_tag(tag_length, idx_id, idx)/1000) % 60;
827 break;
829 case tag_virt_length_min:
830 data = (find_tag(tag_length, idx_id, idx)/1000) / 60;
831 break;
833 case tag_virt_playtime_sec:
834 data = (find_tag(tag_playtime, idx_id, idx)/1000) % 60;
835 break;
837 case tag_virt_playtime_min:
838 data = (find_tag(tag_playtime, idx_id, idx)/1000) / 60;
839 break;
841 case tag_virt_autoscore:
842 if (find_tag(tag_length, idx_id, idx) == 0
843 || find_tag(tag_playcount, idx_id, idx) == 0)
845 data = 0;
847 else
849 /* A straight calculus gives:
850 autoscore = 100 * playtime / length / playcout (1)
851 Now, consider the euclidian division of playtime by length:
852 playtime = alpha * length + beta
853 With:
854 0 <= beta < length
855 Now, (1) becomes:
856 autoscore = 100 * (alpha / playcout + beta / length / playcount)
857 Both terms should be small enough to avoid any overflow
859 data = 100 * (find_tag(tag_playtime, idx_id, idx)
860 / find_tag(tag_length, idx_id, idx))
861 + (100 * (find_tag(tag_playtime, idx_id, idx)
862 % find_tag(tag_length, idx_id, idx)))
863 / find_tag(tag_length, idx_id, idx);
864 data /= find_tag(tag_playcount, idx_id, idx);
866 break;
868 /* How many commits before the file has been added to the DB. */
869 case tag_virt_entryage:
870 data = current_tcmh.commitid
871 - find_tag(tag_commitid, idx_id, idx) - 1;
872 break;
874 default:
875 data = find_tag(tag, idx_id, idx);
878 return data;
881 long tagcache_get_numeric(const struct tagcache_search *tcs, int tag)
883 struct index_entry idx;
885 if (!tc_stat.ready)
886 return false;
888 if (!TAGCACHE_IS_NUMERIC(tag))
889 return -1;
891 if (!get_index(tcs->masterfd, tcs->idx_id, &idx, true))
892 return -2;
894 return check_virtual_tags(tag, tcs->idx_id, &idx);
897 inline static bool str_ends_with(const char *str1, const char *str2)
899 int str_len = strlen(str1);
900 int clause_len = strlen(str2);
902 if (clause_len > str_len)
903 return false;
905 return !strcasecmp(&str1[str_len - clause_len], str2);
908 inline static bool str_oneof(const char *str, const char *list)
910 const char *sep;
911 int l, len = strlen(str);
913 while (*list)
915 sep = strchr(list, '|');
916 l = sep ? (long)sep - (long)list : (int)strlen(list);
917 if ((l==len) && !strncasecmp(str, list, len))
918 return true;
919 list += sep ? l + 1 : l;
922 return false;
925 static bool check_against_clause(long numeric, const char *str,
926 const struct tagcache_search_clause *clause)
928 if (clause->numeric)
930 switch (clause->type)
932 case clause_is:
933 return numeric == clause->numeric_data;
934 case clause_is_not:
935 return numeric != clause->numeric_data;
936 case clause_gt:
937 return numeric > clause->numeric_data;
938 case clause_gteq:
939 return numeric >= clause->numeric_data;
940 case clause_lt:
941 return numeric < clause->numeric_data;
942 case clause_lteq:
943 return numeric <= clause->numeric_data;
944 default:
945 logf("Incorrect numeric tag: %d", clause->type);
948 else
950 switch (clause->type)
952 case clause_is:
953 return !strcasecmp(clause->str, str);
954 case clause_is_not:
955 return strcasecmp(clause->str, str);
956 case clause_gt:
957 return 0>strcasecmp(clause->str, str);
958 case clause_gteq:
959 return 0>=strcasecmp(clause->str, str);
960 case clause_lt:
961 return 0<strcasecmp(clause->str, str);
962 case clause_lteq:
963 return 0<=strcasecmp(clause->str, str);
964 case clause_contains:
965 return (strcasestr(str, clause->str) != NULL);
966 case clause_not_contains:
967 return (strcasestr(str, clause->str) == NULL);
968 case clause_begins_with:
969 return (strcasestr(str, clause->str) == str);
970 case clause_not_begins_with:
971 return (strcasestr(str, clause->str) != str);
972 case clause_ends_with:
973 return str_ends_with(str, clause->str);
974 case clause_not_ends_with:
975 return !str_ends_with(str, clause->str);
976 case clause_oneof:
977 return str_oneof(str, clause->str);
979 default:
980 logf("Incorrect tag: %d", clause->type);
984 return false;
987 static bool check_clauses(struct tagcache_search *tcs,
988 struct index_entry *idx,
989 struct tagcache_search_clause **clauses, int count)
991 int i;
993 /* Go through all conditional clauses. */
994 for (i = 0; i < count; i++)
996 int seek;
997 char buf[256];
998 char *str = buf;
999 struct tagcache_search_clause *clause = clauses[i];
1001 if (clause->type == clause_logical_or)
1002 break; /* all conditions before logical-or satisfied --
1003 stop processing clauses */
1005 seek = check_virtual_tags(clause->tag, tcs->idx_id, idx);
1007 #ifdef HAVE_TC_RAMCACHE
1008 if (tcs->ramsearch)
1010 struct tagfile_entry *tfe;
1012 if (!TAGCACHE_IS_NUMERIC(clause->tag))
1014 if (clause->tag == tag_filename)
1016 retrieve(tcs, idx, tag_filename, buf, sizeof buf);
1018 else
1020 tfe = (struct tagfile_entry *)&hdr->tags[clause->tag][seek];
1021 str = tfe->tag_data;
1025 else
1026 #endif
1028 struct tagfile_entry tfe;
1030 if (!TAGCACHE_IS_NUMERIC(clause->tag))
1032 int fd = tcs->idxfd[clause->tag];
1033 lseek(fd, seek, SEEK_SET);
1034 ecread_tagfile_entry(fd, &tfe);
1035 if (tfe.tag_length >= (int)sizeof(buf))
1037 logf("Too long tag read!");
1038 return false;
1041 read(fd, str, tfe.tag_length);
1042 str[tfe.tag_length] = '\0';
1044 /* Check if entry has been deleted. */
1045 if (str[0] == '\0')
1046 return false;
1050 if (!check_against_clause(seek, str, clause))
1052 /* Clause failed -- try finding a logical-or clause */
1053 while (++i < count)
1055 if (clauses[i]->type == clause_logical_or)
1056 break;
1059 if (i < count) /* Found logical-or? */
1060 continue; /* Check clauses after logical-or */
1062 return false;
1066 return true;
1069 bool tagcache_check_clauses(struct tagcache_search *tcs,
1070 struct tagcache_search_clause **clause, int count)
1072 struct index_entry idx;
1074 if (count == 0)
1075 return true;
1077 if (!get_index(tcs->masterfd, tcs->idx_id, &idx, true))
1078 return false;
1080 return check_clauses(tcs, &idx, clause, count);
1083 static bool add_uniqbuf(struct tagcache_search *tcs, unsigned long id)
1085 int i;
1087 /* If uniq buffer is not defined we must return true for search to work. */
1088 if (tcs->unique_list == NULL || (!TAGCACHE_IS_UNIQUE(tcs->type)
1089 && !TAGCACHE_IS_NUMERIC(tcs->type)))
1091 return true;
1094 for (i = 0; i < tcs->unique_list_count; i++)
1096 /* Return false if entry is found. */
1097 if (tcs->unique_list[i] == id)
1098 return false;
1101 if (tcs->unique_list_count < tcs->unique_list_capacity)
1103 tcs->unique_list[i] = id;
1104 tcs->unique_list_count++;
1107 return true;
1110 static bool build_lookup_list(struct tagcache_search *tcs)
1112 struct index_entry entry;
1113 int i, j;
1115 tcs->seek_list_count = 0;
1117 #ifdef HAVE_TC_RAMCACHE
1118 if (tcs->ramsearch
1119 # ifdef HAVE_DIRCACHE
1120 && (tcs->type != tag_filename || is_dircache_intact())
1121 # endif
1124 for (i = tcs->seek_pos; i < current_tcmh.tch.entry_count; i++)
1126 struct tagcache_seeklist_entry *seeklist;
1127 struct index_entry *idx = &hdr->indices[i];
1128 if (tcs->seek_list_count == SEEK_LIST_SIZE)
1129 break ;
1131 /* Skip deleted files. */
1132 if (idx->flag & FLAG_DELETED)
1133 continue;
1135 /* Go through all filters.. */
1136 for (j = 0; j < tcs->filter_count; j++)
1138 if (idx->tag_seek[tcs->filter_tag[j]] != tcs->filter_seek[j])
1140 break ;
1144 if (j < tcs->filter_count)
1145 continue ;
1147 /* Check for conditions. */
1148 if (!check_clauses(tcs, idx, tcs->clause, tcs->clause_count))
1149 continue;
1151 /* Add to the seek list if not already in uniq buffer. */
1152 if (!add_uniqbuf(tcs, idx->tag_seek[tcs->type]))
1153 continue;
1155 /* Lets add it. */
1156 seeklist = &tcs->seeklist[tcs->seek_list_count];
1157 seeklist->seek = idx->tag_seek[tcs->type];
1158 seeklist->flag = idx->flag;
1159 seeklist->idx_id = i;
1160 tcs->seek_list_count++;
1163 tcs->seek_pos = i;
1165 return tcs->seek_list_count > 0;
1167 #endif
1169 if (tcs->masterfd < 0)
1171 struct master_header tcmh;
1172 tcs->masterfd = open_master_fd(&tcmh, false);
1175 lseek(tcs->masterfd, tcs->seek_pos * sizeof(struct index_entry) +
1176 sizeof(struct master_header), SEEK_SET);
1178 while (ecread_index_entry(tcs->masterfd, &entry)
1179 == sizeof(struct index_entry))
1181 struct tagcache_seeklist_entry *seeklist;
1183 if (tcs->seek_list_count == SEEK_LIST_SIZE)
1184 break ;
1186 i = tcs->seek_pos;
1187 tcs->seek_pos++;
1189 /* Check if entry has been deleted. */
1190 if (entry.flag & FLAG_DELETED)
1191 continue;
1193 /* Go through all filters.. */
1194 for (j = 0; j < tcs->filter_count; j++)
1196 if (entry.tag_seek[tcs->filter_tag[j]] != tcs->filter_seek[j])
1197 break ;
1200 if (j < tcs->filter_count)
1201 continue ;
1203 /* Check for conditions. */
1204 if (!check_clauses(tcs, &entry, tcs->clause, tcs->clause_count))
1205 continue;
1207 /* Add to the seek list if not already in uniq buffer. */
1208 if (!add_uniqbuf(tcs, entry.tag_seek[tcs->type]))
1209 continue;
1211 /* Lets add it. */
1212 seeklist = &tcs->seeklist[tcs->seek_list_count];
1213 seeklist->seek = entry.tag_seek[tcs->type];
1214 seeklist->flag = entry.flag;
1215 seeklist->idx_id = i;
1216 tcs->seek_list_count++;
1218 yield();
1221 return tcs->seek_list_count > 0;
1225 static void remove_files(void)
1227 int i;
1228 char buf[MAX_PATH];
1230 tc_stat.ready = false;
1231 tc_stat.ramcache = false;
1232 tc_stat.econ = false;
1233 remove(TAGCACHE_FILE_MASTER);
1234 for (i = 0; i < TAG_COUNT; i++)
1236 if (TAGCACHE_IS_NUMERIC(i))
1237 continue;
1239 snprintf(buf, sizeof buf, TAGCACHE_FILE_INDEX, i);
1240 remove(buf);
1245 static bool check_all_headers(void)
1247 struct master_header myhdr;
1248 struct tagcache_header tch;
1249 int tag;
1250 int fd;
1252 if ( (fd = open_master_fd(&myhdr, false)) < 0)
1253 return false;
1255 close(fd);
1256 if (myhdr.dirty)
1258 logf("tagcache is dirty!");
1259 return false;
1262 memcpy(&current_tcmh, &myhdr, sizeof(struct master_header));
1264 for (tag = 0; tag < TAG_COUNT; tag++)
1266 if (TAGCACHE_IS_NUMERIC(tag))
1267 continue;
1269 if ( (fd = open_tag_fd(&tch, tag, false)) < 0)
1270 return false;
1272 close(fd);
1275 return true;
1278 bool tagcache_is_busy(void)
1280 return read_lock || write_lock;
1283 bool tagcache_search(struct tagcache_search *tcs, int tag)
1285 struct tagcache_header tag_hdr;
1286 struct master_header master_hdr;
1287 int i;
1289 while (read_lock)
1290 sleep(1);
1292 memset(tcs, 0, sizeof(struct tagcache_search));
1293 if (tc_stat.commit_step > 0 || !tc_stat.ready)
1294 return false;
1296 tcs->position = sizeof(struct tagcache_header);
1297 tcs->type = tag;
1298 tcs->seek_pos = 0;
1299 tcs->list_position = 0;
1300 tcs->seek_list_count = 0;
1301 tcs->filter_count = 0;
1302 tcs->masterfd = -1;
1304 for (i = 0; i < TAG_COUNT; i++)
1305 tcs->idxfd[i] = -1;
1307 #ifndef HAVE_TC_RAMCACHE
1308 tcs->ramsearch = false;
1309 #else
1310 tcs->ramsearch = tc_stat.ramcache;
1311 if (tcs->ramsearch)
1313 tcs->entry_count = hdr->entry_count[tcs->type];
1315 else
1316 #endif
1318 /* Always open as R/W so we can pass tcs to functions that modify data also
1319 * without failing. */
1320 tcs->masterfd = open_master_fd(&master_hdr, true);
1321 if (tcs->masterfd < 0)
1322 return false;
1324 if (!TAGCACHE_IS_NUMERIC(tcs->type))
1326 tcs->idxfd[tcs->type] = open_tag_fd(&tag_hdr, tcs->type, false);
1327 if (tcs->idxfd[tcs->type] < 0)
1328 return false;
1330 tcs->entry_count = tag_hdr.entry_count;
1332 else
1334 tcs->entry_count = master_hdr.tch.entry_count;
1338 tcs->valid = true;
1339 tcs->initialized = true;
1340 write_lock++;
1342 return true;
1345 void tagcache_search_set_uniqbuf(struct tagcache_search *tcs,
1346 void *buffer, long length)
1348 tcs->unique_list = (unsigned long *)buffer;
1349 tcs->unique_list_capacity = length / sizeof(*tcs->unique_list);
1350 tcs->unique_list_count = 0;
1353 bool tagcache_search_add_filter(struct tagcache_search *tcs,
1354 int tag, int seek)
1356 if (tcs->filter_count == TAGCACHE_MAX_FILTERS)
1357 return false;
1359 if (TAGCACHE_IS_NUMERIC_OR_NONUNIQUE(tag))
1360 return false;
1362 tcs->filter_tag[tcs->filter_count] = tag;
1363 tcs->filter_seek[tcs->filter_count] = seek;
1364 tcs->filter_count++;
1366 return true;
1369 bool tagcache_search_add_clause(struct tagcache_search *tcs,
1370 struct tagcache_search_clause *clause)
1372 int i;
1374 if (tcs->clause_count >= TAGCACHE_MAX_CLAUSES)
1376 logf("Too many clauses");
1377 return false;
1380 if (clause->type != clause_logical_or)
1382 /* Check if there is already a similar filter in present (filters are
1383 * much faster than clauses).
1385 for (i = 0; i < tcs->filter_count; i++)
1387 if (tcs->filter_tag[i] == clause->tag)
1388 return true;
1391 if (!TAGCACHE_IS_NUMERIC(clause->tag) && tcs->idxfd[clause->tag] < 0)
1393 char buf[MAX_PATH];
1395 snprintf(buf, sizeof buf, TAGCACHE_FILE_INDEX, clause->tag);
1396 tcs->idxfd[clause->tag] = open(buf, O_RDONLY);
1400 tcs->clause[tcs->clause_count] = clause;
1401 tcs->clause_count++;
1403 return true;
1406 static bool get_next(struct tagcache_search *tcs)
1408 static char buf[TAG_MAXLEN+32];
1409 struct tagfile_entry entry;
1410 #if defined(HAVE_TC_RAMCACHE) && defined(HAVE_DIRCACHE)
1411 long flag = 0;
1412 #endif
1414 if (!tcs->valid || !tc_stat.ready)
1415 return false;
1417 if (tcs->idxfd[tcs->type] < 0 && !TAGCACHE_IS_NUMERIC(tcs->type)
1418 #ifdef HAVE_TC_RAMCACHE
1419 && !tcs->ramsearch
1420 #endif
1422 return false;
1424 /* Relative fetch. */
1425 if (tcs->filter_count > 0 || tcs->clause_count > 0
1426 || TAGCACHE_IS_NUMERIC(tcs->type)
1427 #if defined(HAVE_TC_RAMCACHE) && defined(HAVE_DIRCACHE)
1428 /* We need to retrieve flag status for dircache. */
1429 || (tcs->ramsearch && tcs->type == tag_filename)
1430 #endif
1433 struct tagcache_seeklist_entry *seeklist;
1435 /* Check for end of list. */
1436 if (tcs->list_position == tcs->seek_list_count)
1438 tcs->list_position = 0;
1440 /* Try to fetch more. */
1441 if (!build_lookup_list(tcs))
1443 tcs->valid = false;
1444 return false;
1448 seeklist = &tcs->seeklist[tcs->list_position];
1449 #if defined(HAVE_TC_RAMCACHE) && defined(HAVE_DIRCACHE)
1450 flag = seeklist->flag;
1451 #endif
1452 tcs->position = seeklist->seek;
1453 tcs->idx_id = seeklist->idx_id;
1454 tcs->list_position++;
1456 else
1458 if (tcs->entry_count == 0)
1460 tcs->valid = false;
1461 return false;
1464 tcs->entry_count--;
1467 tcs->result_seek = tcs->position;
1469 if (TAGCACHE_IS_NUMERIC(tcs->type))
1471 snprintf(buf, sizeof(buf), "%ld", tcs->position);
1472 tcs->result = buf;
1473 tcs->result_len = strlen(buf) + 1;
1474 return true;
1477 /* Direct fetch. */
1478 #ifdef HAVE_TC_RAMCACHE
1479 if (tcs->ramsearch)
1482 #if defined(HAVE_TC_RAMCACHE) && defined(HAVE_DIRCACHE)
1483 if (tcs->type == tag_filename && (flag & FLAG_DIRCACHE)
1484 && is_dircache_intact())
1486 size_t len = dircache_copy_path(tcs->position, buf, sizeof buf);
1487 tcs->result_len = len + 1;
1488 tcs->result = buf;
1489 tcs->ramresult = false;
1491 return true;
1493 else
1494 #endif
1495 if (tcs->type != tag_filename)
1497 struct tagfile_entry *ep;
1499 ep = (struct tagfile_entry *)&hdr->tags[tcs->type][tcs->position];
1500 tcs->result = ep->tag_data;
1501 tcs->result_len = strlen(tcs->result) + 1;
1502 tcs->idx_id = ep->idx_id;
1503 tcs->ramresult = true;
1505 /* Increase position for the next run. This may get overwritten. */
1506 tcs->position += sizeof(struct tagfile_entry) + ep->tag_length;
1508 return true;
1511 #endif
1513 if (!open_files(tcs, tcs->type))
1515 tcs->valid = false;
1516 return false;
1519 /* Seek stream to the correct position and continue to direct fetch. */
1520 lseek(tcs->idxfd[tcs->type], tcs->position, SEEK_SET);
1522 if (ecread_tagfile_entry(tcs->idxfd[tcs->type], &entry) != sizeof(struct tagfile_entry))
1524 logf("read error #5");
1525 tcs->valid = false;
1526 return false;
1529 if (entry.tag_length > (long)sizeof(buf))
1531 tcs->valid = false;
1532 logf("too long tag #2");
1533 logf("P:%lX/%lX", tcs->position, entry.tag_length);
1534 return false;
1537 if (read(tcs->idxfd[tcs->type], buf, entry.tag_length) != entry.tag_length)
1539 tcs->valid = false;
1540 logf("read error #4");
1541 return false;
1545 Update the position for the next read (this may be overridden
1546 if filters or clauses are being used).
1548 tcs->position += sizeof(struct tagfile_entry) + entry.tag_length;
1549 tcs->result = buf;
1550 tcs->result_len = strlen(tcs->result) + 1;
1551 tcs->idx_id = entry.idx_id;
1552 tcs->ramresult = false;
1554 return true;
1557 bool tagcache_get_next(struct tagcache_search *tcs)
1559 while (get_next(tcs))
1561 if (tcs->result_len > 1)
1562 return true;
1565 return false;
1568 bool tagcache_retrieve(struct tagcache_search *tcs, int idxid,
1569 int tag, char *buf, long size)
1571 struct index_entry idx;
1573 *buf = '\0';
1574 if (!get_index(tcs->masterfd, idxid, &idx, true))
1575 return false;
1577 return retrieve(tcs, &idx, tag, buf, size);
1580 static bool update_master_header(void)
1582 struct master_header myhdr;
1583 int fd;
1585 if (!tc_stat.ready)
1586 return false;
1588 if ( (fd = open_master_fd(&myhdr, true)) < 0)
1589 return false;
1591 myhdr.serial = current_tcmh.serial;
1592 myhdr.commitid = current_tcmh.commitid;
1593 myhdr.dirty = current_tcmh.dirty;
1595 /* Write it back */
1596 lseek(fd, 0, SEEK_SET);
1597 ecwrite(fd, &myhdr, 1, master_header_ec, tc_stat.econ);
1598 close(fd);
1600 return true;
1603 void tagcache_search_finish(struct tagcache_search *tcs)
1605 int i;
1607 if (!tcs->initialized)
1608 return;
1610 if (tcs->masterfd >= 0)
1612 close(tcs->masterfd);
1613 tcs->masterfd = -1;
1616 for (i = 0; i < TAG_COUNT; i++)
1618 if (tcs->idxfd[i] >= 0)
1620 close(tcs->idxfd[i]);
1621 tcs->idxfd[i] = -1;
1625 tcs->ramsearch = false;
1626 tcs->valid = false;
1627 tcs->initialized = 0;
1628 if (write_lock > 0)
1629 write_lock--;
1632 #if defined(HAVE_TC_RAMCACHE) && defined(HAVE_DIRCACHE)
1633 static struct tagfile_entry *get_tag(const struct index_entry *entry, int tag)
1635 return (struct tagfile_entry *)&hdr->tags[tag][entry->tag_seek[tag]];
1638 static long get_tag_numeric(const struct index_entry *entry, int tag, int idx_id)
1640 return check_virtual_tags(tag, idx_id, entry);
1643 static char* get_tag_string(const struct index_entry *entry, int tag)
1645 char* s = get_tag(entry, tag)->tag_data;
1646 return strcmp(s, UNTAGGED) ? s : NULL;
1649 bool tagcache_fill_tags(struct mp3entry *id3, const char *filename)
1651 struct index_entry *entry;
1652 int idx_id;
1654 if (!tc_stat.ready || !tc_stat.ramcache)
1655 return false;
1657 /* Find the corresponding entry in tagcache. */
1658 idx_id = find_entry_ram(filename, -1);
1659 if (idx_id < 0)
1660 return false;
1662 entry = &hdr->indices[idx_id];
1664 memset(id3, 0, sizeof(struct mp3entry));
1666 id3->title = get_tag_string(entry, tag_title);
1667 id3->artist = get_tag_string(entry, tag_artist);
1668 id3->album = get_tag_string(entry, tag_album);
1669 id3->genre_string = get_tag_string(entry, tag_genre);
1670 id3->composer = get_tag_string(entry, tag_composer);
1671 id3->comment = get_tag_string(entry, tag_comment);
1672 id3->albumartist = get_tag_string(entry, tag_albumartist);
1673 id3->grouping = get_tag_string(entry, tag_grouping);
1675 id3->length = get_tag_numeric(entry, tag_length, idx_id);
1676 id3->playcount = get_tag_numeric(entry, tag_playcount, idx_id);
1677 id3->rating = get_tag_numeric(entry, tag_rating, idx_id);
1678 id3->lastplayed = get_tag_numeric(entry, tag_lastplayed, idx_id);
1679 id3->score = get_tag_numeric(entry, tag_virt_autoscore, idx_id) / 10;
1680 id3->year = get_tag_numeric(entry, tag_year, idx_id);
1682 id3->discnum = get_tag_numeric(entry, tag_discnumber, idx_id);
1683 id3->tracknum = get_tag_numeric(entry, tag_tracknumber, idx_id);
1684 id3->bitrate = get_tag_numeric(entry, tag_bitrate, idx_id);
1685 if (id3->bitrate == 0)
1686 id3->bitrate = 1;
1688 #if CONFIG_CODEC == SWCODEC
1689 if (global_settings.autoresume_enable)
1691 id3->offset = get_tag_numeric(entry, tag_lastoffset, idx_id);
1692 logf("tagcache_fill_tags: Set offset for %s to %lX\n",
1693 id3->title, id3->offset);
1695 #endif
1697 return true;
1699 #endif
1701 static inline void write_item(const char *item)
1703 int len = strlen(item) + 1;
1705 data_size += len;
1706 write(cachefd, item, len);
1709 static int check_if_empty(char **tag)
1711 int length;
1713 if (*tag == NULL || **tag == '\0')
1715 *tag = UNTAGGED;
1716 return sizeof(UNTAGGED); /* Tag length */
1719 length = strlen(*tag);
1720 if (length > TAG_MAXLEN)
1722 logf("over length tag: %s", *tag);
1723 length = TAG_MAXLEN;
1724 (*tag)[length] = '\0';
1727 return length + 1;
1730 #define ADD_TAG(entry,tag,data) \
1731 /* Adding tag */ \
1732 entry.tag_offset[tag] = offset; \
1733 entry.tag_length[tag] = check_if_empty(data); \
1734 offset += entry.tag_length[tag]
1735 /* GCC 3.4.6 for Coldfire can choose to inline this function. Not a good
1736 * idea, as it uses lots of stack and is called from a recursive function
1737 * (check_dir).
1739 static void __attribute__ ((noinline)) add_tagcache(char *path,
1740 unsigned long mtime
1741 #if defined(HAVE_TC_RAMCACHE) && defined(HAVE_DIRCACHE)
1742 ,int dc
1743 #endif
1746 struct mp3entry id3;
1747 struct temp_file_entry entry;
1748 bool ret;
1749 int fd;
1750 int idx_id = -1;
1751 char tracknumfix[3];
1752 int offset = 0;
1753 int path_length = strlen(path);
1754 bool has_albumartist;
1755 bool has_grouping;
1757 #ifdef SIMULATOR
1758 /* Crude logging for the sim - to aid in debugging */
1759 int logfd = open(ROCKBOX_DIR "/database.log",
1760 O_WRONLY | O_APPEND | O_CREAT, 0666);
1761 if (logfd >= 0) {
1762 write(logfd, path, strlen(path));
1763 write(logfd, "\n", 1);
1764 close(logfd);
1766 #endif
1768 if (cachefd < 0)
1769 return ;
1771 /* Check for overlength file path. */
1772 if (path_length > TAG_MAXLEN)
1774 /* Path can't be shortened. */
1775 logf("Too long path: %s", path);
1776 return ;
1779 /* Check if the file is supported. */
1780 if (probe_file_format(path) == AFMT_UNKNOWN)
1781 return ;
1783 /* Check if the file is already cached. */
1784 #if defined(HAVE_TC_RAMCACHE) && defined(HAVE_DIRCACHE)
1785 if (tc_stat.ramcache && is_dircache_intact())
1787 idx_id = find_entry_ram(path, dc);
1789 #endif
1791 /* Be sure the entry doesn't exist. */
1792 if (filenametag_fd >= 0 && idx_id < 0)
1793 idx_id = find_entry_disk(path, false);
1795 /* Check if file has been modified. */
1796 if (idx_id >= 0)
1798 struct index_entry idx;
1800 /* TODO: Mark that the index exists (for fast reverse scan) */
1801 //found_idx[idx_id/8] |= idx_id%8;
1803 if (!get_index(-1, idx_id, &idx, true))
1805 logf("failed to retrieve index entry");
1806 return ;
1809 if ((unsigned long)idx.tag_seek[tag_mtime] == mtime)
1811 /* No changes to file. */
1812 return ;
1815 /* Metadata might have been changed. Delete the entry. */
1816 logf("Re-adding: %s", path);
1817 if (!delete_entry(idx_id))
1819 logf("delete_entry failed: %d", idx_id);
1820 return ;
1824 fd = open(path, O_RDONLY);
1825 if (fd < 0)
1827 logf("open fail: %s", path);
1828 return ;
1831 memset(&id3, 0, sizeof(struct mp3entry));
1832 memset(&entry, 0, sizeof(struct temp_file_entry));
1833 memset(&tracknumfix, 0, sizeof(tracknumfix));
1834 ret = get_metadata(&id3, fd, path);
1835 close(fd);
1837 if (!ret)
1838 return ;
1840 logf("-> %s", path);
1842 if (id3.tracknum <= 0) /* Track number missing? */
1844 id3.tracknum = -1;
1847 /* Numeric tags */
1848 entry.tag_offset[tag_year] = id3.year;
1849 entry.tag_offset[tag_discnumber] = id3.discnum;
1850 entry.tag_offset[tag_tracknumber] = id3.tracknum;
1851 entry.tag_offset[tag_length] = id3.length;
1852 entry.tag_offset[tag_bitrate] = id3.bitrate;
1853 entry.tag_offset[tag_mtime] = mtime;
1855 /* String tags. */
1856 has_albumartist = id3.albumartist != NULL
1857 && strlen(id3.albumartist) > 0;
1858 has_grouping = id3.grouping != NULL
1859 && strlen(id3.grouping) > 0;
1861 ADD_TAG(entry, tag_filename, &path);
1862 ADD_TAG(entry, tag_title, &id3.title);
1863 ADD_TAG(entry, tag_artist, &id3.artist);
1864 ADD_TAG(entry, tag_album, &id3.album);
1865 ADD_TAG(entry, tag_genre, &id3.genre_string);
1866 ADD_TAG(entry, tag_composer, &id3.composer);
1867 ADD_TAG(entry, tag_comment, &id3.comment);
1868 if (has_albumartist)
1870 ADD_TAG(entry, tag_albumartist, &id3.albumartist);
1872 else
1874 ADD_TAG(entry, tag_albumartist, &id3.artist);
1876 if (has_grouping)
1878 ADD_TAG(entry, tag_grouping, &id3.grouping);
1880 else
1882 ADD_TAG(entry, tag_grouping, &id3.title);
1884 entry.data_length = offset;
1886 /* Write the header */
1887 write(cachefd, &entry, sizeof(struct temp_file_entry));
1889 /* And tags also... Correct order is critical */
1890 write_item(path);
1891 write_item(id3.title);
1892 write_item(id3.artist);
1893 write_item(id3.album);
1894 write_item(id3.genre_string);
1895 write_item(id3.composer);
1896 write_item(id3.comment);
1897 if (has_albumartist)
1899 write_item(id3.albumartist);
1901 else
1903 write_item(id3.artist);
1905 if (has_grouping)
1907 write_item(id3.grouping);
1909 else
1911 write_item(id3.title);
1913 total_entry_count++;
1916 static bool tempbuf_insert(char *str, int id, int idx_id, bool unique)
1918 struct tempbuf_searchidx *index = (struct tempbuf_searchidx *)tempbuf;
1919 int len = strlen(str)+1;
1920 int i;
1921 unsigned crc32;
1922 unsigned *crcbuf = (unsigned *)&tempbuf[tempbuf_size-4];
1923 char buf[TAG_MAXLEN+32];
1925 for (i = 0; str[i] != '\0' && i < (int)sizeof(buf)-1; i++)
1926 buf[i] = tolower(str[i]);
1927 buf[i] = '\0';
1929 crc32 = crc_32(buf, i, 0xffffffff);
1931 if (unique)
1933 /* Check if the crc does not exist -> entry does not exist for sure. */
1934 for (i = 0; i < tempbufidx; i++)
1936 if (crcbuf[-i] != crc32)
1937 continue;
1939 if (!strcasecmp(str, index[i].str))
1941 if (id < 0 || id >= lookup_buffer_depth)
1943 logf("lookup buf overf.: %d", id);
1944 return false;
1947 lookup[id] = &index[i];
1948 return true;
1953 /* Insert to CRC buffer. */
1954 crcbuf[-tempbufidx] = crc32;
1955 tempbuf_left -= 4;
1957 /* Insert it to the buffer. */
1958 tempbuf_left -= len;
1959 if (tempbuf_left - 4 < 0 || tempbufidx >= commit_entry_count-1)
1960 return false;
1962 if (id >= lookup_buffer_depth)
1964 logf("lookup buf overf. #2: %d", id);
1965 return false;
1968 if (id >= 0)
1970 lookup[id] = &index[tempbufidx];
1971 index[tempbufidx].idlist.id = id;
1973 else
1974 index[tempbufidx].idlist.id = -1;
1976 index[tempbufidx].idlist.next = NULL;
1977 index[tempbufidx].idx_id = idx_id;
1978 index[tempbufidx].seek = -1;
1979 index[tempbufidx].str = &tempbuf[tempbuf_pos];
1980 memcpy(index[tempbufidx].str, str, len);
1981 tempbuf_pos += len;
1982 tempbufidx++;
1984 return true;
1987 static int compare(const void *p1, const void *p2)
1989 do_timed_yield();
1991 struct tempbuf_searchidx *e1 = (struct tempbuf_searchidx *)p1;
1992 struct tempbuf_searchidx *e2 = (struct tempbuf_searchidx *)p2;
1994 if (strcmp(e1->str, UNTAGGED) == 0)
1996 if (strcmp(e2->str, UNTAGGED) == 0)
1997 return 0;
1998 return -1;
2000 else if (strcmp(e2->str, UNTAGGED) == 0)
2001 return 1;
2003 return strncasecmp(e1->str, e2->str, TAG_MAXLEN);
2006 static int tempbuf_sort(int fd)
2008 struct tempbuf_searchidx *index = (struct tempbuf_searchidx *)tempbuf;
2009 struct tagfile_entry fe;
2010 int i;
2011 int length;
2013 /* Generate reverse lookup entries. */
2014 for (i = 0; i < lookup_buffer_depth; i++)
2016 struct tempbuf_id_list *idlist;
2018 if (!lookup[i])
2019 continue;
2021 if (lookup[i]->idlist.id == i)
2022 continue;
2024 idlist = &lookup[i]->idlist;
2025 while (idlist->next != NULL)
2026 idlist = idlist->next;
2028 tempbuf_left -= sizeof(struct tempbuf_id_list);
2029 if (tempbuf_left - 4 < 0)
2030 return -1;
2032 idlist->next = (struct tempbuf_id_list *)&tempbuf[tempbuf_pos];
2033 if (tempbuf_pos & 0x03)
2035 tempbuf_pos = (tempbuf_pos & ~0x03) + 0x04;
2036 tempbuf_left -= 3;
2037 idlist->next = (struct tempbuf_id_list *)&tempbuf[tempbuf_pos];
2039 tempbuf_pos += sizeof(struct tempbuf_id_list);
2041 idlist = idlist->next;
2042 idlist->id = i;
2043 idlist->next = NULL;
2045 do_timed_yield();
2048 qsort(index, tempbufidx, sizeof(struct tempbuf_searchidx), compare);
2049 memset(lookup, 0, lookup_buffer_depth * sizeof(struct tempbuf_searchidx **));
2051 for (i = 0; i < tempbufidx; i++)
2053 struct tempbuf_id_list *idlist = &index[i].idlist;
2055 /* Fix the lookup list. */
2056 while (idlist != NULL)
2058 if (idlist->id >= 0)
2059 lookup[idlist->id] = &index[i];
2060 idlist = idlist->next;
2063 index[i].seek = lseek(fd, 0, SEEK_CUR);
2064 length = strlen(index[i].str) + 1;
2065 fe.tag_length = length;
2066 fe.idx_id = index[i].idx_id;
2068 /* Check the chunk alignment. */
2069 if ((fe.tag_length + sizeof(struct tagfile_entry))
2070 % TAGFILE_ENTRY_CHUNK_LENGTH)
2072 fe.tag_length += TAGFILE_ENTRY_CHUNK_LENGTH -
2073 ((fe.tag_length + sizeof(struct tagfile_entry))
2074 % TAGFILE_ENTRY_CHUNK_LENGTH);
2077 #ifdef TAGCACHE_STRICT_ALIGN
2078 /* Make sure the entry is long aligned. */
2079 if (index[i].seek & 0x03)
2081 logf("tempbuf_sort: alignment error!");
2082 return -3;
2084 #endif
2086 if (ecwrite(fd, &fe, 1, tagfile_entry_ec, tc_stat.econ) !=
2087 sizeof(struct tagfile_entry))
2089 logf("tempbuf_sort: write error #1");
2090 return -1;
2093 if (write(fd, index[i].str, length) != length)
2095 logf("tempbuf_sort: write error #2");
2096 return -2;
2099 /* Write some padding. */
2100 if (fe.tag_length - length > 0)
2101 write(fd, "XXXXXXXX", fe.tag_length - length);
2104 return i;
2107 inline static struct tempbuf_searchidx* tempbuf_locate(int id)
2109 if (id < 0 || id >= lookup_buffer_depth)
2110 return NULL;
2112 return lookup[id];
2116 inline static int tempbuf_find_location(int id)
2118 struct tempbuf_searchidx *entry;
2120 entry = tempbuf_locate(id);
2121 if (entry == NULL)
2122 return -1;
2124 return entry->seek;
2127 static bool build_numeric_indices(struct tagcache_header *h, int tmpfd)
2129 struct master_header tcmh;
2130 struct index_entry idx;
2131 int masterfd;
2132 int masterfd_pos;
2133 struct temp_file_entry *entrybuf = (struct temp_file_entry *)tempbuf;
2134 int max_entries;
2135 int entries_processed = 0;
2136 int i, j;
2137 char buf[TAG_MAXLEN];
2139 max_entries = tempbuf_size / sizeof(struct temp_file_entry) - 1;
2141 logf("Building numeric indices...");
2142 lseek(tmpfd, sizeof(struct tagcache_header), SEEK_SET);
2144 if ( (masterfd = open_master_fd(&tcmh, true)) < 0)
2145 return false;
2147 masterfd_pos = lseek(masterfd, tcmh.tch.entry_count * sizeof(struct index_entry),
2148 SEEK_CUR);
2149 if (masterfd_pos == filesize(masterfd))
2151 logf("we can't append!");
2152 close(masterfd);
2153 return false;
2156 while (entries_processed < h->entry_count)
2158 int count = MIN(h->entry_count - entries_processed, max_entries);
2160 /* Read in as many entries as possible. */
2161 for (i = 0; i < count; i++)
2163 struct temp_file_entry *tfe = &entrybuf[i];
2164 int datastart;
2166 /* Read in numeric data. */
2167 if (read(tmpfd, tfe, sizeof(struct temp_file_entry)) !=
2168 sizeof(struct temp_file_entry))
2170 logf("read fail #1");
2171 close(masterfd);
2172 return false;
2175 datastart = lseek(tmpfd, 0, SEEK_CUR);
2178 * Read string data from the following tags:
2179 * - tag_filename
2180 * - tag_artist
2181 * - tag_album
2182 * - tag_title
2184 * A crc32 hash is calculated from the read data
2185 * and stored back to the data offset field kept in memory.
2187 #define tmpdb_read_string_tag(tag) \
2188 lseek(tmpfd, tfe->tag_offset[tag], SEEK_CUR); \
2189 if ((unsigned long)tfe->tag_length[tag] > sizeof buf) \
2191 logf("read fail: buffer overflow"); \
2192 close(masterfd); \
2193 return false; \
2196 if (read(tmpfd, buf, tfe->tag_length[tag]) != \
2197 tfe->tag_length[tag]) \
2199 logf("read fail #2"); \
2200 close(masterfd); \
2201 return false; \
2204 tfe->tag_offset[tag] = crc_32(buf, strlen(buf), 0xffffffff); \
2205 lseek(tmpfd, datastart, SEEK_SET)
2207 tmpdb_read_string_tag(tag_filename);
2208 tmpdb_read_string_tag(tag_artist);
2209 tmpdb_read_string_tag(tag_album);
2210 tmpdb_read_string_tag(tag_title);
2212 /* Seek to the end of the string data. */
2213 lseek(tmpfd, tfe->data_length, SEEK_CUR);
2216 /* Backup the master index position. */
2217 masterfd_pos = lseek(masterfd, 0, SEEK_CUR);
2218 lseek(masterfd, sizeof(struct master_header), SEEK_SET);
2220 /* Check if we can resurrect some deleted runtime statistics data. */
2221 for (i = 0; i < tcmh.tch.entry_count; i++)
2223 /* Read the index entry. */
2224 if (ecread_index_entry(masterfd, &idx)
2225 != sizeof(struct index_entry))
2227 logf("read fail #3");
2228 close(masterfd);
2229 return false;
2233 * Skip unless the entry is marked as being deleted
2234 * or the data has already been resurrected.
2236 if (!(idx.flag & FLAG_DELETED) || idx.flag & FLAG_RESURRECTED)
2237 continue;
2239 /* Now try to match the entry. */
2241 * To succesfully match a song, the following conditions
2242 * must apply:
2244 * For numeric fields: tag_length
2245 * - Full identical match is required
2247 * If tag_filename matches, no further checking necessary.
2249 * For string hashes: tag_artist, tag_album, tag_title
2250 * - All three of these must match
2252 for (j = 0; j < count; j++)
2254 struct temp_file_entry *tfe = &entrybuf[j];
2256 /* Try to match numeric fields first. */
2257 if (tfe->tag_offset[tag_length] != idx.tag_seek[tag_length])
2258 continue;
2260 /* Now it's time to do the hash matching. */
2261 if (tfe->tag_offset[tag_filename] != idx.tag_seek[tag_filename])
2263 int match_count = 0;
2265 /* No filename match, check if we can match two other tags. */
2266 #define tmpdb_match(tag) \
2267 if (tfe->tag_offset[tag] == idx.tag_seek[tag]) \
2268 match_count++
2270 tmpdb_match(tag_artist);
2271 tmpdb_match(tag_album);
2272 tmpdb_match(tag_title);
2274 if (match_count < 3)
2276 /* Still no match found, give up. */
2277 continue;
2281 /* A match found, now copy & resurrect the statistical data. */
2282 #define tmpdb_copy_tag(tag) \
2283 tfe->tag_offset[tag] = idx.tag_seek[tag]
2285 tmpdb_copy_tag(tag_playcount);
2286 tmpdb_copy_tag(tag_rating);
2287 tmpdb_copy_tag(tag_playtime);
2288 tmpdb_copy_tag(tag_lastplayed);
2289 tmpdb_copy_tag(tag_commitid);
2290 tmpdb_copy_tag(tag_lastoffset);
2292 /* Avoid processing this entry again. */
2293 idx.flag |= FLAG_RESURRECTED;
2295 lseek(masterfd, -sizeof(struct index_entry), SEEK_CUR);
2296 if (ecwrite_index_entry(masterfd, &idx) != sizeof(struct index_entry))
2298 logf("masterfd writeback fail #1");
2299 close(masterfd);
2300 return false;
2303 logf("Entry resurrected");
2308 /* Restore the master index position. */
2309 lseek(masterfd, masterfd_pos, SEEK_SET);
2311 /* Commit the data to the index. */
2312 for (i = 0; i < count; i++)
2314 int loc = lseek(masterfd, 0, SEEK_CUR);
2316 if (ecread_index_entry(masterfd, &idx) != sizeof(struct index_entry))
2318 logf("read fail #3");
2319 close(masterfd);
2320 return false;
2323 for (j = 0; j < TAG_COUNT; j++)
2325 if (!TAGCACHE_IS_NUMERIC(j))
2326 continue;
2328 idx.tag_seek[j] = entrybuf[i].tag_offset[j];
2330 idx.flag = entrybuf[i].flag;
2332 if (idx.tag_seek[tag_commitid])
2334 /* Data has been resurrected. */
2335 idx.flag |= FLAG_DIRTYNUM;
2337 else if (tc_stat.ready && current_tcmh.commitid > 0)
2339 idx.tag_seek[tag_commitid] = current_tcmh.commitid;
2340 idx.flag |= FLAG_DIRTYNUM;
2343 /* Write back the updated index. */
2344 lseek(masterfd, loc, SEEK_SET);
2345 if (ecwrite_index_entry(masterfd, &idx) != sizeof(struct index_entry))
2347 logf("write fail");
2348 close(masterfd);
2349 return false;
2353 entries_processed += count;
2354 logf("%d/%ld entries processed", entries_processed, h->entry_count);
2357 close(masterfd);
2359 return true;
2363 * Return values:
2364 * > 0 success
2365 * == 0 temporary failure
2366 * < 0 fatal error
2368 static int build_index(int index_type, struct tagcache_header *h, int tmpfd)
2370 int i;
2371 struct tagcache_header tch;
2372 struct master_header tcmh;
2373 struct index_entry idxbuf[IDX_BUF_DEPTH];
2374 int idxbuf_pos;
2375 char buf[TAG_MAXLEN+32];
2376 int fd = -1, masterfd;
2377 bool error = false;
2378 int init;
2379 int masterfd_pos;
2381 logf("Building index: %d", index_type);
2383 /* Check the number of entries we need to allocate ram for. */
2384 commit_entry_count = h->entry_count + 1;
2386 masterfd = open_master_fd(&tcmh, false);
2387 if (masterfd >= 0)
2389 commit_entry_count += tcmh.tch.entry_count;
2390 close(masterfd);
2392 else
2393 remove_files(); /* Just to be sure we are clean. */
2395 /* Open the index file, which contains the tag names. */
2396 fd = open_tag_fd(&tch, index_type, true);
2397 if (fd >= 0)
2399 logf("tch.datasize=%ld", tch.datasize);
2400 lookup_buffer_depth = 1 +
2401 /* First part */ commit_entry_count +
2402 /* Second part */ (tch.datasize / TAGFILE_ENTRY_CHUNK_LENGTH);
2404 else
2406 lookup_buffer_depth = 1 +
2407 /* First part */ commit_entry_count +
2408 /* Second part */ 0;
2411 logf("lookup_buffer_depth=%ld", lookup_buffer_depth);
2412 logf("commit_entry_count=%ld", commit_entry_count);
2414 /* Allocate buffer for all index entries from both old and new
2415 * tag files. */
2416 tempbufidx = 0;
2417 tempbuf_pos = commit_entry_count * sizeof(struct tempbuf_searchidx);
2419 /* Allocate lookup buffer. The first portion of commit_entry_count
2420 * contains the new tags in the temporary file and the second
2421 * part for locating entries already in the db.
2423 * New tags Old tags
2424 * +---------+---------------------------+
2425 * | index | position/ENTRY_CHUNK_SIZE | lookup buffer
2426 * +---------+---------------------------+
2428 * Old tags are inserted to a temporary buffer with position:
2429 * tempbuf_insert(position/ENTRY_CHUNK_SIZE, ...);
2430 * And new tags with index:
2431 * tempbuf_insert(idx, ...);
2433 * The buffer is sorted and written into tag file:
2434 * tempbuf_sort(...);
2435 * leaving master index locations messed up.
2437 * That is fixed using the lookup buffer for old tags:
2438 * new_seek = tempbuf_find_location(old_seek, ...);
2439 * and for new tags:
2440 * new_seek = tempbuf_find_location(idx);
2442 lookup = (struct tempbuf_searchidx **)&tempbuf[tempbuf_pos];
2443 tempbuf_pos += lookup_buffer_depth * sizeof(void **);
2444 memset(lookup, 0, lookup_buffer_depth * sizeof(void **));
2446 /* And calculate the remaining data space used mainly for storing
2447 * tag data (strings). */
2448 tempbuf_left = tempbuf_size - tempbuf_pos - 8;
2449 if (tempbuf_left - TAGFILE_ENTRY_AVG_LENGTH * commit_entry_count < 0)
2451 logf("Buffer way too small!");
2452 return 0;
2455 if (fd >= 0)
2458 * If tag file contains unique tags (sorted index), we will load
2459 * it entirely into memory so we can resort it later for use with
2460 * chunked browsing.
2462 if (TAGCACHE_IS_SORTED(index_type))
2464 logf("loading tags...");
2465 for (i = 0; i < tch.entry_count; i++)
2467 struct tagfile_entry entry;
2468 int loc = lseek(fd, 0, SEEK_CUR);
2469 bool ret;
2471 if (ecread_tagfile_entry(fd, &entry) != sizeof(struct tagfile_entry))
2473 logf("read error #7");
2474 close(fd);
2475 return -2;
2478 if (entry.tag_length >= (int)sizeof(buf))
2480 logf("too long tag #3");
2481 close(fd);
2482 return -2;
2485 if (read(fd, buf, entry.tag_length) != entry.tag_length)
2487 logf("read error #8");
2488 close(fd);
2489 return -2;
2492 /* Skip deleted entries. */
2493 if (buf[0] == '\0')
2494 continue;
2497 * Save the tag and tag id in the memory buffer. Tag id
2498 * is saved so we can later reindex the master lookup
2499 * table when the index gets resorted.
2501 ret = tempbuf_insert(buf, loc/TAGFILE_ENTRY_CHUNK_LENGTH
2502 + commit_entry_count, entry.idx_id,
2503 TAGCACHE_IS_UNIQUE(index_type));
2504 if (!ret)
2506 close(fd);
2507 return -3;
2509 do_timed_yield();
2511 logf("done");
2513 else
2514 tempbufidx = tch.entry_count;
2516 else
2519 * Creating new index file to store the tags. No need to preload
2520 * anything whether the index type is sorted or not.
2522 snprintf(buf, sizeof buf, TAGCACHE_FILE_INDEX, index_type);
2523 fd = open(buf, O_WRONLY | O_CREAT | O_TRUNC, 0666);
2524 if (fd < 0)
2526 logf("%s open fail", buf);
2527 return -2;
2530 tch.magic = TAGCACHE_MAGIC;
2531 tch.entry_count = 0;
2532 tch.datasize = 0;
2534 if (ecwrite(fd, &tch, 1, tagcache_header_ec, tc_stat.econ)
2535 != sizeof(struct tagcache_header))
2537 logf("header write failed");
2538 close(fd);
2539 return -2;
2543 /* Loading the tag lookup file as "master file". */
2544 logf("Loading index file");
2545 masterfd = open(TAGCACHE_FILE_MASTER, O_RDWR);
2547 if (masterfd < 0)
2549 logf("Creating new DB");
2550 masterfd = open(TAGCACHE_FILE_MASTER, O_WRONLY | O_CREAT | O_TRUNC, 0666);
2552 if (masterfd < 0)
2554 logf("Failure to create index file (%s)", TAGCACHE_FILE_MASTER);
2555 close(fd);
2556 return -2;
2559 /* Write the header (write real values later). */
2560 memset(&tcmh, 0, sizeof(struct master_header));
2561 tcmh.tch = *h;
2562 tcmh.tch.entry_count = 0;
2563 tcmh.tch.datasize = 0;
2564 tcmh.dirty = true;
2565 ecwrite(masterfd, &tcmh, 1, master_header_ec, tc_stat.econ);
2566 init = true;
2567 masterfd_pos = lseek(masterfd, 0, SEEK_CUR);
2569 else
2572 * Master file already exists so we need to process the current
2573 * file first.
2575 init = false;
2577 if (ecread(masterfd, &tcmh, 1, master_header_ec, tc_stat.econ) !=
2578 sizeof(struct master_header) || tcmh.tch.magic != TAGCACHE_MAGIC)
2580 logf("header error");
2581 close(fd);
2582 close(masterfd);
2583 return -2;
2587 * If we reach end of the master file, we need to expand it to
2588 * hold new tags. If the current index is not sorted, we can
2589 * simply append new data to end of the file.
2590 * However, if the index is sorted, we need to update all tag
2591 * pointers in the master file for the current index.
2593 masterfd_pos = lseek(masterfd, tcmh.tch.entry_count * sizeof(struct index_entry),
2594 SEEK_CUR);
2595 if (masterfd_pos == filesize(masterfd))
2597 logf("appending...");
2598 init = true;
2603 * Load new unique tags in memory to be sorted later and added
2604 * to the master lookup file.
2606 if (TAGCACHE_IS_SORTED(index_type))
2608 lseek(tmpfd, sizeof(struct tagcache_header), SEEK_SET);
2609 /* h is the header of the temporary file containing new tags. */
2610 logf("inserting new tags...");
2611 for (i = 0; i < h->entry_count; i++)
2613 struct temp_file_entry entry;
2615 if (read(tmpfd, &entry, sizeof(struct temp_file_entry)) !=
2616 sizeof(struct temp_file_entry))
2618 logf("read fail #3");
2619 error = true;
2620 goto error_exit;
2623 /* Read data. */
2624 if (entry.tag_length[index_type] >= (long)sizeof(buf))
2626 logf("too long entry!");
2627 error = true;
2628 goto error_exit;
2631 lseek(tmpfd, entry.tag_offset[index_type], SEEK_CUR);
2632 if (read(tmpfd, buf, entry.tag_length[index_type]) !=
2633 entry.tag_length[index_type])
2635 logf("read fail #4");
2636 error = true;
2637 goto error_exit;
2640 if (TAGCACHE_IS_UNIQUE(index_type))
2641 error = !tempbuf_insert(buf, i, -1, true);
2642 else
2643 error = !tempbuf_insert(buf, i, tcmh.tch.entry_count + i, false);
2645 if (error)
2647 logf("insert error");
2648 goto error_exit;
2651 /* Skip to next. */
2652 lseek(tmpfd, entry.data_length - entry.tag_offset[index_type] -
2653 entry.tag_length[index_type], SEEK_CUR);
2654 do_timed_yield();
2656 logf("done");
2658 /* Sort the buffer data and write it to the index file. */
2659 lseek(fd, sizeof(struct tagcache_header), SEEK_SET);
2661 * We need to truncate the index file now. There can be junk left
2662 * at the end of file (however, we _should_ always follow the
2663 * entry_count and don't crash with that).
2665 ftruncate(fd, lseek(fd, 0, SEEK_CUR));
2667 i = tempbuf_sort(fd);
2668 if (i < 0)
2669 goto error_exit;
2670 logf("sorted %d tags", i);
2673 * Now update all indexes in the master lookup file.
2675 logf("updating indices...");
2676 lseek(masterfd, sizeof(struct master_header), SEEK_SET);
2677 for (i = 0; i < tcmh.tch.entry_count; i += idxbuf_pos)
2679 int j;
2680 int loc = lseek(masterfd, 0, SEEK_CUR);
2682 idxbuf_pos = MIN(tcmh.tch.entry_count - i, IDX_BUF_DEPTH);
2684 if (ecread(masterfd, idxbuf, idxbuf_pos, index_entry_ec, tc_stat.econ)
2685 != (int)sizeof(struct index_entry)*idxbuf_pos)
2687 logf("read fail #5");
2688 error = true;
2689 goto error_exit ;
2691 lseek(masterfd, loc, SEEK_SET);
2693 for (j = 0; j < idxbuf_pos; j++)
2695 if (idxbuf[j].flag & FLAG_DELETED)
2697 /* We can just ignore deleted entries. */
2698 // idxbuf[j].tag_seek[index_type] = 0;
2699 continue;
2702 idxbuf[j].tag_seek[index_type] = tempbuf_find_location(
2703 idxbuf[j].tag_seek[index_type]/TAGFILE_ENTRY_CHUNK_LENGTH
2704 + commit_entry_count);
2706 if (idxbuf[j].tag_seek[index_type] < 0)
2708 logf("update error: %ld/%d/%ld",
2709 idxbuf[j].flag, i+j, tcmh.tch.entry_count);
2710 error = true;
2711 goto error_exit;
2714 do_timed_yield();
2717 /* Write back the updated index. */
2718 if (ecwrite(masterfd, idxbuf, idxbuf_pos,
2719 index_entry_ec, tc_stat.econ) !=
2720 (int)sizeof(struct index_entry)*idxbuf_pos)
2722 logf("write fail");
2723 error = true;
2724 goto error_exit;
2727 logf("done");
2731 * Walk through the temporary file containing the new tags.
2733 // build_normal_index(h, tmpfd, masterfd, idx);
2734 logf("updating new indices...");
2735 lseek(masterfd, masterfd_pos, SEEK_SET);
2736 lseek(tmpfd, sizeof(struct tagcache_header), SEEK_SET);
2737 lseek(fd, 0, SEEK_END);
2738 for (i = 0; i < h->entry_count; i += idxbuf_pos)
2740 int j;
2742 idxbuf_pos = MIN(h->entry_count - i, IDX_BUF_DEPTH);
2743 if (init)
2745 memset(idxbuf, 0, sizeof(struct index_entry)*IDX_BUF_DEPTH);
2747 else
2749 int loc = lseek(masterfd, 0, SEEK_CUR);
2751 if (ecread(masterfd, idxbuf, idxbuf_pos, index_entry_ec, tc_stat.econ)
2752 != (int)sizeof(struct index_entry)*idxbuf_pos)
2754 logf("read fail #6");
2755 error = true;
2756 break ;
2758 lseek(masterfd, loc, SEEK_SET);
2761 /* Read entry headers. */
2762 for (j = 0; j < idxbuf_pos; j++)
2764 if (!TAGCACHE_IS_SORTED(index_type))
2766 struct temp_file_entry entry;
2767 struct tagfile_entry fe;
2769 if (read(tmpfd, &entry, sizeof(struct temp_file_entry)) !=
2770 sizeof(struct temp_file_entry))
2772 logf("read fail #7");
2773 error = true;
2774 break ;
2777 /* Read data. */
2778 if (entry.tag_length[index_type] >= (int)sizeof(buf))
2780 logf("too long entry!");
2781 logf("length=%d", entry.tag_length[index_type]);
2782 logf("pos=0x%02lx", lseek(tmpfd, 0, SEEK_CUR));
2783 error = true;
2784 break ;
2787 lseek(tmpfd, entry.tag_offset[index_type], SEEK_CUR);
2788 if (read(tmpfd, buf, entry.tag_length[index_type]) !=
2789 entry.tag_length[index_type])
2791 logf("read fail #8");
2792 logf("offset=0x%02lx", entry.tag_offset[index_type]);
2793 logf("length=0x%02x", entry.tag_length[index_type]);
2794 error = true;
2795 break ;
2798 /* Write to index file. */
2799 idxbuf[j].tag_seek[index_type] = lseek(fd, 0, SEEK_CUR);
2800 fe.tag_length = entry.tag_length[index_type];
2801 fe.idx_id = tcmh.tch.entry_count + i + j;
2802 ecwrite(fd, &fe, 1, tagfile_entry_ec, tc_stat.econ);
2803 write(fd, buf, fe.tag_length);
2804 tempbufidx++;
2806 /* Skip to next. */
2807 lseek(tmpfd, entry.data_length - entry.tag_offset[index_type] -
2808 entry.tag_length[index_type], SEEK_CUR);
2810 else
2812 /* Locate the correct entry from the sorted array. */
2813 idxbuf[j].tag_seek[index_type] = tempbuf_find_location(i + j);
2814 if (idxbuf[j].tag_seek[index_type] < 0)
2816 logf("entry not found (%d)", j);
2817 error = true;
2818 break ;
2823 /* Write index. */
2824 if (ecwrite(masterfd, idxbuf, idxbuf_pos,
2825 index_entry_ec, tc_stat.econ) !=
2826 (int)sizeof(struct index_entry)*idxbuf_pos)
2828 logf("tagcache: write fail #4");
2829 error = true;
2830 break ;
2833 do_timed_yield();
2835 logf("done");
2837 /* Finally write the header. */
2838 tch.magic = TAGCACHE_MAGIC;
2839 tch.entry_count = tempbufidx;
2840 tch.datasize = lseek(fd, 0, SEEK_END) - sizeof(struct tagcache_header);
2841 lseek(fd, 0, SEEK_SET);
2842 ecwrite(fd, &tch, 1, tagcache_header_ec, tc_stat.econ);
2844 if (index_type != tag_filename)
2845 h->datasize += tch.datasize;
2846 logf("s:%d/%ld/%ld", index_type, tch.datasize, h->datasize);
2847 error_exit:
2849 close(fd);
2850 close(masterfd);
2852 if (error)
2853 return -2;
2855 return 1;
2858 static bool commit(void)
2860 struct tagcache_header tch;
2861 struct master_header tcmh;
2862 int i, len, rc;
2863 int tmpfd;
2864 int masterfd;
2865 #ifdef HAVE_DIRCACHE
2866 bool dircache_buffer_stolen = false;
2867 #endif
2868 bool local_allocation = false;
2870 logf("committing tagcache");
2872 while (write_lock)
2873 sleep(1);
2875 tmpfd = open(TAGCACHE_FILE_TEMP, O_RDONLY);
2876 if (tmpfd < 0)
2878 logf("nothing to commit");
2879 return true;
2883 /* Load the header. */
2884 len = sizeof(struct tagcache_header);
2885 rc = read(tmpfd, &tch, len);
2887 if (tch.magic != TAGCACHE_MAGIC || rc != len)
2889 logf("incorrect tmpheader");
2890 close(tmpfd);
2891 remove(TAGCACHE_FILE_TEMP);
2892 return false;
2895 if (tch.entry_count == 0)
2897 logf("nothing to commit");
2898 close(tmpfd);
2899 remove(TAGCACHE_FILE_TEMP);
2900 return true;
2903 /* Fully initialize existing headers (if any) before going further. */
2904 tc_stat.ready = check_all_headers();
2906 #ifdef HAVE_EEPROM_SETTINGS
2907 remove(TAGCACHE_STATEFILE);
2908 #endif
2910 /* At first be sure to unload the ramcache! */
2911 #ifdef HAVE_TC_RAMCACHE
2912 tc_stat.ramcache = false;
2913 #endif
2915 read_lock++;
2917 /* Try to steal every buffer we can :) */
2918 if (tempbuf_size == 0)
2919 local_allocation = true;
2921 #ifdef HAVE_DIRCACHE
2922 if (tempbuf_size == 0)
2924 /* Try to steal the dircache buffer. */
2925 tempbuf = dircache_steal_buffer(&tempbuf_size);
2926 tempbuf_size &= ~0x03;
2928 if (tempbuf_size > 0)
2930 dircache_buffer_stolen = true;
2933 #endif
2935 #ifdef HAVE_TC_RAMCACHE
2936 if (tempbuf_size == 0 && tc_stat.ramcache_allocated > 0)
2938 tempbuf = (char *)(hdr + 1);
2939 tempbuf_size = tc_stat.ramcache_allocated - sizeof(struct ramcache_header) - 128;
2940 tempbuf_size &= ~0x03;
2942 #endif
2944 /* And finally fail if there are no buffers available. */
2945 if (tempbuf_size == 0)
2947 logf("delaying commit until next boot");
2948 tc_stat.commit_delayed = true;
2949 close(tmpfd);
2950 read_lock--;
2951 return false;
2954 logf("commit %ld entries...", tch.entry_count);
2956 /* Mark DB dirty so it will stay disabled if commit fails. */
2957 current_tcmh.dirty = true;
2958 update_master_header();
2960 /* Now create the index files. */
2961 tc_stat.commit_step = 0;
2962 tch.datasize = 0;
2963 tc_stat.commit_delayed = false;
2965 for (i = 0; i < TAG_COUNT; i++)
2967 int ret;
2969 if (TAGCACHE_IS_NUMERIC(i))
2970 continue;
2972 tc_stat.commit_step++;
2973 ret = build_index(i, &tch, tmpfd);
2974 if (ret <= 0)
2976 close(tmpfd);
2977 logf("tagcache failed init");
2978 if (ret == 0)
2979 tc_stat.commit_delayed = true;
2981 tc_stat.commit_step = 0;
2982 read_lock--;
2983 return false;
2987 if (!build_numeric_indices(&tch, tmpfd))
2989 logf("Failure to commit numeric indices");
2990 close(tmpfd);
2991 tc_stat.commit_step = 0;
2992 read_lock--;
2993 return false;
2996 close(tmpfd);
2998 tc_stat.commit_step = 0;
3000 /* Update the master index headers. */
3001 if ( (masterfd = open_master_fd(&tcmh, true)) < 0)
3003 read_lock--;
3004 return false;
3007 remove(TAGCACHE_FILE_TEMP);
3009 tcmh.tch.entry_count += tch.entry_count;
3010 tcmh.tch.datasize = sizeof(struct master_header)
3011 + sizeof(struct index_entry) * tcmh.tch.entry_count
3012 + tch.datasize;
3013 tcmh.dirty = false;
3014 tcmh.commitid++;
3016 lseek(masterfd, 0, SEEK_SET);
3017 ecwrite(masterfd, &tcmh, 1, master_header_ec, tc_stat.econ);
3018 close(masterfd);
3020 logf("tagcache committed");
3021 tc_stat.ready = check_all_headers();
3022 tc_stat.readyvalid = true;
3024 if (local_allocation)
3026 tempbuf = NULL;
3027 tempbuf_size = 0;
3030 #ifdef HAVE_DIRCACHE
3031 /* Rebuild the dircache, if we stole the buffer. */
3032 if (dircache_buffer_stolen)
3033 dircache_build(0);
3034 #endif
3036 #ifdef HAVE_TC_RAMCACHE
3037 /* Reload tagcache. */
3038 if (tc_stat.ramcache_allocated > 0)
3039 tagcache_start_scan();
3040 #endif
3042 read_lock--;
3044 return true;
3047 static void allocate_tempbuf(void)
3049 /* Yeah, malloc would be really nice now :) */
3050 #ifdef __PCTOOL__
3051 tempbuf_size = 32*1024*1024;
3052 tempbuf = malloc(tempbuf_size);
3053 #else
3054 tempbuf = (char *)(((long)audiobuf & ~0x03) + 0x04);
3055 tempbuf_size = (long)audiobufend - (long)audiobuf - 4;
3056 audiobuf += tempbuf_size;
3057 #endif
3060 static void free_tempbuf(void)
3062 if (tempbuf_size == 0)
3063 return ;
3065 #ifdef __PCTOOL__
3066 free(tempbuf);
3067 #else
3068 audiobuf -= tempbuf_size;
3069 #endif
3070 tempbuf = NULL;
3071 tempbuf_size = 0;
3074 #ifndef __PCTOOL__
3076 static bool modify_numeric_entry(int masterfd, int idx_id, int tag, long data)
3078 struct index_entry idx;
3080 if (!tc_stat.ready)
3081 return false;
3083 if (!TAGCACHE_IS_NUMERIC(tag))
3084 return false;
3086 if (!get_index(masterfd, idx_id, &idx, false))
3087 return false;
3089 idx.tag_seek[tag] = data;
3090 idx.flag |= FLAG_DIRTYNUM;
3092 return write_index(masterfd, idx_id, &idx);
3095 #if 0
3096 bool tagcache_modify_numeric_entry(struct tagcache_search *tcs,
3097 int tag, long data)
3099 struct master_header myhdr;
3101 if (tcs->masterfd < 0)
3103 if ( (tcs->masterfd = open_master_fd(&myhdr, true)) < 0)
3104 return false;
3107 return modify_numeric_entry(tcs->masterfd, tcs->idx_id, tag, data);
3109 #endif
3111 static bool command_queue_is_full(void)
3113 int next;
3115 next = command_queue_widx + 1;
3116 if (next >= TAGCACHE_COMMAND_QUEUE_LENGTH)
3117 next = 0;
3119 return (next == command_queue_ridx);
3122 static void command_queue_sync_callback(void *data)
3124 (void)data;
3125 struct master_header myhdr;
3126 int masterfd;
3128 mutex_lock(&command_queue_mutex);
3130 if ( (masterfd = open_master_fd(&myhdr, true)) < 0)
3131 return;
3133 while (command_queue_ridx != command_queue_widx)
3135 struct tagcache_command_entry *ce = &command_queue[command_queue_ridx];
3137 switch (ce->command)
3139 case CMD_UPDATE_MASTER_HEADER:
3141 close(masterfd);
3142 update_master_header();
3144 /* Re-open the masterfd. */
3145 if ( (masterfd = open_master_fd(&myhdr, true)) < 0)
3146 return;
3148 break;
3150 case CMD_UPDATE_NUMERIC:
3152 modify_numeric_entry(masterfd, ce->idx_id, ce->tag, ce->data);
3153 break;
3157 if (++command_queue_ridx >= TAGCACHE_COMMAND_QUEUE_LENGTH)
3158 command_queue_ridx = 0;
3161 close(masterfd);
3163 tc_stat.queue_length = 0;
3164 mutex_unlock(&command_queue_mutex);
3167 static void run_command_queue(bool force)
3169 if (COMMAND_QUEUE_IS_EMPTY)
3170 return;
3172 if (force || command_queue_is_full())
3173 command_queue_sync_callback(NULL);
3174 else
3175 register_storage_idle_func(command_queue_sync_callback);
3178 static void queue_command(int cmd, long idx_id, int tag, long data)
3180 while (1)
3182 int next;
3184 mutex_lock(&command_queue_mutex);
3185 next = command_queue_widx + 1;
3186 if (next >= TAGCACHE_COMMAND_QUEUE_LENGTH)
3187 next = 0;
3189 /* Make sure queue is not full. */
3190 if (next != command_queue_ridx)
3192 struct tagcache_command_entry *ce = &command_queue[command_queue_widx];
3194 ce->command = cmd;
3195 ce->idx_id = idx_id;
3196 ce->tag = tag;
3197 ce->data = data;
3199 command_queue_widx = next;
3201 tc_stat.queue_length++;
3203 mutex_unlock(&command_queue_mutex);
3204 break;
3207 /* Queue is full, try again later... */
3208 mutex_unlock(&command_queue_mutex);
3209 sleep(1);
3213 long tagcache_increase_serial(void)
3215 long old;
3217 if (!tc_stat.ready)
3218 return -2;
3220 while (read_lock)
3221 sleep(1);
3223 old = current_tcmh.serial++;
3224 queue_command(CMD_UPDATE_MASTER_HEADER, 0, 0, 0);
3226 return old;
3229 void tagcache_update_numeric(int idx_id, int tag, long data)
3231 queue_command(CMD_UPDATE_NUMERIC, idx_id, tag, data);
3233 #endif /* !__PCTOOL__ */
3235 long tagcache_get_serial(void)
3237 return current_tcmh.serial;
3240 long tagcache_get_commitid(void)
3242 return current_tcmh.commitid;
3245 static bool write_tag(int fd, const char *tagstr, const char *datastr)
3247 char buf[512];
3248 int i;
3250 snprintf(buf, sizeof buf, "%s=\"", tagstr);
3251 for (i = strlen(buf); i < (long)sizeof(buf)-4; i++)
3253 if (*datastr == '\0')
3254 break;
3256 if (*datastr == '"' || *datastr == '\\')
3257 buf[i++] = '\\';
3259 else if (*datastr == '\n')
3261 buf[i++] = '\\';
3262 buf[i] = 'n';
3263 continue;
3266 buf[i] = *(datastr++);
3269 strcpy(&buf[i], "\" ");
3271 write(fd, buf, i + 2);
3273 return true;
3276 #ifndef __PCTOOL__
3278 static bool read_tag(char *dest, long size,
3279 const char *src, const char *tagstr)
3281 int pos;
3282 char current_tag[32];
3284 while (*src != '\0')
3286 /* Skip all whitespace */
3287 while (*src == ' ')
3288 src++;
3290 if (*src == '\0')
3291 break;
3293 pos = 0;
3294 /* Read in tag name */
3295 while (*src != '=' && *src != ' ')
3297 current_tag[pos] = *src;
3298 src++;
3299 pos++;
3301 if (*src == '\0' || pos >= (long)sizeof(current_tag))
3302 return false;
3304 current_tag[pos] = '\0';
3306 /* Read in tag data */
3308 /* Find the start. */
3309 while (*src != '"' && *src != '\0')
3310 src++;
3312 if (*src == '\0' || *(++src) == '\0')
3313 return false;
3315 /* Read the data. */
3316 for (pos = 0; pos < size; pos++)
3318 if (*src == '\0')
3319 break;
3321 if (*src == '\\')
3323 src++;
3324 if (*src == 'n')
3325 dest[pos] = '\n';
3326 else
3327 dest[pos] = *src;
3329 src++;
3330 continue;
3333 if (*src == '\0')
3334 break;
3336 if (*src == '"')
3338 src++;
3339 break;
3342 dest[pos] = *(src++);
3345 dest[pos] = '\0';
3347 if (!strcasecmp(tagstr, current_tag))
3348 return true;
3351 return false;
3354 static int parse_changelog_line(int line_n, char *buf, void *parameters)
3356 struct index_entry idx;
3357 char tag_data[TAG_MAXLEN+32];
3358 int idx_id;
3359 long masterfd = (long)parameters;
3360 const int import_tags[] = { tag_playcount, tag_rating, tag_playtime,
3361 tag_lastplayed, tag_commitid, tag_lastoffset };
3362 int i;
3363 (void)line_n;
3365 if (*buf == '#')
3366 return 0;
3368 /* logf("%d/%s", line_n, buf); */
3369 if (!read_tag(tag_data, sizeof tag_data, buf, "filename"))
3371 logf("%d/filename missing", line_n);
3372 logf("-> %s", buf);
3373 return 0;
3376 idx_id = find_index(tag_data);
3377 if (idx_id < 0)
3379 logf("%d/entry not found", line_n);
3380 return 0;
3383 if (!get_index(masterfd, idx_id, &idx, false))
3385 logf("%d/failed to retrieve index entry", line_n);
3386 return 0;
3389 /* Stop if tag has already been modified. */
3390 if (idx.flag & FLAG_DIRTYNUM)
3391 return 0;
3393 logf("%d/import: %s", line_n, tag_data);
3395 idx.flag |= FLAG_DIRTYNUM;
3396 for (i = 0; i < (long)(sizeof(import_tags)/sizeof(import_tags[0])); i++)
3398 int data;
3400 if (!read_tag(tag_data, sizeof tag_data, buf,
3401 tagcache_tag_to_str(import_tags[i])))
3403 continue;
3406 data = atoi(tag_data);
3407 if (data < 0)
3408 continue;
3410 idx.tag_seek[import_tags[i]] = data;
3412 if (import_tags[i] == tag_lastplayed && data >= current_tcmh.serial)
3413 current_tcmh.serial = data + 1;
3414 else if (import_tags[i] == tag_commitid && data >= current_tcmh.commitid)
3415 current_tcmh.commitid = data + 1;
3418 return write_index(masterfd, idx_id, &idx) ? 0 : -5;
3421 bool tagcache_import_changelog(void)
3423 struct master_header myhdr;
3424 struct tagcache_header tch;
3425 int clfd;
3426 long masterfd;
3427 char buf[2048];
3429 if (!tc_stat.ready)
3430 return false;
3432 while (read_lock)
3433 sleep(1);
3435 clfd = open(TAGCACHE_FILE_CHANGELOG, O_RDONLY);
3436 if (clfd < 0)
3438 logf("failure to open changelog");
3439 return false;
3442 if ( (masterfd = open_master_fd(&myhdr, true)) < 0)
3444 close(clfd);
3445 return false;
3448 write_lock++;
3450 filenametag_fd = open_tag_fd(&tch, tag_filename, false);
3452 fast_readline(clfd, buf, sizeof buf, (long *)masterfd,
3453 parse_changelog_line);
3455 close(clfd);
3456 close(masterfd);
3458 if (filenametag_fd >= 0)
3460 close(filenametag_fd);
3461 filenametag_fd = -1;
3464 write_lock--;
3466 update_master_header();
3468 return true;
3471 #endif /* !__PCTOOL__ */
3473 bool tagcache_create_changelog(struct tagcache_search *tcs)
3475 struct master_header myhdr;
3476 struct index_entry idx;
3477 char buf[TAG_MAXLEN+32];
3478 char temp[32];
3479 int clfd;
3480 int i, j;
3482 if (!tc_stat.ready)
3483 return false;
3485 if (!tagcache_search(tcs, tag_filename))
3486 return false;
3488 /* Initialize the changelog */
3489 clfd = open(TAGCACHE_FILE_CHANGELOG, O_WRONLY | O_CREAT | O_TRUNC, 0666);
3490 if (clfd < 0)
3492 logf("failure to open changelog");
3493 return false;
3496 if (tcs->masterfd < 0)
3498 if ( (tcs->masterfd = open_master_fd(&myhdr, false)) < 0)
3499 return false;
3501 else
3503 lseek(tcs->masterfd, 0, SEEK_SET);
3504 ecread(tcs->masterfd, &myhdr, 1, master_header_ec, tc_stat.econ);
3507 write(clfd, "## Changelog version 1\n", 23);
3509 for (i = 0; i < myhdr.tch.entry_count; i++)
3511 if (ecread_index_entry(tcs->masterfd, &idx) != sizeof(struct index_entry))
3513 logf("read error #9");
3514 tagcache_search_finish(tcs);
3515 close(clfd);
3516 return false;
3519 /* Skip until the entry found has been modified. */
3520 if (! (idx.flag & FLAG_DIRTYNUM) )
3521 continue;
3523 /* Skip deleted entries too. */
3524 if (idx.flag & FLAG_DELETED)
3525 continue;
3527 /* Now retrieve all tags. */
3528 for (j = 0; j < TAG_COUNT; j++)
3530 if (TAGCACHE_IS_NUMERIC(j))
3532 snprintf(temp, sizeof temp, "%d", (int)idx.tag_seek[j]);
3533 write_tag(clfd, tagcache_tag_to_str(j), temp);
3534 continue;
3537 tcs->type = j;
3538 tagcache_retrieve(tcs, i, tcs->type, buf, sizeof buf);
3539 write_tag(clfd, tagcache_tag_to_str(j), buf);
3542 write(clfd, "\n", 1);
3543 do_timed_yield();
3546 close(clfd);
3548 tagcache_search_finish(tcs);
3550 return true;
3553 static bool delete_entry(long idx_id)
3555 int fd = -1;
3556 int masterfd = -1;
3557 int tag, i;
3558 struct index_entry idx, myidx;
3559 struct master_header myhdr;
3560 char buf[TAG_MAXLEN+32];
3561 int in_use[TAG_COUNT];
3563 logf("delete_entry(): %ld", idx_id);
3565 #ifdef HAVE_TC_RAMCACHE
3566 /* At first mark the entry removed from ram cache. */
3567 if (tc_stat.ramcache)
3568 hdr->indices[idx_id].flag |= FLAG_DELETED;
3569 #endif
3571 if ( (masterfd = open_master_fd(&myhdr, true) ) < 0)
3572 return false;
3574 lseek(masterfd, idx_id * sizeof(struct index_entry), SEEK_CUR);
3575 if (ecread_index_entry(masterfd, &myidx) != sizeof(struct index_entry))
3577 logf("delete_entry(): read error");
3578 goto cleanup;
3581 if (myidx.flag & FLAG_DELETED)
3583 logf("delete_entry(): already deleted!");
3584 goto cleanup;
3587 myidx.flag |= FLAG_DELETED;
3588 lseek(masterfd, -sizeof(struct index_entry), SEEK_CUR);
3589 if (ecwrite_index_entry(masterfd, &myidx) != sizeof(struct index_entry))
3591 logf("delete_entry(): write_error #1");
3592 goto cleanup;
3595 /* Now check which tags are no longer in use (if any) */
3596 for (tag = 0; tag < TAG_COUNT; tag++)
3597 in_use[tag] = 0;
3599 lseek(masterfd, sizeof(struct master_header), SEEK_SET);
3600 for (i = 0; i < myhdr.tch.entry_count; i++)
3602 struct index_entry *idxp;
3604 #ifdef HAVE_TC_RAMCACHE
3605 /* Use RAM DB if available for greater speed */
3606 if (tc_stat.ramcache)
3607 idxp = &hdr->indices[i];
3608 else
3609 #endif
3611 if (ecread_index_entry(masterfd, &idx) != sizeof(struct index_entry))
3613 logf("delete_entry(): read error #2");
3614 goto cleanup;
3616 idxp = &idx;
3619 if (idxp->flag & FLAG_DELETED)
3620 continue;
3622 for (tag = 0; tag < TAG_COUNT; tag++)
3624 if (TAGCACHE_IS_NUMERIC(tag))
3625 continue;
3627 if (idxp->tag_seek[tag] == myidx.tag_seek[tag])
3628 in_use[tag]++;
3632 /* Now delete all tags no longer in use. */
3633 for (tag = 0; tag < TAG_COUNT; tag++)
3635 struct tagcache_header tch;
3636 int oldseek = myidx.tag_seek[tag];
3638 if (TAGCACHE_IS_NUMERIC(tag))
3639 continue;
3641 /**
3642 * Replace tag seek with a hash value of the field string data.
3643 * That way runtime statistics of moved or altered files can be
3644 * resurrected.
3646 #ifdef HAVE_TC_RAMCACHE
3647 if (tc_stat.ramcache && tag != tag_filename)
3649 struct tagfile_entry *tfe;
3650 int32_t *seek = &hdr->indices[idx_id].tag_seek[tag];
3652 tfe = (struct tagfile_entry *)&hdr->tags[tag][*seek];
3653 *seek = crc_32(tfe->tag_data, strlen(tfe->tag_data), 0xffffffff);
3654 myidx.tag_seek[tag] = *seek;
3656 else
3657 #endif
3659 struct tagfile_entry tfe;
3661 /* Open the index file, which contains the tag names. */
3662 if ((fd = open_tag_fd(&tch, tag, true)) < 0)
3663 goto cleanup;
3665 /* Skip the header block */
3666 lseek(fd, myidx.tag_seek[tag], SEEK_SET);
3667 if (ecread_tagfile_entry(fd, &tfe) != sizeof(struct tagfile_entry))
3669 logf("delete_entry(): read error #3");
3670 goto cleanup;
3673 if (read(fd, buf, tfe.tag_length) != tfe.tag_length)
3675 logf("delete_entry(): read error #4");
3676 goto cleanup;
3679 myidx.tag_seek[tag] = crc_32(buf, strlen(buf), 0xffffffff);
3682 if (in_use[tag])
3684 logf("in use: %d/%d", tag, in_use[tag]);
3685 if (fd >= 0)
3687 close(fd);
3688 fd = -1;
3690 continue;
3693 #ifdef HAVE_TC_RAMCACHE
3694 /* Delete from ram. */
3695 if (tc_stat.ramcache && tag != tag_filename)
3697 struct tagfile_entry *tagentry = (struct tagfile_entry *)&hdr->tags[tag][oldseek];
3698 tagentry->tag_data[0] = '\0';
3700 #endif
3702 /* Open the index file, which contains the tag names. */
3703 if (fd < 0)
3705 if ((fd = open_tag_fd(&tch, tag, true)) < 0)
3706 goto cleanup;
3709 /* Skip the header block */
3710 lseek(fd, oldseek + sizeof(struct tagfile_entry), SEEK_SET);
3712 /* Debug, print 10 first characters of the tag
3713 read(fd, buf, 10);
3714 buf[10]='\0';
3715 logf("TAG:%s", buf);
3716 lseek(fd, -10, SEEK_CUR);
3719 /* Write first data byte in tag as \0 */
3720 write(fd, "", 1);
3722 /* Now tag data has been removed */
3723 close(fd);
3724 fd = -1;
3727 /* Write index entry back into master index. */
3728 lseek(masterfd, sizeof(struct master_header) +
3729 (idx_id * sizeof(struct index_entry)), SEEK_SET);
3730 if (ecwrite_index_entry(masterfd, &myidx) != sizeof(struct index_entry))
3732 logf("delete_entry(): write_error #2");
3733 goto cleanup;
3736 close(masterfd);
3738 return true;
3740 cleanup:
3741 if (fd >= 0)
3742 close(fd);
3743 if (masterfd >= 0)
3744 close(masterfd);
3746 return false;
3749 #ifndef __PCTOOL__
3751 * Returns true if there is an event waiting in the queue
3752 * that requires the current operation to be aborted.
3754 static bool check_event_queue(void)
3756 struct queue_event ev;
3758 if(!queue_peek(&tagcache_queue, &ev))
3759 return false;
3761 switch (ev.id)
3763 case Q_STOP_SCAN:
3764 case SYS_POWEROFF:
3765 case SYS_USB_CONNECTED:
3766 return true;
3769 return false;
3771 #endif
3773 #ifdef HAVE_TC_RAMCACHE
3774 static bool allocate_tagcache(void)
3776 struct master_header tcmh;
3777 int fd;
3779 /* Load the header. */
3780 if ( (fd = open_master_fd(&tcmh, false)) < 0)
3782 hdr = NULL;
3783 return false;
3786 close(fd);
3788 /**
3789 * Now calculate the required cache size plus
3790 * some extra space for alignment fixes.
3792 tc_stat.ramcache_allocated = tcmh.tch.datasize + 128 + TAGCACHE_RESERVE +
3793 sizeof(struct ramcache_header) + TAG_COUNT*sizeof(void *);
3794 hdr = buffer_alloc(tc_stat.ramcache_allocated + 128);
3795 memset(hdr, 0, sizeof(struct ramcache_header));
3796 memcpy(&current_tcmh, &tcmh, sizeof current_tcmh);
3797 logf("tagcache: %d bytes allocated.", tc_stat.ramcache_allocated);
3799 return true;
3802 # ifdef HAVE_EEPROM_SETTINGS
3803 static bool tagcache_dumpload(void)
3805 struct statefile_header shdr;
3806 int fd, rc;
3807 long offpos;
3808 int i;
3810 fd = open(TAGCACHE_STATEFILE, O_RDONLY);
3811 if (fd < 0)
3813 logf("no tagcache statedump");
3814 return false;
3817 /* Check the statefile memory placement */
3818 hdr = buffer_alloc(0);
3819 rc = read(fd, &shdr, sizeof(struct statefile_header));
3820 if (rc != sizeof(struct statefile_header)
3821 || shdr.magic != TAGCACHE_STATEFILE_MAGIC
3822 || shdr.mh.tch.magic != TAGCACHE_MAGIC)
3824 logf("incorrect statefile");
3825 hdr = NULL;
3826 close(fd);
3827 return false;
3830 offpos = (long)hdr - (long)shdr.hdr;
3832 /* Lets allocate real memory and load it */
3833 hdr = buffer_alloc(shdr.tc_stat.ramcache_allocated);
3834 rc = read(fd, hdr, shdr.tc_stat.ramcache_allocated);
3835 close(fd);
3837 if (rc != shdr.tc_stat.ramcache_allocated)
3839 logf("read failure!");
3840 hdr = NULL;
3841 return false;
3844 memcpy(&tc_stat, &shdr.tc_stat, sizeof(struct tagcache_stat));
3846 /* Now fix the pointers */
3847 for (i = 0; i < TAG_COUNT; i++)
3848 hdr->tags[i] += offpos;
3850 /* Load the tagcache master header (should match the actual DB file header). */
3851 memcpy(&current_tcmh, &shdr.mh, sizeof current_tcmh);
3853 return true;
3856 static bool tagcache_dumpsave(void)
3858 struct statefile_header shdr;
3859 int fd;
3861 if (!tc_stat.ramcache)
3862 return false;
3864 fd = open(TAGCACHE_STATEFILE, O_WRONLY | O_CREAT | O_TRUNC, 0666);
3865 if (fd < 0)
3867 logf("failed to create a statedump");
3868 return false;
3871 /* Create the header */
3872 shdr.magic = TAGCACHE_STATEFILE_MAGIC;
3873 shdr.hdr = hdr;
3874 memcpy(&shdr.mh, &current_tcmh, sizeof current_tcmh);
3875 memcpy(&shdr.tc_stat, &tc_stat, sizeof tc_stat);
3876 write(fd, &shdr, sizeof shdr);
3878 /* And dump the data too */
3879 write(fd, hdr, tc_stat.ramcache_allocated);
3880 close(fd);
3882 return true;
3884 # endif
3886 static bool load_tagcache(void)
3888 struct tagcache_header *tch;
3889 struct master_header tcmh;
3890 long bytesleft = tc_stat.ramcache_allocated;
3891 struct index_entry *idx;
3892 int rc, fd;
3893 char *p;
3894 int i, tag;
3896 # ifdef HAVE_DIRCACHE
3897 while (dircache_is_initializing())
3898 sleep(1);
3900 dircache_set_appflag(DIRCACHE_APPFLAG_TAGCACHE);
3901 # endif
3903 logf("loading tagcache to ram...");
3905 fd = open(TAGCACHE_FILE_MASTER, O_RDONLY);
3906 if (fd < 0)
3908 logf("tagcache open failed");
3909 return false;
3912 if (ecread(fd, &tcmh, 1, master_header_ec, tc_stat.econ)
3913 != sizeof(struct master_header)
3914 || tcmh.tch.magic != TAGCACHE_MAGIC)
3916 logf("incorrect header");
3917 return false;
3920 /* Master header copy should already match, this can be redundant to do. */
3921 memcpy(&current_tcmh, &tcmh, sizeof current_tcmh);
3923 idx = hdr->indices;
3925 /* Load the master index table. */
3926 for (i = 0; i < tcmh.tch.entry_count; i++)
3928 /* DEBUG: After tagcache commit and dircache rebuild, hdr-sturcture
3929 * may become corrupt. */
3930 rc = ecread_index_entry(fd, idx);
3931 if (rc != sizeof(struct index_entry))
3933 logf("read error #10");
3934 close(fd);
3935 return false;
3938 bytesleft -= sizeof(struct index_entry);
3939 if (bytesleft < 0 || ((long)idx - (long)hdr->indices) >= tc_stat.ramcache_allocated)
3941 logf("too big tagcache.");
3942 close(fd);
3943 return false;
3946 idx++;
3949 close(fd);
3951 /* Load the tags. */
3952 p = (char *)idx;
3953 for (tag = 0; tag < TAG_COUNT; tag++)
3955 struct tagfile_entry *fe;
3956 char buf[TAG_MAXLEN+32];
3958 if (TAGCACHE_IS_NUMERIC(tag))
3959 continue ;
3961 //p = ((void *)p+1);
3962 p = (char *)((long)p & ~0x03) + 0x04;
3963 hdr->tags[tag] = p;
3965 /* Check the header. */
3966 tch = (struct tagcache_header *)p;
3967 p += sizeof(struct tagcache_header);
3969 if ( (fd = open_tag_fd(tch, tag, false)) < 0)
3970 return false;
3972 for (hdr->entry_count[tag] = 0;
3973 hdr->entry_count[tag] < tch->entry_count;
3974 hdr->entry_count[tag]++)
3976 long pos;
3978 if (do_timed_yield())
3980 /* Abort if we got a critical event in queue */
3981 if (check_event_queue())
3982 return false;
3985 fe = (struct tagfile_entry *)p;
3986 pos = lseek(fd, 0, SEEK_CUR);
3987 rc = ecread_tagfile_entry(fd, fe);
3988 if (rc != sizeof(struct tagfile_entry))
3990 /* End of lookup table. */
3991 logf("read error #11");
3992 close(fd);
3993 return false;
3996 /* We have a special handling for the filename tags. */
3997 if (tag == tag_filename)
3999 # ifdef HAVE_DIRCACHE
4000 int dc;
4001 # endif
4003 idx = &hdr->indices[fe->idx_id];
4005 if (fe->tag_length >= (long)sizeof(buf)-1)
4007 read(fd, buf, 10);
4008 buf[10] = '\0';
4009 logf("TAG:%s", buf);
4010 logf("too long filename");
4011 close(fd);
4012 return false;
4015 rc = read(fd, buf, fe->tag_length);
4016 if (rc != fe->tag_length)
4018 logf("read error #12");
4019 close(fd);
4020 return false;
4023 /* Check if the entry has already been removed */
4024 if (idx->flag & FLAG_DELETED)
4025 continue;
4027 /* This flag must not be used yet. */
4028 if (idx->flag & FLAG_DIRCACHE)
4030 logf("internal error!");
4031 close(fd);
4032 return false;
4035 if (idx->tag_seek[tag] != pos)
4037 logf("corrupt data structures!");
4038 close(fd);
4039 return false;
4042 # ifdef HAVE_DIRCACHE
4043 if (dircache_is_enabled())
4045 dc = dircache_get_entry_id(buf);
4046 if (dc < 0)
4048 logf("Entry no longer valid.");
4049 logf("-> %s", buf);
4050 if (global_settings.tagcache_autoupdate)
4051 delete_entry(fe->idx_id);
4052 continue ;
4055 idx->flag |= FLAG_DIRCACHE;
4056 idx->tag_seek[tag_filename] = dc;
4058 else
4059 # endif
4061 /* This will be very slow unless dircache is enabled
4062 or target is flash based, but do it anyway for
4063 consistency. */
4064 /* Check if entry has been removed. */
4065 if (global_settings.tagcache_autoupdate)
4067 if (!file_exists(buf))
4069 logf("Entry no longer valid.");
4070 logf("-> %s", buf);
4071 delete_entry(fe->idx_id);
4072 continue;
4077 continue ;
4080 bytesleft -= sizeof(struct tagfile_entry) + fe->tag_length;
4081 if (bytesleft < 0)
4083 logf("too big tagcache #2");
4084 logf("tl: %ld", fe->tag_length);
4085 logf("bl: %ld", bytesleft);
4086 close(fd);
4087 return false;
4090 p = fe->tag_data;
4091 rc = read(fd, fe->tag_data, fe->tag_length);
4092 p += rc;
4094 if (rc != fe->tag_length)
4096 logf("read error #13");
4097 logf("rc=0x%04x", rc); // 0x431
4098 logf("len=0x%04lx", fe->tag_length); // 0x4000
4099 logf("pos=0x%04lx", lseek(fd, 0, SEEK_CUR)); // 0x433
4100 logf("tag=0x%02x", tag); // 0x00
4101 close(fd);
4102 return false;
4105 close(fd);
4108 tc_stat.ramcache_used = tc_stat.ramcache_allocated - bytesleft;
4109 logf("tagcache loaded into ram!");
4111 return true;
4113 #endif /* HAVE_TC_RAMCACHE */
4115 static bool check_deleted_files(void)
4117 int fd;
4118 char buf[TAG_MAXLEN+32];
4119 struct tagfile_entry tfe;
4121 logf("reverse scan...");
4122 snprintf(buf, sizeof buf, TAGCACHE_FILE_INDEX, tag_filename);
4123 fd = open(buf, O_RDONLY);
4125 if (fd < 0)
4127 logf("%s open fail", buf);
4128 return false;
4131 lseek(fd, sizeof(struct tagcache_header), SEEK_SET);
4132 while (ecread_tagfile_entry(fd, &tfe) == sizeof(struct tagfile_entry)
4133 #ifndef __PCTOOL__
4134 && !check_event_queue()
4135 #endif
4138 if (tfe.tag_length >= (long)sizeof(buf)-1)
4140 logf("too long tag");
4141 close(fd);
4142 return false;
4145 if (read(fd, buf, tfe.tag_length) != tfe.tag_length)
4147 logf("read error #14");
4148 close(fd);
4149 return false;
4152 /* Check if the file has already deleted from the db. */
4153 if (*buf == '\0')
4154 continue;
4156 /* Now check if the file exists. */
4157 if (!file_exists(buf))
4159 logf("Entry no longer valid.");
4160 logf("-> %s / %ld", buf, tfe.tag_length);
4161 delete_entry(tfe.idx_id);
4165 close(fd);
4167 logf("done");
4169 return true;
4173 /* Note that this function must not be inlined, otherwise the whole point
4174 * of having the code in a separate function is lost.
4176 static void __attribute__ ((noinline)) check_ignore(const char *dirname,
4177 int *ignore, int *unignore)
4179 char newpath[MAX_PATH];
4181 /* check for a database.ignore file */
4182 snprintf(newpath, MAX_PATH, "%s/database.ignore", dirname);
4183 *ignore = file_exists(newpath);
4184 /* check for a database.unignore file */
4185 snprintf(newpath, MAX_PATH, "%s/database.unignore", dirname);
4186 *unignore = file_exists(newpath);
4189 static struct search_roots_ll {
4190 const char *path;
4191 struct search_roots_ll * next;
4192 } roots_ll;
4194 #ifdef APPLICATION
4196 * This adds a path to the search roots, possibly during traveling through
4197 * the filesystem. It only adds if the path is not inside an already existing
4198 * search root.
4200 * Returns true if it added the path to the search roots
4202 * Windows 2000 and greater supports symlinks, but they don't provide
4203 * realpath() or readlink(), and symlinks are rarely used on them so
4204 * ignore this for windows for now
4206 static bool add_search_root(const char *name)
4208 (void)name;
4209 #ifndef WIN32
4210 struct search_roots_ll *this, *prev = NULL;
4211 char target[MAX_PATH];
4212 /* Okay, realpath() is almost completely broken on android
4214 * It doesn't accept NULL for resolved_name to dynamically allocate
4215 * the resulting path; and it assumes resolved_name to be PATH_MAX
4216 * (actually MAXPATHLEN, but it's the same [as of 2.3]) long
4217 * and blindly writes to the end if it
4219 * therefore use sufficiently large static storage here
4220 * Note that PATH_MAX != MAX_PATH
4222 static char abs_target[PATH_MAX];
4223 ssize_t len;
4225 len = readlink(name, target, sizeof(target));
4226 if (len < 0)
4227 return false;
4229 target[len] = '\0';
4230 if (realpath(target, abs_target) == NULL)
4231 return false;
4233 for(this = &roots_ll; this; prev = this, this = this->next)
4235 size_t root_len = strlen(this->path);
4236 /* check if the link target is inside of an existing search root
4237 * don't add if target is inside, we'll scan it later */
4238 if (!strncmp(this->path, abs_target, root_len))
4239 return false;
4242 if (prev)
4244 size_t len = strlen(abs_target) + 1; /* count \0 */
4245 this = malloc(sizeof(struct search_roots_ll) + len );
4246 if (!this || len > MAX_PATH)
4248 logf("Error at adding a search root: %s", this ? "path too long":"OOM");
4249 free(this);
4250 prev->next = NULL;
4251 return false;
4253 this->path = ((char*)this) + sizeof(struct search_roots_ll);
4254 strcpy((char*)this->path, abs_target); /* ok to cast const away here */
4255 this->next = NULL;
4256 prev->next = this;
4257 logf("Added %s to the search roots\n", abs_target);
4258 return true;
4260 #endif
4261 return false;
4264 static int free_search_roots(struct search_roots_ll * start)
4266 int ret = 0;
4267 if (start->next)
4269 ret += free_search_roots(start->next);
4270 ret += sizeof(struct search_roots_ll);
4271 free(start->next);
4273 return ret;
4275 #else /* native, simulator */
4276 #define add_search_root(a) do {} while(0)
4277 #define free_search_roots(a) do {} while(0)
4278 #endif
4280 static bool check_dir(const char *dirname, int add_files)
4282 DIR *dir;
4283 int len;
4284 int success = false;
4285 int ignore, unignore;
4287 dir = opendir(dirname);
4288 if (!dir)
4290 logf("tagcache: opendir(%s) failed", dirname);
4291 return false;
4293 /* check for a database.ignore and database.unignore */
4294 check_ignore(dirname, &ignore, &unignore);
4296 /* don't do anything if both ignore and unignore are there */
4297 if (ignore != unignore)
4298 add_files = unignore;
4300 /* Recursively scan the dir. */
4301 #ifdef __PCTOOL__
4302 while (1)
4303 #else
4304 while (!check_event_queue())
4305 #endif
4307 struct dirent *entry = readdir(dir);
4308 if (entry == NULL)
4310 success = true;
4311 break;
4314 if (!strcmp((char *)entry->d_name, ".") ||
4315 !strcmp((char *)entry->d_name, ".."))
4316 continue;
4318 struct dirinfo info = dir_get_info(dir, entry);
4320 yield();
4322 len = strlen(curpath);
4323 /* don't add an extra / for curpath == / */
4324 if (len <= 1) len = 0;
4325 snprintf(&curpath[len], sizeof(curpath) - len, "/%s", entry->d_name);
4327 processed_dir_count++;
4328 if (info.attribute & ATTR_DIRECTORY)
4329 #ifndef SIMULATOR
4330 { /* don't follow symlinks to dirs, but try to add it as a search root
4331 * this makes able to avoid looping in recursive symlinks */
4332 if (info.attribute & ATTR_LINK)
4333 add_search_root(curpath);
4334 else
4335 check_dir(curpath, add_files);
4337 #else
4338 check_dir(curpath, add_files);
4339 #endif
4340 else if (add_files)
4342 tc_stat.curentry = curpath;
4344 /* Add a new entry to the temporary db file. */
4345 add_tagcache(curpath, (info.wrtdate << 16) | info.wrttime
4346 #if defined(HAVE_TC_RAMCACHE) && defined(HAVE_DIRCACHE)
4347 , dir->internal_entry
4348 #endif
4351 /* Wait until current path for debug screen is read and unset. */
4352 while (tc_stat.syncscreen && tc_stat.curentry != NULL)
4353 yield();
4355 tc_stat.curentry = NULL;
4358 curpath[len] = '\0';
4361 closedir(dir);
4363 return success;
4366 void tagcache_screensync_event(void)
4368 tc_stat.curentry = NULL;
4371 void tagcache_screensync_enable(bool state)
4373 tc_stat.syncscreen = state;
4376 void tagcache_build(const char *path)
4378 struct tagcache_header header;
4379 bool ret;
4381 curpath[0] = '\0';
4382 data_size = 0;
4383 total_entry_count = 0;
4384 processed_dir_count = 0;
4386 #ifdef HAVE_DIRCACHE
4387 while (dircache_is_initializing())
4388 sleep(1);
4389 #endif
4391 logf("updating tagcache");
4393 cachefd = open(TAGCACHE_FILE_TEMP, O_RDONLY);
4394 if (cachefd >= 0)
4396 logf("skipping, cache already waiting for commit");
4397 close(cachefd);
4398 return ;
4401 cachefd = open(TAGCACHE_FILE_TEMP, O_RDWR | O_CREAT | O_TRUNC, 0666);
4402 if (cachefd < 0)
4404 logf("master file open failed: %s", TAGCACHE_FILE_TEMP);
4405 return ;
4408 filenametag_fd = open_tag_fd(&header, tag_filename, false);
4410 cpu_boost(true);
4412 logf("Scanning files...");
4413 /* Scan for new files. */
4414 memset(&header, 0, sizeof(struct tagcache_header));
4415 write(cachefd, &header, sizeof(struct tagcache_header));
4417 ret = true;
4418 roots_ll.path = path;
4419 roots_ll.next = NULL;
4420 struct search_roots_ll * this;
4421 /* check_dir might add new roots */
4422 for(this = &roots_ll; this; this = this->next)
4424 strcpy(curpath, this->path);
4425 ret = ret && check_dir(this->path, true);
4427 if (roots_ll.next)
4428 free_search_roots(roots_ll.next);
4430 /* Write the header. */
4431 header.magic = TAGCACHE_MAGIC;
4432 header.datasize = data_size;
4433 header.entry_count = total_entry_count;
4434 lseek(cachefd, 0, SEEK_SET);
4435 write(cachefd, &header, sizeof(struct tagcache_header));
4436 close(cachefd);
4438 if (filenametag_fd >= 0)
4440 close(filenametag_fd);
4441 filenametag_fd = -1;
4444 if (!ret)
4446 logf("Aborted.");
4447 cpu_boost(false);
4448 return ;
4451 /* Commit changes to the database. */
4452 #ifdef __PCTOOL__
4453 allocate_tempbuf();
4454 #endif
4455 if (commit())
4457 logf("tagcache built!");
4459 #ifdef __PCTOOL__
4460 free_tempbuf();
4461 #endif
4463 #ifdef HAVE_TC_RAMCACHE
4464 if (hdr)
4466 /* Import runtime statistics if we just initialized the db. */
4467 if (current_tcmh.serial == 0)
4468 queue_post(&tagcache_queue, Q_IMPORT_CHANGELOG, 0);
4470 #endif
4472 cpu_boost(false);
4475 #ifdef HAVE_TC_RAMCACHE
4476 static void load_ramcache(void)
4478 if (!hdr)
4479 return ;
4481 cpu_boost(true);
4483 /* At first we should load the cache (if exists). */
4484 tc_stat.ramcache = load_tagcache();
4486 if (!tc_stat.ramcache)
4488 /* If loading failed, it must indicate some problem with the db
4489 * so disable it entirely to prevent further issues. */
4490 tc_stat.ready = false;
4491 hdr = NULL;
4494 cpu_boost(false);
4497 void tagcache_unload_ramcache(void)
4499 tc_stat.ramcache = false;
4500 /* Just to make sure there is no statefile present. */
4501 // remove(TAGCACHE_STATEFILE);
4503 #endif
4505 #ifndef __PCTOOL__
4506 static void tagcache_thread(void)
4508 struct queue_event ev;
4509 bool check_done = false;
4511 /* If the previous cache build/update was interrupted, commit
4512 * the changes first in foreground. */
4513 cpu_boost(true);
4514 allocate_tempbuf();
4515 commit();
4516 free_tempbuf();
4518 #ifdef HAVE_TC_RAMCACHE
4519 # ifdef HAVE_EEPROM_SETTINGS
4520 if (firmware_settings.initialized && firmware_settings.disk_clean
4521 && global_settings.tagcache_ram)
4523 check_done = tagcache_dumpload();
4526 remove(TAGCACHE_STATEFILE);
4527 # endif
4529 /* Allocate space for the tagcache if found on disk. */
4530 if (global_settings.tagcache_ram && !tc_stat.ramcache)
4531 allocate_tagcache();
4532 #endif
4534 cpu_boost(false);
4535 tc_stat.initialized = true;
4537 /* Don't delay bootup with the header check but do it on background. */
4538 if (!tc_stat.ready)
4540 sleep(HZ);
4541 tc_stat.ready = check_all_headers();
4542 tc_stat.readyvalid = true;
4545 while (1)
4547 run_command_queue(false);
4549 queue_wait_w_tmo(&tagcache_queue, &ev, HZ);
4551 switch (ev.id)
4553 case Q_IMPORT_CHANGELOG:
4554 tagcache_import_changelog();
4555 break;
4557 case Q_REBUILD:
4558 remove_files();
4559 remove(TAGCACHE_FILE_TEMP);
4560 tagcache_build("/");
4561 break;
4563 case Q_UPDATE:
4564 tagcache_build("/");
4565 #ifdef HAVE_TC_RAMCACHE
4566 load_ramcache();
4567 #endif
4568 check_deleted_files();
4569 break ;
4571 case Q_START_SCAN:
4572 check_done = false;
4573 case SYS_TIMEOUT:
4574 if (check_done || !tc_stat.ready)
4575 break ;
4577 #ifdef HAVE_TC_RAMCACHE
4578 if (!tc_stat.ramcache && global_settings.tagcache_ram)
4580 load_ramcache();
4581 if (tc_stat.ramcache && global_settings.tagcache_autoupdate)
4582 tagcache_build("/");
4584 else
4585 #endif
4586 if (global_settings.tagcache_autoupdate)
4588 tagcache_build("/");
4590 /* This will be very slow unless dircache is enabled
4591 or target is flash based, but do it anyway for
4592 consistency. */
4593 check_deleted_files();
4596 logf("tagcache check done");
4598 check_done = true;
4599 break ;
4601 case Q_STOP_SCAN:
4602 break ;
4604 case SYS_POWEROFF:
4605 break ;
4607 #if (CONFIG_PLATFORM & PLATFORM_NATIVE)
4608 case SYS_USB_CONNECTED:
4609 logf("USB: TagCache");
4610 usb_acknowledge(SYS_USB_CONNECTED_ACK);
4611 usb_wait_for_disconnect(&tagcache_queue);
4612 break ;
4613 #endif
4618 bool tagcache_prepare_shutdown(void)
4620 if (tagcache_get_commit_step() > 0)
4621 return false;
4623 tagcache_stop_scan();
4624 while (read_lock || write_lock)
4625 sleep(1);
4627 return true;
4630 void tagcache_shutdown(void)
4632 /* Flush the command queue. */
4633 run_command_queue(true);
4635 #ifdef HAVE_EEPROM_SETTINGS
4636 if (tc_stat.ramcache)
4637 tagcache_dumpsave();
4638 #endif
4641 static int get_progress(void)
4643 int total_count = -1;
4645 #ifdef HAVE_DIRCACHE
4646 if (dircache_is_enabled())
4648 total_count = dircache_get_entry_count();
4650 else
4651 #endif
4652 #ifdef HAVE_TC_RAMCACHE
4654 if (hdr && tc_stat.ramcache)
4655 total_count = current_tcmh.tch.entry_count;
4657 #endif
4659 if (total_count < 0)
4660 return -1;
4662 return processed_dir_count * 100 / total_count;
4665 struct tagcache_stat* tagcache_get_stat(void)
4667 tc_stat.progress = get_progress();
4668 tc_stat.processed_entries = processed_dir_count;
4670 return &tc_stat;
4673 void tagcache_start_scan(void)
4675 queue_post(&tagcache_queue, Q_START_SCAN, 0);
4678 bool tagcache_update(void)
4680 if (!tc_stat.ready)
4681 return false;
4683 queue_post(&tagcache_queue, Q_UPDATE, 0);
4684 return false;
4687 bool tagcache_rebuild()
4689 queue_post(&tagcache_queue, Q_REBUILD, 0);
4690 return false;
4693 void tagcache_stop_scan(void)
4695 queue_post(&tagcache_queue, Q_STOP_SCAN, 0);
4698 #ifdef HAVE_TC_RAMCACHE
4699 bool tagcache_is_ramcache(void)
4701 return tc_stat.ramcache;
4703 #endif
4705 #endif /* !__PCTOOL__ */
4708 void tagcache_init(void)
4710 memset(&tc_stat, 0, sizeof(struct tagcache_stat));
4711 memset(&current_tcmh, 0, sizeof(struct master_header));
4712 filenametag_fd = -1;
4713 write_lock = read_lock = 0;
4715 #ifndef __PCTOOL__
4716 mutex_init(&command_queue_mutex);
4717 queue_init(&tagcache_queue, true);
4718 create_thread(tagcache_thread, tagcache_stack,
4719 sizeof(tagcache_stack), 0, tagcache_thread_name
4720 IF_PRIO(, PRIORITY_BACKGROUND)
4721 IF_COP(, CPU));
4722 #else
4723 tc_stat.initialized = true;
4724 allocate_tempbuf();
4725 commit();
4726 free_tempbuf();
4727 tc_stat.ready = check_all_headers();
4728 #endif
4731 #ifdef __PCTOOL__
4732 void tagcache_reverse_scan(void)
4734 logf("Checking for deleted files");
4735 check_deleted_files();
4737 #endif
4739 bool tagcache_is_initialized(void)
4741 return tc_stat.initialized;
4743 bool tagcache_is_fully_initialized(void)
4745 return tc_stat.readyvalid;
4747 bool tagcache_is_usable(void)
4749 return tc_stat.initialized && tc_stat.ready;
4751 int tagcache_get_commit_step(void)
4753 return tc_stat.commit_step;
4755 int tagcache_get_max_commit_step(void)
4757 return (int)(SORTED_TAGS_COUNT)+1;