1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
10 * Copyright (C) 2005 by Miika Pekkarinen
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
25 * ----------x---------x------------------x-----
27 * +---------------x-------+ | TagCache | Libraries
28 * | Modification routines | | Core |
29 * +-x---------x-----------+ | |
31 * | +------x-------------x-+ +-------------x-----+ |
32 * | | x==x Filters & clauses | |
33 * | | Search routines | +-------------------+ |
34 * | | x============================x DirCache
35 * | +-x--------------------+ | (optional)
37 * | | +-------------------------------+ +---------+ |
38 * | | | DB Commit (sort,unique,index) | | | |
39 * | | +-x--------------------------x--+ | Control | |
40 * | | | (R/W) | (R) | Thread | |
41 * | | | +----------------------+ | | | |
42 * | | | | TagCache DB Builder | | +---------+ |
43 * | | | +-x-------------x------+ | |
44 * | | | | (R) | (W) | |
45 * | | | | +--x--------x---------+ |
46 * | | | | | Temporary Commit DB | |
47 * | | | | +---------------------+ |
48 * +-x----x-------x--+ |
49 * | TagCache RAM DB x==\(W) +-----------------+ |
50 * +-----------------+ \===x | |
51 * | | | | (R) | Ram DB Loader x============x DirCache
52 * +-x----x---x---x---+ /==x | | (optional)
53 * | Tagcache Disk DB x==/ +-----------------+ |
54 * +------------------+ |
58 /* #define LOGF_ENABLE */
64 #include "ata_idle_notify.h"
84 #include "eeprom_settings.h"
88 #define yield() do { } while(0)
89 #define sim_sleep(timeout) do { } while(0)
90 #define do_timed_yield() do { } while(0)
94 /* Tag Cache thread. */
95 static struct event_queue tagcache_queue
;
96 static long tagcache_stack
[(DEFAULT_STACK_SIZE
+ 0x4000)/sizeof(long)];
97 static const char tagcache_thread_name
[] = "tagcache";
100 #define UNTAGGED "<Untagged>"
102 /* Previous path when scanning directory tree recursively. */
103 static char curpath
[TAG_MAXLEN
+32];
105 /* Used when removing duplicates. */
106 static char *tempbuf
; /* Allocated when needed. */
107 static long tempbufidx
; /* Current location in buffer. */
108 static long tempbuf_size
; /* Buffer size (TEMPBUF_SIZE). */
109 static long tempbuf_left
; /* Buffer space left. */
110 static long tempbuf_pos
;
112 /* Tags we want to get sorted (loaded to the tempbuf). */
113 static const int sorted_tags
[] = { tag_artist
, tag_album
, tag_genre
,
114 tag_composer
, tag_comment
, tag_albumartist
, tag_grouping
, tag_title
};
116 /* Uniqued tags (we can use these tags with filters and conditional clauses). */
117 static const int unique_tags
[] = { tag_artist
, tag_album
, tag_genre
,
118 tag_composer
, tag_comment
, tag_albumartist
, tag_grouping
};
120 /* Numeric tags (we can use these tags with conditional clauses). */
121 static const int numeric_tags
[] = { tag_year
, tag_discnumber
,
122 tag_tracknumber
, tag_length
, tag_bitrate
, tag_playcount
, tag_rating
,
123 tag_playtime
, tag_lastplayed
, tag_commitid
, tag_mtime
,
124 tag_virt_length_min
, tag_virt_length_sec
,
125 tag_virt_playtime_min
, tag_virt_playtime_sec
,
126 tag_virt_entryage
, tag_virt_autoscore
};
128 /* String presentation of the tags defined in tagcache.h. Must be in correct order! */
129 static const char *tags_str
[] = { "artist", "album", "genre", "title",
130 "filename", "composer", "comment", "albumartist", "grouping", "year",
131 "discnumber", "tracknumber", "bitrate", "length", "playcount", "rating",
132 "playtime", "lastplayed", "commitid", "mtime" };
134 /* Status information of the tagcache. */
135 static struct tagcache_stat tc_stat
;
137 /* Queue commands. */
138 enum tagcache_queue
{
145 /* Internal tagcache command queue. */
146 CMD_UPDATE_MASTER_HEADER
,
150 struct tagcache_command_entry
{
158 static struct tagcache_command_entry command_queue
[TAGCACHE_COMMAND_QUEUE_LENGTH
];
159 static volatile int command_queue_widx
= 0;
160 static volatile int command_queue_ridx
= 0;
161 static struct mutex command_queue_mutex
;
164 /* Tag database structures. */
166 /* Variable-length tag entry in tag files. */
167 struct tagfile_entry
{
168 int32_t tag_length
; /* Length of the data in bytes including '\0' */
169 int32_t idx_id
; /* Corresponding entry location in index file of not unique tags */
170 char tag_data
[0]; /* Begin of the tag data */
173 /* Fixed-size tag entry in master db index. */
175 int32_t tag_seek
[TAG_COUNT
]; /* Location of tag data or numeric tag data */
176 int32_t flag
; /* Status flags */
179 /* Header is the same in every file. */
180 struct tagcache_header
{
181 int32_t magic
; /* Header version number */
182 int32_t datasize
; /* Data size in bytes */
183 int32_t entry_count
; /* Number of entries in this file */
186 struct master_header
{
187 struct tagcache_header tch
;
188 int32_t serial
; /* Increasing counting number */
189 int32_t commitid
; /* Number of commits so far */
193 /* For the endianess correction */
194 static const char *tagfile_entry_ec
= "ll";
196 Note: This should be (1 + TAG_COUNT) amount of l's.
198 static const char *index_entry_ec
= "lllllllllllllllllllll";
200 static const char *tagcache_header_ec
= "lll";
201 static const char *master_header_ec
= "llllll";
203 static struct master_header current_tcmh
;
205 #ifdef HAVE_TC_RAMCACHE
206 /* Header is created when loading database to ram. */
207 struct ramcache_header
{
208 struct master_header h
; /* Header from the master index */
209 struct index_entry
*indices
; /* Master index file content */
210 char *tags
[TAG_COUNT
]; /* Tag file content (not including filename tag) */
211 int entry_count
[TAG_COUNT
]; /* Number of entries in the indices. */
214 # ifdef HAVE_EEPROM_SETTINGS
215 struct statefile_header
{
216 struct ramcache_header
*hdr
;
217 struct tagcache_stat tc_stat
;
221 /* Pointer to allocated ramcache_header */
222 static struct ramcache_header
*hdr
;
226 * Full tag entries stored in a temporary file waiting
227 * for commit to the cache. */
228 struct temp_file_entry
{
229 long tag_offset
[TAG_COUNT
];
230 short tag_length
[TAG_COUNT
];
236 struct tempbuf_id_list
{
238 struct tempbuf_id_list
*next
;
241 struct tempbuf_searchidx
{
245 struct tempbuf_id_list idlist
;
248 /* Lookup buffer for fixing messed up index while after sorting. */
249 static long commit_entry_count
;
250 static long lookup_buffer_depth
;
251 static struct tempbuf_searchidx
**lookup
;
253 /* Used when building the temporary file. */
254 static int cachefd
= -1, filenametag_fd
;
255 static int total_entry_count
= 0;
256 static int data_size
= 0;
257 static int processed_dir_count
;
259 /* Thread safe locking */
260 static volatile int write_lock
;
261 static volatile int read_lock
;
263 static bool delete_entry(long idx_id
);
265 const char* tagcache_tag_to_str(int tag
)
267 return tags_str
[tag
];
270 bool tagcache_is_numeric_tag(int type
)
274 for (i
= 0; i
< (int)(sizeof(numeric_tags
)/sizeof(numeric_tags
[0])); i
++)
276 if (type
== numeric_tags
[i
])
283 bool tagcache_is_unique_tag(int type
)
287 for (i
= 0; i
< (int)(sizeof(unique_tags
)/sizeof(unique_tags
[0])); i
++)
289 if (type
== unique_tags
[i
])
296 bool tagcache_is_sorted_tag(int type
)
300 for (i
= 0; i
< (int)(sizeof(sorted_tags
)/sizeof(sorted_tags
[0])); i
++)
302 if (type
== sorted_tags
[i
])
311 * Returns true if specified flag is still present, i.e., dircache
312 * has not been reloaded.
314 static bool is_dircache_intact(void)
316 return dircache_get_appflag(DIRCACHE_APPFLAG_TAGCACHE
);
320 static int open_tag_fd(struct tagcache_header
*hdr
, int tag
, bool write
)
326 if (tagcache_is_numeric_tag(tag
) || tag
< 0 || tag
>= TAG_COUNT
)
329 snprintf(buf
, sizeof buf
, TAGCACHE_FILE_INDEX
, tag
);
331 fd
= open(buf
, write
? O_RDWR
: O_RDONLY
);
334 logf("tag file open failed: tag=%d write=%d file=%s", tag
, write
, buf
);
335 tc_stat
.ready
= false;
339 /* Check the header. */
340 rc
= ecread(fd
, hdr
, 1, tagcache_header_ec
, tc_stat
.econ
);
341 if (hdr
->magic
!= TAGCACHE_MAGIC
|| rc
!= sizeof(struct tagcache_header
))
343 logf("header error");
344 tc_stat
.ready
= false;
352 static int open_master_fd(struct master_header
*hdr
, bool write
)
357 fd
= open(TAGCACHE_FILE_MASTER
, write
? O_RDWR
: O_RDONLY
);
360 logf("master file open failed for R/W");
361 tc_stat
.ready
= false;
365 tc_stat
.econ
= false;
367 /* Check the header. */
368 rc
= read(fd
, hdr
, sizeof(struct master_header
));
369 if (hdr
->tch
.magic
== TAGCACHE_MAGIC
&& rc
== sizeof(struct master_header
))
375 /* Trying to read again, this time with endianess correction enabled. */
376 lseek(fd
, 0, SEEK_SET
);
378 rc
= ecread(fd
, hdr
, 1, master_header_ec
, true);
379 if (hdr
->tch
.magic
!= TAGCACHE_MAGIC
|| rc
!= sizeof(struct master_header
))
381 logf("header error");
382 tc_stat
.ready
= false;
393 static bool do_timed_yield(void)
395 /* Sorting can lock up for quite a while, so yield occasionally */
396 static long wakeup_tick
= 0;
397 if (current_tick
>= wakeup_tick
)
399 wakeup_tick
= current_tick
+ (HZ
/4);
407 #if defined(HAVE_TC_RAMCACHE) && defined(HAVE_DIRCACHE)
408 static long find_entry_ram(const char *filename
,
409 const struct dirent
*dc
)
411 static long last_pos
= 0;
414 /* Check if we tagcache is loaded into ram. */
415 if (!tc_stat
.ramcache
)
419 dc
= dircache_get_entry_ptr(filename
);
423 logf("tagcache: file not found.");
434 for (; i
< hdr
->h
.tch
.entry_count
; i
++)
436 if (hdr
->indices
[i
].tag_seek
[tag_filename
] == (long)dc
)
438 last_pos
= MAX(0, i
- 3);
455 static long find_entry_disk(const char *filename
)
457 struct tagcache_header tch
;
458 static long last_pos
= -1;
459 long pos_history
[POS_HISTORY_COUNT
];
460 long pos_history_idx
= 0;
462 struct tagfile_entry tfe
;
464 char buf
[TAG_MAXLEN
+32];
475 if ( (fd
= open_tag_fd(&tch
, tag_filename
, false)) < 0)
482 lseek(fd
, last_pos
, SEEK_SET
);
484 lseek(fd
, sizeof(struct tagcache_header
), SEEK_SET
);
488 pos
= lseek(fd
, 0, SEEK_CUR
);
489 for (i
= pos_history_idx
-1; i
>= 0; i
--)
490 pos_history
[i
+1] = pos_history
[i
];
491 pos_history
[0] = pos
;
493 if (ecread(fd
, &tfe
, 1, tagfile_entry_ec
, tc_stat
.econ
)
494 != sizeof(struct tagfile_entry
))
499 if (tfe
.tag_length
>= (long)sizeof(buf
))
501 logf("too long tag #1");
507 if (read(fd
, buf
, tfe
.tag_length
) != tfe
.tag_length
)
509 logf("read error #2");
515 if (!strcasecmp(filename
, buf
))
517 last_pos
= pos_history
[pos_history_idx
];
522 if (pos_history_idx
< POS_HISTORY_COUNT
- 1)
536 if (fd
!= filenametag_fd
)
541 if (fd
!= filenametag_fd
)
547 static int find_index(const char *filename
)
551 #if defined(HAVE_TC_RAMCACHE) && defined(HAVE_DIRCACHE)
552 if (tc_stat
.ramcache
&& is_dircache_intact())
553 idx_id
= find_entry_ram(filename
, NULL
);
557 idx_id
= find_entry_disk(filename
);
562 bool tagcache_find_index(struct tagcache_search
*tcs
, const char *filename
)
569 idx_id
= find_index(filename
);
573 if (!tagcache_search(tcs
, tag_filename
))
576 tcs
->entry_count
= 0;
577 tcs
->idx_id
= idx_id
;
582 static bool get_index(int masterfd
, int idxid
,
583 struct index_entry
*idx
, bool use_ram
)
585 bool localfd
= false;
589 logf("Incorrect idxid: %d", idxid
);
593 #ifdef HAVE_TC_RAMCACHE
594 if (tc_stat
.ramcache
&& use_ram
)
596 if (hdr
->indices
[idxid
].flag
& FLAG_DELETED
)
599 memcpy(idx
, &hdr
->indices
[idxid
], sizeof(struct index_entry
));
608 struct master_header tcmh
;
611 masterfd
= open_master_fd(&tcmh
, false);
616 lseek(masterfd
, idxid
* sizeof(struct index_entry
)
617 + sizeof(struct master_header
), SEEK_SET
);
618 if (ecread(masterfd
, idx
, 1, index_entry_ec
, tc_stat
.econ
)
619 != sizeof(struct index_entry
))
621 logf("read error #3");
631 if (idx
->flag
& FLAG_DELETED
)
639 static bool write_index(int masterfd
, int idxid
, struct index_entry
*idx
)
641 /* We need to exclude all memory only flags & tags when writing to disk. */
642 if (idx
->flag
& FLAG_DIRCACHE
)
644 logf("memory only flags!");
648 #ifdef HAVE_TC_RAMCACHE
649 /* Only update numeric data. Writing the whole index to RAM by memcpy
650 * destroys dircache pointers!
652 if (tc_stat
.ramcache
)
655 struct index_entry
*idx_ram
= &hdr
->indices
[idxid
];
657 for (tag
= 0; tag
< TAG_COUNT
; tag
++)
659 if (tagcache_is_numeric_tag(tag
))
661 idx_ram
->tag_seek
[tag
] = idx
->tag_seek
[tag
];
665 /* Don't touch the dircache flag or attributes. */
666 idx_ram
->flag
= (idx
->flag
& 0x0000ffff)
667 | (idx_ram
->flag
& (0xffff0000 | FLAG_DIRCACHE
));
671 lseek(masterfd
, idxid
* sizeof(struct index_entry
)
672 + sizeof(struct master_header
), SEEK_SET
);
673 if (ecwrite(masterfd
, idx
, 1, index_entry_ec
, tc_stat
.econ
)
674 != sizeof(struct index_entry
))
676 logf("write error #3");
677 logf("idxid: %d", idxid
);
684 #endif /* !__PCTOOL__ */
686 static bool open_files(struct tagcache_search
*tcs
, int tag
)
688 if (tcs
->idxfd
[tag
] < 0)
692 snprintf(fn
, sizeof fn
, TAGCACHE_FILE_INDEX
, tag
);
693 tcs
->idxfd
[tag
] = open(fn
, O_RDONLY
);
696 if (tcs
->idxfd
[tag
] < 0)
698 logf("File not open!");
705 static bool retrieve(struct tagcache_search
*tcs
, struct index_entry
*idx
,
706 int tag
, char *buf
, long size
)
708 struct tagfile_entry tfe
;
713 if (tagcache_is_numeric_tag(tag
))
716 seek
= idx
->tag_seek
[tag
];
719 logf("Retrieve failed");
723 #ifdef HAVE_TC_RAMCACHE
726 struct tagfile_entry
*ep
;
728 # ifdef HAVE_DIRCACHE
729 if (tag
== tag_filename
&& (idx
->flag
& FLAG_DIRCACHE
)
730 && is_dircache_intact())
732 dircache_copy_path((struct dirent
*)seek
,
738 if (tag
!= tag_filename
)
740 ep
= (struct tagfile_entry
*)&hdr
->tags
[tag
][seek
];
741 strncpy(buf
, ep
->tag_data
, size
-1);
748 if (!open_files(tcs
, tag
))
751 lseek(tcs
->idxfd
[tag
], seek
, SEEK_SET
);
752 if (ecread(tcs
->idxfd
[tag
], &tfe
, 1, tagfile_entry_ec
, tc_stat
.econ
)
753 != sizeof(struct tagfile_entry
))
755 logf("read error #5");
759 if (tfe
.tag_length
>= size
)
761 logf("too small buffer");
765 if (read(tcs
->idxfd
[tag
], buf
, tfe
.tag_length
) !=
768 logf("read error #6");
772 buf
[tfe
.tag_length
] = '\0';
777 static long check_virtual_tags(int tag
, const struct index_entry
*idx
)
783 case tag_virt_length_sec
:
784 data
= (idx
->tag_seek
[tag_length
]/1000) % 60;
787 case tag_virt_length_min
:
788 data
= (idx
->tag_seek
[tag_length
]/1000) / 60;
791 case tag_virt_playtime_sec
:
792 data
= (idx
->tag_seek
[tag_playtime
]/1000) % 60;
795 case tag_virt_playtime_min
:
796 data
= (idx
->tag_seek
[tag_playtime
]/1000) / 60;
799 case tag_virt_autoscore
:
800 if (idx
->tag_seek
[tag_length
] == 0
801 || idx
->tag_seek
[tag_playcount
] == 0)
807 data
= 100 * idx
->tag_seek
[tag_playtime
]
808 / idx
->tag_seek
[tag_length
]
809 / idx
->tag_seek
[tag_playcount
];
813 /* How many commits before the file has been added to the DB. */
814 case tag_virt_entryage
:
815 data
= current_tcmh
.commitid
- idx
->tag_seek
[tag_commitid
] - 1;
819 data
= idx
->tag_seek
[tag
];
825 long tagcache_get_numeric(const struct tagcache_search
*tcs
, int tag
)
827 struct index_entry idx
;
832 if (!tagcache_is_numeric_tag(tag
))
835 if (!get_index(tcs
->masterfd
, tcs
->idx_id
, &idx
, true))
838 return check_virtual_tags(tag
, &idx
);
841 inline static bool str_ends_with(const char *str1
, const char *str2
)
843 int str_len
= strlen(str1
);
844 int clause_len
= strlen(str2
);
846 if (clause_len
> str_len
)
849 return !strcasecmp(&str1
[str_len
- clause_len
], str2
);
852 inline static bool str_oneof(const char *str
, const char *list
)
855 int l
, len
= strlen(str
);
859 sep
= strchr(list
, '|');
860 l
= sep
? (long)sep
- (long)list
: (int)strlen(list
);
861 if ((l
==len
) && !strncasecmp(str
, list
, len
))
863 list
+= sep
? l
+ 1 : l
;
869 static bool check_against_clause(long numeric
, const char *str
,
870 const struct tagcache_search_clause
*clause
)
874 switch (clause
->type
)
877 return numeric
== clause
->numeric_data
;
879 return numeric
!= clause
->numeric_data
;
881 return numeric
> clause
->numeric_data
;
883 return numeric
>= clause
->numeric_data
;
885 return numeric
< clause
->numeric_data
;
887 return numeric
<= clause
->numeric_data
;
889 logf("Incorrect numeric tag: %d", clause
->type
);
894 switch (clause
->type
)
897 return !strcasecmp(clause
->str
, str
);
899 return strcasecmp(clause
->str
, str
);
901 return 0>strcasecmp(clause
->str
, str
);
903 return 0>=strcasecmp(clause
->str
, str
);
905 return 0<strcasecmp(clause
->str
, str
);
907 return 0<=strcasecmp(clause
->str
, str
);
908 case clause_contains
:
909 return (strcasestr(str
, clause
->str
) != NULL
);
910 case clause_not_contains
:
911 return (strcasestr(str
, clause
->str
) == NULL
);
912 case clause_begins_with
:
913 return (strcasestr(str
, clause
->str
) == str
);
914 case clause_not_begins_with
:
915 return (strcasestr(str
, clause
->str
) != str
);
916 case clause_ends_with
:
917 return str_ends_with(str
, clause
->str
);
918 case clause_not_ends_with
:
919 return !str_ends_with(str
, clause
->str
);
921 return str_oneof(str
, clause
->str
);
924 logf("Incorrect tag: %d", clause
->type
);
931 static bool check_clauses(struct tagcache_search
*tcs
,
932 struct index_entry
*idx
,
933 struct tagcache_search_clause
**clause
, int count
)
937 #ifdef HAVE_TC_RAMCACHE
940 /* Go through all conditional clauses. */
941 for (i
= 0; i
< count
; i
++)
943 struct tagfile_entry
*tfe
;
948 seek
= check_virtual_tags(clause
[i
]->tag
, idx
);
950 if (!tagcache_is_numeric_tag(clause
[i
]->tag
))
952 if (clause
[i
]->tag
== tag_filename
)
954 retrieve(tcs
, idx
, tag_filename
, buf
, sizeof buf
);
959 tfe
= (struct tagfile_entry
*)&hdr
->tags
[clause
[i
]->tag
][seek
];
964 if (!check_against_clause(seek
, str
, clause
[i
]))
971 /* Check for conditions. */
972 for (i
= 0; i
< count
; i
++)
974 struct tagfile_entry tfe
;
978 seek
= check_virtual_tags(clause
[i
]->tag
, idx
);
980 memset(str
, 0, sizeof str
);
981 if (!tagcache_is_numeric_tag(clause
[i
]->tag
))
983 int fd
= tcs
->idxfd
[clause
[i
]->tag
];
984 lseek(fd
, seek
, SEEK_SET
);
985 ecread(fd
, &tfe
, 1, tagfile_entry_ec
, tc_stat
.econ
);
986 if (tfe
.tag_length
>= (int)sizeof(str
))
988 logf("Too long tag read!");
992 read(fd
, str
, tfe
.tag_length
);
994 /* Check if entry has been deleted. */
999 if (!check_against_clause(seek
, str
, clause
[i
]))
1007 bool tagcache_check_clauses(struct tagcache_search
*tcs
,
1008 struct tagcache_search_clause
**clause
, int count
)
1010 struct index_entry idx
;
1015 if (!get_index(tcs
->masterfd
, tcs
->idx_id
, &idx
, true))
1018 return check_clauses(tcs
, &idx
, clause
, count
);
1021 static bool add_uniqbuf(struct tagcache_search
*tcs
, unsigned long id
)
1025 /* If uniq buffer is not defined we must return true for search to work. */
1026 if (tcs
->unique_list
== NULL
1027 || (!tagcache_is_unique_tag(tcs
->type
)
1028 && !tagcache_is_numeric_tag(tcs
->type
)))
1033 for (i
= 0; i
< tcs
->unique_list_count
; i
++)
1035 /* Return false if entry is found. */
1036 if (tcs
->unique_list
[i
] == id
)
1040 if (tcs
->unique_list_count
< tcs
->unique_list_capacity
)
1042 tcs
->unique_list
[i
] = id
;
1043 tcs
->unique_list_count
++;
1049 static bool build_lookup_list(struct tagcache_search
*tcs
)
1051 struct index_entry entry
;
1054 tcs
->seek_list_count
= 0;
1056 #ifdef HAVE_TC_RAMCACHE
1061 for (i
= tcs
->seek_pos
; i
< hdr
->h
.tch
.entry_count
; i
++)
1063 struct index_entry
*idx
= &hdr
->indices
[i
];
1064 if (tcs
->seek_list_count
== SEEK_LIST_SIZE
)
1067 /* Skip deleted files. */
1068 if (idx
->flag
& FLAG_DELETED
)
1071 /* Go through all filters.. */
1072 for (j
= 0; j
< tcs
->filter_count
; j
++)
1074 if (idx
->tag_seek
[tcs
->filter_tag
[j
]] != tcs
->filter_seek
[j
])
1080 if (j
< tcs
->filter_count
)
1083 /* Check for conditions. */
1084 if (!check_clauses(tcs
, idx
, tcs
->clause
, tcs
->clause_count
))
1087 /* Add to the seek list if not already in uniq buffer. */
1088 if (!add_uniqbuf(tcs
, idx
->tag_seek
[tcs
->type
]))
1092 tcs
->seek_list
[tcs
->seek_list_count
] = idx
->tag_seek
[tcs
->type
];
1093 tcs
->seek_flags
[tcs
->seek_list_count
] = idx
->flag
;
1094 tcs
->seek_list_count
++;
1099 return tcs
->seek_list_count
> 0;
1103 lseek(tcs
->masterfd
, tcs
->seek_pos
* sizeof(struct index_entry
) +
1104 sizeof(struct master_header
), SEEK_SET
);
1106 while (ecread(tcs
->masterfd
, &entry
, 1, index_entry_ec
, tc_stat
.econ
)
1107 == sizeof(struct index_entry
))
1109 if (tcs
->seek_list_count
== SEEK_LIST_SIZE
)
1114 /* Check if entry has been deleted. */
1115 if (entry
.flag
& FLAG_DELETED
)
1118 /* Go through all filters.. */
1119 for (i
= 0; i
< tcs
->filter_count
; i
++)
1121 if (entry
.tag_seek
[tcs
->filter_tag
[i
]] != tcs
->filter_seek
[i
])
1125 if (i
< tcs
->filter_count
)
1128 /* Check for conditions. */
1129 if (!check_clauses(tcs
, &entry
, tcs
->clause
, tcs
->clause_count
))
1132 /* Add to the seek list if not already in uniq buffer. */
1133 if (!add_uniqbuf(tcs
, entry
.tag_seek
[tcs
->type
]))
1137 tcs
->seek_list
[tcs
->seek_list_count
] = entry
.tag_seek
[tcs
->type
];
1138 tcs
->seek_flags
[tcs
->seek_list_count
] = entry
.flag
;
1139 tcs
->seek_list_count
++;
1144 return tcs
->seek_list_count
> 0;
1148 static void remove_files(void)
1153 tc_stat
.ready
= false;
1154 tc_stat
.ramcache
= false;
1155 tc_stat
.econ
= false;
1156 remove(TAGCACHE_FILE_MASTER
);
1157 for (i
= 0; i
< TAG_COUNT
; i
++)
1159 if (tagcache_is_numeric_tag(i
))
1162 snprintf(buf
, sizeof buf
, TAGCACHE_FILE_INDEX
, i
);
1168 static bool check_all_headers(void)
1170 struct master_header myhdr
;
1171 struct tagcache_header tch
;
1175 if ( (fd
= open_master_fd(&myhdr
, false)) < 0)
1181 logf("tagcache is dirty!");
1185 memcpy(¤t_tcmh
, &myhdr
, sizeof(struct master_header
));
1187 for (tag
= 0; tag
< TAG_COUNT
; tag
++)
1189 if (tagcache_is_numeric_tag(tag
))
1192 if ( (fd
= open_tag_fd(&tch
, tag
, false)) < 0)
1201 bool tagcache_search(struct tagcache_search
*tcs
, int tag
)
1203 struct tagcache_header tag_hdr
;
1204 struct master_header master_hdr
;
1207 if (tcs
->initialized
)
1208 tagcache_search_finish(tcs
);
1213 memset(tcs
, 0, sizeof(struct tagcache_search
));
1214 if (tc_stat
.commit_step
> 0 || !tc_stat
.ready
)
1217 tcs
->position
= sizeof(struct tagcache_header
);
1220 tcs
->seek_list_count
= 0;
1221 tcs
->filter_count
= 0;
1224 for (i
= 0; i
< TAG_COUNT
; i
++)
1227 #ifndef HAVE_TC_RAMCACHE
1228 tcs
->ramsearch
= false;
1230 tcs
->ramsearch
= tc_stat
.ramcache
;
1233 tcs
->entry_count
= hdr
->entry_count
[tcs
->type
];
1238 if (!tagcache_is_numeric_tag(tcs
->type
))
1240 tcs
->idxfd
[tcs
->type
] = open_tag_fd(&tag_hdr
, tcs
->type
, false);
1241 if (tcs
->idxfd
[tcs
->type
] < 0)
1245 /* Always open as R/W so we can pass tcs to functions that modify data also
1246 * without failing. */
1247 tcs
->masterfd
= open_master_fd(&master_hdr
, true);
1249 if (tcs
->masterfd
< 0)
1254 tcs
->initialized
= true;
1260 void tagcache_search_set_uniqbuf(struct tagcache_search
*tcs
,
1261 void *buffer
, long length
)
1263 tcs
->unique_list
= (unsigned long *)buffer
;
1264 tcs
->unique_list_capacity
= length
/ sizeof(*tcs
->unique_list
);
1265 tcs
->unique_list_count
= 0;
1268 bool tagcache_search_add_filter(struct tagcache_search
*tcs
,
1271 if (tcs
->filter_count
== TAGCACHE_MAX_FILTERS
)
1274 if (!tagcache_is_unique_tag(tag
) || tagcache_is_numeric_tag(tag
))
1277 tcs
->filter_tag
[tcs
->filter_count
] = tag
;
1278 tcs
->filter_seek
[tcs
->filter_count
] = seek
;
1279 tcs
->filter_count
++;
1284 bool tagcache_search_add_clause(struct tagcache_search
*tcs
,
1285 struct tagcache_search_clause
*clause
)
1289 if (tcs
->clause_count
>= TAGCACHE_MAX_CLAUSES
)
1291 logf("Too many clauses");
1295 /* Check if there is already a similar filter in present (filters are
1296 * much faster than clauses).
1298 for (i
= 0; i
< tcs
->filter_count
; i
++)
1300 if (tcs
->filter_tag
[i
] == clause
->tag
)
1304 if (!tagcache_is_numeric_tag(clause
->tag
) && tcs
->idxfd
[clause
->tag
] < 0)
1308 snprintf(buf
, sizeof buf
, TAGCACHE_FILE_INDEX
, clause
->tag
);
1309 tcs
->idxfd
[clause
->tag
] = open(buf
, O_RDONLY
);
1312 tcs
->clause
[tcs
->clause_count
] = clause
;
1313 tcs
->clause_count
++;
1318 /* TODO: Remove this mess. */
1319 #ifdef HAVE_DIRCACHE
1320 #define TAG_FILENAME_RAM(tcs) ((tcs->type == tag_filename) \
1321 ? ((flag & FLAG_DIRCACHE) && is_dircache_intact()) : 1)
1323 #define TAG_FILENAME_RAM(tcs) (tcs->type != tag_filename)
1326 static bool get_next(struct tagcache_search
*tcs
)
1328 static char buf
[TAG_MAXLEN
+32];
1329 struct tagfile_entry entry
;
1332 if (!tcs
->valid
|| !tc_stat
.ready
)
1335 if (tcs
->idxfd
[tcs
->type
] < 0 && !tagcache_is_numeric_tag(tcs
->type
)
1336 #ifdef HAVE_TC_RAMCACHE
1342 /* Relative fetch. */
1343 if (tcs
->filter_count
> 0 || tcs
->clause_count
> 0
1344 || tagcache_is_numeric_tag(tcs
->type
))
1346 /* Check for end of list. */
1347 if (tcs
->seek_list_count
== 0)
1349 /* Try to fetch more. */
1350 if (!build_lookup_list(tcs
))
1357 tcs
->seek_list_count
--;
1358 flag
= tcs
->seek_flags
[tcs
->seek_list_count
];
1360 /* Seek stream to the correct position and continue to direct fetch. */
1361 if ((!tcs
->ramsearch
|| !TAG_FILENAME_RAM(tcs
))
1362 && !tagcache_is_numeric_tag(tcs
->type
))
1364 if (!open_files(tcs
, tcs
->type
))
1367 lseek(tcs
->idxfd
[tcs
->type
], tcs
->seek_list
[tcs
->seek_list_count
], SEEK_SET
);
1370 tcs
->position
= tcs
->seek_list
[tcs
->seek_list_count
];
1373 if (tagcache_is_numeric_tag(tcs
->type
))
1375 snprintf(buf
, sizeof(buf
), "%d", tcs
->position
);
1376 tcs
->result_seek
= tcs
->position
;
1378 tcs
->result_len
= strlen(buf
) + 1;
1383 #ifdef HAVE_TC_RAMCACHE
1384 if (tcs
->ramsearch
&& TAG_FILENAME_RAM(tcs
))
1386 struct tagfile_entry
*ep
;
1388 if (tcs
->entry_count
== 0)
1395 tcs
->result_seek
= tcs
->position
;
1397 # ifdef HAVE_DIRCACHE
1398 if (tcs
->type
== tag_filename
)
1400 dircache_copy_path((struct dirent
*)tcs
->position
,
1403 tcs
->result_len
= strlen(buf
) + 1;
1404 tcs
->idx_id
= FLAG_GET_ATTR(flag
);
1405 tcs
->ramresult
= false;
1411 ep
= (struct tagfile_entry
*)&hdr
->tags
[tcs
->type
][tcs
->position
];
1412 tcs
->position
+= sizeof(struct tagfile_entry
) + ep
->tag_length
;
1413 tcs
->result
= ep
->tag_data
;
1414 tcs
->result_len
= strlen(tcs
->result
) + 1;
1415 tcs
->idx_id
= ep
->idx_id
;
1416 tcs
->ramresult
= true;
1423 if (!open_files(tcs
, tcs
->type
))
1426 tcs
->result_seek
= lseek(tcs
->idxfd
[tcs
->type
], 0, SEEK_CUR
);
1427 if (ecread(tcs
->idxfd
[tcs
->type
], &entry
, 1,
1428 tagfile_entry_ec
, tc_stat
.econ
) != sizeof(struct tagfile_entry
))
1436 if (entry
.tag_length
> (long)sizeof(buf
))
1439 logf("too long tag #2");
1443 if (read(tcs
->idxfd
[tcs
->type
], buf
, entry
.tag_length
) != entry
.tag_length
)
1446 logf("read error #4");
1451 tcs
->result_len
= strlen(tcs
->result
) + 1;
1452 tcs
->idx_id
= entry
.idx_id
;
1453 tcs
->ramresult
= false;
1458 bool tagcache_get_next(struct tagcache_search
*tcs
)
1460 while (get_next(tcs
))
1462 if (tcs
->result_len
> 1)
1469 bool tagcache_retrieve(struct tagcache_search
*tcs
, int idxid
,
1470 int tag
, char *buf
, long size
)
1472 struct index_entry idx
;
1475 if (!get_index(tcs
->masterfd
, idxid
, &idx
, true))
1478 return retrieve(tcs
, &idx
, tag
, buf
, size
);
1481 static bool update_master_header(void)
1483 struct master_header myhdr
;
1489 if ( (fd
= open_master_fd(&myhdr
, true)) < 0)
1492 myhdr
.serial
= current_tcmh
.serial
;
1493 myhdr
.commitid
= current_tcmh
.commitid
;
1496 lseek(fd
, 0, SEEK_SET
);
1497 ecwrite(fd
, &myhdr
, 1, master_header_ec
, tc_stat
.econ
);
1500 #ifdef HAVE_TC_RAMCACHE
1503 hdr
->h
.serial
= current_tcmh
.serial
;
1504 hdr
->h
.commitid
= current_tcmh
.commitid
;
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");
2908 tc_stat
.commit_delayed
= true;
2909 tc_stat
.commit_step
= 0;
2915 if (!build_numeric_indices(&tch
, tmpfd
))
2917 logf("Failure to commit numeric indices");
2920 tc_stat
.commit_step
= 0;
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 remove(TAGCACHE_FILE_TEMP
);
2948 tc_stat
.ready
= check_all_headers();
2949 tc_stat
.readyvalid
= true;
2951 if (local_allocation
)
2957 #ifdef HAVE_DIRCACHE
2958 /* Rebuild the dircache, if we stole the buffer. */
2959 if (dircache_buffer_stolen
)
2963 #ifdef HAVE_TC_RAMCACHE
2964 /* Reload tagcache. */
2965 if (tc_stat
.ramcache_allocated
> 0)
2966 tagcache_start_scan();
2974 static void allocate_tempbuf(void)
2976 /* Yeah, malloc would be really nice now :) */
2978 tempbuf_size
= 32*1024*1024;
2979 tempbuf
= malloc(tempbuf_size
);
2981 tempbuf
= (char *)(((long)audiobuf
& ~0x03) + 0x04);
2982 tempbuf_size
= (long)audiobufend
- (long)audiobuf
- 4;
2983 audiobuf
+= tempbuf_size
;
2987 static void free_tempbuf(void)
2989 if (tempbuf_size
== 0)
2995 audiobuf
-= tempbuf_size
;
3003 static bool modify_numeric_entry(int masterfd
, int idx_id
, int tag
, long data
)
3005 struct index_entry idx
;
3010 if (!tagcache_is_numeric_tag(tag
))
3013 if (!get_index(masterfd
, idx_id
, &idx
, false))
3016 idx
.tag_seek
[tag
] = data
;
3017 idx
.flag
|= FLAG_DIRTYNUM
;
3019 return write_index(masterfd
, idx_id
, &idx
);
3023 bool tagcache_modify_numeric_entry(struct tagcache_search
*tcs
,
3026 struct master_header myhdr
;
3028 if (tcs
->masterfd
< 0)
3030 if ( (tcs
->masterfd
= open_master_fd(&myhdr
, true)) < 0)
3034 return modify_numeric_entry(tcs
->masterfd
, tcs
->idx_id
, tag
, data
);
3038 #define COMMAND_QUEUE_IS_EMPTY (command_queue_ridx == command_queue_widx)
3040 static bool command_queue_is_full(void)
3044 next
= command_queue_widx
+ 1;
3045 if (next
>= TAGCACHE_COMMAND_QUEUE_LENGTH
)
3048 return (next
== command_queue_ridx
);
3051 static bool command_queue_sync_callback(void)
3054 struct master_header myhdr
;
3057 mutex_lock(&command_queue_mutex
);
3059 if ( (masterfd
= open_master_fd(&myhdr
, true)) < 0)
3062 while (command_queue_ridx
!= command_queue_widx
)
3064 struct tagcache_command_entry
*ce
= &command_queue
[command_queue_ridx
];
3066 switch (ce
->command
)
3068 case CMD_UPDATE_MASTER_HEADER
:
3071 update_master_header();
3073 /* Re-open the masterfd. */
3074 if ( (masterfd
= open_master_fd(&myhdr
, true)) < 0)
3079 case CMD_UPDATE_NUMERIC
:
3081 modify_numeric_entry(masterfd
, ce
->idx_id
, ce
->tag
, ce
->data
);
3086 if (++command_queue_ridx
>= TAGCACHE_COMMAND_QUEUE_LENGTH
)
3087 command_queue_ridx
= 0;
3092 tc_stat
.queue_length
= 0;
3093 mutex_unlock(&command_queue_mutex
);
3097 static void run_command_queue(bool force
)
3099 if (COMMAND_QUEUE_IS_EMPTY
)
3102 if (force
|| command_queue_is_full())
3103 command_queue_sync_callback();
3105 register_storage_idle_func(command_queue_sync_callback
);
3108 static void queue_command(int cmd
, long idx_id
, int tag
, long data
)
3114 mutex_lock(&command_queue_mutex
);
3115 next
= command_queue_widx
+ 1;
3116 if (next
>= TAGCACHE_COMMAND_QUEUE_LENGTH
)
3119 /* Make sure queue is not full. */
3120 if (next
!= command_queue_ridx
)
3122 struct tagcache_command_entry
*ce
= &command_queue
[command_queue_widx
];
3125 ce
->idx_id
= idx_id
;
3129 command_queue_widx
= next
;
3131 tc_stat
.queue_length
++;
3133 mutex_unlock(&command_queue_mutex
);
3137 /* Queue is full, try again later... */
3138 mutex_unlock(&command_queue_mutex
);
3143 long tagcache_increase_serial(void)
3153 old
= current_tcmh
.serial
++;
3154 queue_command(CMD_UPDATE_MASTER_HEADER
, 0, 0, 0);
3159 void tagcache_update_numeric(int idx_id
, int tag
, long data
)
3161 queue_command(CMD_UPDATE_NUMERIC
, idx_id
, tag
, data
);
3163 #endif /* !__PCTOOL__ */
3165 long tagcache_get_serial(void)
3167 return current_tcmh
.serial
;
3170 long tagcache_get_commitid(void)
3172 return current_tcmh
.commitid
;
3175 static bool write_tag(int fd
, const char *tagstr
, const char *datastr
)
3180 snprintf(buf
, sizeof buf
, "%s=\"", tagstr
);
3181 for (i
= strlen(buf
); i
< (long)sizeof(buf
)-4; i
++)
3183 if (*datastr
== '\0')
3186 if (*datastr
== '"' || *datastr
== '\\')
3189 buf
[i
] = *(datastr
++);
3192 strcpy(&buf
[i
], "\" ");
3194 write(fd
, buf
, i
+ 2);
3201 static bool read_tag(char *dest
, long size
,
3202 const char *src
, const char *tagstr
)
3205 char current_tag
[32];
3207 while (*src
!= '\0')
3209 /* Skip all whitespace */
3217 /* Read in tag name */
3218 while (*src
!= '=' && *src
!= ' ')
3220 current_tag
[pos
] = *src
;
3224 if (*src
== '\0' || pos
>= (long)sizeof(current_tag
))
3227 current_tag
[pos
] = '\0';
3229 /* Read in tag data */
3231 /* Find the start. */
3232 while (*src
!= '"' && *src
!= '\0')
3235 if (*src
== '\0' || *(++src
) == '\0')
3238 /* Read the data. */
3239 for (pos
= 0; pos
< size
; pos
++)
3246 dest
[pos
] = *(src
+1);
3266 if (!strcasecmp(tagstr
, current_tag
))
3273 static int parse_changelog_line(int line_n
, const char *buf
, void *parameters
)
3275 struct index_entry idx
;
3276 char tag_data
[TAG_MAXLEN
+32];
3278 long masterfd
= (long)parameters
;
3279 const int import_tags
[] = { tag_playcount
, tag_rating
, tag_playtime
, tag_lastplayed
,
3287 logf("%d/%s", line_n
, buf
);
3288 if (!read_tag(tag_data
, sizeof tag_data
, buf
, "filename"))
3290 logf("filename missing");
3295 idx_id
= find_index(tag_data
);
3298 logf("entry not found");
3302 if (!get_index(masterfd
, idx_id
, &idx
, false))
3304 logf("failed to retrieve index entry");
3308 /* Stop if tag has already been modified. */
3309 if (idx
.flag
& FLAG_DIRTYNUM
)
3312 logf("import: %s", tag_data
);
3314 idx
.flag
|= FLAG_DIRTYNUM
;
3315 for (i
= 0; i
< (long)(sizeof(import_tags
)/sizeof(import_tags
[0])); i
++)
3319 if (!read_tag(tag_data
, sizeof tag_data
, buf
,
3320 tagcache_tag_to_str(import_tags
[i
])))
3325 data
= atoi(tag_data
);
3329 idx
.tag_seek
[import_tags
[i
]] = data
;
3331 if (import_tags
[i
] == tag_lastplayed
&& data
>= current_tcmh
.serial
)
3332 current_tcmh
.serial
= data
+ 1;
3333 else if (import_tags
[i
] == tag_commitid
&& data
>= current_tcmh
.commitid
)
3334 current_tcmh
.commitid
= data
+ 1;
3337 return write_index(masterfd
, idx_id
, &idx
) ? 0 : -5;
3340 bool tagcache_import_changelog(void)
3342 struct master_header myhdr
;
3343 struct tagcache_header tch
;
3354 clfd
= open(TAGCACHE_FILE_CHANGELOG
, O_RDONLY
);
3357 logf("failure to open changelog");
3361 if ( (masterfd
= open_master_fd(&myhdr
, true)) < 0)
3369 filenametag_fd
= open_tag_fd(&tch
, tag_filename
, false);
3371 fast_readline(clfd
, buf
, sizeof buf
, (long *)masterfd
,
3372 parse_changelog_line
);
3377 if (filenametag_fd
>= 0)
3378 close(filenametag_fd
);
3382 update_master_header();
3387 #endif /* !__PCTOOL__ */
3389 bool tagcache_create_changelog(struct tagcache_search
*tcs
)
3391 struct master_header myhdr
;
3392 struct index_entry idx
;
3393 char buf
[TAG_MAXLEN
+32];
3401 if (!tagcache_search(tcs
, tag_filename
))
3404 /* Initialize the changelog */
3405 clfd
= open(TAGCACHE_FILE_CHANGELOG
, O_WRONLY
| O_CREAT
| O_TRUNC
);
3408 logf("failure to open changelog");
3412 if (tcs
->masterfd
< 0)
3414 if ( (tcs
->masterfd
= open_master_fd(&myhdr
, false)) < 0)
3419 lseek(tcs
->masterfd
, 0, SEEK_SET
);
3420 ecread(tcs
->masterfd
, &myhdr
, 1, master_header_ec
, tc_stat
.econ
);
3423 write(clfd
, "## Changelog version 1\n", 23);
3425 for (i
= 0; i
< myhdr
.tch
.entry_count
; i
++)
3427 if (ecread(tcs
->masterfd
, &idx
, 1, index_entry_ec
, tc_stat
.econ
)
3428 != sizeof(struct index_entry
))
3430 logf("read error #9");
3431 tagcache_search_finish(tcs
);
3436 /* Skip until the entry found has been modified. */
3437 if (! (idx
.flag
& FLAG_DIRTYNUM
) )
3440 /* Skip deleted entries too. */
3441 if (idx
.flag
& FLAG_DELETED
)
3444 /* Now retrieve all tags. */
3445 for (j
= 0; j
< TAG_COUNT
; j
++)
3447 if (tagcache_is_numeric_tag(j
))
3449 snprintf(temp
, sizeof temp
, "%d", idx
.tag_seek
[j
]);
3450 write_tag(clfd
, tagcache_tag_to_str(j
), temp
);
3455 tagcache_retrieve(tcs
, i
, tcs
->type
, buf
, sizeof buf
);
3456 write_tag(clfd
, tagcache_tag_to_str(j
), buf
);
3459 write(clfd
, "\n", 1);
3465 tagcache_search_finish(tcs
);
3470 static bool delete_entry(long idx_id
)
3475 struct index_entry idx
, myidx
;
3476 struct master_header myhdr
;
3477 char buf
[TAG_MAXLEN
+32];
3478 int in_use
[TAG_COUNT
];
3480 logf("delete_entry(): %ld", idx_id
);
3482 #ifdef HAVE_TC_RAMCACHE
3483 /* At first mark the entry removed from ram cache. */
3484 if (tc_stat
.ramcache
)
3485 hdr
->indices
[idx_id
].flag
|= FLAG_DELETED
;
3488 if ( (masterfd
= open_master_fd(&myhdr
, true) ) < 0)
3491 lseek(masterfd
, idx_id
* sizeof(struct index_entry
), SEEK_CUR
);
3492 if (ecread(masterfd
, &myidx
, 1, index_entry_ec
, tc_stat
.econ
)
3493 != sizeof(struct index_entry
))
3495 logf("delete_entry(): read error");
3499 if (myidx
.flag
& FLAG_DELETED
)
3501 logf("delete_entry(): already deleted!");
3505 myidx
.flag
|= FLAG_DELETED
;
3506 lseek(masterfd
, -sizeof(struct index_entry
), SEEK_CUR
);
3507 if (ecwrite(masterfd
, &myidx
, 1, index_entry_ec
, tc_stat
.econ
)
3508 != sizeof(struct index_entry
))
3510 logf("delete_entry(): write_error #1");
3514 /* Now check which tags are no longer in use (if any) */
3515 for (tag
= 0; tag
< TAG_COUNT
; tag
++)
3518 lseek(masterfd
, sizeof(struct master_header
), SEEK_SET
);
3519 for (i
= 0; i
< myhdr
.tch
.entry_count
; i
++)
3521 struct index_entry
*idxp
;
3523 #ifdef HAVE_TC_RAMCACHE
3524 /* Use RAM DB if available for greater speed */
3525 if (tc_stat
.ramcache
)
3526 idxp
= &hdr
->indices
[i
];
3530 if (ecread(masterfd
, &idx
, 1, index_entry_ec
, tc_stat
.econ
)
3531 != sizeof(struct index_entry
))
3533 logf("delete_entry(): read error #2");
3539 if (idxp
->flag
& FLAG_DELETED
)
3542 for (tag
= 0; tag
< TAG_COUNT
; tag
++)
3544 if (tagcache_is_numeric_tag(tag
))
3547 if (idxp
->tag_seek
[tag
] == myidx
.tag_seek
[tag
])
3552 /* Now delete all tags no longer in use. */
3553 for (tag
= 0; tag
< TAG_COUNT
; tag
++)
3555 struct tagcache_header tch
;
3556 int oldseek
= myidx
.tag_seek
[tag
];
3558 if (tagcache_is_numeric_tag(tag
))
3562 * Replace tag seek with a hash value of the field string data.
3563 * That way runtime statistics of moved or altered files can be
3566 #ifdef HAVE_TC_RAMCACHE
3567 if (tc_stat
.ramcache
&& tag
!= tag_filename
)
3569 struct tagfile_entry
*tfe
;
3570 int32_t *seek
= &hdr
->indices
[idx_id
].tag_seek
[tag
];
3572 tfe
= (struct tagfile_entry
*)&hdr
->tags
[tag
][*seek
];
3573 *seek
= crc_32(tfe
->tag_data
, strlen(tfe
->tag_data
), 0xffffffff);
3574 myidx
.tag_seek
[tag
] = *seek
;
3579 struct tagfile_entry tfe
;
3581 /* Open the index file, which contains the tag names. */
3582 if ((fd
= open_tag_fd(&tch
, tag
, true)) < 0)
3585 /* Skip the header block */
3586 lseek(fd
, myidx
.tag_seek
[tag
], SEEK_SET
);
3587 if (ecread(fd
, &tfe
, 1, tagfile_entry_ec
, tc_stat
.econ
)
3588 != sizeof(struct tagfile_entry
))
3590 logf("delete_entry(): read error #3");
3594 if (read(fd
, buf
, tfe
.tag_length
) != tfe
.tag_length
)
3596 logf("delete_entry(): read error #4");
3600 myidx
.tag_seek
[tag
] = crc_32(buf
, strlen(buf
), 0xffffffff);
3605 logf("in use: %d/%d", tag
, in_use
[tag
]);
3614 #ifdef HAVE_TC_RAMCACHE
3615 /* Delete from ram. */
3616 if (tc_stat
.ramcache
&& tag
!= tag_filename
)
3618 struct tagfile_entry
*tagentry
= (struct tagfile_entry
*)&hdr
->tags
[tag
][oldseek
];
3619 tagentry
->tag_data
[0] = '\0';
3623 /* Open the index file, which contains the tag names. */
3626 if ((fd
= open_tag_fd(&tch
, tag
, true)) < 0)
3630 /* Skip the header block */
3631 lseek(fd
, oldseek
+ sizeof(struct tagfile_entry
), SEEK_SET
);
3633 /* Debug, print 10 first characters of the tag
3636 logf("TAG:%s", buf);
3637 lseek(fd, -10, SEEK_CUR);
3640 /* Write first data byte in tag as \0 */
3643 /* Now tag data has been removed */
3648 /* Write index entry back into master index. */
3649 lseek(masterfd
, sizeof(struct master_header
) +
3650 (idx_id
* sizeof(struct index_entry
)), SEEK_SET
);
3651 if (ecwrite(masterfd
, &myidx
, 1, index_entry_ec
, tc_stat
.econ
)
3652 != sizeof(struct index_entry
))
3654 logf("delete_entry(): write_error #2");
3673 * Returns true if there is an event waiting in the queue
3674 * that requires the current operation to be aborted.
3676 static bool check_event_queue(void)
3678 struct queue_event ev
;
3680 queue_wait_w_tmo(&tagcache_queue
, &ev
, 0);
3685 case SYS_USB_CONNECTED
:
3686 /* Put the event back into the queue. */
3687 queue_post(&tagcache_queue
, ev
.id
, ev
.data
);
3695 #ifdef HAVE_TC_RAMCACHE
3696 static bool allocate_tagcache(void)
3698 struct master_header tcmh
;
3701 /* Load the header. */
3702 if ( (fd
= open_master_fd(&tcmh
, false)) < 0)
3711 * Now calculate the required cache size plus
3712 * some extra space for alignment fixes.
3714 tc_stat
.ramcache_allocated
= tcmh
.tch
.datasize
+ 128 + TAGCACHE_RESERVE
+
3715 sizeof(struct ramcache_header
) + TAG_COUNT
*sizeof(void *);
3716 hdr
= buffer_alloc(tc_stat
.ramcache_allocated
+ 128);
3717 memset(hdr
, 0, sizeof(struct ramcache_header
));
3718 memcpy(&hdr
->h
, &tcmh
, sizeof(struct master_header
));
3719 hdr
->indices
= (struct index_entry
*)(hdr
+ 1);
3720 logf("tagcache: %d bytes allocated.", tc_stat
.ramcache_allocated
);
3725 # ifdef HAVE_EEPROM_SETTINGS
3726 static bool tagcache_dumpload(void)
3728 struct statefile_header shdr
;
3733 fd
= open(TAGCACHE_STATEFILE
, O_RDONLY
);
3736 logf("no tagcache statedump");
3740 /* Check the statefile memory placement */
3741 hdr
= buffer_alloc(0);
3742 rc
= read(fd
, &shdr
, sizeof(struct statefile_header
));
3743 if (rc
!= sizeof(struct statefile_header
)
3744 /* || (long)hdr != (long)shdr.hdr */)
3746 logf("incorrect statefile");
3752 offpos
= (long)hdr
- (long)shdr
.hdr
;
3754 /* Lets allocate real memory and load it */
3755 hdr
= buffer_alloc(shdr
.tc_stat
.ramcache_allocated
);
3756 rc
= read(fd
, hdr
, shdr
.tc_stat
.ramcache_allocated
);
3759 if (rc
!= shdr
.tc_stat
.ramcache_allocated
)
3761 logf("read failure!");
3766 memcpy(&tc_stat
, &shdr
.tc_stat
, sizeof(struct tagcache_stat
));
3768 /* Now fix the pointers */
3769 hdr
->indices
= (struct index_entry
*)((long)hdr
->indices
+ offpos
);
3770 for (i
= 0; i
< TAG_COUNT
; i
++)
3771 hdr
->tags
[i
] += offpos
;
3776 static bool tagcache_dumpsave(void)
3778 struct statefile_header shdr
;
3781 if (!tc_stat
.ramcache
)
3784 fd
= open(TAGCACHE_STATEFILE
, O_WRONLY
| O_CREAT
| O_TRUNC
);
3787 logf("failed to create a statedump");
3791 /* Create the header */
3793 memcpy(&shdr
.tc_stat
, &tc_stat
, sizeof(struct tagcache_stat
));
3794 write(fd
, &shdr
, sizeof(struct statefile_header
));
3796 /* And dump the data too */
3797 write(fd
, hdr
, tc_stat
.ramcache_allocated
);
3804 static bool load_tagcache(void)
3806 struct tagcache_header
*tch
;
3807 long bytesleft
= tc_stat
.ramcache_allocated
;
3808 struct index_entry
*idx
;
3813 # ifdef HAVE_DIRCACHE
3814 while (dircache_is_initializing())
3817 dircache_set_appflag(DIRCACHE_APPFLAG_TAGCACHE
);
3820 logf("loading tagcache to ram...");
3822 fd
= open(TAGCACHE_FILE_MASTER
, O_RDONLY
);
3825 logf("tagcache open failed");
3829 if (ecread(fd
, &hdr
->h
, 1, master_header_ec
, tc_stat
.econ
)
3830 != sizeof(struct master_header
)
3831 || hdr
->h
.tch
.magic
!= TAGCACHE_MAGIC
)
3833 logf("incorrect header");
3839 /* Load the master index table. */
3840 for (i
= 0; i
< hdr
->h
.tch
.entry_count
; i
++)
3842 rc
= ecread(fd
, idx
, 1, index_entry_ec
, tc_stat
.econ
);
3843 if (rc
!= sizeof(struct index_entry
))
3845 logf("read error #10");
3850 bytesleft
-= sizeof(struct index_entry
);
3851 if (bytesleft
< 0 || ((long)idx
- (long)hdr
->indices
) >= tc_stat
.ramcache_allocated
)
3853 logf("too big tagcache.");
3863 /* Load the tags. */
3865 for (tag
= 0; tag
< TAG_COUNT
; tag
++)
3867 struct tagfile_entry
*fe
;
3868 char buf
[TAG_MAXLEN
+32];
3870 if (tagcache_is_numeric_tag(tag
))
3873 //p = ((void *)p+1);
3874 p
= (char *)((long)p
& ~0x03) + 0x04;
3877 /* Check the header. */
3878 tch
= (struct tagcache_header
*)p
;
3879 p
+= sizeof(struct tagcache_header
);
3881 if ( (fd
= open_tag_fd(tch
, tag
, false)) < 0)
3884 for (hdr
->entry_count
[tag
] = 0;
3885 hdr
->entry_count
[tag
] < tch
->entry_count
;
3886 hdr
->entry_count
[tag
]++)
3890 if (do_timed_yield())
3892 /* Abort if we got a critical event in queue */
3893 if (check_event_queue())
3897 fe
= (struct tagfile_entry
*)p
;
3898 pos
= lseek(fd
, 0, SEEK_CUR
);
3899 rc
= ecread(fd
, fe
, 1, tagfile_entry_ec
, tc_stat
.econ
);
3900 if (rc
!= sizeof(struct tagfile_entry
))
3902 /* End of lookup table. */
3903 logf("read error #11");
3908 /* We have a special handling for the filename tags. */
3909 if (tag
== tag_filename
)
3911 # ifdef HAVE_DIRCACHE
3912 const struct dirent
*dc
;
3915 // FIXME: This is wrong!
3916 // idx = &hdr->indices[hdr->entry_count[i]];
3917 idx
= &hdr
->indices
[fe
->idx_id
];
3919 if (fe
->tag_length
>= (long)sizeof(buf
)-1)
3923 logf("TAG:%s", buf
);
3924 logf("too long filename");
3929 rc
= read(fd
, buf
, fe
->tag_length
);
3930 if (rc
!= fe
->tag_length
)
3932 logf("read error #12");
3937 /* Check if the entry has already been removed */
3938 if (idx
->flag
& FLAG_DELETED
)
3941 /* This flag must not be used yet. */
3942 if (idx
->flag
& FLAG_DIRCACHE
)
3944 logf("internal error!");
3949 if (idx
->tag_seek
[tag
] != pos
)
3951 logf("corrupt data structures!");
3956 # ifdef HAVE_DIRCACHE
3957 if (dircache_is_enabled())
3959 dc
= dircache_get_entry_ptr(buf
);
3962 logf("Entry no longer valid.");
3964 delete_entry(fe
->idx_id
);
3968 idx
->flag
|= FLAG_DIRCACHE
;
3969 FLAG_SET_ATTR(idx
->flag
, fe
->idx_id
);
3970 idx
->tag_seek
[tag_filename
] = (long)dc
;
3975 /* This will be very slow unless dircache is enabled
3976 or target is flash based, but do it anyway for
3978 /* Check if entry has been removed. */
3979 if (global_settings
.tagcache_autoupdate
)
3981 if (!file_exists(buf
))
3983 logf("Entry no longer valid.");
3985 delete_entry(fe
->idx_id
);
3994 bytesleft
-= sizeof(struct tagfile_entry
) + fe
->tag_length
;
3997 logf("too big tagcache #2");
3998 logf("tl: %d", fe
->tag_length
);
3999 logf("bl: %ld", bytesleft
);
4005 rc
= read(fd
, fe
->tag_data
, fe
->tag_length
);
4008 if (rc
!= fe
->tag_length
)
4010 logf("read error #13");
4011 logf("rc=0x%04x", rc
); // 0x431
4012 logf("len=0x%04x", fe
->tag_length
); // 0x4000
4013 logf("pos=0x%04lx", lseek(fd
, 0, SEEK_CUR
)); // 0x433
4014 logf("tag=0x%02x", tag
); // 0x00
4022 tc_stat
.ramcache_used
= tc_stat
.ramcache_allocated
- bytesleft
;
4023 logf("tagcache loaded into ram!");
4027 #endif /* HAVE_TC_RAMCACHE */
4029 static bool check_deleted_files(void)
4032 char buf
[TAG_MAXLEN
+32];
4033 struct tagfile_entry tfe
;
4035 logf("reverse scan...");
4036 snprintf(buf
, sizeof buf
, TAGCACHE_FILE_INDEX
, tag_filename
);
4037 fd
= open(buf
, O_RDONLY
);
4041 logf("%s open fail", buf
);
4045 lseek(fd
, sizeof(struct tagcache_header
), SEEK_SET
);
4046 while (ecread(fd
, &tfe
, 1, tagfile_entry_ec
, tc_stat
.econ
)
4047 == sizeof(struct tagfile_entry
)
4049 && !check_event_queue()
4053 if (tfe
.tag_length
>= (long)sizeof(buf
)-1)
4055 logf("too long tag");
4060 if (read(fd
, buf
, tfe
.tag_length
) != tfe
.tag_length
)
4062 logf("read error #14");
4067 /* Check if the file has already deleted from the db. */
4071 /* Now check if the file exists. */
4072 if (!file_exists(buf
))
4074 logf("Entry no longer valid.");
4075 logf("-> %s / %d", buf
, tfe
.tag_length
);
4076 delete_entry(tfe
.idx_id
);
4087 static bool check_dir(const char *dirname
, int add_files
)
4091 int success
= false;
4092 int ignore
, unignore
;
4093 char newpath
[MAX_PATH
];
4095 dir
= opendir(dirname
);
4098 logf("tagcache: opendir() failed");
4102 /* check for a database.ignore file */
4103 snprintf(newpath
, MAX_PATH
, "%s/database.ignore", dirname
);
4104 ignore
= file_exists(newpath
);
4105 /* check for a database.unignore file */
4106 snprintf(newpath
, MAX_PATH
, "%s/database.unignore", dirname
);
4107 unignore
= file_exists(newpath
);
4109 /* don't do anything if both ignore and unignore are there */
4110 if (ignore
!= unignore
)
4111 add_files
= unignore
;
4113 /* Recursively scan the dir. */
4117 while (!check_event_queue())
4120 struct dirent
*entry
;
4122 entry
= readdir(dir
);
4130 if (!strcmp((char *)entry
->d_name
, ".") ||
4131 !strcmp((char *)entry
->d_name
, ".."))
4136 len
= strlen(curpath
);
4137 snprintf(&curpath
[len
], sizeof(curpath
) - len
, "/%s",
4140 processed_dir_count
++;
4141 if (entry
->attribute
& ATTR_DIRECTORY
)
4142 check_dir(curpath
, add_files
);
4145 tc_stat
.curentry
= curpath
;
4147 /* Add a new entry to the temporary db file. */
4148 add_tagcache(curpath
, (entry
->wrtdate
<< 16) | entry
->wrttime
4149 #if defined(HAVE_TC_RAMCACHE) && defined(HAVE_DIRCACHE)
4150 , dir
->internal_entry
4154 /* Wait until current path for debug screen is read and unset. */
4155 while (tc_stat
.syncscreen
&& tc_stat
.curentry
!= NULL
)
4158 tc_stat
.curentry
= NULL
;
4161 curpath
[len
] = '\0';
4169 void tagcache_screensync_event(void)
4171 tc_stat
.curentry
= NULL
;
4174 void tagcache_screensync_enable(bool state
)
4176 tc_stat
.syncscreen
= state
;
4179 void tagcache_build(const char *path
)
4181 struct tagcache_header header
;
4186 total_entry_count
= 0;
4187 processed_dir_count
= 0;
4189 #ifdef HAVE_DIRCACHE
4190 while (dircache_is_initializing())
4194 logf("updating tagcache");
4196 cachefd
= open(TAGCACHE_FILE_TEMP
, O_RDONLY
);
4199 logf("skipping, cache already waiting for commit");
4204 cachefd
= open(TAGCACHE_FILE_TEMP
, O_RDWR
| O_CREAT
| O_TRUNC
);
4207 logf("master file open failed: %s", TAGCACHE_FILE_TEMP
);
4211 filenametag_fd
= open_tag_fd(&header
, tag_filename
, false);
4215 logf("Scanning files...");
4216 /* Scan for new files. */
4217 memset(&header
, 0, sizeof(struct tagcache_header
));
4218 write(cachefd
, &header
, sizeof(struct tagcache_header
));
4220 if (strcmp("/", path
) != 0)
4221 strcpy(curpath
, path
);
4222 ret
= check_dir(path
, true);
4224 /* Write the header. */
4225 header
.magic
= TAGCACHE_MAGIC
;
4226 header
.datasize
= data_size
;
4227 header
.entry_count
= total_entry_count
;
4228 lseek(cachefd
, 0, SEEK_SET
);
4229 write(cachefd
, &header
, sizeof(struct tagcache_header
));
4232 if (filenametag_fd
>= 0)
4234 close(filenametag_fd
);
4235 filenametag_fd
= -1;
4245 /* Commit changes to the database. */
4251 remove(TAGCACHE_FILE_TEMP
);
4252 logf("tagcache built!");
4258 #ifdef HAVE_TC_RAMCACHE
4261 /* Import runtime statistics if we just initialized the db. */
4262 if (hdr
->h
.serial
== 0)
4263 queue_post(&tagcache_queue
, Q_IMPORT_CHANGELOG
, 0);
4270 #ifdef HAVE_TC_RAMCACHE
4271 static void load_ramcache(void)
4278 /* At first we should load the cache (if exists). */
4279 tc_stat
.ramcache
= load_tagcache();
4281 if (!tc_stat
.ramcache
)
4283 /* If loading failed, it must indicate some problem with the db
4284 * so disable it entirely to prevent further issues. */
4285 tc_stat
.ready
= false;
4292 void tagcache_unload_ramcache(void)
4294 tc_stat
.ramcache
= false;
4295 /* Just to make sure there is no statefile present. */
4296 // remove(TAGCACHE_STATEFILE);
4301 static void tagcache_thread(void)
4303 struct queue_event ev
;
4304 bool check_done
= false;
4306 /* If the previous cache build/update was interrupted, commit
4307 * the changes first in foreground. */
4313 #ifdef HAVE_TC_RAMCACHE
4314 # ifdef HAVE_EEPROM_SETTINGS
4315 if (firmware_settings
.initialized
&& firmware_settings
.disk_clean
)
4316 check_done
= tagcache_dumpload();
4318 remove(TAGCACHE_STATEFILE
);
4321 /* Allocate space for the tagcache if found on disk. */
4322 if (global_settings
.tagcache_ram
&& !tc_stat
.ramcache
)
4323 allocate_tagcache();
4327 tc_stat
.initialized
= true;
4329 /* Don't delay bootup with the header check but do it on background. */
4331 tc_stat
.ready
= check_all_headers();
4332 tc_stat
.readyvalid
= true;
4336 run_command_queue(false);
4338 queue_wait_w_tmo(&tagcache_queue
, &ev
, HZ
);
4342 case Q_IMPORT_CHANGELOG
:
4343 tagcache_import_changelog();
4348 remove(TAGCACHE_FILE_TEMP
);
4349 tagcache_build("/");
4353 tagcache_build("/");
4354 #ifdef HAVE_TC_RAMCACHE
4357 check_deleted_files();
4363 if (check_done
|| !tc_stat
.ready
)
4366 #ifdef HAVE_TC_RAMCACHE
4367 if (!tc_stat
.ramcache
&& global_settings
.tagcache_ram
)
4370 if (tc_stat
.ramcache
&& global_settings
.tagcache_autoupdate
)
4371 tagcache_build("/");
4375 if (global_settings
.tagcache_autoupdate
)
4377 tagcache_build("/");
4379 /* This will be very slow unless dircache is enabled
4380 or target is flash based, but do it anyway for
4382 check_deleted_files();
4385 logf("tagcache check done");
4397 case SYS_USB_CONNECTED
:
4398 logf("USB: TagCache");
4399 usb_acknowledge(SYS_USB_CONNECTED_ACK
);
4400 usb_wait_for_disconnect(&tagcache_queue
);
4407 bool tagcache_prepare_shutdown(void)
4409 if (tagcache_get_commit_step() > 0)
4412 tagcache_stop_scan();
4413 while (read_lock
|| write_lock
)
4419 void tagcache_shutdown(void)
4421 /* Flush the command queue. */
4422 run_command_queue(true);
4424 #ifdef HAVE_EEPROM_SETTINGS
4425 if (tc_stat
.ramcache
)
4426 tagcache_dumpsave();
4430 static int get_progress(void)
4432 int total_count
= -1;
4434 #ifdef HAVE_DIRCACHE
4435 if (dircache_is_enabled())
4437 total_count
= dircache_get_entry_count();
4441 #ifdef HAVE_TC_RAMCACHE
4443 if (hdr
&& tc_stat
.ramcache
)
4444 total_count
= hdr
->h
.tch
.entry_count
;
4448 if (total_count
< 0)
4451 return processed_dir_count
* 100 / total_count
;
4454 struct tagcache_stat
* tagcache_get_stat(void)
4456 tc_stat
.progress
= get_progress();
4457 tc_stat
.processed_entries
= processed_dir_count
;
4462 void tagcache_start_scan(void)
4464 queue_post(&tagcache_queue
, Q_START_SCAN
, 0);
4467 bool tagcache_update(void)
4472 queue_post(&tagcache_queue
, Q_UPDATE
, 0);
4476 bool tagcache_rebuild()
4478 queue_post(&tagcache_queue
, Q_REBUILD
, 0);
4482 void tagcache_stop_scan(void)
4484 queue_post(&tagcache_queue
, Q_STOP_SCAN
, 0);
4487 #ifdef HAVE_TC_RAMCACHE
4488 bool tagcache_is_ramcache(void)
4490 return tc_stat
.ramcache
;
4494 #endif /* !__PCTOOL__ */
4497 void tagcache_init(void)
4499 memset(&tc_stat
, 0, sizeof(struct tagcache_stat
));
4500 memset(¤t_tcmh
, 0, sizeof(struct master_header
));
4501 filenametag_fd
= -1;
4502 write_lock
= read_lock
= 0;
4505 mutex_init(&command_queue_mutex
);
4506 queue_init(&tagcache_queue
, true);
4507 create_thread(tagcache_thread
, tagcache_stack
,
4508 sizeof(tagcache_stack
), 0, tagcache_thread_name
4509 IF_PRIO(, PRIORITY_BACKGROUND
)
4512 tc_stat
.initialized
= true;
4516 tc_stat
.ready
= check_all_headers();
4521 void tagcache_reverse_scan(void)
4523 logf("Checking for deleted files");
4524 check_deleted_files();
4528 bool tagcache_is_initialized(void)
4530 return tc_stat
.initialized
;
4532 bool tagcache_is_usable(void)
4534 return tc_stat
.initialized
&& tc_stat
.ready
;
4536 int tagcache_get_commit_step(void)
4538 return tc_stat
.commit_step
;
4540 int tagcache_get_max_commit_step(void)
4542 return (int)(sizeof(sorted_tags
)/sizeof(sorted_tags
[0]))+1;