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 "ata_idle_notify.h"
85 #include "eeprom_settings.h"
89 #define yield() do { } while(0)
90 #define sim_sleep(timeout) do { } while(0)
91 #define do_timed_yield() do { } while(0)
95 /* Tag Cache thread. */
96 static struct event_queue tagcache_queue
;
97 static long tagcache_stack
[(DEFAULT_STACK_SIZE
+ 0x4000)/sizeof(long)];
98 static const char tagcache_thread_name
[] = "tagcache";
101 #define UNTAGGED "<Untagged>"
103 /* Previous path when scanning directory tree recursively. */
104 static char curpath
[TAG_MAXLEN
+32];
106 /* Used when removing duplicates. */
107 static char *tempbuf
; /* Allocated when needed. */
108 static long tempbufidx
; /* Current location in buffer. */
109 static long tempbuf_size
; /* Buffer size (TEMPBUF_SIZE). */
110 static long tempbuf_left
; /* Buffer space left. */
111 static long tempbuf_pos
;
113 /* Tags we want to get sorted (loaded to the tempbuf). */
114 static const int sorted_tags
[] = { tag_artist
, tag_album
, tag_genre
,
115 tag_composer
, tag_comment
, tag_albumartist
, tag_grouping
, tag_title
};
117 /* Uniqued tags (we can use these tags with filters and conditional clauses). */
118 static const int unique_tags
[] = { tag_artist
, tag_album
, tag_genre
,
119 tag_composer
, tag_comment
, tag_albumartist
, tag_grouping
};
121 /* Numeric tags (we can use these tags with conditional clauses). */
122 static const int numeric_tags
[] = { tag_year
, tag_discnumber
,
123 tag_tracknumber
, tag_length
, tag_bitrate
, tag_playcount
, tag_rating
,
124 tag_playtime
, tag_lastplayed
, tag_commitid
, tag_mtime
,
125 tag_virt_length_min
, tag_virt_length_sec
,
126 tag_virt_playtime_min
, tag_virt_playtime_sec
,
127 tag_virt_entryage
, tag_virt_autoscore
};
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" };
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
{
158 static struct tagcache_command_entry command_queue
[TAGCACHE_COMMAND_QUEUE_LENGTH
];
159 static volatile int command_queue_widx
= 0;
160 static volatile int command_queue_ridx
= 0;
161 static struct mutex command_queue_mutex
;
163 /* Tag database structures. */
165 /* Variable-length tag entry in tag files. */
166 struct tagfile_entry
{
167 short tag_length
; /* Length of the data in bytes including '\0' */
168 short idx_id
; /* Corresponding entry location in index file of not unique tags */
169 char tag_data
[0]; /* Begin of the tag data */
172 /* Fixed-size tag entry in master db index. */
174 int32_t tag_seek
[TAG_COUNT
]; /* Location of tag data or numeric tag data */
175 int32_t flag
; /* Status flags */
178 /* Header is the same in every file. */
179 struct tagcache_header
{
180 int32_t magic
; /* Header version number */
181 int32_t datasize
; /* Data size in bytes */
182 int32_t entry_count
; /* Number of entries in this file */
185 struct master_header
{
186 struct tagcache_header tch
;
187 int32_t serial
; /* Increasing counting number */
188 int32_t commitid
; /* Number of commits so far */
192 /* For the endianess correction */
193 static const char *tagfile_entry_ec
= "ss";
195 Note: This should be (1 + TAG_COUNT) amount of l's.
197 static const char *index_entry_ec
= "lllllllllllllllllllll";
199 static const char *tagcache_header_ec
= "lll";
200 static const char *master_header_ec
= "llllll";
202 static struct master_header current_tcmh
;
204 #ifdef HAVE_TC_RAMCACHE
205 /* Header is created when loading database to ram. */
206 struct ramcache_header
{
207 struct master_header h
; /* Header from the master index */
208 struct index_entry
*indices
; /* Master index file content */
209 char *tags
[TAG_COUNT
]; /* Tag file content (not including filename tag) */
210 int entry_count
[TAG_COUNT
]; /* Number of entries in the indices. */
213 # ifdef HAVE_EEPROM_SETTINGS
214 struct statefile_header
{
215 struct ramcache_header
*hdr
;
216 struct tagcache_stat tc_stat
;
220 /* Pointer to allocated ramcache_header */
221 static struct ramcache_header
*hdr
;
225 * Full tag entries stored in a temporary file waiting
226 * for commit to the cache. */
227 struct temp_file_entry
{
228 long tag_offset
[TAG_COUNT
];
229 short tag_length
[TAG_COUNT
];
235 struct tempbuf_id_list
{
237 struct tempbuf_id_list
*next
;
240 struct tempbuf_searchidx
{
244 struct tempbuf_id_list idlist
;
247 /* Lookup buffer for fixing messed up index while after sorting. */
248 static long commit_entry_count
;
249 static long lookup_buffer_depth
;
250 static struct tempbuf_searchidx
**lookup
;
252 /* Used when building the temporary file. */
253 static int cachefd
= -1, filenametag_fd
;
254 static int total_entry_count
= 0;
255 static int data_size
= 0;
256 static int processed_dir_count
;
258 /* Thread safe locking */
259 static volatile int write_lock
;
260 static volatile int read_lock
;
262 static bool delete_entry(long idx_id
);
264 const char* tagcache_tag_to_str(int tag
)
266 return tags_str
[tag
];
269 bool tagcache_is_numeric_tag(int type
)
273 for (i
= 0; i
< (int)(sizeof(numeric_tags
)/sizeof(numeric_tags
[0])); i
++)
275 if (type
== numeric_tags
[i
])
282 bool tagcache_is_unique_tag(int type
)
286 for (i
= 0; i
< (int)(sizeof(unique_tags
)/sizeof(unique_tags
[0])); i
++)
288 if (type
== unique_tags
[i
])
295 bool tagcache_is_sorted_tag(int type
)
299 for (i
= 0; i
< (int)(sizeof(sorted_tags
)/sizeof(sorted_tags
[0])); i
++)
301 if (type
== sorted_tags
[i
])
310 * Returns true if specified flag is still present, i.e., dircache
311 * has not been reloaded.
313 static bool is_dircache_intact(void)
315 return dircache_get_appflag(DIRCACHE_APPFLAG_TAGCACHE
);
319 static int open_tag_fd(struct tagcache_header
*hdr
, int tag
, bool write
)
325 if (tagcache_is_numeric_tag(tag
) || tag
< 0 || tag
>= TAG_COUNT
)
328 snprintf(buf
, sizeof buf
, TAGCACHE_FILE_INDEX
, tag
);
330 fd
= open(buf
, write
? O_RDWR
: O_RDONLY
);
333 logf("tag file open failed: tag=%d write=%d file=%s", tag
, write
, buf
);
334 tc_stat
.ready
= false;
338 /* Check the header. */
339 rc
= ecread(fd
, hdr
, 1, tagcache_header_ec
, tc_stat
.econ
);
340 if (hdr
->magic
!= TAGCACHE_MAGIC
|| rc
!= sizeof(struct tagcache_header
))
342 logf("header error");
343 tc_stat
.ready
= false;
351 static int open_master_fd(struct master_header
*hdr
, bool write
)
356 fd
= open(TAGCACHE_FILE_MASTER
, write
? O_RDWR
: O_RDONLY
);
359 logf("master file open failed for R/W");
360 tc_stat
.ready
= false;
364 tc_stat
.econ
= false;
366 /* Check the header. */
367 rc
= read(fd
, hdr
, sizeof(struct master_header
));
368 if (hdr
->tch
.magic
== TAGCACHE_MAGIC
&& rc
== sizeof(struct master_header
))
374 /* Trying to read again, this time with endianess correction enabled. */
375 lseek(fd
, 0, SEEK_SET
);
377 rc
= ecread(fd
, hdr
, 1, master_header_ec
, true);
378 if (hdr
->tch
.magic
!= TAGCACHE_MAGIC
|| rc
!= sizeof(struct master_header
))
380 logf("header error");
381 tc_stat
.ready
= false;
392 static bool do_timed_yield(void)
394 /* Sorting can lock up for quite a while, so yield occasionally */
395 static long wakeup_tick
= 0;
396 if (current_tick
>= wakeup_tick
)
398 wakeup_tick
= current_tick
+ (HZ
/4);
406 #if defined(HAVE_TC_RAMCACHE) && defined(HAVE_DIRCACHE)
407 static long find_entry_ram(const char *filename
,
408 const struct dirent
*dc
)
410 static long last_pos
= 0;
413 /* Check if we tagcache is loaded into ram. */
414 if (!tc_stat
.ramcache
)
418 dc
= dircache_get_entry_ptr(filename
);
422 logf("tagcache: file not found.");
433 for (; i
< hdr
->h
.tch
.entry_count
; i
++)
435 if (hdr
->indices
[i
].tag_seek
[tag_filename
] == (long)dc
)
437 last_pos
= MAX(0, i
- 3);
454 static long find_entry_disk(const char *filename
)
456 struct tagcache_header tch
;
457 static long last_pos
= -1;
458 long pos_history
[POS_HISTORY_COUNT
];
459 long pos_history_idx
= 0;
461 struct tagfile_entry tfe
;
463 char buf
[TAG_MAXLEN
+32];
474 if ( (fd
= open_tag_fd(&tch
, tag_filename
, false)) < 0)
481 lseek(fd
, last_pos
, SEEK_SET
);
483 lseek(fd
, sizeof(struct tagcache_header
), SEEK_SET
);
487 pos
= lseek(fd
, 0, SEEK_CUR
);
488 for (i
= pos_history_idx
-1; i
>= 0; i
--)
489 pos_history
[i
+1] = pos_history
[i
];
490 pos_history
[0] = pos
;
492 if (ecread(fd
, &tfe
, 1, tagfile_entry_ec
, tc_stat
.econ
)
493 != sizeof(struct tagfile_entry
))
498 if (tfe
.tag_length
>= (long)sizeof(buf
))
500 logf("too long tag #1");
506 if (read(fd
, buf
, tfe
.tag_length
) != tfe
.tag_length
)
508 logf("read error #2");
514 if (!strcasecmp(filename
, buf
))
516 last_pos
= pos_history
[pos_history_idx
];
521 if (pos_history_idx
< POS_HISTORY_COUNT
- 1)
535 if (fd
!= filenametag_fd
)
540 if (fd
!= filenametag_fd
)
546 static int find_index(const char *filename
)
550 #if defined(HAVE_TC_RAMCACHE) && defined(HAVE_DIRCACHE)
551 if (tc_stat
.ramcache
&& is_dircache_intact())
552 idx_id
= find_entry_ram(filename
, NULL
);
556 idx_id
= find_entry_disk(filename
);
561 bool tagcache_find_index(struct tagcache_search
*tcs
, const char *filename
)
568 idx_id
= find_index(filename
);
572 if (!tagcache_search(tcs
, tag_filename
))
575 tcs
->entry_count
= 0;
576 tcs
->idx_id
= idx_id
;
581 static bool get_index(int masterfd
, int idxid
,
582 struct index_entry
*idx
, bool use_ram
)
584 bool localfd
= false;
588 logf("Incorrect idxid: %d", idxid
);
592 #ifdef HAVE_TC_RAMCACHE
593 if (tc_stat
.ramcache
&& use_ram
)
595 if (hdr
->indices
[idxid
].flag
& FLAG_DELETED
)
598 memcpy(idx
, &hdr
->indices
[idxid
], sizeof(struct index_entry
));
607 struct master_header tcmh
;
610 masterfd
= open_master_fd(&tcmh
, false);
615 lseek(masterfd
, idxid
* sizeof(struct index_entry
)
616 + sizeof(struct master_header
), SEEK_SET
);
617 if (ecread(masterfd
, idx
, 1, index_entry_ec
, tc_stat
.econ
)
618 != sizeof(struct index_entry
))
620 logf("read error #3");
630 if (idx
->flag
& FLAG_DELETED
)
636 static bool write_index(int masterfd
, int idxid
, struct index_entry
*idx
)
638 /* We need to exclude all memory only flags & tags when writing to disk. */
639 if (idx
->flag
& FLAG_DIRCACHE
)
641 logf("memory only flags!");
645 #ifdef HAVE_TC_RAMCACHE
646 /* Only update numeric data. Writing the whole index to RAM by memcpy
647 * destroys dircache pointers!
649 if (tc_stat
.ramcache
)
652 struct index_entry
*idx_ram
= &hdr
->indices
[idxid
];
654 for (tag
= 0; tag
< TAG_COUNT
; tag
++)
656 if (tagcache_is_numeric_tag(tag
))
658 idx_ram
->tag_seek
[tag
] = idx
->tag_seek
[tag
];
662 /* Don't touch the dircache flag or attributes. */
663 idx_ram
->flag
= (idx
->flag
& 0x0000ffff)
664 | (idx_ram
->flag
& (0xffff0000 | FLAG_DIRCACHE
));
668 lseek(masterfd
, idxid
* sizeof(struct index_entry
)
669 + sizeof(struct master_header
), SEEK_SET
);
670 if (ecwrite(masterfd
, idx
, 1, index_entry_ec
, tc_stat
.econ
)
671 != sizeof(struct index_entry
))
673 logf("write error #3");
674 logf("idxid: %d", idxid
);
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(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 dircache_copy_path((struct dirent
*)seek
,
733 if (tag
!= tag_filename
)
735 ep
= (struct tagfile_entry
*)&hdr
->tags
[tag
][seek
];
736 strncpy(buf
, ep
->tag_data
, size
-1);
743 if (!open_files(tcs
, tag
))
746 lseek(tcs
->idxfd
[tag
], seek
, SEEK_SET
);
747 if (ecread(tcs
->idxfd
[tag
], &tfe
, 1, tagfile_entry_ec
, tc_stat
.econ
)
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 static long check_virtual_tags(int tag
, const struct index_entry
*idx
)
778 case tag_virt_length_sec
:
779 data
= (idx
->tag_seek
[tag_length
]/1000) % 60;
782 case tag_virt_length_min
:
783 data
= (idx
->tag_seek
[tag_length
]/1000) / 60;
786 case tag_virt_playtime_sec
:
787 data
= (idx
->tag_seek
[tag_playtime
]/1000) % 60;
790 case tag_virt_playtime_min
:
791 data
= (idx
->tag_seek
[tag_playtime
]/1000) / 60;
794 case tag_virt_autoscore
:
795 if (idx
->tag_seek
[tag_length
] == 0
796 || idx
->tag_seek
[tag_playcount
] == 0)
802 data
= 100 * idx
->tag_seek
[tag_playtime
]
803 / idx
->tag_seek
[tag_length
]
804 / idx
->tag_seek
[tag_playcount
];
808 /* How many commits before the file has been added to the DB. */
809 case tag_virt_entryage
:
810 data
= current_tcmh
.commitid
- idx
->tag_seek
[tag_commitid
] - 1;
814 data
= idx
->tag_seek
[tag
];
820 long tagcache_get_numeric(const struct tagcache_search
*tcs
, int tag
)
822 struct index_entry idx
;
827 if (!tagcache_is_numeric_tag(tag
))
830 if (!get_index(tcs
->masterfd
, tcs
->idx_id
, &idx
, true))
833 return check_virtual_tags(tag
, &idx
);
836 inline static bool str_ends_with(const char *str1
, const char *str2
)
838 int str_len
= strlen(str1
);
839 int clause_len
= strlen(str2
);
841 if (clause_len
> str_len
)
844 return !strcasecmp(&str1
[str_len
- clause_len
], str2
);
847 inline static bool str_oneof(const char *str
, const char *list
)
850 int l
, len
= strlen(str
);
854 sep
= strchr(list
, '|');
855 l
= sep
? (long)sep
- (long)list
: (int)strlen(list
);
856 if ((l
==len
) && !strncasecmp(str
, list
, len
))
858 list
+= sep
? l
+ 1 : l
;
864 static bool check_against_clause(long numeric
, const char *str
,
865 const struct tagcache_search_clause
*clause
)
869 switch (clause
->type
)
872 return numeric
== clause
->numeric_data
;
874 return numeric
!= clause
->numeric_data
;
876 return numeric
> clause
->numeric_data
;
878 return numeric
>= clause
->numeric_data
;
880 return numeric
< clause
->numeric_data
;
882 return numeric
<= clause
->numeric_data
;
884 logf("Incorrect numeric tag: %d", clause
->type
);
889 switch (clause
->type
)
892 return !strcasecmp(clause
->str
, str
);
894 return strcasecmp(clause
->str
, str
);
896 return 0>strcasecmp(clause
->str
, str
);
898 return 0>=strcasecmp(clause
->str
, str
);
900 return 0<strcasecmp(clause
->str
, str
);
902 return 0<=strcasecmp(clause
->str
, str
);
903 case clause_contains
:
904 return (strcasestr(str
, clause
->str
) != NULL
);
905 case clause_not_contains
:
906 return (strcasestr(str
, clause
->str
) == NULL
);
907 case clause_begins_with
:
908 return (strcasestr(str
, clause
->str
) == str
);
909 case clause_not_begins_with
:
910 return (strcasestr(str
, clause
->str
) != str
);
911 case clause_ends_with
:
912 return str_ends_with(str
, clause
->str
);
913 case clause_not_ends_with
:
914 return !str_ends_with(str
, clause
->str
);
916 return str_oneof(str
, clause
->str
);
919 logf("Incorrect tag: %d", clause
->type
);
926 static bool check_clauses(struct tagcache_search
*tcs
,
927 struct index_entry
*idx
,
928 struct tagcache_search_clause
**clause
, int count
)
932 #ifdef HAVE_TC_RAMCACHE
935 /* Go through all conditional clauses. */
936 for (i
= 0; i
< count
; i
++)
938 struct tagfile_entry
*tfe
;
943 seek
= check_virtual_tags(clause
[i
]->tag
, idx
);
945 if (!tagcache_is_numeric_tag(clause
[i
]->tag
))
947 if (clause
[i
]->tag
== tag_filename
)
949 retrieve(tcs
, idx
, tag_filename
, buf
, sizeof buf
);
954 tfe
= (struct tagfile_entry
*)&hdr
->tags
[clause
[i
]->tag
][seek
];
959 if (!check_against_clause(seek
, str
, clause
[i
]))
966 /* Check for conditions. */
967 for (i
= 0; i
< count
; i
++)
969 struct tagfile_entry tfe
;
973 seek
= check_virtual_tags(clause
[i
]->tag
, idx
);
975 memset(str
, 0, sizeof str
);
976 if (!tagcache_is_numeric_tag(clause
[i
]->tag
))
978 int fd
= tcs
->idxfd
[clause
[i
]->tag
];
979 lseek(fd
, seek
, SEEK_SET
);
980 ecread(fd
, &tfe
, 1, tagfile_entry_ec
, tc_stat
.econ
);
981 if (tfe
.tag_length
>= (int)sizeof(str
))
983 logf("Too long tag read!");
987 read(fd
, str
, tfe
.tag_length
);
989 /* Check if entry has been deleted. */
994 if (!check_against_clause(seek
, str
, clause
[i
]))
1002 bool tagcache_check_clauses(struct tagcache_search
*tcs
,
1003 struct tagcache_search_clause
**clause
, int count
)
1005 struct index_entry idx
;
1010 if (!get_index(tcs
->masterfd
, tcs
->idx_id
, &idx
, true))
1013 return check_clauses(tcs
, &idx
, clause
, count
);
1016 static bool add_uniqbuf(struct tagcache_search
*tcs
, unsigned long id
)
1020 /* If uniq buffer is not defined we must return true for search to work. */
1021 if (tcs
->unique_list
== NULL
1022 || (!tagcache_is_unique_tag(tcs
->type
)
1023 && !tagcache_is_numeric_tag(tcs
->type
)))
1028 for (i
= 0; i
< tcs
->unique_list_count
; i
++)
1030 /* Return false if entry is found. */
1031 if (tcs
->unique_list
[i
] == id
)
1035 if (tcs
->unique_list_count
< tcs
->unique_list_capacity
)
1037 tcs
->unique_list
[i
] = id
;
1038 tcs
->unique_list_count
++;
1044 static bool build_lookup_list(struct tagcache_search
*tcs
)
1046 struct index_entry entry
;
1049 tcs
->seek_list_count
= 0;
1051 #ifdef HAVE_TC_RAMCACHE
1056 for (i
= tcs
->seek_pos
; i
< hdr
->h
.tch
.entry_count
; i
++)
1058 struct index_entry
*idx
= &hdr
->indices
[i
];
1059 if (tcs
->seek_list_count
== SEEK_LIST_SIZE
)
1062 /* Skip deleted files. */
1063 if (idx
->flag
& FLAG_DELETED
)
1066 /* Go through all filters.. */
1067 for (j
= 0; j
< tcs
->filter_count
; j
++)
1069 if (idx
->tag_seek
[tcs
->filter_tag
[j
]] != tcs
->filter_seek
[j
])
1075 if (j
< tcs
->filter_count
)
1078 /* Check for conditions. */
1079 if (!check_clauses(tcs
, idx
, tcs
->clause
, tcs
->clause_count
))
1082 /* Add to the seek list if not already in uniq buffer. */
1083 if (!add_uniqbuf(tcs
, idx
->tag_seek
[tcs
->type
]))
1087 tcs
->seek_list
[tcs
->seek_list_count
] = idx
->tag_seek
[tcs
->type
];
1088 tcs
->seek_flags
[tcs
->seek_list_count
] = idx
->flag
;
1089 tcs
->seek_list_count
++;
1094 return tcs
->seek_list_count
> 0;
1098 lseek(tcs
->masterfd
, tcs
->seek_pos
* sizeof(struct index_entry
) +
1099 sizeof(struct master_header
), SEEK_SET
);
1101 while (ecread(tcs
->masterfd
, &entry
, 1, index_entry_ec
, tc_stat
.econ
)
1102 == sizeof(struct index_entry
))
1104 if (tcs
->seek_list_count
== SEEK_LIST_SIZE
)
1109 /* Check if entry has been deleted. */
1110 if (entry
.flag
& FLAG_DELETED
)
1113 /* Go through all filters.. */
1114 for (i
= 0; i
< tcs
->filter_count
; i
++)
1116 if (entry
.tag_seek
[tcs
->filter_tag
[i
]] != tcs
->filter_seek
[i
])
1120 if (i
< tcs
->filter_count
)
1123 /* Check for conditions. */
1124 if (!check_clauses(tcs
, &entry
, tcs
->clause
, tcs
->clause_count
))
1127 /* Add to the seek list if not already in uniq buffer. */
1128 if (!add_uniqbuf(tcs
, entry
.tag_seek
[tcs
->type
]))
1132 tcs
->seek_list
[tcs
->seek_list_count
] = entry
.tag_seek
[tcs
->type
];
1133 tcs
->seek_flags
[tcs
->seek_list_count
] = entry
.flag
;
1134 tcs
->seek_list_count
++;
1139 return tcs
->seek_list_count
> 0;
1143 static void remove_files(void)
1148 tc_stat
.ready
= false;
1149 tc_stat
.ramcache
= false;
1150 tc_stat
.econ
= false;
1151 remove(TAGCACHE_FILE_MASTER
);
1152 for (i
= 0; i
< TAG_COUNT
; i
++)
1154 if (tagcache_is_numeric_tag(i
))
1157 snprintf(buf
, sizeof buf
, TAGCACHE_FILE_INDEX
, i
);
1163 static bool check_all_headers(void)
1165 struct master_header myhdr
;
1166 struct tagcache_header tch
;
1170 if ( (fd
= open_master_fd(&myhdr
, false)) < 0)
1176 logf("tagcache is dirty!");
1180 memcpy(¤t_tcmh
, &myhdr
, sizeof(struct master_header
));
1182 for (tag
= 0; tag
< TAG_COUNT
; tag
++)
1184 if (tagcache_is_numeric_tag(tag
))
1187 if ( (fd
= open_tag_fd(&tch
, tag
, false)) < 0)
1196 bool tagcache_search(struct tagcache_search
*tcs
, int tag
)
1198 struct tagcache_header tag_hdr
;
1199 struct master_header master_hdr
;
1202 if (tcs
->initialized
)
1203 tagcache_search_finish(tcs
);
1208 memset(tcs
, 0, sizeof(struct tagcache_search
));
1209 if (tc_stat
.commit_step
> 0 || !tc_stat
.ready
)
1212 tcs
->position
= sizeof(struct tagcache_header
);
1215 tcs
->seek_list_count
= 0;
1216 tcs
->filter_count
= 0;
1219 for (i
= 0; i
< TAG_COUNT
; i
++)
1222 #ifndef HAVE_TC_RAMCACHE
1223 tcs
->ramsearch
= false;
1225 tcs
->ramsearch
= tc_stat
.ramcache
;
1228 tcs
->entry_count
= hdr
->entry_count
[tcs
->type
];
1233 if (!tagcache_is_numeric_tag(tcs
->type
))
1235 tcs
->idxfd
[tcs
->type
] = open_tag_fd(&tag_hdr
, tcs
->type
, false);
1236 if (tcs
->idxfd
[tcs
->type
] < 0)
1240 /* Always open as R/W so we can pass tcs to functions that modify data also
1241 * without failing. */
1242 tcs
->masterfd
= open_master_fd(&master_hdr
, true);
1244 if (tcs
->masterfd
< 0)
1249 tcs
->initialized
= true;
1255 void tagcache_search_set_uniqbuf(struct tagcache_search
*tcs
,
1256 void *buffer
, long length
)
1258 tcs
->unique_list
= (unsigned long *)buffer
;
1259 tcs
->unique_list_capacity
= length
/ sizeof(*tcs
->unique_list
);
1260 tcs
->unique_list_count
= 0;
1263 bool tagcache_search_add_filter(struct tagcache_search
*tcs
,
1266 if (tcs
->filter_count
== TAGCACHE_MAX_FILTERS
)
1269 if (!tagcache_is_unique_tag(tag
) || tagcache_is_numeric_tag(tag
))
1272 tcs
->filter_tag
[tcs
->filter_count
] = tag
;
1273 tcs
->filter_seek
[tcs
->filter_count
] = seek
;
1274 tcs
->filter_count
++;
1279 bool tagcache_search_add_clause(struct tagcache_search
*tcs
,
1280 struct tagcache_search_clause
*clause
)
1284 if (tcs
->clause_count
>= TAGCACHE_MAX_CLAUSES
)
1286 logf("Too many clauses");
1290 /* Check if there is already a similar filter in present (filters are
1291 * much faster than clauses).
1293 for (i
= 0; i
< tcs
->filter_count
; i
++)
1295 if (tcs
->filter_tag
[i
] == clause
->tag
)
1299 if (!tagcache_is_numeric_tag(clause
->tag
) && tcs
->idxfd
[clause
->tag
] < 0)
1303 snprintf(buf
, sizeof buf
, TAGCACHE_FILE_INDEX
, clause
->tag
);
1304 tcs
->idxfd
[clause
->tag
] = open(buf
, O_RDONLY
);
1307 tcs
->clause
[tcs
->clause_count
] = clause
;
1308 tcs
->clause_count
++;
1313 /* TODO: Remove this mess. */
1314 #ifdef HAVE_DIRCACHE
1315 #define TAG_FILENAME_RAM(tcs) ((tcs->type == tag_filename) \
1316 ? ((flag & FLAG_DIRCACHE) && is_dircache_intact()) : 1)
1318 #define TAG_FILENAME_RAM(tcs) (tcs->type != tag_filename)
1321 static bool get_next(struct tagcache_search
*tcs
)
1323 static char buf
[TAG_MAXLEN
+32];
1324 struct tagfile_entry entry
;
1327 if (!tcs
->valid
|| !tc_stat
.ready
)
1330 if (tcs
->idxfd
[tcs
->type
] < 0 && !tagcache_is_numeric_tag(tcs
->type
)
1331 #ifdef HAVE_TC_RAMCACHE
1337 /* Relative fetch. */
1338 if (tcs
->filter_count
> 0 || tcs
->clause_count
> 0
1339 || tagcache_is_numeric_tag(tcs
->type
))
1341 /* Check for end of list. */
1342 if (tcs
->seek_list_count
== 0)
1344 /* Try to fetch more. */
1345 if (!build_lookup_list(tcs
))
1352 tcs
->seek_list_count
--;
1353 flag
= tcs
->seek_flags
[tcs
->seek_list_count
];
1355 /* Seek stream to the correct position and continue to direct fetch. */
1356 if ((!tcs
->ramsearch
|| !TAG_FILENAME_RAM(tcs
))
1357 && !tagcache_is_numeric_tag(tcs
->type
))
1359 if (!open_files(tcs
, tcs
->type
))
1362 lseek(tcs
->idxfd
[tcs
->type
], tcs
->seek_list
[tcs
->seek_list_count
], SEEK_SET
);
1365 tcs
->position
= tcs
->seek_list
[tcs
->seek_list_count
];
1368 if (tagcache_is_numeric_tag(tcs
->type
))
1370 snprintf(buf
, sizeof(buf
), "%d", tcs
->position
);
1371 tcs
->result_seek
= tcs
->position
;
1373 tcs
->result_len
= strlen(buf
) + 1;
1378 #ifdef HAVE_TC_RAMCACHE
1379 if (tcs
->ramsearch
&& TAG_FILENAME_RAM(tcs
))
1381 struct tagfile_entry
*ep
;
1383 if (tcs
->entry_count
== 0)
1390 tcs
->result_seek
= tcs
->position
;
1392 # ifdef HAVE_DIRCACHE
1393 if (tcs
->type
== tag_filename
)
1395 dircache_copy_path((struct dirent
*)tcs
->position
,
1398 tcs
->result_len
= strlen(buf
) + 1;
1399 tcs
->idx_id
= FLAG_GET_ATTR(flag
);
1400 tcs
->ramresult
= false;
1406 ep
= (struct tagfile_entry
*)&hdr
->tags
[tcs
->type
][tcs
->position
];
1407 tcs
->position
+= sizeof(struct tagfile_entry
) + ep
->tag_length
;
1408 tcs
->result
= ep
->tag_data
;
1409 tcs
->result_len
= strlen(tcs
->result
) + 1;
1410 tcs
->idx_id
= ep
->idx_id
;
1411 tcs
->ramresult
= true;
1418 if (!open_files(tcs
, tcs
->type
))
1421 tcs
->result_seek
= lseek(tcs
->idxfd
[tcs
->type
], 0, SEEK_CUR
);
1422 if (ecread(tcs
->idxfd
[tcs
->type
], &entry
, 1,
1423 tagfile_entry_ec
, tc_stat
.econ
) != sizeof(struct tagfile_entry
))
1431 if (entry
.tag_length
> (long)sizeof(buf
))
1434 logf("too long tag #2");
1438 if (read(tcs
->idxfd
[tcs
->type
], buf
, entry
.tag_length
) != entry
.tag_length
)
1441 logf("read error #4");
1446 tcs
->result_len
= strlen(tcs
->result
) + 1;
1447 tcs
->idx_id
= entry
.idx_id
;
1448 tcs
->ramresult
= false;
1453 bool tagcache_get_next(struct tagcache_search
*tcs
)
1455 while (get_next(tcs
))
1457 if (tcs
->result_len
> 1)
1464 bool tagcache_retrieve(struct tagcache_search
*tcs
, int idxid
,
1465 int tag
, char *buf
, long size
)
1467 struct index_entry idx
;
1470 if (!get_index(tcs
->masterfd
, idxid
, &idx
, true))
1473 return retrieve(tcs
, &idx
, tag
, buf
, size
);
1476 static bool update_master_header(void)
1478 struct master_header myhdr
;
1484 if ( (fd
= open_master_fd(&myhdr
, true)) < 0)
1487 myhdr
.serial
= current_tcmh
.serial
;
1488 myhdr
.commitid
= current_tcmh
.commitid
;
1491 lseek(fd
, 0, SEEK_SET
);
1492 ecwrite(fd
, &myhdr
, 1, master_header_ec
, tc_stat
.econ
);
1495 #ifdef HAVE_TC_RAMCACHE
1498 hdr
->h
.serial
= current_tcmh
.serial
;
1499 hdr
->h
.commitid
= current_tcmh
.commitid
;
1508 void tagcache_modify(struct tagcache_search
*tcs
, int type
, const char *text
)
1510 struct tagentry
*entry
;
1512 if (tcs
->type
!= tag_title
)
1515 /* We will need reserve buffer for this. */
1518 struct tagfile_entry
*ep
;
1520 ep
= (struct tagfile_entry
*)&hdr
->tags
[tcs
->type
][tcs
->result_seek
];
1521 tcs
->seek_list
[tcs
->seek_list_count
];
1524 entry
= find_entry_ram();
1529 void tagcache_search_finish(struct tagcache_search
*tcs
)
1533 if (!tcs
->initialized
)
1536 if (tcs
->masterfd
>= 0)
1538 close(tcs
->masterfd
);
1542 for (i
= 0; i
< TAG_COUNT
; i
++)
1544 if (tcs
->idxfd
[i
] >= 0)
1546 close(tcs
->idxfd
[i
]);
1551 tcs
->ramsearch
= false;
1553 tcs
->initialized
= 0;
1558 #if defined(HAVE_TC_RAMCACHE) && defined(HAVE_DIRCACHE)
1559 static struct tagfile_entry
*get_tag(const struct index_entry
*entry
, int tag
)
1561 return (struct tagfile_entry
*)&hdr
->tags
[tag
][entry
->tag_seek
[tag
]];
1564 static long get_tag_numeric(const struct index_entry
*entry
, int tag
)
1566 return check_virtual_tags(tag
, entry
);
1569 static char* get_tag_string(const struct index_entry
*entry
, int tag
)
1571 char* s
= get_tag(entry
, tag
)->tag_data
;
1572 return strcmp(s
, UNTAGGED
) ? s
: NULL
;
1575 bool tagcache_fill_tags(struct mp3entry
*id3
, const char *filename
)
1577 struct index_entry
*entry
;
1583 /* Find the corresponding entry in tagcache. */
1584 idx_id
= find_entry_ram(filename
, NULL
);
1585 if (idx_id
< 0 || !tc_stat
.ramcache
)
1588 entry
= &hdr
->indices
[idx_id
];
1590 id3
->title
= get_tag_string(entry
, tag_title
);
1591 id3
->artist
= get_tag_string(entry
, tag_artist
);
1592 id3
->album
= get_tag_string(entry
, tag_album
);
1593 id3
->genre_string
= get_tag_string(entry
, tag_genre
);
1594 id3
->composer
= get_tag_string(entry
, tag_composer
);
1595 id3
->comment
= get_tag_string(entry
, tag_comment
);
1596 id3
->albumartist
= get_tag_string(entry
, tag_albumartist
);
1597 id3
->grouping
= get_tag_string(entry
, tag_grouping
);
1599 id3
->playcount
= get_tag_numeric(entry
, tag_playcount
);
1600 id3
->rating
= get_tag_numeric(entry
, tag_rating
);
1601 id3
->lastplayed
= get_tag_numeric(entry
, tag_lastplayed
);
1602 id3
->score
= get_tag_numeric(entry
, tag_virt_autoscore
) / 10;
1603 id3
->year
= get_tag_numeric(entry
, tag_year
);
1605 id3
->discnum
= get_tag_numeric(entry
, tag_discnumber
);
1606 id3
->tracknum
= get_tag_numeric(entry
, tag_tracknumber
);
1607 id3
->bitrate
= get_tag_numeric(entry
, tag_bitrate
);
1608 if (id3
->bitrate
== 0)
1615 static inline void write_item(const char *item
)
1617 int len
= strlen(item
) + 1;
1620 write(cachefd
, item
, len
);
1623 static int check_if_empty(char **tag
)
1627 if (*tag
== NULL
|| **tag
== '\0')
1630 return sizeof(UNTAGGED
); /* Tag length */
1633 length
= strlen(*tag
);
1634 if (length
> TAG_MAXLEN
)
1636 logf("over length tag: %s", *tag
);
1637 length
= TAG_MAXLEN
;
1638 (*tag
)[length
] = '\0';
1644 #define ADD_TAG(entry,tag,data) \
1646 entry.tag_offset[tag] = offset; \
1647 entry.tag_length[tag] = check_if_empty(data); \
1648 offset += entry.tag_length[tag]
1650 static void add_tagcache(char *path
, unsigned long mtime
1651 #if defined(HAVE_TC_RAMCACHE) && defined(HAVE_DIRCACHE)
1652 ,const struct dirent
*dc
1656 struct mp3entry id3
;
1657 struct temp_file_entry entry
;
1661 char tracknumfix
[3];
1663 int path_length
= strlen(path
);
1664 bool has_albumartist
;
1670 /* Check for overlength file path. */
1671 if (path_length
> TAG_MAXLEN
)
1673 /* Path can't be shortened. */
1674 logf("Too long path: %s", path
);
1678 /* Check if the file is supported. */
1679 if (probe_file_format(path
) == AFMT_UNKNOWN
)
1682 /* Check if the file is already cached. */
1683 #if defined(HAVE_TC_RAMCACHE) && defined(HAVE_DIRCACHE)
1684 if (tc_stat
.ramcache
&& is_dircache_intact())
1686 idx_id
= find_entry_ram(path
, dc
);
1691 if (filenametag_fd
>= 0)
1693 idx_id
= find_entry_disk(path
);
1697 /* Check if file has been modified. */
1700 struct index_entry idx
;
1702 /* TODO: Mark that the index exists (for fast reverse scan) */
1703 //found_idx[idx_id/8] |= idx_id%8;
1705 if (!get_index(-1, idx_id
, &idx
, true))
1707 logf("failed to retrieve index entry");
1711 if ((unsigned long)idx
.tag_seek
[tag_mtime
] == mtime
)
1713 /* No changes to file. */
1717 /* Metadata might have been changed. Delete the entry. */
1718 logf("Re-adding: %s", path
);
1719 if (!delete_entry(idx_id
))
1721 logf("delete_entry failed: %d", idx_id
);
1726 fd
= open(path
, O_RDONLY
);
1729 logf("open fail: %s", path
);
1733 memset(&id3
, 0, sizeof(struct mp3entry
));
1734 memset(&entry
, 0, sizeof(struct temp_file_entry
));
1735 memset(&tracknumfix
, 0, sizeof(tracknumfix
));
1736 ret
= get_metadata(&id3
, fd
, path
);
1742 logf("-> %s", path
);
1744 /* Generate track number if missing. */
1745 if (id3
.tracknum
<= 0)
1747 const char *p
= strrchr(path
, '.');
1750 p
= &path
[strlen(path
)-1];
1754 if (isdigit(*p
) && isdigit(*(p
-1)))
1756 tracknumfix
[1] = *p
--;
1757 tracknumfix
[0] = *p
;
1763 if (tracknumfix
[0] != '\0')
1765 id3
.tracknum
= atoi(tracknumfix
);
1766 /* Set a flag to indicate track number has been generated. */
1767 entry
.flag
|= FLAG_TRKNUMGEN
;
1771 /* Unable to generate track number. */
1777 entry
.tag_offset
[tag_year
] = id3
.year
;
1778 entry
.tag_offset
[tag_discnumber
] = id3
.discnum
;
1779 entry
.tag_offset
[tag_tracknumber
] = id3
.tracknum
;
1780 entry
.tag_offset
[tag_length
] = id3
.length
;
1781 entry
.tag_offset
[tag_bitrate
] = id3
.bitrate
;
1782 entry
.tag_offset
[tag_mtime
] = mtime
;
1785 has_albumartist
= id3
.albumartist
!= NULL
1786 && strlen(id3
.albumartist
) > 0;
1787 has_grouping
= id3
.grouping
!= NULL
1788 && strlen(id3
.grouping
) > 0;
1790 ADD_TAG(entry
, tag_filename
, &path
);
1791 ADD_TAG(entry
, tag_title
, &id3
.title
);
1792 ADD_TAG(entry
, tag_artist
, &id3
.artist
);
1793 ADD_TAG(entry
, tag_album
, &id3
.album
);
1794 ADD_TAG(entry
, tag_genre
, &id3
.genre_string
);
1795 ADD_TAG(entry
, tag_composer
, &id3
.composer
);
1796 ADD_TAG(entry
, tag_comment
, &id3
.comment
);
1797 if (has_albumartist
)
1799 ADD_TAG(entry
, tag_albumartist
, &id3
.albumartist
);
1803 ADD_TAG(entry
, tag_albumartist
, &id3
.artist
);
1807 ADD_TAG(entry
, tag_grouping
, &id3
.grouping
);
1811 ADD_TAG(entry
, tag_grouping
, &id3
.title
);
1813 entry
.data_length
= offset
;
1815 /* Write the header */
1816 write(cachefd
, &entry
, sizeof(struct temp_file_entry
));
1818 /* And tags also... Correct order is critical */
1820 write_item(id3
.title
);
1821 write_item(id3
.artist
);
1822 write_item(id3
.album
);
1823 write_item(id3
.genre_string
);
1824 write_item(id3
.composer
);
1825 write_item(id3
.comment
);
1826 if (has_albumartist
)
1828 write_item(id3
.albumartist
);
1832 write_item(id3
.artist
);
1836 write_item(id3
.grouping
);
1840 write_item(id3
.title
);
1842 total_entry_count
++;
1845 static bool tempbuf_insert(char *str
, int id
, int idx_id
, bool unique
)
1847 struct tempbuf_searchidx
*index
= (struct tempbuf_searchidx
*)tempbuf
;
1848 int len
= strlen(str
)+1;
1851 unsigned *crcbuf
= (unsigned *)&tempbuf
[tempbuf_size
-4];
1852 char buf
[TAG_MAXLEN
+32];
1854 for (i
= 0; str
[i
] != '\0' && i
< (int)sizeof(buf
)-1; i
++)
1855 buf
[i
] = tolower(str
[i
]);
1858 crc32
= crc_32(buf
, i
, 0xffffffff);
1862 /* Check if the crc does not exist -> entry does not exist for sure. */
1863 for (i
= 0; i
< tempbufidx
; i
++)
1865 if (crcbuf
[-i
] != crc32
)
1868 if (!strcasecmp(str
, index
[i
].str
))
1870 if (id
< 0 || id
>= lookup_buffer_depth
)
1872 logf("lookup buf overf.: %d", id
);
1876 lookup
[id
] = &index
[i
];
1882 /* Insert to CRC buffer. */
1883 crcbuf
[-tempbufidx
] = crc32
;
1886 /* Insert it to the buffer. */
1887 tempbuf_left
-= len
;
1888 if (tempbuf_left
- 4 < 0 || tempbufidx
>= commit_entry_count
-1)
1891 if (id
>= lookup_buffer_depth
)
1893 logf("lookup buf overf. #2: %d", id
);
1899 lookup
[id
] = &index
[tempbufidx
];
1900 index
[tempbufidx
].idlist
.id
= id
;
1903 index
[tempbufidx
].idlist
.id
= -1;
1905 index
[tempbufidx
].idlist
.next
= NULL
;
1906 index
[tempbufidx
].idx_id
= idx_id
;
1907 index
[tempbufidx
].seek
= -1;
1908 index
[tempbufidx
].str
= &tempbuf
[tempbuf_pos
];
1909 memcpy(index
[tempbufidx
].str
, str
, len
);
1916 static int compare(const void *p1
, const void *p2
)
1920 struct tempbuf_searchidx
*e1
= (struct tempbuf_searchidx
*)p1
;
1921 struct tempbuf_searchidx
*e2
= (struct tempbuf_searchidx
*)p2
;
1923 if (strcmp(e1
->str
, UNTAGGED
) == 0)
1925 if (strcmp(e2
->str
, UNTAGGED
) == 0)
1929 else if (strcmp(e2
->str
, UNTAGGED
) == 0)
1932 return strncasecmp(e1
->str
, e2
->str
, TAG_MAXLEN
);
1935 static int tempbuf_sort(int fd
)
1937 struct tempbuf_searchidx
*index
= (struct tempbuf_searchidx
*)tempbuf
;
1938 struct tagfile_entry fe
;
1942 /* Generate reverse lookup entries. */
1943 for (i
= 0; i
< lookup_buffer_depth
; i
++)
1945 struct tempbuf_id_list
*idlist
;
1950 if (lookup
[i
]->idlist
.id
== i
)
1953 idlist
= &lookup
[i
]->idlist
;
1954 while (idlist
->next
!= NULL
)
1955 idlist
= idlist
->next
;
1957 tempbuf_left
-= sizeof(struct tempbuf_id_list
);
1958 if (tempbuf_left
- 4 < 0)
1961 idlist
->next
= (struct tempbuf_id_list
*)&tempbuf
[tempbuf_pos
];
1962 if (tempbuf_pos
& 0x03)
1964 tempbuf_pos
= (tempbuf_pos
& ~0x03) + 0x04;
1966 idlist
->next
= (struct tempbuf_id_list
*)&tempbuf
[tempbuf_pos
];
1968 tempbuf_pos
+= sizeof(struct tempbuf_id_list
);
1970 idlist
= idlist
->next
;
1972 idlist
->next
= NULL
;
1977 qsort(index
, tempbufidx
, sizeof(struct tempbuf_searchidx
), compare
);
1978 memset(lookup
, 0, lookup_buffer_depth
* sizeof(struct tempbuf_searchidx
**));
1980 for (i
= 0; i
< tempbufidx
; i
++)
1982 struct tempbuf_id_list
*idlist
= &index
[i
].idlist
;
1984 /* Fix the lookup list. */
1985 while (idlist
!= NULL
)
1987 if (idlist
->id
>= 0)
1988 lookup
[idlist
->id
] = &index
[i
];
1989 idlist
= idlist
->next
;
1992 index
[i
].seek
= lseek(fd
, 0, SEEK_CUR
);
1993 length
= strlen(index
[i
].str
) + 1;
1994 fe
.tag_length
= length
;
1995 fe
.idx_id
= index
[i
].idx_id
;
1997 /* Check the chunk alignment. */
1998 if ((fe
.tag_length
+ sizeof(struct tagfile_entry
))
1999 % TAGFILE_ENTRY_CHUNK_LENGTH
)
2001 fe
.tag_length
+= TAGFILE_ENTRY_CHUNK_LENGTH
-
2002 ((fe
.tag_length
+ sizeof(struct tagfile_entry
))
2003 % TAGFILE_ENTRY_CHUNK_LENGTH
);
2006 #ifdef TAGCACHE_STRICT_ALIGN
2007 /* Make sure the entry is long aligned. */
2008 if (index
[i
].seek
& 0x03)
2010 logf("tempbuf_sort: alignment error!");
2015 if (ecwrite(fd
, &fe
, 1, tagfile_entry_ec
, tc_stat
.econ
) !=
2016 sizeof(struct tagfile_entry
))
2018 logf("tempbuf_sort: write error #1");
2022 if (write(fd
, index
[i
].str
, length
) != length
)
2024 logf("tempbuf_sort: write error #2");
2028 /* Write some padding. */
2029 if (fe
.tag_length
- length
> 0)
2030 write(fd
, "XXXXXXXX", fe
.tag_length
- length
);
2036 inline static struct tempbuf_searchidx
* tempbuf_locate(int id
)
2038 if (id
< 0 || id
>= lookup_buffer_depth
)
2045 inline static int tempbuf_find_location(int id
)
2047 struct tempbuf_searchidx
*entry
;
2049 entry
= tempbuf_locate(id
);
2056 static bool build_numeric_indices(struct tagcache_header
*h
, int tmpfd
)
2058 struct master_header tcmh
;
2059 struct index_entry idx
;
2062 struct temp_file_entry
*entrybuf
= (struct temp_file_entry
*)tempbuf
;
2064 int entries_processed
= 0;
2066 char buf
[TAG_MAXLEN
];
2068 max_entries
= tempbuf_size
/ sizeof(struct temp_file_entry
) - 1;
2070 logf("Building numeric indices...");
2071 lseek(tmpfd
, sizeof(struct tagcache_header
), SEEK_SET
);
2073 if ( (masterfd
= open_master_fd(&tcmh
, true)) < 0)
2076 masterfd_pos
= lseek(masterfd
, tcmh
.tch
.entry_count
* sizeof(struct index_entry
),
2078 if (masterfd_pos
== filesize(masterfd
))
2080 logf("we can't append!");
2085 while (entries_processed
< h
->entry_count
)
2087 int count
= MIN(h
->entry_count
- entries_processed
, max_entries
);
2089 /* Read in as many entries as possible. */
2090 for (i
= 0; i
< count
; i
++)
2092 struct temp_file_entry
*tfe
= &entrybuf
[i
];
2095 /* Read in numeric data. */
2096 if (read(tmpfd
, tfe
, sizeof(struct temp_file_entry
)) !=
2097 sizeof(struct temp_file_entry
))
2099 logf("read fail #1");
2104 datastart
= lseek(tmpfd
, 0, SEEK_CUR
);
2107 * Read string data from the following tags:
2113 * A crc32 hash is calculated from the read data
2114 * and stored back to the data offset field kept in memory.
2116 #define tmpdb_read_string_tag(tag) \
2117 lseek(tmpfd, tfe->tag_offset[tag], SEEK_CUR); \
2118 if ((unsigned long)tfe->tag_length[tag] > sizeof buf) \
2120 logf("read fail: buffer overflow"); \
2125 if (read(tmpfd, buf, tfe->tag_length[tag]) != \
2126 tfe->tag_length[tag]) \
2128 logf("read fail #2"); \
2133 tfe->tag_offset[tag] = crc_32(buf, strlen(buf), 0xffffffff); \
2134 lseek(tmpfd, datastart, SEEK_SET)
2136 tmpdb_read_string_tag(tag_filename
);
2137 tmpdb_read_string_tag(tag_artist
);
2138 tmpdb_read_string_tag(tag_album
);
2139 tmpdb_read_string_tag(tag_title
);
2141 /* Seek to the end of the string data. */
2142 lseek(tmpfd
, tfe
->data_length
, SEEK_CUR
);
2145 /* Backup the master index position. */
2146 masterfd_pos
= lseek(masterfd
, 0, SEEK_CUR
);
2147 lseek(masterfd
, sizeof(struct master_header
), SEEK_SET
);
2149 /* Check if we can resurrect some deleted runtime statistics data. */
2150 for (i
= 0; i
< tcmh
.tch
.entry_count
; i
++)
2152 /* Read the index entry. */
2153 if (ecread(masterfd
, &idx
, 1, index_entry_ec
, tc_stat
.econ
)
2154 != sizeof(struct index_entry
))
2156 logf("read fail #3");
2162 * Skip unless the entry is marked as being deleted
2163 * or the data has already been resurrected.
2165 if (!(idx
.flag
& FLAG_DELETED
) || idx
.flag
& FLAG_RESURRECTED
)
2168 /* Now try to match the entry. */
2170 * To succesfully match a song, the following conditions
2173 * For numeric fields: tag_length
2174 * - Full identical match is required
2176 * If tag_filename matches, no further checking necessary.
2178 * For string hashes: tag_artist, tag_album, tag_title
2179 * - Two of these must match
2181 for (j
= 0; j
< count
; j
++)
2183 struct temp_file_entry
*tfe
= &entrybuf
[j
];
2185 /* Try to match numeric fields first. */
2186 if (tfe
->tag_offset
[tag_length
] != idx
.tag_seek
[tag_length
])
2189 /* Now it's time to do the hash matching. */
2190 if (tfe
->tag_offset
[tag_filename
] != idx
.tag_seek
[tag_filename
])
2192 int match_count
= 0;
2194 /* No filename match, check if we can match two other tags. */
2195 #define tmpdb_match(tag) \
2196 if (tfe->tag_offset[tag] == idx.tag_seek[tag]) \
2199 tmpdb_match(tag_artist
);
2200 tmpdb_match(tag_album
);
2201 tmpdb_match(tag_title
);
2203 if (match_count
< 2)
2205 /* Still no match found, give up. */
2210 /* A match found, now copy & resurrect the statistical data. */
2211 #define tmpdb_copy_tag(tag) \
2212 tfe->tag_offset[tag] = idx.tag_seek[tag]
2214 tmpdb_copy_tag(tag_playcount
);
2215 tmpdb_copy_tag(tag_rating
);
2216 tmpdb_copy_tag(tag_playtime
);
2217 tmpdb_copy_tag(tag_lastplayed
);
2218 tmpdb_copy_tag(tag_commitid
);
2220 /* Avoid processing this entry again. */
2221 idx
.flag
|= FLAG_RESURRECTED
;
2223 lseek(masterfd
, -sizeof(struct index_entry
), SEEK_CUR
);
2224 if (ecwrite(masterfd
, &idx
, 1, index_entry_ec
, tc_stat
.econ
)
2225 != sizeof(struct index_entry
))
2227 logf("masterfd writeback fail #1");
2232 logf("Entry resurrected");
2237 /* Restore the master index position. */
2238 lseek(masterfd
, masterfd_pos
, SEEK_SET
);
2240 /* Commit the data to the index. */
2241 for (i
= 0; i
< count
; i
++)
2243 int loc
= lseek(masterfd
, 0, SEEK_CUR
);
2245 if (ecread(masterfd
, &idx
, 1, index_entry_ec
, tc_stat
.econ
)
2246 != sizeof(struct index_entry
))
2248 logf("read fail #3");
2253 for (j
= 0; j
< TAG_COUNT
; j
++)
2255 if (!tagcache_is_numeric_tag(j
))
2258 idx
.tag_seek
[j
] = entrybuf
[i
].tag_offset
[j
];
2260 idx
.flag
= entrybuf
[i
].flag
;
2262 if (idx
.tag_seek
[tag_commitid
])
2264 /* Data has been resurrected. */
2265 idx
.flag
|= FLAG_DIRTYNUM
;
2267 else if (tc_stat
.ready
&& current_tcmh
.commitid
> 0)
2269 idx
.tag_seek
[tag_commitid
] = current_tcmh
.commitid
;
2270 idx
.flag
|= FLAG_DIRTYNUM
;
2273 /* Write back the updated index. */
2274 lseek(masterfd
, loc
, SEEK_SET
);
2275 if (ecwrite(masterfd
, &idx
, 1, index_entry_ec
, tc_stat
.econ
)
2276 != sizeof(struct index_entry
))
2284 entries_processed
+= count
;
2285 logf("%d/%ld entries processed", entries_processed
, h
->entry_count
);
2296 * == 0 temporary failure
2299 static int build_index(int index_type
, struct tagcache_header
*h
, int tmpfd
)
2302 struct tagcache_header tch
;
2303 struct master_header tcmh
;
2304 struct index_entry idxbuf
[IDX_BUF_DEPTH
];
2306 char buf
[TAG_MAXLEN
+32];
2307 int fd
= -1, masterfd
;
2312 logf("Building index: %d", index_type
);
2314 /* Check the number of entries we need to allocate ram for. */
2315 commit_entry_count
= h
->entry_count
+ 1;
2317 masterfd
= open_master_fd(&tcmh
, false);
2320 commit_entry_count
+= tcmh
.tch
.entry_count
;
2324 remove_files(); /* Just to be sure we are clean. */
2326 /* Open the index file, which contains the tag names. */
2327 fd
= open_tag_fd(&tch
, index_type
, true);
2330 logf("tch.datasize=%ld", tch
.datasize
);
2331 lookup_buffer_depth
= 1 +
2332 /* First part */ commit_entry_count
+
2333 /* Second part */ (tch
.datasize
/ TAGFILE_ENTRY_CHUNK_LENGTH
);
2337 lookup_buffer_depth
= 1 +
2338 /* First part */ commit_entry_count
+
2339 /* Second part */ 0;
2342 logf("lookup_buffer_depth=%ld", lookup_buffer_depth
);
2343 logf("commit_entry_count=%ld", commit_entry_count
);
2345 /* Allocate buffer for all index entries from both old and new
2348 tempbuf_pos
= commit_entry_count
* sizeof(struct tempbuf_searchidx
);
2350 /* Allocate lookup buffer. The first portion of commit_entry_count
2351 * contains the new tags in the temporary file and the second
2352 * part for locating entries already in the db.
2355 * +---------+---------------------------+
2356 * | index | position/ENTRY_CHUNK_SIZE | lookup buffer
2357 * +---------+---------------------------+
2359 * Old tags are inserted to a temporary buffer with position:
2360 * tempbuf_insert(position/ENTRY_CHUNK_SIZE, ...);
2361 * And new tags with index:
2362 * tempbuf_insert(idx, ...);
2364 * The buffer is sorted and written into tag file:
2365 * tempbuf_sort(...);
2366 * leaving master index locations messed up.
2368 * That is fixed using the lookup buffer for old tags:
2369 * new_seek = tempbuf_find_location(old_seek, ...);
2371 * new_seek = tempbuf_find_location(idx);
2373 lookup
= (struct tempbuf_searchidx
**)&tempbuf
[tempbuf_pos
];
2374 tempbuf_pos
+= lookup_buffer_depth
* sizeof(void **);
2375 memset(lookup
, 0, lookup_buffer_depth
* sizeof(void **));
2377 /* And calculate the remaining data space used mainly for storing
2378 * tag data (strings). */
2379 tempbuf_left
= tempbuf_size
- tempbuf_pos
- 8;
2380 if (tempbuf_left
- TAGFILE_ENTRY_AVG_LENGTH
* commit_entry_count
< 0)
2382 logf("Buffer way too small!");
2389 * If tag file contains unique tags (sorted index), we will load
2390 * it entirely into memory so we can resort it later for use with
2393 if (tagcache_is_sorted_tag(index_type
))
2395 logf("loading tags...");
2396 for (i
= 0; i
< tch
.entry_count
; i
++)
2398 struct tagfile_entry entry
;
2399 int loc
= lseek(fd
, 0, SEEK_CUR
);
2402 if (ecread(fd
, &entry
, 1, tagfile_entry_ec
, tc_stat
.econ
)
2403 != sizeof(struct tagfile_entry
))
2405 logf("read error #7");
2410 if (entry
.tag_length
>= (int)sizeof(buf
))
2412 logf("too long tag #3");
2417 if (read(fd
, buf
, entry
.tag_length
) != entry
.tag_length
)
2419 logf("read error #8");
2424 /* Skip deleted entries. */
2429 * Save the tag and tag id in the memory buffer. Tag id
2430 * is saved so we can later reindex the master lookup
2431 * table when the index gets resorted.
2433 ret
= tempbuf_insert(buf
, loc
/TAGFILE_ENTRY_CHUNK_LENGTH
2434 + commit_entry_count
, entry
.idx_id
,
2435 tagcache_is_unique_tag(index_type
));
2446 tempbufidx
= tch
.entry_count
;
2451 * Creating new index file to store the tags. No need to preload
2452 * anything whether the index type is sorted or not.
2454 snprintf(buf
, sizeof buf
, TAGCACHE_FILE_INDEX
, index_type
);
2455 fd
= open(buf
, O_WRONLY
| O_CREAT
| O_TRUNC
);
2458 logf("%s open fail", buf
);
2462 tch
.magic
= TAGCACHE_MAGIC
;
2463 tch
.entry_count
= 0;
2466 if (ecwrite(fd
, &tch
, 1, tagcache_header_ec
, tc_stat
.econ
)
2467 != sizeof(struct tagcache_header
))
2469 logf("header write failed");
2475 /* Loading the tag lookup file as "master file". */
2476 logf("Loading index file");
2477 masterfd
= open(TAGCACHE_FILE_MASTER
, O_RDWR
);
2481 logf("Creating new DB");
2482 masterfd
= open(TAGCACHE_FILE_MASTER
, O_WRONLY
| O_CREAT
| O_TRUNC
);
2486 logf("Failure to create index file (%s)", TAGCACHE_FILE_MASTER
);
2491 /* Write the header (write real values later). */
2492 memset(&tcmh
, 0, sizeof(struct master_header
));
2494 tcmh
.tch
.entry_count
= 0;
2495 tcmh
.tch
.datasize
= 0;
2497 ecwrite(masterfd
, &tcmh
, 1, master_header_ec
, tc_stat
.econ
);
2499 masterfd_pos
= lseek(masterfd
, 0, SEEK_CUR
);
2504 * Master file already exists so we need to process the current
2509 if (ecread(masterfd
, &tcmh
, 1, master_header_ec
, tc_stat
.econ
) !=
2510 sizeof(struct master_header
) || tcmh
.tch
.magic
!= TAGCACHE_MAGIC
)
2512 logf("header error");
2519 * If we reach end of the master file, we need to expand it to
2520 * hold new tags. If the current index is not sorted, we can
2521 * simply append new data to end of the file.
2522 * However, if the index is sorted, we need to update all tag
2523 * pointers in the master file for the current index.
2525 masterfd_pos
= lseek(masterfd
, tcmh
.tch
.entry_count
* sizeof(struct index_entry
),
2527 if (masterfd_pos
== filesize(masterfd
))
2529 logf("appending...");
2535 * Load new unique tags in memory to be sorted later and added
2536 * to the master lookup file.
2538 if (tagcache_is_sorted_tag(index_type
))
2540 lseek(tmpfd
, sizeof(struct tagcache_header
), SEEK_SET
);
2541 /* h is the header of the temporary file containing new tags. */
2542 logf("inserting new tags...");
2543 for (i
= 0; i
< h
->entry_count
; i
++)
2545 struct temp_file_entry entry
;
2547 if (read(tmpfd
, &entry
, sizeof(struct temp_file_entry
)) !=
2548 sizeof(struct temp_file_entry
))
2550 logf("read fail #3");
2556 if (entry
.tag_length
[index_type
] >= (long)sizeof(buf
))
2558 logf("too long entry!");
2563 lseek(tmpfd
, entry
.tag_offset
[index_type
], SEEK_CUR
);
2564 if (read(tmpfd
, buf
, entry
.tag_length
[index_type
]) !=
2565 entry
.tag_length
[index_type
])
2567 logf("read fail #4");
2572 if (tagcache_is_unique_tag(index_type
))
2573 error
= !tempbuf_insert(buf
, i
, -1, true);
2575 error
= !tempbuf_insert(buf
, i
, tcmh
.tch
.entry_count
+ i
, false);
2579 logf("insert error");
2584 lseek(tmpfd
, entry
.data_length
- entry
.tag_offset
[index_type
] -
2585 entry
.tag_length
[index_type
], SEEK_CUR
);
2590 /* Sort the buffer data and write it to the index file. */
2591 lseek(fd
, sizeof(struct tagcache_header
), SEEK_SET
);
2592 i
= tempbuf_sort(fd
);
2595 logf("sorted %d tags", i
);
2598 * Now update all indexes in the master lookup file.
2600 logf("updating indices...");
2601 lseek(masterfd
, sizeof(struct master_header
), SEEK_SET
);
2602 for (i
= 0; i
< tcmh
.tch
.entry_count
; i
+= idxbuf_pos
)
2605 int loc
= lseek(masterfd
, 0, SEEK_CUR
);
2607 idxbuf_pos
= MIN(tcmh
.tch
.entry_count
- i
, IDX_BUF_DEPTH
);
2609 if (ecread(masterfd
, idxbuf
, idxbuf_pos
, index_entry_ec
, tc_stat
.econ
)
2610 != (int)sizeof(struct index_entry
)*idxbuf_pos
)
2612 logf("read fail #5");
2616 lseek(masterfd
, loc
, SEEK_SET
);
2618 for (j
= 0; j
< idxbuf_pos
; j
++)
2620 if (idxbuf
[j
].flag
& FLAG_DELETED
)
2622 /* We can just ignore deleted entries. */
2623 // idxbuf[j].tag_seek[index_type] = 0;
2627 idxbuf
[j
].tag_seek
[index_type
] = tempbuf_find_location(
2628 idxbuf
[j
].tag_seek
[index_type
]/TAGFILE_ENTRY_CHUNK_LENGTH
2629 + commit_entry_count
);
2631 if (idxbuf
[j
].tag_seek
[index_type
] < 0)
2633 logf("update error: %d/%d/%d",
2634 idxbuf
[j
].flag
, i
+j
, tcmh
.tch
.entry_count
);
2642 /* Write back the updated index. */
2643 if (ecwrite(masterfd
, idxbuf
, idxbuf_pos
,
2644 index_entry_ec
, tc_stat
.econ
) !=
2645 (int)sizeof(struct index_entry
)*idxbuf_pos
)
2656 * Walk through the temporary file containing the new tags.
2658 // build_normal_index(h, tmpfd, masterfd, idx);
2659 logf("updating new indices...");
2660 lseek(masterfd
, masterfd_pos
, SEEK_SET
);
2661 lseek(tmpfd
, sizeof(struct tagcache_header
), SEEK_SET
);
2662 lseek(fd
, 0, SEEK_END
);
2663 for (i
= 0; i
< h
->entry_count
; i
+= idxbuf_pos
)
2667 idxbuf_pos
= MIN(h
->entry_count
- i
, IDX_BUF_DEPTH
);
2670 memset(idxbuf
, 0, sizeof(struct index_entry
)*IDX_BUF_DEPTH
);
2674 int loc
= lseek(masterfd
, 0, SEEK_CUR
);
2676 if (ecread(masterfd
, idxbuf
, idxbuf_pos
, index_entry_ec
, tc_stat
.econ
)
2677 != (int)sizeof(struct index_entry
)*idxbuf_pos
)
2679 logf("read fail #6");
2683 lseek(masterfd
, loc
, SEEK_SET
);
2686 /* Read entry headers. */
2687 for (j
= 0; j
< idxbuf_pos
; j
++)
2689 if (!tagcache_is_sorted_tag(index_type
))
2691 struct temp_file_entry entry
;
2692 struct tagfile_entry fe
;
2694 if (read(tmpfd
, &entry
, sizeof(struct temp_file_entry
)) !=
2695 sizeof(struct temp_file_entry
))
2697 logf("read fail #7");
2703 if (entry
.tag_length
[index_type
] >= (int)sizeof(buf
))
2705 logf("too long entry!");
2706 logf("length=%d", entry
.tag_length
[index_type
]);
2707 logf("pos=0x%02lx", lseek(tmpfd
, 0, SEEK_CUR
));
2712 lseek(tmpfd
, entry
.tag_offset
[index_type
], SEEK_CUR
);
2713 if (read(tmpfd
, buf
, entry
.tag_length
[index_type
]) !=
2714 entry
.tag_length
[index_type
])
2716 logf("read fail #8");
2717 logf("offset=0x%02lx", entry
.tag_offset
[index_type
]);
2718 logf("length=0x%02x", entry
.tag_length
[index_type
]);
2723 /* Write to index file. */
2724 idxbuf
[j
].tag_seek
[index_type
] = lseek(fd
, 0, SEEK_CUR
);
2725 fe
.tag_length
= entry
.tag_length
[index_type
];
2726 fe
.idx_id
= tcmh
.tch
.entry_count
+ i
+ j
;
2727 ecwrite(fd
, &fe
, 1, tagfile_entry_ec
, tc_stat
.econ
);
2728 write(fd
, buf
, fe
.tag_length
);
2732 lseek(tmpfd
, entry
.data_length
- entry
.tag_offset
[index_type
] -
2733 entry
.tag_length
[index_type
], SEEK_CUR
);
2737 /* Locate the correct entry from the sorted array. */
2738 idxbuf
[j
].tag_seek
[index_type
] = tempbuf_find_location(i
+ j
);
2739 if (idxbuf
[j
].tag_seek
[index_type
] < 0)
2741 logf("entry not found (%d)", j
);
2749 if (ecwrite(masterfd
, idxbuf
, idxbuf_pos
,
2750 index_entry_ec
, tc_stat
.econ
) !=
2751 (int)sizeof(struct index_entry
)*idxbuf_pos
)
2753 logf("tagcache: write fail #4");
2762 /* Finally write the header. */
2763 tch
.magic
= TAGCACHE_MAGIC
;
2764 tch
.entry_count
= tempbufidx
;
2765 tch
.datasize
= lseek(fd
, 0, SEEK_END
) - sizeof(struct tagcache_header
);
2766 lseek(fd
, 0, SEEK_SET
);
2767 ecwrite(fd
, &tch
, 1, tagcache_header_ec
, tc_stat
.econ
);
2769 if (index_type
!= tag_filename
)
2770 h
->datasize
+= tch
.datasize
;
2771 logf("s:%d/%ld/%ld", index_type
, tch
.datasize
, h
->datasize
);
2783 static bool commit(void)
2785 struct tagcache_header tch
;
2786 struct master_header tcmh
;
2790 #ifdef HAVE_DIRCACHE
2791 bool dircache_buffer_stolen
= false;
2793 bool local_allocation
= false;
2795 logf("committing tagcache");
2800 tmpfd
= open(TAGCACHE_FILE_TEMP
, O_RDONLY
);
2803 logf("nothing to commit");
2808 /* Load the header. */
2809 len
= sizeof(struct tagcache_header
);
2810 rc
= read(tmpfd
, &tch
, len
);
2812 if (tch
.magic
!= TAGCACHE_MAGIC
|| rc
!= len
)
2814 logf("incorrect header");
2816 remove(TAGCACHE_FILE_TEMP
);
2820 if (tch
.entry_count
== 0)
2822 logf("nothing to commit");
2824 remove(TAGCACHE_FILE_TEMP
);
2828 #ifdef HAVE_EEPROM_SETTINGS
2829 remove(TAGCACHE_STATEFILE
);
2832 /* At first be sure to unload the ramcache! */
2833 #ifdef HAVE_TC_RAMCACHE
2834 tc_stat
.ramcache
= false;
2839 /* Try to steal every buffer we can :) */
2840 if (tempbuf_size
== 0)
2841 local_allocation
= true;
2843 #ifdef HAVE_DIRCACHE
2844 if (tempbuf_size
== 0)
2846 /* Try to steal the dircache buffer. */
2847 tempbuf
= dircache_steal_buffer(&tempbuf_size
);
2848 tempbuf_size
&= ~0x03;
2850 if (tempbuf_size
> 0)
2852 dircache_buffer_stolen
= true;
2857 #ifdef HAVE_TC_RAMCACHE
2858 if (tempbuf_size
== 0 && tc_stat
.ramcache_allocated
> 0)
2860 tempbuf
= (char *)(hdr
+ 1);
2861 tempbuf_size
= tc_stat
.ramcache_allocated
- sizeof(struct ramcache_header
) - 128;
2862 tempbuf_size
&= ~0x03;
2866 /* And finally fail if there are no buffers available. */
2867 if (tempbuf_size
== 0)
2869 logf("delaying commit until next boot");
2870 tc_stat
.commit_delayed
= true;
2876 logf("commit %ld entries...", tch
.entry_count
);
2878 /* Mark DB dirty so it will stay disabled if commit fails. */
2879 current_tcmh
.dirty
= true;
2880 update_master_header();
2882 /* Now create the index files. */
2883 tc_stat
.commit_step
= 0;
2885 tc_stat
.commit_delayed
= false;
2887 for (i
= 0; i
< TAG_COUNT
; i
++)
2891 if (tagcache_is_numeric_tag(i
))
2894 tc_stat
.commit_step
++;
2895 ret
= build_index(i
, &tch
, tmpfd
);
2899 logf("tagcache failed init");
2903 tc_stat
.commit_delayed
= true;
2904 tc_stat
.commit_step
= 0;
2910 if (!build_numeric_indices(&tch
, tmpfd
))
2912 logf("Failure to commit numeric indices");
2915 tc_stat
.commit_step
= 0;
2921 tc_stat
.commit_step
= 0;
2923 /* Update the master index headers. */
2924 if ( (masterfd
= open_master_fd(&tcmh
, true)) < 0)
2930 tcmh
.tch
.entry_count
+= tch
.entry_count
;
2931 tcmh
.tch
.datasize
= sizeof(struct master_header
)
2932 + sizeof(struct index_entry
) * tcmh
.tch
.entry_count
2937 lseek(masterfd
, 0, SEEK_SET
);
2938 ecwrite(masterfd
, &tcmh
, 1, master_header_ec
, tc_stat
.econ
);
2941 logf("tagcache committed");
2942 remove(TAGCACHE_FILE_TEMP
);
2943 tc_stat
.ready
= check_all_headers();
2944 tc_stat
.readyvalid
= true;
2946 if (local_allocation
)
2952 #ifdef HAVE_DIRCACHE
2953 /* Rebuild the dircache, if we stole the buffer. */
2954 if (dircache_buffer_stolen
)
2958 #ifdef HAVE_TC_RAMCACHE
2959 /* Reload tagcache. */
2960 if (tc_stat
.ramcache_allocated
> 0)
2961 tagcache_start_scan();
2969 static void allocate_tempbuf(void)
2971 /* Yeah, malloc would be really nice now :) */
2973 tempbuf_size
= 32*1024*1024;
2974 tempbuf
= malloc(tempbuf_size
);
2976 tempbuf
= (char *)(((long)audiobuf
& ~0x03) + 0x04);
2977 tempbuf_size
= (long)audiobufend
- (long)audiobuf
- 4;
2978 audiobuf
+= tempbuf_size
;
2982 static void free_tempbuf(void)
2984 if (tempbuf_size
== 0)
2990 audiobuf
-= tempbuf_size
;
2996 static bool modify_numeric_entry(int masterfd
, int idx_id
, int tag
, long data
)
2998 struct index_entry idx
;
3003 if (!tagcache_is_numeric_tag(tag
))
3006 if (!get_index(masterfd
, idx_id
, &idx
, false))
3009 idx
.tag_seek
[tag
] = data
;
3010 idx
.flag
|= FLAG_DIRTYNUM
;
3012 return write_index(masterfd
, idx_id
, &idx
);
3016 bool tagcache_modify_numeric_entry(struct tagcache_search
*tcs
,
3019 struct master_header myhdr
;
3021 if (tcs
->masterfd
< 0)
3023 if ( (tcs
->masterfd
= open_master_fd(&myhdr
, true)) < 0)
3027 return modify_numeric_entry(tcs
->masterfd
, tcs
->idx_id
, tag
, data
);
3031 #define COMMAND_QUEUE_IS_EMPTY (command_queue_ridx == command_queue_widx)
3033 static bool command_queue_is_full(void)
3037 next
= command_queue_widx
+ 1;
3038 if (next
>= TAGCACHE_COMMAND_QUEUE_LENGTH
)
3041 return (next
== command_queue_ridx
);
3043 static bool command_queue_sync_callback(void)
3046 struct master_header myhdr
;
3049 mutex_lock(&command_queue_mutex
);
3051 if ( (masterfd
= open_master_fd(&myhdr
, true)) < 0)
3054 while (command_queue_ridx
!= command_queue_widx
)
3056 struct tagcache_command_entry
*ce
= &command_queue
[command_queue_ridx
];
3058 switch (ce
->command
)
3060 case CMD_UPDATE_MASTER_HEADER
:
3063 update_master_header();
3065 /* Re-open the masterfd. */
3066 if ( (masterfd
= open_master_fd(&myhdr
, true)) < 0)
3071 case CMD_UPDATE_NUMERIC
:
3073 modify_numeric_entry(masterfd
, ce
->idx_id
, ce
->tag
, ce
->data
);
3078 if (++command_queue_ridx
>= TAGCACHE_COMMAND_QUEUE_LENGTH
)
3079 command_queue_ridx
= 0;
3084 tc_stat
.queue_length
= 0;
3085 mutex_unlock(&command_queue_mutex
);
3089 static void run_command_queue(bool force
)
3091 if (COMMAND_QUEUE_IS_EMPTY
)
3094 if (force
|| command_queue_is_full())
3095 command_queue_sync_callback();
3097 register_storage_idle_func(command_queue_sync_callback
);
3100 static void queue_command(int cmd
, long idx_id
, int tag
, long data
)
3106 mutex_lock(&command_queue_mutex
);
3107 next
= command_queue_widx
+ 1;
3108 if (next
>= TAGCACHE_COMMAND_QUEUE_LENGTH
)
3111 /* Make sure queue is not full. */
3112 if (next
!= command_queue_ridx
)
3114 struct tagcache_command_entry
*ce
= &command_queue
[command_queue_widx
];
3117 ce
->idx_id
= idx_id
;
3121 command_queue_widx
= next
;
3123 tc_stat
.queue_length
++;
3125 mutex_unlock(&command_queue_mutex
);
3129 /* Queue is full, try again later... */
3130 mutex_unlock(&command_queue_mutex
);
3135 long tagcache_increase_serial(void)
3145 old
= current_tcmh
.serial
++;
3146 queue_command(CMD_UPDATE_MASTER_HEADER
, 0, 0, 0);
3151 void tagcache_update_numeric(int idx_id
, int tag
, long data
)
3153 queue_command(CMD_UPDATE_NUMERIC
, idx_id
, tag
, data
);
3156 long tagcache_get_serial(void)
3158 return current_tcmh
.serial
;
3161 long tagcache_get_commitid(void)
3163 return current_tcmh
.commitid
;
3166 static bool write_tag(int fd
, const char *tagstr
, const char *datastr
)
3171 snprintf(buf
, sizeof buf
, "%s=\"", tagstr
);
3172 for (i
= strlen(buf
); i
< (long)sizeof(buf
)-4; i
++)
3174 if (*datastr
== '\0')
3177 if (*datastr
== '"' || *datastr
== '\\')
3180 buf
[i
] = *(datastr
++);
3183 strcpy(&buf
[i
], "\" ");
3185 write(fd
, buf
, i
+ 2);
3190 static bool read_tag(char *dest
, long size
,
3191 const char *src
, const char *tagstr
)
3194 char current_tag
[32];
3196 while (*src
!= '\0')
3198 /* Skip all whitespace */
3206 /* Read in tag name */
3207 while (*src
!= '=' && *src
!= ' ')
3209 current_tag
[pos
] = *src
;
3213 if (*src
== '\0' || pos
>= (long)sizeof(current_tag
))
3216 current_tag
[pos
] = '\0';
3218 /* Read in tag data */
3220 /* Find the start. */
3221 while (*src
!= '"' && *src
!= '\0')
3224 if (*src
== '\0' || *(++src
) == '\0')
3227 /* Read the data. */
3228 for (pos
= 0; pos
< size
; pos
++)
3235 dest
[pos
] = *(src
+1);
3255 if (!strcasecmp(tagstr
, current_tag
))
3262 static int parse_changelog_line(int line_n
, const char *buf
, void *parameters
)
3264 struct index_entry idx
;
3265 char tag_data
[TAG_MAXLEN
+32];
3267 long masterfd
= (long)parameters
;
3268 const int import_tags
[] = { tag_playcount
, tag_rating
, tag_playtime
, tag_lastplayed
,
3276 logf("%d/%s", line_n
, buf
);
3277 if (!read_tag(tag_data
, sizeof tag_data
, buf
, "filename"))
3279 logf("filename missing");
3284 idx_id
= find_index(tag_data
);
3287 logf("entry not found");
3291 if (!get_index(masterfd
, idx_id
, &idx
, false))
3293 logf("failed to retrieve index entry");
3297 /* Stop if tag has already been modified. */
3298 if (idx
.flag
& FLAG_DIRTYNUM
)
3301 logf("import: %s", tag_data
);
3303 idx
.flag
|= FLAG_DIRTYNUM
;
3304 for (i
= 0; i
< (long)(sizeof(import_tags
)/sizeof(import_tags
[0])); i
++)
3308 if (!read_tag(tag_data
, sizeof tag_data
, buf
,
3309 tagcache_tag_to_str(import_tags
[i
])))
3314 data
= atoi(tag_data
);
3318 idx
.tag_seek
[import_tags
[i
]] = data
;
3320 if (import_tags
[i
] == tag_lastplayed
&& data
>= current_tcmh
.serial
)
3321 current_tcmh
.serial
= data
+ 1;
3322 else if (import_tags
[i
] == tag_commitid
&& data
>= current_tcmh
.commitid
)
3323 current_tcmh
.commitid
= data
+ 1;
3326 return write_index(masterfd
, idx_id
, &idx
) ? 0 : -5;
3330 bool tagcache_import_changelog(void)
3332 struct master_header myhdr
;
3333 struct tagcache_header tch
;
3344 clfd
= open(TAGCACHE_FILE_CHANGELOG
, O_RDONLY
);
3347 logf("failure to open changelog");
3351 if ( (masterfd
= open_master_fd(&myhdr
, true)) < 0)
3359 filenametag_fd
= open_tag_fd(&tch
, tag_filename
, false);
3361 fast_readline(clfd
, buf
, sizeof buf
, (long *)masterfd
,
3362 parse_changelog_line
);
3367 if (filenametag_fd
>= 0)
3368 close(filenametag_fd
);
3372 update_master_header();
3378 bool tagcache_create_changelog(struct tagcache_search
*tcs
)
3380 struct master_header myhdr
;
3381 struct index_entry idx
;
3382 char buf
[TAG_MAXLEN
+32];
3390 if (!tagcache_search(tcs
, tag_filename
))
3393 /* Initialize the changelog */
3394 clfd
= open(TAGCACHE_FILE_CHANGELOG
, O_WRONLY
| O_CREAT
| O_TRUNC
);
3397 logf("failure to open changelog");
3401 if (tcs
->masterfd
< 0)
3403 if ( (tcs
->masterfd
= open_master_fd(&myhdr
, false)) < 0)
3408 lseek(tcs
->masterfd
, 0, SEEK_SET
);
3409 ecread(tcs
->masterfd
, &myhdr
, 1, master_header_ec
, tc_stat
.econ
);
3412 write(clfd
, "## Changelog version 1\n", 23);
3414 for (i
= 0; i
< myhdr
.tch
.entry_count
; i
++)
3416 if (ecread(tcs
->masterfd
, &idx
, 1, index_entry_ec
, tc_stat
.econ
)
3417 != sizeof(struct index_entry
))
3419 logf("read error #9");
3420 tagcache_search_finish(tcs
);
3425 /* Skip until the entry found has been modified. */
3426 if (! (idx
.flag
& FLAG_DIRTYNUM
) )
3429 /* Skip deleted entries too. */
3430 if (idx
.flag
& FLAG_DELETED
)
3433 /* Now retrieve all tags. */
3434 for (j
= 0; j
< TAG_COUNT
; j
++)
3436 if (tagcache_is_numeric_tag(j
))
3438 snprintf(temp
, sizeof temp
, "%d", idx
.tag_seek
[j
]);
3439 write_tag(clfd
, tagcache_tag_to_str(j
), temp
);
3444 tagcache_retrieve(tcs
, i
, tcs
->type
, buf
, sizeof buf
);
3445 write_tag(clfd
, tagcache_tag_to_str(j
), buf
);
3448 write(clfd
, "\n", 1);
3454 tagcache_search_finish(tcs
);
3459 static bool delete_entry(long idx_id
)
3464 struct index_entry idx
, myidx
;
3465 struct master_header myhdr
;
3466 char buf
[TAG_MAXLEN
+32];
3467 int in_use
[TAG_COUNT
];
3469 logf("delete_entry(): %ld", idx_id
);
3471 #ifdef HAVE_TC_RAMCACHE
3472 /* At first mark the entry removed from ram cache. */
3473 if (tc_stat
.ramcache
)
3474 hdr
->indices
[idx_id
].flag
|= FLAG_DELETED
;
3477 if ( (masterfd
= open_master_fd(&myhdr
, true) ) < 0)
3480 lseek(masterfd
, idx_id
* sizeof(struct index_entry
), SEEK_CUR
);
3481 if (ecread(masterfd
, &myidx
, 1, index_entry_ec
, tc_stat
.econ
)
3482 != sizeof(struct index_entry
))
3484 logf("delete_entry(): read error");
3488 if (myidx
.flag
& FLAG_DELETED
)
3490 logf("delete_entry(): already deleted!");
3494 myidx
.flag
|= FLAG_DELETED
;
3495 lseek(masterfd
, -sizeof(struct index_entry
), SEEK_CUR
);
3496 if (ecwrite(masterfd
, &myidx
, 1, index_entry_ec
, tc_stat
.econ
)
3497 != sizeof(struct index_entry
))
3499 logf("delete_entry(): write_error #1");
3503 /* Now check which tags are no longer in use (if any) */
3504 for (tag
= 0; tag
< TAG_COUNT
; tag
++)
3507 lseek(masterfd
, sizeof(struct master_header
), SEEK_SET
);
3508 for (i
= 0; i
< myhdr
.tch
.entry_count
; i
++)
3510 struct index_entry
*idxp
;
3512 #ifdef HAVE_TC_RAMCACHE
3513 /* Use RAM DB if available for greater speed */
3514 if (tc_stat
.ramcache
)
3515 idxp
= &hdr
->indices
[i
];
3519 if (ecread(masterfd
, &idx
, 1, index_entry_ec
, tc_stat
.econ
)
3520 != sizeof(struct index_entry
))
3522 logf("delete_entry(): read error #2");
3528 if (idxp
->flag
& FLAG_DELETED
)
3531 for (tag
= 0; tag
< TAG_COUNT
; tag
++)
3533 if (tagcache_is_numeric_tag(tag
))
3536 if (idxp
->tag_seek
[tag
] == myidx
.tag_seek
[tag
])
3541 /* Now delete all tags no longer in use. */
3542 for (tag
= 0; tag
< TAG_COUNT
; tag
++)
3544 struct tagcache_header tch
;
3545 int oldseek
= myidx
.tag_seek
[tag
];
3547 if (tagcache_is_numeric_tag(tag
))
3551 * Replace tag seek with a hash value of the field string data.
3552 * That way runtime statistics of moved or altered files can be
3555 #ifdef HAVE_TC_RAMCACHE
3556 if (tc_stat
.ramcache
&& tag
!= tag_filename
)
3558 struct tagfile_entry
*tfe
;
3559 int32_t *seek
= &hdr
->indices
[idx_id
].tag_seek
[tag
];
3561 tfe
= (struct tagfile_entry
*)&hdr
->tags
[tag
][*seek
];
3562 *seek
= crc_32(tfe
->tag_data
, strlen(tfe
->tag_data
), 0xffffffff);
3563 myidx
.tag_seek
[tag
] = *seek
;
3568 struct tagfile_entry tfe
;
3570 /* Open the index file, which contains the tag names. */
3571 if ((fd
= open_tag_fd(&tch
, tag
, true)) < 0)
3574 /* Skip the header block */
3575 lseek(fd
, myidx
.tag_seek
[tag
], SEEK_SET
);
3576 if (ecread(fd
, &tfe
, 1, tagfile_entry_ec
, tc_stat
.econ
)
3577 != sizeof(struct tagfile_entry
))
3579 logf("delete_entry(): read error #3");
3583 if (read(fd
, buf
, tfe
.tag_length
) != tfe
.tag_length
)
3585 logf("delete_entry(): read error #4");
3589 myidx
.tag_seek
[tag
] = crc_32(buf
, strlen(buf
), 0xffffffff);
3594 logf("in use: %d/%d", tag
, in_use
[tag
]);
3603 #ifdef HAVE_TC_RAMCACHE
3604 /* Delete from ram. */
3605 if (tc_stat
.ramcache
&& tag
!= tag_filename
)
3607 struct tagfile_entry
*tagentry
= (struct tagfile_entry
*)&hdr
->tags
[tag
][oldseek
];
3608 tagentry
->tag_data
[0] = '\0';
3612 /* Open the index file, which contains the tag names. */
3615 if ((fd
= open_tag_fd(&tch
, tag
, true)) < 0)
3619 /* Skip the header block */
3620 lseek(fd
, oldseek
+ sizeof(struct tagfile_entry
), SEEK_SET
);
3622 /* Debug, print 10 first characters of the tag
3625 logf("TAG:%s", buf);
3626 lseek(fd, -10, SEEK_CUR);
3629 /* Write first data byte in tag as \0 */
3632 /* Now tag data has been removed */
3637 /* Write index entry back into master index. */
3638 lseek(masterfd
, sizeof(struct master_header
) +
3639 (idx_id
* sizeof(struct index_entry
)), SEEK_SET
);
3640 if (ecwrite(masterfd
, &myidx
, 1, index_entry_ec
, tc_stat
.econ
)
3641 != sizeof(struct index_entry
))
3643 logf("delete_entry(): write_error #2");
3662 * Returns true if there is an event waiting in the queue
3663 * that requires the current operation to be aborted.
3665 static bool check_event_queue(void)
3667 struct queue_event ev
;
3669 queue_wait_w_tmo(&tagcache_queue
, &ev
, 0);
3674 case SYS_USB_CONNECTED
:
3675 /* Put the event back into the queue. */
3676 queue_post(&tagcache_queue
, ev
.id
, ev
.data
);
3684 #ifdef HAVE_TC_RAMCACHE
3685 static bool allocate_tagcache(void)
3687 struct master_header tcmh
;
3690 /* Load the header. */
3691 if ( (fd
= open_master_fd(&tcmh
, false)) < 0)
3700 * Now calculate the required cache size plus
3701 * some extra space for alignment fixes.
3703 tc_stat
.ramcache_allocated
= tcmh
.tch
.datasize
+ 128 + TAGCACHE_RESERVE
+
3704 sizeof(struct ramcache_header
) + TAG_COUNT
*sizeof(void *);
3705 hdr
= buffer_alloc(tc_stat
.ramcache_allocated
+ 128);
3706 memset(hdr
, 0, sizeof(struct ramcache_header
));
3707 memcpy(&hdr
->h
, &tcmh
, sizeof(struct master_header
));
3708 hdr
->indices
= (struct index_entry
*)(hdr
+ 1);
3709 logf("tagcache: %d bytes allocated.", tc_stat
.ramcache_allocated
);
3714 # ifdef HAVE_EEPROM_SETTINGS
3715 static bool tagcache_dumpload(void)
3717 struct statefile_header shdr
;
3722 fd
= open(TAGCACHE_STATEFILE
, O_RDONLY
);
3725 logf("no tagcache statedump");
3729 /* Check the statefile memory placement */
3730 hdr
= buffer_alloc(0);
3731 rc
= read(fd
, &shdr
, sizeof(struct statefile_header
));
3732 if (rc
!= sizeof(struct statefile_header
)
3733 /* || (long)hdr != (long)shdr.hdr */)
3735 logf("incorrect statefile");
3741 offpos
= (long)hdr
- (long)shdr
.hdr
;
3743 /* Lets allocate real memory and load it */
3744 hdr
= buffer_alloc(shdr
.tc_stat
.ramcache_allocated
);
3745 rc
= read(fd
, hdr
, shdr
.tc_stat
.ramcache_allocated
);
3748 if (rc
!= shdr
.tc_stat
.ramcache_allocated
)
3750 logf("read failure!");
3755 memcpy(&tc_stat
, &shdr
.tc_stat
, sizeof(struct tagcache_stat
));
3757 /* Now fix the pointers */
3758 hdr
->indices
= (struct index_entry
*)((long)hdr
->indices
+ offpos
);
3759 for (i
= 0; i
< TAG_COUNT
; i
++)
3760 hdr
->tags
[i
] += offpos
;
3765 static bool tagcache_dumpsave(void)
3767 struct statefile_header shdr
;
3770 if (!tc_stat
.ramcache
)
3773 fd
= open(TAGCACHE_STATEFILE
, O_WRONLY
| O_CREAT
| O_TRUNC
);
3776 logf("failed to create a statedump");
3780 /* Create the header */
3782 memcpy(&shdr
.tc_stat
, &tc_stat
, sizeof(struct tagcache_stat
));
3783 write(fd
, &shdr
, sizeof(struct statefile_header
));
3785 /* And dump the data too */
3786 write(fd
, hdr
, tc_stat
.ramcache_allocated
);
3793 static bool load_tagcache(void)
3795 struct tagcache_header
*tch
;
3796 long bytesleft
= tc_stat
.ramcache_allocated
;
3797 struct index_entry
*idx
;
3802 # ifdef HAVE_DIRCACHE
3803 while (dircache_is_initializing())
3806 dircache_set_appflag(DIRCACHE_APPFLAG_TAGCACHE
);
3809 logf("loading tagcache to ram...");
3811 fd
= open(TAGCACHE_FILE_MASTER
, O_RDONLY
);
3814 logf("tagcache open failed");
3818 if (ecread(fd
, &hdr
->h
, 1, master_header_ec
, tc_stat
.econ
)
3819 != sizeof(struct master_header
)
3820 || hdr
->h
.tch
.magic
!= TAGCACHE_MAGIC
)
3822 logf("incorrect header");
3828 /* Load the master index table. */
3829 for (i
= 0; i
< hdr
->h
.tch
.entry_count
; i
++)
3831 rc
= ecread(fd
, idx
, 1, index_entry_ec
, tc_stat
.econ
);
3832 if (rc
!= sizeof(struct index_entry
))
3834 logf("read error #10");
3839 bytesleft
-= sizeof(struct index_entry
);
3840 if (bytesleft
< 0 || ((long)idx
- (long)hdr
->indices
) >= tc_stat
.ramcache_allocated
)
3842 logf("too big tagcache.");
3852 /* Load the tags. */
3854 for (tag
= 0; tag
< TAG_COUNT
; tag
++)
3856 struct tagfile_entry
*fe
;
3857 char buf
[TAG_MAXLEN
+32];
3859 if (tagcache_is_numeric_tag(tag
))
3862 //p = ((void *)p+1);
3863 p
= (char *)((long)p
& ~0x03) + 0x04;
3866 /* Check the header. */
3867 tch
= (struct tagcache_header
*)p
;
3868 p
+= sizeof(struct tagcache_header
);
3870 if ( (fd
= open_tag_fd(tch
, tag
, false)) < 0)
3873 for (hdr
->entry_count
[tag
] = 0;
3874 hdr
->entry_count
[tag
] < tch
->entry_count
;
3875 hdr
->entry_count
[tag
]++)
3879 if (do_timed_yield())
3881 /* Abort if we got a critical event in queue */
3882 if (check_event_queue())
3886 fe
= (struct tagfile_entry
*)p
;
3887 pos
= lseek(fd
, 0, SEEK_CUR
);
3888 rc
= ecread(fd
, fe
, 1, tagfile_entry_ec
, tc_stat
.econ
);
3889 if (rc
!= sizeof(struct tagfile_entry
))
3891 /* End of lookup table. */
3892 logf("read error #11");
3897 /* We have a special handling for the filename tags. */
3898 if (tag
== tag_filename
)
3900 # ifdef HAVE_DIRCACHE
3901 const struct dirent
*dc
;
3904 // FIXME: This is wrong!
3905 // idx = &hdr->indices[hdr->entry_count[i]];
3906 idx
= &hdr
->indices
[fe
->idx_id
];
3908 if (fe
->tag_length
>= (long)sizeof(buf
)-1)
3912 logf("TAG:%s", buf
);
3913 logf("too long filename");
3918 rc
= read(fd
, buf
, fe
->tag_length
);
3919 if (rc
!= fe
->tag_length
)
3921 logf("read error #12");
3926 /* Check if the entry has already been removed */
3927 if (idx
->flag
& FLAG_DELETED
)
3930 /* This flag must not be used yet. */
3931 if (idx
->flag
& FLAG_DIRCACHE
)
3933 logf("internal error!");
3938 if (idx
->tag_seek
[tag
] != pos
)
3940 logf("corrupt data structures!");
3945 # ifdef HAVE_DIRCACHE
3946 if (dircache_is_enabled())
3948 dc
= dircache_get_entry_ptr(buf
);
3951 logf("Entry no longer valid.");
3953 delete_entry(fe
->idx_id
);
3957 idx
->flag
|= FLAG_DIRCACHE
;
3958 FLAG_SET_ATTR(idx
->flag
, fe
->idx_id
);
3959 idx
->tag_seek
[tag_filename
] = (long)dc
;
3964 /* This will be very slow unless dircache is enabled
3965 or target is flash based, but do it anyway for
3967 /* Check if entry has been removed. */
3968 if (global_settings
.tagcache_autoupdate
)
3970 if (!file_exists(buf
))
3972 logf("Entry no longer valid.");
3974 delete_entry(fe
->idx_id
);
3983 bytesleft
-= sizeof(struct tagfile_entry
) + fe
->tag_length
;
3986 logf("too big tagcache #2");
3987 logf("tl: %d", fe
->tag_length
);
3988 logf("bl: %ld", bytesleft
);
3994 rc
= read(fd
, fe
->tag_data
, fe
->tag_length
);
3997 if (rc
!= fe
->tag_length
)
3999 logf("read error #13");
4000 logf("rc=0x%04x", rc
); // 0x431
4001 logf("len=0x%04x", fe
->tag_length
); // 0x4000
4002 logf("pos=0x%04lx", lseek(fd
, 0, SEEK_CUR
)); // 0x433
4003 logf("tag=0x%02x", tag
); // 0x00
4011 tc_stat
.ramcache_used
= tc_stat
.ramcache_allocated
- bytesleft
;
4012 logf("tagcache loaded into ram!");
4016 #endif /* HAVE_TC_RAMCACHE */
4018 static bool check_deleted_files(void)
4021 char buf
[TAG_MAXLEN
+32];
4022 struct tagfile_entry tfe
;
4024 logf("reverse scan...");
4025 snprintf(buf
, sizeof buf
, TAGCACHE_FILE_INDEX
, tag_filename
);
4026 fd
= open(buf
, O_RDONLY
);
4030 logf("%s open fail", buf
);
4034 lseek(fd
, sizeof(struct tagcache_header
), SEEK_SET
);
4035 while (ecread(fd
, &tfe
, 1, tagfile_entry_ec
, tc_stat
.econ
)
4036 == sizeof(struct tagfile_entry
)
4038 && !check_event_queue()
4042 if (tfe
.tag_length
>= (long)sizeof(buf
)-1)
4044 logf("too long tag");
4049 if (read(fd
, buf
, tfe
.tag_length
) != tfe
.tag_length
)
4051 logf("read error #14");
4056 /* Check if the file has already deleted from the db. */
4060 /* Now check if the file exists. */
4061 if (!file_exists(buf
))
4063 logf("Entry no longer valid.");
4064 logf("-> %s / %d", buf
, tfe
.tag_length
);
4065 delete_entry(tfe
.idx_id
);
4076 static bool check_dir(const char *dirname
, int add_files
)
4080 int success
= false;
4081 int ignore
, unignore
;
4082 char newpath
[MAX_PATH
];
4084 dir
= opendir(dirname
);
4087 logf("tagcache: opendir() failed");
4091 /* check for a database.ignore file */
4092 snprintf(newpath
, MAX_PATH
, "%s/database.ignore", dirname
);
4093 ignore
= file_exists(newpath
);
4094 /* check for a database.unignore file */
4095 snprintf(newpath
, MAX_PATH
, "%s/database.unignore", dirname
);
4096 unignore
= file_exists(newpath
);
4098 /* don't do anything if both ignore and unignore are there */
4099 if (ignore
!= unignore
)
4100 add_files
= unignore
;
4102 /* Recursively scan the dir. */
4106 while (!check_event_queue())
4109 struct dirent
*entry
;
4111 entry
= readdir(dir
);
4119 if (!strcmp((char *)entry
->d_name
, ".") ||
4120 !strcmp((char *)entry
->d_name
, ".."))
4125 len
= strlen(curpath
);
4126 snprintf(&curpath
[len
], sizeof(curpath
) - len
, "/%s",
4129 processed_dir_count
++;
4130 if (entry
->attribute
& ATTR_DIRECTORY
)
4131 check_dir(curpath
, add_files
);
4134 tc_stat
.curentry
= curpath
;
4136 /* Add a new entry to the temporary db file. */
4137 add_tagcache(curpath
, (entry
->wrtdate
<< 16) | entry
->wrttime
4138 #if defined(HAVE_TC_RAMCACHE) && defined(HAVE_DIRCACHE)
4139 , dir
->internal_entry
4143 /* Wait until current path for debug screen is read and unset. */
4144 while (tc_stat
.syncscreen
&& tc_stat
.curentry
!= NULL
)
4147 tc_stat
.curentry
= NULL
;
4150 curpath
[len
] = '\0';
4158 void tagcache_screensync_event(void)
4160 tc_stat
.curentry
= NULL
;
4163 void tagcache_screensync_enable(bool state
)
4165 tc_stat
.syncscreen
= state
;
4168 void tagcache_build(const char *path
)
4170 struct tagcache_header header
;
4175 total_entry_count
= 0;
4176 processed_dir_count
= 0;
4178 #ifdef HAVE_DIRCACHE
4179 while (dircache_is_initializing())
4183 logf("updating tagcache");
4185 cachefd
= open(TAGCACHE_FILE_TEMP
, O_RDONLY
);
4188 logf("skipping, cache already waiting for commit");
4193 cachefd
= open(TAGCACHE_FILE_TEMP
, O_RDWR
| O_CREAT
| O_TRUNC
);
4196 logf("master file open failed: %s", TAGCACHE_FILE_TEMP
);
4200 filenametag_fd
= open_tag_fd(&header
, tag_filename
, false);
4204 logf("Scanning files...");
4205 /* Scan for new files. */
4206 memset(&header
, 0, sizeof(struct tagcache_header
));
4207 write(cachefd
, &header
, sizeof(struct tagcache_header
));
4209 if (strcmp("/", path
) != 0)
4210 strcpy(curpath
, path
);
4211 ret
= check_dir(path
, true);
4213 /* Write the header. */
4214 header
.magic
= TAGCACHE_MAGIC
;
4215 header
.datasize
= data_size
;
4216 header
.entry_count
= total_entry_count
;
4217 lseek(cachefd
, 0, SEEK_SET
);
4218 write(cachefd
, &header
, sizeof(struct tagcache_header
));
4221 if (filenametag_fd
>= 0)
4223 close(filenametag_fd
);
4224 filenametag_fd
= -1;
4234 /* Commit changes to the database. */
4240 remove(TAGCACHE_FILE_TEMP
);
4241 logf("tagcache built!");
4247 #ifdef HAVE_TC_RAMCACHE
4250 /* Import runtime statistics if we just initialized the db. */
4251 if (hdr
->h
.serial
== 0)
4252 queue_post(&tagcache_queue
, Q_IMPORT_CHANGELOG
, 0);
4259 #ifdef HAVE_TC_RAMCACHE
4260 static void load_ramcache(void)
4267 /* At first we should load the cache (if exists). */
4268 tc_stat
.ramcache
= load_tagcache();
4270 if (!tc_stat
.ramcache
)
4272 /* If loading failed, it must indicate some problem with the db
4273 * so disable it entirely to prevent further issues. */
4274 tc_stat
.ready
= false;
4281 void tagcache_unload_ramcache(void)
4283 tc_stat
.ramcache
= false;
4284 /* Just to make sure there is no statefile present. */
4285 // remove(TAGCACHE_STATEFILE);
4290 static void tagcache_thread(void)
4292 struct queue_event ev
;
4293 bool check_done
= false;
4295 /* If the previous cache build/update was interrupted, commit
4296 * the changes first in foreground. */
4302 #ifdef HAVE_TC_RAMCACHE
4303 # ifdef HAVE_EEPROM_SETTINGS
4304 if (firmware_settings
.initialized
&& firmware_settings
.disk_clean
)
4305 check_done
= tagcache_dumpload();
4307 remove(TAGCACHE_STATEFILE
);
4310 /* Allocate space for the tagcache if found on disk. */
4311 if (global_settings
.tagcache_ram
&& !tc_stat
.ramcache
)
4312 allocate_tagcache();
4316 tc_stat
.initialized
= true;
4318 /* Don't delay bootup with the header check but do it on background. */
4320 tc_stat
.ready
= check_all_headers();
4321 tc_stat
.readyvalid
= true;
4325 run_command_queue(false);
4327 queue_wait_w_tmo(&tagcache_queue
, &ev
, HZ
);
4331 case Q_IMPORT_CHANGELOG
:
4332 tagcache_import_changelog();
4337 remove(TAGCACHE_FILE_TEMP
);
4338 tagcache_build("/");
4342 tagcache_build("/");
4343 #ifdef HAVE_TC_RAMCACHE
4346 check_deleted_files();
4352 if (check_done
|| !tc_stat
.ready
)
4355 #ifdef HAVE_TC_RAMCACHE
4356 if (!tc_stat
.ramcache
&& global_settings
.tagcache_ram
)
4359 if (tc_stat
.ramcache
&& global_settings
.tagcache_autoupdate
)
4360 tagcache_build("/");
4364 if (global_settings
.tagcache_autoupdate
)
4366 tagcache_build("/");
4368 /* This will be very slow unless dircache is enabled
4369 or target is flash based, but do it anyway for
4371 check_deleted_files();
4374 logf("tagcache check done");
4386 case SYS_USB_CONNECTED
:
4387 logf("USB: TagCache");
4388 usb_acknowledge(SYS_USB_CONNECTED_ACK
);
4389 usb_wait_for_disconnect(&tagcache_queue
);
4396 bool tagcache_prepare_shutdown(void)
4398 if (tagcache_get_commit_step() > 0)
4401 tagcache_stop_scan();
4402 while (read_lock
|| write_lock
)
4408 void tagcache_shutdown(void)
4410 /* Flush the command queue. */
4411 run_command_queue(true);
4413 #ifdef HAVE_EEPROM_SETTINGS
4414 if (tc_stat
.ramcache
)
4415 tagcache_dumpsave();
4419 static int get_progress(void)
4421 int total_count
= -1;
4423 #ifdef HAVE_DIRCACHE
4424 if (dircache_is_enabled())
4426 total_count
= dircache_get_entry_count();
4430 #ifdef HAVE_TC_RAMCACHE
4432 if (hdr
&& tc_stat
.ramcache
)
4433 total_count
= hdr
->h
.tch
.entry_count
;
4437 if (total_count
< 0)
4440 return processed_dir_count
* 100 / total_count
;
4443 struct tagcache_stat
* tagcache_get_stat(void)
4445 tc_stat
.progress
= get_progress();
4446 tc_stat
.processed_entries
= processed_dir_count
;
4451 void tagcache_start_scan(void)
4453 queue_post(&tagcache_queue
, Q_START_SCAN
, 0);
4456 bool tagcache_update(void)
4461 queue_post(&tagcache_queue
, Q_UPDATE
, 0);
4465 bool tagcache_rebuild()
4467 queue_post(&tagcache_queue
, Q_REBUILD
, 0);
4471 void tagcache_stop_scan(void)
4473 queue_post(&tagcache_queue
, Q_STOP_SCAN
, 0);
4476 #ifdef HAVE_TC_RAMCACHE
4477 bool tagcache_is_ramcache(void)
4479 return tc_stat
.ramcache
;
4483 #endif /* !__PCTOOL__ */
4486 void tagcache_init(void)
4488 memset(&tc_stat
, 0, sizeof(struct tagcache_stat
));
4489 memset(¤t_tcmh
, 0, sizeof(struct master_header
));
4490 filenametag_fd
= -1;
4491 write_lock
= read_lock
= 0;
4494 mutex_init(&command_queue_mutex
);
4495 queue_init(&tagcache_queue
, true);
4496 create_thread(tagcache_thread
, tagcache_stack
,
4497 sizeof(tagcache_stack
), 0, tagcache_thread_name
4498 IF_PRIO(, PRIORITY_BACKGROUND
)
4501 tc_stat
.initialized
= true;
4505 tc_stat
.ready
= check_all_headers();
4510 void tagcache_reverse_scan(void)
4512 logf("Checking for deleted files");
4513 check_deleted_files();
4517 bool tagcache_is_initialized(void)
4519 return tc_stat
.initialized
;
4521 bool tagcache_is_usable(void)
4523 return tc_stat
.initialized
&& tc_stat
.ready
;
4525 int tagcache_get_commit_step(void)
4527 return tc_stat
.commit_step
;
4529 int tagcache_get_max_commit_step(void)
4531 return (int)(sizeof(sorted_tags
)/sizeof(sorted_tags
[0]))+1;