2004-02-17 19:18:02 -05:00
|
|
|
/*
|
|
|
|
* $Id$
|
|
|
|
* Implementation for simple gdbm db
|
|
|
|
*
|
|
|
|
* Copyright (C) 2003 Ron Pedde (ron@pedde.com)
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
|
2004-03-13 20:06:15 -05:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
# include "config.h"
|
|
|
|
#endif
|
|
|
|
|
2004-10-10 02:58:28 -04:00
|
|
|
#undef WANT_SORT
|
2004-03-03 01:50:09 -05:00
|
|
|
#define _XOPEN_SOURCE 600
|
|
|
|
|
2004-02-17 19:18:02 -05:00
|
|
|
#include <errno.h>
|
|
|
|
#include <gdbm.h>
|
2004-03-03 13:51:12 -05:00
|
|
|
#include <limits.h>
|
2004-03-03 01:50:09 -05:00
|
|
|
#include <pthread.h>
|
2004-02-17 19:18:02 -05:00
|
|
|
#include <stdio.h>
|
2004-04-28 19:51:26 -04:00
|
|
|
#include <stdlib.h>
|
2004-02-17 19:18:02 -05:00
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
|
|
|
|
#include "err.h"
|
|
|
|
#include "mp3-scanner.h"
|
2004-03-08 15:36:07 -05:00
|
|
|
#include "playlist.h"
|
2004-04-06 17:27:31 -04:00
|
|
|
#define RB_FREE
|
2004-03-13 19:47:42 -05:00
|
|
|
#include "redblack.h"
|
2004-02-17 19:18:02 -05:00
|
|
|
|
2004-06-14 15:01:06 -04:00
|
|
|
#include "db-memory.h"
|
2004-04-02 01:48:24 -05:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Defines
|
|
|
|
*/
|
2004-11-04 00:04:46 -05:00
|
|
|
#define DB_VERSION 5
|
2004-03-03 01:50:09 -05:00
|
|
|
#define STRLEN(a) (a) ? strlen((a)) + 1 : 1
|
2004-04-02 01:48:24 -05:00
|
|
|
#define MAYBEFREE(a) { if((a)) free((a)); };
|
|
|
|
|
2004-06-02 01:28:52 -04:00
|
|
|
/* For old version of gdbm */
|
|
|
|
#ifndef GDBM_SYNC
|
|
|
|
# define GDBM_SYNC 0
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef GDBM_NOLOCK
|
|
|
|
# define GDBM_NOLOCK 0
|
|
|
|
#endif
|
|
|
|
|
2004-04-02 01:48:24 -05:00
|
|
|
|
2004-02-17 19:18:02 -05:00
|
|
|
/*
|
|
|
|
* Typedefs
|
|
|
|
*/
|
2004-06-14 15:01:06 -04:00
|
|
|
typedef struct tag_mp3record MP3RECORD;
|
|
|
|
struct tag_mp3record {
|
2004-02-17 19:18:02 -05:00
|
|
|
MP3FILE mp3file;
|
2004-06-14 15:01:06 -04:00
|
|
|
MP3RECORD* next;
|
|
|
|
};
|
2004-02-17 19:18:02 -05:00
|
|
|
|
|
|
|
typedef struct tag_playlistentry {
|
|
|
|
unsigned int id;
|
|
|
|
struct tag_playlistentry *next;
|
|
|
|
} DB_PLAYLISTENTRY;
|
|
|
|
|
|
|
|
typedef struct tag_playlist {
|
|
|
|
unsigned int id;
|
|
|
|
int songs;
|
2004-03-11 00:47:15 -05:00
|
|
|
int is_smart;
|
2004-02-17 19:18:02 -05:00
|
|
|
char *name;
|
|
|
|
struct tag_playlistentry *nodes;
|
|
|
|
struct tag_playlist *next;
|
|
|
|
} DB_PLAYLIST;
|
|
|
|
|
2004-03-03 01:50:09 -05:00
|
|
|
typedef struct tag_mp3packed {
|
|
|
|
int version;
|
2004-09-07 23:48:09 -04:00
|
|
|
int struct_size; // so we can upgrade in place next time... doh!
|
2004-03-03 01:50:09 -05:00
|
|
|
int bitrate;
|
|
|
|
int samplerate;
|
|
|
|
int song_length;
|
|
|
|
int file_size;
|
|
|
|
int year;
|
|
|
|
|
|
|
|
int track;
|
|
|
|
int total_tracks;
|
|
|
|
|
|
|
|
int disc;
|
|
|
|
int total_discs;
|
|
|
|
|
|
|
|
int time_added;
|
|
|
|
int time_modified;
|
|
|
|
int time_played;
|
|
|
|
|
2004-09-07 23:48:09 -04:00
|
|
|
int bpm; // DB Version 3
|
|
|
|
|
2004-10-15 16:10:39 -04:00
|
|
|
char compilation;
|
|
|
|
|
2004-03-03 01:50:09 -05:00
|
|
|
unsigned int id; /* inode */
|
|
|
|
|
|
|
|
int path_len;
|
|
|
|
int fname_len;
|
|
|
|
int title_len;
|
|
|
|
int artist_len;
|
|
|
|
int album_len;
|
|
|
|
int genre_len;
|
|
|
|
int comment_len;
|
|
|
|
int type_len;
|
2004-03-16 02:54:37 -05:00
|
|
|
int composer_len;
|
|
|
|
int orchestra_len;
|
|
|
|
int conductor_len;
|
|
|
|
int grouping_len;
|
2004-03-03 01:50:09 -05:00
|
|
|
|
2004-11-04 00:04:46 -05:00
|
|
|
int url_len; /* DB Version 4 */
|
2004-03-03 01:50:09 -05:00
|
|
|
char data[1];
|
|
|
|
} MP3PACKED;
|
|
|
|
|
2004-06-14 15:01:06 -04:00
|
|
|
typedef struct {
|
|
|
|
MP3RECORD* root;
|
|
|
|
MP3RECORD* next;
|
2004-10-10 02:58:28 -04:00
|
|
|
} MP3HELPER;
|
2004-03-03 01:50:09 -05:00
|
|
|
|
2004-02-17 19:18:02 -05:00
|
|
|
/*
|
|
|
|
* Globals
|
|
|
|
*/
|
2004-11-14 01:45:13 -05:00
|
|
|
static int db_version_no; /**< db version, incremented every time add or delete */
|
|
|
|
static int db_update_mode=0; /**< Are we in the middle of a bulk update? */
|
|
|
|
static int db_song_count; /**< Number of songs in the db */
|
|
|
|
static int db_playlist_count=0; /**< Number of active playlists */
|
|
|
|
static int db_last_scan; /**< Dunno... */
|
|
|
|
static DB_PLAYLIST db_playlists; /**< The current playlists */
|
|
|
|
static pthread_rwlock_t db_rwlock; /**< pthread r/w sync for the database */
|
|
|
|
static pthread_once_t db_initlock=PTHREAD_ONCE_INIT; /**< to initialize the rwlock */
|
|
|
|
static GDBM_FILE db_songs; /**< Database that holds the mp3 info */
|
|
|
|
static struct rbtree *db_removed; /**< rbtree to do quick searchs to do background scans */
|
|
|
|
static MP3FILE gdbm_mp3; /**< used during enumerations */
|
2004-11-14 02:24:37 -05:00
|
|
|
static int gdbm_mp3_mustfree=0; /**< is the data in #gdbm_mp3 valid? Should it be freed? */
|
2004-02-17 19:18:02 -05:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Forwards
|
|
|
|
*/
|
|
|
|
|
|
|
|
int db_start_initial_update(void);
|
|
|
|
int db_end_initial_update(void);
|
|
|
|
int db_is_empty(void);
|
2004-06-14 15:01:06 -04:00
|
|
|
int db_open(char *parameters, int reload);
|
2004-04-06 11:26:46 -04:00
|
|
|
int db_init();
|
2004-02-17 19:18:02 -05:00
|
|
|
int db_deinit(void);
|
|
|
|
int db_version(void);
|
|
|
|
int db_add(MP3FILE *mp3file);
|
2004-03-13 19:47:42 -05:00
|
|
|
int db_delete(int id);
|
2004-03-11 00:47:15 -05:00
|
|
|
int db_add_playlist(unsigned int playlistid, char *name, int is_smart);
|
2004-02-17 19:18:02 -05:00
|
|
|
int db_add_playlist_song(unsigned int playlistid, unsigned int itemid);
|
2004-03-08 15:36:07 -05:00
|
|
|
int db_unpackrecord(datum *pdatum, MP3FILE *pmp3);
|
2004-09-15 00:59:14 -04:00
|
|
|
int db_scanning(void);
|
2004-03-08 15:36:07 -05:00
|
|
|
datum *db_packrecord(MP3FILE *pmp3);
|
2004-02-17 19:18:02 -05:00
|
|
|
|
|
|
|
int db_get_song_count(void);
|
|
|
|
int db_get_playlist_count(void);
|
2004-03-11 00:47:15 -05:00
|
|
|
int db_get_playlist_is_smart(int playlistid);
|
2004-02-17 19:18:02 -05:00
|
|
|
int db_get_playlist_entry_count(int playlistid);
|
|
|
|
char *db_get_playlist_name(int playlistid);
|
|
|
|
|
|
|
|
MP3FILE *db_find(int id);
|
|
|
|
|
2004-11-13 15:58:30 -05:00
|
|
|
void db_dispose(MP3FILE *pmp3);
|
2004-03-13 19:47:42 -05:00
|
|
|
int db_compare_rb_nodes(const void *pa, const void *pb, const void *cfg);
|
|
|
|
|
2004-09-15 00:59:14 -04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* db_readlock
|
|
|
|
*
|
|
|
|
* If this fails, something is so amazingly hosed, we might just as well
|
|
|
|
* terminate.
|
|
|
|
*/
|
|
|
|
void db_readlock(void) {
|
|
|
|
int err;
|
|
|
|
|
|
|
|
if((err=pthread_rwlock_rdlock(&db_rwlock))) {
|
2004-11-13 02:14:26 -05:00
|
|
|
DPRINTF(E_FATAL,L_DB,"cannot lock rdlock: %s\n",strerror(err));
|
2004-09-15 00:59:14 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* db_writelock
|
|
|
|
*
|
|
|
|
* same as above
|
|
|
|
*/
|
|
|
|
void db_writelock(void) {
|
|
|
|
int err;
|
|
|
|
|
|
|
|
if((err=pthread_rwlock_wrlock(&db_rwlock))) {
|
2004-11-13 02:14:26 -05:00
|
|
|
DPRINTF(E_FATAL,L_DB,"cannot lock rwlock: %s\n",strerror(err));
|
2004-09-15 00:59:14 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* db_unlock
|
|
|
|
*
|
|
|
|
* useless, but symmetrical
|
|
|
|
*/
|
|
|
|
int db_unlock(void) {
|
|
|
|
return pthread_rwlock_unlock(&db_rwlock);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-03-13 19:47:42 -05:00
|
|
|
/*
|
|
|
|
* db_compare_rb_nodes
|
|
|
|
*
|
|
|
|
* compare redblack nodes, which are just ints
|
|
|
|
*/
|
|
|
|
int db_compare_rb_nodes(const void *pa, const void *pb, const void *cfg) {
|
|
|
|
if(*(int*)pa < *(int *)pb) return -1;
|
|
|
|
if(*(int*)pb < *(int *)pa) return 1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2004-02-17 19:18:02 -05:00
|
|
|
|
|
|
|
/*
|
|
|
|
* db_init_once
|
|
|
|
*
|
|
|
|
* Must dynamically initialize the rwlock, as Mac OSX 10.3 (at least)
|
|
|
|
* doesn't have a static initializer for rwlocks
|
|
|
|
*/
|
|
|
|
void db_init_once(void) {
|
|
|
|
pthread_rwlock_init(&db_rwlock,NULL);
|
|
|
|
}
|
|
|
|
|
2004-04-06 09:44:26 -04:00
|
|
|
/*
|
|
|
|
* db_open
|
|
|
|
*
|
|
|
|
* Open the database, so we can drop privs
|
|
|
|
*/
|
2004-06-14 15:01:06 -04:00
|
|
|
int db_open(char *parameters, int reload) {
|
2004-04-06 09:44:26 -04:00
|
|
|
char db_path[PATH_MAX + 1];
|
|
|
|
|
|
|
|
if(pthread_once(&db_initlock,db_init_once))
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
snprintf(db_path,sizeof(db_path),"%s/%s",parameters,"songs.gdb");
|
2004-06-14 15:01:06 -04:00
|
|
|
|
|
|
|
reload = reload ? GDBM_NEWDB : GDBM_WRCREAT;
|
|
|
|
db_songs=gdbm_open(db_path, 0, reload | GDBM_SYNC | GDBM_NOLOCK,
|
2004-04-06 09:44:26 -04:00
|
|
|
0600,NULL);
|
|
|
|
if(!db_songs) {
|
2004-11-13 02:14:26 -05:00
|
|
|
DPRINTF(E_FATAL,L_DB,"Could not open songs database (%s)\n",
|
2004-04-06 09:44:26 -04:00
|
|
|
db_path);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-02-17 19:18:02 -05:00
|
|
|
/*
|
|
|
|
* db_init
|
|
|
|
*
|
|
|
|
* Initialize the database. Parameter is the directory
|
|
|
|
* of the database files
|
|
|
|
*/
|
2004-04-06 09:44:26 -04:00
|
|
|
int db_init(void) {
|
2004-03-08 15:36:07 -05:00
|
|
|
MP3FILE mp3file;
|
|
|
|
datum tmp_key,tmp_nextkey,song_data;
|
2004-02-17 19:18:02 -05:00
|
|
|
|
2004-03-11 00:47:15 -05:00
|
|
|
pl_register();
|
|
|
|
|
2004-02-17 19:18:02 -05:00
|
|
|
db_version_no=1;
|
|
|
|
db_song_count=0;
|
|
|
|
|
2004-11-13 02:14:26 -05:00
|
|
|
DPRINTF(E_DBG,L_DB|L_PL,"Building playlists\n");
|
2004-03-03 13:51:12 -05:00
|
|
|
|
2004-03-08 15:36:07 -05:00
|
|
|
/* count the actual songs and build the playlists */
|
|
|
|
tmp_key=gdbm_firstkey(db_songs);
|
|
|
|
|
|
|
|
MEMNOTIFY(tmp_key.dptr);
|
|
|
|
|
|
|
|
while(tmp_key.dptr) {
|
|
|
|
/* Fetch that key */
|
|
|
|
song_data=gdbm_fetch(db_songs,tmp_key);
|
|
|
|
MEMNOTIFY(song_data.dptr);
|
|
|
|
if(song_data.dptr) {
|
|
|
|
if(!db_unpackrecord(&song_data,&mp3file)) {
|
|
|
|
/* Check against playlist */
|
|
|
|
pl_eval(&mp3file);
|
2004-11-13 15:58:30 -05:00
|
|
|
db_dispose(&mp3file);
|
2004-03-08 15:36:07 -05:00
|
|
|
}
|
|
|
|
free(song_data.dptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
tmp_nextkey=gdbm_nextkey(db_songs,tmp_key);
|
2004-03-03 13:51:12 -05:00
|
|
|
MEMNOTIFY(tmp_nextkey.dptr);
|
2004-04-06 23:51:01 -04:00
|
|
|
free(tmp_key.dptr);
|
2004-03-08 15:36:07 -05:00
|
|
|
tmp_key=tmp_nextkey;
|
2004-03-03 01:50:09 -05:00
|
|
|
db_song_count++;
|
|
|
|
}
|
|
|
|
|
2004-11-13 02:14:26 -05:00
|
|
|
DPRINTF(E_DBG,L_DB,"Loaded database... found %d songs\n",db_song_count);
|
2004-02-17 19:18:02 -05:00
|
|
|
|
|
|
|
/* and the playlists */
|
2004-03-11 00:47:15 -05:00
|
|
|
return 0;
|
2004-02-17 19:18:02 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* db_deinit
|
|
|
|
*
|
|
|
|
* Close the db, in this case freeing memory
|
|
|
|
*/
|
|
|
|
int db_deinit(void) {
|
|
|
|
DB_PLAYLIST *plist;
|
|
|
|
DB_PLAYLISTENTRY *pentry;
|
|
|
|
|
2004-03-03 01:50:09 -05:00
|
|
|
gdbm_close(db_songs);
|
2004-02-17 19:18:02 -05:00
|
|
|
|
|
|
|
while(db_playlists.next) {
|
|
|
|
plist=db_playlists.next;
|
|
|
|
db_playlists.next=plist->next;
|
|
|
|
free(plist->name);
|
|
|
|
/* free all the nodes */
|
|
|
|
while(plist->nodes) {
|
|
|
|
pentry=plist->nodes;
|
|
|
|
plist->nodes = pentry->next;
|
|
|
|
free(pentry);
|
|
|
|
}
|
|
|
|
free(plist);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2004-09-15 00:59:14 -04:00
|
|
|
/*
|
|
|
|
* db_scanning
|
|
|
|
*
|
|
|
|
* is the db currently in scanning mode?
|
|
|
|
*/
|
|
|
|
int db_scanning(void) {
|
|
|
|
return db_update_mode;
|
|
|
|
}
|
|
|
|
|
2004-02-17 19:18:02 -05:00
|
|
|
/*
|
|
|
|
* db_version
|
|
|
|
*
|
|
|
|
* return the db version
|
|
|
|
*/
|
|
|
|
int db_version(void) {
|
2004-09-15 00:59:14 -04:00
|
|
|
int version;
|
|
|
|
|
|
|
|
db_readlock();
|
|
|
|
version=db_version_no;
|
|
|
|
db_unlock();
|
|
|
|
|
|
|
|
return version;
|
2004-02-17 19:18:02 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* db_start_initial_update
|
|
|
|
*
|
|
|
|
* Set the db to bulk import mode
|
|
|
|
*/
|
|
|
|
int db_start_initial_update(void) {
|
2004-04-06 23:51:01 -04:00
|
|
|
datum tmp_key,tmp_nextkey;
|
2004-09-15 00:59:14 -04:00
|
|
|
int err;
|
|
|
|
|
|
|
|
/* we need a write lock on the db -- stop enums from happening */
|
|
|
|
db_writelock();
|
2004-04-06 23:51:01 -04:00
|
|
|
|
|
|
|
if((db_removed=rbinit(db_compare_rb_nodes,NULL)) == NULL) {
|
|
|
|
errno=ENOMEM;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* load up the red-black tree with all the current songs in the db */
|
|
|
|
|
|
|
|
tmp_key=gdbm_firstkey(db_songs);
|
|
|
|
|
|
|
|
MEMNOTIFY(tmp_key.dptr);
|
|
|
|
|
|
|
|
while(tmp_key.dptr) {
|
|
|
|
/* Add it to the rbtree */
|
|
|
|
if(!rbsearch((void*)tmp_key.dptr,db_removed)) {
|
|
|
|
errno=ENOMEM;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
tmp_nextkey=gdbm_nextkey(db_songs,tmp_key);
|
|
|
|
MEMNOTIFY(tmp_nextkey.dptr);
|
|
|
|
// free(tmp_key.dptr); /* we'll free it in update mode */
|
|
|
|
tmp_key=tmp_nextkey;
|
|
|
|
}
|
|
|
|
|
2004-02-17 19:18:02 -05:00
|
|
|
db_update_mode=1;
|
2004-04-06 23:51:01 -04:00
|
|
|
|
2004-02-17 19:18:02 -05:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* db_end_initial_update
|
|
|
|
*
|
|
|
|
* Take the db out of bulk import mode
|
|
|
|
*/
|
|
|
|
int db_end_initial_update(void) {
|
2004-03-13 19:47:42 -05:00
|
|
|
const void *val;
|
|
|
|
|
2004-02-17 19:18:02 -05:00
|
|
|
db_update_mode=0;
|
2004-03-13 19:47:42 -05:00
|
|
|
|
2004-11-13 02:14:26 -05:00
|
|
|
DPRINTF(E_DBG,L_DB|L_SCAN,"Initial update over. Removing stale items\n");
|
2004-04-02 01:48:24 -05:00
|
|
|
for(val=rblookup(RB_LUFIRST,NULL,db_removed); val != NULL;
|
|
|
|
val=rblookup(RB_LUNEXT,val,db_removed)) {
|
2004-03-13 19:47:42 -05:00
|
|
|
db_delete(*((int*)val));
|
|
|
|
}
|
2004-04-06 17:27:31 -04:00
|
|
|
|
2004-11-13 02:14:26 -05:00
|
|
|
DPRINTF(E_DBG,L_DB|L_SCAN,"Done removing stale items\n");
|
2004-03-13 19:47:42 -05:00
|
|
|
|
|
|
|
rbdestroy(db_removed);
|
|
|
|
|
2004-10-25 00:49:20 -04:00
|
|
|
gdbm_reorganize(db_songs);
|
2004-09-15 00:59:14 -04:00
|
|
|
db_unlock();
|
2004-04-28 14:55:22 -04:00
|
|
|
|
2004-02-17 19:18:02 -05:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* db_is_empty
|
|
|
|
*
|
|
|
|
* See if the db is empty or not -- that is, should
|
|
|
|
* the scanner start up in bulk update mode or in
|
|
|
|
* background update mode
|
|
|
|
*/
|
|
|
|
int db_is_empty(void) {
|
2004-03-03 01:50:09 -05:00
|
|
|
return !db_song_count;
|
2004-02-17 19:18:02 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* db_add_playlist
|
|
|
|
*
|
|
|
|
* Add a new playlist
|
2004-09-15 00:59:14 -04:00
|
|
|
*
|
|
|
|
* FIXME: this assume we are in db_update mode... if this is called at random,
|
|
|
|
* then we have to get a write lock.
|
2004-02-17 19:18:02 -05:00
|
|
|
*/
|
2004-03-11 00:47:15 -05:00
|
|
|
int db_add_playlist(unsigned int playlistid, char *name, int is_smart) {
|
2004-02-17 19:18:02 -05:00
|
|
|
int err;
|
|
|
|
DB_PLAYLIST *pnew;
|
|
|
|
|
|
|
|
pnew=(DB_PLAYLIST*)malloc(sizeof(DB_PLAYLIST));
|
|
|
|
if(!pnew)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
pnew->name=strdup(name);
|
|
|
|
pnew->id=playlistid;
|
|
|
|
pnew->nodes=NULL;
|
|
|
|
pnew->songs=0;
|
2004-03-11 00:47:15 -05:00
|
|
|
pnew->is_smart=is_smart;
|
2004-02-17 19:18:02 -05:00
|
|
|
|
|
|
|
if(!pnew->name) {
|
|
|
|
free(pnew);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2004-11-13 02:14:26 -05:00
|
|
|
DPRINTF(E_DBG,L_DB|L_PL,"Adding new playlist %s\n",name);
|
2004-02-17 19:18:02 -05:00
|
|
|
|
|
|
|
pnew->next=db_playlists.next;
|
|
|
|
db_playlists.next=pnew;
|
|
|
|
|
2004-09-15 00:59:14 -04:00
|
|
|
db_version_no++;
|
2004-02-17 19:18:02 -05:00
|
|
|
|
2004-11-13 02:14:26 -05:00
|
|
|
DPRINTF(E_DBG,L_DB|L_PL,"Added playlist\n");
|
2004-02-17 19:18:02 -05:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* db_add_playlist_song
|
|
|
|
*
|
|
|
|
* Add a song to a particular playlist
|
2004-09-15 00:59:14 -04:00
|
|
|
*
|
|
|
|
* FIXME: as db_add playlist, this assumes we already have a writelock from
|
|
|
|
* db_udpate_mode.
|
2004-02-17 19:18:02 -05:00
|
|
|
*/
|
|
|
|
int db_add_playlist_song(unsigned int playlistid, unsigned int itemid) {
|
|
|
|
DB_PLAYLIST *current;
|
|
|
|
DB_PLAYLISTENTRY *pnew;
|
|
|
|
int err;
|
|
|
|
|
|
|
|
pnew=(DB_PLAYLISTENTRY*)malloc(sizeof(DB_PLAYLISTENTRY));
|
|
|
|
if(!pnew)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
pnew->id=itemid;
|
|
|
|
pnew->next=NULL;
|
|
|
|
|
2004-11-13 02:14:26 -05:00
|
|
|
DPRINTF(E_DBG,L_DB|L_PL,"Adding item %d to %d\n",itemid,playlistid);
|
2004-02-17 19:18:02 -05:00
|
|
|
|
|
|
|
current=db_playlists.next;
|
|
|
|
while(current && (current->id != playlistid))
|
|
|
|
current=current->next;
|
|
|
|
|
|
|
|
if(!current) {
|
2004-11-13 02:14:26 -05:00
|
|
|
DPRINTF(E_WARN,L_DB|L_PL,"Could not find playlist attempting to add to\n");
|
2004-09-15 00:59:14 -04:00
|
|
|
if(!db_update_mode)
|
|
|
|
db_unlock();
|
|
|
|
db_unlock();
|
2004-02-17 19:18:02 -05:00
|
|
|
free(pnew);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!current->songs)
|
|
|
|
db_playlist_count++;
|
|
|
|
|
|
|
|
current->songs++;
|
2004-11-13 02:14:26 -05:00
|
|
|
DPRINTF(E_DBG,L_DB|L_PL,"Playlist now has %d entries\n",current->songs);
|
2004-02-17 19:18:02 -05:00
|
|
|
pnew->next = current->nodes;
|
|
|
|
current->nodes = pnew;
|
|
|
|
|
2004-09-15 00:59:14 -04:00
|
|
|
db_version_no++;
|
2004-02-17 19:18:02 -05:00
|
|
|
|
2004-11-13 02:14:26 -05:00
|
|
|
DPRINTF(E_DBG,L_DB|L_PL,"Added playlist item\n");
|
2004-02-17 19:18:02 -05:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2004-03-03 01:50:09 -05:00
|
|
|
* db_packrecord
|
2004-02-17 19:18:02 -05:00
|
|
|
*
|
2004-03-03 01:50:09 -05:00
|
|
|
* Given an MP3 record, turn it into a datum
|
2004-02-17 19:18:02 -05:00
|
|
|
*/
|
2004-03-03 01:50:09 -05:00
|
|
|
datum *db_packrecord(MP3FILE *pmp3) {
|
|
|
|
int len;
|
|
|
|
datum *result;
|
|
|
|
MP3PACKED *ppacked;
|
|
|
|
int offset;
|
|
|
|
|
2004-04-02 01:48:24 -05:00
|
|
|
len=sizeof(MP3PACKED);
|
2004-03-03 01:50:09 -05:00
|
|
|
len += STRLEN(pmp3->path);
|
|
|
|
len += STRLEN(pmp3->fname);
|
|
|
|
len += STRLEN(pmp3->title);
|
|
|
|
len += STRLEN(pmp3->artist);
|
|
|
|
len += STRLEN(pmp3->album);
|
|
|
|
len += STRLEN(pmp3->genre);
|
|
|
|
len += STRLEN(pmp3->comment);
|
|
|
|
len += STRLEN(pmp3->type);
|
2004-03-16 02:54:37 -05:00
|
|
|
len += STRLEN(pmp3->composer);
|
|
|
|
len += STRLEN(pmp3->orchestra);
|
|
|
|
len += STRLEN(pmp3->conductor);
|
|
|
|
len += STRLEN(pmp3->grouping);
|
2004-11-04 00:04:46 -05:00
|
|
|
len += STRLEN(pmp3->url);
|
2004-03-03 01:50:09 -05:00
|
|
|
|
|
|
|
result = (datum*) malloc(sizeof(datum));
|
|
|
|
if(!result)
|
|
|
|
return NULL;
|
2004-02-17 19:18:02 -05:00
|
|
|
|
2004-03-03 13:51:12 -05:00
|
|
|
result->dptr = (void*)malloc(len);
|
|
|
|
result->dsize=len;
|
|
|
|
|
2004-03-03 01:50:09 -05:00
|
|
|
if(!result->dptr) {
|
|
|
|
free(result);
|
|
|
|
return NULL;
|
|
|
|
}
|
2004-02-17 19:18:02 -05:00
|
|
|
|
2004-03-03 01:50:09 -05:00
|
|
|
memset(result->dptr,0x00,len);
|
|
|
|
|
|
|
|
/* start packing! */
|
|
|
|
ppacked=(MP3PACKED *)result->dptr;
|
|
|
|
|
|
|
|
ppacked->version=DB_VERSION;
|
2004-09-07 23:48:09 -04:00
|
|
|
ppacked->struct_size=sizeof(MP3PACKED);
|
|
|
|
|
2004-03-03 01:50:09 -05:00
|
|
|
ppacked->bitrate=pmp3->bitrate;
|
|
|
|
ppacked->samplerate=pmp3->samplerate;
|
|
|
|
ppacked->song_length=pmp3->song_length;
|
|
|
|
ppacked->file_size=pmp3->file_size;
|
|
|
|
ppacked->year=pmp3->year;
|
|
|
|
ppacked->track=pmp3->track;
|
|
|
|
ppacked->total_tracks=pmp3->total_tracks;
|
|
|
|
ppacked->disc=pmp3->disc;
|
|
|
|
ppacked->total_discs=pmp3->total_discs;
|
|
|
|
ppacked->time_added=pmp3->time_added;
|
|
|
|
ppacked->time_modified=pmp3->time_modified;
|
|
|
|
ppacked->time_played=pmp3->time_played;
|
2004-09-07 23:48:09 -04:00
|
|
|
ppacked->bpm=pmp3->bpm;
|
2004-10-15 16:10:39 -04:00
|
|
|
ppacked->compilation=pmp3->compilation;
|
2004-03-03 01:50:09 -05:00
|
|
|
ppacked->id=pmp3->id;
|
|
|
|
|
|
|
|
ppacked->path_len=STRLEN(pmp3->path);
|
|
|
|
ppacked->fname_len=STRLEN(pmp3->fname);
|
|
|
|
ppacked->title_len=STRLEN(pmp3->title);
|
|
|
|
ppacked->artist_len=STRLEN(pmp3->artist);
|
|
|
|
ppacked->album_len=STRLEN(pmp3->album);
|
|
|
|
ppacked->genre_len=STRLEN(pmp3->genre);
|
|
|
|
ppacked->comment_len=STRLEN(pmp3->comment);
|
|
|
|
ppacked->type_len=STRLEN(pmp3->type);
|
2004-03-16 02:54:37 -05:00
|
|
|
ppacked->composer_len=STRLEN(pmp3->composer);
|
|
|
|
ppacked->orchestra_len=STRLEN(pmp3->orchestra);
|
|
|
|
ppacked->conductor_len=STRLEN(pmp3->conductor);
|
|
|
|
ppacked->grouping_len=STRLEN(pmp3->grouping);
|
2004-11-04 00:04:46 -05:00
|
|
|
ppacked->url_len=STRLEN(pmp3->url);
|
2004-03-03 01:50:09 -05:00
|
|
|
|
|
|
|
offset=0;
|
|
|
|
if(pmp3->path)
|
2004-04-02 01:48:24 -05:00
|
|
|
memcpy(&ppacked->data[offset],pmp3->path,ppacked->path_len);
|
2004-03-03 01:50:09 -05:00
|
|
|
offset+=ppacked->path_len;
|
|
|
|
|
|
|
|
if(pmp3->fname)
|
2004-04-02 01:48:24 -05:00
|
|
|
memcpy(&ppacked->data[offset],pmp3->fname,ppacked->fname_len);
|
2004-03-03 01:50:09 -05:00
|
|
|
offset+=ppacked->fname_len;
|
|
|
|
|
|
|
|
if(pmp3->title)
|
2004-04-02 01:48:24 -05:00
|
|
|
memcpy(&ppacked->data[offset],pmp3->title,ppacked->title_len);
|
2004-03-03 01:50:09 -05:00
|
|
|
offset+=ppacked->title_len;
|
|
|
|
|
|
|
|
if(pmp3->artist)
|
2004-04-02 01:48:24 -05:00
|
|
|
memcpy(&ppacked->data[offset],pmp3->artist,ppacked->artist_len);
|
2004-03-03 01:50:09 -05:00
|
|
|
offset+=ppacked->artist_len;
|
|
|
|
|
|
|
|
if(pmp3->album)
|
2004-04-02 01:48:24 -05:00
|
|
|
memcpy(&ppacked->data[offset],pmp3->album,ppacked->album_len);
|
2004-03-03 01:50:09 -05:00
|
|
|
offset+=ppacked->album_len;
|
|
|
|
|
|
|
|
if(pmp3->genre)
|
2004-04-02 01:48:24 -05:00
|
|
|
memcpy(&ppacked->data[offset],pmp3->genre,ppacked->genre_len);
|
2004-03-03 01:50:09 -05:00
|
|
|
offset+=ppacked->genre_len;
|
|
|
|
|
|
|
|
if(pmp3->comment)
|
2004-04-02 01:48:24 -05:00
|
|
|
memcpy(&ppacked->data[offset],pmp3->comment,ppacked->comment_len);
|
2004-03-03 01:50:09 -05:00
|
|
|
offset+=ppacked->comment_len;
|
|
|
|
|
|
|
|
if(pmp3->type)
|
2004-04-02 01:48:24 -05:00
|
|
|
memcpy(&ppacked->data[offset],pmp3->type,ppacked->type_len);
|
2004-03-03 01:50:09 -05:00
|
|
|
offset+=ppacked->type_len;
|
|
|
|
|
2004-03-16 02:54:37 -05:00
|
|
|
if(pmp3->composer)
|
2004-04-02 01:48:24 -05:00
|
|
|
memcpy(&ppacked->data[offset],pmp3->composer,ppacked->composer_len);
|
2004-03-16 02:54:37 -05:00
|
|
|
offset+=ppacked->composer_len;
|
|
|
|
|
|
|
|
if(pmp3->orchestra)
|
2004-04-02 01:48:24 -05:00
|
|
|
memcpy(&ppacked->data[offset],pmp3->orchestra,ppacked->orchestra_len);
|
2004-03-16 02:54:37 -05:00
|
|
|
offset+=ppacked->orchestra_len;
|
|
|
|
|
|
|
|
if(pmp3->conductor)
|
2004-04-02 01:48:24 -05:00
|
|
|
memcpy(&ppacked->data[offset],pmp3->conductor,ppacked->conductor_len);
|
2004-03-16 02:54:37 -05:00
|
|
|
offset+=ppacked->conductor_len;
|
|
|
|
|
|
|
|
if(pmp3->grouping)
|
2004-04-02 01:48:24 -05:00
|
|
|
memcpy(&ppacked->data[offset],pmp3->grouping,ppacked->grouping_len);
|
2004-03-16 02:54:37 -05:00
|
|
|
offset+=ppacked->grouping_len;
|
|
|
|
|
2004-11-04 00:04:46 -05:00
|
|
|
if(pmp3->url)
|
|
|
|
memcpy(&ppacked->data[offset],pmp3->url,ppacked->url_len);
|
|
|
|
offset+=ppacked->url_len;
|
|
|
|
|
|
|
|
|
2004-03-03 01:50:09 -05:00
|
|
|
/* whew */
|
|
|
|
return result;
|
|
|
|
}
|
2004-02-17 19:18:02 -05:00
|
|
|
|
|
|
|
|
2004-03-03 01:50:09 -05:00
|
|
|
/*
|
|
|
|
* db_unpackrecord
|
|
|
|
*
|
|
|
|
* Given a datum, return an MP3 record
|
|
|
|
*/
|
|
|
|
int db_unpackrecord(datum *pdatum, MP3FILE *pmp3) {
|
|
|
|
MP3PACKED *ppacked;
|
|
|
|
int offset;
|
2004-02-17 19:18:02 -05:00
|
|
|
|
2004-03-03 01:50:09 -05:00
|
|
|
/* should check minimum length (for v1) */
|
2004-02-17 19:18:02 -05:00
|
|
|
|
2004-03-03 13:51:12 -05:00
|
|
|
memset(pmp3,0x0,sizeof(MP3FILE));
|
2004-02-17 19:18:02 -05:00
|
|
|
|
2004-03-03 01:50:09 -05:00
|
|
|
/* VERSION 1 */
|
|
|
|
ppacked=(MP3PACKED*)pdatum->dptr;
|
2004-03-16 02:54:37 -05:00
|
|
|
|
|
|
|
if(ppacked->version != DB_VERSION)
|
2004-11-13 02:14:26 -05:00
|
|
|
DPRINTF(E_FATAL,L_DB,"ON-DISK DATABASE VERSION HAS CHANGED\n"
|
2004-04-13 00:23:36 -04:00
|
|
|
"Delete your songs.gdb and restart.\n");
|
2004-03-16 02:54:37 -05:00
|
|
|
|
2004-03-03 01:50:09 -05:00
|
|
|
pmp3->bitrate=ppacked->bitrate;
|
|
|
|
pmp3->samplerate=ppacked->samplerate;
|
|
|
|
pmp3->song_length=ppacked->song_length;
|
|
|
|
pmp3->file_size=ppacked->file_size;
|
|
|
|
pmp3->year=ppacked->year;
|
|
|
|
pmp3->track=ppacked->track;
|
|
|
|
pmp3->total_tracks=ppacked->total_tracks;
|
|
|
|
pmp3->disc=ppacked->disc;
|
|
|
|
pmp3->total_discs=ppacked->total_discs;
|
|
|
|
pmp3->time_added=ppacked->time_added;
|
|
|
|
pmp3->time_modified=ppacked->time_modified;
|
|
|
|
pmp3->time_played=ppacked->time_played;
|
2004-09-07 23:48:09 -04:00
|
|
|
pmp3->bpm=ppacked->bpm;
|
2004-10-15 16:10:39 -04:00
|
|
|
pmp3->compilation=ppacked->compilation;
|
2004-03-03 01:50:09 -05:00
|
|
|
pmp3->id=ppacked->id;
|
|
|
|
|
|
|
|
offset=0;
|
2004-03-03 13:51:12 -05:00
|
|
|
if(ppacked->path_len > 1)
|
|
|
|
pmp3->path=strdup(&ppacked->data[offset]);
|
2004-03-03 01:50:09 -05:00
|
|
|
offset += ppacked->path_len;
|
|
|
|
|
2004-03-03 13:51:12 -05:00
|
|
|
if(ppacked->fname_len > 1)
|
|
|
|
pmp3->fname=strdup(&ppacked->data[offset]);
|
2004-03-03 01:50:09 -05:00
|
|
|
offset += ppacked->fname_len;
|
|
|
|
|
2004-03-03 13:51:12 -05:00
|
|
|
if(ppacked->title_len > 1)
|
|
|
|
pmp3->title=strdup(&ppacked->data[offset]);
|
2004-03-03 01:50:09 -05:00
|
|
|
offset += ppacked->title_len;
|
2004-02-17 19:18:02 -05:00
|
|
|
|
2004-03-03 13:51:12 -05:00
|
|
|
if(ppacked->artist_len > 1)
|
|
|
|
pmp3->artist=strdup(&ppacked->data[offset]);
|
2004-03-03 01:50:09 -05:00
|
|
|
offset += ppacked->artist_len;
|
2004-02-17 19:18:02 -05:00
|
|
|
|
2004-03-03 13:51:12 -05:00
|
|
|
if(ppacked->album_len > 1)
|
|
|
|
pmp3->album=strdup(&ppacked->data[offset]);
|
2004-03-03 01:50:09 -05:00
|
|
|
offset += ppacked->album_len;
|
2004-02-17 19:18:02 -05:00
|
|
|
|
2004-03-03 13:51:12 -05:00
|
|
|
if(ppacked->genre_len > 1)
|
|
|
|
pmp3->genre=strdup(&ppacked->data[offset]);
|
2004-03-03 01:50:09 -05:00
|
|
|
offset += ppacked->genre_len;
|
2004-02-17 19:18:02 -05:00
|
|
|
|
2004-03-03 13:51:12 -05:00
|
|
|
if(ppacked->comment_len > 1)
|
|
|
|
pmp3->comment=strdup(&ppacked->data[offset]);
|
2004-03-03 01:50:09 -05:00
|
|
|
offset += ppacked->comment_len;
|
|
|
|
|
2004-03-03 13:51:12 -05:00
|
|
|
if(ppacked->type_len > 1)
|
|
|
|
pmp3->type=strdup(&ppacked->data[offset]);
|
2004-03-03 01:50:09 -05:00
|
|
|
offset += ppacked->type_len;
|
2004-03-16 02:54:37 -05:00
|
|
|
|
|
|
|
if(ppacked->composer_len > 1)
|
|
|
|
pmp3->composer=strdup(&ppacked->data[offset]);
|
|
|
|
offset += ppacked->composer_len;
|
|
|
|
|
|
|
|
if(ppacked->orchestra_len > 1)
|
|
|
|
pmp3->orchestra=strdup(&ppacked->data[offset]);
|
|
|
|
offset += ppacked->orchestra_len;
|
|
|
|
|
|
|
|
if(ppacked->conductor_len > 1)
|
|
|
|
pmp3->conductor=strdup(&ppacked->data[offset]);
|
|
|
|
offset += ppacked->conductor_len;
|
|
|
|
|
|
|
|
if(ppacked->grouping_len > 1)
|
|
|
|
pmp3->grouping=strdup(&ppacked->data[offset]);
|
|
|
|
offset += ppacked->grouping_len;
|
2004-11-04 00:04:46 -05:00
|
|
|
|
|
|
|
if(ppacked->url_len > 1)
|
|
|
|
pmp3->url=strdup(&ppacked->data[offset]);
|
|
|
|
offset += ppacked->url_len;
|
2004-03-03 01:50:09 -05:00
|
|
|
|
2004-09-12 19:20:29 -04:00
|
|
|
/* shouldn't this have been done when scanning? */
|
2004-06-14 15:01:06 -04:00
|
|
|
make_composite_tags(pmp3);
|
|
|
|
|
2004-03-03 01:50:09 -05:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* db_add
|
|
|
|
*
|
|
|
|
* add an MP3 file to the database.
|
2004-09-15 00:59:14 -04:00
|
|
|
*
|
|
|
|
* FIXME: Like the playlist adds, this assumes that this will only be called
|
2004-10-10 02:58:28 -04:00
|
|
|
* during a db_update... that the writelock is already held. This wouldn't
|
|
|
|
* necessarily be that case if, say, we were doing an add from the web interface.
|
|
|
|
*
|
2004-03-03 01:50:09 -05:00
|
|
|
*/
|
|
|
|
int db_add(MP3FILE *pmp3) {
|
|
|
|
int err;
|
|
|
|
datum *pnew;
|
|
|
|
datum dkey;
|
|
|
|
MP3PACKED *ppacked;
|
2004-03-03 13:51:12 -05:00
|
|
|
unsigned int id;
|
2004-03-03 01:50:09 -05:00
|
|
|
|
2004-11-13 02:14:26 -05:00
|
|
|
DPRINTF(E_DBG,L_DB,"Adding %s\n",pmp3->path);
|
2004-03-03 01:50:09 -05:00
|
|
|
|
|
|
|
if(!(pnew=db_packrecord(pmp3))) {
|
2004-02-17 19:18:02 -05:00
|
|
|
errno=ENOMEM;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2004-03-03 01:50:09 -05:00
|
|
|
/* insert the datum into the underlying database */
|
|
|
|
dkey.dptr=(void*)&(pmp3->id);
|
|
|
|
dkey.dsize=sizeof(unsigned int);
|
|
|
|
|
|
|
|
/* dummy this up in case the client didn't */
|
|
|
|
ppacked=(MP3PACKED *)pnew->dptr;
|
2004-09-12 19:20:29 -04:00
|
|
|
if(!ppacked->time_added)
|
|
|
|
ppacked->time_added=(int)time(NULL);
|
2004-09-19 01:18:49 -04:00
|
|
|
ppacked->time_modified=(int)time(NULL);
|
2004-09-12 19:20:29 -04:00
|
|
|
ppacked->time_played=0; /* do we want to keep track of this? */
|
2004-03-03 01:50:09 -05:00
|
|
|
|
2004-03-08 14:21:03 -05:00
|
|
|
if(gdbm_store(db_songs,dkey,*pnew,GDBM_REPLACE)) {
|
2004-11-13 02:14:26 -05:00
|
|
|
DPRINTF(E_FATAL,L_DB,"Error inserting file %s in database\n",pmp3->fname);
|
2004-03-03 01:50:09 -05:00
|
|
|
}
|
|
|
|
|
2004-11-13 02:14:26 -05:00
|
|
|
DPRINTF(E_DBG,L_DB,"Testing for %d\n",pmp3->id);
|
2004-03-03 13:51:12 -05:00
|
|
|
id=pmp3->id;
|
|
|
|
dkey.dptr=(void*)&id;
|
|
|
|
dkey.dsize=sizeof(unsigned int);
|
|
|
|
|
|
|
|
if(!gdbm_exists(db_songs,dkey)) {
|
2004-11-13 02:14:26 -05:00
|
|
|
DPRINTF(E_FATAL,L_DB,"Error.. could not find just added file\n");
|
2004-03-03 13:51:12 -05:00
|
|
|
}
|
|
|
|
|
2004-03-03 01:50:09 -05:00
|
|
|
free(pnew->dptr);
|
|
|
|
free(pnew);
|
2004-02-17 19:18:02 -05:00
|
|
|
|
2004-09-15 00:59:14 -04:00
|
|
|
db_version_no++;
|
2004-02-17 19:18:02 -05:00
|
|
|
|
|
|
|
db_song_count++;
|
|
|
|
|
2004-11-13 02:14:26 -05:00
|
|
|
DPRINTF(E_DBG,L_DB,"Added file\n");
|
2004-02-17 19:18:02 -05:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2004-11-13 15:58:30 -05:00
|
|
|
* db_dispose
|
2004-02-17 19:18:02 -05:00
|
|
|
*
|
|
|
|
* free a complete mp3record
|
|
|
|
*/
|
2004-11-13 15:58:30 -05:00
|
|
|
void db_dispose(MP3FILE *pmp3) {
|
2004-03-03 01:50:09 -05:00
|
|
|
MAYBEFREE(pmp3->path);
|
|
|
|
MAYBEFREE(pmp3->fname);
|
|
|
|
MAYBEFREE(pmp3->title);
|
|
|
|
MAYBEFREE(pmp3->artist);
|
|
|
|
MAYBEFREE(pmp3->album);
|
|
|
|
MAYBEFREE(pmp3->genre);
|
|
|
|
MAYBEFREE(pmp3->comment);
|
2004-03-16 02:54:37 -05:00
|
|
|
MAYBEFREE(pmp3->type);
|
|
|
|
MAYBEFREE(pmp3->composer);
|
|
|
|
MAYBEFREE(pmp3->orchestra);
|
|
|
|
MAYBEFREE(pmp3->conductor);
|
|
|
|
MAYBEFREE(pmp3->grouping);
|
2004-06-14 15:01:06 -04:00
|
|
|
MAYBEFREE(pmp3->description);
|
2004-11-04 00:04:46 -05:00
|
|
|
MAYBEFREE(pmp3->url);
|
2004-02-17 19:18:02 -05:00
|
|
|
}
|
|
|
|
|
2004-06-21 21:52:26 -04:00
|
|
|
static int nullstrcmp(const char* a, const char* b)
|
|
|
|
{
|
|
|
|
if(a == 0)
|
|
|
|
return b == 0 ? 1 : 0;
|
|
|
|
if(b == 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
return strcmp(a, b);
|
|
|
|
}
|
|
|
|
|
2004-06-14 15:01:06 -04:00
|
|
|
int compare(MP3RECORD* a, MP3RECORD* b)
|
|
|
|
{
|
|
|
|
int comp;
|
|
|
|
|
2004-06-21 21:52:26 -04:00
|
|
|
if((comp = nullstrcmp(a->mp3file.album, b->mp3file.album)) != 0)
|
2004-06-14 15:01:06 -04:00
|
|
|
return comp;
|
|
|
|
|
|
|
|
if((comp = (a->mp3file.disc - b->mp3file.disc)) != 0)
|
|
|
|
return comp;
|
|
|
|
|
|
|
|
if((comp = (a->mp3file.track - b->mp3file.track)) != 0)
|
|
|
|
return comp;
|
|
|
|
|
|
|
|
return a->mp3file.id - b->mp3file.id;
|
|
|
|
}
|
|
|
|
|
2004-10-10 02:58:28 -04:00
|
|
|
#ifdef WANT_SORT
|
|
|
|
|
2004-09-25 17:43:56 -04:00
|
|
|
/*
|
|
|
|
* db_enum_begin
|
|
|
|
*
|
|
|
|
* Begin to walk through an enum of the database. In order to retain
|
|
|
|
* some sanity in song ordering we scan all the songs and perform an
|
|
|
|
* insertion sort based on album, track, record id.
|
|
|
|
*
|
|
|
|
* Since the list is built entirely within this routine, it's also
|
|
|
|
* safe to release the lock once the list is built.
|
|
|
|
*
|
|
|
|
* Also we eliminate the static db_enum_helper which allows (the
|
|
|
|
* admittedly unnecessary) possibility of reentrance.
|
|
|
|
*
|
|
|
|
* this should be done quickly, as we'll be holding
|
|
|
|
* a reader lock on the db
|
2004-10-10 02:58:28 -04:00
|
|
|
*
|
|
|
|
* RP: Changed the read lock to a write lock, to serialize
|
|
|
|
* enums. On a background scan with multiple clients, the
|
|
|
|
* cost of doing multiple insertion sorts on the database simultaneously
|
|
|
|
* is way too painful. Particularly on embedded device
|
|
|
|
* like the NSLU2.
|
2004-09-25 17:43:56 -04:00
|
|
|
*/
|
2004-06-14 15:01:06 -04:00
|
|
|
ENUMHANDLE db_enum_begin(void) {
|
2004-02-17 19:18:02 -05:00
|
|
|
int err;
|
2004-06-14 15:01:06 -04:00
|
|
|
MP3HELPER* helper;
|
|
|
|
datum key, next;
|
2004-02-17 19:18:02 -05:00
|
|
|
|
2004-10-10 02:58:28 -04:00
|
|
|
db_writelock();
|
2004-02-17 19:18:02 -05:00
|
|
|
|
2004-06-14 15:01:06 -04:00
|
|
|
helper = calloc(1, sizeof(MP3HELPER));
|
2004-10-10 02:58:28 -04:00
|
|
|
|
2004-06-14 15:01:06 -04:00
|
|
|
for(key = gdbm_firstkey(db_songs) ; key.dptr ; key = next)
|
|
|
|
{
|
|
|
|
MP3RECORD* entry = calloc(1, sizeof(MP3RECORD));
|
|
|
|
MP3RECORD** root;
|
|
|
|
datum data;
|
|
|
|
|
|
|
|
data = gdbm_fetch(db_songs, key);
|
|
|
|
if(!data.dptr)
|
2004-11-13 02:14:26 -05:00
|
|
|
DPRINTF(E_FATAL,L_DB, "Cannot find item... corrupt database?\n");
|
2004-06-14 15:01:06 -04:00
|
|
|
|
|
|
|
if(db_unpackrecord(&data, &entry->mp3file))
|
2004-11-13 02:14:26 -05:00
|
|
|
DPRINTF(E_FATAL,L_DB, "Cannot unpack item... corrupt database?\n");
|
2004-06-14 15:01:06 -04:00
|
|
|
|
|
|
|
for(root = &helper->root ; *root ; root = &(**root).next)
|
|
|
|
{
|
|
|
|
if(compare(*root, entry) > 0)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
entry->next = *root;
|
|
|
|
*root = entry;
|
|
|
|
|
|
|
|
next = gdbm_nextkey(db_songs, key);
|
|
|
|
free(key.dptr);
|
|
|
|
}
|
|
|
|
|
2004-10-10 02:58:28 -04:00
|
|
|
db_unlock();
|
2004-06-14 15:01:06 -04:00
|
|
|
helper->next = helper->root;
|
2004-10-10 02:58:28 -04:00
|
|
|
|
2004-06-14 15:01:06 -04:00
|
|
|
return helper;
|
2004-02-17 19:18:02 -05:00
|
|
|
}
|
|
|
|
|
2004-10-10 02:58:28 -04:00
|
|
|
/*
|
|
|
|
* db_enum
|
|
|
|
*
|
|
|
|
* Walk to the next entry
|
|
|
|
*/
|
|
|
|
MP3FILE *db_enum(ENUMHANDLE *current) {
|
|
|
|
MP3HELPER* helper;
|
|
|
|
MP3RECORD* record;
|
|
|
|
|
|
|
|
if(!current)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
helper = *(MP3HELPER**) current;
|
|
|
|
record = helper->next;
|
|
|
|
|
|
|
|
if(helper->next == 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
helper->next = helper->next->next;
|
|
|
|
|
|
|
|
return &record->mp3file;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* db_enum_end
|
|
|
|
*
|
|
|
|
* dispose of the list we built up in db_enum_begin, the lock's
|
|
|
|
* already been released.
|
|
|
|
*/
|
|
|
|
int db_enum_end(ENUMHANDLE handle) {
|
|
|
|
MP3HELPER* helper = (MP3HELPER*) handle;
|
|
|
|
MP3RECORD* record;
|
|
|
|
|
|
|
|
while(helper->root)
|
|
|
|
{
|
|
|
|
MP3RECORD* record = helper->root;
|
|
|
|
|
|
|
|
helper->root = record->next;
|
|
|
|
|
2004-11-13 15:58:30 -05:00
|
|
|
db_dispose(&record->mp3file);
|
2004-10-10 02:58:28 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
free(helper);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#else /* Don't WANT_SORT */
|
|
|
|
|
|
|
|
ENUMHANDLE db_enum_begin(void) {
|
|
|
|
int err;
|
|
|
|
datum *pkey;
|
|
|
|
|
|
|
|
pkey=(datum *)malloc(sizeof(datum));
|
|
|
|
if(!pkey)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
db_writelock();
|
|
|
|
|
2004-11-14 01:45:13 -05:00
|
|
|
if(gdbm_mp3_mustfree) {
|
2004-11-14 02:24:37 -05:00
|
|
|
db_dispose(&gdbm_mp3);
|
2004-11-14 01:45:13 -05:00
|
|
|
}
|
|
|
|
gdbm_mp3_mustfree=0;
|
|
|
|
|
2004-10-10 02:58:28 -04:00
|
|
|
*pkey=gdbm_firstkey(db_songs);
|
|
|
|
return (ENUMHANDLE)pkey;
|
|
|
|
}
|
|
|
|
|
|
|
|
MP3FILE *db_enum(ENUMHANDLE *current) {
|
|
|
|
datum *pkey = *current;
|
2004-10-10 07:24:02 -04:00
|
|
|
datum next;
|
2004-10-10 02:58:28 -04:00
|
|
|
datum data;
|
2004-11-14 01:45:13 -05:00
|
|
|
|
|
|
|
if(gdbm_mp3_mustfree) {
|
2004-11-14 02:24:37 -05:00
|
|
|
db_dispose(&gdbm_mp3);
|
2004-11-14 01:45:13 -05:00
|
|
|
}
|
|
|
|
gdbm_mp3_mustfree=0;
|
2004-10-10 02:58:28 -04:00
|
|
|
|
|
|
|
if(pkey->dptr) {
|
|
|
|
data=gdbm_fetch(db_songs,*pkey);
|
|
|
|
if(!data.dptr)
|
2004-11-13 02:14:26 -05:00
|
|
|
DPRINTF(E_FATAL,L_DB, "Cannot find item.... corrupt database?\n");
|
2004-10-10 02:58:28 -04:00
|
|
|
|
2004-11-14 02:24:37 -05:00
|
|
|
if(db_unpackrecord(&data,&gdbm_mp3))
|
2004-11-13 02:14:26 -05:00
|
|
|
DPRINTF(E_FATAL,L_DB,"Cannot unpack item... corrupt database?\n");
|
2004-10-10 02:58:28 -04:00
|
|
|
|
2004-10-10 07:24:02 -04:00
|
|
|
free(data.dptr);
|
|
|
|
|
|
|
|
next = gdbm_nextkey(db_songs,*pkey);
|
2004-10-10 02:58:28 -04:00
|
|
|
free(pkey->dptr);
|
2004-10-10 07:24:02 -04:00
|
|
|
*pkey=next;
|
2004-10-10 02:58:28 -04:00
|
|
|
|
2004-11-14 02:24:37 -05:00
|
|
|
return &gdbm_mp3;
|
2004-10-10 02:58:28 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
int db_enum_end(ENUMHANDLE handle) {
|
2004-10-10 07:24:02 -04:00
|
|
|
datum *pkey = handle;
|
|
|
|
|
2004-11-14 01:45:13 -05:00
|
|
|
if(gdbm_mp3_mustfree) {
|
2004-11-14 02:24:37 -05:00
|
|
|
db_dispose(&gdbm_mp3);
|
2004-11-14 01:45:13 -05:00
|
|
|
}
|
|
|
|
gdbm_mp3_mustfree=0;
|
|
|
|
|
2004-10-10 07:24:02 -04:00
|
|
|
if(pkey->dptr)
|
|
|
|
free(pkey->dptr);
|
|
|
|
|
2004-10-10 02:58:28 -04:00
|
|
|
db_unlock();
|
2004-10-10 07:24:02 -04:00
|
|
|
free(pkey);
|
|
|
|
|
2004-10-10 02:58:28 -04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* WANT_SORT */
|
|
|
|
|
2004-02-17 19:18:02 -05:00
|
|
|
/*
|
|
|
|
* db_playlist_enum_begin
|
|
|
|
*
|
|
|
|
* Start enumerating playlists
|
|
|
|
*/
|
2004-06-14 15:01:06 -04:00
|
|
|
ENUMHANDLE db_playlist_enum_begin(void) {
|
2004-02-17 19:18:02 -05:00
|
|
|
int err;
|
|
|
|
DB_PLAYLIST *current;
|
|
|
|
|
2004-09-15 00:59:14 -04:00
|
|
|
db_readlock();
|
2004-02-17 19:18:02 -05:00
|
|
|
|
|
|
|
/* find first playlist with a song in it! */
|
|
|
|
current=db_playlists.next;
|
|
|
|
while(current && (!current->songs))
|
|
|
|
current=current->next;
|
|
|
|
|
|
|
|
return current;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* db_playlist_items_enum_begin
|
|
|
|
*
|
|
|
|
* Start enumerating playlist items
|
|
|
|
*/
|
2004-06-14 15:01:06 -04:00
|
|
|
ENUMHANDLE db_playlist_items_enum_begin(int playlistid) {
|
2004-02-17 19:18:02 -05:00
|
|
|
DB_PLAYLIST *current;
|
|
|
|
int err;
|
|
|
|
|
2004-09-15 00:59:14 -04:00
|
|
|
db_readlock();
|
2004-02-17 19:18:02 -05:00
|
|
|
|
|
|
|
current=db_playlists.next;
|
|
|
|
while(current && (current->id != playlistid))
|
|
|
|
current=current->next;
|
|
|
|
|
|
|
|
if(!current)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
return current->nodes;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* db_playlist_enum
|
|
|
|
*
|
|
|
|
* walk to the next entry
|
|
|
|
*/
|
2004-06-14 15:01:06 -04:00
|
|
|
int db_playlist_enum(ENUMHANDLE* handle) {
|
|
|
|
DB_PLAYLIST** current = (DB_PLAYLIST**) handle;
|
2004-02-17 19:18:02 -05:00
|
|
|
int retval;
|
|
|
|
DB_PLAYLIST *p;
|
|
|
|
|
|
|
|
if(*current) {
|
|
|
|
retval = (*current)->id;
|
|
|
|
p=*current;
|
|
|
|
p=p->next;
|
|
|
|
while(p && (!p->songs))
|
|
|
|
p=p->next;
|
|
|
|
|
|
|
|
*current=p;
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* db_playlist_items_enum
|
|
|
|
*
|
|
|
|
* walk to the next entry
|
|
|
|
*/
|
2004-06-14 15:01:06 -04:00
|
|
|
int db_playlist_items_enum(ENUMHANDLE* handle) {
|
2004-09-14 23:14:56 -04:00
|
|
|
DB_PLAYLISTENTRY **current;
|
2004-02-17 19:18:02 -05:00
|
|
|
int retval;
|
|
|
|
|
2004-09-14 23:14:56 -04:00
|
|
|
if(!handle)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
current = (DB_PLAYLISTENTRY**) handle;
|
|
|
|
|
2004-02-17 19:18:02 -05:00
|
|
|
if(*current) {
|
|
|
|
retval = (*current)->id;
|
|
|
|
*current=(*current)->next;
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* db_playlist_enum_end
|
|
|
|
*
|
|
|
|
* quit walking the database
|
|
|
|
*/
|
2004-06-14 15:01:06 -04:00
|
|
|
int db_playlist_enum_end(ENUMHANDLE handle) {
|
2004-09-15 00:59:14 -04:00
|
|
|
return db_unlock();
|
2004-02-17 19:18:02 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* db_playlist_items_enum_end
|
|
|
|
*
|
|
|
|
* Quit walking the database
|
|
|
|
*/
|
2004-06-14 15:01:06 -04:00
|
|
|
int db_playlist_items_enum_end(ENUMHANDLE handle) {
|
2004-09-15 00:59:14 -04:00
|
|
|
return db_unlock();
|
2004-02-17 19:18:02 -05:00
|
|
|
}
|
|
|
|
|
2004-03-08 14:21:03 -05:00
|
|
|
|
2004-02-17 19:18:02 -05:00
|
|
|
/*
|
|
|
|
* db_find
|
|
|
|
*
|
2004-03-03 01:50:09 -05:00
|
|
|
* Find a MP3FILE entry based on file id
|
2004-02-17 19:18:02 -05:00
|
|
|
*/
|
2004-03-03 01:50:09 -05:00
|
|
|
MP3FILE *db_find(int id) { /* FIXME: Not reentrant */
|
2004-09-19 02:00:07 -04:00
|
|
|
MP3FILE *pmp3=NULL;
|
2004-03-03 01:50:09 -05:00
|
|
|
datum key, content;
|
|
|
|
|
|
|
|
key.dptr=(char*)&id;
|
|
|
|
key.dsize=sizeof(int);
|
|
|
|
|
|
|
|
content=gdbm_fetch(db_songs,key);
|
2004-03-03 13:51:12 -05:00
|
|
|
MEMNOTIFY(content.dptr);
|
2004-03-03 01:50:09 -05:00
|
|
|
if(!content.dptr)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
pmp3=(MP3FILE*)malloc(sizeof(MP3FILE));
|
|
|
|
if(!pmp3)
|
2004-02-17 19:18:02 -05:00
|
|
|
return NULL;
|
|
|
|
|
2004-03-03 01:50:09 -05:00
|
|
|
db_unpackrecord(&content,pmp3);
|
2004-09-19 02:00:07 -04:00
|
|
|
free(content.dptr);
|
2004-03-03 01:50:09 -05:00
|
|
|
return pmp3;
|
2004-02-17 19:18:02 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* db_get_playlist_count
|
|
|
|
*
|
|
|
|
* return the number of playlists
|
|
|
|
*/
|
|
|
|
int db_get_playlist_count(void) {
|
|
|
|
return db_playlist_count;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* db_get_song_count
|
|
|
|
*
|
|
|
|
* return the number of songs in the database. Used for the /database
|
|
|
|
* request
|
|
|
|
*/
|
|
|
|
int db_get_song_count(void) {
|
|
|
|
return db_song_count;
|
|
|
|
}
|
|
|
|
|
2004-03-11 00:47:15 -05:00
|
|
|
|
|
|
|
/*
|
|
|
|
* db_get_playlist_is_smart
|
|
|
|
*
|
|
|
|
* return whether or not the playlist is a "smart" playlist
|
|
|
|
*/
|
|
|
|
int db_get_playlist_is_smart(int playlistid) {
|
|
|
|
DB_PLAYLIST *current;
|
|
|
|
int err;
|
|
|
|
int result;
|
|
|
|
|
2004-09-15 00:59:14 -04:00
|
|
|
db_readlock();
|
2004-03-11 00:47:15 -05:00
|
|
|
|
|
|
|
current=db_playlists.next;
|
|
|
|
while(current && (current->id != playlistid))
|
|
|
|
current=current->next;
|
|
|
|
|
|
|
|
if(!current) {
|
|
|
|
result=0;
|
|
|
|
} else {
|
2004-03-13 19:47:42 -05:00
|
|
|
result=current->is_smart;
|
2004-03-11 00:47:15 -05:00
|
|
|
}
|
|
|
|
|
2004-09-15 00:59:14 -04:00
|
|
|
db_unlock();
|
2004-03-11 00:47:15 -05:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2004-02-17 19:18:02 -05:00
|
|
|
/*
|
|
|
|
* db_get_playlist_entry_count
|
|
|
|
*
|
|
|
|
* return the number of songs in a particular playlist
|
|
|
|
*/
|
|
|
|
int db_get_playlist_entry_count(int playlistid) {
|
|
|
|
int count;
|
|
|
|
DB_PLAYLIST *current;
|
|
|
|
int err;
|
|
|
|
|
2004-09-15 00:59:14 -04:00
|
|
|
db_readlock();
|
2004-02-17 19:18:02 -05:00
|
|
|
|
|
|
|
current=db_playlists.next;
|
|
|
|
while(current && (current->id != playlistid))
|
|
|
|
current=current->next;
|
|
|
|
|
|
|
|
if(!current) {
|
|
|
|
count = -1;
|
|
|
|
} else {
|
|
|
|
count = current->songs;
|
|
|
|
}
|
|
|
|
|
2004-09-15 00:59:14 -04:00
|
|
|
db_unlock();
|
2004-02-17 19:18:02 -05:00
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* db_get_playlist_name
|
|
|
|
*
|
|
|
|
* return the name of a playlist
|
|
|
|
*
|
|
|
|
* FIXME: Small race here
|
|
|
|
*/
|
|
|
|
char *db_get_playlist_name(int playlistid) {
|
|
|
|
char *name;
|
|
|
|
DB_PLAYLIST *current;
|
|
|
|
int err;
|
|
|
|
|
2004-09-15 00:59:14 -04:00
|
|
|
db_readlock();
|
2004-02-17 19:18:02 -05:00
|
|
|
|
|
|
|
current=db_playlists.next;
|
|
|
|
while(current && (current->id != playlistid))
|
|
|
|
current=current->next;
|
|
|
|
|
|
|
|
if(!current) {
|
|
|
|
name = NULL;
|
|
|
|
} else {
|
|
|
|
name = current->name;
|
|
|
|
}
|
|
|
|
|
2004-09-15 00:59:14 -04:00
|
|
|
db_unlock();
|
2004-02-17 19:18:02 -05:00
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
2004-03-08 14:21:03 -05:00
|
|
|
|
|
|
|
/*
|
|
|
|
* db_exists
|
|
|
|
*
|
|
|
|
* Check if a particular ID exists or not
|
|
|
|
*/
|
|
|
|
int db_exists(int id) {
|
2004-03-13 19:47:42 -05:00
|
|
|
int *node;
|
|
|
|
int err;
|
2004-03-08 14:21:03 -05:00
|
|
|
MP3FILE *pmp3;
|
2004-09-19 01:18:49 -04:00
|
|
|
datum key,content;
|
2004-03-08 14:21:03 -05:00
|
|
|
|
2004-11-13 02:14:26 -05:00
|
|
|
DPRINTF(E_DBG,L_DB,"Checking if node %d in db\n",id);
|
2004-09-19 01:18:49 -04:00
|
|
|
key.dptr=(char*)&id;
|
|
|
|
key.dsize=sizeof(int);
|
2004-03-13 19:47:42 -05:00
|
|
|
|
2004-09-19 01:18:49 -04:00
|
|
|
content=gdbm_fetch(db_songs,key);
|
|
|
|
MEMNOTIFY(content.dptr);
|
|
|
|
if(!content.dptr) {
|
2004-11-13 02:14:26 -05:00
|
|
|
DPRINTF(E_DBG,L_DB,"Nope! Not in DB\n");
|
2004-09-19 01:18:49 -04:00
|
|
|
return 0;
|
|
|
|
}
|
2004-03-13 19:47:42 -05:00
|
|
|
|
|
|
|
if(db_update_mode) {
|
|
|
|
/* knock it off the maybe list */
|
2004-09-18 23:06:08 -04:00
|
|
|
node = (int*)rbdelete((void*)&id,db_removed);
|
2004-03-13 19:47:42 -05:00
|
|
|
if(node) {
|
2004-11-13 02:14:26 -05:00
|
|
|
DPRINTF(E_DBG,L_DB,"Knocked node %d from the list\n",*node);
|
2004-03-13 19:47:42 -05:00
|
|
|
free(node);
|
|
|
|
}
|
|
|
|
}
|
2004-09-19 01:18:49 -04:00
|
|
|
|
|
|
|
free(content.dptr);
|
2004-11-13 02:14:26 -05:00
|
|
|
DPRINTF(E_DBG,L_DB,"Yup, in database\n");
|
2004-09-19 01:18:49 -04:00
|
|
|
return 1;
|
2004-03-08 14:21:03 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* db_last_modified
|
|
|
|
*
|
|
|
|
* See when the file was last updated in the database
|
|
|
|
*/
|
|
|
|
int db_last_modified(int id) {
|
2004-03-13 19:47:42 -05:00
|
|
|
int retval;
|
2004-03-08 14:21:03 -05:00
|
|
|
MP3FILE *pmp3;
|
2004-03-13 19:47:42 -05:00
|
|
|
int err;
|
|
|
|
|
2004-03-08 14:21:03 -05:00
|
|
|
pmp3=db_find(id);
|
2004-03-13 19:47:42 -05:00
|
|
|
if(!pmp3) {
|
|
|
|
retval=0;
|
|
|
|
} else {
|
|
|
|
retval=pmp3->time_modified;
|
|
|
|
}
|
|
|
|
|
2004-09-19 02:00:07 -04:00
|
|
|
if(pmp3) {
|
2004-11-13 15:58:30 -05:00
|
|
|
db_dispose(pmp3);
|
2004-09-19 02:00:07 -04:00
|
|
|
free(pmp3);
|
|
|
|
}
|
2004-03-13 19:47:42 -05:00
|
|
|
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* db_delete
|
|
|
|
*
|
|
|
|
* Delete an item from the database, and also remove it
|
|
|
|
* from any playlists.
|
|
|
|
*/
|
|
|
|
int db_delete(int id) {
|
|
|
|
int err;
|
|
|
|
datum key;
|
|
|
|
DB_PLAYLIST *pcurrent;
|
|
|
|
DB_PLAYLISTENTRY *phead, *ptail;
|
|
|
|
|
2004-11-13 02:14:26 -05:00
|
|
|
DPRINTF(E_DBG,L_DB,"Removing item %d\n",id);
|
2004-03-08 14:21:03 -05:00
|
|
|
|
2004-03-13 19:47:42 -05:00
|
|
|
if(db_exists(id)) {
|
|
|
|
key.dptr=(void*)&id;
|
|
|
|
key.dsize=sizeof(int);
|
|
|
|
gdbm_delete(db_songs,key);
|
|
|
|
db_song_count--;
|
2004-09-15 00:59:14 -04:00
|
|
|
db_version_no++;
|
2004-03-13 19:47:42 -05:00
|
|
|
|
|
|
|
/* walk the playlists and remove the item */
|
|
|
|
pcurrent=db_playlists.next;
|
|
|
|
while(pcurrent) {
|
|
|
|
phead=ptail=pcurrent->nodes;
|
|
|
|
while(phead && (phead->id != id)) {
|
|
|
|
ptail=phead;
|
|
|
|
phead=phead->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(phead) { /* found it */
|
2004-11-13 02:14:26 -05:00
|
|
|
DPRINTF(E_DBG,L_DB|L_PL,"Removing from playlist %d\n",
|
2004-03-13 19:47:42 -05:00
|
|
|
pcurrent->id);
|
|
|
|
if(phead == pcurrent->nodes) {
|
|
|
|
pcurrent->nodes=phead->next;
|
|
|
|
} else {
|
|
|
|
ptail->next=phead->next;
|
|
|
|
}
|
|
|
|
free(phead);
|
2004-09-14 23:03:45 -04:00
|
|
|
|
|
|
|
if(pcurrent->nodes == NULL) {
|
2004-11-13 02:14:26 -05:00
|
|
|
DPRINTF(E_DBG,L_DB|L_PL,"Empty Playlist!\n");
|
2004-09-14 23:03:45 -04:00
|
|
|
db_playlist_count--;
|
|
|
|
}
|
2004-03-13 19:47:42 -05:00
|
|
|
}
|
|
|
|
pcurrent=pcurrent->next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-09-15 00:59:14 -04:00
|
|
|
db_version_no++;
|
|
|
|
|
2004-03-13 19:47:42 -05:00
|
|
|
return 0;
|
2004-03-08 14:21:03 -05:00
|
|
|
}
|