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"
82 #include "eeprom_settings.h"
86 #define yield() do { } while(0)
87 #define sim_sleep(timeout) do { } while(0)
88 #define do_timed_yield() do { } while(0)
92 /* Tag Cache thread. */
93 static struct event_queue tagcache_queue
;
94 static long tagcache_stack
[(DEFAULT_STACK_SIZE
+ 0x4000)/sizeof(long)];
95 static const char tagcache_thread_name
[] = "tagcache";
98 #define UNTAGGED "<Untagged>"
100 /* Previous path when scanning directory tree recursively. */
101 static char curpath
[TAG_MAXLEN
+32];
103 /* Used when removing duplicates. */
104 static char *tempbuf
; /* Allocated when needed. */
105 static long tempbufidx
; /* Current location in buffer. */
106 static long tempbuf_size
; /* Buffer size (TEMPBUF_SIZE). */
107 static long tempbuf_left
; /* Buffer space left. */
108 static long tempbuf_pos
;
110 /* Tags we want to get sorted (loaded to the tempbuf). */
111 static const int sorted_tags
[] = { tag_artist
, tag_album
, tag_genre
,
112 tag_composer
, tag_comment
, tag_albumartist
, tag_grouping
, tag_title
};
114 /* Uniqued tags (we can use these tags with filters and conditional clauses). */
115 static const int unique_tags
[] = { tag_artist
, tag_album
, tag_genre
,
116 tag_composer
, tag_comment
, tag_albumartist
, tag_grouping
};
118 /* Numeric tags (we can use these tags with conditional clauses). */
119 static const int numeric_tags
[] = { tag_year
, tag_discnumber
,
120 tag_tracknumber
, tag_length
, tag_bitrate
, tag_playcount
, tag_rating
,
121 tag_playtime
, tag_lastplayed
, tag_commitid
, tag_mtime
,
122 tag_virt_length_min
, tag_virt_length_sec
,
123 tag_virt_playtime_min
, tag_virt_playtime_sec
,
124 tag_virt_entryage
, tag_virt_autoscore
};
126 /* String presentation of the tags defined in tagcache.h. Must be in correct order! */
127 static const char *tags_str
[] = { "artist", "album", "genre", "title",
128 "filename", "composer", "comment", "albumartist", "grouping", "year",
129 "discnumber", "tracknumber", "bitrate", "length", "playcount", "rating",
130 "playtime", "lastplayed", "commitid", "mtime" };
132 /* Status information of the tagcache. */
133 static struct tagcache_stat tc_stat
;
135 /* Queue commands. */
136 enum tagcache_queue
{
143 /* Internal tagcache command queue. */
144 CMD_UPDATE_MASTER_HEADER
,
148 struct tagcache_command_entry
{
156 static struct tagcache_command_entry command_queue
[TAGCACHE_COMMAND_QUEUE_LENGTH
];
157 static volatile int command_queue_widx
= 0;
158 static volatile int command_queue_ridx
= 0;
159 static struct mutex command_queue_mutex
;
162 /* Tag database structures. */
164 /* Variable-length tag entry in tag files. */
165 struct tagfile_entry
{
166 int32_t tag_length
; /* Length of the data in bytes including '\0' */
167 int32_t idx_id
; /* Corresponding entry location in index file of not unique tags */
168 char tag_data
[0]; /* Begin of the tag data */
171 /* Fixed-size tag entry in master db index. */
173 int32_t tag_seek
[TAG_COUNT
]; /* Location of tag data or numeric tag data */
174 int32_t flag
; /* Status flags */
177 /* Header is the same in every file. */
178 struct tagcache_header
{
179 int32_t magic
; /* Header version number */
180 int32_t datasize
; /* Data size in bytes */
181 int32_t entry_count
; /* Number of entries in this file */
184 struct master_header
{
185 struct tagcache_header tch
;
186 int32_t serial
; /* Increasing counting number */
187 int32_t commitid
; /* Number of commits so far */
191 /* For the endianess correction */
192 static const char *tagfile_entry_ec
= "ll";
194 Note: This should be (1 + TAG_COUNT) amount of l's.
196 static const char *index_entry_ec
= "lllllllllllllllllllll";
198 static const char *tagcache_header_ec
= "lll";
199 static const char *master_header_ec
= "llllll";
201 static struct master_header current_tcmh
;
203 #ifdef HAVE_TC_RAMCACHE
204 /* Header is created when loading database to ram. */
205 struct ramcache_header
{
206 struct master_header h
; /* Header from the master index */
207 struct index_entry
*indices
; /* Master index file content */
208 char *tags
[TAG_COUNT
]; /* Tag file content (not including filename tag) */
209 int entry_count
[TAG_COUNT
]; /* Number of entries in the indices. */
212 # ifdef HAVE_EEPROM_SETTINGS
213 struct statefile_header
{
214 struct ramcache_header
*hdr
;
215 struct tagcache_stat tc_stat
;
219 /* Pointer to allocated ramcache_header */
220 static struct ramcache_header
*hdr
;
224 * Full tag entries stored in a temporary file waiting
225 * for commit to the cache. */
226 struct temp_file_entry
{
227 long tag_offset
[TAG_COUNT
];
228 short tag_length
[TAG_COUNT
];
234 struct tempbuf_id_list
{
236 struct tempbuf_id_list
*next
;
239 struct tempbuf_searchidx
{
243 struct tempbuf_id_list idlist
;
246 /* Lookup buffer for fixing messed up index while after sorting. */
247 static long commit_entry_count
;
248 static long lookup_buffer_depth
;
249 static struct tempbuf_searchidx
**lookup
;
251 /* Used when building the temporary file. */
252 static int cachefd
= -1, filenametag_fd
;
253 static int total_entry_count
= 0;
254 static int data_size
= 0;
255 static int processed_dir_count
;
257 /* Thread safe locking */
258 static volatile int write_lock
;
259 static volatile int read_lock
;
261 static bool delete_entry(long idx_id
);
263 const char* tagcache_tag_to_str(int tag
)
265 return tags_str
[tag
];
268 bool tagcache_is_numeric_tag(int type
)
272 for (i
= 0; i
< (int)(sizeof(numeric_tags
)/sizeof(numeric_tags
[0])); i
++)
274 if (type
== numeric_tags
[i
])
281 bool tagcache_is_unique_tag(int type
)
285 for (i
= 0; i
< (int)(sizeof(unique_tags
)/sizeof(unique_tags
[0])); i
++)
287 if (type
== unique_tags
[i
])
294 bool tagcache_is_sorted_tag(int type
)
298 for (i
= 0; i
< (int)(sizeof(sorted_tags
)/sizeof(sorted_tags
[0])); i
++)
300 if (type
== sorted_tags
[i
])
309 * Returns true if specified flag is still present, i.e., dircache
310 * has not been reloaded.
312 static bool is_dircache_intact(void)
314 return dircache_get_appflag(DIRCACHE_APPFLAG_TAGCACHE
);
318 static int open_tag_fd(struct tagcache_header
*hdr
, int tag
, bool write
)
324 if (tagcache_is_numeric_tag(tag
) || tag
< 0 || tag
>= TAG_COUNT
)
327 snprintf(buf
, sizeof buf
, TAGCACHE_FILE_INDEX
, tag
);
329 fd
= open(buf
, write
? O_RDWR
: O_RDONLY
);
332 logf("tag file open failed: tag=%d write=%d file=%s", tag
, write
, buf
);
333 tc_stat
.ready
= false;
337 /* Check the header. */
338 rc
= ecread(fd
, hdr
, 1, tagcache_header_ec
, tc_stat
.econ
);
339 if (hdr
->magic
!= TAGCACHE_MAGIC
|| rc
!= sizeof(struct tagcache_header
))
341 logf("header error");
342 tc_stat
.ready
= false;
350 static int open_master_fd(struct master_header
*hdr
, bool write
)
355 fd
= open(TAGCACHE_FILE_MASTER
, write
? O_RDWR
: O_RDONLY
);
358 logf("master file open failed for R/W");
359 tc_stat
.ready
= false;
363 tc_stat
.econ
= false;
365 /* Check the header. */
366 rc
= read(fd
, hdr
, sizeof(struct master_header
));
367 if (hdr
->tch
.magic
== TAGCACHE_MAGIC
&& rc
== sizeof(struct master_header
))
373 /* Trying to read again, this time with endianess correction enabled. */
374 lseek(fd
, 0, SEEK_SET
);
376 rc
= ecread(fd
, hdr
, 1, master_header_ec
, true);
377 if (hdr
->tch
.magic
!= TAGCACHE_MAGIC
|| rc
!= sizeof(struct master_header
))
379 logf("header error");
380 tc_stat
.ready
= false;
391 static bool do_timed_yield(void)
393 /* Sorting can lock up for quite a while, so yield occasionally */
394 static long wakeup_tick
= 0;
395 if (current_tick
>= wakeup_tick
)
397 wakeup_tick
= current_tick
+ (HZ
/4);
405 #if defined(HAVE_TC_RAMCACHE) && defined(HAVE_DIRCACHE)
406 static long find_entry_ram(const char *filename
,
407 const struct dirent
*dc
)
409 static long last_pos
= 0;
412 /* Check if we tagcache is loaded into ram. */
413 if (!tc_stat
.ramcache
)
417 dc
= dircache_get_entry_ptr(filename
);
421 logf("tagcache: file not found.");
432 for (; i
< hdr
->h
.tch
.entry_count
; i
++)
434 if (hdr
->indices
[i
].tag_seek
[tag_filename
] == (long)dc
)
436 last_pos
= MAX(0, i
- 3);
453 static long find_entry_disk(const char *filename
)
455 struct tagcache_header tch
;
456 static long last_pos
= -1;
457 long pos_history
[POS_HISTORY_COUNT
];
458 long pos_history_idx
= 0;
460 struct tagfile_entry tfe
;
462 char buf
[TAG_MAXLEN
+32];
473 if ( (fd
= open_tag_fd(&tch
, tag_filename
, false)) < 0)
480 lseek(fd
, last_pos
, SEEK_SET
);
482 lseek(fd
, sizeof(struct tagcache_header
), SEEK_SET
);
486 pos
= lseek(fd
, 0, SEEK_CUR
);
487 for (i
= pos_history_idx
-1; i
>= 0; i
--)
488 pos_history
[i
+1] = pos_history
[i
];
489 pos_history
[0] = pos
;
491 if (ecread(fd
, &tfe
, 1, tagfile_entry_ec
, tc_stat
.econ
)
492 != sizeof(struct tagfile_entry
))
497 if (tfe
.tag_length
>= (long)sizeof(buf
))
499 logf("too long tag #1");
505 if (read(fd
, buf
, tfe
.tag_length
) != tfe
.tag_length
)
507 logf("read error #2");
513 if (!strcasecmp(filename
, buf
))
515 last_pos
= pos_history
[pos_history_idx
];
520 if (pos_history_idx
< POS_HISTORY_COUNT
- 1)
534 if (fd
!= filenametag_fd
)
539 if (fd
!= filenametag_fd
)
545 static int find_index(const char *filename
)
549 #if defined(HAVE_TC_RAMCACHE) && defined(HAVE_DIRCACHE)
550 if (tc_stat
.ramcache
&& is_dircache_intact())
551 idx_id
= find_entry_ram(filename
, NULL
);
555 idx_id
= find_entry_disk(filename
);
560 bool tagcache_find_index(struct tagcache_search
*tcs
, const char *filename
)
567 idx_id
= find_index(filename
);
571 if (!tagcache_search(tcs
, tag_filename
))
574 tcs
->entry_count
= 0;
575 tcs
->idx_id
= idx_id
;
580 static bool get_index(int masterfd
, int idxid
,
581 struct index_entry
*idx
, bool use_ram
)
583 bool localfd
= false;
587 logf("Incorrect idxid: %d", idxid
);
591 #ifdef HAVE_TC_RAMCACHE
592 if (tc_stat
.ramcache
&& use_ram
)
594 if (hdr
->indices
[idxid
].flag
& FLAG_DELETED
)
597 memcpy(idx
, &hdr
->indices
[idxid
], sizeof(struct index_entry
));
606 struct master_header tcmh
;
609 masterfd
= open_master_fd(&tcmh
, false);
614 lseek(masterfd
, idxid
* sizeof(struct index_entry
)
615 + sizeof(struct master_header
), SEEK_SET
);
616 if (ecread(masterfd
, idx
, 1, index_entry_ec
, tc_stat
.econ
)
617 != sizeof(struct index_entry
))
619 logf("read error #3");
629 if (idx
->flag
& FLAG_DELETED
)
637 static bool write_index(int masterfd
, int idxid
, struct index_entry
*idx
)
639 /* We need to exclude all memory only flags & tags when writing to disk. */
640 if (idx
->flag
& FLAG_DIRCACHE
)
642 logf("memory only flags!");
646 #ifdef HAVE_TC_RAMCACHE
647 /* Only update numeric data. Writing the whole index to RAM by memcpy
648 * destroys dircache pointers!
650 if (tc_stat
.ramcache
)
653 struct index_entry
*idx_ram
= &hdr
->indices
[idxid
];
655 for (tag
= 0; tag
< TAG_COUNT
; tag
++)
657 if (tagcache_is_numeric_tag(tag
))
659 idx_ram
->tag_seek
[tag
] = idx
->tag_seek
[tag
];
663 /* Don't touch the dircache flag or attributes. */
664 idx_ram
->flag
= (idx
->flag
& 0x0000ffff)
665 | (idx_ram
->flag
& (0xffff0000 | FLAG_DIRCACHE
));
669 lseek(masterfd
, idxid
* sizeof(struct index_entry
)
670 + sizeof(struct master_header
), SEEK_SET
);
671 if (ecwrite(masterfd
, idx
, 1, index_entry_ec
, tc_stat
.econ
)
672 != sizeof(struct index_entry
))
674 logf("write error #3");
675 logf("idxid: %d", idxid
);
682 #endif /* !__PCTOOL__ */
684 static bool open_files(struct tagcache_search
*tcs
, int tag
)
686 if (tcs
->idxfd
[tag
] < 0)
690 snprintf(fn
, sizeof fn
, TAGCACHE_FILE_INDEX
, tag
);
691 tcs
->idxfd
[tag
] = open(fn
, O_RDONLY
);
694 if (tcs
->idxfd
[tag
] < 0)
696 logf("File not open!");
703 static bool retrieve(struct tagcache_search
*tcs
, struct index_entry
*idx
,
704 int tag
, char *buf
, long size
)
706 struct tagfile_entry tfe
;
711 if (tagcache_is_numeric_tag(tag
))
714 seek
= idx
->tag_seek
[tag
];
717 logf("Retrieve failed");
721 #ifdef HAVE_TC_RAMCACHE
724 struct tagfile_entry
*ep
;
726 # ifdef HAVE_DIRCACHE
727 if (tag
== tag_filename
&& (idx
->flag
& FLAG_DIRCACHE
)
728 && is_dircache_intact())
730 dircache_copy_path((struct dirent
*)seek
,
736 if (tag
!= tag_filename
)
738 ep
= (struct tagfile_entry
*)&hdr
->tags
[tag
][seek
];
739 strncpy(buf
, ep
->tag_data
, size
-1);
746 if (!open_files(tcs
, tag
))
749 lseek(tcs
->idxfd
[tag
], seek
, SEEK_SET
);
750 if (ecread(tcs
->idxfd
[tag
], &tfe
, 1, tagfile_entry_ec
, tc_stat
.econ
)
751 != sizeof(struct tagfile_entry
))
753 logf("read error #5");
757 if (tfe
.tag_length
>= size
)
759 logf("too small buffer");
763 if (read(tcs
->idxfd
[tag
], buf
, tfe
.tag_length
) !=
766 logf("read error #6");
770 buf
[tfe
.tag_length
] = '\0';
775 static long check_virtual_tags(int tag
, const struct index_entry
*idx
)
781 case tag_virt_length_sec
:
782 data
= (idx
->tag_seek
[tag_length
]/1000) % 60;
785 case tag_virt_length_min
:
786 data
= (idx
->tag_seek
[tag_length
]/1000) / 60;
789 case tag_virt_playtime_sec
:
790 data
= (idx
->tag_seek
[tag_playtime
]/1000) % 60;
793 case tag_virt_playtime_min
:
794 data
= (idx
->tag_seek
[tag_playtime
]/1000) / 60;
797 case tag_virt_autoscore
:
798 if (idx
->tag_seek
[tag_length
] == 0
799 || idx
->tag_seek
[tag_playcount
] == 0)
805 data
= 100 * idx
->tag_seek
[tag_playtime
]
806 / idx
->tag_seek
[tag_length
]
807 / idx
->tag_seek
[tag_playcount
];
811 /* How many commits before the file has been added to the DB. */
812 case tag_virt_entryage
:
813 data
= current_tcmh
.commitid
- idx
->tag_seek
[tag_commitid
] - 1;
817 data
= idx
->tag_seek
[tag
];
823 long tagcache_get_numeric(const struct tagcache_search
*tcs
, int tag
)
825 struct index_entry idx
;
830 if (!tagcache_is_numeric_tag(tag
))
833 if (!get_index(tcs
->masterfd
, tcs
->idx_id
, &idx
, true))
836 return check_virtual_tags(tag
, &idx
);
839 inline static bool str_ends_with(const char *str1
, const char *str2
)
841 int str_len
= strlen(str1
);
842 int clause_len
= strlen(str2
);
844 if (clause_len
> str_len
)
847 return !strcasecmp(&str1
[str_len
- clause_len
], str2
);
850 inline static bool str_oneof(const char *str
, const char *list
)
853 int l
, len
= strlen(str
);
857 sep
= strchr(list
, '|');
858 l
= sep
? (long)sep
- (long)list
: (int)strlen(list
);
859 if ((l
==len
) && !strncasecmp(str
, list
, len
))
861 list
+= sep
? l
+ 1 : l
;
867 static bool check_against_clause(long numeric
, const char *str
,
868 const struct tagcache_search_clause
*clause
)
872 switch (clause
->type
)
875 return numeric
== clause
->numeric_data
;
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 logf("Incorrect numeric tag: %d", clause
->type
);
892 switch (clause
->type
)
895 return !strcasecmp(clause
->str
, str
);
897 return strcasecmp(clause
->str
, str
);
899 return 0>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
);
906 case clause_contains
:
907 return (strcasestr(str
, clause
->str
) != NULL
);
908 case clause_not_contains
:
909 return (strcasestr(str
, clause
->str
) == NULL
);
910 case clause_begins_with
:
911 return (strcasestr(str
, clause
->str
) == str
);
912 case clause_not_begins_with
:
913 return (strcasestr(str
, clause
->str
) != str
);
914 case clause_ends_with
:
915 return str_ends_with(str
, clause
->str
);
916 case clause_not_ends_with
:
917 return !str_ends_with(str
, clause
->str
);
919 return str_oneof(str
, clause
->str
);
922 logf("Incorrect tag: %d", clause
->type
);
929 static bool check_clauses(struct tagcache_search
*tcs
,
930 struct index_entry
*idx
,
931 struct tagcache_search_clause
**clause
, int count
)
935 #ifdef HAVE_TC_RAMCACHE
938 /* Go through all conditional clauses. */
939 for (i
= 0; i
< count
; i
++)
941 struct tagfile_entry
*tfe
;
946 seek
= check_virtual_tags(clause
[i
]->tag
, idx
);
948 if (!tagcache_is_numeric_tag(clause
[i
]->tag
))
950 if (clause
[i
]->tag
== tag_filename
)
952 retrieve(tcs
, idx
, tag_filename
, buf
, sizeof buf
);
957 tfe
= (struct tagfile_entry
*)&hdr
->tags
[clause
[i
]->tag
][seek
];
962 if (!check_against_clause(seek
, str
, clause
[i
]))
969 /* Check for conditions. */
970 for (i
= 0; i
< count
; i
++)
972 struct tagfile_entry tfe
;
976 seek
= check_virtual_tags(clause
[i
]->tag
, idx
);
978 memset(str
, 0, sizeof str
);
979 if (!tagcache_is_numeric_tag(clause
[i
]->tag
))
981 int fd
= tcs
->idxfd
[clause
[i
]->tag
];
982 lseek(fd
, seek
, SEEK_SET
);
983 ecread(fd
, &tfe
, 1, tagfile_entry_ec
, tc_stat
.econ
);
984 if (tfe
.tag_length
>= (int)sizeof(str
))
986 logf("Too long tag read!");
990 read(fd
, str
, tfe
.tag_length
);
992 /* Check if entry has been deleted. */
997 if (!check_against_clause(seek
, str
, clause
[i
]))
1005 bool tagcache_check_clauses(struct tagcache_search
*tcs
,
1006 struct tagcache_search_clause
**clause
, int count
)
1008 struct index_entry idx
;
1013 if (!get_index(tcs
->masterfd
, tcs
->idx_id
, &idx
, true))
1016 return check_clauses(tcs
, &idx
, clause
, count
);
1019 static bool add_uniqbuf(struct tagcache_search
*tcs
, unsigned long id
)
1023 /* If uniq buffer is not defined we must return true for search to work. */
1024 if (tcs
->unique_list
== NULL
1025 || (!tagcache_is_unique_tag(tcs
->type
)
1026 && !tagcache_is_numeric_tag(tcs
->type
)))
1031 for (i
= 0; i
< tcs
->unique_list_count
; i
++)
1033 /* Return false if entry is found. */
1034 if (tcs
->unique_list
[i
] == id
)
1038 if (tcs
->unique_list_count
< tcs
->unique_list_capacity
)
1040 tcs
->unique_list
[i
] = id
;
1041 tcs
->unique_list_count
++;
1047 static bool build_lookup_list(struct tagcache_search
*tcs
)
1049 struct index_entry entry
;
1052 tcs
->seek_list_count
= 0;
1054 #ifdef HAVE_TC_RAMCACHE
1059 for (i
= tcs
->seek_pos
; i
< hdr
->h
.tch
.entry_count
; i
++)
1061 struct index_entry
*idx
= &hdr
->indices
[i
];
1062 if (tcs
->seek_list_count
== SEEK_LIST_SIZE
)
1065 /* Skip deleted files. */
1066 if (idx
->flag
& FLAG_DELETED
)
1069 /* Go through all filters.. */
1070 for (j
= 0; j
< tcs
->filter_count
; j
++)
1072 if (idx
->tag_seek
[tcs
->filter_tag
[j
]] != tcs
->filter_seek
[j
])
1078 if (j
< tcs
->filter_count
)
1081 /* Check for conditions. */
1082 if (!check_clauses(tcs
, idx
, tcs
->clause
, tcs
->clause_count
))
1085 /* Add to the seek list if not already in uniq buffer. */
1086 if (!add_uniqbuf(tcs
, idx
->tag_seek
[tcs
->type
]))
1090 tcs
->seek_list
[tcs
->seek_list_count
] = idx
->tag_seek
[tcs
->type
];
1091 tcs
->seek_flags
[tcs
->seek_list_count
] = idx
->flag
;
1092 tcs
->seek_list_count
++;
1097 return tcs
->seek_list_count
> 0;
1101 lseek(tcs
->masterfd
, tcs
->seek_pos
* sizeof(struct index_entry
) +
1102 sizeof(struct master_header
), SEEK_SET
);
1104 while (ecread(tcs
->masterfd
, &entry
, 1, index_entry_ec
, tc_stat
.econ
)
1105 == sizeof(struct index_entry
))
1107 if (tcs
->seek_list_count
== SEEK_LIST_SIZE
)
1112 /* Check if entry has been deleted. */
1113 if (entry
.flag
& FLAG_DELETED
)
1116 /* Go through all filters.. */
1117 for (i
= 0; i
< tcs
->filter_count
; i
++)
1119 if (entry
.tag_seek
[tcs
->filter_tag
[i
]] != tcs
->filter_seek
[i
])
1123 if (i
< tcs
->filter_count
)
1126 /* Check for conditions. */
1127 if (!check_clauses(tcs
, &entry
, tcs
->clause
, tcs
->clause_count
))
1130 /* Add to the seek list if not already in uniq buffer. */
1131 if (!add_uniqbuf(tcs
, entry
.tag_seek
[tcs
->type
]))
1135 tcs
->seek_list
[tcs
->seek_list_count
] = entry
.tag_seek
[tcs
->type
];
1136 tcs
->seek_flags
[tcs
->seek_list_count
] = entry
.flag
;
1137 tcs
->seek_list_count
++;
1142 return tcs
->seek_list_count
> 0;
1146 static void remove_files(void)
1151 tc_stat
.ready
= false;
1152 tc_stat
.ramcache
= false;
1153 tc_stat
.econ
= false;
1154 remove(TAGCACHE_FILE_MASTER
);
1155 for (i
= 0; i
< TAG_COUNT
; i
++)
1157 if (tagcache_is_numeric_tag(i
))
1160 snprintf(buf
, sizeof buf
, TAGCACHE_FILE_INDEX
, i
);
1166 static bool check_all_headers(void)
1168 struct master_header myhdr
;
1169 struct tagcache_header tch
;
1173 if ( (fd
= open_master_fd(&myhdr
, false)) < 0)
1179 logf("tagcache is dirty!");
1183 memcpy(¤t_tcmh
, &myhdr
, sizeof(struct master_header
));
1185 for (tag
= 0; tag
< TAG_COUNT
; tag
++)
1187 if (tagcache_is_numeric_tag(tag
))
1190 if ( (fd
= open_tag_fd(&tch
, tag
, false)) < 0)
1199 bool tagcache_search(struct tagcache_search
*tcs
, int tag
)
1201 struct tagcache_header tag_hdr
;
1202 struct master_header master_hdr
;
1205 if (tcs
->initialized
)
1206 tagcache_search_finish(tcs
);
1211 memset(tcs
, 0, sizeof(struct tagcache_search
));
1212 if (tc_stat
.commit_step
> 0 || !tc_stat
.ready
)
1215 tcs
->position
= sizeof(struct tagcache_header
);
1218 tcs
->seek_list_count
= 0;
1219 tcs
->filter_count
= 0;
1222 for (i
= 0; i
< TAG_COUNT
; i
++)
1225 #ifndef HAVE_TC_RAMCACHE
1226 tcs
->ramsearch
= false;
1228 tcs
->ramsearch
= tc_stat
.ramcache
;
1231 tcs
->entry_count
= hdr
->entry_count
[tcs
->type
];
1236 if (!tagcache_is_numeric_tag(tcs
->type
))
1238 tcs
->idxfd
[tcs
->type
] = open_tag_fd(&tag_hdr
, tcs
->type
, false);
1239 if (tcs
->idxfd
[tcs
->type
] < 0)
1243 /* Always open as R/W so we can pass tcs to functions that modify data also
1244 * without failing. */
1245 tcs
->masterfd
= open_master_fd(&master_hdr
, true);
1247 if (tcs
->masterfd
< 0)
1252 tcs
->initialized
= true;
1258 void tagcache_search_set_uniqbuf(struct tagcache_search
*tcs
,
1259 void *buffer
, long length
)
1261 tcs
->unique_list
= (unsigned long *)buffer
;
1262 tcs
->unique_list_capacity
= length
/ sizeof(*tcs
->unique_list
);
1263 tcs
->unique_list_count
= 0;
1266 bool tagcache_search_add_filter(struct tagcache_search
*tcs
,
1269 if (tcs
->filter_count
== TAGCACHE_MAX_FILTERS
)
1272 if (!tagcache_is_unique_tag(tag
) || tagcache_is_numeric_tag(tag
))
1275 tcs
->filter_tag
[tcs
->filter_count
] = tag
;
1276 tcs
->filter_seek
[tcs
->filter_count
] = seek
;
1277 tcs
->filter_count
++;
1282 bool tagcache_search_add_clause(struct tagcache_search
*tcs
,
1283 struct tagcache_search_clause
*clause
)
1287 if (tcs
->clause_count
>= TAGCACHE_MAX_CLAUSES
)
1289 logf("Too many clauses");
1293 /* Check if there is already a similar filter in present (filters are
1294 * much faster than clauses).
1296 for (i
= 0; i
< tcs
->filter_count
; i
++)
1298 if (tcs
->filter_tag
[i
] == clause
->tag
)
1302 if (!tagcache_is_numeric_tag(clause
->tag
) && tcs
->idxfd
[clause
->tag
] < 0)
1306 snprintf(buf
, sizeof buf
, TAGCACHE_FILE_INDEX
, clause
->tag
);
1307 tcs
->idxfd
[clause
->tag
] = open(buf
, O_RDONLY
);
1310 tcs
->clause
[tcs
->clause_count
] = clause
;
1311 tcs
->clause_count
++;
1316 /* TODO: Remove this mess. */
1317 #ifdef HAVE_DIRCACHE
1318 #define TAG_FILENAME_RAM(tcs) ((tcs->type == tag_filename) \
1319 ? ((flag & FLAG_DIRCACHE) && is_dircache_intact()) : 1)
1321 #define TAG_FILENAME_RAM(tcs) (tcs->type != tag_filename)
1324 static bool get_next(struct tagcache_search
*tcs
)
1326 static char buf
[TAG_MAXLEN
+32];
1327 struct tagfile_entry entry
;
1330 if (!tcs
->valid
|| !tc_stat
.ready
)
1333 if (tcs
->idxfd
[tcs
->type
] < 0 && !tagcache_is_numeric_tag(tcs
->type
)
1334 #ifdef HAVE_TC_RAMCACHE
1340 /* Relative fetch. */
1341 if (tcs
->filter_count
> 0 || tcs
->clause_count
> 0
1342 || tagcache_is_numeric_tag(tcs
->type
))
1344 /* Check for end of list. */
1345 if (tcs
->seek_list_count
== 0)
1347 /* Try to fetch more. */
1348 if (!build_lookup_list(tcs
))
1355 tcs
->seek_list_count
--;
1356 flag
= tcs
->seek_flags
[tcs
->seek_list_count
];
1358 /* Seek stream to the correct position and continue to direct fetch. */
1359 if ((!tcs
->ramsearch
|| !TAG_FILENAME_RAM(tcs
))
1360 && !tagcache_is_numeric_tag(tcs
->type
))
1362 if (!open_files(tcs
, tcs
->type
))
1365 lseek(tcs
->idxfd
[tcs
->type
], tcs
->seek_list
[tcs
->seek_list_count
], SEEK_SET
);
1368 tcs
->position
= tcs
->seek_list
[tcs
->seek_list_count
];
1371 if (tagcache_is_numeric_tag(tcs
->type
))
1373 snprintf(buf
, sizeof(buf
), "%d", tcs
->position
);
1374 tcs
->result_seek
= tcs
->position
;
1376 tcs
->result_len
= strlen(buf
) + 1;
1381 #ifdef HAVE_TC_RAMCACHE
1382 if (tcs
->ramsearch
&& TAG_FILENAME_RAM(tcs
))
1384 struct tagfile_entry
*ep
;
1386 if (tcs
->entry_count
== 0)
1393 tcs
->result_seek
= tcs
->position
;
1395 # ifdef HAVE_DIRCACHE
1396 if (tcs
->type
== tag_filename
)
1398 dircache_copy_path((struct dirent
*)tcs
->position
,
1401 tcs
->result_len
= strlen(buf
) + 1;
1402 tcs
->idx_id
= FLAG_GET_ATTR(flag
);
1403 tcs
->ramresult
= false;
1409 ep
= (struct tagfile_entry
*)&hdr
->tags
[tcs
->type
][tcs
->position
];
1410 tcs
->position
+= sizeof(struct tagfile_entry
) + ep
->tag_length
;
1411 tcs
->result
= ep
->tag_data
;
1412 tcs
->result_len
= strlen(tcs
->result
) + 1;
1413 tcs
->idx_id
= ep
->idx_id
;
1414 tcs
->ramresult
= true;
1421 if (!open_files(tcs
, tcs
->type
))
1424 tcs
->result_seek
= lseek(tcs
->idxfd
[tcs
->type
], 0, SEEK_CUR
);
1425 if (ecread(tcs
->idxfd
[tcs
->type
], &entry
, 1,
1426 tagfile_entry_ec
, tc_stat
.econ
) != sizeof(struct tagfile_entry
))
1434 if (entry
.tag_length
> (long)sizeof(buf
))
1437 logf("too long tag #2");
1441 if (read(tcs
->idxfd
[tcs
->type
], buf
, entry
.tag_length
) != entry
.tag_length
)
1444 logf("read error #4");
1449 tcs
->result_len
= strlen(tcs
->result
) + 1;
1450 tcs
->idx_id
= entry
.idx_id
;
1451 tcs
->ramresult
= false;
1456 bool tagcache_get_next(struct tagcache_search
*tcs
)
1458 while (get_next(tcs
))
1460 if (tcs
->result_len
> 1)
1467 bool tagcache_retrieve(struct tagcache_search
*tcs
, int idxid
,
1468 int tag
, char *buf
, long size
)
1470 struct index_entry idx
;
1473 if (!get_index(tcs
->masterfd
, idxid
, &idx
, true))
1476 return retrieve(tcs
, &idx
, tag
, buf
, size
);
1479 static bool update_master_header(void)
1481 struct master_header myhdr
;
1487 if ( (fd
= open_master_fd(&myhdr
, true)) < 0)
1490 myhdr
.serial
= current_tcmh
.serial
;
1491 myhdr
.commitid
= current_tcmh
.commitid
;
1492 myhdr
.dirty
= current_tcmh
.dirty
;
1495 lseek(fd
, 0, SEEK_SET
);
1496 ecwrite(fd
, &myhdr
, 1, master_header_ec
, tc_stat
.econ
);
1499 #ifdef HAVE_TC_RAMCACHE
1502 hdr
->h
.serial
= current_tcmh
.serial
;
1503 hdr
->h
.commitid
= current_tcmh
.commitid
;
1504 hdr
->h
.dirty
= current_tcmh
.dirty
;
1513 void tagcache_modify(struct tagcache_search
*tcs
, int type
, const char *text
)
1515 struct tagentry
*entry
;
1517 if (tcs
->type
!= tag_title
)
1520 /* We will need reserve buffer for this. */
1523 struct tagfile_entry
*ep
;
1525 ep
= (struct tagfile_entry
*)&hdr
->tags
[tcs
->type
][tcs
->result_seek
];
1526 tcs
->seek_list
[tcs
->seek_list_count
];
1529 entry
= find_entry_ram();
1534 void tagcache_search_finish(struct tagcache_search
*tcs
)
1538 if (!tcs
->initialized
)
1541 if (tcs
->masterfd
>= 0)
1543 close(tcs
->masterfd
);
1547 for (i
= 0; i
< TAG_COUNT
; i
++)
1549 if (tcs
->idxfd
[i
] >= 0)
1551 close(tcs
->idxfd
[i
]);
1556 tcs
->ramsearch
= false;
1558 tcs
->initialized
= 0;
1563 #if defined(HAVE_TC_RAMCACHE) && defined(HAVE_DIRCACHE)
1564 static struct tagfile_entry
*get_tag(const struct index_entry
*entry
, int tag
)
1566 return (struct tagfile_entry
*)&hdr
->tags
[tag
][entry
->tag_seek
[tag
]];
1569 static long get_tag_numeric(const struct index_entry
*entry
, int tag
)
1571 return check_virtual_tags(tag
, entry
);
1574 static char* get_tag_string(const struct index_entry
*entry
, int tag
)
1576 char* s
= get_tag(entry
, tag
)->tag_data
;
1577 return strcmp(s
, UNTAGGED
) ? s
: NULL
;
1580 bool tagcache_fill_tags(struct mp3entry
*id3
, const char *filename
)
1582 struct index_entry
*entry
;
1588 /* Find the corresponding entry in tagcache. */
1589 idx_id
= find_entry_ram(filename
, NULL
);
1590 if (idx_id
< 0 || !tc_stat
.ramcache
)
1593 entry
= &hdr
->indices
[idx_id
];
1595 id3
->title
= get_tag_string(entry
, tag_title
);
1596 id3
->artist
= get_tag_string(entry
, tag_artist
);
1597 id3
->album
= get_tag_string(entry
, tag_album
);
1598 id3
->genre_string
= get_tag_string(entry
, tag_genre
);
1599 id3
->composer
= get_tag_string(entry
, tag_composer
);
1600 id3
->comment
= get_tag_string(entry
, tag_comment
);
1601 id3
->albumartist
= get_tag_string(entry
, tag_albumartist
);
1602 id3
->grouping
= get_tag_string(entry
, tag_grouping
);
1604 id3
->playcount
= get_tag_numeric(entry
, tag_playcount
);
1605 id3
->rating
= get_tag_numeric(entry
, tag_rating
);
1606 id3
->lastplayed
= get_tag_numeric(entry
, tag_lastplayed
);
1607 id3
->score
= get_tag_numeric(entry
, tag_virt_autoscore
) / 10;
1608 id3
->year
= get_tag_numeric(entry
, tag_year
);
1610 id3
->discnum
= get_tag_numeric(entry
, tag_discnumber
);
1611 id3
->tracknum
= get_tag_numeric(entry
, tag_tracknumber
);
1612 id3
->bitrate
= get_tag_numeric(entry
, tag_bitrate
);
1613 if (id3
->bitrate
== 0)
1620 static inline void write_item(const char *item
)
1622 int len
= strlen(item
) + 1;
1625 write(cachefd
, item
, len
);
1628 static int check_if_empty(char **tag
)
1632 if (*tag
== NULL
|| **tag
== '\0')
1635 return sizeof(UNTAGGED
); /* Tag length */
1638 length
= strlen(*tag
);
1639 if (length
> TAG_MAXLEN
)
1641 logf("over length tag: %s", *tag
);
1642 length
= TAG_MAXLEN
;
1643 (*tag
)[length
] = '\0';
1649 #define ADD_TAG(entry,tag,data) \
1651 entry.tag_offset[tag] = offset; \
1652 entry.tag_length[tag] = check_if_empty(data); \
1653 offset += entry.tag_length[tag]
1655 static void add_tagcache(char *path
, unsigned long mtime
1656 #if defined(HAVE_TC_RAMCACHE) && defined(HAVE_DIRCACHE)
1657 ,const struct dirent
*dc
1661 struct mp3entry id3
;
1662 struct temp_file_entry entry
;
1666 char tracknumfix
[3];
1668 int path_length
= strlen(path
);
1669 bool has_albumartist
;
1675 /* Check for overlength file path. */
1676 if (path_length
> TAG_MAXLEN
)
1678 /* Path can't be shortened. */
1679 logf("Too long path: %s", path
);
1683 /* Check if the file is supported. */
1684 if (probe_file_format(path
) == AFMT_UNKNOWN
)
1687 /* Check if the file is already cached. */
1688 #if defined(HAVE_TC_RAMCACHE) && defined(HAVE_DIRCACHE)
1689 if (tc_stat
.ramcache
&& is_dircache_intact())
1691 idx_id
= find_entry_ram(path
, dc
);
1696 if (filenametag_fd
>= 0)
1698 idx_id
= find_entry_disk(path
);
1702 /* Check if file has been modified. */
1705 struct index_entry idx
;
1707 /* TODO: Mark that the index exists (for fast reverse scan) */
1708 //found_idx[idx_id/8] |= idx_id%8;
1710 if (!get_index(-1, idx_id
, &idx
, true))
1712 logf("failed to retrieve index entry");
1716 if ((unsigned long)idx
.tag_seek
[tag_mtime
] == mtime
)
1718 /* No changes to file. */
1722 /* Metadata might have been changed. Delete the entry. */
1723 logf("Re-adding: %s", path
);
1724 if (!delete_entry(idx_id
))
1726 logf("delete_entry failed: %d", idx_id
);
1731 fd
= open(path
, O_RDONLY
);
1734 logf("open fail: %s", path
);
1738 memset(&id3
, 0, sizeof(struct mp3entry
));
1739 memset(&entry
, 0, sizeof(struct temp_file_entry
));
1740 memset(&tracknumfix
, 0, sizeof(tracknumfix
));
1741 ret
= get_metadata(&id3
, fd
, path
);
1747 logf("-> %s", path
);
1749 /* Generate track number if missing. */
1750 if (id3
.tracknum
<= 0)
1752 const char *p
= strrchr(path
, '.');
1755 p
= &path
[strlen(path
)-1];
1759 if (isdigit(*p
) && isdigit(*(p
-1)))
1761 tracknumfix
[1] = *p
--;
1762 tracknumfix
[0] = *p
;
1768 if (tracknumfix
[0] != '\0')
1770 id3
.tracknum
= atoi(tracknumfix
);
1771 /* Set a flag to indicate track number has been generated. */
1772 entry
.flag
|= FLAG_TRKNUMGEN
;
1776 /* Unable to generate track number. */
1782 entry
.tag_offset
[tag_year
] = id3
.year
;
1783 entry
.tag_offset
[tag_discnumber
] = id3
.discnum
;
1784 entry
.tag_offset
[tag_tracknumber
] = id3
.tracknum
;
1785 entry
.tag_offset
[tag_length
] = id3
.length
;
1786 entry
.tag_offset
[tag_bitrate
] = id3
.bitrate
;
1787 entry
.tag_offset
[tag_mtime
] = mtime
;
1790 has_albumartist
= id3
.albumartist
!= NULL
1791 && strlen(id3
.albumartist
) > 0;
1792 has_grouping
= id3
.grouping
!= NULL
1793 && strlen(id3
.grouping
) > 0;
1795 ADD_TAG(entry
, tag_filename
, &path
);
1796 ADD_TAG(entry
, tag_title
, &id3
.title
);
1797 ADD_TAG(entry
, tag_artist
, &id3
.artist
);
1798 ADD_TAG(entry
, tag_album
, &id3
.album
);
1799 ADD_TAG(entry
, tag_genre
, &id3
.genre_string
);
1800 ADD_TAG(entry
, tag_composer
, &id3
.composer
);
1801 ADD_TAG(entry
, tag_comment
, &id3
.comment
);
1802 if (has_albumartist
)
1804 ADD_TAG(entry
, tag_albumartist
, &id3
.albumartist
);
1808 ADD_TAG(entry
, tag_albumartist
, &id3
.artist
);
1812 ADD_TAG(entry
, tag_grouping
, &id3
.grouping
);
1816 ADD_TAG(entry
, tag_grouping
, &id3
.title
);
1818 entry
.data_length
= offset
;
1820 /* Write the header */
1821 write(cachefd
, &entry
, sizeof(struct temp_file_entry
));
1823 /* And tags also... Correct order is critical */
1825 write_item(id3
.title
);
1826 write_item(id3
.artist
);
1827 write_item(id3
.album
);
1828 write_item(id3
.genre_string
);
1829 write_item(id3
.composer
);
1830 write_item(id3
.comment
);
1831 if (has_albumartist
)
1833 write_item(id3
.albumartist
);
1837 write_item(id3
.artist
);
1841 write_item(id3
.grouping
);
1845 write_item(id3
.title
);
1847 total_entry_count
++;
1850 static bool tempbuf_insert(char *str
, int id
, int idx_id
, bool unique
)
1852 struct tempbuf_searchidx
*index
= (struct tempbuf_searchidx
*)tempbuf
;
1853 int len
= strlen(str
)+1;
1856 unsigned *crcbuf
= (unsigned *)&tempbuf
[tempbuf_size
-4];
1857 char buf
[TAG_MAXLEN
+32];
1859 for (i
= 0; str
[i
] != '\0' && i
< (int)sizeof(buf
)-1; i
++)
1860 buf
[i
] = tolower(str
[i
]);
1863 crc32
= crc_32(buf
, i
, 0xffffffff);
1867 /* Check if the crc does not exist -> entry does not exist for sure. */
1868 for (i
= 0; i
< tempbufidx
; i
++)
1870 if (crcbuf
[-i
] != crc32
)
1873 if (!strcasecmp(str
, index
[i
].str
))
1875 if (id
< 0 || id
>= lookup_buffer_depth
)
1877 logf("lookup buf overf.: %d", id
);
1881 lookup
[id
] = &index
[i
];
1887 /* Insert to CRC buffer. */
1888 crcbuf
[-tempbufidx
] = crc32
;
1891 /* Insert it to the buffer. */
1892 tempbuf_left
-= len
;
1893 if (tempbuf_left
- 4 < 0 || tempbufidx
>= commit_entry_count
-1)
1896 if (id
>= lookup_buffer_depth
)
1898 logf("lookup buf overf. #2: %d", id
);
1904 lookup
[id
] = &index
[tempbufidx
];
1905 index
[tempbufidx
].idlist
.id
= id
;
1908 index
[tempbufidx
].idlist
.id
= -1;
1910 index
[tempbufidx
].idlist
.next
= NULL
;
1911 index
[tempbufidx
].idx_id
= idx_id
;
1912 index
[tempbufidx
].seek
= -1;
1913 index
[tempbufidx
].str
= &tempbuf
[tempbuf_pos
];
1914 memcpy(index
[tempbufidx
].str
, str
, len
);
1921 static int compare(const void *p1
, const void *p2
)
1925 struct tempbuf_searchidx
*e1
= (struct tempbuf_searchidx
*)p1
;
1926 struct tempbuf_searchidx
*e2
= (struct tempbuf_searchidx
*)p2
;
1928 if (strcmp(e1
->str
, UNTAGGED
) == 0)
1930 if (strcmp(e2
->str
, UNTAGGED
) == 0)
1934 else if (strcmp(e2
->str
, UNTAGGED
) == 0)
1937 return strncasecmp(e1
->str
, e2
->str
, TAG_MAXLEN
);
1940 static int tempbuf_sort(int fd
)
1942 struct tempbuf_searchidx
*index
= (struct tempbuf_searchidx
*)tempbuf
;
1943 struct tagfile_entry fe
;
1947 /* Generate reverse lookup entries. */
1948 for (i
= 0; i
< lookup_buffer_depth
; i
++)
1950 struct tempbuf_id_list
*idlist
;
1955 if (lookup
[i
]->idlist
.id
== i
)
1958 idlist
= &lookup
[i
]->idlist
;
1959 while (idlist
->next
!= NULL
)
1960 idlist
= idlist
->next
;
1962 tempbuf_left
-= sizeof(struct tempbuf_id_list
);
1963 if (tempbuf_left
- 4 < 0)
1966 idlist
->next
= (struct tempbuf_id_list
*)&tempbuf
[tempbuf_pos
];
1967 if (tempbuf_pos
& 0x03)
1969 tempbuf_pos
= (tempbuf_pos
& ~0x03) + 0x04;
1971 idlist
->next
= (struct tempbuf_id_list
*)&tempbuf
[tempbuf_pos
];
1973 tempbuf_pos
+= sizeof(struct tempbuf_id_list
);
1975 idlist
= idlist
->next
;
1977 idlist
->next
= NULL
;
1982 qsort(index
, tempbufidx
, sizeof(struct tempbuf_searchidx
), compare
);
1983 memset(lookup
, 0, lookup_buffer_depth
* sizeof(struct tempbuf_searchidx
**));
1985 for (i
= 0; i
< tempbufidx
; i
++)
1987 struct tempbuf_id_list
*idlist
= &index
[i
].idlist
;
1989 /* Fix the lookup list. */
1990 while (idlist
!= NULL
)
1992 if (idlist
->id
>= 0)
1993 lookup
[idlist
->id
] = &index
[i
];
1994 idlist
= idlist
->next
;
1997 index
[i
].seek
= lseek(fd
, 0, SEEK_CUR
);
1998 length
= strlen(index
[i
].str
) + 1;
1999 fe
.tag_length
= length
;
2000 fe
.idx_id
= index
[i
].idx_id
;
2002 /* Check the chunk alignment. */
2003 if ((fe
.tag_length
+ sizeof(struct tagfile_entry
))
2004 % TAGFILE_ENTRY_CHUNK_LENGTH
)
2006 fe
.tag_length
+= TAGFILE_ENTRY_CHUNK_LENGTH
-
2007 ((fe
.tag_length
+ sizeof(struct tagfile_entry
))
2008 % TAGFILE_ENTRY_CHUNK_LENGTH
);
2011 #ifdef TAGCACHE_STRICT_ALIGN
2012 /* Make sure the entry is long aligned. */
2013 if (index
[i
].seek
& 0x03)
2015 logf("tempbuf_sort: alignment error!");
2020 if (ecwrite(fd
, &fe
, 1, tagfile_entry_ec
, tc_stat
.econ
) !=
2021 sizeof(struct tagfile_entry
))
2023 logf("tempbuf_sort: write error #1");
2027 if (write(fd
, index
[i
].str
, length
) != length
)
2029 logf("tempbuf_sort: write error #2");
2033 /* Write some padding. */
2034 if (fe
.tag_length
- length
> 0)
2035 write(fd
, "XXXXXXXX", fe
.tag_length
- length
);
2041 inline static struct tempbuf_searchidx
* tempbuf_locate(int id
)
2043 if (id
< 0 || id
>= lookup_buffer_depth
)
2050 inline static int tempbuf_find_location(int id
)
2052 struct tempbuf_searchidx
*entry
;
2054 entry
= tempbuf_locate(id
);
2061 static bool build_numeric_indices(struct tagcache_header
*h
, int tmpfd
)
2063 struct master_header tcmh
;
2064 struct index_entry idx
;
2067 struct temp_file_entry
*entrybuf
= (struct temp_file_entry
*)tempbuf
;
2069 int entries_processed
= 0;
2071 char buf
[TAG_MAXLEN
];
2073 max_entries
= tempbuf_size
/ sizeof(struct temp_file_entry
) - 1;
2075 logf("Building numeric indices...");
2076 lseek(tmpfd
, sizeof(struct tagcache_header
), SEEK_SET
);
2078 if ( (masterfd
= open_master_fd(&tcmh
, true)) < 0)
2081 masterfd_pos
= lseek(masterfd
, tcmh
.tch
.entry_count
* sizeof(struct index_entry
),
2083 if (masterfd_pos
== filesize(masterfd
))
2085 logf("we can't append!");
2090 while (entries_processed
< h
->entry_count
)
2092 int count
= MIN(h
->entry_count
- entries_processed
, max_entries
);
2094 /* Read in as many entries as possible. */
2095 for (i
= 0; i
< count
; i
++)
2097 struct temp_file_entry
*tfe
= &entrybuf
[i
];
2100 /* Read in numeric data. */
2101 if (read(tmpfd
, tfe
, sizeof(struct temp_file_entry
)) !=
2102 sizeof(struct temp_file_entry
))
2104 logf("read fail #1");
2109 datastart
= lseek(tmpfd
, 0, SEEK_CUR
);
2112 * Read string data from the following tags:
2118 * A crc32 hash is calculated from the read data
2119 * and stored back to the data offset field kept in memory.
2121 #define tmpdb_read_string_tag(tag) \
2122 lseek(tmpfd, tfe->tag_offset[tag], SEEK_CUR); \
2123 if ((unsigned long)tfe->tag_length[tag] > sizeof buf) \
2125 logf("read fail: buffer overflow"); \
2130 if (read(tmpfd, buf, tfe->tag_length[tag]) != \
2131 tfe->tag_length[tag]) \
2133 logf("read fail #2"); \
2138 tfe->tag_offset[tag] = crc_32(buf, strlen(buf), 0xffffffff); \
2139 lseek(tmpfd, datastart, SEEK_SET)
2141 tmpdb_read_string_tag(tag_filename
);
2142 tmpdb_read_string_tag(tag_artist
);
2143 tmpdb_read_string_tag(tag_album
);
2144 tmpdb_read_string_tag(tag_title
);
2146 /* Seek to the end of the string data. */
2147 lseek(tmpfd
, tfe
->data_length
, SEEK_CUR
);
2150 /* Backup the master index position. */
2151 masterfd_pos
= lseek(masterfd
, 0, SEEK_CUR
);
2152 lseek(masterfd
, sizeof(struct master_header
), SEEK_SET
);
2154 /* Check if we can resurrect some deleted runtime statistics data. */
2155 for (i
= 0; i
< tcmh
.tch
.entry_count
; i
++)
2157 /* Read the index entry. */
2158 if (ecread(masterfd
, &idx
, 1, index_entry_ec
, tc_stat
.econ
)
2159 != sizeof(struct index_entry
))
2161 logf("read fail #3");
2167 * Skip unless the entry is marked as being deleted
2168 * or the data has already been resurrected.
2170 if (!(idx
.flag
& FLAG_DELETED
) || idx
.flag
& FLAG_RESURRECTED
)
2173 /* Now try to match the entry. */
2175 * To succesfully match a song, the following conditions
2178 * For numeric fields: tag_length
2179 * - Full identical match is required
2181 * If tag_filename matches, no further checking necessary.
2183 * For string hashes: tag_artist, tag_album, tag_title
2184 * - Two of these must match
2186 for (j
= 0; j
< count
; j
++)
2188 struct temp_file_entry
*tfe
= &entrybuf
[j
];
2190 /* Try to match numeric fields first. */
2191 if (tfe
->tag_offset
[tag_length
] != idx
.tag_seek
[tag_length
])
2194 /* Now it's time to do the hash matching. */
2195 if (tfe
->tag_offset
[tag_filename
] != idx
.tag_seek
[tag_filename
])
2197 int match_count
= 0;
2199 /* No filename match, check if we can match two other tags. */
2200 #define tmpdb_match(tag) \
2201 if (tfe->tag_offset[tag] == idx.tag_seek[tag]) \
2204 tmpdb_match(tag_artist
);
2205 tmpdb_match(tag_album
);
2206 tmpdb_match(tag_title
);
2208 if (match_count
< 2)
2210 /* Still no match found, give up. */
2215 /* A match found, now copy & resurrect the statistical data. */
2216 #define tmpdb_copy_tag(tag) \
2217 tfe->tag_offset[tag] = idx.tag_seek[tag]
2219 tmpdb_copy_tag(tag_playcount
);
2220 tmpdb_copy_tag(tag_rating
);
2221 tmpdb_copy_tag(tag_playtime
);
2222 tmpdb_copy_tag(tag_lastplayed
);
2223 tmpdb_copy_tag(tag_commitid
);
2225 /* Avoid processing this entry again. */
2226 idx
.flag
|= FLAG_RESURRECTED
;
2228 lseek(masterfd
, -sizeof(struct index_entry
), SEEK_CUR
);
2229 if (ecwrite(masterfd
, &idx
, 1, index_entry_ec
, tc_stat
.econ
)
2230 != sizeof(struct index_entry
))
2232 logf("masterfd writeback fail #1");
2237 logf("Entry resurrected");
2242 /* Restore the master index position. */
2243 lseek(masterfd
, masterfd_pos
, SEEK_SET
);
2245 /* Commit the data to the index. */
2246 for (i
= 0; i
< count
; i
++)
2248 int loc
= lseek(masterfd
, 0, SEEK_CUR
);
2250 if (ecread(masterfd
, &idx
, 1, index_entry_ec
, tc_stat
.econ
)
2251 != sizeof(struct index_entry
))
2253 logf("read fail #3");
2258 for (j
= 0; j
< TAG_COUNT
; j
++)
2260 if (!tagcache_is_numeric_tag(j
))
2263 idx
.tag_seek
[j
] = entrybuf
[i
].tag_offset
[j
];
2265 idx
.flag
= entrybuf
[i
].flag
;
2267 if (idx
.tag_seek
[tag_commitid
])
2269 /* Data has been resurrected. */
2270 idx
.flag
|= FLAG_DIRTYNUM
;
2272 else if (tc_stat
.ready
&& current_tcmh
.commitid
> 0)
2274 idx
.tag_seek
[tag_commitid
] = current_tcmh
.commitid
;
2275 idx
.flag
|= FLAG_DIRTYNUM
;
2278 /* Write back the updated index. */
2279 lseek(masterfd
, loc
, SEEK_SET
);
2280 if (ecwrite(masterfd
, &idx
, 1, index_entry_ec
, tc_stat
.econ
)
2281 != sizeof(struct index_entry
))
2289 entries_processed
+= count
;
2290 logf("%d/%ld entries processed", entries_processed
, h
->entry_count
);
2301 * == 0 temporary failure
2304 static int build_index(int index_type
, struct tagcache_header
*h
, int tmpfd
)
2307 struct tagcache_header tch
;
2308 struct master_header tcmh
;
2309 struct index_entry idxbuf
[IDX_BUF_DEPTH
];
2311 char buf
[TAG_MAXLEN
+32];
2312 int fd
= -1, masterfd
;
2317 logf("Building index: %d", index_type
);
2319 /* Check the number of entries we need to allocate ram for. */
2320 commit_entry_count
= h
->entry_count
+ 1;
2322 masterfd
= open_master_fd(&tcmh
, false);
2325 commit_entry_count
+= tcmh
.tch
.entry_count
;
2329 remove_files(); /* Just to be sure we are clean. */
2331 /* Open the index file, which contains the tag names. */
2332 fd
= open_tag_fd(&tch
, index_type
, true);
2335 logf("tch.datasize=%ld", tch
.datasize
);
2336 lookup_buffer_depth
= 1 +
2337 /* First part */ commit_entry_count
+
2338 /* Second part */ (tch
.datasize
/ TAGFILE_ENTRY_CHUNK_LENGTH
);
2342 lookup_buffer_depth
= 1 +
2343 /* First part */ commit_entry_count
+
2344 /* Second part */ 0;
2347 logf("lookup_buffer_depth=%ld", lookup_buffer_depth
);
2348 logf("commit_entry_count=%ld", commit_entry_count
);
2350 /* Allocate buffer for all index entries from both old and new
2353 tempbuf_pos
= commit_entry_count
* sizeof(struct tempbuf_searchidx
);
2355 /* Allocate lookup buffer. The first portion of commit_entry_count
2356 * contains the new tags in the temporary file and the second
2357 * part for locating entries already in the db.
2360 * +---------+---------------------------+
2361 * | index | position/ENTRY_CHUNK_SIZE | lookup buffer
2362 * +---------+---------------------------+
2364 * Old tags are inserted to a temporary buffer with position:
2365 * tempbuf_insert(position/ENTRY_CHUNK_SIZE, ...);
2366 * And new tags with index:
2367 * tempbuf_insert(idx, ...);
2369 * The buffer is sorted and written into tag file:
2370 * tempbuf_sort(...);
2371 * leaving master index locations messed up.
2373 * That is fixed using the lookup buffer for old tags:
2374 * new_seek = tempbuf_find_location(old_seek, ...);
2376 * new_seek = tempbuf_find_location(idx);
2378 lookup
= (struct tempbuf_searchidx
**)&tempbuf
[tempbuf_pos
];
2379 tempbuf_pos
+= lookup_buffer_depth
* sizeof(void **);
2380 memset(lookup
, 0, lookup_buffer_depth
* sizeof(void **));
2382 /* And calculate the remaining data space used mainly for storing
2383 * tag data (strings). */
2384 tempbuf_left
= tempbuf_size
- tempbuf_pos
- 8;
2385 if (tempbuf_left
- TAGFILE_ENTRY_AVG_LENGTH
* commit_entry_count
< 0)
2387 logf("Buffer way too small!");
2394 * If tag file contains unique tags (sorted index), we will load
2395 * it entirely into memory so we can resort it later for use with
2398 if (tagcache_is_sorted_tag(index_type
))
2400 logf("loading tags...");
2401 for (i
= 0; i
< tch
.entry_count
; i
++)
2403 struct tagfile_entry entry
;
2404 int loc
= lseek(fd
, 0, SEEK_CUR
);
2407 if (ecread(fd
, &entry
, 1, tagfile_entry_ec
, tc_stat
.econ
)
2408 != sizeof(struct tagfile_entry
))
2410 logf("read error #7");
2415 if (entry
.tag_length
>= (int)sizeof(buf
))
2417 logf("too long tag #3");
2422 if (read(fd
, buf
, entry
.tag_length
) != entry
.tag_length
)
2424 logf("read error #8");
2429 /* Skip deleted entries. */
2434 * Save the tag and tag id in the memory buffer. Tag id
2435 * is saved so we can later reindex the master lookup
2436 * table when the index gets resorted.
2438 ret
= tempbuf_insert(buf
, loc
/TAGFILE_ENTRY_CHUNK_LENGTH
2439 + commit_entry_count
, entry
.idx_id
,
2440 tagcache_is_unique_tag(index_type
));
2451 tempbufidx
= tch
.entry_count
;
2456 * Creating new index file to store the tags. No need to preload
2457 * anything whether the index type is sorted or not.
2459 snprintf(buf
, sizeof buf
, TAGCACHE_FILE_INDEX
, index_type
);
2460 fd
= open(buf
, O_WRONLY
| O_CREAT
| O_TRUNC
);
2463 logf("%s open fail", buf
);
2467 tch
.magic
= TAGCACHE_MAGIC
;
2468 tch
.entry_count
= 0;
2471 if (ecwrite(fd
, &tch
, 1, tagcache_header_ec
, tc_stat
.econ
)
2472 != sizeof(struct tagcache_header
))
2474 logf("header write failed");
2480 /* Loading the tag lookup file as "master file". */
2481 logf("Loading index file");
2482 masterfd
= open(TAGCACHE_FILE_MASTER
, O_RDWR
);
2486 logf("Creating new DB");
2487 masterfd
= open(TAGCACHE_FILE_MASTER
, O_WRONLY
| O_CREAT
| O_TRUNC
);
2491 logf("Failure to create index file (%s)", TAGCACHE_FILE_MASTER
);
2496 /* Write the header (write real values later). */
2497 memset(&tcmh
, 0, sizeof(struct master_header
));
2499 tcmh
.tch
.entry_count
= 0;
2500 tcmh
.tch
.datasize
= 0;
2502 ecwrite(masterfd
, &tcmh
, 1, master_header_ec
, tc_stat
.econ
);
2504 masterfd_pos
= lseek(masterfd
, 0, SEEK_CUR
);
2509 * Master file already exists so we need to process the current
2514 if (ecread(masterfd
, &tcmh
, 1, master_header_ec
, tc_stat
.econ
) !=
2515 sizeof(struct master_header
) || tcmh
.tch
.magic
!= TAGCACHE_MAGIC
)
2517 logf("header error");
2524 * If we reach end of the master file, we need to expand it to
2525 * hold new tags. If the current index is not sorted, we can
2526 * simply append new data to end of the file.
2527 * However, if the index is sorted, we need to update all tag
2528 * pointers in the master file for the current index.
2530 masterfd_pos
= lseek(masterfd
, tcmh
.tch
.entry_count
* sizeof(struct index_entry
),
2532 if (masterfd_pos
== filesize(masterfd
))
2534 logf("appending...");
2540 * Load new unique tags in memory to be sorted later and added
2541 * to the master lookup file.
2543 if (tagcache_is_sorted_tag(index_type
))
2545 lseek(tmpfd
, sizeof(struct tagcache_header
), SEEK_SET
);
2546 /* h is the header of the temporary file containing new tags. */
2547 logf("inserting new tags...");
2548 for (i
= 0; i
< h
->entry_count
; i
++)
2550 struct temp_file_entry entry
;
2552 if (read(tmpfd
, &entry
, sizeof(struct temp_file_entry
)) !=
2553 sizeof(struct temp_file_entry
))
2555 logf("read fail #3");
2561 if (entry
.tag_length
[index_type
] >= (long)sizeof(buf
))
2563 logf("too long entry!");
2568 lseek(tmpfd
, entry
.tag_offset
[index_type
], SEEK_CUR
);
2569 if (read(tmpfd
, buf
, entry
.tag_length
[index_type
]) !=
2570 entry
.tag_length
[index_type
])
2572 logf("read fail #4");
2577 if (tagcache_is_unique_tag(index_type
))
2578 error
= !tempbuf_insert(buf
, i
, -1, true);
2580 error
= !tempbuf_insert(buf
, i
, tcmh
.tch
.entry_count
+ i
, false);
2584 logf("insert error");
2589 lseek(tmpfd
, entry
.data_length
- entry
.tag_offset
[index_type
] -
2590 entry
.tag_length
[index_type
], SEEK_CUR
);
2595 /* Sort the buffer data and write it to the index file. */
2596 lseek(fd
, sizeof(struct tagcache_header
), SEEK_SET
);
2597 i
= tempbuf_sort(fd
);
2600 logf("sorted %d tags", i
);
2603 * Now update all indexes in the master lookup file.
2605 logf("updating indices...");
2606 lseek(masterfd
, sizeof(struct master_header
), SEEK_SET
);
2607 for (i
= 0; i
< tcmh
.tch
.entry_count
; i
+= idxbuf_pos
)
2610 int loc
= lseek(masterfd
, 0, SEEK_CUR
);
2612 idxbuf_pos
= MIN(tcmh
.tch
.entry_count
- i
, IDX_BUF_DEPTH
);
2614 if (ecread(masterfd
, idxbuf
, idxbuf_pos
, index_entry_ec
, tc_stat
.econ
)
2615 != (int)sizeof(struct index_entry
)*idxbuf_pos
)
2617 logf("read fail #5");
2621 lseek(masterfd
, loc
, SEEK_SET
);
2623 for (j
= 0; j
< idxbuf_pos
; j
++)
2625 if (idxbuf
[j
].flag
& FLAG_DELETED
)
2627 /* We can just ignore deleted entries. */
2628 // idxbuf[j].tag_seek[index_type] = 0;
2632 idxbuf
[j
].tag_seek
[index_type
] = tempbuf_find_location(
2633 idxbuf
[j
].tag_seek
[index_type
]/TAGFILE_ENTRY_CHUNK_LENGTH
2634 + commit_entry_count
);
2636 if (idxbuf
[j
].tag_seek
[index_type
] < 0)
2638 logf("update error: %d/%d/%d",
2639 idxbuf
[j
].flag
, i
+j
, tcmh
.tch
.entry_count
);
2647 /* Write back the updated index. */
2648 if (ecwrite(masterfd
, idxbuf
, idxbuf_pos
,
2649 index_entry_ec
, tc_stat
.econ
) !=
2650 (int)sizeof(struct index_entry
)*idxbuf_pos
)
2661 * Walk through the temporary file containing the new tags.
2663 // build_normal_index(h, tmpfd, masterfd, idx);
2664 logf("updating new indices...");
2665 lseek(masterfd
, masterfd_pos
, SEEK_SET
);
2666 lseek(tmpfd
, sizeof(struct tagcache_header
), SEEK_SET
);
2667 lseek(fd
, 0, SEEK_END
);
2668 for (i
= 0; i
< h
->entry_count
; i
+= idxbuf_pos
)
2672 idxbuf_pos
= MIN(h
->entry_count
- i
, IDX_BUF_DEPTH
);
2675 memset(idxbuf
, 0, sizeof(struct index_entry
)*IDX_BUF_DEPTH
);
2679 int loc
= lseek(masterfd
, 0, SEEK_CUR
);
2681 if (ecread(masterfd
, idxbuf
, idxbuf_pos
, index_entry_ec
, tc_stat
.econ
)
2682 != (int)sizeof(struct index_entry
)*idxbuf_pos
)
2684 logf("read fail #6");
2688 lseek(masterfd
, loc
, SEEK_SET
);
2691 /* Read entry headers. */
2692 for (j
= 0; j
< idxbuf_pos
; j
++)
2694 if (!tagcache_is_sorted_tag(index_type
))
2696 struct temp_file_entry entry
;
2697 struct tagfile_entry fe
;
2699 if (read(tmpfd
, &entry
, sizeof(struct temp_file_entry
)) !=
2700 sizeof(struct temp_file_entry
))
2702 logf("read fail #7");
2708 if (entry
.tag_length
[index_type
] >= (int)sizeof(buf
))
2710 logf("too long entry!");
2711 logf("length=%d", entry
.tag_length
[index_type
]);
2712 logf("pos=0x%02lx", lseek(tmpfd
, 0, SEEK_CUR
));
2717 lseek(tmpfd
, entry
.tag_offset
[index_type
], SEEK_CUR
);
2718 if (read(tmpfd
, buf
, entry
.tag_length
[index_type
]) !=
2719 entry
.tag_length
[index_type
])
2721 logf("read fail #8");
2722 logf("offset=0x%02lx", entry
.tag_offset
[index_type
]);
2723 logf("length=0x%02x", entry
.tag_length
[index_type
]);
2728 /* Write to index file. */
2729 idxbuf
[j
].tag_seek
[index_type
] = lseek(fd
, 0, SEEK_CUR
);
2730 fe
.tag_length
= entry
.tag_length
[index_type
];
2731 fe
.idx_id
= tcmh
.tch
.entry_count
+ i
+ j
;
2732 ecwrite(fd
, &fe
, 1, tagfile_entry_ec
, tc_stat
.econ
);
2733 write(fd
, buf
, fe
.tag_length
);
2737 lseek(tmpfd
, entry
.data_length
- entry
.tag_offset
[index_type
] -
2738 entry
.tag_length
[index_type
], SEEK_CUR
);
2742 /* Locate the correct entry from the sorted array. */
2743 idxbuf
[j
].tag_seek
[index_type
] = tempbuf_find_location(i
+ j
);
2744 if (idxbuf
[j
].tag_seek
[index_type
] < 0)
2746 logf("entry not found (%d)", j
);
2754 if (ecwrite(masterfd
, idxbuf
, idxbuf_pos
,
2755 index_entry_ec
, tc_stat
.econ
) !=
2756 (int)sizeof(struct index_entry
)*idxbuf_pos
)
2758 logf("tagcache: write fail #4");
2767 /* Finally write the header. */
2768 tch
.magic
= TAGCACHE_MAGIC
;
2769 tch
.entry_count
= tempbufidx
;
2770 tch
.datasize
= lseek(fd
, 0, SEEK_END
) - sizeof(struct tagcache_header
);
2771 lseek(fd
, 0, SEEK_SET
);
2772 ecwrite(fd
, &tch
, 1, tagcache_header_ec
, tc_stat
.econ
);
2774 if (index_type
!= tag_filename
)
2775 h
->datasize
+= tch
.datasize
;
2776 logf("s:%d/%ld/%ld", index_type
, tch
.datasize
, h
->datasize
);
2788 static bool commit(void)
2790 struct tagcache_header tch
;
2791 struct master_header tcmh
;
2795 #ifdef HAVE_DIRCACHE
2796 bool dircache_buffer_stolen
= false;
2798 bool local_allocation
= false;
2800 logf("committing tagcache");
2805 tmpfd
= open(TAGCACHE_FILE_TEMP
, O_RDONLY
);
2808 logf("nothing to commit");
2813 /* Load the header. */
2814 len
= sizeof(struct tagcache_header
);
2815 rc
= read(tmpfd
, &tch
, len
);
2817 if (tch
.magic
!= TAGCACHE_MAGIC
|| rc
!= len
)
2819 logf("incorrect header");
2821 remove(TAGCACHE_FILE_TEMP
);
2825 if (tch
.entry_count
== 0)
2827 logf("nothing to commit");
2829 remove(TAGCACHE_FILE_TEMP
);
2833 #ifdef HAVE_EEPROM_SETTINGS
2834 remove(TAGCACHE_STATEFILE
);
2837 /* At first be sure to unload the ramcache! */
2838 #ifdef HAVE_TC_RAMCACHE
2839 tc_stat
.ramcache
= false;
2844 /* Try to steal every buffer we can :) */
2845 if (tempbuf_size
== 0)
2846 local_allocation
= true;
2848 #ifdef HAVE_DIRCACHE
2849 if (tempbuf_size
== 0)
2851 /* Try to steal the dircache buffer. */
2852 tempbuf
= dircache_steal_buffer(&tempbuf_size
);
2853 tempbuf_size
&= ~0x03;
2855 if (tempbuf_size
> 0)
2857 dircache_buffer_stolen
= true;
2862 #ifdef HAVE_TC_RAMCACHE
2863 if (tempbuf_size
== 0 && tc_stat
.ramcache_allocated
> 0)
2865 tempbuf
= (char *)(hdr
+ 1);
2866 tempbuf_size
= tc_stat
.ramcache_allocated
- sizeof(struct ramcache_header
) - 128;
2867 tempbuf_size
&= ~0x03;
2871 /* And finally fail if there are no buffers available. */
2872 if (tempbuf_size
== 0)
2874 logf("delaying commit until next boot");
2875 tc_stat
.commit_delayed
= true;
2881 logf("commit %ld entries...", tch
.entry_count
);
2883 /* Mark DB dirty so it will stay disabled if commit fails. */
2884 current_tcmh
.dirty
= true;
2885 update_master_header();
2887 /* Now create the index files. */
2888 tc_stat
.commit_step
= 0;
2890 tc_stat
.commit_delayed
= false;
2892 for (i
= 0; i
< TAG_COUNT
; i
++)
2896 if (tagcache_is_numeric_tag(i
))
2899 tc_stat
.commit_step
++;
2900 ret
= build_index(i
, &tch
, tmpfd
);
2904 logf("tagcache failed init");
2906 tc_stat
.commit_delayed
= true;
2908 tc_stat
.commit_step
= 0;
2914 if (!build_numeric_indices(&tch
, tmpfd
))
2916 logf("Failure to commit numeric indices");
2918 tc_stat
.commit_step
= 0;
2924 remove(TAGCACHE_FILE_TEMP
);
2926 tc_stat
.commit_step
= 0;
2928 /* Update the master index headers. */
2929 if ( (masterfd
= open_master_fd(&tcmh
, true)) < 0)
2935 tcmh
.tch
.entry_count
+= tch
.entry_count
;
2936 tcmh
.tch
.datasize
= sizeof(struct master_header
)
2937 + sizeof(struct index_entry
) * tcmh
.tch
.entry_count
2942 lseek(masterfd
, 0, SEEK_SET
);
2943 ecwrite(masterfd
, &tcmh
, 1, master_header_ec
, tc_stat
.econ
);
2946 logf("tagcache committed");
2947 tc_stat
.ready
= check_all_headers();
2948 tc_stat
.readyvalid
= true;
2950 if (local_allocation
)
2956 #ifdef HAVE_DIRCACHE
2957 /* Rebuild the dircache, if we stole the buffer. */
2958 if (dircache_buffer_stolen
)
2962 #ifdef HAVE_TC_RAMCACHE
2963 /* Reload tagcache. */
2964 if (tc_stat
.ramcache_allocated
> 0)
2965 tagcache_start_scan();
2973 static void allocate_tempbuf(void)
2975 /* Yeah, malloc would be really nice now :) */
2977 tempbuf_size
= 32*1024*1024;
2978 tempbuf
= malloc(tempbuf_size
);
2980 tempbuf
= (char *)(((long)audiobuf
& ~0x03) + 0x04);
2981 tempbuf_size
= (long)audiobufend
- (long)audiobuf
- 4;
2982 audiobuf
+= tempbuf_size
;
2986 static void free_tempbuf(void)
2988 if (tempbuf_size
== 0)
2994 audiobuf
-= tempbuf_size
;
3002 static bool modify_numeric_entry(int masterfd
, int idx_id
, int tag
, long data
)
3004 struct index_entry idx
;
3009 if (!tagcache_is_numeric_tag(tag
))
3012 if (!get_index(masterfd
, idx_id
, &idx
, false))
3015 idx
.tag_seek
[tag
] = data
;
3016 idx
.flag
|= FLAG_DIRTYNUM
;
3018 return write_index(masterfd
, idx_id
, &idx
);
3022 bool tagcache_modify_numeric_entry(struct tagcache_search
*tcs
,
3025 struct master_header myhdr
;
3027 if (tcs
->masterfd
< 0)
3029 if ( (tcs
->masterfd
= open_master_fd(&myhdr
, true)) < 0)
3033 return modify_numeric_entry(tcs
->masterfd
, tcs
->idx_id
, tag
, data
);
3037 #define COMMAND_QUEUE_IS_EMPTY (command_queue_ridx == command_queue_widx)
3039 static bool command_queue_is_full(void)
3043 next
= command_queue_widx
+ 1;
3044 if (next
>= TAGCACHE_COMMAND_QUEUE_LENGTH
)
3047 return (next
== command_queue_ridx
);
3050 static bool command_queue_sync_callback(void)
3053 struct master_header myhdr
;
3056 mutex_lock(&command_queue_mutex
);
3058 if ( (masterfd
= open_master_fd(&myhdr
, true)) < 0)
3061 while (command_queue_ridx
!= command_queue_widx
)
3063 struct tagcache_command_entry
*ce
= &command_queue
[command_queue_ridx
];
3065 switch (ce
->command
)
3067 case CMD_UPDATE_MASTER_HEADER
:
3070 update_master_header();
3072 /* Re-open the masterfd. */
3073 if ( (masterfd
= open_master_fd(&myhdr
, true)) < 0)
3078 case CMD_UPDATE_NUMERIC
:
3080 modify_numeric_entry(masterfd
, ce
->idx_id
, ce
->tag
, ce
->data
);
3085 if (++command_queue_ridx
>= TAGCACHE_COMMAND_QUEUE_LENGTH
)
3086 command_queue_ridx
= 0;
3091 tc_stat
.queue_length
= 0;
3092 mutex_unlock(&command_queue_mutex
);
3096 static void run_command_queue(bool force
)
3098 if (COMMAND_QUEUE_IS_EMPTY
)
3101 if (force
|| command_queue_is_full())
3102 command_queue_sync_callback();
3104 register_storage_idle_func(command_queue_sync_callback
);
3107 static void queue_command(int cmd
, long idx_id
, int tag
, long data
)
3113 mutex_lock(&command_queue_mutex
);
3114 next
= command_queue_widx
+ 1;
3115 if (next
>= TAGCACHE_COMMAND_QUEUE_LENGTH
)
3118 /* Make sure queue is not full. */
3119 if (next
!= command_queue_ridx
)
3121 struct tagcache_command_entry
*ce
= &command_queue
[command_queue_widx
];
3124 ce
->idx_id
= idx_id
;
3128 command_queue_widx
= next
;
3130 tc_stat
.queue_length
++;
3132 mutex_unlock(&command_queue_mutex
);
3136 /* Queue is full, try again later... */
3137 mutex_unlock(&command_queue_mutex
);
3142 long tagcache_increase_serial(void)
3152 old
= current_tcmh
.serial
++;
3153 queue_command(CMD_UPDATE_MASTER_HEADER
, 0, 0, 0);
3158 void tagcache_update_numeric(int idx_id
, int tag
, long data
)
3160 queue_command(CMD_UPDATE_NUMERIC
, idx_id
, tag
, data
);
3162 #endif /* !__PCTOOL__ */
3164 long tagcache_get_serial(void)
3166 return current_tcmh
.serial
;
3169 long tagcache_get_commitid(void)
3171 return current_tcmh
.commitid
;
3174 static bool write_tag(int fd
, const char *tagstr
, const char *datastr
)
3179 snprintf(buf
, sizeof buf
, "%s=\"", tagstr
);
3180 for (i
= strlen(buf
); i
< (long)sizeof(buf
)-4; i
++)
3182 if (*datastr
== '\0')
3185 if (*datastr
== '"' || *datastr
== '\\')
3188 buf
[i
] = *(datastr
++);
3191 strcpy(&buf
[i
], "\" ");
3193 write(fd
, buf
, i
+ 2);
3200 static bool read_tag(char *dest
, long size
,
3201 const char *src
, const char *tagstr
)
3204 char current_tag
[32];
3206 while (*src
!= '\0')
3208 /* Skip all whitespace */
3216 /* Read in tag name */
3217 while (*src
!= '=' && *src
!= ' ')
3219 current_tag
[pos
] = *src
;
3223 if (*src
== '\0' || pos
>= (long)sizeof(current_tag
))
3226 current_tag
[pos
] = '\0';
3228 /* Read in tag data */
3230 /* Find the start. */
3231 while (*src
!= '"' && *src
!= '\0')
3234 if (*src
== '\0' || *(++src
) == '\0')
3237 /* Read the data. */
3238 for (pos
= 0; pos
< size
; pos
++)
3245 dest
[pos
] = *(src
+1);
3265 if (!strcasecmp(tagstr
, current_tag
))
3272 static int parse_changelog_line(int line_n
, const char *buf
, void *parameters
)
3274 struct index_entry idx
;
3275 char tag_data
[TAG_MAXLEN
+32];
3277 long masterfd
= (long)parameters
;
3278 const int import_tags
[] = { tag_playcount
, tag_rating
, tag_playtime
, tag_lastplayed
,
3286 logf("%d/%s", line_n
, buf
);
3287 if (!read_tag(tag_data
, sizeof tag_data
, buf
, "filename"))
3289 logf("filename missing");
3294 idx_id
= find_index(tag_data
);
3297 logf("entry not found");
3301 if (!get_index(masterfd
, idx_id
, &idx
, false))
3303 logf("failed to retrieve index entry");
3307 /* Stop if tag has already been modified. */
3308 if (idx
.flag
& FLAG_DIRTYNUM
)
3311 logf("import: %s", tag_data
);
3313 idx
.flag
|= FLAG_DIRTYNUM
;
3314 for (i
= 0; i
< (long)(sizeof(import_tags
)/sizeof(import_tags
[0])); i
++)
3318 if (!read_tag(tag_data
, sizeof tag_data
, buf
,
3319 tagcache_tag_to_str(import_tags
[i
])))
3324 data
= atoi(tag_data
);
3328 idx
.tag_seek
[import_tags
[i
]] = data
;
3330 if (import_tags
[i
] == tag_lastplayed
&& data
>= current_tcmh
.serial
)
3331 current_tcmh
.serial
= data
+ 1;
3332 else if (import_tags
[i
] == tag_commitid
&& data
>= current_tcmh
.commitid
)
3333 current_tcmh
.commitid
= data
+ 1;
3336 return write_index(masterfd
, idx_id
, &idx
) ? 0 : -5;
3339 bool tagcache_import_changelog(void)
3341 struct master_header myhdr
;
3342 struct tagcache_header tch
;
3353 clfd
= open(TAGCACHE_FILE_CHANGELOG
, O_RDONLY
);
3356 logf("failure to open changelog");
3360 if ( (masterfd
= open_master_fd(&myhdr
, true)) < 0)
3368 filenametag_fd
= open_tag_fd(&tch
, tag_filename
, false);
3370 fast_readline(clfd
, buf
, sizeof buf
, (long *)masterfd
,
3371 parse_changelog_line
);
3376 if (filenametag_fd
>= 0)
3377 close(filenametag_fd
);
3381 update_master_header();
3386 #endif /* !__PCTOOL__ */
3388 bool tagcache_create_changelog(struct tagcache_search
*tcs
)
3390 struct master_header myhdr
;
3391 struct index_entry idx
;
3392 char buf
[TAG_MAXLEN
+32];
3400 if (!tagcache_search(tcs
, tag_filename
))
3403 /* Initialize the changelog */
3404 clfd
= open(TAGCACHE_FILE_CHANGELOG
, O_WRONLY
| O_CREAT
| O_TRUNC
);
3407 logf("failure to open changelog");
3411 if (tcs
->masterfd
< 0)
3413 if ( (tcs
->masterfd
= open_master_fd(&myhdr
, false)) < 0)
3418 lseek(tcs
->masterfd
, 0, SEEK_SET
);
3419 ecread(tcs
->masterfd
, &myhdr
, 1, master_header_ec
, tc_stat
.econ
);
3422 write(clfd
, "## Changelog version 1\n", 23);
3424 for (i
= 0; i
< myhdr
.tch
.entry_count
; i
++)
3426 if (ecread(tcs
->masterfd
, &idx
, 1, index_entry_ec
, tc_stat
.econ
)
3427 != sizeof(struct index_entry
))
3429 logf("read error #9");
3430 tagcache_search_finish(tcs
);
3435 /* Skip until the entry found has been modified. */
3436 if (! (idx
.flag
& FLAG_DIRTYNUM
) )
3439 /* Skip deleted entries too. */
3440 if (idx
.flag
& FLAG_DELETED
)
3443 /* Now retrieve all tags. */
3444 for (j
= 0; j
< TAG_COUNT
; j
++)
3446 if (tagcache_is_numeric_tag(j
))
3448 snprintf(temp
, sizeof temp
, "%d", idx
.tag_seek
[j
]);
3449 write_tag(clfd
, tagcache_tag_to_str(j
), temp
);
3454 tagcache_retrieve(tcs
, i
, tcs
->type
, buf
, sizeof buf
);
3455 write_tag(clfd
, tagcache_tag_to_str(j
), buf
);
3458 write(clfd
, "\n", 1);
3464 tagcache_search_finish(tcs
);
3469 static bool delete_entry(long idx_id
)
3474 struct index_entry idx
, myidx
;
3475 struct master_header myhdr
;
3476 char buf
[TAG_MAXLEN
+32];
3477 int in_use
[TAG_COUNT
];
3479 logf("delete_entry(): %ld", idx_id
);
3481 #ifdef HAVE_TC_RAMCACHE
3482 /* At first mark the entry removed from ram cache. */
3483 if (tc_stat
.ramcache
)
3484 hdr
->indices
[idx_id
].flag
|= FLAG_DELETED
;
3487 if ( (masterfd
= open_master_fd(&myhdr
, true) ) < 0)
3490 lseek(masterfd
, idx_id
* sizeof(struct index_entry
), SEEK_CUR
);
3491 if (ecread(masterfd
, &myidx
, 1, index_entry_ec
, tc_stat
.econ
)
3492 != sizeof(struct index_entry
))
3494 logf("delete_entry(): read error");
3498 if (myidx
.flag
& FLAG_DELETED
)
3500 logf("delete_entry(): already deleted!");
3504 myidx
.flag
|= FLAG_DELETED
;
3505 lseek(masterfd
, -sizeof(struct index_entry
), SEEK_CUR
);
3506 if (ecwrite(masterfd
, &myidx
, 1, index_entry_ec
, tc_stat
.econ
)
3507 != sizeof(struct index_entry
))
3509 logf("delete_entry(): write_error #1");
3513 /* Now check which tags are no longer in use (if any) */
3514 for (tag
= 0; tag
< TAG_COUNT
; tag
++)
3517 lseek(masterfd
, sizeof(struct master_header
), SEEK_SET
);
3518 for (i
= 0; i
< myhdr
.tch
.entry_count
; i
++)
3520 struct index_entry
*idxp
;
3522 #ifdef HAVE_TC_RAMCACHE
3523 /* Use RAM DB if available for greater speed */
3524 if (tc_stat
.ramcache
)
3525 idxp
= &hdr
->indices
[i
];
3529 if (ecread(masterfd
, &idx
, 1, index_entry_ec
, tc_stat
.econ
)
3530 != sizeof(struct index_entry
))
3532 logf("delete_entry(): read error #2");
3538 if (idxp
->flag
& FLAG_DELETED
)
3541 for (tag
= 0; tag
< TAG_COUNT
; tag
++)
3543 if (tagcache_is_numeric_tag(tag
))
3546 if (idxp
->tag_seek
[tag
] == myidx
.tag_seek
[tag
])
3551 /* Now delete all tags no longer in use. */
3552 for (tag
= 0; tag
< TAG_COUNT
; tag
++)
3554 struct tagcache_header tch
;
3555 int oldseek
= myidx
.tag_seek
[tag
];
3557 if (tagcache_is_numeric_tag(tag
))
3561 * Replace tag seek with a hash value of the field string data.
3562 * That way runtime statistics of moved or altered files can be
3565 #ifdef HAVE_TC_RAMCACHE
3566 if (tc_stat
.ramcache
&& tag
!= tag_filename
)
3568 struct tagfile_entry
*tfe
;
3569 int32_t *seek
= &hdr
->indices
[idx_id
].tag_seek
[tag
];
3571 tfe
= (struct tagfile_entry
*)&hdr
->tags
[tag
][*seek
];
3572 *seek
= crc_32(tfe
->tag_data
, strlen(tfe
->tag_data
), 0xffffffff);
3573 myidx
.tag_seek
[tag
] = *seek
;
3578 struct tagfile_entry tfe
;
3580 /* Open the index file, which contains the tag names. */
3581 if ((fd
= open_tag_fd(&tch
, tag
, true)) < 0)
3584 /* Skip the header block */
3585 lseek(fd
, myidx
.tag_seek
[tag
], SEEK_SET
);
3586 if (ecread(fd
, &tfe
, 1, tagfile_entry_ec
, tc_stat
.econ
)
3587 != sizeof(struct tagfile_entry
))
3589 logf("delete_entry(): read error #3");
3593 if (read(fd
, buf
, tfe
.tag_length
) != tfe
.tag_length
)
3595 logf("delete_entry(): read error #4");
3599 myidx
.tag_seek
[tag
] = crc_32(buf
, strlen(buf
), 0xffffffff);
3604 logf("in use: %d/%d", tag
, in_use
[tag
]);
3613 #ifdef HAVE_TC_RAMCACHE
3614 /* Delete from ram. */
3615 if (tc_stat
.ramcache
&& tag
!= tag_filename
)
3617 struct tagfile_entry
*tagentry
= (struct tagfile_entry
*)&hdr
->tags
[tag
][oldseek
];
3618 tagentry
->tag_data
[0] = '\0';
3622 /* Open the index file, which contains the tag names. */
3625 if ((fd
= open_tag_fd(&tch
, tag
, true)) < 0)
3629 /* Skip the header block */
3630 lseek(fd
, oldseek
+ sizeof(struct tagfile_entry
), SEEK_SET
);
3632 /* Debug, print 10 first characters of the tag
3635 logf("TAG:%s", buf);
3636 lseek(fd, -10, SEEK_CUR);
3639 /* Write first data byte in tag as \0 */
3642 /* Now tag data has been removed */
3647 /* Write index entry back into master index. */
3648 lseek(masterfd
, sizeof(struct master_header
) +
3649 (idx_id
* sizeof(struct index_entry
)), SEEK_SET
);
3650 if (ecwrite(masterfd
, &myidx
, 1, index_entry_ec
, tc_stat
.econ
)
3651 != sizeof(struct index_entry
))
3653 logf("delete_entry(): write_error #2");
3672 * Returns true if there is an event waiting in the queue
3673 * that requires the current operation to be aborted.
3675 static bool check_event_queue(void)
3677 struct queue_event ev
;
3679 queue_wait_w_tmo(&tagcache_queue
, &ev
, 0);
3684 case SYS_USB_CONNECTED
:
3685 /* Put the event back into the queue. */
3686 queue_post(&tagcache_queue
, ev
.id
, ev
.data
);
3694 #ifdef HAVE_TC_RAMCACHE
3695 static bool allocate_tagcache(void)
3697 struct master_header tcmh
;
3700 /* Load the header. */
3701 if ( (fd
= open_master_fd(&tcmh
, false)) < 0)
3710 * Now calculate the required cache size plus
3711 * some extra space for alignment fixes.
3713 tc_stat
.ramcache_allocated
= tcmh
.tch
.datasize
+ 128 + TAGCACHE_RESERVE
+
3714 sizeof(struct ramcache_header
) + TAG_COUNT
*sizeof(void *);
3715 hdr
= buffer_alloc(tc_stat
.ramcache_allocated
+ 128);
3716 memset(hdr
, 0, sizeof(struct ramcache_header
));
3717 memcpy(&hdr
->h
, &tcmh
, sizeof(struct master_header
));
3718 hdr
->indices
= (struct index_entry
*)(hdr
+ 1);
3719 logf("tagcache: %d bytes allocated.", tc_stat
.ramcache_allocated
);
3724 # ifdef HAVE_EEPROM_SETTINGS
3725 static bool tagcache_dumpload(void)
3727 struct statefile_header shdr
;
3732 fd
= open(TAGCACHE_STATEFILE
, O_RDONLY
);
3735 logf("no tagcache statedump");
3739 /* Check the statefile memory placement */
3740 hdr
= buffer_alloc(0);
3741 rc
= read(fd
, &shdr
, sizeof(struct statefile_header
));
3742 if (rc
!= sizeof(struct statefile_header
)
3743 /* || (long)hdr != (long)shdr.hdr */)
3745 logf("incorrect statefile");
3751 offpos
= (long)hdr
- (long)shdr
.hdr
;
3753 /* Lets allocate real memory and load it */
3754 hdr
= buffer_alloc(shdr
.tc_stat
.ramcache_allocated
);
3755 rc
= read(fd
, hdr
, shdr
.tc_stat
.ramcache_allocated
);
3758 if (rc
!= shdr
.tc_stat
.ramcache_allocated
)
3760 logf("read failure!");
3765 memcpy(&tc_stat
, &shdr
.tc_stat
, sizeof(struct tagcache_stat
));
3767 /* Now fix the pointers */
3768 hdr
->indices
= (struct index_entry
*)((long)hdr
->indices
+ offpos
);
3769 for (i
= 0; i
< TAG_COUNT
; i
++)
3770 hdr
->tags
[i
] += offpos
;
3775 static bool tagcache_dumpsave(void)
3777 struct statefile_header shdr
;
3780 if (!tc_stat
.ramcache
)
3783 fd
= open(TAGCACHE_STATEFILE
, O_WRONLY
| O_CREAT
| O_TRUNC
);
3786 logf("failed to create a statedump");
3790 /* Create the header */
3792 memcpy(&shdr
.tc_stat
, &tc_stat
, sizeof(struct tagcache_stat
));
3793 write(fd
, &shdr
, sizeof(struct statefile_header
));
3795 /* And dump the data too */
3796 write(fd
, hdr
, tc_stat
.ramcache_allocated
);
3803 static bool load_tagcache(void)
3805 struct tagcache_header
*tch
;
3806 long bytesleft
= tc_stat
.ramcache_allocated
;
3807 struct index_entry
*idx
;
3812 # ifdef HAVE_DIRCACHE
3813 while (dircache_is_initializing())
3816 dircache_set_appflag(DIRCACHE_APPFLAG_TAGCACHE
);
3819 logf("loading tagcache to ram...");
3821 fd
= open(TAGCACHE_FILE_MASTER
, O_RDONLY
);
3824 logf("tagcache open failed");
3828 if (ecread(fd
, &hdr
->h
, 1, master_header_ec
, tc_stat
.econ
)
3829 != sizeof(struct master_header
)
3830 || hdr
->h
.tch
.magic
!= TAGCACHE_MAGIC
)
3832 logf("incorrect header");
3838 /* Load the master index table. */
3839 for (i
= 0; i
< hdr
->h
.tch
.entry_count
; i
++)
3841 rc
= ecread(fd
, idx
, 1, index_entry_ec
, tc_stat
.econ
);
3842 if (rc
!= sizeof(struct index_entry
))
3844 logf("read error #10");
3849 bytesleft
-= sizeof(struct index_entry
);
3850 if (bytesleft
< 0 || ((long)idx
- (long)hdr
->indices
) >= tc_stat
.ramcache_allocated
)
3852 logf("too big tagcache.");
3862 /* Load the tags. */
3864 for (tag
= 0; tag
< TAG_COUNT
; tag
++)
3866 struct tagfile_entry
*fe
;
3867 char buf
[TAG_MAXLEN
+32];
3869 if (tagcache_is_numeric_tag(tag
))
3872 //p = ((void *)p+1);
3873 p
= (char *)((long)p
& ~0x03) + 0x04;
3876 /* Check the header. */
3877 tch
= (struct tagcache_header
*)p
;
3878 p
+= sizeof(struct tagcache_header
);
3880 if ( (fd
= open_tag_fd(tch
, tag
, false)) < 0)
3883 for (hdr
->entry_count
[tag
] = 0;
3884 hdr
->entry_count
[tag
] < tch
->entry_count
;
3885 hdr
->entry_count
[tag
]++)
3889 if (do_timed_yield())
3891 /* Abort if we got a critical event in queue */
3892 if (check_event_queue())
3896 fe
= (struct tagfile_entry
*)p
;
3897 pos
= lseek(fd
, 0, SEEK_CUR
);
3898 rc
= ecread(fd
, fe
, 1, tagfile_entry_ec
, tc_stat
.econ
);
3899 if (rc
!= sizeof(struct tagfile_entry
))
3901 /* End of lookup table. */
3902 logf("read error #11");
3907 /* We have a special handling for the filename tags. */
3908 if (tag
== tag_filename
)
3910 # ifdef HAVE_DIRCACHE
3911 const struct dirent
*dc
;
3914 // FIXME: This is wrong!
3915 // idx = &hdr->indices[hdr->entry_count[i]];
3916 idx
= &hdr
->indices
[fe
->idx_id
];
3918 if (fe
->tag_length
>= (long)sizeof(buf
)-1)
3922 logf("TAG:%s", buf
);
3923 logf("too long filename");
3928 rc
= read(fd
, buf
, fe
->tag_length
);
3929 if (rc
!= fe
->tag_length
)
3931 logf("read error #12");
3936 /* Check if the entry has already been removed */
3937 if (idx
->flag
& FLAG_DELETED
)
3940 /* This flag must not be used yet. */
3941 if (idx
->flag
& FLAG_DIRCACHE
)
3943 logf("internal error!");
3948 if (idx
->tag_seek
[tag
] != pos
)
3950 logf("corrupt data structures!");
3955 # ifdef HAVE_DIRCACHE
3956 if (dircache_is_enabled())
3958 dc
= dircache_get_entry_ptr(buf
);
3961 logf("Entry no longer valid.");
3963 delete_entry(fe
->idx_id
);
3967 idx
->flag
|= FLAG_DIRCACHE
;
3968 FLAG_SET_ATTR(idx
->flag
, fe
->idx_id
);
3969 idx
->tag_seek
[tag_filename
] = (long)dc
;
3974 /* This will be very slow unless dircache is enabled
3975 or target is flash based, but do it anyway for
3977 /* Check if entry has been removed. */
3978 if (global_settings
.tagcache_autoupdate
)
3980 if (!file_exists(buf
))
3982 logf("Entry no longer valid.");
3984 delete_entry(fe
->idx_id
);
3993 bytesleft
-= sizeof(struct tagfile_entry
) + fe
->tag_length
;
3996 logf("too big tagcache #2");
3997 logf("tl: %d", fe
->tag_length
);
3998 logf("bl: %ld", bytesleft
);
4004 rc
= read(fd
, fe
->tag_data
, fe
->tag_length
);
4007 if (rc
!= fe
->tag_length
)
4009 logf("read error #13");
4010 logf("rc=0x%04x", rc
); // 0x431
4011 logf("len=0x%04x", fe
->tag_length
); // 0x4000
4012 logf("pos=0x%04lx", lseek(fd
, 0, SEEK_CUR
)); // 0x433
4013 logf("tag=0x%02x", tag
); // 0x00
4021 tc_stat
.ramcache_used
= tc_stat
.ramcache_allocated
- bytesleft
;
4022 logf("tagcache loaded into ram!");
4026 #endif /* HAVE_TC_RAMCACHE */
4028 static bool check_deleted_files(void)
4031 char buf
[TAG_MAXLEN
+32];
4032 struct tagfile_entry tfe
;
4034 logf("reverse scan...");
4035 snprintf(buf
, sizeof buf
, TAGCACHE_FILE_INDEX
, tag_filename
);
4036 fd
= open(buf
, O_RDONLY
);
4040 logf("%s open fail", buf
);
4044 lseek(fd
, sizeof(struct tagcache_header
), SEEK_SET
);
4045 while (ecread(fd
, &tfe
, 1, tagfile_entry_ec
, tc_stat
.econ
)
4046 == sizeof(struct tagfile_entry
)
4048 && !check_event_queue()
4052 if (tfe
.tag_length
>= (long)sizeof(buf
)-1)
4054 logf("too long tag");
4059 if (read(fd
, buf
, tfe
.tag_length
) != tfe
.tag_length
)
4061 logf("read error #14");
4066 /* Check if the file has already deleted from the db. */
4070 /* Now check if the file exists. */
4071 if (!file_exists(buf
))
4073 logf("Entry no longer valid.");
4074 logf("-> %s / %d", buf
, tfe
.tag_length
);
4075 delete_entry(tfe
.idx_id
);
4086 static bool check_dir(const char *dirname
, int add_files
)
4090 int success
= false;
4091 int ignore
, unignore
;
4092 char newpath
[MAX_PATH
];
4094 dir
= opendir(dirname
);
4097 logf("tagcache: opendir() failed");
4101 /* check for a database.ignore file */
4102 snprintf(newpath
, MAX_PATH
, "%s/database.ignore", dirname
);
4103 ignore
= file_exists(newpath
);
4104 /* check for a database.unignore file */
4105 snprintf(newpath
, MAX_PATH
, "%s/database.unignore", dirname
);
4106 unignore
= file_exists(newpath
);
4108 /* don't do anything if both ignore and unignore are there */
4109 if (ignore
!= unignore
)
4110 add_files
= unignore
;
4112 /* Recursively scan the dir. */
4116 while (!check_event_queue())
4119 struct dirent
*entry
;
4121 entry
= readdir(dir
);
4129 if (!strcmp((char *)entry
->d_name
, ".") ||
4130 !strcmp((char *)entry
->d_name
, ".."))
4135 len
= strlen(curpath
);
4136 snprintf(&curpath
[len
], sizeof(curpath
) - len
, "/%s",
4139 processed_dir_count
++;
4140 if (entry
->attribute
& ATTR_DIRECTORY
)
4141 check_dir(curpath
, add_files
);
4144 tc_stat
.curentry
= curpath
;
4146 /* Add a new entry to the temporary db file. */
4147 add_tagcache(curpath
, (entry
->wrtdate
<< 16) | entry
->wrttime
4148 #if defined(HAVE_TC_RAMCACHE) && defined(HAVE_DIRCACHE)
4149 , dir
->internal_entry
4153 /* Wait until current path for debug screen is read and unset. */
4154 while (tc_stat
.syncscreen
&& tc_stat
.curentry
!= NULL
)
4157 tc_stat
.curentry
= NULL
;
4160 curpath
[len
] = '\0';
4168 void tagcache_screensync_event(void)
4170 tc_stat
.curentry
= NULL
;
4173 void tagcache_screensync_enable(bool state
)
4175 tc_stat
.syncscreen
= state
;
4178 void tagcache_build(const char *path
)
4180 struct tagcache_header header
;
4185 total_entry_count
= 0;
4186 processed_dir_count
= 0;
4188 #ifdef HAVE_DIRCACHE
4189 while (dircache_is_initializing())
4193 logf("updating tagcache");
4195 cachefd
= open(TAGCACHE_FILE_TEMP
, O_RDONLY
);
4198 logf("skipping, cache already waiting for commit");
4203 cachefd
= open(TAGCACHE_FILE_TEMP
, O_RDWR
| O_CREAT
| O_TRUNC
);
4206 logf("master file open failed: %s", TAGCACHE_FILE_TEMP
);
4210 filenametag_fd
= open_tag_fd(&header
, tag_filename
, false);
4214 logf("Scanning files...");
4215 /* Scan for new files. */
4216 memset(&header
, 0, sizeof(struct tagcache_header
));
4217 write(cachefd
, &header
, sizeof(struct tagcache_header
));
4219 if (strcmp("/", path
) != 0)
4220 strcpy(curpath
, path
);
4221 ret
= check_dir(path
, true);
4223 /* Write the header. */
4224 header
.magic
= TAGCACHE_MAGIC
;
4225 header
.datasize
= data_size
;
4226 header
.entry_count
= total_entry_count
;
4227 lseek(cachefd
, 0, SEEK_SET
);
4228 write(cachefd
, &header
, sizeof(struct tagcache_header
));
4231 if (filenametag_fd
>= 0)
4233 close(filenametag_fd
);
4234 filenametag_fd
= -1;
4244 /* Commit changes to the database. */
4250 remove(TAGCACHE_FILE_TEMP
);
4251 logf("tagcache built!");
4257 #ifdef HAVE_TC_RAMCACHE
4260 /* Import runtime statistics if we just initialized the db. */
4261 if (hdr
->h
.serial
== 0)
4262 queue_post(&tagcache_queue
, Q_IMPORT_CHANGELOG
, 0);
4269 #ifdef HAVE_TC_RAMCACHE
4270 static void load_ramcache(void)
4277 /* At first we should load the cache (if exists). */
4278 tc_stat
.ramcache
= load_tagcache();
4280 if (!tc_stat
.ramcache
)
4282 /* If loading failed, it must indicate some problem with the db
4283 * so disable it entirely to prevent further issues. */
4284 tc_stat
.ready
= false;
4291 void tagcache_unload_ramcache(void)
4293 tc_stat
.ramcache
= false;
4294 /* Just to make sure there is no statefile present. */
4295 // remove(TAGCACHE_STATEFILE);
4300 static void tagcache_thread(void)
4302 struct queue_event ev
;
4303 bool check_done
= false;
4305 /* If the previous cache build/update was interrupted, commit
4306 * the changes first in foreground. */
4312 #ifdef HAVE_TC_RAMCACHE
4313 # ifdef HAVE_EEPROM_SETTINGS
4314 if (firmware_settings
.initialized
&& firmware_settings
.disk_clean
)
4315 check_done
= tagcache_dumpload();
4317 remove(TAGCACHE_STATEFILE
);
4320 /* Allocate space for the tagcache if found on disk. */
4321 if (global_settings
.tagcache_ram
&& !tc_stat
.ramcache
)
4322 allocate_tagcache();
4326 tc_stat
.initialized
= true;
4328 /* Don't delay bootup with the header check but do it on background. */
4330 tc_stat
.ready
= check_all_headers();
4331 tc_stat
.readyvalid
= true;
4335 run_command_queue(false);
4337 queue_wait_w_tmo(&tagcache_queue
, &ev
, HZ
);
4341 case Q_IMPORT_CHANGELOG
:
4342 tagcache_import_changelog();
4347 remove(TAGCACHE_FILE_TEMP
);
4348 tagcache_build("/");
4352 tagcache_build("/");
4353 #ifdef HAVE_TC_RAMCACHE
4356 check_deleted_files();
4362 if (check_done
|| !tc_stat
.ready
)
4365 #ifdef HAVE_TC_RAMCACHE
4366 if (!tc_stat
.ramcache
&& global_settings
.tagcache_ram
)
4369 if (tc_stat
.ramcache
&& global_settings
.tagcache_autoupdate
)
4370 tagcache_build("/");
4374 if (global_settings
.tagcache_autoupdate
)
4376 tagcache_build("/");
4378 /* This will be very slow unless dircache is enabled
4379 or target is flash based, but do it anyway for
4381 check_deleted_files();
4384 logf("tagcache check done");
4396 case SYS_USB_CONNECTED
:
4397 logf("USB: TagCache");
4398 usb_acknowledge(SYS_USB_CONNECTED_ACK
);
4399 usb_wait_for_disconnect(&tagcache_queue
);
4406 bool tagcache_prepare_shutdown(void)
4408 if (tagcache_get_commit_step() > 0)
4411 tagcache_stop_scan();
4412 while (read_lock
|| write_lock
)
4418 void tagcache_shutdown(void)
4420 /* Flush the command queue. */
4421 run_command_queue(true);
4423 #ifdef HAVE_EEPROM_SETTINGS
4424 if (tc_stat
.ramcache
)
4425 tagcache_dumpsave();
4429 static int get_progress(void)
4431 int total_count
= -1;
4433 #ifdef HAVE_DIRCACHE
4434 if (dircache_is_enabled())
4436 total_count
= dircache_get_entry_count();
4440 #ifdef HAVE_TC_RAMCACHE
4442 if (hdr
&& tc_stat
.ramcache
)
4443 total_count
= hdr
->h
.tch
.entry_count
;
4447 if (total_count
< 0)
4450 return processed_dir_count
* 100 / total_count
;
4453 struct tagcache_stat
* tagcache_get_stat(void)
4455 tc_stat
.progress
= get_progress();
4456 tc_stat
.processed_entries
= processed_dir_count
;
4461 void tagcache_start_scan(void)
4463 queue_post(&tagcache_queue
, Q_START_SCAN
, 0);
4466 bool tagcache_update(void)
4471 queue_post(&tagcache_queue
, Q_UPDATE
, 0);
4475 bool tagcache_rebuild()
4477 queue_post(&tagcache_queue
, Q_REBUILD
, 0);
4481 void tagcache_stop_scan(void)
4483 queue_post(&tagcache_queue
, Q_STOP_SCAN
, 0);
4486 #ifdef HAVE_TC_RAMCACHE
4487 bool tagcache_is_ramcache(void)
4489 return tc_stat
.ramcache
;
4493 #endif /* !__PCTOOL__ */
4496 void tagcache_init(void)
4498 memset(&tc_stat
, 0, sizeof(struct tagcache_stat
));
4499 memset(¤t_tcmh
, 0, sizeof(struct master_header
));
4500 filenametag_fd
= -1;
4501 write_lock
= read_lock
= 0;
4504 mutex_init(&command_queue_mutex
);
4505 queue_init(&tagcache_queue
, true);
4506 create_thread(tagcache_thread
, tagcache_stack
,
4507 sizeof(tagcache_stack
), 0, tagcache_thread_name
4508 IF_PRIO(, PRIORITY_BACKGROUND
)
4511 tc_stat
.initialized
= true;
4515 tc_stat
.ready
= check_all_headers();
4520 void tagcache_reverse_scan(void)
4522 logf("Checking for deleted files");
4523 check_deleted_files();
4527 bool tagcache_is_initialized(void)
4529 return tc_stat
.initialized
;
4531 bool tagcache_is_usable(void)
4533 return tc_stat
.initialized
&& tc_stat
.ready
;
4535 int tagcache_get_commit_step(void)
4537 return tc_stat
.commit_step
;
4539 int tagcache_get_max_commit_step(void)
4541 return (int)(sizeof(sorted_tags
)/sizeof(sorted_tags
[0]))+1;