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