* This file is part of DisOrder
* Copyright (C) 2005-2008 Richard Kettlewell
*
- * This program is free software; you can redistribute it and/or modify
+ * 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
+ * the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful, but
- * WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * General Public License for more details.
- *
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
* You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
- * USA
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/** @file lib/trackdb.c
* @brief Track database
* This file is getting in desparate need of splitting up...
*/
-#include <config.h>
-#include "types.h"
+#include "common.h"
-#include <string.h>
-#include <stdio.h>
#include <db.h>
#include <sys/socket.h>
#include <pcre.h>
-#include <assert.h>
#include <unistd.h>
#include <errno.h>
#include <stddef.h>
#include <sys/resource.h>
#include <time.h>
#include <arpa/inet.h>
-#include <sys/wait.h>
#include <dirent.h>
#include <sys/stat.h>
#include <gcrypt.h>
#include "unicode.h"
#include "unidata.h"
#include "base64.h"
+#include "sendmail.h"
#define RESCAN "disorder-rescan"
#define DEADLOCK "disorder-deadlock"
/* setup and teardown ********************************************************/
-static const char *home; /* home had better not change */
-DB_ENV *trackdb_env; /* db environment */
+/** @brief Database home directory
+ *
+ * All database files live below here. It had better never change.
+ */
+static const char *home;
+
+/** @brief Database environment */
+DB_ENV *trackdb_env;
/** @brief The tracks database
* - Keys are UTF-8(NFC(unicode(path name)))
*/
DB *trackdb_noticeddb; /* when track noticed */
+/** @brief The schedule database
+ *
+ * - Keys are ID strings, generated at random
+ * - Values are encoded key-value pairs
+ * - There can be more than one value per key
+ * - Data cannot be reconstructed
+ *
+ * See @ref server/schedule.c for further information.
+ */
+DB *trackdb_scheduledb;
+
/** @brief The user database
* - Keys are usernames
* - Values are encoded key-value pairs
*/
DB *trackdb_usersdb;
-static pid_t db_deadlock_pid = -1; /* deadlock manager PID */
-static pid_t rescan_pid = -1; /* rescanner PID */
-static int initialized, opened; /* state */
+/** @brief The playlists database
+ * - Keys are playlist names
+ * - Values are encoded key-value pairs
+ * - Data is user data and cannot be reconstructed
+ */
+DB *trackdb_playlistsdb;
+
+/** @brief Deadlock manager PID */
+static pid_t db_deadlock_pid = -1;
+
+/** @brief Rescanner PID */
+static pid_t rescan_pid = -1;
+
+/** @brief Set when the database environment exists */
+static int initialized;
+
+/** @brief Set when databases are open */
+static int opened;
+
+/** @brief Current stats subprocess PIDs */
+static hash *stats_pids;
-/* comparison function for keys */
+/** @brief PID of current random track chooser (disorder-choose) */
+static pid_t choose_pid = -1;
+
+/** @brief Our end of pipe from disorder-choose */
+static int choose_fd;
+
+/** @brief Callback to supply random track to */
+static random_callback *choose_callback;
+
+/** @brief Accumulator for output from disorder-choose */
+static struct dynstr choose_output;
+
+/** @brief Current completion status of disorder-choose
+ * A bitmap of @ref CHOOSE_READING and @ref CHOOSE_RUNNING.
+ */
+static unsigned choose_complete;
+
+/* @brief Exit status from disorder-choose */
+static int choose_status;
+
+/** @brief disorder-choose process is running */
+#define CHOOSE_RUNNING 1
+
+/** @brief disorder-choose pipe is still open */
+#define CHOOSE_READING 2
+
+/** @brief Comparison function for filename-based keys */
static int compare(DB attribute((unused)) *db_,
const DBT *a, const DBT *b) {
return compare_path_raw(a->data, a->size, b->data, b->size);
}
+/** @brief Test whether the track database can be read
+ * @return 1 if it can, 0 if it cannot
+ */
+int trackdb_readable(void) {
+ char *usersdb;
+
+ byte_xasprintf(&usersdb, "%s/users.db", config->home);
+ return access(usersdb, R_OK) == 0;
+}
+
/** @brief Open database environment
* @param flags Flags word
*
D(("initialized database environment"));
}
-/* called when deadlock manager terminates */
+/** @brief Called when deadlock manager terminates */
static int reap_db_deadlock(ev_source attribute((unused)) *ev,
pid_t attribute((unused)) pid,
int status,
return 0;
}
+/** @brief Start a subprogram
+ * @param ev Event loop
+ * @param outputfd File descriptor to redirect @c stdout to, or -1
+ * @param prog Program name
+ * @param ... Arguments
+ * @return PID
+ *
+ * Starts a subprocess. Adds the following arguments:
+ * - @c --config to ensure the right config file is used
+ * - @c --debug or @c --no-debug to match debug settings
+ * - @c --syslog or @c --no-syslog to match log settings
+ */
static pid_t subprogram(ev_source *ev, int outputfd, const char *prog,
...) {
pid_t pid;
return pid;
}
-/* start deadlock manager */
+/** @brief Start deadlock manager
+ * @param ev Event loop
+ *
+ * Called from the main server (only).
+ */
void trackdb_master(ev_source *ev) {
assert(db_deadlock_pid == -1);
db_deadlock_pid = subprogram(ev, -1, DEADLOCK, (char *)0);
D(("started deadlock manager"));
}
-/* close environment */
-void trackdb_deinit(void) {
+/** @brief Kill a subprocess and wait for it to terminate
+ * @param ev Event loop or NULL
+ * @param pid Process ID or -1
+ * @param what Description of subprocess
+ *
+ * Used during trackdb_deinit(). This function blocks so don't use it for
+ * normal teardown as that will hang the server.
+ */
+static void terminate_and_wait(ev_source *ev,
+ pid_t pid,
+ const char *what) {
+ int err;
+
+ if(pid == -1)
+ return;
+ if(kill(pid, SIGTERM) < 0)
+ fatal(errno, "error killing %s", what);
+ /* wait for the rescanner to finish */
+ while(waitpid(pid, &err, 0) == -1 && errno == EINTR)
+ ;
+ if(ev)
+ ev_child_cancel(ev, pid);
+}
+
+/** @brief Close database environment
+ * @param ev Event loop
+ */
+void trackdb_deinit(ev_source *ev) {
int err;
/* sanity checks */
if((err = trackdb_env->close(trackdb_env, 0)))
fatal(0, "trackdb_env->close: %s", db_strerror(err));
- if(rescan_pid != -1) {
- /* shut down the rescanner */
- if(kill(rescan_pid, SIGTERM) < 0)
- fatal(errno, "error killing rescanner");
- /* wait for the rescanner to finish */
- while(waitpid(rescan_pid, &err, 0) == -1 && errno == EINTR)
- ;
- }
+ terminate_and_wait(ev, rescan_pid, "disorder-rescan");
+ rescan_pid = -1;
+ terminate_and_wait(ev, choose_pid, "disorder-choose");
+ choose_pid = -1;
- /* TODO kill any stats subprocesses */
+ if(stats_pids) {
+ char **ks = hash_keys(stats_pids);
- /* finally terminate the deadlock manager */
- if(db_deadlock_pid != -1 && kill(db_deadlock_pid, SIGTERM) < 0)
- fatal(errno, "error killing deadlock manager");
- db_deadlock_pid = -1;
+ while(*ks) {
+ pid_t pid = atoi(*ks++);
+ terminate_and_wait(ev, pid, "disorder-stats");
+ }
+ stats_pids = NULL;
+ }
+ terminate_and_wait(ev, db_deadlock_pid, "disorder-deadlock");
+ db_deadlock_pid = -1;
D(("deinitialized database environment"));
}
-/* open a specific database */
+/** @brief Open a specific database
+ * @param path Relative path to database
+ * @param dbflags Database flags: DB_DUP, DB_DUPSORT, etc
+ * @param dbtype Database type: DB_HASH, DB_BTREE, etc
+ * @param openflags Open flags: DB_RDONLY, DB_CREATE, etc
+ * @param mode Permission mask: usually 0666
+ * @return Database handle
+ */
static DB *open_db(const char *path,
u_int32_t dbflags,
DBTYPE dbtype,
u_int32_t openflags,
int mode) {
- int err;
+ int err, err2;
DB *db;
D(("open %s", path));
fatal(0, "db->set_bt_compare %s: %s", path, db_strerror(err));
if((err = db->open(db, 0, path, 0, dbtype,
openflags | DB_AUTO_COMMIT, mode))) {
- if((openflags & DB_CREATE) || errno != ENOENT)
+ if((openflags & DB_CREATE) || errno != ENOENT) {
+ if((err2 = db->close(db, 0)))
+ error(0, "db->close: %s", db_strerror(err2));
+ trackdb_close();
+ trackdb_env->close(trackdb_env,0);
+ trackdb_env = 0;
fatal(0, "db->open %s: %s", path, db_strerror(err));
+ }
db->close(db, 0);
db = 0;
}
trackdb_globaldb = open_db("global.db", 0, DB_HASH, dbflags, 0666);
trackdb_noticeddb = open_db("noticed.db",
DB_DUPSORT, DB_BTREE, dbflags, 0666);
- if(!trackdb_existing_database) {
+ trackdb_scheduledb = open_db("schedule.db", 0, DB_HASH, dbflags, 0666);
+ trackdb_playlistsdb = open_db("playlists.db", 0, DB_HASH, dbflags, 0666);
+ if(!trackdb_existing_database && !(flags & TRACKDB_READ_ONLY)) {
/* Stash the database version */
char buf[32];
D(("opened databases"));
}
-/* close track databases */
+/** @brief Close track databases */
void trackdb_close(void) {
int err;
/* sanity checks */
assert(opened == 1);
--opened;
- if((err = trackdb_tracksdb->close(trackdb_tracksdb, 0)))
- fatal(0, "error closing tracks.db: %s", db_strerror(err));
- if((err = trackdb_searchdb->close(trackdb_searchdb, 0)))
- fatal(0, "error closing search.db: %s", db_strerror(err));
- if((err = trackdb_tagsdb->close(trackdb_tagsdb, 0)))
- fatal(0, "error closing tags.db: %s", db_strerror(err));
- if((err = trackdb_prefsdb->close(trackdb_prefsdb, 0)))
- fatal(0, "error closing prefs.db: %s", db_strerror(err));
- if((err = trackdb_globaldb->close(trackdb_globaldb, 0)))
- fatal(0, "error closing global.db: %s", db_strerror(err));
- if((err = trackdb_noticeddb->close(trackdb_noticeddb, 0)))
- fatal(0, "error closing noticed.db: %s", db_strerror(err));
- if((err = trackdb_usersdb->close(trackdb_usersdb, 0)))
- fatal(0, "error closing users.db: %s", db_strerror(err));
- trackdb_tracksdb = trackdb_searchdb = trackdb_prefsdb = 0;
- trackdb_tagsdb = trackdb_globaldb = 0;
+#define CLOSE(N, V) do { \
+ if(V && (err = V->close(V, 0))) \
+ fatal(0, "error closing %s: %s", N, db_strerror(err)); \
+ V = 0; \
+} while(0)
+ CLOSE("tracks.db", trackdb_tracksdb);
+ CLOSE("search.db", trackdb_searchdb);
+ CLOSE("tags.db", trackdb_tagsdb);
+ CLOSE("prefs.db", trackdb_prefsdb);
+ CLOSE("global.db", trackdb_globaldb);
+ CLOSE("noticed.db", trackdb_noticeddb);
+ CLOSE("schedule.db", trackdb_scheduledb);
+ CLOSE("users.db", trackdb_usersdb);
+ CLOSE("playlists.db", trackdb_playlistsdb);
D(("closed databases"));
}
/* generic db routines *******************************************************/
-/* fetch and decode a database entry. Returns 0, DB_NOTFOUND or
- * DB_LOCK_DEADLOCK. */
+/** @brief Fetch and decode a database entry
+ * @param db Database
+ * @param track Track name
+ * @param kp Where to put decoded list (or NULL if you don't care)
+ * @param tid Owning transaction
+ * @return 0, @c DB_NOTFOUND or @c DB_LOCK_DEADLOCK
+ */
int trackdb_getdata(DB *db,
const char *track,
struct kvp **kp,
switch(err = db->get(db, tid, make_key(&key, track),
prepare_data(&data), 0)) {
case 0:
- *kp = kvp_urldecode(data.data, data.size);
+ if(kp)
+ *kp = kvp_urldecode(data.data, data.size);
return 0;
case DB_NOTFOUND:
- *kp = 0;
+ if(kp)
+ *kp = 0;
return err;
case DB_LOCK_DEADLOCK:
error(0, "error querying database: %s", db_strerror(err));
}
}
-/* encode and store a database entry. Returns 0, DB_KEYEXIST or
- * DB_LOCK_DEADLOCK. */
+/** @brief Encode and store a database entry
+ * @param db Database
+ * @param track Track name
+ * @param k List of key/value pairs to store
+ * @param tid Owning transaction
+ * @param flags DB flags e.g. DB_NOOVERWRITE
+ * @return 0, DB_KEYEXIST or DB_LOCK_DEADLOCK
+ */
int trackdb_putdata(DB *db,
const char *track,
const struct kvp *k,
}
}
-/* open a database cursor */
+/** @brief Open a database cursor
+ * @param db Database
+ * @param tid Owning transaction
+ * @return Cursor
+ */
DBC *trackdb_opencursor(DB *db, DB_TXN *tid) {
int err;
DBC *c;
return c;
}
-/* close a database cursor; returns 0 or DB_LOCK_DEADLOCK */
+/** @brief Close a database cursor
+ * @param c Cursor
+ * @return 0 or DB_LOCK_DEADLOCK
+ */
int trackdb_closecursor(DBC *c) {
int err;
}
}
-/* delete a (key,data) pair. Returns 0, DB_NOTFOUND or DB_LOCK_DEADLOCK. */
+/** @brief Delete a key/data pair
+ * @param db Database
+ * @param word Key
+ * @param track Data
+ * @param tid Owning transaction
+ * @return 0, DB_NOTFOUND or DB_LOCK_DEADLOCK
+ *
+ * Used by the search and tags databases, hence the odd parameter names.
+ * See also register_word().
+ */
int trackdb_delkeydata(DB *db,
const char *word,
const char *track,
return err;
}
-/* start a transaction */
+/** @brief Start a transaction
+ * @return Transaction
+ */
DB_TXN *trackdb_begin_transaction(void) {
DB_TXN *tid;
int err;
return tid;
}
-/* abort transaction */
+/** @brief Abort transaction
+ * @param tid Transaction (or NULL)
+ *
+ * If @p tid is NULL then nothing happens.
+ */
void trackdb_abort_transaction(DB_TXN *tid) {
int err;
fatal(0, "tid->abort: %s", db_strerror(err));
}
-/* commit transaction */
+/** @brief Commit transaction
+ * @param tid Transaction (must not be NULL)
+ */
void trackdb_commit_transaction(DB_TXN *tid) {
int err;
/* search/tags shared code ***************************************************/
-/* comparison function used by dedupe() */
+/** @brief Comparison function used by dedupe()
+ * @param a Pointer to first key
+ * @param b Pointer to second key
+ * @return -1, 0 or 1
+ *
+ * Passed to qsort().
+ */
static int wordcmp(const void *a, const void *b) {
return strcmp(*(const char **)a, *(const char **)b);
}
-/* sort and de-dupe VEC */
+/** @brief Sort and de-duplicate @p vec
+ * @param vec Vector to sort
+ * @param nvec Length of @p vec
+ * @return @p vec
+ *
+ * The returned vector is NULL-terminated, and there must be room for this NULL
+ * even if there are no duplicates (i.e. it must have more than @p nvec
+ * elements.)
+ */
static char **dedupe(char **vec, int nvec) {
int m, n;
return vec;
}
-/* update a key/track database. Returns 0 or DB_DEADLOCK. */
+/** @brief Store a key/data pair
+ * @param db Database
+ * @param what Description
+ * @param track Data
+ * @param word Key
+ * @param tid Owning transaction
+ * @return 0 or DB_DEADLOCK
+ *
+ * Used by the search and tags databases, hence the odd parameter names.
+ * See also trackdb_delkeydata().
+ */
static int register_word(DB *db, const char *what,
const char *track, const char *word,
DB_TXN *tid) {
/* search primitives *********************************************************/
-/* return true iff NAME is a trackname_display_ pref */
+/** @brief Return true iff @p name is a trackname_display_ pref
+ * @param name Preference name
+ * @return Non-zero iff @p name is a trackname_display_ pref
+ */
static int is_display_pref(const char *name) {
static const char prefix[] = "trackname_display_";
return !strncmp(name, prefix, (sizeof prefix) - 1);
}
-/** @brief Word_Break property tailor that treats underscores as spaces */
+/** @brief Word_Break property tailor that treats underscores as spaces
+ * @param c Code point
+ * @return Tailored property or -1 to use standard value
+ *
+ * Passed to utf32_word_split() when splitting a track name into words.
+ * See word_split() and @ref unicode_property_tailor.
+ */
static int tailor_underscore_Word_Break_Other(uint32_t c) {
switch(c) {
default:
return t - start;
}
-/** @brief Normalize and split a string using a given tailoring */
+/** @brief Normalize and split a string using a given tailoring
+ * @param v Where to store words from string
+ * @param s Input string
+ * @param pt Word_Break property tailor, or NULL
+ *
+ * The output words will be:
+ * - case-folded
+ * - have any combination characters stripped
+ * - not include any word break code points (as tailored)
+ *
+ * Used by track_to_words(), with @p pt set to @ref
+ * tailor_underscore_Word_Break_Other, and by normalize_tag() with no
+ * tailoring.
+ */
static void word_split(struct vector *v,
const char *s,
unicode_property_tailor *pt) {
return d->vec;
}
-/* compute the words of a track name */
+/** @brief Compute the words of a track name
+ * @param track Track name
+ * @param p Preferences (for display prefs)
+ * @return NULL-terminated, de-duplicated list or words
+ */
static char **track_to_words(const char *track,
const struct kvp *p) {
struct vector v;
return dedupe(v.vec, v.nvec);
}
-/* return nonzero iff WORD is a stopword */
+/** @brief Test for a stopword
+ * @param word Word
+ * @return Non-zero if @p word is a stopword
+ */
static int stopword(const char *word) {
int n;
return n < config->stopword.n;
}
-/* record that WORD appears in TRACK. Returns 0 or DB_LOCK_DEADLOCK. */
+/** @brief Register a search term
+ * @param track Track name
+ * @param word A word that appears in the name of @p track
+ * @param tid Owning transaction
+ * @return 0 or DB_LOCK_DEADLOCK
+ */
static int register_search_word(const char *track, const char *word,
DB_TXN *tid) {
if(stopword(word)) return 0;
/* Tags **********************************************************************/
-/* Return nonzero if C is a valid tag character */
+/** @brief Test for tag characters
+ * @param c Character
+ * @return Non-zero if @p c is a tag character
+ *
+ * The current rule is that commas and the control characters 0-31 are not
+ * allowed but anything else is permitted. This is arguably a bit loose.
+ */
static int tagchar(int c) {
switch(c) {
case ',':
}
}
-/* Parse and de-dupe a tag list. If S=0 then assumes "". */
+/** @brief Parse a tag list
+ * @param s Tag list or NULL (equivalent to "")
+ * @return Parsed tag list
+ *
+ * The tags will be normalized (as per normalize_tag()) and de-duplicated.
+ */
char **parsetags(const char *s) {
const char *t;
struct vector v;
return dedupe(v.vec, v.nvec);
}
-/* Record that TRACK has TAG. Returns 0 or DB_LOCK_DEADLOCK. */
+/** @brief Register a tag
+ * @param track Track name
+ * @param tag Tag name
+ * @param tid Owning transaction
+ * @return 0 or DB_LOCK_DEADLOCK
+ */
static int register_tag(const char *track, const char *tag, DB_TXN *tid) {
return register_word(trackdb_tagsdb, "tags", track, tag, tid);
}
int err, n;
struct kvp *t, *a, *p;
int t_changed, ret;
- char *alias, **w;
+ char *alias, **w, *noticed;
+ time_t now;
/* notice whether the tracks.db entry changes */
t_changed = 0;
/* this is a real track */
t_changed += kvp_set(&t, "_alias_for", 0);
t_changed += kvp_set(&t, "_path", path);
+ xtime(&now);
+ if(ret == DB_NOTFOUND) {
+ /* It's a new track; record the time */
+ byte_xasprintf(¬iced, "%lld", (long long)now);
+ t_changed += kvp_set(&t, "_noticed", noticed);
+ }
/* if we have an alias record it in the database */
if((err = compute_alias(&alias, track, p, tid))) return err;
if(alias) {
return err;
if(ret == DB_NOTFOUND) {
uint32_t timestamp[2];
- time_t now;
DBT key, data;
- time(&now);
timestamp[0] = htonl((uint64_t)now >> 32);
timestamp[1] = htonl((uint32_t)now);
memset(&key, 0, sizeof key);
}
static int stats_finished(ev_source attribute((unused)) *ev,
- pid_t attribute((unused)) pid,
+ pid_t pid,
int status,
const struct rusage attribute((unused)) *rusage,
void *u) {
if(status)
error(0, "disorder-stats %s", wstat(status));
stats_complete(d);
+ char *k;
+ byte_xasprintf(&k, "%lu", (unsigned long)pid);
+ hash_remove(stats_pids, k);
return 0;
}
pid = subprogram(ev, p[1], "disorder-stats", (char *)0);
xclose(p[1]);
ev_child(ev, pid, 0, stats_finished, d);
- ev_reader_new(ev, p[0], stats_read, stats_error, d, "disorder-stats reader");
+ if(!ev_reader_new(ev, p[0], stats_read, stats_error, d,
+ "disorder-stats reader"))
+ fatal(0, "ev_reader_new for disorder-stats reader failed");
+ /* Remember the PID */
+ if(!stats_pids)
+ stats_pids = hash_new(1);
+ char *k;
+ byte_xasprintf(&k, "%lu", (unsigned long)pid);
+ hash_add(stats_pids, k, "", HASH_INSERT);
+}
+
+/** @brief Parse a track name part preference
+ * @param name Preference name
+ * @param partp Where to store part name
+ * @param contextp Where to store context name
+ * @return 0 on success, non-0 if parse fails
+ */
+static int trackdb__parse_namepref(const char *name,
+ char **partp,
+ char **contextp) {
+ char *c;
+ static const char prefix[] = "trackname_";
+
+ if(strncmp(name, prefix, strlen(prefix)))
+ return -1; /* not trackname_* at all */
+ name += strlen(prefix);
+ /* There had better be a _ between context and part */
+ c = strchr(name, '_');
+ if(!c)
+ return -1;
+ /* Context is first in the pref name even though most APIs have the part
+ * first. Confusing; sorry. */
+ *contextp = xstrndup(name, c - name);
+ ++c;
+ /* There had better NOT be a second _ */
+ if(strchr(c, '_'))
+ return -1;
+ *partp = xstrdup(c);
+ return 0;
+}
+
+/** @brief Compute the default value for a track preference
+ * @param track Track name
+ * @param name Preference name
+ * @return Default value or 0 if none/not known
+ */
+static const char *trackdb__default(const char *track, const char *name) {
+ char *context, *part;
+
+ if(!trackdb__parse_namepref(name, &part, &context)) {
+ /* We can work out the default for a trackname_ pref */
+ return trackname_part(track, context, part);
+ } else if(!strcmp(name, "weight")) {
+ /* We know the default weight */
+ return "90000";
+ } else if(!strcmp(name, "pick_at_random")) {
+ /* By default everything is eligible for picking at random */
+ return "1";
+ } else if(!strcmp(name, "tags")) {
+ /* By default everything no track has any tags */
+ return "";
+ }
+ return 0;
}
/* set a pref (remove if value=0) */
DB_TXN *tid;
int err, cmp;
char *oldalias, *newalias, **oldtags = 0, **newtags;
+ const char *def;
+ /* If the value matches the default then unset instead, to keep the database
+ * tidy. Older versions did not have this feature so your database may yet
+ * have some default values stored in it. */
if(value) {
- /* TODO: if value matches default then set value=0 */
+ def = trackdb__default(track, name);
+ if(def && !strcmp(value, def))
+ value = 0;
}
for(;;) {
return 0;
}
-static pid_t choose_pid = -1;
-static int choose_fd;
-static random_callback *choose_callback;
-static struct dynstr choose_output;
-static unsigned choose_complete;
-static int choose_status;
-#define CHOOSE_RUNNING 1
-#define CHOOSE_READING 2
-
static void choose_finished(ev_source *ev, unsigned which) {
choose_complete |= which;
if(choose_complete != (CHOOSE_RUNNING|CHOOSE_READING))
choose_callback = callback;
choose_output.nvec = 0;
choose_complete = 0;
- ev_reader_new(ev, p[0], choose_readable, choose_read_error, 0,
- "disorder-choose reader"); /* owns p[0] */
+ if(!ev_reader_new(ev, p[0], choose_readable, choose_read_error, 0,
+ "disorder-choose reader")) /* owns p[0] */
+ fatal(0, "ev_reader_new for disorder-choose reader failed");
ev_child(ev, choose_pid, 0, choose_exited, 0); /* owns the subprocess */
return 0;
}
char *ptr;
int err;
size_t l, last_dir_len = 0;
- char *last_dir = 0, *track, *alias;
+ char *last_dir = 0, *track;
struct kvp *p;
dl = strlen(dir);
if((err = trackdb_getdata(trackdb_prefsdb,
track, &p, tid)) == DB_LOCK_DEADLOCK)
goto deadlocked;
+ /* There's an awkward question here...
+ *
+ * If a track shares a directory with its alias then we could
+ * do one of three things:
+ * - report both. Looks ridiculuous in most UIs.
+ * - report just the alias. Remarkably inconvenient to write
+ * UI code for!
+ * - report just the real name. Ugly if the UI doesn't prettify
+ * names via the name parts.
+ */
+#if 1
+ /* If this file is an alias for a track in the same directory then we
+ * skip it */
+ struct kvp *t = kvp_urldecode(d.data, d.size);
+ const char *alias_target = kvp_get(t, "_alias_for");
+ if(!(alias_target
+ && !strcmp(d_dirname(alias_target),
+ d_dirname(track))))
+ if(track_matches(dl, k.data, k.size, re))
+ vector_append(v, track);
+#else
/* if this file has an alias in the same directory then we skip it */
+ char *alias;
if((err = compute_alias(&alias, track, p, tid)))
goto deadlocked;
if(!(alias && !strcmp(d_dirname(alias), d_dirname(track))))
if(track_matches(dl, k.data, k.size, re))
vector_append(v, track);
+#endif
}
}
err = cursor->c_get(cursor, &k, &d, DB_NEXT);
/* trackdb_rescan ************************************************************/
+/** @brief Node in the list of rescan-complete callbacks */
+struct rescanned_node {
+ struct rescanned_node *next;
+ void (*rescanned)(void *ru);
+ void *ru;
+};
+
+/** @brief List of rescan-complete callbacks */
+static struct rescanned_node *rescanned_list;
+
+/** @brief Add a rescan completion callback */
+void trackdb_add_rescanned(void (*rescanned)(void *ru),
+ void *ru) {
+ if(rescanned) {
+ struct rescanned_node *n = xmalloc(sizeof *n);
+ n->next = rescanned_list;
+ n->rescanned = rescanned;
+ n->ru = ru;
+ rescanned_list = n;
+ }
+}
+
/* called when the rescanner terminates */
static int reap_rescan(ev_source attribute((unused)) *ev,
pid_t pid,
/* Our cache of file lookups is out of date now */
cache_clean(&cache_files_type);
eventlog("rescanned", (char *)0);
+ /* Call rescanned callbacks */
+ while(rescanned_list) {
+ void (*rescanned)(void *u_) = rescanned_list->rescanned;
+ void *ru = rescanned_list->ru;
+
+ rescanned_list = rescanned_list->next;
+ rescanned(ru);
+ }
return 0;
}
/** @brief Initiate a rescan
* @param ev Event loop or 0 to block
* @param recheck 1 to recheck lengths, 0 to suppress check
+ * @param rescanned Called on completion (if not NULL)
+ * @param ru Passed to @p rescanned
*/
-void trackdb_rescan(ev_source *ev, int recheck) {
+void trackdb_rescan(ev_source *ev, int recheck,
+ void (*rescanned)(void *ru),
+ void *ru) {
int w;
if(rescan_pid != -1) {
+ trackdb_add_rescanned(rescanned, ru);
error(0, "rescan already underway");
return;
}
rescan_pid = subprogram(ev, -1, RESCAN,
recheck ? "--check" : "--no-check",
(char *)0);
+ trackdb_add_rescanned(rescanned, ru);
if(ev) {
ev_child(ev, rescan_pid, 0, reap_rescan, 0);
D(("started rescanner"));
return 1;
}
+/** @brief Return true if a rescan is underway */
+int trackdb_rescan_underway(void) {
+ return rescan_pid != -1;
+}
+
/* global prefs **************************************************************/
void trackdb_set_global(const char *name,
DBT k, d;
int err = 0;
struct vector tracks[1];
+ hash *h = hash_new(1);
vector_init(tracks);
c = trackdb_opencursor(trackdb_noticeddb, tid);
while((maxtracks <= 0 || tracks->nvec < maxtracks)
- && !(err = c->c_get(c, prepare_data(&k), prepare_data(&d), DB_PREV)))
- vector_append(tracks, xstrndup(d.data, d.size));
+ && !(err = c->c_get(c, prepare_data(&k), prepare_data(&d), DB_PREV))) {
+ char *const track = xstrndup(d.data, d.size);
+ /* Don't add any track more than once */
+ if(hash_add(h, track, "", HASH_INSERT))
+ continue;
+ /* See if the track still exists */
+ err = trackdb_getdata(trackdb_tracksdb, track, NULL/*kp*/, tid);
+ if(err == DB_NOTFOUND)
+ continue; /* It doesn't, skip it */
+ if(err == DB_LOCK_DEADLOCK)
+ break; /* Doh */
+ vector_append(tracks, track);
+ }
switch(err) {
case 0: /* hit maxtracks */
case DB_NOTFOUND: /* ran out of tracks */
* Currently we only allow the letters and digits in ASCII. We could be more
* liberal than this but it is a nice simple test. It is critical that
* semicolons are never allowed.
+ *
+ * NB also used by playlist_parse_name() to validate playlist names!
*/
-static int valid_username(const char *user) {
+int valid_username(const char *user) {
if(!*user)
return 0;
while(*user) {
kvp_set(&k, "email", email);
if(confirmation)
kvp_set(&k, "confirmation", confirmation);
- snprintf(s, sizeof s, "%jd", (intmax_t)time(0));
+ snprintf(s, sizeof s, "%jd", (intmax_t)xtime(0));
kvp_set(&k, "created", s);
return trackdb_putdata(trackdb_usersdb, user, k, tid, flags);
}
user, rights, email);
else
info("created user '%s' with rights '%s'", user, rights);
+ eventlog("user_add", user, (char *)0);
return 0;
}
}
return -1;
}
info("deleted user '%s'", user);
+ eventlog("user_delete", user, (char *)0);
return 0;
}
return -1;
}
} else if(!strcmp(key, "email")) {
- if(!strchr(value, '@')) {
- error(0, "invalid email address '%s' for user '%s'", user, value);
- return -1;
- }
+ if(*value) {
+ if(!email_valid(value)) {
+ error(0, "invalid email address '%s' for user '%s'", value, user);
+ return -1;
+ }
+ } else
+ value = 0; /* no email -> remove key */
} else if(!strcmp(key, "created")) {
error(0, "cannot change creation date for user '%s'", user);
return -1;
if(e) {
error(0, "unknown user '%s'", user);
return -1;
- } else
+ } else {
+ eventlog("user_edit", user, key, (char *)0);
return 0;
+ }
}
/** @brief List all users
switch(e) {
case 0:
info("registration confirmed for user '%s'", user);
+ eventlog("user_confirm", user, (char *)0);
return 0;
case DB_NOTFOUND:
error(0, "confirmation for nonexistent user '%s'", user);