2006-03-26 11:33:42 +00:00
|
|
|
/***************************************************************************
|
|
|
|
* __________ __ ___.
|
|
|
|
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
|
|
|
|
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
|
|
|
|
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
|
|
|
|
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
|
|
|
|
* \/ \/ \/ \/ \/
|
|
|
|
* $Id$
|
|
|
|
*
|
|
|
|
* Copyright (C) 2005 by Miika Pekkarinen
|
|
|
|
*
|
2008-06-28 18:10:04 +00:00
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
2006-03-26 11:33:42 +00:00
|
|
|
*
|
|
|
|
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
|
|
|
|
* KIND, either express or implied.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2006-07-25 10:44:19 +00:00
|
|
|
/*
|
|
|
|
* TagCache API
|
2021-07-31 14:26:59 +00:00
|
|
|
*
|
2006-07-25 10:44:19 +00:00
|
|
|
* ----------x---------x------------------x-----
|
|
|
|
* | | | External
|
|
|
|
* +---------------x-------+ | TagCache | Libraries
|
2021-07-31 14:26:59 +00:00
|
|
|
* | Modification routines | | Core |
|
|
|
|
* +-x---------x-----------+ | |
|
2006-07-25 10:44:19 +00:00
|
|
|
* | (R/W) | | | |
|
|
|
|
* | +------x-------------x-+ +-------------x-----+ |
|
|
|
|
* | | x==x Filters & clauses | |
|
|
|
|
* | | Search routines | +-------------------+ |
|
|
|
|
* | | x============================x DirCache
|
|
|
|
* | +-x--------------------+ | (optional)
|
|
|
|
* | | (R) |
|
|
|
|
* | | +-------------------------------+ +---------+ |
|
|
|
|
* | | | DB Commit (sort,unique,index) | | | |
|
|
|
|
* | | +-x--------------------------x--+ | Control | |
|
|
|
|
* | | | (R/W) | (R) | Thread | |
|
|
|
|
* | | | +----------------------+ | | | |
|
|
|
|
* | | | | TagCache DB Builder | | +---------+ |
|
|
|
|
* | | | +-x-------------x------+ | |
|
|
|
|
* | | | | (R) | (W) | |
|
|
|
|
* | | | | +--x--------x---------+ |
|
|
|
|
* | | | | | Temporary Commit DB | |
|
|
|
|
* | | | | +---------------------+ |
|
2006-08-02 17:39:34 +00:00
|
|
|
* +-x----x-------x--+ |
|
2006-07-25 10:44:19 +00:00
|
|
|
* | TagCache RAM DB x==\(W) +-----------------+ |
|
2006-08-02 17:39:34 +00:00
|
|
|
* +-----------------+ \===x | |
|
2006-07-25 10:44:19 +00:00
|
|
|
* | | | | (R) | Ram DB Loader x============x DirCache
|
|
|
|
* +-x----x---x---x---+ /==x | | (optional)
|
2021-07-31 14:26:59 +00:00
|
|
|
* | Tagcache Disk DB x==/ +-----------------+ |
|
2006-07-25 10:44:19 +00:00
|
|
|
* +------------------+ |
|
2021-07-31 14:26:59 +00:00
|
|
|
*
|
2006-07-25 10:44:19 +00:00
|
|
|
*/
|
|
|
|
|
2009-11-03 16:25:03 +00:00
|
|
|
/*#define LOGF_ENABLE*/
|
2022-03-19 06:15:35 +00:00
|
|
|
/*#define LOGF_CLAUSES define to enable logf clause matching (LOGF_ENABLE req'd) */
|
2022-03-19 06:11:57 +00:00
|
|
|
|
2006-03-26 11:33:42 +00:00
|
|
|
#include <stdio.h>
|
2007-05-07 13:32:56 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <ctype.h>
|
2010-12-23 19:02:18 +00:00
|
|
|
#ifdef APPLICATION
|
|
|
|
#include <unistd.h> /* readlink() */
|
2011-01-29 01:26:43 +00:00
|
|
|
#include <limits.h> /* PATH_MAX */
|
2010-12-23 19:02:18 +00:00
|
|
|
#endif
|
2006-09-16 16:18:11 +00:00
|
|
|
#include "config.h"
|
2008-02-17 18:35:27 +00:00
|
|
|
#include "ata_idle_notify.h"
|
2006-03-26 11:33:42 +00:00
|
|
|
#include "thread.h"
|
|
|
|
#include "kernel.h"
|
|
|
|
#include "system.h"
|
|
|
|
#include "logf.h"
|
2010-05-06 21:04:40 +00:00
|
|
|
#include "string-extra.h"
|
2006-03-26 11:33:42 +00:00
|
|
|
#include "usb.h"
|
|
|
|
#include "metadata.h"
|
|
|
|
#include "tagcache.h"
|
2011-08-30 14:01:33 +00:00
|
|
|
#include "core_alloc.h"
|
2006-04-18 18:56:56 +00:00
|
|
|
#include "crc32.h"
|
2006-10-21 20:37:33 +00:00
|
|
|
#include "misc.h"
|
2006-11-10 08:03:33 +00:00
|
|
|
#include "settings.h"
|
2007-07-20 17:06:55 +00:00
|
|
|
#include "dir.h"
|
Rewrite filesystem code (WIP)
This patch redoes the filesystem code from the FAT driver up to the
clipboard code in onplay.c.
Not every aspect of this is finished therefore it is still "WIP". I
don't wish to do too much at once (haha!). What is left to do is get
dircache back in the sim and find an implementation for the dircache
indicies in the tagcache and playlist code or do something else that
has the same benefit. Leaving these out for now does not make anything
unusable. All the basics are done.
Phone app code should probably get vetted (and app path handling
just plain rewritten as environment expansions); the SDL app and
Android run well.
Main things addressed:
1) Thread safety: There is none right now in the trunk code. Most of
what currently works is luck when multiple threads are involved or
multiple descriptors to the same file are open.
2) POSIX compliance: Many of the functions behave nothing like their
counterparts on a host system. This leads to inconsistent code or very
different behavior from native to hosted. One huge offender was
rename(). Going point by point would fill a book.
3) Actual running RAM usage: Many targets will use less RAM and less
stack space (some more RAM because I upped the number of cache buffers
for large memory). There's very little memory lying fallow in rarely-used
areas (see 'Key core changes' below). Also, all targets may open the same
number of directory streams whereas before those with less than 8MB RAM
were limited to 8, not 12 implying those targets will save slightly
less.
4) Performance: The test_disk plugin shows markedly improved performance,
particularly in the area of (uncached) directory scanning, due partly to
more optimal directory reading and to a better sector cache algorithm.
Uncached times tend to be better while there is a bit of a slowdown in
dircache due to it being a bit heavier of an implementation. It's not
noticeable by a human as far as I can say.
Key core changes:
1) Files and directories share core code and data structures.
2) The filesystem code knows which descriptors refer to same file.
This ensures that changes from one stream are appropriately reflected
in every open descriptor for that file (fileobj_mgr.c).
3) File and directory cache buffers are borrowed from the main sector
cache. This means that when they are not in use by a file, they are not
wasted, but used for the cache. Most of the time, only a few of them
are needed. It also means that adding more file and directory handles
is less expensive. All one must do in ensure a large enough cache to
borrow from.
4) Relative path components are supported and the namespace is unified.
It does not support full relative paths to an implied current directory;
what is does support is use of "." and "..". Adding the former would
not be very difficult. The namespace is unified in the sense that
volumes may be specified several times along with relative parts, e.g.:
"/<0>/foo/../../<1>/bar" :<=> "/<1>/bar".
5) Stack usage is down due to sharing of data, static allocation and
less duplication of strings on the stack. This requires more
serialization than I would like but since the number of threads is
limited to a low number, the tradoff in favor of the stack seems
reasonable.
6) Separates and heirarchicalizes (sic) the SIM and APP filesystem
code. SIM path and volume handling is just like the target. Some
aspects of the APP file code get more straightforward (e.g. no path
hashing is needed).
Dircache:
Deserves its own section. Dircache is new but pays homage to the old.
The old one was not compatible and so it, since it got redone, does
all the stuff it always should have done such as:
1) It may be update and used at any time during the build process.
No longer has one to wait for it to finish building to do basic file
management (create, remove, rename, etc.).
2) It does not need to be either fully scanned or completely disabled;
it can be incomplete (i.e. overfilled, missing paths), still be
of benefit and be correct.
3) Handles mounting and dismounting of individual volumes which means
a full rebuild is not needed just because you pop a new SD card in the
slot. Now, because it reuses its freed entry data, may rebuild only
that volume.
4) Much more fundamental to the file code. When it is built, it is
the keeper of the master file list whether enabled or not ("disabled"
is just a state of the cache). Its must always to ready to be started
and bind all streams opened prior to being enabled.
5) Maintains any short filenames in OEM format which means that it does
not need to be rebuilt when changing the default codepage.
Miscellaneous Compatibility:
1) Update any other code that would otherwise not work such as the
hotswap mounting code in various card drivers.
2) File management: Clipboard needed updating because of the behavioral
changes. Still needs a little more work on some finer points.
3) Remove now-obsolete functionality such as the mutex's "no preempt"
flag (which was only for the prior FAT driver).
4) struct dirinfo uses time_t rather than raw FAT directory entry
time fields. I plan to follow up on genericizing everything there
(i.e. no FAT attributes).
5) unicode.c needed some redoing so that the file code does not try
try to load codepages during a scan, which is actually a problem with
the current code. The default codepage, if any is required, is now
kept in RAM separarately (bufalloced) from codepages specified to
iso_decode() (which must not be bufalloced because the conversion
may be done by playback threads).
Brings with it some additional reusable core code:
1) Revised file functions: Reusable code that does things such as
safe path concatenation and parsing without buffer limitations or
data duplication. Variants that copy or alter the input path may be
based off these.
To do:
1) Put dircache functionality back in the sim. Treating it internally
as a different kind of file system seems the best approach at this
time.
2) Restore use of dircache indexes in the playlist and database or
something effectively the same. Since the cache doesn't have to be
complete in order to be used, not getting a hit on the cache doesn't
unambiguously say if the path exists or not.
Change-Id: Ia30f3082a136253e3a0eae0784e3091d138915c8
Reviewed-on: http://gerrit.rockbox.org/566
Reviewed-by: Michael Sevakis <jethead71@rockbox.org>
Tested: Michael Sevakis <jethead71@rockbox.org>
2013-08-06 02:02:45 +00:00
|
|
|
#include "pathfuncs.h"
|
2007-02-13 21:51:18 +00:00
|
|
|
#include "structec.h"
|
2010-12-23 19:02:18 +00:00
|
|
|
#include "debug.h"
|
2017-01-18 09:39:35 +00:00
|
|
|
#include "dircache.h"
|
2008-05-03 08:35:14 +00:00
|
|
|
|
2006-11-10 08:03:33 +00:00
|
|
|
#ifndef __PCTOOL__
|
|
|
|
#include "lang.h"
|
|
|
|
#include "eeprom_settings.h"
|
|
|
|
#endif
|
2006-03-26 11:33:42 +00:00
|
|
|
|
2006-11-10 08:03:33 +00:00
|
|
|
#ifdef __PCTOOL__
|
|
|
|
#define yield() do { } while(0)
|
|
|
|
#define sim_sleep(timeout) do { } while(0)
|
2007-03-10 14:34:56 +00:00
|
|
|
#define do_timed_yield() do { } while(0)
|
2006-11-10 08:03:33 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef __PCTOOL__
|
2006-03-26 11:33:42 +00:00
|
|
|
/* Tag Cache thread. */
|
2011-02-14 11:27:45 +00:00
|
|
|
static struct event_queue tagcache_queue SHAREDBSS_ATTR;
|
2006-03-26 11:33:42 +00:00
|
|
|
static long tagcache_stack[(DEFAULT_STACK_SIZE + 0x4000)/sizeof(long)];
|
|
|
|
static const char tagcache_thread_name[] = "tagcache";
|
2006-11-10 08:03:33 +00:00
|
|
|
#endif
|
2021-08-01 02:24:33 +00:00
|
|
|
/* buffer size for all the (stack allocated & static) buffers handling tc data */
|
|
|
|
#define TAGCACHE_BUFSZ (TAG_MAXLEN+32)
|
|
|
|
|
2006-03-26 11:33:42 +00:00
|
|
|
|
|
|
|
/* Previous path when scanning directory tree recursively. */
|
2021-08-01 02:24:33 +00:00
|
|
|
static char curpath[TAGCACHE_BUFSZ];
|
2006-03-26 11:33:42 +00:00
|
|
|
|
|
|
|
/* Used when removing duplicates. */
|
|
|
|
static char *tempbuf; /* Allocated when needed. */
|
|
|
|
static long tempbufidx; /* Current location in buffer. */
|
2011-08-14 15:13:00 +00:00
|
|
|
static size_t tempbuf_size; /* Buffer size (TEMPBUF_SIZE). */
|
2006-03-26 11:33:42 +00:00
|
|
|
static long tempbuf_left; /* Buffer space left. */
|
|
|
|
static long tempbuf_pos;
|
2017-01-17 19:45:07 +00:00
|
|
|
#ifndef __PCTOOL__
|
|
|
|
static int tempbuf_handle;
|
|
|
|
#endif
|
2006-03-26 11:33:42 +00:00
|
|
|
|
2021-09-15 23:33:41 +00:00
|
|
|
#define SORTED_TAGS_COUNT 9
|
2009-06-06 00:00:58 +00:00
|
|
|
#define TAGCACHE_IS_UNIQUE(tag) (BIT_N(tag) & TAGCACHE_UNIQUE_TAGS)
|
|
|
|
#define TAGCACHE_IS_SORTED(tag) (BIT_N(tag) & TAGCACHE_SORTED_TAGS)
|
|
|
|
#define TAGCACHE_IS_NUMERIC_OR_NONUNIQUE(tag) \
|
|
|
|
(BIT_N(tag) & (TAGCACHE_NUMERIC_TAGS | ~TAGCACHE_UNIQUE_TAGS))
|
2006-03-27 07:44:32 +00:00
|
|
|
/* Tags we want to get sorted (loaded to the tempbuf). */
|
2009-06-03 08:19:32 +00:00
|
|
|
#define TAGCACHE_SORTED_TAGS ((1LU << tag_artist) | (1LU << tag_album) | \
|
|
|
|
(1LU << tag_genre) | (1LU << tag_composer) | (1LU << tag_comment) | \
|
2021-09-15 23:33:41 +00:00
|
|
|
(1LU << tag_albumartist) | (1LU << tag_grouping) | (1LU << tag_title) | \
|
|
|
|
(1LU << tag_virt_canonicalartist))
|
2006-04-10 10:26:24 +00:00
|
|
|
|
|
|
|
/* Uniqued tags (we can use these tags with filters and conditional clauses). */
|
2009-06-03 08:19:32 +00:00
|
|
|
#define TAGCACHE_UNIQUE_TAGS ((1LU << tag_artist) | (1LU << tag_album) | \
|
|
|
|
(1LU << tag_genre) | (1LU << tag_composer) | (1LU << tag_comment) | \
|
2021-09-15 23:33:41 +00:00
|
|
|
(1LU << tag_albumartist) | (1LU << tag_grouping) | \
|
|
|
|
(1LU << tag_virt_canonicalartist))
|
2006-03-26 11:33:42 +00:00
|
|
|
|
2007-02-10 12:09:28 +00:00
|
|
|
/* String presentation of the tags defined in tagcache.h. Must be in correct order! */
|
2021-07-31 14:26:59 +00:00
|
|
|
static const char *tags_str[] = { "artist", "album", "genre", "title",
|
|
|
|
"filename", "composer", "comment", "albumartist", "grouping", "year",
|
2021-09-15 23:33:41 +00:00
|
|
|
"discnumber", "tracknumber", "canonicalartist", "bitrate", "length",
|
|
|
|
"playcount", "rating", "playtime", "lastplayed", "commitid", "mtime",
|
2022-03-19 06:11:57 +00:00
|
|
|
"lastelapsed", "lastoffset"
|
2022-03-19 05:59:07 +00:00
|
|
|
#if !defined(LOGF_ENABLE)
|
|
|
|
};
|
2022-03-19 06:11:57 +00:00
|
|
|
#define logf_clauses(...) do { } while(0)
|
2022-03-19 05:59:07 +00:00
|
|
|
#elif defined(LOGF_CLAUSES) /* strings for logf debugging */
|
|
|
|
"tag_virt_basename", "tag_virt_length_min", "tag_virt_length_sec",
|
|
|
|
"tag_virt_playtime_min", "tag_virt_playtime_sec",
|
|
|
|
"tag_virt_entryage", "tag_virt_autoscore"
|
|
|
|
};
|
|
|
|
/* more debug strings */
|
|
|
|
static const char *tag_type_str[] = {
|
|
|
|
[clause_none] = "clause_none", [clause_is] = "clause_is",
|
|
|
|
[clause_is_not] = "clause_is_not", [clause_gt] = "clause_gt",
|
|
|
|
[clause_gteq] = "clause_gteq", [clause_lt] = "clause_lt",
|
|
|
|
[clause_lteq] = "clause_lteq", [clause_contains] = "clause_contains",
|
|
|
|
[clause_not_contains] = "clause_not_contains",
|
|
|
|
[clause_begins_with] = "clause_begins_with",
|
|
|
|
[clause_not_begins_with] = "clause_not_begins_with",
|
|
|
|
[clause_ends_with] = "clause_ends_with",
|
|
|
|
[clause_not_ends_with] = "clause_not_ends_with",
|
|
|
|
[clause_oneof] = "clause_oneof",
|
2022-03-19 06:15:35 +00:00
|
|
|
[clause_begins_oneof] = "clause_begins_oneof",
|
|
|
|
[clause_ends_oneof] = "clause_ends_oneof",
|
2022-03-19 05:59:07 +00:00
|
|
|
[clause_logical_or] = "clause_logical_or"
|
|
|
|
};
|
2022-03-19 06:11:57 +00:00
|
|
|
#define logf_clauses logf
|
2022-03-19 05:59:07 +00:00
|
|
|
#endif /* ndef LOGF_ENABLE */
|
2006-07-20 12:19:31 +00:00
|
|
|
|
2006-04-23 18:47:26 +00:00
|
|
|
/* Status information of the tagcache. */
|
2006-11-10 08:03:33 +00:00
|
|
|
static struct tagcache_stat tc_stat;
|
2006-04-12 10:31:24 +00:00
|
|
|
|
2006-03-26 11:33:42 +00:00
|
|
|
/* Queue commands. */
|
2006-04-18 18:56:56 +00:00
|
|
|
enum tagcache_queue {
|
|
|
|
Q_STOP_SCAN = 0,
|
|
|
|
Q_START_SCAN,
|
2006-07-23 15:35:53 +00:00
|
|
|
Q_IMPORT_CHANGELOG,
|
2006-07-10 16:22:03 +00:00
|
|
|
Q_UPDATE,
|
|
|
|
Q_REBUILD,
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2007-07-21 17:35:19 +00:00
|
|
|
/* Internal tagcache command queue. */
|
|
|
|
CMD_UPDATE_MASTER_HEADER,
|
|
|
|
CMD_UPDATE_NUMERIC,
|
|
|
|
};
|
|
|
|
|
|
|
|
struct tagcache_command_entry {
|
2008-04-24 07:54:32 +00:00
|
|
|
int32_t command;
|
|
|
|
int32_t idx_id;
|
|
|
|
int32_t tag;
|
|
|
|
int32_t data;
|
2006-04-18 18:56:56 +00:00
|
|
|
};
|
2006-03-30 12:07:32 +00:00
|
|
|
|
2008-12-15 23:42:19 +00:00
|
|
|
#ifndef __PCTOOL__
|
2007-07-21 17:35:19 +00:00
|
|
|
static struct tagcache_command_entry command_queue[TAGCACHE_COMMAND_QUEUE_LENGTH];
|
|
|
|
static volatile int command_queue_widx = 0;
|
|
|
|
static volatile int command_queue_ridx = 0;
|
2011-02-14 11:27:45 +00:00
|
|
|
static struct mutex command_queue_mutex SHAREDBSS_ATTR;
|
2008-12-15 23:42:19 +00:00
|
|
|
#endif
|
2006-04-16 17:32:54 +00:00
|
|
|
|
2006-03-26 11:33:42 +00:00
|
|
|
/* Tag database structures. */
|
|
|
|
|
|
|
|
/* Variable-length tag entry in tag files. */
|
|
|
|
struct tagfile_entry {
|
2009-03-06 15:21:58 +00:00
|
|
|
int32_t tag_length; /* Length of the data in bytes including '\0' */
|
|
|
|
int32_t idx_id; /* Corresponding entry location in index file of not unique tags */
|
2006-07-20 12:19:31 +00:00
|
|
|
char tag_data[0]; /* Begin of the tag data */
|
2006-03-26 11:33:42 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/* Fixed-size tag entry in master db index. */
|
|
|
|
struct index_entry {
|
2008-04-24 07:54:32 +00:00
|
|
|
int32_t tag_seek[TAG_COUNT]; /* Location of tag data or numeric tag data */
|
|
|
|
int32_t flag; /* Status flags */
|
2006-03-26 11:33:42 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/* Header is the same in every file. */
|
|
|
|
struct tagcache_header {
|
2008-04-24 07:54:32 +00:00
|
|
|
int32_t magic; /* Header version number */
|
|
|
|
int32_t datasize; /* Data size in bytes */
|
|
|
|
int32_t entry_count; /* Number of entries in this file */
|
2006-07-20 12:19:31 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct master_header {
|
|
|
|
struct tagcache_header tch;
|
2008-04-24 07:54:32 +00:00
|
|
|
int32_t serial; /* Increasing counting number */
|
|
|
|
int32_t commitid; /* Number of commits so far */
|
|
|
|
int32_t dirty;
|
2006-03-26 11:33:42 +00:00
|
|
|
};
|
|
|
|
|
2007-02-13 21:51:18 +00:00
|
|
|
/* For the endianess correction */
|
2011-12-17 18:31:55 +00:00
|
|
|
static const char * const tagfile_entry_ec = "ll";
|
2008-08-29 21:14:58 +00:00
|
|
|
/**
|
|
|
|
Note: This should be (1 + TAG_COUNT) amount of l's.
|
|
|
|
*/
|
2021-09-15 23:33:41 +00:00
|
|
|
static const char * const index_entry_ec = "llllllllllllllllllllllll";
|
2008-08-29 21:14:58 +00:00
|
|
|
|
2011-12-17 18:31:55 +00:00
|
|
|
static const char * const tagcache_header_ec = "lll";
|
|
|
|
static const char * const master_header_ec = "llllll";
|
2007-02-13 21:51:18 +00:00
|
|
|
|
2007-02-25 20:41:51 +00:00
|
|
|
static struct master_header current_tcmh;
|
2006-07-20 12:19:31 +00:00
|
|
|
|
2006-03-26 11:33:42 +00:00
|
|
|
#ifdef HAVE_TC_RAMCACHE
|
2017-01-18 09:39:35 +00:00
|
|
|
|
2017-04-01 14:19:52 +00:00
|
|
|
#define TC_ALIGN_PTR(p, type, gap_out_p) \
|
2017-01-18 09:39:35 +00:00
|
|
|
({ typeof (p) __p = (p); \
|
2017-04-01 14:19:52 +00:00
|
|
|
typeof (p) __palgn = ALIGN_UP(__p, __alignof__(type)); \
|
|
|
|
*(gap_out_p) = (char *)__palgn - (char *)__p; \
|
2017-01-18 09:39:35 +00:00
|
|
|
__palgn; })
|
|
|
|
|
|
|
|
#define IF_TCRCDC(...) IF_DIRCACHE(__VA_ARGS__)
|
|
|
|
|
|
|
|
#ifdef HAVE_DIRCACHE
|
|
|
|
#define tcrc_dcfrefs \
|
|
|
|
((struct dircache_fileref *)(tcramcache.hdr->tags[tag_filename] + \
|
|
|
|
sizeof (struct tagcache_header)))
|
|
|
|
#endif /* HAVE_DIRCACHE */
|
|
|
|
|
2006-03-26 11:33:42 +00:00
|
|
|
/* Header is created when loading database to ram. */
|
|
|
|
struct ramcache_header {
|
2017-01-18 09:39:35 +00:00
|
|
|
char *tags[TAG_COUNT]; /* Tag file content (dcfrefs if tag_filename) */
|
2006-07-20 12:19:31 +00:00
|
|
|
int entry_count[TAG_COUNT]; /* Number of entries in the indices. */
|
2011-07-01 13:48:28 +00:00
|
|
|
struct index_entry indices[0]; /* Master index file content */
|
2006-03-26 11:33:42 +00:00
|
|
|
};
|
|
|
|
|
2017-01-18 09:39:35 +00:00
|
|
|
#ifdef HAVE_EEPROM_SETTINGS
|
2006-08-05 20:19:10 +00:00
|
|
|
struct statefile_header {
|
2011-07-01 13:48:28 +00:00
|
|
|
int32_t magic; /* Statefile version number */
|
2011-06-19 14:36:26 +00:00
|
|
|
struct master_header mh; /* Header from the master index */
|
2011-07-01 13:48:28 +00:00
|
|
|
struct ramcache_header *hdr; /* Old load address of hdr for relocation */
|
2006-11-10 08:03:33 +00:00
|
|
|
struct tagcache_stat tc_stat;
|
2006-08-05 20:19:10 +00:00
|
|
|
};
|
2017-01-18 09:39:35 +00:00
|
|
|
#endif /* HAVE_EEPROM_SETTINGS */
|
2006-08-05 20:19:10 +00:00
|
|
|
|
2017-01-18 09:39:35 +00:00
|
|
|
/* In-RAM ramcache structure (not persisted) */
|
|
|
|
static struct tcramcache
|
|
|
|
{
|
|
|
|
struct ramcache_header *hdr; /* allocated ramcache_header */
|
|
|
|
int handle; /* buffer handle */
|
|
|
|
int move_lock;
|
|
|
|
} tcramcache;
|
|
|
|
|
|
|
|
static inline void tcrc_buffer_lock(void)
|
|
|
|
{
|
|
|
|
tcramcache.move_lock++;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void tcrc_buffer_unlock(void)
|
|
|
|
{
|
|
|
|
tcramcache.move_lock--;
|
|
|
|
}
|
|
|
|
|
|
|
|
#else /* ndef HAVE_TC_RAMCACHE */
|
|
|
|
|
|
|
|
#define IF_TCRCDC(...)
|
|
|
|
|
|
|
|
#endif /* HAVE_TC_RAMCACHE */
|
2006-03-26 11:33:42 +00:00
|
|
|
|
2021-07-31 14:26:59 +00:00
|
|
|
/**
|
|
|
|
* Full tag entries stored in a temporary file waiting
|
2006-03-26 11:33:42 +00:00
|
|
|
* for commit to the cache. */
|
|
|
|
struct temp_file_entry {
|
|
|
|
long tag_offset[TAG_COUNT];
|
|
|
|
short tag_length[TAG_COUNT];
|
2006-10-04 09:05:01 +00:00
|
|
|
long flag;
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2006-03-26 11:33:42 +00:00
|
|
|
long data_length;
|
|
|
|
};
|
|
|
|
|
2006-04-23 18:47:26 +00:00
|
|
|
struct tempbuf_id_list {
|
|
|
|
long id;
|
|
|
|
struct tempbuf_id_list *next;
|
2006-03-26 11:33:42 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct tempbuf_searchidx {
|
2006-03-30 12:07:32 +00:00
|
|
|
long idx_id;
|
2006-03-26 11:33:42 +00:00
|
|
|
char *str;
|
|
|
|
int seek;
|
2006-04-23 18:47:26 +00:00
|
|
|
struct tempbuf_id_list idlist;
|
2006-03-26 11:33:42 +00:00
|
|
|
};
|
|
|
|
|
2006-09-26 11:23:18 +00:00
|
|
|
/* Lookup buffer for fixing messed up index while after sorting. */
|
2006-07-22 07:09:57 +00:00
|
|
|
static long commit_entry_count;
|
2006-09-26 11:23:18 +00:00
|
|
|
static long lookup_buffer_depth;
|
2006-12-25 14:01:47 +00:00
|
|
|
static struct tempbuf_searchidx **lookup;
|
2006-03-26 11:33:42 +00:00
|
|
|
|
|
|
|
/* Used when building the temporary file. */
|
|
|
|
static int cachefd = -1, filenametag_fd;
|
|
|
|
static int total_entry_count = 0;
|
|
|
|
static int data_size = 0;
|
|
|
|
static int processed_dir_count;
|
|
|
|
|
2006-08-02 17:39:34 +00:00
|
|
|
/* Thread safe locking */
|
|
|
|
static volatile int write_lock;
|
|
|
|
static volatile int read_lock;
|
|
|
|
|
2007-12-16 21:10:26 +00:00
|
|
|
static bool delete_entry(long idx_id);
|
|
|
|
|
2021-08-01 02:24:33 +00:00
|
|
|
static inline void str_setlen(char *buf, size_t len)
|
|
|
|
{
|
|
|
|
buf[len] = '\0';
|
|
|
|
}
|
|
|
|
|
2017-01-18 09:39:35 +00:00
|
|
|
static void allocate_tempbuf(void)
|
|
|
|
{
|
|
|
|
/* Yeah, malloc would be really nice now :) */
|
|
|
|
size_t size;
|
|
|
|
tempbuf_size = 0;
|
|
|
|
|
|
|
|
#ifdef __PCTOOL__
|
|
|
|
size = 32*1024*1024;
|
|
|
|
tempbuf = malloc(size);
|
|
|
|
if (tempbuf)
|
|
|
|
tempbuf_size = size;
|
|
|
|
#else /* !__PCTOOL__ */
|
2022-01-18 18:20:53 +00:00
|
|
|
/* Need to pass dummy ops to prevent the buffer being moved
|
|
|
|
* out from under us, since we yield during the tagcache commit. */
|
2022-01-18 18:57:06 +00:00
|
|
|
tempbuf_handle = core_alloc_maximum("tc tempbuf", &size, &buflib_ops_locked);
|
2017-01-18 09:39:35 +00:00
|
|
|
if (tempbuf_handle > 0)
|
|
|
|
{
|
|
|
|
tempbuf = core_get_data(tempbuf_handle);
|
|
|
|
tempbuf_size = size;
|
|
|
|
}
|
|
|
|
#endif /* __PCTOOL__ */
|
|
|
|
}
|
|
|
|
|
|
|
|
static void free_tempbuf(void)
|
|
|
|
{
|
|
|
|
if (tempbuf_size == 0)
|
|
|
|
return ;
|
|
|
|
|
|
|
|
#ifdef __PCTOOL__
|
|
|
|
free(tempbuf);
|
|
|
|
#else
|
|
|
|
tempbuf_handle = core_free(tempbuf_handle);
|
|
|
|
#endif
|
|
|
|
tempbuf = NULL;
|
|
|
|
tempbuf_size = 0;
|
|
|
|
}
|
|
|
|
|
2006-07-20 12:19:31 +00:00
|
|
|
const char* tagcache_tag_to_str(int tag)
|
|
|
|
{
|
|
|
|
return tags_str[tag];
|
|
|
|
}
|
|
|
|
|
2010-03-16 12:15:56 +00:00
|
|
|
/* Helper functions for the two most read/write data structure: tagfile_entry and index_entry */
|
2010-03-17 08:23:50 +00:00
|
|
|
static ssize_t ecread_tagfile_entry(int fd, struct tagfile_entry *buf)
|
2010-03-16 12:15:56 +00:00
|
|
|
{
|
|
|
|
return ecread(fd, buf, 1, tagfile_entry_ec, tc_stat.econ);
|
|
|
|
}
|
|
|
|
|
2021-08-01 02:24:33 +00:00
|
|
|
enum e_ecread_errors {e_SUCCESS = 0, e_ENTRY_SIZEMISMATCH = 1, e_TAG_TOOLONG, e_TAG_SIZEMISMATCH};
|
|
|
|
static enum e_ecread_errors ecread_tagfile_entry_and_tag
|
|
|
|
(int fd, struct tagfile_entry *tfe, char* buf, int bufsz)
|
|
|
|
{
|
|
|
|
long tag_length;
|
|
|
|
str_setlen(buf, 0);
|
|
|
|
if (ecread_tagfile_entry(fd, tfe)!= sizeof(struct tagfile_entry))
|
|
|
|
{
|
|
|
|
return e_ENTRY_SIZEMISMATCH;
|
|
|
|
}
|
|
|
|
tag_length = tfe->tag_length;
|
|
|
|
if (tag_length >= bufsz)
|
|
|
|
return e_TAG_TOOLONG;
|
|
|
|
|
|
|
|
if (read(fd, buf, tag_length) != tag_length)
|
|
|
|
return e_TAG_SIZEMISMATCH;
|
|
|
|
|
|
|
|
str_setlen(buf, tag_length);
|
|
|
|
return e_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2010-03-17 08:23:50 +00:00
|
|
|
static ssize_t ecread_index_entry(int fd, struct index_entry *buf)
|
2010-03-16 12:15:56 +00:00
|
|
|
{
|
|
|
|
return ecread(fd, buf, 1, index_entry_ec, tc_stat.econ);
|
|
|
|
}
|
|
|
|
|
2010-03-17 08:23:50 +00:00
|
|
|
static ssize_t ecwrite_index_entry(int fd, struct index_entry *buf)
|
2010-03-16 12:15:56 +00:00
|
|
|
{
|
|
|
|
return ecwrite(fd, buf, 1, index_entry_ec, tc_stat.econ);
|
|
|
|
}
|
|
|
|
|
2006-07-23 11:15:28 +00:00
|
|
|
static int open_tag_fd(struct tagcache_header *hdr, int tag, bool write)
|
|
|
|
{
|
|
|
|
int fd;
|
2010-12-06 22:26:31 +00:00
|
|
|
char buf[MAX_PATH];
|
2021-07-30 06:13:57 +00:00
|
|
|
const int bufsz = sizeof(buf);
|
2006-07-23 14:28:12 +00:00
|
|
|
int rc;
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2009-06-03 08:19:32 +00:00
|
|
|
if (TAGCACHE_IS_NUMERIC(tag) || tag < 0 || tag >= TAG_COUNT)
|
2006-07-23 11:15:28 +00:00
|
|
|
return -1;
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2021-07-30 06:13:57 +00:00
|
|
|
snprintf(buf, bufsz, TAGCACHE_FILE_INDEX, tag);
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2010-12-06 22:26:31 +00:00
|
|
|
fd = open(buf, write ? O_RDWR : O_RDONLY);
|
2006-07-23 11:15:28 +00:00
|
|
|
if (fd < 0)
|
|
|
|
{
|
2006-11-10 08:03:33 +00:00
|
|
|
logf("tag file open failed: tag=%d write=%d file=%s", tag, write, buf);
|
|
|
|
tc_stat.ready = false;
|
2006-07-23 11:15:28 +00:00
|
|
|
return fd;
|
|
|
|
}
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2006-07-23 11:15:28 +00:00
|
|
|
/* Check the header. */
|
2007-02-13 21:51:18 +00:00
|
|
|
rc = ecread(fd, hdr, 1, tagcache_header_ec, tc_stat.econ);
|
2006-07-23 14:28:12 +00:00
|
|
|
if (hdr->magic != TAGCACHE_MAGIC || rc != sizeof(struct tagcache_header))
|
2006-07-23 11:15:28 +00:00
|
|
|
{
|
|
|
|
logf("header error");
|
2006-11-10 08:03:33 +00:00
|
|
|
tc_stat.ready = false;
|
2006-07-23 11:15:28 +00:00
|
|
|
close(fd);
|
|
|
|
return -2;
|
|
|
|
}
|
2006-09-26 11:23:18 +00:00
|
|
|
|
2006-07-23 11:15:28 +00:00
|
|
|
return fd;
|
|
|
|
}
|
|
|
|
|
2007-12-16 21:10:26 +00:00
|
|
|
static int open_master_fd(struct master_header *hdr, bool write)
|
|
|
|
{
|
|
|
|
int fd;
|
|
|
|
int rc;
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2010-12-06 22:26:31 +00:00
|
|
|
fd = open(TAGCACHE_FILE_MASTER, write ? O_RDWR : O_RDONLY);
|
2007-12-16 21:10:26 +00:00
|
|
|
if (fd < 0)
|
|
|
|
{
|
|
|
|
logf("master file open failed for R/W");
|
|
|
|
tc_stat.ready = false;
|
|
|
|
return fd;
|
|
|
|
}
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2007-12-16 21:10:26 +00:00
|
|
|
tc_stat.econ = false;
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2007-12-16 21:10:26 +00:00
|
|
|
/* Check the header. */
|
|
|
|
rc = read(fd, hdr, sizeof(struct master_header));
|
2022-02-27 16:01:24 +00:00
|
|
|
if (rc == sizeof(struct master_header) && hdr->tch.magic == TAGCACHE_MAGIC)
|
2007-12-16 21:10:26 +00:00
|
|
|
{
|
|
|
|
/* Success. */
|
|
|
|
return fd;
|
|
|
|
}
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2007-12-16 21:10:26 +00:00
|
|
|
/* Trying to read again, this time with endianess correction enabled. */
|
|
|
|
lseek(fd, 0, SEEK_SET);
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2007-12-16 21:10:26 +00:00
|
|
|
rc = ecread(fd, hdr, 1, master_header_ec, true);
|
2022-02-27 16:01:24 +00:00
|
|
|
if (rc != sizeof(struct master_header) || hdr->tch.magic != TAGCACHE_MAGIC)
|
2007-12-16 21:10:26 +00:00
|
|
|
{
|
|
|
|
logf("header error");
|
|
|
|
tc_stat.ready = false;
|
|
|
|
close(fd);
|
|
|
|
return -2;
|
|
|
|
}
|
|
|
|
|
|
|
|
tc_stat.econ = true;
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2007-12-16 21:10:26 +00:00
|
|
|
return fd;
|
|
|
|
}
|
|
|
|
|
2007-03-10 14:34:56 +00:00
|
|
|
#ifndef __PCTOOL__
|
|
|
|
static bool do_timed_yield(void)
|
|
|
|
{
|
|
|
|
/* Sorting can lock up for quite a while, so yield occasionally */
|
|
|
|
static long wakeup_tick = 0;
|
2009-10-18 15:50:30 +00:00
|
|
|
if (TIME_AFTER(current_tick, wakeup_tick))
|
2007-03-10 14:34:56 +00:00
|
|
|
{
|
|
|
|
yield();
|
2017-02-12 12:13:25 +00:00
|
|
|
wakeup_tick = current_tick + (HZ/50);
|
2007-03-10 14:34:56 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2017-01-18 09:39:35 +00:00
|
|
|
#endif /* __PCTOOL__ */
|
2007-03-10 14:34:56 +00:00
|
|
|
|
2006-07-10 16:22:03 +00:00
|
|
|
#if defined(HAVE_TC_RAMCACHE) && defined(HAVE_DIRCACHE)
|
2011-06-20 20:12:42 +00:00
|
|
|
/* find the ramcache entry corresponding to the file indicated by
|
|
|
|
* filename and dc (it's corresponding dircache id). */
|
2017-01-18 09:39:35 +00:00
|
|
|
static long find_entry_ram(const char *filename)
|
2006-03-26 11:33:42 +00:00
|
|
|
{
|
|
|
|
static long last_pos = 0;
|
2017-01-18 09:39:35 +00:00
|
|
|
struct dircache_fileref dcfref;
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2010-12-08 22:08:08 +00:00
|
|
|
/* Check if tagcache is loaded into ram. */
|
2017-01-18 09:39:35 +00:00
|
|
|
if (!tc_stat.ramcache)
|
2006-07-15 17:36:25 +00:00
|
|
|
return -1;
|
2006-03-26 11:33:42 +00:00
|
|
|
|
2017-01-18 09:39:35 +00:00
|
|
|
if (dircache_search(DCS_CACHED_PATH | DCS_UPDATE_FILEREF, &dcfref,
|
|
|
|
filename) <= 0)
|
2006-03-26 11:33:42 +00:00
|
|
|
{
|
|
|
|
logf("tagcache: file not found.");
|
2006-07-15 17:36:25 +00:00
|
|
|
return -1;
|
2006-03-26 11:33:42 +00:00
|
|
|
}
|
|
|
|
|
2017-01-18 09:39:35 +00:00
|
|
|
/* Search references */
|
|
|
|
int end_pos = current_tcmh.tch.entry_count;
|
|
|
|
while (1)
|
2006-03-26 11:33:42 +00:00
|
|
|
{
|
2017-01-18 09:39:35 +00:00
|
|
|
for (int i = last_pos; i < end_pos; i++)
|
2006-03-26 11:33:42 +00:00
|
|
|
{
|
2017-02-12 12:13:25 +00:00
|
|
|
do_timed_yield();
|
2017-01-18 09:39:35 +00:00
|
|
|
|
2017-02-12 12:13:25 +00:00
|
|
|
if (!(tcramcache.hdr->indices[i].flag & FLAG_DIRCACHE))
|
|
|
|
continue;
|
2017-01-18 09:39:35 +00:00
|
|
|
|
2017-02-12 12:13:25 +00:00
|
|
|
int cmp = dircache_fileref_cmp(&tcrc_dcfrefs[i], &dcfref);
|
|
|
|
if (cmp < 3)
|
|
|
|
continue;
|
2017-01-18 09:39:35 +00:00
|
|
|
|
2017-02-12 12:13:25 +00:00
|
|
|
last_pos = MAX(0, i - 3);
|
|
|
|
return i;
|
2006-03-26 11:33:42 +00:00
|
|
|
}
|
|
|
|
|
2017-01-18 09:39:35 +00:00
|
|
|
if (last_pos == 0)
|
|
|
|
{
|
|
|
|
last_pos = MAX(0, end_pos - 3);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
end_pos = last_pos;
|
2006-03-26 11:33:42 +00:00
|
|
|
last_pos = 0;
|
|
|
|
}
|
|
|
|
|
2006-07-15 17:36:25 +00:00
|
|
|
return -1;
|
2006-03-26 11:33:42 +00:00
|
|
|
}
|
2017-01-17 19:45:07 +00:00
|
|
|
#endif /* defined (HAVE_TC_RAMCACHE) && defined (HAVE_DIRCACHE) */
|
2006-03-26 11:33:42 +00:00
|
|
|
|
2011-02-16 01:01:49 +00:00
|
|
|
static long find_entry_disk(const char *filename_raw, bool localfd)
|
2006-03-26 11:33:42 +00:00
|
|
|
{
|
2006-07-23 11:15:28 +00:00
|
|
|
struct tagcache_header tch;
|
2006-03-26 11:33:42 +00:00
|
|
|
static long last_pos = -1;
|
|
|
|
long pos_history[POS_HISTORY_COUNT];
|
|
|
|
long pos_history_idx = 0;
|
|
|
|
bool found = false;
|
|
|
|
struct tagfile_entry tfe;
|
2006-07-15 17:36:25 +00:00
|
|
|
int fd;
|
2021-08-01 02:24:33 +00:00
|
|
|
char buf[TAGCACHE_BUFSZ];
|
2021-07-30 06:13:57 +00:00
|
|
|
const long bufsz = sizeof(buf);
|
2006-03-26 11:33:42 +00:00
|
|
|
int i;
|
|
|
|
int pos = -1;
|
|
|
|
|
2011-02-16 17:58:57 +00:00
|
|
|
const char *filename = filename_raw;
|
2011-02-16 01:01:49 +00:00
|
|
|
#ifdef APPLICATION
|
2011-02-16 18:40:44 +00:00
|
|
|
char pathbuf[PATH_MAX]; /* Note: Don't use MAX_PATH here, it's too small */
|
2011-02-16 01:01:49 +00:00
|
|
|
if (realpath(filename, pathbuf) == pathbuf)
|
|
|
|
filename = pathbuf;
|
2017-01-18 09:39:35 +00:00
|
|
|
#endif /* APPLICATION */
|
2011-02-16 01:01:49 +00:00
|
|
|
|
2006-11-10 08:03:33 +00:00
|
|
|
if (!tc_stat.ready)
|
2006-07-23 11:15:28 +00:00
|
|
|
return -2;
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2006-07-15 17:36:25 +00:00
|
|
|
fd = filenametag_fd;
|
2009-06-20 16:17:54 +00:00
|
|
|
if (fd < 0 || localfd)
|
2006-03-26 11:33:42 +00:00
|
|
|
{
|
|
|
|
last_pos = -1;
|
2006-07-23 11:15:28 +00:00
|
|
|
if ( (fd = open_tag_fd(&tch, tag_filename, false)) < 0)
|
2006-07-15 17:36:25 +00:00
|
|
|
return -1;
|
2006-03-26 11:33:42 +00:00
|
|
|
}
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2006-03-26 11:33:42 +00:00
|
|
|
check_again:
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2006-03-26 11:33:42 +00:00
|
|
|
if (last_pos > 0)
|
|
|
|
lseek(fd, last_pos, SEEK_SET);
|
2006-07-24 12:10:50 +00:00
|
|
|
else
|
|
|
|
lseek(fd, sizeof(struct tagcache_header), SEEK_SET);
|
2006-03-26 11:33:42 +00:00
|
|
|
|
|
|
|
while (true)
|
|
|
|
{
|
|
|
|
pos = lseek(fd, 0, SEEK_CUR);
|
|
|
|
for (i = pos_history_idx-1; i >= 0; i--)
|
|
|
|
pos_history[i+1] = pos_history[i];
|
|
|
|
pos_history[0] = pos;
|
|
|
|
|
2021-08-01 02:24:33 +00:00
|
|
|
int res = ecread_tagfile_entry_and_tag(fd, &tfe, buf, bufsz);
|
|
|
|
if (res == e_SUCCESS)
|
|
|
|
{;;}
|
|
|
|
else if (res == e_ENTRY_SIZEMISMATCH)
|
|
|
|
break;
|
|
|
|
else
|
2006-03-26 11:33:42 +00:00
|
|
|
{
|
2021-08-01 02:24:33 +00:00
|
|
|
switch (res)
|
|
|
|
{
|
|
|
|
case e_TAG_TOOLONG:
|
|
|
|
logf("too long tag #1");
|
|
|
|
close(fd);
|
|
|
|
if (!localfd)
|
|
|
|
filenametag_fd = -1;
|
|
|
|
last_pos = -1;
|
|
|
|
return -2;
|
|
|
|
case e_TAG_SIZEMISMATCH:
|
|
|
|
logf("read error #2");
|
|
|
|
close(fd);
|
|
|
|
if (!localfd)
|
|
|
|
filenametag_fd = -1;
|
|
|
|
last_pos = -1;
|
|
|
|
return -3;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2006-03-26 11:33:42 +00:00
|
|
|
}
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2011-08-02 22:10:06 +00:00
|
|
|
if (!strcmp(filename, buf))
|
2006-03-26 11:33:42 +00:00
|
|
|
{
|
|
|
|
last_pos = pos_history[pos_history_idx];
|
|
|
|
found = true;
|
|
|
|
break ;
|
|
|
|
}
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2006-03-26 11:33:42 +00:00
|
|
|
if (pos_history_idx < POS_HISTORY_COUNT - 1)
|
|
|
|
pos_history_idx++;
|
|
|
|
}
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2006-03-26 11:33:42 +00:00
|
|
|
/* Not found? */
|
|
|
|
if (!found)
|
|
|
|
{
|
|
|
|
if (last_pos > 0)
|
|
|
|
{
|
|
|
|
last_pos = -1;
|
|
|
|
logf("seek again");
|
|
|
|
goto check_again;
|
|
|
|
}
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2009-06-20 16:17:54 +00:00
|
|
|
if (fd != filenametag_fd || localfd)
|
2006-07-15 17:36:25 +00:00
|
|
|
close(fd);
|
|
|
|
return -4;
|
2006-03-26 11:33:42 +00:00
|
|
|
}
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2009-06-20 16:17:54 +00:00
|
|
|
if (fd != filenametag_fd || localfd)
|
2006-07-15 17:36:25 +00:00
|
|
|
close(fd);
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2006-07-15 17:36:25 +00:00
|
|
|
return tfe.idx_id;
|
|
|
|
}
|
|
|
|
|
2006-07-20 12:19:31 +00:00
|
|
|
static int find_index(const char *filename)
|
2006-07-15 17:36:25 +00:00
|
|
|
{
|
|
|
|
long idx_id = -1;
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2006-07-15 17:36:25 +00:00
|
|
|
#if defined(HAVE_TC_RAMCACHE) && defined(HAVE_DIRCACHE)
|
2017-01-18 09:39:35 +00:00
|
|
|
idx_id = find_entry_ram(filename);
|
2006-07-15 17:36:25 +00:00
|
|
|
#endif
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2006-07-15 17:36:25 +00:00
|
|
|
if (idx_id < 0)
|
2009-06-20 16:17:54 +00:00
|
|
|
idx_id = find_entry_disk(filename, true);
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2006-07-20 12:19:31 +00:00
|
|
|
return idx_id;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool tagcache_find_index(struct tagcache_search *tcs, const char *filename)
|
|
|
|
{
|
|
|
|
int idx_id;
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2006-11-10 08:03:33 +00:00
|
|
|
if (!tc_stat.ready)
|
2006-07-23 11:15:28 +00:00
|
|
|
return false;
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2006-07-20 12:19:31 +00:00
|
|
|
idx_id = find_index(filename);
|
|
|
|
if (idx_id < 0)
|
|
|
|
return false;
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2006-07-15 17:36:25 +00:00
|
|
|
if (!tagcache_search(tcs, tag_filename))
|
|
|
|
return false;
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2006-07-15 17:36:25 +00:00
|
|
|
tcs->entry_count = 0;
|
|
|
|
tcs->idx_id = idx_id;
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2006-07-15 17:36:25 +00:00
|
|
|
return true;
|
2006-03-26 11:33:42 +00:00
|
|
|
}
|
|
|
|
|
2021-07-31 14:26:59 +00:00
|
|
|
static bool get_index(int masterfd, int idxid,
|
2006-07-24 15:35:46 +00:00
|
|
|
struct index_entry *idx, bool use_ram)
|
2006-03-30 12:07:32 +00:00
|
|
|
{
|
2007-12-16 21:10:26 +00:00
|
|
|
bool localfd = false;
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2006-08-30 18:18:37 +00:00
|
|
|
if (idxid < 0)
|
|
|
|
{
|
|
|
|
logf("Incorrect idxid: %d", idxid);
|
|
|
|
return false;
|
|
|
|
}
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2006-03-30 12:07:32 +00:00
|
|
|
#ifdef HAVE_TC_RAMCACHE
|
2006-11-10 08:03:33 +00:00
|
|
|
if (tc_stat.ramcache && use_ram)
|
2006-03-30 12:07:32 +00:00
|
|
|
{
|
2017-01-18 09:39:35 +00:00
|
|
|
if (tcramcache.hdr->indices[idxid].flag & FLAG_DELETED)
|
2006-04-19 18:56:59 +00:00
|
|
|
return false;
|
2017-01-18 09:39:35 +00:00
|
|
|
|
|
|
|
*idx = tcramcache.hdr->indices[idxid];
|
|
|
|
return true;
|
2006-03-30 12:07:32 +00:00
|
|
|
}
|
2017-01-18 09:39:35 +00:00
|
|
|
#endif /* HAVE_TC_RAMCACHE */
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2007-12-16 21:10:26 +00:00
|
|
|
if (masterfd < 0)
|
|
|
|
{
|
|
|
|
struct master_header tcmh;
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2007-12-16 21:10:26 +00:00
|
|
|
localfd = true;
|
|
|
|
masterfd = open_master_fd(&tcmh, false);
|
|
|
|
if (masterfd < 0)
|
|
|
|
return false;
|
|
|
|
}
|
2021-07-31 14:26:59 +00:00
|
|
|
|
|
|
|
lseek(masterfd, idxid * sizeof(struct index_entry)
|
2006-07-20 12:19:31 +00:00
|
|
|
+ sizeof(struct master_header), SEEK_SET);
|
2021-07-31 14:26:59 +00:00
|
|
|
if (ecread_index_entry(masterfd, idx)
|
2007-02-13 21:51:18 +00:00
|
|
|
!= sizeof(struct index_entry))
|
2006-03-30 12:07:32 +00:00
|
|
|
{
|
|
|
|
logf("read error #3");
|
2007-12-16 21:10:26 +00:00
|
|
|
if (localfd)
|
|
|
|
close(masterfd);
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2006-07-15 17:36:25 +00:00
|
|
|
return false;
|
|
|
|
}
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2007-12-16 21:10:26 +00:00
|
|
|
if (localfd)
|
|
|
|
close(masterfd);
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2006-07-23 15:35:53 +00:00
|
|
|
if (idx->flag & FLAG_DELETED)
|
|
|
|
return false;
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2006-07-23 15:35:53 +00:00
|
|
|
return true;
|
2017-01-17 19:45:07 +00:00
|
|
|
|
|
|
|
(void)use_ram;
|
2006-07-23 15:35:53 +00:00
|
|
|
}
|
|
|
|
|
2008-12-15 23:42:19 +00:00
|
|
|
#ifndef __PCTOOL__
|
|
|
|
|
2006-07-23 15:35:53 +00:00
|
|
|
static bool write_index(int masterfd, int idxid, struct index_entry *idx)
|
|
|
|
{
|
2006-07-24 15:35:46 +00:00
|
|
|
/* We need to exclude all memory only flags & tags when writing to disk. */
|
|
|
|
if (idx->flag & FLAG_DIRCACHE)
|
|
|
|
{
|
|
|
|
logf("memory only flags!");
|
|
|
|
return false;
|
|
|
|
}
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2006-07-23 15:35:53 +00:00
|
|
|
#ifdef HAVE_TC_RAMCACHE
|
2007-05-27 11:40:55 +00:00
|
|
|
/* Only update numeric data. Writing the whole index to RAM by memcpy
|
|
|
|
* destroys dircache pointers!
|
|
|
|
*/
|
2006-11-10 08:03:33 +00:00
|
|
|
if (tc_stat.ramcache)
|
2006-07-24 15:35:46 +00:00
|
|
|
{
|
2017-01-18 09:39:35 +00:00
|
|
|
struct index_entry *idx_ram = &tcramcache.hdr->indices[idxid];
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2017-01-18 09:39:35 +00:00
|
|
|
for (int tag = 0; tag < TAG_COUNT; tag++)
|
2007-05-27 11:40:55 +00:00
|
|
|
{
|
2009-06-03 08:19:32 +00:00
|
|
|
if (TAGCACHE_IS_NUMERIC(tag))
|
2007-05-27 11:40:55 +00:00
|
|
|
{
|
|
|
|
idx_ram->tag_seek[tag] = idx->tag_seek[tag];
|
|
|
|
}
|
|
|
|
}
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2008-08-29 21:14:58 +00:00
|
|
|
/* Don't touch the dircache flag or attributes. */
|
2021-07-31 14:26:59 +00:00
|
|
|
idx_ram->flag = (idx->flag & 0x0000ffff)
|
2008-08-29 21:14:58 +00:00
|
|
|
| (idx_ram->flag & (0xffff0000 | FLAG_DIRCACHE));
|
2006-07-24 15:35:46 +00:00
|
|
|
}
|
2017-01-18 09:39:35 +00:00
|
|
|
#endif /* HAVE_TC_RAMCACHE */
|
2021-07-31 14:26:59 +00:00
|
|
|
|
|
|
|
lseek(masterfd, idxid * sizeof(struct index_entry)
|
2006-07-23 15:35:53 +00:00
|
|
|
+ sizeof(struct master_header), SEEK_SET);
|
2010-03-16 12:15:56 +00:00
|
|
|
if (ecwrite_index_entry(masterfd, idx) != sizeof(struct index_entry))
|
2006-07-23 15:35:53 +00:00
|
|
|
{
|
|
|
|
logf("write error #3");
|
2006-08-02 17:39:34 +00:00
|
|
|
logf("idxid: %d", idxid);
|
2006-07-23 15:35:53 +00:00
|
|
|
return false;
|
|
|
|
}
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2006-07-15 17:36:25 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2008-12-15 23:42:19 +00:00
|
|
|
#endif /* !__PCTOOL__ */
|
|
|
|
|
2006-10-15 11:01:18 +00:00
|
|
|
static bool open_files(struct tagcache_search *tcs, int tag)
|
|
|
|
{
|
|
|
|
if (tcs->idxfd[tag] < 0)
|
|
|
|
{
|
2010-12-06 22:26:31 +00:00
|
|
|
char fn[MAX_PATH];
|
2006-10-15 11:01:18 +00:00
|
|
|
|
|
|
|
snprintf(fn, sizeof fn, TAGCACHE_FILE_INDEX, tag);
|
2010-12-06 22:26:31 +00:00
|
|
|
tcs->idxfd[tag] = open(fn, O_RDONLY);
|
2006-10-15 11:01:18 +00:00
|
|
|
}
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2006-10-15 11:01:18 +00:00
|
|
|
if (tcs->idxfd[tag] < 0)
|
|
|
|
{
|
|
|
|
logf("File not open!");
|
|
|
|
return false;
|
|
|
|
}
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2006-10-15 11:01:18 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-02-10 10:44:04 +00:00
|
|
|
static bool retrieve(struct tagcache_search *tcs, IF_DIRCACHE(int idx_id,)
|
2021-08-01 02:24:33 +00:00
|
|
|
struct index_entry *idx, int tag, char *buf, long bufsz)
|
2006-10-15 11:01:18 +00:00
|
|
|
{
|
|
|
|
struct tagfile_entry tfe;
|
|
|
|
long seek;
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2021-08-01 02:24:33 +00:00
|
|
|
str_setlen(buf, 0);
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2009-06-03 08:19:32 +00:00
|
|
|
if (TAGCACHE_IS_NUMERIC(tag))
|
2006-10-15 11:01:18 +00:00
|
|
|
return false;
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2006-10-15 11:01:18 +00:00
|
|
|
seek = idx->tag_seek[tag];
|
|
|
|
if (seek < 0)
|
|
|
|
{
|
|
|
|
logf("Retrieve failed");
|
|
|
|
return false;
|
|
|
|
}
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2006-10-15 11:01:18 +00:00
|
|
|
#ifdef HAVE_TC_RAMCACHE
|
|
|
|
if (tcs->ramsearch)
|
|
|
|
{
|
2017-01-17 19:45:07 +00:00
|
|
|
#ifdef HAVE_DIRCACHE
|
2011-08-04 23:03:43 +00:00
|
|
|
if (tag == tag_filename && (idx->flag & FLAG_DIRCACHE))
|
2006-10-15 11:01:18 +00:00
|
|
|
{
|
2021-08-01 02:24:33 +00:00
|
|
|
if (dircache_get_fileref_path(&tcrc_dcfrefs[idx_id], buf, bufsz) >= 0)
|
2011-08-04 23:03:43 +00:00
|
|
|
return true;
|
2006-10-15 11:01:18 +00:00
|
|
|
}
|
|
|
|
else
|
2017-01-17 19:45:07 +00:00
|
|
|
#endif /* HAVE_DIRCACHE */
|
2006-10-15 11:01:18 +00:00
|
|
|
if (tag != tag_filename)
|
|
|
|
{
|
2017-01-18 09:39:35 +00:00
|
|
|
struct tagfile_entry *ep =
|
|
|
|
(struct tagfile_entry *)&tcramcache.hdr->tags[tag][seek];
|
2021-08-01 02:24:33 +00:00
|
|
|
strlcpy(buf, ep->tag_data, bufsz);
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2006-10-15 11:01:18 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2017-01-18 09:39:35 +00:00
|
|
|
#endif /* HAVE_TC_RAMCACHE */
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2006-10-15 11:01:18 +00:00
|
|
|
if (!open_files(tcs, tag))
|
|
|
|
return false;
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2006-10-15 11:01:18 +00:00
|
|
|
lseek(tcs->idxfd[tag], seek, SEEK_SET);
|
2021-08-01 02:24:33 +00:00
|
|
|
switch (ecread_tagfile_entry_and_tag(tcs->idxfd[tag], &tfe, buf, bufsz))
|
2006-10-15 11:01:18 +00:00
|
|
|
{
|
2021-08-01 02:24:33 +00:00
|
|
|
case e_SUCCESS:
|
|
|
|
break;
|
|
|
|
case e_ENTRY_SIZEMISMATCH:
|
|
|
|
logf("read error #5");
|
|
|
|
return false;
|
|
|
|
case e_TAG_TOOLONG:
|
|
|
|
logf("too long tag #5");
|
|
|
|
return false;
|
|
|
|
case e_TAG_SIZEMISMATCH:
|
|
|
|
logf("read error #6");
|
|
|
|
return false;
|
|
|
|
default:
|
|
|
|
logf("unknown_error");
|
|
|
|
break;;
|
2006-10-15 11:01:18 +00:00
|
|
|
}
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2006-10-15 11:01:18 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-11-23 00:15:13 +00:00
|
|
|
#define COMMAND_QUEUE_IS_EMPTY (command_queue_ridx == command_queue_widx)
|
|
|
|
|
2011-06-06 22:48:57 +00:00
|
|
|
static long find_tag(int tag, int idx_id, const struct index_entry *idx)
|
2010-11-23 00:15:13 +00:00
|
|
|
{
|
2010-11-23 00:27:54 +00:00
|
|
|
#ifndef __PCTOOL__
|
2011-06-06 22:48:57 +00:00
|
|
|
if (! COMMAND_QUEUE_IS_EMPTY && TAGCACHE_IS_NUMERIC(tag))
|
2010-11-23 00:15:13 +00:00
|
|
|
{
|
|
|
|
/* Attempt to find tag data through store-to-load forwarding in
|
|
|
|
command queue */
|
|
|
|
long result = -1;
|
|
|
|
|
|
|
|
mutex_lock(&command_queue_mutex);
|
|
|
|
|
|
|
|
int ridx = command_queue_widx;
|
|
|
|
|
|
|
|
while (ridx != command_queue_ridx)
|
|
|
|
{
|
|
|
|
if (--ridx < 0)
|
|
|
|
ridx = TAGCACHE_COMMAND_QUEUE_LENGTH - 1;
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2010-11-23 00:15:13 +00:00
|
|
|
if (command_queue[ridx].command == CMD_UPDATE_NUMERIC
|
|
|
|
&& command_queue[ridx].idx_id == idx_id
|
|
|
|
&& command_queue[ridx].tag == tag)
|
|
|
|
{
|
|
|
|
result = command_queue[ridx].data;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
mutex_unlock(&command_queue_mutex);
|
|
|
|
|
|
|
|
if (result >= 0)
|
|
|
|
{
|
2011-06-06 22:48:57 +00:00
|
|
|
logf("find_tag: "
|
2010-11-23 00:15:13 +00:00
|
|
|
"Recovered tag %d value %lX from write queue",
|
|
|
|
tag, result);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|
2012-07-31 08:33:27 +00:00
|
|
|
#else
|
|
|
|
(void)idx_id;
|
2010-11-23 00:25:48 +00:00
|
|
|
#endif
|
2010-11-23 00:15:13 +00:00
|
|
|
|
|
|
|
return idx->tag_seek[tag];
|
|
|
|
}
|
|
|
|
|
2021-08-01 02:24:33 +00:00
|
|
|
static inline long sec_in_ms(long ms)
|
|
|
|
{
|
|
|
|
return (ms/1000) % 60;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline long min_in_ms(long ms)
|
|
|
|
{
|
|
|
|
return (ms/1000) / 60;
|
|
|
|
}
|
2010-11-23 00:15:13 +00:00
|
|
|
|
2021-07-31 14:26:59 +00:00
|
|
|
static long check_virtual_tags(int tag, int idx_id,
|
2010-11-23 00:15:13 +00:00
|
|
|
const struct index_entry *idx)
|
2006-07-15 17:36:25 +00:00
|
|
|
{
|
|
|
|
long data = 0;
|
2021-07-31 14:26:59 +00:00
|
|
|
|
|
|
|
switch (tag)
|
2006-07-15 17:36:25 +00:00
|
|
|
{
|
2007-04-12 20:14:05 +00:00
|
|
|
case tag_virt_length_sec:
|
2021-08-01 02:24:33 +00:00
|
|
|
data = sec_in_ms(find_tag(tag_length, idx_id, idx));
|
2007-04-12 20:14:05 +00:00
|
|
|
break;
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2007-04-12 20:14:05 +00:00
|
|
|
case tag_virt_length_min:
|
2021-08-01 02:24:33 +00:00
|
|
|
data = min_in_ms(find_tag(tag_length, idx_id, idx));
|
2007-04-12 20:14:05 +00:00
|
|
|
break;
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2007-04-12 20:21:56 +00:00
|
|
|
case tag_virt_playtime_sec:
|
2021-08-01 02:24:33 +00:00
|
|
|
data = sec_in_ms(find_tag(tag_playtime, idx_id, idx));
|
2007-04-12 20:21:56 +00:00
|
|
|
break;
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2007-04-12 20:21:56 +00:00
|
|
|
case tag_virt_playtime_min:
|
2021-08-01 02:24:33 +00:00
|
|
|
data = min_in_ms(find_tag(tag_playtime, idx_id, idx));
|
2007-04-12 20:21:56 +00:00
|
|
|
break;
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2006-07-15 17:36:25 +00:00
|
|
|
case tag_virt_autoscore:
|
2021-07-31 14:26:59 +00:00
|
|
|
if (find_tag(tag_length, idx_id, idx) == 0
|
2011-06-06 22:48:57 +00:00
|
|
|
|| find_tag(tag_playcount, idx_id, idx) == 0)
|
2006-07-15 17:36:25 +00:00
|
|
|
{
|
|
|
|
data = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2010-02-02 20:49:35 +00:00
|
|
|
/* A straight calculus gives:
|
|
|
|
autoscore = 100 * playtime / length / playcout (1)
|
|
|
|
Now, consider the euclidian division of playtime by length:
|
|
|
|
playtime = alpha * length + beta
|
|
|
|
With:
|
|
|
|
0 <= beta < length
|
|
|
|
Now, (1) becomes:
|
|
|
|
autoscore = 100 * (alpha / playcout + beta / length / playcount)
|
|
|
|
Both terms should be small enough to avoid any overflow
|
|
|
|
*/
|
2021-08-01 02:24:33 +00:00
|
|
|
long playtime = find_tag(tag_playtime, idx_id, idx);
|
|
|
|
long length = find_tag(tag_length, idx_id, idx);
|
|
|
|
long playcount = find_tag(tag_playcount, idx_id, idx);
|
|
|
|
data = 100 * (playtime / length) + (100 * (playtime % length)) / length;
|
|
|
|
data /= playcount;
|
2006-07-15 17:36:25 +00:00
|
|
|
}
|
|
|
|
break;
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2007-02-25 20:41:51 +00:00
|
|
|
/* How many commits before the file has been added to the DB. */
|
|
|
|
case tag_virt_entryage:
|
2021-07-31 14:26:59 +00:00
|
|
|
data = current_tcmh.commitid
|
2011-06-06 22:48:57 +00:00
|
|
|
- find_tag(tag_commitid, idx_id, idx) - 1;
|
2007-02-25 20:41:51 +00:00
|
|
|
break;
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2011-07-31 16:26:35 +00:00
|
|
|
case tag_virt_basename:
|
|
|
|
tag = tag_filename; /* return filename; caller handles basename */
|
|
|
|
/* FALLTHRU */
|
|
|
|
|
2006-07-15 17:36:25 +00:00
|
|
|
default:
|
2011-06-06 22:48:57 +00:00
|
|
|
data = find_tag(tag, idx_id, idx);
|
2006-03-30 12:07:32 +00:00
|
|
|
}
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2006-07-15 17:36:25 +00:00
|
|
|
return data;
|
2006-03-30 12:07:32 +00:00
|
|
|
}
|
|
|
|
|
2006-04-10 10:26:24 +00:00
|
|
|
long tagcache_get_numeric(const struct tagcache_search *tcs, int tag)
|
|
|
|
{
|
2006-07-15 17:36:25 +00:00
|
|
|
struct index_entry idx;
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2006-11-10 08:03:33 +00:00
|
|
|
if (!tc_stat.ready)
|
2006-07-23 11:15:28 +00:00
|
|
|
return false;
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2009-06-03 08:19:32 +00:00
|
|
|
if (!TAGCACHE_IS_NUMERIC(tag))
|
2006-04-10 10:26:24 +00:00
|
|
|
return -1;
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2006-07-24 15:35:46 +00:00
|
|
|
if (!get_index(tcs->masterfd, tcs->idx_id, &idx, true))
|
2006-07-15 17:36:25 +00:00
|
|
|
return -2;
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2010-11-23 00:15:13 +00:00
|
|
|
return check_virtual_tags(tag, tcs->idx_id, &idx);
|
2006-04-10 10:26:24 +00:00
|
|
|
}
|
|
|
|
|
2006-08-13 14:53:19 +00:00
|
|
|
inline static bool str_ends_with(const char *str1, const char *str2)
|
|
|
|
{
|
2022-03-19 05:59:07 +00:00
|
|
|
logf_clauses("%s %s %s", str1, __func__, str2);
|
2006-08-13 14:53:19 +00:00
|
|
|
int str_len = strlen(str1);
|
|
|
|
int clause_len = strlen(str2);
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2006-08-13 14:53:19 +00:00
|
|
|
if (clause_len > str_len)
|
|
|
|
return false;
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2006-08-13 14:53:19 +00:00
|
|
|
return !strcasecmp(&str1[str_len - clause_len], str2);
|
|
|
|
}
|
|
|
|
|
2006-10-09 10:23:35 +00:00
|
|
|
inline static bool str_oneof(const char *str, const char *list)
|
|
|
|
{
|
2022-03-19 05:59:07 +00:00
|
|
|
logf_clauses("%s %s %s", str, __func__, list);
|
2006-10-09 10:23:35 +00:00
|
|
|
const char *sep;
|
|
|
|
int l, len = strlen(str);
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2006-10-09 10:23:35 +00:00
|
|
|
while (*list)
|
|
|
|
{
|
|
|
|
sep = strchr(list, '|');
|
2017-01-15 23:10:38 +00:00
|
|
|
l = sep ? (intptr_t)sep - (intptr_t)list : (int)strlen(list);
|
2006-10-09 10:23:35 +00:00
|
|
|
if ((l==len) && !strncasecmp(str, list, len))
|
|
|
|
return true;
|
|
|
|
list += sep ? l + 1 : l;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-03-19 06:15:35 +00:00
|
|
|
inline static bool str_begins_ends_oneof(const char *str, const char *list, bool begins)
|
|
|
|
{
|
|
|
|
logf_clauses("%s %s (%s) %s", str, __func__, begins ? "begins" : "ends", list);
|
|
|
|
const char *sep;
|
|
|
|
int l, p, len = strlen(str);
|
|
|
|
|
|
|
|
while (*list)
|
|
|
|
{
|
|
|
|
sep = strchr(list, '|');
|
|
|
|
l = sep ? (intptr_t)sep - (intptr_t)list : (int)strlen(list);
|
|
|
|
p = begins ? 0 : len - l;
|
|
|
|
if (l <= len && !strncasecmp(&str[p], list, l))
|
|
|
|
return true;
|
|
|
|
list += sep ? l + 1 : l;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2006-04-03 18:57:34 +00:00
|
|
|
static bool check_against_clause(long numeric, const char *str,
|
|
|
|
const struct tagcache_search_clause *clause)
|
|
|
|
{
|
2006-10-05 09:49:18 +00:00
|
|
|
if (clause->numeric)
|
2006-04-03 18:57:34 +00:00
|
|
|
{
|
2006-10-05 09:49:18 +00:00
|
|
|
switch (clause->type)
|
|
|
|
{
|
|
|
|
case clause_is:
|
2006-04-03 18:57:34 +00:00
|
|
|
return numeric == clause->numeric_data;
|
2006-10-05 09:49:18 +00:00
|
|
|
case clause_is_not:
|
|
|
|
return numeric != clause->numeric_data;
|
|
|
|
case clause_gt:
|
|
|
|
return numeric > clause->numeric_data;
|
|
|
|
case clause_gteq:
|
|
|
|
return numeric >= clause->numeric_data;
|
|
|
|
case clause_lt:
|
|
|
|
return numeric < clause->numeric_data;
|
|
|
|
case clause_lteq:
|
2021-07-31 14:26:59 +00:00
|
|
|
return numeric <= clause->numeric_data;
|
2006-10-05 09:49:18 +00:00
|
|
|
default:
|
|
|
|
logf("Incorrect numeric tag: %d", clause->type);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
switch (clause->type)
|
|
|
|
{
|
|
|
|
case clause_is:
|
2006-04-03 18:57:34 +00:00
|
|
|
return !strcasecmp(clause->str, str);
|
2006-10-05 09:49:18 +00:00
|
|
|
case clause_is_not:
|
|
|
|
return strcasecmp(clause->str, str);
|
|
|
|
case clause_gt:
|
|
|
|
return 0>strcasecmp(clause->str, str);
|
|
|
|
case clause_gteq:
|
|
|
|
return 0>=strcasecmp(clause->str, str);
|
|
|
|
case clause_lt:
|
|
|
|
return 0<strcasecmp(clause->str, str);
|
|
|
|
case clause_lteq:
|
|
|
|
return 0<=strcasecmp(clause->str, str);
|
|
|
|
case clause_contains:
|
|
|
|
return (strcasestr(str, clause->str) != NULL);
|
|
|
|
case clause_not_contains:
|
|
|
|
return (strcasestr(str, clause->str) == NULL);
|
|
|
|
case clause_begins_with:
|
|
|
|
return (strcasestr(str, clause->str) == str);
|
|
|
|
case clause_not_begins_with:
|
2007-02-27 11:09:09 +00:00
|
|
|
return (strcasestr(str, clause->str) != str);
|
2006-10-05 09:49:18 +00:00
|
|
|
case clause_ends_with:
|
|
|
|
return str_ends_with(str, clause->str);
|
|
|
|
case clause_not_ends_with:
|
|
|
|
return !str_ends_with(str, clause->str);
|
2006-10-09 10:23:35 +00:00
|
|
|
case clause_oneof:
|
|
|
|
return str_oneof(str, clause->str);
|
2022-03-19 06:15:35 +00:00
|
|
|
case clause_begins_oneof:
|
|
|
|
return str_begins_ends_oneof(str, clause->str, true);
|
|
|
|
case clause_ends_oneof:
|
|
|
|
return str_begins_ends_oneof(str, clause->str, false);
|
2006-10-05 09:49:18 +00:00
|
|
|
default:
|
|
|
|
logf("Incorrect tag: %d", clause->type);
|
|
|
|
}
|
2006-04-03 18:57:34 +00:00
|
|
|
}
|
2006-08-13 14:53:19 +00:00
|
|
|
|
2006-04-03 18:57:34 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2006-12-25 14:01:47 +00:00
|
|
|
static bool check_clauses(struct tagcache_search *tcs,
|
|
|
|
struct index_entry *idx,
|
2011-05-10 10:25:41 +00:00
|
|
|
struct tagcache_search_clause **clauses, int count)
|
2006-10-15 11:01:18 +00:00
|
|
|
{
|
|
|
|
int i;
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2011-05-10 10:25:41 +00:00
|
|
|
/* Go through all conditional clauses. */
|
|
|
|
for (i = 0; i < count; i++)
|
2006-10-15 11:01:18 +00:00
|
|
|
{
|
2011-05-10 10:25:41 +00:00
|
|
|
int seek;
|
|
|
|
char buf[256];
|
2021-07-30 06:13:57 +00:00
|
|
|
const int bufsz = sizeof(buf);
|
2011-06-06 22:49:07 +00:00
|
|
|
char *str = buf;
|
2011-05-10 10:25:41 +00:00
|
|
|
struct tagcache_search_clause *clause = clauses[i];
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2022-03-19 05:59:07 +00:00
|
|
|
logf_clauses("%s clause %d %s %s [%ld] %s",
|
|
|
|
"Checking", i, tag_type_str[clause->type],
|
|
|
|
tags_str[clause->tag], clause->numeric_data,
|
|
|
|
(clause->numeric || clause->str == NULL) ? "[NUMERIC?]" : clause->str);
|
|
|
|
|
2011-05-10 10:25:41 +00:00
|
|
|
if (clause->type == clause_logical_or)
|
2022-03-19 05:59:07 +00:00
|
|
|
{
|
|
|
|
logf_clauses("Bailing");
|
2011-05-10 10:25:41 +00:00
|
|
|
break; /* all conditions before logical-or satisfied --
|
|
|
|
stop processing clauses */
|
2022-03-19 05:59:07 +00:00
|
|
|
}
|
2011-06-06 22:49:07 +00:00
|
|
|
seek = check_virtual_tags(clause->tag, tcs->idx_id, idx);
|
2011-05-10 10:25:41 +00:00
|
|
|
|
2011-06-06 22:49:07 +00:00
|
|
|
#ifdef HAVE_TC_RAMCACHE
|
2011-05-10 10:25:41 +00:00
|
|
|
if (tcs->ramsearch)
|
2006-10-15 11:01:18 +00:00
|
|
|
{
|
|
|
|
struct tagfile_entry *tfe;
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2011-05-10 10:25:41 +00:00
|
|
|
if (!TAGCACHE_IS_NUMERIC(clause->tag))
|
2006-10-15 11:01:18 +00:00
|
|
|
{
|
2011-07-31 16:26:35 +00:00
|
|
|
if (clause->tag == tag_filename
|
|
|
|
|| clause->tag == tag_virt_basename)
|
2006-10-15 11:01:18 +00:00
|
|
|
{
|
2017-02-10 10:44:04 +00:00
|
|
|
retrieve(tcs, IF_DIRCACHE(tcs->idx_id,) idx, tag_filename,
|
2021-07-30 06:13:57 +00:00
|
|
|
buf, bufsz);
|
2006-10-15 11:01:18 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2011-08-02 14:35:10 +00:00
|
|
|
tfe = (struct tagfile_entry *)
|
2017-01-18 09:39:35 +00:00
|
|
|
&tcramcache.hdr->tags[clause->tag][seek];
|
2011-08-30 14:01:45 +00:00
|
|
|
/* str points to movable data, but no locking required here,
|
|
|
|
* as no yield() is following */
|
2006-10-15 11:01:18 +00:00
|
|
|
str = tfe->tag_data;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-05-10 10:25:41 +00:00
|
|
|
else
|
2017-01-18 09:39:35 +00:00
|
|
|
#endif /* HAVE_TC_RAMCACHE */
|
2006-10-15 11:01:18 +00:00
|
|
|
{
|
|
|
|
struct tagfile_entry tfe;
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2011-05-10 10:25:41 +00:00
|
|
|
if (!TAGCACHE_IS_NUMERIC(clause->tag))
|
2006-10-15 11:01:18 +00:00
|
|
|
{
|
2011-07-31 16:26:35 +00:00
|
|
|
int tag = clause->tag;
|
|
|
|
if (tag == tag_virt_basename)
|
|
|
|
tag = tag_filename;
|
|
|
|
|
|
|
|
int fd = tcs->idxfd[tag];
|
2006-10-15 11:01:18 +00:00
|
|
|
lseek(fd, seek, SEEK_SET);
|
|
|
|
|
2021-08-01 02:24:33 +00:00
|
|
|
switch (ecread_tagfile_entry_and_tag(fd, &tfe, str, bufsz))
|
2021-07-30 06:13:57 +00:00
|
|
|
{
|
2021-08-01 02:24:33 +00:00
|
|
|
case e_SUCCESS:
|
|
|
|
break;
|
|
|
|
case e_ENTRY_SIZEMISMATCH:
|
|
|
|
logf("read error #15");
|
|
|
|
return false;
|
|
|
|
case e_TAG_TOOLONG:
|
|
|
|
logf("too long tag #6");
|
|
|
|
return false;
|
|
|
|
case e_TAG_SIZEMISMATCH:
|
|
|
|
logf("read error #16");
|
|
|
|
return false;
|
|
|
|
default:
|
|
|
|
logf("unknown_error");
|
|
|
|
break;;
|
2021-07-30 06:13:57 +00:00
|
|
|
}
|
2021-08-01 02:24:33 +00:00
|
|
|
|
2006-10-15 11:01:18 +00:00
|
|
|
/* Check if entry has been deleted. */
|
|
|
|
if (str[0] == '\0')
|
2011-05-10 10:25:41 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-07-31 16:26:35 +00:00
|
|
|
if (clause->tag == tag_virt_basename)
|
|
|
|
{
|
|
|
|
char *basename = strrchr(str, '/');
|
|
|
|
if (basename)
|
|
|
|
str = basename + 1;
|
|
|
|
}
|
|
|
|
|
2011-05-10 10:25:41 +00:00
|
|
|
if (!check_against_clause(seek, str, clause))
|
|
|
|
{
|
|
|
|
/* Clause failed -- try finding a logical-or clause */
|
|
|
|
while (++i < count)
|
|
|
|
{
|
|
|
|
if (clauses[i]->type == clause_logical_or)
|
2006-10-15 11:01:18 +00:00
|
|
|
break;
|
|
|
|
}
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2011-05-10 10:25:41 +00:00
|
|
|
if (i < count) /* Found logical-or? */
|
|
|
|
continue; /* Check clauses after logical-or */
|
|
|
|
|
|
|
|
return false;
|
2006-10-15 11:01:18 +00:00
|
|
|
}
|
2022-03-19 05:59:07 +00:00
|
|
|
|
|
|
|
logf_clauses("%s clause %d %s %s [%ld] %s",
|
|
|
|
"Found", i, tag_type_str[clause->type],
|
|
|
|
tags_str[clause->tag], clause->numeric_data,
|
|
|
|
(clause->numeric || clause->str == NULL) ? "[NUMERIC?]" : clause->str);
|
2006-10-15 11:01:18 +00:00
|
|
|
}
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2006-10-15 11:01:18 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool tagcache_check_clauses(struct tagcache_search *tcs,
|
|
|
|
struct tagcache_search_clause **clause, int count)
|
|
|
|
{
|
|
|
|
struct index_entry idx;
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2006-10-15 11:01:18 +00:00
|
|
|
if (count == 0)
|
|
|
|
return true;
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2006-10-15 11:01:18 +00:00
|
|
|
if (!get_index(tcs->masterfd, tcs->idx_id, &idx, true))
|
|
|
|
return false;
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2006-10-15 11:01:18 +00:00
|
|
|
return check_clauses(tcs, &idx, clause, count);
|
|
|
|
}
|
|
|
|
|
2022-03-02 03:37:11 +00:00
|
|
|
static bool add_uniqbuf(struct tagcache_search *tcs, uint32_t id)
|
2006-08-25 13:22:46 +00:00
|
|
|
{
|
|
|
|
int i;
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2006-08-25 13:22:46 +00:00
|
|
|
/* If uniq buffer is not defined we must return true for search to work. */
|
2009-06-03 08:19:32 +00:00
|
|
|
if (tcs->unique_list == NULL || (!TAGCACHE_IS_UNIQUE(tcs->type)
|
2011-06-06 22:48:53 +00:00
|
|
|
&& !TAGCACHE_IS_NUMERIC(tcs->type)))
|
2006-08-25 21:13:49 +00:00
|
|
|
{
|
2006-08-25 13:22:46 +00:00
|
|
|
return true;
|
2006-08-25 21:13:49 +00:00
|
|
|
}
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2022-03-02 03:37:11 +00:00
|
|
|
if (id <= UINT16_MAX)
|
2006-08-25 13:22:46 +00:00
|
|
|
{
|
2022-03-02 03:37:11 +00:00
|
|
|
/* place two 16-bit entries in a single 32-bit slot */
|
|
|
|
uint32_t idtmp;
|
|
|
|
union uentry{
|
|
|
|
uint16_t u16[2];
|
|
|
|
uint32_t u32;
|
|
|
|
} *entry;
|
|
|
|
id |= 1; /*odd - flag 16-bit entry */
|
|
|
|
for (i = 0; i < tcs->unique_list_count; i++)
|
|
|
|
{
|
|
|
|
entry = (union uentry *) &tcs->unique_list[i];
|
|
|
|
if ((entry->u32 & 1) == 0) /* contains a 32-bit entry */
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/* Return false if entry is found. */
|
|
|
|
if (entry->u16[0] == id || entry->u16[1] == id)
|
|
|
|
{
|
2022-03-19 05:59:07 +00:00
|
|
|
//logf("%d Exists (16) @ %d", id, i);
|
2022-03-02 03:37:11 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (entry->u16[1] == 0 && (entry->u16[0] & 1) == 1)
|
|
|
|
{
|
|
|
|
entry->u16[1] = id & UINT16_MAX;
|
|
|
|
return true; /*no more 16bit entries add to empty 16bit slot */
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
/* Not Found and no empty slot add a new entry */
|
|
|
|
entry = (union uentry *) &idtmp;
|
|
|
|
entry->u16[1] = 0;
|
|
|
|
entry->u16[0] = id & UINT16_MAX;
|
|
|
|
id = idtmp;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
id &= ~1; /* even - flag 32-bit entry */
|
|
|
|
for (i = 0; i < tcs->unique_list_count; i++)
|
|
|
|
{
|
|
|
|
/* Return false if entry is found. */
|
|
|
|
if (tcs->unique_list[i] == id)
|
|
|
|
{
|
2022-03-19 05:59:07 +00:00
|
|
|
//logf("%d Exists (32)@ %d", id, i);
|
2022-03-02 03:37:11 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2006-08-25 13:22:46 +00:00
|
|
|
}
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2006-08-25 13:22:46 +00:00
|
|
|
if (tcs->unique_list_count < tcs->unique_list_capacity)
|
|
|
|
{
|
|
|
|
tcs->unique_list[i] = id;
|
|
|
|
tcs->unique_list_count++;
|
|
|
|
}
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2006-08-25 13:22:46 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2006-03-26 11:33:42 +00:00
|
|
|
static bool build_lookup_list(struct tagcache_search *tcs)
|
|
|
|
{
|
|
|
|
struct index_entry entry;
|
2009-06-20 16:17:54 +00:00
|
|
|
int i, j;
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2006-03-26 11:33:42 +00:00
|
|
|
tcs->seek_list_count = 0;
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2006-03-26 11:33:42 +00:00
|
|
|
#ifdef HAVE_TC_RAMCACHE
|
2017-01-18 09:39:35 +00:00
|
|
|
if (tcs->ramsearch)
|
2006-03-26 11:33:42 +00:00
|
|
|
{
|
2017-01-18 09:39:35 +00:00
|
|
|
tcrc_buffer_lock(); /* lock because below makes a pointer to movable data */
|
|
|
|
|
2011-06-19 14:36:26 +00:00
|
|
|
for (i = tcs->seek_pos; i < current_tcmh.tch.entry_count; i++)
|
2006-03-26 11:33:42 +00:00
|
|
|
{
|
2009-06-20 16:17:54 +00:00
|
|
|
struct tagcache_seeklist_entry *seeklist;
|
2011-08-30 14:01:45 +00:00
|
|
|
/* idx points to movable data, don't yield or reload */
|
2017-01-18 09:39:35 +00:00
|
|
|
struct index_entry *idx = &tcramcache.hdr->indices[i];
|
2006-03-26 11:33:42 +00:00
|
|
|
if (tcs->seek_list_count == SEEK_LIST_SIZE)
|
|
|
|
break ;
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2006-04-19 18:56:59 +00:00
|
|
|
/* Skip deleted files. */
|
2006-10-15 11:01:18 +00:00
|
|
|
if (idx->flag & FLAG_DELETED)
|
2006-04-19 18:56:59 +00:00
|
|
|
continue;
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2006-04-09 20:00:49 +00:00
|
|
|
/* Go through all filters.. */
|
2006-03-26 11:33:42 +00:00
|
|
|
for (j = 0; j < tcs->filter_count; j++)
|
|
|
|
{
|
2006-10-15 11:01:18 +00:00
|
|
|
if (idx->tag_seek[tcs->filter_tag[j]] != tcs->filter_seek[j])
|
2006-08-25 21:13:49 +00:00
|
|
|
{
|
2006-03-26 11:33:42 +00:00
|
|
|
break ;
|
2006-08-25 21:13:49 +00:00
|
|
|
}
|
2006-03-26 11:33:42 +00:00
|
|
|
}
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2006-03-26 11:33:42 +00:00
|
|
|
if (j < tcs->filter_count)
|
|
|
|
continue ;
|
2006-04-09 20:00:49 +00:00
|
|
|
|
2006-10-15 11:01:18 +00:00
|
|
|
/* Check for conditions. */
|
|
|
|
if (!check_clauses(tcs, idx, tcs->clause, tcs->clause_count))
|
|
|
|
continue;
|
2011-08-30 14:01:45 +00:00
|
|
|
/* Add to the seek list if not already in uniq buffer (doesn't yield)*/
|
2006-10-15 11:01:18 +00:00
|
|
|
if (!add_uniqbuf(tcs, idx->tag_seek[tcs->type]))
|
2006-08-25 13:22:46 +00:00
|
|
|
continue;
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2006-03-26 11:33:42 +00:00
|
|
|
/* Lets add it. */
|
2009-06-20 16:17:54 +00:00
|
|
|
seeklist = &tcs->seeklist[tcs->seek_list_count];
|
|
|
|
seeklist->seek = idx->tag_seek[tcs->type];
|
|
|
|
seeklist->flag = idx->flag;
|
|
|
|
seeklist->idx_id = i;
|
2006-08-25 13:22:46 +00:00
|
|
|
tcs->seek_list_count++;
|
2006-03-26 11:33:42 +00:00
|
|
|
}
|
2017-01-18 09:39:35 +00:00
|
|
|
|
|
|
|
tcrc_buffer_unlock();
|
|
|
|
|
2006-03-26 11:33:42 +00:00
|
|
|
tcs->seek_pos = i;
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2006-03-26 11:33:42 +00:00
|
|
|
return tcs->seek_list_count > 0;
|
|
|
|
}
|
2017-01-18 09:39:35 +00:00
|
|
|
#endif /* HAVE_TC_RAMCACHE */
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2009-06-20 16:17:54 +00:00
|
|
|
if (tcs->masterfd < 0)
|
|
|
|
{
|
|
|
|
struct master_header tcmh;
|
|
|
|
tcs->masterfd = open_master_fd(&tcmh, false);
|
|
|
|
}
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2006-04-09 20:00:49 +00:00
|
|
|
lseek(tcs->masterfd, tcs->seek_pos * sizeof(struct index_entry) +
|
2006-07-20 12:19:31 +00:00
|
|
|
sizeof(struct master_header), SEEK_SET);
|
2021-07-31 14:26:59 +00:00
|
|
|
|
|
|
|
while (ecread_index_entry(tcs->masterfd, &entry)
|
2007-02-13 21:51:18 +00:00
|
|
|
== sizeof(struct index_entry))
|
2006-03-26 11:33:42 +00:00
|
|
|
{
|
2009-06-20 16:17:54 +00:00
|
|
|
struct tagcache_seeklist_entry *seeklist;
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2007-03-11 08:52:33 +00:00
|
|
|
if (tcs->seek_list_count == SEEK_LIST_SIZE)
|
|
|
|
break ;
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2009-06-20 16:17:54 +00:00
|
|
|
i = tcs->seek_pos;
|
2007-03-11 08:52:33 +00:00
|
|
|
tcs->seek_pos++;
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2006-07-10 16:22:03 +00:00
|
|
|
/* Check if entry has been deleted. */
|
|
|
|
if (entry.flag & FLAG_DELETED)
|
|
|
|
continue;
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2006-04-09 20:00:49 +00:00
|
|
|
/* Go through all filters.. */
|
2009-06-20 16:17:54 +00:00
|
|
|
for (j = 0; j < tcs->filter_count; j++)
|
2006-03-26 11:33:42 +00:00
|
|
|
{
|
2009-06-20 16:17:54 +00:00
|
|
|
if (entry.tag_seek[tcs->filter_tag[j]] != tcs->filter_seek[j])
|
2006-03-26 11:33:42 +00:00
|
|
|
break ;
|
|
|
|
}
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2009-06-20 16:17:54 +00:00
|
|
|
if (j < tcs->filter_count)
|
2006-03-26 11:33:42 +00:00
|
|
|
continue ;
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2006-04-03 18:57:34 +00:00
|
|
|
/* Check for conditions. */
|
2006-10-15 11:01:18 +00:00
|
|
|
if (!check_clauses(tcs, &entry, tcs->clause, tcs->clause_count))
|
|
|
|
continue;
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2006-08-25 13:22:46 +00:00
|
|
|
/* Add to the seek list if not already in uniq buffer. */
|
|
|
|
if (!add_uniqbuf(tcs, entry.tag_seek[tcs->type]))
|
|
|
|
continue;
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2006-03-26 11:33:42 +00:00
|
|
|
/* Lets add it. */
|
2009-06-20 16:17:54 +00:00
|
|
|
seeklist = &tcs->seeklist[tcs->seek_list_count];
|
|
|
|
seeklist->seek = entry.tag_seek[tcs->type];
|
|
|
|
seeklist->flag = entry.flag;
|
|
|
|
seeklist->idx_id = i;
|
2006-08-25 13:22:46 +00:00
|
|
|
tcs->seek_list_count++;
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2006-08-25 21:13:49 +00:00
|
|
|
yield();
|
2006-03-26 11:33:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return tcs->seek_list_count > 0;
|
|
|
|
}
|
|
|
|
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2006-07-20 12:19:31 +00:00
|
|
|
static void remove_files(void)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
char buf[MAX_PATH];
|
2021-07-30 06:13:57 +00:00
|
|
|
const int bufsz = sizeof(buf);
|
|
|
|
|
2006-11-10 08:03:33 +00:00
|
|
|
tc_stat.ready = false;
|
|
|
|
tc_stat.ramcache = false;
|
2007-02-13 21:51:18 +00:00
|
|
|
tc_stat.econ = false;
|
2010-12-06 22:26:31 +00:00
|
|
|
remove(TAGCACHE_FILE_MASTER);
|
2006-07-20 12:19:31 +00:00
|
|
|
for (i = 0; i < TAG_COUNT; i++)
|
|
|
|
{
|
2009-06-03 08:19:32 +00:00
|
|
|
if (TAGCACHE_IS_NUMERIC(i))
|
2006-07-20 12:19:31 +00:00
|
|
|
continue;
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2021-07-30 06:13:57 +00:00
|
|
|
snprintf(buf, bufsz, TAGCACHE_FILE_INDEX, i);
|
2010-12-06 22:26:31 +00:00
|
|
|
remove(buf);
|
2006-07-20 12:19:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-07-23 11:15:28 +00:00
|
|
|
|
2007-02-25 20:41:51 +00:00
|
|
|
static bool check_all_headers(void)
|
|
|
|
{
|
|
|
|
struct master_header myhdr;
|
|
|
|
struct tagcache_header tch;
|
|
|
|
int tag;
|
|
|
|
int fd;
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2007-02-25 20:41:51 +00:00
|
|
|
if ( (fd = open_master_fd(&myhdr, false)) < 0)
|
|
|
|
return false;
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2007-02-25 20:41:51 +00:00
|
|
|
close(fd);
|
|
|
|
if (myhdr.dirty)
|
|
|
|
{
|
|
|
|
logf("tagcache is dirty!");
|
|
|
|
return false;
|
|
|
|
}
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2007-02-25 20:41:51 +00:00
|
|
|
memcpy(¤t_tcmh, &myhdr, sizeof(struct master_header));
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2007-02-25 20:41:51 +00:00
|
|
|
for (tag = 0; tag < TAG_COUNT; tag++)
|
|
|
|
{
|
2009-06-03 08:19:32 +00:00
|
|
|
if (TAGCACHE_IS_NUMERIC(tag))
|
2007-02-25 20:41:51 +00:00
|
|
|
continue;
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2007-02-25 20:41:51 +00:00
|
|
|
if ( (fd = open_tag_fd(&tch, tag, false)) < 0)
|
|
|
|
return false;
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2007-02-25 20:41:51 +00:00
|
|
|
close(fd);
|
|
|
|
}
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2007-02-25 20:41:51 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2006-03-26 11:33:42 +00:00
|
|
|
bool tagcache_search(struct tagcache_search *tcs, int tag)
|
|
|
|
{
|
2006-07-20 12:19:31 +00:00
|
|
|
struct tagcache_header tag_hdr;
|
|
|
|
struct master_header master_hdr;
|
2006-04-08 08:03:51 +00:00
|
|
|
int i;
|
2006-03-26 11:33:42 +00:00
|
|
|
|
2006-08-02 17:39:34 +00:00
|
|
|
while (read_lock)
|
|
|
|
sleep(1);
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2006-04-03 18:57:34 +00:00
|
|
|
memset(tcs, 0, sizeof(struct tagcache_search));
|
2006-11-10 08:03:33 +00:00
|
|
|
if (tc_stat.commit_step > 0 || !tc_stat.ready)
|
2006-04-16 17:32:54 +00:00
|
|
|
return false;
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2006-03-26 11:33:42 +00:00
|
|
|
tcs->position = sizeof(struct tagcache_header);
|
|
|
|
tcs->type = tag;
|
|
|
|
tcs->seek_pos = 0;
|
2009-06-20 16:17:54 +00:00
|
|
|
tcs->list_position = 0;
|
2006-03-26 11:33:42 +00:00
|
|
|
tcs->seek_list_count = 0;
|
|
|
|
tcs->filter_count = 0;
|
2006-04-03 18:57:34 +00:00
|
|
|
tcs->masterfd = -1;
|
2006-03-26 11:33:42 +00:00
|
|
|
|
2006-04-08 08:03:51 +00:00
|
|
|
for (i = 0; i < TAG_COUNT; i++)
|
|
|
|
tcs->idxfd[i] = -1;
|
|
|
|
|
2006-03-26 11:33:42 +00:00
|
|
|
#ifndef HAVE_TC_RAMCACHE
|
|
|
|
tcs->ramsearch = false;
|
|
|
|
#else
|
2006-11-10 08:03:33 +00:00
|
|
|
tcs->ramsearch = tc_stat.ramcache;
|
2006-03-26 11:33:42 +00:00
|
|
|
if (tcs->ramsearch)
|
|
|
|
{
|
2017-01-18 09:39:35 +00:00
|
|
|
tcs->entry_count = tcramcache.hdr->entry_count[tcs->type];
|
2006-03-26 11:33:42 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
{
|
2009-06-20 16:17:54 +00:00
|
|
|
/* Always open as R/W so we can pass tcs to functions that modify data also
|
|
|
|
* without failing. */
|
|
|
|
tcs->masterfd = open_master_fd(&master_hdr, true);
|
|
|
|
if (tcs->masterfd < 0)
|
|
|
|
return false;
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2009-06-03 08:19:32 +00:00
|
|
|
if (!TAGCACHE_IS_NUMERIC(tcs->type))
|
2006-08-02 17:39:34 +00:00
|
|
|
{
|
2006-08-25 21:13:49 +00:00
|
|
|
tcs->idxfd[tcs->type] = open_tag_fd(&tag_hdr, tcs->type, false);
|
|
|
|
if (tcs->idxfd[tcs->type] < 0)
|
|
|
|
return false;
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2009-06-20 16:17:54 +00:00
|
|
|
tcs->entry_count = tag_hdr.entry_count;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
tcs->entry_count = master_hdr.tch.entry_count;
|
2006-08-02 17:39:34 +00:00
|
|
|
}
|
2006-03-26 11:33:42 +00:00
|
|
|
}
|
|
|
|
|
2006-08-02 17:39:34 +00:00
|
|
|
tcs->valid = true;
|
|
|
|
tcs->initialized = true;
|
|
|
|
write_lock++;
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2006-03-26 11:33:42 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2006-08-25 13:22:46 +00:00
|
|
|
void tagcache_search_set_uniqbuf(struct tagcache_search *tcs,
|
|
|
|
void *buffer, long length)
|
|
|
|
{
|
2022-03-02 03:37:11 +00:00
|
|
|
tcs->unique_list = (uint32_t *)buffer;
|
2006-08-25 13:22:46 +00:00
|
|
|
tcs->unique_list_capacity = length / sizeof(*tcs->unique_list);
|
|
|
|
tcs->unique_list_count = 0;
|
2022-03-02 03:37:11 +00:00
|
|
|
memset(tcs->unique_list, 0, tcs->unique_list_capacity);
|
2006-08-25 13:22:46 +00:00
|
|
|
}
|
|
|
|
|
2006-03-26 11:33:42 +00:00
|
|
|
bool tagcache_search_add_filter(struct tagcache_search *tcs,
|
|
|
|
int tag, int seek)
|
|
|
|
{
|
|
|
|
if (tcs->filter_count == TAGCACHE_MAX_FILTERS)
|
|
|
|
return false;
|
|
|
|
|
2009-06-06 00:00:58 +00:00
|
|
|
if (TAGCACHE_IS_NUMERIC_OR_NONUNIQUE(tag))
|
2006-04-10 10:26:24 +00:00
|
|
|
return false;
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2006-03-26 11:33:42 +00:00
|
|
|
tcs->filter_tag[tcs->filter_count] = tag;
|
|
|
|
tcs->filter_seek[tcs->filter_count] = seek;
|
|
|
|
tcs->filter_count++;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2006-04-03 18:57:34 +00:00
|
|
|
bool tagcache_search_add_clause(struct tagcache_search *tcs,
|
|
|
|
struct tagcache_search_clause *clause)
|
|
|
|
{
|
2006-08-25 13:22:46 +00:00
|
|
|
int i;
|
2022-02-26 02:34:29 +00:00
|
|
|
int clause_count = tcs->clause_count;
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2022-02-26 02:34:29 +00:00
|
|
|
if (clause_count >= TAGCACHE_MAX_CLAUSES)
|
2006-04-03 18:57:34 +00:00
|
|
|
{
|
|
|
|
logf("Too many clauses");
|
|
|
|
return false;
|
|
|
|
}
|
2006-04-08 08:03:51 +00:00
|
|
|
|
2011-05-10 10:25:41 +00:00
|
|
|
if (clause->type != clause_logical_or)
|
2006-08-25 13:22:46 +00:00
|
|
|
{
|
2022-02-26 02:34:29 +00:00
|
|
|
/* BUGFIX OR'd clauses seem to be mishandled once made into a filter */
|
|
|
|
if (clause_count <= 1 || tcs->clause[clause_count - 1]->type != clause_logical_or)
|
2011-05-10 10:25:41 +00:00
|
|
|
{
|
2022-02-26 02:34:29 +00:00
|
|
|
/* Check if there is already a similar filter in present (filters are
|
|
|
|
* much faster than clauses).
|
|
|
|
*/
|
|
|
|
for (i = 0; i < tcs->filter_count; i++)
|
|
|
|
{
|
|
|
|
if (tcs->filter_tag[i] == clause->tag)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2011-05-10 10:25:41 +00:00
|
|
|
}
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2011-05-10 10:25:41 +00:00
|
|
|
if (!TAGCACHE_IS_NUMERIC(clause->tag) && tcs->idxfd[clause->tag] < 0)
|
|
|
|
{
|
|
|
|
char buf[MAX_PATH];
|
2021-07-30 06:13:57 +00:00
|
|
|
const int bufsz = sizeof(buf);
|
|
|
|
|
|
|
|
snprintf(buf, bufsz, TAGCACHE_FILE_INDEX, clause->tag);
|
2011-05-10 10:25:41 +00:00
|
|
|
tcs->idxfd[clause->tag] = open(buf, O_RDONLY);
|
|
|
|
}
|
2006-04-08 08:03:51 +00:00
|
|
|
}
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2006-04-03 18:57:34 +00:00
|
|
|
tcs->clause[tcs->clause_count] = clause;
|
|
|
|
tcs->clause_count++;
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2006-04-03 18:57:34 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2006-07-10 16:22:03 +00:00
|
|
|
static bool get_next(struct tagcache_search *tcs)
|
2006-03-26 11:33:42 +00:00
|
|
|
{
|
2021-07-30 06:13:57 +00:00
|
|
|
/* WARNING pointers into buf are used in outside functions */
|
2021-08-01 02:24:33 +00:00
|
|
|
static char buf[TAGCACHE_BUFSZ];
|
2021-07-30 06:13:57 +00:00
|
|
|
const int bufsz = sizeof(buf);
|
2006-03-26 11:33:42 +00:00
|
|
|
struct tagfile_entry entry;
|
2011-05-03 20:28:56 +00:00
|
|
|
#if defined(HAVE_TC_RAMCACHE) && defined(HAVE_DIRCACHE)
|
2006-08-30 18:18:37 +00:00
|
|
|
long flag = 0;
|
2011-05-03 20:28:56 +00:00
|
|
|
#endif
|
2006-03-26 11:33:42 +00:00
|
|
|
|
2006-11-10 08:03:33 +00:00
|
|
|
if (!tcs->valid || !tc_stat.ready)
|
2006-03-26 11:33:42 +00:00
|
|
|
return false;
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2009-06-03 08:19:32 +00:00
|
|
|
if (tcs->idxfd[tcs->type] < 0 && !TAGCACHE_IS_NUMERIC(tcs->type)
|
2006-03-26 11:33:42 +00:00
|
|
|
#ifdef HAVE_TC_RAMCACHE
|
|
|
|
&& !tcs->ramsearch
|
|
|
|
#endif
|
|
|
|
)
|
|
|
|
return false;
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2006-03-26 11:33:42 +00:00
|
|
|
/* Relative fetch. */
|
2006-08-25 21:13:49 +00:00
|
|
|
if (tcs->filter_count > 0 || tcs->clause_count > 0
|
2009-06-20 16:17:54 +00:00
|
|
|
|| TAGCACHE_IS_NUMERIC(tcs->type)
|
|
|
|
#if defined(HAVE_TC_RAMCACHE) && defined(HAVE_DIRCACHE)
|
|
|
|
/* We need to retrieve flag status for dircache. */
|
|
|
|
|| (tcs->ramsearch && tcs->type == tag_filename)
|
|
|
|
#endif
|
|
|
|
)
|
2006-03-26 11:33:42 +00:00
|
|
|
{
|
2009-06-20 16:17:54 +00:00
|
|
|
struct tagcache_seeklist_entry *seeklist;
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2006-03-26 11:33:42 +00:00
|
|
|
/* Check for end of list. */
|
2009-06-20 16:17:54 +00:00
|
|
|
if (tcs->list_position == tcs->seek_list_count)
|
2006-03-26 11:33:42 +00:00
|
|
|
{
|
2009-06-20 16:17:54 +00:00
|
|
|
tcs->list_position = 0;
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2006-03-26 11:33:42 +00:00
|
|
|
/* Try to fetch more. */
|
|
|
|
if (!build_lookup_list(tcs))
|
2006-08-25 21:13:49 +00:00
|
|
|
{
|
|
|
|
tcs->valid = false;
|
2006-03-26 11:33:42 +00:00
|
|
|
return false;
|
2006-08-25 21:13:49 +00:00
|
|
|
}
|
2006-03-26 11:33:42 +00:00
|
|
|
}
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2009-06-20 16:17:54 +00:00
|
|
|
seeklist = &tcs->seeklist[tcs->list_position];
|
2011-05-03 20:28:56 +00:00
|
|
|
#if defined(HAVE_TC_RAMCACHE) && defined(HAVE_DIRCACHE)
|
2009-06-20 16:17:54 +00:00
|
|
|
flag = seeklist->flag;
|
2011-05-03 20:28:56 +00:00
|
|
|
#endif
|
2009-06-20 16:17:54 +00:00
|
|
|
tcs->position = seeklist->seek;
|
|
|
|
tcs->idx_id = seeklist->idx_id;
|
|
|
|
tcs->list_position++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (tcs->entry_count == 0)
|
|
|
|
{
|
|
|
|
tcs->valid = false;
|
|
|
|
return false;
|
|
|
|
}
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2009-06-20 16:17:54 +00:00
|
|
|
tcs->entry_count--;
|
2006-03-26 11:33:42 +00:00
|
|
|
}
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2009-06-20 16:17:54 +00:00
|
|
|
tcs->result_seek = tcs->position;
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2009-06-03 08:19:32 +00:00
|
|
|
if (TAGCACHE_IS_NUMERIC(tcs->type))
|
2006-08-25 21:13:49 +00:00
|
|
|
{
|
2021-07-30 06:13:57 +00:00
|
|
|
snprintf(buf, bufsz, "%ld", tcs->position);
|
2006-08-25 21:13:49 +00:00
|
|
|
tcs->result = buf;
|
|
|
|
tcs->result_len = strlen(buf) + 1;
|
|
|
|
return true;
|
|
|
|
}
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2006-03-26 11:33:42 +00:00
|
|
|
/* Direct fetch. */
|
|
|
|
#ifdef HAVE_TC_RAMCACHE
|
2009-06-20 16:17:54 +00:00
|
|
|
if (tcs->ramsearch)
|
2006-03-26 11:33:42 +00:00
|
|
|
{
|
2017-01-17 19:45:07 +00:00
|
|
|
#ifdef HAVE_DIRCACHE
|
2011-08-04 23:03:43 +00:00
|
|
|
if (tcs->type == tag_filename && (flag & FLAG_DIRCACHE))
|
2006-03-26 11:33:42 +00:00
|
|
|
{
|
2017-01-18 09:39:35 +00:00
|
|
|
ssize_t len = dircache_get_fileref_path(&tcrc_dcfrefs[tcs->idx_id],
|
|
|
|
buf, sizeof (buf));
|
|
|
|
if (len >= 0)
|
2011-08-04 23:03:43 +00:00
|
|
|
{
|
|
|
|
tcs->result_len = len + 1;
|
2017-01-18 09:39:35 +00:00
|
|
|
tcs->result = buf;
|
|
|
|
tcs->ramresult = false;
|
2011-08-04 23:03:43 +00:00
|
|
|
return true;
|
|
|
|
}
|
2017-01-18 09:39:35 +00:00
|
|
|
/* else do it the hard way */
|
2006-03-26 11:33:42 +00:00
|
|
|
}
|
2017-01-17 19:45:07 +00:00
|
|
|
#endif /* HAVE_DIRCACHE */
|
2017-01-18 09:39:35 +00:00
|
|
|
|
2009-06-20 16:17:54 +00:00
|
|
|
if (tcs->type != tag_filename)
|
|
|
|
{
|
|
|
|
struct tagfile_entry *ep;
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2017-01-18 09:39:35 +00:00
|
|
|
ep = (struct tagfile_entry *)&tcramcache.hdr->tags[tcs->type][tcs->position];
|
2011-08-30 14:01:45 +00:00
|
|
|
/* don't return ep->tag_data directly as it may move */
|
|
|
|
tcs->result_len = strlcpy(buf, ep->tag_data, sizeof(buf)) + 1;
|
|
|
|
tcs->result = buf;
|
2009-06-20 16:17:54 +00:00
|
|
|
tcs->idx_id = ep->idx_id;
|
2011-08-30 14:01:45 +00:00
|
|
|
tcs->ramresult = false; /* was true before we copied to buf too */
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2009-06-20 16:17:54 +00:00
|
|
|
/* Increase position for the next run. This may get overwritten. */
|
|
|
|
tcs->position += sizeof(struct tagfile_entry) + ep->tag_length;
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2009-06-20 16:17:54 +00:00
|
|
|
return true;
|
|
|
|
}
|
2006-03-26 11:33:42 +00:00
|
|
|
}
|
2017-01-18 09:39:35 +00:00
|
|
|
#endif /* HAVE_TC_RAMCACHE */
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2009-06-19 14:30:59 +00:00
|
|
|
if (!open_files(tcs, tcs->type))
|
2009-06-20 16:17:54 +00:00
|
|
|
{
|
|
|
|
tcs->valid = false;
|
2009-06-19 14:30:59 +00:00
|
|
|
return false;
|
2009-06-20 16:17:54 +00:00
|
|
|
}
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2009-06-19 14:30:59 +00:00
|
|
|
/* Seek stream to the correct position and continue to direct fetch. */
|
|
|
|
lseek(tcs->idxfd[tcs->type], tcs->position, SEEK_SET);
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2021-08-01 02:24:33 +00:00
|
|
|
switch (ecread_tagfile_entry_and_tag(tcs->idxfd[tcs->type], &entry, buf, bufsz))
|
2006-03-26 11:33:42 +00:00
|
|
|
{
|
2021-08-01 02:24:33 +00:00
|
|
|
case e_SUCCESS:
|
|
|
|
break;
|
|
|
|
case e_ENTRY_SIZEMISMATCH:
|
|
|
|
logf("read error #5");
|
|
|
|
tcs->valid = false;
|
|
|
|
return false;
|
|
|
|
case e_TAG_TOOLONG:
|
|
|
|
tcs->valid = false;
|
|
|
|
logf("too long tag #2");
|
|
|
|
logf("P:%lX/%lX", tcs->position, entry.tag_length);
|
|
|
|
return false;
|
|
|
|
case e_TAG_SIZEMISMATCH:
|
|
|
|
tcs->valid = false;
|
|
|
|
logf("read error #4");
|
|
|
|
return false;
|
|
|
|
default:
|
|
|
|
logf("unknown_error");
|
|
|
|
break;;
|
2006-03-26 11:33:42 +00:00
|
|
|
}
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2009-06-19 14:30:59 +00:00
|
|
|
/**
|
|
|
|
Update the position for the next read (this may be overridden
|
|
|
|
if filters or clauses are being used).
|
|
|
|
*/
|
|
|
|
tcs->position += sizeof(struct tagfile_entry) + entry.tag_length;
|
2021-08-01 02:24:33 +00:00
|
|
|
str_setlen(buf, entry.tag_length);
|
|
|
|
|
2006-03-26 11:33:42 +00:00
|
|
|
tcs->result = buf;
|
2006-07-10 16:22:03 +00:00
|
|
|
tcs->result_len = strlen(tcs->result) + 1;
|
2006-03-30 12:07:32 +00:00
|
|
|
tcs->idx_id = entry.idx_id;
|
2006-08-30 18:18:37 +00:00
|
|
|
tcs->ramresult = false;
|
2006-04-10 10:26:24 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2006-07-10 16:22:03 +00:00
|
|
|
bool tagcache_get_next(struct tagcache_search *tcs)
|
|
|
|
{
|
|
|
|
while (get_next(tcs))
|
|
|
|
{
|
|
|
|
if (tcs->result_len > 1)
|
|
|
|
return true;
|
|
|
|
}
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2022-03-02 03:37:11 +00:00
|
|
|
#ifdef LOGF_ENABLE
|
|
|
|
if (tcs->unique_list_count > 0)
|
|
|
|
logf(" uniqbuf: %d used / %d avail", tcs->unique_list_count, tcs->unique_list_capacity);
|
|
|
|
#endif
|
|
|
|
|
2006-07-10 16:22:03 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-07-31 14:26:59 +00:00
|
|
|
bool tagcache_retrieve(struct tagcache_search *tcs, int idxid,
|
2006-10-24 16:20:48 +00:00
|
|
|
int tag, char *buf, long size)
|
2006-04-10 10:26:24 +00:00
|
|
|
{
|
2006-07-15 17:36:25 +00:00
|
|
|
struct index_entry idx;
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2006-09-23 10:29:14 +00:00
|
|
|
*buf = '\0';
|
2006-07-24 15:35:46 +00:00
|
|
|
if (!get_index(tcs->masterfd, idxid, &idx, true))
|
2006-07-15 17:36:25 +00:00
|
|
|
return false;
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2017-02-10 10:44:04 +00:00
|
|
|
return retrieve(tcs, IF_DIRCACHE(idxid,) &idx, tag, buf, size);
|
2006-03-26 11:33:42 +00:00
|
|
|
}
|
|
|
|
|
2007-02-25 20:41:51 +00:00
|
|
|
static bool update_master_header(void)
|
|
|
|
{
|
|
|
|
struct master_header myhdr;
|
|
|
|
int fd;
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2007-02-25 20:41:51 +00:00
|
|
|
if (!tc_stat.ready)
|
|
|
|
return false;
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2007-02-25 20:41:51 +00:00
|
|
|
if ( (fd = open_master_fd(&myhdr, true)) < 0)
|
|
|
|
return false;
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2007-02-25 20:41:51 +00:00
|
|
|
myhdr.serial = current_tcmh.serial;
|
|
|
|
myhdr.commitid = current_tcmh.commitid;
|
2009-04-15 06:46:25 +00:00
|
|
|
myhdr.dirty = current_tcmh.dirty;
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2007-02-25 20:41:51 +00:00
|
|
|
/* Write it back */
|
|
|
|
lseek(fd, 0, SEEK_SET);
|
|
|
|
ecwrite(fd, &myhdr, 1, master_header_ec, tc_stat.econ);
|
|
|
|
close(fd);
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2007-02-25 20:41:51 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2006-03-26 11:33:42 +00:00
|
|
|
void tagcache_search_finish(struct tagcache_search *tcs)
|
|
|
|
{
|
2006-04-08 08:03:51 +00:00
|
|
|
int i;
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2006-08-02 17:39:34 +00:00
|
|
|
if (!tcs->initialized)
|
|
|
|
return;
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2006-04-03 18:57:34 +00:00
|
|
|
if (tcs->masterfd >= 0)
|
|
|
|
{
|
|
|
|
close(tcs->masterfd);
|
|
|
|
tcs->masterfd = -1;
|
|
|
|
}
|
2006-04-08 08:03:51 +00:00
|
|
|
|
|
|
|
for (i = 0; i < TAG_COUNT; i++)
|
|
|
|
{
|
|
|
|
if (tcs->idxfd[i] >= 0)
|
|
|
|
{
|
|
|
|
close(tcs->idxfd[i]);
|
|
|
|
tcs->idxfd[i] = -1;
|
|
|
|
}
|
|
|
|
}
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2006-04-03 18:57:34 +00:00
|
|
|
tcs->ramsearch = false;
|
|
|
|
tcs->valid = false;
|
2006-08-02 17:39:34 +00:00
|
|
|
tcs->initialized = 0;
|
|
|
|
if (write_lock > 0)
|
|
|
|
write_lock--;
|
2006-03-26 11:33:42 +00:00
|
|
|
}
|
|
|
|
|
2006-07-10 16:22:03 +00:00
|
|
|
#if defined(HAVE_TC_RAMCACHE) && defined(HAVE_DIRCACHE)
|
2006-04-03 06:00:56 +00:00
|
|
|
static struct tagfile_entry *get_tag(const struct index_entry *entry, int tag)
|
2006-03-26 11:33:42 +00:00
|
|
|
{
|
2017-01-18 09:39:35 +00:00
|
|
|
return (struct tagfile_entry *)&tcramcache.hdr->tags[tag][entry->tag_seek[tag]];
|
2006-03-26 11:33:42 +00:00
|
|
|
}
|
|
|
|
|
2010-11-23 00:15:13 +00:00
|
|
|
static long get_tag_numeric(const struct index_entry *entry, int tag, int idx_id)
|
2006-04-03 06:00:56 +00:00
|
|
|
{
|
2010-11-23 00:15:13 +00:00
|
|
|
return check_virtual_tags(tag, idx_id, entry);
|
2006-04-03 06:00:56 +00:00
|
|
|
}
|
2006-07-10 16:22:03 +00:00
|
|
|
|
2007-03-04 19:54:49 +00:00
|
|
|
static char* get_tag_string(const struct index_entry *entry, int tag)
|
|
|
|
{
|
|
|
|
char* s = get_tag(entry, tag)->tag_data;
|
|
|
|
return strcmp(s, UNTAGGED) ? s : NULL;
|
|
|
|
}
|
|
|
|
|
2006-03-26 11:33:42 +00:00
|
|
|
bool tagcache_fill_tags(struct mp3entry *id3, const char *filename)
|
|
|
|
{
|
|
|
|
struct index_entry *entry;
|
2006-07-15 17:36:25 +00:00
|
|
|
int idx_id;
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2009-06-20 16:17:54 +00:00
|
|
|
if (!tc_stat.ready || !tc_stat.ramcache)
|
2006-07-23 11:15:28 +00:00
|
|
|
return false;
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2006-03-26 11:33:42 +00:00
|
|
|
/* Find the corresponding entry in tagcache. */
|
2017-01-18 09:39:35 +00:00
|
|
|
idx_id = find_entry_ram(filename);
|
2009-06-20 16:17:54 +00:00
|
|
|
if (idx_id < 0)
|
2006-03-26 11:33:42 +00:00
|
|
|
return false;
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2017-01-18 09:39:35 +00:00
|
|
|
entry = &tcramcache.hdr->indices[idx_id];
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2010-05-06 09:22:55 +00:00
|
|
|
memset(id3, 0, sizeof(struct mp3entry));
|
2011-08-30 14:01:45 +00:00
|
|
|
char* buf = id3->id3v2buf;
|
|
|
|
ssize_t remaining = sizeof(id3->id3v2buf);
|
|
|
|
|
|
|
|
/* this macro sets id3 strings by copying to the id3v2buf */
|
|
|
|
#define SET(x, y) do \
|
|
|
|
{ \
|
|
|
|
if (remaining > 0) \
|
|
|
|
{ \
|
|
|
|
x = NULL; /* initialize with null if tag doesn't exist */ \
|
|
|
|
char* src = get_tag_string(entry, y); \
|
|
|
|
if (src) \
|
|
|
|
{ \
|
|
|
|
x = buf; \
|
|
|
|
size_t len = strlcpy(buf, src, remaining) +1; \
|
|
|
|
buf += len; remaining -= len; \
|
|
|
|
} \
|
|
|
|
} \
|
|
|
|
} while(0)
|
2021-07-31 14:26:59 +00:00
|
|
|
|
|
|
|
|
2011-08-30 14:01:45 +00:00
|
|
|
SET(id3->title, tag_title);
|
|
|
|
SET(id3->artist, tag_artist);
|
|
|
|
SET(id3->album, tag_album);
|
|
|
|
SET(id3->genre_string, tag_genre);
|
|
|
|
SET(id3->composer, tag_composer);
|
|
|
|
SET(id3->comment, tag_comment);
|
|
|
|
SET(id3->albumartist, tag_albumartist);
|
|
|
|
SET(id3->grouping, tag_grouping);
|
2007-03-04 17:30:12 +00:00
|
|
|
|
2010-11-23 00:15:13 +00:00
|
|
|
id3->length = get_tag_numeric(entry, tag_length, idx_id);
|
|
|
|
id3->playcount = get_tag_numeric(entry, tag_playcount, idx_id);
|
|
|
|
id3->rating = get_tag_numeric(entry, tag_rating, idx_id);
|
|
|
|
id3->lastplayed = get_tag_numeric(entry, tag_lastplayed, idx_id);
|
|
|
|
id3->score = get_tag_numeric(entry, tag_virt_autoscore, idx_id) / 10;
|
|
|
|
id3->year = get_tag_numeric(entry, tag_year, idx_id);
|
2007-03-04 17:30:12 +00:00
|
|
|
|
2010-11-23 00:15:13 +00:00
|
|
|
id3->discnum = get_tag_numeric(entry, tag_discnumber, idx_id);
|
|
|
|
id3->tracknum = get_tag_numeric(entry, tag_tracknumber, idx_id);
|
|
|
|
id3->bitrate = get_tag_numeric(entry, tag_bitrate, idx_id);
|
2006-04-03 06:00:56 +00:00
|
|
|
if (id3->bitrate == 0)
|
|
|
|
id3->bitrate = 1;
|
2006-03-26 11:33:42 +00:00
|
|
|
|
2011-01-02 02:49:13 +00:00
|
|
|
if (global_settings.autoresume_enable)
|
|
|
|
{
|
2013-07-14 11:59:39 +00:00
|
|
|
id3->elapsed = get_tag_numeric(entry, tag_lastelapsed, idx_id);
|
|
|
|
logf("tagcache_fill_tags: Set elapsed for %s to %lX\n",
|
|
|
|
id3->title, id3->elapsed);
|
|
|
|
|
2011-01-02 02:49:13 +00:00
|
|
|
id3->offset = get_tag_numeric(entry, tag_lastoffset, idx_id);
|
2021-07-31 14:26:59 +00:00
|
|
|
logf("tagcache_fill_tags: Set offset for %s to %lX\n",
|
2011-01-02 02:49:13 +00:00
|
|
|
id3->title, id3->offset);
|
|
|
|
}
|
2020-07-17 04:01:32 +00:00
|
|
|
|
2006-03-26 11:33:42 +00:00
|
|
|
return true;
|
|
|
|
}
|
2017-01-18 09:39:35 +00:00
|
|
|
#endif /* defined(HAVE_TC_RAMCACHE) && defined(HAVE_DIRCACHE) */
|
2006-03-26 11:33:42 +00:00
|
|
|
|
|
|
|
static inline void write_item(const char *item)
|
|
|
|
{
|
|
|
|
int len = strlen(item) + 1;
|
2008-01-25 21:21:41 +00:00
|
|
|
|
2006-03-26 11:33:42 +00:00
|
|
|
data_size += len;
|
|
|
|
write(cachefd, item, len);
|
|
|
|
}
|
|
|
|
|
2006-10-04 09:05:01 +00:00
|
|
|
static int check_if_empty(char **tag)
|
2006-03-26 11:33:42 +00:00
|
|
|
{
|
2006-10-04 09:05:01 +00:00
|
|
|
int length;
|
2008-01-25 21:21:41 +00:00
|
|
|
|
|
|
|
if (*tag == NULL || **tag == '\0')
|
2006-10-04 09:05:01 +00:00
|
|
|
{
|
2007-03-04 17:30:12 +00:00
|
|
|
*tag = UNTAGGED;
|
2007-03-04 19:30:03 +00:00
|
|
|
return sizeof(UNTAGGED); /* Tag length */
|
2006-10-04 09:05:01 +00:00
|
|
|
}
|
2008-01-25 21:21:41 +00:00
|
|
|
|
2006-10-04 09:05:01 +00:00
|
|
|
length = strlen(*tag);
|
2006-11-10 08:03:33 +00:00
|
|
|
if (length > TAG_MAXLEN)
|
2006-10-04 09:05:01 +00:00
|
|
|
{
|
|
|
|
logf("over length tag: %s", *tag);
|
2006-11-10 08:03:33 +00:00
|
|
|
length = TAG_MAXLEN;
|
2021-08-01 02:24:33 +00:00
|
|
|
str_setlen((*tag), length);
|
2006-10-04 09:05:01 +00:00
|
|
|
}
|
2008-01-25 21:21:41 +00:00
|
|
|
|
2006-10-04 09:05:01 +00:00
|
|
|
return length + 1;
|
2006-03-26 11:33:42 +00:00
|
|
|
}
|
|
|
|
|
2009-06-30 21:32:38 +00:00
|
|
|
/* GCC 3.4.6 for Coldfire can choose to inline this function. Not a good
|
|
|
|
* idea, as it uses lots of stack and is called from a recursive function
|
|
|
|
* (check_dir).
|
|
|
|
*/
|
2017-01-18 09:39:35 +00:00
|
|
|
static void NO_INLINE add_tagcache(char *path, unsigned long mtime)
|
2006-03-26 11:33:42 +00:00
|
|
|
{
|
2017-01-18 09:39:35 +00:00
|
|
|
#define ADD_TAG(entry, tag, data) \
|
|
|
|
/* Adding tag */ \
|
|
|
|
entry.tag_length[tag] = check_if_empty(data); \
|
2021-08-01 02:24:33 +00:00
|
|
|
entry.tag_offset[tag] = offset; \
|
2017-01-18 09:39:35 +00:00
|
|
|
offset += entry.tag_length[tag]
|
|
|
|
|
2007-10-01 20:06:21 +00:00
|
|
|
struct mp3entry id3;
|
2006-03-26 11:33:42 +00:00
|
|
|
struct temp_file_entry entry;
|
|
|
|
bool ret;
|
|
|
|
int fd;
|
2007-12-16 21:10:26 +00:00
|
|
|
int idx_id = -1;
|
2006-03-30 12:07:32 +00:00
|
|
|
char tracknumfix[3];
|
2006-10-04 09:05:01 +00:00
|
|
|
int offset = 0;
|
2006-11-10 08:03:33 +00:00
|
|
|
int path_length = strlen(path);
|
2021-09-15 23:33:41 +00:00
|
|
|
bool has_artist;
|
2007-08-08 10:19:56 +00:00
|
|
|
bool has_grouping;
|
2006-03-26 11:33:42 +00:00
|
|
|
|
2009-07-04 19:28:47 +00:00
|
|
|
#ifdef SIMULATOR
|
|
|
|
/* Crude logging for the sim - to aid in debugging */
|
|
|
|
int logfd = open(ROCKBOX_DIR "/database.log",
|
2010-05-06 17:35:13 +00:00
|
|
|
O_WRONLY | O_APPEND | O_CREAT, 0666);
|
2017-01-18 09:39:35 +00:00
|
|
|
if (logfd >= 0)
|
|
|
|
{
|
2009-07-04 19:28:47 +00:00
|
|
|
write(logfd, path, strlen(path));
|
|
|
|
write(logfd, "\n", 1);
|
|
|
|
close(logfd);
|
|
|
|
}
|
2017-01-18 09:39:35 +00:00
|
|
|
#endif /* SIMULATOR */
|
2009-07-04 19:28:47 +00:00
|
|
|
|
2006-03-26 11:33:42 +00:00
|
|
|
if (cachefd < 0)
|
|
|
|
return ;
|
|
|
|
|
2006-11-10 08:03:33 +00:00
|
|
|
/* Check for overlength file path. */
|
|
|
|
if (path_length > TAG_MAXLEN)
|
|
|
|
{
|
|
|
|
/* Path can't be shortened. */
|
2007-03-18 09:50:53 +00:00
|
|
|
logf("Too long path: %s", path);
|
2006-11-10 08:03:33 +00:00
|
|
|
return ;
|
|
|
|
}
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2006-03-26 11:33:42 +00:00
|
|
|
/* Check if the file is supported. */
|
|
|
|
if (probe_file_format(path) == AFMT_UNKNOWN)
|
|
|
|
return ;
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2006-03-26 11:33:42 +00:00
|
|
|
/* Check if the file is already cached. */
|
2006-07-10 16:22:03 +00:00
|
|
|
#if defined(HAVE_TC_RAMCACHE) && defined(HAVE_DIRCACHE)
|
2017-01-18 09:39:35 +00:00
|
|
|
idx_id = find_entry_ram(path);
|
2006-03-26 11:33:42 +00:00
|
|
|
#endif
|
2009-06-20 16:17:54 +00:00
|
|
|
|
|
|
|
/* Be sure the entry doesn't exist. */
|
|
|
|
if (filenametag_fd >= 0 && idx_id < 0)
|
|
|
|
idx_id = find_entry_disk(path, false);
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2007-12-16 21:10:26 +00:00
|
|
|
/* Check if file has been modified. */
|
|
|
|
if (idx_id >= 0)
|
|
|
|
{
|
|
|
|
struct index_entry idx;
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2008-08-29 21:14:58 +00:00
|
|
|
/* TODO: Mark that the index exists (for fast reverse scan) */
|
|
|
|
//found_idx[idx_id/8] |= idx_id%8;
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2007-12-16 21:10:26 +00:00
|
|
|
if (!get_index(-1, idx_id, &idx, true))
|
|
|
|
{
|
|
|
|
logf("failed to retrieve index entry");
|
|
|
|
return ;
|
|
|
|
}
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2007-12-16 21:10:26 +00:00
|
|
|
if ((unsigned long)idx.tag_seek[tag_mtime] == mtime)
|
|
|
|
{
|
|
|
|
/* No changes to file. */
|
|
|
|
return ;
|
|
|
|
}
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2007-12-16 21:10:26 +00:00
|
|
|
/* Metadata might have been changed. Delete the entry. */
|
|
|
|
logf("Re-adding: %s", path);
|
|
|
|
if (!delete_entry(idx_id))
|
|
|
|
{
|
|
|
|
logf("delete_entry failed: %d", idx_id);
|
|
|
|
return ;
|
2006-07-23 11:15:28 +00:00
|
|
|
}
|
2006-03-26 11:33:42 +00:00
|
|
|
}
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2006-03-26 11:33:42 +00:00
|
|
|
fd = open(path, O_RDONLY);
|
|
|
|
if (fd < 0)
|
|
|
|
{
|
|
|
|
logf("open fail: %s", path);
|
|
|
|
return ;
|
|
|
|
}
|
|
|
|
|
2007-10-01 20:06:21 +00:00
|
|
|
memset(&id3, 0, sizeof(struct mp3entry));
|
2006-03-30 12:07:32 +00:00
|
|
|
memset(&entry, 0, sizeof(struct temp_file_entry));
|
|
|
|
memset(&tracknumfix, 0, sizeof(tracknumfix));
|
2007-10-01 20:06:21 +00:00
|
|
|
ret = get_metadata(&id3, fd, path);
|
2006-03-26 11:33:42 +00:00
|
|
|
close(fd);
|
|
|
|
|
|
|
|
if (!ret)
|
2006-03-27 10:46:09 +00:00
|
|
|
return ;
|
|
|
|
|
2006-09-23 10:29:14 +00:00
|
|
|
logf("-> %s", path);
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2011-06-06 23:03:31 +00:00
|
|
|
if (id3.tracknum <= 0) /* Track number missing? */
|
2006-03-30 12:07:32 +00:00
|
|
|
{
|
2011-06-06 23:03:31 +00:00
|
|
|
id3.tracknum = -1;
|
2006-03-30 12:07:32 +00:00
|
|
|
}
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2006-10-04 09:05:01 +00:00
|
|
|
/* Numeric tags */
|
2007-10-01 20:06:21 +00:00
|
|
|
entry.tag_offset[tag_year] = id3.year;
|
|
|
|
entry.tag_offset[tag_discnumber] = id3.discnum;
|
|
|
|
entry.tag_offset[tag_tracknumber] = id3.tracknum;
|
|
|
|
entry.tag_offset[tag_length] = id3.length;
|
|
|
|
entry.tag_offset[tag_bitrate] = id3.bitrate;
|
2007-12-16 21:10:26 +00:00
|
|
|
entry.tag_offset[tag_mtime] = mtime;
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2006-10-04 09:05:01 +00:00
|
|
|
/* String tags. */
|
2021-09-15 23:33:41 +00:00
|
|
|
has_artist = id3.artist != NULL
|
|
|
|
&& strlen(id3.artist) > 0;
|
2007-10-01 20:06:21 +00:00
|
|
|
has_grouping = id3.grouping != NULL
|
|
|
|
&& strlen(id3.grouping) > 0;
|
2007-06-25 11:33:21 +00:00
|
|
|
|
2006-10-04 09:05:01 +00:00
|
|
|
ADD_TAG(entry, tag_filename, &path);
|
2007-10-01 20:06:21 +00:00
|
|
|
ADD_TAG(entry, tag_title, &id3.title);
|
|
|
|
ADD_TAG(entry, tag_artist, &id3.artist);
|
|
|
|
ADD_TAG(entry, tag_album, &id3.album);
|
|
|
|
ADD_TAG(entry, tag_genre, &id3.genre_string);
|
|
|
|
ADD_TAG(entry, tag_composer, &id3.composer);
|
|
|
|
ADD_TAG(entry, tag_comment, &id3.comment);
|
2021-09-15 23:33:41 +00:00
|
|
|
ADD_TAG(entry, tag_albumartist, &id3.albumartist);
|
|
|
|
if (has_artist)
|
2007-06-25 11:33:21 +00:00
|
|
|
{
|
2021-09-15 23:33:41 +00:00
|
|
|
ADD_TAG(entry, tag_virt_canonicalartist, &id3.artist);
|
2007-06-25 11:33:21 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-09-15 23:33:41 +00:00
|
|
|
ADD_TAG(entry, tag_virt_canonicalartist, &id3.albumartist);
|
2007-06-25 11:33:21 +00:00
|
|
|
}
|
2007-08-08 10:19:56 +00:00
|
|
|
if (has_grouping)
|
|
|
|
{
|
2007-10-01 20:06:21 +00:00
|
|
|
ADD_TAG(entry, tag_grouping, &id3.grouping);
|
2007-08-08 10:19:56 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2007-10-01 20:06:21 +00:00
|
|
|
ADD_TAG(entry, tag_grouping, &id3.title);
|
2007-08-08 10:19:56 +00:00
|
|
|
}
|
2006-10-04 09:05:01 +00:00
|
|
|
entry.data_length = offset;
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2006-10-04 09:05:01 +00:00
|
|
|
/* Write the header */
|
2006-03-26 11:33:42 +00:00
|
|
|
write(cachefd, &entry, sizeof(struct temp_file_entry));
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2006-10-04 09:05:01 +00:00
|
|
|
/* And tags also... Correct order is critical */
|
2006-03-26 11:33:42 +00:00
|
|
|
write_item(path);
|
2007-10-01 20:06:21 +00:00
|
|
|
write_item(id3.title);
|
|
|
|
write_item(id3.artist);
|
|
|
|
write_item(id3.album);
|
|
|
|
write_item(id3.genre_string);
|
|
|
|
write_item(id3.composer);
|
|
|
|
write_item(id3.comment);
|
2021-09-15 23:33:41 +00:00
|
|
|
write_item(id3.albumartist);
|
|
|
|
if (has_artist)
|
2007-06-25 11:33:21 +00:00
|
|
|
{
|
2021-09-15 23:33:41 +00:00
|
|
|
write_item(id3.artist);
|
2007-06-25 11:33:21 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-09-15 23:33:41 +00:00
|
|
|
write_item(id3.albumartist);
|
2007-06-25 11:33:21 +00:00
|
|
|
}
|
2007-08-08 10:19:56 +00:00
|
|
|
if (has_grouping)
|
|
|
|
{
|
2007-10-01 20:06:21 +00:00
|
|
|
write_item(id3.grouping);
|
2007-08-08 10:19:56 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2007-10-01 20:06:21 +00:00
|
|
|
write_item(id3.title);
|
2007-08-08 10:19:56 +00:00
|
|
|
}
|
2017-01-18 09:39:35 +00:00
|
|
|
|
|
|
|
total_entry_count++;
|
|
|
|
|
|
|
|
#undef ADD_TAG
|
2006-03-26 11:33:42 +00:00
|
|
|
}
|
|
|
|
|
2006-04-23 18:47:26 +00:00
|
|
|
static bool tempbuf_insert(char *str, int id, int idx_id, bool unique)
|
2006-03-26 11:33:42 +00:00
|
|
|
{
|
|
|
|
struct tempbuf_searchidx *index = (struct tempbuf_searchidx *)tempbuf;
|
|
|
|
int len = strlen(str)+1;
|
|
|
|
int i;
|
2006-04-18 18:56:56 +00:00
|
|
|
unsigned crc32;
|
|
|
|
unsigned *crcbuf = (unsigned *)&tempbuf[tempbuf_size-4];
|
2021-08-01 02:24:33 +00:00
|
|
|
char buf[TAGCACHE_BUFSZ];
|
2021-07-30 06:13:57 +00:00
|
|
|
const int bufsz = sizeof(buf);
|
|
|
|
|
|
|
|
for (i = 0; str[i] != '\0' && i < bufsz-1; i++)
|
2006-04-23 18:47:26 +00:00
|
|
|
buf[i] = tolower(str[i]);
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2006-04-23 18:47:26 +00:00
|
|
|
crc32 = crc_32(buf, i, 0xffffffff);
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2006-04-23 18:47:26 +00:00
|
|
|
if (unique)
|
2006-03-26 11:33:42 +00:00
|
|
|
{
|
2006-04-23 18:47:26 +00:00
|
|
|
/* Check if the crc does not exist -> entry does not exist for sure. */
|
|
|
|
for (i = 0; i < tempbufidx; i++)
|
2006-03-26 11:33:42 +00:00
|
|
|
{
|
2006-04-23 18:47:26 +00:00
|
|
|
if (crcbuf[-i] != crc32)
|
|
|
|
continue;
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2006-04-18 18:56:56 +00:00
|
|
|
if (!strcasecmp(str, index[i].str))
|
2006-03-26 18:36:31 +00:00
|
|
|
{
|
2006-09-26 11:23:18 +00:00
|
|
|
if (id < 0 || id >= lookup_buffer_depth)
|
|
|
|
{
|
|
|
|
logf("lookup buf overf.: %d", id);
|
|
|
|
return false;
|
|
|
|
}
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2006-09-26 11:23:18 +00:00
|
|
|
lookup[id] = &index[i];
|
2006-04-18 18:56:56 +00:00
|
|
|
return true;
|
|
|
|
}
|
2006-03-26 11:33:42 +00:00
|
|
|
}
|
|
|
|
}
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2006-04-23 18:47:26 +00:00
|
|
|
/* Insert to CRC buffer. */
|
|
|
|
crcbuf[-tempbufidx] = crc32;
|
2006-04-18 18:56:56 +00:00
|
|
|
tempbuf_left -= 4;
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2006-04-23 18:47:26 +00:00
|
|
|
/* Insert it to the buffer. */
|
|
|
|
tempbuf_left -= len;
|
2006-07-22 07:09:57 +00:00
|
|
|
if (tempbuf_left - 4 < 0 || tempbufidx >= commit_entry_count-1)
|
2006-04-23 18:47:26 +00:00
|
|
|
return false;
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2006-09-26 11:23:18 +00:00
|
|
|
if (id >= lookup_buffer_depth)
|
|
|
|
{
|
|
|
|
logf("lookup buf overf. #2: %d", id);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (id >= 0)
|
2006-04-23 18:47:26 +00:00
|
|
|
{
|
|
|
|
lookup[id] = &index[tempbufidx];
|
|
|
|
index[tempbufidx].idlist.id = id;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
index[tempbufidx].idlist.id = -1;
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2006-04-23 18:47:26 +00:00
|
|
|
index[tempbufidx].idlist.next = NULL;
|
|
|
|
index[tempbufidx].idx_id = idx_id;
|
|
|
|
index[tempbufidx].seek = -1;
|
|
|
|
index[tempbufidx].str = &tempbuf[tempbuf_pos];
|
|
|
|
memcpy(index[tempbufidx].str, str, len);
|
|
|
|
tempbuf_pos += len;
|
|
|
|
tempbufidx++;
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2006-04-23 18:47:26 +00:00
|
|
|
return true;
|
2006-03-26 11:33:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int compare(const void *p1, const void *p2)
|
|
|
|
{
|
2007-03-10 14:34:56 +00:00
|
|
|
do_timed_yield();
|
|
|
|
|
2006-03-26 11:33:42 +00:00
|
|
|
struct tempbuf_searchidx *e1 = (struct tempbuf_searchidx *)p1;
|
|
|
|
struct tempbuf_searchidx *e2 = (struct tempbuf_searchidx *)p2;
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2007-03-07 10:52:19 +00:00
|
|
|
if (strcmp(e1->str, UNTAGGED) == 0)
|
|
|
|
{
|
|
|
|
if (strcmp(e2->str, UNTAGGED) == 0)
|
|
|
|
return 0;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
else if (strcmp(e2->str, UNTAGGED) == 0)
|
|
|
|
return 1;
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2006-11-10 08:03:33 +00:00
|
|
|
return strncasecmp(e1->str, e2->str, TAG_MAXLEN);
|
2006-03-26 11:33:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int tempbuf_sort(int fd)
|
|
|
|
{
|
|
|
|
struct tempbuf_searchidx *index = (struct tempbuf_searchidx *)tempbuf;
|
|
|
|
struct tagfile_entry fe;
|
|
|
|
int i;
|
2006-03-27 10:46:09 +00:00
|
|
|
int length;
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2006-04-23 18:47:26 +00:00
|
|
|
/* Generate reverse lookup entries. */
|
2006-09-26 11:23:18 +00:00
|
|
|
for (i = 0; i < lookup_buffer_depth; i++)
|
2006-04-23 18:47:26 +00:00
|
|
|
{
|
|
|
|
struct tempbuf_id_list *idlist;
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2006-04-23 18:47:26 +00:00
|
|
|
if (!lookup[i])
|
|
|
|
continue;
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2006-04-23 18:47:26 +00:00
|
|
|
if (lookup[i]->idlist.id == i)
|
|
|
|
continue;
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2006-04-23 18:47:26 +00:00
|
|
|
idlist = &lookup[i]->idlist;
|
|
|
|
while (idlist->next != NULL)
|
|
|
|
idlist = idlist->next;
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2006-04-23 18:47:26 +00:00
|
|
|
tempbuf_left -= sizeof(struct tempbuf_id_list);
|
|
|
|
if (tempbuf_left - 4 < 0)
|
|
|
|
return -1;
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2006-04-23 18:47:26 +00:00
|
|
|
idlist->next = (struct tempbuf_id_list *)&tempbuf[tempbuf_pos];
|
|
|
|
if (tempbuf_pos & 0x03)
|
|
|
|
{
|
|
|
|
tempbuf_pos = (tempbuf_pos & ~0x03) + 0x04;
|
|
|
|
tempbuf_left -= 3;
|
|
|
|
idlist->next = (struct tempbuf_id_list *)&tempbuf[tempbuf_pos];
|
|
|
|
}
|
|
|
|
tempbuf_pos += sizeof(struct tempbuf_id_list);
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2006-04-23 18:47:26 +00:00
|
|
|
idlist = idlist->next;
|
|
|
|
idlist->id = i;
|
|
|
|
idlist->next = NULL;
|
2007-03-10 14:34:56 +00:00
|
|
|
|
|
|
|
do_timed_yield();
|
2006-04-23 18:47:26 +00:00
|
|
|
}
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2006-03-26 11:33:42 +00:00
|
|
|
qsort(index, tempbufidx, sizeof(struct tempbuf_searchidx), compare);
|
2006-09-26 11:23:18 +00:00
|
|
|
memset(lookup, 0, lookup_buffer_depth * sizeof(struct tempbuf_searchidx **));
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2006-03-26 11:33:42 +00:00
|
|
|
for (i = 0; i < tempbufidx; i++)
|
|
|
|
{
|
2006-04-23 18:47:26 +00:00
|
|
|
struct tempbuf_id_list *idlist = &index[i].idlist;
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2006-04-23 18:47:26 +00:00
|
|
|
/* Fix the lookup list. */
|
|
|
|
while (idlist != NULL)
|
|
|
|
{
|
|
|
|
if (idlist->id >= 0)
|
|
|
|
lookup[idlist->id] = &index[i];
|
|
|
|
idlist = idlist->next;
|
|
|
|
}
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2006-03-26 11:33:42 +00:00
|
|
|
index[i].seek = lseek(fd, 0, SEEK_CUR);
|
2006-03-27 10:46:09 +00:00
|
|
|
length = strlen(index[i].str) + 1;
|
|
|
|
fe.tag_length = length;
|
2006-03-30 12:07:32 +00:00
|
|
|
fe.idx_id = index[i].idx_id;
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2006-04-23 18:47:26 +00:00
|
|
|
/* Check the chunk alignment. */
|
2021-07-31 14:26:59 +00:00
|
|
|
if ((fe.tag_length + sizeof(struct tagfile_entry))
|
2006-04-23 18:47:26 +00:00
|
|
|
% TAGFILE_ENTRY_CHUNK_LENGTH)
|
|
|
|
{
|
2021-07-31 14:26:59 +00:00
|
|
|
fe.tag_length += TAGFILE_ENTRY_CHUNK_LENGTH -
|
|
|
|
((fe.tag_length + sizeof(struct tagfile_entry))
|
2006-04-23 18:47:26 +00:00
|
|
|
% TAGFILE_ENTRY_CHUNK_LENGTH);
|
|
|
|
}
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2006-04-18 18:56:56 +00:00
|
|
|
#ifdef TAGCACHE_STRICT_ALIGN
|
2006-03-27 10:46:09 +00:00
|
|
|
/* Make sure the entry is long aligned. */
|
|
|
|
if (index[i].seek & 0x03)
|
|
|
|
{
|
|
|
|
logf("tempbuf_sort: alignment error!");
|
|
|
|
return -3;
|
|
|
|
}
|
|
|
|
#endif
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2007-02-13 21:51:18 +00:00
|
|
|
if (ecwrite(fd, &fe, 1, tagfile_entry_ec, tc_stat.econ) !=
|
2006-03-26 11:33:42 +00:00
|
|
|
sizeof(struct tagfile_entry))
|
|
|
|
{
|
|
|
|
logf("tempbuf_sort: write error #1");
|
|
|
|
return -1;
|
|
|
|
}
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2006-03-27 10:46:09 +00:00
|
|
|
if (write(fd, index[i].str, length) != length)
|
2006-03-26 11:33:42 +00:00
|
|
|
{
|
|
|
|
logf("tempbuf_sort: write error #2");
|
|
|
|
return -2;
|
|
|
|
}
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2006-03-27 10:46:09 +00:00
|
|
|
/* Write some padding. */
|
2006-04-23 18:47:26 +00:00
|
|
|
if (fe.tag_length - length > 0)
|
|
|
|
write(fd, "XXXXXXXX", fe.tag_length - length);
|
2006-03-26 11:33:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return i;
|
|
|
|
}
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2006-04-18 18:56:56 +00:00
|
|
|
inline static struct tempbuf_searchidx* tempbuf_locate(int id)
|
2006-03-26 11:33:42 +00:00
|
|
|
{
|
2006-09-26 11:23:18 +00:00
|
|
|
if (id < 0 || id >= lookup_buffer_depth)
|
2006-04-23 18:47:26 +00:00
|
|
|
return NULL;
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2006-04-23 18:47:26 +00:00
|
|
|
return lookup[id];
|
2006-03-26 11:33:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-04-18 18:56:56 +00:00
|
|
|
inline static int tempbuf_find_location(int id)
|
2006-03-26 11:33:42 +00:00
|
|
|
{
|
|
|
|
struct tempbuf_searchidx *entry;
|
|
|
|
|
|
|
|
entry = tempbuf_locate(id);
|
|
|
|
if (entry == NULL)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
return entry->seek;
|
|
|
|
}
|
|
|
|
|
2006-10-04 09:05:01 +00:00
|
|
|
static bool build_numeric_indices(struct tagcache_header *h, int tmpfd)
|
2006-03-30 12:07:32 +00:00
|
|
|
{
|
2007-12-18 21:50:29 +00:00
|
|
|
struct master_header tcmh;
|
2006-03-30 12:07:32 +00:00
|
|
|
struct index_entry idx;
|
|
|
|
int masterfd;
|
|
|
|
int masterfd_pos;
|
2006-10-04 09:05:01 +00:00
|
|
|
struct temp_file_entry *entrybuf = (struct temp_file_entry *)tempbuf;
|
2006-03-30 12:07:32 +00:00
|
|
|
int max_entries;
|
2006-10-04 09:05:01 +00:00
|
|
|
int entries_processed = 0;
|
|
|
|
int i, j;
|
2021-07-30 06:13:57 +00:00
|
|
|
|
2021-08-01 02:24:33 +00:00
|
|
|
char buf[TAGCACHE_BUFSZ];
|
2021-07-30 06:13:57 +00:00
|
|
|
const int bufsz = sizeof(buf);
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2006-10-04 09:05:01 +00:00
|
|
|
max_entries = tempbuf_size / sizeof(struct temp_file_entry) - 1;
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2006-10-04 09:05:01 +00:00
|
|
|
logf("Building numeric indices...");
|
2006-03-30 12:07:32 +00:00
|
|
|
lseek(tmpfd, sizeof(struct tagcache_header), SEEK_SET);
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2006-07-20 12:19:31 +00:00
|
|
|
if ( (masterfd = open_master_fd(&tcmh, true)) < 0)
|
2006-03-30 12:07:32 +00:00
|
|
|
return false;
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2006-07-20 12:19:31 +00:00
|
|
|
masterfd_pos = lseek(masterfd, tcmh.tch.entry_count * sizeof(struct index_entry),
|
2006-03-30 12:07:32 +00:00
|
|
|
SEEK_CUR);
|
2021-05-19 20:30:32 +00:00
|
|
|
if (masterfd_pos < 0)
|
2006-03-30 12:07:32 +00:00
|
|
|
{
|
|
|
|
logf("we can't append!");
|
|
|
|
close(masterfd);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2006-10-04 09:05:01 +00:00
|
|
|
while (entries_processed < h->entry_count)
|
2006-03-30 12:07:32 +00:00
|
|
|
{
|
2007-02-25 20:41:51 +00:00
|
|
|
int count = MIN(h->entry_count - entries_processed, max_entries);
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2006-10-04 09:05:01 +00:00
|
|
|
/* Read in as many entries as possible. */
|
|
|
|
for (i = 0; i < count; i++)
|
2006-03-30 12:07:32 +00:00
|
|
|
{
|
2007-12-18 21:50:29 +00:00
|
|
|
struct temp_file_entry *tfe = &entrybuf[i];
|
|
|
|
int datastart;
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2006-10-04 09:05:01 +00:00
|
|
|
/* Read in numeric data. */
|
2007-12-18 21:50:29 +00:00
|
|
|
if (read(tmpfd, tfe, sizeof(struct temp_file_entry)) !=
|
2006-10-04 09:05:01 +00:00
|
|
|
sizeof(struct temp_file_entry))
|
|
|
|
{
|
|
|
|
logf("read fail #1");
|
|
|
|
close(masterfd);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2007-12-18 21:50:29 +00:00
|
|
|
datastart = lseek(tmpfd, 0, SEEK_CUR);
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2007-12-18 21:50:29 +00:00
|
|
|
/**
|
|
|
|
* Read string data from the following tags:
|
|
|
|
* - tag_filename
|
|
|
|
* - tag_artist
|
|
|
|
* - tag_album
|
|
|
|
* - tag_title
|
2021-07-31 14:26:59 +00:00
|
|
|
*
|
2007-12-18 21:50:29 +00:00
|
|
|
* A crc32 hash is calculated from the read data
|
|
|
|
* and stored back to the data offset field kept in memory.
|
|
|
|
*/
|
|
|
|
#define tmpdb_read_string_tag(tag) \
|
|
|
|
lseek(tmpfd, tfe->tag_offset[tag], SEEK_CUR); \
|
2021-07-30 06:13:57 +00:00
|
|
|
if ((unsigned long)tfe->tag_length[tag] > (unsigned long)bufsz) \
|
2007-12-18 21:50:29 +00:00
|
|
|
{ \
|
|
|
|
logf("read fail: buffer overflow"); \
|
|
|
|
close(masterfd); \
|
|
|
|
return false; \
|
|
|
|
} \
|
|
|
|
\
|
|
|
|
if (read(tmpfd, buf, tfe->tag_length[tag]) != \
|
|
|
|
tfe->tag_length[tag]) \
|
|
|
|
{ \
|
|
|
|
logf("read fail #2"); \
|
|
|
|
close(masterfd); \
|
|
|
|
return false; \
|
|
|
|
} \
|
2021-08-01 02:24:33 +00:00
|
|
|
str_setlen(buf, tfe->tag_length[tag]); \
|
2007-12-18 21:50:29 +00:00
|
|
|
\
|
|
|
|
tfe->tag_offset[tag] = crc_32(buf, strlen(buf), 0xffffffff); \
|
|
|
|
lseek(tmpfd, datastart, SEEK_SET)
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2007-12-18 21:50:29 +00:00
|
|
|
tmpdb_read_string_tag(tag_filename);
|
|
|
|
tmpdb_read_string_tag(tag_artist);
|
|
|
|
tmpdb_read_string_tag(tag_album);
|
|
|
|
tmpdb_read_string_tag(tag_title);
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2007-12-18 21:50:29 +00:00
|
|
|
/* Seek to the end of the string data. */
|
|
|
|
lseek(tmpfd, tfe->data_length, SEEK_CUR);
|
|
|
|
}
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2007-12-18 21:50:29 +00:00
|
|
|
/* Backup the master index position. */
|
|
|
|
masterfd_pos = lseek(masterfd, 0, SEEK_CUR);
|
|
|
|
lseek(masterfd, sizeof(struct master_header), SEEK_SET);
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2007-12-18 21:50:29 +00:00
|
|
|
/* Check if we can resurrect some deleted runtime statistics data. */
|
|
|
|
for (i = 0; i < tcmh.tch.entry_count; i++)
|
|
|
|
{
|
|
|
|
/* Read the index entry. */
|
2021-07-31 14:26:59 +00:00
|
|
|
if (ecread_index_entry(masterfd, &idx)
|
2007-12-18 21:50:29 +00:00
|
|
|
!= sizeof(struct index_entry))
|
|
|
|
{
|
|
|
|
logf("read fail #3");
|
|
|
|
close(masterfd);
|
|
|
|
return false;
|
|
|
|
}
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2007-12-18 21:50:29 +00:00
|
|
|
/**
|
|
|
|
* Skip unless the entry is marked as being deleted
|
|
|
|
* or the data has already been resurrected.
|
|
|
|
*/
|
2017-01-18 09:39:35 +00:00
|
|
|
if (!(idx.flag & FLAG_DELETED) || (idx.flag & FLAG_RESURRECTED))
|
2007-12-18 21:50:29 +00:00
|
|
|
continue;
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2007-12-18 21:50:29 +00:00
|
|
|
/* Now try to match the entry. */
|
|
|
|
/**
|
|
|
|
* To succesfully match a song, the following conditions
|
|
|
|
* must apply:
|
2021-07-31 14:26:59 +00:00
|
|
|
*
|
2007-12-18 21:50:29 +00:00
|
|
|
* For numeric fields: tag_length
|
|
|
|
* - Full identical match is required
|
2021-07-31 14:26:59 +00:00
|
|
|
*
|
2007-12-18 21:50:29 +00:00
|
|
|
* If tag_filename matches, no further checking necessary.
|
2021-07-31 14:26:59 +00:00
|
|
|
*
|
2007-12-18 21:50:29 +00:00
|
|
|
* For string hashes: tag_artist, tag_album, tag_title
|
2011-05-11 22:37:46 +00:00
|
|
|
* - All three of these must match
|
2007-12-18 21:50:29 +00:00
|
|
|
*/
|
|
|
|
for (j = 0; j < count; j++)
|
|
|
|
{
|
|
|
|
struct temp_file_entry *tfe = &entrybuf[j];
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2007-12-18 21:50:29 +00:00
|
|
|
/* Try to match numeric fields first. */
|
|
|
|
if (tfe->tag_offset[tag_length] != idx.tag_seek[tag_length])
|
|
|
|
continue;
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2007-12-18 21:50:29 +00:00
|
|
|
/* Now it's time to do the hash matching. */
|
|
|
|
if (tfe->tag_offset[tag_filename] != idx.tag_seek[tag_filename])
|
|
|
|
{
|
|
|
|
int match_count = 0;
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2007-12-18 21:50:29 +00:00
|
|
|
/* No filename match, check if we can match two other tags. */
|
|
|
|
#define tmpdb_match(tag) \
|
|
|
|
if (tfe->tag_offset[tag] == idx.tag_seek[tag]) \
|
|
|
|
match_count++
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2007-12-18 21:50:29 +00:00
|
|
|
tmpdb_match(tag_artist);
|
|
|
|
tmpdb_match(tag_album);
|
|
|
|
tmpdb_match(tag_title);
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2011-05-11 22:37:46 +00:00
|
|
|
if (match_count < 3)
|
2007-12-18 21:50:29 +00:00
|
|
|
{
|
|
|
|
/* Still no match found, give up. */
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2007-12-18 21:50:29 +00:00
|
|
|
/* A match found, now copy & resurrect the statistical data. */
|
|
|
|
#define tmpdb_copy_tag(tag) \
|
|
|
|
tfe->tag_offset[tag] = idx.tag_seek[tag]
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2007-12-18 21:50:29 +00:00
|
|
|
tmpdb_copy_tag(tag_playcount);
|
|
|
|
tmpdb_copy_tag(tag_rating);
|
|
|
|
tmpdb_copy_tag(tag_playtime);
|
|
|
|
tmpdb_copy_tag(tag_lastplayed);
|
|
|
|
tmpdb_copy_tag(tag_commitid);
|
2013-07-14 11:59:39 +00:00
|
|
|
tmpdb_copy_tag(tag_lastelapsed);
|
2011-01-02 02:49:13 +00:00
|
|
|
tmpdb_copy_tag(tag_lastoffset);
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2007-12-18 21:50:29 +00:00
|
|
|
/* Avoid processing this entry again. */
|
|
|
|
idx.flag |= FLAG_RESURRECTED;
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2017-01-15 23:10:38 +00:00
|
|
|
lseek(masterfd, -(off_t)sizeof(struct index_entry), SEEK_CUR);
|
2010-03-16 12:15:56 +00:00
|
|
|
if (ecwrite_index_entry(masterfd, &idx) != sizeof(struct index_entry))
|
2007-12-18 21:50:29 +00:00
|
|
|
{
|
|
|
|
logf("masterfd writeback fail #1");
|
|
|
|
close(masterfd);
|
|
|
|
return false;
|
|
|
|
}
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2007-12-18 21:50:29 +00:00
|
|
|
logf("Entry resurrected");
|
|
|
|
}
|
2006-03-30 12:07:32 +00:00
|
|
|
}
|
2021-07-31 14:26:59 +00:00
|
|
|
|
|
|
|
|
2007-12-18 21:50:29 +00:00
|
|
|
/* Restore the master index position. */
|
|
|
|
lseek(masterfd, masterfd_pos, SEEK_SET);
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2006-10-04 09:05:01 +00:00
|
|
|
/* Commit the data to the index. */
|
|
|
|
for (i = 0; i < count; i++)
|
2006-03-30 12:07:32 +00:00
|
|
|
{
|
2006-10-04 09:05:01 +00:00
|
|
|
int loc = lseek(masterfd, 0, SEEK_CUR);
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2010-03-16 12:15:56 +00:00
|
|
|
if (ecread_index_entry(masterfd, &idx) != sizeof(struct index_entry))
|
2006-10-04 09:05:01 +00:00
|
|
|
{
|
2007-12-18 21:50:29 +00:00
|
|
|
logf("read fail #3");
|
2006-10-04 09:05:01 +00:00
|
|
|
close(masterfd);
|
|
|
|
return false;
|
|
|
|
}
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2006-10-04 09:05:01 +00:00
|
|
|
for (j = 0; j < TAG_COUNT; j++)
|
|
|
|
{
|
2009-06-03 08:19:32 +00:00
|
|
|
if (!TAGCACHE_IS_NUMERIC(j))
|
2006-10-04 09:05:01 +00:00
|
|
|
continue;
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2006-10-04 09:05:01 +00:00
|
|
|
idx.tag_seek[j] = entrybuf[i].tag_offset[j];
|
|
|
|
}
|
|
|
|
idx.flag = entrybuf[i].flag;
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2007-12-18 21:50:29 +00:00
|
|
|
if (idx.tag_seek[tag_commitid])
|
|
|
|
{
|
|
|
|
/* Data has been resurrected. */
|
|
|
|
idx.flag |= FLAG_DIRTYNUM;
|
|
|
|
}
|
|
|
|
else if (tc_stat.ready && current_tcmh.commitid > 0)
|
2007-02-25 20:41:51 +00:00
|
|
|
{
|
|
|
|
idx.tag_seek[tag_commitid] = current_tcmh.commitid;
|
|
|
|
idx.flag |= FLAG_DIRTYNUM;
|
|
|
|
}
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2006-10-04 09:05:01 +00:00
|
|
|
/* Write back the updated index. */
|
|
|
|
lseek(masterfd, loc, SEEK_SET);
|
2010-03-16 12:15:56 +00:00
|
|
|
if (ecwrite_index_entry(masterfd, &idx) != sizeof(struct index_entry))
|
2006-10-04 09:05:01 +00:00
|
|
|
{
|
|
|
|
logf("write fail");
|
|
|
|
close(masterfd);
|
|
|
|
return false;
|
|
|
|
}
|
2006-03-30 12:07:32 +00:00
|
|
|
}
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2006-10-04 09:05:01 +00:00
|
|
|
entries_processed += count;
|
2007-03-18 09:50:53 +00:00
|
|
|
logf("%d/%ld entries processed", entries_processed, h->entry_count);
|
2006-03-30 12:07:32 +00:00
|
|
|
}
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2006-03-30 12:07:32 +00:00
|
|
|
close(masterfd);
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2006-03-30 12:07:32 +00:00
|
|
|
return true;
|
|
|
|
}
|
2006-04-18 18:56:56 +00:00
|
|
|
|
2006-04-23 18:47:26 +00:00
|
|
|
/**
|
|
|
|
* Return values:
|
|
|
|
* > 0 success
|
|
|
|
* == 0 temporary failure
|
|
|
|
* < 0 fatal error
|
|
|
|
*/
|
|
|
|
static int build_index(int index_type, struct tagcache_header *h, int tmpfd)
|
2006-03-26 11:33:42 +00:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
struct tagcache_header tch;
|
2006-07-20 12:19:31 +00:00
|
|
|
struct master_header tcmh;
|
2006-04-18 18:56:56 +00:00
|
|
|
struct index_entry idxbuf[IDX_BUF_DEPTH];
|
|
|
|
int idxbuf_pos;
|
2021-08-01 02:24:33 +00:00
|
|
|
char buf[TAGCACHE_BUFSZ];
|
2021-07-30 06:13:57 +00:00
|
|
|
const long bufsz = sizeof(buf);
|
2006-03-26 11:33:42 +00:00
|
|
|
int fd = -1, masterfd;
|
|
|
|
bool error = false;
|
|
|
|
int init;
|
|
|
|
int masterfd_pos;
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2006-03-26 11:33:42 +00:00
|
|
|
logf("Building index: %d", index_type);
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2006-07-22 07:09:57 +00:00
|
|
|
/* Check the number of entries we need to allocate ram for. */
|
|
|
|
commit_entry_count = h->entry_count + 1;
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2006-07-22 07:09:57 +00:00
|
|
|
masterfd = open_master_fd(&tcmh, false);
|
|
|
|
if (masterfd >= 0)
|
|
|
|
{
|
|
|
|
commit_entry_count += tcmh.tch.entry_count;
|
|
|
|
close(masterfd);
|
|
|
|
}
|
2007-02-25 20:41:51 +00:00
|
|
|
else
|
|
|
|
remove_files(); /* Just to be sure we are clean. */
|
2006-09-26 11:23:18 +00:00
|
|
|
|
|
|
|
/* Open the index file, which contains the tag names. */
|
|
|
|
fd = open_tag_fd(&tch, index_type, true);
|
|
|
|
if (fd >= 0)
|
|
|
|
{
|
2007-03-18 09:50:53 +00:00
|
|
|
logf("tch.datasize=%ld", tch.datasize);
|
2006-09-26 11:23:18 +00:00
|
|
|
lookup_buffer_depth = 1 +
|
|
|
|
/* First part */ commit_entry_count +
|
|
|
|
/* Second part */ (tch.datasize / TAGFILE_ENTRY_CHUNK_LENGTH);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
lookup_buffer_depth = 1 +
|
|
|
|
/* First part */ commit_entry_count +
|
|
|
|
/* Second part */ 0;
|
|
|
|
}
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2007-03-18 09:50:53 +00:00
|
|
|
logf("lookup_buffer_depth=%ld", lookup_buffer_depth);
|
|
|
|
logf("commit_entry_count=%ld", commit_entry_count);
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2006-09-26 11:23:18 +00:00
|
|
|
/* Allocate buffer for all index entries from both old and new
|
|
|
|
* tag files. */
|
2006-03-26 11:33:42 +00:00
|
|
|
tempbufidx = 0;
|
2006-07-22 07:09:57 +00:00
|
|
|
tempbuf_pos = commit_entry_count * sizeof(struct tempbuf_searchidx);
|
2006-09-26 11:23:18 +00:00
|
|
|
|
|
|
|
/* Allocate lookup buffer. The first portion of commit_entry_count
|
|
|
|
* contains the new tags in the temporary file and the second
|
|
|
|
* part for locating entries already in the db.
|
2021-07-31 14:26:59 +00:00
|
|
|
*
|
2006-09-26 11:23:18 +00:00
|
|
|
* New tags Old tags
|
|
|
|
* +---------+---------------------------+
|
|
|
|
* | index | position/ENTRY_CHUNK_SIZE | lookup buffer
|
|
|
|
* +---------+---------------------------+
|
2021-07-31 14:26:59 +00:00
|
|
|
*
|
2006-09-26 11:23:18 +00:00
|
|
|
* Old tags are inserted to a temporary buffer with position:
|
|
|
|
* tempbuf_insert(position/ENTRY_CHUNK_SIZE, ...);
|
|
|
|
* And new tags with index:
|
|
|
|
* tempbuf_insert(idx, ...);
|
2021-07-31 14:26:59 +00:00
|
|
|
*
|
2006-09-26 11:23:18 +00:00
|
|
|
* The buffer is sorted and written into tag file:
|
|
|
|
* tempbuf_sort(...);
|
|
|
|
* leaving master index locations messed up.
|
2021-07-31 14:26:59 +00:00
|
|
|
*
|
2006-09-26 11:23:18 +00:00
|
|
|
* That is fixed using the lookup buffer for old tags:
|
|
|
|
* new_seek = tempbuf_find_location(old_seek, ...);
|
|
|
|
* and for new tags:
|
|
|
|
* new_seek = tempbuf_find_location(idx);
|
|
|
|
*/
|
|
|
|
lookup = (struct tempbuf_searchidx **)&tempbuf[tempbuf_pos];
|
|
|
|
tempbuf_pos += lookup_buffer_depth * sizeof(void **);
|
|
|
|
memset(lookup, 0, lookup_buffer_depth * sizeof(void **));
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2006-09-26 11:23:18 +00:00
|
|
|
/* And calculate the remaining data space used mainly for storing
|
|
|
|
* tag data (strings). */
|
2006-04-23 18:47:26 +00:00
|
|
|
tempbuf_left = tempbuf_size - tempbuf_pos - 8;
|
2006-07-22 07:09:57 +00:00
|
|
|
if (tempbuf_left - TAGFILE_ENTRY_AVG_LENGTH * commit_entry_count < 0)
|
2006-03-26 11:33:42 +00:00
|
|
|
{
|
|
|
|
logf("Buffer way too small!");
|
2021-07-30 06:13:57 +00:00
|
|
|
close(fd);
|
2006-04-23 18:47:26 +00:00
|
|
|
return 0;
|
2006-03-26 11:33:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (fd >= 0)
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* If tag file contains unique tags (sorted index), we will load
|
|
|
|
* it entirely into memory so we can resort it later for use with
|
|
|
|
* chunked browsing.
|
|
|
|
*/
|
2009-06-03 08:19:32 +00:00
|
|
|
if (TAGCACHE_IS_SORTED(index_type))
|
2006-03-26 11:33:42 +00:00
|
|
|
{
|
2006-04-18 18:56:56 +00:00
|
|
|
logf("loading tags...");
|
2006-03-26 11:33:42 +00:00
|
|
|
for (i = 0; i < tch.entry_count; i++)
|
|
|
|
{
|
|
|
|
struct tagfile_entry entry;
|
|
|
|
int loc = lseek(fd, 0, SEEK_CUR);
|
2006-09-26 11:23:18 +00:00
|
|
|
bool ret;
|
2021-08-01 02:24:33 +00:00
|
|
|
switch (ecread_tagfile_entry_and_tag(fd, &entry, buf, bufsz))
|
2006-03-26 11:33:42 +00:00
|
|
|
{
|
2021-08-01 02:24:33 +00:00
|
|
|
case e_SUCCESS:
|
|
|
|
break;
|
|
|
|
case e_ENTRY_SIZEMISMATCH:
|
|
|
|
logf("read error #7");
|
|
|
|
close(fd);
|
|
|
|
return -2;
|
|
|
|
case e_TAG_TOOLONG:
|
|
|
|
logf("too long tag #3");
|
|
|
|
close(fd);
|
|
|
|
return -2;
|
|
|
|
case e_TAG_SIZEMISMATCH:
|
|
|
|
logf("read error #8");
|
|
|
|
close(fd);
|
|
|
|
return -2;
|
|
|
|
default:
|
|
|
|
logf("unknown_error");
|
|
|
|
break;;
|
2006-03-26 11:33:42 +00:00
|
|
|
}
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2006-07-10 16:22:03 +00:00
|
|
|
/* Skip deleted entries. */
|
|
|
|
if (buf[0] == '\0')
|
|
|
|
continue;
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2006-03-26 11:33:42 +00:00
|
|
|
/**
|
|
|
|
* Save the tag and tag id in the memory buffer. Tag id
|
|
|
|
* is saved so we can later reindex the master lookup
|
|
|
|
* table when the index gets resorted.
|
|
|
|
*/
|
2021-07-31 14:26:59 +00:00
|
|
|
ret = tempbuf_insert(buf, loc/TAGFILE_ENTRY_CHUNK_LENGTH
|
2006-09-26 11:23:18 +00:00
|
|
|
+ commit_entry_count, entry.idx_id,
|
2009-06-03 08:19:32 +00:00
|
|
|
TAGCACHE_IS_UNIQUE(index_type));
|
2006-09-26 11:23:18 +00:00
|
|
|
if (!ret)
|
|
|
|
{
|
|
|
|
close(fd);
|
|
|
|
return -3;
|
|
|
|
}
|
2007-03-10 14:34:56 +00:00
|
|
|
do_timed_yield();
|
2006-03-26 11:33:42 +00:00
|
|
|
}
|
2006-04-18 18:56:56 +00:00
|
|
|
logf("done");
|
2006-03-26 11:33:42 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
tempbufidx = tch.entry_count;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Creating new index file to store the tags. No need to preload
|
|
|
|
* anything whether the index type is sorted or not.
|
|
|
|
*/
|
2021-07-30 06:13:57 +00:00
|
|
|
snprintf(buf, bufsz, TAGCACHE_FILE_INDEX, index_type);
|
2010-12-06 22:26:31 +00:00
|
|
|
fd = open(buf, O_WRONLY | O_CREAT | O_TRUNC, 0666);
|
2006-03-26 11:33:42 +00:00
|
|
|
if (fd < 0)
|
|
|
|
{
|
|
|
|
logf("%s open fail", buf);
|
2006-04-23 18:47:26 +00:00
|
|
|
return -2;
|
2006-03-26 11:33:42 +00:00
|
|
|
}
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2006-03-26 11:33:42 +00:00
|
|
|
tch.magic = TAGCACHE_MAGIC;
|
|
|
|
tch.entry_count = 0;
|
|
|
|
tch.datasize = 0;
|
2021-07-31 14:26:59 +00:00
|
|
|
|
|
|
|
if (ecwrite(fd, &tch, 1, tagcache_header_ec, tc_stat.econ)
|
2007-02-13 21:51:18 +00:00
|
|
|
!= sizeof(struct tagcache_header))
|
2006-03-26 11:33:42 +00:00
|
|
|
{
|
|
|
|
logf("header write failed");
|
|
|
|
close(fd);
|
2006-04-23 18:47:26 +00:00
|
|
|
return -2;
|
2006-03-26 11:33:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Loading the tag lookup file as "master file". */
|
|
|
|
logf("Loading index file");
|
2010-12-06 22:26:31 +00:00
|
|
|
masterfd = open(TAGCACHE_FILE_MASTER, O_RDWR);
|
2006-03-26 11:33:42 +00:00
|
|
|
|
|
|
|
if (masterfd < 0)
|
|
|
|
{
|
2007-02-25 20:41:51 +00:00
|
|
|
logf("Creating new DB");
|
2010-12-06 22:26:31 +00:00
|
|
|
masterfd = open(TAGCACHE_FILE_MASTER, O_WRONLY | O_CREAT | O_TRUNC, 0666);
|
2006-03-26 11:33:42 +00:00
|
|
|
|
|
|
|
if (masterfd < 0)
|
|
|
|
{
|
2010-12-06 22:26:31 +00:00
|
|
|
logf("Failure to create index file (%s)", TAGCACHE_FILE_MASTER);
|
2006-03-26 11:33:42 +00:00
|
|
|
close(fd);
|
2006-04-23 18:47:26 +00:00
|
|
|
return -2;
|
2006-03-26 11:33:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Write the header (write real values later). */
|
2006-07-20 12:19:31 +00:00
|
|
|
memset(&tcmh, 0, sizeof(struct master_header));
|
|
|
|
tcmh.tch = *h;
|
|
|
|
tcmh.tch.entry_count = 0;
|
|
|
|
tcmh.tch.datasize = 0;
|
2007-02-25 20:41:51 +00:00
|
|
|
tcmh.dirty = true;
|
2007-02-13 21:51:18 +00:00
|
|
|
ecwrite(masterfd, &tcmh, 1, master_header_ec, tc_stat.econ);
|
2006-03-26 11:33:42 +00:00
|
|
|
init = true;
|
|
|
|
masterfd_pos = lseek(masterfd, 0, SEEK_CUR);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Master file already exists so we need to process the current
|
|
|
|
* file first.
|
|
|
|
*/
|
|
|
|
init = false;
|
|
|
|
|
2007-02-13 21:51:18 +00:00
|
|
|
if (ecread(masterfd, &tcmh, 1, master_header_ec, tc_stat.econ) !=
|
2006-07-20 12:19:31 +00:00
|
|
|
sizeof(struct master_header) || tcmh.tch.magic != TAGCACHE_MAGIC)
|
2006-03-26 11:33:42 +00:00
|
|
|
{
|
|
|
|
logf("header error");
|
|
|
|
close(fd);
|
|
|
|
close(masterfd);
|
2006-04-23 18:47:26 +00:00
|
|
|
return -2;
|
2006-03-26 11:33:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* If we reach end of the master file, we need to expand it to
|
|
|
|
* hold new tags. If the current index is not sorted, we can
|
|
|
|
* simply append new data to end of the file.
|
|
|
|
* However, if the index is sorted, we need to update all tag
|
|
|
|
* pointers in the master file for the current index.
|
|
|
|
*/
|
2006-07-20 12:19:31 +00:00
|
|
|
masterfd_pos = lseek(masterfd, tcmh.tch.entry_count * sizeof(struct index_entry),
|
2006-03-26 11:33:42 +00:00
|
|
|
SEEK_CUR);
|
|
|
|
if (masterfd_pos == filesize(masterfd))
|
|
|
|
{
|
|
|
|
logf("appending...");
|
|
|
|
init = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Load new unique tags in memory to be sorted later and added
|
|
|
|
* to the master lookup file.
|
|
|
|
*/
|
2009-06-03 08:19:32 +00:00
|
|
|
if (TAGCACHE_IS_SORTED(index_type))
|
2006-03-26 11:33:42 +00:00
|
|
|
{
|
|
|
|
lseek(tmpfd, sizeof(struct tagcache_header), SEEK_SET);
|
|
|
|
/* h is the header of the temporary file containing new tags. */
|
2006-04-18 18:56:56 +00:00
|
|
|
logf("inserting new tags...");
|
2006-03-26 11:33:42 +00:00
|
|
|
for (i = 0; i < h->entry_count; i++)
|
|
|
|
{
|
|
|
|
struct temp_file_entry entry;
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2006-03-26 11:33:42 +00:00
|
|
|
if (read(tmpfd, &entry, sizeof(struct temp_file_entry)) !=
|
|
|
|
sizeof(struct temp_file_entry))
|
|
|
|
{
|
2007-02-13 21:51:18 +00:00
|
|
|
logf("read fail #3");
|
2006-03-26 11:33:42 +00:00
|
|
|
error = true;
|
|
|
|
goto error_exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Read data. */
|
2021-07-30 06:13:57 +00:00
|
|
|
if (entry.tag_length[index_type] >= (long)bufsz)
|
2006-03-26 11:33:42 +00:00
|
|
|
{
|
|
|
|
logf("too long entry!");
|
|
|
|
error = true;
|
|
|
|
goto error_exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
lseek(tmpfd, entry.tag_offset[index_type], SEEK_CUR);
|
|
|
|
if (read(tmpfd, buf, entry.tag_length[index_type]) !=
|
|
|
|
entry.tag_length[index_type])
|
|
|
|
{
|
2007-02-13 21:51:18 +00:00
|
|
|
logf("read fail #4");
|
2006-03-26 11:33:42 +00:00
|
|
|
error = true;
|
|
|
|
goto error_exit;
|
|
|
|
}
|
2021-08-01 02:24:33 +00:00
|
|
|
str_setlen(buf, entry.tag_length[index_type]);
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2009-06-03 08:19:32 +00:00
|
|
|
if (TAGCACHE_IS_UNIQUE(index_type))
|
2006-04-23 18:47:26 +00:00
|
|
|
error = !tempbuf_insert(buf, i, -1, true);
|
2006-03-27 07:44:32 +00:00
|
|
|
else
|
2006-07-20 12:19:31 +00:00
|
|
|
error = !tempbuf_insert(buf, i, tcmh.tch.entry_count + i, false);
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2006-03-27 07:44:32 +00:00
|
|
|
if (error)
|
2006-03-26 11:33:42 +00:00
|
|
|
{
|
|
|
|
logf("insert error");
|
|
|
|
goto error_exit;
|
|
|
|
}
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2006-03-26 11:33:42 +00:00
|
|
|
/* Skip to next. */
|
|
|
|
lseek(tmpfd, entry.data_length - entry.tag_offset[index_type] -
|
|
|
|
entry.tag_length[index_type], SEEK_CUR);
|
2007-03-10 14:34:56 +00:00
|
|
|
do_timed_yield();
|
2006-03-26 11:33:42 +00:00
|
|
|
}
|
2006-04-18 18:56:56 +00:00
|
|
|
logf("done");
|
2006-03-26 11:33:42 +00:00
|
|
|
|
|
|
|
/* Sort the buffer data and write it to the index file. */
|
|
|
|
lseek(fd, sizeof(struct tagcache_header), SEEK_SET);
|
2009-06-20 16:17:54 +00:00
|
|
|
/**
|
|
|
|
* We need to truncate the index file now. There can be junk left
|
|
|
|
* at the end of file (however, we _should_ always follow the
|
|
|
|
* entry_count and don't crash with that).
|
|
|
|
*/
|
|
|
|
ftruncate(fd, lseek(fd, 0, SEEK_CUR));
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2006-03-26 11:33:42 +00:00
|
|
|
i = tempbuf_sort(fd);
|
|
|
|
if (i < 0)
|
|
|
|
goto error_exit;
|
|
|
|
logf("sorted %d tags", i);
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2006-03-26 11:33:42 +00:00
|
|
|
/**
|
|
|
|
* Now update all indexes in the master lookup file.
|
|
|
|
*/
|
2006-04-18 18:56:56 +00:00
|
|
|
logf("updating indices...");
|
2006-07-20 12:19:31 +00:00
|
|
|
lseek(masterfd, sizeof(struct master_header), SEEK_SET);
|
|
|
|
for (i = 0; i < tcmh.tch.entry_count; i += idxbuf_pos)
|
2006-03-26 11:33:42 +00:00
|
|
|
{
|
2006-04-18 18:56:56 +00:00
|
|
|
int j;
|
2006-03-26 11:33:42 +00:00
|
|
|
int loc = lseek(masterfd, 0, SEEK_CUR);
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2006-07-20 12:19:31 +00:00
|
|
|
idxbuf_pos = MIN(tcmh.tch.entry_count - i, IDX_BUF_DEPTH);
|
2021-07-31 14:26:59 +00:00
|
|
|
|
|
|
|
if (ecread(masterfd, idxbuf, idxbuf_pos, index_entry_ec, tc_stat.econ)
|
2007-02-13 21:51:18 +00:00
|
|
|
!= (int)sizeof(struct index_entry)*idxbuf_pos)
|
2006-03-26 11:33:42 +00:00
|
|
|
{
|
2007-02-13 21:51:18 +00:00
|
|
|
logf("read fail #5");
|
2006-03-26 11:33:42 +00:00
|
|
|
error = true;
|
|
|
|
goto error_exit ;
|
|
|
|
}
|
2006-04-18 18:56:56 +00:00
|
|
|
lseek(masterfd, loc, SEEK_SET);
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2006-04-18 18:56:56 +00:00
|
|
|
for (j = 0; j < idxbuf_pos; j++)
|
2006-03-26 11:33:42 +00:00
|
|
|
{
|
2006-07-10 16:22:03 +00:00
|
|
|
if (idxbuf[j].flag & FLAG_DELETED)
|
|
|
|
{
|
2006-07-22 18:58:28 +00:00
|
|
|
/* We can just ignore deleted entries. */
|
2007-12-18 21:50:29 +00:00
|
|
|
// idxbuf[j].tag_seek[index_type] = 0;
|
2006-07-10 16:22:03 +00:00
|
|
|
continue;
|
|
|
|
}
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2006-04-18 18:56:56 +00:00
|
|
|
idxbuf[j].tag_seek[index_type] = tempbuf_find_location(
|
2006-04-23 18:47:26 +00:00
|
|
|
idxbuf[j].tag_seek[index_type]/TAGFILE_ENTRY_CHUNK_LENGTH
|
2006-07-22 07:09:57 +00:00
|
|
|
+ commit_entry_count);
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2006-04-18 18:56:56 +00:00
|
|
|
if (idxbuf[j].tag_seek[index_type] < 0)
|
|
|
|
{
|
2021-07-31 14:26:59 +00:00
|
|
|
logf("update error: %ld/%d/%ld",
|
2007-12-18 21:50:29 +00:00
|
|
|
idxbuf[j].flag, i+j, tcmh.tch.entry_count);
|
2006-04-18 18:56:56 +00:00
|
|
|
error = true;
|
|
|
|
goto error_exit;
|
|
|
|
}
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2007-03-10 14:34:56 +00:00
|
|
|
do_timed_yield();
|
2006-03-26 11:33:42 +00:00
|
|
|
}
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2006-03-26 11:33:42 +00:00
|
|
|
/* Write back the updated index. */
|
2007-02-13 21:51:18 +00:00
|
|
|
if (ecwrite(masterfd, idxbuf, idxbuf_pos,
|
|
|
|
index_entry_ec, tc_stat.econ) !=
|
2006-04-18 18:56:56 +00:00
|
|
|
(int)sizeof(struct index_entry)*idxbuf_pos)
|
2006-03-26 11:33:42 +00:00
|
|
|
{
|
|
|
|
logf("write fail");
|
|
|
|
error = true;
|
|
|
|
goto error_exit;
|
|
|
|
}
|
|
|
|
}
|
2006-04-18 18:56:56 +00:00
|
|
|
logf("done");
|
2006-03-26 11:33:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Walk through the temporary file containing the new tags.
|
|
|
|
*/
|
|
|
|
// build_normal_index(h, tmpfd, masterfd, idx);
|
2006-04-18 18:56:56 +00:00
|
|
|
logf("updating new indices...");
|
2006-03-26 11:33:42 +00:00
|
|
|
lseek(masterfd, masterfd_pos, SEEK_SET);
|
|
|
|
lseek(tmpfd, sizeof(struct tagcache_header), SEEK_SET);
|
|
|
|
lseek(fd, 0, SEEK_END);
|
2006-04-18 18:56:56 +00:00
|
|
|
for (i = 0; i < h->entry_count; i += idxbuf_pos)
|
2006-03-26 11:33:42 +00:00
|
|
|
{
|
2006-04-18 18:56:56 +00:00
|
|
|
int j;
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2006-04-18 18:56:56 +00:00
|
|
|
idxbuf_pos = MIN(h->entry_count - i, IDX_BUF_DEPTH);
|
2006-03-26 11:33:42 +00:00
|
|
|
if (init)
|
|
|
|
{
|
2006-04-18 18:56:56 +00:00
|
|
|
memset(idxbuf, 0, sizeof(struct index_entry)*IDX_BUF_DEPTH);
|
2006-03-26 11:33:42 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2006-04-18 18:56:56 +00:00
|
|
|
int loc = lseek(masterfd, 0, SEEK_CUR);
|
2021-07-31 14:26:59 +00:00
|
|
|
|
|
|
|
if (ecread(masterfd, idxbuf, idxbuf_pos, index_entry_ec, tc_stat.econ)
|
2007-02-13 21:51:18 +00:00
|
|
|
!= (int)sizeof(struct index_entry)*idxbuf_pos)
|
2006-03-26 11:33:42 +00:00
|
|
|
{
|
2007-02-13 21:51:18 +00:00
|
|
|
logf("read fail #6");
|
2006-03-26 11:33:42 +00:00
|
|
|
error = true;
|
|
|
|
break ;
|
|
|
|
}
|
2006-04-18 18:56:56 +00:00
|
|
|
lseek(masterfd, loc, SEEK_SET);
|
2006-03-26 11:33:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Read entry headers. */
|
2006-04-18 18:56:56 +00:00
|
|
|
for (j = 0; j < idxbuf_pos; j++)
|
2006-03-26 11:33:42 +00:00
|
|
|
{
|
2009-06-03 08:19:32 +00:00
|
|
|
if (!TAGCACHE_IS_SORTED(index_type))
|
2006-03-26 11:33:42 +00:00
|
|
|
{
|
2006-04-18 18:56:56 +00:00
|
|
|
struct temp_file_entry entry;
|
|
|
|
struct tagfile_entry fe;
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2006-04-18 18:56:56 +00:00
|
|
|
if (read(tmpfd, &entry, sizeof(struct temp_file_entry)) !=
|
|
|
|
sizeof(struct temp_file_entry))
|
|
|
|
{
|
2007-02-13 21:51:18 +00:00
|
|
|
logf("read fail #7");
|
2006-04-18 18:56:56 +00:00
|
|
|
error = true;
|
|
|
|
break ;
|
|
|
|
}
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2006-04-18 18:56:56 +00:00
|
|
|
/* Read data. */
|
|
|
|
if (entry.tag_length[index_type] >= (int)sizeof(buf))
|
|
|
|
{
|
|
|
|
logf("too long entry!");
|
|
|
|
logf("length=%d", entry.tag_length[index_type]);
|
2007-03-20 13:53:23 +00:00
|
|
|
logf("pos=0x%02lx", lseek(tmpfd, 0, SEEK_CUR));
|
2006-04-18 18:56:56 +00:00
|
|
|
error = true;
|
|
|
|
break ;
|
|
|
|
}
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2006-04-18 18:56:56 +00:00
|
|
|
lseek(tmpfd, entry.tag_offset[index_type], SEEK_CUR);
|
|
|
|
if (read(tmpfd, buf, entry.tag_length[index_type]) !=
|
|
|
|
entry.tag_length[index_type])
|
|
|
|
{
|
2007-02-13 21:51:18 +00:00
|
|
|
logf("read fail #8");
|
2007-03-18 09:50:53 +00:00
|
|
|
logf("offset=0x%02lx", entry.tag_offset[index_type]);
|
2006-04-18 18:56:56 +00:00
|
|
|
logf("length=0x%02x", entry.tag_length[index_type]);
|
|
|
|
error = true;
|
|
|
|
break ;
|
|
|
|
}
|
2006-03-26 11:33:42 +00:00
|
|
|
|
2006-04-18 18:56:56 +00:00
|
|
|
/* Write to index file. */
|
|
|
|
idxbuf[j].tag_seek[index_type] = lseek(fd, 0, SEEK_CUR);
|
|
|
|
fe.tag_length = entry.tag_length[index_type];
|
2006-07-20 12:19:31 +00:00
|
|
|
fe.idx_id = tcmh.tch.entry_count + i + j;
|
2007-02-13 21:51:18 +00:00
|
|
|
ecwrite(fd, &fe, 1, tagfile_entry_ec, tc_stat.econ);
|
2006-04-18 18:56:56 +00:00
|
|
|
write(fd, buf, fe.tag_length);
|
|
|
|
tempbufidx++;
|
|
|
|
|
|
|
|
/* Skip to next. */
|
|
|
|
lseek(tmpfd, entry.data_length - entry.tag_offset[index_type] -
|
|
|
|
entry.tag_length[index_type], SEEK_CUR);
|
2006-03-26 11:33:42 +00:00
|
|
|
}
|
2006-04-18 18:56:56 +00:00
|
|
|
else
|
2006-03-26 11:33:42 +00:00
|
|
|
{
|
2006-04-18 18:56:56 +00:00
|
|
|
/* Locate the correct entry from the sorted array. */
|
|
|
|
idxbuf[j].tag_seek[index_type] = tempbuf_find_location(i + j);
|
|
|
|
if (idxbuf[j].tag_seek[index_type] < 0)
|
|
|
|
{
|
2007-03-20 13:53:23 +00:00
|
|
|
logf("entry not found (%d)", j);
|
2006-04-18 18:56:56 +00:00
|
|
|
error = true;
|
|
|
|
break ;
|
|
|
|
}
|
2006-03-26 11:33:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Write index. */
|
2007-02-13 21:51:18 +00:00
|
|
|
if (ecwrite(masterfd, idxbuf, idxbuf_pos,
|
|
|
|
index_entry_ec, tc_stat.econ) !=
|
2006-04-18 18:56:56 +00:00
|
|
|
(int)sizeof(struct index_entry)*idxbuf_pos)
|
2006-03-26 11:33:42 +00:00
|
|
|
{
|
|
|
|
logf("tagcache: write fail #4");
|
|
|
|
error = true;
|
|
|
|
break ;
|
|
|
|
}
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2007-03-10 14:34:56 +00:00
|
|
|
do_timed_yield();
|
2006-03-26 11:33:42 +00:00
|
|
|
}
|
2006-04-18 18:56:56 +00:00
|
|
|
logf("done");
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2006-04-23 18:47:26 +00:00
|
|
|
/* Finally write the header. */
|
|
|
|
tch.magic = TAGCACHE_MAGIC;
|
|
|
|
tch.entry_count = tempbufidx;
|
|
|
|
tch.datasize = lseek(fd, 0, SEEK_END) - sizeof(struct tagcache_header);
|
|
|
|
lseek(fd, 0, SEEK_SET);
|
2007-02-13 21:51:18 +00:00
|
|
|
ecwrite(fd, &tch, 1, tagcache_header_ec, tc_stat.econ);
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2006-04-23 18:47:26 +00:00
|
|
|
if (index_type != tag_filename)
|
|
|
|
h->datasize += tch.datasize;
|
2007-03-18 09:50:53 +00:00
|
|
|
logf("s:%d/%ld/%ld", index_type, tch.datasize, h->datasize);
|
2006-03-26 11:33:42 +00:00
|
|
|
error_exit:
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2006-03-26 11:33:42 +00:00
|
|
|
close(fd);
|
|
|
|
close(masterfd);
|
|
|
|
|
2006-04-23 18:47:26 +00:00
|
|
|
if (error)
|
|
|
|
return -2;
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2006-04-23 18:47:26 +00:00
|
|
|
return 1;
|
2006-03-26 11:33:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static bool commit(void)
|
|
|
|
{
|
2006-07-20 12:19:31 +00:00
|
|
|
struct tagcache_header tch;
|
|
|
|
struct master_header tcmh;
|
2006-03-26 11:33:42 +00:00
|
|
|
int i, len, rc;
|
|
|
|
int tmpfd;
|
|
|
|
int masterfd;
|
2006-05-30 18:13:18 +00:00
|
|
|
#ifdef HAVE_DIRCACHE
|
|
|
|
bool dircache_buffer_stolen = false;
|
2011-08-30 14:01:45 +00:00
|
|
|
#endif
|
|
|
|
#ifdef HAVE_TC_RAMCACHE
|
|
|
|
bool ramcache_buffer_stolen = false;
|
2006-05-30 18:13:18 +00:00
|
|
|
#endif
|
2006-03-26 11:33:42 +00:00
|
|
|
logf("committing tagcache");
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2006-08-02 17:39:34 +00:00
|
|
|
while (write_lock)
|
|
|
|
sleep(1);
|
2006-09-26 11:23:18 +00:00
|
|
|
|
2010-12-06 22:26:31 +00:00
|
|
|
tmpfd = open(TAGCACHE_FILE_TEMP, O_RDONLY);
|
2006-03-26 11:33:42 +00:00
|
|
|
if (tmpfd < 0)
|
|
|
|
{
|
|
|
|
logf("nothing to commit");
|
|
|
|
return true;
|
|
|
|
}
|
2021-07-31 14:26:59 +00:00
|
|
|
|
|
|
|
|
2006-03-26 11:33:42 +00:00
|
|
|
/* Load the header. */
|
|
|
|
len = sizeof(struct tagcache_header);
|
2006-07-20 12:19:31 +00:00
|
|
|
rc = read(tmpfd, &tch, len);
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2006-07-20 12:19:31 +00:00
|
|
|
if (tch.magic != TAGCACHE_MAGIC || rc != len)
|
2006-03-26 11:33:42 +00:00
|
|
|
{
|
2010-07-18 18:17:47 +00:00
|
|
|
logf("incorrect tmpheader");
|
2006-03-26 11:33:42 +00:00
|
|
|
close(tmpfd);
|
2010-12-06 22:26:31 +00:00
|
|
|
remove(TAGCACHE_FILE_TEMP);
|
2006-03-26 11:33:42 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2006-07-20 12:19:31 +00:00
|
|
|
if (tch.entry_count == 0)
|
2006-03-26 11:33:42 +00:00
|
|
|
logf("nothing to commit");
|
|
|
|
|
2010-07-18 18:17:47 +00:00
|
|
|
/* Fully initialize existing headers (if any) before going further. */
|
|
|
|
tc_stat.ready = check_all_headers();
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2006-09-26 11:23:18 +00:00
|
|
|
#ifdef HAVE_EEPROM_SETTINGS
|
2010-12-06 22:26:31 +00:00
|
|
|
remove(TAGCACHE_STATEFILE);
|
2006-09-26 11:23:18 +00:00
|
|
|
#endif
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2006-09-26 11:23:18 +00:00
|
|
|
/* At first be sure to unload the ramcache! */
|
|
|
|
#ifdef HAVE_TC_RAMCACHE
|
2006-11-10 08:03:33 +00:00
|
|
|
tc_stat.ramcache = false;
|
2006-09-26 11:23:18 +00:00
|
|
|
#endif
|
2017-01-17 19:45:07 +00:00
|
|
|
|
|
|
|
/* Beyond here, jump to commit_error to undo locks and restore dircache */
|
|
|
|
rc = false;
|
2006-08-02 17:39:34 +00:00
|
|
|
read_lock++;
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2006-04-16 17:32:54 +00:00
|
|
|
/* Try to steal every buffer we can :) */
|
|
|
|
#ifdef HAVE_DIRCACHE
|
|
|
|
if (tempbuf_size == 0)
|
|
|
|
{
|
2017-01-17 19:45:07 +00:00
|
|
|
/* Suspend dircache to free its allocation. */
|
Rewrite filesystem code (WIP)
This patch redoes the filesystem code from the FAT driver up to the
clipboard code in onplay.c.
Not every aspect of this is finished therefore it is still "WIP". I
don't wish to do too much at once (haha!). What is left to do is get
dircache back in the sim and find an implementation for the dircache
indicies in the tagcache and playlist code or do something else that
has the same benefit. Leaving these out for now does not make anything
unusable. All the basics are done.
Phone app code should probably get vetted (and app path handling
just plain rewritten as environment expansions); the SDL app and
Android run well.
Main things addressed:
1) Thread safety: There is none right now in the trunk code. Most of
what currently works is luck when multiple threads are involved or
multiple descriptors to the same file are open.
2) POSIX compliance: Many of the functions behave nothing like their
counterparts on a host system. This leads to inconsistent code or very
different behavior from native to hosted. One huge offender was
rename(). Going point by point would fill a book.
3) Actual running RAM usage: Many targets will use less RAM and less
stack space (some more RAM because I upped the number of cache buffers
for large memory). There's very little memory lying fallow in rarely-used
areas (see 'Key core changes' below). Also, all targets may open the same
number of directory streams whereas before those with less than 8MB RAM
were limited to 8, not 12 implying those targets will save slightly
less.
4) Performance: The test_disk plugin shows markedly improved performance,
particularly in the area of (uncached) directory scanning, due partly to
more optimal directory reading and to a better sector cache algorithm.
Uncached times tend to be better while there is a bit of a slowdown in
dircache due to it being a bit heavier of an implementation. It's not
noticeable by a human as far as I can say.
Key core changes:
1) Files and directories share core code and data structures.
2) The filesystem code knows which descriptors refer to same file.
This ensures that changes from one stream are appropriately reflected
in every open descriptor for that file (fileobj_mgr.c).
3) File and directory cache buffers are borrowed from the main sector
cache. This means that when they are not in use by a file, they are not
wasted, but used for the cache. Most of the time, only a few of them
are needed. It also means that adding more file and directory handles
is less expensive. All one must do in ensure a large enough cache to
borrow from.
4) Relative path components are supported and the namespace is unified.
It does not support full relative paths to an implied current directory;
what is does support is use of "." and "..". Adding the former would
not be very difficult. The namespace is unified in the sense that
volumes may be specified several times along with relative parts, e.g.:
"/<0>/foo/../../<1>/bar" :<=> "/<1>/bar".
5) Stack usage is down due to sharing of data, static allocation and
less duplication of strings on the stack. This requires more
serialization than I would like but since the number of threads is
limited to a low number, the tradoff in favor of the stack seems
reasonable.
6) Separates and heirarchicalizes (sic) the SIM and APP filesystem
code. SIM path and volume handling is just like the target. Some
aspects of the APP file code get more straightforward (e.g. no path
hashing is needed).
Dircache:
Deserves its own section. Dircache is new but pays homage to the old.
The old one was not compatible and so it, since it got redone, does
all the stuff it always should have done such as:
1) It may be update and used at any time during the build process.
No longer has one to wait for it to finish building to do basic file
management (create, remove, rename, etc.).
2) It does not need to be either fully scanned or completely disabled;
it can be incomplete (i.e. overfilled, missing paths), still be
of benefit and be correct.
3) Handles mounting and dismounting of individual volumes which means
a full rebuild is not needed just because you pop a new SD card in the
slot. Now, because it reuses its freed entry data, may rebuild only
that volume.
4) Much more fundamental to the file code. When it is built, it is
the keeper of the master file list whether enabled or not ("disabled"
is just a state of the cache). Its must always to ready to be started
and bind all streams opened prior to being enabled.
5) Maintains any short filenames in OEM format which means that it does
not need to be rebuilt when changing the default codepage.
Miscellaneous Compatibility:
1) Update any other code that would otherwise not work such as the
hotswap mounting code in various card drivers.
2) File management: Clipboard needed updating because of the behavioral
changes. Still needs a little more work on some finer points.
3) Remove now-obsolete functionality such as the mutex's "no preempt"
flag (which was only for the prior FAT driver).
4) struct dirinfo uses time_t rather than raw FAT directory entry
time fields. I plan to follow up on genericizing everything there
(i.e. no FAT attributes).
5) unicode.c needed some redoing so that the file code does not try
try to load codepages during a scan, which is actually a problem with
the current code. The default codepage, if any is required, is now
kept in RAM separarately (bufalloced) from codepages specified to
iso_decode() (which must not be bufalloced because the conversion
may be done by playback threads).
Brings with it some additional reusable core code:
1) Revised file functions: Reusable code that does things such as
safe path concatenation and parsing without buffer limitations or
data duplication. Variants that copy or alter the input path may be
based off these.
To do:
1) Put dircache functionality back in the sim. Treating it internally
as a different kind of file system seems the best approach at this
time.
2) Restore use of dircache indexes in the playlist and database or
something effectively the same. Since the cache doesn't have to be
complete in order to be used, not getting a hit on the cache doesn't
unambiguously say if the path exists or not.
Change-Id: Ia30f3082a136253e3a0eae0784e3091d138915c8
Reviewed-on: http://gerrit.rockbox.org/566
Reviewed-by: Michael Sevakis <jethead71@rockbox.org>
Tested: Michael Sevakis <jethead71@rockbox.org>
2013-08-06 02:02:45 +00:00
|
|
|
dircache_free_buffer();
|
2017-01-17 19:45:07 +00:00
|
|
|
dircache_buffer_stolen = true;
|
|
|
|
|
|
|
|
allocate_tempbuf();
|
2006-04-16 17:32:54 +00:00
|
|
|
}
|
2017-01-17 19:45:07 +00:00
|
|
|
#endif /* HAVE_DIRCACHE */
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2006-05-28 09:59:24 +00:00
|
|
|
#ifdef HAVE_TC_RAMCACHE
|
2006-11-10 08:03:33 +00:00
|
|
|
if (tempbuf_size == 0 && tc_stat.ramcache_allocated > 0)
|
2006-05-28 09:59:24 +00:00
|
|
|
{
|
2017-01-18 09:39:35 +00:00
|
|
|
tcrc_buffer_lock();
|
|
|
|
tempbuf = (char *)(tcramcache.hdr + 1);
|
2006-11-10 08:03:33 +00:00
|
|
|
tempbuf_size = tc_stat.ramcache_allocated - sizeof(struct ramcache_header) - 128;
|
2006-05-28 09:59:24 +00:00
|
|
|
tempbuf_size &= ~0x03;
|
2011-08-30 14:01:45 +00:00
|
|
|
ramcache_buffer_stolen = true;
|
2006-05-28 09:59:24 +00:00
|
|
|
}
|
2017-01-18 09:39:35 +00:00
|
|
|
#endif /* HAVE_TC_RAMCACHE */
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2006-04-16 17:32:54 +00:00
|
|
|
/* And finally fail if there are no buffers available. */
|
2006-03-26 11:33:42 +00:00
|
|
|
if (tempbuf_size == 0)
|
|
|
|
{
|
|
|
|
logf("delaying commit until next boot");
|
2006-11-10 08:03:33 +00:00
|
|
|
tc_stat.commit_delayed = true;
|
2006-03-26 11:33:42 +00:00
|
|
|
close(tmpfd);
|
2017-01-17 19:45:07 +00:00
|
|
|
goto commit_error;
|
2006-03-26 11:33:42 +00:00
|
|
|
}
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2007-03-18 09:50:53 +00:00
|
|
|
logf("commit %ld entries...", tch.entry_count);
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2007-02-25 20:41:51 +00:00
|
|
|
/* Mark DB dirty so it will stay disabled if commit fails. */
|
|
|
|
current_tcmh.dirty = true;
|
|
|
|
update_master_header();
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2006-03-26 11:33:42 +00:00
|
|
|
/* Now create the index files. */
|
2006-11-10 08:03:33 +00:00
|
|
|
tc_stat.commit_step = 0;
|
2006-07-20 12:19:31 +00:00
|
|
|
tch.datasize = 0;
|
2006-11-10 08:03:33 +00:00
|
|
|
tc_stat.commit_delayed = false;
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2006-03-26 11:33:42 +00:00
|
|
|
for (i = 0; i < TAG_COUNT; i++)
|
|
|
|
{
|
2006-04-23 18:47:26 +00:00
|
|
|
int ret;
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2009-06-03 08:19:32 +00:00
|
|
|
if (TAGCACHE_IS_NUMERIC(i))
|
2006-04-23 18:47:26 +00:00
|
|
|
continue;
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2006-11-10 08:03:33 +00:00
|
|
|
tc_stat.commit_step++;
|
2006-07-20 12:19:31 +00:00
|
|
|
ret = build_index(i, &tch, tmpfd);
|
2006-04-23 18:47:26 +00:00
|
|
|
if (ret <= 0)
|
2006-03-26 11:33:42 +00:00
|
|
|
{
|
2006-05-28 09:59:24 +00:00
|
|
|
close(tmpfd);
|
2006-03-26 11:33:42 +00:00
|
|
|
logf("tagcache failed init");
|
2009-04-15 06:46:25 +00:00
|
|
|
if (ret == 0)
|
2006-11-10 08:03:33 +00:00
|
|
|
tc_stat.commit_delayed = true;
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2006-11-10 08:03:33 +00:00
|
|
|
tc_stat.commit_step = 0;
|
2017-01-17 19:45:07 +00:00
|
|
|
goto commit_error;
|
2006-03-26 11:33:42 +00:00
|
|
|
}
|
|
|
|
}
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2007-02-25 20:41:51 +00:00
|
|
|
if (!build_numeric_indices(&tch, tmpfd))
|
|
|
|
{
|
|
|
|
logf("Failure to commit numeric indices");
|
|
|
|
close(tmpfd);
|
|
|
|
tc_stat.commit_step = 0;
|
2017-01-17 19:45:07 +00:00
|
|
|
goto commit_error;
|
2007-02-25 20:41:51 +00:00
|
|
|
}
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2006-03-26 11:33:42 +00:00
|
|
|
close(tmpfd);
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2006-11-10 08:03:33 +00:00
|
|
|
tc_stat.commit_step = 0;
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2006-03-26 11:33:42 +00:00
|
|
|
/* Update the master index headers. */
|
2006-07-20 12:19:31 +00:00
|
|
|
if ( (masterfd = open_master_fd(&tcmh, true)) < 0)
|
2017-01-17 19:45:07 +00:00
|
|
|
goto commit_error;
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2011-04-15 21:45:45 +00:00
|
|
|
remove(TAGCACHE_FILE_TEMP);
|
|
|
|
|
2006-07-20 12:19:31 +00:00
|
|
|
tcmh.tch.entry_count += tch.entry_count;
|
2021-07-31 14:26:59 +00:00
|
|
|
tcmh.tch.datasize = sizeof(struct master_header)
|
2006-07-20 12:19:31 +00:00
|
|
|
+ sizeof(struct index_entry) * tcmh.tch.entry_count
|
|
|
|
+ tch.datasize;
|
2007-02-25 20:41:51 +00:00
|
|
|
tcmh.dirty = false;
|
|
|
|
tcmh.commitid++;
|
2006-03-26 11:33:42 +00:00
|
|
|
|
|
|
|
lseek(masterfd, 0, SEEK_SET);
|
2007-02-13 21:51:18 +00:00
|
|
|
ecwrite(masterfd, &tcmh, 1, master_header_ec, tc_stat.econ);
|
2006-03-26 11:33:42 +00:00
|
|
|
close(masterfd);
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2006-03-26 11:33:42 +00:00
|
|
|
logf("tagcache committed");
|
2007-02-25 20:41:51 +00:00
|
|
|
tc_stat.ready = check_all_headers();
|
2007-03-08 08:20:30 +00:00
|
|
|
tc_stat.readyvalid = true;
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2006-04-16 17:32:54 +00:00
|
|
|
#ifdef HAVE_TC_RAMCACHE
|
2011-08-30 14:01:45 +00:00
|
|
|
if (ramcache_buffer_stolen)
|
2017-01-17 19:45:07 +00:00
|
|
|
{
|
2017-01-18 09:39:35 +00:00
|
|
|
tempbuf = NULL;
|
|
|
|
tempbuf_size = 0;
|
2017-01-17 19:45:07 +00:00
|
|
|
ramcache_buffer_stolen = false;
|
2017-01-18 09:39:35 +00:00
|
|
|
tcrc_buffer_unlock();
|
2017-01-17 19:45:07 +00:00
|
|
|
}
|
|
|
|
|
2006-04-16 17:32:54 +00:00
|
|
|
/* Reload tagcache. */
|
2006-11-10 08:03:33 +00:00
|
|
|
if (tc_stat.ramcache_allocated > 0)
|
2006-04-16 17:32:54 +00:00
|
|
|
tagcache_start_scan();
|
2017-01-18 09:39:35 +00:00
|
|
|
#endif /* HAVE_TC_RAMCACHE */
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2017-01-17 19:45:07 +00:00
|
|
|
rc = true;
|
|
|
|
|
|
|
|
commit_error:
|
|
|
|
#ifdef HAVE_TC_RAMCACHE
|
|
|
|
if (ramcache_buffer_stolen)
|
2017-01-18 09:39:35 +00:00
|
|
|
{
|
|
|
|
tempbuf = NULL;
|
|
|
|
tempbuf_size = 0;
|
|
|
|
tcrc_buffer_unlock();
|
|
|
|
}
|
|
|
|
#endif /* HAVE_TC_RAMCACHE */
|
2017-01-17 19:45:07 +00:00
|
|
|
|
2006-08-02 17:39:34 +00:00
|
|
|
read_lock--;
|
2006-03-26 11:33:42 +00:00
|
|
|
|
2017-01-17 19:45:07 +00:00
|
|
|
#ifdef HAVE_DIRCACHE
|
|
|
|
/* Resume the dircache, if we stole the buffer. */
|
|
|
|
if (dircache_buffer_stolen)
|
2017-01-18 09:39:35 +00:00
|
|
|
{
|
|
|
|
free_tempbuf();
|
2017-01-17 19:45:07 +00:00
|
|
|
dircache_resume();
|
2017-01-18 09:39:35 +00:00
|
|
|
}
|
|
|
|
#endif /* HAVE_DIRCACHE */
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2017-01-17 19:45:07 +00:00
|
|
|
return rc;
|
|
|
|
}
|
2012-07-31 08:33:27 +00:00
|
|
|
|
2008-12-15 23:42:19 +00:00
|
|
|
#ifndef __PCTOOL__
|
|
|
|
|
2006-07-20 12:19:31 +00:00
|
|
|
static bool modify_numeric_entry(int masterfd, int idx_id, int tag, long data)
|
|
|
|
{
|
|
|
|
struct index_entry idx;
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2006-11-10 08:03:33 +00:00
|
|
|
if (!tc_stat.ready)
|
2006-07-23 11:15:28 +00:00
|
|
|
return false;
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2009-06-03 08:19:32 +00:00
|
|
|
if (!TAGCACHE_IS_NUMERIC(tag))
|
2006-07-20 12:19:31 +00:00
|
|
|
return false;
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2006-07-24 15:35:46 +00:00
|
|
|
if (!get_index(masterfd, idx_id, &idx, false))
|
2006-07-20 12:19:31 +00:00
|
|
|
return false;
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2006-07-20 12:19:31 +00:00
|
|
|
idx.tag_seek[tag] = data;
|
2006-07-23 15:35:53 +00:00
|
|
|
idx.flag |= FLAG_DIRTYNUM;
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2006-07-23 15:35:53 +00:00
|
|
|
return write_index(masterfd, idx_id, &idx);
|
2006-07-20 12:19:31 +00:00
|
|
|
}
|
|
|
|
|
2007-07-21 17:35:19 +00:00
|
|
|
#if 0
|
2021-07-31 14:26:59 +00:00
|
|
|
bool tagcache_modify_numeric_entry(struct tagcache_search *tcs,
|
2006-07-20 12:19:31 +00:00
|
|
|
int tag, long data)
|
|
|
|
{
|
|
|
|
struct master_header myhdr;
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2006-07-20 12:19:31 +00:00
|
|
|
if (tcs->masterfd < 0)
|
|
|
|
{
|
|
|
|
if ( (tcs->masterfd = open_master_fd(&myhdr, true)) < 0)
|
|
|
|
return false;
|
|
|
|
}
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2006-07-20 12:19:31 +00:00
|
|
|
return modify_numeric_entry(tcs->masterfd, tcs->idx_id, tag, data);
|
2006-07-15 17:36:25 +00:00
|
|
|
}
|
2007-07-21 17:35:19 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
static bool command_queue_is_full(void)
|
|
|
|
{
|
|
|
|
int next;
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2007-07-21 17:35:19 +00:00
|
|
|
next = command_queue_widx + 1;
|
|
|
|
if (next >= TAGCACHE_COMMAND_QUEUE_LENGTH)
|
|
|
|
next = 0;
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2007-07-21 17:35:19 +00:00
|
|
|
return (next == command_queue_ridx);
|
|
|
|
}
|
2008-12-15 23:42:19 +00:00
|
|
|
|
2014-03-14 22:15:16 +00:00
|
|
|
static void command_queue_sync_callback(void)
|
2007-07-21 17:35:19 +00:00
|
|
|
{
|
|
|
|
struct master_header myhdr;
|
|
|
|
int masterfd;
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2007-07-21 17:35:19 +00:00
|
|
|
mutex_lock(&command_queue_mutex);
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2007-07-21 17:35:19 +00:00
|
|
|
if ( (masterfd = open_master_fd(&myhdr, true)) < 0)
|
2009-10-20 21:54:44 +00:00
|
|
|
return;
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2007-07-21 17:35:19 +00:00
|
|
|
while (command_queue_ridx != command_queue_widx)
|
|
|
|
{
|
|
|
|
struct tagcache_command_entry *ce = &command_queue[command_queue_ridx];
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2007-07-21 17:35:19 +00:00
|
|
|
switch (ce->command)
|
|
|
|
{
|
|
|
|
case CMD_UPDATE_MASTER_HEADER:
|
|
|
|
{
|
|
|
|
close(masterfd);
|
|
|
|
update_master_header();
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2007-07-21 17:35:19 +00:00
|
|
|
/* Re-open the masterfd. */
|
|
|
|
if ( (masterfd = open_master_fd(&myhdr, true)) < 0)
|
2009-10-20 21:54:44 +00:00
|
|
|
return;
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2007-07-21 17:35:19 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case CMD_UPDATE_NUMERIC:
|
|
|
|
{
|
|
|
|
modify_numeric_entry(masterfd, ce->idx_id, ce->tag, ce->data);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2007-07-21 17:35:19 +00:00
|
|
|
if (++command_queue_ridx >= TAGCACHE_COMMAND_QUEUE_LENGTH)
|
|
|
|
command_queue_ridx = 0;
|
|
|
|
}
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2007-07-21 17:35:19 +00:00
|
|
|
close(masterfd);
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2008-02-17 18:35:27 +00:00
|
|
|
tc_stat.queue_length = 0;
|
2007-07-21 17:35:19 +00:00
|
|
|
mutex_unlock(&command_queue_mutex);
|
2008-02-17 18:35:27 +00:00
|
|
|
}
|
|
|
|
|
2008-04-19 23:22:40 +00:00
|
|
|
static void run_command_queue(bool force)
|
2008-02-17 18:35:27 +00:00
|
|
|
{
|
|
|
|
if (COMMAND_QUEUE_IS_EMPTY)
|
|
|
|
return;
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2008-02-17 18:35:27 +00:00
|
|
|
if (force || command_queue_is_full())
|
2014-03-14 22:15:16 +00:00
|
|
|
command_queue_sync_callback();
|
2008-02-17 18:35:27 +00:00
|
|
|
else
|
2008-11-01 16:14:28 +00:00
|
|
|
register_storage_idle_func(command_queue_sync_callback);
|
2007-07-21 17:35:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void queue_command(int cmd, long idx_id, int tag, long data)
|
|
|
|
{
|
|
|
|
while (1)
|
|
|
|
{
|
|
|
|
int next;
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2007-07-21 17:35:19 +00:00
|
|
|
mutex_lock(&command_queue_mutex);
|
|
|
|
next = command_queue_widx + 1;
|
|
|
|
if (next >= TAGCACHE_COMMAND_QUEUE_LENGTH)
|
|
|
|
next = 0;
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2007-07-21 17:35:19 +00:00
|
|
|
/* Make sure queue is not full. */
|
|
|
|
if (next != command_queue_ridx)
|
|
|
|
{
|
|
|
|
struct tagcache_command_entry *ce = &command_queue[command_queue_widx];
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2007-07-21 17:35:19 +00:00
|
|
|
ce->command = cmd;
|
|
|
|
ce->idx_id = idx_id;
|
|
|
|
ce->tag = tag;
|
|
|
|
ce->data = data;
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2007-07-21 17:35:19 +00:00
|
|
|
command_queue_widx = next;
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2008-02-17 18:35:27 +00:00
|
|
|
tc_stat.queue_length++;
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2007-07-21 17:35:19 +00:00
|
|
|
mutex_unlock(&command_queue_mutex);
|
|
|
|
break;
|
|
|
|
}
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2007-07-21 17:35:19 +00:00
|
|
|
/* Queue is full, try again later... */
|
|
|
|
mutex_unlock(&command_queue_mutex);
|
|
|
|
sleep(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
long tagcache_increase_serial(void)
|
|
|
|
{
|
|
|
|
long old;
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2007-07-21 17:35:19 +00:00
|
|
|
if (!tc_stat.ready)
|
|
|
|
return -2;
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2007-07-21 17:35:19 +00:00
|
|
|
while (read_lock)
|
|
|
|
sleep(1);
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2007-07-21 17:35:19 +00:00
|
|
|
old = current_tcmh.serial++;
|
|
|
|
queue_command(CMD_UPDATE_MASTER_HEADER, 0, 0, 0);
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2007-07-21 17:35:19 +00:00
|
|
|
return old;
|
|
|
|
}
|
|
|
|
|
|
|
|
void tagcache_update_numeric(int idx_id, int tag, long data)
|
|
|
|
{
|
|
|
|
queue_command(CMD_UPDATE_NUMERIC, idx_id, tag, data);
|
|
|
|
}
|
2008-12-15 23:42:19 +00:00
|
|
|
#endif /* !__PCTOOL__ */
|
2007-07-21 17:35:19 +00:00
|
|
|
|
2006-07-16 15:04:46 +00:00
|
|
|
static bool write_tag(int fd, const char *tagstr, const char *datastr)
|
|
|
|
{
|
2007-02-25 19:01:32 +00:00
|
|
|
char buf[512];
|
2021-07-30 06:13:57 +00:00
|
|
|
const int bufsz = sizeof(buf);
|
2006-07-16 15:04:46 +00:00
|
|
|
int i;
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2021-07-30 06:13:57 +00:00
|
|
|
snprintf(buf, bufsz, "%s=\"", tagstr);
|
|
|
|
|
2008-04-19 23:48:13 +00:00
|
|
|
for (i = strlen(buf); i < (long)sizeof(buf)-4; i++)
|
2006-07-16 15:04:46 +00:00
|
|
|
{
|
|
|
|
if (*datastr == '\0')
|
|
|
|
break;
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2008-04-19 23:48:13 +00:00
|
|
|
if (*datastr == '"' || *datastr == '\\')
|
|
|
|
buf[i++] = '\\';
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2011-06-20 15:53:33 +00:00
|
|
|
else if (*datastr == '\n')
|
|
|
|
{
|
|
|
|
buf[i++] = '\\';
|
|
|
|
buf[i] = 'n';
|
|
|
|
continue;
|
|
|
|
}
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2006-07-16 15:04:46 +00:00
|
|
|
buf[i] = *(datastr++);
|
|
|
|
}
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2021-08-01 02:24:33 +00:00
|
|
|
str_setlen(buf, bufsz - 1);
|
2021-07-30 06:13:57 +00:00
|
|
|
strlcpy(&buf[i], "\" ", (bufsz - i - 1));
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2006-07-16 15:04:46 +00:00
|
|
|
write(fd, buf, i + 2);
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2006-07-16 15:04:46 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2008-12-15 23:42:19 +00:00
|
|
|
#ifndef __PCTOOL__
|
|
|
|
|
2021-07-31 14:26:59 +00:00
|
|
|
static bool read_tag(char *dest, long size,
|
2006-07-20 12:19:31 +00:00
|
|
|
const char *src, const char *tagstr)
|
|
|
|
{
|
|
|
|
int pos;
|
|
|
|
char current_tag[32];
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2006-07-20 12:19:31 +00:00
|
|
|
while (*src != '\0')
|
|
|
|
{
|
|
|
|
/* Skip all whitespace */
|
|
|
|
while (*src == ' ')
|
|
|
|
src++;
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2006-07-20 12:19:31 +00:00
|
|
|
if (*src == '\0')
|
|
|
|
break;
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2006-07-20 12:19:31 +00:00
|
|
|
pos = 0;
|
|
|
|
/* Read in tag name */
|
|
|
|
while (*src != '=' && *src != ' ')
|
|
|
|
{
|
|
|
|
current_tag[pos] = *src;
|
|
|
|
src++;
|
|
|
|
pos++;
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2021-08-01 02:24:33 +00:00
|
|
|
if (*src == '\0' || pos >= (int) sizeof(current_tag))
|
2006-07-20 12:19:31 +00:00
|
|
|
return false;
|
|
|
|
}
|
2021-08-01 02:24:33 +00:00
|
|
|
|
|
|
|
str_setlen(current_tag, pos);
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2006-07-20 12:19:31 +00:00
|
|
|
/* Read in tag data */
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2006-07-20 12:19:31 +00:00
|
|
|
/* Find the start. */
|
|
|
|
while (*src != '"' && *src != '\0')
|
|
|
|
src++;
|
|
|
|
|
|
|
|
if (*src == '\0' || *(++src) == '\0')
|
|
|
|
return false;
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2006-07-20 12:19:31 +00:00
|
|
|
/* Read the data. */
|
|
|
|
for (pos = 0; pos < size; pos++)
|
|
|
|
{
|
|
|
|
if (*src == '\0')
|
|
|
|
break;
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2008-04-19 23:48:13 +00:00
|
|
|
if (*src == '\\')
|
2006-07-20 12:19:31 +00:00
|
|
|
{
|
2011-06-20 15:53:33 +00:00
|
|
|
src++;
|
|
|
|
if (*src == 'n')
|
|
|
|
dest[pos] = '\n';
|
|
|
|
else
|
|
|
|
dest[pos] = *src;
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2011-06-20 15:53:33 +00:00
|
|
|
src++;
|
2006-07-20 12:19:31 +00:00
|
|
|
continue;
|
|
|
|
}
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2011-06-20 15:53:33 +00:00
|
|
|
if (*src == '\0')
|
|
|
|
break;
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2006-07-20 12:19:31 +00:00
|
|
|
if (*src == '"')
|
|
|
|
{
|
|
|
|
src++;
|
|
|
|
break;
|
|
|
|
}
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2011-06-20 15:53:33 +00:00
|
|
|
dest[pos] = *(src++);
|
2006-07-20 12:19:31 +00:00
|
|
|
}
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2021-08-01 02:24:33 +00:00
|
|
|
str_setlen(dest, pos);
|
2006-07-20 12:19:31 +00:00
|
|
|
|
|
|
|
if (!strcasecmp(tagstr, current_tag))
|
|
|
|
return true;
|
|
|
|
}
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2006-07-20 12:19:31 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-06-23 20:22:00 +00:00
|
|
|
static int parse_changelog_line(int line_n, char *buf, void *parameters)
|
2006-07-20 12:19:31 +00:00
|
|
|
{
|
2006-07-23 15:35:53 +00:00
|
|
|
struct index_entry idx;
|
2021-08-01 02:24:33 +00:00
|
|
|
char tag_data[TAGCACHE_BUFSZ];
|
2006-07-20 12:19:31 +00:00
|
|
|
int idx_id;
|
2017-01-15 23:10:38 +00:00
|
|
|
long masterfd = (long)(intptr_t)parameters;
|
2011-02-28 23:47:11 +00:00
|
|
|
const int import_tags[] = { tag_playcount, tag_rating, tag_playtime,
|
2013-07-14 11:59:39 +00:00
|
|
|
tag_lastplayed, tag_commitid, tag_lastelapsed,
|
|
|
|
tag_lastoffset };
|
2006-07-20 12:19:31 +00:00
|
|
|
int i;
|
2006-10-21 20:37:33 +00:00
|
|
|
(void)line_n;
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2006-07-20 12:19:31 +00:00
|
|
|
if (*buf == '#')
|
2006-10-21 20:37:33 +00:00
|
|
|
return 0;
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2011-06-20 15:53:33 +00:00
|
|
|
/* logf("%d/%s", line_n, buf); */
|
2006-07-20 12:19:31 +00:00
|
|
|
if (!read_tag(tag_data, sizeof tag_data, buf, "filename"))
|
|
|
|
{
|
2011-06-20 15:53:33 +00:00
|
|
|
logf("%d/filename missing", line_n);
|
2006-07-20 12:19:31 +00:00
|
|
|
logf("-> %s", buf);
|
2006-10-24 17:39:06 +00:00
|
|
|
return 0;
|
2006-07-20 12:19:31 +00:00
|
|
|
}
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2006-07-20 12:19:31 +00:00
|
|
|
idx_id = find_index(tag_data);
|
|
|
|
if (idx_id < 0)
|
|
|
|
{
|
2011-06-20 15:53:33 +00:00
|
|
|
logf("%d/entry not found", line_n);
|
2006-10-24 17:39:06 +00:00
|
|
|
return 0;
|
2006-07-20 12:19:31 +00:00
|
|
|
}
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2006-07-24 15:35:46 +00:00
|
|
|
if (!get_index(masterfd, idx_id, &idx, false))
|
2006-07-23 15:35:53 +00:00
|
|
|
{
|
2011-06-20 15:53:33 +00:00
|
|
|
logf("%d/failed to retrieve index entry", line_n);
|
2006-10-24 17:39:06 +00:00
|
|
|
return 0;
|
2006-07-23 15:35:53 +00:00
|
|
|
}
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2006-07-23 15:35:53 +00:00
|
|
|
/* Stop if tag has already been modified. */
|
|
|
|
if (idx.flag & FLAG_DIRTYNUM)
|
2006-10-24 17:39:06 +00:00
|
|
|
return 0;
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2011-06-20 15:53:33 +00:00
|
|
|
logf("%d/import: %s", line_n, tag_data);
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2006-07-23 15:35:53 +00:00
|
|
|
idx.flag |= FLAG_DIRTYNUM;
|
2006-07-20 12:19:31 +00:00
|
|
|
for (i = 0; i < (long)(sizeof(import_tags)/sizeof(import_tags[0])); i++)
|
|
|
|
{
|
|
|
|
int data;
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2006-07-20 12:19:31 +00:00
|
|
|
if (!read_tag(tag_data, sizeof tag_data, buf,
|
|
|
|
tagcache_tag_to_str(import_tags[i])))
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2006-07-20 12:19:31 +00:00
|
|
|
data = atoi(tag_data);
|
|
|
|
if (data < 0)
|
|
|
|
continue;
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2006-07-23 15:35:53 +00:00
|
|
|
idx.tag_seek[import_tags[i]] = data;
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2008-08-29 21:14:58 +00:00
|
|
|
if (import_tags[i] == tag_lastplayed && data >= current_tcmh.serial)
|
|
|
|
current_tcmh.serial = data + 1;
|
2007-02-25 20:41:51 +00:00
|
|
|
else if (import_tags[i] == tag_commitid && data >= current_tcmh.commitid)
|
|
|
|
current_tcmh.commitid = data + 1;
|
2006-07-20 12:19:31 +00:00
|
|
|
}
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2006-10-21 20:37:33 +00:00
|
|
|
return write_index(masterfd, idx_id, &idx) ? 0 : -5;
|
2006-07-20 12:19:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool tagcache_import_changelog(void)
|
|
|
|
{
|
|
|
|
struct master_header myhdr;
|
2006-08-26 09:24:20 +00:00
|
|
|
struct tagcache_header tch;
|
2006-10-21 21:05:23 +00:00
|
|
|
int clfd;
|
|
|
|
long masterfd;
|
2010-12-06 22:26:31 +00:00
|
|
|
char buf[2048];
|
2021-07-30 06:13:57 +00:00
|
|
|
const int bufsz = sizeof(buf);
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2006-11-10 08:03:33 +00:00
|
|
|
if (!tc_stat.ready)
|
2006-07-23 11:15:28 +00:00
|
|
|
return false;
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2006-08-02 17:39:34 +00:00
|
|
|
while (read_lock)
|
|
|
|
sleep(1);
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2010-12-06 22:26:31 +00:00
|
|
|
clfd = open(TAGCACHE_FILE_CHANGELOG, O_RDONLY);
|
2006-07-20 12:19:31 +00:00
|
|
|
if (clfd < 0)
|
|
|
|
{
|
|
|
|
logf("failure to open changelog");
|
|
|
|
return false;
|
|
|
|
}
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2006-07-20 12:19:31 +00:00
|
|
|
if ( (masterfd = open_master_fd(&myhdr, true)) < 0)
|
|
|
|
{
|
|
|
|
close(clfd);
|
|
|
|
return false;
|
|
|
|
}
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2006-08-02 17:39:34 +00:00
|
|
|
write_lock++;
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2006-08-26 09:24:20 +00:00
|
|
|
filenametag_fd = open_tag_fd(&tch, tag_filename, false);
|
2021-07-30 06:13:57 +00:00
|
|
|
|
|
|
|
fast_readline(clfd, buf, bufsz, (void *)(intptr_t)masterfd,
|
2006-10-21 20:37:33 +00:00
|
|
|
parse_changelog_line);
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2006-07-20 12:19:31 +00:00
|
|
|
close(clfd);
|
|
|
|
close(masterfd);
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2006-08-26 09:24:20 +00:00
|
|
|
if (filenametag_fd >= 0)
|
2009-06-20 16:17:54 +00:00
|
|
|
{
|
2006-08-26 09:24:20 +00:00
|
|
|
close(filenametag_fd);
|
2009-06-20 16:17:54 +00:00
|
|
|
filenametag_fd = -1;
|
|
|
|
}
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2006-08-02 17:39:34 +00:00
|
|
|
write_lock--;
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2007-02-25 20:41:51 +00:00
|
|
|
update_master_header();
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2006-07-20 12:19:31 +00:00
|
|
|
return true;
|
|
|
|
}
|
2008-12-15 23:42:19 +00:00
|
|
|
|
|
|
|
#endif /* !__PCTOOL__ */
|
2006-07-20 12:19:31 +00:00
|
|
|
|
2006-07-16 15:04:46 +00:00
|
|
|
bool tagcache_create_changelog(struct tagcache_search *tcs)
|
|
|
|
{
|
2006-07-20 12:19:31 +00:00
|
|
|
struct master_header myhdr;
|
2006-07-16 15:04:46 +00:00
|
|
|
struct index_entry idx;
|
2021-08-01 02:24:33 +00:00
|
|
|
char buf[TAGCACHE_BUFSZ];
|
2021-07-30 06:13:57 +00:00
|
|
|
const int bufsz = sizeof(buf);
|
2006-07-16 15:04:46 +00:00
|
|
|
char temp[32];
|
|
|
|
int clfd;
|
|
|
|
int i, j;
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2006-11-10 08:03:33 +00:00
|
|
|
if (!tc_stat.ready)
|
2006-07-23 11:15:28 +00:00
|
|
|
return false;
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2006-07-16 15:04:46 +00:00
|
|
|
if (!tagcache_search(tcs, tag_filename))
|
|
|
|
return false;
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2006-07-16 15:04:46 +00:00
|
|
|
/* Initialize the changelog */
|
2010-12-06 22:26:31 +00:00
|
|
|
clfd = open(TAGCACHE_FILE_CHANGELOG, O_WRONLY | O_CREAT | O_TRUNC, 0666);
|
2006-07-16 15:04:46 +00:00
|
|
|
if (clfd < 0)
|
|
|
|
{
|
|
|
|
logf("failure to open changelog");
|
|
|
|
return false;
|
|
|
|
}
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2006-07-16 15:04:46 +00:00
|
|
|
if (tcs->masterfd < 0)
|
|
|
|
{
|
2006-07-20 12:19:31 +00:00
|
|
|
if ( (tcs->masterfd = open_master_fd(&myhdr, false)) < 0)
|
2011-08-25 19:42:00 +00:00
|
|
|
{
|
|
|
|
close(clfd);
|
2006-07-16 15:04:46 +00:00
|
|
|
return false;
|
2011-08-25 19:42:00 +00:00
|
|
|
}
|
2006-07-16 15:04:46 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
lseek(tcs->masterfd, 0, SEEK_SET);
|
2007-02-13 21:51:18 +00:00
|
|
|
ecread(tcs->masterfd, &myhdr, 1, master_header_ec, tc_stat.econ);
|
2006-07-16 15:04:46 +00:00
|
|
|
}
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2006-07-16 15:04:46 +00:00
|
|
|
write(clfd, "## Changelog version 1\n", 23);
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2006-07-20 12:19:31 +00:00
|
|
|
for (i = 0; i < myhdr.tch.entry_count; i++)
|
2006-07-16 15:04:46 +00:00
|
|
|
{
|
2010-03-16 12:15:56 +00:00
|
|
|
if (ecread_index_entry(tcs->masterfd, &idx) != sizeof(struct index_entry))
|
2006-07-16 15:04:46 +00:00
|
|
|
{
|
2006-09-26 11:23:18 +00:00
|
|
|
logf("read error #9");
|
2006-07-16 15:04:46 +00:00
|
|
|
tagcache_search_finish(tcs);
|
|
|
|
close(clfd);
|
|
|
|
return false;
|
|
|
|
}
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2006-07-16 15:04:46 +00:00
|
|
|
/* Skip until the entry found has been modified. */
|
|
|
|
if (! (idx.flag & FLAG_DIRTYNUM) )
|
|
|
|
continue;
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2007-02-10 12:09:28 +00:00
|
|
|
/* Skip deleted entries too. */
|
|
|
|
if (idx.flag & FLAG_DELETED)
|
|
|
|
continue;
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2006-07-16 15:04:46 +00:00
|
|
|
/* Now retrieve all tags. */
|
2006-07-20 12:19:31 +00:00
|
|
|
for (j = 0; j < TAG_COUNT; j++)
|
2006-07-16 15:04:46 +00:00
|
|
|
{
|
2009-06-03 08:19:32 +00:00
|
|
|
if (TAGCACHE_IS_NUMERIC(j))
|
2006-07-16 15:04:46 +00:00
|
|
|
{
|
2010-05-06 21:04:40 +00:00
|
|
|
snprintf(temp, sizeof temp, "%d", (int)idx.tag_seek[j]);
|
2006-07-20 12:19:31 +00:00
|
|
|
write_tag(clfd, tagcache_tag_to_str(j), temp);
|
2006-07-16 15:04:46 +00:00
|
|
|
continue;
|
|
|
|
}
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2006-07-20 12:19:31 +00:00
|
|
|
tcs->type = j;
|
2021-07-30 06:13:57 +00:00
|
|
|
tagcache_retrieve(tcs, i, tcs->type, buf, bufsz);
|
2006-07-20 12:19:31 +00:00
|
|
|
write_tag(clfd, tagcache_tag_to_str(j), buf);
|
2006-07-16 15:04:46 +00:00
|
|
|
}
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2006-07-16 15:04:46 +00:00
|
|
|
write(clfd, "\n", 1);
|
2007-03-10 14:34:56 +00:00
|
|
|
do_timed_yield();
|
2006-07-16 15:04:46 +00:00
|
|
|
}
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2006-07-16 15:04:46 +00:00
|
|
|
close(clfd);
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2006-07-16 15:04:46 +00:00
|
|
|
tagcache_search_finish(tcs);
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2006-07-16 15:04:46 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2006-07-15 17:36:25 +00:00
|
|
|
static bool delete_entry(long idx_id)
|
|
|
|
{
|
2007-12-16 21:10:26 +00:00
|
|
|
int fd = -1;
|
2007-12-18 21:50:29 +00:00
|
|
|
int masterfd = -1;
|
2006-07-15 17:36:25 +00:00
|
|
|
int tag, i;
|
|
|
|
struct index_entry idx, myidx;
|
2006-07-20 12:19:31 +00:00
|
|
|
struct master_header myhdr;
|
2021-08-01 02:24:33 +00:00
|
|
|
char buf[TAGCACHE_BUFSZ];
|
2021-07-30 06:13:57 +00:00
|
|
|
const int bufsz = sizeof(buf);
|
2006-07-15 17:36:25 +00:00
|
|
|
int in_use[TAG_COUNT];
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2007-03-18 09:50:53 +00:00
|
|
|
logf("delete_entry(): %ld", idx_id);
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2006-07-15 17:36:25 +00:00
|
|
|
#ifdef HAVE_TC_RAMCACHE
|
|
|
|
/* At first mark the entry removed from ram cache. */
|
2007-04-29 14:40:59 +00:00
|
|
|
if (tc_stat.ramcache)
|
2017-01-18 09:39:35 +00:00
|
|
|
tcramcache.hdr->indices[idx_id].flag |= FLAG_DELETED;
|
2006-07-15 17:36:25 +00:00
|
|
|
#endif
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2007-12-18 21:50:29 +00:00
|
|
|
if ( (masterfd = open_master_fd(&myhdr, true) ) < 0)
|
2006-07-15 17:36:25 +00:00
|
|
|
return false;
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2007-12-18 21:50:29 +00:00
|
|
|
lseek(masterfd, idx_id * sizeof(struct index_entry), SEEK_CUR);
|
2010-03-16 12:15:56 +00:00
|
|
|
if (ecread_index_entry(masterfd, &myidx) != sizeof(struct index_entry))
|
2006-07-10 16:22:03 +00:00
|
|
|
{
|
2006-09-26 11:23:18 +00:00
|
|
|
logf("delete_entry(): read error");
|
2007-12-16 21:10:26 +00:00
|
|
|
goto cleanup;
|
2006-07-10 16:22:03 +00:00
|
|
|
}
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2007-12-18 21:50:29 +00:00
|
|
|
if (myidx.flag & FLAG_DELETED)
|
2006-07-10 16:22:03 +00:00
|
|
|
{
|
2007-12-18 21:50:29 +00:00
|
|
|
logf("delete_entry(): already deleted!");
|
2007-12-16 21:10:26 +00:00
|
|
|
goto cleanup;
|
2006-07-10 16:22:03 +00:00
|
|
|
}
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2007-12-18 21:50:29 +00:00
|
|
|
myidx.flag |= FLAG_DELETED;
|
2017-01-15 23:10:38 +00:00
|
|
|
lseek(masterfd, -(off_t)sizeof(struct index_entry), SEEK_CUR);
|
2010-03-16 12:15:56 +00:00
|
|
|
if (ecwrite_index_entry(masterfd, &myidx) != sizeof(struct index_entry))
|
2008-01-07 15:10:49 +00:00
|
|
|
{
|
|
|
|
logf("delete_entry(): write_error #1");
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
2006-07-10 16:22:03 +00:00
|
|
|
/* Now check which tags are no longer in use (if any) */
|
|
|
|
for (tag = 0; tag < TAG_COUNT; tag++)
|
|
|
|
in_use[tag] = 0;
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2007-12-18 21:50:29 +00:00
|
|
|
lseek(masterfd, sizeof(struct master_header), SEEK_SET);
|
2006-07-20 12:19:31 +00:00
|
|
|
for (i = 0; i < myhdr.tch.entry_count; i++)
|
2006-07-10 16:22:03 +00:00
|
|
|
{
|
2007-04-29 14:40:59 +00:00
|
|
|
struct index_entry *idxp;
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2007-04-29 14:40:59 +00:00
|
|
|
#ifdef HAVE_TC_RAMCACHE
|
|
|
|
/* Use RAM DB if available for greater speed */
|
|
|
|
if (tc_stat.ramcache)
|
2017-01-18 09:39:35 +00:00
|
|
|
idxp = &tcramcache.hdr->indices[i];
|
2007-04-29 14:40:59 +00:00
|
|
|
else
|
|
|
|
#endif
|
2006-07-10 16:22:03 +00:00
|
|
|
{
|
2010-03-16 12:15:56 +00:00
|
|
|
if (ecread_index_entry(masterfd, &idx) != sizeof(struct index_entry))
|
2007-04-29 14:40:59 +00:00
|
|
|
{
|
|
|
|
logf("delete_entry(): read error #2");
|
2007-12-16 21:10:26 +00:00
|
|
|
goto cleanup;
|
2007-04-29 14:40:59 +00:00
|
|
|
}
|
|
|
|
idxp = &idx;
|
2006-07-10 16:22:03 +00:00
|
|
|
}
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2007-04-29 14:40:59 +00:00
|
|
|
if (idxp->flag & FLAG_DELETED)
|
2006-07-10 16:22:03 +00:00
|
|
|
continue;
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2006-07-10 16:22:03 +00:00
|
|
|
for (tag = 0; tag < TAG_COUNT; tag++)
|
|
|
|
{
|
2009-06-03 08:19:32 +00:00
|
|
|
if (TAGCACHE_IS_NUMERIC(tag))
|
2006-07-10 16:22:03 +00:00
|
|
|
continue;
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2007-04-29 14:40:59 +00:00
|
|
|
if (idxp->tag_seek[tag] == myidx.tag_seek[tag])
|
2006-07-10 16:22:03 +00:00
|
|
|
in_use[tag]++;
|
|
|
|
}
|
|
|
|
}
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2006-07-10 16:22:03 +00:00
|
|
|
/* Now delete all tags no longer in use. */
|
|
|
|
for (tag = 0; tag < TAG_COUNT; tag++)
|
|
|
|
{
|
2007-12-18 21:50:29 +00:00
|
|
|
struct tagcache_header tch;
|
|
|
|
int oldseek = myidx.tag_seek[tag];
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2009-06-03 08:19:32 +00:00
|
|
|
if (TAGCACHE_IS_NUMERIC(tag))
|
2006-07-10 16:22:03 +00:00
|
|
|
continue;
|
2021-07-31 14:26:59 +00:00
|
|
|
|
|
|
|
/**
|
2007-12-18 21:50:29 +00:00
|
|
|
* Replace tag seek with a hash value of the field string data.
|
|
|
|
* That way runtime statistics of moved or altered files can be
|
|
|
|
* resurrected.
|
|
|
|
*/
|
|
|
|
#ifdef HAVE_TC_RAMCACHE
|
|
|
|
if (tc_stat.ramcache && tag != tag_filename)
|
|
|
|
{
|
|
|
|
struct tagfile_entry *tfe;
|
2017-01-18 09:39:35 +00:00
|
|
|
int32_t *seek = &tcramcache.hdr->indices[idx_id].tag_seek[tag];
|
2011-08-30 14:01:45 +00:00
|
|
|
|
2017-01-18 09:39:35 +00:00
|
|
|
tfe = (struct tagfile_entry *)&tcramcache.hdr->tags[tag][*seek];
|
|
|
|
tcrc_buffer_lock(); /* protect tfe and seek if crc_32() yield()s */
|
2007-12-18 21:50:29 +00:00
|
|
|
*seek = crc_32(tfe->tag_data, strlen(tfe->tag_data), 0xffffffff);
|
2017-01-18 09:39:35 +00:00
|
|
|
tcrc_buffer_unlock();
|
2007-12-18 21:50:29 +00:00
|
|
|
myidx.tag_seek[tag] = *seek;
|
|
|
|
}
|
|
|
|
else
|
2017-01-18 09:39:35 +00:00
|
|
|
#endif /* HAVE_TC_RAMCACHE */
|
2007-12-18 21:50:29 +00:00
|
|
|
{
|
|
|
|
struct tagfile_entry tfe;
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2007-12-18 21:50:29 +00:00
|
|
|
/* Open the index file, which contains the tag names. */
|
|
|
|
if ((fd = open_tag_fd(&tch, tag, true)) < 0)
|
|
|
|
goto cleanup;
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2007-12-18 21:50:29 +00:00
|
|
|
/* Skip the header block */
|
|
|
|
lseek(fd, myidx.tag_seek[tag], SEEK_SET);
|
2021-07-30 06:13:57 +00:00
|
|
|
|
2021-08-01 02:24:33 +00:00
|
|
|
switch (ecread_tagfile_entry_and_tag(fd, &tfe, buf, bufsz))
|
2007-12-18 21:50:29 +00:00
|
|
|
{
|
2021-08-01 02:24:33 +00:00
|
|
|
case e_SUCCESS:
|
|
|
|
break;
|
|
|
|
case e_ENTRY_SIZEMISMATCH:
|
|
|
|
logf("delete_entry(): read error #3");
|
|
|
|
goto cleanup;
|
|
|
|
case e_TAG_TOOLONG:
|
|
|
|
logf("too long tag #4");
|
|
|
|
goto cleanup;
|
|
|
|
case e_TAG_SIZEMISMATCH:
|
|
|
|
logf("delete_entry(): read error #3");
|
|
|
|
goto cleanup;
|
|
|
|
default:
|
|
|
|
logf("unknown_error");
|
|
|
|
break;;
|
2007-12-18 21:50:29 +00:00
|
|
|
}
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2007-12-18 21:50:29 +00:00
|
|
|
myidx.tag_seek[tag] = crc_32(buf, strlen(buf), 0xffffffff);
|
|
|
|
}
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2006-07-10 16:22:03 +00:00
|
|
|
if (in_use[tag])
|
|
|
|
{
|
|
|
|
logf("in use: %d/%d", tag, in_use[tag]);
|
2007-12-18 21:50:29 +00:00
|
|
|
if (fd >= 0)
|
|
|
|
{
|
|
|
|
close(fd);
|
|
|
|
fd = -1;
|
|
|
|
}
|
2006-07-10 16:22:03 +00:00
|
|
|
continue;
|
|
|
|
}
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2007-04-29 14:40:59 +00:00
|
|
|
#ifdef HAVE_TC_RAMCACHE
|
|
|
|
/* Delete from ram. */
|
|
|
|
if (tc_stat.ramcache && tag != tag_filename)
|
|
|
|
{
|
2011-08-02 14:35:10 +00:00
|
|
|
struct tagfile_entry *tagentry =
|
2017-01-18 09:39:35 +00:00
|
|
|
(struct tagfile_entry *)&tcramcache.hdr->tags[tag][oldseek];
|
2021-08-01 02:24:33 +00:00
|
|
|
str_setlen(tagentry->tag_data, 0);
|
2007-04-29 14:40:59 +00:00
|
|
|
}
|
2017-01-18 09:39:35 +00:00
|
|
|
#endif /* HAVE_TC_RAMCACHE */
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2006-07-10 16:22:03 +00:00
|
|
|
/* Open the index file, which contains the tag names. */
|
|
|
|
if (fd < 0)
|
|
|
|
{
|
2007-12-18 21:50:29 +00:00
|
|
|
if ((fd = open_tag_fd(&tch, tag, true)) < 0)
|
|
|
|
goto cleanup;
|
2006-07-10 16:22:03 +00:00
|
|
|
}
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2006-07-10 16:22:03 +00:00
|
|
|
/* Skip the header block */
|
2007-12-18 21:50:29 +00:00
|
|
|
lseek(fd, oldseek + sizeof(struct tagfile_entry), SEEK_SET);
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2006-10-15 11:01:18 +00:00
|
|
|
/* Debug, print 10 first characters of the tag
|
2006-07-10 16:22:03 +00:00
|
|
|
read(fd, buf, 10);
|
|
|
|
buf[10]='\0';
|
|
|
|
logf("TAG:%s", buf);
|
|
|
|
lseek(fd, -10, SEEK_CUR);
|
2006-10-15 11:01:18 +00:00
|
|
|
*/
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2006-07-10 16:22:03 +00:00
|
|
|
/* Write first data byte in tag as \0 */
|
|
|
|
write(fd, "", 1);
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2006-07-10 16:22:03 +00:00
|
|
|
/* Now tag data has been removed */
|
|
|
|
close(fd);
|
2007-12-18 21:50:29 +00:00
|
|
|
fd = -1;
|
2006-07-10 16:22:03 +00:00
|
|
|
}
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2007-12-18 21:50:29 +00:00
|
|
|
/* Write index entry back into master index. */
|
|
|
|
lseek(masterfd, sizeof(struct master_header) +
|
|
|
|
(idx_id * sizeof(struct index_entry)), SEEK_SET);
|
2010-03-16 12:15:56 +00:00
|
|
|
if (ecwrite_index_entry(masterfd, &myidx) != sizeof(struct index_entry))
|
2007-12-18 21:50:29 +00:00
|
|
|
{
|
2008-01-07 15:10:49 +00:00
|
|
|
logf("delete_entry(): write_error #2");
|
2007-12-18 21:50:29 +00:00
|
|
|
goto cleanup;
|
|
|
|
}
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2007-12-18 21:50:29 +00:00
|
|
|
close(masterfd);
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2006-07-10 16:22:03 +00:00
|
|
|
return true;
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2007-12-16 21:10:26 +00:00
|
|
|
cleanup:
|
|
|
|
if (fd >= 0)
|
|
|
|
close(fd);
|
2007-12-18 21:50:29 +00:00
|
|
|
if (masterfd >= 0)
|
|
|
|
close(masterfd);
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2007-12-16 21:10:26 +00:00
|
|
|
return false;
|
2006-07-10 16:22:03 +00:00
|
|
|
}
|
|
|
|
|
2006-09-23 11:02:09 +00:00
|
|
|
/**
|
|
|
|
* Returns true if there is an event waiting in the queue
|
|
|
|
* that requires the current operation to be aborted.
|
|
|
|
*/
|
|
|
|
static bool check_event_queue(void)
|
|
|
|
{
|
2017-01-18 09:39:35 +00:00
|
|
|
#ifndef __PCTOOL__
|
2007-10-16 01:25:17 +00:00
|
|
|
struct queue_event ev;
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2010-05-21 15:12:07 +00:00
|
|
|
if(!queue_peek(&tagcache_queue, &ev))
|
|
|
|
return false;
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2006-09-23 11:02:09 +00:00
|
|
|
switch (ev.id)
|
|
|
|
{
|
|
|
|
case Q_STOP_SCAN:
|
|
|
|
case SYS_POWEROFF:
|
|
|
|
case SYS_USB_CONNECTED:
|
|
|
|
return true;
|
|
|
|
}
|
2017-01-18 09:39:35 +00:00
|
|
|
#endif /* __PCTOOL__ */
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2006-09-23 11:02:09 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2006-03-26 11:33:42 +00:00
|
|
|
#ifdef HAVE_TC_RAMCACHE
|
2011-08-30 14:01:45 +00:00
|
|
|
|
|
|
|
static void fix_ramcache(void* old_addr, void* new_addr)
|
|
|
|
{
|
|
|
|
ptrdiff_t offpos = new_addr - old_addr;
|
|
|
|
for (int i = 0; i < TAG_COUNT; i++)
|
2017-01-18 09:39:35 +00:00
|
|
|
tcramcache.hdr->tags[i] += offpos;
|
2011-08-30 14:01:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int move_cb(int handle, void* current, void* new)
|
|
|
|
{
|
2022-02-27 16:01:24 +00:00
|
|
|
(void)handle;
|
2017-01-18 09:39:35 +00:00
|
|
|
if (tcramcache.move_lock > 0)
|
2011-08-30 14:01:45 +00:00
|
|
|
return BUFLIB_CB_CANNOT_MOVE;
|
|
|
|
|
|
|
|
fix_ramcache(current, new);
|
2017-01-18 09:39:35 +00:00
|
|
|
tcramcache.hdr = new;
|
2011-08-30 14:01:45 +00:00
|
|
|
return BUFLIB_CB_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct buflib_callbacks ops = {
|
|
|
|
.move_callback = move_cb,
|
|
|
|
.shrink_callback = NULL,
|
|
|
|
};
|
|
|
|
|
2006-03-26 11:33:42 +00:00
|
|
|
static bool allocate_tagcache(void)
|
|
|
|
{
|
2017-04-01 14:19:52 +00:00
|
|
|
tc_stat.ramcache_allocated = 0;
|
|
|
|
tcramcache.handle = 0;
|
|
|
|
tcramcache.hdr = NULL;
|
|
|
|
|
2006-03-26 11:33:42 +00:00
|
|
|
/* Load the header. */
|
2017-01-18 09:39:35 +00:00
|
|
|
struct master_header tcmh;
|
|
|
|
int fd = open_master_fd(&tcmh, false);
|
|
|
|
if (fd < 0)
|
2006-03-26 11:33:42 +00:00
|
|
|
return false;
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2006-07-23 11:15:28 +00:00
|
|
|
close(fd);
|
2021-07-31 14:26:59 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Now calculate the required cache size plus
|
|
|
|
* some extra space for alignment fixes.
|
2006-03-27 07:02:14 +00:00
|
|
|
*/
|
2017-01-18 09:39:35 +00:00
|
|
|
size_t alloc_size = tcmh.tch.datasize + 256 + TAGCACHE_RESERVE +
|
2006-03-26 11:33:42 +00:00
|
|
|
sizeof(struct ramcache_header) + TAG_COUNT*sizeof(void *);
|
2017-01-18 09:39:35 +00:00
|
|
|
#ifdef HAVE_DIRCACHE
|
|
|
|
alloc_size += tcmh.tch.entry_count*sizeof(struct dircache_fileref);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
int handle = core_alloc_ex("tc ramcache", alloc_size, &ops);
|
|
|
|
if (handle <= 0)
|
|
|
|
return false;
|
|
|
|
|
2017-04-01 14:19:52 +00:00
|
|
|
tcramcache.handle = handle;
|
|
|
|
tcramcache.hdr = core_get_data(handle);
|
2017-01-18 09:39:35 +00:00
|
|
|
tc_stat.ramcache_allocated = alloc_size;
|
|
|
|
|
|
|
|
memset(tcramcache.hdr, 0, sizeof(struct ramcache_header));
|
2011-06-19 14:36:26 +00:00
|
|
|
memcpy(¤t_tcmh, &tcmh, sizeof current_tcmh);
|
2017-04-01 14:19:52 +00:00
|
|
|
logf("tagcache: %d bytes allocated.", tc_stat.ramcache_allocated);
|
2006-03-26 11:33:42 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-01-18 09:39:35 +00:00
|
|
|
#ifdef HAVE_EEPROM_SETTINGS
|
2006-08-05 20:19:10 +00:00
|
|
|
static bool tagcache_dumpload(void)
|
|
|
|
{
|
|
|
|
struct statefile_header shdr;
|
2011-08-30 14:32:16 +00:00
|
|
|
int fd, rc, handle;
|
2017-01-18 09:39:35 +00:00
|
|
|
|
2017-04-01 14:19:52 +00:00
|
|
|
tcramcache.handle = 0;
|
2017-01-18 09:39:35 +00:00
|
|
|
tcramcache.hdr = NULL;
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2010-12-06 22:26:31 +00:00
|
|
|
fd = open(TAGCACHE_STATEFILE, O_RDONLY);
|
2006-08-05 20:19:10 +00:00
|
|
|
if (fd < 0)
|
|
|
|
{
|
|
|
|
logf("no tagcache statedump");
|
|
|
|
return false;
|
|
|
|
}
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2006-08-05 20:19:10 +00:00
|
|
|
/* Check the statefile memory placement */
|
|
|
|
rc = read(fd, &shdr, sizeof(struct statefile_header));
|
|
|
|
if (rc != sizeof(struct statefile_header)
|
2011-07-01 13:48:28 +00:00
|
|
|
|| shdr.magic != TAGCACHE_STATEFILE_MAGIC
|
2011-06-19 14:36:26 +00:00
|
|
|
|| shdr.mh.tch.magic != TAGCACHE_MAGIC)
|
2006-08-05 20:19:10 +00:00
|
|
|
{
|
|
|
|
logf("incorrect statefile");
|
|
|
|
close(fd);
|
|
|
|
return false;
|
|
|
|
}
|
2017-01-18 09:39:35 +00:00
|
|
|
|
2006-08-05 20:19:10 +00:00
|
|
|
/* Lets allocate real memory and load it */
|
2011-08-30 14:01:45 +00:00
|
|
|
handle = core_alloc_ex("tc ramcache", shdr.tc_stat.ramcache_allocated, &ops);
|
2017-01-18 09:39:35 +00:00
|
|
|
if (handle <= 0)
|
|
|
|
{
|
|
|
|
logf("alloc failure");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
tcrc_buffer_lock();
|
2017-04-01 14:19:52 +00:00
|
|
|
tcramcache.handle = handle;
|
2017-01-18 09:39:35 +00:00
|
|
|
tcramcache.hdr = core_get_data(handle);
|
|
|
|
rc = read(fd, tcramcache.hdr, shdr.tc_stat.ramcache_allocated);
|
|
|
|
tcrc_buffer_unlock();
|
|
|
|
|
2006-08-05 20:19:10 +00:00
|
|
|
close(fd);
|
2011-08-30 14:01:45 +00:00
|
|
|
|
2006-11-10 08:03:33 +00:00
|
|
|
if (rc != shdr.tc_stat.ramcache_allocated)
|
2006-08-05 20:19:10 +00:00
|
|
|
{
|
|
|
|
logf("read failure!");
|
2017-01-18 09:39:35 +00:00
|
|
|
core_free(handle);
|
2006-08-05 20:19:10 +00:00
|
|
|
return false;
|
|
|
|
}
|
2017-01-18 09:39:35 +00:00
|
|
|
|
|
|
|
tc_stat = shdr.tc_stat;
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2006-08-05 20:19:10 +00:00
|
|
|
/* Now fix the pointers */
|
2017-01-18 09:39:35 +00:00
|
|
|
fix_ramcache(shdr.hdr, tcramcache.hdr);
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2011-06-19 14:36:26 +00:00
|
|
|
/* Load the tagcache master header (should match the actual DB file header). */
|
|
|
|
memcpy(¤t_tcmh, &shdr.mh, sizeof current_tcmh);
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2006-08-05 20:19:10 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool tagcache_dumpsave(void)
|
|
|
|
{
|
|
|
|
struct statefile_header shdr;
|
|
|
|
int fd;
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2006-11-10 08:03:33 +00:00
|
|
|
if (!tc_stat.ramcache)
|
2006-08-05 20:19:10 +00:00
|
|
|
return false;
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2010-12-06 22:26:31 +00:00
|
|
|
fd = open(TAGCACHE_STATEFILE, O_WRONLY | O_CREAT | O_TRUNC, 0666);
|
2006-08-05 20:19:10 +00:00
|
|
|
if (fd < 0)
|
|
|
|
{
|
|
|
|
logf("failed to create a statedump");
|
|
|
|
return false;
|
|
|
|
}
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2006-08-05 20:19:10 +00:00
|
|
|
/* Create the header */
|
2011-07-01 13:48:28 +00:00
|
|
|
shdr.magic = TAGCACHE_STATEFILE_MAGIC;
|
2017-01-18 09:39:35 +00:00
|
|
|
shdr.hdr = tcramcache.hdr;
|
2011-06-19 14:36:26 +00:00
|
|
|
memcpy(&shdr.mh, ¤t_tcmh, sizeof current_tcmh);
|
|
|
|
memcpy(&shdr.tc_stat, &tc_stat, sizeof tc_stat);
|
|
|
|
write(fd, &shdr, sizeof shdr);
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2006-08-05 20:19:10 +00:00
|
|
|
/* And dump the data too */
|
2017-01-18 09:39:35 +00:00
|
|
|
tcrc_buffer_lock();
|
|
|
|
write(fd, tcramcache.hdr, tc_stat.ramcache_allocated);
|
|
|
|
tcrc_buffer_unlock();
|
2006-08-05 20:19:10 +00:00
|
|
|
close(fd);
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2006-08-05 20:19:10 +00:00
|
|
|
return true;
|
|
|
|
}
|
2017-01-18 09:39:35 +00:00
|
|
|
#endif /* HAVE_EEPROM_SETTINGS */
|
2006-08-05 20:19:10 +00:00
|
|
|
|
2006-03-26 11:33:42 +00:00
|
|
|
static bool load_tagcache(void)
|
|
|
|
{
|
2017-01-18 09:39:35 +00:00
|
|
|
/* DEBUG: After tagcache commit and dircache rebuild, hdr-sturcture
|
|
|
|
* may become corrupt. */
|
|
|
|
|
|
|
|
bool const auto_update = global_settings.tagcache_autoupdate;
|
|
|
|
|
|
|
|
bool ok = false;
|
|
|
|
ssize_t bytesleft = tc_stat.ramcache_allocated - sizeof(struct ramcache_header);
|
|
|
|
int fd;
|
|
|
|
|
|
|
|
#ifdef HAVE_DIRCACHE
|
|
|
|
/* Wait for any in-progress dircache build to complete */
|
|
|
|
dircache_wait();
|
|
|
|
#endif /* HAVE_DIRCACHE */
|
2008-03-09 20:33:19 +00:00
|
|
|
|
2006-09-23 10:29:14 +00:00
|
|
|
logf("loading tagcache to ram...");
|
2017-04-01 14:19:52 +00:00
|
|
|
|
|
|
|
tcrc_buffer_lock(); /* lock for the rest of the scan, simpler to handle */
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2010-12-06 22:26:31 +00:00
|
|
|
fd = open(TAGCACHE_FILE_MASTER, O_RDONLY);
|
2006-03-26 11:33:42 +00:00
|
|
|
if (fd < 0)
|
|
|
|
{
|
|
|
|
logf("tagcache open failed");
|
2017-01-18 09:39:35 +00:00
|
|
|
goto failure;
|
2006-03-26 11:33:42 +00:00
|
|
|
}
|
2017-01-18 09:39:35 +00:00
|
|
|
|
|
|
|
struct master_header tcmh;
|
2011-06-19 14:36:26 +00:00
|
|
|
if (ecread(fd, &tcmh, 1, master_header_ec, tc_stat.econ)
|
2006-07-20 12:19:31 +00:00
|
|
|
!= sizeof(struct master_header)
|
2011-06-19 14:36:26 +00:00
|
|
|
|| tcmh.tch.magic != TAGCACHE_MAGIC)
|
2006-04-16 17:32:54 +00:00
|
|
|
{
|
|
|
|
logf("incorrect header");
|
2017-01-18 09:39:35 +00:00
|
|
|
goto failure;
|
2006-04-16 17:32:54 +00:00
|
|
|
}
|
2006-03-26 11:33:42 +00:00
|
|
|
|
2011-06-19 14:36:26 +00:00
|
|
|
/* Master header copy should already match, this can be redundant to do. */
|
2017-01-18 09:39:35 +00:00
|
|
|
current_tcmh = tcmh;
|
2006-03-26 11:33:42 +00:00
|
|
|
|
|
|
|
/* Load the master index table. */
|
2017-01-18 09:39:35 +00:00
|
|
|
for (int i = 0; i < tcmh.tch.entry_count; i++)
|
2006-03-26 11:33:42 +00:00
|
|
|
{
|
2011-08-04 10:21:40 +00:00
|
|
|
bytesleft -= sizeof(struct index_entry);
|
|
|
|
if (bytesleft < 0)
|
|
|
|
{
|
|
|
|
logf("too big tagcache.");
|
2011-08-30 14:01:45 +00:00
|
|
|
goto failure;
|
2011-08-04 10:21:40 +00:00
|
|
|
}
|
|
|
|
|
2017-01-18 09:39:35 +00:00
|
|
|
int rc = ecread_index_entry(fd, &tcramcache.hdr->indices[i]);
|
|
|
|
if (rc != sizeof (struct index_entry))
|
2006-03-26 11:33:42 +00:00
|
|
|
{
|
2006-09-26 11:23:18 +00:00
|
|
|
logf("read error #10");
|
2011-08-30 14:01:45 +00:00
|
|
|
goto failure;
|
2006-03-26 11:33:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
close(fd);
|
2017-01-18 09:39:35 +00:00
|
|
|
fd = -1;
|
2006-03-26 11:33:42 +00:00
|
|
|
|
2017-01-18 09:39:35 +00:00
|
|
|
/* Load the tags right after the index entries */
|
|
|
|
char *p = (char *)&tcramcache.hdr->indices[tcmh.tch.entry_count];
|
|
|
|
|
|
|
|
for (int tag = 0; tag < TAG_COUNT; tag++)
|
2006-03-26 11:33:42 +00:00
|
|
|
{
|
2017-01-18 09:39:35 +00:00
|
|
|
ssize_t rc;
|
2006-03-26 11:33:42 +00:00
|
|
|
|
2009-06-03 08:19:32 +00:00
|
|
|
if (TAGCACHE_IS_NUMERIC(tag))
|
2017-01-18 09:39:35 +00:00
|
|
|
continue;
|
|
|
|
|
2017-04-01 14:19:52 +00:00
|
|
|
p = TC_ALIGN_PTR(p, struct tagcache_header, &rc);
|
2017-01-18 09:39:35 +00:00
|
|
|
bytesleft -= rc;
|
2017-02-12 12:13:25 +00:00
|
|
|
if (bytesleft < (ssize_t)sizeof(struct tagcache_header))
|
2017-01-18 09:39:35 +00:00
|
|
|
{
|
|
|
|
logf("Too big tagcache #10.5");
|
|
|
|
goto failure;
|
|
|
|
}
|
|
|
|
|
|
|
|
tcramcache.hdr->tags[tag] = p;
|
2006-03-26 11:33:42 +00:00
|
|
|
|
2017-01-18 09:39:35 +00:00
|
|
|
/* Load the header */
|
|
|
|
struct tagcache_header *tch = (struct tagcache_header *)p;
|
2006-07-23 11:15:28 +00:00
|
|
|
p += sizeof(struct tagcache_header);
|
2017-01-18 09:39:35 +00:00
|
|
|
bytesleft -= sizeof (struct tagcache_header);
|
|
|
|
|
|
|
|
fd = open_tag_fd(tch, tag, false);
|
|
|
|
if (rc < 0)
|
|
|
|
goto failure;
|
|
|
|
|
|
|
|
/* Load the entries for this tag */
|
|
|
|
for (tcramcache.hdr->entry_count[tag] = 0;
|
|
|
|
tcramcache.hdr->entry_count[tag] < tch->entry_count;
|
|
|
|
tcramcache.hdr->entry_count[tag]++)
|
2006-03-26 11:33:42 +00:00
|
|
|
{
|
2017-01-18 09:39:35 +00:00
|
|
|
/* Abort if we got a critical event in queue */
|
|
|
|
if (do_timed_yield() && check_event_queue())
|
|
|
|
goto failure;
|
|
|
|
|
2017-04-01 14:19:52 +00:00
|
|
|
p = TC_ALIGN_PTR(p, struct tagfile_entry, &rc);
|
2017-01-18 09:39:35 +00:00
|
|
|
bytesleft -= rc;
|
2017-02-12 12:13:25 +00:00
|
|
|
if (bytesleft < (ssize_t)sizeof(struct tagfile_entry))
|
2006-09-23 10:29:14 +00:00
|
|
|
{
|
2017-01-18 09:39:35 +00:00
|
|
|
logf("Too big tagcache #10.75");
|
|
|
|
goto failure;
|
|
|
|
}
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2017-01-18 09:39:35 +00:00
|
|
|
struct tagfile_entry *fe = (struct tagfile_entry *)p;
|
|
|
|
off_t pos = lseek(fd, 0, SEEK_CUR);
|
|
|
|
|
|
|
|
/* Load the header for the tag itself */
|
|
|
|
if (ecread_tagfile_entry(fd, fe) != sizeof(struct tagfile_entry))
|
2006-03-26 11:33:42 +00:00
|
|
|
{
|
|
|
|
/* End of lookup table. */
|
2006-09-26 11:23:18 +00:00
|
|
|
logf("read error #11");
|
2011-08-30 14:01:45 +00:00
|
|
|
goto failure;
|
2006-03-26 11:33:42 +00:00
|
|
|
}
|
|
|
|
|
2017-04-01 14:19:52 +00:00
|
|
|
int idx_id = fe->idx_id; /* dircache reference clobbers *fe */
|
|
|
|
struct index_entry *idx = &tcramcache.hdr->indices[idx_id];
|
|
|
|
|
|
|
|
if (idx_id != -1 || tag == tag_filename) /* filename NOT optional */
|
2006-03-26 11:33:42 +00:00
|
|
|
{
|
2017-02-12 12:13:25 +00:00
|
|
|
if (idx_id < 0 || idx_id >= tcmh.tch.entry_count)
|
|
|
|
{
|
2017-04-01 14:19:52 +00:00
|
|
|
logf("corrupt tagfile entry:tag=%d:idxid=%d", tag, idx_id);
|
2017-02-12 12:13:25 +00:00
|
|
|
goto failure;
|
|
|
|
}
|
|
|
|
|
2017-04-01 14:19:52 +00:00
|
|
|
if (idx->tag_seek[tag] != pos)
|
|
|
|
{
|
|
|
|
logf("corrupt data structures!:");
|
|
|
|
logf(" tag_seek[%d]=%ld:pos=%ld", tag,
|
|
|
|
idx->tag_seek[tag], pos);
|
|
|
|
goto failure;
|
|
|
|
}
|
|
|
|
}
|
2017-01-18 09:39:35 +00:00
|
|
|
|
2017-04-01 14:19:52 +00:00
|
|
|
/* We have a special handling for the filename tags; neither the
|
|
|
|
paths nor the entry headers are stored; only the tagcache header
|
|
|
|
and dircache references are. */
|
|
|
|
if (tag == tag_filename)
|
|
|
|
{
|
2017-01-18 09:39:35 +00:00
|
|
|
#ifdef HAVE_DIRCACHE
|
2006-07-12 12:50:26 +00:00
|
|
|
if (idx->flag & FLAG_DIRCACHE)
|
|
|
|
{
|
2017-01-18 09:39:35 +00:00
|
|
|
/* This flag must not be used yet. */
|
2006-07-12 12:50:26 +00:00
|
|
|
logf("internal error!");
|
2011-08-30 14:01:45 +00:00
|
|
|
goto failure;
|
2006-07-12 12:50:26 +00:00
|
|
|
}
|
2017-01-18 09:39:35 +00:00
|
|
|
|
|
|
|
p += sizeof (struct dircache_fileref);
|
|
|
|
bytesleft -= sizeof (struct dircache_fileref);
|
|
|
|
#endif /* HAVE_DIRCACHE */
|
|
|
|
|
2021-08-01 02:24:33 +00:00
|
|
|
char filename[TAGCACHE_BUFSZ];
|
2017-01-18 09:39:35 +00:00
|
|
|
if (fe->tag_length >= (long)sizeof(filename)-1)
|
2006-03-26 11:33:42 +00:00
|
|
|
{
|
2017-01-18 09:39:35 +00:00
|
|
|
read(fd, filename, 10);
|
2021-08-01 02:24:33 +00:00
|
|
|
str_setlen(filename, 10);
|
2017-01-18 09:39:35 +00:00
|
|
|
logf("TAG:%s", filename);
|
|
|
|
logf("too long filename");
|
|
|
|
goto failure;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((idx->flag & FLAG_DELETED) IFN_DIRCACHE( || !auto_update ))
|
|
|
|
{
|
2017-04-01 14:19:52 +00:00
|
|
|
/* seek over tag data instead of reading */
|
2017-01-18 09:39:35 +00:00
|
|
|
if (lseek(fd, fe->tag_length, SEEK_CUR) < 0)
|
2006-07-10 16:22:03 +00:00
|
|
|
{
|
2017-01-18 09:39:35 +00:00
|
|
|
logf("read error #11.5");
|
|
|
|
goto failure;
|
2006-07-10 16:22:03 +00:00
|
|
|
}
|
2017-04-01 14:19:52 +00:00
|
|
|
|
|
|
|
continue;
|
2017-01-18 09:39:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (read(fd, filename, fe->tag_length) != fe->tag_length)
|
|
|
|
{
|
|
|
|
logf("read error #12");
|
|
|
|
goto failure;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef HAVE_DIRCACHE
|
|
|
|
/* If auto updating, check storage too, otherwise simply
|
|
|
|
attempt to load cache references */
|
|
|
|
unsigned int searchflag = auto_update ? DCS_STORAGE_PATH :
|
|
|
|
DCS_CACHED_PATH;
|
2006-07-10 16:22:03 +00:00
|
|
|
|
2017-01-18 09:39:35 +00:00
|
|
|
int rc = dircache_search(searchflag | DCS_UPDATE_FILEREF,
|
|
|
|
&tcrc_dcfrefs[idx_id], filename);
|
|
|
|
if (rc > 0) /* in cache and we have fileref */
|
2006-07-10 16:22:03 +00:00
|
|
|
idx->flag |= FLAG_DIRCACHE;
|
2017-01-18 09:39:35 +00:00
|
|
|
else if (rc == 0) /* not in cache but okay */
|
|
|
|
;
|
|
|
|
else if (auto_update)
|
|
|
|
#else /* ndef HAVE_DIRCACHE */
|
|
|
|
/* Check if path is no longer valid */
|
2017-04-01 14:19:52 +00:00
|
|
|
if (!file_exists(filename))
|
2017-01-18 09:39:35 +00:00
|
|
|
#endif /* HAVE_DIRCACHE */
|
|
|
|
{
|
|
|
|
logf("Entry no longer valid.");
|
|
|
|
logf("-> %s", filename);
|
|
|
|
delete_entry(idx_id);
|
2006-03-26 11:33:42 +00:00
|
|
|
}
|
2017-01-18 09:39:35 +00:00
|
|
|
|
|
|
|
continue;
|
2006-03-26 11:33:42 +00:00
|
|
|
}
|
2017-01-18 09:39:35 +00:00
|
|
|
|
2006-03-26 11:33:42 +00:00
|
|
|
bytesleft -= sizeof(struct tagfile_entry) + fe->tag_length;
|
|
|
|
if (bytesleft < 0)
|
|
|
|
{
|
2006-05-28 09:59:24 +00:00
|
|
|
logf("too big tagcache #2");
|
2010-03-16 12:15:44 +00:00
|
|
|
logf("tl: %ld", fe->tag_length);
|
2007-03-18 09:50:53 +00:00
|
|
|
logf("bl: %ld", bytesleft);
|
2011-08-30 14:01:45 +00:00
|
|
|
goto failure;
|
2006-03-26 11:33:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
p = fe->tag_data;
|
2017-01-18 09:39:35 +00:00
|
|
|
rc = read(fd, p, fe->tag_length);
|
2006-03-26 11:33:42 +00:00
|
|
|
p += rc;
|
|
|
|
|
|
|
|
if (rc != fe->tag_length)
|
|
|
|
{
|
2006-09-26 11:23:18 +00:00
|
|
|
logf("read error #13");
|
2017-04-01 14:19:52 +00:00
|
|
|
logf("rc=0x%04x", (unsigned int)rc); // 0x431
|
2010-03-16 12:15:44 +00:00
|
|
|
logf("len=0x%04lx", fe->tag_length); // 0x4000
|
2007-03-20 13:53:23 +00:00
|
|
|
logf("pos=0x%04lx", lseek(fd, 0, SEEK_CUR)); // 0x433
|
2006-07-12 12:50:26 +00:00
|
|
|
logf("tag=0x%02x", tag); // 0x00
|
2011-08-30 14:01:45 +00:00
|
|
|
goto failure;
|
2006-03-26 11:33:42 +00:00
|
|
|
}
|
|
|
|
}
|
2017-01-18 09:39:35 +00:00
|
|
|
|
|
|
|
#ifdef HAVE_DIRCACHE
|
|
|
|
if (tag == tag_filename)
|
|
|
|
p = (char *)&tcrc_dcfrefs[tcmh.tch.entry_count];
|
|
|
|
#endif /* HAVE_DIRCACHE */
|
|
|
|
|
2006-03-26 11:33:42 +00:00
|
|
|
close(fd);
|
|
|
|
}
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2006-11-10 08:03:33 +00:00
|
|
|
tc_stat.ramcache_used = tc_stat.ramcache_allocated - bytesleft;
|
2006-03-26 11:33:42 +00:00
|
|
|
logf("tagcache loaded into ram!");
|
2017-01-18 09:39:35 +00:00
|
|
|
logf("utilization: %d%%", 100*tc_stat.ramcache_used / tc_stat.ramcache_allocated);
|
2006-03-26 11:33:42 +00:00
|
|
|
|
2017-01-18 09:39:35 +00:00
|
|
|
ok = true;
|
2011-08-30 14:01:45 +00:00
|
|
|
|
|
|
|
failure:
|
2017-01-18 09:39:35 +00:00
|
|
|
if (fd >= 0)
|
|
|
|
close(fd);
|
|
|
|
|
|
|
|
tcrc_buffer_unlock();
|
|
|
|
return ok;
|
2006-03-26 11:33:42 +00:00
|
|
|
}
|
2006-07-10 16:22:03 +00:00
|
|
|
#endif /* HAVE_TC_RAMCACHE */
|
|
|
|
|
|
|
|
static bool check_deleted_files(void)
|
|
|
|
{
|
2008-01-13 19:13:37 +00:00
|
|
|
int fd;
|
2021-08-01 02:24:33 +00:00
|
|
|
char buf[TAGCACHE_BUFSZ];
|
2021-07-30 06:13:57 +00:00
|
|
|
const int bufsz = sizeof(buf);
|
2006-07-10 16:22:03 +00:00
|
|
|
struct tagfile_entry tfe;
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2006-07-10 16:22:03 +00:00
|
|
|
logf("reverse scan...");
|
2021-07-30 06:13:57 +00:00
|
|
|
snprintf(buf, bufsz, TAGCACHE_FILE_INDEX, tag_filename);
|
2010-12-06 22:26:31 +00:00
|
|
|
fd = open(buf, O_RDONLY);
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2006-07-10 16:22:03 +00:00
|
|
|
if (fd < 0)
|
|
|
|
{
|
|
|
|
logf("%s open fail", buf);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
lseek(fd, sizeof(struct tagcache_header), SEEK_SET);
|
2021-07-31 14:26:59 +00:00
|
|
|
while (ecread_tagfile_entry(fd, &tfe) == sizeof(struct tagfile_entry)
|
2017-01-18 09:39:35 +00:00
|
|
|
&& !check_event_queue())
|
2006-07-10 16:22:03 +00:00
|
|
|
{
|
2021-07-30 06:13:57 +00:00
|
|
|
if (tfe.tag_length >= (long)bufsz-1)
|
2006-07-10 16:22:03 +00:00
|
|
|
{
|
|
|
|
logf("too long tag");
|
|
|
|
close(fd);
|
|
|
|
return false;
|
|
|
|
}
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2006-07-10 16:22:03 +00:00
|
|
|
if (read(fd, buf, tfe.tag_length) != tfe.tag_length)
|
|
|
|
{
|
2006-09-26 11:23:18 +00:00
|
|
|
logf("read error #14");
|
2006-07-10 16:22:03 +00:00
|
|
|
close(fd);
|
|
|
|
return false;
|
|
|
|
}
|
2021-08-01 02:24:33 +00:00
|
|
|
str_setlen(buf, tfe.tag_length);
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2006-11-19 19:11:30 +00:00
|
|
|
/* Check if the file has already deleted from the db. */
|
|
|
|
if (*buf == '\0')
|
|
|
|
continue;
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2006-07-10 16:22:03 +00:00
|
|
|
/* Now check if the file exists. */
|
2008-01-13 19:13:37 +00:00
|
|
|
if (!file_exists(buf))
|
2006-07-10 16:22:03 +00:00
|
|
|
{
|
|
|
|
logf("Entry no longer valid.");
|
2010-03-16 12:15:44 +00:00
|
|
|
logf("-> %s / %ld", buf, tfe.tag_length);
|
2006-07-10 16:22:03 +00:00
|
|
|
delete_entry(tfe.idx_id);
|
|
|
|
}
|
2017-02-04 09:15:48 +00:00
|
|
|
|
|
|
|
do_timed_yield();
|
2006-07-10 16:22:03 +00:00
|
|
|
}
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2006-07-10 16:22:03 +00:00
|
|
|
close(fd);
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2006-07-10 16:22:03 +00:00
|
|
|
logf("done");
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2006-07-10 16:22:03 +00:00
|
|
|
return true;
|
|
|
|
}
|
2006-03-26 11:33:42 +00:00
|
|
|
|
2009-06-30 17:39:03 +00:00
|
|
|
|
|
|
|
/* Note that this function must not be inlined, otherwise the whole point
|
|
|
|
* of having the code in a separate function is lost.
|
|
|
|
*/
|
2017-01-18 09:39:35 +00:00
|
|
|
static void NO_INLINE check_ignore(const char *dirname,
|
2009-06-30 17:39:03 +00:00
|
|
|
int *ignore, int *unignore)
|
|
|
|
{
|
|
|
|
char newpath[MAX_PATH];
|
2021-07-30 06:13:57 +00:00
|
|
|
const int bufsz = sizeof(newpath);
|
2009-06-30 17:39:03 +00:00
|
|
|
|
|
|
|
/* check for a database.ignore file */
|
2021-07-30 06:13:57 +00:00
|
|
|
snprintf(newpath, bufsz, "%s/database.ignore", dirname);
|
2009-06-30 17:39:03 +00:00
|
|
|
*ignore = file_exists(newpath);
|
|
|
|
/* check for a database.unignore file */
|
2021-07-30 06:13:57 +00:00
|
|
|
snprintf(newpath, bufsz, "%s/database.unignore", dirname);
|
2009-06-30 17:39:03 +00:00
|
|
|
*unignore = file_exists(newpath);
|
|
|
|
}
|
|
|
|
|
2012-07-18 21:36:57 +00:00
|
|
|
/* max roots on native. on application more can be added via malloc() */
|
|
|
|
#define MAX_STATIC_ROOTS 12
|
|
|
|
|
2010-12-23 19:02:18 +00:00
|
|
|
static struct search_roots_ll {
|
|
|
|
const char *path;
|
|
|
|
struct search_roots_ll * next;
|
2012-07-18 21:36:57 +00:00
|
|
|
} roots_ll[MAX_STATIC_ROOTS];
|
|
|
|
|
|
|
|
/* check if the path is already included in the search roots, by the
|
|
|
|
* means that the path itself or one of its parents folders is in the list */
|
|
|
|
static bool search_root_exists(const char *path)
|
|
|
|
{
|
|
|
|
struct search_roots_ll *this;
|
|
|
|
for(this = &roots_ll[0]; this; this = this->next)
|
|
|
|
{
|
|
|
|
size_t root_len = strlen(this->path);
|
|
|
|
/* check if the link target is inside of an existing search root
|
|
|
|
* don't add if target is inside, we'll scan it later */
|
|
|
|
if (!strncmp(this->path, path, root_len))
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2010-12-23 19:02:18 +00:00
|
|
|
|
|
|
|
#ifdef APPLICATION
|
|
|
|
/*
|
|
|
|
* This adds a path to the search roots, possibly during traveling through
|
|
|
|
* the filesystem. It only adds if the path is not inside an already existing
|
|
|
|
* search root.
|
|
|
|
*
|
|
|
|
* Returns true if it added the path to the search roots
|
2021-07-31 14:26:59 +00:00
|
|
|
*
|
2010-12-23 19:02:18 +00:00
|
|
|
* Windows 2000 and greater supports symlinks, but they don't provide
|
|
|
|
* realpath() or readlink(), and symlinks are rarely used on them so
|
|
|
|
* ignore this for windows for now
|
|
|
|
**/
|
|
|
|
static bool add_search_root(const char *name)
|
|
|
|
{
|
|
|
|
(void)name;
|
|
|
|
#ifndef WIN32
|
|
|
|
struct search_roots_ll *this, *prev = NULL;
|
|
|
|
char target[MAX_PATH];
|
2021-07-30 06:13:57 +00:00
|
|
|
const int target_bufsz = sizeof(target);
|
2011-01-27 20:17:55 +00:00
|
|
|
/* Okay, realpath() is almost completely broken on android
|
|
|
|
*
|
|
|
|
* It doesn't accept NULL for resolved_name to dynamically allocate
|
|
|
|
* the resulting path; and it assumes resolved_name to be PATH_MAX
|
|
|
|
* (actually MAXPATHLEN, but it's the same [as of 2.3]) long
|
|
|
|
* and blindly writes to the end if it
|
|
|
|
*
|
|
|
|
* therefore use sufficiently large static storage here
|
|
|
|
* Note that PATH_MAX != MAX_PATH
|
|
|
|
**/
|
|
|
|
static char abs_target[PATH_MAX];
|
2010-12-23 19:02:18 +00:00
|
|
|
ssize_t len;
|
|
|
|
|
2021-07-30 06:13:57 +00:00
|
|
|
len = readlink(name, target, target_bufsz-1);
|
2010-12-23 19:02:18 +00:00
|
|
|
if (len < 0)
|
|
|
|
return false;
|
|
|
|
|
2021-08-01 02:24:33 +00:00
|
|
|
str_setlen(target, len);
|
2011-01-27 20:17:55 +00:00
|
|
|
if (realpath(target, abs_target) == NULL)
|
2010-12-23 19:02:18 +00:00
|
|
|
return false;
|
|
|
|
|
2012-07-18 21:36:57 +00:00
|
|
|
if (search_root_exists(abs_target))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
/* get the end of the list */
|
|
|
|
for(this = &roots_ll[0]; this; prev = this, this = this->next);
|
2010-12-23 19:02:18 +00:00
|
|
|
|
|
|
|
if (prev)
|
|
|
|
{
|
|
|
|
size_t len = strlen(abs_target) + 1; /* count \0 */
|
|
|
|
this = malloc(sizeof(struct search_roots_ll) + len );
|
2021-07-30 06:13:57 +00:00
|
|
|
if (!this || len > MIN(PATH_MAX, MAX_PATH))
|
2010-12-23 19:02:18 +00:00
|
|
|
{
|
|
|
|
logf("Error at adding a search root: %s", this ? "path too long":"OOM");
|
|
|
|
free(this);
|
|
|
|
prev->next = NULL;
|
2011-01-27 20:17:55 +00:00
|
|
|
return false;
|
2010-12-23 19:02:18 +00:00
|
|
|
}
|
|
|
|
this->path = ((char*)this) + sizeof(struct search_roots_ll);
|
|
|
|
strcpy((char*)this->path, abs_target); /* ok to cast const away here */
|
|
|
|
this->next = NULL;
|
|
|
|
prev->next = this;
|
|
|
|
logf("Added %s to the search roots\n", abs_target);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-07-18 21:36:57 +00:00
|
|
|
static int free_search_root_single(struct search_roots_ll * start)
|
|
|
|
{
|
|
|
|
if (start < &roots_ll[0] && start >= &roots_ll[MAX_STATIC_ROOTS])
|
|
|
|
{
|
|
|
|
free(start->next);
|
|
|
|
return sizeof(struct search_roots_ll);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-12-23 19:02:18 +00:00
|
|
|
static int free_search_roots(struct search_roots_ll * start)
|
|
|
|
{
|
|
|
|
int ret = 0;
|
|
|
|
if (start->next)
|
|
|
|
{
|
2012-07-18 21:36:57 +00:00
|
|
|
ret += free_search_root_single(start->next);
|
2010-12-23 19:02:18 +00:00
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
#else /* native, simulator */
|
|
|
|
#define add_search_root(a) do {} while(0)
|
|
|
|
#define free_search_roots(a) do {} while(0)
|
|
|
|
#endif
|
2009-06-30 17:39:03 +00:00
|
|
|
|
2008-02-04 21:20:04 +00:00
|
|
|
static bool check_dir(const char *dirname, int add_files)
|
2006-03-26 11:33:42 +00:00
|
|
|
{
|
|
|
|
int success = false;
|
|
|
|
|
2017-01-18 09:39:35 +00:00
|
|
|
DIR *dir = opendir(dirname);
|
2006-03-26 11:33:42 +00:00
|
|
|
if (!dir)
|
|
|
|
{
|
2010-03-16 12:15:56 +00:00
|
|
|
logf("tagcache: opendir(%s) failed", dirname);
|
2006-03-26 11:33:42 +00:00
|
|
|
return false;
|
|
|
|
}
|
2017-01-18 09:39:35 +00:00
|
|
|
|
2009-06-30 17:39:03 +00:00
|
|
|
/* check for a database.ignore and database.unignore */
|
2017-01-18 09:39:35 +00:00
|
|
|
int ignore, unignore;
|
2009-06-30 17:39:03 +00:00
|
|
|
check_ignore(dirname, &ignore, &unignore);
|
2008-02-04 21:20:04 +00:00
|
|
|
|
|
|
|
/* don't do anything if both ignore and unignore are there */
|
|
|
|
if (ignore != unignore)
|
|
|
|
add_files = unignore;
|
|
|
|
|
2006-03-26 11:33:42 +00:00
|
|
|
/* Recursively scan the dir. */
|
2006-09-23 10:29:14 +00:00
|
|
|
while (!check_event_queue())
|
2006-03-26 11:33:42 +00:00
|
|
|
{
|
2010-12-23 19:02:18 +00:00
|
|
|
struct dirent *entry = readdir(dir);
|
2006-03-26 11:33:42 +00:00
|
|
|
if (entry == NULL)
|
|
|
|
{
|
|
|
|
success = true;
|
2010-12-23 19:02:18 +00:00
|
|
|
break;
|
2006-03-26 11:33:42 +00:00
|
|
|
}
|
2010-09-01 21:29:34 +00:00
|
|
|
|
2017-01-18 09:39:35 +00:00
|
|
|
if (is_dotdir_name(entry->d_name))
|
2006-03-26 11:33:42 +00:00
|
|
|
continue;
|
|
|
|
|
2010-12-23 19:02:18 +00:00
|
|
|
struct dirinfo info = dir_get_info(dir, entry);
|
2017-01-18 09:39:35 +00:00
|
|
|
size_t len = strlen(curpath);
|
2017-02-12 12:13:25 +00:00
|
|
|
path_append(&curpath[len-1], PA_SEP_HARD, entry->d_name,
|
2017-01-18 09:39:35 +00:00
|
|
|
sizeof (curpath) - len);
|
2010-12-23 19:02:18 +00:00
|
|
|
|
2006-03-26 11:33:42 +00:00
|
|
|
processed_dir_count++;
|
2010-09-01 21:29:34 +00:00
|
|
|
if (info.attribute & ATTR_DIRECTORY)
|
2017-01-18 09:39:35 +00:00
|
|
|
{
|
2011-03-05 23:19:06 +00:00
|
|
|
#ifndef SIMULATOR
|
2017-01-18 09:39:35 +00:00
|
|
|
/* don't follow symlinks to dirs, but try to add it as a search root
|
2010-12-23 19:02:18 +00:00
|
|
|
* this makes able to avoid looping in recursive symlinks */
|
|
|
|
if (info.attribute & ATTR_LINK)
|
|
|
|
add_search_root(curpath);
|
|
|
|
else
|
2017-01-18 09:39:35 +00:00
|
|
|
#endif /* SIMULATOR */
|
2010-12-23 19:02:18 +00:00
|
|
|
check_dir(curpath, add_files);
|
|
|
|
}
|
2008-02-04 21:20:04 +00:00
|
|
|
else if (add_files)
|
2007-10-21 11:06:30 +00:00
|
|
|
{
|
|
|
|
tc_stat.curentry = curpath;
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2007-12-16 21:10:26 +00:00
|
|
|
/* Add a new entry to the temporary db file. */
|
2017-01-18 09:39:35 +00:00
|
|
|
add_tagcache(curpath, info.mtime);
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2007-10-21 11:06:30 +00:00
|
|
|
/* Wait until current path for debug screen is read and unset. */
|
|
|
|
while (tc_stat.syncscreen && tc_stat.curentry != NULL)
|
|
|
|
yield();
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2007-10-21 11:06:30 +00:00
|
|
|
tc_stat.curentry = NULL;
|
|
|
|
}
|
2006-03-26 11:33:42 +00:00
|
|
|
|
2021-08-01 02:24:33 +00:00
|
|
|
str_setlen(curpath, len);
|
2006-03-26 11:33:42 +00:00
|
|
|
}
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2007-07-20 17:06:55 +00:00
|
|
|
closedir(dir);
|
2006-03-26 11:33:42 +00:00
|
|
|
|
|
|
|
return success;
|
|
|
|
}
|
|
|
|
|
2007-10-21 11:06:30 +00:00
|
|
|
void tagcache_screensync_event(void)
|
|
|
|
{
|
|
|
|
tc_stat.curentry = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
void tagcache_screensync_enable(bool state)
|
|
|
|
{
|
|
|
|
tc_stat.syncscreen = state;
|
|
|
|
}
|
|
|
|
|
2012-07-31 08:33:27 +00:00
|
|
|
#ifndef __PCTOOL__
|
|
|
|
/* this is called by the database tool to not pull in global_settings */
|
|
|
|
static
|
|
|
|
#endif
|
|
|
|
void do_tagcache_build(const char *path[])
|
2006-03-26 11:33:42 +00:00
|
|
|
{
|
|
|
|
struct tagcache_header header;
|
|
|
|
bool ret;
|
|
|
|
|
2021-08-01 02:24:33 +00:00
|
|
|
str_setlen(curpath, 0);
|
2006-03-26 11:33:42 +00:00
|
|
|
data_size = 0;
|
|
|
|
total_entry_count = 0;
|
|
|
|
processed_dir_count = 0;
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2006-08-26 09:24:20 +00:00
|
|
|
#ifdef HAVE_DIRCACHE
|
2017-01-18 09:39:35 +00:00
|
|
|
dircache_wait();
|
2006-08-26 09:24:20 +00:00
|
|
|
#endif
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2006-09-23 10:29:14 +00:00
|
|
|
logf("updating tagcache");
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2010-12-06 22:26:31 +00:00
|
|
|
cachefd = open(TAGCACHE_FILE_TEMP, O_RDONLY);
|
|
|
|
if (cachefd >= 0)
|
2006-03-26 11:33:42 +00:00
|
|
|
{
|
|
|
|
logf("skipping, cache already waiting for commit");
|
2010-12-06 22:26:31 +00:00
|
|
|
close(cachefd);
|
2006-03-26 11:33:42 +00:00
|
|
|
return ;
|
|
|
|
}
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2010-12-06 22:26:31 +00:00
|
|
|
cachefd = open(TAGCACHE_FILE_TEMP, O_RDWR | O_CREAT | O_TRUNC, 0666);
|
2006-03-26 11:33:42 +00:00
|
|
|
if (cachefd < 0)
|
|
|
|
{
|
2010-12-06 22:26:31 +00:00
|
|
|
logf("master file open failed: %s", TAGCACHE_FILE_TEMP);
|
2006-03-26 11:33:42 +00:00
|
|
|
return ;
|
|
|
|
}
|
|
|
|
|
2006-07-23 11:15:28 +00:00
|
|
|
filenametag_fd = open_tag_fd(&header, tag_filename, false);
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2006-12-05 20:01:48 +00:00
|
|
|
cpu_boost(true);
|
2006-03-26 11:33:42 +00:00
|
|
|
|
2006-11-10 08:03:33 +00:00
|
|
|
logf("Scanning files...");
|
2006-03-26 11:33:42 +00:00
|
|
|
/* Scan for new files. */
|
|
|
|
memset(&header, 0, sizeof(struct tagcache_header));
|
|
|
|
write(cachefd, &header, sizeof(struct tagcache_header));
|
|
|
|
|
2010-12-23 19:02:18 +00:00
|
|
|
ret = true;
|
2012-07-18 21:36:57 +00:00
|
|
|
|
|
|
|
roots_ll[0].path = path[0];
|
|
|
|
roots_ll[0].next = NULL;
|
|
|
|
/* i is for the path vector, j for the roots_ll array
|
|
|
|
* path can be skipped , but root_ll entries can't */
|
|
|
|
for(int i = 1, j = 1; path[i] && j < MAX_STATIC_ROOTS; i++)
|
|
|
|
{
|
|
|
|
if (search_root_exists(path[i])) /* skip this path */
|
|
|
|
continue;
|
|
|
|
|
|
|
|
roots_ll[j].path = path[i];
|
|
|
|
roots_ll[j-1].next = &roots_ll[j];
|
|
|
|
j++;
|
|
|
|
}
|
|
|
|
|
2010-12-23 19:02:18 +00:00
|
|
|
struct search_roots_ll * this;
|
|
|
|
/* check_dir might add new roots */
|
2012-07-18 21:36:57 +00:00
|
|
|
for(this = &roots_ll[0]; this; this = this->next)
|
2010-12-23 19:02:18 +00:00
|
|
|
{
|
2021-07-30 06:13:57 +00:00
|
|
|
strlcpy(curpath, this->path, sizeof(curpath));
|
2010-12-23 19:02:18 +00:00
|
|
|
ret = ret && check_dir(this->path, true);
|
|
|
|
}
|
2012-07-18 21:36:57 +00:00
|
|
|
free_search_roots(&roots_ll[0]);
|
2010-12-23 19:02:18 +00:00
|
|
|
|
2006-03-26 11:33:42 +00:00
|
|
|
/* Write the header. */
|
|
|
|
header.magic = TAGCACHE_MAGIC;
|
|
|
|
header.datasize = data_size;
|
|
|
|
header.entry_count = total_entry_count;
|
|
|
|
lseek(cachefd, 0, SEEK_SET);
|
|
|
|
write(cachefd, &header, sizeof(struct tagcache_header));
|
|
|
|
close(cachefd);
|
|
|
|
|
|
|
|
if (filenametag_fd >= 0)
|
|
|
|
{
|
|
|
|
close(filenametag_fd);
|
|
|
|
filenametag_fd = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!ret)
|
|
|
|
{
|
|
|
|
logf("Aborted.");
|
2006-12-05 20:01:48 +00:00
|
|
|
cpu_boost(false);
|
2006-03-26 11:33:42 +00:00
|
|
|
return ;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Commit changes to the database. */
|
2006-11-10 08:03:33 +00:00
|
|
|
#ifdef __PCTOOL__
|
|
|
|
allocate_tempbuf();
|
|
|
|
#endif
|
2006-03-26 11:33:42 +00:00
|
|
|
if (commit())
|
|
|
|
{
|
|
|
|
logf("tagcache built!");
|
|
|
|
}
|
2006-11-10 08:03:33 +00:00
|
|
|
#ifdef __PCTOOL__
|
|
|
|
free_tempbuf();
|
|
|
|
#endif
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2006-09-23 10:29:14 +00:00
|
|
|
#ifdef HAVE_TC_RAMCACHE
|
2017-01-18 09:39:35 +00:00
|
|
|
if (tcramcache.hdr)
|
2006-09-23 10:29:14 +00:00
|
|
|
{
|
|
|
|
/* Import runtime statistics if we just initialized the db. */
|
2011-06-19 14:36:26 +00:00
|
|
|
if (current_tcmh.serial == 0)
|
2006-09-23 10:29:14 +00:00
|
|
|
queue_post(&tagcache_queue, Q_IMPORT_CHANGELOG, 0);
|
|
|
|
}
|
|
|
|
#endif
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2006-12-05 20:01:48 +00:00
|
|
|
cpu_boost(false);
|
2006-03-26 11:33:42 +00:00
|
|
|
}
|
|
|
|
|
2012-07-31 08:33:27 +00:00
|
|
|
#ifndef __PCTOOL__
|
2012-07-18 21:36:57 +00:00
|
|
|
void tagcache_build(void)
|
|
|
|
{
|
|
|
|
char *vect[MAX_STATIC_ROOTS + 1]; /* +1 to ensure NULL sentinel */
|
|
|
|
char str[sizeof(global_settings.tagcache_scan_paths)];
|
|
|
|
strlcpy(str, global_settings.tagcache_scan_paths, sizeof(str));
|
|
|
|
|
|
|
|
int res = split_string(str, ':', vect, MAX_STATIC_ROOTS);
|
|
|
|
vect[res] = NULL;
|
|
|
|
|
|
|
|
do_tagcache_build((const char**)vect);
|
|
|
|
}
|
2017-01-18 09:39:35 +00:00
|
|
|
#endif /* __PCTOOL__ */
|
2012-07-18 21:36:57 +00:00
|
|
|
|
2006-03-26 11:33:42 +00:00
|
|
|
#ifdef HAVE_TC_RAMCACHE
|
|
|
|
static void load_ramcache(void)
|
|
|
|
{
|
2017-01-18 09:39:35 +00:00
|
|
|
if (!tcramcache.hdr)
|
2006-03-26 11:33:42 +00:00
|
|
|
return ;
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2006-12-05 20:01:48 +00:00
|
|
|
cpu_boost(true);
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2006-03-26 11:33:42 +00:00
|
|
|
/* At first we should load the cache (if exists). */
|
2006-11-10 08:03:33 +00:00
|
|
|
tc_stat.ramcache = load_tagcache();
|
2006-03-26 11:33:42 +00:00
|
|
|
|
2006-11-10 08:03:33 +00:00
|
|
|
if (!tc_stat.ramcache)
|
2006-07-23 17:31:35 +00:00
|
|
|
{
|
|
|
|
/* If loading failed, it must indicate some problem with the db
|
|
|
|
* so disable it entirely to prevent further issues. */
|
2006-11-10 08:03:33 +00:00
|
|
|
tc_stat.ready = false;
|
2017-01-18 09:39:35 +00:00
|
|
|
tcramcache.hdr = NULL;
|
2017-04-01 14:19:52 +00:00
|
|
|
int handle = tcramcache.handle;
|
|
|
|
tcramcache.handle = 0;
|
|
|
|
core_free(handle);
|
2006-07-23 17:31:35 +00:00
|
|
|
}
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2006-12-05 20:01:48 +00:00
|
|
|
cpu_boost(false);
|
2006-03-26 11:33:42 +00:00
|
|
|
}
|
2006-08-09 07:41:28 +00:00
|
|
|
|
|
|
|
void tagcache_unload_ramcache(void)
|
|
|
|
{
|
2006-11-10 08:03:33 +00:00
|
|
|
tc_stat.ramcache = false;
|
2006-08-09 07:41:28 +00:00
|
|
|
/* Just to make sure there is no statefile present. */
|
2010-12-06 22:26:31 +00:00
|
|
|
// remove(TAGCACHE_STATEFILE);
|
2006-08-09 07:41:28 +00:00
|
|
|
}
|
2017-01-18 09:39:35 +00:00
|
|
|
#endif /* HAVE_TC_RAMCACHE */
|
2006-03-26 11:33:42 +00:00
|
|
|
|
2006-11-10 08:03:33 +00:00
|
|
|
#ifndef __PCTOOL__
|
2006-03-26 11:33:42 +00:00
|
|
|
static void tagcache_thread(void)
|
|
|
|
{
|
2007-10-16 01:25:17 +00:00
|
|
|
struct queue_event ev;
|
2006-03-26 11:33:42 +00:00
|
|
|
bool check_done = false;
|
|
|
|
|
2006-04-12 10:31:24 +00:00
|
|
|
/* If the previous cache build/update was interrupted, commit
|
2006-04-16 17:32:54 +00:00
|
|
|
* the changes first in foreground. */
|
2006-12-05 20:01:48 +00:00
|
|
|
cpu_boost(true);
|
2006-04-12 10:31:24 +00:00
|
|
|
allocate_tempbuf();
|
|
|
|
commit();
|
|
|
|
free_tempbuf();
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2006-04-23 18:47:26 +00:00
|
|
|
#ifdef HAVE_TC_RAMCACHE
|
2017-01-18 09:39:35 +00:00
|
|
|
#ifdef HAVE_EEPROM_SETTINGS
|
2010-07-18 18:17:47 +00:00
|
|
|
if (firmware_settings.initialized && firmware_settings.disk_clean
|
|
|
|
&& global_settings.tagcache_ram)
|
|
|
|
{
|
2006-08-05 20:19:10 +00:00
|
|
|
check_done = tagcache_dumpload();
|
2010-07-18 18:17:47 +00:00
|
|
|
}
|
2006-08-05 20:19:10 +00:00
|
|
|
|
2010-12-06 22:26:31 +00:00
|
|
|
remove(TAGCACHE_STATEFILE);
|
2017-01-18 09:39:35 +00:00
|
|
|
#endif /* HAVE_EEPROM_SETTINGS */
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2006-04-23 18:47:26 +00:00
|
|
|
/* Allocate space for the tagcache if found on disk. */
|
2006-11-10 08:03:33 +00:00
|
|
|
if (global_settings.tagcache_ram && !tc_stat.ramcache)
|
2006-04-23 18:47:26 +00:00
|
|
|
allocate_tagcache();
|
2017-01-18 09:39:35 +00:00
|
|
|
#endif /* HAVE_TC_RAMCACHE */
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2006-12-05 20:01:48 +00:00
|
|
|
cpu_boost(false);
|
2006-11-10 08:03:33 +00:00
|
|
|
tc_stat.initialized = true;
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2006-07-23 11:15:28 +00:00
|
|
|
/* Don't delay bootup with the header check but do it on background. */
|
2010-07-18 18:17:47 +00:00
|
|
|
if (!tc_stat.ready)
|
|
|
|
{
|
|
|
|
sleep(HZ);
|
|
|
|
tc_stat.ready = check_all_headers();
|
|
|
|
tc_stat.readyvalid = true;
|
|
|
|
}
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2006-03-26 11:33:42 +00:00
|
|
|
while (1)
|
|
|
|
{
|
2007-07-21 17:35:19 +00:00
|
|
|
run_command_queue(false);
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2006-03-26 11:33:42 +00:00
|
|
|
queue_wait_w_tmo(&tagcache_queue, &ev, HZ);
|
|
|
|
|
|
|
|
switch (ev.id)
|
|
|
|
{
|
2006-07-23 15:35:53 +00:00
|
|
|
case Q_IMPORT_CHANGELOG:
|
|
|
|
tagcache_import_changelog();
|
|
|
|
break;
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2006-07-10 16:22:03 +00:00
|
|
|
case Q_REBUILD:
|
|
|
|
remove_files();
|
2010-12-06 22:26:31 +00:00
|
|
|
remove(TAGCACHE_FILE_TEMP);
|
2012-07-18 21:36:57 +00:00
|
|
|
tagcache_build();
|
2006-07-10 16:22:03 +00:00
|
|
|
break;
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2006-07-10 16:22:03 +00:00
|
|
|
case Q_UPDATE:
|
2012-07-18 21:36:57 +00:00
|
|
|
tagcache_build();
|
2006-09-26 11:23:18 +00:00
|
|
|
#ifdef HAVE_TC_RAMCACHE
|
|
|
|
load_ramcache();
|
|
|
|
#endif
|
2006-07-10 16:22:03 +00:00
|
|
|
check_deleted_files();
|
2006-03-26 11:33:42 +00:00
|
|
|
break ;
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2006-07-23 15:35:53 +00:00
|
|
|
case Q_START_SCAN:
|
|
|
|
check_done = false;
|
2021-07-24 14:39:01 +00:00
|
|
|
/* fallthrough */
|
2006-03-26 11:33:42 +00:00
|
|
|
case SYS_TIMEOUT:
|
2006-11-10 08:03:33 +00:00
|
|
|
if (check_done || !tc_stat.ready)
|
2006-03-26 11:33:42 +00:00
|
|
|
break ;
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2006-07-10 16:22:03 +00:00
|
|
|
#ifdef HAVE_TC_RAMCACHE
|
2006-11-10 08:03:33 +00:00
|
|
|
if (!tc_stat.ramcache && global_settings.tagcache_ram)
|
2006-07-10 16:22:03 +00:00
|
|
|
{
|
2006-03-26 11:33:42 +00:00
|
|
|
load_ramcache();
|
2006-11-10 08:03:33 +00:00
|
|
|
if (tc_stat.ramcache && global_settings.tagcache_autoupdate)
|
2012-07-18 21:36:57 +00:00
|
|
|
tagcache_build();
|
2006-07-10 16:22:03 +00:00
|
|
|
}
|
|
|
|
else
|
2017-01-18 09:39:35 +00:00
|
|
|
#endif /* HAVE_RC_RAMCACHE */
|
2006-07-10 16:22:03 +00:00
|
|
|
if (global_settings.tagcache_autoupdate)
|
|
|
|
{
|
2012-07-18 21:36:57 +00:00
|
|
|
tagcache_build();
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2008-01-13 19:13:37 +00:00
|
|
|
/* This will be very slow unless dircache is enabled
|
|
|
|
or target is flash based, but do it anyway for
|
|
|
|
consistency. */
|
2007-10-21 11:06:30 +00:00
|
|
|
check_deleted_files();
|
2006-07-10 16:22:03 +00:00
|
|
|
}
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2006-07-22 08:04:28 +00:00
|
|
|
logf("tagcache check done");
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2006-03-26 11:33:42 +00:00
|
|
|
check_done = true;
|
|
|
|
break ;
|
|
|
|
|
|
|
|
case Q_STOP_SCAN:
|
|
|
|
break ;
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2006-03-26 11:33:42 +00:00
|
|
|
case SYS_POWEROFF:
|
|
|
|
break ;
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2006-03-26 11:33:42 +00:00
|
|
|
case SYS_USB_CONNECTED:
|
2006-04-16 17:32:54 +00:00
|
|
|
logf("USB: TagCache");
|
2006-03-26 11:33:42 +00:00
|
|
|
usb_acknowledge(SYS_USB_CONNECTED_ACK);
|
|
|
|
usb_wait_for_disconnect(&tagcache_queue);
|
|
|
|
break ;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-08-05 20:19:10 +00:00
|
|
|
bool tagcache_prepare_shutdown(void)
|
|
|
|
{
|
|
|
|
if (tagcache_get_commit_step() > 0)
|
|
|
|
return false;
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2006-09-23 10:29:14 +00:00
|
|
|
tagcache_stop_scan();
|
|
|
|
while (read_lock || write_lock)
|
|
|
|
sleep(1);
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2006-09-23 10:29:14 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void tagcache_shutdown(void)
|
|
|
|
{
|
2007-07-21 17:35:19 +00:00
|
|
|
/* Flush the command queue. */
|
|
|
|
run_command_queue(true);
|
2021-07-31 14:26:59 +00:00
|
|
|
|
Rewrite filesystem code (WIP)
This patch redoes the filesystem code from the FAT driver up to the
clipboard code in onplay.c.
Not every aspect of this is finished therefore it is still "WIP". I
don't wish to do too much at once (haha!). What is left to do is get
dircache back in the sim and find an implementation for the dircache
indicies in the tagcache and playlist code or do something else that
has the same benefit. Leaving these out for now does not make anything
unusable. All the basics are done.
Phone app code should probably get vetted (and app path handling
just plain rewritten as environment expansions); the SDL app and
Android run well.
Main things addressed:
1) Thread safety: There is none right now in the trunk code. Most of
what currently works is luck when multiple threads are involved or
multiple descriptors to the same file are open.
2) POSIX compliance: Many of the functions behave nothing like their
counterparts on a host system. This leads to inconsistent code or very
different behavior from native to hosted. One huge offender was
rename(). Going point by point would fill a book.
3) Actual running RAM usage: Many targets will use less RAM and less
stack space (some more RAM because I upped the number of cache buffers
for large memory). There's very little memory lying fallow in rarely-used
areas (see 'Key core changes' below). Also, all targets may open the same
number of directory streams whereas before those with less than 8MB RAM
were limited to 8, not 12 implying those targets will save slightly
less.
4) Performance: The test_disk plugin shows markedly improved performance,
particularly in the area of (uncached) directory scanning, due partly to
more optimal directory reading and to a better sector cache algorithm.
Uncached times tend to be better while there is a bit of a slowdown in
dircache due to it being a bit heavier of an implementation. It's not
noticeable by a human as far as I can say.
Key core changes:
1) Files and directories share core code and data structures.
2) The filesystem code knows which descriptors refer to same file.
This ensures that changes from one stream are appropriately reflected
in every open descriptor for that file (fileobj_mgr.c).
3) File and directory cache buffers are borrowed from the main sector
cache. This means that when they are not in use by a file, they are not
wasted, but used for the cache. Most of the time, only a few of them
are needed. It also means that adding more file and directory handles
is less expensive. All one must do in ensure a large enough cache to
borrow from.
4) Relative path components are supported and the namespace is unified.
It does not support full relative paths to an implied current directory;
what is does support is use of "." and "..". Adding the former would
not be very difficult. The namespace is unified in the sense that
volumes may be specified several times along with relative parts, e.g.:
"/<0>/foo/../../<1>/bar" :<=> "/<1>/bar".
5) Stack usage is down due to sharing of data, static allocation and
less duplication of strings on the stack. This requires more
serialization than I would like but since the number of threads is
limited to a low number, the tradoff in favor of the stack seems
reasonable.
6) Separates and heirarchicalizes (sic) the SIM and APP filesystem
code. SIM path and volume handling is just like the target. Some
aspects of the APP file code get more straightforward (e.g. no path
hashing is needed).
Dircache:
Deserves its own section. Dircache is new but pays homage to the old.
The old one was not compatible and so it, since it got redone, does
all the stuff it always should have done such as:
1) It may be update and used at any time during the build process.
No longer has one to wait for it to finish building to do basic file
management (create, remove, rename, etc.).
2) It does not need to be either fully scanned or completely disabled;
it can be incomplete (i.e. overfilled, missing paths), still be
of benefit and be correct.
3) Handles mounting and dismounting of individual volumes which means
a full rebuild is not needed just because you pop a new SD card in the
slot. Now, because it reuses its freed entry data, may rebuild only
that volume.
4) Much more fundamental to the file code. When it is built, it is
the keeper of the master file list whether enabled or not ("disabled"
is just a state of the cache). Its must always to ready to be started
and bind all streams opened prior to being enabled.
5) Maintains any short filenames in OEM format which means that it does
not need to be rebuilt when changing the default codepage.
Miscellaneous Compatibility:
1) Update any other code that would otherwise not work such as the
hotswap mounting code in various card drivers.
2) File management: Clipboard needed updating because of the behavioral
changes. Still needs a little more work on some finer points.
3) Remove now-obsolete functionality such as the mutex's "no preempt"
flag (which was only for the prior FAT driver).
4) struct dirinfo uses time_t rather than raw FAT directory entry
time fields. I plan to follow up on genericizing everything there
(i.e. no FAT attributes).
5) unicode.c needed some redoing so that the file code does not try
try to load codepages during a scan, which is actually a problem with
the current code. The default codepage, if any is required, is now
kept in RAM separarately (bufalloced) from codepages specified to
iso_decode() (which must not be bufalloced because the conversion
may be done by playback threads).
Brings with it some additional reusable core code:
1) Revised file functions: Reusable code that does things such as
safe path concatenation and parsing without buffer limitations or
data duplication. Variants that copy or alter the input path may be
based off these.
To do:
1) Put dircache functionality back in the sim. Treating it internally
as a different kind of file system seems the best approach at this
time.
2) Restore use of dircache indexes in the playlist and database or
something effectively the same. Since the cache doesn't have to be
complete in order to be used, not getting a hit on the cache doesn't
unambiguously say if the path exists or not.
Change-Id: Ia30f3082a136253e3a0eae0784e3091d138915c8
Reviewed-on: http://gerrit.rockbox.org/566
Reviewed-by: Michael Sevakis <jethead71@rockbox.org>
Tested: Michael Sevakis <jethead71@rockbox.org>
2013-08-06 02:02:45 +00:00
|
|
|
#if defined(HAVE_EEPROM_SETTINGS) && defined(HAVE_TC_RAMCACHE)
|
2006-11-10 08:03:33 +00:00
|
|
|
if (tc_stat.ramcache)
|
2006-08-05 20:19:10 +00:00
|
|
|
tagcache_dumpsave();
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2006-04-23 18:47:26 +00:00
|
|
|
static int get_progress(void)
|
2006-03-26 11:33:42 +00:00
|
|
|
{
|
2006-04-19 18:56:59 +00:00
|
|
|
int total_count = -1;
|
2006-03-26 11:33:42 +00:00
|
|
|
|
|
|
|
#ifdef HAVE_DIRCACHE
|
2017-01-18 09:39:35 +00:00
|
|
|
struct dircache_info dcinfo;
|
|
|
|
dircache_get_info(&dcinfo);
|
|
|
|
if (dcinfo.status != DIRCACHE_IDLE)
|
2006-03-26 11:33:42 +00:00
|
|
|
{
|
2017-01-18 09:39:35 +00:00
|
|
|
total_count = dcinfo.entry_count;
|
2006-03-26 11:33:42 +00:00
|
|
|
}
|
|
|
|
else
|
2017-01-18 09:39:35 +00:00
|
|
|
#endif /* HAVE_DIRCACHE */
|
2006-07-10 16:22:03 +00:00
|
|
|
#ifdef HAVE_TC_RAMCACHE
|
2006-03-26 11:33:42 +00:00
|
|
|
{
|
2017-01-18 09:39:35 +00:00
|
|
|
if (tcramcache.hdr && tc_stat.ramcache)
|
2011-06-19 14:36:26 +00:00
|
|
|
total_count = current_tcmh.tch.entry_count;
|
2006-03-26 11:33:42 +00:00
|
|
|
}
|
2017-01-18 09:39:35 +00:00
|
|
|
#endif /* HAVE_TC_RAMCACHE */
|
2006-03-26 11:33:42 +00:00
|
|
|
|
|
|
|
if (total_count < 0)
|
|
|
|
return -1;
|
2017-01-18 09:39:35 +00:00
|
|
|
else if (total_count == 0)
|
|
|
|
return 0;
|
|
|
|
else if (processed_dir_count > total_count)
|
|
|
|
return 100;
|
|
|
|
else
|
|
|
|
return processed_dir_count * 100 / total_count;
|
2006-03-26 11:33:42 +00:00
|
|
|
}
|
|
|
|
|
2006-04-23 18:47:26 +00:00
|
|
|
struct tagcache_stat* tagcache_get_stat(void)
|
2006-04-19 18:56:59 +00:00
|
|
|
{
|
2006-11-10 08:03:33 +00:00
|
|
|
tc_stat.progress = get_progress();
|
|
|
|
tc_stat.processed_entries = processed_dir_count;
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2006-11-10 08:03:33 +00:00
|
|
|
return &tc_stat;
|
2006-04-19 18:56:59 +00:00
|
|
|
}
|
2006-11-10 08:03:33 +00:00
|
|
|
|
2006-03-26 11:33:42 +00:00
|
|
|
void tagcache_start_scan(void)
|
|
|
|
{
|
|
|
|
queue_post(&tagcache_queue, Q_START_SCAN, 0);
|
|
|
|
}
|
|
|
|
|
2006-07-10 16:22:03 +00:00
|
|
|
bool tagcache_update(void)
|
|
|
|
{
|
2006-11-10 08:03:33 +00:00
|
|
|
if (!tc_stat.ready)
|
2006-07-20 12:19:31 +00:00
|
|
|
return false;
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2006-07-10 16:22:03 +00:00
|
|
|
queue_post(&tagcache_queue, Q_UPDATE, 0);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2007-03-07 19:56:15 +00:00
|
|
|
bool tagcache_rebuild()
|
2006-03-26 11:33:42 +00:00
|
|
|
{
|
2006-07-10 16:22:03 +00:00
|
|
|
queue_post(&tagcache_queue, Q_REBUILD, 0);
|
2006-03-26 11:33:42 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void tagcache_stop_scan(void)
|
|
|
|
{
|
|
|
|
queue_post(&tagcache_queue, Q_STOP_SCAN, 0);
|
|
|
|
}
|
|
|
|
|
2006-11-10 08:03:33 +00:00
|
|
|
#endif /* !__PCTOOL__ */
|
|
|
|
|
2006-04-23 18:47:26 +00:00
|
|
|
|
2006-03-26 11:33:42 +00:00
|
|
|
void tagcache_init(void)
|
|
|
|
{
|
2006-11-10 08:03:33 +00:00
|
|
|
memset(&tc_stat, 0, sizeof(struct tagcache_stat));
|
2007-02-25 20:41:51 +00:00
|
|
|
memset(¤t_tcmh, 0, sizeof(struct master_header));
|
2006-07-15 17:36:25 +00:00
|
|
|
filenametag_fd = -1;
|
2006-08-02 17:39:34 +00:00
|
|
|
write_lock = read_lock = 0;
|
2021-07-31 14:26:59 +00:00
|
|
|
|
2006-11-10 08:03:33 +00:00
|
|
|
#ifndef __PCTOOL__
|
2007-07-21 17:35:19 +00:00
|
|
|
mutex_init(&command_queue_mutex);
|
2006-09-16 16:18:11 +00:00
|
|
|
queue_init(&tagcache_queue, true);
|
2006-03-26 11:33:42 +00:00
|
|
|
create_thread(tagcache_thread, tagcache_stack,
|
2021-07-31 14:26:59 +00:00
|
|
|
sizeof(tagcache_stack), 0, tagcache_thread_name
|
2007-03-04 20:06:41 +00:00
|
|
|
IF_PRIO(, PRIORITY_BACKGROUND)
|
2010-02-07 18:38:47 +00:00
|
|
|
IF_COP(, CPU));
|
2006-11-10 08:03:33 +00:00
|
|
|
#else
|
|
|
|
tc_stat.initialized = true;
|
|
|
|
allocate_tempbuf();
|
|
|
|
commit();
|
|
|
|
free_tempbuf();
|
|
|
|
tc_stat.ready = check_all_headers();
|
|
|
|
#endif
|
2006-03-26 11:33:42 +00:00
|
|
|
}
|
|
|
|
|
2006-11-19 19:11:30 +00:00
|
|
|
#ifdef __PCTOOL__
|
|
|
|
void tagcache_reverse_scan(void)
|
|
|
|
{
|
|
|
|
logf("Checking for deleted files");
|
|
|
|
check_deleted_files();
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2006-04-12 10:31:24 +00:00
|
|
|
bool tagcache_is_initialized(void)
|
|
|
|
{
|
2006-11-10 08:03:33 +00:00
|
|
|
return tc_stat.initialized;
|
2006-04-12 10:31:24 +00:00
|
|
|
}
|
2011-01-17 22:28:36 +00:00
|
|
|
bool tagcache_is_fully_initialized(void)
|
|
|
|
{
|
|
|
|
return tc_stat.readyvalid;
|
|
|
|
}
|
2007-03-01 11:14:46 +00:00
|
|
|
bool tagcache_is_usable(void)
|
|
|
|
{
|
|
|
|
return tc_stat.initialized && tc_stat.ready;
|
|
|
|
}
|
2006-04-12 10:31:24 +00:00
|
|
|
int tagcache_get_commit_step(void)
|
|
|
|
{
|
2006-11-10 08:03:33 +00:00
|
|
|
return tc_stat.commit_step;
|
2006-04-12 10:31:24 +00:00
|
|
|
}
|
2007-03-07 19:56:15 +00:00
|
|
|
int tagcache_get_max_commit_step(void)
|
|
|
|
{
|
2009-06-03 08:19:32 +00:00
|
|
|
return (int)(SORTED_TAGS_COUNT)+1;
|
2007-03-07 19:56:15 +00:00
|
|
|
}
|