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_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
};
117 /* Numeric tags (we can use these tags with conditional clauses). */
118 static const int numeric_tags
[] = { tag_year
, 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", "year", "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
= "llllllllllllllllll"; /* (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 entry
->tag_seek
[tag
];
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
);
1553 id3
->playcount
= get_tag_numeric(entry
, tag_playcount
);
1554 id3
->rating
= get_tag_numeric(entry
, tag_rating
);
1555 id3
->lastplayed
= get_tag_numeric(entry
, tag_lastplayed
);
1556 id3
->score
= get_tag_numeric(entry
, tag_virt_autoscore
) / 10;
1557 id3
->year
= get_tag_numeric(entry
, tag_year
);
1559 id3
->tracknum
= get_tag_numeric(entry
, tag_tracknumber
);
1560 id3
->bitrate
= get_tag_numeric(entry
, tag_bitrate
);
1561 if (id3
->bitrate
== 0)
1568 static inline void write_item(const char *item
)
1570 int len
= strlen(item
) + 1;
1573 write(cachefd
, item
, len
);
1576 static int check_if_empty(char **tag
)
1580 if (*tag
== NULL
|| *tag
[0] == '\0')
1583 return sizeof(UNTAGGED
); /* Tag length */
1586 length
= strlen(*tag
);
1587 if (length
> TAG_MAXLEN
)
1589 logf("over length tag: %s", *tag
);
1590 length
= TAG_MAXLEN
;
1591 *tag
[length
] = '\0';
1597 #define ADD_TAG(entry,tag,data) \
1599 entry.tag_offset[tag] = offset; \
1600 entry.tag_length[tag] = check_if_empty(data); \
1601 offset += entry.tag_length[tag]
1603 #if defined(HAVE_TC_RAMCACHE) && defined(HAVE_DIRCACHE)
1604 static void add_tagcache(char *path
, const struct dirent
*dc
)
1606 static void add_tagcache(char *path
)
1609 struct track_info track
;
1610 struct temp_file_entry entry
;
1613 char tracknumfix
[3];
1615 int path_length
= strlen(path
);
1616 bool has_albumartist
;
1621 /* Check for overlength file path. */
1622 if (path_length
> TAG_MAXLEN
)
1624 /* Path can't be shortened. */
1625 logf("Too long path: %s", path
);
1629 /* Check if the file is supported. */
1630 if (probe_file_format(path
) == AFMT_UNKNOWN
)
1633 /* Check if the file is already cached. */
1634 #if defined(HAVE_TC_RAMCACHE) && defined(HAVE_DIRCACHE)
1635 if (tc_stat
.ramcache
&& dircache_is_enabled())
1637 if (find_entry_ram(path
, dc
) >= 0)
1643 if (filenametag_fd
>= 0)
1645 if (find_entry_disk(path
) >= 0)
1650 fd
= open(path
, O_RDONLY
);
1653 logf("open fail: %s", path
);
1657 memset(&track
, 0, sizeof(struct track_info
));
1658 memset(&entry
, 0, sizeof(struct temp_file_entry
));
1659 memset(&tracknumfix
, 0, sizeof(tracknumfix
));
1660 ret
= get_metadata(&track
, fd
, path
, false);
1666 logf("-> %s", path
);
1668 /* Generate track number if missing. */
1669 if (track
.id3
.tracknum
<= 0)
1671 const char *p
= strrchr(path
, '.');
1674 p
= &path
[strlen(path
)-1];
1678 if (isdigit(*p
) && isdigit(*(p
-1)))
1680 tracknumfix
[1] = *p
--;
1681 tracknumfix
[0] = *p
;
1687 if (tracknumfix
[0] != '\0')
1689 track
.id3
.tracknum
= atoi(tracknumfix
);
1690 /* Set a flag to indicate track number has been generated. */
1691 entry
.flag
|= FLAG_TRKNUMGEN
;
1695 /* Unable to generate track number. */
1696 track
.id3
.tracknum
= -1;
1701 entry
.tag_offset
[tag_year
] = track
.id3
.year
;
1702 entry
.tag_offset
[tag_tracknumber
] = track
.id3
.tracknum
;
1703 entry
.tag_offset
[tag_length
] = track
.id3
.length
;
1704 entry
.tag_offset
[tag_bitrate
] = track
.id3
.bitrate
;
1707 has_albumartist
= track
.id3
.albumartist
!= NULL
1708 && strlen(track
.id3
.albumartist
) > 0;
1710 ADD_TAG(entry
, tag_filename
, &path
);
1711 ADD_TAG(entry
, tag_title
, &track
.id3
.title
);
1712 ADD_TAG(entry
, tag_artist
, &track
.id3
.artist
);
1713 ADD_TAG(entry
, tag_album
, &track
.id3
.album
);
1714 ADD_TAG(entry
, tag_genre
, &track
.id3
.genre_string
);
1715 ADD_TAG(entry
, tag_composer
, &track
.id3
.composer
);
1716 ADD_TAG(entry
, tag_comment
, &track
.id3
.comment
);
1717 if (has_albumartist
)
1719 ADD_TAG(entry
, tag_albumartist
, &track
.id3
.albumartist
);
1723 ADD_TAG(entry
, tag_albumartist
, &track
.id3
.artist
);
1725 entry
.data_length
= offset
;
1727 /* Write the header */
1728 write(cachefd
, &entry
, sizeof(struct temp_file_entry
));
1730 /* And tags also... Correct order is critical */
1732 write_item(track
.id3
.title
);
1733 write_item(track
.id3
.artist
);
1734 write_item(track
.id3
.album
);
1735 write_item(track
.id3
.genre_string
);
1736 write_item(track
.id3
.composer
);
1737 write_item(track
.id3
.comment
);
1738 if (has_albumartist
)
1740 write_item(track
.id3
.albumartist
);
1744 write_item(track
.id3
.artist
);
1746 total_entry_count
++;
1749 static bool tempbuf_insert(char *str
, int id
, int idx_id
, bool unique
)
1751 struct tempbuf_searchidx
*index
= (struct tempbuf_searchidx
*)tempbuf
;
1752 int len
= strlen(str
)+1;
1755 unsigned *crcbuf
= (unsigned *)&tempbuf
[tempbuf_size
-4];
1756 char buf
[TAG_MAXLEN
+32];
1758 for (i
= 0; str
[i
] != '\0' && i
< (int)sizeof(buf
)-1; i
++)
1759 buf
[i
] = tolower(str
[i
]);
1762 crc32
= crc_32(buf
, i
, 0xffffffff);
1766 /* Check if the crc does not exist -> entry does not exist for sure. */
1767 for (i
= 0; i
< tempbufidx
; i
++)
1769 if (crcbuf
[-i
] != crc32
)
1772 if (!strcasecmp(str
, index
[i
].str
))
1774 if (id
< 0 || id
>= lookup_buffer_depth
)
1776 logf("lookup buf overf.: %d", id
);
1780 lookup
[id
] = &index
[i
];
1786 /* Insert to CRC buffer. */
1787 crcbuf
[-tempbufidx
] = crc32
;
1790 /* Insert it to the buffer. */
1791 tempbuf_left
-= len
;
1792 if (tempbuf_left
- 4 < 0 || tempbufidx
>= commit_entry_count
-1)
1795 if (id
>= lookup_buffer_depth
)
1797 logf("lookup buf overf. #2: %d", id
);
1803 lookup
[id
] = &index
[tempbufidx
];
1804 index
[tempbufidx
].idlist
.id
= id
;
1807 index
[tempbufidx
].idlist
.id
= -1;
1809 index
[tempbufidx
].idlist
.next
= NULL
;
1810 index
[tempbufidx
].idx_id
= idx_id
;
1811 index
[tempbufidx
].seek
= -1;
1812 index
[tempbufidx
].str
= &tempbuf
[tempbuf_pos
];
1813 memcpy(index
[tempbufidx
].str
, str
, len
);
1820 static int compare(const void *p1
, const void *p2
)
1824 struct tempbuf_searchidx
*e1
= (struct tempbuf_searchidx
*)p1
;
1825 struct tempbuf_searchidx
*e2
= (struct tempbuf_searchidx
*)p2
;
1827 if (strcmp(e1
->str
, UNTAGGED
) == 0)
1829 if (strcmp(e2
->str
, UNTAGGED
) == 0)
1833 else if (strcmp(e2
->str
, UNTAGGED
) == 0)
1836 return strncasecmp(e1
->str
, e2
->str
, TAG_MAXLEN
);
1839 static int tempbuf_sort(int fd
)
1841 struct tempbuf_searchidx
*index
= (struct tempbuf_searchidx
*)tempbuf
;
1842 struct tagfile_entry fe
;
1846 /* Generate reverse lookup entries. */
1847 for (i
= 0; i
< lookup_buffer_depth
; i
++)
1849 struct tempbuf_id_list
*idlist
;
1854 if (lookup
[i
]->idlist
.id
== i
)
1857 idlist
= &lookup
[i
]->idlist
;
1858 while (idlist
->next
!= NULL
)
1859 idlist
= idlist
->next
;
1861 tempbuf_left
-= sizeof(struct tempbuf_id_list
);
1862 if (tempbuf_left
- 4 < 0)
1865 idlist
->next
= (struct tempbuf_id_list
*)&tempbuf
[tempbuf_pos
];
1866 if (tempbuf_pos
& 0x03)
1868 tempbuf_pos
= (tempbuf_pos
& ~0x03) + 0x04;
1870 idlist
->next
= (struct tempbuf_id_list
*)&tempbuf
[tempbuf_pos
];
1872 tempbuf_pos
+= sizeof(struct tempbuf_id_list
);
1874 idlist
= idlist
->next
;
1876 idlist
->next
= NULL
;
1881 qsort(index
, tempbufidx
, sizeof(struct tempbuf_searchidx
), compare
);
1882 memset(lookup
, 0, lookup_buffer_depth
* sizeof(struct tempbuf_searchidx
**));
1884 for (i
= 0; i
< tempbufidx
; i
++)
1886 struct tempbuf_id_list
*idlist
= &index
[i
].idlist
;
1888 /* Fix the lookup list. */
1889 while (idlist
!= NULL
)
1891 if (idlist
->id
>= 0)
1892 lookup
[idlist
->id
] = &index
[i
];
1893 idlist
= idlist
->next
;
1896 index
[i
].seek
= lseek(fd
, 0, SEEK_CUR
);
1897 length
= strlen(index
[i
].str
) + 1;
1898 fe
.tag_length
= length
;
1899 fe
.idx_id
= index
[i
].idx_id
;
1901 /* Check the chunk alignment. */
1902 if ((fe
.tag_length
+ sizeof(struct tagfile_entry
))
1903 % TAGFILE_ENTRY_CHUNK_LENGTH
)
1905 fe
.tag_length
+= TAGFILE_ENTRY_CHUNK_LENGTH
-
1906 ((fe
.tag_length
+ sizeof(struct tagfile_entry
))
1907 % TAGFILE_ENTRY_CHUNK_LENGTH
);
1910 #ifdef TAGCACHE_STRICT_ALIGN
1911 /* Make sure the entry is long aligned. */
1912 if (index
[i
].seek
& 0x03)
1914 logf("tempbuf_sort: alignment error!");
1919 if (ecwrite(fd
, &fe
, 1, tagfile_entry_ec
, tc_stat
.econ
) !=
1920 sizeof(struct tagfile_entry
))
1922 logf("tempbuf_sort: write error #1");
1926 if (write(fd
, index
[i
].str
, length
) != length
)
1928 logf("tempbuf_sort: write error #2");
1932 /* Write some padding. */
1933 if (fe
.tag_length
- length
> 0)
1934 write(fd
, "XXXXXXXX", fe
.tag_length
- length
);
1940 inline static struct tempbuf_searchidx
* tempbuf_locate(int id
)
1942 if (id
< 0 || id
>= lookup_buffer_depth
)
1949 inline static int tempbuf_find_location(int id
)
1951 struct tempbuf_searchidx
*entry
;
1953 entry
= tempbuf_locate(id
);
1960 static bool build_numeric_indices(struct tagcache_header
*h
, int tmpfd
)
1962 struct master_header tcmh
;
1963 struct index_entry idx
;
1966 struct temp_file_entry
*entrybuf
= (struct temp_file_entry
*)tempbuf
;
1968 int entries_processed
= 0;
1971 max_entries
= tempbuf_size
/ sizeof(struct temp_file_entry
) - 1;
1973 logf("Building numeric indices...");
1974 lseek(tmpfd
, sizeof(struct tagcache_header
), SEEK_SET
);
1976 if ( (masterfd
= open_master_fd(&tcmh
, true)) < 0)
1979 masterfd_pos
= lseek(masterfd
, tcmh
.tch
.entry_count
* sizeof(struct index_entry
),
1981 if (masterfd_pos
== filesize(masterfd
))
1983 logf("we can't append!");
1988 while (entries_processed
< h
->entry_count
)
1990 int count
= MIN(h
->entry_count
- entries_processed
, max_entries
);
1992 /* Read in as many entries as possible. */
1993 for (i
= 0; i
< count
; i
++)
1995 /* Read in numeric data. */
1996 if (read(tmpfd
, &entrybuf
[i
], sizeof(struct temp_file_entry
)) !=
1997 sizeof(struct temp_file_entry
))
1999 logf("read fail #1");
2004 /* Skip string data. */
2005 lseek(tmpfd
, entrybuf
[i
].data_length
, SEEK_CUR
);
2008 /* Commit the data to the index. */
2009 for (i
= 0; i
< count
; i
++)
2011 int loc
= lseek(masterfd
, 0, SEEK_CUR
);
2013 if (ecread(masterfd
, &idx
, 1, index_entry_ec
, tc_stat
.econ
)
2014 != sizeof(struct index_entry
))
2016 logf("read fail #2");
2021 for (j
= 0; j
< TAG_COUNT
; j
++)
2023 if (!tagcache_is_numeric_tag(j
))
2026 idx
.tag_seek
[j
] = entrybuf
[i
].tag_offset
[j
];
2028 idx
.flag
= entrybuf
[i
].flag
;
2030 if (tc_stat
.ready
&& current_tcmh
.commitid
> 0)
2032 idx
.tag_seek
[tag_commitid
] = current_tcmh
.commitid
;
2033 idx
.flag
|= FLAG_DIRTYNUM
;
2036 /* Write back the updated index. */
2037 lseek(masterfd
, loc
, SEEK_SET
);
2038 if (ecwrite(masterfd
, &idx
, 1, index_entry_ec
, tc_stat
.econ
)
2039 != sizeof(struct index_entry
))
2047 entries_processed
+= count
;
2048 logf("%d/%ld entries processed", entries_processed
, h
->entry_count
);
2059 * == 0 temporary failure
2062 static int build_index(int index_type
, struct tagcache_header
*h
, int tmpfd
)
2065 struct tagcache_header tch
;
2066 struct master_header tcmh
;
2067 struct index_entry idxbuf
[IDX_BUF_DEPTH
];
2069 char buf
[TAG_MAXLEN
+32];
2070 int fd
= -1, masterfd
;
2075 logf("Building index: %d", index_type
);
2077 /* Check the number of entries we need to allocate ram for. */
2078 commit_entry_count
= h
->entry_count
+ 1;
2080 masterfd
= open_master_fd(&tcmh
, false);
2083 commit_entry_count
+= tcmh
.tch
.entry_count
;
2087 remove_files(); /* Just to be sure we are clean. */
2089 /* Open the index file, which contains the tag names. */
2090 fd
= open_tag_fd(&tch
, index_type
, true);
2093 logf("tch.datasize=%ld", tch
.datasize
);
2094 lookup_buffer_depth
= 1 +
2095 /* First part */ commit_entry_count
+
2096 /* Second part */ (tch
.datasize
/ TAGFILE_ENTRY_CHUNK_LENGTH
);
2100 lookup_buffer_depth
= 1 +
2101 /* First part */ commit_entry_count
+
2102 /* Second part */ 0;
2105 logf("lookup_buffer_depth=%ld", lookup_buffer_depth
);
2106 logf("commit_entry_count=%ld", commit_entry_count
);
2108 /* Allocate buffer for all index entries from both old and new
2111 tempbuf_pos
= commit_entry_count
* sizeof(struct tempbuf_searchidx
);
2113 /* Allocate lookup buffer. The first portion of commit_entry_count
2114 * contains the new tags in the temporary file and the second
2115 * part for locating entries already in the db.
2118 * +---------+---------------------------+
2119 * | index | position/ENTRY_CHUNK_SIZE | lookup buffer
2120 * +---------+---------------------------+
2122 * Old tags are inserted to a temporary buffer with position:
2123 * tempbuf_insert(position/ENTRY_CHUNK_SIZE, ...);
2124 * And new tags with index:
2125 * tempbuf_insert(idx, ...);
2127 * The buffer is sorted and written into tag file:
2128 * tempbuf_sort(...);
2129 * leaving master index locations messed up.
2131 * That is fixed using the lookup buffer for old tags:
2132 * new_seek = tempbuf_find_location(old_seek, ...);
2134 * new_seek = tempbuf_find_location(idx);
2136 lookup
= (struct tempbuf_searchidx
**)&tempbuf
[tempbuf_pos
];
2137 tempbuf_pos
+= lookup_buffer_depth
* sizeof(void **);
2138 memset(lookup
, 0, lookup_buffer_depth
* sizeof(void **));
2140 /* And calculate the remaining data space used mainly for storing
2141 * tag data (strings). */
2142 tempbuf_left
= tempbuf_size
- tempbuf_pos
- 8;
2143 if (tempbuf_left
- TAGFILE_ENTRY_AVG_LENGTH
* commit_entry_count
< 0)
2145 logf("Buffer way too small!");
2152 * If tag file contains unique tags (sorted index), we will load
2153 * it entirely into memory so we can resort it later for use with
2156 if (tagcache_is_sorted_tag(index_type
))
2158 logf("loading tags...");
2159 for (i
= 0; i
< tch
.entry_count
; i
++)
2161 struct tagfile_entry entry
;
2162 int loc
= lseek(fd
, 0, SEEK_CUR
);
2165 if (ecread(fd
, &entry
, 1, tagfile_entry_ec
, tc_stat
.econ
)
2166 != sizeof(struct tagfile_entry
))
2168 logf("read error #7");
2173 if (entry
.tag_length
>= (int)sizeof(buf
))
2175 logf("too long tag #3");
2180 if (read(fd
, buf
, entry
.tag_length
) != entry
.tag_length
)
2182 logf("read error #8");
2187 /* Skip deleted entries. */
2192 * Save the tag and tag id in the memory buffer. Tag id
2193 * is saved so we can later reindex the master lookup
2194 * table when the index gets resorted.
2196 ret
= tempbuf_insert(buf
, loc
/TAGFILE_ENTRY_CHUNK_LENGTH
2197 + commit_entry_count
, entry
.idx_id
,
2198 tagcache_is_unique_tag(index_type
));
2209 tempbufidx
= tch
.entry_count
;
2214 * Creating new index file to store the tags. No need to preload
2215 * anything whether the index type is sorted or not.
2217 snprintf(buf
, sizeof buf
, TAGCACHE_FILE_INDEX
, index_type
);
2218 fd
= open(buf
, O_WRONLY
| O_CREAT
| O_TRUNC
);
2221 logf("%s open fail", buf
);
2225 tch
.magic
= TAGCACHE_MAGIC
;
2226 tch
.entry_count
= 0;
2229 if (ecwrite(fd
, &tch
, 1, tagcache_header_ec
, tc_stat
.econ
)
2230 != sizeof(struct tagcache_header
))
2232 logf("header write failed");
2238 /* Loading the tag lookup file as "master file". */
2239 logf("Loading index file");
2240 masterfd
= open(TAGCACHE_FILE_MASTER
, O_RDWR
);
2244 logf("Creating new DB");
2245 masterfd
= open(TAGCACHE_FILE_MASTER
, O_WRONLY
| O_CREAT
| O_TRUNC
);
2249 logf("Failure to create index file (%s)", TAGCACHE_FILE_MASTER
);
2254 /* Write the header (write real values later). */
2255 memset(&tcmh
, 0, sizeof(struct master_header
));
2257 tcmh
.tch
.entry_count
= 0;
2258 tcmh
.tch
.datasize
= 0;
2260 ecwrite(masterfd
, &tcmh
, 1, master_header_ec
, tc_stat
.econ
);
2262 masterfd_pos
= lseek(masterfd
, 0, SEEK_CUR
);
2267 * Master file already exists so we need to process the current
2272 if (ecread(masterfd
, &tcmh
, 1, master_header_ec
, tc_stat
.econ
) !=
2273 sizeof(struct master_header
) || tcmh
.tch
.magic
!= TAGCACHE_MAGIC
)
2275 logf("header error");
2282 * If we reach end of the master file, we need to expand it to
2283 * hold new tags. If the current index is not sorted, we can
2284 * simply append new data to end of the file.
2285 * However, if the index is sorted, we need to update all tag
2286 * pointers in the master file for the current index.
2288 masterfd_pos
= lseek(masterfd
, tcmh
.tch
.entry_count
* sizeof(struct index_entry
),
2290 if (masterfd_pos
== filesize(masterfd
))
2292 logf("appending...");
2298 * Load new unique tags in memory to be sorted later and added
2299 * to the master lookup file.
2301 if (tagcache_is_sorted_tag(index_type
))
2303 lseek(tmpfd
, sizeof(struct tagcache_header
), SEEK_SET
);
2304 /* h is the header of the temporary file containing new tags. */
2305 logf("inserting new tags...");
2306 for (i
= 0; i
< h
->entry_count
; i
++)
2308 struct temp_file_entry entry
;
2310 if (read(tmpfd
, &entry
, sizeof(struct temp_file_entry
)) !=
2311 sizeof(struct temp_file_entry
))
2313 logf("read fail #3");
2319 if (entry
.tag_length
[index_type
] >= (long)sizeof(buf
))
2321 logf("too long entry!");
2326 lseek(tmpfd
, entry
.tag_offset
[index_type
], SEEK_CUR
);
2327 if (read(tmpfd
, buf
, entry
.tag_length
[index_type
]) !=
2328 entry
.tag_length
[index_type
])
2330 logf("read fail #4");
2335 if (tagcache_is_unique_tag(index_type
))
2336 error
= !tempbuf_insert(buf
, i
, -1, true);
2338 error
= !tempbuf_insert(buf
, i
, tcmh
.tch
.entry_count
+ i
, false);
2342 logf("insert error");
2347 lseek(tmpfd
, entry
.data_length
- entry
.tag_offset
[index_type
] -
2348 entry
.tag_length
[index_type
], SEEK_CUR
);
2353 /* Sort the buffer data and write it to the index file. */
2354 lseek(fd
, sizeof(struct tagcache_header
), SEEK_SET
);
2355 i
= tempbuf_sort(fd
);
2358 logf("sorted %d tags", i
);
2361 * Now update all indexes in the master lookup file.
2363 logf("updating indices...");
2364 lseek(masterfd
, sizeof(struct master_header
), SEEK_SET
);
2365 for (i
= 0; i
< tcmh
.tch
.entry_count
; i
+= idxbuf_pos
)
2368 int loc
= lseek(masterfd
, 0, SEEK_CUR
);
2370 idxbuf_pos
= MIN(tcmh
.tch
.entry_count
- i
, IDX_BUF_DEPTH
);
2372 if (ecread(masterfd
, idxbuf
, idxbuf_pos
, index_entry_ec
, tc_stat
.econ
)
2373 != (int)sizeof(struct index_entry
)*idxbuf_pos
)
2375 logf("read fail #5");
2379 lseek(masterfd
, loc
, SEEK_SET
);
2381 for (j
= 0; j
< idxbuf_pos
; j
++)
2383 if (idxbuf
[j
].flag
& FLAG_DELETED
)
2385 /* We can just ignore deleted entries. */
2386 idxbuf
[j
].tag_seek
[index_type
] = 0;
2390 idxbuf
[j
].tag_seek
[index_type
] = tempbuf_find_location(
2391 idxbuf
[j
].tag_seek
[index_type
]/TAGFILE_ENTRY_CHUNK_LENGTH
2392 + commit_entry_count
);
2394 if (idxbuf
[j
].tag_seek
[index_type
] < 0)
2396 logf("update error: %d/%ld", i
+j
, tcmh
.tch
.entry_count
);
2404 /* Write back the updated index. */
2405 if (ecwrite(masterfd
, idxbuf
, idxbuf_pos
,
2406 index_entry_ec
, tc_stat
.econ
) !=
2407 (int)sizeof(struct index_entry
)*idxbuf_pos
)
2418 * Walk through the temporary file containing the new tags.
2420 // build_normal_index(h, tmpfd, masterfd, idx);
2421 logf("updating new indices...");
2422 lseek(masterfd
, masterfd_pos
, SEEK_SET
);
2423 lseek(tmpfd
, sizeof(struct tagcache_header
), SEEK_SET
);
2424 lseek(fd
, 0, SEEK_END
);
2425 for (i
= 0; i
< h
->entry_count
; i
+= idxbuf_pos
)
2429 idxbuf_pos
= MIN(h
->entry_count
- i
, IDX_BUF_DEPTH
);
2432 memset(idxbuf
, 0, sizeof(struct index_entry
)*IDX_BUF_DEPTH
);
2436 int loc
= lseek(masterfd
, 0, SEEK_CUR
);
2438 if (ecread(masterfd
, idxbuf
, idxbuf_pos
, index_entry_ec
, tc_stat
.econ
)
2439 != (int)sizeof(struct index_entry
)*idxbuf_pos
)
2441 logf("read fail #6");
2445 lseek(masterfd
, loc
, SEEK_SET
);
2448 /* Read entry headers. */
2449 for (j
= 0; j
< idxbuf_pos
; j
++)
2451 if (!tagcache_is_sorted_tag(index_type
))
2453 struct temp_file_entry entry
;
2454 struct tagfile_entry fe
;
2456 if (read(tmpfd
, &entry
, sizeof(struct temp_file_entry
)) !=
2457 sizeof(struct temp_file_entry
))
2459 logf("read fail #7");
2465 if (entry
.tag_length
[index_type
] >= (int)sizeof(buf
))
2467 logf("too long entry!");
2468 logf("length=%d", entry
.tag_length
[index_type
]);
2469 logf("pos=0x%02lx", lseek(tmpfd
, 0, SEEK_CUR
));
2474 lseek(tmpfd
, entry
.tag_offset
[index_type
], SEEK_CUR
);
2475 if (read(tmpfd
, buf
, entry
.tag_length
[index_type
]) !=
2476 entry
.tag_length
[index_type
])
2478 logf("read fail #8");
2479 logf("offset=0x%02lx", entry
.tag_offset
[index_type
]);
2480 logf("length=0x%02x", entry
.tag_length
[index_type
]);
2485 /* Write to index file. */
2486 idxbuf
[j
].tag_seek
[index_type
] = lseek(fd
, 0, SEEK_CUR
);
2487 fe
.tag_length
= entry
.tag_length
[index_type
];
2488 fe
.idx_id
= tcmh
.tch
.entry_count
+ i
+ j
;
2489 ecwrite(fd
, &fe
, 1, tagfile_entry_ec
, tc_stat
.econ
);
2490 write(fd
, buf
, fe
.tag_length
);
2494 lseek(tmpfd
, entry
.data_length
- entry
.tag_offset
[index_type
] -
2495 entry
.tag_length
[index_type
], SEEK_CUR
);
2499 /* Locate the correct entry from the sorted array. */
2500 idxbuf
[j
].tag_seek
[index_type
] = tempbuf_find_location(i
+ j
);
2501 if (idxbuf
[j
].tag_seek
[index_type
] < 0)
2503 logf("entry not found (%d)", j
);
2511 if (ecwrite(masterfd
, idxbuf
, idxbuf_pos
,
2512 index_entry_ec
, tc_stat
.econ
) !=
2513 (int)sizeof(struct index_entry
)*idxbuf_pos
)
2515 logf("tagcache: write fail #4");
2524 /* Finally write the header. */
2525 tch
.magic
= TAGCACHE_MAGIC
;
2526 tch
.entry_count
= tempbufidx
;
2527 tch
.datasize
= lseek(fd
, 0, SEEK_END
) - sizeof(struct tagcache_header
);
2528 lseek(fd
, 0, SEEK_SET
);
2529 ecwrite(fd
, &tch
, 1, tagcache_header_ec
, tc_stat
.econ
);
2531 if (index_type
!= tag_filename
)
2532 h
->datasize
+= tch
.datasize
;
2533 logf("s:%d/%ld/%ld", index_type
, tch
.datasize
, h
->datasize
);
2545 static bool commit(void)
2547 struct tagcache_header tch
;
2548 struct master_header tcmh
;
2552 #ifdef HAVE_DIRCACHE
2553 bool dircache_buffer_stolen
= false;
2555 bool local_allocation
= false;
2557 logf("committing tagcache");
2562 tmpfd
= open(TAGCACHE_FILE_TEMP
, O_RDONLY
);
2565 logf("nothing to commit");
2570 /* Load the header. */
2571 len
= sizeof(struct tagcache_header
);
2572 rc
= read(tmpfd
, &tch
, len
);
2574 if (tch
.magic
!= TAGCACHE_MAGIC
|| rc
!= len
)
2576 logf("incorrect header");
2578 remove(TAGCACHE_FILE_TEMP
);
2582 if (tch
.entry_count
== 0)
2584 logf("nothing to commit");
2586 remove(TAGCACHE_FILE_TEMP
);
2590 #ifdef HAVE_EEPROM_SETTINGS
2591 remove(TAGCACHE_STATEFILE
);
2594 /* At first be sure to unload the ramcache! */
2595 #ifdef HAVE_TC_RAMCACHE
2596 tc_stat
.ramcache
= false;
2601 /* Try to steal every buffer we can :) */
2602 if (tempbuf_size
== 0)
2603 local_allocation
= true;
2605 #ifdef HAVE_DIRCACHE
2606 if (tempbuf_size
== 0)
2608 /* Try to steal the dircache buffer. */
2609 tempbuf
= dircache_steal_buffer(&tempbuf_size
);
2610 tempbuf_size
&= ~0x03;
2612 if (tempbuf_size
> 0)
2614 dircache_buffer_stolen
= true;
2619 #ifdef HAVE_TC_RAMCACHE
2620 if (tempbuf_size
== 0 && tc_stat
.ramcache_allocated
> 0)
2622 tempbuf
= (char *)(hdr
+ 1);
2623 tempbuf_size
= tc_stat
.ramcache_allocated
- sizeof(struct ramcache_header
) - 128;
2624 tempbuf_size
&= ~0x03;
2628 /* And finally fail if there are no buffers available. */
2629 if (tempbuf_size
== 0)
2631 logf("delaying commit until next boot");
2632 tc_stat
.commit_delayed
= true;
2638 logf("commit %ld entries...", tch
.entry_count
);
2640 /* Mark DB dirty so it will stay disabled if commit fails. */
2641 current_tcmh
.dirty
= true;
2642 update_master_header();
2644 /* Now create the index files. */
2645 tc_stat
.commit_step
= 0;
2647 tc_stat
.commit_delayed
= false;
2649 for (i
= 0; i
< TAG_COUNT
; i
++)
2653 if (tagcache_is_numeric_tag(i
))
2656 tc_stat
.commit_step
++;
2657 ret
= build_index(i
, &tch
, tmpfd
);
2661 logf("tagcache failed init");
2665 tc_stat
.commit_delayed
= true;
2666 tc_stat
.commit_step
= 0;
2672 if (!build_numeric_indices(&tch
, tmpfd
))
2674 logf("Failure to commit numeric indices");
2677 tc_stat
.commit_step
= 0;
2683 tc_stat
.commit_step
= 0;
2685 /* Update the master index headers. */
2686 if ( (masterfd
= open_master_fd(&tcmh
, true)) < 0)
2692 tcmh
.tch
.entry_count
+= tch
.entry_count
;
2693 tcmh
.tch
.datasize
= sizeof(struct master_header
)
2694 + sizeof(struct index_entry
) * tcmh
.tch
.entry_count
2699 lseek(masterfd
, 0, SEEK_SET
);
2700 ecwrite(masterfd
, &tcmh
, 1, master_header_ec
, tc_stat
.econ
);
2703 logf("tagcache committed");
2704 remove(TAGCACHE_FILE_TEMP
);
2705 tc_stat
.ready
= check_all_headers();
2706 tc_stat
.readyvalid
= true;
2708 if (local_allocation
)
2714 #ifdef HAVE_DIRCACHE
2715 /* Rebuild the dircache, if we stole the buffer. */
2716 if (dircache_buffer_stolen
)
2720 #ifdef HAVE_TC_RAMCACHE
2721 /* Reload tagcache. */
2722 if (tc_stat
.ramcache_allocated
> 0)
2723 tagcache_start_scan();
2731 static void allocate_tempbuf(void)
2733 /* Yeah, malloc would be really nice now :) */
2735 tempbuf_size
= 32*1024*1024;
2736 tempbuf
= malloc(tempbuf_size
);
2738 tempbuf
= (char *)(((long)audiobuf
& ~0x03) + 0x04);
2739 tempbuf_size
= (long)audiobufend
- (long)audiobuf
- 4;
2740 audiobuf
+= tempbuf_size
;
2744 static void free_tempbuf(void)
2746 if (tempbuf_size
== 0)
2752 audiobuf
-= tempbuf_size
;
2758 static bool modify_numeric_entry(int masterfd
, int idx_id
, int tag
, long data
)
2760 struct index_entry idx
;
2765 if (!tagcache_is_numeric_tag(tag
))
2768 if (!get_index(masterfd
, idx_id
, &idx
, false))
2771 idx
.tag_seek
[tag
] = data
;
2772 idx
.flag
|= FLAG_DIRTYNUM
;
2774 return write_index(masterfd
, idx_id
, &idx
);
2778 bool tagcache_modify_numeric_entry(struct tagcache_search
*tcs
,
2781 struct master_header myhdr
;
2783 if (tcs
->masterfd
< 0)
2785 if ( (tcs
->masterfd
= open_master_fd(&myhdr
, true)) < 0)
2789 return modify_numeric_entry(tcs
->masterfd
, tcs
->idx_id
, tag
, data
);
2793 #define COMMAND_QUEUE_IS_EMPTY (command_queue_ridx == command_queue_widx)
2795 static bool command_queue_is_full(void)
2799 next
= command_queue_widx
+ 1;
2800 if (next
>= TAGCACHE_COMMAND_QUEUE_LENGTH
)
2803 return (next
== command_queue_ridx
);
2806 void run_command_queue(bool force
)
2808 struct master_header myhdr
;
2811 if (COMMAND_QUEUE_IS_EMPTY
)
2814 if (!force
&& !command_queue_is_full()
2815 && current_tick
- TAGCACHE_COMMAND_QUEUE_COMMIT_DELAY
2816 < command_queue_timestamp
)
2821 mutex_lock(&command_queue_mutex
);
2823 if ( (masterfd
= open_master_fd(&myhdr
, true)) < 0)
2826 while (command_queue_ridx
!= command_queue_widx
)
2828 struct tagcache_command_entry
*ce
= &command_queue
[command_queue_ridx
];
2830 switch (ce
->command
)
2832 case CMD_UPDATE_MASTER_HEADER
:
2835 update_master_header();
2837 /* Re-open the masterfd. */
2838 if ( (masterfd
= open_master_fd(&myhdr
, true)) < 0)
2843 case CMD_UPDATE_NUMERIC
:
2845 modify_numeric_entry(masterfd
, ce
->idx_id
, ce
->tag
, ce
->data
);
2850 if (++command_queue_ridx
>= TAGCACHE_COMMAND_QUEUE_LENGTH
)
2851 command_queue_ridx
= 0;
2856 mutex_unlock(&command_queue_mutex
);
2859 static void queue_command(int cmd
, long idx_id
, int tag
, long data
)
2865 mutex_lock(&command_queue_mutex
);
2866 next
= command_queue_widx
+ 1;
2867 if (next
>= TAGCACHE_COMMAND_QUEUE_LENGTH
)
2870 /* Make sure queue is not full. */
2871 if (next
!= command_queue_ridx
)
2873 struct tagcache_command_entry
*ce
= &command_queue
[command_queue_widx
];
2876 ce
->idx_id
= idx_id
;
2880 command_queue_widx
= next
;
2881 command_queue_timestamp
= current_tick
;
2882 mutex_unlock(&command_queue_mutex
);
2886 /* Queue is full, try again later... */
2887 mutex_unlock(&command_queue_mutex
);
2892 long tagcache_increase_serial(void)
2902 old
= current_tcmh
.serial
++;
2903 queue_command(CMD_UPDATE_MASTER_HEADER
, 0, 0, 0);
2908 void tagcache_update_numeric(int idx_id
, int tag
, long data
)
2910 queue_command(CMD_UPDATE_NUMERIC
, idx_id
, tag
, data
);
2913 long tagcache_get_serial(void)
2915 return current_tcmh
.serial
;
2918 long tagcache_get_commitid(void)
2920 return current_tcmh
.commitid
;
2923 static bool write_tag(int fd
, const char *tagstr
, const char *datastr
)
2928 snprintf(buf
, sizeof buf
, "%s=\"", tagstr
);
2929 for (i
= strlen(buf
); i
< (long)sizeof(buf
)-3; i
++)
2931 if (*datastr
== '\0')
2934 if (*datastr
== '"')
2941 buf
[i
] = *(datastr
++);
2944 strcpy(&buf
[i
], "\" ");
2946 write(fd
, buf
, i
+ 2);
2951 static bool read_tag(char *dest
, long size
,
2952 const char *src
, const char *tagstr
)
2955 char current_tag
[32];
2957 while (*src
!= '\0')
2959 /* Skip all whitespace */
2967 /* Read in tag name */
2968 while (*src
!= '=' && *src
!= ' ')
2970 current_tag
[pos
] = *src
;
2974 if (*src
== '\0' || pos
>= (long)sizeof(current_tag
))
2977 current_tag
[pos
] = '\0';
2979 /* Read in tag data */
2981 /* Find the start. */
2982 while (*src
!= '"' && *src
!= '\0')
2985 if (*src
== '\0' || *(++src
) == '\0')
2988 /* Read the data. */
2989 for (pos
= 0; pos
< size
; pos
++)
2994 if (*src
== '\\' && *(src
+1) == '"')
3016 if (!strcasecmp(tagstr
, current_tag
))
3023 static int parse_changelog_line(int line_n
, const char *buf
, void *parameters
)
3025 struct index_entry idx
;
3026 char tag_data
[TAG_MAXLEN
+32];
3028 long masterfd
= (long)parameters
;
3029 const int import_tags
[] = { tag_playcount
, tag_rating
, tag_playtime
, tag_lastplayed
,
3037 logf("%d/%s", line_n
, buf
);
3038 if (!read_tag(tag_data
, sizeof tag_data
, buf
, "filename"))
3040 logf("filename missing");
3045 idx_id
= find_index(tag_data
);
3048 logf("entry not found");
3052 if (!get_index(masterfd
, idx_id
, &idx
, false))
3054 logf("failed to retrieve index entry");
3058 /* Stop if tag has already been modified. */
3059 if (idx
.flag
& FLAG_DIRTYNUM
)
3062 logf("import: %s", tag_data
);
3064 idx
.flag
|= FLAG_DIRTYNUM
;
3065 for (i
= 0; i
< (long)(sizeof(import_tags
)/sizeof(import_tags
[0])); i
++)
3069 if (!read_tag(tag_data
, sizeof tag_data
, buf
,
3070 tagcache_tag_to_str(import_tags
[i
])))
3075 data
= atoi(tag_data
);
3079 idx
.tag_seek
[import_tags
[i
]] = data
;
3081 if (import_tags
[i
] == tag_lastplayed
&& data
> current_tcmh
.serial
)
3082 current_tcmh
.serial
= data
;
3083 else if (import_tags
[i
] == tag_commitid
&& data
>= current_tcmh
.commitid
)
3084 current_tcmh
.commitid
= data
+ 1;
3087 return write_index(masterfd
, idx_id
, &idx
) ? 0 : -5;
3091 bool tagcache_import_changelog(void)
3093 struct master_header myhdr
;
3094 struct tagcache_header tch
;
3105 clfd
= open(TAGCACHE_FILE_CHANGELOG
, O_RDONLY
);
3108 logf("failure to open changelog");
3112 if ( (masterfd
= open_master_fd(&myhdr
, true)) < 0)
3120 filenametag_fd
= open_tag_fd(&tch
, tag_filename
, false);
3122 fast_readline(clfd
, buf
, sizeof buf
, (long *)masterfd
,
3123 parse_changelog_line
);
3128 if (filenametag_fd
>= 0)
3129 close(filenametag_fd
);
3133 update_master_header();
3139 bool tagcache_create_changelog(struct tagcache_search
*tcs
)
3141 struct master_header myhdr
;
3142 struct index_entry idx
;
3143 char buf
[TAG_MAXLEN
+32];
3151 if (!tagcache_search(tcs
, tag_filename
))
3154 /* Initialize the changelog */
3155 clfd
= open(TAGCACHE_FILE_CHANGELOG
, O_WRONLY
| O_CREAT
| O_TRUNC
);
3158 logf("failure to open changelog");
3162 if (tcs
->masterfd
< 0)
3164 if ( (tcs
->masterfd
= open_master_fd(&myhdr
, false)) < 0)
3169 lseek(tcs
->masterfd
, 0, SEEK_SET
);
3170 ecread(tcs
->masterfd
, &myhdr
, 1, master_header_ec
, tc_stat
.econ
);
3173 write(clfd
, "## Changelog version 1\n", 23);
3175 for (i
= 0; i
< myhdr
.tch
.entry_count
; i
++)
3177 if (ecread(tcs
->masterfd
, &idx
, 1, index_entry_ec
, tc_stat
.econ
)
3178 != sizeof(struct index_entry
))
3180 logf("read error #9");
3181 tagcache_search_finish(tcs
);
3186 /* Skip until the entry found has been modified. */
3187 if (! (idx
.flag
& FLAG_DIRTYNUM
) )
3190 /* Skip deleted entries too. */
3191 if (idx
.flag
& FLAG_DELETED
)
3194 /* Now retrieve all tags. */
3195 for (j
= 0; j
< TAG_COUNT
; j
++)
3197 if (tagcache_is_numeric_tag(j
))
3199 snprintf(temp
, sizeof temp
, "%d", idx
.tag_seek
[j
]);
3200 write_tag(clfd
, tagcache_tag_to_str(j
), temp
);
3205 tagcache_retrieve(tcs
, i
, tcs
->type
, buf
, sizeof buf
);
3206 write_tag(clfd
, tagcache_tag_to_str(j
), buf
);
3209 write(clfd
, "\n", 1);
3215 tagcache_search_finish(tcs
);
3220 static bool delete_entry(long idx_id
)
3224 struct index_entry idx
, myidx
;
3225 struct master_header myhdr
;
3226 char buf
[TAG_MAXLEN
+32];
3227 int in_use
[TAG_COUNT
];
3229 logf("delete_entry(): %ld", idx_id
);
3231 #ifdef HAVE_TC_RAMCACHE
3232 /* At first mark the entry removed from ram cache. */
3233 if (tc_stat
.ramcache
)
3234 hdr
->indices
[idx_id
].flag
|= FLAG_DELETED
;
3237 if ( (fd
= open_master_fd(&myhdr
, true) ) < 0)
3240 lseek(fd
, idx_id
* sizeof(struct index_entry
), SEEK_CUR
);
3241 if (ecread(fd
, &myidx
, 1, index_entry_ec
, tc_stat
.econ
)
3242 != sizeof(struct index_entry
))
3244 logf("delete_entry(): read error");
3249 myidx
.flag
|= FLAG_DELETED
;
3250 lseek(fd
, -sizeof(struct index_entry
), SEEK_CUR
);
3251 if (ecwrite(fd
, &myidx
, 1, index_entry_ec
, tc_stat
.econ
)
3252 != sizeof(struct index_entry
))
3254 logf("delete_entry(): write_error");
3259 /* Now check which tags are no longer in use (if any) */
3260 for (tag
= 0; tag
< TAG_COUNT
; tag
++)
3263 lseek(fd
, sizeof(struct master_header
), SEEK_SET
);
3264 for (i
= 0; i
< myhdr
.tch
.entry_count
; i
++)
3266 struct index_entry
*idxp
;
3268 #ifdef HAVE_TC_RAMCACHE
3269 /* Use RAM DB if available for greater speed */
3270 if (tc_stat
.ramcache
)
3271 idxp
= &hdr
->indices
[i
];
3275 if (ecread(fd
, &idx
, 1, index_entry_ec
, tc_stat
.econ
)
3276 != sizeof(struct index_entry
))
3278 logf("delete_entry(): read error #2");
3285 if (idxp
->flag
& FLAG_DELETED
)
3288 for (tag
= 0; tag
< TAG_COUNT
; tag
++)
3290 if (tagcache_is_numeric_tag(tag
))
3293 if (idxp
->tag_seek
[tag
] == myidx
.tag_seek
[tag
])
3300 /* Now delete all tags no longer in use. */
3301 for (tag
= 0; tag
< TAG_COUNT
; tag
++)
3303 if (tagcache_is_numeric_tag(tag
))
3308 logf("in use: %d/%d", tag
, in_use
[tag
]);
3312 #ifdef HAVE_TC_RAMCACHE
3313 /* Delete from ram. */
3314 if (tc_stat
.ramcache
&& tag
!= tag_filename
)
3316 struct tagfile_entry
*tagentry
= get_tag(&myidx
, tag
);
3317 tagentry
->tag_data
[0] = '\0';
3321 /* Open the index file, which contains the tag names. */
3322 snprintf(buf
, sizeof buf
, TAGCACHE_FILE_INDEX
, tag
);
3323 fd
= open(buf
, O_RDWR
);
3327 logf("open failed");
3331 /* Skip the header block */
3332 lseek(fd
, myidx
.tag_seek
[tag
] + sizeof(struct tagfile_entry
), SEEK_SET
);
3334 /* Debug, print 10 first characters of the tag
3337 logf("TAG:%s", buf);
3338 lseek(fd, -10, SEEK_CUR);
3341 /* Write first data byte in tag as \0 */
3344 /* Now tag data has been removed */
3353 * Returns true if there is an event waiting in the queue
3354 * that requires the current operation to be aborted.
3356 static bool check_event_queue(void)
3360 queue_wait_w_tmo(&tagcache_queue
, &ev
, 0);
3365 case SYS_USB_CONNECTED
:
3366 /* Put the event back into the queue. */
3367 queue_post(&tagcache_queue
, ev
.id
, ev
.data
);
3375 #ifdef HAVE_TC_RAMCACHE
3376 static bool allocate_tagcache(void)
3378 struct master_header tcmh
;
3381 /* Load the header. */
3382 if ( (fd
= open_master_fd(&tcmh
, false)) < 0)
3391 * Now calculate the required cache size plus
3392 * some extra space for alignment fixes.
3394 tc_stat
.ramcache_allocated
= tcmh
.tch
.datasize
+ 128 + TAGCACHE_RESERVE
+
3395 sizeof(struct ramcache_header
) + TAG_COUNT
*sizeof(void *);
3396 hdr
= buffer_alloc(tc_stat
.ramcache_allocated
+ 128);
3397 memset(hdr
, 0, sizeof(struct ramcache_header
));
3398 memcpy(&hdr
->h
, &tcmh
, sizeof(struct master_header
));
3399 hdr
->indices
= (struct index_entry
*)(hdr
+ 1);
3400 logf("tagcache: %d bytes allocated.", tc_stat
.ramcache_allocated
);
3405 # ifdef HAVE_EEPROM_SETTINGS
3406 static bool tagcache_dumpload(void)
3408 struct statefile_header shdr
;
3413 fd
= open(TAGCACHE_STATEFILE
, O_RDONLY
);
3416 logf("no tagcache statedump");
3420 /* Check the statefile memory placement */
3421 hdr
= buffer_alloc(0);
3422 rc
= read(fd
, &shdr
, sizeof(struct statefile_header
));
3423 if (rc
!= sizeof(struct statefile_header
)
3424 /* || (long)hdr != (long)shdr.hdr */)
3426 logf("incorrect statefile");
3432 offpos
= (long)hdr
- (long)shdr
.hdr
;
3434 /* Lets allocate real memory and load it */
3435 hdr
= buffer_alloc(shdr
.tc_stat
.ramcache_allocated
);
3436 rc
= read(fd
, hdr
, shdr
.tc_stat
.ramcache_allocated
);
3439 if (rc
!= shdr
.tc_stat
.ramcache_allocated
)
3441 logf("read failure!");
3446 memcpy(&tc_stat
, &shdr
.tc_stat
, sizeof(struct tagcache_stat
));
3448 /* Now fix the pointers */
3449 hdr
->indices
= (struct index_entry
*)((long)hdr
->indices
+ offpos
);
3450 for (i
= 0; i
< TAG_COUNT
; i
++)
3451 hdr
->tags
[i
] += offpos
;
3456 static bool tagcache_dumpsave(void)
3458 struct statefile_header shdr
;
3461 if (!tc_stat
.ramcache
)
3464 fd
= open(TAGCACHE_STATEFILE
, O_WRONLY
| O_CREAT
| O_TRUNC
);
3467 logf("failed to create a statedump");
3471 /* Create the header */
3473 memcpy(&shdr
.tc_stat
, &tc_stat
, sizeof(struct tagcache_stat
));
3474 write(fd
, &shdr
, sizeof(struct statefile_header
));
3476 /* And dump the data too */
3477 write(fd
, hdr
, tc_stat
.ramcache_allocated
);
3484 static bool load_tagcache(void)
3486 struct tagcache_header
*tch
;
3487 long bytesleft
= tc_stat
.ramcache_allocated
;
3488 struct index_entry
*idx
;
3493 # ifdef HAVE_DIRCACHE
3494 while (dircache_is_initializing())
3498 logf("loading tagcache to ram...");
3500 fd
= open(TAGCACHE_FILE_MASTER
, O_RDONLY
);
3503 logf("tagcache open failed");
3507 if (ecread(fd
, &hdr
->h
, 1, master_header_ec
, tc_stat
.econ
)
3508 != sizeof(struct master_header
)
3509 || hdr
->h
.tch
.magic
!= TAGCACHE_MAGIC
)
3511 logf("incorrect header");
3517 /* Load the master index table. */
3518 for (i
= 0; i
< hdr
->h
.tch
.entry_count
; i
++)
3520 rc
= ecread(fd
, idx
, 1, index_entry_ec
, tc_stat
.econ
);
3521 if (rc
!= sizeof(struct index_entry
))
3523 logf("read error #10");
3528 bytesleft
-= sizeof(struct index_entry
);
3529 if (bytesleft
< 0 || ((long)idx
- (long)hdr
->indices
) >= tc_stat
.ramcache_allocated
)
3531 logf("too big tagcache.");
3541 /* Load the tags. */
3543 for (tag
= 0; tag
< TAG_COUNT
; tag
++)
3545 struct tagfile_entry
*fe
;
3546 char buf
[TAG_MAXLEN
+32];
3548 if (tagcache_is_numeric_tag(tag
))
3551 //p = ((void *)p+1);
3552 p
= (char *)((long)p
& ~0x03) + 0x04;
3555 /* Check the header. */
3556 tch
= (struct tagcache_header
*)p
;
3557 p
+= sizeof(struct tagcache_header
);
3559 if ( (fd
= open_tag_fd(tch
, tag
, false)) < 0)
3562 for (hdr
->entry_count
[tag
] = 0;
3563 hdr
->entry_count
[tag
] < tch
->entry_count
;
3564 hdr
->entry_count
[tag
]++)
3568 if (do_timed_yield())
3570 /* Abort if we got a critical event in queue */
3571 if (check_event_queue())
3575 fe
= (struct tagfile_entry
*)p
;
3576 pos
= lseek(fd
, 0, SEEK_CUR
);
3577 rc
= ecread(fd
, fe
, 1, tagfile_entry_ec
, tc_stat
.econ
);
3578 if (rc
!= sizeof(struct tagfile_entry
))
3580 /* End of lookup table. */
3581 logf("read error #11");
3586 /* We have a special handling for the filename tags. */
3587 if (tag
== tag_filename
)
3589 # ifdef HAVE_DIRCACHE
3590 const struct dirent
*dc
;
3593 // FIXME: This is wrong!
3594 // idx = &hdr->indices[hdr->entry_count[i]];
3595 idx
= &hdr
->indices
[fe
->idx_id
];
3597 if (fe
->tag_length
>= (long)sizeof(buf
)-1)
3601 logf("TAG:%s", buf
);
3602 logf("too long filename");
3607 rc
= read(fd
, buf
, fe
->tag_length
);
3608 if (rc
!= fe
->tag_length
)
3610 logf("read error #12");
3615 /* Check if the entry has already been removed */
3616 if (idx
->flag
& FLAG_DELETED
)
3619 /* This flag must not be used yet. */
3620 if (idx
->flag
& FLAG_DIRCACHE
)
3622 logf("internal error!");
3627 if (idx
->tag_seek
[tag
] != pos
)
3629 logf("corrupt data structures!");
3634 # ifdef HAVE_DIRCACHE
3635 if (dircache_is_enabled())
3637 dc
= dircache_get_entry_ptr(buf
);
3640 logf("Entry no longer valid.");
3642 delete_entry(fe
->idx_id
);
3646 idx
->flag
|= FLAG_DIRCACHE
;
3647 FLAG_SET_ATTR(idx
->flag
, fe
->idx_id
);
3648 idx
->tag_seek
[tag_filename
] = (long)dc
;
3654 # if 0 /* Maybe we could enable this for flash players. Too slow otherwise. */
3655 /* Check if entry has been removed. */
3656 if (global_settings
.tagcache_autoupdate
)
3660 testfd
= open(buf
, O_RDONLY
);
3663 logf("Entry no longer valid.");
3665 delete_entry(fe
->idx_id
);
3676 bytesleft
-= sizeof(struct tagfile_entry
) + fe
->tag_length
;
3679 logf("too big tagcache #2");
3680 logf("tl: %d", fe
->tag_length
);
3681 logf("bl: %ld", bytesleft
);
3687 rc
= read(fd
, fe
->tag_data
, fe
->tag_length
);
3690 if (rc
!= fe
->tag_length
)
3692 logf("read error #13");
3693 logf("rc=0x%04x", rc
); // 0x431
3694 logf("len=0x%04x", fe
->tag_length
); // 0x4000
3695 logf("pos=0x%04lx", lseek(fd
, 0, SEEK_CUR
)); // 0x433
3696 logf("tag=0x%02x", tag
); // 0x00
3704 tc_stat
.ramcache_used
= tc_stat
.ramcache_allocated
- bytesleft
;
3705 logf("tagcache loaded into ram!");
3709 #endif /* HAVE_TC_RAMCACHE */
3711 static bool check_deleted_files(void)
3714 char buf
[TAG_MAXLEN
+32];
3715 struct tagfile_entry tfe
;
3717 logf("reverse scan...");
3718 snprintf(buf
, sizeof buf
, TAGCACHE_FILE_INDEX
, tag_filename
);
3719 fd
= open(buf
, O_RDONLY
);
3723 logf("%s open fail", buf
);
3727 lseek(fd
, sizeof(struct tagcache_header
), SEEK_SET
);
3728 while (ecread(fd
, &tfe
, 1, tagfile_entry_ec
, tc_stat
.econ
)
3729 == sizeof(struct tagfile_entry
)
3731 && !check_event_queue()
3735 if (tfe
.tag_length
>= (long)sizeof(buf
)-1)
3737 logf("too long tag");
3742 if (read(fd
, buf
, tfe
.tag_length
) != tfe
.tag_length
)
3744 logf("read error #14");
3749 /* Check if the file has already deleted from the db. */
3753 /* Now check if the file exists. */
3754 testfd
= open(buf
, O_RDONLY
);
3757 logf("Entry no longer valid.");
3758 logf("-> %s / %d", buf
, tfe
.tag_length
);
3759 delete_entry(tfe
.idx_id
);
3771 static bool check_dir(const char *dirname
)
3775 int success
= false;
3777 dir
= opendir(dirname
);
3780 logf("tagcache: opendir() failed");
3784 /* Recursively scan the dir. */
3788 while (!check_event_queue())
3791 struct dirent
*entry
;
3793 entry
= readdir(dir
);
3801 if (!strcmp((char *)entry
->d_name
, ".") ||
3802 !strcmp((char *)entry
->d_name
, ".."))
3807 len
= strlen(curpath
);
3808 snprintf(&curpath
[len
], curpath_size
- len
, "/%s",
3811 processed_dir_count
++;
3812 if (entry
->attribute
& ATTR_DIRECTORY
)
3815 #if defined(HAVE_TC_RAMCACHE) && defined(HAVE_DIRCACHE)
3816 add_tagcache(curpath
, dir
->internal_entry
);
3818 add_tagcache(curpath
);
3821 curpath
[len
] = '\0';
3829 void build_tagcache(const char *path
)
3831 struct tagcache_header header
;
3836 total_entry_count
= 0;
3837 processed_dir_count
= 0;
3839 #ifdef HAVE_DIRCACHE
3840 while (dircache_is_initializing())
3844 logf("updating tagcache");
3846 cachefd
= open(TAGCACHE_FILE_TEMP
, O_RDONLY
);
3849 logf("skipping, cache already waiting for commit");
3854 cachefd
= open(TAGCACHE_FILE_TEMP
, O_RDWR
| O_CREAT
| O_TRUNC
);
3857 logf("master file open failed: %s", TAGCACHE_FILE_TEMP
);
3861 filenametag_fd
= open_tag_fd(&header
, tag_filename
, false);
3865 logf("Scanning files...");
3866 /* Scan for new files. */
3867 memset(&header
, 0, sizeof(struct tagcache_header
));
3868 write(cachefd
, &header
, sizeof(struct tagcache_header
));
3870 if (strcmp("/", path
) != 0)
3871 strcpy(curpath
, path
);
3872 ret
= check_dir(path
);
3874 /* Write the header. */
3875 header
.magic
= TAGCACHE_MAGIC
;
3876 header
.datasize
= data_size
;
3877 header
.entry_count
= total_entry_count
;
3878 lseek(cachefd
, 0, SEEK_SET
);
3879 write(cachefd
, &header
, sizeof(struct tagcache_header
));
3882 if (filenametag_fd
>= 0)
3884 close(filenametag_fd
);
3885 filenametag_fd
= -1;
3895 /* Commit changes to the database. */
3901 remove(TAGCACHE_FILE_TEMP
);
3902 logf("tagcache built!");
3908 #ifdef HAVE_TC_RAMCACHE
3911 /* Import runtime statistics if we just initialized the db. */
3912 if (hdr
->h
.serial
== 0)
3913 queue_post(&tagcache_queue
, Q_IMPORT_CHANGELOG
, 0);
3920 #ifdef HAVE_TC_RAMCACHE
3921 static void load_ramcache(void)
3928 /* At first we should load the cache (if exists). */
3929 tc_stat
.ramcache
= load_tagcache();
3931 if (!tc_stat
.ramcache
)
3933 /* If loading failed, it must indicate some problem with the db
3934 * so disable it entirely to prevent further issues. */
3935 tc_stat
.ready
= false;
3942 void tagcache_unload_ramcache(void)
3944 tc_stat
.ramcache
= false;
3945 /* Just to make sure there is no statefile present. */
3946 // remove(TAGCACHE_STATEFILE);
3951 static void tagcache_thread(void)
3954 bool check_done
= false;
3956 /* If the previous cache build/update was interrupted, commit
3957 * the changes first in foreground. */
3963 #ifdef HAVE_TC_RAMCACHE
3964 # ifdef HAVE_EEPROM_SETTINGS
3965 if (firmware_settings
.initialized
&& firmware_settings
.disk_clean
)
3966 check_done
= tagcache_dumpload();
3968 remove(TAGCACHE_STATEFILE
);
3971 /* Allocate space for the tagcache if found on disk. */
3972 if (global_settings
.tagcache_ram
&& !tc_stat
.ramcache
)
3973 allocate_tagcache();
3977 tc_stat
.initialized
= true;
3979 /* Don't delay bootup with the header check but do it on background. */
3981 tc_stat
.ready
= check_all_headers();
3982 tc_stat
.readyvalid
= true;
3986 run_command_queue(false);
3988 queue_wait_w_tmo(&tagcache_queue
, &ev
, HZ
);
3992 case Q_IMPORT_CHANGELOG
:
3993 tagcache_import_changelog();
3998 build_tagcache("/");
4002 build_tagcache("/");
4003 #ifdef HAVE_TC_RAMCACHE
4006 check_deleted_files();
4012 if (check_done
|| !tc_stat
.ready
)
4015 #ifdef HAVE_TC_RAMCACHE
4016 if (!tc_stat
.ramcache
&& global_settings
.tagcache_ram
)
4019 if (tc_stat
.ramcache
&& global_settings
.tagcache_autoupdate
)
4020 build_tagcache("/");
4024 if (global_settings
.tagcache_autoupdate
)
4026 build_tagcache("/");
4027 /* Don't do auto removal without dircache (very slow). */
4028 #ifdef HAVE_DIRCACHE
4029 if (dircache_is_enabled())
4030 check_deleted_files();
4034 logf("tagcache check done");
4046 case SYS_USB_CONNECTED
:
4047 logf("USB: TagCache");
4048 usb_acknowledge(SYS_USB_CONNECTED_ACK
);
4049 usb_wait_for_disconnect(&tagcache_queue
);
4056 bool tagcache_prepare_shutdown(void)
4058 if (tagcache_get_commit_step() > 0)
4061 tagcache_stop_scan();
4062 while (read_lock
|| write_lock
)
4068 void tagcache_shutdown(void)
4070 /* Flush the command queue. */
4071 run_command_queue(true);
4073 #ifdef HAVE_EEPROM_SETTINGS
4074 if (tc_stat
.ramcache
)
4075 tagcache_dumpsave();
4079 static int get_progress(void)
4081 int total_count
= -1;
4083 #ifdef HAVE_DIRCACHE
4084 if (dircache_is_enabled())
4086 total_count
= dircache_get_entry_count();
4090 #ifdef HAVE_TC_RAMCACHE
4092 if (hdr
&& tc_stat
.ramcache
)
4093 total_count
= hdr
->h
.tch
.entry_count
;
4097 if (total_count
< 0)
4100 return processed_dir_count
* 100 / total_count
;
4103 struct tagcache_stat
* tagcache_get_stat(void)
4105 tc_stat
.progress
= get_progress();
4106 tc_stat
.processed_entries
= processed_dir_count
;
4111 void tagcache_start_scan(void)
4113 queue_post(&tagcache_queue
, Q_START_SCAN
, 0);
4116 bool tagcache_update(void)
4121 queue_post(&tagcache_queue
, Q_UPDATE
, 0);
4125 bool tagcache_rebuild()
4127 queue_post(&tagcache_queue
, Q_REBUILD
, 0);
4131 void tagcache_stop_scan(void)
4133 queue_post(&tagcache_queue
, Q_STOP_SCAN
, 0);
4136 #ifdef HAVE_TC_RAMCACHE
4137 bool tagcache_is_ramcache(void)
4139 return tc_stat
.ramcache
;
4143 #endif /* !__PCTOOL__ */
4146 void tagcache_init(void)
4148 memset(&tc_stat
, 0, sizeof(struct tagcache_stat
));
4149 memset(¤t_tcmh
, 0, sizeof(struct master_header
));
4150 filenametag_fd
= -1;
4151 write_lock
= read_lock
= 0;
4154 mutex_init(&command_queue_mutex
);
4155 queue_init(&tagcache_queue
, true);
4156 create_thread(tagcache_thread
, tagcache_stack
,
4157 sizeof(tagcache_stack
), tagcache_thread_name
4158 IF_PRIO(, PRIORITY_BACKGROUND
)
4159 IF_COP(, CPU
, false));
4161 tc_stat
.initialized
= true;
4165 tc_stat
.ready
= check_all_headers();
4170 void tagcache_reverse_scan(void)
4172 logf("Checking for deleted files");
4173 check_deleted_files();
4177 bool tagcache_is_initialized(void)
4179 return tc_stat
.initialized
;
4181 bool tagcache_is_usable(void)
4183 return tc_stat
.initialized
&& tc_stat
.ready
;
4185 int tagcache_get_commit_step(void)
4187 return tc_stat
.commit_step
;
4189 int tagcache_get_max_commit_step(void)
4191 return (int)(sizeof(sorted_tags
)/sizeof(sorted_tags
[0]))+1;