Miika Pekkarinen | 7c4e0c8 | 2006-03-26 11:33:42 +0000 | [diff] [blame] | 1 | /*************************************************************************** |
| 2 | * __________ __ ___. |
| 3 | * Open \______ \ ____ ____ | | _\_ |__ _______ ___ |
| 4 | * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / |
| 5 | * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < |
| 6 | * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ |
| 7 | * \/ \/ \/ \/ \/ |
| 8 | * $Id$ |
| 9 | * |
| 10 | * Copyright (C) 2005 by Miika Pekkarinen |
| 11 | * |
Daniel Stenberg | 2acc0ac | 2008-06-28 18:10:04 +0000 | [diff] [blame^] | 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. |
Miika Pekkarinen | 7c4e0c8 | 2006-03-26 11:33:42 +0000 | [diff] [blame] | 16 | * |
| 17 | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY |
| 18 | * KIND, either express or implied. |
| 19 | * |
| 20 | ****************************************************************************/ |
| 21 | |
Miika Pekkarinen | 7136fd9 | 2006-07-25 10:44:19 +0000 | [diff] [blame] | 22 | /* |
| 23 | * TagCache API |
| 24 | * |
| 25 | * ----------x---------x------------------x----- |
| 26 | * | | | External |
| 27 | * +---------------x-------+ | TagCache | Libraries |
| 28 | * | Modification routines | | Core | |
| 29 | * +-x---------x-----------+ | | |
| 30 | * | (R/W) | | | | |
| 31 | * | +------x-------------x-+ +-------------x-----+ | |
| 32 | * | | x==x Filters & clauses | | |
| 33 | * | | Search routines | +-------------------+ | |
| 34 | * | | x============================x DirCache |
| 35 | * | +-x--------------------+ | (optional) |
| 36 | * | | (R) | |
| 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 | * | | | | +---------------------+ | |
Miika Pekkarinen | d8ac607 | 2006-08-02 17:39:34 +0000 | [diff] [blame] | 48 | * +-x----x-------x--+ | |
Miika Pekkarinen | 7136fd9 | 2006-07-25 10:44:19 +0000 | [diff] [blame] | 49 | * | TagCache RAM DB x==\(W) +-----------------+ | |
Miika Pekkarinen | d8ac607 | 2006-08-02 17:39:34 +0000 | [diff] [blame] | 50 | * +-----------------+ \===x | | |
Miika Pekkarinen | 7136fd9 | 2006-07-25 10:44:19 +0000 | [diff] [blame] | 51 | * | | | | (R) | Ram DB Loader x============x DirCache |
| 52 | * +-x----x---x---x---+ /==x | | (optional) |
| 53 | * | Tagcache Disk DB x==/ +-----------------+ | |
| 54 | * +------------------+ | |
| 55 | * |
| 56 | */ |
| 57 | |
Miika Pekkarinen | 7c4e0c8 | 2006-03-26 11:33:42 +0000 | [diff] [blame] | 58 | #include <stdio.h> |
Dave Chapman | 1feb8bd | 2007-05-07 13:32:56 +0000 | [diff] [blame] | 59 | #include <stdlib.h> |
| 60 | #include <ctype.h> |
Miika Pekkarinen | a85044b | 2006-09-16 16:18:11 +0000 | [diff] [blame] | 61 | #include "config.h" |
Miika Pekkarinen | 9eec03f | 2008-02-17 18:35:27 +0000 | [diff] [blame] | 62 | #include "ata_idle_notify.h" |
Miika Pekkarinen | 7c4e0c8 | 2006-03-26 11:33:42 +0000 | [diff] [blame] | 63 | #include "thread.h" |
| 64 | #include "kernel.h" |
| 65 | #include "system.h" |
| 66 | #include "logf.h" |
| 67 | #include "string.h" |
| 68 | #include "usb.h" |
Miika Pekkarinen | 7c4e0c8 | 2006-03-26 11:33:42 +0000 | [diff] [blame] | 69 | #include "metadata.h" |
| 70 | #include "id3.h" |
Miika Pekkarinen | 7c4e0c8 | 2006-03-26 11:33:42 +0000 | [diff] [blame] | 71 | #include "tagcache.h" |
Miika Pekkarinen | a93e591 | 2006-03-26 12:49:19 +0000 | [diff] [blame] | 72 | #include "buffer.h" |
Miika Pekkarinen | fa893c6 | 2006-04-18 18:56:56 +0000 | [diff] [blame] | 73 | #include "crc32.h" |
Miika Pekkarinen | a1ac743 | 2006-10-21 20:37:33 +0000 | [diff] [blame] | 74 | #include "misc.h" |
Miika Pekkarinen | 0dd7ea2 | 2006-11-10 08:03:33 +0000 | [diff] [blame] | 75 | #include "settings.h" |
Kevin Ferrare | 011a325 | 2007-07-20 17:06:55 +0000 | [diff] [blame] | 76 | #include "dir.h" |
Miika Pekkarinen | 9b9539c | 2007-02-13 21:51:18 +0000 | [diff] [blame] | 77 | #include "structec.h" |
Bertrik Sikken | e15f8a2 | 2008-05-03 08:35:14 +0000 | [diff] [blame] | 78 | #include "tagcache.h" |
| 79 | |
Miika Pekkarinen | 0dd7ea2 | 2006-11-10 08:03:33 +0000 | [diff] [blame] | 80 | #ifndef __PCTOOL__ |
Miika Pekkarinen | 0dd7ea2 | 2006-11-10 08:03:33 +0000 | [diff] [blame] | 81 | #include "splash.h" |
| 82 | #include "lang.h" |
| 83 | #include "eeprom_settings.h" |
| 84 | #endif |
Miika Pekkarinen | 7c4e0c8 | 2006-03-26 11:33:42 +0000 | [diff] [blame] | 85 | |
Miika Pekkarinen | 0dd7ea2 | 2006-11-10 08:03:33 +0000 | [diff] [blame] | 86 | #ifdef __PCTOOL__ |
Miika Pekkarinen | 0dd7ea2 | 2006-11-10 08:03:33 +0000 | [diff] [blame] | 87 | #define yield() do { } while(0) |
| 88 | #define sim_sleep(timeout) do { } while(0) |
Steve Bavin | 6d7f68c | 2007-03-10 14:34:56 +0000 | [diff] [blame] | 89 | #define do_timed_yield() do { } while(0) |
Miika Pekkarinen | 0dd7ea2 | 2006-11-10 08:03:33 +0000 | [diff] [blame] | 90 | #endif |
| 91 | |
Miika Pekkarinen | 0dd7ea2 | 2006-11-10 08:03:33 +0000 | [diff] [blame] | 92 | #ifndef __PCTOOL__ |
Miika Pekkarinen | 7c4e0c8 | 2006-03-26 11:33:42 +0000 | [diff] [blame] | 93 | /* Tag Cache thread. */ |
Michael Sevakis | 84f5c5c | 2007-10-16 22:00:51 +0000 | [diff] [blame] | 94 | static struct event_queue tagcache_queue; |
Miika Pekkarinen | 7c4e0c8 | 2006-03-26 11:33:42 +0000 | [diff] [blame] | 95 | static long tagcache_stack[(DEFAULT_STACK_SIZE + 0x4000)/sizeof(long)]; |
| 96 | static const char tagcache_thread_name[] = "tagcache"; |
Miika Pekkarinen | 0dd7ea2 | 2006-11-10 08:03:33 +0000 | [diff] [blame] | 97 | #endif |
Miika Pekkarinen | 7c4e0c8 | 2006-03-26 11:33:42 +0000 | [diff] [blame] | 98 | |
Robert Kukla | 28530bc | 2007-03-04 17:30:12 +0000 | [diff] [blame] | 99 | #define UNTAGGED "<Untagged>" |
| 100 | |
Miika Pekkarinen | 7c4e0c8 | 2006-03-26 11:33:42 +0000 | [diff] [blame] | 101 | /* Previous path when scanning directory tree recursively. */ |
Miika Pekkarinen | 0dd7ea2 | 2006-11-10 08:03:33 +0000 | [diff] [blame] | 102 | static char curpath[TAG_MAXLEN+32]; |
Miika Pekkarinen | 7c4e0c8 | 2006-03-26 11:33:42 +0000 | [diff] [blame] | 103 | static long curpath_size = sizeof(curpath); |
| 104 | |
| 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; |
| 111 | |
Miika Pekkarinen | ea07cd5 | 2006-03-27 07:44:32 +0000 | [diff] [blame] | 112 | /* Tags we want to get sorted (loaded to the tempbuf). */ |
Miika Pekkarinen | 4e97664 | 2007-02-10 12:09:28 +0000 | [diff] [blame] | 113 | static const int sorted_tags[] = { tag_artist, tag_album, tag_genre, |
Dan Everton | eecfe9f | 2007-08-08 10:19:56 +0000 | [diff] [blame] | 114 | tag_composer, tag_comment, tag_albumartist, tag_grouping, tag_title }; |
Miika Pekkarinen | 58fe4de | 2006-04-10 10:26:24 +0000 | [diff] [blame] | 115 | |
| 116 | /* Uniqued tags (we can use these tags with filters and conditional clauses). */ |
Miika Pekkarinen | 4e97664 | 2007-02-10 12:09:28 +0000 | [diff] [blame] | 117 | static const int unique_tags[] = { tag_artist, tag_album, tag_genre, |
Dan Everton | eecfe9f | 2007-08-08 10:19:56 +0000 | [diff] [blame] | 118 | tag_composer, tag_comment, tag_albumartist, tag_grouping }; |
Miika Pekkarinen | 58fe4de | 2006-04-10 10:26:24 +0000 | [diff] [blame] | 119 | |
| 120 | /* Numeric tags (we can use these tags with conditional clauses). */ |
Miika Pekkarinen | f603946 | 2007-12-16 21:10:26 +0000 | [diff] [blame] | 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, |
Miika Pekkarinen | 9d9937a | 2007-04-12 20:14:05 +0000 | [diff] [blame] | 124 | tag_virt_length_min, tag_virt_length_sec, |
Miika Pekkarinen | 5e47daa | 2007-04-12 20:21:56 +0000 | [diff] [blame] | 125 | tag_virt_playtime_min, tag_virt_playtime_sec, |
Miika Pekkarinen | f5184f3 | 2007-02-25 20:41:51 +0000 | [diff] [blame] | 126 | tag_virt_entryage, tag_virt_autoscore }; |
Miika Pekkarinen | 7c4e0c8 | 2006-03-26 11:33:42 +0000 | [diff] [blame] | 127 | |
Miika Pekkarinen | 4e97664 | 2007-02-10 12:09:28 +0000 | [diff] [blame] | 128 | /* String presentation of the tags defined in tagcache.h. Must be in correct order! */ |
Miika Pekkarinen | cb8c795 | 2006-07-20 12:19:31 +0000 | [diff] [blame] | 129 | static const char *tags_str[] = { "artist", "album", "genre", "title", |
Miika Pekkarinen | f603946 | 2007-12-16 21:10:26 +0000 | [diff] [blame] | 130 | "filename", "composer", "comment", "albumartist", "grouping", "year", |
| 131 | "discnumber", "tracknumber", "bitrate", "length", "playcount", "rating", |
| 132 | "playtime", "lastplayed", "commitid", "mtime" }; |
Miika Pekkarinen | cb8c795 | 2006-07-20 12:19:31 +0000 | [diff] [blame] | 133 | |
Miika Pekkarinen | 4142710 | 2006-04-23 18:47:26 +0000 | [diff] [blame] | 134 | /* Status information of the tagcache. */ |
Miika Pekkarinen | 0dd7ea2 | 2006-11-10 08:03:33 +0000 | [diff] [blame] | 135 | static struct tagcache_stat tc_stat; |
Miika Pekkarinen | 1b18dd0 | 2006-04-12 10:31:24 +0000 | [diff] [blame] | 136 | |
Miika Pekkarinen | 7c4e0c8 | 2006-03-26 11:33:42 +0000 | [diff] [blame] | 137 | /* Queue commands. */ |
Miika Pekkarinen | fa893c6 | 2006-04-18 18:56:56 +0000 | [diff] [blame] | 138 | enum tagcache_queue { |
| 139 | Q_STOP_SCAN = 0, |
| 140 | Q_START_SCAN, |
Miika Pekkarinen | 86f07c7 | 2006-07-23 15:35:53 +0000 | [diff] [blame] | 141 | Q_IMPORT_CHANGELOG, |
Miika Pekkarinen | 9cd5c3e | 2006-07-10 16:22:03 +0000 | [diff] [blame] | 142 | Q_UPDATE, |
| 143 | Q_REBUILD, |
Miika Pekkarinen | 9d756e2 | 2007-07-21 17:35:19 +0000 | [diff] [blame] | 144 | |
| 145 | /* Internal tagcache command queue. */ |
| 146 | CMD_UPDATE_MASTER_HEADER, |
| 147 | CMD_UPDATE_NUMERIC, |
Miika Pekkarinen | fa893c6 | 2006-04-18 18:56:56 +0000 | [diff] [blame] | 148 | }; |
Miika Pekkarinen | 7c4e0c8 | 2006-03-26 11:33:42 +0000 | [diff] [blame] | 149 | |
Miika Pekkarinen | 9d756e2 | 2007-07-21 17:35:19 +0000 | [diff] [blame] | 150 | struct tagcache_command_entry { |
Jonathan Gordon | 7c0a8e1 | 2008-04-24 07:54:32 +0000 | [diff] [blame] | 151 | int32_t command; |
| 152 | int32_t idx_id; |
| 153 | int32_t tag; |
| 154 | int32_t data; |
Miika Pekkarinen | 9d756e2 | 2007-07-21 17:35:19 +0000 | [diff] [blame] | 155 | }; |
| 156 | |
| 157 | static struct tagcache_command_entry command_queue[TAGCACHE_COMMAND_QUEUE_LENGTH]; |
| 158 | static volatile int command_queue_widx = 0; |
| 159 | static volatile int command_queue_ridx = 0; |
Michael Sevakis | 84f5c5c | 2007-10-16 22:00:51 +0000 | [diff] [blame] | 160 | static struct mutex command_queue_mutex; |
Miika Pekkarinen | 3b31346 | 2006-04-16 17:32:54 +0000 | [diff] [blame] | 161 | |
Miika Pekkarinen | 7c4e0c8 | 2006-03-26 11:33:42 +0000 | [diff] [blame] | 162 | /* Tag database structures. */ |
Miika Pekkarinen | 7c4e0c8 | 2006-03-26 11:33:42 +0000 | [diff] [blame] | 163 | |
| 164 | /* Variable-length tag entry in tag files. */ |
| 165 | struct tagfile_entry { |
Miika Pekkarinen | cb8c795 | 2006-07-20 12:19:31 +0000 | [diff] [blame] | 166 | short tag_length; /* Length of the data in bytes including '\0' */ |
| 167 | short idx_id; /* Corresponding entry location in index file of not unique tags */ |
| 168 | char tag_data[0]; /* Begin of the tag data */ |
Miika Pekkarinen | 7c4e0c8 | 2006-03-26 11:33:42 +0000 | [diff] [blame] | 169 | }; |
| 170 | |
| 171 | /* Fixed-size tag entry in master db index. */ |
| 172 | struct index_entry { |
Jonathan Gordon | 7c0a8e1 | 2008-04-24 07:54:32 +0000 | [diff] [blame] | 173 | int32_t tag_seek[TAG_COUNT]; /* Location of tag data or numeric tag data */ |
| 174 | int32_t flag; /* Status flags */ |
Miika Pekkarinen | 7c4e0c8 | 2006-03-26 11:33:42 +0000 | [diff] [blame] | 175 | }; |
| 176 | |
| 177 | /* Header is the same in every file. */ |
| 178 | struct tagcache_header { |
Jonathan Gordon | 7c0a8e1 | 2008-04-24 07:54:32 +0000 | [diff] [blame] | 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 */ |
Miika Pekkarinen | 7c4e0c8 | 2006-03-26 11:33:42 +0000 | [diff] [blame] | 182 | }; |
| 183 | |
Miika Pekkarinen | cb8c795 | 2006-07-20 12:19:31 +0000 | [diff] [blame] | 184 | struct master_header { |
| 185 | struct tagcache_header tch; |
Jonathan Gordon | 7c0a8e1 | 2008-04-24 07:54:32 +0000 | [diff] [blame] | 186 | int32_t serial; /* Increasing counting number */ |
| 187 | int32_t commitid; /* Number of commits so far */ |
| 188 | int32_t dirty; |
Miika Pekkarinen | cb8c795 | 2006-07-20 12:19:31 +0000 | [diff] [blame] | 189 | }; |
| 190 | |
Miika Pekkarinen | 9b9539c | 2007-02-13 21:51:18 +0000 | [diff] [blame] | 191 | /* For the endianess correction */ |
| 192 | static const char *tagfile_entry_ec = "ss"; |
Miika Pekkarinen | f603946 | 2007-12-16 21:10:26 +0000 | [diff] [blame] | 193 | static const char *index_entry_ec = "lllllllllllllllllllll"; /* (1 + TAG_COUNT) * l */ |
Miika Pekkarinen | 9b9539c | 2007-02-13 21:51:18 +0000 | [diff] [blame] | 194 | static const char *tagcache_header_ec = "lll"; |
Miika Pekkarinen | f5184f3 | 2007-02-25 20:41:51 +0000 | [diff] [blame] | 195 | static const char *master_header_ec = "llllll"; |
Miika Pekkarinen | 9b9539c | 2007-02-13 21:51:18 +0000 | [diff] [blame] | 196 | |
Miika Pekkarinen | f5184f3 | 2007-02-25 20:41:51 +0000 | [diff] [blame] | 197 | static struct master_header current_tcmh; |
Miika Pekkarinen | cb8c795 | 2006-07-20 12:19:31 +0000 | [diff] [blame] | 198 | |
Miika Pekkarinen | 7c4e0c8 | 2006-03-26 11:33:42 +0000 | [diff] [blame] | 199 | #ifdef HAVE_TC_RAMCACHE |
| 200 | /* Header is created when loading database to ram. */ |
| 201 | struct ramcache_header { |
Miika Pekkarinen | cb8c795 | 2006-07-20 12:19:31 +0000 | [diff] [blame] | 202 | struct master_header h; /* Header from the master index */ |
| 203 | struct index_entry *indices; /* Master index file content */ |
| 204 | char *tags[TAG_COUNT]; /* Tag file content (not including filename tag) */ |
| 205 | int entry_count[TAG_COUNT]; /* Number of entries in the indices. */ |
Miika Pekkarinen | 7c4e0c8 | 2006-03-26 11:33:42 +0000 | [diff] [blame] | 206 | }; |
| 207 | |
Peter D'Hoye | c4a59a2 | 2006-08-15 22:54:06 +0000 | [diff] [blame] | 208 | # ifdef HAVE_EEPROM_SETTINGS |
Miika Pekkarinen | 954b732 | 2006-08-05 20:19:10 +0000 | [diff] [blame] | 209 | struct statefile_header { |
| 210 | struct ramcache_header *hdr; |
Miika Pekkarinen | 0dd7ea2 | 2006-11-10 08:03:33 +0000 | [diff] [blame] | 211 | struct tagcache_stat tc_stat; |
Miika Pekkarinen | 954b732 | 2006-08-05 20:19:10 +0000 | [diff] [blame] | 212 | }; |
| 213 | # endif |
| 214 | |
Miika Pekkarinen | cb8c795 | 2006-07-20 12:19:31 +0000 | [diff] [blame] | 215 | /* Pointer to allocated ramcache_header */ |
Miika Pekkarinen | 7c4e0c8 | 2006-03-26 11:33:42 +0000 | [diff] [blame] | 216 | static struct ramcache_header *hdr; |
Miika Pekkarinen | 7c4e0c8 | 2006-03-26 11:33:42 +0000 | [diff] [blame] | 217 | #endif |
| 218 | |
| 219 | /** |
| 220 | * Full tag entries stored in a temporary file waiting |
| 221 | * for commit to the cache. */ |
| 222 | struct temp_file_entry { |
| 223 | long tag_offset[TAG_COUNT]; |
| 224 | short tag_length[TAG_COUNT]; |
Miika Pekkarinen | 812cbad | 2006-10-04 09:05:01 +0000 | [diff] [blame] | 225 | long flag; |
| 226 | |
Miika Pekkarinen | 7c4e0c8 | 2006-03-26 11:33:42 +0000 | [diff] [blame] | 227 | long data_length; |
| 228 | }; |
| 229 | |
Miika Pekkarinen | 4142710 | 2006-04-23 18:47:26 +0000 | [diff] [blame] | 230 | struct tempbuf_id_list { |
| 231 | long id; |
| 232 | struct tempbuf_id_list *next; |
Miika Pekkarinen | 7c4e0c8 | 2006-03-26 11:33:42 +0000 | [diff] [blame] | 233 | }; |
| 234 | |
| 235 | struct tempbuf_searchidx { |
Miika Pekkarinen | 44b76bc | 2006-03-30 12:07:32 +0000 | [diff] [blame] | 236 | long idx_id; |
Miika Pekkarinen | 7c4e0c8 | 2006-03-26 11:33:42 +0000 | [diff] [blame] | 237 | char *str; |
| 238 | int seek; |
Miika Pekkarinen | 4142710 | 2006-04-23 18:47:26 +0000 | [diff] [blame] | 239 | struct tempbuf_id_list idlist; |
Miika Pekkarinen | 7c4e0c8 | 2006-03-26 11:33:42 +0000 | [diff] [blame] | 240 | }; |
| 241 | |
Miika Pekkarinen | 36e3081 | 2006-09-26 11:23:18 +0000 | [diff] [blame] | 242 | /* Lookup buffer for fixing messed up index while after sorting. */ |
Miika Pekkarinen | a47bd73 | 2006-07-22 07:09:57 +0000 | [diff] [blame] | 243 | static long commit_entry_count; |
Miika Pekkarinen | 36e3081 | 2006-09-26 11:23:18 +0000 | [diff] [blame] | 244 | static long lookup_buffer_depth; |
Jens Arnold | 2597a13 | 2006-12-25 14:01:47 +0000 | [diff] [blame] | 245 | static struct tempbuf_searchidx **lookup; |
Miika Pekkarinen | 7c4e0c8 | 2006-03-26 11:33:42 +0000 | [diff] [blame] | 246 | |
| 247 | /* Used when building the temporary file. */ |
| 248 | static int cachefd = -1, filenametag_fd; |
| 249 | static int total_entry_count = 0; |
| 250 | static int data_size = 0; |
| 251 | static int processed_dir_count; |
| 252 | |
Miika Pekkarinen | d8ac607 | 2006-08-02 17:39:34 +0000 | [diff] [blame] | 253 | /* Thread safe locking */ |
| 254 | static volatile int write_lock; |
| 255 | static volatile int read_lock; |
| 256 | |
Miika Pekkarinen | f603946 | 2007-12-16 21:10:26 +0000 | [diff] [blame] | 257 | static bool delete_entry(long idx_id); |
| 258 | |
Miika Pekkarinen | cb8c795 | 2006-07-20 12:19:31 +0000 | [diff] [blame] | 259 | const char* tagcache_tag_to_str(int tag) |
| 260 | { |
| 261 | return tags_str[tag]; |
| 262 | } |
| 263 | |
Miika Pekkarinen | 4c6fd0f | 2006-04-03 18:57:34 +0000 | [diff] [blame] | 264 | bool tagcache_is_numeric_tag(int type) |
Miika Pekkarinen | 44b76bc | 2006-03-30 12:07:32 +0000 | [diff] [blame] | 265 | { |
| 266 | int i; |
| 267 | |
| 268 | for (i = 0; i < (int)(sizeof(numeric_tags)/sizeof(numeric_tags[0])); i++) |
| 269 | { |
| 270 | if (type == numeric_tags[i]) |
| 271 | return true; |
| 272 | } |
| 273 | |
| 274 | return false; |
| 275 | } |
| 276 | |
Miika Pekkarinen | 58fe4de | 2006-04-10 10:26:24 +0000 | [diff] [blame] | 277 | bool tagcache_is_unique_tag(int type) |
| 278 | { |
| 279 | int i; |
| 280 | |
| 281 | for (i = 0; i < (int)(sizeof(unique_tags)/sizeof(unique_tags[0])); i++) |
| 282 | { |
| 283 | if (type == unique_tags[i]) |
| 284 | return true; |
| 285 | } |
| 286 | |
| 287 | return false; |
| 288 | } |
| 289 | |
| 290 | bool tagcache_is_sorted_tag(int type) |
| 291 | { |
| 292 | int i; |
| 293 | |
| 294 | for (i = 0; i < (int)(sizeof(sorted_tags)/sizeof(sorted_tags[0])); i++) |
| 295 | { |
| 296 | if (type == sorted_tags[i]) |
| 297 | return true; |
| 298 | } |
| 299 | |
| 300 | return false; |
| 301 | } |
| 302 | |
Miika Pekkarinen | 784112a | 2008-01-13 19:34:49 +0000 | [diff] [blame] | 303 | #ifdef HAVE_DIRCACHE |
Miika Pekkarinen | 39c597b | 2008-01-13 19:13:37 +0000 | [diff] [blame] | 304 | /** |
| 305 | * Returns true if specified flag is still present, i.e., dircache |
| 306 | * has not been reloaded. |
| 307 | */ |
| 308 | static bool is_dircache_intact(void) |
| 309 | { |
| 310 | return dircache_get_appflag(DIRCACHE_APPFLAG_TAGCACHE); |
| 311 | } |
Miika Pekkarinen | 784112a | 2008-01-13 19:34:49 +0000 | [diff] [blame] | 312 | #endif |
Miika Pekkarinen | 39c597b | 2008-01-13 19:13:37 +0000 | [diff] [blame] | 313 | |
Miika Pekkarinen | 29fa15f | 2006-07-23 11:15:28 +0000 | [diff] [blame] | 314 | static int open_tag_fd(struct tagcache_header *hdr, int tag, bool write) |
| 315 | { |
| 316 | int fd; |
| 317 | char buf[MAX_PATH]; |
Miika Pekkarinen | 9161425 | 2006-07-23 14:28:12 +0000 | [diff] [blame] | 318 | int rc; |
Miika Pekkarinen | 29fa15f | 2006-07-23 11:15:28 +0000 | [diff] [blame] | 319 | |
| 320 | if (tagcache_is_numeric_tag(tag) || tag < 0 || tag >= TAG_COUNT) |
| 321 | return -1; |
| 322 | |
| 323 | snprintf(buf, sizeof buf, TAGCACHE_FILE_INDEX, tag); |
| 324 | |
| 325 | fd = open(buf, write ? O_RDWR : O_RDONLY); |
| 326 | if (fd < 0) |
| 327 | { |
Miika Pekkarinen | 0dd7ea2 | 2006-11-10 08:03:33 +0000 | [diff] [blame] | 328 | logf("tag file open failed: tag=%d write=%d file=%s", tag, write, buf); |
| 329 | tc_stat.ready = false; |
Miika Pekkarinen | 29fa15f | 2006-07-23 11:15:28 +0000 | [diff] [blame] | 330 | return fd; |
| 331 | } |
| 332 | |
| 333 | /* Check the header. */ |
Miika Pekkarinen | 9b9539c | 2007-02-13 21:51:18 +0000 | [diff] [blame] | 334 | rc = ecread(fd, hdr, 1, tagcache_header_ec, tc_stat.econ); |
Miika Pekkarinen | 9161425 | 2006-07-23 14:28:12 +0000 | [diff] [blame] | 335 | if (hdr->magic != TAGCACHE_MAGIC || rc != sizeof(struct tagcache_header)) |
Miika Pekkarinen | 29fa15f | 2006-07-23 11:15:28 +0000 | [diff] [blame] | 336 | { |
| 337 | logf("header error"); |
Miika Pekkarinen | 0dd7ea2 | 2006-11-10 08:03:33 +0000 | [diff] [blame] | 338 | tc_stat.ready = false; |
Miika Pekkarinen | 29fa15f | 2006-07-23 11:15:28 +0000 | [diff] [blame] | 339 | close(fd); |
| 340 | return -2; |
| 341 | } |
Miika Pekkarinen | 36e3081 | 2006-09-26 11:23:18 +0000 | [diff] [blame] | 342 | |
Miika Pekkarinen | 29fa15f | 2006-07-23 11:15:28 +0000 | [diff] [blame] | 343 | return fd; |
| 344 | } |
| 345 | |
Miika Pekkarinen | f603946 | 2007-12-16 21:10:26 +0000 | [diff] [blame] | 346 | static int open_master_fd(struct master_header *hdr, bool write) |
| 347 | { |
| 348 | int fd; |
| 349 | int rc; |
| 350 | |
| 351 | fd = open(TAGCACHE_FILE_MASTER, write ? O_RDWR : O_RDONLY); |
| 352 | if (fd < 0) |
| 353 | { |
| 354 | logf("master file open failed for R/W"); |
| 355 | tc_stat.ready = false; |
| 356 | return fd; |
| 357 | } |
| 358 | |
| 359 | tc_stat.econ = false; |
| 360 | |
| 361 | /* Check the header. */ |
| 362 | rc = read(fd, hdr, sizeof(struct master_header)); |
| 363 | if (hdr->tch.magic == TAGCACHE_MAGIC && rc == sizeof(struct master_header)) |
| 364 | { |
| 365 | /* Success. */ |
| 366 | return fd; |
| 367 | } |
| 368 | |
| 369 | /* Trying to read again, this time with endianess correction enabled. */ |
| 370 | lseek(fd, 0, SEEK_SET); |
| 371 | |
| 372 | rc = ecread(fd, hdr, 1, master_header_ec, true); |
| 373 | if (hdr->tch.magic != TAGCACHE_MAGIC || rc != sizeof(struct master_header)) |
| 374 | { |
| 375 | logf("header error"); |
| 376 | tc_stat.ready = false; |
| 377 | close(fd); |
| 378 | return -2; |
| 379 | } |
| 380 | |
| 381 | tc_stat.econ = true; |
| 382 | |
| 383 | return fd; |
| 384 | } |
| 385 | |
Steve Bavin | 6d7f68c | 2007-03-10 14:34:56 +0000 | [diff] [blame] | 386 | #ifndef __PCTOOL__ |
| 387 | static bool do_timed_yield(void) |
| 388 | { |
| 389 | /* Sorting can lock up for quite a while, so yield occasionally */ |
| 390 | static long wakeup_tick = 0; |
| 391 | if (current_tick >= wakeup_tick) |
| 392 | { |
| 393 | wakeup_tick = current_tick + (HZ/4); |
| 394 | yield(); |
| 395 | return true; |
| 396 | } |
| 397 | return false; |
| 398 | } |
| 399 | #endif |
| 400 | |
Miika Pekkarinen | 9cd5c3e | 2006-07-10 16:22:03 +0000 | [diff] [blame] | 401 | #if defined(HAVE_TC_RAMCACHE) && defined(HAVE_DIRCACHE) |
Miika Pekkarinen | 45dfe2a | 2006-07-15 17:36:25 +0000 | [diff] [blame] | 402 | static long find_entry_ram(const char *filename, |
Kevin Ferrare | 011a325 | 2007-07-20 17:06:55 +0000 | [diff] [blame] | 403 | const struct dirent *dc) |
Miika Pekkarinen | 7c4e0c8 | 2006-03-26 11:33:42 +0000 | [diff] [blame] | 404 | { |
| 405 | static long last_pos = 0; |
Miika Pekkarinen | 7c4e0c8 | 2006-03-26 11:33:42 +0000 | [diff] [blame] | 406 | int i; |
| 407 | |
| 408 | /* Check if we tagcache is loaded into ram. */ |
Miika Pekkarinen | 0dd7ea2 | 2006-11-10 08:03:33 +0000 | [diff] [blame] | 409 | if (!tc_stat.ramcache) |
Miika Pekkarinen | 45dfe2a | 2006-07-15 17:36:25 +0000 | [diff] [blame] | 410 | return -1; |
Miika Pekkarinen | 7c4e0c8 | 2006-03-26 11:33:42 +0000 | [diff] [blame] | 411 | |
| 412 | if (dc == NULL) |
| 413 | dc = dircache_get_entry_ptr(filename); |
| 414 | |
| 415 | if (dc == NULL) |
| 416 | { |
| 417 | logf("tagcache: file not found."); |
Miika Pekkarinen | 45dfe2a | 2006-07-15 17:36:25 +0000 | [diff] [blame] | 418 | return -1; |
Miika Pekkarinen | 7c4e0c8 | 2006-03-26 11:33:42 +0000 | [diff] [blame] | 419 | } |
| 420 | |
| 421 | try_again: |
| 422 | |
| 423 | if (last_pos > 0) |
| 424 | i = last_pos; |
| 425 | else |
| 426 | i = 0; |
| 427 | |
Miika Pekkarinen | cb8c795 | 2006-07-20 12:19:31 +0000 | [diff] [blame] | 428 | for (; i < hdr->h.tch.entry_count; i++) |
Miika Pekkarinen | 7c4e0c8 | 2006-03-26 11:33:42 +0000 | [diff] [blame] | 429 | { |
| 430 | if (hdr->indices[i].tag_seek[tag_filename] == (long)dc) |
| 431 | { |
| 432 | last_pos = MAX(0, i - 3); |
Miika Pekkarinen | 45dfe2a | 2006-07-15 17:36:25 +0000 | [diff] [blame] | 433 | return i; |
Miika Pekkarinen | 7c4e0c8 | 2006-03-26 11:33:42 +0000 | [diff] [blame] | 434 | } |
| 435 | |
Steve Bavin | 6d7f68c | 2007-03-10 14:34:56 +0000 | [diff] [blame] | 436 | do_timed_yield(); |
Miika Pekkarinen | 7c4e0c8 | 2006-03-26 11:33:42 +0000 | [diff] [blame] | 437 | } |
| 438 | |
| 439 | if (last_pos > 0) |
| 440 | { |
| 441 | last_pos = 0; |
| 442 | goto try_again; |
| 443 | } |
| 444 | |
Miika Pekkarinen | 45dfe2a | 2006-07-15 17:36:25 +0000 | [diff] [blame] | 445 | return -1; |
Miika Pekkarinen | 7c4e0c8 | 2006-03-26 11:33:42 +0000 | [diff] [blame] | 446 | } |
| 447 | #endif |
| 448 | |
Miika Pekkarinen | 45dfe2a | 2006-07-15 17:36:25 +0000 | [diff] [blame] | 449 | static long find_entry_disk(const char *filename) |
Miika Pekkarinen | 7c4e0c8 | 2006-03-26 11:33:42 +0000 | [diff] [blame] | 450 | { |
Miika Pekkarinen | 29fa15f | 2006-07-23 11:15:28 +0000 | [diff] [blame] | 451 | struct tagcache_header tch; |
Miika Pekkarinen | 7c4e0c8 | 2006-03-26 11:33:42 +0000 | [diff] [blame] | 452 | static long last_pos = -1; |
| 453 | long pos_history[POS_HISTORY_COUNT]; |
| 454 | long pos_history_idx = 0; |
Miika Pekkarinen | 7c4e0c8 | 2006-03-26 11:33:42 +0000 | [diff] [blame] | 455 | bool found = false; |
| 456 | struct tagfile_entry tfe; |
Miika Pekkarinen | 45dfe2a | 2006-07-15 17:36:25 +0000 | [diff] [blame] | 457 | int fd; |
Miika Pekkarinen | 0dd7ea2 | 2006-11-10 08:03:33 +0000 | [diff] [blame] | 458 | char buf[TAG_MAXLEN+32]; |
Miika Pekkarinen | 7c4e0c8 | 2006-03-26 11:33:42 +0000 | [diff] [blame] | 459 | int i; |
| 460 | int pos = -1; |
| 461 | |
Miika Pekkarinen | 0dd7ea2 | 2006-11-10 08:03:33 +0000 | [diff] [blame] | 462 | if (!tc_stat.ready) |
Miika Pekkarinen | 29fa15f | 2006-07-23 11:15:28 +0000 | [diff] [blame] | 463 | return -2; |
| 464 | |
Miika Pekkarinen | 45dfe2a | 2006-07-15 17:36:25 +0000 | [diff] [blame] | 465 | fd = filenametag_fd; |
Miika Pekkarinen | 7c4e0c8 | 2006-03-26 11:33:42 +0000 | [diff] [blame] | 466 | if (fd < 0) |
| 467 | { |
| 468 | last_pos = -1; |
Miika Pekkarinen | 29fa15f | 2006-07-23 11:15:28 +0000 | [diff] [blame] | 469 | if ( (fd = open_tag_fd(&tch, tag_filename, false)) < 0) |
Miika Pekkarinen | 45dfe2a | 2006-07-15 17:36:25 +0000 | [diff] [blame] | 470 | return -1; |
Miika Pekkarinen | 7c4e0c8 | 2006-03-26 11:33:42 +0000 | [diff] [blame] | 471 | } |
| 472 | |
| 473 | check_again: |
| 474 | |
| 475 | if (last_pos > 0) |
| 476 | lseek(fd, last_pos, SEEK_SET); |
Miika Pekkarinen | 9c0b54a | 2006-07-24 12:10:50 +0000 | [diff] [blame] | 477 | else |
| 478 | lseek(fd, sizeof(struct tagcache_header), SEEK_SET); |
Miika Pekkarinen | 7c4e0c8 | 2006-03-26 11:33:42 +0000 | [diff] [blame] | 479 | |
| 480 | while (true) |
| 481 | { |
| 482 | pos = lseek(fd, 0, SEEK_CUR); |
| 483 | for (i = pos_history_idx-1; i >= 0; i--) |
| 484 | pos_history[i+1] = pos_history[i]; |
| 485 | pos_history[0] = pos; |
| 486 | |
Miika Pekkarinen | 9b9539c | 2007-02-13 21:51:18 +0000 | [diff] [blame] | 487 | if (ecread(fd, &tfe, 1, tagfile_entry_ec, tc_stat.econ) |
| 488 | != sizeof(struct tagfile_entry)) |
Miika Pekkarinen | 7c4e0c8 | 2006-03-26 11:33:42 +0000 | [diff] [blame] | 489 | { |
| 490 | break ; |
| 491 | } |
| 492 | |
| 493 | if (tfe.tag_length >= (long)sizeof(buf)) |
| 494 | { |
Miika Pekkarinen | 9cd5c3e | 2006-07-10 16:22:03 +0000 | [diff] [blame] | 495 | logf("too long tag #1"); |
Miika Pekkarinen | 7c4e0c8 | 2006-03-26 11:33:42 +0000 | [diff] [blame] | 496 | close(fd); |
| 497 | last_pos = -1; |
Miika Pekkarinen | 45dfe2a | 2006-07-15 17:36:25 +0000 | [diff] [blame] | 498 | return -2; |
Miika Pekkarinen | 7c4e0c8 | 2006-03-26 11:33:42 +0000 | [diff] [blame] | 499 | } |
| 500 | |
| 501 | if (read(fd, buf, tfe.tag_length) != tfe.tag_length) |
| 502 | { |
| 503 | logf("read error #2"); |
| 504 | close(fd); |
| 505 | last_pos = -1; |
Miika Pekkarinen | 45dfe2a | 2006-07-15 17:36:25 +0000 | [diff] [blame] | 506 | return -3; |
Miika Pekkarinen | 7c4e0c8 | 2006-03-26 11:33:42 +0000 | [diff] [blame] | 507 | } |
| 508 | |
| 509 | if (!strcasecmp(filename, buf)) |
| 510 | { |
| 511 | last_pos = pos_history[pos_history_idx]; |
| 512 | found = true; |
| 513 | break ; |
| 514 | } |
| 515 | |
| 516 | if (pos_history_idx < POS_HISTORY_COUNT - 1) |
| 517 | pos_history_idx++; |
| 518 | } |
| 519 | |
| 520 | /* Not found? */ |
| 521 | if (!found) |
| 522 | { |
| 523 | if (last_pos > 0) |
| 524 | { |
| 525 | last_pos = -1; |
| 526 | logf("seek again"); |
| 527 | goto check_again; |
| 528 | } |
Miika Pekkarinen | 7c4e0c8 | 2006-03-26 11:33:42 +0000 | [diff] [blame] | 529 | |
Miika Pekkarinen | 45dfe2a | 2006-07-15 17:36:25 +0000 | [diff] [blame] | 530 | if (fd != filenametag_fd) |
| 531 | close(fd); |
| 532 | return -4; |
Miika Pekkarinen | 7c4e0c8 | 2006-03-26 11:33:42 +0000 | [diff] [blame] | 533 | } |
| 534 | |
Miika Pekkarinen | 45dfe2a | 2006-07-15 17:36:25 +0000 | [diff] [blame] | 535 | if (fd != filenametag_fd) |
| 536 | close(fd); |
| 537 | |
| 538 | return tfe.idx_id; |
Miika Pekkarinen | 7c4e0c8 | 2006-03-26 11:33:42 +0000 | [diff] [blame] | 539 | } |
| 540 | |
Miika Pekkarinen | cb8c795 | 2006-07-20 12:19:31 +0000 | [diff] [blame] | 541 | static int find_index(const char *filename) |
Miika Pekkarinen | 44b76bc | 2006-03-30 12:07:32 +0000 | [diff] [blame] | 542 | { |
Miika Pekkarinen | 45dfe2a | 2006-07-15 17:36:25 +0000 | [diff] [blame] | 543 | long idx_id = -1; |
Miika Pekkarinen | 44b76bc | 2006-03-30 12:07:32 +0000 | [diff] [blame] | 544 | |
Miika Pekkarinen | 45dfe2a | 2006-07-15 17:36:25 +0000 | [diff] [blame] | 545 | #if defined(HAVE_TC_RAMCACHE) && defined(HAVE_DIRCACHE) |
Miika Pekkarinen | 39c597b | 2008-01-13 19:13:37 +0000 | [diff] [blame] | 546 | if (tc_stat.ramcache && is_dircache_intact()) |
Miika Pekkarinen | 45dfe2a | 2006-07-15 17:36:25 +0000 | [diff] [blame] | 547 | idx_id = find_entry_ram(filename, NULL); |
| 548 | #endif |
| 549 | |
| 550 | if (idx_id < 0) |
| 551 | idx_id = find_entry_disk(filename); |
| 552 | |
Miika Pekkarinen | cb8c795 | 2006-07-20 12:19:31 +0000 | [diff] [blame] | 553 | return idx_id; |
| 554 | } |
| 555 | |
| 556 | bool tagcache_find_index(struct tagcache_search *tcs, const char *filename) |
| 557 | { |
| 558 | int idx_id; |
| 559 | |
Miika Pekkarinen | 0dd7ea2 | 2006-11-10 08:03:33 +0000 | [diff] [blame] | 560 | if (!tc_stat.ready) |
Miika Pekkarinen | 29fa15f | 2006-07-23 11:15:28 +0000 | [diff] [blame] | 561 | return false; |
| 562 | |
Miika Pekkarinen | cb8c795 | 2006-07-20 12:19:31 +0000 | [diff] [blame] | 563 | idx_id = find_index(filename); |
| 564 | if (idx_id < 0) |
| 565 | return false; |
| 566 | |
Miika Pekkarinen | 45dfe2a | 2006-07-15 17:36:25 +0000 | [diff] [blame] | 567 | if (!tagcache_search(tcs, tag_filename)) |
| 568 | return false; |
| 569 | |
| 570 | tcs->entry_count = 0; |
| 571 | tcs->idx_id = idx_id; |
| 572 | |
| 573 | return true; |
| 574 | } |
| 575 | |
Miika Pekkarinen | dafa0d4 | 2006-07-24 15:35:46 +0000 | [diff] [blame] | 576 | static bool get_index(int masterfd, int idxid, |
| 577 | struct index_entry *idx, bool use_ram) |
Miika Pekkarinen | 45dfe2a | 2006-07-15 17:36:25 +0000 | [diff] [blame] | 578 | { |
Miika Pekkarinen | f603946 | 2007-12-16 21:10:26 +0000 | [diff] [blame] | 579 | bool localfd = false; |
| 580 | |
Miika Pekkarinen | 4294615 | 2006-08-30 18:18:37 +0000 | [diff] [blame] | 581 | if (idxid < 0) |
| 582 | { |
| 583 | logf("Incorrect idxid: %d", idxid); |
| 584 | return false; |
| 585 | } |
| 586 | |
Miika Pekkarinen | 44b76bc | 2006-03-30 12:07:32 +0000 | [diff] [blame] | 587 | #ifdef HAVE_TC_RAMCACHE |
Miika Pekkarinen | 0dd7ea2 | 2006-11-10 08:03:33 +0000 | [diff] [blame] | 588 | if (tc_stat.ramcache && use_ram) |
Miika Pekkarinen | 44b76bc | 2006-03-30 12:07:32 +0000 | [diff] [blame] | 589 | { |
Miika Pekkarinen | f9bfd73 | 2006-04-19 18:56:59 +0000 | [diff] [blame] | 590 | if (hdr->indices[idxid].flag & FLAG_DELETED) |
| 591 | return false; |
| 592 | |
Miika Pekkarinen | 45dfe2a | 2006-07-15 17:36:25 +0000 | [diff] [blame] | 593 | memcpy(idx, &hdr->indices[idxid], sizeof(struct index_entry)); |
| 594 | return true; |
Miika Pekkarinen | 44b76bc | 2006-03-30 12:07:32 +0000 | [diff] [blame] | 595 | } |
Jens Arnold | a5f42cb | 2006-08-02 06:57:13 +0000 | [diff] [blame] | 596 | #else |
| 597 | (void)use_ram; |
Miika Pekkarinen | 44b76bc | 2006-03-30 12:07:32 +0000 | [diff] [blame] | 598 | #endif |
| 599 | |
Miika Pekkarinen | f603946 | 2007-12-16 21:10:26 +0000 | [diff] [blame] | 600 | if (masterfd < 0) |
| 601 | { |
| 602 | struct master_header tcmh; |
| 603 | |
| 604 | localfd = true; |
| 605 | masterfd = open_master_fd(&tcmh, false); |
| 606 | if (masterfd < 0) |
| 607 | return false; |
| 608 | } |
| 609 | |
Miika Pekkarinen | 86f07c7 | 2006-07-23 15:35:53 +0000 | [diff] [blame] | 610 | lseek(masterfd, idxid * sizeof(struct index_entry) |
Miika Pekkarinen | cb8c795 | 2006-07-20 12:19:31 +0000 | [diff] [blame] | 611 | + sizeof(struct master_header), SEEK_SET); |
Miika Pekkarinen | 9b9539c | 2007-02-13 21:51:18 +0000 | [diff] [blame] | 612 | if (ecread(masterfd, idx, 1, index_entry_ec, tc_stat.econ) |
| 613 | != sizeof(struct index_entry)) |
Miika Pekkarinen | 44b76bc | 2006-03-30 12:07:32 +0000 | [diff] [blame] | 614 | { |
| 615 | logf("read error #3"); |
Miika Pekkarinen | f603946 | 2007-12-16 21:10:26 +0000 | [diff] [blame] | 616 | if (localfd) |
| 617 | close(masterfd); |
| 618 | |
Miika Pekkarinen | 45dfe2a | 2006-07-15 17:36:25 +0000 | [diff] [blame] | 619 | return false; |
Miika Pekkarinen | 44b76bc | 2006-03-30 12:07:32 +0000 | [diff] [blame] | 620 | } |
Miika Pekkarinen | 44b76bc | 2006-03-30 12:07:32 +0000 | [diff] [blame] | 621 | |
Miika Pekkarinen | f603946 | 2007-12-16 21:10:26 +0000 | [diff] [blame] | 622 | if (localfd) |
| 623 | close(masterfd); |
| 624 | |
Miika Pekkarinen | 86f07c7 | 2006-07-23 15:35:53 +0000 | [diff] [blame] | 625 | if (idx->flag & FLAG_DELETED) |
| 626 | return false; |
| 627 | |
| 628 | return true; |
| 629 | } |
| 630 | |
| 631 | static bool write_index(int masterfd, int idxid, struct index_entry *idx) |
| 632 | { |
Miika Pekkarinen | dafa0d4 | 2006-07-24 15:35:46 +0000 | [diff] [blame] | 633 | /* We need to exclude all memory only flags & tags when writing to disk. */ |
| 634 | if (idx->flag & FLAG_DIRCACHE) |
| 635 | { |
| 636 | logf("memory only flags!"); |
| 637 | return false; |
| 638 | } |
| 639 | |
Miika Pekkarinen | 86f07c7 | 2006-07-23 15:35:53 +0000 | [diff] [blame] | 640 | #ifdef HAVE_TC_RAMCACHE |
Miika Pekkarinen | 4cee8d9 | 2007-05-27 11:40:55 +0000 | [diff] [blame] | 641 | /* Only update numeric data. Writing the whole index to RAM by memcpy |
| 642 | * destroys dircache pointers! |
| 643 | */ |
Miika Pekkarinen | 0dd7ea2 | 2006-11-10 08:03:33 +0000 | [diff] [blame] | 644 | if (tc_stat.ramcache) |
Miika Pekkarinen | dafa0d4 | 2006-07-24 15:35:46 +0000 | [diff] [blame] | 645 | { |
Miika Pekkarinen | 4cee8d9 | 2007-05-27 11:40:55 +0000 | [diff] [blame] | 646 | int tag; |
| 647 | struct index_entry *idx_ram = &hdr->indices[idxid]; |
| 648 | |
| 649 | for (tag = 0; tag < TAG_COUNT; tag++) |
| 650 | { |
| 651 | if (tagcache_is_numeric_tag(tag)) |
| 652 | { |
| 653 | idx_ram->tag_seek[tag] = idx->tag_seek[tag]; |
| 654 | } |
| 655 | } |
| 656 | |
| 657 | /* Don't touch the dircache flag. */ |
| 658 | idx_ram->flag = idx->flag | (idx_ram->flag & FLAG_DIRCACHE); |
Miika Pekkarinen | dafa0d4 | 2006-07-24 15:35:46 +0000 | [diff] [blame] | 659 | } |
Miika Pekkarinen | 86f07c7 | 2006-07-23 15:35:53 +0000 | [diff] [blame] | 660 | #endif |
| 661 | |
| 662 | lseek(masterfd, idxid * sizeof(struct index_entry) |
| 663 | + sizeof(struct master_header), SEEK_SET); |
Miika Pekkarinen | 9b9539c | 2007-02-13 21:51:18 +0000 | [diff] [blame] | 664 | if (ecwrite(masterfd, idx, 1, index_entry_ec, tc_stat.econ) |
| 665 | != sizeof(struct index_entry)) |
Miika Pekkarinen | 86f07c7 | 2006-07-23 15:35:53 +0000 | [diff] [blame] | 666 | { |
| 667 | logf("write error #3"); |
Miika Pekkarinen | d8ac607 | 2006-08-02 17:39:34 +0000 | [diff] [blame] | 668 | logf("idxid: %d", idxid); |
Miika Pekkarinen | 86f07c7 | 2006-07-23 15:35:53 +0000 | [diff] [blame] | 669 | return false; |
| 670 | } |
| 671 | |
Miika Pekkarinen | 45dfe2a | 2006-07-15 17:36:25 +0000 | [diff] [blame] | 672 | return true; |
| 673 | } |
| 674 | |
Miika Pekkarinen | b89b5ba | 2006-10-15 11:01:18 +0000 | [diff] [blame] | 675 | static bool open_files(struct tagcache_search *tcs, int tag) |
| 676 | { |
| 677 | if (tcs->idxfd[tag] < 0) |
| 678 | { |
| 679 | char fn[MAX_PATH]; |
| 680 | |
| 681 | snprintf(fn, sizeof fn, TAGCACHE_FILE_INDEX, tag); |
| 682 | tcs->idxfd[tag] = open(fn, O_RDONLY); |
| 683 | } |
| 684 | |
| 685 | if (tcs->idxfd[tag] < 0) |
| 686 | { |
| 687 | logf("File not open!"); |
| 688 | return false; |
| 689 | } |
| 690 | |
| 691 | return true; |
| 692 | } |
| 693 | |
| 694 | static bool retrieve(struct tagcache_search *tcs, struct index_entry *idx, |
| 695 | int tag, char *buf, long size) |
| 696 | { |
| 697 | struct tagfile_entry tfe; |
| 698 | long seek; |
| 699 | |
| 700 | *buf = '\0'; |
| 701 | |
| 702 | if (tagcache_is_numeric_tag(tag)) |
| 703 | return false; |
| 704 | |
| 705 | seek = idx->tag_seek[tag]; |
| 706 | if (seek < 0) |
| 707 | { |
| 708 | logf("Retrieve failed"); |
| 709 | return false; |
| 710 | } |
| 711 | |
| 712 | #ifdef HAVE_TC_RAMCACHE |
| 713 | if (tcs->ramsearch) |
| 714 | { |
| 715 | struct tagfile_entry *ep; |
| 716 | |
| 717 | # ifdef HAVE_DIRCACHE |
Miika Pekkarinen | 39c597b | 2008-01-13 19:13:37 +0000 | [diff] [blame] | 718 | if (tag == tag_filename && (idx->flag & FLAG_DIRCACHE) |
| 719 | && is_dircache_intact()) |
Miika Pekkarinen | b89b5ba | 2006-10-15 11:01:18 +0000 | [diff] [blame] | 720 | { |
Kevin Ferrare | 011a325 | 2007-07-20 17:06:55 +0000 | [diff] [blame] | 721 | dircache_copy_path((struct dirent *)seek, |
Miika Pekkarinen | b89b5ba | 2006-10-15 11:01:18 +0000 | [diff] [blame] | 722 | buf, size); |
| 723 | return true; |
| 724 | } |
| 725 | else |
| 726 | # endif |
| 727 | if (tag != tag_filename) |
| 728 | { |
| 729 | ep = (struct tagfile_entry *)&hdr->tags[tag][seek]; |
| 730 | strncpy(buf, ep->tag_data, size-1); |
| 731 | |
| 732 | return true; |
| 733 | } |
| 734 | } |
| 735 | #endif |
| 736 | |
| 737 | if (!open_files(tcs, tag)) |
| 738 | return false; |
| 739 | |
| 740 | lseek(tcs->idxfd[tag], seek, SEEK_SET); |
Miika Pekkarinen | 9b9539c | 2007-02-13 21:51:18 +0000 | [diff] [blame] | 741 | if (ecread(tcs->idxfd[tag], &tfe, 1, tagfile_entry_ec, tc_stat.econ) |
| 742 | != sizeof(struct tagfile_entry)) |
Miika Pekkarinen | b89b5ba | 2006-10-15 11:01:18 +0000 | [diff] [blame] | 743 | { |
| 744 | logf("read error #5"); |
| 745 | return false; |
| 746 | } |
| 747 | |
| 748 | if (tfe.tag_length >= size) |
| 749 | { |
| 750 | logf("too small buffer"); |
| 751 | return false; |
| 752 | } |
| 753 | |
| 754 | if (read(tcs->idxfd[tag], buf, tfe.tag_length) != |
| 755 | tfe.tag_length) |
| 756 | { |
| 757 | logf("read error #6"); |
| 758 | return false; |
| 759 | } |
| 760 | |
| 761 | buf[tfe.tag_length] = '\0'; |
| 762 | |
| 763 | return true; |
| 764 | } |
| 765 | |
Miika Pekkarinen | 45dfe2a | 2006-07-15 17:36:25 +0000 | [diff] [blame] | 766 | static long check_virtual_tags(int tag, const struct index_entry *idx) |
| 767 | { |
| 768 | long data = 0; |
| 769 | |
| 770 | switch (tag) |
| 771 | { |
Miika Pekkarinen | 9d9937a | 2007-04-12 20:14:05 +0000 | [diff] [blame] | 772 | case tag_virt_length_sec: |
| 773 | data = (idx->tag_seek[tag_length]/1000) % 60; |
| 774 | break; |
| 775 | |
| 776 | case tag_virt_length_min: |
| 777 | data = (idx->tag_seek[tag_length]/1000) / 60; |
| 778 | break; |
| 779 | |
Miika Pekkarinen | 5e47daa | 2007-04-12 20:21:56 +0000 | [diff] [blame] | 780 | case tag_virt_playtime_sec: |
| 781 | data = (idx->tag_seek[tag_playtime]/1000) % 60; |
| 782 | break; |
| 783 | |
| 784 | case tag_virt_playtime_min: |
| 785 | data = (idx->tag_seek[tag_playtime]/1000) / 60; |
| 786 | break; |
| 787 | |
Miika Pekkarinen | 45dfe2a | 2006-07-15 17:36:25 +0000 | [diff] [blame] | 788 | case tag_virt_autoscore: |
| 789 | if (idx->tag_seek[tag_length] == 0 |
| 790 | || idx->tag_seek[tag_playcount] == 0) |
| 791 | { |
| 792 | data = 0; |
| 793 | } |
| 794 | else |
| 795 | { |
| 796 | data = 100 * idx->tag_seek[tag_playtime] |
| 797 | / idx->tag_seek[tag_length] |
| 798 | / idx->tag_seek[tag_playcount]; |
| 799 | } |
| 800 | break; |
| 801 | |
Miika Pekkarinen | f5184f3 | 2007-02-25 20:41:51 +0000 | [diff] [blame] | 802 | /* How many commits before the file has been added to the DB. */ |
| 803 | case tag_virt_entryage: |
| 804 | data = current_tcmh.commitid - idx->tag_seek[tag_commitid] - 1; |
| 805 | break; |
| 806 | |
Miika Pekkarinen | 45dfe2a | 2006-07-15 17:36:25 +0000 | [diff] [blame] | 807 | default: |
| 808 | data = idx->tag_seek[tag]; |
| 809 | } |
| 810 | |
| 811 | return data; |
Miika Pekkarinen | 44b76bc | 2006-03-30 12:07:32 +0000 | [diff] [blame] | 812 | } |
| 813 | |
Miika Pekkarinen | 58fe4de | 2006-04-10 10:26:24 +0000 | [diff] [blame] | 814 | long tagcache_get_numeric(const struct tagcache_search *tcs, int tag) |
| 815 | { |
Miika Pekkarinen | 45dfe2a | 2006-07-15 17:36:25 +0000 | [diff] [blame] | 816 | struct index_entry idx; |
Miika Pekkarinen | 58fe4de | 2006-04-10 10:26:24 +0000 | [diff] [blame] | 817 | |
Miika Pekkarinen | 0dd7ea2 | 2006-11-10 08:03:33 +0000 | [diff] [blame] | 818 | if (!tc_stat.ready) |
Miika Pekkarinen | 29fa15f | 2006-07-23 11:15:28 +0000 | [diff] [blame] | 819 | return false; |
| 820 | |
Miika Pekkarinen | 58fe4de | 2006-04-10 10:26:24 +0000 | [diff] [blame] | 821 | if (!tagcache_is_numeric_tag(tag)) |
| 822 | return -1; |
| 823 | |
Miika Pekkarinen | dafa0d4 | 2006-07-24 15:35:46 +0000 | [diff] [blame] | 824 | if (!get_index(tcs->masterfd, tcs->idx_id, &idx, true)) |
Miika Pekkarinen | 45dfe2a | 2006-07-15 17:36:25 +0000 | [diff] [blame] | 825 | return -2; |
| 826 | |
| 827 | return check_virtual_tags(tag, &idx); |
Miika Pekkarinen | 58fe4de | 2006-04-10 10:26:24 +0000 | [diff] [blame] | 828 | } |
| 829 | |
Miika Pekkarinen | e5059a7 | 2006-08-13 14:53:19 +0000 | [diff] [blame] | 830 | inline static bool str_ends_with(const char *str1, const char *str2) |
| 831 | { |
| 832 | int str_len = strlen(str1); |
| 833 | int clause_len = strlen(str2); |
| 834 | |
| 835 | if (clause_len > str_len) |
| 836 | return false; |
| 837 | |
| 838 | return !strcasecmp(&str1[str_len - clause_len], str2); |
| 839 | } |
| 840 | |
Miika Pekkarinen | be2eb02 | 2006-10-09 10:23:35 +0000 | [diff] [blame] | 841 | inline static bool str_oneof(const char *str, const char *list) |
| 842 | { |
| 843 | const char *sep; |
| 844 | int l, len = strlen(str); |
| 845 | |
| 846 | while (*list) |
| 847 | { |
| 848 | sep = strchr(list, '|'); |
| 849 | l = sep ? (long)sep - (long)list : (int)strlen(list); |
| 850 | if ((l==len) && !strncasecmp(str, list, len)) |
| 851 | return true; |
| 852 | list += sep ? l + 1 : l; |
| 853 | } |
| 854 | |
| 855 | return false; |
| 856 | } |
| 857 | |
Miika Pekkarinen | 4c6fd0f | 2006-04-03 18:57:34 +0000 | [diff] [blame] | 858 | static bool check_against_clause(long numeric, const char *str, |
| 859 | const struct tagcache_search_clause *clause) |
| 860 | { |
Miika Pekkarinen | 3eb9e70 | 2006-10-05 09:49:18 +0000 | [diff] [blame] | 861 | if (clause->numeric) |
Miika Pekkarinen | 4c6fd0f | 2006-04-03 18:57:34 +0000 | [diff] [blame] | 862 | { |
Miika Pekkarinen | 3eb9e70 | 2006-10-05 09:49:18 +0000 | [diff] [blame] | 863 | switch (clause->type) |
| 864 | { |
| 865 | case clause_is: |
Miika Pekkarinen | 4c6fd0f | 2006-04-03 18:57:34 +0000 | [diff] [blame] | 866 | return numeric == clause->numeric_data; |
Miika Pekkarinen | 3eb9e70 | 2006-10-05 09:49:18 +0000 | [diff] [blame] | 867 | case clause_is_not: |
| 868 | return numeric != clause->numeric_data; |
| 869 | case clause_gt: |
| 870 | return numeric > clause->numeric_data; |
| 871 | case clause_gteq: |
| 872 | return numeric >= clause->numeric_data; |
| 873 | case clause_lt: |
| 874 | return numeric < clause->numeric_data; |
| 875 | case clause_lteq: |
| 876 | return numeric <= clause->numeric_data; |
| 877 | default: |
| 878 | logf("Incorrect numeric tag: %d", clause->type); |
| 879 | } |
| 880 | } |
| 881 | else |
| 882 | { |
| 883 | switch (clause->type) |
| 884 | { |
| 885 | case clause_is: |
Miika Pekkarinen | 4c6fd0f | 2006-04-03 18:57:34 +0000 | [diff] [blame] | 886 | return !strcasecmp(clause->str, str); |
Miika Pekkarinen | 3eb9e70 | 2006-10-05 09:49:18 +0000 | [diff] [blame] | 887 | case clause_is_not: |
| 888 | return strcasecmp(clause->str, str); |
| 889 | case clause_gt: |
| 890 | return 0>strcasecmp(clause->str, str); |
| 891 | case clause_gteq: |
| 892 | return 0>=strcasecmp(clause->str, str); |
| 893 | case clause_lt: |
| 894 | return 0<strcasecmp(clause->str, str); |
| 895 | case clause_lteq: |
| 896 | return 0<=strcasecmp(clause->str, str); |
| 897 | case clause_contains: |
| 898 | return (strcasestr(str, clause->str) != NULL); |
| 899 | case clause_not_contains: |
| 900 | return (strcasestr(str, clause->str) == NULL); |
| 901 | case clause_begins_with: |
| 902 | return (strcasestr(str, clause->str) == str); |
| 903 | case clause_not_begins_with: |
Jonathan Gordon | 8ca99d3 | 2007-02-27 11:09:09 +0000 | [diff] [blame] | 904 | return (strcasestr(str, clause->str) != str); |
Miika Pekkarinen | 3eb9e70 | 2006-10-05 09:49:18 +0000 | [diff] [blame] | 905 | case clause_ends_with: |
| 906 | return str_ends_with(str, clause->str); |
| 907 | case clause_not_ends_with: |
| 908 | return !str_ends_with(str, clause->str); |
Miika Pekkarinen | be2eb02 | 2006-10-09 10:23:35 +0000 | [diff] [blame] | 909 | case clause_oneof: |
| 910 | return str_oneof(str, clause->str); |
Miika Pekkarinen | 3eb9e70 | 2006-10-05 09:49:18 +0000 | [diff] [blame] | 911 | |
| 912 | default: |
| 913 | logf("Incorrect tag: %d", clause->type); |
| 914 | } |
Miika Pekkarinen | 4c6fd0f | 2006-04-03 18:57:34 +0000 | [diff] [blame] | 915 | } |
Miika Pekkarinen | e5059a7 | 2006-08-13 14:53:19 +0000 | [diff] [blame] | 916 | |
Miika Pekkarinen | 4c6fd0f | 2006-04-03 18:57:34 +0000 | [diff] [blame] | 917 | return false; |
| 918 | } |
| 919 | |
Jens Arnold | 2597a13 | 2006-12-25 14:01:47 +0000 | [diff] [blame] | 920 | static bool check_clauses(struct tagcache_search *tcs, |
| 921 | struct index_entry *idx, |
| 922 | struct tagcache_search_clause **clause, int count) |
Miika Pekkarinen | b89b5ba | 2006-10-15 11:01:18 +0000 | [diff] [blame] | 923 | { |
| 924 | int i; |
| 925 | |
| 926 | #ifdef HAVE_TC_RAMCACHE |
| 927 | if (tcs->ramsearch) |
| 928 | { |
| 929 | /* Go through all conditional clauses. */ |
| 930 | for (i = 0; i < count; i++) |
| 931 | { |
| 932 | struct tagfile_entry *tfe; |
| 933 | int seek; |
| 934 | char buf[256]; |
| 935 | char *str = NULL; |
| 936 | |
| 937 | seek = check_virtual_tags(clause[i]->tag, idx); |
| 938 | |
| 939 | if (!tagcache_is_numeric_tag(clause[i]->tag)) |
| 940 | { |
| 941 | if (clause[i]->tag == tag_filename) |
| 942 | { |
| 943 | retrieve(tcs, idx, tag_filename, buf, sizeof buf); |
| 944 | str = buf; |
| 945 | } |
| 946 | else |
| 947 | { |
| 948 | tfe = (struct tagfile_entry *)&hdr->tags[clause[i]->tag][seek]; |
| 949 | str = tfe->tag_data; |
| 950 | } |
| 951 | } |
| 952 | |
| 953 | if (!check_against_clause(seek, str, clause[i])) |
| 954 | return false; |
| 955 | } |
| 956 | } |
| 957 | else |
| 958 | #endif |
| 959 | { |
| 960 | /* Check for conditions. */ |
| 961 | for (i = 0; i < count; i++) |
| 962 | { |
| 963 | struct tagfile_entry tfe; |
| 964 | int seek; |
| 965 | char str[256]; |
| 966 | |
| 967 | seek = check_virtual_tags(clause[i]->tag, idx); |
| 968 | |
| 969 | memset(str, 0, sizeof str); |
| 970 | if (!tagcache_is_numeric_tag(clause[i]->tag)) |
| 971 | { |
| 972 | int fd = tcs->idxfd[clause[i]->tag]; |
| 973 | lseek(fd, seek, SEEK_SET); |
Miika Pekkarinen | 9b9539c | 2007-02-13 21:51:18 +0000 | [diff] [blame] | 974 | ecread(fd, &tfe, 1, tagfile_entry_ec, tc_stat.econ); |
Miika Pekkarinen | b89b5ba | 2006-10-15 11:01:18 +0000 | [diff] [blame] | 975 | if (tfe.tag_length >= (int)sizeof(str)) |
| 976 | { |
| 977 | logf("Too long tag read!"); |
| 978 | break ; |
| 979 | } |
| 980 | |
| 981 | read(fd, str, tfe.tag_length); |
| 982 | |
| 983 | /* Check if entry has been deleted. */ |
| 984 | if (str[0] == '\0') |
| 985 | break; |
| 986 | } |
| 987 | |
| 988 | if (!check_against_clause(seek, str, clause[i])) |
| 989 | return false; |
| 990 | } |
| 991 | } |
| 992 | |
| 993 | return true; |
| 994 | } |
| 995 | |
| 996 | bool tagcache_check_clauses(struct tagcache_search *tcs, |
| 997 | struct tagcache_search_clause **clause, int count) |
| 998 | { |
| 999 | struct index_entry idx; |
| 1000 | |
| 1001 | if (count == 0) |
| 1002 | return true; |
| 1003 | |
| 1004 | if (!get_index(tcs->masterfd, tcs->idx_id, &idx, true)) |
| 1005 | return false; |
| 1006 | |
| 1007 | return check_clauses(tcs, &idx, clause, count); |
| 1008 | } |
| 1009 | |
Miika Pekkarinen | 0dd7ea2 | 2006-11-10 08:03:33 +0000 | [diff] [blame] | 1010 | static bool add_uniqbuf(struct tagcache_search *tcs, unsigned long id) |
Miika Pekkarinen | e308064 | 2006-08-25 13:22:46 +0000 | [diff] [blame] | 1011 | { |
| 1012 | int i; |
| 1013 | |
| 1014 | /* If uniq buffer is not defined we must return true for search to work. */ |
Miika Pekkarinen | 93bbd44 | 2006-08-25 21:13:49 +0000 | [diff] [blame] | 1015 | if (tcs->unique_list == NULL |
| 1016 | || (!tagcache_is_unique_tag(tcs->type) |
| 1017 | && !tagcache_is_numeric_tag(tcs->type))) |
| 1018 | { |
Miika Pekkarinen | e308064 | 2006-08-25 13:22:46 +0000 | [diff] [blame] | 1019 | return true; |
Miika Pekkarinen | 93bbd44 | 2006-08-25 21:13:49 +0000 | [diff] [blame] | 1020 | } |
Miika Pekkarinen | e308064 | 2006-08-25 13:22:46 +0000 | [diff] [blame] | 1021 | |
| 1022 | for (i = 0; i < tcs->unique_list_count; i++) |
| 1023 | { |
| 1024 | /* Return false if entry is found. */ |
| 1025 | if (tcs->unique_list[i] == id) |
| 1026 | return false; |
| 1027 | } |
| 1028 | |
| 1029 | if (tcs->unique_list_count < tcs->unique_list_capacity) |
| 1030 | { |
| 1031 | tcs->unique_list[i] = id; |
| 1032 | tcs->unique_list_count++; |
| 1033 | } |
| 1034 | |
| 1035 | return true; |
| 1036 | } |
| 1037 | |
Miika Pekkarinen | 7c4e0c8 | 2006-03-26 11:33:42 +0000 | [diff] [blame] | 1038 | static bool build_lookup_list(struct tagcache_search *tcs) |
| 1039 | { |
Miika Pekkarinen | 7c4e0c8 | 2006-03-26 11:33:42 +0000 | [diff] [blame] | 1040 | struct index_entry entry; |
Miika Pekkarinen | 7c4e0c8 | 2006-03-26 11:33:42 +0000 | [diff] [blame] | 1041 | int i; |
| 1042 | |
| 1043 | tcs->seek_list_count = 0; |
| 1044 | |
| 1045 | #ifdef HAVE_TC_RAMCACHE |
| 1046 | if (tcs->ramsearch) |
| 1047 | { |
| 1048 | int j; |
| 1049 | |
Miika Pekkarinen | cb8c795 | 2006-07-20 12:19:31 +0000 | [diff] [blame] | 1050 | for (i = tcs->seek_pos; i < hdr->h.tch.entry_count; i++) |
Miika Pekkarinen | 7c4e0c8 | 2006-03-26 11:33:42 +0000 | [diff] [blame] | 1051 | { |
Miika Pekkarinen | b89b5ba | 2006-10-15 11:01:18 +0000 | [diff] [blame] | 1052 | struct index_entry *idx = &hdr->indices[i]; |
Miika Pekkarinen | 7c4e0c8 | 2006-03-26 11:33:42 +0000 | [diff] [blame] | 1053 | if (tcs->seek_list_count == SEEK_LIST_SIZE) |
| 1054 | break ; |
Miika Pekkarinen | f9bfd73 | 2006-04-19 18:56:59 +0000 | [diff] [blame] | 1055 | |
| 1056 | /* Skip deleted files. */ |
Miika Pekkarinen | b89b5ba | 2006-10-15 11:01:18 +0000 | [diff] [blame] | 1057 | if (idx->flag & FLAG_DELETED) |
Miika Pekkarinen | f9bfd73 | 2006-04-19 18:56:59 +0000 | [diff] [blame] | 1058 | continue; |
| 1059 | |
Miika Pekkarinen | 93ed0a7 | 2006-04-09 20:00:49 +0000 | [diff] [blame] | 1060 | /* Go through all filters.. */ |
Miika Pekkarinen | 7c4e0c8 | 2006-03-26 11:33:42 +0000 | [diff] [blame] | 1061 | for (j = 0; j < tcs->filter_count; j++) |
| 1062 | { |
Miika Pekkarinen | b89b5ba | 2006-10-15 11:01:18 +0000 | [diff] [blame] | 1063 | if (idx->tag_seek[tcs->filter_tag[j]] != tcs->filter_seek[j]) |
Miika Pekkarinen | 93bbd44 | 2006-08-25 21:13:49 +0000 | [diff] [blame] | 1064 | { |
Miika Pekkarinen | 7c4e0c8 | 2006-03-26 11:33:42 +0000 | [diff] [blame] | 1065 | break ; |
Miika Pekkarinen | 93bbd44 | 2006-08-25 21:13:49 +0000 | [diff] [blame] | 1066 | } |
Miika Pekkarinen | 7c4e0c8 | 2006-03-26 11:33:42 +0000 | [diff] [blame] | 1067 | } |
| 1068 | |
| 1069 | if (j < tcs->filter_count) |
| 1070 | continue ; |
Miika Pekkarinen | 93ed0a7 | 2006-04-09 20:00:49 +0000 | [diff] [blame] | 1071 | |
Miika Pekkarinen | b89b5ba | 2006-10-15 11:01:18 +0000 | [diff] [blame] | 1072 | /* Check for conditions. */ |
| 1073 | if (!check_clauses(tcs, idx, tcs->clause, tcs->clause_count)) |
| 1074 | continue; |
Miika Pekkarinen | 4c6fd0f | 2006-04-03 18:57:34 +0000 | [diff] [blame] | 1075 | |
Miika Pekkarinen | e308064 | 2006-08-25 13:22:46 +0000 | [diff] [blame] | 1076 | /* Add to the seek list if not already in uniq buffer. */ |
Miika Pekkarinen | b89b5ba | 2006-10-15 11:01:18 +0000 | [diff] [blame] | 1077 | if (!add_uniqbuf(tcs, idx->tag_seek[tcs->type])) |
Miika Pekkarinen | e308064 | 2006-08-25 13:22:46 +0000 | [diff] [blame] | 1078 | continue; |
| 1079 | |
Miika Pekkarinen | 7c4e0c8 | 2006-03-26 11:33:42 +0000 | [diff] [blame] | 1080 | /* Lets add it. */ |
Miika Pekkarinen | b89b5ba | 2006-10-15 11:01:18 +0000 | [diff] [blame] | 1081 | tcs->seek_list[tcs->seek_list_count] = idx->tag_seek[tcs->type]; |
| 1082 | tcs->seek_flags[tcs->seek_list_count] = idx->flag; |
Miika Pekkarinen | e308064 | 2006-08-25 13:22:46 +0000 | [diff] [blame] | 1083 | tcs->seek_list_count++; |
Miika Pekkarinen | 7c4e0c8 | 2006-03-26 11:33:42 +0000 | [diff] [blame] | 1084 | } |
| 1085 | |
| 1086 | tcs->seek_pos = i; |
| 1087 | |
| 1088 | return tcs->seek_list_count > 0; |
| 1089 | } |
| 1090 | #endif |
| 1091 | |
Miika Pekkarinen | 93ed0a7 | 2006-04-09 20:00:49 +0000 | [diff] [blame] | 1092 | lseek(tcs->masterfd, tcs->seek_pos * sizeof(struct index_entry) + |
Miika Pekkarinen | cb8c795 | 2006-07-20 12:19:31 +0000 | [diff] [blame] | 1093 | sizeof(struct master_header), SEEK_SET); |
Miika Pekkarinen | 7c4e0c8 | 2006-03-26 11:33:42 +0000 | [diff] [blame] | 1094 | |
Miika Pekkarinen | 9b9539c | 2007-02-13 21:51:18 +0000 | [diff] [blame] | 1095 | while (ecread(tcs->masterfd, &entry, 1, index_entry_ec, tc_stat.econ) |
| 1096 | == sizeof(struct index_entry)) |
Miika Pekkarinen | 7c4e0c8 | 2006-03-26 11:33:42 +0000 | [diff] [blame] | 1097 | { |
Miika Pekkarinen | 408dfd6 | 2007-03-11 08:52:33 +0000 | [diff] [blame] | 1098 | if (tcs->seek_list_count == SEEK_LIST_SIZE) |
| 1099 | break ; |
| 1100 | |
| 1101 | tcs->seek_pos++; |
| 1102 | |
Miika Pekkarinen | 9cd5c3e | 2006-07-10 16:22:03 +0000 | [diff] [blame] | 1103 | /* Check if entry has been deleted. */ |
| 1104 | if (entry.flag & FLAG_DELETED) |
| 1105 | continue; |
| 1106 | |
Miika Pekkarinen | 93ed0a7 | 2006-04-09 20:00:49 +0000 | [diff] [blame] | 1107 | /* Go through all filters.. */ |
Miika Pekkarinen | 7c4e0c8 | 2006-03-26 11:33:42 +0000 | [diff] [blame] | 1108 | for (i = 0; i < tcs->filter_count; i++) |
| 1109 | { |
| 1110 | if (entry.tag_seek[tcs->filter_tag[i]] != tcs->filter_seek[i]) |
| 1111 | break ; |
| 1112 | } |
| 1113 | |
Miika Pekkarinen | 7c4e0c8 | 2006-03-26 11:33:42 +0000 | [diff] [blame] | 1114 | if (i < tcs->filter_count) |
| 1115 | continue ; |
| 1116 | |
Miika Pekkarinen | 4c6fd0f | 2006-04-03 18:57:34 +0000 | [diff] [blame] | 1117 | /* Check for conditions. */ |
Miika Pekkarinen | b89b5ba | 2006-10-15 11:01:18 +0000 | [diff] [blame] | 1118 | if (!check_clauses(tcs, &entry, tcs->clause, tcs->clause_count)) |
| 1119 | continue; |
Miika Pekkarinen | 4c6fd0f | 2006-04-03 18:57:34 +0000 | [diff] [blame] | 1120 | |
Miika Pekkarinen | e308064 | 2006-08-25 13:22:46 +0000 | [diff] [blame] | 1121 | /* Add to the seek list if not already in uniq buffer. */ |
| 1122 | if (!add_uniqbuf(tcs, entry.tag_seek[tcs->type])) |
| 1123 | continue; |
| 1124 | |
Miika Pekkarinen | 7c4e0c8 | 2006-03-26 11:33:42 +0000 | [diff] [blame] | 1125 | /* Lets add it. */ |
Miika Pekkarinen | e308064 | 2006-08-25 13:22:46 +0000 | [diff] [blame] | 1126 | tcs->seek_list[tcs->seek_list_count] = entry.tag_seek[tcs->type]; |
| 1127 | tcs->seek_flags[tcs->seek_list_count] = entry.flag; |
| 1128 | tcs->seek_list_count++; |
Miika Pekkarinen | 93bbd44 | 2006-08-25 21:13:49 +0000 | [diff] [blame] | 1129 | |
| 1130 | yield(); |
Miika Pekkarinen | 7c4e0c8 | 2006-03-26 11:33:42 +0000 | [diff] [blame] | 1131 | } |
Miika Pekkarinen | 7c4e0c8 | 2006-03-26 11:33:42 +0000 | [diff] [blame] | 1132 | |
| 1133 | return tcs->seek_list_count > 0; |
| 1134 | } |
| 1135 | |
Miika Pekkarinen | 58fe4de | 2006-04-10 10:26:24 +0000 | [diff] [blame] | 1136 | |
Miika Pekkarinen | cb8c795 | 2006-07-20 12:19:31 +0000 | [diff] [blame] | 1137 | static void remove_files(void) |
| 1138 | { |
| 1139 | int i; |
| 1140 | char buf[MAX_PATH]; |
| 1141 | |
Miika Pekkarinen | 0dd7ea2 | 2006-11-10 08:03:33 +0000 | [diff] [blame] | 1142 | tc_stat.ready = false; |
| 1143 | tc_stat.ramcache = false; |
Miika Pekkarinen | 9b9539c | 2007-02-13 21:51:18 +0000 | [diff] [blame] | 1144 | tc_stat.econ = false; |
Miika Pekkarinen | cb8c795 | 2006-07-20 12:19:31 +0000 | [diff] [blame] | 1145 | remove(TAGCACHE_FILE_MASTER); |
| 1146 | for (i = 0; i < TAG_COUNT; i++) |
| 1147 | { |
| 1148 | if (tagcache_is_numeric_tag(i)) |
| 1149 | continue; |
| 1150 | |
| 1151 | snprintf(buf, sizeof buf, TAGCACHE_FILE_INDEX, i); |
| 1152 | remove(buf); |
| 1153 | } |
| 1154 | } |
| 1155 | |
Miika Pekkarinen | 29fa15f | 2006-07-23 11:15:28 +0000 | [diff] [blame] | 1156 | |
Miika Pekkarinen | f5184f3 | 2007-02-25 20:41:51 +0000 | [diff] [blame] | 1157 | static bool check_all_headers(void) |
| 1158 | { |
| 1159 | struct master_header myhdr; |
| 1160 | struct tagcache_header tch; |
| 1161 | int tag; |
| 1162 | int fd; |
| 1163 | |
| 1164 | if ( (fd = open_master_fd(&myhdr, false)) < 0) |
| 1165 | return false; |
| 1166 | |
| 1167 | close(fd); |
| 1168 | if (myhdr.dirty) |
| 1169 | { |
| 1170 | logf("tagcache is dirty!"); |
| 1171 | return false; |
| 1172 | } |
| 1173 | |
| 1174 | memcpy(¤t_tcmh, &myhdr, sizeof(struct master_header)); |
| 1175 | |
| 1176 | for (tag = 0; tag < TAG_COUNT; tag++) |
| 1177 | { |
| 1178 | if (tagcache_is_numeric_tag(tag)) |
| 1179 | continue; |
| 1180 | |
| 1181 | if ( (fd = open_tag_fd(&tch, tag, false)) < 0) |
| 1182 | return false; |
| 1183 | |
| 1184 | close(fd); |
| 1185 | } |
| 1186 | |
| 1187 | return true; |
| 1188 | } |
| 1189 | |
Miika Pekkarinen | 7c4e0c8 | 2006-03-26 11:33:42 +0000 | [diff] [blame] | 1190 | bool tagcache_search(struct tagcache_search *tcs, int tag) |
| 1191 | { |
Miika Pekkarinen | cb8c795 | 2006-07-20 12:19:31 +0000 | [diff] [blame] | 1192 | struct tagcache_header tag_hdr; |
| 1193 | struct master_header master_hdr; |
Miika Pekkarinen | 53e921c | 2006-04-08 08:03:51 +0000 | [diff] [blame] | 1194 | int i; |
Miika Pekkarinen | 7c4e0c8 | 2006-03-26 11:33:42 +0000 | [diff] [blame] | 1195 | |
Miika Pekkarinen | d8ac607 | 2006-08-02 17:39:34 +0000 | [diff] [blame] | 1196 | if (tcs->initialized) |
Miika Pekkarinen | 7c4e0c8 | 2006-03-26 11:33:42 +0000 | [diff] [blame] | 1197 | tagcache_search_finish(tcs); |
| 1198 | |
Miika Pekkarinen | d8ac607 | 2006-08-02 17:39:34 +0000 | [diff] [blame] | 1199 | while (read_lock) |
| 1200 | sleep(1); |
| 1201 | |
Miika Pekkarinen | 4c6fd0f | 2006-04-03 18:57:34 +0000 | [diff] [blame] | 1202 | memset(tcs, 0, sizeof(struct tagcache_search)); |
Miika Pekkarinen | 0dd7ea2 | 2006-11-10 08:03:33 +0000 | [diff] [blame] | 1203 | if (tc_stat.commit_step > 0 || !tc_stat.ready) |
Miika Pekkarinen | 3b31346 | 2006-04-16 17:32:54 +0000 | [diff] [blame] | 1204 | return false; |
| 1205 | |
Miika Pekkarinen | 7c4e0c8 | 2006-03-26 11:33:42 +0000 | [diff] [blame] | 1206 | tcs->position = sizeof(struct tagcache_header); |
Miika Pekkarinen | 7c4e0c8 | 2006-03-26 11:33:42 +0000 | [diff] [blame] | 1207 | tcs->type = tag; |
| 1208 | tcs->seek_pos = 0; |
| 1209 | tcs->seek_list_count = 0; |
| 1210 | tcs->filter_count = 0; |
Miika Pekkarinen | 4c6fd0f | 2006-04-03 18:57:34 +0000 | [diff] [blame] | 1211 | tcs->masterfd = -1; |
Miika Pekkarinen | 7c4e0c8 | 2006-03-26 11:33:42 +0000 | [diff] [blame] | 1212 | |
Miika Pekkarinen | 53e921c | 2006-04-08 08:03:51 +0000 | [diff] [blame] | 1213 | for (i = 0; i < TAG_COUNT; i++) |
| 1214 | tcs->idxfd[i] = -1; |
| 1215 | |
Miika Pekkarinen | 7c4e0c8 | 2006-03-26 11:33:42 +0000 | [diff] [blame] | 1216 | #ifndef HAVE_TC_RAMCACHE |
| 1217 | tcs->ramsearch = false; |
| 1218 | #else |
Miika Pekkarinen | 0dd7ea2 | 2006-11-10 08:03:33 +0000 | [diff] [blame] | 1219 | tcs->ramsearch = tc_stat.ramcache; |
Miika Pekkarinen | 7c4e0c8 | 2006-03-26 11:33:42 +0000 | [diff] [blame] | 1220 | if (tcs->ramsearch) |
| 1221 | { |
| 1222 | tcs->entry_count = hdr->entry_count[tcs->type]; |
| 1223 | } |
| 1224 | else |
| 1225 | #endif |
| 1226 | { |
Miika Pekkarinen | 93bbd44 | 2006-08-25 21:13:49 +0000 | [diff] [blame] | 1227 | if (!tagcache_is_numeric_tag(tcs->type)) |
Miika Pekkarinen | d8ac607 | 2006-08-02 17:39:34 +0000 | [diff] [blame] | 1228 | { |
Miika Pekkarinen | 93bbd44 | 2006-08-25 21:13:49 +0000 | [diff] [blame] | 1229 | tcs->idxfd[tcs->type] = open_tag_fd(&tag_hdr, tcs->type, false); |
| 1230 | if (tcs->idxfd[tcs->type] < 0) |
| 1231 | return false; |
Miika Pekkarinen | d8ac607 | 2006-08-02 17:39:34 +0000 | [diff] [blame] | 1232 | } |
Miika Pekkarinen | 44b76bc | 2006-03-30 12:07:32 +0000 | [diff] [blame] | 1233 | |
Miika Pekkarinen | d8ac607 | 2006-08-02 17:39:34 +0000 | [diff] [blame] | 1234 | /* Always open as R/W so we can pass tcs to functions that modify data also |
| 1235 | * without failing. */ |
| 1236 | tcs->masterfd = open_master_fd(&master_hdr, true); |
Miika Pekkarinen | 4c6fd0f | 2006-04-03 18:57:34 +0000 | [diff] [blame] | 1237 | |
| 1238 | if (tcs->masterfd < 0) |
Miika Pekkarinen | 4c6fd0f | 2006-04-03 18:57:34 +0000 | [diff] [blame] | 1239 | return false; |
Miika Pekkarinen | 7c4e0c8 | 2006-03-26 11:33:42 +0000 | [diff] [blame] | 1240 | } |
| 1241 | |
Miika Pekkarinen | d8ac607 | 2006-08-02 17:39:34 +0000 | [diff] [blame] | 1242 | tcs->valid = true; |
| 1243 | tcs->initialized = true; |
| 1244 | write_lock++; |
| 1245 | |
Miika Pekkarinen | 7c4e0c8 | 2006-03-26 11:33:42 +0000 | [diff] [blame] | 1246 | return true; |
| 1247 | } |
| 1248 | |
Miika Pekkarinen | e308064 | 2006-08-25 13:22:46 +0000 | [diff] [blame] | 1249 | void tagcache_search_set_uniqbuf(struct tagcache_search *tcs, |
| 1250 | void *buffer, long length) |
| 1251 | { |
| 1252 | tcs->unique_list = (unsigned long *)buffer; |
| 1253 | tcs->unique_list_capacity = length / sizeof(*tcs->unique_list); |
| 1254 | tcs->unique_list_count = 0; |
| 1255 | } |
| 1256 | |
Miika Pekkarinen | 7c4e0c8 | 2006-03-26 11:33:42 +0000 | [diff] [blame] | 1257 | bool tagcache_search_add_filter(struct tagcache_search *tcs, |
| 1258 | int tag, int seek) |
| 1259 | { |
| 1260 | if (tcs->filter_count == TAGCACHE_MAX_FILTERS) |
| 1261 | return false; |
| 1262 | |
Miika Pekkarinen | 58fe4de | 2006-04-10 10:26:24 +0000 | [diff] [blame] | 1263 | if (!tagcache_is_unique_tag(tag) || tagcache_is_numeric_tag(tag)) |
| 1264 | return false; |
| 1265 | |
Miika Pekkarinen | 7c4e0c8 | 2006-03-26 11:33:42 +0000 | [diff] [blame] | 1266 | tcs->filter_tag[tcs->filter_count] = tag; |
| 1267 | tcs->filter_seek[tcs->filter_count] = seek; |
| 1268 | tcs->filter_count++; |
| 1269 | |
| 1270 | return true; |
| 1271 | } |
| 1272 | |
Miika Pekkarinen | 4c6fd0f | 2006-04-03 18:57:34 +0000 | [diff] [blame] | 1273 | bool tagcache_search_add_clause(struct tagcache_search *tcs, |
| 1274 | struct tagcache_search_clause *clause) |
| 1275 | { |
Miika Pekkarinen | e308064 | 2006-08-25 13:22:46 +0000 | [diff] [blame] | 1276 | int i; |
| 1277 | |
Miika Pekkarinen | 4c6fd0f | 2006-04-03 18:57:34 +0000 | [diff] [blame] | 1278 | if (tcs->clause_count >= TAGCACHE_MAX_CLAUSES) |
| 1279 | { |
| 1280 | logf("Too many clauses"); |
| 1281 | return false; |
| 1282 | } |
Miika Pekkarinen | 53e921c | 2006-04-08 08:03:51 +0000 | [diff] [blame] | 1283 | |
Miika Pekkarinen | e308064 | 2006-08-25 13:22:46 +0000 | [diff] [blame] | 1284 | /* Check if there is already a similar filter in present (filters are |
| 1285 | * much faster than clauses). |
| 1286 | */ |
| 1287 | for (i = 0; i < tcs->filter_count; i++) |
| 1288 | { |
| 1289 | if (tcs->filter_tag[i] == clause->tag) |
| 1290 | return true; |
| 1291 | } |
| 1292 | |
Miika Pekkarinen | 53e921c | 2006-04-08 08:03:51 +0000 | [diff] [blame] | 1293 | if (!tagcache_is_numeric_tag(clause->tag) && tcs->idxfd[clause->tag] < 0) |
| 1294 | { |
| 1295 | char buf[MAX_PATH]; |
| 1296 | |
| 1297 | snprintf(buf, sizeof buf, TAGCACHE_FILE_INDEX, clause->tag); |
| 1298 | tcs->idxfd[clause->tag] = open(buf, O_RDONLY); |
| 1299 | } |
Miika Pekkarinen | 4c6fd0f | 2006-04-03 18:57:34 +0000 | [diff] [blame] | 1300 | |
| 1301 | tcs->clause[tcs->clause_count] = clause; |
| 1302 | tcs->clause_count++; |
| 1303 | |
| 1304 | return true; |
| 1305 | } |
| 1306 | |
Miika Pekkarinen | 784112a | 2008-01-13 19:34:49 +0000 | [diff] [blame] | 1307 | /* TODO: Remove this mess. */ |
| 1308 | #ifdef HAVE_DIRCACHE |
Miika Pekkarinen | 9cd5c3e | 2006-07-10 16:22:03 +0000 | [diff] [blame] | 1309 | #define TAG_FILENAME_RAM(tcs) ((tcs->type == tag_filename) \ |
Miika Pekkarinen | 39c597b | 2008-01-13 19:13:37 +0000 | [diff] [blame] | 1310 | ? ((flag & FLAG_DIRCACHE) && is_dircache_intact()) : 1) |
Miika Pekkarinen | 784112a | 2008-01-13 19:34:49 +0000 | [diff] [blame] | 1311 | #else |
| 1312 | #define TAG_FILENAME_RAM(tcs) (tcs->type != tag_filename) |
| 1313 | #endif |
Miika Pekkarinen | 9cd5c3e | 2006-07-10 16:22:03 +0000 | [diff] [blame] | 1314 | |
| 1315 | static bool get_next(struct tagcache_search *tcs) |
Miika Pekkarinen | 7c4e0c8 | 2006-03-26 11:33:42 +0000 | [diff] [blame] | 1316 | { |
Miika Pekkarinen | 0dd7ea2 | 2006-11-10 08:03:33 +0000 | [diff] [blame] | 1317 | static char buf[TAG_MAXLEN+32]; |
Miika Pekkarinen | 7c4e0c8 | 2006-03-26 11:33:42 +0000 | [diff] [blame] | 1318 | struct tagfile_entry entry; |
Miika Pekkarinen | 4294615 | 2006-08-30 18:18:37 +0000 | [diff] [blame] | 1319 | long flag = 0; |
Miika Pekkarinen | 7c4e0c8 | 2006-03-26 11:33:42 +0000 | [diff] [blame] | 1320 | |
Miika Pekkarinen | 0dd7ea2 | 2006-11-10 08:03:33 +0000 | [diff] [blame] | 1321 | if (!tcs->valid || !tc_stat.ready) |
Miika Pekkarinen | 7c4e0c8 | 2006-03-26 11:33:42 +0000 | [diff] [blame] | 1322 | return false; |
| 1323 | |
Miika Pekkarinen | 9cd5c3e | 2006-07-10 16:22:03 +0000 | [diff] [blame] | 1324 | if (tcs->idxfd[tcs->type] < 0 && !tagcache_is_numeric_tag(tcs->type) |
Miika Pekkarinen | 7c4e0c8 | 2006-03-26 11:33:42 +0000 | [diff] [blame] | 1325 | #ifdef HAVE_TC_RAMCACHE |
| 1326 | && !tcs->ramsearch |
| 1327 | #endif |
| 1328 | ) |
| 1329 | return false; |
| 1330 | |
| 1331 | /* Relative fetch. */ |
Miika Pekkarinen | 93bbd44 | 2006-08-25 21:13:49 +0000 | [diff] [blame] | 1332 | if (tcs->filter_count > 0 || tcs->clause_count > 0 |
| 1333 | || tagcache_is_numeric_tag(tcs->type)) |
Miika Pekkarinen | 7c4e0c8 | 2006-03-26 11:33:42 +0000 | [diff] [blame] | 1334 | { |
| 1335 | /* Check for end of list. */ |
| 1336 | if (tcs->seek_list_count == 0) |
| 1337 | { |
| 1338 | /* Try to fetch more. */ |
| 1339 | if (!build_lookup_list(tcs)) |
Miika Pekkarinen | 93bbd44 | 2006-08-25 21:13:49 +0000 | [diff] [blame] | 1340 | { |
| 1341 | tcs->valid = false; |
Miika Pekkarinen | 7c4e0c8 | 2006-03-26 11:33:42 +0000 | [diff] [blame] | 1342 | return false; |
Miika Pekkarinen | 93bbd44 | 2006-08-25 21:13:49 +0000 | [diff] [blame] | 1343 | } |
Miika Pekkarinen | 7c4e0c8 | 2006-03-26 11:33:42 +0000 | [diff] [blame] | 1344 | } |
| 1345 | |
| 1346 | tcs->seek_list_count--; |
Miika Pekkarinen | 4294615 | 2006-08-30 18:18:37 +0000 | [diff] [blame] | 1347 | flag = tcs->seek_flags[tcs->seek_list_count]; |
Miika Pekkarinen | 7c4e0c8 | 2006-03-26 11:33:42 +0000 | [diff] [blame] | 1348 | |
| 1349 | /* Seek stream to the correct position and continue to direct fetch. */ |
Miika Pekkarinen | 93bbd44 | 2006-08-25 21:13:49 +0000 | [diff] [blame] | 1350 | if ((!tcs->ramsearch || !TAG_FILENAME_RAM(tcs)) |
| 1351 | && !tagcache_is_numeric_tag(tcs->type)) |
Miika Pekkarinen | 9cd5c3e | 2006-07-10 16:22:03 +0000 | [diff] [blame] | 1352 | { |
Miika Pekkarinen | b89b5ba | 2006-10-15 11:01:18 +0000 | [diff] [blame] | 1353 | if (!open_files(tcs, tcs->type)) |
Miika Pekkarinen | 9cd5c3e | 2006-07-10 16:22:03 +0000 | [diff] [blame] | 1354 | return false; |
| 1355 | |
| 1356 | lseek(tcs->idxfd[tcs->type], tcs->seek_list[tcs->seek_list_count], SEEK_SET); |
| 1357 | } |
Miika Pekkarinen | 7c4e0c8 | 2006-03-26 11:33:42 +0000 | [diff] [blame] | 1358 | else |
| 1359 | tcs->position = tcs->seek_list[tcs->seek_list_count]; |
| 1360 | } |
| 1361 | |
Miika Pekkarinen | 93bbd44 | 2006-08-25 21:13:49 +0000 | [diff] [blame] | 1362 | if (tagcache_is_numeric_tag(tcs->type)) |
| 1363 | { |
Miika Pekkarinen | 93bbd44 | 2006-08-25 21:13:49 +0000 | [diff] [blame] | 1364 | snprintf(buf, sizeof(buf), "%d", tcs->position); |
| 1365 | tcs->result_seek = tcs->position; |
| 1366 | tcs->result = buf; |
| 1367 | tcs->result_len = strlen(buf) + 1; |
| 1368 | return true; |
| 1369 | } |
| 1370 | |
Miika Pekkarinen | 7c4e0c8 | 2006-03-26 11:33:42 +0000 | [diff] [blame] | 1371 | /* Direct fetch. */ |
| 1372 | #ifdef HAVE_TC_RAMCACHE |
Miika Pekkarinen | 9cd5c3e | 2006-07-10 16:22:03 +0000 | [diff] [blame] | 1373 | if (tcs->ramsearch && TAG_FILENAME_RAM(tcs)) |
Miika Pekkarinen | 7c4e0c8 | 2006-03-26 11:33:42 +0000 | [diff] [blame] | 1374 | { |
| 1375 | struct tagfile_entry *ep; |
| 1376 | |
| 1377 | if (tcs->entry_count == 0) |
| 1378 | { |
| 1379 | tcs->valid = false; |
| 1380 | return false; |
| 1381 | } |
| 1382 | tcs->entry_count--; |
Miika Pekkarinen | 7c4e0c8 | 2006-03-26 11:33:42 +0000 | [diff] [blame] | 1383 | |
Miika Pekkarinen | 4294615 | 2006-08-30 18:18:37 +0000 | [diff] [blame] | 1384 | tcs->result_seek = tcs->position; |
Miika Pekkarinen | 9812765 | 2006-08-27 14:58:46 +0000 | [diff] [blame] | 1385 | |
Miika Pekkarinen | 9cd5c3e | 2006-07-10 16:22:03 +0000 | [diff] [blame] | 1386 | # ifdef HAVE_DIRCACHE |
Miika Pekkarinen | 7c4e0c8 | 2006-03-26 11:33:42 +0000 | [diff] [blame] | 1387 | if (tcs->type == tag_filename) |
| 1388 | { |
Kevin Ferrare | 011a325 | 2007-07-20 17:06:55 +0000 | [diff] [blame] | 1389 | dircache_copy_path((struct dirent *)tcs->position, |
Miika Pekkarinen | 9cd5c3e | 2006-07-10 16:22:03 +0000 | [diff] [blame] | 1390 | buf, sizeof buf); |
Miika Pekkarinen | 7c4e0c8 | 2006-03-26 11:33:42 +0000 | [diff] [blame] | 1391 | tcs->result = buf; |
| 1392 | tcs->result_len = strlen(buf) + 1; |
Miika Pekkarinen | 4294615 | 2006-08-30 18:18:37 +0000 | [diff] [blame] | 1393 | tcs->idx_id = FLAG_GET_ATTR(flag); |
| 1394 | tcs->ramresult = false; |
Miika Pekkarinen | 7c4e0c8 | 2006-03-26 11:33:42 +0000 | [diff] [blame] | 1395 | |
| 1396 | return true; |
| 1397 | } |
Miika Pekkarinen | 9cd5c3e | 2006-07-10 16:22:03 +0000 | [diff] [blame] | 1398 | # endif |
Miika Pekkarinen | 7c4e0c8 | 2006-03-26 11:33:42 +0000 | [diff] [blame] | 1399 | |
| 1400 | ep = (struct tagfile_entry *)&hdr->tags[tcs->type][tcs->position]; |
| 1401 | tcs->position += sizeof(struct tagfile_entry) + ep->tag_length; |
| 1402 | tcs->result = ep->tag_data; |
Miika Pekkarinen | 9cd5c3e | 2006-07-10 16:22:03 +0000 | [diff] [blame] | 1403 | tcs->result_len = strlen(tcs->result) + 1; |
Miika Pekkarinen | 44b76bc | 2006-03-30 12:07:32 +0000 | [diff] [blame] | 1404 | tcs->idx_id = ep->idx_id; |
Miika Pekkarinen | 4294615 | 2006-08-30 18:18:37 +0000 | [diff] [blame] | 1405 | tcs->ramresult = true; |
Miika Pekkarinen | 58fe4de | 2006-04-10 10:26:24 +0000 | [diff] [blame] | 1406 | |
Miika Pekkarinen | 7c4e0c8 | 2006-03-26 11:33:42 +0000 | [diff] [blame] | 1407 | return true; |
| 1408 | } |
| 1409 | else |
| 1410 | #endif |
| 1411 | { |
Miika Pekkarinen | b89b5ba | 2006-10-15 11:01:18 +0000 | [diff] [blame] | 1412 | if (!open_files(tcs, tcs->type)) |
Miika Pekkarinen | 9cd5c3e | 2006-07-10 16:22:03 +0000 | [diff] [blame] | 1413 | return false; |
| 1414 | |
| 1415 | tcs->result_seek = lseek(tcs->idxfd[tcs->type], 0, SEEK_CUR); |
Miika Pekkarinen | 9b9539c | 2007-02-13 21:51:18 +0000 | [diff] [blame] | 1416 | if (ecread(tcs->idxfd[tcs->type], &entry, 1, |
| 1417 | tagfile_entry_ec, tc_stat.econ) != sizeof(struct tagfile_entry)) |
Miika Pekkarinen | 7c4e0c8 | 2006-03-26 11:33:42 +0000 | [diff] [blame] | 1418 | { |
| 1419 | /* End of data. */ |
| 1420 | tcs->valid = false; |
| 1421 | return false; |
| 1422 | } |
| 1423 | } |
| 1424 | |
| 1425 | if (entry.tag_length > (long)sizeof(buf)) |
| 1426 | { |
| 1427 | tcs->valid = false; |
Miika Pekkarinen | 9cd5c3e | 2006-07-10 16:22:03 +0000 | [diff] [blame] | 1428 | logf("too long tag #2"); |
Miika Pekkarinen | 7c4e0c8 | 2006-03-26 11:33:42 +0000 | [diff] [blame] | 1429 | return false; |
| 1430 | } |
| 1431 | |
Miika Pekkarinen | 9cd5c3e | 2006-07-10 16:22:03 +0000 | [diff] [blame] | 1432 | if (read(tcs->idxfd[tcs->type], buf, entry.tag_length) != entry.tag_length) |
Miika Pekkarinen | 7c4e0c8 | 2006-03-26 11:33:42 +0000 | [diff] [blame] | 1433 | { |
| 1434 | tcs->valid = false; |
Miika Pekkarinen | 36e3081 | 2006-09-26 11:23:18 +0000 | [diff] [blame] | 1435 | logf("read error #4"); |
Miika Pekkarinen | 7c4e0c8 | 2006-03-26 11:33:42 +0000 | [diff] [blame] | 1436 | return false; |
| 1437 | } |
| 1438 | |
| 1439 | tcs->result = buf; |
Miika Pekkarinen | 9cd5c3e | 2006-07-10 16:22:03 +0000 | [diff] [blame] | 1440 | tcs->result_len = strlen(tcs->result) + 1; |
Miika Pekkarinen | 44b76bc | 2006-03-30 12:07:32 +0000 | [diff] [blame] | 1441 | tcs->idx_id = entry.idx_id; |
Miika Pekkarinen | 4294615 | 2006-08-30 18:18:37 +0000 | [diff] [blame] | 1442 | tcs->ramresult = false; |
Miika Pekkarinen | 58fe4de | 2006-04-10 10:26:24 +0000 | [diff] [blame] | 1443 | |
| 1444 | return true; |
| 1445 | } |
| 1446 | |
Miika Pekkarinen | 9cd5c3e | 2006-07-10 16:22:03 +0000 | [diff] [blame] | 1447 | bool tagcache_get_next(struct tagcache_search *tcs) |
| 1448 | { |
| 1449 | while (get_next(tcs)) |
| 1450 | { |
| 1451 | if (tcs->result_len > 1) |
| 1452 | return true; |
| 1453 | } |
| 1454 | |
| 1455 | return false; |
| 1456 | } |
| 1457 | |
Miika Pekkarinen | 58fe4de | 2006-04-10 10:26:24 +0000 | [diff] [blame] | 1458 | bool tagcache_retrieve(struct tagcache_search *tcs, int idxid, |
Miika Pekkarinen | 02df5a8 | 2006-10-24 16:20:48 +0000 | [diff] [blame] | 1459 | int tag, char *buf, long size) |
Miika Pekkarinen | 58fe4de | 2006-04-10 10:26:24 +0000 | [diff] [blame] | 1460 | { |
Miika Pekkarinen | 45dfe2a | 2006-07-15 17:36:25 +0000 | [diff] [blame] | 1461 | struct index_entry idx; |
Miika Pekkarinen | 58fe4de | 2006-04-10 10:26:24 +0000 | [diff] [blame] | 1462 | |
Miika Pekkarinen | 6ee82e7 | 2006-09-23 10:29:14 +0000 | [diff] [blame] | 1463 | *buf = '\0'; |
Miika Pekkarinen | dafa0d4 | 2006-07-24 15:35:46 +0000 | [diff] [blame] | 1464 | if (!get_index(tcs->masterfd, idxid, &idx, true)) |
Miika Pekkarinen | 45dfe2a | 2006-07-15 17:36:25 +0000 | [diff] [blame] | 1465 | return false; |
| 1466 | |
Miika Pekkarinen | 02df5a8 | 2006-10-24 16:20:48 +0000 | [diff] [blame] | 1467 | return retrieve(tcs, &idx, tag, buf, size); |
Miika Pekkarinen | 7c4e0c8 | 2006-03-26 11:33:42 +0000 | [diff] [blame] | 1468 | } |
| 1469 | |
Miika Pekkarinen | f5184f3 | 2007-02-25 20:41:51 +0000 | [diff] [blame] | 1470 | static bool update_master_header(void) |
| 1471 | { |
| 1472 | struct master_header myhdr; |
| 1473 | int fd; |
| 1474 | |
| 1475 | if (!tc_stat.ready) |
| 1476 | return false; |
| 1477 | |
| 1478 | if ( (fd = open_master_fd(&myhdr, true)) < 0) |
| 1479 | return false; |
| 1480 | |
| 1481 | myhdr.serial = current_tcmh.serial; |
| 1482 | myhdr.commitid = current_tcmh.commitid; |
| 1483 | |
| 1484 | /* Write it back */ |
| 1485 | lseek(fd, 0, SEEK_SET); |
| 1486 | ecwrite(fd, &myhdr, 1, master_header_ec, tc_stat.econ); |
| 1487 | close(fd); |
| 1488 | |
| 1489 | #ifdef HAVE_TC_RAMCACHE |
| 1490 | if (hdr) |
| 1491 | { |
| 1492 | hdr->h.serial = current_tcmh.serial; |
| 1493 | hdr->h.commitid = current_tcmh.commitid; |
| 1494 | } |
| 1495 | #endif |
| 1496 | |
| 1497 | return true; |
| 1498 | } |
| 1499 | |
Miika Pekkarinen | 7c4e0c8 | 2006-03-26 11:33:42 +0000 | [diff] [blame] | 1500 | #if 0 |
Miika Pekkarinen | fa893c6 | 2006-04-18 18:56:56 +0000 | [diff] [blame] | 1501 | |
Miika Pekkarinen | 7c4e0c8 | 2006-03-26 11:33:42 +0000 | [diff] [blame] | 1502 | void tagcache_modify(struct tagcache_search *tcs, int type, const char *text) |
| 1503 | { |
| 1504 | struct tagentry *entry; |
| 1505 | |
| 1506 | if (tcs->type != tag_title) |
| 1507 | return ; |
| 1508 | |
| 1509 | /* We will need reserve buffer for this. */ |
| 1510 | if (tcs->ramcache) |
| 1511 | { |
| 1512 | struct tagfile_entry *ep; |
| 1513 | |
| 1514 | ep = (struct tagfile_entry *)&hdr->tags[tcs->type][tcs->result_seek]; |
| 1515 | tcs->seek_list[tcs->seek_list_count]; |
| 1516 | } |
| 1517 | |
Miika Pekkarinen | 9cd5c3e | 2006-07-10 16:22:03 +0000 | [diff] [blame] | 1518 | entry = find_entry_ram(); |
Miika Pekkarinen | 7c4e0c8 | 2006-03-26 11:33:42 +0000 | [diff] [blame] | 1519 | |
| 1520 | } |
| 1521 | #endif |
| 1522 | |
| 1523 | void tagcache_search_finish(struct tagcache_search *tcs) |
| 1524 | { |
Miika Pekkarinen | 53e921c | 2006-04-08 08:03:51 +0000 | [diff] [blame] | 1525 | int i; |
| 1526 | |
Miika Pekkarinen | d8ac607 | 2006-08-02 17:39:34 +0000 | [diff] [blame] | 1527 | if (!tcs->initialized) |
| 1528 | return; |
| 1529 | |
Miika Pekkarinen | 4c6fd0f | 2006-04-03 18:57:34 +0000 | [diff] [blame] | 1530 | if (tcs->masterfd >= 0) |
| 1531 | { |
| 1532 | close(tcs->masterfd); |
| 1533 | tcs->masterfd = -1; |
| 1534 | } |
Miika Pekkarinen | 53e921c | 2006-04-08 08:03:51 +0000 | [diff] [blame] | 1535 | |
| 1536 | for (i = 0; i < TAG_COUNT; i++) |
| 1537 | { |
| 1538 | if (tcs->idxfd[i] >= 0) |
| 1539 | { |
| 1540 | close(tcs->idxfd[i]); |
| 1541 | tcs->idxfd[i] = -1; |
| 1542 | } |
| 1543 | } |
Miika Pekkarinen | 4c6fd0f | 2006-04-03 18:57:34 +0000 | [diff] [blame] | 1544 | |
| 1545 | tcs->ramsearch = false; |
| 1546 | tcs->valid = false; |
Miika Pekkarinen | d8ac607 | 2006-08-02 17:39:34 +0000 | [diff] [blame] | 1547 | tcs->initialized = 0; |
| 1548 | if (write_lock > 0) |
| 1549 | write_lock--; |
Miika Pekkarinen | 7c4e0c8 | 2006-03-26 11:33:42 +0000 | [diff] [blame] | 1550 | } |
| 1551 | |
Miika Pekkarinen | 9cd5c3e | 2006-07-10 16:22:03 +0000 | [diff] [blame] | 1552 | #if defined(HAVE_TC_RAMCACHE) && defined(HAVE_DIRCACHE) |
Miika Pekkarinen | 70c0791 | 2006-04-03 06:00:56 +0000 | [diff] [blame] | 1553 | static struct tagfile_entry *get_tag(const struct index_entry *entry, int tag) |
Miika Pekkarinen | 7c4e0c8 | 2006-03-26 11:33:42 +0000 | [diff] [blame] | 1554 | { |
| 1555 | return (struct tagfile_entry *)&hdr->tags[tag][entry->tag_seek[tag]]; |
| 1556 | } |
| 1557 | |
Miika Pekkarinen | 70c0791 | 2006-04-03 06:00:56 +0000 | [diff] [blame] | 1558 | static long get_tag_numeric(const struct index_entry *entry, int tag) |
| 1559 | { |
Miika Pekkarinen | e461f2e | 2007-09-15 22:45:08 +0000 | [diff] [blame] | 1560 | return check_virtual_tags(tag, entry); |
Miika Pekkarinen | 70c0791 | 2006-04-03 06:00:56 +0000 | [diff] [blame] | 1561 | } |
Miika Pekkarinen | 9cd5c3e | 2006-07-10 16:22:03 +0000 | [diff] [blame] | 1562 | |
Magnus Holmgren | 74e572c | 2007-03-04 19:54:49 +0000 | [diff] [blame] | 1563 | static char* get_tag_string(const struct index_entry *entry, int tag) |
| 1564 | { |
| 1565 | char* s = get_tag(entry, tag)->tag_data; |
| 1566 | return strcmp(s, UNTAGGED) ? s : NULL; |
| 1567 | } |
| 1568 | |
Miika Pekkarinen | 7c4e0c8 | 2006-03-26 11:33:42 +0000 | [diff] [blame] | 1569 | bool tagcache_fill_tags(struct mp3entry *id3, const char *filename) |
| 1570 | { |
| 1571 | struct index_entry *entry; |
Miika Pekkarinen | 45dfe2a | 2006-07-15 17:36:25 +0000 | [diff] [blame] | 1572 | int idx_id; |
Miika Pekkarinen | 7c4e0c8 | 2006-03-26 11:33:42 +0000 | [diff] [blame] | 1573 | |
Miika Pekkarinen | 0dd7ea2 | 2006-11-10 08:03:33 +0000 | [diff] [blame] | 1574 | if (!tc_stat.ready) |
Miika Pekkarinen | 29fa15f | 2006-07-23 11:15:28 +0000 | [diff] [blame] | 1575 | return false; |
| 1576 | |
Miika Pekkarinen | 7c4e0c8 | 2006-03-26 11:33:42 +0000 | [diff] [blame] | 1577 | /* Find the corresponding entry in tagcache. */ |
Miika Pekkarinen | 45dfe2a | 2006-07-15 17:36:25 +0000 | [diff] [blame] | 1578 | idx_id = find_entry_ram(filename, NULL); |
Miika Pekkarinen | 0dd7ea2 | 2006-11-10 08:03:33 +0000 | [diff] [blame] | 1579 | if (idx_id < 0 || !tc_stat.ramcache) |
Miika Pekkarinen | 7c4e0c8 | 2006-03-26 11:33:42 +0000 | [diff] [blame] | 1580 | return false; |
| 1581 | |
Miika Pekkarinen | 45dfe2a | 2006-07-15 17:36:25 +0000 | [diff] [blame] | 1582 | entry = &hdr->indices[idx_id]; |
| 1583 | |
Magnus Holmgren | 74e572c | 2007-03-04 19:54:49 +0000 | [diff] [blame] | 1584 | id3->title = get_tag_string(entry, tag_title); |
| 1585 | id3->artist = get_tag_string(entry, tag_artist); |
| 1586 | id3->album = get_tag_string(entry, tag_album); |
| 1587 | id3->genre_string = get_tag_string(entry, tag_genre); |
| 1588 | id3->composer = get_tag_string(entry, tag_composer); |
| 1589 | id3->comment = get_tag_string(entry, tag_comment); |
| 1590 | id3->albumartist = get_tag_string(entry, tag_albumartist); |
Dan Everton | eecfe9f | 2007-08-08 10:19:56 +0000 | [diff] [blame] | 1591 | id3->grouping = get_tag_string(entry, tag_grouping); |
Robert Kukla | 28530bc | 2007-03-04 17:30:12 +0000 | [diff] [blame] | 1592 | |
| 1593 | id3->playcount = get_tag_numeric(entry, tag_playcount); |
Robert Kukla | 226cb7b | 2007-03-26 15:08:59 +0000 | [diff] [blame] | 1594 | id3->rating = get_tag_numeric(entry, tag_rating); |
Robert Kukla | 28530bc | 2007-03-04 17:30:12 +0000 | [diff] [blame] | 1595 | id3->lastplayed = get_tag_numeric(entry, tag_lastplayed); |
Robert Kukla | 226cb7b | 2007-03-26 15:08:59 +0000 | [diff] [blame] | 1596 | id3->score = get_tag_numeric(entry, tag_virt_autoscore) / 10; |
Robert Kukla | 28530bc | 2007-03-04 17:30:12 +0000 | [diff] [blame] | 1597 | id3->year = get_tag_numeric(entry, tag_year); |
| 1598 | |
Dan Everton | f4a61f0 | 2007-08-03 10:00:42 +0000 | [diff] [blame] | 1599 | id3->discnum = get_tag_numeric(entry, tag_discnumber); |
Miika Pekkarinen | 70c0791 | 2006-04-03 06:00:56 +0000 | [diff] [blame] | 1600 | id3->tracknum = get_tag_numeric(entry, tag_tracknumber); |
| 1601 | id3->bitrate = get_tag_numeric(entry, tag_bitrate); |
| 1602 | if (id3->bitrate == 0) |
| 1603 | id3->bitrate = 1; |
Miika Pekkarinen | 7c4e0c8 | 2006-03-26 11:33:42 +0000 | [diff] [blame] | 1604 | |
| 1605 | return true; |
| 1606 | } |
| 1607 | #endif |
| 1608 | |
| 1609 | static inline void write_item(const char *item) |
| 1610 | { |
| 1611 | int len = strlen(item) + 1; |
Peter D'Hoye | b4f80fb | 2008-01-25 21:21:41 +0000 | [diff] [blame] | 1612 | |
Miika Pekkarinen | 7c4e0c8 | 2006-03-26 11:33:42 +0000 | [diff] [blame] | 1613 | data_size += len; |
| 1614 | write(cachefd, item, len); |
| 1615 | } |
| 1616 | |
Miika Pekkarinen | 812cbad | 2006-10-04 09:05:01 +0000 | [diff] [blame] | 1617 | static int check_if_empty(char **tag) |
Miika Pekkarinen | 7c4e0c8 | 2006-03-26 11:33:42 +0000 | [diff] [blame] | 1618 | { |
Miika Pekkarinen | 812cbad | 2006-10-04 09:05:01 +0000 | [diff] [blame] | 1619 | int length; |
Peter D'Hoye | b4f80fb | 2008-01-25 21:21:41 +0000 | [diff] [blame] | 1620 | |
| 1621 | if (*tag == NULL || **tag == '\0') |
Miika Pekkarinen | 812cbad | 2006-10-04 09:05:01 +0000 | [diff] [blame] | 1622 | { |
Robert Kukla | 28530bc | 2007-03-04 17:30:12 +0000 | [diff] [blame] | 1623 | *tag = UNTAGGED; |
Linus Nielsen Feltzing | 4162186 | 2007-03-04 19:30:03 +0000 | [diff] [blame] | 1624 | return sizeof(UNTAGGED); /* Tag length */ |
Miika Pekkarinen | 812cbad | 2006-10-04 09:05:01 +0000 | [diff] [blame] | 1625 | } |
Peter D'Hoye | b4f80fb | 2008-01-25 21:21:41 +0000 | [diff] [blame] | 1626 | |
Miika Pekkarinen | 812cbad | 2006-10-04 09:05:01 +0000 | [diff] [blame] | 1627 | length = strlen(*tag); |
Miika Pekkarinen | 0dd7ea2 | 2006-11-10 08:03:33 +0000 | [diff] [blame] | 1628 | if (length > TAG_MAXLEN) |
Miika Pekkarinen | 812cbad | 2006-10-04 09:05:01 +0000 | [diff] [blame] | 1629 | { |
| 1630 | logf("over length tag: %s", *tag); |
Miika Pekkarinen | 0dd7ea2 | 2006-11-10 08:03:33 +0000 | [diff] [blame] | 1631 | length = TAG_MAXLEN; |
Peter D'Hoye | b4f80fb | 2008-01-25 21:21:41 +0000 | [diff] [blame] | 1632 | (*tag)[length] = '\0'; |
Miika Pekkarinen | 812cbad | 2006-10-04 09:05:01 +0000 | [diff] [blame] | 1633 | } |
Peter D'Hoye | b4f80fb | 2008-01-25 21:21:41 +0000 | [diff] [blame] | 1634 | |
Miika Pekkarinen | 812cbad | 2006-10-04 09:05:01 +0000 | [diff] [blame] | 1635 | return length + 1; |
Miika Pekkarinen | 7c4e0c8 | 2006-03-26 11:33:42 +0000 | [diff] [blame] | 1636 | } |
| 1637 | |
Miika Pekkarinen | 812cbad | 2006-10-04 09:05:01 +0000 | [diff] [blame] | 1638 | #define ADD_TAG(entry,tag,data) \ |
| 1639 | /* Adding tag */ \ |
| 1640 | entry.tag_offset[tag] = offset; \ |
| 1641 | entry.tag_length[tag] = check_if_empty(data); \ |
| 1642 | offset += entry.tag_length[tag] |
| 1643 | |
Miika Pekkarinen | f603946 | 2007-12-16 21:10:26 +0000 | [diff] [blame] | 1644 | static void add_tagcache(char *path, unsigned long mtime |
Miika Pekkarinen | 9cd5c3e | 2006-07-10 16:22:03 +0000 | [diff] [blame] | 1645 | #if defined(HAVE_TC_RAMCACHE) && defined(HAVE_DIRCACHE) |
Miika Pekkarinen | f603946 | 2007-12-16 21:10:26 +0000 | [diff] [blame] | 1646 | ,const struct dirent *dc |
Miika Pekkarinen | 7c4e0c8 | 2006-03-26 11:33:42 +0000 | [diff] [blame] | 1647 | #endif |
Miika Pekkarinen | f603946 | 2007-12-16 21:10:26 +0000 | [diff] [blame] | 1648 | ) |
Miika Pekkarinen | 7c4e0c8 | 2006-03-26 11:33:42 +0000 | [diff] [blame] | 1649 | { |
Nicolas Pennequin | 67f5082 | 2007-10-01 20:06:21 +0000 | [diff] [blame] | 1650 | struct mp3entry id3; |
Miika Pekkarinen | 7c4e0c8 | 2006-03-26 11:33:42 +0000 | [diff] [blame] | 1651 | struct temp_file_entry entry; |
| 1652 | bool ret; |
| 1653 | int fd; |
Miika Pekkarinen | f603946 | 2007-12-16 21:10:26 +0000 | [diff] [blame] | 1654 | int idx_id = -1; |
Miika Pekkarinen | 44b76bc | 2006-03-30 12:07:32 +0000 | [diff] [blame] | 1655 | char tracknumfix[3]; |
Miika Pekkarinen | 812cbad | 2006-10-04 09:05:01 +0000 | [diff] [blame] | 1656 | int offset = 0; |
Miika Pekkarinen | 0dd7ea2 | 2006-11-10 08:03:33 +0000 | [diff] [blame] | 1657 | int path_length = strlen(path); |
Dan Everton | 00dd149 | 2007-06-25 11:33:21 +0000 | [diff] [blame] | 1658 | bool has_albumartist; |
Dan Everton | eecfe9f | 2007-08-08 10:19:56 +0000 | [diff] [blame] | 1659 | bool has_grouping; |
Miika Pekkarinen | 7c4e0c8 | 2006-03-26 11:33:42 +0000 | [diff] [blame] | 1660 | |
| 1661 | if (cachefd < 0) |
| 1662 | return ; |
| 1663 | |
Miika Pekkarinen | 0dd7ea2 | 2006-11-10 08:03:33 +0000 | [diff] [blame] | 1664 | /* Check for overlength file path. */ |
| 1665 | if (path_length > TAG_MAXLEN) |
| 1666 | { |
| 1667 | /* Path can't be shortened. */ |
Magnus Holmgren | 01a010f | 2007-03-18 09:50:53 +0000 | [diff] [blame] | 1668 | logf("Too long path: %s", path); |
Miika Pekkarinen | 0dd7ea2 | 2006-11-10 08:03:33 +0000 | [diff] [blame] | 1669 | return ; |
| 1670 | } |
| 1671 | |
Miika Pekkarinen | 7c4e0c8 | 2006-03-26 11:33:42 +0000 | [diff] [blame] | 1672 | /* Check if the file is supported. */ |
| 1673 | if (probe_file_format(path) == AFMT_UNKNOWN) |
| 1674 | return ; |
| 1675 | |
| 1676 | /* Check if the file is already cached. */ |
Miika Pekkarinen | 9cd5c3e | 2006-07-10 16:22:03 +0000 | [diff] [blame] | 1677 | #if defined(HAVE_TC_RAMCACHE) && defined(HAVE_DIRCACHE) |
Miika Pekkarinen | 39c597b | 2008-01-13 19:13:37 +0000 | [diff] [blame] | 1678 | if (tc_stat.ramcache && is_dircache_intact()) |
Miika Pekkarinen | 7c4e0c8 | 2006-03-26 11:33:42 +0000 | [diff] [blame] | 1679 | { |
Miika Pekkarinen | f603946 | 2007-12-16 21:10:26 +0000 | [diff] [blame] | 1680 | idx_id = find_entry_ram(path, dc); |
Miika Pekkarinen | 7c4e0c8 | 2006-03-26 11:33:42 +0000 | [diff] [blame] | 1681 | } |
| 1682 | else |
| 1683 | #endif |
| 1684 | { |
Miika Pekkarinen | 29fa15f | 2006-07-23 11:15:28 +0000 | [diff] [blame] | 1685 | if (filenametag_fd >= 0) |
| 1686 | { |
Miika Pekkarinen | f603946 | 2007-12-16 21:10:26 +0000 | [diff] [blame] | 1687 | idx_id = find_entry_disk(path); |
| 1688 | } |
| 1689 | } |
| 1690 | |
| 1691 | /* Check if file has been modified. */ |
| 1692 | if (idx_id >= 0) |
| 1693 | { |
| 1694 | struct index_entry idx; |
| 1695 | |
| 1696 | if (!get_index(-1, idx_id, &idx, true)) |
| 1697 | { |
| 1698 | logf("failed to retrieve index entry"); |
| 1699 | return ; |
| 1700 | } |
| 1701 | |
| 1702 | if ((unsigned long)idx.tag_seek[tag_mtime] == mtime) |
| 1703 | { |
| 1704 | /* No changes to file. */ |
| 1705 | return ; |
| 1706 | } |
| 1707 | |
| 1708 | /* Metadata might have been changed. Delete the entry. */ |
| 1709 | logf("Re-adding: %s", path); |
| 1710 | if (!delete_entry(idx_id)) |
| 1711 | { |
| 1712 | logf("delete_entry failed: %d", idx_id); |
| 1713 | return ; |
Miika Pekkarinen | 29fa15f | 2006-07-23 11:15:28 +0000 | [diff] [blame] | 1714 | } |
Miika Pekkarinen | 7c4e0c8 | 2006-03-26 11:33:42 +0000 | [diff] [blame] | 1715 | } |
| 1716 | |
| 1717 | fd = open(path, O_RDONLY); |
| 1718 | if (fd < 0) |
| 1719 | { |
| 1720 | logf("open fail: %s", path); |
| 1721 | return ; |
| 1722 | } |
| 1723 | |
Nicolas Pennequin | 67f5082 | 2007-10-01 20:06:21 +0000 | [diff] [blame] | 1724 | memset(&id3, 0, sizeof(struct mp3entry)); |
Miika Pekkarinen | 44b76bc | 2006-03-30 12:07:32 +0000 | [diff] [blame] | 1725 | memset(&entry, 0, sizeof(struct temp_file_entry)); |
| 1726 | memset(&tracknumfix, 0, sizeof(tracknumfix)); |
Nicolas Pennequin | 67f5082 | 2007-10-01 20:06:21 +0000 | [diff] [blame] | 1727 | ret = get_metadata(&id3, fd, path); |
Miika Pekkarinen | 7c4e0c8 | 2006-03-26 11:33:42 +0000 | [diff] [blame] | 1728 | close(fd); |
| 1729 | |
| 1730 | if (!ret) |
Miika Pekkarinen | 3f3aeb0 | 2006-03-27 10:46:09 +0000 | [diff] [blame] | 1731 | return ; |
| 1732 | |
Miika Pekkarinen | 6ee82e7 | 2006-09-23 10:29:14 +0000 | [diff] [blame] | 1733 | logf("-> %s", path); |
Miika Pekkarinen | 9c0b54a | 2006-07-24 12:10:50 +0000 | [diff] [blame] | 1734 | |
Miika Pekkarinen | 812cbad | 2006-10-04 09:05:01 +0000 | [diff] [blame] | 1735 | /* Generate track number if missing. */ |
Nicolas Pennequin | 67f5082 | 2007-10-01 20:06:21 +0000 | [diff] [blame] | 1736 | if (id3.tracknum <= 0) |
Miika Pekkarinen | 44b76bc | 2006-03-30 12:07:32 +0000 | [diff] [blame] | 1737 | { |
Miika Pekkarinen | 35d213a | 2006-04-05 18:46:38 +0000 | [diff] [blame] | 1738 | const char *p = strrchr(path, '.'); |
Miika Pekkarinen | 44b76bc | 2006-03-30 12:07:32 +0000 | [diff] [blame] | 1739 | |
Miika Pekkarinen | 35d213a | 2006-04-05 18:46:38 +0000 | [diff] [blame] | 1740 | if (p == NULL) |
| 1741 | p = &path[strlen(path)-1]; |
Miika Pekkarinen | 44b76bc | 2006-03-30 12:07:32 +0000 | [diff] [blame] | 1742 | |
Miika Pekkarinen | 35d213a | 2006-04-05 18:46:38 +0000 | [diff] [blame] | 1743 | while (*p != '/') |
Miika Pekkarinen | 44b76bc | 2006-03-30 12:07:32 +0000 | [diff] [blame] | 1744 | { |
Miika Pekkarinen | 35d213a | 2006-04-05 18:46:38 +0000 | [diff] [blame] | 1745 | if (isdigit(*p) && isdigit(*(p-1))) |
| 1746 | { |
| 1747 | tracknumfix[1] = *p--; |
| 1748 | tracknumfix[0] = *p; |
| 1749 | break; |
| 1750 | } |
| 1751 | p--; |
Miika Pekkarinen | 44b76bc | 2006-03-30 12:07:32 +0000 | [diff] [blame] | 1752 | } |
Miika Pekkarinen | 35d213a | 2006-04-05 18:46:38 +0000 | [diff] [blame] | 1753 | |
Miika Pekkarinen | 44b76bc | 2006-03-30 12:07:32 +0000 | [diff] [blame] | 1754 | if (tracknumfix[0] != '\0') |
Miika Pekkarinen | 812cbad | 2006-10-04 09:05:01 +0000 | [diff] [blame] | 1755 | { |
Nicolas Pennequin | 67f5082 | 2007-10-01 20:06:21 +0000 | [diff] [blame] | 1756 | id3.tracknum = atoi(tracknumfix); |
Miika Pekkarinen | 812cbad | 2006-10-04 09:05:01 +0000 | [diff] [blame] | 1757 | /* Set a flag to indicate track number has been generated. */ |
| 1758 | entry.flag |= FLAG_TRKNUMGEN; |
| 1759 | } |
Miika Pekkarinen | 44b76bc | 2006-03-30 12:07:32 +0000 | [diff] [blame] | 1760 | else |
Miika Pekkarinen | 812cbad | 2006-10-04 09:05:01 +0000 | [diff] [blame] | 1761 | { |
| 1762 | /* Unable to generate track number. */ |
Nicolas Pennequin | 67f5082 | 2007-10-01 20:06:21 +0000 | [diff] [blame] | 1763 | id3.tracknum = -1; |
Miika Pekkarinen | 812cbad | 2006-10-04 09:05:01 +0000 | [diff] [blame] | 1764 | } |
Miika Pekkarinen | 44b76bc | 2006-03-30 12:07:32 +0000 | [diff] [blame] | 1765 | } |
Miika Pekkarinen | 7c4e0c8 | 2006-03-26 11:33:42 +0000 | [diff] [blame] | 1766 | |
Miika Pekkarinen | 812cbad | 2006-10-04 09:05:01 +0000 | [diff] [blame] | 1767 | /* Numeric tags */ |
Nicolas Pennequin | 67f5082 | 2007-10-01 20:06:21 +0000 | [diff] [blame] | 1768 | entry.tag_offset[tag_year] = id3.year; |
| 1769 | entry.tag_offset[tag_discnumber] = id3.discnum; |
| 1770 | entry.tag_offset[tag_tracknumber] = id3.tracknum; |
| 1771 | entry.tag_offset[tag_length] = id3.length; |
| 1772 | entry.tag_offset[tag_bitrate] = id3.bitrate; |
Miika Pekkarinen | f603946 | 2007-12-16 21:10:26 +0000 | [diff] [blame] | 1773 | entry.tag_offset[tag_mtime] = mtime; |
Miika Pekkarinen | 812cbad | 2006-10-04 09:05:01 +0000 | [diff] [blame] | 1774 | |
| 1775 | /* String tags. */ |
Nicolas Pennequin | 67f5082 | 2007-10-01 20:06:21 +0000 | [diff] [blame] | 1776 | has_albumartist = id3.albumartist != NULL |
| 1777 | && strlen(id3.albumartist) > 0; |
| 1778 | has_grouping = id3.grouping != NULL |
| 1779 | && strlen(id3.grouping) > 0; |
Dan Everton | 00dd149 | 2007-06-25 11:33:21 +0000 | [diff] [blame] | 1780 | |
Miika Pekkarinen | 812cbad | 2006-10-04 09:05:01 +0000 | [diff] [blame] | 1781 | ADD_TAG(entry, tag_filename, &path); |
Nicolas Pennequin | 67f5082 | 2007-10-01 20:06:21 +0000 | [diff] [blame] | 1782 | ADD_TAG(entry, tag_title, &id3.title); |
| 1783 | ADD_TAG(entry, tag_artist, &id3.artist); |
| 1784 | ADD_TAG(entry, tag_album, &id3.album); |
| 1785 | ADD_TAG(entry, tag_genre, &id3.genre_string); |
| 1786 | ADD_TAG(entry, tag_composer, &id3.composer); |
| 1787 | ADD_TAG(entry, tag_comment, &id3.comment); |
Dan Everton | 00dd149 | 2007-06-25 11:33:21 +0000 | [diff] [blame] | 1788 | if (has_albumartist) |
| 1789 | { |
Nicolas Pennequin | 67f5082 | 2007-10-01 20:06:21 +0000 | [diff] [blame] | 1790 | ADD_TAG(entry, tag_albumartist, &id3.albumartist); |
Dan Everton | 00dd149 | 2007-06-25 11:33:21 +0000 | [diff] [blame] | 1791 | } |
| 1792 | else |
| 1793 | { |
Nicolas Pennequin | 67f5082 | 2007-10-01 20:06:21 +0000 | [diff] [blame] | 1794 | ADD_TAG(entry, tag_albumartist, &id3.artist); |
Dan Everton | 00dd149 | 2007-06-25 11:33:21 +0000 | [diff] [blame] | 1795 | } |
Dan Everton | eecfe9f | 2007-08-08 10:19:56 +0000 | [diff] [blame] | 1796 | if (has_grouping) |
| 1797 | { |
Nicolas Pennequin | 67f5082 | 2007-10-01 20:06:21 +0000 | [diff] [blame] | 1798 | ADD_TAG(entry, tag_grouping, &id3.grouping); |
Dan Everton | eecfe9f | 2007-08-08 10:19:56 +0000 | [diff] [blame] | 1799 | } |
| 1800 | else |
| 1801 | { |
Nicolas Pennequin | 67f5082 | 2007-10-01 20:06:21 +0000 | [diff] [blame] | 1802 | ADD_TAG(entry, tag_grouping, &id3.title); |
Dan Everton | eecfe9f | 2007-08-08 10:19:56 +0000 | [diff] [blame] | 1803 | } |
Miika Pekkarinen | 812cbad | 2006-10-04 09:05:01 +0000 | [diff] [blame] | 1804 | entry.data_length = offset; |
| 1805 | |
| 1806 | /* Write the header */ |
Miika Pekkarinen | 7c4e0c8 | 2006-03-26 11:33:42 +0000 | [diff] [blame] | 1807 | write(cachefd, &entry, sizeof(struct temp_file_entry)); |
Miika Pekkarinen | 812cbad | 2006-10-04 09:05:01 +0000 | [diff] [blame] | 1808 | |
| 1809 | /* And tags also... Correct order is critical */ |
Miika Pekkarinen | 7c4e0c8 | 2006-03-26 11:33:42 +0000 | [diff] [blame] | 1810 | write_item(path); |
Nicolas Pennequin | 67f5082 | 2007-10-01 20:06:21 +0000 | [diff] [blame] | 1811 | write_item(id3.title); |
| 1812 | write_item(id3.artist); |
| 1813 | write_item(id3.album); |
| 1814 | write_item(id3.genre_string); |
| 1815 | write_item(id3.composer); |
| 1816 | write_item(id3.comment); |
Dan Everton | 00dd149 | 2007-06-25 11:33:21 +0000 | [diff] [blame] | 1817 | if (has_albumartist) |
| 1818 | { |
Nicolas Pennequin | 67f5082 | 2007-10-01 20:06:21 +0000 | [diff] [blame] | 1819 | write_item(id3.albumartist); |
Dan Everton | 00dd149 | 2007-06-25 11:33:21 +0000 | [diff] [blame] | 1820 | } |
| 1821 | else |
| 1822 | { |
Nicolas Pennequin | 67f5082 | 2007-10-01 20:06:21 +0000 | [diff] [blame] | 1823 | write_item(id3.artist); |
Dan Everton | 00dd149 | 2007-06-25 11:33:21 +0000 | [diff] [blame] | 1824 | } |
Dan Everton | eecfe9f | 2007-08-08 10:19:56 +0000 | [diff] [blame] | 1825 | if (has_grouping) |
| 1826 | { |
Nicolas Pennequin | 67f5082 | 2007-10-01 20:06:21 +0000 | [diff] [blame] | 1827 | write_item(id3.grouping); |
Dan Everton | eecfe9f | 2007-08-08 10:19:56 +0000 | [diff] [blame] | 1828 | } |
| 1829 | else |
| 1830 | { |
Nicolas Pennequin | 67f5082 | 2007-10-01 20:06:21 +0000 | [diff] [blame] | 1831 | write_item(id3.title); |
Dan Everton | eecfe9f | 2007-08-08 10:19:56 +0000 | [diff] [blame] | 1832 | } |
Miika Pekkarinen | 7c4e0c8 | 2006-03-26 11:33:42 +0000 | [diff] [blame] | 1833 | total_entry_count++; |
| 1834 | } |
| 1835 | |
Miika Pekkarinen | 4142710 | 2006-04-23 18:47:26 +0000 | [diff] [blame] | 1836 | static bool tempbuf_insert(char *str, int id, int idx_id, bool unique) |
Miika Pekkarinen | 7c4e0c8 | 2006-03-26 11:33:42 +0000 | [diff] [blame] | 1837 | { |
| 1838 | struct tempbuf_searchidx *index = (struct tempbuf_searchidx *)tempbuf; |
| 1839 | int len = strlen(str)+1; |
Miika Pekkarinen | 4142710 | 2006-04-23 18:47:26 +0000 | [diff] [blame] | 1840 | int i; |
| 1841 | unsigned crc32; |
| 1842 | unsigned *crcbuf = (unsigned *)&tempbuf[tempbuf_size-4]; |
Miika Pekkarinen | 0dd7ea2 | 2006-11-10 08:03:33 +0000 | [diff] [blame] | 1843 | char buf[TAG_MAXLEN+32]; |
Miika Pekkarinen | 4142710 | 2006-04-23 18:47:26 +0000 | [diff] [blame] | 1844 | |
| 1845 | for (i = 0; str[i] != '\0' && i < (int)sizeof(buf)-1; i++) |
| 1846 | buf[i] = tolower(str[i]); |
| 1847 | buf[i] = '\0'; |
| 1848 | |
| 1849 | crc32 = crc_32(buf, i, 0xffffffff); |
| 1850 | |
| 1851 | if (unique) |
| 1852 | { |
| 1853 | /* Check if the crc does not exist -> entry does not exist for sure. */ |
| 1854 | for (i = 0; i < tempbufidx; i++) |
| 1855 | { |
| 1856 | if (crcbuf[-i] != crc32) |
| 1857 | continue; |
| 1858 | |
| 1859 | if (!strcasecmp(str, index[i].str)) |
| 1860 | { |
Miika Pekkarinen | 36e3081 | 2006-09-26 11:23:18 +0000 | [diff] [blame] | 1861 | if (id < 0 || id >= lookup_buffer_depth) |
| 1862 | { |
| 1863 | logf("lookup buf overf.: %d", id); |
| 1864 | return false; |
| 1865 | } |
Miika Pekkarinen | 4142710 | 2006-04-23 18:47:26 +0000 | [diff] [blame] | 1866 | |
Miika Pekkarinen | 36e3081 | 2006-09-26 11:23:18 +0000 | [diff] [blame] | 1867 | lookup[id] = &index[i]; |
Miika Pekkarinen | 4142710 | 2006-04-23 18:47:26 +0000 | [diff] [blame] | 1868 | return true; |
| 1869 | } |
| 1870 | } |
| 1871 | } |
| 1872 | |
| 1873 | /* Insert to CRC buffer. */ |
| 1874 | crcbuf[-tempbufidx] = crc32; |
| 1875 | tempbuf_left -= 4; |
Miika Pekkarinen | 7c4e0c8 | 2006-03-26 11:33:42 +0000 | [diff] [blame] | 1876 | |
| 1877 | /* Insert it to the buffer. */ |
Miika Pekkarinen | 4142710 | 2006-04-23 18:47:26 +0000 | [diff] [blame] | 1878 | tempbuf_left -= len; |
Miika Pekkarinen | a47bd73 | 2006-07-22 07:09:57 +0000 | [diff] [blame] | 1879 | if (tempbuf_left - 4 < 0 || tempbufidx >= commit_entry_count-1) |
Miika Pekkarinen | 7c4e0c8 | 2006-03-26 11:33:42 +0000 | [diff] [blame] | 1880 | return false; |
| 1881 | |
Miika Pekkarinen | 36e3081 | 2006-09-26 11:23:18 +0000 | [diff] [blame] | 1882 | if (id >= lookup_buffer_depth) |
| 1883 | { |
| 1884 | logf("lookup buf overf. #2: %d", id); |
| 1885 | return false; |
| 1886 | } |
| 1887 | |
| 1888 | if (id >= 0) |
Miika Pekkarinen | 7c4e0c8 | 2006-03-26 11:33:42 +0000 | [diff] [blame] | 1889 | { |
Miika Pekkarinen | 4142710 | 2006-04-23 18:47:26 +0000 | [diff] [blame] | 1890 | lookup[id] = &index[tempbufidx]; |
| 1891 | index[tempbufidx].idlist.id = id; |
Miika Pekkarinen | 7c4e0c8 | 2006-03-26 11:33:42 +0000 | [diff] [blame] | 1892 | } |
Miika Pekkarinen | 4142710 | 2006-04-23 18:47:26 +0000 | [diff] [blame] | 1893 | else |
| 1894 | index[tempbufidx].idlist.id = -1; |
Miika Pekkarinen | 7c4e0c8 | 2006-03-26 11:33:42 +0000 | [diff] [blame] | 1895 | |
Miika Pekkarinen | 4142710 | 2006-04-23 18:47:26 +0000 | [diff] [blame] | 1896 | index[tempbufidx].idlist.next = NULL; |
| 1897 | index[tempbufidx].idx_id = idx_id; |
Miika Pekkarinen | 7c4e0c8 | 2006-03-26 11:33:42 +0000 | [diff] [blame] | 1898 | index[tempbufidx].seek = -1; |
| 1899 | index[tempbufidx].str = &tempbuf[tempbuf_pos]; |
| 1900 | memcpy(index[tempbufidx].str, str, len); |
| 1901 | tempbuf_pos += len; |
| 1902 | tempbufidx++; |
| 1903 | |
| 1904 | return true; |
| 1905 | } |
| 1906 | |
Miika Pekkarinen | 7c4e0c8 | 2006-03-26 11:33:42 +0000 | [diff] [blame] | 1907 | static int compare(const void *p1, const void *p2) |
| 1908 | { |
Steve Bavin | 6d7f68c | 2007-03-10 14:34:56 +0000 | [diff] [blame] | 1909 | do_timed_yield(); |
| 1910 | |
Miika Pekkarinen | 7c4e0c8 | 2006-03-26 11:33:42 +0000 | [diff] [blame] | 1911 | struct tempbuf_searchidx *e1 = (struct tempbuf_searchidx *)p1; |
| 1912 | struct tempbuf_searchidx *e2 = (struct tempbuf_searchidx *)p2; |
| 1913 | |
Steve Bavin | 5eb2967 | 2007-03-07 10:52:19 +0000 | [diff] [blame] | 1914 | if (strcmp(e1->str, UNTAGGED) == 0) |
| 1915 | { |
| 1916 | if (strcmp(e2->str, UNTAGGED) == 0) |
| 1917 | return 0; |
| 1918 | return -1; |
| 1919 | } |
| 1920 | else if (strcmp(e2->str, UNTAGGED) == 0) |
| 1921 | return 1; |
Miika Pekkarinen | 7c4e0c8 | 2006-03-26 11:33:42 +0000 | [diff] [blame] | 1922 | |
Miika Pekkarinen | 0dd7ea2 | 2006-11-10 08:03:33 +0000 | [diff] [blame] | 1923 | return strncasecmp(e1->str, e2->str, TAG_MAXLEN); |
Miika Pekkarinen | 7c4e0c8 | 2006-03-26 11:33:42 +0000 | [diff] [blame] | 1924 | } |
| 1925 | |
| 1926 | static int tempbuf_sort(int fd) |
| 1927 | { |
| 1928 | struct tempbuf_searchidx *index = (struct tempbuf_searchidx *)tempbuf; |
| 1929 | struct tagfile_entry fe; |
| 1930 | int i; |
Miika Pekkarinen | 3f3aeb0 | 2006-03-27 10:46:09 +0000 | [diff] [blame] | 1931 | int length; |
Miika Pekkarinen | 4142710 | 2006-04-23 18:47:26 +0000 | [diff] [blame] | 1932 | |
| 1933 | /* Generate reverse lookup entries. */ |
Miika Pekkarinen | 36e3081 | 2006-09-26 11:23:18 +0000 | [diff] [blame] | 1934 | for (i = 0; i < lookup_buffer_depth; i++) |
Miika Pekkarinen | 4142710 | 2006-04-23 18:47:26 +0000 | [diff] [blame] | 1935 | { |
| 1936 | struct tempbuf_id_list *idlist; |
| 1937 | |
| 1938 | if (!lookup[i]) |
| 1939 | continue; |
| 1940 | |
| 1941 | if (lookup[i]->idlist.id == i) |
| 1942 | continue; |
| 1943 | |
| 1944 | idlist = &lookup[i]->idlist; |
| 1945 | while (idlist->next != NULL) |
| 1946 | idlist = idlist->next; |
| 1947 | |
| 1948 | tempbuf_left -= sizeof(struct tempbuf_id_list); |
| 1949 | if (tempbuf_left - 4 < 0) |
| 1950 | return -1; |
| 1951 | |
| 1952 | idlist->next = (struct tempbuf_id_list *)&tempbuf[tempbuf_pos]; |
| 1953 | if (tempbuf_pos & 0x03) |
| 1954 | { |
| 1955 | tempbuf_pos = (tempbuf_pos & ~0x03) + 0x04; |
| 1956 | tempbuf_left -= 3; |
| 1957 | idlist->next = (struct tempbuf_id_list *)&tempbuf[tempbuf_pos]; |
| 1958 | } |
| 1959 | tempbuf_pos += sizeof(struct tempbuf_id_list); |
| 1960 | |
| 1961 | idlist = idlist->next; |
| 1962 | idlist->id = i; |
| 1963 | idlist->next = NULL; |
Steve Bavin | 6d7f68c | 2007-03-10 14:34:56 +0000 | [diff] [blame] | 1964 | |
| 1965 | do_timed_yield(); |
Miika Pekkarinen | 4142710 | 2006-04-23 18:47:26 +0000 | [diff] [blame] | 1966 | } |
Miika Pekkarinen | 7c4e0c8 | 2006-03-26 11:33:42 +0000 | [diff] [blame] | 1967 | |
| 1968 | qsort(index, tempbufidx, sizeof(struct tempbuf_searchidx
|