1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
10 * Copyright (C) 2005 by Miika Pekkarinen
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
25 * ----------x---------x------------------x-----
27 * +---------------x-------+ | TagCache | Libraries
28 * | Modification routines | | Core |
29 * +-x---------x-----------+ | |
31 * | +------x-------------x-+ +-------------x-----+ |
32 * | | x==x Filters & clauses | |
33 * | | Search routines | +-------------------+ |
34 * | | x============================x DirCache
35 * | +-x--------------------+ | (optional)
37 * | | +-------------------------------+ +---------+ |
38 * | | | DB Commit (sort,unique,index) | | | |
39 * | | +-x--------------------------x--+ | Control | |
40 * | | | (R/W) | (R) | Thread | |
41 * | | | +----------------------+ | | | |
42 * | | | | TagCache DB Builder | | +---------+ |
43 * | | | +-x-------------x------+ | |
44 * | | | | (R) | (W) | |
45 * | | | | +--x--------x---------+ |
46 * | | | | | Temporary Commit DB | |
47 * | | | | +---------------------+ |
48 * +-x----x-------x--+ |
49 * | TagCache RAM DB x==\(W) +-----------------+ |
50 * +-----------------+ \===x | |
51 * | | | | (R) | Ram DB Loader x============x DirCache
52 * +-x----x---x---x---+ /==x | | (optional)
53 * | Tagcache Disk DB x==/ +-----------------+ |
54 * +------------------+ |
58 /* #define LOGF_ENABLE */
64 #include "ata_idle_notify.h"
84 #include "eeprom_settings.h"
88 #define yield() do { } while(0)
89 #define sim_sleep(timeout) do { } while(0)
90 #define do_timed_yield() do { } while(0)
94 /* Tag Cache thread. */
95 static struct event_queue tagcache_queue
;
96 static long tagcache_stack
[(DEFAULT_STACK_SIZE
+ 0x4000)/sizeof(long)];
97 static const char tagcache_thread_name
[] = "tagcache";
100 #define UNTAGGED "<Untagged>"
102 /* Previous path when scanning directory tree recursively. */
103 static char curpath
[TAG_MAXLEN
+32];
105 /* Used when removing duplicates. */
106 static char *tempbuf
; /* Allocated when needed. */
107 static long tempbufidx
; /* Current location in buffer. */
108 static long tempbuf_size
; /* Buffer size (TEMPBUF_SIZE). */
109 static long tempbuf_left
; /* Buffer space left. */
110 static long tempbuf_pos
;
112 /* Tags we want to get sorted (loaded to the tempbuf). */
113 static const int sorted_tags
[] = { tag_artist
, tag_album
, tag_genre
,
114 tag_composer
, tag_comment
, tag_albumartist
, tag_grouping
, tag_title
};
116 /* Uniqued tags (we can use these tags with filters and conditional clauses). */
117 static const int unique_tags
[] = { tag_artist
, tag_album
, tag_genre
,
118 tag_composer
, tag_comment
, tag_albumartist
, tag_grouping
};
120 /* Numeric tags (we can use these tags with conditional clauses). */
121 static const int numeric_tags
[] = { tag_year
, tag_discnumber
,
122 tag_tracknumber
, tag_length
, tag_bitrate
, tag_playcount
, tag_rating
,
123 tag_playtime
, tag_lastplayed
, tag_commitid
, tag_mtime
,
124 tag_virt_length_min
, tag_virt_length_sec
,
125 tag_virt_playtime_min
, tag_virt_playtime_sec
,
126 tag_virt_entryage
, tag_virt_autoscore
};
128 /* String presentation of the tags defined in tagcache.h. Must be in correct order! */
129 static const char *tags_str
[] = { "artist", "album", "genre", "title",
130 "filename", "composer", "comment", "albumartist", "grouping", "year",
131 "discnumber", "tracknumber", "bitrate", "length", "playcount", "rating",
132 "playtime", "lastplayed", "commitid", "mtime" };
134 /* Status information of the tagcache. */
135 static struct tagcache_stat tc_stat
;
137 /* Queue commands. */
138 enum tagcache_queue
{
145 /* Internal tagcache command queue. */
146 CMD_UPDATE_MASTER_HEADER
,
150 struct tagcache_command_entry
{
158 static struct tagcache_command_entry command_queue
[TAGCACHE_COMMAND_QUEUE_LENGTH
];
159 static volatile int command_queue_widx
= 0;
160 static volatile int command_queue_ridx
= 0;
161 static struct mutex command_queue_mutex
;
164 /* Tag database structures. */
166 /* Variable-length tag entry in tag files. */
167 struct tagfile_entry
{
168 int32_t tag_length
; /* Length of the data in bytes including '\0' */
169 int32_t idx_id
; /* Corresponding entry location in index file of not unique tags */
170 char tag_data
[0]; /* Begin of the tag data */
173 /* Fixed-size tag entry in master db index. */
175 int32_t tag_seek
[TAG_COUNT
]; /* Location of tag data or numeric tag data */
176 int32_t flag
; /* Status flags */
179 /* Header is the same in every file. */
180 struct tagcache_header
{
181 int32_t magic
; /* Header version number */
182 int32_t datasize
; /* Data size in bytes */
183 int32_t entry_count
; /* Number of entries in this file */
186 struct master_header
{
187 struct tagcache_header tch
;
188 int32_t serial
; /* Increasing counting number */
189 int32_t commitid
; /* Number of commits so far */
193 /* For the endianess correction */
194 static const char *tagfile_entry_ec
= "ll";
196 Note: This should be (1 + TAG_COUNT) amount of l's.
198 static const char *index_entry_ec
= "lllllllllllllllllllll";
200 static const char *tagcache_header_ec
= "lll";
201 static const char *master_header_ec
= "llllll";
203 static struct master_header current_tcmh
;
205 #ifdef HAVE_TC_RAMCACHE
206 /* Header is created when loading database to ram. */
207 struct ramcache_header
{
208 struct master_header h
; /* Header from the master index */
209 struct index_entry
*indices
; /* Master index file content */
210 char *tags
[TAG_COUNT
]; /* Tag file content (not including filename tag) */
211 int entry_count
[TAG_COUNT
]; /* Number of entries in the indices. */
214 # ifdef HAVE_EEPROM_SETTINGS
215 struct statefile_header
{
216 struct ramcache_header
*hdr
;
217 struct tagcache_stat tc_stat
;
221 /* Pointer to allocated ramcache_header */
222 static struct ramcache_header
*hdr
;
226 * Full tag entries stored in a temporary file waiting
227 * for commit to the cache. */
228 struct temp_file_entry
{
229 long tag_offset
[TAG_COUNT
];
230 short tag_length
[TAG_COUNT
];
236 struct tempbuf_id_list
{
238 struct tempbuf_id_list
*next
;
241 struct tempbuf_searchidx
{
245 struct tempbuf_id_list idlist
;
248 /* Lookup buffer for fixing messed up index while after sorting. */
249 static long commit_entry_count
;
250 static long lookup_buffer_depth
;
251 static struct tempbuf_searchidx
**lookup
;
253 /* Used when building the temporary file. */
254 static int cachefd
= -1, filenametag_fd
;
255 static int total_entry_count
= 0;
256 static int data_size
= 0;
257 static int processed_dir_count
;
259 /* Thread safe locking */
260 static volatile int write_lock
;
261 static volatile int read_lock
;
263 static bool delete_entry(long idx_id
);
265 const char* tagcache_tag_to_str(int tag
)
267 return tags_str
[tag
];
270 bool tagcache_is_numeric_tag(int type
)
274 for (i
= 0; i
< (int)(sizeof(numeric_tags
)/sizeof(numeric_tags
[0])); i
++)
276 if (type
== numeric_tags
[i
])
283 bool tagcache_is_unique_tag(int type
)
287 for (i
= 0; i
< (int)(sizeof(unique_tags
)/sizeof(unique_tags
[0])); i
++)
289 if (type
== unique_tags
[i
])
296 bool tagcache_is_sorted_tag(int type
)
300 for (i
= 0; i
< (int)(sizeof(sorted_tags
)/sizeof(sorted_tags
[0])); i
++)
302 if (type
== sorted_tags
[i
])
311 * Returns true if specified flag is still present, i.e., dircache
312 * has not been reloaded.
314 static bool is_dircache_intact(void)
316 return dircache_get_appflag(DIRCACHE_APPFLAG_TAGCACHE
);
320 static int open_tag_fd(struct tagcache_header
*hdr
, int tag
, bool write
)
326 if (tagcache_is_numeric_tag(tag
) || tag
< 0 || tag
>= TAG_COUNT
)
329 snprintf(buf
, sizeof buf
, TAGCACHE_FILE_INDEX
, tag
);
331 fd
= open(buf
, write
? O_RDWR
: O_RDONLY
);
334 logf("tag file open failed: tag=%d write=%d file=%s", tag
, write
, buf
);
335 tc_stat
.ready
= false;
339 /* Check the header. */
340 rc
= ecread(fd
, hdr
, 1, tagcache_header_ec
, tc_stat
.econ
);
341 if (hdr
->magic
!= TAGCACHE_MAGIC
|| rc
!= sizeof(struct tagcache_header
))
343 logf("header error");
344 tc_stat
.ready
= false;
352 static int open_master_fd(struct master_header
*hdr
, bool write
)
357 fd
= open(TAGCACHE_FILE_MASTER
, write
? O_RDWR
: O_RDONLY
);
360 logf("master file open failed for R/W");
361 tc_stat
.ready
= false;
365 tc_stat
.econ
= false;
367 /* Check the header. */
368 rc
= read(fd
, hdr
, sizeof(struct master_header
));
369 if (hdr
->tch
.magic
== TAGCACHE_MAGIC
&& rc
== sizeof(struct master_header
))
375 /* Trying to read again, this time with endianess correction enabled. */
376 lseek(fd
, 0, SEEK_SET
);
378 rc
= ecread(fd
, hdr
, 1, master_header_ec
, true);
379 if (hdr
->tch
.magic
!= TAGCACHE_MAGIC
|| rc
!= sizeof(struct master_header
))
381 logf("header error");
382 tc_stat
.ready
= false;
393 static bool do_timed_yield(void)
395 /* Sorting can lock up for quite a while, so yield occasionally */
396 static long wakeup_tick
= 0;
397 if (current_tick
>= wakeup_tick
)
399 wakeup_tick
= current_tick
+ (HZ
/4);
407 #if defined(HAVE_TC_RAMCACHE) && defined(HAVE_DIRCACHE)
408 static long find_entry_ram(const char *filename
,
409 const struct dirent
*dc
)
411 static long last_pos
= 0;
414 /* Check if we tagcache is loaded into ram. */
415 if (!tc_stat
.ramcache
)
419 dc
= dircache_get_entry_ptr(filename
);
423 logf("tagcache: file not found.");
434 for (; i
< hdr
->h
.tch
.entry_count
; i
++)
436 if (hdr
->indices
[i
].tag_seek
[tag_filename
] == (long)dc
)
438 last_pos
= MAX(0, i
- 3);
455 static long find_entry_disk(const char *filename
)
457 struct tagcache_header tch
;
458 static long last_pos
= -1;
459 long pos_history
[POS_HISTORY_COUNT
];
460 long pos_history_idx
= 0;
462 struct tagfile_entry tfe
;
464 char buf
[TAG_MAXLEN
+32];
475 if ( (fd
= open_tag_fd(&tch
, tag_filename
, false)) < 0)
482 lseek(fd
, last_pos
, SEEK_SET
);
484 lseek(fd
, sizeof(struct tagcache_header
), SEEK_SET
);
488 pos
= lseek(fd
, 0, SEEK_CUR
);
489 for (i
= pos_history_idx
-1; i
>= 0; i
--)
490 pos_history
[i
+1] = pos_history
[i
];
491 pos_history
[0] = pos
;
493 if (ecread(fd
, &tfe
, 1, tagfile_entry_ec
, tc_stat
.econ
)
494 != sizeof(struct tagfile_entry
))
499 if (tfe
.tag_length
>= (long)sizeof(buf
))
501 logf("too long tag #1");
507 if (read(fd
, buf
, tfe
.tag_length
) != tfe
.tag_length
)
509 logf("read error #2");
515 if (!strcasecmp(filename
, buf
))
517 last_pos
= pos_history
[pos_history_idx
];
522 if (pos_history_idx
< POS_HISTORY_COUNT
- 1)
536 if (fd
!= filenametag_fd
)
541 if (fd
!= filenametag_fd
)
547 static int find_index(const char *filename
)
551 #if defined(HAVE_TC_RAMCACHE) && defined(HAVE_DIRCACHE)
552 if (tc_stat
.ramcache
&& is_dircache_intact())
553 idx_id
= find_entry_ram(filename
, NULL
);
557 idx_id
= find_entry_disk(filename
);
562 bool tagcache_find_index(struct tagcache_search
*tcs
, const char *filename
)
569 idx_id
= find_index(filename
);
573 if (!tagcache_search(tcs
, tag_filename
))
576 tcs
->entry_count
= 0;
577 tcs
->idx_id
= idx_id
;
582 static bool get_index(int masterfd
, int idxid
,
583 struct index_entry
*idx
, bool use_ram
)
585 bool localfd
= false;
589 logf("Incorrect idxid: %d", idxid
);
593 #ifdef HAVE_TC_RAMCACHE
594 if (tc_stat
.ramcache
&& use_ram
)
596 if (hdr
->indices
[idxid
].flag
& FLAG_DELETED
)
599 memcpy(idx
, &hdr
->indices
[idxid
], sizeof(struct index_entry
));
608 struct master_header tcmh
;
611 masterfd
= open_master_fd(&tcmh
, false);
616 lseek(masterfd
, idxid
* sizeof(struct index_entry
)
617 + sizeof(struct master_header
), SEEK_SET
);
618 if (ecread(masterfd
, idx
, 1, index_entry_ec
, tc_stat
.econ
)
619 != sizeof(struct index_entry
))
621 logf("read error #3");
631 if (idx
->flag
& FLAG_DELETED
)
639 static bool write_index(int masterfd
, int idxid
, struct index_entry
*idx
)
641 /* We need to exclude all memory only flags & tags when writing to disk. */
642 if (idx
->flag
& FLAG_DIRCACHE
)
644 logf("memory only flags!");
648 #ifdef HAVE_TC_RAMCACHE
649 /* Only update numeric data. Writing the whole index to RAM by memcpy
650 * destroys dircache pointers!
652 if (tc_stat
.ramcache
)
655 struct index_entry
*idx_ram
= &hdr
->indices
[idxid
];
657 for (tag
= 0; tag
< TAG_COUNT
; tag
++)
659 if (tagcache_is_numeric_tag(tag
))
661 idx_ram
->tag_seek
[tag
] = idx
->tag_seek
[tag
];
665 /* Don't touch the dircache flag or attributes. */
666 idx_ram
->flag
= (idx
->flag
& 0x0000ffff)
667 | (idx_ram
->flag
& (0xffff0000 | FLAG_DIRCACHE
));
671 lseek(masterfd
, idxid
* sizeof(struct index_entry
)
672 + sizeof(struct master_header
), SEEK_SET
);
673 if (ecwrite(masterfd
, idx
, 1, index_entry_ec
, tc_stat
.econ
)
674 != sizeof(struct index_entry
))
676 logf("write error #3");
677 logf("idxid: %d", idxid
);
684 #endif /* !__PCTOOL__ */
686 static bool open_files(struct tagcache_search
*tcs
, int tag
)
688 if (tcs
->idxfd
[tag
] < 0)
692 snprintf(fn
, sizeof fn
, TAGCACHE_FILE_INDEX
, tag
);
693 tcs
->idxfd
[tag
] = open(fn
, O_RDONLY
);
696 if (tcs
->idxfd
[tag
] < 0)
698 logf("File not open!");
705 static bool retrieve(struct tagcache_search
*tcs
, struct index_entry
*idx
,
706 int tag
, char *buf
, long size
)
708 struct tagfile_entry tfe
;
713 if (tagcache_is_numeric_tag(tag
))
716 seek
= idx
->tag_seek
[tag
];
719 logf("Retrieve failed");
723 #ifdef HAVE_TC_RAMCACHE
726 struct tagfile_entry
*ep
;
728 # ifdef HAVE_DIRCACHE
729 if (tag
== tag_filename
&& (idx
->flag
& FLAG_DIRCACHE
)
730 && is_dircache_intact())
732 dircache_copy_path((struct dirent
*)seek
,
738 if (tag
!= tag_filename
)
740 ep
= (struct tagfile_entry
*)&hdr
->tags
[tag
][seek
];
741 strncpy(buf
, ep
->tag_data
, size
-1);
748 if (!open_files(tcs
, tag
))
751 lseek(tcs
->idxfd
[tag
], seek
, SEEK_SET
);
752 if (ecread(tcs
->idxfd
[tag
], &tfe
, 1, tagfile_entry_ec
, tc_stat
.econ
)
753 != sizeof(struct tagfile_entry
))
755 logf("read error #5");
759 if (tfe
.tag_length
>= size
)
761 logf("too small buffer");
765 if (read(tcs
->idxfd
[tag
], buf
, tfe
.tag_length
) !=
768 logf("read error #6");
772 buf
[tfe
.tag_length
] = '\0';
777 static long check_virtual_tags(int tag
, const struct index_entry
*idx
)
783 case tag_virt_length_sec
:
784 data
= (idx
->tag_seek
[tag_length
]/1000) % 60;
787 case tag_virt_length_min
:
788 data
= (idx
->tag_seek
[tag_length
]/1000) / 60;
791 case tag_virt_playtime_sec
:
792 data
= (idx
->tag_seek
[tag_playtime
]/1000) % 60;
795 case tag_virt_playtime_min
:
796 data
= (idx
->tag_seek
[tag_playtime
]/1000) / 60;
799 case tag_virt_autoscore
:
800 if (idx
->tag_seek
[tag_length
] == 0
801 || idx
->tag_seek
[tag_playcount
] == 0)
807 data
= 100 * idx
->tag_seek
[tag_playtime
]
808 / idx
->tag_seek
[tag_length
]
809 / idx
->tag_seek
[tag_playcount
];
813 /* How many commits before the file has been added to the DB. */
814 case tag_virt_entryage
:
815 data
= current_tcmh
.commitid
- idx
->tag_seek
[tag_commitid
] - 1;
819 data
= idx
->tag_seek
[tag
];
825 long tagcache_get_numeric(const struct tagcache_search
*tcs
, int tag
)
827 struct index_entry idx
;
832 if (!tagcache_is_numeric_tag(tag
))
835 if (!get_index(tcs
->masterfd
, tcs
->idx_id
, &idx
, true))
838 return check_virtual_tags(tag
, &idx
);
841 inline static bool str_ends_with(const char *str1
, const char *str2
)
843 int str_len
= strlen(str1
);
844 int clause_len
= strlen(str2
);
846 if (clause_len
> str_len
)
849 return !strcasecmp(&str1
[str_len
- clause_len
], str2
);
852 inline static bool str_oneof(const char *str
, const char *list
)
855 int l
, len
= strlen(str
);
859 sep
= strchr(list
, '|');
860 l
= sep
? (long)sep
- (long)list
: (int)strlen(list
);
861 if ((l
==len
) && !strncasecmp(str
, list
, len
))
863 list
+= sep
? l
+ 1 : l
;
869 static bool check_against_clause(long numeric
, const char *str
,
870 const struct tagcache_search_clause
*clause
)
874 switch (clause
->type
)
877 return numeric
== clause
->numeric_data
;
879 return numeric
!= clause
->numeric_data
;
881 return numeric
> clause
->numeric_data
;
883 return numeric
>= clause
->numeric_data
;
885 return numeric
< clause
->numeric_data
;
887 return numeric
<= clause
->numeric_data
;
889 logf("Incorrect numeric tag: %d", clause
->type
);
894 switch (clause
->type
)
897 return !strcasecmp(clause
->str
, str
);
899 return strcasecmp(clause
->str
, str
);
901 return 0>strcasecmp(clause
->str
, str
);
903 return 0>=strcasecmp(clause
->str
, str
);
905 return 0<strcasecmp(clause
->str
, str
);
907 return 0<=strcasecmp(clause
->str
, str
);
908 case clause_contains
:
909 return (strcasestr(str
, clause
->str
) != NULL
);
910 case clause_not_contains
:
911 return (strcasestr(str
, clause
->str
) == NULL
);
912 case clause_begins_with
:
913 return (strcasestr(str
, clause
->str
) == str
);
914 case clause_not_begins_with
:
915 return (strcasestr(str
, clause
->str
) != str
);
916 case clause_ends_with
:
917 return str_ends_with(str
, clause
->str
);
918 case clause_not_ends_with
:
919 return !str_ends_with(str
, clause
->str
);
921 return str_oneof(str
, clause
->str
);
924 logf("Incorrect tag: %d", clause
->type
);
931 static bool check_clauses(struct tagcache_search
*tcs
,
932 struct index_entry
*idx
,
933 struct tagcache_search_clause
**clause
, int count
)
937 #ifdef HAVE_TC_RAMCACHE
940 /* Go through all conditional clauses. */
941 for (i
= 0; i
< count
; i
++)
943 struct tagfile_entry
*tfe
;
948 seek
= check_virtual_tags(clause
[i
]->tag
, idx
);
950 if (!tagcache_is_numeric_tag(clause
[i
]->tag
))
952 if (clause
[i
]->tag
== tag_filename
)
954 retrieve(tcs
, idx
, tag_filename
, buf
, sizeof buf
);
959 tfe
= (struct tagfile_entry
*)&hdr
->tags
[clause
[i
]->tag
][seek
];
964 if (!check_against_clause(seek
, str
, clause
[i
]))
971 /* Check for conditions. */
972 for (i
= 0; i
< count
; i
++)
974 struct tagfile_entry tfe
;
978 seek
= check_virtual_tags(clause
[i
]->tag
, idx
);
980 memset(str
, 0, sizeof str
);
981 if (!tagcache_is_numeric_tag(clause
[i
]->tag
))
983 int fd
= tcs
->idxfd
[clause
[i
]->tag
];
984 lseek(fd
, seek
, SEEK_SET
);
985 ecread(fd
, &tfe
, 1, tagfile_entry_ec
, tc_stat
.econ
);
986 if (tfe
.tag_length
>= (int)sizeof(str
))
988 logf("Too long tag read!");
992 read(fd
, str
, tfe
.tag_length
);
994 /* Check if entry has been deleted. */
999 if (!check_against_clause(seek
, str
, clause
[i
]))
1007 bool tagcache_check_clauses(struct tagcache_search
*tcs
,
1008 struct tagcache_search_clause
**clause
, int count
)
1010 struct index_entry idx
;
1015 if (!get_index(tcs
->masterfd
, tcs
->idx_id
, &idx
, true))
1018 return check_clauses(tcs
, &idx
, clause
, count
);
1021 static bool add_uniqbuf(struct tagcache_search
*tcs
, unsigned long id
)
1025 /* If uniq buffer is not defined we must return true for search to work. */
1026 if (tcs
->unique_list
== NULL
1027 || (!tagcache_is_unique_tag(tcs
->type
)
1028 && !tagcache_is_numeric_tag(tcs
->type
)))
1033 for (i
= 0; i
< tcs
->unique_list_count
; i
++)
1035 /* Return false if entry is found. */
1036 if (tcs
->unique_list
[i
] == id
)
1040 if (tcs
->unique_list_count
< tcs
->unique_list_capacity
)
1042 tcs
->unique_list
[i
] = id
;
1043 tcs
->unique_list_count
++;
1049 static bool build_lookup_list(struct tagcache_search
*tcs
)
1051 struct index_entry entry
;
1054 tcs
->seek_list_count
= 0;
1056 #ifdef HAVE_TC_RAMCACHE
1061 for (i
= tcs
->seek_pos
; i
< hdr
->h
.tch
.entry_count
; i
++)
1063 struct index_entry
*idx
= &hdr
->indices
[i
];
1064 if (tcs
->seek_list_count
== SEEK_LIST_SIZE
)
1067 /* Skip deleted files. */
1068 if (idx
->flag
& FLAG_DELETED
)
1071 /* Go through all filters.. */
1072 for (j
= 0; j
< tcs
->filter_count
; j
++)
1074 if (idx
->tag_seek
[tcs
->filter_tag
[j
]] != tcs
->filter_seek
[j
])
1080 if (j
< tcs
->filter_count
)
1083 /* Check for conditions. */
1084 if (!check_clauses(tcs
, idx
, tcs
->clause
, tcs
->clause_count
))
1087 /* Add to the seek list if not already in uniq buffer. */
1088 if (!add_uniqbuf(tcs
, idx
->tag_seek
[tcs
->type
]))
1092 tcs
->seek_list
[tcs
->seek_list_count
] = idx
->tag_seek
[tcs
->type
];
1093 tcs
->seek_flags
[tcs
->seek_list_count
] = idx
->flag
;
1094 tcs
->seek_list_count
++;
1099 return tcs
->seek_list_count
> 0;
1103 lseek(tcs
->masterfd
, tcs
->seek_pos
* sizeof(struct index_entry
) +
1104 sizeof(struct master_header
), SEEK_SET
);
1106 while (ecread(tcs
->masterfd
, &entry
, 1, index_entry_ec
, tc_stat
.econ
)
1107 == sizeof(struct index_entry
))
1109 if (tcs
->seek_list_count
== SEEK_LIST_SIZE
)
1114 /* Check if entry has been deleted. */
1115 if (entry
.flag
& FLAG_DELETED
)
1118 /* Go through all filters.. */
1119 for (i
= 0; i
< tcs
->filter_count
; i
++)
1121 if (entry
.tag_seek
[tcs
->filter_tag
[i
]] != tcs
->filter_seek
[i
])
1125 if (i
< tcs
->filter_count
)
1128 /* Check for conditions. */
1129 if (!check_clauses(tcs
, &entry
, tcs
->clause
, tcs
->clause_count
))
1132 /* Add to the seek list if not already in uniq buffer. */
1133 if (!add_uniqbuf(tcs
, entry
.tag_seek
[tcs
->type
]))
1137 tcs
->seek_list
[tcs
->seek_list_count
] = entry
.tag_seek
[tcs
->type
];
1138 tcs
->seek_flags
[tcs
->seek_list_count
] = entry
.flag
;
1139 tcs
->seek_list_count
++;
1144 return tcs
->seek_list_count
> 0;
1148 static void remove_files(void)
1153 tc_stat
.ready
= false;
1154 tc_stat
.ramcache
= false;
1155 tc_stat
.econ
= false;
1156 remove(TAGCACHE_FILE_MASTER
);
1157 for (i
= 0; i
< TAG_COUNT
; i
++)
1159 if (tagcache_is_numeric_tag(i
))
1162 snprintf(buf
, sizeof buf
, TAGCACHE_FILE_INDEX
, i
);
1168 static bool check_all_headers(void)
1170 struct master_header myhdr
;
1171 struct tagcache_header tch
;
1175 if ( (fd
= open_master_fd(&myhdr
, false)) < 0)
1181 logf("tagcache is dirty!");
1185 memcpy(¤t_tcmh
, &myhdr
, sizeof(struct master_header
));
1187 for (tag
= 0; tag
< TAG_COUNT
; tag
++)
1189 if (tagcache_is_numeric_tag(tag
))
1192 if ( (fd
= open_tag_fd(&tch
, tag
, false)) < 0)
1201 bool tagcache_search(struct tagcache_search
*tcs
, int tag
)
1203 struct tagcache_header tag_hdr
;
1204 struct master_header master_hdr
;
1207 if (tcs
->initialized
)
1208 tagcache_search_finish(tcs
);
1213 memset(tcs
, 0, sizeof(struct tagcache_search
));
1214 if (tc_stat
.commit_step
> 0 || !tc_stat
.ready
)
1217 tcs
->position
= sizeof(struct tagcache_header
);
1220 tcs
->seek_list_count
= 0;
1221 tcs
->filter_count
= 0;
1224 for (i
= 0; i
< TAG_COUNT
; i
++)
1227 #ifndef HAVE_TC_RAMCACHE
1228 tcs
->ramsearch
= false;
1230 tcs
->ramsearch
= tc_stat
.ramcache
;
1233 tcs
->entry_count
= hdr
->entry_count
[tcs
->type
];
1238 if (!tagcache_is_numeric_tag(tcs
->type
))
1240 tcs
->idxfd
[tcs
->type
] = open_tag_fd(&tag_hdr
, tcs
->type
, false);
1241 if (tcs
->idxfd
[tcs
->type
] < 0)
1245 /* Always open as R/W so we can pass tcs to functions that modify data also
1246 * without failing. */
1247 tcs
->masterfd
= open_master_fd(&master_hdr
, true);
1249 if (tcs
->masterfd
< 0)
1254 tcs
->initialized
= true;
1260 void tagcache_search_set_uniqbuf(struct tagcache_search
*tcs
,
1261 void *buffer
, long length
)
1263 tcs
->unique_list
= (unsigned long *)buffer
;
1264 tcs
->unique_list_capacity
= length
/ sizeof(*tcs
->unique_list
);
1265 tcs
->unique_list_count
= 0;
1268 bool tagcache_search_add_filter(struct tagcache_search
*tcs
,
1271 if (tcs
->filter_count
== TAGCACHE_MAX_FILTERS
)
1274 if (!tagcache_is_unique_tag(tag
) || tagcache_is_numeric_tag(tag
))
1277 tcs
->filter_tag
[tcs
->filter_count
] = tag
;
1278 tcs
->filter_seek
[tcs
->filter_count
] = seek
;
1279 tcs
->filter_count
++;
1284 bool tagcache_search_add_clause(struct tagcache_search
*tcs
,
1285 struct tagcache_search_clause
*clause
)
1289 if (tcs
->clause_count
>= TAGCACHE_MAX_CLAUSES
)
1291 logf("Too many clauses");
1295 /* Check if there is already a similar filter in present (filters are
1296 * much faster than clauses).
1298 for (i
= 0; i
< tcs
->filter_count
; i
++)
1300 if (tcs
->filter_tag
[i
] == clause
->tag
)
1304 if (!tagcache_is_numeric_tag(clause
->tag
) && tcs
->idxfd
[clause
->tag
] < 0)
1308 snprintf(buf
, sizeof buf
, TAGCACHE_FILE_INDEX
, clause
->tag
);
1309 tcs
->idxfd
[clause
->tag
] = open(buf
, O_RDONLY
);
1312 tcs
->clause
[tcs
->clause_count
] = clause
;
1313 tcs
->clause_count
++;
1318 /* TODO: Remove this mess. */
1319 #ifdef HAVE_DIRCACHE
1320 #define TAG_FILENAME_RAM(tcs) ((tcs->type == tag_filename) \
1321 ? ((flag & FLAG_DIRCACHE) && is_dircache_intact()) : 1)
1323 #define TAG_FILENAME_RAM(tcs) (tcs->type != tag_filename)
1326 static bool get_next(struct tagcache_search
*tcs
)
1328 static char buf
[TAG_MAXLEN
+32];
1329 struct tagfile_entry entry
;
1332 if (!tcs
->valid
|| !tc_stat
.ready
)
1335 if (tcs
->idxfd
[tcs
->type
] < 0 && !tagcache_is_numeric_tag(tcs
->type
)
1336 #ifdef HAVE_TC_RAMCACHE
1342 /* Relative fetch. */
1343 if (tcs
->filter_count
> 0 || tcs
->clause_count
> 0
1344 || tagcache_is_numeric_tag(tcs
->type
))
1346 /* Check for end of list. */
1347 if (tcs
->seek_list_count
== 0)
1349 /* Try to fetch more. */
1350 if (!build_lookup_list(tcs
))
1357 tcs
->seek_list_count
--;
1358 flag
= tcs
->seek_flags
[tcs
->seek_list_count
];
1360 /* Seek stream to the correct position and continue to direct fetch. */
1361 if ((!tcs
->ramsearch
|| !TAG_FILENAME_RAM(tcs
))
1362 && !tagcache_is_numeric_tag(tcs
->type
))
1364 if (!open_files(tcs
, tcs
->type
))
1367 lseek(tcs
->idxfd
[tcs
->type
], tcs
->seek_list
[tcs
->seek_list_count
], SEEK_SET
);
1370 tcs
->position
= tcs
->seek_list
[tcs
->seek_list_count
];
1373 if (tagcache_is_numeric_tag(tcs
->type
))
1375 snprintf(buf
, sizeof(buf
), "%d", tcs
->position
);
1376 tcs
->result_seek
= tcs
->position
;
1378 tcs
->result_len
= strlen(buf
) + 1;
1383 #ifdef HAVE_TC_RAMCACHE
1384 if (tcs
->ramsearch
&& TAG_FILENAME_RAM(tcs
))
1386 struct tagfile_entry
*ep
;
1388 if (tcs
->entry_count
== 0)
1395 tcs
->result_seek
= tcs
->position
;
1397 # ifdef HAVE_DIRCACHE
1398 if (tcs
->type
== tag_filename
)
1400 dircache_copy_path((struct dirent
*)tcs
->position
,
1403 tcs
->result_len
= strlen(buf
) + 1;
1404 tcs
->idx_id
= FLAG_GET_ATTR(flag
);
1405 tcs
->ramresult
= false;
1411 ep
= (struct tagfile_entry
*)&hdr
->tags
[tcs
->type
][tcs
->position
];
1412 tcs
->position
+= sizeof(struct tagfile_entry
) + ep
->tag_length
;
1413 tcs
->result
= ep
->tag_data
;
1414 tcs
->result_len
= strlen(tcs
->result
) + 1;
1415 tcs
->idx_id
= ep
->idx_id
;
1416 tcs
->ramresult
= true;
1423 if (!open_files(tcs
, tcs
->type
))
1426 tcs
->result_seek
= lseek(tcs
->idxfd
[tcs
->type
], 0, SEEK_CUR
);
1427 if (ecread(tcs
->idxfd
[tcs
->type
], &entry
, 1,
1428 tagfile_entry_ec
, tc_stat
.econ
) != sizeof(struct tagfile_entry
))
1436 if (entry
.tag_length
> (long)sizeof(buf
))
1439 logf("too long tag #2");
1443 if (read(tcs
->idxfd
[tcs
->type
], buf
, entry
.tag_length
) != entry
.tag_length
)
1446 logf("read error #4");
1451 tcs
->result_len
= strlen(tcs
->result
) + 1;
1452 tcs
->idx_id
= entry
.idx_id
;
1453 tcs
->ramresult
= false;
1458 bool tagcache_get_next(struct tagcache_search
*tcs
)
1460 while (get_next(tcs
))
1462 if (tcs
->result_len
> 1)
1469 bool tagcache_retrieve(struct tagcache_search
*tcs
, int idxid
,
1470 int tag
, char *buf
, long size
)
1472 struct index_entry idx
;
1475 if (!get_index(tcs
->masterfd
, idxid
, &idx
, true))
1478 return retrieve(tcs
, &idx
, tag
, buf
, size
);
1481 static bool update_master_header(void)
1483 struct master_header myhdr
;
1489 if ( (fd
= open_master_fd(&myhdr
, true)) < 0)
1492 myhdr
.serial
= current_tcmh
.serial
;
1493 myhdr
.commitid
= current_tcmh
.commitid
;
1494 myhdr
.dirty
= current_tcmh
.dirty
;
1497 lseek(fd
, 0, SEEK_SET
);
1498 ecwrite(fd
, &myhdr
, 1, master_header_ec
, tc_stat
.econ
);
1501 #ifdef HAVE_TC_RAMCACHE
1504 hdr
->h
.serial
= current_tcmh
.serial
;
1505 hdr
->h
.commitid
= current_tcmh
.commitid
;
1506 hdr
->h
.dirty
= current_tcmh
.dirty
;
1515 void tagcache_modify(struct tagcache_search
*tcs
, int type
, const char *text
)
1517 struct tagentry
*entry
;
1519 if (tcs
->type
!= tag_title
)
1522 /* We will need reserve buffer for this. */
1525 struct tagfile_entry
*ep
;
1527 ep
= (struct tagfile_entry
*)&hdr
->tags
[tcs
->type
][tcs
->result_seek
];
1528 tcs
->seek_list
[tcs
->seek_list_count
];
1531 entry
= find_entry_ram();
1536 void tagcache_search_finish(struct tagcache_search
*tcs
)
1540 if (!tcs
->initialized
)
1543 if (tcs
->masterfd
>= 0)
1545 close(tcs
->masterfd
);
1549 for (i
= 0; i
< TAG_COUNT
; i
++)
1551 if (tcs
->idxfd
[i
] >= 0)
1553 close(tcs
->idxfd
[i
]);
1558 tcs
->ramsearch
= false;
1560 tcs
->initialized
= 0;
1565 #if defined(HAVE_TC_RAMCACHE) && defined(HAVE_DIRCACHE)
1566 static struct tagfile_entry
*get_tag(const struct index_entry
*entry
, int tag
)
1568 return (struct tagfile_entry
*)&hdr
->tags
[tag
][entry
->tag_seek
[tag
]];
1571 static long get_tag_numeric(const struct index_entry
*entry
, int tag
)
1573 return check_virtual_tags(tag
, entry
);
1576 static char* get_tag_string(const struct index_entry
*entry
, int tag
)
1578 char* s
= get_tag(entry
, tag
)->tag_data
;
1579 return strcmp(s
, UNTAGGED
) ? s
: NULL
;
1582 bool tagcache_fill_tags(struct mp3entry
*id3
, const char *filename
)
1584 struct index_entry
*entry
;
1590 /* Find the corresponding entry in tagcache. */
1591 idx_id
= find_entry_ram(filename
, NULL
);
1592 if (idx_id
< 0 || !tc_stat
.ramcache
)
1595 entry
= &hdr
->indices
[idx_id
];
1597 id3
->title
= get_tag_string(entry
, tag_title
);
1598 id3
->artist
= get_tag_string(entry
, tag_artist
);
1599 id3
->album
= get_tag_string(entry
, tag_album
);
1600 id3
->genre_string
= get_tag_string(entry
, tag_genre
);
1601 id3
->composer
= get_tag_string(entry
, tag_composer
);
1602 id3
->comment
= get_tag_string(entry
, tag_comment
);
1603 id3
->albumartist
= get_tag_string(entry
, tag_albumartist
);
1604 id3
->grouping
= get_tag_string(entry
, tag_grouping
);
1606 id3
->playcount
= get_tag_numeric(entry
, tag_playcount
);
1607 id3
->rating
= get_tag_numeric(entry
, tag_rating
);
1608 id3
->lastplayed
= get_tag_numeric(entry
, tag_lastplayed
);
1609 id3
->score
= get_tag_numeric(entry
, tag_virt_autoscore
) / 10;
1610 id3
->year
= get_tag_numeric(entry
, tag_year
);
1612 id3
->discnum
= get_tag_numeric(entry
, tag_discnumber
);
1613 id3
->tracknum
= get_tag_numeric(entry
, tag_tracknumber
);
1614 id3
->bitrate
= get_tag_numeric(entry
, tag_bitrate
);
1615 if (id3
->bitrate
== 0)
1622 static inline void write_item(const char *item
)
1624 int len
= strlen(item
) + 1;
1627 write(cachefd
, item
, len
);
1630 static int check_if_empty(char **tag
)
1634 if (*tag
== NULL
|| **tag
== '\0')
1637 return sizeof(UNTAGGED
); /* Tag length */
1640 length
= strlen(*tag
);
1641 if (length
> TAG_MAXLEN
)
1643 logf("over length tag: %s", *tag
);
1644 length
= TAG_MAXLEN
;
1645 (*tag
)[length
] = '\0';
1651 #define ADD_TAG(entry,tag,data) \
1653 entry.tag_offset[tag] = offset; \
1654 entry.tag_length[tag] = check_if_empty(data); \
1655 offset += entry.tag_length[tag]
1657 static void add_tagcache(char *path
, unsigned long mtime
1658 #if defined(HAVE_TC_RAMCACHE) && defined(HAVE_DIRCACHE)
1659 ,const struct dirent
*dc
1663 struct mp3entry id3
;
1664 struct temp_file_entry entry
;
1668 char tracknumfix
[3];
1670 int path_length
= strlen(path
);
1671 bool has_albumartist
;
1677 /* Check for overlength file path. */
1678 if (path_length
> TAG_MAXLEN
)
1680 /* Path can't be shortened. */
1681 logf("Too long path: %s", path
);
1685 /* Check if the file is supported. */
1686 if (probe_file_format(path
) == AFMT_UNKNOWN
)
1689 /* Check if the file is already cached. */
1690 #if defined(HAVE_TC_RAMCACHE) && defined(HAVE_DIRCACHE)
1691 if (tc_stat
.ramcache
&& is_dircache_intact())
1693 idx_id
= find_entry_ram(path
, dc
);
1698 if (filenametag_fd
>= 0)
1700 idx_id
= find_entry_disk(path
);
1704 /* Check if file has been modified. */
1707 struct index_entry idx
;
1709 /* TODO: Mark that the index exists (for fast reverse scan) */
1710 //found_idx[idx_id/8] |= idx_id%8;
1712 if (!get_index(-1, idx_id
, &idx
, true))
1714 logf("failed to retrieve index entry");
1718 if ((unsigned long)idx
.tag_seek
[tag_mtime
] == mtime
)
1720 /* No changes to file. */
1724 /* Metadata might have been changed. Delete the entry. */
1725 logf("Re-adding: %s", path
);
1726 if (!delete_entry(idx_id
))
1728 logf("delete_entry failed: %d", idx_id
);
1733 fd
= open(path
, O_RDONLY
);
1736 logf("open fail: %s", path
);
1740 memset(&id3
, 0, sizeof(struct mp3entry
));
1741 memset(&entry
, 0, sizeof(struct temp_file_entry
));
1742 memset(&tracknumfix
, 0, sizeof(tracknumfix
));
1743 ret
= get_metadata(&id3
, fd
, path
);
1749 logf("-> %s", path
);
1751 /* Generate track number if missing. */
1752 if (id3
.tracknum
<= 0)
1754 const char *p
= strrchr(path
, '.');
1757 p
= &path
[strlen(path
)-1];
1761 if (isdigit(*p
) && isdigit(*(p
-1)))
1763 tracknumfix
[1] = *p
--;
1764 tracknumfix
[0] = *p
;
1770 if (tracknumfix
[0] != '\0')
1772 id3
.tracknum
= atoi(tracknumfix
);
1773 /* Set a flag to indicate track number has been generated. */
1774 entry
.flag
|= FLAG_TRKNUMGEN
;
1778 /* Unable to generate track number. */
1784 entry
.tag_offset
[tag_year
] = id3
.year
;
1785 entry
.tag_offset
[tag_discnumber
] = id3
.discnum
;
1786 entry
.tag_offset
[tag_tracknumber
] = id3
.tracknum
;
1787 entry
.tag_offset
[tag_length
] = id3
.length
;
1788 entry
.tag_offset
[tag_bitrate
] = id3
.bitrate
;
1789 entry
.tag_offset
[tag_mtime
] = mtime
;
1792 has_albumartist
= id3
.albumartist
!= NULL
1793 && strlen(id3
.albumartist
) > 0;
1794 has_grouping
= id3
.grouping
!= NULL
1795 && strlen(id3
.grouping
) > 0;
1797 ADD_TAG(entry
, tag_filename
, &path
);
1798 ADD_TAG(entry
, tag_title
, &id3
.title
);
1799 ADD_TAG(entry
, tag_artist
, &id3
.artist
);
1800 ADD_TAG(entry
, tag_album
, &id3
.album
);
1801 ADD_TAG(entry
, tag_genre
, &id3
.genre_string
);
1802 ADD_TAG(entry
, tag_composer
, &id3
.composer
);
1803 ADD_TAG(entry
, tag_comment
, &id3
.comment
);
1804 if (has_albumartist
)
1806 ADD_TAG(entry
, tag_albumartist
, &id3
.albumartist
);
1810 ADD_TAG(entry
, tag_albumartist
, &id3
.artist
);
1814 ADD_TAG(entry
, tag_grouping
, &id3
.grouping
);
1818 ADD_TAG(entry
, tag_grouping
, &id3
.title
);
1820 entry
.data_length
= offset
;
1822 /* Write the header */
1823 write(cachefd
, &entry
, sizeof(struct temp_file_entry
));
1825 /* And tags also... Correct order is critical */
1827 write_item(id3
.title
);
1828 write_item(id3
.artist
);
1829 write_item(id3
.album
);
1830 write_item(id3
.genre_string
);
1831 write_item(id3
.composer
);
1832 write_item(id3
.comment
);
1833 if (has_albumartist
)
1835 write_item(id3
.albumartist
);
1839 write_item(id3
.artist
);
1843 write_item(id3
.grouping
);
1847 write_item(id3
.title
);
1849 total_entry_count
++;
1852 static bool tempbuf_insert(char *str
, int id
, int idx_id
, bool unique
)
1854 struct tempbuf_searchidx
*index
= (struct tempbuf_searchidx
*)tempbuf
;
1855 int len
= strlen(str
)+1;
1858 unsigned *crcbuf
= (unsigned *)&tempbuf
[tempbuf_size
-4];
1859 char buf
[TAG_MAXLEN
+32];
1861 for (i
= 0; str
[i
] != '\0' && i
< (int)sizeof(buf
)-1; i
++)
1862 buf
[i
] = tolower(str
[i
]);
1865 crc32
= crc_32(buf
, i
, 0xffffffff);
1869 /* Check if the crc does not exist -> entry does not exist for sure. */
1870 for (i
= 0; i
< tempbufidx
; i
++)
1872 if (crcbuf
[-i
] != crc32
)
1875 if (!strcasecmp(str
, index
[i
].str
))
1877 if (id
< 0 || id
>= lookup_buffer_depth
)
1879 logf("lookup buf overf.: %d", id
);
1883 lookup
[id
] = &index
[i
];
1889 /* Insert to CRC buffer. */
1890 crcbuf
[-tempbufidx
] = crc32
;
1893 /* Insert it to the buffer. */
1894 tempbuf_left
-= len
;
1895 if (tempbuf_left
- 4 < 0 || tempbufidx
>= commit_entry_count
-1)
1898 if (id
>= lookup_buffer_depth
)
1900 logf("lookup buf overf. #2: %d", id
);
1906 lookup
[id
] = &index
[tempbufidx
];
1907 index
[tempbufidx
].idlist
.id
= id
;
1910 index
[tempbufidx
].idlist
.id
= -1;
1912 index
[tempbufidx
].idlist
.next
= NULL
;
1913 index
[tempbufidx
].idx_id
= idx_id
;
1914 index
[tempbufidx
].seek
= -1;
1915 index
[tempbufidx
].str
= &tempbuf
[tempbuf_pos
];
1916 memcpy(index
[tempbufidx
].str
, str
, len
);
1923 static int compare(const void *p1
, const void *p2
)
1927 struct tempbuf_searchidx
*e1
= (struct tempbuf_searchidx
*)p1
;
1928 struct tempbuf_searchidx
*e2
= (struct tempbuf_searchidx
*)p2
;
1930 if (strcmp(e1
->str
, UNTAGGED
) == 0)
1932 if (strcmp(e2
->str
, UNTAGGED
) == 0)
1936 else if (strcmp(e2
->str
, UNTAGGED
) == 0)
1939 return strncasecmp(e1
->str
, e2
->str
, TAG_MAXLEN
);
1942 static int tempbuf_sort(int fd
)
1944 struct tempbuf_searchidx
*index
= (struct tempbuf_searchidx
*)tempbuf
;
1945 struct tagfile_entry fe
;
1949 /* Generate reverse lookup entries. */
1950 for (i
= 0; i
< lookup_buffer_depth
; i
++)
1952 struct tempbuf_id_list
*idlist
;
1957 if (lookup
[i
]->idlist
.id
== i
)
1960 idlist
= &lookup
[i
]->idlist
;
1961 while (idlist
->next
!= NULL
)
1962 idlist
= idlist
->next
;
1964 tempbuf_left
-= sizeof(struct tempbuf_id_list
);
1965 if (tempbuf_left
- 4 < 0)
1968 idlist
->next
= (struct tempbuf_id_list
*)&tempbuf
[tempbuf_pos
];
1969 if (tempbuf_pos
& 0x03)
1971 tempbuf_pos
= (tempbuf_pos
& ~0x03) + 0x04;
1973 idlist
->next
= (struct tempbuf_id_list
*)&tempbuf
[tempbuf_pos
];
1975 tempbuf_pos
+= sizeof(struct tempbuf_id_list
);
1977 idlist
= idlist
->next
;
1979 idlist
->next
= NULL
;
1984 qsort(index
, tempbufidx
, sizeof(struct tempbuf_searchidx
), compare
);
1985 memset(lookup
, 0, lookup_buffer_depth
* sizeof(struct tempbuf_searchidx
**));
1987 for (i
= 0; i
< tempbufidx
; i
++)
1989 struct tempbuf_id_list
*idlist
= &index
[i
].idlist
;
1991 /* Fix the lookup list. */
1992 while (idlist
!= NULL
)
1994 if (idlist
->id
>= 0)
1995 lookup
[idlist
->id
] = &index
[i
];
1996 idlist
= idlist
->next
;
1999 index
[i
].seek
= lseek(fd
, 0, SEEK_CUR
);
2000 length
= strlen(index
[i
].str
) + 1;
2001 fe
.tag_length
= length
;
2002 fe
.idx_id
= index
[i
].idx_id
;
2004 /* Check the chunk alignment. */
2005 if ((fe
.tag_length
+ sizeof(struct tagfile_entry
))
2006 % TAGFILE_ENTRY_CHUNK_LENGTH
)
2008 fe
.tag_length
+= TAGFILE_ENTRY_CHUNK_LENGTH
-
2009 ((fe
.tag_length
+ sizeof(struct tagfile_entry
))
2010 % TAGFILE_ENTRY_CHUNK_LENGTH
);
2013 #ifdef TAGCACHE_STRICT_ALIGN
2014 /* Make sure the entry is long aligned. */
2015 if (index
[i
].seek
& 0x03)
2017 logf("tempbuf_sort: alignment error!");
2022 if (ecwrite(fd
, &fe
, 1, tagfile_entry_ec
, tc_stat
.econ
) !=
2023 sizeof(struct tagfile_entry
))
2025 logf("tempbuf_sort: write error #1");
2029 if (write(fd
, index
[i
].str
, length
) != length
)
2031 logf("tempbuf_sort: write error #2");
2035 /* Write some padding. */
2036 if (fe
.tag_length
- length
> 0)
2037 write(fd
, "XXXXXXXX", fe
.tag_length
- length
);
2043 inline static struct tempbuf_searchidx
* tempbuf_locate(int id
)
2045 if (id
< 0 || id
>= lookup_buffer_depth
)
2052 inline static int tempbuf_find_location(int id
)
2054 struct tempbuf_searchidx
*entry
;
2056 entry
= tempbuf_locate(id
);
2063 static bool build_numeric_indices(struct tagcache_header
*h
, int tmpfd
)
2065 struct master_header tcmh
;
2066 struct index_entry idx
;
2069 struct temp_file_entry
*entrybuf
= (struct temp_file_entry
*)tempbuf
;
2071 int entries_processed
= 0;
2073 char buf
[TAG_MAXLEN
];
2075 max_entries
= tempbuf_size
/ sizeof(struct temp_file_entry
) - 1;
2077 logf("Building numeric indices...");
2078 lseek(tmpfd
, sizeof(struct tagcache_header
), SEEK_SET
);
2080 if ( (masterfd
= open_master_fd(&tcmh
, true)) < 0)
2083 masterfd_pos
= lseek(masterfd
, tcmh
.tch
.entry_count
* sizeof(struct index_entry
),
2085 if (masterfd_pos
== filesize(masterfd
))
2087 logf("we can't append!");
2092 while (entries_processed
< h
->entry_count
)
2094 int count
= MIN(h
->entry_count
- entries_processed
, max_entries
);
2096 /* Read in as many entries as possible. */
2097 for (i
= 0; i
< count
; i
++)
2099 struct temp_file_entry
*tfe
= &entrybuf
[i
];
2102 /* Read in numeric data. */
2103 if (read(tmpfd
, tfe
, sizeof(struct temp_file_entry
)) !=
2104 sizeof(struct temp_file_entry
))
2106 logf("read fail #1");
2111 datastart
= lseek(tmpfd
, 0, SEEK_CUR
);
2114 * Read string data from the following tags:
2120 * A crc32 hash is calculated from the read data
2121 * and stored back to the data offset field kept in memory.
2123 #define tmpdb_read_string_tag(tag) \
2124 lseek(tmpfd, tfe->tag_offset[tag], SEEK_CUR); \
2125 if ((unsigned long)tfe->tag_length[tag] > sizeof buf) \
2127 logf("read fail: buffer overflow"); \
2132 if (read(tmpfd, buf, tfe->tag_length[tag]) != \
2133 tfe->tag_length[tag]) \
2135 logf("read fail #2"); \
2140 tfe->tag_offset[tag] = crc_32(buf, strlen(buf), 0xffffffff); \
2141 lseek(tmpfd, datastart, SEEK_SET)
2143 tmpdb_read_string_tag(tag_filename
);
2144 tmpdb_read_string_tag(tag_artist
);
2145 tmpdb_read_string_tag(tag_album
);
2146 tmpdb_read_string_tag(tag_title
);
2148 /* Seek to the end of the string data. */
2149 lseek(tmpfd
, tfe
->data_length
, SEEK_CUR
);
2152 /* Backup the master index position. */
2153 masterfd_pos
= lseek(masterfd
, 0, SEEK_CUR
);
2154 lseek(masterfd
, sizeof(struct master_header
), SEEK_SET
);
2156 /* Check if we can resurrect some deleted runtime statistics data. */
2157 for (i
= 0; i
< tcmh
.tch
.entry_count
; i
++)
2159 /* Read the index entry. */
2160 if (ecread(masterfd
, &idx
, 1, index_entry_ec
, tc_stat
.econ
)
2161 != sizeof(struct index_entry
))
2163 logf("read fail #3");
2169 * Skip unless the entry is marked as being deleted
2170 * or the data has already been resurrected.
2172 if (!(idx
.flag
& FLAG_DELETED
) || idx
.flag
& FLAG_RESURRECTED
)
2175 /* Now try to match the entry. */
2177 * To succesfully match a song, the following conditions
2180 * For numeric fields: tag_length
2181 * - Full identical match is required
2183 * If tag_filename matches, no further checking necessary.
2185 * For string hashes: tag_artist, tag_album, tag_title
2186 * - Two of these must match
2188 for (j
= 0; j
< count
; j
++)
2190 struct temp_file_entry
*tfe
= &entrybuf
[j
];
2192 /* Try to match numeric fields first. */
2193 if (tfe
->tag_offset
[tag_length
] != idx
.tag_seek
[tag_length
])
2196 /* Now it's time to do the hash matching. */
2197 if (tfe
->tag_offset
[tag_filename
] != idx
.tag_seek
[tag_filename
])
2199 int match_count
= 0;
2201 /* No filename match, check if we can match two other tags. */
2202 #define tmpdb_match(tag) \
2203 if (tfe->tag_offset[tag] == idx.tag_seek[tag]) \
2206 tmpdb_match(tag_artist
);
2207 tmpdb_match(tag_album
);
2208 tmpdb_match(tag_title
);
2210 if (match_count
< 2)
2212 /* Still no match found, give up. */
2217 /* A match found, now copy & resurrect the statistical data. */
2218 #define tmpdb_copy_tag(tag) \
2219 tfe->tag_offset[tag] = idx.tag_seek[tag]
2221 tmpdb_copy_tag(tag_playcount
);
2222 tmpdb_copy_tag(tag_rating
);
2223 tmpdb_copy_tag(tag_playtime
);
2224 tmpdb_copy_tag(tag_lastplayed
);
2225 tmpdb_copy_tag(tag_commitid
);
2227 /* Avoid processing this entry again. */
2228 idx
.flag
|= FLAG_RESURRECTED
;
2230 lseek(masterfd
, -sizeof(struct index_entry
), SEEK_CUR
);
2231 if (ecwrite(masterfd
, &idx
, 1, index_entry_ec
, tc_stat
.econ
)
2232 != sizeof(struct index_entry
))
2234 logf("masterfd writeback fail #1");
2239 logf("Entry resurrected");
2244 /* Restore the master index position. */
2245 lseek(masterfd
, masterfd_pos
, SEEK_SET
);
2247 /* Commit the data to the index. */
2248 for (i
= 0; i
< count
; i
++)
2250 int loc
= lseek(masterfd
, 0, SEEK_CUR
);
2252 if (ecread(masterfd
, &idx
, 1, index_entry_ec
, tc_stat
.econ
)
2253 != sizeof(struct index_entry
))
2255 logf("read fail #3");
2260 for (j
= 0; j
< TAG_COUNT
; j
++)
2262 if (!tagcache_is_numeric_tag(j
))
2265 idx
.tag_seek
[j
] = entrybuf
[i
].tag_offset
[j
];
2267 idx
.flag
= entrybuf
[i
].flag
;
2269 if (idx
.tag_seek
[tag_commitid
])
2271 /* Data has been resurrected. */
2272 idx
.flag
|= FLAG_DIRTYNUM
;
2274 else if (tc_stat
.ready
&& current_tcmh
.commitid
> 0)
2276 idx
.tag_seek
[tag_commitid
] = current_tcmh
.commitid
;
2277 idx
.flag
|= FLAG_DIRTYNUM
;
2280 /* Write back the updated index. */
2281 lseek(masterfd
, loc
, SEEK_SET
);
2282 if (ecwrite(masterfd
, &idx
, 1, index_entry_ec
, tc_stat
.econ
)
2283 != sizeof(struct index_entry
))
2291 entries_processed
+= count
;
2292 logf("%d/%ld entries processed", entries_processed
, h
->entry_count
);
2303 * == 0 temporary failure
2306 static int build_index(int index_type
, struct tagcache_header
*h
, int tmpfd
)
2309 struct tagcache_header tch
;
2310 struct master_header tcmh
;
2311 struct index_entry idxbuf
[IDX_BUF_DEPTH
];
2313 char buf
[TAG_MAXLEN
+32];
2314 int fd
= -1, masterfd
;
2319 logf("Building index: %d", index_type
);
2321 /* Check the number of entries we need to allocate ram for. */
2322 commit_entry_count
= h
->entry_count
+ 1;
2324 masterfd
= open_master_fd(&tcmh
, false);
2327 commit_entry_count
+= tcmh
.tch
.entry_count
;
2331 remove_files(); /* Just to be sure we are clean. */
2333 /* Open the index file, which contains the tag names. */
2334 fd
= open_tag_fd(&tch
, index_type
, true);
2337 logf("tch.datasize=%ld", tch
.datasize
);
2338 lookup_buffer_depth
= 1 +
2339 /* First part */ commit_entry_count
+
2340 /* Second part */ (tch
.datasize
/ TAGFILE_ENTRY_CHUNK_LENGTH
);
2344 lookup_buffer_depth
= 1 +
2345 /* First part */ commit_entry_count
+
2346 /* Second part */ 0;
2349 logf("lookup_buffer_depth=%ld", lookup_buffer_depth
);
2350 logf("commit_entry_count=%ld", commit_entry_count
);
2352 /* Allocate buffer for all index entries from both old and new
2355 tempbuf_pos
= commit_entry_count
* sizeof(struct tempbuf_searchidx
);
2357 /* Allocate lookup buffer. The first portion of commit_entry_count
2358 * contains the new tags in the temporary file and the second
2359 * part for locating entries already in the db.
2362 * +---------+---------------------------+
2363 * | index | position/ENTRY_CHUNK_SIZE | lookup buffer
2364 * +---------+---------------------------+
2366 * Old tags are inserted to a temporary buffer with position:
2367 * tempbuf_insert(position/ENTRY_CHUNK_SIZE, ...);
2368 * And new tags with index:
2369 * tempbuf_insert(idx, ...);
2371 * The buffer is sorted and written into tag file:
2372 * tempbuf_sort(...);
2373 * leaving master index locations messed up.
2375 * That is fixed using the lookup buffer for old tags:
2376 * new_seek = tempbuf_find_location(old_seek, ...);
2378 * new_seek = tempbuf_find_location(idx);
2380 lookup
= (struct tempbuf_searchidx
**)&tempbuf
[tempbuf_pos
];
2381 tempbuf_pos
+= lookup_buffer_depth
* sizeof(void **);
2382 memset(lookup
, 0, lookup_buffer_depth
* sizeof(void **));
2384 /* And calculate the remaining data space used mainly for storing
2385 * tag data (strings). */
2386 tempbuf_left
= tempbuf_size
- tempbuf_pos
- 8;
2387 if (tempbuf_left
- TAGFILE_ENTRY_AVG_LENGTH
* commit_entry_count
< 0)
2389 logf("Buffer way too small!");
2396 * If tag file contains unique tags (sorted index), we will load
2397 * it entirely into memory so we can resort it later for use with
2400 if (tagcache_is_sorted_tag(index_type
))
2402 logf("loading tags...");
2403 for (i
= 0; i
< tch
.entry_count
; i
++)
2405 struct tagfile_entry entry
;
2406 int loc
= lseek(fd
, 0, SEEK_CUR
);
2409 if (ecread(fd
, &entry
, 1, tagfile_entry_ec
, tc_stat
.econ
)
2410 != sizeof(struct tagfile_entry
))
2412 logf("read error #7");
2417 if (entry
.tag_length
>= (int)sizeof(buf
))
2419 logf("too long tag #3");
2424 if (read(fd
, buf
, entry
.tag_length
) != entry
.tag_length
)
2426 logf("read error #8");
2431 /* Skip deleted entries. */
2436 * Save the tag and tag id in the memory buffer. Tag id
2437 * is saved so we can later reindex the master lookup
2438 * table when the index gets resorted.
2440 ret
= tempbuf_insert(buf
, loc
/TAGFILE_ENTRY_CHUNK_LENGTH
2441 + commit_entry_count
, entry
.idx_id
,
2442 tagcache_is_unique_tag(index_type
));
2453 tempbufidx
= tch
.entry_count
;
2458 * Creating new index file to store the tags. No need to preload
2459 * anything whether the index type is sorted or not.
2461 snprintf(buf
, sizeof buf
, TAGCACHE_FILE_INDEX
, index_type
);
2462 fd
= open(buf
, O_WRONLY
| O_CREAT
| O_TRUNC
);
2465 logf("%s open fail", buf
);
2469 tch
.magic
= TAGCACHE_MAGIC
;
2470 tch
.entry_count
= 0;
2473 if (ecwrite(fd
, &tch
, 1, tagcache_header_ec
, tc_stat
.econ
)
2474 != sizeof(struct tagcache_header
))
2476 logf("header write failed");
2482 /* Loading the tag lookup file as "master file". */
2483 logf("Loading index file");
2484 masterfd
= open(TAGCACHE_FILE_MASTER
, O_RDWR
);
2488 logf("Creating new DB");
2489 masterfd
= open(TAGCACHE_FILE_MASTER
, O_WRONLY
| O_CREAT
| O_TRUNC
);
2493 logf("Failure to create index file (%s)", TAGCACHE_FILE_MASTER
);
2498 /* Write the header (write real values later). */
2499 memset(&tcmh
, 0, sizeof(struct master_header
));
2501 tcmh
.tch
.entry_count
= 0;
2502 tcmh
.tch
.datasize
= 0;
2504 ecwrite(masterfd
, &tcmh
, 1, master_header_ec
, tc_stat
.econ
);
2506 masterfd_pos
= lseek(masterfd
, 0, SEEK_CUR
);
2511 * Master file already exists so we need to process the current
2516 if (ecread(masterfd
, &tcmh
, 1, master_header_ec
, tc_stat
.econ
) !=
2517 sizeof(struct master_header
) || tcmh
.tch
.magic
!= TAGCACHE_MAGIC
)
2519 logf("header error");
2526 * If we reach end of the master file, we need to expand it to
2527 * hold new tags. If the current index is not sorted, we can
2528 * simply append new data to end of the file.
2529 * However, if the index is sorted, we need to update all tag
2530 * pointers in the master file for the current index.
2532 masterfd_pos
= lseek(masterfd
, tcmh
.tch
.entry_count
* sizeof(struct index_entry
),
2534 if (masterfd_pos
== filesize(masterfd
))
2536 logf("appending...");
2542 * Load new unique tags in memory to be sorted later and added
2543 * to the master lookup file.
2545 if (tagcache_is_sorted_tag(index_type
))
2547 lseek(tmpfd
, sizeof(struct tagcache_header
), SEEK_SET
);
2548 /* h is the header of the temporary file containing new tags. */
2549 logf("inserting new tags...");
2550 for (i
= 0; i
< h
->entry_count
; i
++)
2552 struct temp_file_entry entry
;
2554 if (read(tmpfd
, &entry
, sizeof(struct temp_file_entry
)) !=
2555 sizeof(struct temp_file_entry
))
2557 logf("read fail #3");
2563 if (entry
.tag_length
[index_type
] >= (long)sizeof(buf
))
2565 logf("too long entry!");
2570 lseek(tmpfd
, entry
.tag_offset
[index_type
], SEEK_CUR
);
2571 if (read(tmpfd
, buf
, entry
.tag_length
[index_type
]) !=
2572 entry
.tag_length
[index_type
])
2574 logf("read fail #4");
2579 if (tagcache_is_unique_tag(index_type
))
2580 error
= !tempbuf_insert(buf
, i
, -1, true);
2582 error
= !tempbuf_insert(buf
, i
, tcmh
.tch
.entry_count
+ i
, false);
2586 logf("insert error");
2591 lseek(tmpfd
, entry
.data_length
- entry
.tag_offset
[index_type
] -
2592 entry
.tag_length
[index_type
], SEEK_CUR
);
2597 /* Sort the buffer data and write it to the index file. */
2598 lseek(fd
, sizeof(struct tagcache_header
), SEEK_SET
);
2599 i
= tempbuf_sort(fd
);
2602 logf("sorted %d tags", i
);
2605 * Now update all indexes in the master lookup file.
2607 logf("updating indices...");
2608 lseek(masterfd
, sizeof(struct master_header
), SEEK_SET
);
2609 for (i
= 0; i
< tcmh
.tch
.entry_count
; i
+= idxbuf_pos
)
2612 int loc
= lseek(masterfd
, 0, SEEK_CUR
);
2614 idxbuf_pos
= MIN(tcmh
.tch
.entry_count
- i
, IDX_BUF_DEPTH
);
2616 if (ecread(masterfd
, idxbuf
, idxbuf_pos
, index_entry_ec
, tc_stat
.econ
)
2617 != (int)sizeof(struct index_entry
)*idxbuf_pos
)
2619 logf("read fail #5");
2623 lseek(masterfd
, loc
, SEEK_SET
);
2625 for (j
= 0; j
< idxbuf_pos
; j
++)
2627 if (idxbuf
[j
].flag
& FLAG_DELETED
)
2629 /* We can just ignore deleted entries. */
2630 // idxbuf[j].tag_seek[index_type] = 0;
2634 idxbuf
[j
].tag_seek
[index_type
] = tempbuf_find_location(
2635 idxbuf
[j
].tag_seek
[index_type
]/TAGFILE_ENTRY_CHUNK_LENGTH
2636 + commit_entry_count
);
2638 if (idxbuf
[j
].tag_seek
[index_type
] < 0)
2640 logf("update error: %d/%d/%d",
2641 idxbuf
[j
].flag
, i
+j
, tcmh
.tch
.entry_count
);
2649 /* Write back the updated index. */
2650 if (ecwrite(masterfd
, idxbuf
, idxbuf_pos
,
2651 index_entry_ec
, tc_stat
.econ
) !=
2652 (int)sizeof(struct index_entry
)*idxbuf_pos
)
2663 * Walk through the temporary file containing the new tags.
2665 // build_normal_index(h, tmpfd, masterfd, idx);
2666 logf("updating new indices...");
2667 lseek(masterfd
, masterfd_pos
, SEEK_SET
);
2668 lseek(tmpfd
, sizeof(struct tagcache_header
), SEEK_SET
);
2669 lseek(fd
, 0, SEEK_END
);
2670 for (i
= 0; i
< h
->entry_count
; i
+= idxbuf_pos
)
2674 idxbuf_pos
= MIN(h
->entry_count
- i
, IDX_BUF_DEPTH
);
2677 memset(idxbuf
, 0, sizeof(struct index_entry
)*IDX_BUF_DEPTH
);
2681 int loc
= lseek(masterfd
, 0, SEEK_CUR
);
2683 if (ecread(masterfd
, idxbuf
, idxbuf_pos
, index_entry_ec
, tc_stat
.econ
)
2684 != (int)sizeof(struct index_entry
)*idxbuf_pos
)
2686 logf("read fail #6");
2690 lseek(masterfd
, loc
, SEEK_SET
);
2693 /* Read entry headers. */
2694 for (j
= 0; j
< idxbuf_pos
; j
++)
2696 if (!tagcache_is_sorted_tag(index_type
))
2698 struct temp_file_entry entry
;
2699 struct tagfile_entry fe
;
2701 if (read(tmpfd
, &entry
, sizeof(struct temp_file_entry
)) !=
2702 sizeof(struct temp_file_entry
))
2704 logf("read fail #7");
2710 if (entry
.tag_length
[index_type
] >= (int)sizeof(buf
))
2712 logf("too long entry!");
2713 logf("length=%d", entry
.tag_length
[index_type
]);
2714 logf("pos=0x%02lx", lseek(tmpfd
, 0, SEEK_CUR
));
2719 lseek(tmpfd
, entry
.tag_offset
[index_type
], SEEK_CUR
);
2720 if (read(tmpfd
, buf
, entry
.tag_length
[index_type
]) !=
2721 entry
.tag_length
[index_type
])
2723 logf("read fail #8");
2724 logf("offset=0x%02lx", entry
.tag_offset
[index_type
]);
2725 logf("length=0x%02x", entry
.tag_length
[index_type
]);
2730 /* Write to index file. */
2731 idxbuf
[j
].tag_seek
[index_type
] = lseek(fd
, 0, SEEK_CUR
);
2732 fe
.tag_length
= entry
.tag_length
[index_type
];
2733 fe
.idx_id
= tcmh
.tch
.entry_count
+ i
+ j
;
2734 ecwrite(fd
, &fe
, 1, tagfile_entry_ec
, tc_stat
.econ
);
2735 write(fd
, buf
, fe
.tag_length
);
2739 lseek(tmpfd
, entry
.data_length
- entry
.tag_offset
[index_type
] -
2740 entry
.tag_length
[index_type
], SEEK_CUR
);
2744 /* Locate the correct entry from the sorted array. */
2745 idxbuf
[j
].tag_seek
[index_type
] = tempbuf_find_location(i
+ j
);
2746 if (idxbuf
[j
].tag_seek
[index_type
] < 0)
2748 logf("entry not found (%d)", j
);
2756 if (ecwrite(masterfd
, idxbuf
, idxbuf_pos
,
2757 index_entry_ec
, tc_stat
.econ
) !=
2758 (int)sizeof(struct index_entry
)*idxbuf_pos
)
2760 logf("tagcache: write fail #4");
2769 /* Finally write the header. */
2770 tch
.magic
= TAGCACHE_MAGIC
;
2771 tch
.entry_count
= tempbufidx
;
2772 tch
.datasize
= lseek(fd
, 0, SEEK_END
) - sizeof(struct tagcache_header
);
2773 lseek(fd
, 0, SEEK_SET
);
2774 ecwrite(fd
, &tch
, 1, tagcache_header_ec
, tc_stat
.econ
);
2776 if (index_type
!= tag_filename
)
2777 h
->datasize
+= tch
.datasize
;
2778 logf("s:%d/%ld/%ld", index_type
, tch
.datasize
, h
->datasize
);
2790 static bool commit(void)
2792 struct tagcache_header tch
;
2793 struct master_header tcmh
;
2797 #ifdef HAVE_DIRCACHE
2798 bool dircache_buffer_stolen
= false;
2800 bool local_allocation
= false;
2802 logf("committing tagcache");
2807 tmpfd
= open(TAGCACHE_FILE_TEMP
, O_RDONLY
);
2810 logf("nothing to commit");
2815 /* Load the header. */
2816 len
= sizeof(struct tagcache_header
);
2817 rc
= read(tmpfd
, &tch
, len
);
2819 if (tch
.magic
!= TAGCACHE_MAGIC
|| rc
!= len
)
2821 logf("incorrect header");
2823 remove(TAGCACHE_FILE_TEMP
);
2827 if (tch
.entry_count
== 0)
2829 logf("nothing to commit");
2831 remove(TAGCACHE_FILE_TEMP
);
2835 #ifdef HAVE_EEPROM_SETTINGS
2836 remove(TAGCACHE_STATEFILE
);
2839 /* At first be sure to unload the ramcache! */
2840 #ifdef HAVE_TC_RAMCACHE
2841 tc_stat
.ramcache
= false;
2846 /* Try to steal every buffer we can :) */
2847 if (tempbuf_size
== 0)
2848 local_allocation
= true;
2850 #ifdef HAVE_DIRCACHE
2851 if (tempbuf_size
== 0)
2853 /* Try to steal the dircache buffer. */
2854 tempbuf
= dircache_steal_buffer(&tempbuf_size
);
2855 tempbuf_size
&= ~0x03;
2857 if (tempbuf_size
> 0)
2859 dircache_buffer_stolen
= true;
2864 #ifdef HAVE_TC_RAMCACHE
2865 if (tempbuf_size
== 0 && tc_stat
.ramcache_allocated
> 0)
2867 tempbuf
= (char *)(hdr
+ 1);
2868 tempbuf_size
= tc_stat
.ramcache_allocated
- sizeof(struct ramcache_header
) - 128;
2869 tempbuf_size
&= ~0x03;
2873 /* And finally fail if there are no buffers available. */
2874 if (tempbuf_size
== 0)
2876 logf("delaying commit until next boot");
2877 tc_stat
.commit_delayed
= true;
2883 logf("commit %ld entries...", tch
.entry_count
);
2885 /* Mark DB dirty so it will stay disabled if commit fails. */
2886 current_tcmh
.dirty
= true;
2887 update_master_header();
2889 /* Now create the index files. */
2890 tc_stat
.commit_step
= 0;
2892 tc_stat
.commit_delayed
= false;
2894 for (i
= 0; i
< TAG_COUNT
; i
++)
2898 if (tagcache_is_numeric_tag(i
))
2901 tc_stat
.commit_step
++;
2902 ret
= build_index(i
, &tch
, tmpfd
);
2906 logf("tagcache failed init");
2908 tc_stat
.commit_delayed
= true;
2910 tc_stat
.commit_step
= 0;
2916 if (!build_numeric_indices(&tch
, tmpfd
))
2918 logf("Failure to commit numeric indices");
2920 tc_stat
.commit_step
= 0;
2926 remove(TAGCACHE_FILE_TEMP
);
2928 tc_stat
.commit_step
= 0;
2930 /* Update the master index headers. */
2931 if ( (masterfd
= open_master_fd(&tcmh
, true)) < 0)
2937 tcmh
.tch
.entry_count
+= tch
.entry_count
;
2938 tcmh
.tch
.datasize
= sizeof(struct master_header
)
2939 + sizeof(struct index_entry
) * tcmh
.tch
.entry_count
2944 lseek(masterfd
, 0, SEEK_SET
);
2945 ecwrite(masterfd
, &tcmh
, 1, master_header_ec
, tc_stat
.econ
);
2948 logf("tagcache committed");
2949 tc_stat
.ready
= check_all_headers();
2950 tc_stat
.readyvalid
= true;
2952 if (local_allocation
)
2958 #ifdef HAVE_DIRCACHE
2959 /* Rebuild the dircache, if we stole the buffer. */
2960 if (dircache_buffer_stolen
)
2964 #ifdef HAVE_TC_RAMCACHE
2965 /* Reload tagcache. */
2966 if (tc_stat
.ramcache_allocated
> 0)
2967 tagcache_start_scan();
2975 static void allocate_tempbuf(void)
2977 /* Yeah, malloc would be really nice now :) */
2979 tempbuf_size
= 32*1024*1024;
2980 tempbuf
= malloc(tempbuf_size
);
2982 tempbuf
= (char *)(((long)audiobuf
& ~0x03) + 0x04);
2983 tempbuf_size
= (long)audiobufend
- (long)audiobuf
- 4;
2984 audiobuf
+= tempbuf_size
;
2988 static void free_tempbuf(void)
2990 if (tempbuf_size
== 0)
2996 audiobuf
-= tempbuf_size
;
3004 static bool modify_numeric_entry(int masterfd
, int idx_id
, int tag
, long data
)
3006 struct index_entry idx
;
3011 if (!tagcache_is_numeric_tag(tag
))
3014 if (!get_index(masterfd
, idx_id
, &idx
, false))
3017 idx
.tag_seek
[tag
] = data
;
3018 idx
.flag
|= FLAG_DIRTYNUM
;
3020 return write_index(masterfd
, idx_id
, &idx
);
3024 bool tagcache_modify_numeric_entry(struct tagcache_search
*tcs
,
3027 struct master_header myhdr
;
3029 if (tcs
->masterfd
< 0)
3031 if ( (tcs
->masterfd
= open_master_fd(&myhdr
, true)) < 0)
3035 return modify_numeric_entry(tcs
->masterfd
, tcs
->idx_id
, tag
, data
);
3039 #define COMMAND_QUEUE_IS_EMPTY (command_queue_ridx == command_queue_widx)
3041 static bool command_queue_is_full(void)
3045 next
= command_queue_widx
+ 1;
3046 if (next
>= TAGCACHE_COMMAND_QUEUE_LENGTH
)
3049 return (next
== command_queue_ridx
);
3052 static bool command_queue_sync_callback(void)
3055 struct master_header myhdr
;
3058 mutex_lock(&command_queue_mutex
);
3060 if ( (masterfd
= open_master_fd(&myhdr
, true)) < 0)
3063 while (command_queue_ridx
!= command_queue_widx
)
3065 struct tagcache_command_entry
*ce
= &command_queue
[command_queue_ridx
];
3067 switch (ce
->command
)
3069 case CMD_UPDATE_MASTER_HEADER
:
3072 update_master_header();
3074 /* Re-open the masterfd. */
3075 if ( (masterfd
= open_master_fd(&myhdr
, true)) < 0)
3080 case CMD_UPDATE_NUMERIC
:
3082 modify_numeric_entry(masterfd
, ce
->idx_id
, ce
->tag
, ce
->data
);
3087 if (++command_queue_ridx
>= TAGCACHE_COMMAND_QUEUE_LENGTH
)
3088 command_queue_ridx
= 0;
3093 tc_stat
.queue_length
= 0;
3094 mutex_unlock(&command_queue_mutex
);
3098 static void run_command_queue(bool force
)
3100 if (COMMAND_QUEUE_IS_EMPTY
)
3103 if (force
|| command_queue_is_full())
3104 command_queue_sync_callback();
3106 register_storage_idle_func(command_queue_sync_callback
);
3109 static void queue_command(int cmd
, long idx_id
, int tag
, long data
)
3115 mutex_lock(&command_queue_mutex
);
3116 next
= command_queue_widx
+ 1;
3117 if (next
>= TAGCACHE_COMMAND_QUEUE_LENGTH
)
3120 /* Make sure queue is not full. */
3121 if (next
!= command_queue_ridx
)
3123 struct tagcache_command_entry
*ce
= &command_queue
[command_queue_widx
];
3126 ce
->idx_id
= idx_id
;
3130 command_queue_widx
= next
;
3132 tc_stat
.queue_length
++;
3134 mutex_unlock(&command_queue_mutex
);
3138 /* Queue is full, try again later... */
3139 mutex_unlock(&command_queue_mutex
);
3144 long tagcache_increase_serial(void)
3154 old
= current_tcmh
.serial
++;
3155 queue_command(CMD_UPDATE_MASTER_HEADER
, 0, 0, 0);
3160 void tagcache_update_numeric(int idx_id
, int tag
, long data
)
3162 queue_command(CMD_UPDATE_NUMERIC
, idx_id
, tag
, data
);
3164 #endif /* !__PCTOOL__ */
3166 long tagcache_get_serial(void)
3168 return current_tcmh
.serial
;
3171 long tagcache_get_commitid(void)
3173 return current_tcmh
.commitid
;
3176 static bool write_tag(int fd
, const char *tagstr
, const char *datastr
)
3181 snprintf(buf
, sizeof buf
, "%s=\"", tagstr
);
3182 for (i
= strlen(buf
); i
< (long)sizeof(buf
)-4; i
++)
3184 if (*datastr
== '\0')
3187 if (*datastr
== '"' || *datastr
== '\\')
3190 buf
[i
] = *(datastr
++);
3193 strcpy(&buf
[i
], "\" ");
3195 write(fd
, buf
, i
+ 2);
3202 static bool read_tag(char *dest
, long size
,
3203 const char *src
, const char *tagstr
)
3206 char current_tag
[32];
3208 while (*src
!= '\0')
3210 /* Skip all whitespace */
3218 /* Read in tag name */
3219 while (*src
!= '=' && *src
!= ' ')
3221 current_tag
[pos
] = *src
;
3225 if (*src
== '\0' || pos
>= (long)sizeof(current_tag
))
3228 current_tag
[pos
] = '\0';
3230 /* Read in tag data */
3232 /* Find the start. */
3233 while (*src
!= '"' && *src
!= '\0')
3236 if (*src
== '\0' || *(++src
) == '\0')
3239 /* Read the data. */
3240 for (pos
= 0; pos
< size
; pos
++)
3247 dest
[pos
] = *(src
+1);
3267 if (!strcasecmp(tagstr
, current_tag
))
3274 static int parse_changelog_line(int line_n
, const char *buf
, void *parameters
)
3276 struct index_entry idx
;
3277 char tag_data
[TAG_MAXLEN
+32];
3279 long masterfd
= (long)parameters
;
3280 const int import_tags
[] = { tag_playcount
, tag_rating
, tag_playtime
, tag_lastplayed
,
3288 logf("%d/%s", line_n
, buf
);
3289 if (!read_tag(tag_data
, sizeof tag_data
, buf
, "filename"))
3291 logf("filename missing");
3296 idx_id
= find_index(tag_data
);
3299 logf("entry not found");
3303 if (!get_index(masterfd
, idx_id
, &idx
, false))
3305 logf("failed to retrieve index entry");
3309 /* Stop if tag has already been modified. */
3310 if (idx
.flag
& FLAG_DIRTYNUM
)
3313 logf("import: %s", tag_data
);
3315 idx
.flag
|= FLAG_DIRTYNUM
;
3316 for (i
= 0; i
< (long)(sizeof(import_tags
)/sizeof(import_tags
[0])); i
++)
3320 if (!read_tag(tag_data
, sizeof tag_data
, buf
,
3321 tagcache_tag_to_str(import_tags
[i
])))
3326 data
= atoi(tag_data
);
3330 idx
.tag_seek
[import_tags
[i
]] = data
;
3332 if (import_tags
[i
] == tag_lastplayed
&& data
>= current_tcmh
.serial
)
3333 current_tcmh
.serial
= data
+ 1;
3334 else if (import_tags
[i
] == tag_commitid
&& data
>= current_tcmh
.commitid
)
3335 current_tcmh
.commitid
= data
+ 1;
3338 return write_index(masterfd
, idx_id
, &idx
) ? 0 : -5;
3341 bool tagcache_import_changelog(void)
3343 struct master_header myhdr
;
3344 struct tagcache_header tch
;
3355 clfd
= open(TAGCACHE_FILE_CHANGELOG
, O_RDONLY
);
3358 logf("failure to open changelog");
3362 if ( (masterfd
= open_master_fd(&myhdr
, true)) < 0)
3370 filenametag_fd
= open_tag_fd(&tch
, tag_filename
, false);
3372 fast_readline(clfd
, buf
, sizeof buf
, (long *)masterfd
,
3373 parse_changelog_line
);
3378 if (filenametag_fd
>= 0)
3379 close(filenametag_fd
);
3383 update_master_header();
3388 #endif /* !__PCTOOL__ */
3390 bool tagcache_create_changelog(struct tagcache_search
*tcs
)
3392 struct master_header myhdr
;
3393 struct index_entry idx
;
3394 char buf
[TAG_MAXLEN
+32];
3402 if (!tagcache_search(tcs
, tag_filename
))
3405 /* Initialize the changelog */
3406 clfd
= open(TAGCACHE_FILE_CHANGELOG
, O_WRONLY
| O_CREAT
| O_TRUNC
);
3409 logf("failure to open changelog");
3413 if (tcs
->masterfd
< 0)
3415 if ( (tcs
->masterfd
= open_master_fd(&myhdr
, false)) < 0)
3420 lseek(tcs
->masterfd
, 0, SEEK_SET
);
3421 ecread(tcs
->masterfd
, &myhdr
, 1, master_header_ec
, tc_stat
.econ
);
3424 write(clfd
, "## Changelog version 1\n", 23);
3426 for (i
= 0; i
< myhdr
.tch
.entry_count
; i
++)
3428 if (ecread(tcs
->masterfd
, &idx
, 1, index_entry_ec
, tc_stat
.econ
)
3429 != sizeof(struct index_entry
))
3431 logf("read error #9");
3432 tagcache_search_finish(tcs
);
3437 /* Skip until the entry found has been modified. */
3438 if (! (idx
.flag
& FLAG_DIRTYNUM
) )
3441 /* Skip deleted entries too. */
3442 if (idx
.flag
& FLAG_DELETED
)
3445 /* Now retrieve all tags. */
3446 for (j
= 0; j
< TAG_COUNT
; j
++)
3448 if (tagcache_is_numeric_tag(j
))
3450 snprintf(temp
, sizeof temp
, "%d", idx
.tag_seek
[j
]);
3451 write_tag(clfd
, tagcache_tag_to_str(j
), temp
);
3456 tagcache_retrieve(tcs
, i
, tcs
->type
, buf
, sizeof buf
);
3457 write_tag(clfd
, tagcache_tag_to_str(j
), buf
);
3460 write(clfd
, "\n", 1);
3466 tagcache_search_finish(tcs
);
3471 static bool delete_entry(long idx_id
)
3476 struct index_entry idx
, myidx
;
3477 struct master_header myhdr
;
3478 char buf
[TAG_MAXLEN
+32];
3479 int in_use
[TAG_COUNT
];
3481 logf("delete_entry(): %ld", idx_id
);
3483 #ifdef HAVE_TC_RAMCACHE
3484 /* At first mark the entry removed from ram cache. */
3485 if (tc_stat
.ramcache
)
3486 hdr
->indices
[idx_id
].flag
|= FLAG_DELETED
;
3489 if ( (masterfd
= open_master_fd(&myhdr
, true) ) < 0)
3492 lseek(masterfd
, idx_id
* sizeof(struct index_entry
), SEEK_CUR
);
3493 if (ecread(masterfd
, &myidx
, 1, index_entry_ec
, tc_stat
.econ
)
3494 != sizeof(struct index_entry
))
3496 logf("delete_entry(): read error");
3500 if (myidx
.flag
& FLAG_DELETED
)
3502 logf("delete_entry(): already deleted!");
3506 myidx
.flag
|= FLAG_DELETED
;
3507 lseek(masterfd
, -sizeof(struct index_entry
), SEEK_CUR
);
3508 if (ecwrite(masterfd
, &myidx
, 1, index_entry_ec
, tc_stat
.econ
)
3509 != sizeof(struct index_entry
))
3511 logf("delete_entry(): write_error #1");
3515 /* Now check which tags are no longer in use (if any) */
3516 for (tag
= 0; tag
< TAG_COUNT
; tag
++)
3519 lseek(masterfd
, sizeof(struct master_header
), SEEK_SET
);
3520 for (i
= 0; i
< myhdr
.tch
.entry_count
; i
++)
3522 struct index_entry
*idxp
;
3524 #ifdef HAVE_TC_RAMCACHE
3525 /* Use RAM DB if available for greater speed */
3526 if (tc_stat
.ramcache
)
3527 idxp
= &hdr
->indices
[i
];
3531 if (ecread(masterfd
, &idx
, 1, index_entry_ec
, tc_stat
.econ
)
3532 != sizeof(struct index_entry
))
3534 logf("delete_entry(): read error #2");
3540 if (idxp
->flag
& FLAG_DELETED
)
3543 for (tag
= 0; tag
< TAG_COUNT
; tag
++)
3545 if (tagcache_is_numeric_tag(tag
))
3548 if (idxp
->tag_seek
[tag
] == myidx
.tag_seek
[tag
])
3553 /* Now delete all tags no longer in use. */
3554 for (tag
= 0; tag
< TAG_COUNT
; tag
++)
3556 struct tagcache_header tch
;
3557 int oldseek
= myidx
.tag_seek
[tag
];
3559 if (tagcache_is_numeric_tag(tag
))
3563 * Replace tag seek with a hash value of the field string data.
3564 * That way runtime statistics of moved or altered files can be
3567 #ifdef HAVE_TC_RAMCACHE
3568 if (tc_stat
.ramcache
&& tag
!= tag_filename
)
3570 struct tagfile_entry
*tfe
;
3571 int32_t *seek
= &hdr
->indices
[idx_id
].tag_seek
[tag
];
3573 tfe
= (struct tagfile_entry
*)&hdr
->tags
[tag
][*seek
];
3574 *seek
= crc_32(tfe
->tag_data
, strlen(tfe
->tag_data
), 0xffffffff);
3575 myidx
.tag_seek
[tag
] = *seek
;
3580 struct tagfile_entry tfe
;
3582 /* Open the index file, which contains the tag names. */
3583 if ((fd
= open_tag_fd(&tch
, tag
, true)) < 0)
3586 /* Skip the header block */
3587 lseek(fd
, myidx
.tag_seek
[tag
], SEEK_SET
);
3588 if (ecread(fd
, &tfe
, 1, tagfile_entry_ec
, tc_stat
.econ
)
3589 != sizeof(struct tagfile_entry
))
3591 logf("delete_entry(): read error #3");
3595 if (read(fd
, buf
, tfe
.tag_length
) != tfe
.tag_length
)
3597 logf("delete_entry(): read error #4");
3601 myidx
.tag_seek
[tag
] = crc_32(buf
, strlen(buf
), 0xffffffff);
3606 logf("in use: %d/%d", tag
, in_use
[tag
]);
3615 #ifdef HAVE_TC_RAMCACHE
3616 /* Delete from ram. */
3617 if (tc_stat
.ramcache
&& tag
!= tag_filename
)
3619 struct tagfile_entry
*tagentry
= (struct tagfile_entry
*)&hdr
->tags
[tag
][oldseek
];
3620 tagentry
->tag_data
[0] = '\0';
3624 /* Open the index file, which contains the tag names. */
3627 if ((fd
= open_tag_fd(&tch
, tag
, true)) < 0)
3631 /* Skip the header block */
3632 lseek(fd
, oldseek
+ sizeof(struct tagfile_entry
), SEEK_SET
);
3634 /* Debug, print 10 first characters of the tag
3637 logf("TAG:%s", buf);
3638 lseek(fd, -10, SEEK_CUR);
3641 /* Write first data byte in tag as \0 */
3644 /* Now tag data has been removed */
3649 /* Write index entry back into master index. */
3650 lseek(masterfd
, sizeof(struct master_header
) +
3651 (idx_id
* sizeof(struct index_entry
)), SEEK_SET
);
3652 if (ecwrite(masterfd
, &myidx
, 1, index_entry_ec
, tc_stat
.econ
)
3653 != sizeof(struct index_entry
))
3655 logf("delete_entry(): write_error #2");
3674 * Returns true if there is an event waiting in the queue
3675 * that requires the current operation to be aborted.
3677 static bool check_event_queue(void)
3679 struct queue_event ev
;
3681 queue_wait_w_tmo(&tagcache_queue
, &ev
, 0);
3686 case SYS_USB_CONNECTED
:
3687 /* Put the event back into the queue. */
3688 queue_post(&tagcache_queue
, ev
.id
, ev
.data
);
3696 #ifdef HAVE_TC_RAMCACHE
3697 static bool allocate_tagcache(void)
3699 struct master_header tcmh
;
3702 /* Load the header. */
3703 if ( (fd
= open_master_fd(&tcmh
, false)) < 0)
3712 * Now calculate the required cache size plus
3713 * some extra space for alignment fixes.
3715 tc_stat
.ramcache_allocated
= tcmh
.tch
.datasize
+ 128 + TAGCACHE_RESERVE
+
3716 sizeof(struct ramcache_header
) + TAG_COUNT
*sizeof(void *);
3717 hdr
= buffer_alloc(tc_stat
.ramcache_allocated
+ 128);
3718 memset(hdr
, 0, sizeof(struct ramcache_header
));
3719 memcpy(&hdr
->h
, &tcmh
, sizeof(struct master_header
));
3720 hdr
->indices
= (struct index_entry
*)(hdr
+ 1);
3721 logf("tagcache: %d bytes allocated.", tc_stat
.ramcache_allocated
);
3726 # ifdef HAVE_EEPROM_SETTINGS
3727 static bool tagcache_dumpload(void)
3729 struct statefile_header shdr
;
3734 fd
= open(TAGCACHE_STATEFILE
, O_RDONLY
);
3737 logf("no tagcache statedump");
3741 /* Check the statefile memory placement */
3742 hdr
= buffer_alloc(0);
3743 rc
= read(fd
, &shdr
, sizeof(struct statefile_header
));
3744 if (rc
!= sizeof(struct statefile_header
)
3745 /* || (long)hdr != (long)shdr.hdr */)
3747 logf("incorrect statefile");
3753 offpos
= (long)hdr
- (long)shdr
.hdr
;
3755 /* Lets allocate real memory and load it */
3756 hdr
= buffer_alloc(shdr
.tc_stat
.ramcache_allocated
);
3757 rc
= read(fd
, hdr
, shdr
.tc_stat
.ramcache_allocated
);
3760 if (rc
!= shdr
.tc_stat
.ramcache_allocated
)
3762 logf("read failure!");
3767 memcpy(&tc_stat
, &shdr
.tc_stat
, sizeof(struct tagcache_stat
));
3769 /* Now fix the pointers */
3770 hdr
->indices
= (struct index_entry
*)((long)hdr
->indices
+ offpos
);
3771 for (i
= 0; i
< TAG_COUNT
; i
++)
3772 hdr
->tags
[i
] += offpos
;
3777 static bool tagcache_dumpsave(void)
3779 struct statefile_header shdr
;
3782 if (!tc_stat
.ramcache
)
3785 fd
= open(TAGCACHE_STATEFILE
, O_WRONLY
| O_CREAT
| O_TRUNC
);
3788 logf("failed to create a statedump");
3792 /* Create the header */
3794 memcpy(&shdr
.tc_stat
, &tc_stat
, sizeof(struct tagcache_stat
));
3795 write(fd
, &shdr
, sizeof(struct statefile_header
));
3797 /* And dump the data too */
3798 write(fd
, hdr
, tc_stat
.ramcache_allocated
);
3805 static bool load_tagcache(void)
3807 struct tagcache_header
*tch
;
3808 long bytesleft
= tc_stat
.ramcache_allocated
;
3809 struct index_entry
*idx
;
3814 # ifdef HAVE_DIRCACHE
3815 while (dircache_is_initializing())
3818 dircache_set_appflag(DIRCACHE_APPFLAG_TAGCACHE
);
3821 logf("loading tagcache to ram...");
3823 fd
= open(TAGCACHE_FILE_MASTER
, O_RDONLY
);
3826 logf("tagcache open failed");
3830 if (ecread(fd
, &hdr
->h
, 1, master_header_ec
, tc_stat
.econ
)
3831 != sizeof(struct master_header
)
3832 || hdr
->h
.tch
.magic
!= TAGCACHE_MAGIC
)
3834 logf("incorrect header");
3840 /* Load the master index table. */
3841 for (i
= 0; i
< hdr
->h
.tch
.entry_count
; i
++)
3843 rc
= ecread(fd
, idx
, 1, index_entry_ec
, tc_stat
.econ
);
3844 if (rc
!= sizeof(struct index_entry
))
3846 logf("read error #10");
3851 bytesleft
-= sizeof(struct index_entry
);
3852 if (bytesleft
< 0 || ((long)idx
- (long)hdr
->indices
) >= tc_stat
.ramcache_allocated
)
3854 logf("too big tagcache.");
3864 /* Load the tags. */
3866 for (tag
= 0; tag
< TAG_COUNT
; tag
++)
3868 struct tagfile_entry
*fe
;
3869 char buf
[TAG_MAXLEN
+32];
3871 if (tagcache_is_numeric_tag(tag
))
3874 //p = ((void *)p+1);
3875 p
= (char *)((long)p
& ~0x03) + 0x04;
3878 /* Check the header. */
3879 tch
= (struct tagcache_header
*)p
;
3880 p
+= sizeof(struct tagcache_header
);
3882 if ( (fd
= open_tag_fd(tch
, tag
, false)) < 0)
3885 for (hdr
->entry_count
[tag
] = 0;
3886 hdr
->entry_count
[tag
] < tch
->entry_count
;
3887 hdr
->entry_count
[tag
]++)
3891 if (do_timed_yield())
3893 /* Abort if we got a critical event in queue */
3894 if (check_event_queue())
3898 fe
= (struct tagfile_entry
*)p
;
3899 pos
= lseek(fd
, 0, SEEK_CUR
);
3900 rc
= ecread(fd
, fe
, 1, tagfile_entry_ec
, tc_stat
.econ
);
3901 if (rc
!= sizeof(struct tagfile_entry
))
3903 /* End of lookup table. */
3904 logf("read error #11");
3909 /* We have a special handling for the filename tags. */
3910 if (tag
== tag_filename
)
3912 # ifdef HAVE_DIRCACHE
3913 const struct dirent
*dc
;
3916 // FIXME: This is wrong!
3917 // idx = &hdr->indices[hdr->entry_count[i]];
3918 idx
= &hdr
->indices
[fe
->idx_id
];
3920 if (fe
->tag_length
>= (long)sizeof(buf
)-1)
3924 logf("TAG:%s", buf
);
3925 logf("too long filename");
3930 rc
= read(fd
, buf
, fe
->tag_length
);
3931 if (rc
!= fe
->tag_length
)
3933 logf("read error #12");
3938 /* Check if the entry has already been removed */
3939 if (idx
->flag
& FLAG_DELETED
)
3942 /* This flag must not be used yet. */
3943 if (idx
->flag
& FLAG_DIRCACHE
)
3945 logf("internal error!");
3950 if (idx
->tag_seek
[tag
] != pos
)
3952 logf("corrupt data structures!");
3957 # ifdef HAVE_DIRCACHE
3958 if (dircache_is_enabled())
3960 dc
= dircache_get_entry_ptr(buf
);
3963 logf("Entry no longer valid.");
3965 delete_entry(fe
->idx_id
);
3969 idx
->flag
|= FLAG_DIRCACHE
;
3970 FLAG_SET_ATTR(idx
->flag
, fe
->idx_id
);
3971 idx
->tag_seek
[tag_filename
] = (long)dc
;
3976 /* This will be very slow unless dircache is enabled
3977 or target is flash based, but do it anyway for
3979 /* Check if entry has been removed. */
3980 if (global_settings
.tagcache_autoupdate
)
3982 if (!file_exists(buf
))
3984 logf("Entry no longer valid.");
3986 delete_entry(fe
->idx_id
);
3995 bytesleft
-= sizeof(struct tagfile_entry
) + fe
->tag_length
;
3998 logf("too big tagcache #2");
3999 logf("tl: %d", fe
->tag_length
);
4000 logf("bl: %ld", bytesleft
);
4006 rc
= read(fd
, fe
->tag_data
, fe
->tag_length
);
4009 if (rc
!= fe
->tag_length
)
4011 logf("read error #13");
4012 logf("rc=0x%04x", rc
); // 0x431
4013 logf("len=0x%04x", fe
->tag_length
); // 0x4000
4014 logf("pos=0x%04lx", lseek(fd
, 0, SEEK_CUR
)); // 0x433
4015 logf("tag=0x%02x", tag
); // 0x00
4023 tc_stat
.ramcache_used
= tc_stat
.ramcache_allocated
- bytesleft
;
4024 logf("tagcache loaded into ram!");
4028 #endif /* HAVE_TC_RAMCACHE */
4030 static bool check_deleted_files(void)
4033 char buf
[TAG_MAXLEN
+32];
4034 struct tagfile_entry tfe
;
4036 logf("reverse scan...");
4037 snprintf(buf
, sizeof buf
, TAGCACHE_FILE_INDEX
, tag_filename
);
4038 fd
= open(buf
, O_RDONLY
);
4042 logf("%s open fail", buf
);
4046 lseek(fd
, sizeof(struct tagcache_header
), SEEK_SET
);
4047 while (ecread(fd
, &tfe
, 1, tagfile_entry_ec
, tc_stat
.econ
)
4048 == sizeof(struct tagfile_entry
)
4050 && !check_event_queue()
4054 if (tfe
.tag_length
>= (long)sizeof(buf
)-1)
4056 logf("too long tag");
4061 if (read(fd
, buf
, tfe
.tag_length
) != tfe
.tag_length
)
4063 logf("read error #14");
4068 /* Check if the file has already deleted from the db. */
4072 /* Now check if the file exists. */
4073 if (!file_exists(buf
))
4075 logf("Entry no longer valid.");
4076 logf("-> %s / %d", buf
, tfe
.tag_length
);
4077 delete_entry(tfe
.idx_id
);
4088 static bool check_dir(const char *dirname
, int add_files
)
4092 int success
= false;
4093 int ignore
, unignore
;
4094 char newpath
[MAX_PATH
];
4096 dir
= opendir(dirname
);
4099 logf("tagcache: opendir() failed");
4103 /* check for a database.ignore file */
4104 snprintf(newpath
, MAX_PATH
, "%s/database.ignore", dirname
);
4105 ignore
= file_exists(newpath
);
4106 /* check for a database.unignore file */
4107 snprintf(newpath
, MAX_PATH
, "%s/database.unignore", dirname
);
4108 unignore
= file_exists(newpath
);
4110 /* don't do anything if both ignore and unignore are there */
4111 if (ignore
!= unignore
)
4112 add_files
= unignore
;
4114 /* Recursively scan the dir. */
4118 while (!check_event_queue())
4121 struct dirent
*entry
;
4123 entry
= readdir(dir
);
4131 if (!strcmp((char *)entry
->d_name
, ".") ||
4132 !strcmp((char *)entry
->d_name
, ".."))
4137 len
= strlen(curpath
);
4138 snprintf(&curpath
[len
], sizeof(curpath
) - len
, "/%s",
4141 processed_dir_count
++;
4142 if (entry
->attribute
& ATTR_DIRECTORY
)
4143 check_dir(curpath
, add_files
);
4146 tc_stat
.curentry
= curpath
;
4148 /* Add a new entry to the temporary db file. */
4149 add_tagcache(curpath
, (entry
->wrtdate
<< 16) | entry
->wrttime
4150 #if defined(HAVE_TC_RAMCACHE) && defined(HAVE_DIRCACHE)
4151 , dir
->internal_entry
4155 /* Wait until current path for debug screen is read and unset. */
4156 while (tc_stat
.syncscreen
&& tc_stat
.curentry
!= NULL
)
4159 tc_stat
.curentry
= NULL
;
4162 curpath
[len
] = '\0';
4170 void tagcache_screensync_event(void)
4172 tc_stat
.curentry
= NULL
;
4175 void tagcache_screensync_enable(bool state
)
4177 tc_stat
.syncscreen
= state
;
4180 void tagcache_build(const char *path
)
4182 struct tagcache_header header
;
4187 total_entry_count
= 0;
4188 processed_dir_count
= 0;
4190 #ifdef HAVE_DIRCACHE
4191 while (dircache_is_initializing())
4195 logf("updating tagcache");
4197 cachefd
= open(TAGCACHE_FILE_TEMP
, O_RDONLY
);
4200 logf("skipping, cache already waiting for commit");
4205 cachefd
= open(TAGCACHE_FILE_TEMP
, O_RDWR
| O_CREAT
| O_TRUNC
);
4208 logf("master file open failed: %s", TAGCACHE_FILE_TEMP
);
4212 filenametag_fd
= open_tag_fd(&header
, tag_filename
, false);
4216 logf("Scanning files...");
4217 /* Scan for new files. */
4218 memset(&header
, 0, sizeof(struct tagcache_header
));
4219 write(cachefd
, &header
, sizeof(struct tagcache_header
));
4221 if (strcmp("/", path
) != 0)
4222 strcpy(curpath
, path
);
4223 ret
= check_dir(path
, true);
4225 /* Write the header. */
4226 header
.magic
= TAGCACHE_MAGIC
;
4227 header
.datasize
= data_size
;
4228 header
.entry_count
= total_entry_count
;
4229 lseek(cachefd
, 0, SEEK_SET
);
4230 write(cachefd
, &header
, sizeof(struct tagcache_header
));
4233 if (filenametag_fd
>= 0)
4235 close(filenametag_fd
);
4236 filenametag_fd
= -1;
4246 /* Commit changes to the database. */
4252 remove(TAGCACHE_FILE_TEMP
);
4253 logf("tagcache built!");
4259 #ifdef HAVE_TC_RAMCACHE
4262 /* Import runtime statistics if we just initialized the db. */
4263 if (hdr
->h
.serial
== 0)
4264 queue_post(&tagcache_queue
, Q_IMPORT_CHANGELOG
, 0);
4271 #ifdef HAVE_TC_RAMCACHE
4272 static void load_ramcache(void)
4279 /* At first we should load the cache (if exists). */
4280 tc_stat
.ramcache
= load_tagcache();
4282 if (!tc_stat
.ramcache
)
4284 /* If loading failed, it must indicate some problem with the db
4285 * so disable it entirely to prevent further issues. */
4286 tc_stat
.ready
= false;
4293 void tagcache_unload_ramcache(void)
4295 tc_stat
.ramcache
= false;
4296 /* Just to make sure there is no statefile present. */
4297 // remove(TAGCACHE_STATEFILE);
4302 static void tagcache_thread(void)
4304 struct queue_event ev
;
4305 bool check_done
= false;
4307 /* If the previous cache build/update was interrupted, commit
4308 * the changes first in foreground. */
4314 #ifdef HAVE_TC_RAMCACHE
4315 # ifdef HAVE_EEPROM_SETTINGS
4316 if (firmware_settings
.initialized
&& firmware_settings
.disk_clean
)
4317 check_done
= tagcache_dumpload();
4319 remove(TAGCACHE_STATEFILE
);
4322 /* Allocate space for the tagcache if found on disk. */
4323 if (global_settings
.tagcache_ram
&& !tc_stat
.ramcache
)
4324 allocate_tagcache();
4328 tc_stat
.initialized
= true;
4330 /* Don't delay bootup with the header check but do it on background. */
4332 tc_stat
.ready
= check_all_headers();
4333 tc_stat
.readyvalid
= true;
4337 run_command_queue(false);
4339 queue_wait_w_tmo(&tagcache_queue
, &ev
, HZ
);
4343 case Q_IMPORT_CHANGELOG
:
4344 tagcache_import_changelog();
4349 remove(TAGCACHE_FILE_TEMP
);
4350 tagcache_build("/");
4354 tagcache_build("/");
4355 #ifdef HAVE_TC_RAMCACHE
4358 check_deleted_files();
4364 if (check_done
|| !tc_stat
.ready
)
4367 #ifdef HAVE_TC_RAMCACHE
4368 if (!tc_stat
.ramcache
&& global_settings
.tagcache_ram
)
4371 if (tc_stat
.ramcache
&& global_settings
.tagcache_autoupdate
)
4372 tagcache_build("/");
4376 if (global_settings
.tagcache_autoupdate
)
4378 tagcache_build("/");
4380 /* This will be very slow unless dircache is enabled
4381 or target is flash based, but do it anyway for
4383 check_deleted_files();
4386 logf("tagcache check done");
4398 case SYS_USB_CONNECTED
:
4399 logf("USB: TagCache");
4400 usb_acknowledge(SYS_USB_CONNECTED_ACK
);
4401 usb_wait_for_disconnect(&tagcache_queue
);
4408 bool tagcache_prepare_shutdown(void)
4410 if (tagcache_get_commit_step() > 0)
4413 tagcache_stop_scan();
4414 while (read_lock
|| write_lock
)
4420 void tagcache_shutdown(void)
4422 /* Flush the command queue. */
4423 run_command_queue(true);
4425 #ifdef HAVE_EEPROM_SETTINGS
4426 if (tc_stat
.ramcache
)
4427 tagcache_dumpsave();
4431 static int get_progress(void)
4433 int total_count
= -1;
4435 #ifdef HAVE_DIRCACHE
4436 if (dircache_is_enabled())
4438 total_count
= dircache_get_entry_count();
4442 #ifdef HAVE_TC_RAMCACHE
4444 if (hdr
&& tc_stat
.ramcache
)
4445 total_count
= hdr
->h
.tch
.entry_count
;
4449 if (total_count
< 0)
4452 return processed_dir_count
* 100 / total_count
;
4455 struct tagcache_stat
* tagcache_get_stat(void)
4457 tc_stat
.progress
= get_progress();
4458 tc_stat
.processed_entries
= processed_dir_count
;
4463 void tagcache_start_scan(void)
4465 queue_post(&tagcache_queue
, Q_START_SCAN
, 0);
4468 bool tagcache_update(void)
4473 queue_post(&tagcache_queue
, Q_UPDATE
, 0);
4477 bool tagcache_rebuild()
4479 queue_post(&tagcache_queue
, Q_REBUILD
, 0);
4483 void tagcache_stop_scan(void)
4485 queue_post(&tagcache_queue
, Q_STOP_SCAN
, 0);
4488 #ifdef HAVE_TC_RAMCACHE
4489 bool tagcache_is_ramcache(void)
4491 return tc_stat
.ramcache
;
4495 #endif /* !__PCTOOL__ */
4498 void tagcache_init(void)
4500 memset(&tc_stat
, 0, sizeof(struct tagcache_stat
));
4501 memset(¤t_tcmh
, 0, sizeof(struct master_header
));
4502 filenametag_fd
= -1;
4503 write_lock
= read_lock
= 0;
4506 mutex_init(&command_queue_mutex
);
4507 queue_init(&tagcache_queue
, true);
4508 create_thread(tagcache_thread
, tagcache_stack
,
4509 sizeof(tagcache_stack
), 0, tagcache_thread_name
4510 IF_PRIO(, PRIORITY_BACKGROUND
)
4513 tc_stat
.initialized
= true;
4517 tc_stat
.ready
= check_all_headers();
4522 void tagcache_reverse_scan(void)
4524 logf("Checking for deleted files");
4525 check_deleted_files();
4529 bool tagcache_is_initialized(void)
4531 return tc_stat
.initialized
;
4533 bool tagcache_is_usable(void)
4535 return tc_stat
.initialized
&& tc_stat
.ready
;
4537 int tagcache_get_commit_step(void)
4539 return tc_stat
.commit_step
;
4541 int tagcache_get_max_commit_step(void)
4543 return (int)(sizeof(sorted_tags
)/sizeof(sorted_tags
[0]))+1;