1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
10 * Copyright (C) 2005 by Miika Pekkarinen
12 * All files in this archive are subject to the GNU General Public License.
13 * See the file COPYING in the source tree root for full license agreement.
15 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
16 * KIND, either express or implied.
18 ****************************************************************************/
23 * ----------x---------x------------------x-----
25 * +---------------x-------+ | TagCache | Libraries
26 * | Modification routines | | Core |
27 * +-x---------x-----------+ | |
29 * | +------x-------------x-+ +-------------x-----+ |
30 * | | x==x Filters & clauses | |
31 * | | Search routines | +-------------------+ |
32 * | | x============================x DirCache
33 * | +-x--------------------+ | (optional)
35 * | | +-------------------------------+ +---------+ |
36 * | | | DB Commit (sort,unique,index) | | | |
37 * | | +-x--------------------------x--+ | Control | |
38 * | | | (R/W) | (R) | Thread | |
39 * | | | +----------------------+ | | | |
40 * | | | | TagCache DB Builder | | +---------+ |
41 * | | | +-x-------------x------+ | |
42 * | | | | (R) | (W) | |
43 * | | | | +--x--------x---------+ |
44 * | | | | | Temporary Commit DB | |
45 * | | | | +---------------------+ |
46 * +-x----x-------x--+ |
47 * | TagCache RAM DB x==\(W) +-----------------+ |
48 * +-----------------+ \===x | |
49 * | | | | (R) | Ram DB Loader x============x DirCache
50 * +-x----x---x---x---+ /==x | | (optional)
51 * | Tagcache Disk DB x==/ +-----------------+ |
52 * +------------------+ |
79 #include "eeprom_settings.h"
83 #define yield() do { } while(0)
84 #define sim_sleep(timeout) do { } while(0)
85 #define do_timed_yield() do { } while(0)
90 /* Tag Cache thread. */
91 static struct event_queue tagcache_queue
;
92 static long tagcache_stack
[(DEFAULT_STACK_SIZE
+ 0x4000)/sizeof(long)];
93 static const char tagcache_thread_name
[] = "tagcache";
96 #define UNTAGGED "<Untagged>"
98 /* Previous path when scanning directory tree recursively. */
99 static char curpath
[TAG_MAXLEN
+32];
100 static long curpath_size
= sizeof(curpath
);
102 /* Used when removing duplicates. */
103 static char *tempbuf
; /* Allocated when needed. */
104 static long tempbufidx
; /* Current location in buffer. */
105 static long tempbuf_size
; /* Buffer size (TEMPBUF_SIZE). */
106 static long tempbuf_left
; /* Buffer space left. */
107 static long tempbuf_pos
;
109 /* Tags we want to get sorted (loaded to the tempbuf). */
110 static const int sorted_tags
[] = { tag_artist
, tag_album
, tag_genre
,
111 tag_composer
, tag_comment
, tag_albumartist
, tag_grouping
, tag_title
};
113 /* Uniqued tags (we can use these tags with filters and conditional clauses). */
114 static const int unique_tags
[] = { tag_artist
, tag_album
, tag_genre
,
115 tag_composer
, tag_comment
, tag_albumartist
, tag_grouping
};
117 /* Numeric tags (we can use these tags with conditional clauses). */
118 static const int numeric_tags
[] = { tag_year
, tag_discnumber
, tag_tracknumber
, tag_length
,
119 tag_bitrate
, tag_playcount
, tag_rating
, tag_playtime
, tag_lastplayed
, tag_commitid
,
120 tag_virt_length_min
, tag_virt_length_sec
,
121 tag_virt_playtime_min
, tag_virt_playtime_sec
,
122 tag_virt_entryage
, tag_virt_autoscore
};
124 /* String presentation of the tags defined in tagcache.h. Must be in correct order! */
125 static const char *tags_str
[] = { "artist", "album", "genre", "title",
126 "filename", "composer", "comment", "albumartist", "grouping", "year", "discnumber", "tracknumber",
127 "bitrate", "length", "playcount", "rating", "playtime", "lastplayed", "commitid" };
129 /* Status information of the tagcache. */
130 static struct tagcache_stat tc_stat
;
132 /* Queue commands. */
133 enum tagcache_queue
{
140 /* Internal tagcache command queue. */
141 CMD_UPDATE_MASTER_HEADER
,
145 struct tagcache_command_entry
{
152 static struct tagcache_command_entry command_queue
[TAGCACHE_COMMAND_QUEUE_LENGTH
];
153 static volatile int command_queue_widx
= 0;
154 static volatile int command_queue_ridx
= 0;
155 static struct mutex command_queue_mutex
;
156 /* Timestamp of the last added event, so we can wait a bit before committing the
157 * whole queue at once. */
158 static long command_queue_timestamp
= 0;
160 /* Tag database structures. */
162 /* Variable-length tag entry in tag files. */
163 struct tagfile_entry
{
164 short tag_length
; /* Length of the data in bytes including '\0' */
165 short idx_id
; /* Corresponding entry location in index file of not unique tags */
166 char tag_data
[0]; /* Begin of the tag data */
169 /* Fixed-size tag entry in master db index. */
171 long tag_seek
[TAG_COUNT
]; /* Location of tag data or numeric tag data */
172 long flag
; /* Status flags */
175 /* Header is the same in every file. */
176 struct tagcache_header
{
177 long magic
; /* Header version number */
178 long datasize
; /* Data size in bytes */
179 long entry_count
; /* Number of entries in this file */
182 struct master_header
{
183 struct tagcache_header tch
;
184 long serial
; /* Increasing counting number */
185 long commitid
; /* Number of commits so far */
189 /* For the endianess correction */
190 static const char *tagfile_entry_ec
= "ss";
191 static const char *index_entry_ec
= "llllllllllllllllllll"; /* (1 + TAG_COUNT) * l */
192 static const char *tagcache_header_ec
= "lll";
193 static const char *master_header_ec
= "llllll";
195 static struct master_header current_tcmh
;
197 #ifdef HAVE_TC_RAMCACHE
198 /* Header is created when loading database to ram. */
199 struct ramcache_header
{
200 struct master_header h
; /* Header from the master index */
201 struct index_entry
*indices
; /* Master index file content */
202 char *tags
[TAG_COUNT
]; /* Tag file content (not including filename tag) */
203 int entry_count
[TAG_COUNT
]; /* Number of entries in the indices. */
206 # ifdef HAVE_EEPROM_SETTINGS
207 struct statefile_header
{
208 struct ramcache_header
*hdr
;
209 struct tagcache_stat tc_stat
;
213 /* Pointer to allocated ramcache_header */
214 static struct ramcache_header
*hdr
;
218 * Full tag entries stored in a temporary file waiting
219 * for commit to the cache. */
220 struct temp_file_entry
{
221 long tag_offset
[TAG_COUNT
];
222 short tag_length
[TAG_COUNT
];
228 struct tempbuf_id_list
{
230 struct tempbuf_id_list
*next
;
233 struct tempbuf_searchidx
{
237 struct tempbuf_id_list idlist
;
240 /* Lookup buffer for fixing messed up index while after sorting. */
241 static long commit_entry_count
;
242 static long lookup_buffer_depth
;
243 static struct tempbuf_searchidx
**lookup
;
245 /* Used when building the temporary file. */
246 static int cachefd
= -1, filenametag_fd
;
247 static int total_entry_count
= 0;
248 static int data_size
= 0;
249 static int processed_dir_count
;
251 /* Thread safe locking */
252 static volatile int write_lock
;
253 static volatile int read_lock
;
255 const char* tagcache_tag_to_str(int tag
)
257 return tags_str
[tag
];
260 bool tagcache_is_numeric_tag(int type
)
264 for (i
= 0; i
< (int)(sizeof(numeric_tags
)/sizeof(numeric_tags
[0])); i
++)
266 if (type
== numeric_tags
[i
])
273 bool tagcache_is_unique_tag(int type
)
277 for (i
= 0; i
< (int)(sizeof(unique_tags
)/sizeof(unique_tags
[0])); i
++)
279 if (type
== unique_tags
[i
])
286 bool tagcache_is_sorted_tag(int type
)
290 for (i
= 0; i
< (int)(sizeof(sorted_tags
)/sizeof(sorted_tags
[0])); i
++)
292 if (type
== sorted_tags
[i
])
299 static int open_tag_fd(struct tagcache_header
*hdr
, int tag
, bool write
)
305 if (tagcache_is_numeric_tag(tag
) || tag
< 0 || tag
>= TAG_COUNT
)
308 snprintf(buf
, sizeof buf
, TAGCACHE_FILE_INDEX
, tag
);
310 fd
= open(buf
, write
? O_RDWR
: O_RDONLY
);
313 logf("tag file open failed: tag=%d write=%d file=%s", tag
, write
, buf
);
314 tc_stat
.ready
= false;
318 /* Check the header. */
319 rc
= ecread(fd
, hdr
, 1, tagcache_header_ec
, tc_stat
.econ
);
320 if (hdr
->magic
!= TAGCACHE_MAGIC
|| rc
!= sizeof(struct tagcache_header
))
322 logf("header error");
323 tc_stat
.ready
= false;
332 static bool do_timed_yield(void)
334 /* Sorting can lock up for quite a while, so yield occasionally */
335 static long wakeup_tick
= 0;
336 if (current_tick
>= wakeup_tick
)
338 wakeup_tick
= current_tick
+ (HZ
/4);
346 #if defined(HAVE_TC_RAMCACHE) && defined(HAVE_DIRCACHE)
347 static long find_entry_ram(const char *filename
,
348 const struct dirent
*dc
)
350 static long last_pos
= 0;
353 /* Check if we tagcache is loaded into ram. */
354 if (!tc_stat
.ramcache
)
358 dc
= dircache_get_entry_ptr(filename
);
362 logf("tagcache: file not found.");
373 for (; i
< hdr
->h
.tch
.entry_count
; i
++)
375 if (hdr
->indices
[i
].tag_seek
[tag_filename
] == (long)dc
)
377 last_pos
= MAX(0, i
- 3);
394 static long find_entry_disk(const char *filename
)
396 struct tagcache_header tch
;
397 static long last_pos
= -1;
398 long pos_history
[POS_HISTORY_COUNT
];
399 long pos_history_idx
= 0;
401 struct tagfile_entry tfe
;
403 char buf
[TAG_MAXLEN
+32];
414 if ( (fd
= open_tag_fd(&tch
, tag_filename
, false)) < 0)
421 lseek(fd
, last_pos
, SEEK_SET
);
423 lseek(fd
, sizeof(struct tagcache_header
), SEEK_SET
);
427 pos
= lseek(fd
, 0, SEEK_CUR
);
428 for (i
= pos_history_idx
-1; i
>= 0; i
--)
429 pos_history
[i
+1] = pos_history
[i
];
430 pos_history
[0] = pos
;
432 if (ecread(fd
, &tfe
, 1, tagfile_entry_ec
, tc_stat
.econ
)
433 != sizeof(struct tagfile_entry
))
438 if (tfe
.tag_length
>= (long)sizeof(buf
))
440 logf("too long tag #1");
446 if (read(fd
, buf
, tfe
.tag_length
) != tfe
.tag_length
)
448 logf("read error #2");
454 if (!strcasecmp(filename
, buf
))
456 last_pos
= pos_history
[pos_history_idx
];
461 if (pos_history_idx
< POS_HISTORY_COUNT
- 1)
475 if (fd
!= filenametag_fd
)
480 if (fd
!= filenametag_fd
)
486 static int find_index(const char *filename
)
490 #if defined(HAVE_TC_RAMCACHE) && defined(HAVE_DIRCACHE)
491 if (tc_stat
.ramcache
&& dircache_is_enabled())
492 idx_id
= find_entry_ram(filename
, NULL
);
496 idx_id
= find_entry_disk(filename
);
501 bool tagcache_find_index(struct tagcache_search
*tcs
, const char *filename
)
508 idx_id
= find_index(filename
);
512 if (!tagcache_search(tcs
, tag_filename
))
515 tcs
->entry_count
= 0;
516 tcs
->idx_id
= idx_id
;
521 static bool get_index(int masterfd
, int idxid
,
522 struct index_entry
*idx
, bool use_ram
)
526 logf("Incorrect idxid: %d", idxid
);
530 #ifdef HAVE_TC_RAMCACHE
531 if (tc_stat
.ramcache
&& use_ram
)
533 if (hdr
->indices
[idxid
].flag
& FLAG_DELETED
)
536 memcpy(idx
, &hdr
->indices
[idxid
], sizeof(struct index_entry
));
543 lseek(masterfd
, idxid
* sizeof(struct index_entry
)
544 + sizeof(struct master_header
), SEEK_SET
);
545 if (ecread(masterfd
, idx
, 1, index_entry_ec
, tc_stat
.econ
)
546 != sizeof(struct index_entry
))
548 logf("read error #3");
552 if (idx
->flag
& FLAG_DELETED
)
558 static bool write_index(int masterfd
, int idxid
, struct index_entry
*idx
)
560 /* We need to exclude all memory only flags & tags when writing to disk. */
561 if (idx
->flag
& FLAG_DIRCACHE
)
563 logf("memory only flags!");
567 #ifdef HAVE_TC_RAMCACHE
568 /* Only update numeric data. Writing the whole index to RAM by memcpy
569 * destroys dircache pointers!
571 if (tc_stat
.ramcache
)
574 struct index_entry
*idx_ram
= &hdr
->indices
[idxid
];
576 for (tag
= 0; tag
< TAG_COUNT
; tag
++)
578 if (tagcache_is_numeric_tag(tag
))
580 idx_ram
->tag_seek
[tag
] = idx
->tag_seek
[tag
];
584 /* Don't touch the dircache flag. */
585 idx_ram
->flag
= idx
->flag
| (idx_ram
->flag
& FLAG_DIRCACHE
);
589 lseek(masterfd
, idxid
* sizeof(struct index_entry
)
590 + sizeof(struct master_header
), SEEK_SET
);
591 if (ecwrite(masterfd
, idx
, 1, index_entry_ec
, tc_stat
.econ
)
592 != sizeof(struct index_entry
))
594 logf("write error #3");
595 logf("idxid: %d", idxid
);
602 static bool open_files(struct tagcache_search
*tcs
, int tag
)
604 if (tcs
->idxfd
[tag
] < 0)
608 snprintf(fn
, sizeof fn
, TAGCACHE_FILE_INDEX
, tag
);
609 tcs
->idxfd
[tag
] = open(fn
, O_RDONLY
);
612 if (tcs
->idxfd
[tag
] < 0)
614 logf("File not open!");
621 static bool retrieve(struct tagcache_search
*tcs
, struct index_entry
*idx
,
622 int tag
, char *buf
, long size
)
624 struct tagfile_entry tfe
;
629 if (tagcache_is_numeric_tag(tag
))
632 seek
= idx
->tag_seek
[tag
];
635 logf("Retrieve failed");
639 #ifdef HAVE_TC_RAMCACHE
642 struct tagfile_entry
*ep
;
644 # ifdef HAVE_DIRCACHE
645 if (tag
== tag_filename
&& idx
->flag
& FLAG_DIRCACHE
)
647 dircache_copy_path((struct dirent
*)seek
,
653 if (tag
!= tag_filename
)
655 ep
= (struct tagfile_entry
*)&hdr
->tags
[tag
][seek
];
656 strncpy(buf
, ep
->tag_data
, size
-1);
663 if (!open_files(tcs
, tag
))
666 lseek(tcs
->idxfd
[tag
], seek
, SEEK_SET
);
667 if (ecread(tcs
->idxfd
[tag
], &tfe
, 1, tagfile_entry_ec
, tc_stat
.econ
)
668 != sizeof(struct tagfile_entry
))
670 logf("read error #5");
674 if (tfe
.tag_length
>= size
)
676 logf("too small buffer");
680 if (read(tcs
->idxfd
[tag
], buf
, tfe
.tag_length
) !=
683 logf("read error #6");
687 buf
[tfe
.tag_length
] = '\0';
692 static long check_virtual_tags(int tag
, const struct index_entry
*idx
)
698 case tag_virt_length_sec
:
699 data
= (idx
->tag_seek
[tag_length
]/1000) % 60;
702 case tag_virt_length_min
:
703 data
= (idx
->tag_seek
[tag_length
]/1000) / 60;
706 case tag_virt_playtime_sec
:
707 data
= (idx
->tag_seek
[tag_playtime
]/1000) % 60;
710 case tag_virt_playtime_min
:
711 data
= (idx
->tag_seek
[tag_playtime
]/1000) / 60;
714 case tag_virt_autoscore
:
715 if (idx
->tag_seek
[tag_length
] == 0
716 || idx
->tag_seek
[tag_playcount
] == 0)
722 data
= 100 * idx
->tag_seek
[tag_playtime
]
723 / idx
->tag_seek
[tag_length
]
724 / idx
->tag_seek
[tag_playcount
];
728 /* How many commits before the file has been added to the DB. */
729 case tag_virt_entryage
:
730 data
= current_tcmh
.commitid
- idx
->tag_seek
[tag_commitid
] - 1;
734 data
= idx
->tag_seek
[tag
];
740 long tagcache_get_numeric(const struct tagcache_search
*tcs
, int tag
)
742 struct index_entry idx
;
747 if (!tagcache_is_numeric_tag(tag
))
750 if (!get_index(tcs
->masterfd
, tcs
->idx_id
, &idx
, true))
753 return check_virtual_tags(tag
, &idx
);
756 inline static bool str_ends_with(const char *str1
, const char *str2
)
758 int str_len
= strlen(str1
);
759 int clause_len
= strlen(str2
);
761 if (clause_len
> str_len
)
764 return !strcasecmp(&str1
[str_len
- clause_len
], str2
);
767 inline static bool str_oneof(const char *str
, const char *list
)
770 int l
, len
= strlen(str
);
774 sep
= strchr(list
, '|');
775 l
= sep
? (long)sep
- (long)list
: (int)strlen(list
);
776 if ((l
==len
) && !strncasecmp(str
, list
, len
))
778 list
+= sep
? l
+ 1 : l
;
784 static bool check_against_clause(long numeric
, const char *str
,
785 const struct tagcache_search_clause
*clause
)
789 switch (clause
->type
)
792 return numeric
== clause
->numeric_data
;
794 return numeric
!= clause
->numeric_data
;
796 return numeric
> clause
->numeric_data
;
798 return numeric
>= clause
->numeric_data
;
800 return numeric
< clause
->numeric_data
;
802 return numeric
<= clause
->numeric_data
;
804 logf("Incorrect numeric tag: %d", clause
->type
);
809 switch (clause
->type
)
812 return !strcasecmp(clause
->str
, str
);
814 return strcasecmp(clause
->str
, str
);
816 return 0>strcasecmp(clause
->str
, str
);
818 return 0>=strcasecmp(clause
->str
, str
);
820 return 0<strcasecmp(clause
->str
, str
);
822 return 0<=strcasecmp(clause
->str
, str
);
823 case clause_contains
:
824 return (strcasestr(str
, clause
->str
) != NULL
);
825 case clause_not_contains
:
826 return (strcasestr(str
, clause
->str
) == NULL
);
827 case clause_begins_with
:
828 return (strcasestr(str
, clause
->str
) == str
);
829 case clause_not_begins_with
:
830 return (strcasestr(str
, clause
->str
) != str
);
831 case clause_ends_with
:
832 return str_ends_with(str
, clause
->str
);
833 case clause_not_ends_with
:
834 return !str_ends_with(str
, clause
->str
);
836 return str_oneof(str
, clause
->str
);
839 logf("Incorrect tag: %d", clause
->type
);
846 static bool check_clauses(struct tagcache_search
*tcs
,
847 struct index_entry
*idx
,
848 struct tagcache_search_clause
**clause
, int count
)
852 #ifdef HAVE_TC_RAMCACHE
855 /* Go through all conditional clauses. */
856 for (i
= 0; i
< count
; i
++)
858 struct tagfile_entry
*tfe
;
863 seek
= check_virtual_tags(clause
[i
]->tag
, idx
);
865 if (!tagcache_is_numeric_tag(clause
[i
]->tag
))
867 if (clause
[i
]->tag
== tag_filename
)
869 retrieve(tcs
, idx
, tag_filename
, buf
, sizeof buf
);
874 tfe
= (struct tagfile_entry
*)&hdr
->tags
[clause
[i
]->tag
][seek
];
879 if (!check_against_clause(seek
, str
, clause
[i
]))
886 /* Check for conditions. */
887 for (i
= 0; i
< count
; i
++)
889 struct tagfile_entry tfe
;
893 seek
= check_virtual_tags(clause
[i
]->tag
, idx
);
895 memset(str
, 0, sizeof str
);
896 if (!tagcache_is_numeric_tag(clause
[i
]->tag
))
898 int fd
= tcs
->idxfd
[clause
[i
]->tag
];
899 lseek(fd
, seek
, SEEK_SET
);
900 ecread(fd
, &tfe
, 1, tagfile_entry_ec
, tc_stat
.econ
);
901 if (tfe
.tag_length
>= (int)sizeof(str
))
903 logf("Too long tag read!");
907 read(fd
, str
, tfe
.tag_length
);
909 /* Check if entry has been deleted. */
914 if (!check_against_clause(seek
, str
, clause
[i
]))
922 bool tagcache_check_clauses(struct tagcache_search
*tcs
,
923 struct tagcache_search_clause
**clause
, int count
)
925 struct index_entry idx
;
930 if (!get_index(tcs
->masterfd
, tcs
->idx_id
, &idx
, true))
933 return check_clauses(tcs
, &idx
, clause
, count
);
936 static bool add_uniqbuf(struct tagcache_search
*tcs
, unsigned long id
)
940 /* If uniq buffer is not defined we must return true for search to work. */
941 if (tcs
->unique_list
== NULL
942 || (!tagcache_is_unique_tag(tcs
->type
)
943 && !tagcache_is_numeric_tag(tcs
->type
)))
948 for (i
= 0; i
< tcs
->unique_list_count
; i
++)
950 /* Return false if entry is found. */
951 if (tcs
->unique_list
[i
] == id
)
955 if (tcs
->unique_list_count
< tcs
->unique_list_capacity
)
957 tcs
->unique_list
[i
] = id
;
958 tcs
->unique_list_count
++;
964 static bool build_lookup_list(struct tagcache_search
*tcs
)
966 struct index_entry entry
;
969 tcs
->seek_list_count
= 0;
971 #ifdef HAVE_TC_RAMCACHE
976 for (i
= tcs
->seek_pos
; i
< hdr
->h
.tch
.entry_count
; i
++)
978 struct index_entry
*idx
= &hdr
->indices
[i
];
979 if (tcs
->seek_list_count
== SEEK_LIST_SIZE
)
982 /* Skip deleted files. */
983 if (idx
->flag
& FLAG_DELETED
)
986 /* Go through all filters.. */
987 for (j
= 0; j
< tcs
->filter_count
; j
++)
989 if (idx
->tag_seek
[tcs
->filter_tag
[j
]] != tcs
->filter_seek
[j
])
995 if (j
< tcs
->filter_count
)
998 /* Check for conditions. */
999 if (!check_clauses(tcs
, idx
, tcs
->clause
, tcs
->clause_count
))
1002 /* Add to the seek list if not already in uniq buffer. */
1003 if (!add_uniqbuf(tcs
, idx
->tag_seek
[tcs
->type
]))
1007 tcs
->seek_list
[tcs
->seek_list_count
] = idx
->tag_seek
[tcs
->type
];
1008 tcs
->seek_flags
[tcs
->seek_list_count
] = idx
->flag
;
1009 tcs
->seek_list_count
++;
1014 return tcs
->seek_list_count
> 0;
1018 lseek(tcs
->masterfd
, tcs
->seek_pos
* sizeof(struct index_entry
) +
1019 sizeof(struct master_header
), SEEK_SET
);
1021 while (ecread(tcs
->masterfd
, &entry
, 1, index_entry_ec
, tc_stat
.econ
)
1022 == sizeof(struct index_entry
))
1024 if (tcs
->seek_list_count
== SEEK_LIST_SIZE
)
1029 /* Check if entry has been deleted. */
1030 if (entry
.flag
& FLAG_DELETED
)
1033 /* Go through all filters.. */
1034 for (i
= 0; i
< tcs
->filter_count
; i
++)
1036 if (entry
.tag_seek
[tcs
->filter_tag
[i
]] != tcs
->filter_seek
[i
])
1040 if (i
< tcs
->filter_count
)
1043 /* Check for conditions. */
1044 if (!check_clauses(tcs
, &entry
, tcs
->clause
, tcs
->clause_count
))
1047 /* Add to the seek list if not already in uniq buffer. */
1048 if (!add_uniqbuf(tcs
, entry
.tag_seek
[tcs
->type
]))
1052 tcs
->seek_list
[tcs
->seek_list_count
] = entry
.tag_seek
[tcs
->type
];
1053 tcs
->seek_flags
[tcs
->seek_list_count
] = entry
.flag
;
1054 tcs
->seek_list_count
++;
1059 return tcs
->seek_list_count
> 0;
1063 static void remove_files(void)
1068 tc_stat
.ready
= false;
1069 tc_stat
.ramcache
= false;
1070 tc_stat
.econ
= false;
1071 remove(TAGCACHE_FILE_MASTER
);
1072 for (i
= 0; i
< TAG_COUNT
; i
++)
1074 if (tagcache_is_numeric_tag(i
))
1077 snprintf(buf
, sizeof buf
, TAGCACHE_FILE_INDEX
, i
);
1083 static int open_master_fd(struct master_header
*hdr
, bool write
)
1088 fd
= open(TAGCACHE_FILE_MASTER
, write
? O_RDWR
: O_RDONLY
);
1091 logf("master file open failed for R/W");
1092 tc_stat
.ready
= false;
1096 tc_stat
.econ
= false;
1098 /* Check the header. */
1099 rc
= read(fd
, hdr
, sizeof(struct master_header
));
1100 if (hdr
->tch
.magic
== TAGCACHE_MAGIC
&& rc
== sizeof(struct master_header
))
1106 /* Trying to read again, this time with endianess correction enabled. */
1107 lseek(fd
, 0, SEEK_SET
);
1109 rc
= ecread(fd
, hdr
, 1, master_header_ec
, true);
1110 if (hdr
->tch
.magic
!= TAGCACHE_MAGIC
|| rc
!= sizeof(struct master_header
))
1112 logf("header error");
1113 tc_stat
.ready
= false;
1118 tc_stat
.econ
= true;
1123 static bool check_all_headers(void)
1125 struct master_header myhdr
;
1126 struct tagcache_header tch
;
1130 if ( (fd
= open_master_fd(&myhdr
, false)) < 0)
1136 logf("tagcache is dirty!");
1140 memcpy(¤t_tcmh
, &myhdr
, sizeof(struct master_header
));
1142 for (tag
= 0; tag
< TAG_COUNT
; tag
++)
1144 if (tagcache_is_numeric_tag(tag
))
1147 if ( (fd
= open_tag_fd(&tch
, tag
, false)) < 0)
1156 bool tagcache_search(struct tagcache_search
*tcs
, int tag
)
1158 struct tagcache_header tag_hdr
;
1159 struct master_header master_hdr
;
1162 if (tcs
->initialized
)
1163 tagcache_search_finish(tcs
);
1168 memset(tcs
, 0, sizeof(struct tagcache_search
));
1169 if (tc_stat
.commit_step
> 0 || !tc_stat
.ready
)
1172 tcs
->position
= sizeof(struct tagcache_header
);
1175 tcs
->seek_list_count
= 0;
1176 tcs
->filter_count
= 0;
1179 for (i
= 0; i
< TAG_COUNT
; i
++)
1182 #ifndef HAVE_TC_RAMCACHE
1183 tcs
->ramsearch
= false;
1185 tcs
->ramsearch
= tc_stat
.ramcache
;
1188 tcs
->entry_count
= hdr
->entry_count
[tcs
->type
];
1193 if (!tagcache_is_numeric_tag(tcs
->type
))
1195 tcs
->idxfd
[tcs
->type
] = open_tag_fd(&tag_hdr
, tcs
->type
, false);
1196 if (tcs
->idxfd
[tcs
->type
] < 0)
1200 /* Always open as R/W so we can pass tcs to functions that modify data also
1201 * without failing. */
1202 tcs
->masterfd
= open_master_fd(&master_hdr
, true);
1204 if (tcs
->masterfd
< 0)
1209 tcs
->initialized
= true;
1215 void tagcache_search_set_uniqbuf(struct tagcache_search
*tcs
,
1216 void *buffer
, long length
)
1218 tcs
->unique_list
= (unsigned long *)buffer
;
1219 tcs
->unique_list_capacity
= length
/ sizeof(*tcs
->unique_list
);
1220 tcs
->unique_list_count
= 0;
1223 bool tagcache_search_add_filter(struct tagcache_search
*tcs
,
1226 if (tcs
->filter_count
== TAGCACHE_MAX_FILTERS
)
1229 if (!tagcache_is_unique_tag(tag
) || tagcache_is_numeric_tag(tag
))
1232 tcs
->filter_tag
[tcs
->filter_count
] = tag
;
1233 tcs
->filter_seek
[tcs
->filter_count
] = seek
;
1234 tcs
->filter_count
++;
1239 bool tagcache_search_add_clause(struct tagcache_search
*tcs
,
1240 struct tagcache_search_clause
*clause
)
1244 if (tcs
->clause_count
>= TAGCACHE_MAX_CLAUSES
)
1246 logf("Too many clauses");
1250 /* Check if there is already a similar filter in present (filters are
1251 * much faster than clauses).
1253 for (i
= 0; i
< tcs
->filter_count
; i
++)
1255 if (tcs
->filter_tag
[i
] == clause
->tag
)
1259 if (!tagcache_is_numeric_tag(clause
->tag
) && tcs
->idxfd
[clause
->tag
] < 0)
1263 snprintf(buf
, sizeof buf
, TAGCACHE_FILE_INDEX
, clause
->tag
);
1264 tcs
->idxfd
[clause
->tag
] = open(buf
, O_RDONLY
);
1267 tcs
->clause
[tcs
->clause_count
] = clause
;
1268 tcs
->clause_count
++;
1273 #define TAG_FILENAME_RAM(tcs) ((tcs->type == tag_filename) \
1274 ? (flag & FLAG_DIRCACHE) : 1)
1276 static bool get_next(struct tagcache_search
*tcs
)
1278 static char buf
[TAG_MAXLEN
+32];
1279 struct tagfile_entry entry
;
1282 if (!tcs
->valid
|| !tc_stat
.ready
)
1285 if (tcs
->idxfd
[tcs
->type
] < 0 && !tagcache_is_numeric_tag(tcs
->type
)
1286 #ifdef HAVE_TC_RAMCACHE
1292 /* Relative fetch. */
1293 if (tcs
->filter_count
> 0 || tcs
->clause_count
> 0
1294 || tagcache_is_numeric_tag(tcs
->type
))
1296 /* Check for end of list. */
1297 if (tcs
->seek_list_count
== 0)
1299 /* Try to fetch more. */
1300 if (!build_lookup_list(tcs
))
1307 tcs
->seek_list_count
--;
1308 flag
= tcs
->seek_flags
[tcs
->seek_list_count
];
1310 /* Seek stream to the correct position and continue to direct fetch. */
1311 if ((!tcs
->ramsearch
|| !TAG_FILENAME_RAM(tcs
))
1312 && !tagcache_is_numeric_tag(tcs
->type
))
1314 if (!open_files(tcs
, tcs
->type
))
1317 lseek(tcs
->idxfd
[tcs
->type
], tcs
->seek_list
[tcs
->seek_list_count
], SEEK_SET
);
1320 tcs
->position
= tcs
->seek_list
[tcs
->seek_list_count
];
1323 if (tagcache_is_numeric_tag(tcs
->type
))
1325 snprintf(buf
, sizeof(buf
), "%d", tcs
->position
);
1326 tcs
->result_seek
= tcs
->position
;
1328 tcs
->result_len
= strlen(buf
) + 1;
1333 #ifdef HAVE_TC_RAMCACHE
1334 if (tcs
->ramsearch
&& TAG_FILENAME_RAM(tcs
))
1336 struct tagfile_entry
*ep
;
1338 if (tcs
->entry_count
== 0)
1345 tcs
->result_seek
= tcs
->position
;
1347 # ifdef HAVE_DIRCACHE
1348 if (tcs
->type
== tag_filename
)
1350 dircache_copy_path((struct dirent
*)tcs
->position
,
1353 tcs
->result_len
= strlen(buf
) + 1;
1354 tcs
->idx_id
= FLAG_GET_ATTR(flag
);
1355 tcs
->ramresult
= false;
1361 ep
= (struct tagfile_entry
*)&hdr
->tags
[tcs
->type
][tcs
->position
];
1362 tcs
->position
+= sizeof(struct tagfile_entry
) + ep
->tag_length
;
1363 tcs
->result
= ep
->tag_data
;
1364 tcs
->result_len
= strlen(tcs
->result
) + 1;
1365 tcs
->idx_id
= ep
->idx_id
;
1366 tcs
->ramresult
= true;
1373 if (!open_files(tcs
, tcs
->type
))
1376 tcs
->result_seek
= lseek(tcs
->idxfd
[tcs
->type
], 0, SEEK_CUR
);
1377 if (ecread(tcs
->idxfd
[tcs
->type
], &entry
, 1,
1378 tagfile_entry_ec
, tc_stat
.econ
) != sizeof(struct tagfile_entry
))
1386 if (entry
.tag_length
> (long)sizeof(buf
))
1389 logf("too long tag #2");
1393 if (read(tcs
->idxfd
[tcs
->type
], buf
, entry
.tag_length
) != entry
.tag_length
)
1396 logf("read error #4");
1401 tcs
->result_len
= strlen(tcs
->result
) + 1;
1402 tcs
->idx_id
= entry
.idx_id
;
1403 tcs
->ramresult
= false;
1408 bool tagcache_get_next(struct tagcache_search
*tcs
)
1410 while (get_next(tcs
))
1412 if (tcs
->result_len
> 1)
1419 bool tagcache_retrieve(struct tagcache_search
*tcs
, int idxid
,
1420 int tag
, char *buf
, long size
)
1422 struct index_entry idx
;
1425 if (!get_index(tcs
->masterfd
, idxid
, &idx
, true))
1428 return retrieve(tcs
, &idx
, tag
, buf
, size
);
1431 static bool update_master_header(void)
1433 struct master_header myhdr
;
1439 if ( (fd
= open_master_fd(&myhdr
, true)) < 0)
1442 myhdr
.serial
= current_tcmh
.serial
;
1443 myhdr
.commitid
= current_tcmh
.commitid
;
1446 lseek(fd
, 0, SEEK_SET
);
1447 ecwrite(fd
, &myhdr
, 1, master_header_ec
, tc_stat
.econ
);
1450 #ifdef HAVE_TC_RAMCACHE
1453 hdr
->h
.serial
= current_tcmh
.serial
;
1454 hdr
->h
.commitid
= current_tcmh
.commitid
;
1463 void tagcache_modify(struct tagcache_search
*tcs
, int type
, const char *text
)
1465 struct tagentry
*entry
;
1467 if (tcs
->type
!= tag_title
)
1470 /* We will need reserve buffer for this. */
1473 struct tagfile_entry
*ep
;
1475 ep
= (struct tagfile_entry
*)&hdr
->tags
[tcs
->type
][tcs
->result_seek
];
1476 tcs
->seek_list
[tcs
->seek_list_count
];
1479 entry
= find_entry_ram();
1484 void tagcache_search_finish(struct tagcache_search
*tcs
)
1488 if (!tcs
->initialized
)
1491 if (tcs
->masterfd
>= 0)
1493 close(tcs
->masterfd
);
1497 for (i
= 0; i
< TAG_COUNT
; i
++)
1499 if (tcs
->idxfd
[i
] >= 0)
1501 close(tcs
->idxfd
[i
]);
1506 tcs
->ramsearch
= false;
1508 tcs
->initialized
= 0;
1513 #if defined(HAVE_TC_RAMCACHE) && defined(HAVE_DIRCACHE)
1514 static struct tagfile_entry
*get_tag(const struct index_entry
*entry
, int tag
)
1516 return (struct tagfile_entry
*)&hdr
->tags
[tag
][entry
->tag_seek
[tag
]];
1519 static long get_tag_numeric(const struct index_entry
*entry
, int tag
)
1521 return check_virtual_tags(tag
, entry
);
1524 static char* get_tag_string(const struct index_entry
*entry
, int tag
)
1526 char* s
= get_tag(entry
, tag
)->tag_data
;
1527 return strcmp(s
, UNTAGGED
) ? s
: NULL
;
1530 bool tagcache_fill_tags(struct mp3entry
*id3
, const char *filename
)
1532 struct index_entry
*entry
;
1538 /* Find the corresponding entry in tagcache. */
1539 idx_id
= find_entry_ram(filename
, NULL
);
1540 if (idx_id
< 0 || !tc_stat
.ramcache
)
1543 entry
= &hdr
->indices
[idx_id
];
1545 id3
->title
= get_tag_string(entry
, tag_title
);
1546 id3
->artist
= get_tag_string(entry
, tag_artist
);
1547 id3
->album
= get_tag_string(entry
, tag_album
);
1548 id3
->genre_string
= get_tag_string(entry
, tag_genre
);
1549 id3
->composer
= get_tag_string(entry
, tag_composer
);
1550 id3
->comment
= get_tag_string(entry
, tag_comment
);
1551 id3
->albumartist
= get_tag_string(entry
, tag_albumartist
);
1552 id3
->grouping
= get_tag_string(entry
, tag_grouping
);
1554 id3
->playcount
= get_tag_numeric(entry
, tag_playcount
);
1555 id3
->rating
= get_tag_numeric(entry
, tag_rating
);
1556 id3
->lastplayed
= get_tag_numeric(entry
, tag_lastplayed
);
1557 id3
->score
= get_tag_numeric(entry
, tag_virt_autoscore
) / 10;
1558 id3
->year
= get_tag_numeric(entry
, tag_year
);
1560 id3
->discnum
= get_tag_numeric(entry
, tag_discnumber
);
1561 id3
->tracknum
= get_tag_numeric(entry
, tag_tracknumber
);
1562 id3
->bitrate
= get_tag_numeric(entry
, tag_bitrate
);
1563 if (id3
->bitrate
== 0)
1570 static inline void write_item(const char *item
)
1572 int len
= strlen(item
) + 1;
1575 write(cachefd
, item
, len
);
1578 static int check_if_empty(char **tag
)
1582 if (*tag
== NULL
|| *tag
[0] == '\0')
1585 return sizeof(UNTAGGED
); /* Tag length */
1588 length
= strlen(*tag
);
1589 if (length
> TAG_MAXLEN
)
1591 logf("over length tag: %s", *tag
);
1592 length
= TAG_MAXLEN
;
1593 *tag
[length
] = '\0';
1599 #define ADD_TAG(entry,tag,data) \
1601 entry.tag_offset[tag] = offset; \
1602 entry.tag_length[tag] = check_if_empty(data); \
1603 offset += entry.tag_length[tag]
1605 #if defined(HAVE_TC_RAMCACHE) && defined(HAVE_DIRCACHE)
1606 static void add_tagcache(char *path
, const struct dirent
*dc
)
1608 static void add_tagcache(char *path
)
1611 struct track_info track
;
1612 struct temp_file_entry entry
;
1615 char tracknumfix
[3];
1617 int path_length
= strlen(path
);
1618 bool has_albumartist
;
1624 /* Check for overlength file path. */
1625 if (path_length
> TAG_MAXLEN
)
1627 /* Path can't be shortened. */
1628 logf("Too long path: %s", path
);
1632 /* Check if the file is supported. */
1633 if (probe_file_format(path
) == AFMT_UNKNOWN
)
1636 /* Check if the file is already cached. */
1637 #if defined(HAVE_TC_RAMCACHE) && defined(HAVE_DIRCACHE)
1638 if (tc_stat
.ramcache
&& dircache_is_enabled())
1640 if (find_entry_ram(path
, dc
) >= 0)
1646 if (filenametag_fd
>= 0)
1648 if (find_entry_disk(path
) >= 0)
1653 fd
= open(path
, O_RDONLY
);
1656 logf("open fail: %s", path
);
1660 memset(&track
, 0, sizeof(struct track_info
));
1661 memset(&entry
, 0, sizeof(struct temp_file_entry
));
1662 memset(&tracknumfix
, 0, sizeof(tracknumfix
));
1663 ret
= get_metadata(&(track
.id3
), fd
, path
, false);
1669 logf("-> %s", path
);
1671 /* Generate track number if missing. */
1672 if (track
.id3
.tracknum
<= 0)
1674 const char *p
= strrchr(path
, '.');
1677 p
= &path
[strlen(path
)-1];
1681 if (isdigit(*p
) && isdigit(*(p
-1)))
1683 tracknumfix
[1] = *p
--;
1684 tracknumfix
[0] = *p
;
1690 if (tracknumfix
[0] != '\0')
1692 track
.id3
.tracknum
= atoi(tracknumfix
);
1693 /* Set a flag to indicate track number has been generated. */
1694 entry
.flag
|= FLAG_TRKNUMGEN
;
1698 /* Unable to generate track number. */
1699 track
.id3
.tracknum
= -1;
1704 entry
.tag_offset
[tag_year
] = track
.id3
.year
;
1705 entry
.tag_offset
[tag_discnumber
] = track
.id3
.discnum
;
1706 entry
.tag_offset
[tag_tracknumber
] = track
.id3
.tracknum
;
1707 entry
.tag_offset
[tag_length
] = track
.id3
.length
;
1708 entry
.tag_offset
[tag_bitrate
] = track
.id3
.bitrate
;
1711 has_albumartist
= track
.id3
.albumartist
!= NULL
1712 && strlen(track
.id3
.albumartist
) > 0;
1713 has_grouping
= track
.id3
.grouping
!= NULL
1714 && strlen(track
.id3
.grouping
) > 0;
1716 ADD_TAG(entry
, tag_filename
, &path
);
1717 ADD_TAG(entry
, tag_title
, &track
.id3
.title
);
1718 ADD_TAG(entry
, tag_artist
, &track
.id3
.artist
);
1719 ADD_TAG(entry
, tag_album
, &track
.id3
.album
);
1720 ADD_TAG(entry
, tag_genre
, &track
.id3
.genre_string
);
1721 ADD_TAG(entry
, tag_composer
, &track
.id3
.composer
);
1722 ADD_TAG(entry
, tag_comment
, &track
.id3
.comment
);
1723 if (has_albumartist
)
1725 ADD_TAG(entry
, tag_albumartist
, &track
.id3
.albumartist
);
1729 ADD_TAG(entry
, tag_albumartist
, &track
.id3
.artist
);
1733 ADD_TAG(entry
, tag_grouping
, &track
.id3
.grouping
);
1737 ADD_TAG(entry
, tag_grouping
, &track
.id3
.title
);
1739 entry
.data_length
= offset
;
1741 /* Write the header */
1742 write(cachefd
, &entry
, sizeof(struct temp_file_entry
));
1744 /* And tags also... Correct order is critical */
1746 write_item(track
.id3
.title
);
1747 write_item(track
.id3
.artist
);
1748 write_item(track
.id3
.album
);
1749 write_item(track
.id3
.genre_string
);
1750 write_item(track
.id3
.composer
);
1751 write_item(track
.id3
.comment
);
1752 if (has_albumartist
)
1754 write_item(track
.id3
.albumartist
);
1758 write_item(track
.id3
.artist
);
1762 write_item(track
.id3
.grouping
);
1766 write_item(track
.id3
.title
);
1768 total_entry_count
++;
1771 static bool tempbuf_insert(char *str
, int id
, int idx_id
, bool unique
)
1773 struct tempbuf_searchidx
*index
= (struct tempbuf_searchidx
*)tempbuf
;
1774 int len
= strlen(str
)+1;
1777 unsigned *crcbuf
= (unsigned *)&tempbuf
[tempbuf_size
-4];
1778 char buf
[TAG_MAXLEN
+32];
1780 for (i
= 0; str
[i
] != '\0' && i
< (int)sizeof(buf
)-1; i
++)
1781 buf
[i
] = tolower(str
[i
]);
1784 crc32
= crc_32(buf
, i
, 0xffffffff);
1788 /* Check if the crc does not exist -> entry does not exist for sure. */
1789 for (i
= 0; i
< tempbufidx
; i
++)
1791 if (crcbuf
[-i
] != crc32
)
1794 if (!strcasecmp(str
, index
[i
].str
))
1796 if (id
< 0 || id
>= lookup_buffer_depth
)
1798 logf("lookup buf overf.: %d", id
);
1802 lookup
[id
] = &index
[i
];
1808 /* Insert to CRC buffer. */
1809 crcbuf
[-tempbufidx
] = crc32
;
1812 /* Insert it to the buffer. */
1813 tempbuf_left
-= len
;
1814 if (tempbuf_left
- 4 < 0 || tempbufidx
>= commit_entry_count
-1)
1817 if (id
>= lookup_buffer_depth
)
1819 logf("lookup buf overf. #2: %d", id
);
1825 lookup
[id
] = &index
[tempbufidx
];
1826 index
[tempbufidx
].idlist
.id
= id
;
1829 index
[tempbufidx
].idlist
.id
= -1;
1831 index
[tempbufidx
].idlist
.next
= NULL
;
1832 index
[tempbufidx
].idx_id
= idx_id
;
1833 index
[tempbufidx
].seek
= -1;
1834 index
[tempbufidx
].str
= &tempbuf
[tempbuf_pos
];
1835 memcpy(index
[tempbufidx
].str
, str
, len
);
1842 static int compare(const void *p1
, const void *p2
)
1846 struct tempbuf_searchidx
*e1
= (struct tempbuf_searchidx
*)p1
;
1847 struct tempbuf_searchidx
*e2
= (struct tempbuf_searchidx
*)p2
;
1849 if (strcmp(e1
->str
, UNTAGGED
) == 0)
1851 if (strcmp(e2
->str
, UNTAGGED
) == 0)
1855 else if (strcmp(e2
->str
, UNTAGGED
) == 0)
1858 return strncasecmp(e1
->str
, e2
->str
, TAG_MAXLEN
);
1861 static int tempbuf_sort(int fd
)
1863 struct tempbuf_searchidx
*index
= (struct tempbuf_searchidx
*)tempbuf
;
1864 struct tagfile_entry fe
;
1868 /* Generate reverse lookup entries. */
1869 for (i
= 0; i
< lookup_buffer_depth
; i
++)
1871 struct tempbuf_id_list
*idlist
;
1876 if (lookup
[i
]->idlist
.id
== i
)
1879 idlist
= &lookup
[i
]->idlist
;
1880 while (idlist
->next
!= NULL
)
1881 idlist
= idlist
->next
;
1883 tempbuf_left
-= sizeof(struct tempbuf_id_list
);
1884 if (tempbuf_left
- 4 < 0)
1887 idlist
->next
= (struct tempbuf_id_list
*)&tempbuf
[tempbuf_pos
];
1888 if (tempbuf_pos
& 0x03)
1890 tempbuf_pos
= (tempbuf_pos
& ~0x03) + 0x04;
1892 idlist
->next
= (struct tempbuf_id_list
*)&tempbuf
[tempbuf_pos
];
1894 tempbuf_pos
+= sizeof(struct tempbuf_id_list
);
1896 idlist
= idlist
->next
;
1898 idlist
->next
= NULL
;
1903 qsort(index
, tempbufidx
, sizeof(struct tempbuf_searchidx
), compare
);
1904 memset(lookup
, 0, lookup_buffer_depth
* sizeof(struct tempbuf_searchidx
**));
1906 for (i
= 0; i
< tempbufidx
; i
++)
1908 struct tempbuf_id_list
*idlist
= &index
[i
].idlist
;
1910 /* Fix the lookup list. */
1911 while (idlist
!= NULL
)
1913 if (idlist
->id
>= 0)
1914 lookup
[idlist
->id
] = &index
[i
];
1915 idlist
= idlist
->next
;
1918 index
[i
].seek
= lseek(fd
, 0, SEEK_CUR
);
1919 length
= strlen(index
[i
].str
) + 1;
1920 fe
.tag_length
= length
;
1921 fe
.idx_id
= index
[i
].idx_id
;
1923 /* Check the chunk alignment. */
1924 if ((fe
.tag_length
+ sizeof(struct tagfile_entry
))
1925 % TAGFILE_ENTRY_CHUNK_LENGTH
)
1927 fe
.tag_length
+= TAGFILE_ENTRY_CHUNK_LENGTH
-
1928 ((fe
.tag_length
+ sizeof(struct tagfile_entry
))
1929 % TAGFILE_ENTRY_CHUNK_LENGTH
);
1932 #ifdef TAGCACHE_STRICT_ALIGN
1933 /* Make sure the entry is long aligned. */
1934 if (index
[i
].seek
& 0x03)
1936 logf("tempbuf_sort: alignment error!");
1941 if (ecwrite(fd
, &fe
, 1, tagfile_entry_ec
, tc_stat
.econ
) !=
1942 sizeof(struct tagfile_entry
))
1944 logf("tempbuf_sort: write error #1");
1948 if (write(fd
, index
[i
].str
, length
) != length
)
1950 logf("tempbuf_sort: write error #2");
1954 /* Write some padding. */
1955 if (fe
.tag_length
- length
> 0)
1956 write(fd
, "XXXXXXXX", fe
.tag_length
- length
);
1962 inline static struct tempbuf_searchidx
* tempbuf_locate(int id
)
1964 if (id
< 0 || id
>= lookup_buffer_depth
)
1971 inline static int tempbuf_find_location(int id
)
1973 struct tempbuf_searchidx
*entry
;
1975 entry
= tempbuf_locate(id
);
1982 static bool build_numeric_indices(struct tagcache_header
*h
, int tmpfd
)
1984 struct master_header tcmh
;
1985 struct index_entry idx
;
1988 struct temp_file_entry
*entrybuf
= (struct temp_file_entry
*)tempbuf
;
1990 int entries_processed
= 0;
1993 max_entries
= tempbuf_size
/ sizeof(struct temp_file_entry
) - 1;
1995 logf("Building numeric indices...");
1996 lseek(tmpfd
, sizeof(struct tagcache_header
), SEEK_SET
);
1998 if ( (masterfd
= open_master_fd(&tcmh
, true)) < 0)
2001 masterfd_pos
= lseek(masterfd
, tcmh
.tch
.entry_count
* sizeof(struct index_entry
),
2003 if (masterfd_pos
== filesize(masterfd
))
2005 logf("we can't append!");
2010 while (entries_processed
< h
->entry_count
)
2012 int count
= MIN(h
->entry_count
- entries_processed
, max_entries
);
2014 /* Read in as many entries as possible. */
2015 for (i
= 0; i
< count
; i
++)
2017 /* Read in numeric data. */
2018 if (read(tmpfd
, &entrybuf
[i
], sizeof(struct temp_file_entry
)) !=
2019 sizeof(struct temp_file_entry
))
2021 logf("read fail #1");
2026 /* Skip string data. */
2027 lseek(tmpfd
, entrybuf
[i
].data_length
, SEEK_CUR
);
2030 /* Commit the data to the index. */
2031 for (i
= 0; i
< count
; i
++)
2033 int loc
= lseek(masterfd
, 0, SEEK_CUR
);
2035 if (ecread(masterfd
, &idx
, 1, index_entry_ec
, tc_stat
.econ
)
2036 != sizeof(struct index_entry
))
2038 logf("read fail #2");
2043 for (j
= 0; j
< TAG_COUNT
; j
++)
2045 if (!tagcache_is_numeric_tag(j
))
2048 idx
.tag_seek
[j
] = entrybuf
[i
].tag_offset
[j
];
2050 idx
.flag
= entrybuf
[i
].flag
;
2052 if (tc_stat
.ready
&& current_tcmh
.commitid
> 0)
2054 idx
.tag_seek
[tag_commitid
] = current_tcmh
.commitid
;
2055 idx
.flag
|= FLAG_DIRTYNUM
;
2058 /* Write back the updated index. */
2059 lseek(masterfd
, loc
, SEEK_SET
);
2060 if (ecwrite(masterfd
, &idx
, 1, index_entry_ec
, tc_stat
.econ
)
2061 != sizeof(struct index_entry
))
2069 entries_processed
+= count
;
2070 logf("%d/%ld entries processed", entries_processed
, h
->entry_count
);
2081 * == 0 temporary failure
2084 static int build_index(int index_type
, struct tagcache_header
*h
, int tmpfd
)
2087 struct tagcache_header tch
;
2088 struct master_header tcmh
;
2089 struct index_entry idxbuf
[IDX_BUF_DEPTH
];
2091 char buf
[TAG_MAXLEN
+32];
2092 int fd
= -1, masterfd
;
2097 logf("Building index: %d", index_type
);
2099 /* Check the number of entries we need to allocate ram for. */
2100 commit_entry_count
= h
->entry_count
+ 1;
2102 masterfd
= open_master_fd(&tcmh
, false);
2105 commit_entry_count
+= tcmh
.tch
.entry_count
;
2109 remove_files(); /* Just to be sure we are clean. */
2111 /* Open the index file, which contains the tag names. */
2112 fd
= open_tag_fd(&tch
, index_type
, true);
2115 logf("tch.datasize=%ld", tch
.datasize
);
2116 lookup_buffer_depth
= 1 +
2117 /* First part */ commit_entry_count
+
2118 /* Second part */ (tch
.datasize
/ TAGFILE_ENTRY_CHUNK_LENGTH
);
2122 lookup_buffer_depth
= 1 +
2123 /* First part */ commit_entry_count
+
2124 /* Second part */ 0;
2127 logf("lookup_buffer_depth=%ld", lookup_buffer_depth
);
2128 logf("commit_entry_count=%ld", commit_entry_count
);
2130 /* Allocate buffer for all index entries from both old and new
2133 tempbuf_pos
= commit_entry_count
* sizeof(struct tempbuf_searchidx
);
2135 /* Allocate lookup buffer. The first portion of commit_entry_count
2136 * contains the new tags in the temporary file and the second
2137 * part for locating entries already in the db.
2140 * +---------+---------------------------+
2141 * | index | position/ENTRY_CHUNK_SIZE | lookup buffer
2142 * +---------+---------------------------+
2144 * Old tags are inserted to a temporary buffer with position:
2145 * tempbuf_insert(position/ENTRY_CHUNK_SIZE, ...);
2146 * And new tags with index:
2147 * tempbuf_insert(idx, ...);
2149 * The buffer is sorted and written into tag file:
2150 * tempbuf_sort(...);
2151 * leaving master index locations messed up.
2153 * That is fixed using the lookup buffer for old tags:
2154 * new_seek = tempbuf_find_location(old_seek, ...);
2156 * new_seek = tempbuf_find_location(idx);
2158 lookup
= (struct tempbuf_searchidx
**)&tempbuf
[tempbuf_pos
];
2159 tempbuf_pos
+= lookup_buffer_depth
* sizeof(void **);
2160 memset(lookup
, 0, lookup_buffer_depth
* sizeof(void **));
2162 /* And calculate the remaining data space used mainly for storing
2163 * tag data (strings). */
2164 tempbuf_left
= tempbuf_size
- tempbuf_pos
- 8;
2165 if (tempbuf_left
- TAGFILE_ENTRY_AVG_LENGTH
* commit_entry_count
< 0)
2167 logf("Buffer way too small!");
2174 * If tag file contains unique tags (sorted index), we will load
2175 * it entirely into memory so we can resort it later for use with
2178 if (tagcache_is_sorted_tag(index_type
))
2180 logf("loading tags...");
2181 for (i
= 0; i
< tch
.entry_count
; i
++)
2183 struct tagfile_entry entry
;
2184 int loc
= lseek(fd
, 0, SEEK_CUR
);
2187 if (ecread(fd
, &entry
, 1, tagfile_entry_ec
, tc_stat
.econ
)
2188 != sizeof(struct tagfile_entry
))
2190 logf("read error #7");
2195 if (entry
.tag_length
>= (int)sizeof(buf
))
2197 logf("too long tag #3");
2202 if (read(fd
, buf
, entry
.tag_length
) != entry
.tag_length
)
2204 logf("read error #8");
2209 /* Skip deleted entries. */
2214 * Save the tag and tag id in the memory buffer. Tag id
2215 * is saved so we can later reindex the master lookup
2216 * table when the index gets resorted.
2218 ret
= tempbuf_insert(buf
, loc
/TAGFILE_ENTRY_CHUNK_LENGTH
2219 + commit_entry_count
, entry
.idx_id
,
2220 tagcache_is_unique_tag(index_type
));
2231 tempbufidx
= tch
.entry_count
;
2236 * Creating new index file to store the tags. No need to preload
2237 * anything whether the index type is sorted or not.
2239 snprintf(buf
, sizeof buf
, TAGCACHE_FILE_INDEX
, index_type
);
2240 fd
= open(buf
, O_WRONLY
| O_CREAT
| O_TRUNC
);
2243 logf("%s open fail", buf
);
2247 tch
.magic
= TAGCACHE_MAGIC
;
2248 tch
.entry_count
= 0;
2251 if (ecwrite(fd
, &tch
, 1, tagcache_header_ec
, tc_stat
.econ
)
2252 != sizeof(struct tagcache_header
))
2254 logf("header write failed");
2260 /* Loading the tag lookup file as "master file". */
2261 logf("Loading index file");
2262 masterfd
= open(TAGCACHE_FILE_MASTER
, O_RDWR
);
2266 logf("Creating new DB");
2267 masterfd
= open(TAGCACHE_FILE_MASTER
, O_WRONLY
| O_CREAT
| O_TRUNC
);
2271 logf("Failure to create index file (%s)", TAGCACHE_FILE_MASTER
);
2276 /* Write the header (write real values later). */
2277 memset(&tcmh
, 0, sizeof(struct master_header
));
2279 tcmh
.tch
.entry_count
= 0;
2280 tcmh
.tch
.datasize
= 0;
2282 ecwrite(masterfd
, &tcmh
, 1, master_header_ec
, tc_stat
.econ
);
2284 masterfd_pos
= lseek(masterfd
, 0, SEEK_CUR
);
2289 * Master file already exists so we need to process the current
2294 if (ecread(masterfd
, &tcmh
, 1, master_header_ec
, tc_stat
.econ
) !=
2295 sizeof(struct master_header
) || tcmh
.tch
.magic
!= TAGCACHE_MAGIC
)
2297 logf("header error");
2304 * If we reach end of the master file, we need to expand it to
2305 * hold new tags. If the current index is not sorted, we can
2306 * simply append new data to end of the file.
2307 * However, if the index is sorted, we need to update all tag
2308 * pointers in the master file for the current index.
2310 masterfd_pos
= lseek(masterfd
, tcmh
.tch
.entry_count
* sizeof(struct index_entry
),
2312 if (masterfd_pos
== filesize(masterfd
))
2314 logf("appending...");
2320 * Load new unique tags in memory to be sorted later and added
2321 * to the master lookup file.
2323 if (tagcache_is_sorted_tag(index_type
))
2325 lseek(tmpfd
, sizeof(struct tagcache_header
), SEEK_SET
);
2326 /* h is the header of the temporary file containing new tags. */
2327 logf("inserting new tags...");
2328 for (i
= 0; i
< h
->entry_count
; i
++)
2330 struct temp_file_entry entry
;
2332 if (read(tmpfd
, &entry
, sizeof(struct temp_file_entry
)) !=
2333 sizeof(struct temp_file_entry
))
2335 logf("read fail #3");
2341 if (entry
.tag_length
[index_type
] >= (long)sizeof(buf
))
2343 logf("too long entry!");
2348 lseek(tmpfd
, entry
.tag_offset
[index_type
], SEEK_CUR
);
2349 if (read(tmpfd
, buf
, entry
.tag_length
[index_type
]) !=
2350 entry
.tag_length
[index_type
])
2352 logf("read fail #4");
2357 if (tagcache_is_unique_tag(index_type
))
2358 error
= !tempbuf_insert(buf
, i
, -1, true);
2360 error
= !tempbuf_insert(buf
, i
, tcmh
.tch
.entry_count
+ i
, false);
2364 logf("insert error");
2369 lseek(tmpfd
, entry
.data_length
- entry
.tag_offset
[index_type
] -
2370 entry
.tag_length
[index_type
], SEEK_CUR
);
2375 /* Sort the buffer data and write it to the index file. */
2376 lseek(fd
, sizeof(struct tagcache_header
), SEEK_SET
);
2377 i
= tempbuf_sort(fd
);
2380 logf("sorted %d tags", i
);
2383 * Now update all indexes in the master lookup file.
2385 logf("updating indices...");
2386 lseek(masterfd
, sizeof(struct master_header
), SEEK_SET
);
2387 for (i
= 0; i
< tcmh
.tch
.entry_count
; i
+= idxbuf_pos
)
2390 int loc
= lseek(masterfd
, 0, SEEK_CUR
);
2392 idxbuf_pos
= MIN(tcmh
.tch
.entry_count
- i
, IDX_BUF_DEPTH
);
2394 if (ecread(masterfd
, idxbuf
, idxbuf_pos
, index_entry_ec
, tc_stat
.econ
)
2395 != (int)sizeof(struct index_entry
)*idxbuf_pos
)
2397 logf("read fail #5");
2401 lseek(masterfd
, loc
, SEEK_SET
);
2403 for (j
= 0; j
< idxbuf_pos
; j
++)
2405 if (idxbuf
[j
].flag
& FLAG_DELETED
)
2407 /* We can just ignore deleted entries. */
2408 idxbuf
[j
].tag_seek
[index_type
] = 0;
2412 idxbuf
[j
].tag_seek
[index_type
] = tempbuf_find_location(
2413 idxbuf
[j
].tag_seek
[index_type
]/TAGFILE_ENTRY_CHUNK_LENGTH
2414 + commit_entry_count
);
2416 if (idxbuf
[j
].tag_seek
[index_type
] < 0)
2418 logf("update error: %d/%ld", i
+j
, tcmh
.tch
.entry_count
);
2426 /* Write back the updated index. */
2427 if (ecwrite(masterfd
, idxbuf
, idxbuf_pos
,
2428 index_entry_ec
, tc_stat
.econ
) !=
2429 (int)sizeof(struct index_entry
)*idxbuf_pos
)
2440 * Walk through the temporary file containing the new tags.
2442 // build_normal_index(h, tmpfd, masterfd, idx);
2443 logf("updating new indices...");
2444 lseek(masterfd
, masterfd_pos
, SEEK_SET
);
2445 lseek(tmpfd
, sizeof(struct tagcache_header
), SEEK_SET
);
2446 lseek(fd
, 0, SEEK_END
);
2447 for (i
= 0; i
< h
->entry_count
; i
+= idxbuf_pos
)
2451 idxbuf_pos
= MIN(h
->entry_count
- i
, IDX_BUF_DEPTH
);
2454 memset(idxbuf
, 0, sizeof(struct index_entry
)*IDX_BUF_DEPTH
);
2458 int loc
= lseek(masterfd
, 0, SEEK_CUR
);
2460 if (ecread(masterfd
, idxbuf
, idxbuf_pos
, index_entry_ec
, tc_stat
.econ
)
2461 != (int)sizeof(struct index_entry
)*idxbuf_pos
)
2463 logf("read fail #6");
2467 lseek(masterfd
, loc
, SEEK_SET
);
2470 /* Read entry headers. */
2471 for (j
= 0; j
< idxbuf_pos
; j
++)
2473 if (!tagcache_is_sorted_tag(index_type
))
2475 struct temp_file_entry entry
;
2476 struct tagfile_entry fe
;
2478 if (read(tmpfd
, &entry
, sizeof(struct temp_file_entry
)) !=
2479 sizeof(struct temp_file_entry
))
2481 logf("read fail #7");
2487 if (entry
.tag_length
[index_type
] >= (int)sizeof(buf
))
2489 logf("too long entry!");
2490 logf("length=%d", entry
.tag_length
[index_type
]);
2491 logf("pos=0x%02lx", lseek(tmpfd
, 0, SEEK_CUR
));
2496 lseek(tmpfd
, entry
.tag_offset
[index_type
], SEEK_CUR
);
2497 if (read(tmpfd
, buf
, entry
.tag_length
[index_type
]) !=
2498 entry
.tag_length
[index_type
])
2500 logf("read fail #8");
2501 logf("offset=0x%02lx", entry
.tag_offset
[index_type
]);
2502 logf("length=0x%02x", entry
.tag_length
[index_type
]);
2507 /* Write to index file. */
2508 idxbuf
[j
].tag_seek
[index_type
] = lseek(fd
, 0, SEEK_CUR
);
2509 fe
.tag_length
= entry
.tag_length
[index_type
];
2510 fe
.idx_id
= tcmh
.tch
.entry_count
+ i
+ j
;
2511 ecwrite(fd
, &fe
, 1, tagfile_entry_ec
, tc_stat
.econ
);
2512 write(fd
, buf
, fe
.tag_length
);
2516 lseek(tmpfd
, entry
.data_length
- entry
.tag_offset
[index_type
] -
2517 entry
.tag_length
[index_type
], SEEK_CUR
);
2521 /* Locate the correct entry from the sorted array. */
2522 idxbuf
[j
].tag_seek
[index_type
] = tempbuf_find_location(i
+ j
);
2523 if (idxbuf
[j
].tag_seek
[index_type
] < 0)
2525 logf("entry not found (%d)", j
);
2533 if (ecwrite(masterfd
, idxbuf
, idxbuf_pos
,
2534 index_entry_ec
, tc_stat
.econ
) !=
2535 (int)sizeof(struct index_entry
)*idxbuf_pos
)
2537 logf("tagcache: write fail #4");
2546 /* Finally write the header. */
2547 tch
.magic
= TAGCACHE_MAGIC
;
2548 tch
.entry_count
= tempbufidx
;
2549 tch
.datasize
= lseek(fd
, 0, SEEK_END
) - sizeof(struct tagcache_header
);
2550 lseek(fd
, 0, SEEK_SET
);
2551 ecwrite(fd
, &tch
, 1, tagcache_header_ec
, tc_stat
.econ
);
2553 if (index_type
!= tag_filename
)
2554 h
->datasize
+= tch
.datasize
;
2555 logf("s:%d/%ld/%ld", index_type
, tch
.datasize
, h
->datasize
);
2567 static bool commit(void)
2569 struct tagcache_header tch
;
2570 struct master_header tcmh
;
2574 #ifdef HAVE_DIRCACHE
2575 bool dircache_buffer_stolen
= false;
2577 bool local_allocation
= false;
2579 logf("committing tagcache");
2584 tmpfd
= open(TAGCACHE_FILE_TEMP
, O_RDONLY
);
2587 logf("nothing to commit");
2592 /* Load the header. */
2593 len
= sizeof(struct tagcache_header
);
2594 rc
= read(tmpfd
, &tch
, len
);
2596 if (tch
.magic
!= TAGCACHE_MAGIC
|| rc
!= len
)
2598 logf("incorrect header");
2600 remove(TAGCACHE_FILE_TEMP
);
2604 if (tch
.entry_count
== 0)
2606 logf("nothing to commit");
2608 remove(TAGCACHE_FILE_TEMP
);
2612 #ifdef HAVE_EEPROM_SETTINGS
2613 remove(TAGCACHE_STATEFILE
);
2616 /* At first be sure to unload the ramcache! */
2617 #ifdef HAVE_TC_RAMCACHE
2618 tc_stat
.ramcache
= false;
2623 /* Try to steal every buffer we can :) */
2624 if (tempbuf_size
== 0)
2625 local_allocation
= true;
2627 #ifdef HAVE_DIRCACHE
2628 if (tempbuf_size
== 0)
2630 /* Try to steal the dircache buffer. */
2631 tempbuf
= dircache_steal_buffer(&tempbuf_size
);
2632 tempbuf_size
&= ~0x03;
2634 if (tempbuf_size
> 0)
2636 dircache_buffer_stolen
= true;
2641 #ifdef HAVE_TC_RAMCACHE
2642 if (tempbuf_size
== 0 && tc_stat
.ramcache_allocated
> 0)
2644 tempbuf
= (char *)(hdr
+ 1);
2645 tempbuf_size
= tc_stat
.ramcache_allocated
- sizeof(struct ramcache_header
) - 128;
2646 tempbuf_size
&= ~0x03;
2650 /* And finally fail if there are no buffers available. */
2651 if (tempbuf_size
== 0)
2653 logf("delaying commit until next boot");
2654 tc_stat
.commit_delayed
= true;
2660 logf("commit %ld entries...", tch
.entry_count
);
2662 /* Mark DB dirty so it will stay disabled if commit fails. */
2663 current_tcmh
.dirty
= true;
2664 update_master_header();
2666 /* Now create the index files. */
2667 tc_stat
.commit_step
= 0;
2669 tc_stat
.commit_delayed
= false;
2671 for (i
= 0; i
< TAG_COUNT
; i
++)
2675 if (tagcache_is_numeric_tag(i
))
2678 tc_stat
.commit_step
++;
2679 ret
= build_index(i
, &tch
, tmpfd
);
2683 logf("tagcache failed init");
2687 tc_stat
.commit_delayed
= true;
2688 tc_stat
.commit_step
= 0;
2694 if (!build_numeric_indices(&tch
, tmpfd
))
2696 logf("Failure to commit numeric indices");
2699 tc_stat
.commit_step
= 0;
2705 tc_stat
.commit_step
= 0;
2707 /* Update the master index headers. */
2708 if ( (masterfd
= open_master_fd(&tcmh
, true)) < 0)
2714 tcmh
.tch
.entry_count
+= tch
.entry_count
;
2715 tcmh
.tch
.datasize
= sizeof(struct master_header
)
2716 + sizeof(struct index_entry
) * tcmh
.tch
.entry_count
2721 lseek(masterfd
, 0, SEEK_SET
);
2722 ecwrite(masterfd
, &tcmh
, 1, master_header_ec
, tc_stat
.econ
);
2725 logf("tagcache committed");
2726 remove(TAGCACHE_FILE_TEMP
);
2727 tc_stat
.ready
= check_all_headers();
2728 tc_stat
.readyvalid
= true;
2730 if (local_allocation
)
2736 #ifdef HAVE_DIRCACHE
2737 /* Rebuild the dircache, if we stole the buffer. */
2738 if (dircache_buffer_stolen
)
2742 #ifdef HAVE_TC_RAMCACHE
2743 /* Reload tagcache. */
2744 if (tc_stat
.ramcache_allocated
> 0)
2745 tagcache_start_scan();
2753 static void allocate_tempbuf(void)
2755 /* Yeah, malloc would be really nice now :) */
2757 tempbuf_size
= 32*1024*1024;
2758 tempbuf
= malloc(tempbuf_size
);
2760 tempbuf
= (char *)(((long)audiobuf
& ~0x03) + 0x04);
2761 tempbuf_size
= (long)audiobufend
- (long)audiobuf
- 4;
2762 audiobuf
+= tempbuf_size
;
2766 static void free_tempbuf(void)
2768 if (tempbuf_size
== 0)
2774 audiobuf
-= tempbuf_size
;
2780 static bool modify_numeric_entry(int masterfd
, int idx_id
, int tag
, long data
)
2782 struct index_entry idx
;
2787 if (!tagcache_is_numeric_tag(tag
))
2790 if (!get_index(masterfd
, idx_id
, &idx
, false))
2793 idx
.tag_seek
[tag
] = data
;
2794 idx
.flag
|= FLAG_DIRTYNUM
;
2796 return write_index(masterfd
, idx_id
, &idx
);
2800 bool tagcache_modify_numeric_entry(struct tagcache_search
*tcs
,
2803 struct master_header myhdr
;
2805 if (tcs
->masterfd
< 0)
2807 if ( (tcs
->masterfd
= open_master_fd(&myhdr
, true)) < 0)
2811 return modify_numeric_entry(tcs
->masterfd
, tcs
->idx_id
, tag
, data
);
2815 #define COMMAND_QUEUE_IS_EMPTY (command_queue_ridx == command_queue_widx)
2817 static bool command_queue_is_full(void)
2821 next
= command_queue_widx
+ 1;
2822 if (next
>= TAGCACHE_COMMAND_QUEUE_LENGTH
)
2825 return (next
== command_queue_ridx
);
2828 void run_command_queue(bool force
)
2830 struct master_header myhdr
;
2833 if (COMMAND_QUEUE_IS_EMPTY
)
2836 if (!force
&& !command_queue_is_full()
2837 && current_tick
- TAGCACHE_COMMAND_QUEUE_COMMIT_DELAY
2838 < command_queue_timestamp
)
2843 mutex_lock(&command_queue_mutex
);
2845 if ( (masterfd
= open_master_fd(&myhdr
, true)) < 0)
2848 while (command_queue_ridx
!= command_queue_widx
)
2850 struct tagcache_command_entry
*ce
= &command_queue
[command_queue_ridx
];
2852 switch (ce
->command
)
2854 case CMD_UPDATE_MASTER_HEADER
:
2857 update_master_header();
2859 /* Re-open the masterfd. */
2860 if ( (masterfd
= open_master_fd(&myhdr
, true)) < 0)
2865 case CMD_UPDATE_NUMERIC
:
2867 modify_numeric_entry(masterfd
, ce
->idx_id
, ce
->tag
, ce
->data
);
2872 if (++command_queue_ridx
>= TAGCACHE_COMMAND_QUEUE_LENGTH
)
2873 command_queue_ridx
= 0;
2878 mutex_unlock(&command_queue_mutex
);
2881 static void queue_command(int cmd
, long idx_id
, int tag
, long data
)
2887 mutex_lock(&command_queue_mutex
);
2888 next
= command_queue_widx
+ 1;
2889 if (next
>= TAGCACHE_COMMAND_QUEUE_LENGTH
)
2892 /* Make sure queue is not full. */
2893 if (next
!= command_queue_ridx
)
2895 struct tagcache_command_entry
*ce
= &command_queue
[command_queue_widx
];
2898 ce
->idx_id
= idx_id
;
2902 command_queue_widx
= next
;
2903 command_queue_timestamp
= current_tick
;
2904 mutex_unlock(&command_queue_mutex
);
2908 /* Queue is full, try again later... */
2909 mutex_unlock(&command_queue_mutex
);
2914 long tagcache_increase_serial(void)
2924 old
= current_tcmh
.serial
++;
2925 queue_command(CMD_UPDATE_MASTER_HEADER
, 0, 0, 0);
2930 void tagcache_update_numeric(int idx_id
, int tag
, long data
)
2932 queue_command(CMD_UPDATE_NUMERIC
, idx_id
, tag
, data
);
2935 long tagcache_get_serial(void)
2937 return current_tcmh
.serial
;
2940 long tagcache_get_commitid(void)
2942 return current_tcmh
.commitid
;
2945 static bool write_tag(int fd
, const char *tagstr
, const char *datastr
)
2950 snprintf(buf
, sizeof buf
, "%s=\"", tagstr
);
2951 for (i
= strlen(buf
); i
< (long)sizeof(buf
)-3; i
++)
2953 if (*datastr
== '\0')
2956 if (*datastr
== '"')
2963 buf
[i
] = *(datastr
++);
2966 strcpy(&buf
[i
], "\" ");
2968 write(fd
, buf
, i
+ 2);
2973 static bool read_tag(char *dest
, long size
,
2974 const char *src
, const char *tagstr
)
2977 char current_tag
[32];
2979 while (*src
!= '\0')
2981 /* Skip all whitespace */
2989 /* Read in tag name */
2990 while (*src
!= '=' && *src
!= ' ')
2992 current_tag
[pos
] = *src
;
2996 if (*src
== '\0' || pos
>= (long)sizeof(current_tag
))
2999 current_tag
[pos
] = '\0';
3001 /* Read in tag data */
3003 /* Find the start. */
3004 while (*src
!= '"' && *src
!= '\0')
3007 if (*src
== '\0' || *(++src
) == '\0')
3010 /* Read the data. */
3011 for (pos
= 0; pos
< size
; pos
++)
3016 if (*src
== '\\' && *(src
+1) == '"')
3038 if (!strcasecmp(tagstr
, current_tag
))
3045 static int parse_changelog_line(int line_n
, const char *buf
, void *parameters
)
3047 struct index_entry idx
;
3048 char tag_data
[TAG_MAXLEN
+32];
3050 long masterfd
= (long)parameters
;
3051 const int import_tags
[] = { tag_playcount
, tag_rating
, tag_playtime
, tag_lastplayed
,
3059 logf("%d/%s", line_n
, buf
);
3060 if (!read_tag(tag_data
, sizeof tag_data
, buf
, "filename"))
3062 logf("filename missing");
3067 idx_id
= find_index(tag_data
);
3070 logf("entry not found");
3074 if (!get_index(masterfd
, idx_id
, &idx
, false))
3076 logf("failed to retrieve index entry");
3080 /* Stop if tag has already been modified. */
3081 if (idx
.flag
& FLAG_DIRTYNUM
)
3084 logf("import: %s", tag_data
);
3086 idx
.flag
|= FLAG_DIRTYNUM
;
3087 for (i
= 0; i
< (long)(sizeof(import_tags
)/sizeof(import_tags
[0])); i
++)
3091 if (!read_tag(tag_data
, sizeof tag_data
, buf
,
3092 tagcache_tag_to_str(import_tags
[i
])))
3097 data
= atoi(tag_data
);
3101 idx
.tag_seek
[import_tags
[i
]] = data
;
3103 if (import_tags
[i
] == tag_lastplayed
&& data
> current_tcmh
.serial
)
3104 current_tcmh
.serial
= data
;
3105 else if (import_tags
[i
] == tag_commitid
&& data
>= current_tcmh
.commitid
)
3106 current_tcmh
.commitid
= data
+ 1;
3109 return write_index(masterfd
, idx_id
, &idx
) ? 0 : -5;
3113 bool tagcache_import_changelog(void)
3115 struct master_header myhdr
;
3116 struct tagcache_header tch
;
3127 clfd
= open(TAGCACHE_FILE_CHANGELOG
, O_RDONLY
);
3130 logf("failure to open changelog");
3134 if ( (masterfd
= open_master_fd(&myhdr
, true)) < 0)
3142 filenametag_fd
= open_tag_fd(&tch
, tag_filename
, false);
3144 fast_readline(clfd
, buf
, sizeof buf
, (long *)masterfd
,
3145 parse_changelog_line
);
3150 if (filenametag_fd
>= 0)
3151 close(filenametag_fd
);
3155 update_master_header();
3161 bool tagcache_create_changelog(struct tagcache_search
*tcs
)
3163 struct master_header myhdr
;
3164 struct index_entry idx
;
3165 char buf
[TAG_MAXLEN
+32];
3173 if (!tagcache_search(tcs
, tag_filename
))
3176 /* Initialize the changelog */
3177 clfd
= open(TAGCACHE_FILE_CHANGELOG
, O_WRONLY
| O_CREAT
| O_TRUNC
);
3180 logf("failure to open changelog");
3184 if (tcs
->masterfd
< 0)
3186 if ( (tcs
->masterfd
= open_master_fd(&myhdr
, false)) < 0)
3191 lseek(tcs
->masterfd
, 0, SEEK_SET
);
3192 ecread(tcs
->masterfd
, &myhdr
, 1, master_header_ec
, tc_stat
.econ
);
3195 write(clfd
, "## Changelog version 1\n", 23);
3197 for (i
= 0; i
< myhdr
.tch
.entry_count
; i
++)
3199 if (ecread(tcs
->masterfd
, &idx
, 1, index_entry_ec
, tc_stat
.econ
)
3200 != sizeof(struct index_entry
))
3202 logf("read error #9");
3203 tagcache_search_finish(tcs
);
3208 /* Skip until the entry found has been modified. */
3209 if (! (idx
.flag
& FLAG_DIRTYNUM
) )
3212 /* Skip deleted entries too. */
3213 if (idx
.flag
& FLAG_DELETED
)
3216 /* Now retrieve all tags. */
3217 for (j
= 0; j
< TAG_COUNT
; j
++)
3219 if (tagcache_is_numeric_tag(j
))
3221 snprintf(temp
, sizeof temp
, "%d", idx
.tag_seek
[j
]);
3222 write_tag(clfd
, tagcache_tag_to_str(j
), temp
);
3227 tagcache_retrieve(tcs
, i
, tcs
->type
, buf
, sizeof buf
);
3228 write_tag(clfd
, tagcache_tag_to_str(j
), buf
);
3231 write(clfd
, "\n", 1);
3237 tagcache_search_finish(tcs
);
3242 static bool delete_entry(long idx_id
)
3246 struct index_entry idx
, myidx
;
3247 struct master_header myhdr
;
3248 char buf
[TAG_MAXLEN
+32];
3249 int in_use
[TAG_COUNT
];
3251 logf("delete_entry(): %ld", idx_id
);
3253 #ifdef HAVE_TC_RAMCACHE
3254 /* At first mark the entry removed from ram cache. */
3255 if (tc_stat
.ramcache
)
3256 hdr
->indices
[idx_id
].flag
|= FLAG_DELETED
;
3259 if ( (fd
= open_master_fd(&myhdr
, true) ) < 0)
3262 lseek(fd
, idx_id
* sizeof(struct index_entry
), SEEK_CUR
);
3263 if (ecread(fd
, &myidx
, 1, index_entry_ec
, tc_stat
.econ
)
3264 != sizeof(struct index_entry
))
3266 logf("delete_entry(): read error");
3271 myidx
.flag
|= FLAG_DELETED
;
3272 lseek(fd
, -sizeof(struct index_entry
), SEEK_CUR
);
3273 if (ecwrite(fd
, &myidx
, 1, index_entry_ec
, tc_stat
.econ
)
3274 != sizeof(struct index_entry
))
3276 logf("delete_entry(): write_error");
3281 /* Now check which tags are no longer in use (if any) */
3282 for (tag
= 0; tag
< TAG_COUNT
; tag
++)
3285 lseek(fd
, sizeof(struct master_header
), SEEK_SET
);
3286 for (i
= 0; i
< myhdr
.tch
.entry_count
; i
++)
3288 struct index_entry
*idxp
;
3290 #ifdef HAVE_TC_RAMCACHE
3291 /* Use RAM DB if available for greater speed */
3292 if (tc_stat
.ramcache
)
3293 idxp
= &hdr
->indices
[i
];
3297 if (ecread(fd
, &idx
, 1, index_entry_ec
, tc_stat
.econ
)
3298 != sizeof(struct index_entry
))
3300 logf("delete_entry(): read error #2");
3307 if (idxp
->flag
& FLAG_DELETED
)
3310 for (tag
= 0; tag
< TAG_COUNT
; tag
++)
3312 if (tagcache_is_numeric_tag(tag
))
3315 if (idxp
->tag_seek
[tag
] == myidx
.tag_seek
[tag
])
3322 /* Now delete all tags no longer in use. */
3323 for (tag
= 0; tag
< TAG_COUNT
; tag
++)
3325 if (tagcache_is_numeric_tag(tag
))
3330 logf("in use: %d/%d", tag
, in_use
[tag
]);
3334 #ifdef HAVE_TC_RAMCACHE
3335 /* Delete from ram. */
3336 if (tc_stat
.ramcache
&& tag
!= tag_filename
)
3338 struct tagfile_entry
*tagentry
= get_tag(&myidx
, tag
);
3339 tagentry
->tag_data
[0] = '\0';
3343 /* Open the index file, which contains the tag names. */
3344 snprintf(buf
, sizeof buf
, TAGCACHE_FILE_INDEX
, tag
);
3345 fd
= open(buf
, O_RDWR
);
3349 logf("open failed");
3353 /* Skip the header block */
3354 lseek(fd
, myidx
.tag_seek
[tag
] + sizeof(struct tagfile_entry
), SEEK_SET
);
3356 /* Debug, print 10 first characters of the tag
3359 logf("TAG:%s", buf);
3360 lseek(fd, -10, SEEK_CUR);
3363 /* Write first data byte in tag as \0 */
3366 /* Now tag data has been removed */
3375 * Returns true if there is an event waiting in the queue
3376 * that requires the current operation to be aborted.
3378 static bool check_event_queue(void)
3382 queue_wait_w_tmo(&tagcache_queue
, &ev
, 0);
3387 case SYS_USB_CONNECTED
:
3388 /* Put the event back into the queue. */
3389 queue_post(&tagcache_queue
, ev
.id
, ev
.data
);
3397 #ifdef HAVE_TC_RAMCACHE
3398 static bool allocate_tagcache(void)
3400 struct master_header tcmh
;
3403 /* Load the header. */
3404 if ( (fd
= open_master_fd(&tcmh
, false)) < 0)
3413 * Now calculate the required cache size plus
3414 * some extra space for alignment fixes.
3416 tc_stat
.ramcache_allocated
= tcmh
.tch
.datasize
+ 128 + TAGCACHE_RESERVE
+
3417 sizeof(struct ramcache_header
) + TAG_COUNT
*sizeof(void *);
3418 hdr
= buffer_alloc(tc_stat
.ramcache_allocated
+ 128);
3419 memset(hdr
, 0, sizeof(struct ramcache_header
));
3420 memcpy(&hdr
->h
, &tcmh
, sizeof(struct master_header
));
3421 hdr
->indices
= (struct index_entry
*)(hdr
+ 1);
3422 logf("tagcache: %d bytes allocated.", tc_stat
.ramcache_allocated
);
3427 # ifdef HAVE_EEPROM_SETTINGS
3428 static bool tagcache_dumpload(void)
3430 struct statefile_header shdr
;
3435 fd
= open(TAGCACHE_STATEFILE
, O_RDONLY
);
3438 logf("no tagcache statedump");
3442 /* Check the statefile memory placement */
3443 hdr
= buffer_alloc(0);
3444 rc
= read(fd
, &shdr
, sizeof(struct statefile_header
));
3445 if (rc
!= sizeof(struct statefile_header
)
3446 /* || (long)hdr != (long)shdr.hdr */)
3448 logf("incorrect statefile");
3454 offpos
= (long)hdr
- (long)shdr
.hdr
;
3456 /* Lets allocate real memory and load it */
3457 hdr
= buffer_alloc(shdr
.tc_stat
.ramcache_allocated
);
3458 rc
= read(fd
, hdr
, shdr
.tc_stat
.ramcache_allocated
);
3461 if (rc
!= shdr
.tc_stat
.ramcache_allocated
)
3463 logf("read failure!");
3468 memcpy(&tc_stat
, &shdr
.tc_stat
, sizeof(struct tagcache_stat
));
3470 /* Now fix the pointers */
3471 hdr
->indices
= (struct index_entry
*)((long)hdr
->indices
+ offpos
);
3472 for (i
= 0; i
< TAG_COUNT
; i
++)
3473 hdr
->tags
[i
] += offpos
;
3478 static bool tagcache_dumpsave(void)
3480 struct statefile_header shdr
;
3483 if (!tc_stat
.ramcache
)
3486 fd
= open(TAGCACHE_STATEFILE
, O_WRONLY
| O_CREAT
| O_TRUNC
);
3489 logf("failed to create a statedump");
3493 /* Create the header */
3495 memcpy(&shdr
.tc_stat
, &tc_stat
, sizeof(struct tagcache_stat
));
3496 write(fd
, &shdr
, sizeof(struct statefile_header
));
3498 /* And dump the data too */
3499 write(fd
, hdr
, tc_stat
.ramcache_allocated
);
3506 static bool load_tagcache(void)
3508 struct tagcache_header
*tch
;
3509 long bytesleft
= tc_stat
.ramcache_allocated
;
3510 struct index_entry
*idx
;
3515 # ifdef HAVE_DIRCACHE
3516 while (dircache_is_initializing())
3520 logf("loading tagcache to ram...");
3522 fd
= open(TAGCACHE_FILE_MASTER
, O_RDONLY
);
3525 logf("tagcache open failed");
3529 if (ecread(fd
, &hdr
->h
, 1, master_header_ec
, tc_stat
.econ
)
3530 != sizeof(struct master_header
)
3531 || hdr
->h
.tch
.magic
!= TAGCACHE_MAGIC
)
3533 logf("incorrect header");
3539 /* Load the master index table. */
3540 for (i
= 0; i
< hdr
->h
.tch
.entry_count
; i
++)
3542 rc
= ecread(fd
, idx
, 1, index_entry_ec
, tc_stat
.econ
);
3543 if (rc
!= sizeof(struct index_entry
))
3545 logf("read error #10");
3550 bytesleft
-= sizeof(struct index_entry
);
3551 if (bytesleft
< 0 || ((long)idx
- (long)hdr
->indices
) >= tc_stat
.ramcache_allocated
)
3553 logf("too big tagcache.");
3563 /* Load the tags. */
3565 for (tag
= 0; tag
< TAG_COUNT
; tag
++)
3567 struct tagfile_entry
*fe
;
3568 char buf
[TAG_MAXLEN
+32];
3570 if (tagcache_is_numeric_tag(tag
))
3573 //p = ((void *)p+1);
3574 p
= (char *)((long)p
& ~0x03) + 0x04;
3577 /* Check the header. */
3578 tch
= (struct tagcache_header
*)p
;
3579 p
+= sizeof(struct tagcache_header
);
3581 if ( (fd
= open_tag_fd(tch
, tag
, false)) < 0)
3584 for (hdr
->entry_count
[tag
] = 0;
3585 hdr
->entry_count
[tag
] < tch
->entry_count
;
3586 hdr
->entry_count
[tag
]++)
3590 if (do_timed_yield())
3592 /* Abort if we got a critical event in queue */
3593 if (check_event_queue())
3597 fe
= (struct tagfile_entry
*)p
;
3598 pos
= lseek(fd
, 0, SEEK_CUR
);
3599 rc
= ecread(fd
, fe
, 1, tagfile_entry_ec
, tc_stat
.econ
);
3600 if (rc
!= sizeof(struct tagfile_entry
))
3602 /* End of lookup table. */
3603 logf("read error #11");
3608 /* We have a special handling for the filename tags. */
3609 if (tag
== tag_filename
)
3611 # ifdef HAVE_DIRCACHE
3612 const struct dirent
*dc
;
3615 // FIXME: This is wrong!
3616 // idx = &hdr->indices[hdr->entry_count[i]];
3617 idx
= &hdr
->indices
[fe
->idx_id
];
3619 if (fe
->tag_length
>= (long)sizeof(buf
)-1)
3623 logf("TAG:%s", buf
);
3624 logf("too long filename");
3629 rc
= read(fd
, buf
, fe
->tag_length
);
3630 if (rc
!= fe
->tag_length
)
3632 logf("read error #12");
3637 /* Check if the entry has already been removed */
3638 if (idx
->flag
& FLAG_DELETED
)
3641 /* This flag must not be used yet. */
3642 if (idx
->flag
& FLAG_DIRCACHE
)
3644 logf("internal error!");
3649 if (idx
->tag_seek
[tag
] != pos
)
3651 logf("corrupt data structures!");
3656 # ifdef HAVE_DIRCACHE
3657 if (dircache_is_enabled())
3659 dc
= dircache_get_entry_ptr(buf
);
3662 logf("Entry no longer valid.");
3664 delete_entry(fe
->idx_id
);
3668 idx
->flag
|= FLAG_DIRCACHE
;
3669 FLAG_SET_ATTR(idx
->flag
, fe
->idx_id
);
3670 idx
->tag_seek
[tag_filename
] = (long)dc
;
3676 # if 0 /* Maybe we could enable this for flash players. Too slow otherwise. */
3677 /* Check if entry has been removed. */
3678 if (global_settings
.tagcache_autoupdate
)
3682 testfd
= open(buf
, O_RDONLY
);
3685 logf("Entry no longer valid.");
3687 delete_entry(fe
->idx_id
);
3698 bytesleft
-= sizeof(struct tagfile_entry
) + fe
->tag_length
;
3701 logf("too big tagcache #2");
3702 logf("tl: %d", fe
->tag_length
);
3703 logf("bl: %ld", bytesleft
);
3709 rc
= read(fd
, fe
->tag_data
, fe
->tag_length
);
3712 if (rc
!= fe
->tag_length
)
3714 logf("read error #13");
3715 logf("rc=0x%04x", rc
); // 0x431
3716 logf("len=0x%04x", fe
->tag_length
); // 0x4000
3717 logf("pos=0x%04lx", lseek(fd
, 0, SEEK_CUR
)); // 0x433
3718 logf("tag=0x%02x", tag
); // 0x00
3726 tc_stat
.ramcache_used
= tc_stat
.ramcache_allocated
- bytesleft
;
3727 logf("tagcache loaded into ram!");
3731 #endif /* HAVE_TC_RAMCACHE */
3733 static bool check_deleted_files(void)
3736 char buf
[TAG_MAXLEN
+32];
3737 struct tagfile_entry tfe
;
3739 logf("reverse scan...");
3740 snprintf(buf
, sizeof buf
, TAGCACHE_FILE_INDEX
, tag_filename
);
3741 fd
= open(buf
, O_RDONLY
);
3745 logf("%s open fail", buf
);
3749 lseek(fd
, sizeof(struct tagcache_header
), SEEK_SET
);
3750 while (ecread(fd
, &tfe
, 1, tagfile_entry_ec
, tc_stat
.econ
)
3751 == sizeof(struct tagfile_entry
)
3753 && !check_event_queue()
3757 if (tfe
.tag_length
>= (long)sizeof(buf
)-1)
3759 logf("too long tag");
3764 if (read(fd
, buf
, tfe
.tag_length
) != tfe
.tag_length
)
3766 logf("read error #14");
3771 /* Check if the file has already deleted from the db. */
3775 /* Now check if the file exists. */
3776 testfd
= open(buf
, O_RDONLY
);
3779 logf("Entry no longer valid.");
3780 logf("-> %s / %d", buf
, tfe
.tag_length
);
3781 delete_entry(tfe
.idx_id
);
3793 static bool check_dir(const char *dirname
)
3797 int success
= false;
3799 dir
= opendir(dirname
);
3802 logf("tagcache: opendir() failed");
3806 /* Recursively scan the dir. */
3810 while (!check_event_queue())
3813 struct dirent
*entry
;
3815 entry
= readdir(dir
);
3823 if (!strcmp((char *)entry
->d_name
, ".") ||
3824 !strcmp((char *)entry
->d_name
, ".."))
3829 len
= strlen(curpath
);
3830 snprintf(&curpath
[len
], curpath_size
- len
, "/%s",
3833 processed_dir_count
++;
3834 if (entry
->attribute
& ATTR_DIRECTORY
)
3837 #if defined(HAVE_TC_RAMCACHE) && defined(HAVE_DIRCACHE)
3838 add_tagcache(curpath
, dir
->internal_entry
);
3840 add_tagcache(curpath
);
3843 curpath
[len
] = '\0';
3851 void build_tagcache(const char *path
)
3853 struct tagcache_header header
;
3858 total_entry_count
= 0;
3859 processed_dir_count
= 0;
3861 #ifdef HAVE_DIRCACHE
3862 while (dircache_is_initializing())
3866 logf("updating tagcache");
3868 cachefd
= open(TAGCACHE_FILE_TEMP
, O_RDONLY
);
3871 logf("skipping, cache already waiting for commit");
3876 cachefd
= open(TAGCACHE_FILE_TEMP
, O_RDWR
| O_CREAT
| O_TRUNC
);
3879 logf("master file open failed: %s", TAGCACHE_FILE_TEMP
);
3883 filenametag_fd
= open_tag_fd(&header
, tag_filename
, false);
3887 logf("Scanning files...");
3888 /* Scan for new files. */
3889 memset(&header
, 0, sizeof(struct tagcache_header
));
3890 write(cachefd
, &header
, sizeof(struct tagcache_header
));
3892 if (strcmp("/", path
) != 0)
3893 strcpy(curpath
, path
);
3894 ret
= check_dir(path
);
3896 /* Write the header. */
3897 header
.magic
= TAGCACHE_MAGIC
;
3898 header
.datasize
= data_size
;
3899 header
.entry_count
= total_entry_count
;
3900 lseek(cachefd
, 0, SEEK_SET
);
3901 write(cachefd
, &header
, sizeof(struct tagcache_header
));
3904 if (filenametag_fd
>= 0)
3906 close(filenametag_fd
);
3907 filenametag_fd
= -1;
3917 /* Commit changes to the database. */
3923 remove(TAGCACHE_FILE_TEMP
);
3924 logf("tagcache built!");
3930 #ifdef HAVE_TC_RAMCACHE
3933 /* Import runtime statistics if we just initialized the db. */
3934 if (hdr
->h
.serial
== 0)
3935 queue_post(&tagcache_queue
, Q_IMPORT_CHANGELOG
, 0);
3942 #ifdef HAVE_TC_RAMCACHE
3943 static void load_ramcache(void)
3950 /* At first we should load the cache (if exists). */
3951 tc_stat
.ramcache
= load_tagcache();
3953 if (!tc_stat
.ramcache
)
3955 /* If loading failed, it must indicate some problem with the db
3956 * so disable it entirely to prevent further issues. */
3957 tc_stat
.ready
= false;
3964 void tagcache_unload_ramcache(void)
3966 tc_stat
.ramcache
= false;
3967 /* Just to make sure there is no statefile present. */
3968 // remove(TAGCACHE_STATEFILE);
3973 static void tagcache_thread(void)
3976 bool check_done
= false;
3978 /* If the previous cache build/update was interrupted, commit
3979 * the changes first in foreground. */
3985 #ifdef HAVE_TC_RAMCACHE
3986 # ifdef HAVE_EEPROM_SETTINGS
3987 if (firmware_settings
.initialized
&& firmware_settings
.disk_clean
)
3988 check_done
= tagcache_dumpload();
3990 remove(TAGCACHE_STATEFILE
);
3993 /* Allocate space for the tagcache if found on disk. */
3994 if (global_settings
.tagcache_ram
&& !tc_stat
.ramcache
)
3995 allocate_tagcache();
3999 tc_stat
.initialized
= true;
4001 /* Don't delay bootup with the header check but do it on background. */
4003 tc_stat
.ready
= check_all_headers();
4004 tc_stat
.readyvalid
= true;
4008 run_command_queue(false);
4010 queue_wait_w_tmo(&tagcache_queue
, &ev
, HZ
);
4014 case Q_IMPORT_CHANGELOG
:
4015 tagcache_import_changelog();
4020 build_tagcache("/");
4024 build_tagcache("/");
4025 #ifdef HAVE_TC_RAMCACHE
4028 check_deleted_files();
4034 if (check_done
|| !tc_stat
.ready
)
4037 #ifdef HAVE_TC_RAMCACHE
4038 if (!tc_stat
.ramcache
&& global_settings
.tagcache_ram
)
4041 if (tc_stat
.ramcache
&& global_settings
.tagcache_autoupdate
)
4042 build_tagcache("/");
4046 if (global_settings
.tagcache_autoupdate
)
4048 build_tagcache("/");
4049 /* Don't do auto removal without dircache (very slow). */
4050 #ifdef HAVE_DIRCACHE
4051 if (dircache_is_enabled())
4052 check_deleted_files();
4056 logf("tagcache check done");
4068 case SYS_USB_CONNECTED
:
4069 logf("USB: TagCache");
4070 usb_acknowledge(SYS_USB_CONNECTED_ACK
);
4071 usb_wait_for_disconnect(&tagcache_queue
);
4078 bool tagcache_prepare_shutdown(void)
4080 if (tagcache_get_commit_step() > 0)
4083 tagcache_stop_scan();
4084 while (read_lock
|| write_lock
)
4090 void tagcache_shutdown(void)
4092 /* Flush the command queue. */
4093 run_command_queue(true);
4095 #ifdef HAVE_EEPROM_SETTINGS
4096 if (tc_stat
.ramcache
)
4097 tagcache_dumpsave();
4101 static int get_progress(void)
4103 int total_count
= -1;
4105 #ifdef HAVE_DIRCACHE
4106 if (dircache_is_enabled())
4108 total_count
= dircache_get_entry_count();
4112 #ifdef HAVE_TC_RAMCACHE
4114 if (hdr
&& tc_stat
.ramcache
)
4115 total_count
= hdr
->h
.tch
.entry_count
;
4119 if (total_count
< 0)
4122 return processed_dir_count
* 100 / total_count
;
4125 struct tagcache_stat
* tagcache_get_stat(void)
4127 tc_stat
.progress
= get_progress();
4128 tc_stat
.processed_entries
= processed_dir_count
;
4133 void tagcache_start_scan(void)
4135 queue_post(&tagcache_queue
, Q_START_SCAN
, 0);
4138 bool tagcache_update(void)
4143 queue_post(&tagcache_queue
, Q_UPDATE
, 0);
4147 bool tagcache_rebuild()
4149 queue_post(&tagcache_queue
, Q_REBUILD
, 0);
4153 void tagcache_stop_scan(void)
4155 queue_post(&tagcache_queue
, Q_STOP_SCAN
, 0);
4158 #ifdef HAVE_TC_RAMCACHE
4159 bool tagcache_is_ramcache(void)
4161 return tc_stat
.ramcache
;
4165 #endif /* !__PCTOOL__ */
4168 void tagcache_init(void)
4170 memset(&tc_stat
, 0, sizeof(struct tagcache_stat
));
4171 memset(¤t_tcmh
, 0, sizeof(struct master_header
));
4172 filenametag_fd
= -1;
4173 write_lock
= read_lock
= 0;
4176 mutex_init(&command_queue_mutex
);
4177 queue_init(&tagcache_queue
, true);
4178 create_thread(tagcache_thread
, tagcache_stack
,
4179 sizeof(tagcache_stack
), tagcache_thread_name
4180 IF_PRIO(, PRIORITY_BACKGROUND
)
4181 IF_COP(, CPU
, false));
4183 tc_stat
.initialized
= true;
4187 tc_stat
.ready
= check_all_headers();
4192 void tagcache_reverse_scan(void)
4194 logf("Checking for deleted files");
4195 check_deleted_files();
4199 bool tagcache_is_initialized(void)
4201 return tc_stat
.initialized
;
4203 bool tagcache_is_usable(void)
4205 return tc_stat
.initialized
&& tc_stat
.ready
;
4207 int tagcache_get_commit_step(void)
4209 return tc_stat
.commit_step
;
4211 int tagcache_get_max_commit_step(void)
4213 return (int)(sizeof(sorted_tags
)/sizeof(sorted_tags
[0]))+1;