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)
89 /* Tag Cache thread. */
90 static struct event_queue tagcache_queue
;
91 static long tagcache_stack
[(DEFAULT_STACK_SIZE
+ 0x4000)/sizeof(long)];
92 static const char tagcache_thread_name
[] = "tagcache";
95 #define UNTAGGED "<Untagged>"
97 /* Previous path when scanning directory tree recursively. */
98 static char curpath
[TAG_MAXLEN
+32];
99 static long curpath_size
= sizeof(curpath
);
101 /* Used when removing duplicates. */
102 static char *tempbuf
; /* Allocated when needed. */
103 static long tempbufidx
; /* Current location in buffer. */
104 static long tempbuf_size
; /* Buffer size (TEMPBUF_SIZE). */
105 static long tempbuf_left
; /* Buffer space left. */
106 static long tempbuf_pos
;
108 /* Tags we want to get sorted (loaded to the tempbuf). */
109 static const int sorted_tags
[] = { tag_artist
, tag_album
, tag_genre
,
110 tag_composer
, tag_comment
, tag_albumartist
, tag_grouping
, tag_title
};
112 /* Uniqued tags (we can use these tags with filters and conditional clauses). */
113 static const int unique_tags
[] = { tag_artist
, tag_album
, tag_genre
,
114 tag_composer
, tag_comment
, tag_albumartist
, tag_grouping
};
116 /* Numeric tags (we can use these tags with conditional clauses). */
117 static const int numeric_tags
[] = { tag_year
, tag_discnumber
,
118 tag_tracknumber
, tag_length
, tag_bitrate
, tag_playcount
, tag_rating
,
119 tag_playtime
, tag_lastplayed
, tag_commitid
, tag_mtime
,
120 tag_virt_length_min
, tag_virt_length_sec
,
121 tag_virt_playtime_min
, tag_virt_playtime_sec
,
122 tag_virt_entryage
, tag_virt_autoscore
};
124 /* String presentation of the tags defined in tagcache.h. Must be in correct order! */
125 static const char *tags_str
[] = { "artist", "album", "genre", "title",
126 "filename", "composer", "comment", "albumartist", "grouping", "year",
127 "discnumber", "tracknumber", "bitrate", "length", "playcount", "rating",
128 "playtime", "lastplayed", "commitid", "mtime" };
130 /* Status information of the tagcache. */
131 static struct tagcache_stat tc_stat
;
133 /* Queue commands. */
134 enum tagcache_queue
{
141 /* Internal tagcache command queue. */
142 CMD_UPDATE_MASTER_HEADER
,
146 struct tagcache_command_entry
{
153 static struct tagcache_command_entry command_queue
[TAGCACHE_COMMAND_QUEUE_LENGTH
];
154 static volatile int command_queue_widx
= 0;
155 static volatile int command_queue_ridx
= 0;
156 static struct mutex command_queue_mutex
;
157 /* Timestamp of the last added event, so we can wait a bit before committing the
158 * whole queue at once. */
159 static long command_queue_timestamp
= 0;
161 /* Tag database structures. */
163 /* Variable-length tag entry in tag files. */
164 struct tagfile_entry
{
165 short tag_length
; /* Length of the data in bytes including '\0' */
166 short idx_id
; /* Corresponding entry location in index file of not unique tags */
167 char tag_data
[0]; /* Begin of the tag data */
170 /* Fixed-size tag entry in master db index. */
172 long tag_seek
[TAG_COUNT
]; /* Location of tag data or numeric tag data */
173 long flag
; /* Status flags */
176 /* Header is the same in every file. */
177 struct tagcache_header
{
178 long magic
; /* Header version number */
179 long datasize
; /* Data size in bytes */
180 long entry_count
; /* Number of entries in this file */
183 struct master_header
{
184 struct tagcache_header tch
;
185 long serial
; /* Increasing counting number */
186 long commitid
; /* Number of commits so far */
190 /* For the endianess correction */
191 static const char *tagfile_entry_ec
= "ss";
192 static const char *index_entry_ec
= "lllllllllllllllllllll"; /* (1 + TAG_COUNT) * l */
193 static const char *tagcache_header_ec
= "lll";
194 static const char *master_header_ec
= "llllll";
196 static struct master_header current_tcmh
;
198 #ifdef HAVE_TC_RAMCACHE
199 /* Header is created when loading database to ram. */
200 struct ramcache_header
{
201 struct master_header h
; /* Header from the master index */
202 struct index_entry
*indices
; /* Master index file content */
203 char *tags
[TAG_COUNT
]; /* Tag file content (not including filename tag) */
204 int entry_count
[TAG_COUNT
]; /* Number of entries in the indices. */
207 # ifdef HAVE_EEPROM_SETTINGS
208 struct statefile_header
{
209 struct ramcache_header
*hdr
;
210 struct tagcache_stat tc_stat
;
214 /* Pointer to allocated ramcache_header */
215 static struct ramcache_header
*hdr
;
219 * Full tag entries stored in a temporary file waiting
220 * for commit to the cache. */
221 struct temp_file_entry
{
222 long tag_offset
[TAG_COUNT
];
223 short tag_length
[TAG_COUNT
];
229 struct tempbuf_id_list
{
231 struct tempbuf_id_list
*next
;
234 struct tempbuf_searchidx
{
238 struct tempbuf_id_list idlist
;
241 /* Lookup buffer for fixing messed up index while after sorting. */
242 static long commit_entry_count
;
243 static long lookup_buffer_depth
;
244 static struct tempbuf_searchidx
**lookup
;
246 /* Used when building the temporary file. */
247 static int cachefd
= -1, filenametag_fd
;
248 static int total_entry_count
= 0;
249 static int data_size
= 0;
250 static int processed_dir_count
;
252 /* Thread safe locking */
253 static volatile int write_lock
;
254 static volatile int read_lock
;
256 static bool delete_entry(long idx_id
);
258 const char* tagcache_tag_to_str(int tag
)
260 return tags_str
[tag
];
263 bool tagcache_is_numeric_tag(int type
)
267 for (i
= 0; i
< (int)(sizeof(numeric_tags
)/sizeof(numeric_tags
[0])); i
++)
269 if (type
== numeric_tags
[i
])
276 bool tagcache_is_unique_tag(int type
)
280 for (i
= 0; i
< (int)(sizeof(unique_tags
)/sizeof(unique_tags
[0])); i
++)
282 if (type
== unique_tags
[i
])
289 bool tagcache_is_sorted_tag(int type
)
293 for (i
= 0; i
< (int)(sizeof(sorted_tags
)/sizeof(sorted_tags
[0])); i
++)
295 if (type
== sorted_tags
[i
])
304 * Returns true if specified flag is still present, i.e., dircache
305 * has not been reloaded.
307 static bool is_dircache_intact(void)
309 return dircache_get_appflag(DIRCACHE_APPFLAG_TAGCACHE
);
313 static int open_tag_fd(struct tagcache_header
*hdr
, int tag
, bool write
)
319 if (tagcache_is_numeric_tag(tag
) || tag
< 0 || tag
>= TAG_COUNT
)
322 snprintf(buf
, sizeof buf
, TAGCACHE_FILE_INDEX
, tag
);
324 fd
= open(buf
, write
? O_RDWR
: O_RDONLY
);
327 logf("tag file open failed: tag=%d write=%d file=%s", tag
, write
, buf
);
328 tc_stat
.ready
= false;
332 /* Check the header. */
333 rc
= ecread(fd
, hdr
, 1, tagcache_header_ec
, tc_stat
.econ
);
334 if (hdr
->magic
!= TAGCACHE_MAGIC
|| rc
!= sizeof(struct tagcache_header
))
336 logf("header error");
337 tc_stat
.ready
= false;
345 static int open_master_fd(struct master_header
*hdr
, bool write
)
350 fd
= open(TAGCACHE_FILE_MASTER
, write
? O_RDWR
: O_RDONLY
);
353 logf("master file open failed for R/W");
354 tc_stat
.ready
= false;
358 tc_stat
.econ
= false;
360 /* Check the header. */
361 rc
= read(fd
, hdr
, sizeof(struct master_header
));
362 if (hdr
->tch
.magic
== TAGCACHE_MAGIC
&& rc
== sizeof(struct master_header
))
368 /* Trying to read again, this time with endianess correction enabled. */
369 lseek(fd
, 0, SEEK_SET
);
371 rc
= ecread(fd
, hdr
, 1, master_header_ec
, true);
372 if (hdr
->tch
.magic
!= TAGCACHE_MAGIC
|| rc
!= sizeof(struct master_header
))
374 logf("header error");
375 tc_stat
.ready
= false;
386 static bool do_timed_yield(void)
388 /* Sorting can lock up for quite a while, so yield occasionally */
389 static long wakeup_tick
= 0;
390 if (current_tick
>= wakeup_tick
)
392 wakeup_tick
= current_tick
+ (HZ
/4);
400 #if defined(HAVE_TC_RAMCACHE) && defined(HAVE_DIRCACHE)
401 static long find_entry_ram(const char *filename
,
402 const struct dirent
*dc
)
404 static long last_pos
= 0;
407 /* Check if we tagcache is loaded into ram. */
408 if (!tc_stat
.ramcache
)
412 dc
= dircache_get_entry_ptr(filename
);
416 logf("tagcache: file not found.");
427 for (; i
< hdr
->h
.tch
.entry_count
; i
++)
429 if (hdr
->indices
[i
].tag_seek
[tag_filename
] == (long)dc
)
431 last_pos
= MAX(0, i
- 3);
448 static long find_entry_disk(const char *filename
)
450 struct tagcache_header tch
;
451 static long last_pos
= -1;
452 long pos_history
[POS_HISTORY_COUNT
];
453 long pos_history_idx
= 0;
455 struct tagfile_entry tfe
;
457 char buf
[TAG_MAXLEN
+32];
468 if ( (fd
= open_tag_fd(&tch
, tag_filename
, false)) < 0)
475 lseek(fd
, last_pos
, SEEK_SET
);
477 lseek(fd
, sizeof(struct tagcache_header
), SEEK_SET
);
481 pos
= lseek(fd
, 0, SEEK_CUR
);
482 for (i
= pos_history_idx
-1; i
>= 0; i
--)
483 pos_history
[i
+1] = pos_history
[i
];
484 pos_history
[0] = pos
;
486 if (ecread(fd
, &tfe
, 1, tagfile_entry_ec
, tc_stat
.econ
)
487 != sizeof(struct tagfile_entry
))
492 if (tfe
.tag_length
>= (long)sizeof(buf
))
494 logf("too long tag #1");
500 if (read(fd
, buf
, tfe
.tag_length
) != tfe
.tag_length
)
502 logf("read error #2");
508 if (!strcasecmp(filename
, buf
))
510 last_pos
= pos_history
[pos_history_idx
];
515 if (pos_history_idx
< POS_HISTORY_COUNT
- 1)
529 if (fd
!= filenametag_fd
)
534 if (fd
!= filenametag_fd
)
540 static int find_index(const char *filename
)
544 #if defined(HAVE_TC_RAMCACHE) && defined(HAVE_DIRCACHE)
545 if (tc_stat
.ramcache
&& is_dircache_intact())
546 idx_id
= find_entry_ram(filename
, NULL
);
550 idx_id
= find_entry_disk(filename
);
555 bool tagcache_find_index(struct tagcache_search
*tcs
, const char *filename
)
562 idx_id
= find_index(filename
);
566 if (!tagcache_search(tcs
, tag_filename
))
569 tcs
->entry_count
= 0;
570 tcs
->idx_id
= idx_id
;
575 static bool get_index(int masterfd
, int idxid
,
576 struct index_entry
*idx
, bool use_ram
)
578 bool localfd
= false;
582 logf("Incorrect idxid: %d", idxid
);
586 #ifdef HAVE_TC_RAMCACHE
587 if (tc_stat
.ramcache
&& use_ram
)
589 if (hdr
->indices
[idxid
].flag
& FLAG_DELETED
)
592 memcpy(idx
, &hdr
->indices
[idxid
], sizeof(struct index_entry
));
601 struct master_header tcmh
;
604 masterfd
= open_master_fd(&tcmh
, false);
609 lseek(masterfd
, idxid
* sizeof(struct index_entry
)
610 + sizeof(struct master_header
), SEEK_SET
);
611 if (ecread(masterfd
, idx
, 1, index_entry_ec
, tc_stat
.econ
)
612 != sizeof(struct index_entry
))
614 logf("read error #3");
624 if (idx
->flag
& FLAG_DELETED
)
630 static bool write_index(int masterfd
, int idxid
, struct index_entry
*idx
)
632 /* We need to exclude all memory only flags & tags when writing to disk. */
633 if (idx
->flag
& FLAG_DIRCACHE
)
635 logf("memory only flags!");
639 #ifdef HAVE_TC_RAMCACHE
640 /* Only update numeric data. Writing the whole index to RAM by memcpy
641 * destroys dircache pointers!
643 if (tc_stat
.ramcache
)
646 struct index_entry
*idx_ram
= &hdr
->indices
[idxid
];
648 for (tag
= 0; tag
< TAG_COUNT
; tag
++)
650 if (tagcache_is_numeric_tag(tag
))
652 idx_ram
->tag_seek
[tag
] = idx
->tag_seek
[tag
];
656 /* Don't touch the dircache flag. */
657 idx_ram
->flag
= idx
->flag
| (idx_ram
->flag
& FLAG_DIRCACHE
);
661 lseek(masterfd
, idxid
* sizeof(struct index_entry
)
662 + sizeof(struct master_header
), SEEK_SET
);
663 if (ecwrite(masterfd
, idx
, 1, index_entry_ec
, tc_stat
.econ
)
664 != sizeof(struct index_entry
))
666 logf("write error #3");
667 logf("idxid: %d", idxid
);
674 static bool open_files(struct tagcache_search
*tcs
, int tag
)
676 if (tcs
->idxfd
[tag
] < 0)
680 snprintf(fn
, sizeof fn
, TAGCACHE_FILE_INDEX
, tag
);
681 tcs
->idxfd
[tag
] = open(fn
, O_RDONLY
);
684 if (tcs
->idxfd
[tag
] < 0)
686 logf("File not open!");
693 static bool retrieve(struct tagcache_search
*tcs
, struct index_entry
*idx
,
694 int tag
, char *buf
, long size
)
696 struct tagfile_entry tfe
;
701 if (tagcache_is_numeric_tag(tag
))
704 seek
= idx
->tag_seek
[tag
];
707 logf("Retrieve failed");
711 #ifdef HAVE_TC_RAMCACHE
714 struct tagfile_entry
*ep
;
716 # ifdef HAVE_DIRCACHE
717 if (tag
== tag_filename
&& (idx
->flag
& FLAG_DIRCACHE
)
718 && is_dircache_intact())
720 dircache_copy_path((struct dirent
*)seek
,
726 if (tag
!= tag_filename
)
728 ep
= (struct tagfile_entry
*)&hdr
->tags
[tag
][seek
];
729 strncpy(buf
, ep
->tag_data
, size
-1);
736 if (!open_files(tcs
, tag
))
739 lseek(tcs
->idxfd
[tag
], seek
, SEEK_SET
);
740 if (ecread(tcs
->idxfd
[tag
], &tfe
, 1, tagfile_entry_ec
, tc_stat
.econ
)
741 != sizeof(struct tagfile_entry
))
743 logf("read error #5");
747 if (tfe
.tag_length
>= size
)
749 logf("too small buffer");
753 if (read(tcs
->idxfd
[tag
], buf
, tfe
.tag_length
) !=
756 logf("read error #6");
760 buf
[tfe
.tag_length
] = '\0';
765 static long check_virtual_tags(int tag
, const struct index_entry
*idx
)
771 case tag_virt_length_sec
:
772 data
= (idx
->tag_seek
[tag_length
]/1000) % 60;
775 case tag_virt_length_min
:
776 data
= (idx
->tag_seek
[tag_length
]/1000) / 60;
779 case tag_virt_playtime_sec
:
780 data
= (idx
->tag_seek
[tag_playtime
]/1000) % 60;
783 case tag_virt_playtime_min
:
784 data
= (idx
->tag_seek
[tag_playtime
]/1000) / 60;
787 case tag_virt_autoscore
:
788 if (idx
->tag_seek
[tag_length
] == 0
789 || idx
->tag_seek
[tag_playcount
] == 0)
795 data
= 100 * idx
->tag_seek
[tag_playtime
]
796 / idx
->tag_seek
[tag_length
]
797 / idx
->tag_seek
[tag_playcount
];
801 /* How many commits before the file has been added to the DB. */
802 case tag_virt_entryage
:
803 data
= current_tcmh
.commitid
- idx
->tag_seek
[tag_commitid
] - 1;
807 data
= idx
->tag_seek
[tag
];
813 long tagcache_get_numeric(const struct tagcache_search
*tcs
, int tag
)
815 struct index_entry idx
;
820 if (!tagcache_is_numeric_tag(tag
))
823 if (!get_index(tcs
->masterfd
, tcs
->idx_id
, &idx
, true))
826 return check_virtual_tags(tag
, &idx
);
829 inline static bool str_ends_with(const char *str1
, const char *str2
)
831 int str_len
= strlen(str1
);
832 int clause_len
= strlen(str2
);
834 if (clause_len
> str_len
)
837 return !strcasecmp(&str1
[str_len
- clause_len
], str2
);
840 inline static bool str_oneof(const char *str
, const char *list
)
843 int l
, len
= strlen(str
);
847 sep
= strchr(list
, '|');
848 l
= sep
? (long)sep
- (long)list
: (int)strlen(list
);
849 if ((l
==len
) && !strncasecmp(str
, list
, len
))
851 list
+= sep
? l
+ 1 : l
;
857 static bool check_against_clause(long numeric
, const char *str
,
858 const struct tagcache_search_clause
*clause
)
862 switch (clause
->type
)
865 return numeric
== clause
->numeric_data
;
867 return numeric
!= clause
->numeric_data
;
869 return numeric
> clause
->numeric_data
;
871 return numeric
>= clause
->numeric_data
;
873 return numeric
< clause
->numeric_data
;
875 return numeric
<= clause
->numeric_data
;
877 logf("Incorrect numeric tag: %d", clause
->type
);
882 switch (clause
->type
)
885 return !strcasecmp(clause
->str
, str
);
887 return strcasecmp(clause
->str
, str
);
889 return 0>strcasecmp(clause
->str
, str
);
891 return 0>=strcasecmp(clause
->str
, str
);
893 return 0<strcasecmp(clause
->str
, str
);
895 return 0<=strcasecmp(clause
->str
, str
);
896 case clause_contains
:
897 return (strcasestr(str
, clause
->str
) != NULL
);
898 case clause_not_contains
:
899 return (strcasestr(str
, clause
->str
) == NULL
);
900 case clause_begins_with
:
901 return (strcasestr(str
, clause
->str
) == str
);
902 case clause_not_begins_with
:
903 return (strcasestr(str
, clause
->str
) != str
);
904 case clause_ends_with
:
905 return str_ends_with(str
, clause
->str
);
906 case clause_not_ends_with
:
907 return !str_ends_with(str
, clause
->str
);
909 return str_oneof(str
, clause
->str
);
912 logf("Incorrect tag: %d", clause
->type
);
919 static bool check_clauses(struct tagcache_search
*tcs
,
920 struct index_entry
*idx
,
921 struct tagcache_search_clause
**clause
, int count
)
925 #ifdef HAVE_TC_RAMCACHE
928 /* Go through all conditional clauses. */
929 for (i
= 0; i
< count
; i
++)
931 struct tagfile_entry
*tfe
;
936 seek
= check_virtual_tags(clause
[i
]->tag
, idx
);
938 if (!tagcache_is_numeric_tag(clause
[i
]->tag
))
940 if (clause
[i
]->tag
== tag_filename
)
942 retrieve(tcs
, idx
, tag_filename
, buf
, sizeof buf
);
947 tfe
= (struct tagfile_entry
*)&hdr
->tags
[clause
[i
]->tag
][seek
];
952 if (!check_against_clause(seek
, str
, clause
[i
]))
959 /* Check for conditions. */
960 for (i
= 0; i
< count
; i
++)
962 struct tagfile_entry tfe
;
966 seek
= check_virtual_tags(clause
[i
]->tag
, idx
);
968 memset(str
, 0, sizeof str
);
969 if (!tagcache_is_numeric_tag(clause
[i
]->tag
))
971 int fd
= tcs
->idxfd
[clause
[i
]->tag
];
972 lseek(fd
, seek
, SEEK_SET
);
973 ecread(fd
, &tfe
, 1, tagfile_entry_ec
, tc_stat
.econ
);
974 if (tfe
.tag_length
>= (int)sizeof(str
))
976 logf("Too long tag read!");
980 read(fd
, str
, tfe
.tag_length
);
982 /* Check if entry has been deleted. */
987 if (!check_against_clause(seek
, str
, clause
[i
]))
995 bool tagcache_check_clauses(struct tagcache_search
*tcs
,
996 struct tagcache_search_clause
**clause
, int count
)
998 struct index_entry idx
;
1003 if (!get_index(tcs
->masterfd
, tcs
->idx_id
, &idx
, true))
1006 return check_clauses(tcs
, &idx
, clause
, count
);
1009 static bool add_uniqbuf(struct tagcache_search
*tcs
, unsigned long id
)
1013 /* If uniq buffer is not defined we must return true for search to work. */
1014 if (tcs
->unique_list
== NULL
1015 || (!tagcache_is_unique_tag(tcs
->type
)
1016 && !tagcache_is_numeric_tag(tcs
->type
)))
1021 for (i
= 0; i
< tcs
->unique_list_count
; i
++)
1023 /* Return false if entry is found. */
1024 if (tcs
->unique_list
[i
] == id
)
1028 if (tcs
->unique_list_count
< tcs
->unique_list_capacity
)
1030 tcs
->unique_list
[i
] = id
;
1031 tcs
->unique_list_count
++;
1037 static bool build_lookup_list(struct tagcache_search
*tcs
)
1039 struct index_entry entry
;
1042 tcs
->seek_list_count
= 0;
1044 #ifdef HAVE_TC_RAMCACHE
1049 for (i
= tcs
->seek_pos
; i
< hdr
->h
.tch
.entry_count
; i
++)
1051 struct index_entry
*idx
= &hdr
->indices
[i
];
1052 if (tcs
->seek_list_count
== SEEK_LIST_SIZE
)
1055 /* Skip deleted files. */
1056 if (idx
->flag
& FLAG_DELETED
)
1059 /* Go through all filters.. */
1060 for (j
= 0; j
< tcs
->filter_count
; j
++)
1062 if (idx
->tag_seek
[tcs
->filter_tag
[j
]] != tcs
->filter_seek
[j
])
1068 if (j
< tcs
->filter_count
)
1071 /* Check for conditions. */
1072 if (!check_clauses(tcs
, idx
, tcs
->clause
, tcs
->clause_count
))
1075 /* Add to the seek list if not already in uniq buffer. */
1076 if (!add_uniqbuf(tcs
, idx
->tag_seek
[tcs
->type
]))
1080 tcs
->seek_list
[tcs
->seek_list_count
] = idx
->tag_seek
[tcs
->type
];
1081 tcs
->seek_flags
[tcs
->seek_list_count
] = idx
->flag
;
1082 tcs
->seek_list_count
++;
1087 return tcs
->seek_list_count
> 0;
1091 lseek(tcs
->masterfd
, tcs
->seek_pos
* sizeof(struct index_entry
) +
1092 sizeof(struct master_header
), SEEK_SET
);
1094 while (ecread(tcs
->masterfd
, &entry
, 1, index_entry_ec
, tc_stat
.econ
)
1095 == sizeof(struct index_entry
))
1097 if (tcs
->seek_list_count
== SEEK_LIST_SIZE
)
1102 /* Check if entry has been deleted. */
1103 if (entry
.flag
& FLAG_DELETED
)
1106 /* Go through all filters.. */
1107 for (i
= 0; i
< tcs
->filter_count
; i
++)
1109 if (entry
.tag_seek
[tcs
->filter_tag
[i
]] != tcs
->filter_seek
[i
])
1113 if (i
< tcs
->filter_count
)
1116 /* Check for conditions. */
1117 if (!check_clauses(tcs
, &entry
, tcs
->clause
, tcs
->clause_count
))
1120 /* Add to the seek list if not already in uniq buffer. */
1121 if (!add_uniqbuf(tcs
, entry
.tag_seek
[tcs
->type
]))
1125 tcs
->seek_list
[tcs
->seek_list_count
] = entry
.tag_seek
[tcs
->type
];
1126 tcs
->seek_flags
[tcs
->seek_list_count
] = entry
.flag
;
1127 tcs
->seek_list_count
++;
1132 return tcs
->seek_list_count
> 0;
1136 static void remove_files(void)
1141 tc_stat
.ready
= false;
1142 tc_stat
.ramcache
= false;
1143 tc_stat
.econ
= false;
1144 remove(TAGCACHE_FILE_MASTER
);
1145 for (i
= 0; i
< TAG_COUNT
; i
++)
1147 if (tagcache_is_numeric_tag(i
))
1150 snprintf(buf
, sizeof buf
, TAGCACHE_FILE_INDEX
, i
);
1156 static bool check_all_headers(void)
1158 struct master_header myhdr
;
1159 struct tagcache_header tch
;
1163 if ( (fd
= open_master_fd(&myhdr
, false)) < 0)
1169 logf("tagcache is dirty!");
1173 memcpy(¤t_tcmh
, &myhdr
, sizeof(struct master_header
));
1175 for (tag
= 0; tag
< TAG_COUNT
; tag
++)
1177 if (tagcache_is_numeric_tag(tag
))
1180 if ( (fd
= open_tag_fd(&tch
, tag
, false)) < 0)
1189 bool tagcache_search(struct tagcache_search
*tcs
, int tag
)
1191 struct tagcache_header tag_hdr
;
1192 struct master_header master_hdr
;
1195 if (tcs
->initialized
)
1196 tagcache_search_finish(tcs
);
1201 memset(tcs
, 0, sizeof(struct tagcache_search
));
1202 if (tc_stat
.commit_step
> 0 || !tc_stat
.ready
)
1205 tcs
->position
= sizeof(struct tagcache_header
);
1208 tcs
->seek_list_count
= 0;
1209 tcs
->filter_count
= 0;
1212 for (i
= 0; i
< TAG_COUNT
; i
++)
1215 #ifndef HAVE_TC_RAMCACHE
1216 tcs
->ramsearch
= false;
1218 tcs
->ramsearch
= tc_stat
.ramcache
;
1221 tcs
->entry_count
= hdr
->entry_count
[tcs
->type
];
1226 if (!tagcache_is_numeric_tag(tcs
->type
))
1228 tcs
->idxfd
[tcs
->type
] = open_tag_fd(&tag_hdr
, tcs
->type
, false);
1229 if (tcs
->idxfd
[tcs
->type
] < 0)
1233 /* Always open as R/W so we can pass tcs to functions that modify data also
1234 * without failing. */
1235 tcs
->masterfd
= open_master_fd(&master_hdr
, true);
1237 if (tcs
->masterfd
< 0)
1242 tcs
->initialized
= true;
1248 void tagcache_search_set_uniqbuf(struct tagcache_search
*tcs
,
1249 void *buffer
, long length
)
1251 tcs
->unique_list
= (unsigned long *)buffer
;
1252 tcs
->unique_list_capacity
= length
/ sizeof(*tcs
->unique_list
);
1253 tcs
->unique_list_count
= 0;
1256 bool tagcache_search_add_filter(struct tagcache_search
*tcs
,
1259 if (tcs
->filter_count
== TAGCACHE_MAX_FILTERS
)
1262 if (!tagcache_is_unique_tag(tag
) || tagcache_is_numeric_tag(tag
))
1265 tcs
->filter_tag
[tcs
->filter_count
] = tag
;
1266 tcs
->filter_seek
[tcs
->filter_count
] = seek
;
1267 tcs
->filter_count
++;
1272 bool tagcache_search_add_clause(struct tagcache_search
*tcs
,
1273 struct tagcache_search_clause
*clause
)
1277 if (tcs
->clause_count
>= TAGCACHE_MAX_CLAUSES
)
1279 logf("Too many clauses");
1283 /* Check if there is already a similar filter in present (filters are
1284 * much faster than clauses).
1286 for (i
= 0; i
< tcs
->filter_count
; i
++)
1288 if (tcs
->filter_tag
[i
] == clause
->tag
)
1292 if (!tagcache_is_numeric_tag(clause
->tag
) && tcs
->idxfd
[clause
->tag
] < 0)
1296 snprintf(buf
, sizeof buf
, TAGCACHE_FILE_INDEX
, clause
->tag
);
1297 tcs
->idxfd
[clause
->tag
] = open(buf
, O_RDONLY
);
1300 tcs
->clause
[tcs
->clause_count
] = clause
;
1301 tcs
->clause_count
++;
1306 /* TODO: Remove this mess. */
1307 #ifdef HAVE_DIRCACHE
1308 #define TAG_FILENAME_RAM(tcs) ((tcs->type == tag_filename) \
1309 ? ((flag & FLAG_DIRCACHE) && is_dircache_intact()) : 1)
1311 #define TAG_FILENAME_RAM(tcs) (tcs->type != tag_filename)
1314 static bool get_next(struct tagcache_search
*tcs
)
1316 static char buf
[TAG_MAXLEN
+32];
1317 struct tagfile_entry entry
;
1320 if (!tcs
->valid
|| !tc_stat
.ready
)
1323 if (tcs
->idxfd
[tcs
->type
] < 0 && !tagcache_is_numeric_tag(tcs
->type
)
1324 #ifdef HAVE_TC_RAMCACHE
1330 /* Relative fetch. */
1331 if (tcs
->filter_count
> 0 || tcs
->clause_count
> 0
1332 || tagcache_is_numeric_tag(tcs
->type
))
1334 /* Check for end of list. */
1335 if (tcs
->seek_list_count
== 0)
1337 /* Try to fetch more. */
1338 if (!build_lookup_list(tcs
))
1345 tcs
->seek_list_count
--;
1346 flag
= tcs
->seek_flags
[tcs
->seek_list_count
];
1348 /* Seek stream to the correct position and continue to direct fetch. */
1349 if ((!tcs
->ramsearch
|| !TAG_FILENAME_RAM(tcs
))
1350 && !tagcache_is_numeric_tag(tcs
->type
))
1352 if (!open_files(tcs
, tcs
->type
))
1355 lseek(tcs
->idxfd
[tcs
->type
], tcs
->seek_list
[tcs
->seek_list_count
], SEEK_SET
);
1358 tcs
->position
= tcs
->seek_list
[tcs
->seek_list_count
];
1361 if (tagcache_is_numeric_tag(tcs
->type
))
1363 snprintf(buf
, sizeof(buf
), "%d", tcs
->position
);
1364 tcs
->result_seek
= tcs
->position
;
1366 tcs
->result_len
= strlen(buf
) + 1;
1371 #ifdef HAVE_TC_RAMCACHE
1372 if (tcs
->ramsearch
&& TAG_FILENAME_RAM(tcs
))
1374 struct tagfile_entry
*ep
;
1376 if (tcs
->entry_count
== 0)
1383 tcs
->result_seek
= tcs
->position
;
1385 # ifdef HAVE_DIRCACHE
1386 if (tcs
->type
== tag_filename
)
1388 dircache_copy_path((struct dirent
*)tcs
->position
,
1391 tcs
->result_len
= strlen(buf
) + 1;
1392 tcs
->idx_id
= FLAG_GET_ATTR(flag
);
1393 tcs
->ramresult
= false;
1399 ep
= (struct tagfile_entry
*)&hdr
->tags
[tcs
->type
][tcs
->position
];
1400 tcs
->position
+= sizeof(struct tagfile_entry
) + ep
->tag_length
;
1401 tcs
->result
= ep
->tag_data
;
1402 tcs
->result_len
= strlen(tcs
->result
) + 1;
1403 tcs
->idx_id
= ep
->idx_id
;
1404 tcs
->ramresult
= true;
1411 if (!open_files(tcs
, tcs
->type
))
1414 tcs
->result_seek
= lseek(tcs
->idxfd
[tcs
->type
], 0, SEEK_CUR
);
1415 if (ecread(tcs
->idxfd
[tcs
->type
], &entry
, 1,
1416 tagfile_entry_ec
, tc_stat
.econ
) != sizeof(struct tagfile_entry
))
1424 if (entry
.tag_length
> (long)sizeof(buf
))
1427 logf("too long tag #2");
1431 if (read(tcs
->idxfd
[tcs
->type
], buf
, entry
.tag_length
) != entry
.tag_length
)
1434 logf("read error #4");
1439 tcs
->result_len
= strlen(tcs
->result
) + 1;
1440 tcs
->idx_id
= entry
.idx_id
;
1441 tcs
->ramresult
= false;
1446 bool tagcache_get_next(struct tagcache_search
*tcs
)
1448 while (get_next(tcs
))
1450 if (tcs
->result_len
> 1)
1457 bool tagcache_retrieve(struct tagcache_search
*tcs
, int idxid
,
1458 int tag
, char *buf
, long size
)
1460 struct index_entry idx
;
1463 if (!get_index(tcs
->masterfd
, idxid
, &idx
, true))
1466 return retrieve(tcs
, &idx
, tag
, buf
, size
);
1469 static bool update_master_header(void)
1471 struct master_header myhdr
;
1477 if ( (fd
= open_master_fd(&myhdr
, true)) < 0)
1480 myhdr
.serial
= current_tcmh
.serial
;
1481 myhdr
.commitid
= current_tcmh
.commitid
;
1484 lseek(fd
, 0, SEEK_SET
);
1485 ecwrite(fd
, &myhdr
, 1, master_header_ec
, tc_stat
.econ
);
1488 #ifdef HAVE_TC_RAMCACHE
1491 hdr
->h
.serial
= current_tcmh
.serial
;
1492 hdr
->h
.commitid
= current_tcmh
.commitid
;
1501 void tagcache_modify(struct tagcache_search
*tcs
, int type
, const char *text
)
1503 struct tagentry
*entry
;
1505 if (tcs
->type
!= tag_title
)
1508 /* We will need reserve buffer for this. */
1511 struct tagfile_entry
*ep
;
1513 ep
= (struct tagfile_entry
*)&hdr
->tags
[tcs
->type
][tcs
->result_seek
];
1514 tcs
->seek_list
[tcs
->seek_list_count
];
1517 entry
= find_entry_ram();
1522 void tagcache_search_finish(struct tagcache_search
*tcs
)
1526 if (!tcs
->initialized
)
1529 if (tcs
->masterfd
>= 0)
1531 close(tcs
->masterfd
);
1535 for (i
= 0; i
< TAG_COUNT
; i
++)
1537 if (tcs
->idxfd
[i
] >= 0)
1539 close(tcs
->idxfd
[i
]);
1544 tcs
->ramsearch
= false;
1546 tcs
->initialized
= 0;
1551 #if defined(HAVE_TC_RAMCACHE) && defined(HAVE_DIRCACHE)
1552 static struct tagfile_entry
*get_tag(const struct index_entry
*entry
, int tag
)
1554 return (struct tagfile_entry
*)&hdr
->tags
[tag
][entry
->tag_seek
[tag
]];
1557 static long get_tag_numeric(const struct index_entry
*entry
, int tag
)
1559 return check_virtual_tags(tag
, entry
);
1562 static char* get_tag_string(const struct index_entry
*entry
, int tag
)
1564 char* s
= get_tag(entry
, tag
)->tag_data
;
1565 return strcmp(s
, UNTAGGED
) ? s
: NULL
;
1568 bool tagcache_fill_tags(struct mp3entry
*id3
, const char *filename
)
1570 struct index_entry
*entry
;
1576 /* Find the corresponding entry in tagcache. */
1577 idx_id
= find_entry_ram(filename
, NULL
);
1578 if (idx_id
< 0 || !tc_stat
.ramcache
)
1581 entry
= &hdr
->indices
[idx_id
];
1583 id3
->title
= get_tag_string(entry
, tag_title
);
1584 id3
->artist
= get_tag_string(entry
, tag_artist
);
1585 id3
->album
= get_tag_string(entry
, tag_album
);
1586 id3
->genre_string
= get_tag_string(entry
, tag_genre
);
1587 id3
->composer
= get_tag_string(entry
, tag_composer
);
1588 id3
->comment
= get_tag_string(entry
, tag_comment
);
1589 id3
->albumartist
= get_tag_string(entry
, tag_albumartist
);
1590 id3
->grouping
= get_tag_string(entry
, tag_grouping
);
1592 id3
->playcount
= get_tag_numeric(entry
, tag_playcount
);
1593 id3
->rating
= get_tag_numeric(entry
, tag_rating
);
1594 id3
->lastplayed
= get_tag_numeric(entry
, tag_lastplayed
);
1595 id3
->score
= get_tag_numeric(entry
, tag_virt_autoscore
) / 10;
1596 id3
->year
= get_tag_numeric(entry
, tag_year
);
1598 id3
->discnum
= get_tag_numeric(entry
, tag_discnumber
);
1599 id3
->tracknum
= get_tag_numeric(entry
, tag_tracknumber
);
1600 id3
->bitrate
= get_tag_numeric(entry
, tag_bitrate
);
1601 if (id3
->bitrate
== 0)
1608 static inline void write_item(const char *item
)
1610 int len
= strlen(item
) + 1;
1613 write(cachefd
, item
, len
);
1616 static int check_if_empty(char **tag
)
1620 if (*tag
== NULL
|| **tag
== '\0')
1623 return sizeof(UNTAGGED
); /* Tag length */
1626 length
= strlen(*tag
);
1627 if (length
> TAG_MAXLEN
)
1629 logf("over length tag: %s", *tag
);
1630 length
= TAG_MAXLEN
;
1631 (*tag
)[length
] = '\0';
1637 #define ADD_TAG(entry,tag,data) \
1639 entry.tag_offset[tag] = offset; \
1640 entry.tag_length[tag] = check_if_empty(data); \
1641 offset += entry.tag_length[tag]
1643 static void add_tagcache(char *path
, unsigned long mtime
1644 #if defined(HAVE_TC_RAMCACHE) && defined(HAVE_DIRCACHE)
1645 ,const struct dirent
*dc
1649 struct mp3entry id3
;
1650 struct temp_file_entry entry
;
1654 char tracknumfix
[3];
1656 int path_length
= strlen(path
);
1657 bool has_albumartist
;
1663 /* Check for overlength file path. */
1664 if (path_length
> TAG_MAXLEN
)
1666 /* Path can't be shortened. */
1667 logf("Too long path: %s", path
);
1671 /* Check if the file is supported. */
1672 if (probe_file_format(path
) == AFMT_UNKNOWN
)
1675 /* Check if the file is already cached. */
1676 #if defined(HAVE_TC_RAMCACHE) && defined(HAVE_DIRCACHE)
1677 if (tc_stat
.ramcache
&& is_dircache_intact())
1679 idx_id
= find_entry_ram(path
, dc
);
1684 if (filenametag_fd
>= 0)
1686 idx_id
= find_entry_disk(path
);
1690 /* Check if file has been modified. */
1693 struct index_entry idx
;
1695 if (!get_index(-1, idx_id
, &idx
, true))
1697 logf("failed to retrieve index entry");
1701 if ((unsigned long)idx
.tag_seek
[tag_mtime
] == mtime
)
1703 /* No changes to file. */
1707 /* Metadata might have been changed. Delete the entry. */
1708 logf("Re-adding: %s", path
);
1709 if (!delete_entry(idx_id
))
1711 logf("delete_entry failed: %d", idx_id
);
1716 fd
= open(path
, O_RDONLY
);
1719 logf("open fail: %s", path
);
1723 memset(&id3
, 0, sizeof(struct mp3entry
));
1724 memset(&entry
, 0, sizeof(struct temp_file_entry
));
1725 memset(&tracknumfix
, 0, sizeof(tracknumfix
));
1726 ret
= get_metadata(&id3
, fd
, path
);
1732 logf("-> %s", path
);
1734 /* Generate track number if missing. */
1735 if (id3
.tracknum
<= 0)
1737 const char *p
= strrchr(path
, '.');
1740 p
= &path
[strlen(path
)-1];
1744 if (isdigit(*p
) && isdigit(*(p
-1)))
1746 tracknumfix
[1] = *p
--;
1747 tracknumfix
[0] = *p
;
1753 if (tracknumfix
[0] != '\0')
1755 id3
.tracknum
= atoi(tracknumfix
);
1756 /* Set a flag to indicate track number has been generated. */
1757 entry
.flag
|= FLAG_TRKNUMGEN
;
1761 /* Unable to generate track number. */
1767 entry
.tag_offset
[tag_year
] = id3
.year
;
1768 entry
.tag_offset
[tag_discnumber
] = id3
.discnum
;
1769 entry
.tag_offset
[tag_tracknumber
] = id3
.tracknum
;
1770 entry
.tag_offset
[tag_length
] = id3
.length
;
1771 entry
.tag_offset
[tag_bitrate
] = id3
.bitrate
;
1772 entry
.tag_offset
[tag_mtime
] = mtime
;
1775 has_albumartist
= id3
.albumartist
!= NULL
1776 && strlen(id3
.albumartist
) > 0;
1777 has_grouping
= id3
.grouping
!= NULL
1778 && strlen(id3
.grouping
) > 0;
1780 ADD_TAG(entry
, tag_filename
, &path
);
1781 ADD_TAG(entry
, tag_title
, &id3
.title
);
1782 ADD_TAG(entry
, tag_artist
, &id3
.artist
);
1783 ADD_TAG(entry
, tag_album
, &id3
.album
);
1784 ADD_TAG(entry
, tag_genre
, &id3
.genre_string
);
1785 ADD_TAG(entry
, tag_composer
, &id3
.composer
);
1786 ADD_TAG(entry
, tag_comment
, &id3
.comment
);
1787 if (has_albumartist
)
1789 ADD_TAG(entry
, tag_albumartist
, &id3
.albumartist
);
1793 ADD_TAG(entry
, tag_albumartist
, &id3
.artist
);
1797 ADD_TAG(entry
, tag_grouping
, &id3
.grouping
);
1801 ADD_TAG(entry
, tag_grouping
, &id3
.title
);
1803 entry
.data_length
= offset
;
1805 /* Write the header */
1806 write(cachefd
, &entry
, sizeof(struct temp_file_entry
));
1808 /* And tags also... Correct order is critical */
1810 write_item(id3
.title
);
1811 write_item(id3
.artist
);
1812 write_item(id3
.album
);
1813 write_item(id3
.genre_string
);
1814 write_item(id3
.composer
);
1815 write_item(id3
.comment
);
1816 if (has_albumartist
)
1818 write_item(id3
.albumartist
);
1822 write_item(id3
.artist
);
1826 write_item(id3
.grouping
);
1830 write_item(id3
.title
);
1832 total_entry_count
++;
1835 static bool tempbuf_insert(char *str
, int id
, int idx_id
, bool unique
)
1837 struct tempbuf_searchidx
*index
= (struct tempbuf_searchidx
*)tempbuf
;
1838 int len
= strlen(str
)+1;
1841 unsigned *crcbuf
= (unsigned *)&tempbuf
[tempbuf_size
-4];
1842 char buf
[TAG_MAXLEN
+32];
1844 for (i
= 0; str
[i
] != '\0' && i
< (int)sizeof(buf
)-1; i
++)
1845 buf
[i
] = tolower(str
[i
]);
1848 crc32
= crc_32(buf
, i
, 0xffffffff);
1852 /* Check if the crc does not exist -> entry does not exist for sure. */
1853 for (i
= 0; i
< tempbufidx
; i
++)
1855 if (crcbuf
[-i
] != crc32
)
1858 if (!strcasecmp(str
, index
[i
].str
))
1860 if (id
< 0 || id
>= lookup_buffer_depth
)
1862 logf("lookup buf overf.: %d", id
);
1866 lookup
[id
] = &index
[i
];
1872 /* Insert to CRC buffer. */
1873 crcbuf
[-tempbufidx
] = crc32
;
1876 /* Insert it to the buffer. */
1877 tempbuf_left
-= len
;
1878 if (tempbuf_left
- 4 < 0 || tempbufidx
>= commit_entry_count
-1)
1881 if (id
>= lookup_buffer_depth
)
1883 logf("lookup buf overf. #2: %d", id
);
1889 lookup
[id
] = &index
[tempbufidx
];
1890 index
[tempbufidx
].idlist
.id
= id
;
1893 index
[tempbufidx
].idlist
.id
= -1;
1895 index
[tempbufidx
].idlist
.next
= NULL
;
1896 index
[tempbufidx
].idx_id
= idx_id
;
1897 index
[tempbufidx
].seek
= -1;
1898 index
[tempbufidx
].str
= &tempbuf
[tempbuf_pos
];
1899 memcpy(index
[tempbufidx
].str
, str
, len
);
1906 static int compare(const void *p1
, const void *p2
)
1910 struct tempbuf_searchidx
*e1
= (struct tempbuf_searchidx
*)p1
;
1911 struct tempbuf_searchidx
*e2
= (struct tempbuf_searchidx
*)p2
;
1913 if (strcmp(e1
->str
, UNTAGGED
) == 0)
1915 if (strcmp(e2
->str
, UNTAGGED
) == 0)
1919 else if (strcmp(e2
->str
, UNTAGGED
) == 0)
1922 return strncasecmp(e1
->str
, e2
->str
, TAG_MAXLEN
);
1925 static int tempbuf_sort(int fd
)
1927 struct tempbuf_searchidx
*index
= (struct tempbuf_searchidx
*)tempbuf
;
1928 struct tagfile_entry fe
;
1932 /* Generate reverse lookup entries. */
1933 for (i
= 0; i
< lookup_buffer_depth
; i
++)
1935 struct tempbuf_id_list
*idlist
;
1940 if (lookup
[i
]->idlist
.id
== i
)
1943 idlist
= &lookup
[i
]->idlist
;
1944 while (idlist
->next
!= NULL
)
1945 idlist
= idlist
->next
;
1947 tempbuf_left
-= sizeof(struct tempbuf_id_list
);
1948 if (tempbuf_left
- 4 < 0)
1951 idlist
->next
= (struct tempbuf_id_list
*)&tempbuf
[tempbuf_pos
];
1952 if (tempbuf_pos
& 0x03)
1954 tempbuf_pos
= (tempbuf_pos
& ~0x03) + 0x04;
1956 idlist
->next
= (struct tempbuf_id_list
*)&tempbuf
[tempbuf_pos
];
1958 tempbuf_pos
+= sizeof(struct tempbuf_id_list
);
1960 idlist
= idlist
->next
;
1962 idlist
->next
= NULL
;
1967 qsort(index
, tempbufidx
, sizeof(struct tempbuf_searchidx
), compare
);
1968 memset(lookup
, 0, lookup_buffer_depth
* sizeof(struct tempbuf_searchidx
**));
1970 for (i
= 0; i
< tempbufidx
; i
++)
1972 struct tempbuf_id_list
*idlist
= &index
[i
].idlist
;
1974 /* Fix the lookup list. */
1975 while (idlist
!= NULL
)
1977 if (idlist
->id
>= 0)
1978 lookup
[idlist
->id
] = &index
[i
];
1979 idlist
= idlist
->next
;
1982 index
[i
].seek
= lseek(fd
, 0, SEEK_CUR
);
1983 length
= strlen(index
[i
].str
) + 1;
1984 fe
.tag_length
= length
;
1985 fe
.idx_id
= index
[i
].idx_id
;
1987 /* Check the chunk alignment. */
1988 if ((fe
.tag_length
+ sizeof(struct tagfile_entry
))
1989 % TAGFILE_ENTRY_CHUNK_LENGTH
)
1991 fe
.tag_length
+= TAGFILE_ENTRY_CHUNK_LENGTH
-
1992 ((fe
.tag_length
+ sizeof(struct tagfile_entry
))
1993 % TAGFILE_ENTRY_CHUNK_LENGTH
);
1996 #ifdef TAGCACHE_STRICT_ALIGN
1997 /* Make sure the entry is long aligned. */
1998 if (index
[i
].seek
& 0x03)
2000 logf("tempbuf_sort: alignment error!");
2005 if (ecwrite(fd
, &fe
, 1, tagfile_entry_ec
, tc_stat
.econ
) !=
2006 sizeof(struct tagfile_entry
))
2008 logf("tempbuf_sort: write error #1");
2012 if (write(fd
, index
[i
].str
, length
) != length
)
2014 logf("tempbuf_sort: write error #2");
2018 /* Write some padding. */
2019 if (fe
.tag_length
- length
> 0)
2020 write(fd
, "XXXXXXXX", fe
.tag_length
- length
);
2026 inline static struct tempbuf_searchidx
* tempbuf_locate(int id
)
2028 if (id
< 0 || id
>= lookup_buffer_depth
)
2035 inline static int tempbuf_find_location(int id
)
2037 struct tempbuf_searchidx
*entry
;
2039 entry
= tempbuf_locate(id
);
2046 static bool build_numeric_indices(struct tagcache_header
*h
, int tmpfd
)
2048 struct master_header tcmh
;
2049 struct index_entry idx
;
2052 struct temp_file_entry
*entrybuf
= (struct temp_file_entry
*)tempbuf
;
2054 int entries_processed
= 0;
2056 char buf
[TAG_MAXLEN
];
2058 max_entries
= tempbuf_size
/ sizeof(struct temp_file_entry
) - 1;
2060 logf("Building numeric indices...");
2061 lseek(tmpfd
, sizeof(struct tagcache_header
), SEEK_SET
);
2063 if ( (masterfd
= open_master_fd(&tcmh
, true)) < 0)
2066 masterfd_pos
= lseek(masterfd
, tcmh
.tch
.entry_count
* sizeof(struct index_entry
),
2068 if (masterfd_pos
== filesize(masterfd
))
2070 logf("we can't append!");
2075 while (entries_processed
< h
->entry_count
)
2077 int count
= MIN(h
->entry_count
- entries_processed
, max_entries
);
2079 /* Read in as many entries as possible. */
2080 for (i
= 0; i
< count
; i
++)
2082 struct temp_file_entry
*tfe
= &entrybuf
[i
];
2085 /* Read in numeric data. */
2086 if (read(tmpfd
, tfe
, sizeof(struct temp_file_entry
)) !=
2087 sizeof(struct temp_file_entry
))
2089 logf("read fail #1");
2094 datastart
= lseek(tmpfd
, 0, SEEK_CUR
);
2097 * Read string data from the following tags:
2103 * A crc32 hash is calculated from the read data
2104 * and stored back to the data offset field kept in memory.
2106 #define tmpdb_read_string_tag(tag) \
2107 lseek(tmpfd, tfe->tag_offset[tag], SEEK_CUR); \
2108 if ((unsigned long)tfe->tag_length[tag] > sizeof buf) \
2110 logf("read fail: buffer overflow"); \
2115 if (read(tmpfd, buf, tfe->tag_length[tag]) != \
2116 tfe->tag_length[tag]) \
2118 logf("read fail #2"); \
2123 tfe->tag_offset[tag] = crc_32(buf, strlen(buf), 0xffffffff); \
2124 lseek(tmpfd, datastart, SEEK_SET)
2126 tmpdb_read_string_tag(tag_filename
);
2127 tmpdb_read_string_tag(tag_artist
);
2128 tmpdb_read_string_tag(tag_album
);
2129 tmpdb_read_string_tag(tag_title
);
2131 /* Seek to the end of the string data. */
2132 lseek(tmpfd
, tfe
->data_length
, SEEK_CUR
);
2135 /* Backup the master index position. */
2136 masterfd_pos
= lseek(masterfd
, 0, SEEK_CUR
);
2137 lseek(masterfd
, sizeof(struct master_header
), SEEK_SET
);
2139 /* Check if we can resurrect some deleted runtime statistics data. */
2140 for (i
= 0; i
< tcmh
.tch
.entry_count
; i
++)
2142 /* Read the index entry. */
2143 if (ecread(masterfd
, &idx
, 1, index_entry_ec
, tc_stat
.econ
)
2144 != sizeof(struct index_entry
))
2146 logf("read fail #3");
2152 * Skip unless the entry is marked as being deleted
2153 * or the data has already been resurrected.
2155 if (!(idx
.flag
& FLAG_DELETED
) || idx
.flag
& FLAG_RESURRECTED
)
2158 /* Now try to match the entry. */
2160 * To succesfully match a song, the following conditions
2163 * For numeric fields: tag_length
2164 * - Full identical match is required
2166 * If tag_filename matches, no further checking necessary.
2168 * For string hashes: tag_artist, tag_album, tag_title
2169 * - Two of these must match
2171 for (j
= 0; j
< count
; j
++)
2173 struct temp_file_entry
*tfe
= &entrybuf
[j
];
2175 /* Try to match numeric fields first. */
2176 if (tfe
->tag_offset
[tag_length
] != idx
.tag_seek
[tag_length
])
2179 /* Now it's time to do the hash matching. */
2180 if (tfe
->tag_offset
[tag_filename
] != idx
.tag_seek
[tag_filename
])
2182 int match_count
= 0;
2184 /* No filename match, check if we can match two other tags. */
2185 #define tmpdb_match(tag) \
2186 if (tfe->tag_offset[tag] == idx.tag_seek[tag]) \
2189 tmpdb_match(tag_artist
);
2190 tmpdb_match(tag_album
);
2191 tmpdb_match(tag_title
);
2193 if (match_count
< 2)
2195 /* Still no match found, give up. */
2200 /* A match found, now copy & resurrect the statistical data. */
2201 #define tmpdb_copy_tag(tag) \
2202 tfe->tag_offset[tag] = idx.tag_seek[tag]
2204 tmpdb_copy_tag(tag_playcount
);
2205 tmpdb_copy_tag(tag_rating
);
2206 tmpdb_copy_tag(tag_playtime
);
2207 tmpdb_copy_tag(tag_lastplayed
);
2208 tmpdb_copy_tag(tag_commitid
);
2210 /* Avoid processing this entry again. */
2211 idx
.flag
|= FLAG_RESURRECTED
;
2213 lseek(masterfd
, -sizeof(struct index_entry
), SEEK_CUR
);
2214 if (ecwrite(masterfd
, &idx
, 1, index_entry_ec
, tc_stat
.econ
)
2215 != sizeof(struct index_entry
))
2217 logf("masterfd writeback fail #1");
2222 logf("Entry resurrected");
2227 /* Restore the master index position. */
2228 lseek(masterfd
, masterfd_pos
, SEEK_SET
);
2230 /* Commit the data to the index. */
2231 for (i
= 0; i
< count
; i
++)
2233 int loc
= lseek(masterfd
, 0, SEEK_CUR
);
2235 if (ecread(masterfd
, &idx
, 1, index_entry_ec
, tc_stat
.econ
)
2236 != sizeof(struct index_entry
))
2238 logf("read fail #3");
2243 for (j
= 0; j
< TAG_COUNT
; j
++)
2245 if (!tagcache_is_numeric_tag(j
))
2248 idx
.tag_seek
[j
] = entrybuf
[i
].tag_offset
[j
];
2250 idx
.flag
= entrybuf
[i
].flag
;
2252 if (idx
.tag_seek
[tag_commitid
])
2254 /* Data has been resurrected. */
2255 idx
.flag
|= FLAG_DIRTYNUM
;
2257 else if (tc_stat
.ready
&& current_tcmh
.commitid
> 0)
2259 idx
.tag_seek
[tag_commitid
] = current_tcmh
.commitid
;
2260 idx
.flag
|= FLAG_DIRTYNUM
;
2263 /* Write back the updated index. */
2264 lseek(masterfd
, loc
, SEEK_SET
);
2265 if (ecwrite(masterfd
, &idx
, 1, index_entry_ec
, tc_stat
.econ
)
2266 != sizeof(struct index_entry
))
2274 entries_processed
+= count
;
2275 logf("%d/%ld entries processed", entries_processed
, h
->entry_count
);
2286 * == 0 temporary failure
2289 static int build_index(int index_type
, struct tagcache_header
*h
, int tmpfd
)
2292 struct tagcache_header tch
;
2293 struct master_header tcmh
;
2294 struct index_entry idxbuf
[IDX_BUF_DEPTH
];
2296 char buf
[TAG_MAXLEN
+32];
2297 int fd
= -1, masterfd
;
2302 logf("Building index: %d", index_type
);
2304 /* Check the number of entries we need to allocate ram for. */
2305 commit_entry_count
= h
->entry_count
+ 1;
2307 masterfd
= open_master_fd(&tcmh
, false);
2310 commit_entry_count
+= tcmh
.tch
.entry_count
;
2314 remove_files(); /* Just to be sure we are clean. */
2316 /* Open the index file, which contains the tag names. */
2317 fd
= open_tag_fd(&tch
, index_type
, true);
2320 logf("tch.datasize=%ld", tch
.datasize
);
2321 lookup_buffer_depth
= 1 +
2322 /* First part */ commit_entry_count
+
2323 /* Second part */ (tch
.datasize
/ TAGFILE_ENTRY_CHUNK_LENGTH
);
2327 lookup_buffer_depth
= 1 +
2328 /* First part */ commit_entry_count
+
2329 /* Second part */ 0;
2332 logf("lookup_buffer_depth=%ld", lookup_buffer_depth
);
2333 logf("commit_entry_count=%ld", commit_entry_count
);
2335 /* Allocate buffer for all index entries from both old and new
2338 tempbuf_pos
= commit_entry_count
* sizeof(struct tempbuf_searchidx
);
2340 /* Allocate lookup buffer. The first portion of commit_entry_count
2341 * contains the new tags in the temporary file and the second
2342 * part for locating entries already in the db.
2345 * +---------+---------------------------+
2346 * | index | position/ENTRY_CHUNK_SIZE | lookup buffer
2347 * +---------+---------------------------+
2349 * Old tags are inserted to a temporary buffer with position:
2350 * tempbuf_insert(position/ENTRY_CHUNK_SIZE, ...);
2351 * And new tags with index:
2352 * tempbuf_insert(idx, ...);
2354 * The buffer is sorted and written into tag file:
2355 * tempbuf_sort(...);
2356 * leaving master index locations messed up.
2358 * That is fixed using the lookup buffer for old tags:
2359 * new_seek = tempbuf_find_location(old_seek, ...);
2361 * new_seek = tempbuf_find_location(idx);
2363 lookup
= (struct tempbuf_searchidx
**)&tempbuf
[tempbuf_pos
];
2364 tempbuf_pos
+= lookup_buffer_depth
* sizeof(void **);
2365 memset(lookup
, 0, lookup_buffer_depth
* sizeof(void **));
2367 /* And calculate the remaining data space used mainly for storing
2368 * tag data (strings). */
2369 tempbuf_left
= tempbuf_size
- tempbuf_pos
- 8;
2370 if (tempbuf_left
- TAGFILE_ENTRY_AVG_LENGTH
* commit_entry_count
< 0)
2372 logf("Buffer way too small!");
2379 * If tag file contains unique tags (sorted index), we will load
2380 * it entirely into memory so we can resort it later for use with
2383 if (tagcache_is_sorted_tag(index_type
))
2385 logf("loading tags...");
2386 for (i
= 0; i
< tch
.entry_count
; i
++)
2388 struct tagfile_entry entry
;
2389 int loc
= lseek(fd
, 0, SEEK_CUR
);
2392 if (ecread(fd
, &entry
, 1, tagfile_entry_ec
, tc_stat
.econ
)
2393 != sizeof(struct tagfile_entry
))
2395 logf("read error #7");
2400 if (entry
.tag_length
>= (int)sizeof(buf
))
2402 logf("too long tag #3");
2407 if (read(fd
, buf
, entry
.tag_length
) != entry
.tag_length
)
2409 logf("read error #8");
2414 /* Skip deleted entries. */
2419 * Save the tag and tag id in the memory buffer. Tag id
2420 * is saved so we can later reindex the master lookup
2421 * table when the index gets resorted.
2423 ret
= tempbuf_insert(buf
, loc
/TAGFILE_ENTRY_CHUNK_LENGTH
2424 + commit_entry_count
, entry
.idx_id
,
2425 tagcache_is_unique_tag(index_type
));
2436 tempbufidx
= tch
.entry_count
;
2441 * Creating new index file to store the tags. No need to preload
2442 * anything whether the index type is sorted or not.
2444 snprintf(buf
, sizeof buf
, TAGCACHE_FILE_INDEX
, index_type
);
2445 fd
= open(buf
, O_WRONLY
| O_CREAT
| O_TRUNC
);
2448 logf("%s open fail", buf
);
2452 tch
.magic
= TAGCACHE_MAGIC
;
2453 tch
.entry_count
= 0;
2456 if (ecwrite(fd
, &tch
, 1, tagcache_header_ec
, tc_stat
.econ
)
2457 != sizeof(struct tagcache_header
))
2459 logf("header write failed");
2465 /* Loading the tag lookup file as "master file". */
2466 logf("Loading index file");
2467 masterfd
= open(TAGCACHE_FILE_MASTER
, O_RDWR
);
2471 logf("Creating new DB");
2472 masterfd
= open(TAGCACHE_FILE_MASTER
, O_WRONLY
| O_CREAT
| O_TRUNC
);
2476 logf("Failure to create index file (%s)", TAGCACHE_FILE_MASTER
);
2481 /* Write the header (write real values later). */
2482 memset(&tcmh
, 0, sizeof(struct master_header
));
2484 tcmh
.tch
.entry_count
= 0;
2485 tcmh
.tch
.datasize
= 0;
2487 ecwrite(masterfd
, &tcmh
, 1, master_header_ec
, tc_stat
.econ
);
2489 masterfd_pos
= lseek(masterfd
, 0, SEEK_CUR
);
2494 * Master file already exists so we need to process the current
2499 if (ecread(masterfd
, &tcmh
, 1, master_header_ec
, tc_stat
.econ
) !=
2500 sizeof(struct master_header
) || tcmh
.tch
.magic
!= TAGCACHE_MAGIC
)
2502 logf("header error");
2509 * If we reach end of the master file, we need to expand it to
2510 * hold new tags. If the current index is not sorted, we can
2511 * simply append new data to end of the file.
2512 * However, if the index is sorted, we need to update all tag
2513 * pointers in the master file for the current index.
2515 masterfd_pos
= lseek(masterfd
, tcmh
.tch
.entry_count
* sizeof(struct index_entry
),
2517 if (masterfd_pos
== filesize(masterfd
))
2519 logf("appending...");
2525 * Load new unique tags in memory to be sorted later and added
2526 * to the master lookup file.
2528 if (tagcache_is_sorted_tag(index_type
))
2530 lseek(tmpfd
, sizeof(struct tagcache_header
), SEEK_SET
);
2531 /* h is the header of the temporary file containing new tags. */
2532 logf("inserting new tags...");
2533 for (i
= 0; i
< h
->entry_count
; i
++)
2535 struct temp_file_entry entry
;
2537 if (read(tmpfd
, &entry
, sizeof(struct temp_file_entry
)) !=
2538 sizeof(struct temp_file_entry
))
2540 logf("read fail #3");
2546 if (entry
.tag_length
[index_type
] >= (long)sizeof(buf
))
2548 logf("too long entry!");
2553 lseek(tmpfd
, entry
.tag_offset
[index_type
], SEEK_CUR
);
2554 if (read(tmpfd
, buf
, entry
.tag_length
[index_type
]) !=
2555 entry
.tag_length
[index_type
])
2557 logf("read fail #4");
2562 if (tagcache_is_unique_tag(index_type
))
2563 error
= !tempbuf_insert(buf
, i
, -1, true);
2565 error
= !tempbuf_insert(buf
, i
, tcmh
.tch
.entry_count
+ i
, false);
2569 logf("insert error");
2574 lseek(tmpfd
, entry
.data_length
- entry
.tag_offset
[index_type
] -
2575 entry
.tag_length
[index_type
], SEEK_CUR
);
2580 /* Sort the buffer data and write it to the index file. */
2581 lseek(fd
, sizeof(struct tagcache_header
), SEEK_SET
);
2582 i
= tempbuf_sort(fd
);
2585 logf("sorted %d tags", i
);
2588 * Now update all indexes in the master lookup file.
2590 logf("updating indices...");
2591 lseek(masterfd
, sizeof(struct master_header
), SEEK_SET
);
2592 for (i
= 0; i
< tcmh
.tch
.entry_count
; i
+= idxbuf_pos
)
2595 int loc
= lseek(masterfd
, 0, SEEK_CUR
);
2597 idxbuf_pos
= MIN(tcmh
.tch
.entry_count
- i
, IDX_BUF_DEPTH
);
2599 if (ecread(masterfd
, idxbuf
, idxbuf_pos
, index_entry_ec
, tc_stat
.econ
)
2600 != (int)sizeof(struct index_entry
)*idxbuf_pos
)
2602 logf("read fail #5");
2606 lseek(masterfd
, loc
, SEEK_SET
);
2608 for (j
= 0; j
< idxbuf_pos
; j
++)
2610 if (idxbuf
[j
].flag
& FLAG_DELETED
)
2612 /* We can just ignore deleted entries. */
2613 // idxbuf[j].tag_seek[index_type] = 0;
2617 idxbuf
[j
].tag_seek
[index_type
] = tempbuf_find_location(
2618 idxbuf
[j
].tag_seek
[index_type
]/TAGFILE_ENTRY_CHUNK_LENGTH
2619 + commit_entry_count
);
2621 if (idxbuf
[j
].tag_seek
[index_type
] < 0)
2623 logf("update error: %d/%d/%ld",
2624 idxbuf
[j
].flag
, i
+j
, tcmh
.tch
.entry_count
);
2632 /* Write back the updated index. */
2633 if (ecwrite(masterfd
, idxbuf
, idxbuf_pos
,
2634 index_entry_ec
, tc_stat
.econ
) !=
2635 (int)sizeof(struct index_entry
)*idxbuf_pos
)
2646 * Walk through the temporary file containing the new tags.
2648 // build_normal_index(h, tmpfd, masterfd, idx);
2649 logf("updating new indices...");
2650 lseek(masterfd
, masterfd_pos
, SEEK_SET
);
2651 lseek(tmpfd
, sizeof(struct tagcache_header
), SEEK_SET
);
2652 lseek(fd
, 0, SEEK_END
);
2653 for (i
= 0; i
< h
->entry_count
; i
+= idxbuf_pos
)
2657 idxbuf_pos
= MIN(h
->entry_count
- i
, IDX_BUF_DEPTH
);
2660 memset(idxbuf
, 0, sizeof(struct index_entry
)*IDX_BUF_DEPTH
);
2664 int loc
= lseek(masterfd
, 0, SEEK_CUR
);
2666 if (ecread(masterfd
, idxbuf
, idxbuf_pos
, index_entry_ec
, tc_stat
.econ
)
2667 != (int)sizeof(struct index_entry
)*idxbuf_pos
)
2669 logf("read fail #6");
2673 lseek(masterfd
, loc
, SEEK_SET
);
2676 /* Read entry headers. */
2677 for (j
= 0; j
< idxbuf_pos
; j
++)
2679 if (!tagcache_is_sorted_tag(index_type
))
2681 struct temp_file_entry entry
;
2682 struct tagfile_entry fe
;
2684 if (read(tmpfd
, &entry
, sizeof(struct temp_file_entry
)) !=
2685 sizeof(struct temp_file_entry
))
2687 logf("read fail #7");
2693 if (entry
.tag_length
[index_type
] >= (int)sizeof(buf
))
2695 logf("too long entry!");
2696 logf("length=%d", entry
.tag_length
[index_type
]);
2697 logf("pos=0x%02lx", lseek(tmpfd
, 0, SEEK_CUR
));
2702 lseek(tmpfd
, entry
.tag_offset
[index_type
], SEEK_CUR
);
2703 if (read(tmpfd
, buf
, entry
.tag_length
[index_type
]) !=
2704 entry
.tag_length
[index_type
])
2706 logf("read fail #8");
2707 logf("offset=0x%02lx", entry
.tag_offset
[index_type
]);
2708 logf("length=0x%02x", entry
.tag_length
[index_type
]);
2713 /* Write to index file. */
2714 idxbuf
[j
].tag_seek
[index_type
] = lseek(fd
, 0, SEEK_CUR
);
2715 fe
.tag_length
= entry
.tag_length
[index_type
];
2716 fe
.idx_id
= tcmh
.tch
.entry_count
+ i
+ j
;
2717 ecwrite(fd
, &fe
, 1, tagfile_entry_ec
, tc_stat
.econ
);
2718 write(fd
, buf
, fe
.tag_length
);
2722 lseek(tmpfd
, entry
.data_length
- entry
.tag_offset
[index_type
] -
2723 entry
.tag_length
[index_type
], SEEK_CUR
);
2727 /* Locate the correct entry from the sorted array. */
2728 idxbuf
[j
].tag_seek
[index_type
] = tempbuf_find_location(i
+ j
);
2729 if (idxbuf
[j
].tag_seek
[index_type
] < 0)
2731 logf("entry not found (%d)", j
);
2739 if (ecwrite(masterfd
, idxbuf
, idxbuf_pos
,
2740 index_entry_ec
, tc_stat
.econ
) !=
2741 (int)sizeof(struct index_entry
)*idxbuf_pos
)
2743 logf("tagcache: write fail #4");
2752 /* Finally write the header. */
2753 tch
.magic
= TAGCACHE_MAGIC
;
2754 tch
.entry_count
= tempbufidx
;
2755 tch
.datasize
= lseek(fd
, 0, SEEK_END
) - sizeof(struct tagcache_header
);
2756 lseek(fd
, 0, SEEK_SET
);
2757 ecwrite(fd
, &tch
, 1, tagcache_header_ec
, tc_stat
.econ
);
2759 if (index_type
!= tag_filename
)
2760 h
->datasize
+= tch
.datasize
;
2761 logf("s:%d/%ld/%ld", index_type
, tch
.datasize
, h
->datasize
);
2773 static bool commit(void)
2775 struct tagcache_header tch
;
2776 struct master_header tcmh
;
2780 #ifdef HAVE_DIRCACHE
2781 bool dircache_buffer_stolen
= false;
2783 bool local_allocation
= false;
2785 logf("committing tagcache");
2790 tmpfd
= open(TAGCACHE_FILE_TEMP
, O_RDONLY
);
2793 logf("nothing to commit");
2798 /* Load the header. */
2799 len
= sizeof(struct tagcache_header
);
2800 rc
= read(tmpfd
, &tch
, len
);
2802 if (tch
.magic
!= TAGCACHE_MAGIC
|| rc
!= len
)
2804 logf("incorrect header");
2806 remove(TAGCACHE_FILE_TEMP
);
2810 if (tch
.entry_count
== 0)
2812 logf("nothing to commit");
2814 remove(TAGCACHE_FILE_TEMP
);
2818 #ifdef HAVE_EEPROM_SETTINGS
2819 remove(TAGCACHE_STATEFILE
);
2822 /* At first be sure to unload the ramcache! */
2823 #ifdef HAVE_TC_RAMCACHE
2824 tc_stat
.ramcache
= false;
2829 /* Try to steal every buffer we can :) */
2830 if (tempbuf_size
== 0)
2831 local_allocation
= true;
2833 #ifdef HAVE_DIRCACHE
2834 if (tempbuf_size
== 0)
2836 /* Try to steal the dircache buffer. */
2837 tempbuf
= dircache_steal_buffer(&tempbuf_size
);
2838 tempbuf_size
&= ~0x03;
2840 if (tempbuf_size
> 0)
2842 dircache_buffer_stolen
= true;
2847 #ifdef HAVE_TC_RAMCACHE
2848 if (tempbuf_size
== 0 && tc_stat
.ramcache_allocated
> 0)
2850 tempbuf
= (char *)(hdr
+ 1);
2851 tempbuf_size
= tc_stat
.ramcache_allocated
- sizeof(struct ramcache_header
) - 128;
2852 tempbuf_size
&= ~0x03;
2856 /* And finally fail if there are no buffers available. */
2857 if (tempbuf_size
== 0)
2859 logf("delaying commit until next boot");
2860 tc_stat
.commit_delayed
= true;
2866 logf("commit %ld entries...", tch
.entry_count
);
2868 /* Mark DB dirty so it will stay disabled if commit fails. */
2869 current_tcmh
.dirty
= true;
2870 update_master_header();
2872 /* Now create the index files. */
2873 tc_stat
.commit_step
= 0;
2875 tc_stat
.commit_delayed
= false;
2877 for (i
= 0; i
< TAG_COUNT
; i
++)
2881 if (tagcache_is_numeric_tag(i
))
2884 tc_stat
.commit_step
++;
2885 ret
= build_index(i
, &tch
, tmpfd
);
2889 logf("tagcache failed init");
2893 tc_stat
.commit_delayed
= true;
2894 tc_stat
.commit_step
= 0;
2900 if (!build_numeric_indices(&tch
, tmpfd
))
2902 logf("Failure to commit numeric indices");
2905 tc_stat
.commit_step
= 0;
2911 tc_stat
.commit_step
= 0;
2913 /* Update the master index headers. */
2914 if ( (masterfd
= open_master_fd(&tcmh
, true)) < 0)
2920 tcmh
.tch
.entry_count
+= tch
.entry_count
;
2921 tcmh
.tch
.datasize
= sizeof(struct master_header
)
2922 + sizeof(struct index_entry
) * tcmh
.tch
.entry_count
2927 lseek(masterfd
, 0, SEEK_SET
);
2928 ecwrite(masterfd
, &tcmh
, 1, master_header_ec
, tc_stat
.econ
);
2931 logf("tagcache committed");
2932 remove(TAGCACHE_FILE_TEMP
);
2933 tc_stat
.ready
= check_all_headers();
2934 tc_stat
.readyvalid
= true;
2936 if (local_allocation
)
2942 #ifdef HAVE_DIRCACHE
2943 /* Rebuild the dircache, if we stole the buffer. */
2944 if (dircache_buffer_stolen
)
2948 #ifdef HAVE_TC_RAMCACHE
2949 /* Reload tagcache. */
2950 if (tc_stat
.ramcache_allocated
> 0)
2951 tagcache_start_scan();
2959 static void allocate_tempbuf(void)
2961 /* Yeah, malloc would be really nice now :) */
2963 tempbuf_size
= 32*1024*1024;
2964 tempbuf
= malloc(tempbuf_size
);
2966 tempbuf
= (char *)(((long)audiobuf
& ~0x03) + 0x04);
2967 tempbuf_size
= (long)audiobufend
- (long)audiobuf
- 4;
2968 audiobuf
+= tempbuf_size
;
2972 static void free_tempbuf(void)
2974 if (tempbuf_size
== 0)
2980 audiobuf
-= tempbuf_size
;
2986 static bool modify_numeric_entry(int masterfd
, int idx_id
, int tag
, long data
)
2988 struct index_entry idx
;
2993 if (!tagcache_is_numeric_tag(tag
))
2996 if (!get_index(masterfd
, idx_id
, &idx
, false))
2999 idx
.tag_seek
[tag
] = data
;
3000 idx
.flag
|= FLAG_DIRTYNUM
;
3002 return write_index(masterfd
, idx_id
, &idx
);
3006 bool tagcache_modify_numeric_entry(struct tagcache_search
*tcs
,
3009 struct master_header myhdr
;
3011 if (tcs
->masterfd
< 0)
3013 if ( (tcs
->masterfd
= open_master_fd(&myhdr
, true)) < 0)
3017 return modify_numeric_entry(tcs
->masterfd
, tcs
->idx_id
, tag
, data
);
3021 #define COMMAND_QUEUE_IS_EMPTY (command_queue_ridx == command_queue_widx)
3023 static bool command_queue_is_full(void)
3027 next
= command_queue_widx
+ 1;
3028 if (next
>= TAGCACHE_COMMAND_QUEUE_LENGTH
)
3031 return (next
== command_queue_ridx
);
3034 void run_command_queue(bool force
)
3036 struct master_header myhdr
;
3039 if (COMMAND_QUEUE_IS_EMPTY
)
3042 if (!force
&& !command_queue_is_full()
3043 && current_tick
- TAGCACHE_COMMAND_QUEUE_COMMIT_DELAY
3044 < command_queue_timestamp
)
3049 mutex_lock(&command_queue_mutex
);
3051 if ( (masterfd
= open_master_fd(&myhdr
, true)) < 0)
3054 while (command_queue_ridx
!= command_queue_widx
)
3056 struct tagcache_command_entry
*ce
= &command_queue
[command_queue_ridx
];
3058 switch (ce
->command
)
3060 case CMD_UPDATE_MASTER_HEADER
:
3063 update_master_header();
3065 /* Re-open the masterfd. */
3066 if ( (masterfd
= open_master_fd(&myhdr
, true)) < 0)
3071 case CMD_UPDATE_NUMERIC
:
3073 modify_numeric_entry(masterfd
, ce
->idx_id
, ce
->tag
, ce
->data
);
3078 if (++command_queue_ridx
>= TAGCACHE_COMMAND_QUEUE_LENGTH
)
3079 command_queue_ridx
= 0;
3084 mutex_unlock(&command_queue_mutex
);
3087 static void queue_command(int cmd
, long idx_id
, int tag
, long data
)
3093 mutex_lock(&command_queue_mutex
);
3094 next
= command_queue_widx
+ 1;
3095 if (next
>= TAGCACHE_COMMAND_QUEUE_LENGTH
)
3098 /* Make sure queue is not full. */
3099 if (next
!= command_queue_ridx
)
3101 struct tagcache_command_entry
*ce
= &command_queue
[command_queue_widx
];
3104 ce
->idx_id
= idx_id
;
3108 command_queue_widx
= next
;
3109 command_queue_timestamp
= current_tick
;
3110 mutex_unlock(&command_queue_mutex
);
3114 /* Queue is full, try again later... */
3115 mutex_unlock(&command_queue_mutex
);
3120 long tagcache_increase_serial(void)
3130 old
= current_tcmh
.serial
++;
3131 queue_command(CMD_UPDATE_MASTER_HEADER
, 0, 0, 0);
3136 void tagcache_update_numeric(int idx_id
, int tag
, long data
)
3138 queue_command(CMD_UPDATE_NUMERIC
, idx_id
, tag
, data
);
3141 long tagcache_get_serial(void)
3143 return current_tcmh
.serial
;
3146 long tagcache_get_commitid(void)
3148 return current_tcmh
.commitid
;
3151 static bool write_tag(int fd
, const char *tagstr
, const char *datastr
)
3156 snprintf(buf
, sizeof buf
, "%s=\"", tagstr
);
3157 for (i
= strlen(buf
); i
< (long)sizeof(buf
)-3; i
++)
3159 if (*datastr
== '\0')
3162 if (*datastr
== '"')
3169 buf
[i
] = *(datastr
++);
3172 strcpy(&buf
[i
], "\" ");
3174 write(fd
, buf
, i
+ 2);
3179 static bool read_tag(char *dest
, long size
,
3180 const char *src
, const char *tagstr
)
3183 char current_tag
[32];
3185 while (*src
!= '\0')
3187 /* Skip all whitespace */
3195 /* Read in tag name */
3196 while (*src
!= '=' && *src
!= ' ')
3198 current_tag
[pos
] = *src
;
3202 if (*src
== '\0' || pos
>= (long)sizeof(current_tag
))
3205 current_tag
[pos
] = '\0';
3207 /* Read in tag data */
3209 /* Find the start. */
3210 while (*src
!= '"' && *src
!= '\0')
3213 if (*src
== '\0' || *(++src
) == '\0')
3216 /* Read the data. */
3217 for (pos
= 0; pos
< size
; pos
++)
3222 if (*src
== '\\' && *(src
+1) == '"')
3244 if (!strcasecmp(tagstr
, current_tag
))
3251 static int parse_changelog_line(int line_n
, const char *buf
, void *parameters
)
3253 struct index_entry idx
;
3254 char tag_data
[TAG_MAXLEN
+32];
3256 long masterfd
= (long)parameters
;
3257 const int import_tags
[] = { tag_playcount
, tag_rating
, tag_playtime
, tag_lastplayed
,
3265 logf("%d/%s", line_n
, buf
);
3266 if (!read_tag(tag_data
, sizeof tag_data
, buf
, "filename"))
3268 logf("filename missing");
3273 idx_id
= find_index(tag_data
);
3276 logf("entry not found");
3280 if (!get_index(masterfd
, idx_id
, &idx
, false))
3282 logf("failed to retrieve index entry");
3286 /* Stop if tag has already been modified. */
3287 if (idx
.flag
& FLAG_DIRTYNUM
)
3290 logf("import: %s", tag_data
);
3292 idx
.flag
|= FLAG_DIRTYNUM
;
3293 for (i
= 0; i
< (long)(sizeof(import_tags
)/sizeof(import_tags
[0])); i
++)
3297 if (!read_tag(tag_data
, sizeof tag_data
, buf
,
3298 tagcache_tag_to_str(import_tags
[i
])))
3303 data
= atoi(tag_data
);
3307 idx
.tag_seek
[import_tags
[i
]] = data
;
3309 if (import_tags
[i
] == tag_lastplayed
&& data
> current_tcmh
.serial
)
3310 current_tcmh
.serial
= data
;
3311 else if (import_tags
[i
] == tag_commitid
&& data
>= current_tcmh
.commitid
)
3312 current_tcmh
.commitid
= data
+ 1;
3315 return write_index(masterfd
, idx_id
, &idx
) ? 0 : -5;
3319 bool tagcache_import_changelog(void)
3321 struct master_header myhdr
;
3322 struct tagcache_header tch
;
3333 clfd
= open(TAGCACHE_FILE_CHANGELOG
, O_RDONLY
);
3336 logf("failure to open changelog");
3340 if ( (masterfd
= open_master_fd(&myhdr
, true)) < 0)
3348 filenametag_fd
= open_tag_fd(&tch
, tag_filename
, false);
3350 fast_readline(clfd
, buf
, sizeof buf
, (long *)masterfd
,
3351 parse_changelog_line
);
3356 if (filenametag_fd
>= 0)
3357 close(filenametag_fd
);
3361 update_master_header();
3367 bool tagcache_create_changelog(struct tagcache_search
*tcs
)
3369 struct master_header myhdr
;
3370 struct index_entry idx
;
3371 char buf
[TAG_MAXLEN
+32];
3379 if (!tagcache_search(tcs
, tag_filename
))
3382 /* Initialize the changelog */
3383 clfd
= open(TAGCACHE_FILE_CHANGELOG
, O_WRONLY
| O_CREAT
| O_TRUNC
);
3386 logf("failure to open changelog");
3390 if (tcs
->masterfd
< 0)
3392 if ( (tcs
->masterfd
= open_master_fd(&myhdr
, false)) < 0)
3397 lseek(tcs
->masterfd
, 0, SEEK_SET
);
3398 ecread(tcs
->masterfd
, &myhdr
, 1, master_header_ec
, tc_stat
.econ
);
3401 write(clfd
, "## Changelog version 1\n", 23);
3403 for (i
= 0; i
< myhdr
.tch
.entry_count
; i
++)
3405 if (ecread(tcs
->masterfd
, &idx
, 1, index_entry_ec
, tc_stat
.econ
)
3406 != sizeof(struct index_entry
))
3408 logf("read error #9");
3409 tagcache_search_finish(tcs
);
3414 /* Skip until the entry found has been modified. */
3415 if (! (idx
.flag
& FLAG_DIRTYNUM
) )
3418 /* Skip deleted entries too. */
3419 if (idx
.flag
& FLAG_DELETED
)
3422 /* Now retrieve all tags. */
3423 for (j
= 0; j
< TAG_COUNT
; j
++)
3425 if (tagcache_is_numeric_tag(j
))
3427 snprintf(temp
, sizeof temp
, "%d", idx
.tag_seek
[j
]);
3428 write_tag(clfd
, tagcache_tag_to_str(j
), temp
);
3433 tagcache_retrieve(tcs
, i
, tcs
->type
, buf
, sizeof buf
);
3434 write_tag(clfd
, tagcache_tag_to_str(j
), buf
);
3437 write(clfd
, "\n", 1);
3443 tagcache_search_finish(tcs
);
3448 static bool delete_entry(long idx_id
)
3453 struct index_entry idx
, myidx
;
3454 struct master_header myhdr
;
3455 char buf
[TAG_MAXLEN
+32];
3456 int in_use
[TAG_COUNT
];
3458 logf("delete_entry(): %ld", idx_id
);
3460 #ifdef HAVE_TC_RAMCACHE
3461 /* At first mark the entry removed from ram cache. */
3462 if (tc_stat
.ramcache
)
3463 hdr
->indices
[idx_id
].flag
|= FLAG_DELETED
;
3466 if ( (masterfd
= open_master_fd(&myhdr
, true) ) < 0)
3469 lseek(masterfd
, idx_id
* sizeof(struct index_entry
), SEEK_CUR
);
3470 if (ecread(masterfd
, &myidx
, 1, index_entry_ec
, tc_stat
.econ
)
3471 != sizeof(struct index_entry
))
3473 logf("delete_entry(): read error");
3477 if (myidx
.flag
& FLAG_DELETED
)
3479 logf("delete_entry(): already deleted!");
3483 myidx
.flag
|= FLAG_DELETED
;
3484 lseek(masterfd
, -sizeof(struct index_entry
), SEEK_CUR
);
3485 if (ecwrite(masterfd
, &myidx
, 1, index_entry_ec
, tc_stat
.econ
)
3486 != sizeof(struct index_entry
))
3488 logf("delete_entry(): write_error #1");
3492 /* Now check which tags are no longer in use (if any) */
3493 for (tag
= 0; tag
< TAG_COUNT
; tag
++)
3496 lseek(masterfd
, sizeof(struct master_header
), SEEK_SET
);
3497 for (i
= 0; i
< myhdr
.tch
.entry_count
; i
++)
3499 struct index_entry
*idxp
;
3501 #ifdef HAVE_TC_RAMCACHE
3502 /* Use RAM DB if available for greater speed */
3503 if (tc_stat
.ramcache
)
3504 idxp
= &hdr
->indices
[i
];
3508 if (ecread(masterfd
, &idx
, 1, index_entry_ec
, tc_stat
.econ
)
3509 != sizeof(struct index_entry
))
3511 logf("delete_entry(): read error #2");
3517 if (idxp
->flag
& FLAG_DELETED
)
3520 for (tag
= 0; tag
< TAG_COUNT
; tag
++)
3522 if (tagcache_is_numeric_tag(tag
))
3525 if (idxp
->tag_seek
[tag
] == myidx
.tag_seek
[tag
])
3530 /* Now delete all tags no longer in use. */
3531 for (tag
= 0; tag
< TAG_COUNT
; tag
++)
3533 struct tagcache_header tch
;
3534 int oldseek
= myidx
.tag_seek
[tag
];
3536 if (tagcache_is_numeric_tag(tag
))
3540 * Replace tag seek with a hash value of the field string data.
3541 * That way runtime statistics of moved or altered files can be
3544 #ifdef HAVE_TC_RAMCACHE
3545 if (tc_stat
.ramcache
&& tag
!= tag_filename
)
3547 struct tagfile_entry
*tfe
;
3548 long *seek
= &hdr
->indices
[idx_id
].tag_seek
[tag
];
3550 tfe
= (struct tagfile_entry
*)&hdr
->tags
[tag
][*seek
];
3551 *seek
= crc_32(tfe
->tag_data
, strlen(tfe
->tag_data
), 0xffffffff);
3552 myidx
.tag_seek
[tag
] = *seek
;
3557 struct tagfile_entry tfe
;
3559 /* Open the index file, which contains the tag names. */
3560 if ((fd
= open_tag_fd(&tch
, tag
, true)) < 0)
3563 /* Skip the header block */
3564 lseek(fd
, myidx
.tag_seek
[tag
], SEEK_SET
);
3565 if (ecread(fd
, &tfe
, 1, tagfile_entry_ec
, tc_stat
.econ
)
3566 != sizeof(struct tagfile_entry
))
3568 logf("delete_entry(): read error #3");
3572 if (read(fd
, buf
, tfe
.tag_length
) != tfe
.tag_length
)
3574 logf("delete_entry(): read error #4");
3578 myidx
.tag_seek
[tag
] = crc_32(buf
, strlen(buf
), 0xffffffff);
3583 logf("in use: %d/%d", tag
, in_use
[tag
]);
3592 #ifdef HAVE_TC_RAMCACHE
3593 /* Delete from ram. */
3594 if (tc_stat
.ramcache
&& tag
!= tag_filename
)
3596 struct tagfile_entry
*tagentry
= (struct tagfile_entry
*)&hdr
->tags
[tag
][oldseek
];
3597 tagentry
->tag_data
[0] = '\0';
3601 /* Open the index file, which contains the tag names. */
3604 if ((fd
= open_tag_fd(&tch
, tag
, true)) < 0)
3608 /* Skip the header block */
3609 lseek(fd
, oldseek
+ sizeof(struct tagfile_entry
), SEEK_SET
);
3611 /* Debug, print 10 first characters of the tag
3614 logf("TAG:%s", buf);
3615 lseek(fd, -10, SEEK_CUR);
3618 /* Write first data byte in tag as \0 */
3621 /* Now tag data has been removed */
3626 /* Write index entry back into master index. */
3627 lseek(masterfd
, sizeof(struct master_header
) +
3628 (idx_id
* sizeof(struct index_entry
)), SEEK_SET
);
3629 if (ecwrite(masterfd
, &myidx
, 1, index_entry_ec
, tc_stat
.econ
)
3630 != sizeof(struct index_entry
))
3632 logf("delete_entry(): write_error #2");
3651 * Returns true if there is an event waiting in the queue
3652 * that requires the current operation to be aborted.
3654 static bool check_event_queue(void)
3656 struct queue_event ev
;
3658 queue_wait_w_tmo(&tagcache_queue
, &ev
, 0);
3663 case SYS_USB_CONNECTED
:
3664 /* Put the event back into the queue. */
3665 queue_post(&tagcache_queue
, ev
.id
, ev
.data
);
3673 #ifdef HAVE_TC_RAMCACHE
3674 static bool allocate_tagcache(void)
3676 struct master_header tcmh
;
3679 /* Load the header. */
3680 if ( (fd
= open_master_fd(&tcmh
, false)) < 0)
3689 * Now calculate the required cache size plus
3690 * some extra space for alignment fixes.
3692 tc_stat
.ramcache_allocated
= tcmh
.tch
.datasize
+ 128 + TAGCACHE_RESERVE
+
3693 sizeof(struct ramcache_header
) + TAG_COUNT
*sizeof(void *);
3694 hdr
= buffer_alloc(tc_stat
.ramcache_allocated
+ 128);
3695 memset(hdr
, 0, sizeof(struct ramcache_header
));
3696 memcpy(&hdr
->h
, &tcmh
, sizeof(struct master_header
));
3697 hdr
->indices
= (struct index_entry
*)(hdr
+ 1);
3698 logf("tagcache: %d bytes allocated.", tc_stat
.ramcache_allocated
);
3703 # ifdef HAVE_EEPROM_SETTINGS
3704 static bool tagcache_dumpload(void)
3706 struct statefile_header shdr
;
3711 fd
= open(TAGCACHE_STATEFILE
, O_RDONLY
);
3714 logf("no tagcache statedump");
3718 /* Check the statefile memory placement */
3719 hdr
= buffer_alloc(0);
3720 rc
= read(fd
, &shdr
, sizeof(struct statefile_header
));
3721 if (rc
!= sizeof(struct statefile_header
)
3722 /* || (long)hdr != (long)shdr.hdr */)
3724 logf("incorrect statefile");
3730 offpos
= (long)hdr
- (long)shdr
.hdr
;
3732 /* Lets allocate real memory and load it */
3733 hdr
= buffer_alloc(shdr
.tc_stat
.ramcache_allocated
);
3734 rc
= read(fd
, hdr
, shdr
.tc_stat
.ramcache_allocated
);
3737 if (rc
!= shdr
.tc_stat
.ramcache_allocated
)
3739 logf("read failure!");
3744 memcpy(&tc_stat
, &shdr
.tc_stat
, sizeof(struct tagcache_stat
));
3746 /* Now fix the pointers */
3747 hdr
->indices
= (struct index_entry
*)((long)hdr
->indices
+ offpos
);
3748 for (i
= 0; i
< TAG_COUNT
; i
++)
3749 hdr
->tags
[i
] += offpos
;
3754 static bool tagcache_dumpsave(void)
3756 struct statefile_header shdr
;
3759 if (!tc_stat
.ramcache
)
3762 fd
= open(TAGCACHE_STATEFILE
, O_WRONLY
| O_CREAT
| O_TRUNC
);
3765 logf("failed to create a statedump");
3769 /* Create the header */
3771 memcpy(&shdr
.tc_stat
, &tc_stat
, sizeof(struct tagcache_stat
));
3772 write(fd
, &shdr
, sizeof(struct statefile_header
));
3774 /* And dump the data too */
3775 write(fd
, hdr
, tc_stat
.ramcache_allocated
);
3782 static bool load_tagcache(void)
3784 struct tagcache_header
*tch
;
3785 long bytesleft
= tc_stat
.ramcache_allocated
;
3786 struct index_entry
*idx
;
3791 # ifdef HAVE_DIRCACHE
3792 while (dircache_is_initializing())
3796 dircache_set_appflag(DIRCACHE_APPFLAG_TAGCACHE
);
3798 logf("loading tagcache to ram...");
3800 fd
= open(TAGCACHE_FILE_MASTER
, O_RDONLY
);
3803 logf("tagcache open failed");
3807 if (ecread(fd
, &hdr
->h
, 1, master_header_ec
, tc_stat
.econ
)
3808 != sizeof(struct master_header
)
3809 || hdr
->h
.tch
.magic
!= TAGCACHE_MAGIC
)
3811 logf("incorrect header");
3817 /* Load the master index table. */
3818 for (i
= 0; i
< hdr
->h
.tch
.entry_count
; i
++)
3820 rc
= ecread(fd
, idx
, 1, index_entry_ec
, tc_stat
.econ
);
3821 if (rc
!= sizeof(struct index_entry
))
3823 logf("read error #10");
3828 bytesleft
-= sizeof(struct index_entry
);
3829 if (bytesleft
< 0 || ((long)idx
- (long)hdr
->indices
) >= tc_stat
.ramcache_allocated
)
3831 logf("too big tagcache.");
3841 /* Load the tags. */
3843 for (tag
= 0; tag
< TAG_COUNT
; tag
++)
3845 struct tagfile_entry
*fe
;
3846 char buf
[TAG_MAXLEN
+32];
3848 if (tagcache_is_numeric_tag(tag
))
3851 //p = ((void *)p+1);
3852 p
= (char *)((long)p
& ~0x03) + 0x04;
3855 /* Check the header. */
3856 tch
= (struct tagcache_header
*)p
;
3857 p
+= sizeof(struct tagcache_header
);
3859 if ( (fd
= open_tag_fd(tch
, tag
, false)) < 0)
3862 for (hdr
->entry_count
[tag
] = 0;
3863 hdr
->entry_count
[tag
] < tch
->entry_count
;
3864 hdr
->entry_count
[tag
]++)
3868 if (do_timed_yield())
3870 /* Abort if we got a critical event in queue */
3871 if (check_event_queue())
3875 fe
= (struct tagfile_entry
*)p
;
3876 pos
= lseek(fd
, 0, SEEK_CUR
);
3877 rc
= ecread(fd
, fe
, 1, tagfile_entry_ec
, tc_stat
.econ
);
3878 if (rc
!= sizeof(struct tagfile_entry
))
3880 /* End of lookup table. */
3881 logf("read error #11");
3886 /* We have a special handling for the filename tags. */
3887 if (tag
== tag_filename
)
3889 # ifdef HAVE_DIRCACHE
3890 const struct dirent
*dc
;
3893 // FIXME: This is wrong!
3894 // idx = &hdr->indices[hdr->entry_count[i]];
3895 idx
= &hdr
->indices
[fe
->idx_id
];
3897 if (fe
->tag_length
>= (long)sizeof(buf
)-1)
3901 logf("TAG:%s", buf
);
3902 logf("too long filename");
3907 rc
= read(fd
, buf
, fe
->tag_length
);
3908 if (rc
!= fe
->tag_length
)
3910 logf("read error #12");
3915 /* Check if the entry has already been removed */
3916 if (idx
->flag
& FLAG_DELETED
)
3919 /* This flag must not be used yet. */
3920 if (idx
->flag
& FLAG_DIRCACHE
)
3922 logf("internal error!");
3927 if (idx
->tag_seek
[tag
] != pos
)
3929 logf("corrupt data structures!");
3934 # ifdef HAVE_DIRCACHE
3935 if (dircache_is_enabled())
3937 dc
= dircache_get_entry_ptr(buf
);
3940 logf("Entry no longer valid.");
3942 delete_entry(fe
->idx_id
);
3946 idx
->flag
|= FLAG_DIRCACHE
;
3947 FLAG_SET_ATTR(idx
->flag
, fe
->idx_id
);
3948 idx
->tag_seek
[tag_filename
] = (long)dc
;
3953 /* This will be very slow unless dircache is enabled
3954 or target is flash based, but do it anyway for
3956 /* Check if entry has been removed. */
3957 if (global_settings
.tagcache_autoupdate
)
3959 if (!file_exists(buf
))
3961 logf("Entry no longer valid.");
3963 delete_entry(fe
->idx_id
);
3972 bytesleft
-= sizeof(struct tagfile_entry
) + fe
->tag_length
;
3975 logf("too big tagcache #2");
3976 logf("tl: %d", fe
->tag_length
);
3977 logf("bl: %ld", bytesleft
);
3983 rc
= read(fd
, fe
->tag_data
, fe
->tag_length
);
3986 if (rc
!= fe
->tag_length
)
3988 logf("read error #13");
3989 logf("rc=0x%04x", rc
); // 0x431
3990 logf("len=0x%04x", fe
->tag_length
); // 0x4000
3991 logf("pos=0x%04lx", lseek(fd
, 0, SEEK_CUR
)); // 0x433
3992 logf("tag=0x%02x", tag
); // 0x00
4000 tc_stat
.ramcache_used
= tc_stat
.ramcache_allocated
- bytesleft
;
4001 logf("tagcache loaded into ram!");
4005 #endif /* HAVE_TC_RAMCACHE */
4007 static bool check_deleted_files(void)
4010 char buf
[TAG_MAXLEN
+32];
4011 struct tagfile_entry tfe
;
4013 logf("reverse scan...");
4014 snprintf(buf
, sizeof buf
, TAGCACHE_FILE_INDEX
, tag_filename
);
4015 fd
= open(buf
, O_RDONLY
);
4019 logf("%s open fail", buf
);
4023 lseek(fd
, sizeof(struct tagcache_header
), SEEK_SET
);
4024 while (ecread(fd
, &tfe
, 1, tagfile_entry_ec
, tc_stat
.econ
)
4025 == sizeof(struct tagfile_entry
)
4027 && !check_event_queue()
4031 if (tfe
.tag_length
>= (long)sizeof(buf
)-1)
4033 logf("too long tag");
4038 if (read(fd
, buf
, tfe
.tag_length
) != tfe
.tag_length
)
4040 logf("read error #14");
4045 /* Check if the file has already deleted from the db. */
4049 /* Now check if the file exists. */
4050 if (!file_exists(buf
))
4052 logf("Entry no longer valid.");
4053 logf("-> %s / %d", buf
, tfe
.tag_length
);
4054 delete_entry(tfe
.idx_id
);
4065 static bool check_dir(const char *dirname
, int add_files
)
4069 int success
= false;
4070 int ignore
, unignore
;
4071 char newpath
[MAX_PATH
];
4073 dir
= opendir(dirname
);
4076 logf("tagcache: opendir() failed");
4080 /* check for a database.ignore file */
4081 snprintf(newpath
, MAX_PATH
, "%s/database.ignore", dirname
);
4082 ignore
= file_exists(newpath
);
4083 /* check for a database.unignore file */
4084 snprintf(newpath
, MAX_PATH
, "%s/database.unignore", dirname
);
4085 unignore
= file_exists(newpath
);
4087 /* don't do anything if both ignore and unignore are there */
4088 if (ignore
!= unignore
)
4089 add_files
= unignore
;
4091 /* Recursively scan the dir. */
4095 while (!check_event_queue())
4098 struct dirent
*entry
;
4100 entry
= readdir(dir
);
4108 if (!strcmp((char *)entry
->d_name
, ".") ||
4109 !strcmp((char *)entry
->d_name
, ".."))
4114 len
= strlen(curpath
);
4115 snprintf(&curpath
[len
], curpath_size
- len
, "/%s",
4118 processed_dir_count
++;
4119 if (entry
->attribute
& ATTR_DIRECTORY
)
4120 check_dir(curpath
, add_files
);
4123 tc_stat
.curentry
= curpath
;
4125 /* Add a new entry to the temporary db file. */
4126 add_tagcache(curpath
, (entry
->wrtdate
<< 16) | entry
->wrttime
4127 #if defined(HAVE_TC_RAMCACHE) && defined(HAVE_DIRCACHE)
4128 , dir
->internal_entry
4132 /* Wait until current path for debug screen is read and unset. */
4133 while (tc_stat
.syncscreen
&& tc_stat
.curentry
!= NULL
)
4136 tc_stat
.curentry
= NULL
;
4139 curpath
[len
] = '\0';
4147 void tagcache_screensync_event(void)
4149 tc_stat
.curentry
= NULL
;
4152 void tagcache_screensync_enable(bool state
)
4154 tc_stat
.syncscreen
= state
;
4157 void build_tagcache(const char *path
)
4159 struct tagcache_header header
;
4164 total_entry_count
= 0;
4165 processed_dir_count
= 0;
4167 #ifdef HAVE_DIRCACHE
4168 while (dircache_is_initializing())
4172 logf("updating tagcache");
4174 cachefd
= open(TAGCACHE_FILE_TEMP
, O_RDONLY
);
4177 logf("skipping, cache already waiting for commit");
4182 cachefd
= open(TAGCACHE_FILE_TEMP
, O_RDWR
| O_CREAT
| O_TRUNC
);
4185 logf("master file open failed: %s", TAGCACHE_FILE_TEMP
);
4189 filenametag_fd
= open_tag_fd(&header
, tag_filename
, false);
4193 logf("Scanning files...");
4194 /* Scan for new files. */
4195 memset(&header
, 0, sizeof(struct tagcache_header
));
4196 write(cachefd
, &header
, sizeof(struct tagcache_header
));
4198 if (strcmp("/", path
) != 0)
4199 strcpy(curpath
, path
);
4200 ret
= check_dir(path
, true);
4202 /* Write the header. */
4203 header
.magic
= TAGCACHE_MAGIC
;
4204 header
.datasize
= data_size
;
4205 header
.entry_count
= total_entry_count
;
4206 lseek(cachefd
, 0, SEEK_SET
);
4207 write(cachefd
, &header
, sizeof(struct tagcache_header
));
4210 if (filenametag_fd
>= 0)
4212 close(filenametag_fd
);
4213 filenametag_fd
= -1;
4223 /* Commit changes to the database. */
4229 remove(TAGCACHE_FILE_TEMP
);
4230 logf("tagcache built!");
4236 #ifdef HAVE_TC_RAMCACHE
4239 /* Import runtime statistics if we just initialized the db. */
4240 if (hdr
->h
.serial
== 0)
4241 queue_post(&tagcache_queue
, Q_IMPORT_CHANGELOG
, 0);
4248 #ifdef HAVE_TC_RAMCACHE
4249 static void load_ramcache(void)
4256 /* At first we should load the cache (if exists). */
4257 tc_stat
.ramcache
= load_tagcache();
4259 if (!tc_stat
.ramcache
)
4261 /* If loading failed, it must indicate some problem with the db
4262 * so disable it entirely to prevent further issues. */
4263 tc_stat
.ready
= false;
4270 void tagcache_unload_ramcache(void)
4272 tc_stat
.ramcache
= false;
4273 /* Just to make sure there is no statefile present. */
4274 // remove(TAGCACHE_STATEFILE);
4279 static void tagcache_thread(void)
4281 struct queue_event ev
;
4282 bool check_done
= false;
4284 /* If the previous cache build/update was interrupted, commit
4285 * the changes first in foreground. */
4291 #ifdef HAVE_TC_RAMCACHE
4292 # ifdef HAVE_EEPROM_SETTINGS
4293 if (firmware_settings
.initialized
&& firmware_settings
.disk_clean
)
4294 check_done
= tagcache_dumpload();
4296 remove(TAGCACHE_STATEFILE
);
4299 /* Allocate space for the tagcache if found on disk. */
4300 if (global_settings
.tagcache_ram
&& !tc_stat
.ramcache
)
4301 allocate_tagcache();
4305 tc_stat
.initialized
= true;
4307 /* Don't delay bootup with the header check but do it on background. */
4309 tc_stat
.ready
= check_all_headers();
4310 tc_stat
.readyvalid
= true;
4314 run_command_queue(false);
4316 queue_wait_w_tmo(&tagcache_queue
, &ev
, HZ
);
4320 case Q_IMPORT_CHANGELOG
:
4321 tagcache_import_changelog();
4326 build_tagcache("/");
4330 build_tagcache("/");
4331 #ifdef HAVE_TC_RAMCACHE
4334 check_deleted_files();
4340 if (check_done
|| !tc_stat
.ready
)
4343 #ifdef HAVE_TC_RAMCACHE
4344 if (!tc_stat
.ramcache
&& global_settings
.tagcache_ram
)
4347 if (tc_stat
.ramcache
&& global_settings
.tagcache_autoupdate
)
4348 build_tagcache("/");
4352 if (global_settings
.tagcache_autoupdate
)
4354 build_tagcache("/");
4356 /* This will be very slow unless dircache is enabled
4357 or target is flash based, but do it anyway for
4359 check_deleted_files();
4362 logf("tagcache check done");
4374 case SYS_USB_CONNECTED
:
4375 logf("USB: TagCache");
4376 usb_acknowledge(SYS_USB_CONNECTED_ACK
);
4377 usb_wait_for_disconnect(&tagcache_queue
);
4384 bool tagcache_prepare_shutdown(void)
4386 if (tagcache_get_commit_step() > 0)
4389 tagcache_stop_scan();
4390 while (read_lock
|| write_lock
)
4396 void tagcache_shutdown(void)
4398 /* Flush the command queue. */
4399 run_command_queue(true);
4401 #ifdef HAVE_EEPROM_SETTINGS
4402 if (tc_stat
.ramcache
)
4403 tagcache_dumpsave();
4407 static int get_progress(void)
4409 int total_count
= -1;
4411 #ifdef HAVE_DIRCACHE
4412 if (dircache_is_enabled())
4414 total_count
= dircache_get_entry_count();
4418 #ifdef HAVE_TC_RAMCACHE
4420 if (hdr
&& tc_stat
.ramcache
)
4421 total_count
= hdr
->h
.tch
.entry_count
;
4425 if (total_count
< 0)
4428 return processed_dir_count
* 100 / total_count
;
4431 struct tagcache_stat
* tagcache_get_stat(void)
4433 tc_stat
.progress
= get_progress();
4434 tc_stat
.processed_entries
= processed_dir_count
;
4439 void tagcache_start_scan(void)
4441 queue_post(&tagcache_queue
, Q_START_SCAN
, 0);
4444 bool tagcache_update(void)
4449 queue_post(&tagcache_queue
, Q_UPDATE
, 0);
4453 bool tagcache_rebuild()
4455 queue_post(&tagcache_queue
, Q_REBUILD
, 0);
4459 void tagcache_stop_scan(void)
4461 queue_post(&tagcache_queue
, Q_STOP_SCAN
, 0);
4464 #ifdef HAVE_TC_RAMCACHE
4465 bool tagcache_is_ramcache(void)
4467 return tc_stat
.ramcache
;
4471 #endif /* !__PCTOOL__ */
4474 void tagcache_init(void)
4476 memset(&tc_stat
, 0, sizeof(struct tagcache_stat
));
4477 memset(¤t_tcmh
, 0, sizeof(struct master_header
));
4478 filenametag_fd
= -1;
4479 write_lock
= read_lock
= 0;
4482 mutex_init(&command_queue_mutex
);
4483 queue_init(&tagcache_queue
, true);
4484 create_thread(tagcache_thread
, tagcache_stack
,
4485 sizeof(tagcache_stack
), 0, tagcache_thread_name
4486 IF_PRIO(, PRIORITY_BACKGROUND
)
4489 tc_stat
.initialized
= true;
4493 tc_stat
.ready
= check_all_headers();
4498 void tagcache_reverse_scan(void)
4500 logf("Checking for deleted files");
4501 check_deleted_files();
4505 bool tagcache_is_initialized(void)
4507 return tc_stat
.initialized
;
4509 bool tagcache_is_usable(void)
4511 return tc_stat
.initialized
&& tc_stat
.ready
;
4513 int tagcache_get_commit_step(void)
4515 return tc_stat
.commit_step
;
4517 int tagcache_get_max_commit_step(void)
4519 return (int)(sizeof(sorted_tags
)/sizeof(sorted_tags
[0]))+1;