1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
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 ****************************************************************************/
25 * ----------x---------x------------------x-----
27 * +---------------x-------+ | TagCache | Libraries
28 * | Modification routines | | Core |
29 * +-x---------x-----------+ | |
31 * | +------x-------------x-+ +-------------x-----+ |
32 * | | x==x Filters & clauses | |
33 * | | Search routines | +-------------------+ |
34 * | | x============================x DirCache
35 * | +-x--------------------+ | (optional)
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*/
64 #include <unistd.h> /* readlink() */
65 #include <limits.h> /* PATH_MAX */
68 #include "ata_idle_notify.h"
73 #include "string-extra.h"
82 #include "filefuncs.h"
88 #include "eeprom_settings.h"
92 #define yield() do { } while(0)
93 #define sim_sleep(timeout) do { } while(0)
94 #define do_timed_yield() do { } while(0)
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";
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
{
146 /* Internal tagcache command queue. */
147 CMD_UPDATE_MASTER_HEADER
,
151 struct tagcache_command_entry
{
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
;
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. */
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 */
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
;
223 /* Pointer to allocated ramcache_header */
224 static struct ramcache_header
*ramcache_hdr
;
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
];
238 struct tempbuf_id_list
{
240 struct tempbuf_id_list
*next
;
243 struct tempbuf_searchidx
{
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
);
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
);
299 static int open_tag_fd(struct tagcache_header
*hdr
, int tag
, bool write
)
305 if (TAGCACHE_IS_NUMERIC(tag
) || tag
< 0 || tag
>= TAG_COUNT
)
308 snprintf(buf
, sizeof buf
, TAGCACHE_FILE_INDEX
, tag
);
310 fd
= open(buf
, write
? O_RDWR
: O_RDONLY
);
313 logf("tag file open failed: tag=%d write=%d file=%s", tag
, write
, buf
);
314 tc_stat
.ready
= false;
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;
331 static int open_master_fd(struct master_header
*hdr
, bool write
)
336 fd
= open(TAGCACHE_FILE_MASTER
, write
? O_RDWR
: O_RDONLY
);
339 logf("master file open failed for R/W");
340 tc_stat
.ready
= false;
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
))
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;
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);
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;
394 /* Check if tagcache is loaded into ram. */
395 if (!tc_stat
.ramcache
|| !is_dircache_intact())
399 dc
= dircache_get_entry_id(filename
);
403 logf("tagcache: file not found.");
414 for (; i
< current_tcmh
.tch
.entry_count
; i
++)
416 if (ramcache_hdr
->indices
[i
].tag_seek
[tag_filename
] == dc
)
418 last_pos
= MAX(0, i
- 3);
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;
442 struct tagfile_entry tfe
;
444 char buf
[TAG_MAXLEN
+32];
448 const char *filename
= filename_raw
;
450 char pathbuf
[PATH_MAX
]; /* Note: Don't use MAX_PATH here, it's too small */
451 if (realpath(filename
, pathbuf
) == pathbuf
)
459 if (fd
< 0 || localfd
)
462 if ( (fd
= open_tag_fd(&tch
, tag_filename
, false)) < 0)
469 lseek(fd
, last_pos
, SEEK_SET
);
471 lseek(fd
, sizeof(struct tagcache_header
), SEEK_SET
);
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
))
486 if (tfe
.tag_length
>= (long)sizeof(buf
))
488 logf("too long tag #1");
496 if (read(fd
, buf
, tfe
.tag_length
) != tfe
.tag_length
)
498 logf("read error #2");
506 if (!strcmp(filename
, buf
))
508 last_pos
= pos_history
[pos_history_idx
];
513 if (pos_history_idx
< POS_HISTORY_COUNT
- 1)
527 if (fd
!= filenametag_fd
|| localfd
)
532 if (fd
!= filenametag_fd
|| localfd
)
538 static int find_index(const char *filename
)
542 #if defined(HAVE_TC_RAMCACHE) && defined(HAVE_DIRCACHE)
543 idx_id
= find_entry_ram(filename
, -1);
547 idx_id
= find_entry_disk(filename
, true);
552 bool tagcache_find_index(struct tagcache_search
*tcs
, const char *filename
)
559 idx_id
= find_index(filename
);
563 if (!tagcache_search(tcs
, tag_filename
))
566 tcs
->entry_count
= 0;
567 tcs
->idx_id
= idx_id
;
572 static bool get_index(int masterfd
, int idxid
,
573 struct index_entry
*idx
, bool use_ram
)
575 bool localfd
= false;
579 logf("Incorrect idxid: %d", idxid
);
583 #ifdef HAVE_TC_RAMCACHE
584 if (tc_stat
.ramcache
&& use_ram
)
586 if (ramcache_hdr
->indices
[idxid
].flag
& FLAG_DELETED
)
589 # ifdef HAVE_DIRCACHE
590 if (!(ramcache_hdr
->indices
[idxid
].flag
& FLAG_DIRCACHE
)
591 || is_dircache_intact())
594 memcpy(idx
, &ramcache_hdr
->indices
[idxid
], sizeof(struct index_entry
));
604 struct master_header tcmh
;
607 masterfd
= open_master_fd(&tcmh
, false);
612 lseek(masterfd
, idxid
* sizeof(struct index_entry
)
613 + sizeof(struct master_header
), SEEK_SET
);
614 if (ecread_index_entry(masterfd
, idx
)
615 != sizeof(struct index_entry
))
617 logf("read error #3");
627 if (idx
->flag
& FLAG_DELETED
)
635 static bool write_index(int masterfd
, int idxid
, struct index_entry
*idx
)
637 /* We need to exclude all memory only flags & tags when writing to disk. */
638 if (idx
->flag
& FLAG_DIRCACHE
)
640 logf("memory only flags!");
644 #ifdef HAVE_TC_RAMCACHE
645 /* Only update numeric data. Writing the whole index to RAM by memcpy
646 * destroys dircache pointers!
648 if (tc_stat
.ramcache
)
651 struct index_entry
*idx_ram
= &ramcache_hdr
->indices
[idxid
];
653 for (tag
= 0; tag
< TAG_COUNT
; tag
++)
655 if (TAGCACHE_IS_NUMERIC(tag
))
657 idx_ram
->tag_seek
[tag
] = idx
->tag_seek
[tag
];
661 /* Don't touch the dircache flag or attributes. */
662 idx_ram
->flag
= (idx
->flag
& 0x0000ffff)
663 | (idx_ram
->flag
& (0xffff0000 | FLAG_DIRCACHE
));
667 lseek(masterfd
, idxid
* sizeof(struct index_entry
)
668 + sizeof(struct master_header
), SEEK_SET
);
669 if (ecwrite_index_entry(masterfd
, idx
) != sizeof(struct index_entry
))
671 logf("write error #3");
672 logf("idxid: %d", idxid
);
679 #endif /* !__PCTOOL__ */
681 static bool open_files(struct tagcache_search
*tcs
, int tag
)
683 if (tcs
->idxfd
[tag
] < 0)
687 snprintf(fn
, sizeof fn
, TAGCACHE_FILE_INDEX
, tag
);
688 tcs
->idxfd
[tag
] = open(fn
, O_RDONLY
);
691 if (tcs
->idxfd
[tag
] < 0)
693 logf("File not open!");
700 static bool retrieve(struct tagcache_search
*tcs
, struct index_entry
*idx
,
701 int tag
, char *buf
, long size
)
703 struct tagfile_entry tfe
;
708 if (TAGCACHE_IS_NUMERIC(tag
))
711 seek
= idx
->tag_seek
[tag
];
714 logf("Retrieve failed");
718 #ifdef HAVE_TC_RAMCACHE
721 struct tagfile_entry
*ep
;
723 # ifdef HAVE_DIRCACHE
724 if (tag
== tag_filename
&& (idx
->flag
& FLAG_DIRCACHE
)
725 && is_dircache_intact())
727 /* for tag_filename, seek is a dircache index */
728 dircache_copy_path(seek
, buf
, size
);
733 if (tag
!= tag_filename
)
735 ep
= (struct tagfile_entry
*)&ramcache_hdr
->tags
[tag
][seek
];
736 strlcpy(buf
, ep
->tag_data
, size
);
743 if (!open_files(tcs
, tag
))
746 lseek(tcs
->idxfd
[tag
], seek
, SEEK_SET
);
747 if (ecread_tagfile_entry(tcs
->idxfd
[tag
], &tfe
)
748 != sizeof(struct tagfile_entry
))
750 logf("read error #5");
754 if (tfe
.tag_length
>= size
)
756 logf("too small buffer");
760 if (read(tcs
->idxfd
[tag
], buf
, tfe
.tag_length
) !=
763 logf("read error #6");
767 buf
[tfe
.tag_length
] = '\0';
772 #define COMMAND_QUEUE_IS_EMPTY (command_queue_ridx == command_queue_widx)
774 static long find_tag(int tag
, int idx_id
, const struct index_entry
*idx
)
777 if (! COMMAND_QUEUE_IS_EMPTY
&& TAGCACHE_IS_NUMERIC(tag
))
779 /* Attempt to find tag data through store-to-load forwarding in
783 mutex_lock(&command_queue_mutex
);
785 int ridx
= command_queue_widx
;
787 while (ridx
!= command_queue_ridx
)
790 ridx
= TAGCACHE_COMMAND_QUEUE_LENGTH
- 1;
792 if (command_queue
[ridx
].command
== CMD_UPDATE_NUMERIC
793 && command_queue
[ridx
].idx_id
== idx_id
794 && command_queue
[ridx
].tag
== tag
)
796 result
= command_queue
[ridx
].data
;
801 mutex_unlock(&command_queue_mutex
);
806 "Recovered tag %d value %lX from write queue",
813 return idx
->tag_seek
[tag
];
817 static long check_virtual_tags(int tag
, int idx_id
,
818 const struct index_entry
*idx
)
824 case tag_virt_length_sec
:
825 data
= (find_tag(tag_length
, idx_id
, idx
)/1000) % 60;
828 case tag_virt_length_min
:
829 data
= (find_tag(tag_length
, idx_id
, idx
)/1000) / 60;
832 case tag_virt_playtime_sec
:
833 data
= (find_tag(tag_playtime
, idx_id
, idx
)/1000) % 60;
836 case tag_virt_playtime_min
:
837 data
= (find_tag(tag_playtime
, idx_id
, idx
)/1000) / 60;
840 case tag_virt_autoscore
:
841 if (find_tag(tag_length
, idx_id
, idx
) == 0
842 || find_tag(tag_playcount
, idx_id
, idx
) == 0)
848 /* A straight calculus gives:
849 autoscore = 100 * playtime / length / playcout (1)
850 Now, consider the euclidian division of playtime by length:
851 playtime = alpha * length + beta
855 autoscore = 100 * (alpha / playcout + beta / length / playcount)
856 Both terms should be small enough to avoid any overflow
858 data
= 100 * (find_tag(tag_playtime
, idx_id
, idx
)
859 / find_tag(tag_length
, idx_id
, idx
))
860 + (100 * (find_tag(tag_playtime
, idx_id
, idx
)
861 % find_tag(tag_length
, idx_id
, idx
)))
862 / find_tag(tag_length
, idx_id
, idx
);
863 data
/= find_tag(tag_playcount
, idx_id
, idx
);
867 /* How many commits before the file has been added to the DB. */
868 case tag_virt_entryage
:
869 data
= current_tcmh
.commitid
870 - find_tag(tag_commitid
, idx_id
, idx
) - 1;
873 case tag_virt_basename
:
874 tag
= tag_filename
; /* return filename; caller handles basename */
878 data
= find_tag(tag
, idx_id
, idx
);
884 long tagcache_get_numeric(const struct tagcache_search
*tcs
, int tag
)
886 struct index_entry idx
;
891 if (!TAGCACHE_IS_NUMERIC(tag
))
894 if (!get_index(tcs
->masterfd
, tcs
->idx_id
, &idx
, true))
897 return check_virtual_tags(tag
, tcs
->idx_id
, &idx
);
900 inline static bool str_ends_with(const char *str1
, const char *str2
)
902 int str_len
= strlen(str1
);
903 int clause_len
= strlen(str2
);
905 if (clause_len
> str_len
)
908 return !strcasecmp(&str1
[str_len
- clause_len
], str2
);
911 inline static bool str_oneof(const char *str
, const char *list
)
914 int l
, len
= strlen(str
);
918 sep
= strchr(list
, '|');
919 l
= sep
? (long)sep
- (long)list
: (int)strlen(list
);
920 if ((l
==len
) && !strncasecmp(str
, list
, len
))
922 list
+= sep
? l
+ 1 : l
;
928 static bool check_against_clause(long numeric
, const char *str
,
929 const struct tagcache_search_clause
*clause
)
933 switch (clause
->type
)
936 return numeric
== clause
->numeric_data
;
938 return numeric
!= clause
->numeric_data
;
940 return numeric
> clause
->numeric_data
;
942 return numeric
>= clause
->numeric_data
;
944 return numeric
< clause
->numeric_data
;
946 return numeric
<= clause
->numeric_data
;
948 logf("Incorrect numeric tag: %d", clause
->type
);
953 switch (clause
->type
)
956 return !strcasecmp(clause
->str
, str
);
958 return strcasecmp(clause
->str
, str
);
960 return 0>strcasecmp(clause
->str
, str
);
962 return 0>=strcasecmp(clause
->str
, str
);
964 return 0<strcasecmp(clause
->str
, str
);
966 return 0<=strcasecmp(clause
->str
, str
);
967 case clause_contains
:
968 return (strcasestr(str
, clause
->str
) != NULL
);
969 case clause_not_contains
:
970 return (strcasestr(str
, clause
->str
) == NULL
);
971 case clause_begins_with
:
972 return (strcasestr(str
, clause
->str
) == str
);
973 case clause_not_begins_with
:
974 return (strcasestr(str
, clause
->str
) != str
);
975 case clause_ends_with
:
976 return str_ends_with(str
, clause
->str
);
977 case clause_not_ends_with
:
978 return !str_ends_with(str
, clause
->str
);
980 return str_oneof(str
, clause
->str
);
983 logf("Incorrect tag: %d", clause
->type
);
990 static bool check_clauses(struct tagcache_search
*tcs
,
991 struct index_entry
*idx
,
992 struct tagcache_search_clause
**clauses
, int count
)
996 /* Go through all conditional clauses. */
997 for (i
= 0; i
< count
; i
++)
1002 struct tagcache_search_clause
*clause
= clauses
[i
];
1004 if (clause
->type
== clause_logical_or
)
1005 break; /* all conditions before logical-or satisfied --
1006 stop processing clauses */
1008 seek
= check_virtual_tags(clause
->tag
, tcs
->idx_id
, idx
);
1010 #ifdef HAVE_TC_RAMCACHE
1013 struct tagfile_entry
*tfe
;
1015 if (!TAGCACHE_IS_NUMERIC(clause
->tag
))
1017 if (clause
->tag
== tag_filename
1018 || clause
->tag
== tag_virt_basename
)
1020 retrieve(tcs
, idx
, tag_filename
, buf
, sizeof buf
);
1024 tfe
= (struct tagfile_entry
*)
1025 &ramcache_hdr
->tags
[clause
->tag
][seek
];
1026 str
= tfe
->tag_data
;
1033 struct tagfile_entry tfe
;
1035 if (!TAGCACHE_IS_NUMERIC(clause
->tag
))
1037 int tag
= clause
->tag
;
1038 if (tag
== tag_virt_basename
)
1041 int fd
= tcs
->idxfd
[tag
];
1042 lseek(fd
, seek
, SEEK_SET
);
1043 ecread_tagfile_entry(fd
, &tfe
);
1044 if (tfe
.tag_length
>= (int)sizeof(buf
))
1046 logf("Too long tag read!");
1050 read(fd
, str
, tfe
.tag_length
);
1051 str
[tfe
.tag_length
] = '\0';
1053 /* Check if entry has been deleted. */
1059 if (clause
->tag
== tag_virt_basename
)
1061 char *basename
= strrchr(str
, '/');
1066 if (!check_against_clause(seek
, str
, clause
))
1068 /* Clause failed -- try finding a logical-or clause */
1071 if (clauses
[i
]->type
== clause_logical_or
)
1075 if (i
< count
) /* Found logical-or? */
1076 continue; /* Check clauses after logical-or */
1085 bool tagcache_check_clauses(struct tagcache_search
*tcs
,
1086 struct tagcache_search_clause
**clause
, int count
)
1088 struct index_entry idx
;
1093 if (!get_index(tcs
->masterfd
, tcs
->idx_id
, &idx
, true))
1096 return check_clauses(tcs
, &idx
, clause
, count
);
1099 static bool add_uniqbuf(struct tagcache_search
*tcs
, unsigned long id
)
1103 /* If uniq buffer is not defined we must return true for search to work. */
1104 if (tcs
->unique_list
== NULL
|| (!TAGCACHE_IS_UNIQUE(tcs
->type
)
1105 && !TAGCACHE_IS_NUMERIC(tcs
->type
)))
1110 for (i
= 0; i
< tcs
->unique_list_count
; i
++)
1112 /* Return false if entry is found. */
1113 if (tcs
->unique_list
[i
] == id
)
1117 if (tcs
->unique_list_count
< tcs
->unique_list_capacity
)
1119 tcs
->unique_list
[i
] = id
;
1120 tcs
->unique_list_count
++;
1126 static bool build_lookup_list(struct tagcache_search
*tcs
)
1128 struct index_entry entry
;
1131 tcs
->seek_list_count
= 0;
1133 #ifdef HAVE_TC_RAMCACHE
1135 # ifdef HAVE_DIRCACHE
1136 && (tcs
->type
!= tag_filename
|| is_dircache_intact())
1140 for (i
= tcs
->seek_pos
; i
< current_tcmh
.tch
.entry_count
; i
++)
1142 struct tagcache_seeklist_entry
*seeklist
;
1143 struct index_entry
*idx
= &ramcache_hdr
->indices
[i
];
1144 if (tcs
->seek_list_count
== SEEK_LIST_SIZE
)
1147 /* Skip deleted files. */
1148 if (idx
->flag
& FLAG_DELETED
)
1151 /* Go through all filters.. */
1152 for (j
= 0; j
< tcs
->filter_count
; j
++)
1154 if (idx
->tag_seek
[tcs
->filter_tag
[j
]] != tcs
->filter_seek
[j
])
1160 if (j
< tcs
->filter_count
)
1163 /* Check for conditions. */
1164 if (!check_clauses(tcs
, idx
, tcs
->clause
, tcs
->clause_count
))
1167 /* Add to the seek list if not already in uniq buffer. */
1168 if (!add_uniqbuf(tcs
, idx
->tag_seek
[tcs
->type
]))
1172 seeklist
= &tcs
->seeklist
[tcs
->seek_list_count
];
1173 seeklist
->seek
= idx
->tag_seek
[tcs
->type
];
1174 seeklist
->flag
= idx
->flag
;
1175 seeklist
->idx_id
= i
;
1176 tcs
->seek_list_count
++;
1181 return tcs
->seek_list_count
> 0;
1185 if (tcs
->masterfd
< 0)
1187 struct master_header tcmh
;
1188 tcs
->masterfd
= open_master_fd(&tcmh
, false);
1191 lseek(tcs
->masterfd
, tcs
->seek_pos
* sizeof(struct index_entry
) +
1192 sizeof(struct master_header
), SEEK_SET
);
1194 while (ecread_index_entry(tcs
->masterfd
, &entry
)
1195 == sizeof(struct index_entry
))
1197 struct tagcache_seeklist_entry
*seeklist
;
1199 if (tcs
->seek_list_count
== SEEK_LIST_SIZE
)
1205 /* Check if entry has been deleted. */
1206 if (entry
.flag
& FLAG_DELETED
)
1209 /* Go through all filters.. */
1210 for (j
= 0; j
< tcs
->filter_count
; j
++)
1212 if (entry
.tag_seek
[tcs
->filter_tag
[j
]] != tcs
->filter_seek
[j
])
1216 if (j
< tcs
->filter_count
)
1219 /* Check for conditions. */
1220 if (!check_clauses(tcs
, &entry
, tcs
->clause
, tcs
->clause_count
))
1223 /* Add to the seek list if not already in uniq buffer. */
1224 if (!add_uniqbuf(tcs
, entry
.tag_seek
[tcs
->type
]))
1228 seeklist
= &tcs
->seeklist
[tcs
->seek_list_count
];
1229 seeklist
->seek
= entry
.tag_seek
[tcs
->type
];
1230 seeklist
->flag
= entry
.flag
;
1231 seeklist
->idx_id
= i
;
1232 tcs
->seek_list_count
++;
1237 return tcs
->seek_list_count
> 0;
1241 static void remove_files(void)
1246 tc_stat
.ready
= false;
1247 tc_stat
.ramcache
= false;
1248 tc_stat
.econ
= false;
1249 remove(TAGCACHE_FILE_MASTER
);
1250 for (i
= 0; i
< TAG_COUNT
; i
++)
1252 if (TAGCACHE_IS_NUMERIC(i
))
1255 snprintf(buf
, sizeof buf
, TAGCACHE_FILE_INDEX
, i
);
1261 static bool check_all_headers(void)
1263 struct master_header myhdr
;
1264 struct tagcache_header tch
;
1268 if ( (fd
= open_master_fd(&myhdr
, false)) < 0)
1274 logf("tagcache is dirty!");
1278 memcpy(¤t_tcmh
, &myhdr
, sizeof(struct master_header
));
1280 for (tag
= 0; tag
< TAG_COUNT
; tag
++)
1282 if (TAGCACHE_IS_NUMERIC(tag
))
1285 if ( (fd
= open_tag_fd(&tch
, tag
, false)) < 0)
1294 bool tagcache_is_busy(void)
1296 return read_lock
|| write_lock
;
1299 bool tagcache_search(struct tagcache_search
*tcs
, int tag
)
1301 struct tagcache_header tag_hdr
;
1302 struct master_header master_hdr
;
1308 memset(tcs
, 0, sizeof(struct tagcache_search
));
1309 if (tc_stat
.commit_step
> 0 || !tc_stat
.ready
)
1312 tcs
->position
= sizeof(struct tagcache_header
);
1315 tcs
->list_position
= 0;
1316 tcs
->seek_list_count
= 0;
1317 tcs
->filter_count
= 0;
1320 for (i
= 0; i
< TAG_COUNT
; i
++)
1323 #ifndef HAVE_TC_RAMCACHE
1324 tcs
->ramsearch
= false;
1326 tcs
->ramsearch
= tc_stat
.ramcache
;
1329 tcs
->entry_count
= ramcache_hdr
->entry_count
[tcs
->type
];
1334 /* Always open as R/W so we can pass tcs to functions that modify data also
1335 * without failing. */
1336 tcs
->masterfd
= open_master_fd(&master_hdr
, true);
1337 if (tcs
->masterfd
< 0)
1340 if (!TAGCACHE_IS_NUMERIC(tcs
->type
))
1342 tcs
->idxfd
[tcs
->type
] = open_tag_fd(&tag_hdr
, tcs
->type
, false);
1343 if (tcs
->idxfd
[tcs
->type
] < 0)
1346 tcs
->entry_count
= tag_hdr
.entry_count
;
1350 tcs
->entry_count
= master_hdr
.tch
.entry_count
;
1355 tcs
->initialized
= true;
1361 void tagcache_search_set_uniqbuf(struct tagcache_search
*tcs
,
1362 void *buffer
, long length
)
1364 tcs
->unique_list
= (unsigned long *)buffer
;
1365 tcs
->unique_list_capacity
= length
/ sizeof(*tcs
->unique_list
);
1366 tcs
->unique_list_count
= 0;
1369 bool tagcache_search_add_filter(struct tagcache_search
*tcs
,
1372 if (tcs
->filter_count
== TAGCACHE_MAX_FILTERS
)
1375 if (TAGCACHE_IS_NUMERIC_OR_NONUNIQUE(tag
))
1378 tcs
->filter_tag
[tcs
->filter_count
] = tag
;
1379 tcs
->filter_seek
[tcs
->filter_count
] = seek
;
1380 tcs
->filter_count
++;
1385 bool tagcache_search_add_clause(struct tagcache_search
*tcs
,
1386 struct tagcache_search_clause
*clause
)
1390 if (tcs
->clause_count
>= TAGCACHE_MAX_CLAUSES
)
1392 logf("Too many clauses");
1396 if (clause
->type
!= clause_logical_or
)
1398 /* Check if there is already a similar filter in present (filters are
1399 * much faster than clauses).
1401 for (i
= 0; i
< tcs
->filter_count
; i
++)
1403 if (tcs
->filter_tag
[i
] == clause
->tag
)
1407 if (!TAGCACHE_IS_NUMERIC(clause
->tag
) && tcs
->idxfd
[clause
->tag
] < 0)
1411 snprintf(buf
, sizeof buf
, TAGCACHE_FILE_INDEX
, clause
->tag
);
1412 tcs
->idxfd
[clause
->tag
] = open(buf
, O_RDONLY
);
1416 tcs
->clause
[tcs
->clause_count
] = clause
;
1417 tcs
->clause_count
++;
1422 static bool get_next(struct tagcache_search
*tcs
)
1424 static char buf
[TAG_MAXLEN
+32];
1425 struct tagfile_entry entry
;
1426 #if defined(HAVE_TC_RAMCACHE) && defined(HAVE_DIRCACHE)
1430 if (!tcs
->valid
|| !tc_stat
.ready
)
1433 if (tcs
->idxfd
[tcs
->type
] < 0 && !TAGCACHE_IS_NUMERIC(tcs
->type
)
1434 #ifdef HAVE_TC_RAMCACHE
1440 /* Relative fetch. */
1441 if (tcs
->filter_count
> 0 || tcs
->clause_count
> 0
1442 || TAGCACHE_IS_NUMERIC(tcs
->type
)
1443 #if defined(HAVE_TC_RAMCACHE) && defined(HAVE_DIRCACHE)
1444 /* We need to retrieve flag status for dircache. */
1445 || (tcs
->ramsearch
&& tcs
->type
== tag_filename
)
1449 struct tagcache_seeklist_entry
*seeklist
;
1451 /* Check for end of list. */
1452 if (tcs
->list_position
== tcs
->seek_list_count
)
1454 tcs
->list_position
= 0;
1456 /* Try to fetch more. */
1457 if (!build_lookup_list(tcs
))
1464 seeklist
= &tcs
->seeklist
[tcs
->list_position
];
1465 #if defined(HAVE_TC_RAMCACHE) && defined(HAVE_DIRCACHE)
1466 flag
= seeklist
->flag
;
1468 tcs
->position
= seeklist
->seek
;
1469 tcs
->idx_id
= seeklist
->idx_id
;
1470 tcs
->list_position
++;
1474 if (tcs
->entry_count
== 0)
1483 tcs
->result_seek
= tcs
->position
;
1485 if (TAGCACHE_IS_NUMERIC(tcs
->type
))
1487 snprintf(buf
, sizeof(buf
), "%ld", tcs
->position
);
1489 tcs
->result_len
= strlen(buf
) + 1;
1494 #ifdef HAVE_TC_RAMCACHE
1498 #if defined(HAVE_TC_RAMCACHE) && defined(HAVE_DIRCACHE)
1499 if (tcs
->type
== tag_filename
&& (flag
& FLAG_DIRCACHE
)
1500 && is_dircache_intact())
1502 size_t len
= dircache_copy_path(tcs
->position
, buf
, sizeof buf
);
1503 tcs
->result_len
= len
+ 1;
1505 tcs
->ramresult
= false;
1511 if (tcs
->type
!= tag_filename
)
1513 struct tagfile_entry
*ep
;
1515 ep
= (struct tagfile_entry
*)&ramcache_hdr
->tags
[tcs
->type
][tcs
->position
];
1516 tcs
->result
= ep
->tag_data
;
1517 tcs
->result_len
= strlen(tcs
->result
) + 1;
1518 tcs
->idx_id
= ep
->idx_id
;
1519 tcs
->ramresult
= true;
1521 /* Increase position for the next run. This may get overwritten. */
1522 tcs
->position
+= sizeof(struct tagfile_entry
) + ep
->tag_length
;
1529 if (!open_files(tcs
, tcs
->type
))
1535 /* Seek stream to the correct position and continue to direct fetch. */
1536 lseek(tcs
->idxfd
[tcs
->type
], tcs
->position
, SEEK_SET
);
1538 if (ecread_tagfile_entry(tcs
->idxfd
[tcs
->type
], &entry
) != sizeof(struct tagfile_entry
))
1540 logf("read error #5");
1545 if (entry
.tag_length
> (long)sizeof(buf
))
1548 logf("too long tag #2");
1549 logf("P:%lX/%lX", tcs
->position
, entry
.tag_length
);
1553 if (read(tcs
->idxfd
[tcs
->type
], buf
, entry
.tag_length
) != entry
.tag_length
)
1556 logf("read error #4");
1561 Update the position for the next read (this may be overridden
1562 if filters or clauses are being used).
1564 tcs
->position
+= sizeof(struct tagfile_entry
) + entry
.tag_length
;
1566 tcs
->result_len
= strlen(tcs
->result
) + 1;
1567 tcs
->idx_id
= entry
.idx_id
;
1568 tcs
->ramresult
= false;
1573 bool tagcache_get_next(struct tagcache_search
*tcs
)
1575 while (get_next(tcs
))
1577 if (tcs
->result_len
> 1)
1584 bool tagcache_retrieve(struct tagcache_search
*tcs
, int idxid
,
1585 int tag
, char *buf
, long size
)
1587 struct index_entry idx
;
1590 if (!get_index(tcs
->masterfd
, idxid
, &idx
, true))
1593 return retrieve(tcs
, &idx
, tag
, buf
, size
);
1596 static bool update_master_header(void)
1598 struct master_header myhdr
;
1604 if ( (fd
= open_master_fd(&myhdr
, true)) < 0)
1607 myhdr
.serial
= current_tcmh
.serial
;
1608 myhdr
.commitid
= current_tcmh
.commitid
;
1609 myhdr
.dirty
= current_tcmh
.dirty
;
1612 lseek(fd
, 0, SEEK_SET
);
1613 ecwrite(fd
, &myhdr
, 1, master_header_ec
, tc_stat
.econ
);
1619 void tagcache_search_finish(struct tagcache_search
*tcs
)
1623 if (!tcs
->initialized
)
1626 if (tcs
->masterfd
>= 0)
1628 close(tcs
->masterfd
);
1632 for (i
= 0; i
< TAG_COUNT
; i
++)
1634 if (tcs
->idxfd
[i
] >= 0)
1636 close(tcs
->idxfd
[i
]);
1641 tcs
->ramsearch
= false;
1643 tcs
->initialized
= 0;
1648 #if defined(HAVE_TC_RAMCACHE) && defined(HAVE_DIRCACHE)
1649 static struct tagfile_entry
*get_tag(const struct index_entry
*entry
, int tag
)
1651 return (struct tagfile_entry
*)&ramcache_hdr
->tags
[tag
][entry
->tag_seek
[tag
]];
1654 static long get_tag_numeric(const struct index_entry
*entry
, int tag
, int idx_id
)
1656 return check_virtual_tags(tag
, idx_id
, entry
);
1659 static char* get_tag_string(const struct index_entry
*entry
, int tag
)
1661 char* s
= get_tag(entry
, tag
)->tag_data
;
1662 return strcmp(s
, UNTAGGED
) ? s
: NULL
;
1665 bool tagcache_fill_tags(struct mp3entry
*id3
, const char *filename
)
1667 struct index_entry
*entry
;
1670 if (!tc_stat
.ready
|| !tc_stat
.ramcache
)
1673 /* Find the corresponding entry in tagcache. */
1674 idx_id
= find_entry_ram(filename
, -1);
1678 entry
= &ramcache_hdr
->indices
[idx_id
];
1680 memset(id3
, 0, sizeof(struct mp3entry
));
1682 id3
->title
= get_tag_string(entry
, tag_title
);
1683 id3
->artist
= get_tag_string(entry
, tag_artist
);
1684 id3
->album
= get_tag_string(entry
, tag_album
);
1685 id3
->genre_string
= get_tag_string(entry
, tag_genre
);
1686 id3
->composer
= get_tag_string(entry
, tag_composer
);
1687 id3
->comment
= get_tag_string(entry
, tag_comment
);
1688 id3
->albumartist
= get_tag_string(entry
, tag_albumartist
);
1689 id3
->grouping
= get_tag_string(entry
, tag_grouping
);
1691 id3
->length
= get_tag_numeric(entry
, tag_length
, idx_id
);
1692 id3
->playcount
= get_tag_numeric(entry
, tag_playcount
, idx_id
);
1693 id3
->rating
= get_tag_numeric(entry
, tag_rating
, idx_id
);
1694 id3
->lastplayed
= get_tag_numeric(entry
, tag_lastplayed
, idx_id
);
1695 id3
->score
= get_tag_numeric(entry
, tag_virt_autoscore
, idx_id
) / 10;
1696 id3
->year
= get_tag_numeric(entry
, tag_year
, idx_id
);
1698 id3
->discnum
= get_tag_numeric(entry
, tag_discnumber
, idx_id
);
1699 id3
->tracknum
= get_tag_numeric(entry
, tag_tracknumber
, idx_id
);
1700 id3
->bitrate
= get_tag_numeric(entry
, tag_bitrate
, idx_id
);
1701 if (id3
->bitrate
== 0)
1704 #if CONFIG_CODEC == SWCODEC
1705 if (global_settings
.autoresume_enable
)
1707 id3
->offset
= get_tag_numeric(entry
, tag_lastoffset
, idx_id
);
1708 logf("tagcache_fill_tags: Set offset for %s to %lX\n",
1709 id3
->title
, id3
->offset
);
1717 static inline void write_item(const char *item
)
1719 int len
= strlen(item
) + 1;
1722 write(cachefd
, item
, len
);
1725 static int check_if_empty(char **tag
)
1729 if (*tag
== NULL
|| **tag
== '\0')
1732 return sizeof(UNTAGGED
); /* Tag length */
1735 length
= strlen(*tag
);
1736 if (length
> TAG_MAXLEN
)
1738 logf("over length tag: %s", *tag
);
1739 length
= TAG_MAXLEN
;
1740 (*tag
)[length
] = '\0';
1746 #define ADD_TAG(entry,tag,data) \
1748 entry.tag_offset[tag] = offset; \
1749 entry.tag_length[tag] = check_if_empty(data); \
1750 offset += entry.tag_length[tag]
1751 /* GCC 3.4.6 for Coldfire can choose to inline this function. Not a good
1752 * idea, as it uses lots of stack and is called from a recursive function
1755 static void __attribute__ ((noinline
)) add_tagcache(char *path
,
1757 #if defined(HAVE_TC_RAMCACHE) && defined(HAVE_DIRCACHE)
1762 struct mp3entry id3
;
1763 struct temp_file_entry entry
;
1767 char tracknumfix
[3];
1769 int path_length
= strlen(path
);
1770 bool has_albumartist
;
1774 /* Crude logging for the sim - to aid in debugging */
1775 int logfd
= open(ROCKBOX_DIR
"/database.log",
1776 O_WRONLY
| O_APPEND
| O_CREAT
, 0666);
1778 write(logfd
, path
, strlen(path
));
1779 write(logfd
, "\n", 1);
1787 /* Check for overlength file path. */
1788 if (path_length
> TAG_MAXLEN
)
1790 /* Path can't be shortened. */
1791 logf("Too long path: %s", path
);
1795 /* Check if the file is supported. */
1796 if (probe_file_format(path
) == AFMT_UNKNOWN
)
1799 /* Check if the file is already cached. */
1800 #if defined(HAVE_TC_RAMCACHE) && defined(HAVE_DIRCACHE)
1801 idx_id
= find_entry_ram(path
, dc
);
1804 /* Be sure the entry doesn't exist. */
1805 if (filenametag_fd
>= 0 && idx_id
< 0)
1806 idx_id
= find_entry_disk(path
, false);
1808 /* Check if file has been modified. */
1811 struct index_entry idx
;
1813 /* TODO: Mark that the index exists (for fast reverse scan) */
1814 //found_idx[idx_id/8] |= idx_id%8;
1816 if (!get_index(-1, idx_id
, &idx
, true))
1818 logf("failed to retrieve index entry");
1822 if ((unsigned long)idx
.tag_seek
[tag_mtime
] == mtime
)
1824 /* No changes to file. */
1828 /* Metadata might have been changed. Delete the entry. */
1829 logf("Re-adding: %s", path
);
1830 if (!delete_entry(idx_id
))
1832 logf("delete_entry failed: %d", idx_id
);
1837 fd
= open(path
, O_RDONLY
);
1840 logf("open fail: %s", path
);
1844 memset(&id3
, 0, sizeof(struct mp3entry
));
1845 memset(&entry
, 0, sizeof(struct temp_file_entry
));
1846 memset(&tracknumfix
, 0, sizeof(tracknumfix
));
1847 ret
= get_metadata(&id3
, fd
, path
);
1853 logf("-> %s", path
);
1855 if (id3
.tracknum
<= 0) /* Track number missing? */
1861 entry
.tag_offset
[tag_year
] = id3
.year
;
1862 entry
.tag_offset
[tag_discnumber
] = id3
.discnum
;
1863 entry
.tag_offset
[tag_tracknumber
] = id3
.tracknum
;
1864 entry
.tag_offset
[tag_length
] = id3
.length
;
1865 entry
.tag_offset
[tag_bitrate
] = id3
.bitrate
;
1866 entry
.tag_offset
[tag_mtime
] = mtime
;
1869 has_albumartist
= id3
.albumartist
!= NULL
1870 && strlen(id3
.albumartist
) > 0;
1871 has_grouping
= id3
.grouping
!= NULL
1872 && strlen(id3
.grouping
) > 0;
1874 ADD_TAG(entry
, tag_filename
, &path
);
1875 ADD_TAG(entry
, tag_title
, &id3
.title
);
1876 ADD_TAG(entry
, tag_artist
, &id3
.artist
);
1877 ADD_TAG(entry
, tag_album
, &id3
.album
);
1878 ADD_TAG(entry
, tag_genre
, &id3
.genre_string
);
1879 ADD_TAG(entry
, tag_composer
, &id3
.composer
);
1880 ADD_TAG(entry
, tag_comment
, &id3
.comment
);
1881 if (has_albumartist
)
1883 ADD_TAG(entry
, tag_albumartist
, &id3
.albumartist
);
1887 ADD_TAG(entry
, tag_albumartist
, &id3
.artist
);
1891 ADD_TAG(entry
, tag_grouping
, &id3
.grouping
);
1895 ADD_TAG(entry
, tag_grouping
, &id3
.title
);
1897 entry
.data_length
= offset
;
1899 /* Write the header */
1900 write(cachefd
, &entry
, sizeof(struct temp_file_entry
));
1902 /* And tags also... Correct order is critical */
1904 write_item(id3
.title
);
1905 write_item(id3
.artist
);
1906 write_item(id3
.album
);
1907 write_item(id3
.genre_string
);
1908 write_item(id3
.composer
);
1909 write_item(id3
.comment
);
1910 if (has_albumartist
)
1912 write_item(id3
.albumartist
);
1916 write_item(id3
.artist
);
1920 write_item(id3
.grouping
);
1924 write_item(id3
.title
);
1926 total_entry_count
++;
1929 static bool tempbuf_insert(char *str
, int id
, int idx_id
, bool unique
)
1931 struct tempbuf_searchidx
*index
= (struct tempbuf_searchidx
*)tempbuf
;
1932 int len
= strlen(str
)+1;
1935 unsigned *crcbuf
= (unsigned *)&tempbuf
[tempbuf_size
-4];
1936 char buf
[TAG_MAXLEN
+32];
1938 for (i
= 0; str
[i
] != '\0' && i
< (int)sizeof(buf
)-1; i
++)
1939 buf
[i
] = tolower(str
[i
]);
1942 crc32
= crc_32(buf
, i
, 0xffffffff);
1946 /* Check if the crc does not exist -> entry does not exist for sure. */
1947 for (i
= 0; i
< tempbufidx
; i
++)
1949 if (crcbuf
[-i
] != crc32
)
1952 if (!strcasecmp(str
, index
[i
].str
))
1954 if (id
< 0 || id
>= lookup_buffer_depth
)
1956 logf("lookup buf overf.: %d", id
);
1960 lookup
[id
] = &index
[i
];
1966 /* Insert to CRC buffer. */
1967 crcbuf
[-tempbufidx
] = crc32
;
1970 /* Insert it to the buffer. */
1971 tempbuf_left
-= len
;
1972 if (tempbuf_left
- 4 < 0 || tempbufidx
>= commit_entry_count
-1)
1975 if (id
>= lookup_buffer_depth
)
1977 logf("lookup buf overf. #2: %d", id
);
1983 lookup
[id
] = &index
[tempbufidx
];
1984 index
[tempbufidx
].idlist
.id
= id
;
1987 index
[tempbufidx
].idlist
.id
= -1;
1989 index
[tempbufidx
].idlist
.next
= NULL
;
1990 index
[tempbufidx
].idx_id
= idx_id
;
1991 index
[tempbufidx
].seek
= -1;
1992 index
[tempbufidx
].str
= &tempbuf
[tempbuf_pos
];
1993 memcpy(index
[tempbufidx
].str
, str
, len
);
2000 static int compare(const void *p1
, const void *p2
)
2004 struct tempbuf_searchidx
*e1
= (struct tempbuf_searchidx
*)p1
;
2005 struct tempbuf_searchidx
*e2
= (struct tempbuf_searchidx
*)p2
;
2007 if (strcmp(e1
->str
, UNTAGGED
) == 0)
2009 if (strcmp(e2
->str
, UNTAGGED
) == 0)
2013 else if (strcmp(e2
->str
, UNTAGGED
) == 0)
2016 return strncasecmp(e1
->str
, e2
->str
, TAG_MAXLEN
);
2019 static int tempbuf_sort(int fd
)
2021 struct tempbuf_searchidx
*index
= (struct tempbuf_searchidx
*)tempbuf
;
2022 struct tagfile_entry fe
;
2026 /* Generate reverse lookup entries. */
2027 for (i
= 0; i
< lookup_buffer_depth
; i
++)
2029 struct tempbuf_id_list
*idlist
;
2034 if (lookup
[i
]->idlist
.id
== i
)
2037 idlist
= &lookup
[i
]->idlist
;
2038 while (idlist
->next
!= NULL
)
2039 idlist
= idlist
->next
;
2041 tempbuf_left
-= sizeof(struct tempbuf_id_list
);
2042 if (tempbuf_left
- 4 < 0)
2045 idlist
->next
= (struct tempbuf_id_list
*)&tempbuf
[tempbuf_pos
];
2046 if (tempbuf_pos
& 0x03)
2048 tempbuf_pos
= (tempbuf_pos
& ~0x03) + 0x04;
2050 idlist
->next
= (struct tempbuf_id_list
*)&tempbuf
[tempbuf_pos
];
2052 tempbuf_pos
+= sizeof(struct tempbuf_id_list
);
2054 idlist
= idlist
->next
;
2056 idlist
->next
= NULL
;
2061 qsort(index
, tempbufidx
, sizeof(struct tempbuf_searchidx
), compare
);
2062 memset(lookup
, 0, lookup_buffer_depth
* sizeof(struct tempbuf_searchidx
**));
2064 for (i
= 0; i
< tempbufidx
; i
++)
2066 struct tempbuf_id_list
*idlist
= &index
[i
].idlist
;
2068 /* Fix the lookup list. */
2069 while (idlist
!= NULL
)
2071 if (idlist
->id
>= 0)
2072 lookup
[idlist
->id
] = &index
[i
];
2073 idlist
= idlist
->next
;
2076 index
[i
].seek
= lseek(fd
, 0, SEEK_CUR
);
2077 length
= strlen(index
[i
].str
) + 1;
2078 fe
.tag_length
= length
;
2079 fe
.idx_id
= index
[i
].idx_id
;
2081 /* Check the chunk alignment. */
2082 if ((fe
.tag_length
+ sizeof(struct tagfile_entry
))
2083 % TAGFILE_ENTRY_CHUNK_LENGTH
)
2085 fe
.tag_length
+= TAGFILE_ENTRY_CHUNK_LENGTH
-
2086 ((fe
.tag_length
+ sizeof(struct tagfile_entry
))
2087 % TAGFILE_ENTRY_CHUNK_LENGTH
);
2090 #ifdef TAGCACHE_STRICT_ALIGN
2091 /* Make sure the entry is long aligned. */
2092 if (index
[i
].seek
& 0x03)
2094 logf("tempbuf_sort: alignment error!");
2099 if (ecwrite(fd
, &fe
, 1, tagfile_entry_ec
, tc_stat
.econ
) !=
2100 sizeof(struct tagfile_entry
))
2102 logf("tempbuf_sort: write error #1");
2106 if (write(fd
, index
[i
].str
, length
) != length
)
2108 logf("tempbuf_sort: write error #2");
2112 /* Write some padding. */
2113 if (fe
.tag_length
- length
> 0)
2114 write(fd
, "XXXXXXXX", fe
.tag_length
- length
);
2120 inline static struct tempbuf_searchidx
* tempbuf_locate(int id
)
2122 if (id
< 0 || id
>= lookup_buffer_depth
)
2129 inline static int tempbuf_find_location(int id
)
2131 struct tempbuf_searchidx
*entry
;
2133 entry
= tempbuf_locate(id
);
2140 static bool build_numeric_indices(struct tagcache_header
*h
, int tmpfd
)
2142 struct master_header tcmh
;
2143 struct index_entry idx
;
2146 struct temp_file_entry
*entrybuf
= (struct temp_file_entry
*)tempbuf
;
2148 int entries_processed
= 0;
2150 char buf
[TAG_MAXLEN
];
2152 max_entries
= tempbuf_size
/ sizeof(struct temp_file_entry
) - 1;
2154 logf("Building numeric indices...");
2155 lseek(tmpfd
, sizeof(struct tagcache_header
), SEEK_SET
);
2157 if ( (masterfd
= open_master_fd(&tcmh
, true)) < 0)
2160 masterfd_pos
= lseek(masterfd
, tcmh
.tch
.entry_count
* sizeof(struct index_entry
),
2162 if (masterfd_pos
== filesize(masterfd
))
2164 logf("we can't append!");
2169 while (entries_processed
< h
->entry_count
)
2171 int count
= MIN(h
->entry_count
- entries_processed
, max_entries
);
2173 /* Read in as many entries as possible. */
2174 for (i
= 0; i
< count
; i
++)
2176 struct temp_file_entry
*tfe
= &entrybuf
[i
];
2179 /* Read in numeric data. */
2180 if (read(tmpfd
, tfe
, sizeof(struct temp_file_entry
)) !=
2181 sizeof(struct temp_file_entry
))
2183 logf("read fail #1");
2188 datastart
= lseek(tmpfd
, 0, SEEK_CUR
);
2191 * Read string data from the following tags:
2197 * A crc32 hash is calculated from the read data
2198 * and stored back to the data offset field kept in memory.
2200 #define tmpdb_read_string_tag(tag) \
2201 lseek(tmpfd, tfe->tag_offset[tag], SEEK_CUR); \
2202 if ((unsigned long)tfe->tag_length[tag] > sizeof buf) \
2204 logf("read fail: buffer overflow"); \
2209 if (read(tmpfd, buf, tfe->tag_length[tag]) != \
2210 tfe->tag_length[tag]) \
2212 logf("read fail #2"); \
2217 tfe->tag_offset[tag] = crc_32(buf, strlen(buf), 0xffffffff); \
2218 lseek(tmpfd, datastart, SEEK_SET)
2220 tmpdb_read_string_tag(tag_filename
);
2221 tmpdb_read_string_tag(tag_artist
);
2222 tmpdb_read_string_tag(tag_album
);
2223 tmpdb_read_string_tag(tag_title
);
2225 /* Seek to the end of the string data. */
2226 lseek(tmpfd
, tfe
->data_length
, SEEK_CUR
);
2229 /* Backup the master index position. */
2230 masterfd_pos
= lseek(masterfd
, 0, SEEK_CUR
);
2231 lseek(masterfd
, sizeof(struct master_header
), SEEK_SET
);
2233 /* Check if we can resurrect some deleted runtime statistics data. */
2234 for (i
= 0; i
< tcmh
.tch
.entry_count
; i
++)
2236 /* Read the index entry. */
2237 if (ecread_index_entry(masterfd
, &idx
)
2238 != sizeof(struct index_entry
))
2240 logf("read fail #3");
2246 * Skip unless the entry is marked as being deleted
2247 * or the data has already been resurrected.
2249 if (!(idx
.flag
& FLAG_DELETED
) || idx
.flag
& FLAG_RESURRECTED
)
2252 /* Now try to match the entry. */
2254 * To succesfully match a song, the following conditions
2257 * For numeric fields: tag_length
2258 * - Full identical match is required
2260 * If tag_filename matches, no further checking necessary.
2262 * For string hashes: tag_artist, tag_album, tag_title
2263 * - All three of these must match
2265 for (j
= 0; j
< count
; j
++)
2267 struct temp_file_entry
*tfe
= &entrybuf
[j
];
2269 /* Try to match numeric fields first. */
2270 if (tfe
->tag_offset
[tag_length
] != idx
.tag_seek
[tag_length
])
2273 /* Now it's time to do the hash matching. */
2274 if (tfe
->tag_offset
[tag_filename
] != idx
.tag_seek
[tag_filename
])
2276 int match_count
= 0;
2278 /* No filename match, check if we can match two other tags. */
2279 #define tmpdb_match(tag) \
2280 if (tfe->tag_offset[tag] == idx.tag_seek[tag]) \
2283 tmpdb_match(tag_artist
);
2284 tmpdb_match(tag_album
);
2285 tmpdb_match(tag_title
);
2287 if (match_count
< 3)
2289 /* Still no match found, give up. */
2294 /* A match found, now copy & resurrect the statistical data. */
2295 #define tmpdb_copy_tag(tag) \
2296 tfe->tag_offset[tag] = idx.tag_seek[tag]
2298 tmpdb_copy_tag(tag_playcount
);
2299 tmpdb_copy_tag(tag_rating
);
2300 tmpdb_copy_tag(tag_playtime
);
2301 tmpdb_copy_tag(tag_lastplayed
);
2302 tmpdb_copy_tag(tag_commitid
);
2303 tmpdb_copy_tag(tag_lastoffset
);
2305 /* Avoid processing this entry again. */
2306 idx
.flag
|= FLAG_RESURRECTED
;
2308 lseek(masterfd
, -sizeof(struct index_entry
), SEEK_CUR
);
2309 if (ecwrite_index_entry(masterfd
, &idx
) != sizeof(struct index_entry
))
2311 logf("masterfd writeback fail #1");
2316 logf("Entry resurrected");
2321 /* Restore the master index position. */
2322 lseek(masterfd
, masterfd_pos
, SEEK_SET
);
2324 /* Commit the data to the index. */
2325 for (i
= 0; i
< count
; i
++)
2327 int loc
= lseek(masterfd
, 0, SEEK_CUR
);
2329 if (ecread_index_entry(masterfd
, &idx
) != sizeof(struct index_entry
))
2331 logf("read fail #3");
2336 for (j
= 0; j
< TAG_COUNT
; j
++)
2338 if (!TAGCACHE_IS_NUMERIC(j
))
2341 idx
.tag_seek
[j
] = entrybuf
[i
].tag_offset
[j
];
2343 idx
.flag
= entrybuf
[i
].flag
;
2345 if (idx
.tag_seek
[tag_commitid
])
2347 /* Data has been resurrected. */
2348 idx
.flag
|= FLAG_DIRTYNUM
;
2350 else if (tc_stat
.ready
&& current_tcmh
.commitid
> 0)
2352 idx
.tag_seek
[tag_commitid
] = current_tcmh
.commitid
;
2353 idx
.flag
|= FLAG_DIRTYNUM
;
2356 /* Write back the updated index. */
2357 lseek(masterfd
, loc
, SEEK_SET
);
2358 if (ecwrite_index_entry(masterfd
, &idx
) != sizeof(struct index_entry
))
2366 entries_processed
+= count
;
2367 logf("%d/%ld entries processed", entries_processed
, h
->entry_count
);
2378 * == 0 temporary failure
2381 static int build_index(int index_type
, struct tagcache_header
*h
, int tmpfd
)
2384 struct tagcache_header tch
;
2385 struct master_header tcmh
;
2386 struct index_entry idxbuf
[IDX_BUF_DEPTH
];
2388 char buf
[TAG_MAXLEN
+32];
2389 int fd
= -1, masterfd
;
2394 logf("Building index: %d", index_type
);
2396 /* Check the number of entries we need to allocate ram for. */
2397 commit_entry_count
= h
->entry_count
+ 1;
2399 masterfd
= open_master_fd(&tcmh
, false);
2402 commit_entry_count
+= tcmh
.tch
.entry_count
;
2406 remove_files(); /* Just to be sure we are clean. */
2408 /* Open the index file, which contains the tag names. */
2409 fd
= open_tag_fd(&tch
, index_type
, true);
2412 logf("tch.datasize=%ld", tch
.datasize
);
2413 lookup_buffer_depth
= 1 +
2414 /* First part */ commit_entry_count
+
2415 /* Second part */ (tch
.datasize
/ TAGFILE_ENTRY_CHUNK_LENGTH
);
2419 lookup_buffer_depth
= 1 +
2420 /* First part */ commit_entry_count
+
2421 /* Second part */ 0;
2424 logf("lookup_buffer_depth=%ld", lookup_buffer_depth
);
2425 logf("commit_entry_count=%ld", commit_entry_count
);
2427 /* Allocate buffer for all index entries from both old and new
2430 tempbuf_pos
= commit_entry_count
* sizeof(struct tempbuf_searchidx
);
2432 /* Allocate lookup buffer. The first portion of commit_entry_count
2433 * contains the new tags in the temporary file and the second
2434 * part for locating entries already in the db.
2437 * +---------+---------------------------+
2438 * | index | position/ENTRY_CHUNK_SIZE | lookup buffer
2439 * +---------+---------------------------+
2441 * Old tags are inserted to a temporary buffer with position:
2442 * tempbuf_insert(position/ENTRY_CHUNK_SIZE, ...);
2443 * And new tags with index:
2444 * tempbuf_insert(idx, ...);
2446 * The buffer is sorted and written into tag file:
2447 * tempbuf_sort(...);
2448 * leaving master index locations messed up.
2450 * That is fixed using the lookup buffer for old tags:
2451 * new_seek = tempbuf_find_location(old_seek, ...);
2453 * new_seek = tempbuf_find_location(idx);
2455 lookup
= (struct tempbuf_searchidx
**)&tempbuf
[tempbuf_pos
];
2456 tempbuf_pos
+= lookup_buffer_depth
* sizeof(void **);
2457 memset(lookup
, 0, lookup_buffer_depth
* sizeof(void **));
2459 /* And calculate the remaining data space used mainly for storing
2460 * tag data (strings). */
2461 tempbuf_left
= tempbuf_size
- tempbuf_pos
- 8;
2462 if (tempbuf_left
- TAGFILE_ENTRY_AVG_LENGTH
* commit_entry_count
< 0)
2464 logf("Buffer way too small!");
2471 * If tag file contains unique tags (sorted index), we will load
2472 * it entirely into memory so we can resort it later for use with
2475 if (TAGCACHE_IS_SORTED(index_type
))
2477 logf("loading tags...");
2478 for (i
= 0; i
< tch
.entry_count
; i
++)
2480 struct tagfile_entry entry
;
2481 int loc
= lseek(fd
, 0, SEEK_CUR
);
2484 if (ecread_tagfile_entry(fd
, &entry
) != sizeof(struct tagfile_entry
))
2486 logf("read error #7");
2491 if (entry
.tag_length
>= (int)sizeof(buf
))
2493 logf("too long tag #3");
2498 if (read(fd
, buf
, entry
.tag_length
) != entry
.tag_length
)
2500 logf("read error #8");
2505 /* Skip deleted entries. */
2510 * Save the tag and tag id in the memory buffer. Tag id
2511 * is saved so we can later reindex the master lookup
2512 * table when the index gets resorted.
2514 ret
= tempbuf_insert(buf
, loc
/TAGFILE_ENTRY_CHUNK_LENGTH
2515 + commit_entry_count
, entry
.idx_id
,
2516 TAGCACHE_IS_UNIQUE(index_type
));
2527 tempbufidx
= tch
.entry_count
;
2532 * Creating new index file to store the tags. No need to preload
2533 * anything whether the index type is sorted or not.
2535 snprintf(buf
, sizeof buf
, TAGCACHE_FILE_INDEX
, index_type
);
2536 fd
= open(buf
, O_WRONLY
| O_CREAT
| O_TRUNC
, 0666);
2539 logf("%s open fail", buf
);
2543 tch
.magic
= TAGCACHE_MAGIC
;
2544 tch
.entry_count
= 0;
2547 if (ecwrite(fd
, &tch
, 1, tagcache_header_ec
, tc_stat
.econ
)
2548 != sizeof(struct tagcache_header
))
2550 logf("header write failed");
2556 /* Loading the tag lookup file as "master file". */
2557 logf("Loading index file");
2558 masterfd
= open(TAGCACHE_FILE_MASTER
, O_RDWR
);
2562 logf("Creating new DB");
2563 masterfd
= open(TAGCACHE_FILE_MASTER
, O_WRONLY
| O_CREAT
| O_TRUNC
, 0666);
2567 logf("Failure to create index file (%s)", TAGCACHE_FILE_MASTER
);
2572 /* Write the header (write real values later). */
2573 memset(&tcmh
, 0, sizeof(struct master_header
));
2575 tcmh
.tch
.entry_count
= 0;
2576 tcmh
.tch
.datasize
= 0;
2578 ecwrite(masterfd
, &tcmh
, 1, master_header_ec
, tc_stat
.econ
);
2580 masterfd_pos
= lseek(masterfd
, 0, SEEK_CUR
);
2585 * Master file already exists so we need to process the current
2590 if (ecread(masterfd
, &tcmh
, 1, master_header_ec
, tc_stat
.econ
) !=
2591 sizeof(struct master_header
) || tcmh
.tch
.magic
!= TAGCACHE_MAGIC
)
2593 logf("header error");
2600 * If we reach end of the master file, we need to expand it to
2601 * hold new tags. If the current index is not sorted, we can
2602 * simply append new data to end of the file.
2603 * However, if the index is sorted, we need to update all tag
2604 * pointers in the master file for the current index.
2606 masterfd_pos
= lseek(masterfd
, tcmh
.tch
.entry_count
* sizeof(struct index_entry
),
2608 if (masterfd_pos
== filesize(masterfd
))
2610 logf("appending...");
2616 * Load new unique tags in memory to be sorted later and added
2617 * to the master lookup file.
2619 if (TAGCACHE_IS_SORTED(index_type
))
2621 lseek(tmpfd
, sizeof(struct tagcache_header
), SEEK_SET
);
2622 /* h is the header of the temporary file containing new tags. */
2623 logf("inserting new tags...");
2624 for (i
= 0; i
< h
->entry_count
; i
++)
2626 struct temp_file_entry entry
;
2628 if (read(tmpfd
, &entry
, sizeof(struct temp_file_entry
)) !=
2629 sizeof(struct temp_file_entry
))
2631 logf("read fail #3");
2637 if (entry
.tag_length
[index_type
] >= (long)sizeof(buf
))
2639 logf("too long entry!");
2644 lseek(tmpfd
, entry
.tag_offset
[index_type
], SEEK_CUR
);
2645 if (read(tmpfd
, buf
, entry
.tag_length
[index_type
]) !=
2646 entry
.tag_length
[index_type
])
2648 logf("read fail #4");
2653 if (TAGCACHE_IS_UNIQUE(index_type
))
2654 error
= !tempbuf_insert(buf
, i
, -1, true);
2656 error
= !tempbuf_insert(buf
, i
, tcmh
.tch
.entry_count
+ i
, false);
2660 logf("insert error");
2665 lseek(tmpfd
, entry
.data_length
- entry
.tag_offset
[index_type
] -
2666 entry
.tag_length
[index_type
], SEEK_CUR
);
2671 /* Sort the buffer data and write it to the index file. */
2672 lseek(fd
, sizeof(struct tagcache_header
), SEEK_SET
);
2674 * We need to truncate the index file now. There can be junk left
2675 * at the end of file (however, we _should_ always follow the
2676 * entry_count and don't crash with that).
2678 ftruncate(fd
, lseek(fd
, 0, SEEK_CUR
));
2680 i
= tempbuf_sort(fd
);
2683 logf("sorted %d tags", i
);
2686 * Now update all indexes in the master lookup file.
2688 logf("updating indices...");
2689 lseek(masterfd
, sizeof(struct master_header
), SEEK_SET
);
2690 for (i
= 0; i
< tcmh
.tch
.entry_count
; i
+= idxbuf_pos
)
2693 int loc
= lseek(masterfd
, 0, SEEK_CUR
);
2695 idxbuf_pos
= MIN(tcmh
.tch
.entry_count
- i
, IDX_BUF_DEPTH
);
2697 if (ecread(masterfd
, idxbuf
, idxbuf_pos
, index_entry_ec
, tc_stat
.econ
)
2698 != (int)sizeof(struct index_entry
)*idxbuf_pos
)
2700 logf("read fail #5");
2704 lseek(masterfd
, loc
, SEEK_SET
);
2706 for (j
= 0; j
< idxbuf_pos
; j
++)
2708 if (idxbuf
[j
].flag
& FLAG_DELETED
)
2710 /* We can just ignore deleted entries. */
2711 // idxbuf[j].tag_seek[index_type] = 0;
2715 idxbuf
[j
].tag_seek
[index_type
] = tempbuf_find_location(
2716 idxbuf
[j
].tag_seek
[index_type
]/TAGFILE_ENTRY_CHUNK_LENGTH
2717 + commit_entry_count
);
2719 if (idxbuf
[j
].tag_seek
[index_type
] < 0)
2721 logf("update error: %ld/%d/%ld",
2722 idxbuf
[j
].flag
, i
+j
, tcmh
.tch
.entry_count
);
2730 /* Write back the updated index. */
2731 if (ecwrite(masterfd
, idxbuf
, idxbuf_pos
,
2732 index_entry_ec
, tc_stat
.econ
) !=
2733 (int)sizeof(struct index_entry
)*idxbuf_pos
)
2744 * Walk through the temporary file containing the new tags.
2746 // build_normal_index(h, tmpfd, masterfd, idx);
2747 logf("updating new indices...");
2748 lseek(masterfd
, masterfd_pos
, SEEK_SET
);
2749 lseek(tmpfd
, sizeof(struct tagcache_header
), SEEK_SET
);
2750 lseek(fd
, 0, SEEK_END
);
2751 for (i
= 0; i
< h
->entry_count
; i
+= idxbuf_pos
)
2755 idxbuf_pos
= MIN(h
->entry_count
- i
, IDX_BUF_DEPTH
);
2758 memset(idxbuf
, 0, sizeof(struct index_entry
)*IDX_BUF_DEPTH
);
2762 int loc
= lseek(masterfd
, 0, SEEK_CUR
);
2764 if (ecread(masterfd
, idxbuf
, idxbuf_pos
, index_entry_ec
, tc_stat
.econ
)
2765 != (int)sizeof(struct index_entry
)*idxbuf_pos
)
2767 logf("read fail #6");
2771 lseek(masterfd
, loc
, SEEK_SET
);
2774 /* Read entry headers. */
2775 for (j
= 0; j
< idxbuf_pos
; j
++)
2777 if (!TAGCACHE_IS_SORTED(index_type
))
2779 struct temp_file_entry entry
;
2780 struct tagfile_entry fe
;
2782 if (read(tmpfd
, &entry
, sizeof(struct temp_file_entry
)) !=
2783 sizeof(struct temp_file_entry
))
2785 logf("read fail #7");
2791 if (entry
.tag_length
[index_type
] >= (int)sizeof(buf
))
2793 logf("too long entry!");
2794 logf("length=%d", entry
.tag_length
[index_type
]);
2795 logf("pos=0x%02lx", lseek(tmpfd
, 0, SEEK_CUR
));
2800 lseek(tmpfd
, entry
.tag_offset
[index_type
], SEEK_CUR
);
2801 if (read(tmpfd
, buf
, entry
.tag_length
[index_type
]) !=
2802 entry
.tag_length
[index_type
])
2804 logf("read fail #8");
2805 logf("offset=0x%02lx", entry
.tag_offset
[index_type
]);
2806 logf("length=0x%02x", entry
.tag_length
[index_type
]);
2811 /* Write to index file. */
2812 idxbuf
[j
].tag_seek
[index_type
] = lseek(fd
, 0, SEEK_CUR
);
2813 fe
.tag_length
= entry
.tag_length
[index_type
];
2814 fe
.idx_id
= tcmh
.tch
.entry_count
+ i
+ j
;
2815 ecwrite(fd
, &fe
, 1, tagfile_entry_ec
, tc_stat
.econ
);
2816 write(fd
, buf
, fe
.tag_length
);
2820 lseek(tmpfd
, entry
.data_length
- entry
.tag_offset
[index_type
] -
2821 entry
.tag_length
[index_type
], SEEK_CUR
);
2825 /* Locate the correct entry from the sorted array. */
2826 idxbuf
[j
].tag_seek
[index_type
] = tempbuf_find_location(i
+ j
);
2827 if (idxbuf
[j
].tag_seek
[index_type
] < 0)
2829 logf("entry not found (%d)", j
);
2837 if (ecwrite(masterfd
, idxbuf
, idxbuf_pos
,
2838 index_entry_ec
, tc_stat
.econ
) !=
2839 (int)sizeof(struct index_entry
)*idxbuf_pos
)
2841 logf("tagcache: write fail #4");
2850 /* Finally write the header. */
2851 tch
.magic
= TAGCACHE_MAGIC
;
2852 tch
.entry_count
= tempbufidx
;
2853 tch
.datasize
= lseek(fd
, 0, SEEK_END
) - sizeof(struct tagcache_header
);
2854 lseek(fd
, 0, SEEK_SET
);
2855 ecwrite(fd
, &tch
, 1, tagcache_header_ec
, tc_stat
.econ
);
2857 if (index_type
!= tag_filename
)
2858 h
->datasize
+= tch
.datasize
;
2859 logf("s:%d/%ld/%ld", index_type
, tch
.datasize
, h
->datasize
);
2871 static bool commit(void)
2873 struct tagcache_header tch
;
2874 struct master_header tcmh
;
2878 #ifdef HAVE_DIRCACHE
2879 bool dircache_buffer_stolen
= false;
2881 bool local_allocation
= false;
2883 logf("committing tagcache");
2888 tmpfd
= open(TAGCACHE_FILE_TEMP
, O_RDONLY
);
2891 logf("nothing to commit");
2896 /* Load the header. */
2897 len
= sizeof(struct tagcache_header
);
2898 rc
= read(tmpfd
, &tch
, len
);
2900 if (tch
.magic
!= TAGCACHE_MAGIC
|| rc
!= len
)
2902 logf("incorrect tmpheader");
2904 remove(TAGCACHE_FILE_TEMP
);
2908 if (tch
.entry_count
== 0)
2910 logf("nothing to commit");
2912 remove(TAGCACHE_FILE_TEMP
);
2916 /* Fully initialize existing headers (if any) before going further. */
2917 tc_stat
.ready
= check_all_headers();
2919 #ifdef HAVE_EEPROM_SETTINGS
2920 remove(TAGCACHE_STATEFILE
);
2923 /* At first be sure to unload the ramcache! */
2924 #ifdef HAVE_TC_RAMCACHE
2925 tc_stat
.ramcache
= false;
2930 /* Try to steal every buffer we can :) */
2931 if (tempbuf_size
== 0)
2932 local_allocation
= true;
2934 #ifdef HAVE_DIRCACHE
2935 if (tempbuf_size
== 0)
2937 /* Try to steal the dircache buffer. */
2938 tempbuf
= dircache_steal_buffer(&tempbuf_size
);
2939 tempbuf_size
&= ~0x03;
2941 if (tempbuf_size
> 0)
2943 dircache_buffer_stolen
= true;
2948 #ifdef HAVE_TC_RAMCACHE
2949 if (tempbuf_size
== 0 && tc_stat
.ramcache_allocated
> 0)
2951 tempbuf
= (char *)(ramcache_hdr
+ 1);
2952 tempbuf_size
= tc_stat
.ramcache_allocated
- sizeof(struct ramcache_header
) - 128;
2953 tempbuf_size
&= ~0x03;
2957 /* And finally fail if there are no buffers available. */
2958 if (tempbuf_size
== 0)
2960 logf("delaying commit until next boot");
2961 tc_stat
.commit_delayed
= true;
2967 logf("commit %ld entries...", tch
.entry_count
);
2969 /* Mark DB dirty so it will stay disabled if commit fails. */
2970 current_tcmh
.dirty
= true;
2971 update_master_header();
2973 /* Now create the index files. */
2974 tc_stat
.commit_step
= 0;
2976 tc_stat
.commit_delayed
= false;
2978 for (i
= 0; i
< TAG_COUNT
; i
++)
2982 if (TAGCACHE_IS_NUMERIC(i
))
2985 tc_stat
.commit_step
++;
2986 ret
= build_index(i
, &tch
, tmpfd
);
2990 logf("tagcache failed init");
2992 tc_stat
.commit_delayed
= true;
2994 tc_stat
.commit_step
= 0;
3000 if (!build_numeric_indices(&tch
, tmpfd
))
3002 logf("Failure to commit numeric indices");
3004 tc_stat
.commit_step
= 0;
3011 tc_stat
.commit_step
= 0;
3013 /* Update the master index headers. */
3014 if ( (masterfd
= open_master_fd(&tcmh
, true)) < 0)
3020 remove(TAGCACHE_FILE_TEMP
);
3022 tcmh
.tch
.entry_count
+= tch
.entry_count
;
3023 tcmh
.tch
.datasize
= sizeof(struct master_header
)
3024 + sizeof(struct index_entry
) * tcmh
.tch
.entry_count
3029 lseek(masterfd
, 0, SEEK_SET
);
3030 ecwrite(masterfd
, &tcmh
, 1, master_header_ec
, tc_stat
.econ
);
3033 logf("tagcache committed");
3034 tc_stat
.ready
= check_all_headers();
3035 tc_stat
.readyvalid
= true;
3037 if (local_allocation
)
3043 #ifdef HAVE_DIRCACHE
3044 /* Rebuild the dircache, if we stole the buffer. */
3045 if (dircache_buffer_stolen
)
3049 #ifdef HAVE_TC_RAMCACHE
3050 /* Reload tagcache. */
3051 if (tc_stat
.ramcache_allocated
> 0)
3052 tagcache_start_scan();
3060 static void allocate_tempbuf(void)
3062 /* Yeah, malloc would be really nice now :) */
3064 tempbuf_size
= 32*1024*1024;
3065 tempbuf
= malloc(tempbuf_size
);
3067 tempbuf
= (char *)(((long)audiobuf
& ~0x03) + 0x04);
3068 tempbuf_size
= (long)audiobufend
- (long)audiobuf
- 4;
3069 audiobuf
+= tempbuf_size
;
3073 static void free_tempbuf(void)
3075 if (tempbuf_size
== 0)
3081 audiobuf
-= tempbuf_size
;
3089 static bool modify_numeric_entry(int masterfd
, int idx_id
, int tag
, long data
)
3091 struct index_entry idx
;
3096 if (!TAGCACHE_IS_NUMERIC(tag
))
3099 if (!get_index(masterfd
, idx_id
, &idx
, false))
3102 idx
.tag_seek
[tag
] = data
;
3103 idx
.flag
|= FLAG_DIRTYNUM
;
3105 return write_index(masterfd
, idx_id
, &idx
);
3109 bool tagcache_modify_numeric_entry(struct tagcache_search
*tcs
,
3112 struct master_header myhdr
;
3114 if (tcs
->masterfd
< 0)
3116 if ( (tcs
->masterfd
= open_master_fd(&myhdr
, true)) < 0)
3120 return modify_numeric_entry(tcs
->masterfd
, tcs
->idx_id
, tag
, data
);
3124 static bool command_queue_is_full(void)
3128 next
= command_queue_widx
+ 1;
3129 if (next
>= TAGCACHE_COMMAND_QUEUE_LENGTH
)
3132 return (next
== command_queue_ridx
);
3135 static void command_queue_sync_callback(void *data
)
3138 struct master_header myhdr
;
3141 mutex_lock(&command_queue_mutex
);
3143 if ( (masterfd
= open_master_fd(&myhdr
, true)) < 0)
3146 while (command_queue_ridx
!= command_queue_widx
)
3148 struct tagcache_command_entry
*ce
= &command_queue
[command_queue_ridx
];
3150 switch (ce
->command
)
3152 case CMD_UPDATE_MASTER_HEADER
:
3155 update_master_header();
3157 /* Re-open the masterfd. */
3158 if ( (masterfd
= open_master_fd(&myhdr
, true)) < 0)
3163 case CMD_UPDATE_NUMERIC
:
3165 modify_numeric_entry(masterfd
, ce
->idx_id
, ce
->tag
, ce
->data
);
3170 if (++command_queue_ridx
>= TAGCACHE_COMMAND_QUEUE_LENGTH
)
3171 command_queue_ridx
= 0;
3176 tc_stat
.queue_length
= 0;
3177 mutex_unlock(&command_queue_mutex
);
3180 static void run_command_queue(bool force
)
3182 if (COMMAND_QUEUE_IS_EMPTY
)
3185 if (force
|| command_queue_is_full())
3186 command_queue_sync_callback(NULL
);
3188 register_storage_idle_func(command_queue_sync_callback
);
3191 static void queue_command(int cmd
, long idx_id
, int tag
, long data
)
3197 mutex_lock(&command_queue_mutex
);
3198 next
= command_queue_widx
+ 1;
3199 if (next
>= TAGCACHE_COMMAND_QUEUE_LENGTH
)
3202 /* Make sure queue is not full. */
3203 if (next
!= command_queue_ridx
)
3205 struct tagcache_command_entry
*ce
= &command_queue
[command_queue_widx
];
3208 ce
->idx_id
= idx_id
;
3212 command_queue_widx
= next
;
3214 tc_stat
.queue_length
++;
3216 mutex_unlock(&command_queue_mutex
);
3220 /* Queue is full, try again later... */
3221 mutex_unlock(&command_queue_mutex
);
3226 long tagcache_increase_serial(void)
3236 old
= current_tcmh
.serial
++;
3237 queue_command(CMD_UPDATE_MASTER_HEADER
, 0, 0, 0);
3242 void tagcache_update_numeric(int idx_id
, int tag
, long data
)
3244 queue_command(CMD_UPDATE_NUMERIC
, idx_id
, tag
, data
);
3246 #endif /* !__PCTOOL__ */
3248 long tagcache_get_serial(void)
3250 return current_tcmh
.serial
;
3253 long tagcache_get_commitid(void)
3255 return current_tcmh
.commitid
;
3258 static bool write_tag(int fd
, const char *tagstr
, const char *datastr
)
3263 snprintf(buf
, sizeof buf
, "%s=\"", tagstr
);
3264 for (i
= strlen(buf
); i
< (long)sizeof(buf
)-4; i
++)
3266 if (*datastr
== '\0')
3269 if (*datastr
== '"' || *datastr
== '\\')
3272 else if (*datastr
== '\n')
3279 buf
[i
] = *(datastr
++);
3282 strcpy(&buf
[i
], "\" ");
3284 write(fd
, buf
, i
+ 2);
3291 static bool read_tag(char *dest
, long size
,
3292 const char *src
, const char *tagstr
)
3295 char current_tag
[32];
3297 while (*src
!= '\0')
3299 /* Skip all whitespace */
3307 /* Read in tag name */
3308 while (*src
!= '=' && *src
!= ' ')
3310 current_tag
[pos
] = *src
;
3314 if (*src
== '\0' || pos
>= (long)sizeof(current_tag
))
3317 current_tag
[pos
] = '\0';
3319 /* Read in tag data */
3321 /* Find the start. */
3322 while (*src
!= '"' && *src
!= '\0')
3325 if (*src
== '\0' || *(++src
) == '\0')
3328 /* Read the data. */
3329 for (pos
= 0; pos
< size
; pos
++)
3355 dest
[pos
] = *(src
++);
3360 if (!strcasecmp(tagstr
, current_tag
))
3367 static int parse_changelog_line(int line_n
, char *buf
, void *parameters
)
3369 struct index_entry idx
;
3370 char tag_data
[TAG_MAXLEN
+32];
3372 long masterfd
= (long)parameters
;
3373 const int import_tags
[] = { tag_playcount
, tag_rating
, tag_playtime
,
3374 tag_lastplayed
, tag_commitid
, tag_lastoffset
};
3381 /* logf("%d/%s", line_n, buf); */
3382 if (!read_tag(tag_data
, sizeof tag_data
, buf
, "filename"))
3384 logf("%d/filename missing", line_n
);
3389 idx_id
= find_index(tag_data
);
3392 logf("%d/entry not found", line_n
);
3396 if (!get_index(masterfd
, idx_id
, &idx
, false))
3398 logf("%d/failed to retrieve index entry", line_n
);
3402 /* Stop if tag has already been modified. */
3403 if (idx
.flag
& FLAG_DIRTYNUM
)
3406 logf("%d/import: %s", line_n
, tag_data
);
3408 idx
.flag
|= FLAG_DIRTYNUM
;
3409 for (i
= 0; i
< (long)(sizeof(import_tags
)/sizeof(import_tags
[0])); i
++)
3413 if (!read_tag(tag_data
, sizeof tag_data
, buf
,
3414 tagcache_tag_to_str(import_tags
[i
])))
3419 data
= atoi(tag_data
);
3423 idx
.tag_seek
[import_tags
[i
]] = data
;
3425 if (import_tags
[i
] == tag_lastplayed
&& data
>= current_tcmh
.serial
)
3426 current_tcmh
.serial
= data
+ 1;
3427 else if (import_tags
[i
] == tag_commitid
&& data
>= current_tcmh
.commitid
)
3428 current_tcmh
.commitid
= data
+ 1;
3431 return write_index(masterfd
, idx_id
, &idx
) ? 0 : -5;
3434 bool tagcache_import_changelog(void)
3436 struct master_header myhdr
;
3437 struct tagcache_header tch
;
3448 clfd
= open(TAGCACHE_FILE_CHANGELOG
, O_RDONLY
);
3451 logf("failure to open changelog");
3455 if ( (masterfd
= open_master_fd(&myhdr
, true)) < 0)
3463 filenametag_fd
= open_tag_fd(&tch
, tag_filename
, false);
3465 fast_readline(clfd
, buf
, sizeof buf
, (long *)masterfd
,
3466 parse_changelog_line
);
3471 if (filenametag_fd
>= 0)
3473 close(filenametag_fd
);
3474 filenametag_fd
= -1;
3479 update_master_header();
3484 #endif /* !__PCTOOL__ */
3486 bool tagcache_create_changelog(struct tagcache_search
*tcs
)
3488 struct master_header myhdr
;
3489 struct index_entry idx
;
3490 char buf
[TAG_MAXLEN
+32];
3498 if (!tagcache_search(tcs
, tag_filename
))
3501 /* Initialize the changelog */
3502 clfd
= open(TAGCACHE_FILE_CHANGELOG
, O_WRONLY
| O_CREAT
| O_TRUNC
, 0666);
3505 logf("failure to open changelog");
3509 if (tcs
->masterfd
< 0)
3511 if ( (tcs
->masterfd
= open_master_fd(&myhdr
, false)) < 0)
3516 lseek(tcs
->masterfd
, 0, SEEK_SET
);
3517 ecread(tcs
->masterfd
, &myhdr
, 1, master_header_ec
, tc_stat
.econ
);
3520 write(clfd
, "## Changelog version 1\n", 23);
3522 for (i
= 0; i
< myhdr
.tch
.entry_count
; i
++)
3524 if (ecread_index_entry(tcs
->masterfd
, &idx
) != sizeof(struct index_entry
))
3526 logf("read error #9");
3527 tagcache_search_finish(tcs
);
3532 /* Skip until the entry found has been modified. */
3533 if (! (idx
.flag
& FLAG_DIRTYNUM
) )
3536 /* Skip deleted entries too. */
3537 if (idx
.flag
& FLAG_DELETED
)
3540 /* Now retrieve all tags. */
3541 for (j
= 0; j
< TAG_COUNT
; j
++)
3543 if (TAGCACHE_IS_NUMERIC(j
))
3545 snprintf(temp
, sizeof temp
, "%d", (int)idx
.tag_seek
[j
]);
3546 write_tag(clfd
, tagcache_tag_to_str(j
), temp
);
3551 tagcache_retrieve(tcs
, i
, tcs
->type
, buf
, sizeof buf
);
3552 write_tag(clfd
, tagcache_tag_to_str(j
), buf
);
3555 write(clfd
, "\n", 1);
3561 tagcache_search_finish(tcs
);
3566 static bool delete_entry(long idx_id
)
3571 struct index_entry idx
, myidx
;
3572 struct master_header myhdr
;
3573 char buf
[TAG_MAXLEN
+32];
3574 int in_use
[TAG_COUNT
];
3576 logf("delete_entry(): %ld", idx_id
);
3578 #ifdef HAVE_TC_RAMCACHE
3579 /* At first mark the entry removed from ram cache. */
3580 if (tc_stat
.ramcache
)
3581 ramcache_hdr
->indices
[idx_id
].flag
|= FLAG_DELETED
;
3584 if ( (masterfd
= open_master_fd(&myhdr
, true) ) < 0)
3587 lseek(masterfd
, idx_id
* sizeof(struct index_entry
), SEEK_CUR
);
3588 if (ecread_index_entry(masterfd
, &myidx
) != sizeof(struct index_entry
))
3590 logf("delete_entry(): read error");
3594 if (myidx
.flag
& FLAG_DELETED
)
3596 logf("delete_entry(): already deleted!");
3600 myidx
.flag
|= FLAG_DELETED
;
3601 lseek(masterfd
, -sizeof(struct index_entry
), SEEK_CUR
);
3602 if (ecwrite_index_entry(masterfd
, &myidx
) != sizeof(struct index_entry
))
3604 logf("delete_entry(): write_error #1");
3608 /* Now check which tags are no longer in use (if any) */
3609 for (tag
= 0; tag
< TAG_COUNT
; tag
++)
3612 lseek(masterfd
, sizeof(struct master_header
), SEEK_SET
);
3613 for (i
= 0; i
< myhdr
.tch
.entry_count
; i
++)
3615 struct index_entry
*idxp
;
3617 #ifdef HAVE_TC_RAMCACHE
3618 /* Use RAM DB if available for greater speed */
3619 if (tc_stat
.ramcache
)
3620 idxp
= &ramcache_hdr
->indices
[i
];
3624 if (ecread_index_entry(masterfd
, &idx
) != sizeof(struct index_entry
))
3626 logf("delete_entry(): read error #2");
3632 if (idxp
->flag
& FLAG_DELETED
)
3635 for (tag
= 0; tag
< TAG_COUNT
; tag
++)
3637 if (TAGCACHE_IS_NUMERIC(tag
))
3640 if (idxp
->tag_seek
[tag
] == myidx
.tag_seek
[tag
])
3645 /* Now delete all tags no longer in use. */
3646 for (tag
= 0; tag
< TAG_COUNT
; tag
++)
3648 struct tagcache_header tch
;
3649 int oldseek
= myidx
.tag_seek
[tag
];
3651 if (TAGCACHE_IS_NUMERIC(tag
))
3655 * Replace tag seek with a hash value of the field string data.
3656 * That way runtime statistics of moved or altered files can be
3659 #ifdef HAVE_TC_RAMCACHE
3660 if (tc_stat
.ramcache
&& tag
!= tag_filename
)
3662 struct tagfile_entry
*tfe
;
3663 int32_t *seek
= &ramcache_hdr
->indices
[idx_id
].tag_seek
[tag
];
3665 tfe
= (struct tagfile_entry
*)&ramcache_hdr
->tags
[tag
][*seek
];
3666 *seek
= crc_32(tfe
->tag_data
, strlen(tfe
->tag_data
), 0xffffffff);
3667 myidx
.tag_seek
[tag
] = *seek
;
3672 struct tagfile_entry tfe
;
3674 /* Open the index file, which contains the tag names. */
3675 if ((fd
= open_tag_fd(&tch
, tag
, true)) < 0)
3678 /* Skip the header block */
3679 lseek(fd
, myidx
.tag_seek
[tag
], SEEK_SET
);
3680 if (ecread_tagfile_entry(fd
, &tfe
) != sizeof(struct tagfile_entry
))
3682 logf("delete_entry(): read error #3");
3686 if (read(fd
, buf
, tfe
.tag_length
) != tfe
.tag_length
)
3688 logf("delete_entry(): read error #4");
3692 myidx
.tag_seek
[tag
] = crc_32(buf
, strlen(buf
), 0xffffffff);
3697 logf("in use: %d/%d", tag
, in_use
[tag
]);
3706 #ifdef HAVE_TC_RAMCACHE
3707 /* Delete from ram. */
3708 if (tc_stat
.ramcache
&& tag
!= tag_filename
)
3710 struct tagfile_entry
*tagentry
=
3711 (struct tagfile_entry
*)&ramcache_hdr
->tags
[tag
][oldseek
];
3712 tagentry
->tag_data
[0] = '\0';
3716 /* Open the index file, which contains the tag names. */
3719 if ((fd
= open_tag_fd(&tch
, tag
, true)) < 0)
3723 /* Skip the header block */
3724 lseek(fd
, oldseek
+ sizeof(struct tagfile_entry
), SEEK_SET
);
3726 /* Debug, print 10 first characters of the tag
3729 logf("TAG:%s", buf);
3730 lseek(fd, -10, SEEK_CUR);
3733 /* Write first data byte in tag as \0 */
3736 /* Now tag data has been removed */
3741 /* Write index entry back into master index. */
3742 lseek(masterfd
, sizeof(struct master_header
) +
3743 (idx_id
* sizeof(struct index_entry
)), SEEK_SET
);
3744 if (ecwrite_index_entry(masterfd
, &myidx
) != sizeof(struct index_entry
))
3746 logf("delete_entry(): write_error #2");
3765 * Returns true if there is an event waiting in the queue
3766 * that requires the current operation to be aborted.
3768 static bool check_event_queue(void)
3770 struct queue_event ev
;
3772 if(!queue_peek(&tagcache_queue
, &ev
))
3779 case SYS_USB_CONNECTED
:
3787 #ifdef HAVE_TC_RAMCACHE
3788 static bool allocate_tagcache(void)
3790 struct master_header tcmh
;
3793 /* Load the header. */
3794 if ( (fd
= open_master_fd(&tcmh
, false)) < 0)
3796 ramcache_hdr
= NULL
;
3803 * Now calculate the required cache size plus
3804 * some extra space for alignment fixes.
3806 tc_stat
.ramcache_allocated
= tcmh
.tch
.datasize
+ 128 + TAGCACHE_RESERVE
+
3807 sizeof(struct ramcache_header
) + TAG_COUNT
*sizeof(void *);
3808 ramcache_hdr
= buffer_alloc(tc_stat
.ramcache_allocated
+ 128);
3809 memset(ramcache_hdr
, 0, sizeof(struct ramcache_header
));
3810 memcpy(¤t_tcmh
, &tcmh
, sizeof current_tcmh
);
3811 logf("tagcache: %d bytes allocated.", tc_stat
.ramcache_allocated
);
3816 # ifdef HAVE_EEPROM_SETTINGS
3817 static bool tagcache_dumpload(void)
3819 struct statefile_header shdr
;
3824 fd
= open(TAGCACHE_STATEFILE
, O_RDONLY
);
3827 logf("no tagcache statedump");
3831 /* Check the statefile memory placement */
3832 ramcache_hdr
= buffer_alloc(0);
3833 rc
= read(fd
, &shdr
, sizeof(struct statefile_header
));
3834 if (rc
!= sizeof(struct statefile_header
)
3835 || shdr
.magic
!= TAGCACHE_STATEFILE_MAGIC
3836 || shdr
.mh
.tch
.magic
!= TAGCACHE_MAGIC
)
3838 logf("incorrect statefile");
3839 ramcache_hdr
= NULL
;
3844 offpos
= (long)ramcache_hdr
- (long)shdr
.hdr
;
3846 /* Lets allocate real memory and load it */
3847 ramcache_hdr
= buffer_alloc(shdr
.tc_stat
.ramcache_allocated
);
3848 rc
= read(fd
, ramcache_hdr
, shdr
.tc_stat
.ramcache_allocated
);
3851 if (rc
!= shdr
.tc_stat
.ramcache_allocated
)
3853 logf("read failure!");
3854 ramcache_hdr
= NULL
;
3858 memcpy(&tc_stat
, &shdr
.tc_stat
, sizeof(struct tagcache_stat
));
3860 /* Now fix the pointers */
3861 for (i
= 0; i
< TAG_COUNT
; i
++)
3862 ramcache_hdr
->tags
[i
] += offpos
;
3864 /* Load the tagcache master header (should match the actual DB file header). */
3865 memcpy(¤t_tcmh
, &shdr
.mh
, sizeof current_tcmh
);
3870 static bool tagcache_dumpsave(void)
3872 struct statefile_header shdr
;
3875 if (!tc_stat
.ramcache
)
3878 fd
= open(TAGCACHE_STATEFILE
, O_WRONLY
| O_CREAT
| O_TRUNC
, 0666);
3881 logf("failed to create a statedump");
3885 /* Create the header */
3886 shdr
.magic
= TAGCACHE_STATEFILE_MAGIC
;
3887 shdr
.hdr
= ramcache_hdr
;
3888 memcpy(&shdr
.mh
, ¤t_tcmh
, sizeof current_tcmh
);
3889 memcpy(&shdr
.tc_stat
, &tc_stat
, sizeof tc_stat
);
3890 write(fd
, &shdr
, sizeof shdr
);
3892 /* And dump the data too */
3893 write(fd
, ramcache_hdr
, tc_stat
.ramcache_allocated
);
3900 static bool load_tagcache(void)
3902 struct tagcache_header
*tch
;
3903 struct master_header tcmh
;
3904 long bytesleft
= tc_stat
.ramcache_allocated
- sizeof(struct ramcache_header
);
3905 struct index_entry
*idx
;
3910 # ifdef HAVE_DIRCACHE
3911 while (dircache_is_initializing())
3914 dircache_set_appflag(DIRCACHE_APPFLAG_TAGCACHE
);
3917 logf("loading tagcache to ram...");
3919 fd
= open(TAGCACHE_FILE_MASTER
, O_RDONLY
);
3922 logf("tagcache open failed");
3926 if (ecread(fd
, &tcmh
, 1, master_header_ec
, tc_stat
.econ
)
3927 != sizeof(struct master_header
)
3928 || tcmh
.tch
.magic
!= TAGCACHE_MAGIC
)
3930 logf("incorrect header");
3934 /* Master header copy should already match, this can be redundant to do. */
3935 memcpy(¤t_tcmh
, &tcmh
, sizeof current_tcmh
);
3937 idx
= ramcache_hdr
->indices
;
3939 /* Load the master index table. */
3940 for (i
= 0; i
< tcmh
.tch
.entry_count
; i
++)
3942 bytesleft
-= sizeof(struct index_entry
);
3945 logf("too big tagcache.");
3950 /* DEBUG: After tagcache commit and dircache rebuild, hdr-sturcture
3951 * may become corrupt. */
3952 rc
= ecread_index_entry(fd
, idx
);
3953 if (rc
!= sizeof(struct index_entry
))
3955 logf("read error #10");
3965 /* Load the tags. */
3967 for (tag
= 0; tag
< TAG_COUNT
; tag
++)
3969 struct tagfile_entry
*fe
;
3970 char buf
[TAG_MAXLEN
+32];
3972 if (TAGCACHE_IS_NUMERIC(tag
))
3975 //p = ((void *)p+1);
3976 p
= (char *)((long)p
& ~0x03) + 0x04;
3977 ramcache_hdr
->tags
[tag
] = p
;
3979 /* Check the header. */
3980 tch
= (struct tagcache_header
*)p
;
3981 p
+= sizeof(struct tagcache_header
);
3983 if ( (fd
= open_tag_fd(tch
, tag
, false)) < 0)
3986 for (ramcache_hdr
->entry_count
[tag
] = 0;
3987 ramcache_hdr
->entry_count
[tag
] < tch
->entry_count
;
3988 ramcache_hdr
->entry_count
[tag
]++)
3992 if (do_timed_yield())
3994 /* Abort if we got a critical event in queue */
3995 if (check_event_queue())
3999 fe
= (struct tagfile_entry
*)p
;
4000 pos
= lseek(fd
, 0, SEEK_CUR
);
4001 rc
= ecread_tagfile_entry(fd
, fe
);
4002 if (rc
!= sizeof(struct tagfile_entry
))
4004 /* End of lookup table. */
4005 logf("read error #11");
4010 /* We have a special handling for the filename tags. */
4011 if (tag
== tag_filename
)
4013 # ifdef HAVE_DIRCACHE
4017 idx
= &ramcache_hdr
->indices
[fe
->idx_id
];
4019 if (fe
->tag_length
>= (long)sizeof(buf
)-1)
4023 logf("TAG:%s", buf
);
4024 logf("too long filename");
4029 rc
= read(fd
, buf
, fe
->tag_length
);
4030 if (rc
!= fe
->tag_length
)
4032 logf("read error #12");
4037 /* Check if the entry has already been removed */
4038 if (idx
->flag
& FLAG_DELETED
)
4041 /* This flag must not be used yet. */
4042 if (idx
->flag
& FLAG_DIRCACHE
)
4044 logf("internal error!");
4049 if (idx
->tag_seek
[tag
] != pos
)
4051 logf("corrupt data structures!");
4056 # ifdef HAVE_DIRCACHE
4057 if (dircache_is_enabled())
4059 dc
= dircache_get_entry_id(buf
);
4062 logf("Entry no longer valid.");
4064 if (global_settings
.tagcache_autoupdate
)
4065 delete_entry(fe
->idx_id
);
4069 idx
->flag
|= FLAG_DIRCACHE
;
4070 idx
->tag_seek
[tag_filename
] = dc
;
4075 /* This will be very slow unless dircache is enabled
4076 or target is flash based, but do it anyway for
4078 /* Check if entry has been removed. */
4079 if (global_settings
.tagcache_autoupdate
)
4081 if (!file_exists(buf
))
4083 logf("Entry no longer valid.");
4085 delete_entry(fe
->idx_id
);
4094 bytesleft
-= sizeof(struct tagfile_entry
) + fe
->tag_length
;
4097 logf("too big tagcache #2");
4098 logf("tl: %ld", fe
->tag_length
);
4099 logf("bl: %ld", bytesleft
);
4105 rc
= read(fd
, fe
->tag_data
, fe
->tag_length
);
4108 if (rc
!= fe
->tag_length
)
4110 logf("read error #13");
4111 logf("rc=0x%04x", rc
); // 0x431
4112 logf("len=0x%04lx", fe
->tag_length
); // 0x4000
4113 logf("pos=0x%04lx", lseek(fd
, 0, SEEK_CUR
)); // 0x433
4114 logf("tag=0x%02x", tag
); // 0x00
4122 tc_stat
.ramcache_used
= tc_stat
.ramcache_allocated
- bytesleft
;
4123 logf("tagcache loaded into ram!");
4127 #endif /* HAVE_TC_RAMCACHE */
4129 static bool check_deleted_files(void)
4132 char buf
[TAG_MAXLEN
+32];
4133 struct tagfile_entry tfe
;
4135 logf("reverse scan...");
4136 snprintf(buf
, sizeof buf
, TAGCACHE_FILE_INDEX
, tag_filename
);
4137 fd
= open(buf
, O_RDONLY
);
4141 logf("%s open fail", buf
);
4145 lseek(fd
, sizeof(struct tagcache_header
), SEEK_SET
);
4146 while (ecread_tagfile_entry(fd
, &tfe
) == sizeof(struct tagfile_entry
)
4148 && !check_event_queue()
4152 if (tfe
.tag_length
>= (long)sizeof(buf
)-1)
4154 logf("too long tag");
4159 if (read(fd
, buf
, tfe
.tag_length
) != tfe
.tag_length
)
4161 logf("read error #14");
4166 /* Check if the file has already deleted from the db. */
4170 /* Now check if the file exists. */
4171 if (!file_exists(buf
))
4173 logf("Entry no longer valid.");
4174 logf("-> %s / %ld", buf
, tfe
.tag_length
);
4175 delete_entry(tfe
.idx_id
);
4187 /* Note that this function must not be inlined, otherwise the whole point
4188 * of having the code in a separate function is lost.
4190 static void __attribute__ ((noinline
)) check_ignore(const char *dirname
,
4191 int *ignore
, int *unignore
)
4193 char newpath
[MAX_PATH
];
4195 /* check for a database.ignore file */
4196 snprintf(newpath
, MAX_PATH
, "%s/database.ignore", dirname
);
4197 *ignore
= file_exists(newpath
);
4198 /* check for a database.unignore file */
4199 snprintf(newpath
, MAX_PATH
, "%s/database.unignore", dirname
);
4200 *unignore
= file_exists(newpath
);
4203 static struct search_roots_ll
{
4205 struct search_roots_ll
* next
;
4210 * This adds a path to the search roots, possibly during traveling through
4211 * the filesystem. It only adds if the path is not inside an already existing
4214 * Returns true if it added the path to the search roots
4216 * Windows 2000 and greater supports symlinks, but they don't provide
4217 * realpath() or readlink(), and symlinks are rarely used on them so
4218 * ignore this for windows for now
4220 static bool add_search_root(const char *name
)
4224 struct search_roots_ll
*this, *prev
= NULL
;
4225 char target
[MAX_PATH
];
4226 /* Okay, realpath() is almost completely broken on android
4228 * It doesn't accept NULL for resolved_name to dynamically allocate
4229 * the resulting path; and it assumes resolved_name to be PATH_MAX
4230 * (actually MAXPATHLEN, but it's the same [as of 2.3]) long
4231 * and blindly writes to the end if it
4233 * therefore use sufficiently large static storage here
4234 * Note that PATH_MAX != MAX_PATH
4236 static char abs_target
[PATH_MAX
];
4239 len
= readlink(name
, target
, sizeof(target
));
4244 if (realpath(target
, abs_target
) == NULL
)
4247 for(this = &roots_ll
; this; prev
= this, this = this->next
)
4249 size_t root_len
= strlen(this->path
);
4250 /* check if the link target is inside of an existing search root
4251 * don't add if target is inside, we'll scan it later */
4252 if (!strncmp(this->path
, abs_target
, root_len
))
4258 size_t len
= strlen(abs_target
) + 1; /* count \0 */
4259 this = malloc(sizeof(struct search_roots_ll
) + len
);
4260 if (!this || len
> MAX_PATH
)
4262 logf("Error at adding a search root: %s", this ? "path too long":"OOM");
4267 this->path
= ((char*)this) + sizeof(struct search_roots_ll
);
4268 strcpy((char*)this->path
, abs_target
); /* ok to cast const away here */
4271 logf("Added %s to the search roots\n", abs_target
);
4278 static int free_search_roots(struct search_roots_ll
* start
)
4283 ret
+= free_search_roots(start
->next
);
4284 ret
+= sizeof(struct search_roots_ll
);
4289 #else /* native, simulator */
4290 #define add_search_root(a) do {} while(0)
4291 #define free_search_roots(a) do {} while(0)
4294 static bool check_dir(const char *dirname
, int add_files
)
4298 int success
= false;
4299 int ignore
, unignore
;
4301 dir
= opendir(dirname
);
4304 logf("tagcache: opendir(%s) failed", dirname
);
4307 /* check for a database.ignore and database.unignore */
4308 check_ignore(dirname
, &ignore
, &unignore
);
4310 /* don't do anything if both ignore and unignore are there */
4311 if (ignore
!= unignore
)
4312 add_files
= unignore
;
4314 /* Recursively scan the dir. */
4318 while (!check_event_queue())
4321 struct dirent
*entry
= readdir(dir
);
4328 if (!strcmp((char *)entry
->d_name
, ".") ||
4329 !strcmp((char *)entry
->d_name
, ".."))
4332 struct dirinfo info
= dir_get_info(dir
, entry
);
4336 len
= strlen(curpath
);
4337 /* don't add an extra / for curpath == / */
4338 if (len
<= 1) len
= 0;
4339 snprintf(&curpath
[len
], sizeof(curpath
) - len
, "/%s", entry
->d_name
);
4341 processed_dir_count
++;
4342 if (info
.attribute
& ATTR_DIRECTORY
)
4344 { /* don't follow symlinks to dirs, but try to add it as a search root
4345 * this makes able to avoid looping in recursive symlinks */
4346 if (info
.attribute
& ATTR_LINK
)
4347 add_search_root(curpath
);
4349 check_dir(curpath
, add_files
);
4352 check_dir(curpath
, add_files
);
4356 tc_stat
.curentry
= curpath
;
4358 /* Add a new entry to the temporary db file. */
4359 add_tagcache(curpath
, (info
.wrtdate
<< 16) | info
.wrttime
4360 #if defined(HAVE_TC_RAMCACHE) && defined(HAVE_DIRCACHE)
4361 , dir
->internal_entry
4365 /* Wait until current path for debug screen is read and unset. */
4366 while (tc_stat
.syncscreen
&& tc_stat
.curentry
!= NULL
)
4369 tc_stat
.curentry
= NULL
;
4372 curpath
[len
] = '\0';
4380 void tagcache_screensync_event(void)
4382 tc_stat
.curentry
= NULL
;
4385 void tagcache_screensync_enable(bool state
)
4387 tc_stat
.syncscreen
= state
;
4390 void tagcache_build(const char *path
)
4392 struct tagcache_header header
;
4397 total_entry_count
= 0;
4398 processed_dir_count
= 0;
4400 #ifdef HAVE_DIRCACHE
4401 while (dircache_is_initializing())
4405 logf("updating tagcache");
4407 cachefd
= open(TAGCACHE_FILE_TEMP
, O_RDONLY
);
4410 logf("skipping, cache already waiting for commit");
4415 cachefd
= open(TAGCACHE_FILE_TEMP
, O_RDWR
| O_CREAT
| O_TRUNC
, 0666);
4418 logf("master file open failed: %s", TAGCACHE_FILE_TEMP
);
4422 filenametag_fd
= open_tag_fd(&header
, tag_filename
, false);
4426 logf("Scanning files...");
4427 /* Scan for new files. */
4428 memset(&header
, 0, sizeof(struct tagcache_header
));
4429 write(cachefd
, &header
, sizeof(struct tagcache_header
));
4432 roots_ll
.path
= path
;
4433 roots_ll
.next
= NULL
;
4434 struct search_roots_ll
* this;
4435 /* check_dir might add new roots */
4436 for(this = &roots_ll
; this; this = this->next
)
4438 strcpy(curpath
, this->path
);
4439 ret
= ret
&& check_dir(this->path
, true);
4442 free_search_roots(roots_ll
.next
);
4444 /* Write the header. */
4445 header
.magic
= TAGCACHE_MAGIC
;
4446 header
.datasize
= data_size
;
4447 header
.entry_count
= total_entry_count
;
4448 lseek(cachefd
, 0, SEEK_SET
);
4449 write(cachefd
, &header
, sizeof(struct tagcache_header
));
4452 if (filenametag_fd
>= 0)
4454 close(filenametag_fd
);
4455 filenametag_fd
= -1;
4465 /* Commit changes to the database. */
4471 logf("tagcache built!");
4477 #ifdef HAVE_TC_RAMCACHE
4480 /* Import runtime statistics if we just initialized the db. */
4481 if (current_tcmh
.serial
== 0)
4482 queue_post(&tagcache_queue
, Q_IMPORT_CHANGELOG
, 0);
4489 #ifdef HAVE_TC_RAMCACHE
4490 static void load_ramcache(void)
4497 /* At first we should load the cache (if exists). */
4498 tc_stat
.ramcache
= load_tagcache();
4500 if (!tc_stat
.ramcache
)
4502 /* If loading failed, it must indicate some problem with the db
4503 * so disable it entirely to prevent further issues. */
4504 tc_stat
.ready
= false;
4505 ramcache_hdr
= NULL
;
4511 void tagcache_unload_ramcache(void)
4513 tc_stat
.ramcache
= false;
4514 /* Just to make sure there is no statefile present. */
4515 // remove(TAGCACHE_STATEFILE);
4520 static void tagcache_thread(void)
4522 struct queue_event ev
;
4523 bool check_done
= false;
4525 /* If the previous cache build/update was interrupted, commit
4526 * the changes first in foreground. */
4532 #ifdef HAVE_TC_RAMCACHE
4533 # ifdef HAVE_EEPROM_SETTINGS
4534 if (firmware_settings
.initialized
&& firmware_settings
.disk_clean
4535 && global_settings
.tagcache_ram
)
4537 check_done
= tagcache_dumpload();
4540 remove(TAGCACHE_STATEFILE
);
4543 /* Allocate space for the tagcache if found on disk. */
4544 if (global_settings
.tagcache_ram
&& !tc_stat
.ramcache
)
4545 allocate_tagcache();
4549 tc_stat
.initialized
= true;
4551 /* Don't delay bootup with the header check but do it on background. */
4555 tc_stat
.ready
= check_all_headers();
4556 tc_stat
.readyvalid
= true;
4561 run_command_queue(false);
4563 queue_wait_w_tmo(&tagcache_queue
, &ev
, HZ
);
4567 case Q_IMPORT_CHANGELOG
:
4568 tagcache_import_changelog();
4573 remove(TAGCACHE_FILE_TEMP
);
4574 tagcache_build("/");
4578 tagcache_build("/");
4579 #ifdef HAVE_TC_RAMCACHE
4582 check_deleted_files();
4588 if (check_done
|| !tc_stat
.ready
)
4591 #ifdef HAVE_TC_RAMCACHE
4592 if (!tc_stat
.ramcache
&& global_settings
.tagcache_ram
)
4595 if (tc_stat
.ramcache
&& global_settings
.tagcache_autoupdate
)
4596 tagcache_build("/");
4600 if (global_settings
.tagcache_autoupdate
)
4602 tagcache_build("/");
4604 /* This will be very slow unless dircache is enabled
4605 or target is flash based, but do it anyway for
4607 check_deleted_files();
4610 logf("tagcache check done");
4621 #if (CONFIG_PLATFORM & PLATFORM_NATIVE)
4622 case SYS_USB_CONNECTED
:
4623 logf("USB: TagCache");
4624 usb_acknowledge(SYS_USB_CONNECTED_ACK
);
4625 usb_wait_for_disconnect(&tagcache_queue
);
4632 bool tagcache_prepare_shutdown(void)
4634 if (tagcache_get_commit_step() > 0)
4637 tagcache_stop_scan();
4638 while (read_lock
|| write_lock
)
4644 void tagcache_shutdown(void)
4646 /* Flush the command queue. */
4647 run_command_queue(true);
4649 #ifdef HAVE_EEPROM_SETTINGS
4650 if (tc_stat
.ramcache
)
4651 tagcache_dumpsave();
4655 static int get_progress(void)
4657 int total_count
= -1;
4659 #ifdef HAVE_DIRCACHE
4660 if (dircache_is_enabled())
4662 total_count
= dircache_get_entry_count();
4666 #ifdef HAVE_TC_RAMCACHE
4668 if (ramcache_hdr
&& tc_stat
.ramcache
)
4669 total_count
= current_tcmh
.tch
.entry_count
;
4673 if (total_count
< 0)
4676 return processed_dir_count
* 100 / total_count
;
4679 struct tagcache_stat
* tagcache_get_stat(void)
4681 tc_stat
.progress
= get_progress();
4682 tc_stat
.processed_entries
= processed_dir_count
;
4687 void tagcache_start_scan(void)
4689 queue_post(&tagcache_queue
, Q_START_SCAN
, 0);
4692 bool tagcache_update(void)
4697 queue_post(&tagcache_queue
, Q_UPDATE
, 0);
4701 bool tagcache_rebuild()
4703 queue_post(&tagcache_queue
, Q_REBUILD
, 0);
4707 void tagcache_stop_scan(void)
4709 queue_post(&tagcache_queue
, Q_STOP_SCAN
, 0);
4712 #ifdef HAVE_TC_RAMCACHE
4713 bool tagcache_is_ramcache(void)
4715 return tc_stat
.ramcache
;
4719 #endif /* !__PCTOOL__ */
4722 void tagcache_init(void)
4724 memset(&tc_stat
, 0, sizeof(struct tagcache_stat
));
4725 memset(¤t_tcmh
, 0, sizeof(struct master_header
));
4726 filenametag_fd
= -1;
4727 write_lock
= read_lock
= 0;
4730 mutex_init(&command_queue_mutex
);
4731 queue_init(&tagcache_queue
, true);
4732 create_thread(tagcache_thread
, tagcache_stack
,
4733 sizeof(tagcache_stack
), 0, tagcache_thread_name
4734 IF_PRIO(, PRIORITY_BACKGROUND
)
4737 tc_stat
.initialized
= true;
4741 tc_stat
.ready
= check_all_headers();
4746 void tagcache_reverse_scan(void)
4748 logf("Checking for deleted files");
4749 check_deleted_files();
4753 bool tagcache_is_initialized(void)
4755 return tc_stat
.initialized
;
4757 bool tagcache_is_fully_initialized(void)
4759 return tc_stat
.readyvalid
;
4761 bool tagcache_is_usable(void)
4763 return tc_stat
.initialized
&& tc_stat
.ready
;
4765 int tagcache_get_commit_step(void)
4767 return tc_stat
.commit_step
;
4769 int tagcache_get_max_commit_step(void)
4771 return (int)(SORTED_TAGS_COUNT
)+1;