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 * +------------------+ |
62 #include "ata_idle_notify.h"
83 #include "eeprom_settings.h"
87 #define yield() do { } while(0)
88 #define sim_sleep(timeout) do { } while(0)
89 #define do_timed_yield() do { } while(0)
93 /* Tag Cache thread. */
94 static struct event_queue tagcache_queue
;
95 static long tagcache_stack
[(DEFAULT_STACK_SIZE
+ 0x4000)/sizeof(long)];
96 static const char tagcache_thread_name
[] = "tagcache";
99 #define UNTAGGED "<Untagged>"
101 /* Previous path when scanning directory tree recursively. */
102 static char curpath
[TAG_MAXLEN
+32];
103 static long curpath_size
= sizeof(curpath
);
105 /* Used when removing duplicates. */
106 static char *tempbuf
; /* Allocated when needed. */
107 static long tempbufidx
; /* Current location in buffer. */
108 static long tempbuf_size
; /* Buffer size (TEMPBUF_SIZE). */
109 static long tempbuf_left
; /* Buffer space left. */
110 static long tempbuf_pos
;
112 /* Tags we want to get sorted (loaded to the tempbuf). */
113 static const int sorted_tags
[] = { tag_artist
, tag_album
, tag_genre
,
114 tag_composer
, tag_comment
, tag_albumartist
, tag_grouping
, tag_title
};
116 /* Uniqued tags (we can use these tags with filters and conditional clauses). */
117 static const int unique_tags
[] = { tag_artist
, tag_album
, tag_genre
,
118 tag_composer
, tag_comment
, tag_albumartist
, tag_grouping
};
120 /* Numeric tags (we can use these tags with conditional clauses). */
121 static const int numeric_tags
[] = { tag_year
, tag_discnumber
,
122 tag_tracknumber
, tag_length
, tag_bitrate
, tag_playcount
, tag_rating
,
123 tag_playtime
, tag_lastplayed
, tag_commitid
, tag_mtime
,
124 tag_virt_length_min
, tag_virt_length_sec
,
125 tag_virt_playtime_min
, tag_virt_playtime_sec
,
126 tag_virt_entryage
, tag_virt_autoscore
};
128 /* String presentation of the tags defined in tagcache.h. Must be in correct order! */
129 static const char *tags_str
[] = { "artist", "album", "genre", "title",
130 "filename", "composer", "comment", "albumartist", "grouping", "year",
131 "discnumber", "tracknumber", "bitrate", "length", "playcount", "rating",
132 "playtime", "lastplayed", "commitid", "mtime" };
134 /* Status information of the tagcache. */
135 static struct tagcache_stat tc_stat
;
137 /* Queue commands. */
138 enum tagcache_queue
{
145 /* Internal tagcache command queue. */
146 CMD_UPDATE_MASTER_HEADER
,
150 struct tagcache_command_entry
{
157 static struct tagcache_command_entry command_queue
[TAGCACHE_COMMAND_QUEUE_LENGTH
];
158 static volatile int command_queue_widx
= 0;
159 static volatile int command_queue_ridx
= 0;
160 static struct mutex command_queue_mutex
;
162 /* Tag database structures. */
164 /* Variable-length tag entry in tag files. */
165 struct tagfile_entry
{
166 short tag_length
; /* Length of the data in bytes including '\0' */
167 short idx_id
; /* Corresponding entry location in index file of not unique tags */
168 char tag_data
[0]; /* Begin of the tag data */
171 /* Fixed-size tag entry in master db index. */
173 int32_t tag_seek
[TAG_COUNT
]; /* Location of tag data or numeric tag data */
174 int32_t flag
; /* Status flags */
177 /* Header is the same in every file. */
178 struct tagcache_header
{
179 int32_t magic
; /* Header version number */
180 int32_t datasize
; /* Data size in bytes */
181 int32_t entry_count
; /* Number of entries in this file */
184 struct master_header
{
185 struct tagcache_header tch
;
186 int32_t serial
; /* Increasing counting number */
187 int32_t commitid
; /* Number of commits so far */
191 /* For the endianess correction */
192 static const char *tagfile_entry_ec
= "ss";
193 static const char *index_entry_ec
= "lllllllllllllllllllll"; /* (1 + TAG_COUNT) * l */
194 static const char *tagcache_header_ec
= "lll";
195 static const char *master_header_ec
= "llllll";
197 static struct master_header current_tcmh
;
199 #ifdef HAVE_TC_RAMCACHE
200 /* Header is created when loading database to ram. */
201 struct ramcache_header
{
202 struct master_header h
; /* Header from the master index */
203 struct index_entry
*indices
; /* Master index file content */
204 char *tags
[TAG_COUNT
]; /* Tag file content (not including filename tag) */
205 int entry_count
[TAG_COUNT
]; /* Number of entries in the indices. */
208 # ifdef HAVE_EEPROM_SETTINGS
209 struct statefile_header
{
210 struct ramcache_header
*hdr
;
211 struct tagcache_stat tc_stat
;
215 /* Pointer to allocated ramcache_header */
216 static struct ramcache_header
*hdr
;
220 * Full tag entries stored in a temporary file waiting
221 * for commit to the cache. */
222 struct temp_file_entry
{
223 long tag_offset
[TAG_COUNT
];
224 short tag_length
[TAG_COUNT
];
230 struct tempbuf_id_list
{
232 struct tempbuf_id_list
*next
;
235 struct tempbuf_searchidx
{
239 struct tempbuf_id_list idlist
;
242 /* Lookup buffer for fixing messed up index while after sorting. */
243 static long commit_entry_count
;
244 static long lookup_buffer_depth
;
245 static struct tempbuf_searchidx
**lookup
;
247 /* Used when building the temporary file. */
248 static int cachefd
= -1, filenametag_fd
;
249 static int total_entry_count
= 0;
250 static int data_size
= 0;
251 static int processed_dir_count
;
253 /* Thread safe locking */
254 static volatile int write_lock
;
255 static volatile int read_lock
;
257 static bool delete_entry(long idx_id
);
259 const char* tagcache_tag_to_str(int tag
)
261 return tags_str
[tag
];
264 bool tagcache_is_numeric_tag(int type
)
268 for (i
= 0; i
< (int)(sizeof(numeric_tags
)/sizeof(numeric_tags
[0])); i
++)
270 if (type
== numeric_tags
[i
])
277 bool tagcache_is_unique_tag(int type
)
281 for (i
= 0; i
< (int)(sizeof(unique_tags
)/sizeof(unique_tags
[0])); i
++)
283 if (type
== unique_tags
[i
])
290 bool tagcache_is_sorted_tag(int type
)
294 for (i
= 0; i
< (int)(sizeof(sorted_tags
)/sizeof(sorted_tags
[0])); i
++)
296 if (type
== sorted_tags
[i
])
305 * Returns true if specified flag is still present, i.e., dircache
306 * has not been reloaded.
308 static bool is_dircache_intact(void)
310 return dircache_get_appflag(DIRCACHE_APPFLAG_TAGCACHE
);
314 static int open_tag_fd(struct tagcache_header
*hdr
, int tag
, bool write
)
320 if (tagcache_is_numeric_tag(tag
) || tag
< 0 || tag
>= TAG_COUNT
)
323 snprintf(buf
, sizeof buf
, TAGCACHE_FILE_INDEX
, tag
);
325 fd
= open(buf
, write
? O_RDWR
: O_RDONLY
);
328 logf("tag file open failed: tag=%d write=%d file=%s", tag
, write
, buf
);
329 tc_stat
.ready
= false;
333 /* Check the header. */
334 rc
= ecread(fd
, hdr
, 1, tagcache_header_ec
, tc_stat
.econ
);
335 if (hdr
->magic
!= TAGCACHE_MAGIC
|| rc
!= sizeof(struct tagcache_header
))
337 logf("header error");
338 tc_stat
.ready
= false;
346 static int open_master_fd(struct master_header
*hdr
, bool write
)
351 fd
= open(TAGCACHE_FILE_MASTER
, write
? O_RDWR
: O_RDONLY
);
354 logf("master file open failed for R/W");
355 tc_stat
.ready
= false;
359 tc_stat
.econ
= false;
361 /* Check the header. */
362 rc
= read(fd
, hdr
, sizeof(struct master_header
));
363 if (hdr
->tch
.magic
== TAGCACHE_MAGIC
&& rc
== sizeof(struct master_header
))
369 /* Trying to read again, this time with endianess correction enabled. */
370 lseek(fd
, 0, SEEK_SET
);
372 rc
= ecread(fd
, hdr
, 1, master_header_ec
, true);
373 if (hdr
->tch
.magic
!= TAGCACHE_MAGIC
|| rc
!= sizeof(struct master_header
))
375 logf("header error");
376 tc_stat
.ready
= false;
387 static bool do_timed_yield(void)
389 /* Sorting can lock up for quite a while, so yield occasionally */
390 static long wakeup_tick
= 0;
391 if (current_tick
>= wakeup_tick
)
393 wakeup_tick
= current_tick
+ (HZ
/4);
401 #if defined(HAVE_TC_RAMCACHE) && defined(HAVE_DIRCACHE)
402 static long find_entry_ram(const char *filename
,
403 const struct dirent
*dc
)
405 static long last_pos
= 0;
408 /* Check if we tagcache is loaded into ram. */
409 if (!tc_stat
.ramcache
)
413 dc
= dircache_get_entry_ptr(filename
);
417 logf("tagcache: file not found.");
428 for (; i
< hdr
->h
.tch
.entry_count
; i
++)
430 if (hdr
->indices
[i
].tag_seek
[tag_filename
] == (long)dc
)
432 last_pos
= MAX(0, i
- 3);
449 static long find_entry_disk(const char *filename
)
451 struct tagcache_header tch
;
452 static long last_pos
= -1;
453 long pos_history
[POS_HISTORY_COUNT
];
454 long pos_history_idx
= 0;
456 struct tagfile_entry tfe
;
458 char buf
[TAG_MAXLEN
+32];
469 if ( (fd
= open_tag_fd(&tch
, tag_filename
, false)) < 0)
476 lseek(fd
, last_pos
, SEEK_SET
);
478 lseek(fd
, sizeof(struct tagcache_header
), SEEK_SET
);
482 pos
= lseek(fd
, 0, SEEK_CUR
);
483 for (i
= pos_history_idx
-1; i
>= 0; i
--)
484 pos_history
[i
+1] = pos_history
[i
];
485 pos_history
[0] = pos
;
487 if (ecread(fd
, &tfe
, 1, tagfile_entry_ec
, tc_stat
.econ
)
488 != sizeof(struct tagfile_entry
))
493 if (tfe
.tag_length
>= (long)sizeof(buf
))
495 logf("too long tag #1");
501 if (read(fd
, buf
, tfe
.tag_length
) != tfe
.tag_length
)
503 logf("read error #2");
509 if (!strcasecmp(filename
, buf
))
511 last_pos
= pos_history
[pos_history_idx
];
516 if (pos_history_idx
< POS_HISTORY_COUNT
- 1)
530 if (fd
!= filenametag_fd
)
535 if (fd
!= filenametag_fd
)
541 static int find_index(const char *filename
)
545 #if defined(HAVE_TC_RAMCACHE) && defined(HAVE_DIRCACHE)
546 if (tc_stat
.ramcache
&& is_dircache_intact())
547 idx_id
= find_entry_ram(filename
, NULL
);
551 idx_id
= find_entry_disk(filename
);
556 bool tagcache_find_index(struct tagcache_search
*tcs
, const char *filename
)
563 idx_id
= find_index(filename
);
567 if (!tagcache_search(tcs
, tag_filename
))
570 tcs
->entry_count
= 0;
571 tcs
->idx_id
= idx_id
;
576 static bool get_index(int masterfd
, int idxid
,
577 struct index_entry
*idx
, bool use_ram
)
579 bool localfd
= false;
583 logf("Incorrect idxid: %d", idxid
);
587 #ifdef HAVE_TC_RAMCACHE
588 if (tc_stat
.ramcache
&& use_ram
)
590 if (hdr
->indices
[idxid
].flag
& FLAG_DELETED
)
593 memcpy(idx
, &hdr
->indices
[idxid
], sizeof(struct index_entry
));
602 struct master_header tcmh
;
605 masterfd
= open_master_fd(&tcmh
, false);
610 lseek(masterfd
, idxid
* sizeof(struct index_entry
)
611 + sizeof(struct master_header
), SEEK_SET
);
612 if (ecread(masterfd
, idx
, 1, index_entry_ec
, tc_stat
.econ
)
613 != sizeof(struct index_entry
))
615 logf("read error #3");
625 if (idx
->flag
& FLAG_DELETED
)
631 static bool write_index(int masterfd
, int idxid
, struct index_entry
*idx
)
633 /* We need to exclude all memory only flags & tags when writing to disk. */
634 if (idx
->flag
& FLAG_DIRCACHE
)
636 logf("memory only flags!");
640 #ifdef HAVE_TC_RAMCACHE
641 /* Only update numeric data. Writing the whole index to RAM by memcpy
642 * destroys dircache pointers!
644 if (tc_stat
.ramcache
)
647 struct index_entry
*idx_ram
= &hdr
->indices
[idxid
];
649 for (tag
= 0; tag
< TAG_COUNT
; tag
++)
651 if (tagcache_is_numeric_tag(tag
))
653 idx_ram
->tag_seek
[tag
] = idx
->tag_seek
[tag
];
657 /* Don't touch the dircache flag. */
658 idx_ram
->flag
= idx
->flag
| (idx_ram
->flag
& FLAG_DIRCACHE
);
662 lseek(masterfd
, idxid
* sizeof(struct index_entry
)
663 + sizeof(struct master_header
), SEEK_SET
);
664 if (ecwrite(masterfd
, idx
, 1, index_entry_ec
, tc_stat
.econ
)
665 != sizeof(struct index_entry
))
667 logf("write error #3");
668 logf("idxid: %d", idxid
);
675 static bool open_files(struct tagcache_search
*tcs
, int tag
)
677 if (tcs
->idxfd
[tag
] < 0)
681 snprintf(fn
, sizeof fn
, TAGCACHE_FILE_INDEX
, tag
);
682 tcs
->idxfd
[tag
] = open(fn
, O_RDONLY
);
685 if (tcs
->idxfd
[tag
] < 0)
687 logf("File not open!");
694 static bool retrieve(struct tagcache_search
*tcs
, struct index_entry
*idx
,
695 int tag
, char *buf
, long size
)
697 struct tagfile_entry tfe
;
702 if (tagcache_is_numeric_tag(tag
))
705 seek
= idx
->tag_seek
[tag
];
708 logf("Retrieve failed");
712 #ifdef HAVE_TC_RAMCACHE
715 struct tagfile_entry
*ep
;
717 # ifdef HAVE_DIRCACHE
718 if (tag
== tag_filename
&& (idx
->flag
& FLAG_DIRCACHE
)
719 && is_dircache_intact())
721 dircache_copy_path((struct dirent
*)seek
,
727 if (tag
!= tag_filename
)
729 ep
= (struct tagfile_entry
*)&hdr
->tags
[tag
][seek
];
730 strncpy(buf
, ep
->tag_data
, size
-1);
737 if (!open_files(tcs
, tag
))
740 lseek(tcs
->idxfd
[tag
], seek
, SEEK_SET
);
741 if (ecread(tcs
->idxfd
[tag
], &tfe
, 1, tagfile_entry_ec
, tc_stat
.econ
)
742 != sizeof(struct tagfile_entry
))
744 logf("read error #5");
748 if (tfe
.tag_length
>= size
)
750 logf("too small buffer");
754 if (read(tcs
->idxfd
[tag
], buf
, tfe
.tag_length
) !=
757 logf("read error #6");
761 buf
[tfe
.tag_length
] = '\0';
766 static long check_virtual_tags(int tag
, const struct index_entry
*idx
)
772 case tag_virt_length_sec
:
773 data
= (idx
->tag_seek
[tag_length
]/1000) % 60;
776 case tag_virt_length_min
:
777 data
= (idx
->tag_seek
[tag_length
]/1000) / 60;
780 case tag_virt_playtime_sec
:
781 data
= (idx
->tag_seek
[tag_playtime
]/1000) % 60;
784 case tag_virt_playtime_min
:
785 data
= (idx
->tag_seek
[tag_playtime
]/1000) / 60;
788 case tag_virt_autoscore
:
789 if (idx
->tag_seek
[tag_length
] == 0
790 || idx
->tag_seek
[tag_playcount
] == 0)
796 data
= 100 * idx
->tag_seek
[tag_playtime
]
797 / idx
->tag_seek
[tag_length
]
798 / idx
->tag_seek
[tag_playcount
];
802 /* How many commits before the file has been added to the DB. */
803 case tag_virt_entryage
:
804 data
= current_tcmh
.commitid
- idx
->tag_seek
[tag_commitid
] - 1;
808 data
= idx
->tag_seek
[tag
];
814 long tagcache_get_numeric(const struct tagcache_search
*tcs
, int tag
)
816 struct index_entry idx
;
821 if (!tagcache_is_numeric_tag(tag
))
824 if (!get_index(tcs
->masterfd
, tcs
->idx_id
, &idx
, true))
827 return check_virtual_tags(tag
, &idx
);
830 inline static bool str_ends_with(const char *str1
, const char *str2
)
832 int str_len
= strlen(str1
);
833 int clause_len
= strlen(str2
);
835 if (clause_len
> str_len
)
838 return !strcasecmp(&str1
[str_len
- clause_len
], str2
);
841 inline static bool str_oneof(const char *str
, const char *list
)
844 int l
, len
= strlen(str
);
848 sep
= strchr(list
, '|');
849 l
= sep
? (long)sep
- (long)list
: (int)strlen(list
);
850 if ((l
==len
) && !strncasecmp(str
, list
, len
))
852 list
+= sep
? l
+ 1 : l
;
858 static bool check_against_clause(long numeric
, const char *str
,
859 const struct tagcache_search_clause
*clause
)
863 switch (clause
->type
)
866 return numeric
== clause
->numeric_data
;
868 return numeric
!= clause
->numeric_data
;
870 return numeric
> clause
->numeric_data
;
872 return numeric
>= clause
->numeric_data
;
874 return numeric
< clause
->numeric_data
;
876 return numeric
<= clause
->numeric_data
;
878 logf("Incorrect numeric tag: %d", clause
->type
);
883 switch (clause
->type
)
886 return !strcasecmp(clause
->str
, str
);
888 return strcasecmp(clause
->str
, str
);
890 return 0>strcasecmp(clause
->str
, str
);
892 return 0>=strcasecmp(clause
->str
, str
);
894 return 0<strcasecmp(clause
->str
, str
);
896 return 0<=strcasecmp(clause
->str
, str
);
897 case clause_contains
:
898 return (strcasestr(str
, clause
->str
) != NULL
);
899 case clause_not_contains
:
900 return (strcasestr(str
, clause
->str
) == NULL
);
901 case clause_begins_with
:
902 return (strcasestr(str
, clause
->str
) == str
);
903 case clause_not_begins_with
:
904 return (strcasestr(str
, clause
->str
) != str
);
905 case clause_ends_with
:
906 return str_ends_with(str
, clause
->str
);
907 case clause_not_ends_with
:
908 return !str_ends_with(str
, clause
->str
);
910 return str_oneof(str
, clause
->str
);
913 logf("Incorrect tag: %d", clause
->type
);
920 static bool check_clauses(struct tagcache_search
*tcs
,
921 struct index_entry
*idx
,
922 struct tagcache_search_clause
**clause
, int count
)
926 #ifdef HAVE_TC_RAMCACHE
929 /* Go through all conditional clauses. */
930 for (i
= 0; i
< count
; i
++)
932 struct tagfile_entry
*tfe
;
937 seek
= check_virtual_tags(clause
[i
]->tag
, idx
);
939 if (!tagcache_is_numeric_tag(clause
[i
]->tag
))
941 if (clause
[i
]->tag
== tag_filename
)
943 retrieve(tcs
, idx
, tag_filename
, buf
, sizeof buf
);
948 tfe
= (struct tagfile_entry
*)&hdr
->tags
[clause
[i
]->tag
][seek
];
953 if (!check_against_clause(seek
, str
, clause
[i
]))
960 /* Check for conditions. */
961 for (i
= 0; i
< count
; i
++)
963 struct tagfile_entry tfe
;
967 seek
= check_virtual_tags(clause
[i
]->tag
, idx
);
969 memset(str
, 0, sizeof str
);
970 if (!tagcache_is_numeric_tag(clause
[i
]->tag
))
972 int fd
= tcs
->idxfd
[clause
[i
]->tag
];
973 lseek(fd
, seek
, SEEK_SET
);
974 ecread(fd
, &tfe
, 1, tagfile_entry_ec
, tc_stat
.econ
);
975 if (tfe
.tag_length
>= (int)sizeof(str
))
977 logf("Too long tag read!");
981 read(fd
, str
, tfe
.tag_length
);
983 /* Check if entry has been deleted. */
988 if (!check_against_clause(seek
, str
, clause
[i
]))
996 bool tagcache_check_clauses(struct tagcache_search
*tcs
,
997 struct tagcache_search_clause
**clause
, int count
)
999 struct index_entry idx
;
1004 if (!get_index(tcs
->masterfd
, tcs
->idx_id
, &idx
, true))
1007 return check_clauses(tcs
, &idx
, clause
, count
);
1010 static bool add_uniqbuf(struct tagcache_search
*tcs
, unsigned long id
)
1014 /* If uniq buffer is not defined we must return true for search to work. */
1015 if (tcs
->unique_list
== NULL
1016 || (!tagcache_is_unique_tag(tcs
->type
)
1017 && !tagcache_is_numeric_tag(tcs
->type
)))
1022 for (i
= 0; i
< tcs
->unique_list_count
; i
++)
1024 /* Return false if entry is found. */
1025 if (tcs
->unique_list
[i
] == id
)
1029 if (tcs
->unique_list_count
< tcs
->unique_list_capacity
)
1031 tcs
->unique_list
[i
] = id
;
1032 tcs
->unique_list_count
++;
1038 static bool build_lookup_list(struct tagcache_search
*tcs
)
1040 struct index_entry entry
;
1043 tcs
->seek_list_count
= 0;
1045 #ifdef HAVE_TC_RAMCACHE
1050 for (i
= tcs
->seek_pos
; i
< hdr
->h
.tch
.entry_count
; i
++)
1052 struct index_entry
*idx
= &hdr
->indices
[i
];
1053 if (tcs
->seek_list_count
== SEEK_LIST_SIZE
)
1056 /* Skip deleted files. */
1057 if (idx
->flag
& FLAG_DELETED
)
1060 /* Go through all filters.. */
1061 for (j
= 0; j
< tcs
->filter_count
; j
++)
1063 if (idx
->tag_seek
[tcs
->filter_tag
[j
]] != tcs
->filter_seek
[j
])
1069 if (j
< tcs
->filter_count
)
1072 /* Check for conditions. */
1073 if (!check_clauses(tcs
, idx
, tcs
->clause
, tcs
->clause_count
))
1076 /* Add to the seek list if not already in uniq buffer. */
1077 if (!add_uniqbuf(tcs
, idx
->tag_seek
[tcs
->type
]))
1081 tcs
->seek_list
[tcs
->seek_list_count
] = idx
->tag_seek
[tcs
->type
];
1082 tcs
->seek_flags
[tcs
->seek_list_count
] = idx
->flag
;
1083 tcs
->seek_list_count
++;
1088 return tcs
->seek_list_count
> 0;
1092 lseek(tcs
->masterfd
, tcs
->seek_pos
* sizeof(struct index_entry
) +
1093 sizeof(struct master_header
), SEEK_SET
);
1095 while (ecread(tcs
->masterfd
, &entry
, 1, index_entry_ec
, tc_stat
.econ
)
1096 == sizeof(struct index_entry
))
1098 if (tcs
->seek_list_count
== SEEK_LIST_SIZE
)
1103 /* Check if entry has been deleted. */
1104 if (entry
.flag
& FLAG_DELETED
)
1107 /* Go through all filters.. */
1108 for (i
= 0; i
< tcs
->filter_count
; i
++)
1110 if (entry
.tag_seek
[tcs
->filter_tag
[i
]] != tcs
->filter_seek
[i
])
1114 if (i
< tcs
->filter_count
)
1117 /* Check for conditions. */
1118 if (!check_clauses(tcs
, &entry
, tcs
->clause
, tcs
->clause_count
))
1121 /* Add to the seek list if not already in uniq buffer. */
1122 if (!add_uniqbuf(tcs
, entry
.tag_seek
[tcs
->type
]))
1126 tcs
->seek_list
[tcs
->seek_list_count
] = entry
.tag_seek
[tcs
->type
];
1127 tcs
->seek_flags
[tcs
->seek_list_count
] = entry
.flag
;
1128 tcs
->seek_list_count
++;
1133 return tcs
->seek_list_count
> 0;
1137 static void remove_files(void)
1142 tc_stat
.ready
= false;
1143 tc_stat
.ramcache
= false;
1144 tc_stat
.econ
= false;
1145 remove(TAGCACHE_FILE_MASTER
);
1146 for (i
= 0; i
< TAG_COUNT
; i
++)
1148 if (tagcache_is_numeric_tag(i
))
1151 snprintf(buf
, sizeof buf
, TAGCACHE_FILE_INDEX
, i
);
1157 static bool check_all_headers(void)
1159 struct master_header myhdr
;
1160 struct tagcache_header tch
;
1164 if ( (fd
= open_master_fd(&myhdr
, false)) < 0)
1170 logf("tagcache is dirty!");
1174 memcpy(¤t_tcmh
, &myhdr
, sizeof(struct master_header
));
1176 for (tag
= 0; tag
< TAG_COUNT
; tag
++)
1178 if (tagcache_is_numeric_tag(tag
))
1181 if ( (fd
= open_tag_fd(&tch
, tag
, false)) < 0)
1190 bool tagcache_search(struct tagcache_search
*tcs
, int tag
)
1192 struct tagcache_header tag_hdr
;
1193 struct master_header master_hdr
;
1196 if (tcs
->initialized
)
1197 tagcache_search_finish(tcs
);
1202 memset(tcs
, 0, sizeof(struct tagcache_search
));
1203 if (tc_stat
.commit_step
> 0 || !tc_stat
.ready
)
1206 tcs
->position
= sizeof(struct tagcache_header
);
1209 tcs
->seek_list_count
= 0;
1210 tcs
->filter_count
= 0;
1213 for (i
= 0; i
< TAG_COUNT
; i
++)
1216 #ifndef HAVE_TC_RAMCACHE
1217 tcs
->ramsearch
= false;
1219 tcs
->ramsearch
= tc_stat
.ramcache
;
1222 tcs
->entry_count
= hdr
->entry_count
[tcs
->type
];
1227 if (!tagcache_is_numeric_tag(tcs
->type
))
1229 tcs
->idxfd
[tcs
->type
] = open_tag_fd(&tag_hdr
, tcs
->type
, false);
1230 if (tcs
->idxfd
[tcs
->type
] < 0)
1234 /* Always open as R/W so we can pass tcs to functions that modify data also
1235 * without failing. */
1236 tcs
->masterfd
= open_master_fd(&master_hdr
, true);
1238 if (tcs
->masterfd
< 0)
1243 tcs
->initialized
= true;
1249 void tagcache_search_set_uniqbuf(struct tagcache_search
*tcs
,
1250 void *buffer
, long length
)
1252 tcs
->unique_list
= (unsigned long *)buffer
;
1253 tcs
->unique_list_capacity
= length
/ sizeof(*tcs
->unique_list
);
1254 tcs
->unique_list_count
= 0;
1257 bool tagcache_search_add_filter(struct tagcache_search
*tcs
,
1260 if (tcs
->filter_count
== TAGCACHE_MAX_FILTERS
)
1263 if (!tagcache_is_unique_tag(tag
) || tagcache_is_numeric_tag(tag
))
1266 tcs
->filter_tag
[tcs
->filter_count
] = tag
;
1267 tcs
->filter_seek
[tcs
->filter_count
] = seek
;
1268 tcs
->filter_count
++;
1273 bool tagcache_search_add_clause(struct tagcache_search
*tcs
,
1274 struct tagcache_search_clause
*clause
)
1278 if (tcs
->clause_count
>= TAGCACHE_MAX_CLAUSES
)
1280 logf("Too many clauses");
1284 /* Check if there is already a similar filter in present (filters are
1285 * much faster than clauses).
1287 for (i
= 0; i
< tcs
->filter_count
; i
++)
1289 if (tcs
->filter_tag
[i
] == clause
->tag
)
1293 if (!tagcache_is_numeric_tag(clause
->tag
) && tcs
->idxfd
[clause
->tag
] < 0)
1297 snprintf(buf
, sizeof buf
, TAGCACHE_FILE_INDEX
, clause
->tag
);
1298 tcs
->idxfd
[clause
->tag
] = open(buf
, O_RDONLY
);
1301 tcs
->clause
[tcs
->clause_count
] = clause
;
1302 tcs
->clause_count
++;
1307 /* TODO: Remove this mess. */
1308 #ifdef HAVE_DIRCACHE
1309 #define TAG_FILENAME_RAM(tcs) ((tcs->type == tag_filename) \
1310 ? ((flag & FLAG_DIRCACHE) && is_dircache_intact()) : 1)
1312 #define TAG_FILENAME_RAM(tcs) (tcs->type != tag_filename)
1315 static bool get_next(struct tagcache_search
*tcs
)
1317 static char buf
[TAG_MAXLEN
+32];
1318 struct tagfile_entry entry
;
1321 if (!tcs
->valid
|| !tc_stat
.ready
)
1324 if (tcs
->idxfd
[tcs
->type
] < 0 && !tagcache_is_numeric_tag(tcs
->type
)
1325 #ifdef HAVE_TC_RAMCACHE
1331 /* Relative fetch. */
1332 if (tcs
->filter_count
> 0 || tcs
->clause_count
> 0
1333 || tagcache_is_numeric_tag(tcs
->type
))
1335 /* Check for end of list. */
1336 if (tcs
->seek_list_count
== 0)
1338 /* Try to fetch more. */
1339 if (!build_lookup_list(tcs
))
1346 tcs
->seek_list_count
--;
1347 flag
= tcs
->seek_flags
[tcs
->seek_list_count
];
1349 /* Seek stream to the correct position and continue to direct fetch. */
1350 if ((!tcs
->ramsearch
|| !TAG_FILENAME_RAM(tcs
))
1351 && !tagcache_is_numeric_tag(tcs
->type
))
1353 if (!open_files(tcs
, tcs
->type
))
1356 lseek(tcs
->idxfd
[tcs
->type
], tcs
->seek_list
[tcs
->seek_list_count
], SEEK_SET
);
1359 tcs
->position
= tcs
->seek_list
[tcs
->seek_list_count
];
1362 if (tagcache_is_numeric_tag(tcs
->type
))
1364 snprintf(buf
, sizeof(buf
), "%d", tcs
->position
);
1365 tcs
->result_seek
= tcs
->position
;
1367 tcs
->result_len
= strlen(buf
) + 1;
1372 #ifdef HAVE_TC_RAMCACHE
1373 if (tcs
->ramsearch
&& TAG_FILENAME_RAM(tcs
))
1375 struct tagfile_entry
*ep
;
1377 if (tcs
->entry_count
== 0)
1384 tcs
->result_seek
= tcs
->position
;
1386 # ifdef HAVE_DIRCACHE
1387 if (tcs
->type
== tag_filename
)
1389 dircache_copy_path((struct dirent
*)tcs
->position
,
1392 tcs
->result_len
= strlen(buf
) + 1;
1393 tcs
->idx_id
= FLAG_GET_ATTR(flag
);
1394 tcs
->ramresult
= false;
1400 ep
= (struct tagfile_entry
*)&hdr
->tags
[tcs
->type
][tcs
->position
];
1401 tcs
->position
+= sizeof(struct tagfile_entry
) + ep
->tag_length
;
1402 tcs
->result
= ep
->tag_data
;
1403 tcs
->result_len
= strlen(tcs
->result
) + 1;
1404 tcs
->idx_id
= ep
->idx_id
;
1405 tcs
->ramresult
= true;
1412 if (!open_files(tcs
, tcs
->type
))
1415 tcs
->result_seek
= lseek(tcs
->idxfd
[tcs
->type
], 0, SEEK_CUR
);
1416 if (ecread(tcs
->idxfd
[tcs
->type
], &entry
, 1,
1417 tagfile_entry_ec
, tc_stat
.econ
) != sizeof(struct tagfile_entry
))
1425 if (entry
.tag_length
> (long)sizeof(buf
))
1428 logf("too long tag #2");
1432 if (read(tcs
->idxfd
[tcs
->type
], buf
, entry
.tag_length
) != entry
.tag_length
)
1435 logf("read error #4");
1440 tcs
->result_len
= strlen(tcs
->result
) + 1;
1441 tcs
->idx_id
= entry
.idx_id
;
1442 tcs
->ramresult
= false;
1447 bool tagcache_get_next(struct tagcache_search
*tcs
)
1449 while (get_next(tcs
))
1451 if (tcs
->result_len
> 1)
1458 bool tagcache_retrieve(struct tagcache_search
*tcs
, int idxid
,
1459 int tag
, char *buf
, long size
)
1461 struct index_entry idx
;
1464 if (!get_index(tcs
->masterfd
, idxid
, &idx
, true))
1467 return retrieve(tcs
, &idx
, tag
, buf
, size
);
1470 static bool update_master_header(void)
1472 struct master_header myhdr
;
1478 if ( (fd
= open_master_fd(&myhdr
, true)) < 0)
1481 myhdr
.serial
= current_tcmh
.serial
;
1482 myhdr
.commitid
= current_tcmh
.commitid
;
1485 lseek(fd
, 0, SEEK_SET
);
1486 ecwrite(fd
, &myhdr
, 1, master_header_ec
, tc_stat
.econ
);
1489 #ifdef HAVE_TC_RAMCACHE
1492 hdr
->h
.serial
= current_tcmh
.serial
;
1493 hdr
->h
.commitid
= current_tcmh
.commitid
;
1502 void tagcache_modify(struct tagcache_search
*tcs
, int type
, const char *text
)
1504 struct tagentry
*entry
;
1506 if (tcs
->type
!= tag_title
)
1509 /* We will need reserve buffer for this. */
1512 struct tagfile_entry
*ep
;
1514 ep
= (struct tagfile_entry
*)&hdr
->tags
[tcs
->type
][tcs
->result_seek
];
1515 tcs
->seek_list
[tcs
->seek_list_count
];
1518 entry
= find_entry_ram();
1523 void tagcache_search_finish(struct tagcache_search
*tcs
)
1527 if (!tcs
->initialized
)
1530 if (tcs
->masterfd
>= 0)
1532 close(tcs
->masterfd
);
1536 for (i
= 0; i
< TAG_COUNT
; i
++)
1538 if (tcs
->idxfd
[i
] >= 0)
1540 close(tcs
->idxfd
[i
]);
1545 tcs
->ramsearch
= false;
1547 tcs
->initialized
= 0;
1552 #if defined(HAVE_TC_RAMCACHE) && defined(HAVE_DIRCACHE)
1553 static struct tagfile_entry
*get_tag(const struct index_entry
*entry
, int tag
)
1555 return (struct tagfile_entry
*)&hdr
->tags
[tag
][entry
->tag_seek
[tag
]];
1558 static long get_tag_numeric(const struct index_entry
*entry
, int tag
)
1560 return check_virtual_tags(tag
, entry
);
1563 static char* get_tag_string(const struct index_entry
*entry
, int tag
)
1565 char* s
= get_tag(entry
, tag
)->tag_data
;
1566 return strcmp(s
, UNTAGGED
) ? s
: NULL
;
1569 bool tagcache_fill_tags(struct mp3entry
*id3
, const char *filename
)
1571 struct index_entry
*entry
;
1577 /* Find the corresponding entry in tagcache. */
1578 idx_id
= find_entry_ram(filename
, NULL
);
1579 if (idx_id
< 0 || !tc_stat
.ramcache
)
1582 entry
= &hdr
->indices
[idx_id
];
1584 id3
->title
= get_tag_string(entry
, tag_title
);
1585 id3
->artist
= get_tag_string(entry
, tag_artist
);
1586 id3
->album
= get_tag_string(entry
, tag_album
);
1587 id3
->genre_string
= get_tag_string(entry
, tag_genre
);
1588 id3
->composer
= get_tag_string(entry
, tag_composer
);
1589 id3
->comment
= get_tag_string(entry
, tag_comment
);
1590 id3
->albumartist
= get_tag_string(entry
, tag_albumartist
);
1591 id3
->grouping
= get_tag_string(entry
, tag_grouping
);
1593 id3
->playcount
= get_tag_numeric(entry
, tag_playcount
);
1594 id3
->rating
= get_tag_numeric(entry
, tag_rating
);
1595 id3
->lastplayed
= get_tag_numeric(entry
, tag_lastplayed
);
1596 id3
->score
= get_tag_numeric(entry
, tag_virt_autoscore
) / 10;
1597 id3
->year
= get_tag_numeric(entry
, tag_year
);
1599 id3
->discnum
= get_tag_numeric(entry
, tag_discnumber
);
1600 id3
->tracknum
= get_tag_numeric(entry
, tag_tracknumber
);
1601 id3
->bitrate
= get_tag_numeric(entry
, tag_bitrate
);
1602 if (id3
->bitrate
== 0)
1609 static inline void write_item(const char *item
)
1611 int len
= strlen(item
) + 1;
1614 write(cachefd
, item
, len
);
1617 static int check_if_empty(char **tag
)
1621 if (*tag
== NULL
|| **tag
== '\0')
1624 return sizeof(UNTAGGED
); /* Tag length */
1627 length
= strlen(*tag
);
1628 if (length
> TAG_MAXLEN
)
1630 logf("over length tag: %s", *tag
);
1631 length
= TAG_MAXLEN
;
1632 (*tag
)[length
] = '\0';
1638 #define ADD_TAG(entry,tag,data) \
1640 entry.tag_offset[tag] = offset; \
1641 entry.tag_length[tag] = check_if_empty(data); \
1642 offset += entry.tag_length[tag]
1644 static void add_tagcache(char *path
, unsigned long mtime
1645 #if defined(HAVE_TC_RAMCACHE) && defined(HAVE_DIRCACHE)
1646 ,const struct dirent
*dc
1650 struct mp3entry id3
;
1651 struct temp_file_entry entry
;
1655 char tracknumfix
[3];
1657 int path_length
= strlen(path
);
1658 bool has_albumartist
;
1664 /* Check for overlength file path. */
1665 if (path_length
> TAG_MAXLEN
)
1667 /* Path can't be shortened. */
1668 logf("Too long path: %s", path
);
1672 /* Check if the file is supported. */
1673 if (probe_file_format(path
) == AFMT_UNKNOWN
)
1676 /* Check if the file is already cached. */
1677 #if defined(HAVE_TC_RAMCACHE) && defined(HAVE_DIRCACHE)
1678 if (tc_stat
.ramcache
&& is_dircache_intact())
1680 idx_id
= find_entry_ram(path
, dc
);
1685 if (filenametag_fd
>= 0)
1687 idx_id
= find_entry_disk(path
);
1691 /* Check if file has been modified. */
1694 struct index_entry idx
;
1696 if (!get_index(-1, idx_id
, &idx
, true))
1698 logf("failed to retrieve index entry");
1702 if ((unsigned long)idx
.tag_seek
[tag_mtime
] == mtime
)
1704 /* No changes to file. */
1708 /* Metadata might have been changed. Delete the entry. */
1709 logf("Re-adding: %s", path
);
1710 if (!delete_entry(idx_id
))
1712 logf("delete_entry failed: %d", idx_id
);
1717 fd
= open(path
, O_RDONLY
);
1720 logf("open fail: %s", path
);
1724 memset(&id3
, 0, sizeof(struct mp3entry
));
1725 memset(&entry
, 0, sizeof(struct temp_file_entry
));
1726 memset(&tracknumfix
, 0, sizeof(tracknumfix
));
1727 ret
= get_metadata(&id3
, fd
, path
);
1733 logf("-> %s", path
);
1735 /* Generate track number if missing. */
1736 if (id3
.tracknum
<= 0)
1738 const char *p
= strrchr(path
, '.');
1741 p
= &path
[strlen(path
)-1];
1745 if (isdigit(*p
) && isdigit(*(p
-1)))
1747 tracknumfix
[1] = *p
--;
1748 tracknumfix
[0] = *p
;
1754 if (tracknumfix
[0] != '\0')
1756 id3
.tracknum
= atoi(tracknumfix
);
1757 /* Set a flag to indicate track number has been generated. */
1758 entry
.flag
|= FLAG_TRKNUMGEN
;
1762 /* Unable to generate track number. */
1768 entry
.tag_offset
[tag_year
] = id3
.year
;
1769 entry
.tag_offset
[tag_discnumber
] = id3
.discnum
;
1770 entry
.tag_offset
[tag_tracknumber
] = id3
.tracknum
;
1771 entry
.tag_offset
[tag_length
] = id3
.length
;
1772 entry
.tag_offset
[tag_bitrate
] = id3
.bitrate
;
1773 entry
.tag_offset
[tag_mtime
] = mtime
;
1776 has_albumartist
= id3
.albumartist
!= NULL
1777 && strlen(id3
.albumartist
) > 0;
1778 has_grouping
= id3
.grouping
!= NULL
1779 && strlen(id3
.grouping
) > 0;
1781 ADD_TAG(entry
, tag_filename
, &path
);
1782 ADD_TAG(entry
, tag_title
, &id3
.title
);
1783 ADD_TAG(entry
, tag_artist
, &id3
.artist
);
1784 ADD_TAG(entry
, tag_album
, &id3
.album
);
1785 ADD_TAG(entry
, tag_genre
, &id3
.genre_string
);
1786 ADD_TAG(entry
, tag_composer
, &id3
.composer
);
1787 ADD_TAG(entry
, tag_comment
, &id3
.comment
);
1788 if (has_albumartist
)
1790 ADD_TAG(entry
, tag_albumartist
, &id3
.albumartist
);
1794 ADD_TAG(entry
, tag_albumartist
, &id3
.artist
);
1798 ADD_TAG(entry
, tag_grouping
, &id3
.grouping
);
1802 ADD_TAG(entry
, tag_grouping
, &id3
.title
);
1804 entry
.data_length
= offset
;
1806 /* Write the header */
1807 write(cachefd
, &entry
, sizeof(struct temp_file_entry
));
1809 /* And tags also... Correct order is critical */
1811 write_item(id3
.title
);
1812 write_item(id3
.artist
);
1813 write_item(id3
.album
);
1814 write_item(id3
.genre_string
);
1815 write_item(id3
.composer
);
1816 write_item(id3
.comment
);
1817 if (has_albumartist
)
1819 write_item(id3
.albumartist
);
1823 write_item(id3
.artist
);
1827 write_item(id3
.grouping
);
1831 write_item(id3
.title
);
1833 total_entry_count
++;
1836 static bool tempbuf_insert(char *str
, int id
, int idx_id
, bool unique
)
1838 struct tempbuf_searchidx
*index
= (struct tempbuf_searchidx
*)tempbuf
;
1839 int len
= strlen(str
)+1;
1842 unsigned *crcbuf
= (unsigned *)&tempbuf
[tempbuf_size
-4];
1843 char buf
[TAG_MAXLEN
+32];
1845 for (i
= 0; str
[i
] != '\0' && i
< (int)sizeof(buf
)-1; i
++)
1846 buf
[i
] = tolower(str
[i
]);
1849 crc32
= crc_32(buf
, i
, 0xffffffff);
1853 /* Check if the crc does not exist -> entry does not exist for sure. */
1854 for (i
= 0; i
< tempbufidx
; i
++)
1856 if (crcbuf
[-i
] != crc32
)
1859 if (!strcasecmp(str
, index
[i
].str
))
1861 if (id
< 0 || id
>= lookup_buffer_depth
)
1863 logf("lookup buf overf.: %d", id
);
1867 lookup
[id
] = &index
[i
];
1873 /* Insert to CRC buffer. */
1874 crcbuf
[-tempbufidx
] = crc32
;
1877 /* Insert it to the buffer. */
1878 tempbuf_left
-= len
;
1879 if (tempbuf_left
- 4 < 0 || tempbufidx
>= commit_entry_count
-1)
1882 if (id
>= lookup_buffer_depth
)
1884 logf("lookup buf overf. #2: %d", id
);
1890 lookup
[id
] = &index
[tempbufidx
];
1891 index
[tempbufidx
].idlist
.id
= id
;
1894 index
[tempbufidx
].idlist
.id
= -1;
1896 index
[tempbufidx
].idlist
.next
= NULL
;
1897 index
[tempbufidx
].idx_id
= idx_id
;
1898 index
[tempbufidx
].seek
= -1;
1899 index
[tempbufidx
].str
= &tempbuf
[tempbuf_pos
];
1900 memcpy(index
[tempbufidx
].str
, str
, len
);
1907 static int compare(const void *p1
, const void *p2
)
1911 struct tempbuf_searchidx
*e1
= (struct tempbuf_searchidx
*)p1
;
1912 struct tempbuf_searchidx
*e2
= (struct tempbuf_searchidx
*)p2
;
1914 if (strcmp(e1
->str
, UNTAGGED
) == 0)
1916 if (strcmp(e2
->str
, UNTAGGED
) == 0)
1920 else if (strcmp(e2
->str
, UNTAGGED
) == 0)
1923 return strncasecmp(e1
->str
, e2
->str
, TAG_MAXLEN
);
1926 static int tempbuf_sort(int fd
)
1928 struct tempbuf_searchidx
*index
= (struct tempbuf_searchidx
*)tempbuf
;
1929 struct tagfile_entry fe
;
1933 /* Generate reverse lookup entries. */
1934 for (i
= 0; i
< lookup_buffer_depth
; i
++)
1936 struct tempbuf_id_list
*idlist
;
1941 if (lookup
[i
]->idlist
.id
== i
)
1944 idlist
= &lookup
[i
]->idlist
;
1945 while (idlist
->next
!= NULL
)
1946 idlist
= idlist
->next
;
1948 tempbuf_left
-= sizeof(struct tempbuf_id_list
);
1949 if (tempbuf_left
- 4 < 0)
1952 idlist
->next
= (struct tempbuf_id_list
*)&tempbuf
[tempbuf_pos
];
1953 if (tempbuf_pos
& 0x03)
1955 tempbuf_pos
= (tempbuf_pos
& ~0x03) + 0x04;
1957 idlist
->next
= (struct tempbuf_id_list
*)&tempbuf
[tempbuf_pos
];
1959 tempbuf_pos
+= sizeof(struct tempbuf_id_list
);
1961 idlist
= idlist
->next
;
1963 idlist
->next
= NULL
;
1968 qsort(index
, tempbufidx
, sizeof(struct tempbuf_searchidx
), compare
);
1969 memset(lookup
, 0, lookup_buffer_depth
* sizeof(struct tempbuf_searchidx
**));
1971 for (i
= 0; i
< tempbufidx
; i
++)
1973 struct tempbuf_id_list
*idlist
= &index
[i
].idlist
;
1975 /* Fix the lookup list. */
1976 while (idlist
!= NULL
)
1978 if (idlist
->id
>= 0)
1979 lookup
[idlist
->id
] = &index
[i
];
1980 idlist
= idlist
->next
;
1983 index
[i
].seek
= lseek(fd
, 0, SEEK_CUR
);
1984 length
= strlen(index
[i
].str
) + 1;
1985 fe
.tag_length
= length
;
1986 fe
.idx_id
= index
[i
].idx_id
;
1988 /* Check the chunk alignment. */
1989 if ((fe
.tag_length
+ sizeof(struct tagfile_entry
))
1990 % TAGFILE_ENTRY_CHUNK_LENGTH
)
1992 fe
.tag_length
+= TAGFILE_ENTRY_CHUNK_LENGTH
-
1993 ((fe
.tag_length
+ sizeof(struct tagfile_entry
))
1994 % TAGFILE_ENTRY_CHUNK_LENGTH
);
1997 #ifdef TAGCACHE_STRICT_ALIGN
1998 /* Make sure the entry is long aligned. */
1999 if (index
[i
].seek
& 0x03)
2001 logf("tempbuf_sort: alignment error!");
2006 if (ecwrite(fd
, &fe
, 1, tagfile_entry_ec
, tc_stat
.econ
) !=
2007 sizeof(struct tagfile_entry
))
2009 logf("tempbuf_sort: write error #1");
2013 if (write(fd
, index
[i
].str
, length
) != length
)
2015 logf("tempbuf_sort: write error #2");
2019 /* Write some padding. */
2020 if (fe
.tag_length
- length
> 0)
2021 write(fd
, "XXXXXXXX", fe
.tag_length
- length
);
2027 inline static struct tempbuf_searchidx
* tempbuf_locate(int id
)
2029 if (id
< 0 || id
>= lookup_buffer_depth
)
2036 inline static int tempbuf_find_location(int id
)
2038 struct tempbuf_searchidx
*entry
;
2040 entry
= tempbuf_locate(id
);
2047 static bool build_numeric_indices(struct tagcache_header
*h
, int tmpfd
)
2049 struct master_header tcmh
;
2050 struct index_entry idx
;
2053 struct temp_file_entry
*entrybuf
= (struct temp_file_entry
*)tempbuf
;
2055 int entries_processed
= 0;
2057 char buf
[TAG_MAXLEN
];
2059 max_entries
= tempbuf_size
/ sizeof(struct temp_file_entry
) - 1;
2061 logf("Building numeric indices...");
2062 lseek(tmpfd
, sizeof(struct tagcache_header
), SEEK_SET
);
2064 if ( (masterfd
= open_master_fd(&tcmh
, true)) < 0)
2067 masterfd_pos
= lseek(masterfd
, tcmh
.tch
.entry_count
* sizeof(struct index_entry
),
2069 if (masterfd_pos
== filesize(masterfd
))
2071 logf("we can't append!");
2076 while (entries_processed
< h
->entry_count
)
2078 int count
= MIN(h
->entry_count
- entries_processed
, max_entries
);
2080 /* Read in as many entries as possible. */
2081 for (i
= 0; i
< count
; i
++)
2083 struct temp_file_entry
*tfe
= &entrybuf
[i
];
2086 /* Read in numeric data. */
2087 if (read(tmpfd
, tfe
, sizeof(struct temp_file_entry
)) !=
2088 sizeof(struct temp_file_entry
))
2090 logf("read fail #1");
2095 datastart
= lseek(tmpfd
, 0, SEEK_CUR
);
2098 * Read string data from the following tags:
2104 * A crc32 hash is calculated from the read data
2105 * and stored back to the data offset field kept in memory.
2107 #define tmpdb_read_string_tag(tag) \
2108 lseek(tmpfd, tfe->tag_offset[tag], SEEK_CUR); \
2109 if ((unsigned long)tfe->tag_length[tag] > sizeof buf) \
2111 logf("read fail: buffer overflow"); \
2116 if (read(tmpfd, buf, tfe->tag_length[tag]) != \
2117 tfe->tag_length[tag]) \
2119 logf("read fail #2"); \
2124 tfe->tag_offset[tag] = crc_32(buf, strlen(buf), 0xffffffff); \
2125 lseek(tmpfd, datastart, SEEK_SET)
2127 tmpdb_read_string_tag(tag_filename
);
2128 tmpdb_read_string_tag(tag_artist
);
2129 tmpdb_read_string_tag(tag_album
);
2130 tmpdb_read_string_tag(tag_title
);
2132 /* Seek to the end of the string data. */
2133 lseek(tmpfd
, tfe
->data_length
, SEEK_CUR
);
2136 /* Backup the master index position. */
2137 masterfd_pos
= lseek(masterfd
, 0, SEEK_CUR
);
2138 lseek(masterfd
, sizeof(struct master_header
), SEEK_SET
);
2140 /* Check if we can resurrect some deleted runtime statistics data. */
2141 for (i
= 0; i
< tcmh
.tch
.entry_count
; i
++)
2143 /* Read the index entry. */
2144 if (ecread(masterfd
, &idx
, 1, index_entry_ec
, tc_stat
.econ
)
2145 != sizeof(struct index_entry
))
2147 logf("read fail #3");
2153 * Skip unless the entry is marked as being deleted
2154 * or the data has already been resurrected.
2156 if (!(idx
.flag
& FLAG_DELETED
) || idx
.flag
& FLAG_RESURRECTED
)
2159 /* Now try to match the entry. */
2161 * To succesfully match a song, the following conditions
2164 * For numeric fields: tag_length
2165 * - Full identical match is required
2167 * If tag_filename matches, no further checking necessary.
2169 * For string hashes: tag_artist, tag_album, tag_title
2170 * - Two of these must match
2172 for (j
= 0; j
< count
; j
++)
2174 struct temp_file_entry
*tfe
= &entrybuf
[j
];
2176 /* Try to match numeric fields first. */
2177 if (tfe
->tag_offset
[tag_length
] != idx
.tag_seek
[tag_length
])
2180 /* Now it's time to do the hash matching. */
2181 if (tfe
->tag_offset
[tag_filename
] != idx
.tag_seek
[tag_filename
])
2183 int match_count
= 0;
2185 /* No filename match, check if we can match two other tags. */
2186 #define tmpdb_match(tag) \
2187 if (tfe->tag_offset[tag] == idx.tag_seek[tag]) \
2190 tmpdb_match(tag_artist
);
2191 tmpdb_match(tag_album
);
2192 tmpdb_match(tag_title
);
2194 if (match_count
< 2)
2196 /* Still no match found, give up. */
2201 /* A match found, now copy & resurrect the statistical data. */
2202 #define tmpdb_copy_tag(tag) \
2203 tfe->tag_offset[tag] = idx.tag_seek[tag]
2205 tmpdb_copy_tag(tag_playcount
);
2206 tmpdb_copy_tag(tag_rating
);
2207 tmpdb_copy_tag(tag_playtime
);
2208 tmpdb_copy_tag(tag_lastplayed
);
2209 tmpdb_copy_tag(tag_commitid
);
2211 /* Avoid processing this entry again. */
2212 idx
.flag
|= FLAG_RESURRECTED
;
2214 lseek(masterfd
, -sizeof(struct index_entry
), SEEK_CUR
);
2215 if (ecwrite(masterfd
, &idx
, 1, index_entry_ec
, tc_stat
.econ
)
2216 != sizeof(struct index_entry
))
2218 logf("masterfd writeback fail #1");
2223 logf("Entry resurrected");
2228 /* Restore the master index position. */
2229 lseek(masterfd
, masterfd_pos
, SEEK_SET
);
2231 /* Commit the data to the index. */
2232 for (i
= 0; i
< count
; i
++)
2234 int loc
= lseek(masterfd
, 0, SEEK_CUR
);
2236 if (ecread(masterfd
, &idx
, 1, index_entry_ec
, tc_stat
.econ
)
2237 != sizeof(struct index_entry
))
2239 logf("read fail #3");
2244 for (j
= 0; j
< TAG_COUNT
; j
++)
2246 if (!tagcache_is_numeric_tag(j
))
2249 idx
.tag_seek
[j
] = entrybuf
[i
].tag_offset
[j
];
2251 idx
.flag
= entrybuf
[i
].flag
;
2253 if (idx
.tag_seek
[tag_commitid
])
2255 /* Data has been resurrected. */
2256 idx
.flag
|= FLAG_DIRTYNUM
;
2258 else if (tc_stat
.ready
&& current_tcmh
.commitid
> 0)
2260 idx
.tag_seek
[tag_commitid
] = current_tcmh
.commitid
;
2261 idx
.flag
|= FLAG_DIRTYNUM
;
2264 /* Write back the updated index. */
2265 lseek(masterfd
, loc
, SEEK_SET
);
2266 if (ecwrite(masterfd
, &idx
, 1, index_entry_ec
, tc_stat
.econ
)
2267 != sizeof(struct index_entry
))
2275 entries_processed
+= count
;
2276 logf("%d/%ld entries processed", entries_processed
, h
->entry_count
);
2287 * == 0 temporary failure
2290 static int build_index(int index_type
, struct tagcache_header
*h
, int tmpfd
)
2293 struct tagcache_header tch
;
2294 struct master_header tcmh
;
2295 struct index_entry idxbuf
[IDX_BUF_DEPTH
];
2297 char buf
[TAG_MAXLEN
+32];
2298 int fd
= -1, masterfd
;
2303 logf("Building index: %d", index_type
);
2305 /* Check the number of entries we need to allocate ram for. */
2306 commit_entry_count
= h
->entry_count
+ 1;
2308 masterfd
= open_master_fd(&tcmh
, false);
2311 commit_entry_count
+= tcmh
.tch
.entry_count
;
2315 remove_files(); /* Just to be sure we are clean. */
2317 /* Open the index file, which contains the tag names. */
2318 fd
= open_tag_fd(&tch
, index_type
, true);
2321 logf("tch.datasize=%ld", tch
.datasize
);
2322 lookup_buffer_depth
= 1 +
2323 /* First part */ commit_entry_count
+
2324 /* Second part */ (tch
.datasize
/ TAGFILE_ENTRY_CHUNK_LENGTH
);
2328 lookup_buffer_depth
= 1 +
2329 /* First part */ commit_entry_count
+
2330 /* Second part */ 0;
2333 logf("lookup_buffer_depth=%ld", lookup_buffer_depth
);
2334 logf("commit_entry_count=%ld", commit_entry_count
);
2336 /* Allocate buffer for all index entries from both old and new
2339 tempbuf_pos
= commit_entry_count
* sizeof(struct tempbuf_searchidx
);
2341 /* Allocate lookup buffer. The first portion of commit_entry_count
2342 * contains the new tags in the temporary file and the second
2343 * part for locating entries already in the db.
2346 * +---------+---------------------------+
2347 * | index | position/ENTRY_CHUNK_SIZE | lookup buffer
2348 * +---------+---------------------------+
2350 * Old tags are inserted to a temporary buffer with position:
2351 * tempbuf_insert(position/ENTRY_CHUNK_SIZE, ...);
2352 * And new tags with index:
2353 * tempbuf_insert(idx, ...);
2355 * The buffer is sorted and written into tag file:
2356 * tempbuf_sort(...);
2357 * leaving master index locations messed up.
2359 * That is fixed using the lookup buffer for old tags:
2360 * new_seek = tempbuf_find_location(old_seek, ...);
2362 * new_seek = tempbuf_find_location(idx);
2364 lookup
= (struct tempbuf_searchidx
**)&tempbuf
[tempbuf_pos
];
2365 tempbuf_pos
+= lookup_buffer_depth
* sizeof(void **);
2366 memset(lookup
, 0, lookup_buffer_depth
* sizeof(void **));
2368 /* And calculate the remaining data space used mainly for storing
2369 * tag data (strings). */
2370 tempbuf_left
= tempbuf_size
- tempbuf_pos
- 8;
2371 if (tempbuf_left
- TAGFILE_ENTRY_AVG_LENGTH
* commit_entry_count
< 0)
2373 logf("Buffer way too small!");
2380 * If tag file contains unique tags (sorted index), we will load
2381 * it entirely into memory so we can resort it later for use with
2384 if (tagcache_is_sorted_tag(index_type
))
2386 logf("loading tags...");
2387 for (i
= 0; i
< tch
.entry_count
; i
++)
2389 struct tagfile_entry entry
;
2390 int loc
= lseek(fd
, 0, SEEK_CUR
);
2393 if (ecread(fd
, &entry
, 1, tagfile_entry_ec
, tc_stat
.econ
)
2394 != sizeof(struct tagfile_entry
))
2396 logf("read error #7");
2401 if (entry
.tag_length
>= (int)sizeof(buf
))
2403 logf("too long tag #3");
2408 if (read(fd
, buf
, entry
.tag_length
) != entry
.tag_length
)
2410 logf("read error #8");
2415 /* Skip deleted entries. */
2420 * Save the tag and tag id in the memory buffer. Tag id
2421 * is saved so we can later reindex the master lookup
2422 * table when the index gets resorted.
2424 ret
= tempbuf_insert(buf
, loc
/TAGFILE_ENTRY_CHUNK_LENGTH
2425 + commit_entry_count
, entry
.idx_id
,
2426 tagcache_is_unique_tag(index_type
));
2437 tempbufidx
= tch
.entry_count
;
2442 * Creating new index file to store the tags. No need to preload
2443 * anything whether the index type is sorted or not.
2445 snprintf(buf
, sizeof buf
, TAGCACHE_FILE_INDEX
, index_type
);
2446 fd
= open(buf
, O_WRONLY
| O_CREAT
| O_TRUNC
);
2449 logf("%s open fail", buf
);
2453 tch
.magic
= TAGCACHE_MAGIC
;
2454 tch
.entry_count
= 0;
2457 if (ecwrite(fd
, &tch
, 1, tagcache_header_ec
, tc_stat
.econ
)
2458 != sizeof(struct tagcache_header
))
2460 logf("header write failed");
2466 /* Loading the tag lookup file as "master file". */
2467 logf("Loading index file");
2468 masterfd
= open(TAGCACHE_FILE_MASTER
, O_RDWR
);
2472 logf("Creating new DB");
2473 masterfd
= open(TAGCACHE_FILE_MASTER
, O_WRONLY
| O_CREAT
| O_TRUNC
);
2477 logf("Failure to create index file (%s)", TAGCACHE_FILE_MASTER
);
2482 /* Write the header (write real values later). */
2483 memset(&tcmh
, 0, sizeof(struct master_header
));
2485 tcmh
.tch
.entry_count
= 0;
2486 tcmh
.tch
.datasize
= 0;
2488 ecwrite(masterfd
, &tcmh
, 1, master_header_ec
, tc_stat
.econ
);
2490 masterfd_pos
= lseek(masterfd
, 0, SEEK_CUR
);
2495 * Master file already exists so we need to process the current
2500 if (ecread(masterfd
, &tcmh
, 1, master_header_ec
, tc_stat
.econ
) !=
2501 sizeof(struct master_header
) || tcmh
.tch
.magic
!= TAGCACHE_MAGIC
)
2503 logf("header error");
2510 * If we reach end of the master file, we need to expand it to
2511 * hold new tags. If the current index is not sorted, we can
2512 * simply append new data to end of the file.
2513 * However, if the index is sorted, we need to update all tag
2514 * pointers in the master file for the current index.
2516 masterfd_pos
= lseek(masterfd
, tcmh
.tch
.entry_count
* sizeof(struct index_entry
),
2518 if (masterfd_pos
== filesize(masterfd
))
2520 logf("appending...");
2526 * Load new unique tags in memory to be sorted later and added
2527 * to the master lookup file.
2529 if (tagcache_is_sorted_tag(index_type
))
2531 lseek(tmpfd
, sizeof(struct tagcache_header
), SEEK_SET
);
2532 /* h is the header of the temporary file containing new tags. */
2533 logf("inserting new tags...");
2534 for (i
= 0; i
< h
->entry_count
; i
++)
2536 struct temp_file_entry entry
;
2538 if (read(tmpfd
, &entry
, sizeof(struct temp_file_entry
)) !=
2539 sizeof(struct temp_file_entry
))
2541 logf("read fail #3");
2547 if (entry
.tag_length
[index_type
] >= (long)sizeof(buf
))
2549 logf("too long entry!");
2554 lseek(tmpfd
, entry
.tag_offset
[index_type
], SEEK_CUR
);
2555 if (read(tmpfd
, buf
, entry
.tag_length
[index_type
]) !=
2556 entry
.tag_length
[index_type
])
2558 logf("read fail #4");
2563 if (tagcache_is_unique_tag(index_type
))
2564 error
= !tempbuf_insert(buf
, i
, -1, true);
2566 error
= !tempbuf_insert(buf
, i
, tcmh
.tch
.entry_count
+ i
, false);
2570 logf("insert error");
2575 lseek(tmpfd
, entry
.data_length
- entry
.tag_offset
[index_type
] -
2576 entry
.tag_length
[index_type
], SEEK_CUR
);
2581 /* Sort the buffer data and write it to the index file. */
2582 lseek(fd
, sizeof(struct tagcache_header
), SEEK_SET
);
2583 i
= tempbuf_sort(fd
);
2586 logf("sorted %d tags", i
);
2589 * Now update all indexes in the master lookup file.
2591 logf("updating indices...");
2592 lseek(masterfd
, sizeof(struct master_header
), SEEK_SET
);
2593 for (i
= 0; i
< tcmh
.tch
.entry_count
; i
+= idxbuf_pos
)
2596 int loc
= lseek(masterfd
, 0, SEEK_CUR
);
2598 idxbuf_pos
= MIN(tcmh
.tch
.entry_count
- i
, IDX_BUF_DEPTH
);
2600 if (ecread(masterfd
, idxbuf
, idxbuf_pos
, index_entry_ec
, tc_stat
.econ
)
2601 != (int)sizeof(struct index_entry
)*idxbuf_pos
)
2603 logf("read fail #5");
2607 lseek(masterfd
, loc
, SEEK_SET
);
2609 for (j
= 0; j
< idxbuf_pos
; j
++)
2611 if (idxbuf
[j
].flag
& FLAG_DELETED
)
2613 /* We can just ignore deleted entries. */
2614 // idxbuf[j].tag_seek[index_type] = 0;
2618 idxbuf
[j
].tag_seek
[index_type
] = tempbuf_find_location(
2619 idxbuf
[j
].tag_seek
[index_type
]/TAGFILE_ENTRY_CHUNK_LENGTH
2620 + commit_entry_count
);
2622 if (idxbuf
[j
].tag_seek
[index_type
] < 0)
2624 logf("update error: %d/%d/%ld",
2625 idxbuf
[j
].flag
, i
+j
, tcmh
.tch
.entry_count
);
2633 /* Write back the updated index. */
2634 if (ecwrite(masterfd
, idxbuf
, idxbuf_pos
,
2635 index_entry_ec
, tc_stat
.econ
) !=
2636 (int)sizeof(struct index_entry
)*idxbuf_pos
)
2647 * Walk through the temporary file containing the new tags.
2649 // build_normal_index(h, tmpfd, masterfd, idx);
2650 logf("updating new indices...");
2651 lseek(masterfd
, masterfd_pos
, SEEK_SET
);
2652 lseek(tmpfd
, sizeof(struct tagcache_header
), SEEK_SET
);
2653 lseek(fd
, 0, SEEK_END
);
2654 for (i
= 0; i
< h
->entry_count
; i
+= idxbuf_pos
)
2658 idxbuf_pos
= MIN(h
->entry_count
- i
, IDX_BUF_DEPTH
);
2661 memset(idxbuf
, 0, sizeof(struct index_entry
)*IDX_BUF_DEPTH
);
2665 int loc
= lseek(masterfd
, 0, SEEK_CUR
);
2667 if (ecread(masterfd
, idxbuf
, idxbuf_pos
, index_entry_ec
, tc_stat
.econ
)
2668 != (int)sizeof(struct index_entry
)*idxbuf_pos
)
2670 logf("read fail #6");
2674 lseek(masterfd
, loc
, SEEK_SET
);
2677 /* Read entry headers. */
2678 for (j
= 0; j
< idxbuf_pos
; j
++)
2680 if (!tagcache_is_sorted_tag(index_type
))
2682 struct temp_file_entry entry
;
2683 struct tagfile_entry fe
;
2685 if (read(tmpfd
, &entry
, sizeof(struct temp_file_entry
)) !=
2686 sizeof(struct temp_file_entry
))
2688 logf("read fail #7");
2694 if (entry
.tag_length
[index_type
] >= (int)sizeof(buf
))
2696 logf("too long entry!");
2697 logf("length=%d", entry
.tag_length
[index_type
]);
2698 logf("pos=0x%02lx", lseek(tmpfd
, 0, SEEK_CUR
));
2703 lseek(tmpfd
, entry
.tag_offset
[index_type
], SEEK_CUR
);
2704 if (read(tmpfd
, buf
, entry
.tag_length
[index_type
]) !=
2705 entry
.tag_length
[index_type
])
2707 logf("read fail #8");
2708 logf("offset=0x%02lx", entry
.tag_offset
[index_type
]);
2709 logf("length=0x%02x", entry
.tag_length
[index_type
]);
2714 /* Write to index file. */
2715 idxbuf
[j
].tag_seek
[index_type
] = lseek(fd
, 0, SEEK_CUR
);
2716 fe
.tag_length
= entry
.tag_length
[index_type
];
2717 fe
.idx_id
= tcmh
.tch
.entry_count
+ i
+ j
;
2718 ecwrite(fd
, &fe
, 1, tagfile_entry_ec
, tc_stat
.econ
);
2719 write(fd
, buf
, fe
.tag_length
);
2723 lseek(tmpfd
, entry
.data_length
- entry
.tag_offset
[index_type
] -
2724 entry
.tag_length
[index_type
], SEEK_CUR
);
2728 /* Locate the correct entry from the sorted array. */
2729 idxbuf
[j
].tag_seek
[index_type
] = tempbuf_find_location(i
+ j
);
2730 if (idxbuf
[j
].tag_seek
[index_type
] < 0)
2732 logf("entry not found (%d)", j
);
2740 if (ecwrite(masterfd
, idxbuf
, idxbuf_pos
,
2741 index_entry_ec
, tc_stat
.econ
) !=
2742 (int)sizeof(struct index_entry
)*idxbuf_pos
)
2744 logf("tagcache: write fail #4");
2753 /* Finally write the header. */
2754 tch
.magic
= TAGCACHE_MAGIC
;
2755 tch
.entry_count
= tempbufidx
;
2756 tch
.datasize
= lseek(fd
, 0, SEEK_END
) - sizeof(struct tagcache_header
);
2757 lseek(fd
, 0, SEEK_SET
);
2758 ecwrite(fd
, &tch
, 1, tagcache_header_ec
, tc_stat
.econ
);
2760 if (index_type
!= tag_filename
)
2761 h
->datasize
+= tch
.datasize
;
2762 logf("s:%d/%ld/%ld", index_type
, tch
.datasize
, h
->datasize
);
2774 static bool commit(void)
2776 struct tagcache_header tch
;
2777 struct master_header tcmh
;
2781 #ifdef HAVE_DIRCACHE
2782 bool dircache_buffer_stolen
= false;
2784 bool local_allocation
= false;
2786 logf("committing tagcache");
2791 tmpfd
= open(TAGCACHE_FILE_TEMP
, O_RDONLY
);
2794 logf("nothing to commit");
2799 /* Load the header. */
2800 len
= sizeof(struct tagcache_header
);
2801 rc
= read(tmpfd
, &tch
, len
);
2803 if (tch
.magic
!= TAGCACHE_MAGIC
|| rc
!= len
)
2805 logf("incorrect header");
2807 remove(TAGCACHE_FILE_TEMP
);
2811 if (tch
.entry_count
== 0)
2813 logf("nothing to commit");
2815 remove(TAGCACHE_FILE_TEMP
);
2819 #ifdef HAVE_EEPROM_SETTINGS
2820 remove(TAGCACHE_STATEFILE
);
2823 /* At first be sure to unload the ramcache! */
2824 #ifdef HAVE_TC_RAMCACHE
2825 tc_stat
.ramcache
= false;
2830 /* Try to steal every buffer we can :) */
2831 if (tempbuf_size
== 0)
2832 local_allocation
= true;
2834 #ifdef HAVE_DIRCACHE
2835 if (tempbuf_size
== 0)
2837 /* Try to steal the dircache buffer. */
2838 tempbuf
= dircache_steal_buffer(&tempbuf_size
);
2839 tempbuf_size
&= ~0x03;
2841 if (tempbuf_size
> 0)
2843 dircache_buffer_stolen
= true;
2848 #ifdef HAVE_TC_RAMCACHE
2849 if (tempbuf_size
== 0 && tc_stat
.ramcache_allocated
> 0)
2851 tempbuf
= (char *)(hdr
+ 1);
2852 tempbuf_size
= tc_stat
.ramcache_allocated
- sizeof(struct ramcache_header
) - 128;
2853 tempbuf_size
&= ~0x03;
2857 /* And finally fail if there are no buffers available. */
2858 if (tempbuf_size
== 0)
2860 logf("delaying commit until next boot");
2861 tc_stat
.commit_delayed
= true;
2867 logf("commit %ld entries...", tch
.entry_count
);
2869 /* Mark DB dirty so it will stay disabled if commit fails. */
2870 current_tcmh
.dirty
= true;
2871 update_master_header();
2873 /* Now create the index files. */
2874 tc_stat
.commit_step
= 0;
2876 tc_stat
.commit_delayed
= false;
2878 for (i
= 0; i
< TAG_COUNT
; i
++)
2882 if (tagcache_is_numeric_tag(i
))
2885 tc_stat
.commit_step
++;
2886 ret
= build_index(i
, &tch
, tmpfd
);
2890 logf("tagcache failed init");
2894 tc_stat
.commit_delayed
= true;
2895 tc_stat
.commit_step
= 0;
2901 if (!build_numeric_indices(&tch
, tmpfd
))
2903 logf("Failure to commit numeric indices");
2906 tc_stat
.commit_step
= 0;
2912 tc_stat
.commit_step
= 0;
2914 /* Update the master index headers. */
2915 if ( (masterfd
= open_master_fd(&tcmh
, true)) < 0)
2921 tcmh
.tch
.entry_count
+= tch
.entry_count
;
2922 tcmh
.tch
.datasize
= sizeof(struct master_header
)
2923 + sizeof(struct index_entry
) * tcmh
.tch
.entry_count
2928 lseek(masterfd
, 0, SEEK_SET
);
2929 ecwrite(masterfd
, &tcmh
, 1, master_header_ec
, tc_stat
.econ
);
2932 logf("tagcache committed");
2933 remove(TAGCACHE_FILE_TEMP
);
2934 tc_stat
.ready
= check_all_headers();
2935 tc_stat
.readyvalid
= true;
2937 if (local_allocation
)
2943 #ifdef HAVE_DIRCACHE
2944 /* Rebuild the dircache, if we stole the buffer. */
2945 if (dircache_buffer_stolen
)
2949 #ifdef HAVE_TC_RAMCACHE
2950 /* Reload tagcache. */
2951 if (tc_stat
.ramcache_allocated
> 0)
2952 tagcache_start_scan();
2960 static void allocate_tempbuf(void)
2962 /* Yeah, malloc would be really nice now :) */
2964 tempbuf_size
= 32*1024*1024;
2965 tempbuf
= malloc(tempbuf_size
);
2967 tempbuf
= (char *)(((long)audiobuf
& ~0x03) + 0x04);
2968 tempbuf_size
= (long)audiobufend
- (long)audiobuf
- 4;
2969 audiobuf
+= tempbuf_size
;
2973 static void free_tempbuf(void)
2975 if (tempbuf_size
== 0)
2981 audiobuf
-= tempbuf_size
;
2987 static bool modify_numeric_entry(int masterfd
, int idx_id
, int tag
, long data
)
2989 struct index_entry idx
;
2994 if (!tagcache_is_numeric_tag(tag
))
2997 if (!get_index(masterfd
, idx_id
, &idx
, false))
3000 idx
.tag_seek
[tag
] = data
;
3001 idx
.flag
|= FLAG_DIRTYNUM
;
3003 return write_index(masterfd
, idx_id
, &idx
);
3007 bool tagcache_modify_numeric_entry(struct tagcache_search
*tcs
,
3010 struct master_header myhdr
;
3012 if (tcs
->masterfd
< 0)
3014 if ( (tcs
->masterfd
= open_master_fd(&myhdr
, true)) < 0)
3018 return modify_numeric_entry(tcs
->masterfd
, tcs
->idx_id
, tag
, data
);
3022 #define COMMAND_QUEUE_IS_EMPTY (command_queue_ridx == command_queue_widx)
3024 static bool command_queue_is_full(void)
3028 next
= command_queue_widx
+ 1;
3029 if (next
>= TAGCACHE_COMMAND_QUEUE_LENGTH
)
3032 return (next
== command_queue_ridx
);
3034 static bool command_queue_sync_callback(void)
3037 struct master_header myhdr
;
3040 mutex_lock(&command_queue_mutex
);
3042 if ( (masterfd
= open_master_fd(&myhdr
, true)) < 0)
3045 while (command_queue_ridx
!= command_queue_widx
)
3047 struct tagcache_command_entry
*ce
= &command_queue
[command_queue_ridx
];
3049 switch (ce
->command
)
3051 case CMD_UPDATE_MASTER_HEADER
:
3054 update_master_header();
3056 /* Re-open the masterfd. */
3057 if ( (masterfd
= open_master_fd(&myhdr
, true)) < 0)
3062 case CMD_UPDATE_NUMERIC
:
3064 modify_numeric_entry(masterfd
, ce
->idx_id
, ce
->tag
, ce
->data
);
3069 if (++command_queue_ridx
>= TAGCACHE_COMMAND_QUEUE_LENGTH
)
3070 command_queue_ridx
= 0;
3075 tc_stat
.queue_length
= 0;
3076 mutex_unlock(&command_queue_mutex
);
3080 static void run_command_queue(bool force
)
3082 if (COMMAND_QUEUE_IS_EMPTY
)
3085 if (force
|| command_queue_is_full())
3086 command_queue_sync_callback();
3088 register_ata_idle_func(command_queue_sync_callback
);
3091 static void queue_command(int cmd
, long idx_id
, int tag
, long data
)
3097 mutex_lock(&command_queue_mutex
);
3098 next
= command_queue_widx
+ 1;
3099 if (next
>= TAGCACHE_COMMAND_QUEUE_LENGTH
)
3102 /* Make sure queue is not full. */
3103 if (next
!= command_queue_ridx
)
3105 struct tagcache_command_entry
*ce
= &command_queue
[command_queue_widx
];
3108 ce
->idx_id
= idx_id
;
3112 command_queue_widx
= next
;
3114 tc_stat
.queue_length
++;
3116 mutex_unlock(&command_queue_mutex
);
3120 /* Queue is full, try again later... */
3121 mutex_unlock(&command_queue_mutex
);
3126 long tagcache_increase_serial(void)
3136 old
= current_tcmh
.serial
++;
3137 queue_command(CMD_UPDATE_MASTER_HEADER
, 0, 0, 0);
3142 void tagcache_update_numeric(int idx_id
, int tag
, long data
)
3144 queue_command(CMD_UPDATE_NUMERIC
, idx_id
, tag
, data
);
3147 long tagcache_get_serial(void)
3149 return current_tcmh
.serial
;
3152 long tagcache_get_commitid(void)
3154 return current_tcmh
.commitid
;
3157 static bool write_tag(int fd
, const char *tagstr
, const char *datastr
)
3162 snprintf(buf
, sizeof buf
, "%s=\"", tagstr
);
3163 for (i
= strlen(buf
); i
< (long)sizeof(buf
)-4; i
++)
3165 if (*datastr
== '\0')
3168 if (*datastr
== '"' || *datastr
== '\\')
3171 buf
[i
] = *(datastr
++);
3174 strcpy(&buf
[i
], "\" ");
3176 write(fd
, buf
, i
+ 2);
3181 static bool read_tag(char *dest
, long size
,
3182 const char *src
, const char *tagstr
)
3185 char current_tag
[32];
3187 while (*src
!= '\0')
3189 /* Skip all whitespace */
3197 /* Read in tag name */
3198 while (*src
!= '=' && *src
!= ' ')
3200 current_tag
[pos
] = *src
;
3204 if (*src
== '\0' || pos
>= (long)sizeof(current_tag
))
3207 current_tag
[pos
] = '\0';
3209 /* Read in tag data */
3211 /* Find the start. */
3212 while (*src
!= '"' && *src
!= '\0')
3215 if (*src
== '\0' || *(++src
) == '\0')
3218 /* Read the data. */
3219 for (pos
= 0; pos
< size
; pos
++)
3226 dest
[pos
] = *(src
+1);
3246 if (!strcasecmp(tagstr
, current_tag
))
3253 static int parse_changelog_line(int line_n
, const char *buf
, void *parameters
)
3255 struct index_entry idx
;
3256 char tag_data
[TAG_MAXLEN
+32];
3258 long masterfd
= (long)parameters
;
3259 const int import_tags
[] = { tag_playcount
, tag_rating
, tag_playtime
, tag_lastplayed
,
3267 logf("%d/%s", line_n
, buf
);
3268 if (!read_tag(tag_data
, sizeof tag_data
, buf
, "filename"))
3270 logf("filename missing");
3275 idx_id
= find_index(tag_data
);
3278 logf("entry not found");
3282 if (!get_index(masterfd
, idx_id
, &idx
, false))
3284 logf("failed to retrieve index entry");
3288 /* Stop if tag has already been modified. */
3289 if (idx
.flag
& FLAG_DIRTYNUM
)
3292 logf("import: %s", tag_data
);
3294 idx
.flag
|= FLAG_DIRTYNUM
;
3295 for (i
= 0; i
< (long)(sizeof(import_tags
)/sizeof(import_tags
[0])); i
++)
3299 if (!read_tag(tag_data
, sizeof tag_data
, buf
,
3300 tagcache_tag_to_str(import_tags
[i
])))
3305 data
= atoi(tag_data
);
3309 idx
.tag_seek
[import_tags
[i
]] = data
;
3311 if (import_tags
[i
] == tag_lastplayed
&& data
> current_tcmh
.serial
)
3312 current_tcmh
.serial
= data
;
3313 else if (import_tags
[i
] == tag_commitid
&& data
>= current_tcmh
.commitid
)
3314 current_tcmh
.commitid
= data
+ 1;
3317 return write_index(masterfd
, idx_id
, &idx
) ? 0 : -5;
3321 bool tagcache_import_changelog(void)
3323 struct master_header myhdr
;
3324 struct tagcache_header tch
;
3335 clfd
= open(TAGCACHE_FILE_CHANGELOG
, O_RDONLY
);
3338 logf("failure to open changelog");
3342 if ( (masterfd
= open_master_fd(&myhdr
, true)) < 0)
3350 filenametag_fd
= open_tag_fd(&tch
, tag_filename
, false);
3352 fast_readline(clfd
, buf
, sizeof buf
, (long *)masterfd
,
3353 parse_changelog_line
);
3358 if (filenametag_fd
>= 0)
3359 close(filenametag_fd
);
3363 update_master_header();
3369 bool tagcache_create_changelog(struct tagcache_search
*tcs
)
3371 struct master_header myhdr
;
3372 struct index_entry idx
;
3373 char buf
[TAG_MAXLEN
+32];
3381 if (!tagcache_search(tcs
, tag_filename
))
3384 /* Initialize the changelog */
3385 clfd
= open(TAGCACHE_FILE_CHANGELOG
, O_WRONLY
| O_CREAT
| O_TRUNC
);
3388 logf("failure to open changelog");
3392 if (tcs
->masterfd
< 0)
3394 if ( (tcs
->masterfd
= open_master_fd(&myhdr
, false)) < 0)
3399 lseek(tcs
->masterfd
, 0, SEEK_SET
);
3400 ecread(tcs
->masterfd
, &myhdr
, 1, master_header_ec
, tc_stat
.econ
);
3403 write(clfd
, "## Changelog version 1\n", 23);
3405 for (i
= 0; i
< myhdr
.tch
.entry_count
; i
++)
3407 if (ecread(tcs
->masterfd
, &idx
, 1, index_entry_ec
, tc_stat
.econ
)
3408 != sizeof(struct index_entry
))
3410 logf("read error #9");
3411 tagcache_search_finish(tcs
);
3416 /* Skip until the entry found has been modified. */
3417 if (! (idx
.flag
& FLAG_DIRTYNUM
) )
3420 /* Skip deleted entries too. */
3421 if (idx
.flag
& FLAG_DELETED
)
3424 /* Now retrieve all tags. */
3425 for (j
= 0; j
< TAG_COUNT
; j
++)
3427 if (tagcache_is_numeric_tag(j
))
3429 snprintf(temp
, sizeof temp
, "%d", idx
.tag_seek
[j
]);
3430 write_tag(clfd
, tagcache_tag_to_str(j
), temp
);
3435 tagcache_retrieve(tcs
, i
, tcs
->type
, buf
, sizeof buf
);
3436 write_tag(clfd
, tagcache_tag_to_str(j
), buf
);
3439 write(clfd
, "\n", 1);
3445 tagcache_search_finish(tcs
);
3450 static bool delete_entry(long idx_id
)
3455 struct index_entry idx
, myidx
;
3456 struct master_header myhdr
;
3457 char buf
[TAG_MAXLEN
+32];
3458 int in_use
[TAG_COUNT
];
3460 logf("delete_entry(): %ld", idx_id
);
3462 #ifdef HAVE_TC_RAMCACHE
3463 /* At first mark the entry removed from ram cache. */
3464 if (tc_stat
.ramcache
)
3465 hdr
->indices
[idx_id
].flag
|= FLAG_DELETED
;
3468 if ( (masterfd
= open_master_fd(&myhdr
, true) ) < 0)
3471 lseek(masterfd
, idx_id
* sizeof(struct index_entry
), SEEK_CUR
);
3472 if (ecread(masterfd
, &myidx
, 1, index_entry_ec
, tc_stat
.econ
)
3473 != sizeof(struct index_entry
))
3475 logf("delete_entry(): read error");
3479 if (myidx
.flag
& FLAG_DELETED
)
3481 logf("delete_entry(): already deleted!");
3485 myidx
.flag
|= FLAG_DELETED
;
3486 lseek(masterfd
, -sizeof(struct index_entry
), SEEK_CUR
);
3487 if (ecwrite(masterfd
, &myidx
, 1, index_entry_ec
, tc_stat
.econ
)
3488 != sizeof(struct index_entry
))
3490 logf("delete_entry(): write_error #1");
3494 /* Now check which tags are no longer in use (if any) */
3495 for (tag
= 0; tag
< TAG_COUNT
; tag
++)
3498 lseek(masterfd
, sizeof(struct master_header
), SEEK_SET
);
3499 for (i
= 0; i
< myhdr
.tch
.entry_count
; i
++)
3501 struct index_entry
*idxp
;
3503 #ifdef HAVE_TC_RAMCACHE
3504 /* Use RAM DB if available for greater speed */
3505 if (tc_stat
.ramcache
)
3506 idxp
= &hdr
->indices
[i
];
3510 if (ecread(masterfd
, &idx
, 1, index_entry_ec
, tc_stat
.econ
)
3511 != sizeof(struct index_entry
))
3513 logf("delete_entry(): read error #2");
3519 if (idxp
->flag
& FLAG_DELETED
)
3522 for (tag
= 0; tag
< TAG_COUNT
; tag
++)
3524 if (tagcache_is_numeric_tag(tag
))
3527 if (idxp
->tag_seek
[tag
] == myidx
.tag_seek
[tag
])
3532 /* Now delete all tags no longer in use. */
3533 for (tag
= 0; tag
< TAG_COUNT
; tag
++)
3535 struct tagcache_header tch
;
3536 int oldseek
= myidx
.tag_seek
[tag
];
3538 if (tagcache_is_numeric_tag(tag
))
3542 * Replace tag seek with a hash value of the field string data.
3543 * That way runtime statistics of moved or altered files can be
3546 #ifdef HAVE_TC_RAMCACHE
3547 if (tc_stat
.ramcache
&& tag
!= tag_filename
)
3549 struct tagfile_entry
*tfe
;
3550 int32_t *seek
= &hdr
->indices
[idx_id
].tag_seek
[tag
];
3552 tfe
= (struct tagfile_entry
*)&hdr
->tags
[tag
][*seek
];
3553 *seek
= crc_32(tfe
->tag_data
, strlen(tfe
->tag_data
), 0xffffffff);
3554 myidx
.tag_seek
[tag
] = *seek
;
3559 struct tagfile_entry tfe
;
3561 /* Open the index file, which contains the tag names. */
3562 if ((fd
= open_tag_fd(&tch
, tag
, true)) < 0)
3565 /* Skip the header block */
3566 lseek(fd
, myidx
.tag_seek
[tag
], SEEK_SET
);
3567 if (ecread(fd
, &tfe
, 1, tagfile_entry_ec
, tc_stat
.econ
)
3568 != sizeof(struct tagfile_entry
))
3570 logf("delete_entry(): read error #3");
3574 if (read(fd
, buf
, tfe
.tag_length
) != tfe
.tag_length
)
3576 logf("delete_entry(): read error #4");
3580 myidx
.tag_seek
[tag
] = crc_32(buf
, strlen(buf
), 0xffffffff);
3585 logf("in use: %d/%d", tag
, in_use
[tag
]);
3594 #ifdef HAVE_TC_RAMCACHE
3595 /* Delete from ram. */
3596 if (tc_stat
.ramcache
&& tag
!= tag_filename
)
3598 struct tagfile_entry
*tagentry
= (struct tagfile_entry
*)&hdr
->tags
[tag
][oldseek
];
3599 tagentry
->tag_data
[0] = '\0';
3603 /* Open the index file, which contains the tag names. */
3606 if ((fd
= open_tag_fd(&tch
, tag
, true)) < 0)
3610 /* Skip the header block */
3611 lseek(fd
, oldseek
+ sizeof(struct tagfile_entry
), SEEK_SET
);
3613 /* Debug, print 10 first characters of the tag
3616 logf("TAG:%s", buf);
3617 lseek(fd, -10, SEEK_CUR);
3620 /* Write first data byte in tag as \0 */
3623 /* Now tag data has been removed */
3628 /* Write index entry back into master index. */
3629 lseek(masterfd
, sizeof(struct master_header
) +
3630 (idx_id
* sizeof(struct index_entry
)), SEEK_SET
);
3631 if (ecwrite(masterfd
, &myidx
, 1, index_entry_ec
, tc_stat
.econ
)
3632 != sizeof(struct index_entry
))
3634 logf("delete_entry(): write_error #2");
3653 * Returns true if there is an event waiting in the queue
3654 * that requires the current operation to be aborted.
3656 static bool check_event_queue(void)
3658 struct queue_event ev
;
3660 queue_wait_w_tmo(&tagcache_queue
, &ev
, 0);
3665 case SYS_USB_CONNECTED
:
3666 /* Put the event back into the queue. */
3667 queue_post(&tagcache_queue
, ev
.id
, ev
.data
);
3675 #ifdef HAVE_TC_RAMCACHE
3676 static bool allocate_tagcache(void)
3678 struct master_header tcmh
;
3681 /* Load the header. */
3682 if ( (fd
= open_master_fd(&tcmh
, false)) < 0)
3691 * Now calculate the required cache size plus
3692 * some extra space for alignment fixes.
3694 tc_stat
.ramcache_allocated
= tcmh
.tch
.datasize
+ 128 + TAGCACHE_RESERVE
+
3695 sizeof(struct ramcache_header
) + TAG_COUNT
*sizeof(void *);
3696 hdr
= buffer_alloc(tc_stat
.ramcache_allocated
+ 128);
3697 memset(hdr
, 0, sizeof(struct ramcache_header
));
3698 memcpy(&hdr
->h
, &tcmh
, sizeof(struct master_header
));
3699 hdr
->indices
= (struct index_entry
*)(hdr
+ 1);
3700 logf("tagcache: %d bytes allocated.", tc_stat
.ramcache_allocated
);
3705 # ifdef HAVE_EEPROM_SETTINGS
3706 static bool tagcache_dumpload(void)
3708 struct statefile_header shdr
;
3713 fd
= open(TAGCACHE_STATEFILE
, O_RDONLY
);
3716 logf("no tagcache statedump");
3720 /* Check the statefile memory placement */
3721 hdr
= buffer_alloc(0);
3722 rc
= read(fd
, &shdr
, sizeof(struct statefile_header
));
3723 if (rc
!= sizeof(struct statefile_header
)
3724 /* || (long)hdr != (long)shdr.hdr */)
3726 logf("incorrect statefile");
3732 offpos
= (long)hdr
- (long)shdr
.hdr
;
3734 /* Lets allocate real memory and load it */
3735 hdr
= buffer_alloc(shdr
.tc_stat
.ramcache_allocated
);
3736 rc
= read(fd
, hdr
, shdr
.tc_stat
.ramcache_allocated
);
3739 if (rc
!= shdr
.tc_stat
.ramcache_allocated
)
3741 logf("read failure!");
3746 memcpy(&tc_stat
, &shdr
.tc_stat
, sizeof(struct tagcache_stat
));
3748 /* Now fix the pointers */
3749 hdr
->indices
= (struct index_entry
*)((long)hdr
->indices
+ offpos
);
3750 for (i
= 0; i
< TAG_COUNT
; i
++)
3751 hdr
->tags
[i
] += offpos
;
3756 static bool tagcache_dumpsave(void)
3758 struct statefile_header shdr
;
3761 if (!tc_stat
.ramcache
)
3764 fd
= open(TAGCACHE_STATEFILE
, O_WRONLY
| O_CREAT
| O_TRUNC
);
3767 logf("failed to create a statedump");
3771 /* Create the header */
3773 memcpy(&shdr
.tc_stat
, &tc_stat
, sizeof(struct tagcache_stat
));
3774 write(fd
, &shdr
, sizeof(struct statefile_header
));
3776 /* And dump the data too */
3777 write(fd
, hdr
, tc_stat
.ramcache_allocated
);
3784 static bool load_tagcache(void)
3786 struct tagcache_header
*tch
;
3787 long bytesleft
= tc_stat
.ramcache_allocated
;
3788 struct index_entry
*idx
;
3793 # ifdef HAVE_DIRCACHE
3794 while (dircache_is_initializing())
3797 dircache_set_appflag(DIRCACHE_APPFLAG_TAGCACHE
);
3800 logf("loading tagcache to ram...");
3802 fd
= open(TAGCACHE_FILE_MASTER
, O_RDONLY
);
3805 logf("tagcache open failed");
3809 if (ecread(fd
, &hdr
->h
, 1, master_header_ec
, tc_stat
.econ
)
3810 != sizeof(struct master_header
)
3811 || hdr
->h
.tch
.magic
!= TAGCACHE_MAGIC
)
3813 logf("incorrect header");
3819 /* Load the master index table. */
3820 for (i
= 0; i
< hdr
->h
.tch
.entry_count
; i
++)
3822 rc
= ecread(fd
, idx
, 1, index_entry_ec
, tc_stat
.econ
);
3823 if (rc
!= sizeof(struct index_entry
))
3825 logf("read error #10");
3830 bytesleft
-= sizeof(struct index_entry
);
3831 if (bytesleft
< 0 || ((long)idx
- (long)hdr
->indices
) >= tc_stat
.ramcache_allocated
)
3833 logf("too big tagcache.");
3843 /* Load the tags. */
3845 for (tag
= 0; tag
< TAG_COUNT
; tag
++)
3847 struct tagfile_entry
*fe
;
3848 char buf
[TAG_MAXLEN
+32];
3850 if (tagcache_is_numeric_tag(tag
))
3853 //p = ((void *)p+1);
3854 p
= (char *)((long)p
& ~0x03) + 0x04;
3857 /* Check the header. */
3858 tch
= (struct tagcache_header
*)p
;
3859 p
+= sizeof(struct tagcache_header
);
3861 if ( (fd
= open_tag_fd(tch
, tag
, false)) < 0)
3864 for (hdr
->entry_count
[tag
] = 0;
3865 hdr
->entry_count
[tag
] < tch
->entry_count
;
3866 hdr
->entry_count
[tag
]++)
3870 if (do_timed_yield())
3872 /* Abort if we got a critical event in queue */
3873 if (check_event_queue())
3877 fe
= (struct tagfile_entry
*)p
;
3878 pos
= lseek(fd
, 0, SEEK_CUR
);
3879 rc
= ecread(fd
, fe
, 1, tagfile_entry_ec
, tc_stat
.econ
);
3880 if (rc
!= sizeof(struct tagfile_entry
))
3882 /* End of lookup table. */
3883 logf("read error #11");
3888 /* We have a special handling for the filename tags. */
3889 if (tag
== tag_filename
)
3891 # ifdef HAVE_DIRCACHE
3892 const struct dirent
*dc
;
3895 // FIXME: This is wrong!
3896 // idx = &hdr->indices[hdr->entry_count[i]];
3897 idx
= &hdr
->indices
[fe
->idx_id
];
3899 if (fe
->tag_length
>= (long)sizeof(buf
)-1)
3903 logf("TAG:%s", buf
);
3904 logf("too long filename");
3909 rc
= read(fd
, buf
, fe
->tag_length
);
3910 if (rc
!= fe
->tag_length
)
3912 logf("read error #12");
3917 /* Check if the entry has already been removed */
3918 if (idx
->flag
& FLAG_DELETED
)
3921 /* This flag must not be used yet. */
3922 if (idx
->flag
& FLAG_DIRCACHE
)
3924 logf("internal error!");
3929 if (idx
->tag_seek
[tag
] != pos
)
3931 logf("corrupt data structures!");
3936 # ifdef HAVE_DIRCACHE
3937 if (dircache_is_enabled())
3939 dc
= dircache_get_entry_ptr(buf
);
3942 logf("Entry no longer valid.");
3944 delete_entry(fe
->idx_id
);
3948 idx
->flag
|= FLAG_DIRCACHE
;
3949 FLAG_SET_ATTR(idx
->flag
, fe
->idx_id
);
3950 idx
->tag_seek
[tag_filename
] = (long)dc
;
3955 /* This will be very slow unless dircache is enabled
3956 or target is flash based, but do it anyway for
3958 /* Check if entry has been removed. */
3959 if (global_settings
.tagcache_autoupdate
)
3961 if (!file_exists(buf
))
3963 logf("Entry no longer valid.");
3965 delete_entry(fe
->idx_id
);
3974 bytesleft
-= sizeof(struct tagfile_entry
) + fe
->tag_length
;
3977 logf("too big tagcache #2");
3978 logf("tl: %d", fe
->tag_length
);
3979 logf("bl: %ld", bytesleft
);
3985 rc
= read(fd
, fe
->tag_data
, fe
->tag_length
);
3988 if (rc
!= fe
->tag_length
)
3990 logf("read error #13");
3991 logf("rc=0x%04x", rc
); // 0x431
3992 logf("len=0x%04x", fe
->tag_length
); // 0x4000
3993 logf("pos=0x%04lx", lseek(fd
, 0, SEEK_CUR
)); // 0x433
3994 logf("tag=0x%02x", tag
); // 0x00
4002 tc_stat
.ramcache_used
= tc_stat
.ramcache_allocated
- bytesleft
;
4003 logf("tagcache loaded into ram!");
4007 #endif /* HAVE_TC_RAMCACHE */
4009 static bool check_deleted_files(void)
4012 char buf
[TAG_MAXLEN
+32];
4013 struct tagfile_entry tfe
;
4015 logf("reverse scan...");
4016 snprintf(buf
, sizeof buf
, TAGCACHE_FILE_INDEX
, tag_filename
);
4017 fd
= open(buf
, O_RDONLY
);
4021 logf("%s open fail", buf
);
4025 lseek(fd
, sizeof(struct tagcache_header
), SEEK_SET
);
4026 while (ecread(fd
, &tfe
, 1, tagfile_entry_ec
, tc_stat
.econ
)
4027 == sizeof(struct tagfile_entry
)
4029 && !check_event_queue()
4033 if (tfe
.tag_length
>= (long)sizeof(buf
)-1)
4035 logf("too long tag");
4040 if (read(fd
, buf
, tfe
.tag_length
) != tfe
.tag_length
)
4042 logf("read error #14");
4047 /* Check if the file has already deleted from the db. */
4051 /* Now check if the file exists. */
4052 if (!file_exists(buf
))
4054 logf("Entry no longer valid.");
4055 logf("-> %s / %d", buf
, tfe
.tag_length
);
4056 delete_entry(tfe
.idx_id
);
4067 static bool check_dir(const char *dirname
, int add_files
)
4071 int success
= false;
4072 int ignore
, unignore
;
4073 char newpath
[MAX_PATH
];
4075 dir
= opendir(dirname
);
4078 logf("tagcache: opendir() failed");
4082 /* check for a database.ignore file */
4083 snprintf(newpath
, MAX_PATH
, "%s/database.ignore", dirname
);
4084 ignore
= file_exists(newpath
);
4085 /* check for a database.unignore file */
4086 snprintf(newpath
, MAX_PATH
, "%s/database.unignore", dirname
);
4087 unignore
= file_exists(newpath
);
4089 /* don't do anything if both ignore and unignore are there */
4090 if (ignore
!= unignore
)
4091 add_files
= unignore
;
4093 /* Recursively scan the dir. */
4097 while (!check_event_queue())
4100 struct dirent
*entry
;
4102 entry
= readdir(dir
);
4110 if (!strcmp((char *)entry
->d_name
, ".") ||
4111 !strcmp((char *)entry
->d_name
, ".."))
4116 len
= strlen(curpath
);
4117 snprintf(&curpath
[len
], curpath_size
- len
, "/%s",
4120 processed_dir_count
++;
4121 if (entry
->attribute
& ATTR_DIRECTORY
)
4122 check_dir(curpath
, add_files
);
4125 tc_stat
.curentry
= curpath
;
4127 /* Add a new entry to the temporary db file. */
4128 add_tagcache(curpath
, (entry
->wrtdate
<< 16) | entry
->wrttime
4129 #if defined(HAVE_TC_RAMCACHE) && defined(HAVE_DIRCACHE)
4130 , dir
->internal_entry
4134 /* Wait until current path for debug screen is read and unset. */
4135 while (tc_stat
.syncscreen
&& tc_stat
.curentry
!= NULL
)
4138 tc_stat
.curentry
= NULL
;
4141 curpath
[len
] = '\0';
4149 void tagcache_screensync_event(void)
4151 tc_stat
.curentry
= NULL
;
4154 void tagcache_screensync_enable(bool state
)
4156 tc_stat
.syncscreen
= state
;
4159 void tagcache_build(const char *path
)
4161 struct tagcache_header header
;
4166 total_entry_count
= 0;
4167 processed_dir_count
= 0;
4169 #ifdef HAVE_DIRCACHE
4170 while (dircache_is_initializing())
4174 logf("updating tagcache");
4176 cachefd
= open(TAGCACHE_FILE_TEMP
, O_RDONLY
);
4179 logf("skipping, cache already waiting for commit");
4184 cachefd
= open(TAGCACHE_FILE_TEMP
, O_RDWR
| O_CREAT
| O_TRUNC
);
4187 logf("master file open failed: %s", TAGCACHE_FILE_TEMP
);
4191 filenametag_fd
= open_tag_fd(&header
, tag_filename
, false);
4195 logf("Scanning files...");
4196 /* Scan for new files. */
4197 memset(&header
, 0, sizeof(struct tagcache_header
));
4198 write(cachefd
, &header
, sizeof(struct tagcache_header
));
4200 if (strcmp("/", path
) != 0)
4201 strcpy(curpath
, path
);
4202 ret
= check_dir(path
, true);
4204 /* Write the header. */
4205 header
.magic
= TAGCACHE_MAGIC
;
4206 header
.datasize
= data_size
;
4207 header
.entry_count
= total_entry_count
;
4208 lseek(cachefd
, 0, SEEK_SET
);
4209 write(cachefd
, &header
, sizeof(struct tagcache_header
));
4212 if (filenametag_fd
>= 0)
4214 close(filenametag_fd
);
4215 filenametag_fd
= -1;
4225 /* Commit changes to the database. */
4231 remove(TAGCACHE_FILE_TEMP
);
4232 logf("tagcache built!");
4238 #ifdef HAVE_TC_RAMCACHE
4241 /* Import runtime statistics if we just initialized the db. */
4242 if (hdr
->h
.serial
== 0)
4243 queue_post(&tagcache_queue
, Q_IMPORT_CHANGELOG
, 0);
4250 #ifdef HAVE_TC_RAMCACHE
4251 static void load_ramcache(void)
4258 /* At first we should load the cache (if exists). */
4259 tc_stat
.ramcache
= load_tagcache();
4261 if (!tc_stat
.ramcache
)
4263 /* If loading failed, it must indicate some problem with the db
4264 * so disable it entirely to prevent further issues. */
4265 tc_stat
.ready
= false;
4272 void tagcache_unload_ramcache(void)
4274 tc_stat
.ramcache
= false;
4275 /* Just to make sure there is no statefile present. */
4276 // remove(TAGCACHE_STATEFILE);
4281 static void tagcache_thread(void)
4283 struct queue_event ev
;
4284 bool check_done
= false;
4286 /* If the previous cache build/update was interrupted, commit
4287 * the changes first in foreground. */
4293 #ifdef HAVE_TC_RAMCACHE
4294 # ifdef HAVE_EEPROM_SETTINGS
4295 if (firmware_settings
.initialized
&& firmware_settings
.disk_clean
)
4296 check_done
= tagcache_dumpload();
4298 remove(TAGCACHE_STATEFILE
);
4301 /* Allocate space for the tagcache if found on disk. */
4302 if (global_settings
.tagcache_ram
&& !tc_stat
.ramcache
)
4303 allocate_tagcache();
4307 tc_stat
.initialized
= true;
4309 /* Don't delay bootup with the header check but do it on background. */
4311 tc_stat
.ready
= check_all_headers();
4312 tc_stat
.readyvalid
= true;
4316 run_command_queue(false);
4318 queue_wait_w_tmo(&tagcache_queue
, &ev
, HZ
);
4322 case Q_IMPORT_CHANGELOG
:
4323 tagcache_import_changelog();
4328 remove(TAGCACHE_FILE_TEMP
);
4329 tagcache_build("/");
4333 tagcache_build("/");
4334 #ifdef HAVE_TC_RAMCACHE
4337 check_deleted_files();
4343 if (check_done
|| !tc_stat
.ready
)
4346 #ifdef HAVE_TC_RAMCACHE
4347 if (!tc_stat
.ramcache
&& global_settings
.tagcache_ram
)
4350 if (tc_stat
.ramcache
&& global_settings
.tagcache_autoupdate
)
4351 tagcache_build("/");
4355 if (global_settings
.tagcache_autoupdate
)
4357 tagcache_build("/");
4359 /* This will be very slow unless dircache is enabled
4360 or target is flash based, but do it anyway for
4362 check_deleted_files();
4365 logf("tagcache check done");
4377 case SYS_USB_CONNECTED
:
4378 logf("USB: TagCache");
4379 usb_acknowledge(SYS_USB_CONNECTED_ACK
);
4380 usb_wait_for_disconnect(&tagcache_queue
);
4387 bool tagcache_prepare_shutdown(void)
4389 if (tagcache_get_commit_step() > 0)
4392 tagcache_stop_scan();
4393 while (read_lock
|| write_lock
)
4399 void tagcache_shutdown(void)
4401 /* Flush the command queue. */
4402 run_command_queue(true);
4404 #ifdef HAVE_EEPROM_SETTINGS
4405 if (tc_stat
.ramcache
)
4406 tagcache_dumpsave();
4410 static int get_progress(void)
4412 int total_count
= -1;
4414 #ifdef HAVE_DIRCACHE
4415 if (dircache_is_enabled())
4417 total_count
= dircache_get_entry_count();
4421 #ifdef HAVE_TC_RAMCACHE
4423 if (hdr
&& tc_stat
.ramcache
)
4424 total_count
= hdr
->h
.tch
.entry_count
;
4428 if (total_count
< 0)
4431 return processed_dir_count
* 100 / total_count
;
4434 struct tagcache_stat
* tagcache_get_stat(void)
4436 tc_stat
.progress
= get_progress();
4437 tc_stat
.processed_entries
= processed_dir_count
;
4442 void tagcache_start_scan(void)
4444 queue_post(&tagcache_queue
, Q_START_SCAN
, 0);
4447 bool tagcache_update(void)
4452 queue_post(&tagcache_queue
, Q_UPDATE
, 0);
4456 bool tagcache_rebuild()
4458 queue_post(&tagcache_queue
, Q_REBUILD
, 0);
4462 void tagcache_stop_scan(void)
4464 queue_post(&tagcache_queue
, Q_STOP_SCAN
, 0);
4467 #ifdef HAVE_TC_RAMCACHE
4468 bool tagcache_is_ramcache(void)
4470 return tc_stat
.ramcache
;
4474 #endif /* !__PCTOOL__ */
4477 void tagcache_init(void)
4479 memset(&tc_stat
, 0, sizeof(struct tagcache_stat
));
4480 memset(¤t_tcmh
, 0, sizeof(struct master_header
));
4481 filenametag_fd
= -1;
4482 write_lock
= read_lock
= 0;
4485 mutex_init(&command_queue_mutex
);
4486 queue_init(&tagcache_queue
, true);
4487 create_thread(tagcache_thread
, tagcache_stack
,
4488 sizeof(tagcache_stack
), 0, tagcache_thread_name
4489 IF_PRIO(, PRIORITY_BACKGROUND
)
4492 tc_stat
.initialized
= true;
4496 tc_stat
.ready
= check_all_headers();
4501 void tagcache_reverse_scan(void)
4503 logf("Checking for deleted files");
4504 check_deleted_files();
4508 bool tagcache_is_initialized(void)
4510 return tc_stat
.initialized
;
4512 bool tagcache_is_usable(void)
4514 return tc_stat
.initialized
&& tc_stat
.ready
;
4516 int tagcache_get_commit_step(void)
4518 return tc_stat
.commit_step
;
4520 int tagcache_get_max_commit_step(void)
4522 return (int)(sizeof(sorted_tags
)/sizeof(sorted_tags
[0]))+1;