2004-02-24 00:41:20 +01:00
|
|
|
/* the Music Player Daemon (MPD)
|
2007-04-05 05:22:33 +02:00
|
|
|
* Copyright (C) 2003-2007 by Warren Dukes (warren.dukes@gmail.com)
|
2004-02-24 00:41:20 +01:00
|
|
|
* This project's homepage is: http://www.musicpd.org
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "song.h"
|
|
|
|
#include "ls.h"
|
|
|
|
#include "directory.h"
|
|
|
|
#include "utils.h"
|
|
|
|
#include "log.h"
|
2008-10-14 11:10:49 +02:00
|
|
|
#include "mapper.h"
|
2004-03-10 03:38:31 +01:00
|
|
|
#include "path.h"
|
2004-03-10 10:55:54 +01:00
|
|
|
#include "playlist.h"
|
2008-08-26 08:27:08 +02:00
|
|
|
#include "decoder_list.h"
|
2008-08-26 08:27:08 +02:00
|
|
|
#include "decoder_api.h"
|
2008-09-23 20:48:39 +02:00
|
|
|
|
2008-12-29 17:28:32 +01:00
|
|
|
#include <assert.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
2008-10-08 11:05:25 +02:00
|
|
|
static struct song *
|
2008-10-08 10:49:05 +02:00
|
|
|
song_alloc(const char *url, struct directory *parent)
|
2008-10-06 18:46:52 +02:00
|
|
|
{
|
2008-10-07 21:20:34 +02:00
|
|
|
size_t urllen;
|
2008-10-08 10:49:11 +02:00
|
|
|
struct song *song;
|
2008-10-07 21:20:34 +02:00
|
|
|
|
|
|
|
assert(url);
|
|
|
|
urllen = strlen(url);
|
|
|
|
assert(urllen);
|
|
|
|
song = xmalloc(sizeof(*song) - sizeof(song->url) + urllen + 1);
|
2008-10-06 18:46:52 +02:00
|
|
|
|
|
|
|
song->tag = NULL;
|
|
|
|
memcpy(song->url, url, urllen + 1);
|
2008-10-08 11:05:34 +02:00
|
|
|
song->parent = parent;
|
2008-10-06 18:46:52 +02:00
|
|
|
|
|
|
|
return song;
|
|
|
|
}
|
2004-03-10 10:55:54 +01:00
|
|
|
|
2008-10-08 10:49:11 +02:00
|
|
|
struct song *
|
2008-10-08 11:05:25 +02:00
|
|
|
song_remote_new(const char *url)
|
|
|
|
{
|
|
|
|
return song_alloc(url, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct song *
|
|
|
|
song_file_new(const char *path, struct directory *parent)
|
|
|
|
{
|
2008-10-15 22:35:13 +02:00
|
|
|
assert((parent == NULL) == (*path == '/'));
|
2008-10-08 11:05:25 +02:00
|
|
|
|
|
|
|
return song_alloc(path, parent);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct song *
|
|
|
|
song_file_load(const char *path, struct directory *parent)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2008-10-08 10:49:11 +02:00
|
|
|
struct song *song;
|
2008-10-08 11:06:27 +02:00
|
|
|
bool ret;
|
2004-06-02 04:07:07 +02:00
|
|
|
|
2008-10-15 22:35:13 +02:00
|
|
|
assert((parent == NULL) == (*path == '/'));
|
2008-10-08 11:05:25 +02:00
|
|
|
|
|
|
|
if (strchr(path, '\n')) {
|
|
|
|
DEBUG("newSong: '%s' is not a valid uri\n", path);
|
2005-09-08 23:08:02 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
2004-06-02 04:07:07 +02:00
|
|
|
|
2008-10-08 11:05:25 +02:00
|
|
|
song = song_file_new(path, parent);
|
|
|
|
|
2008-12-16 21:42:42 +01:00
|
|
|
//in archive ?
|
|
|
|
if (parent->device == DEVICE_INARCHIVE) {
|
|
|
|
ret = song_file_update_inarchive(song);
|
|
|
|
} else {
|
|
|
|
ret = song_file_update(song);
|
|
|
|
}
|
2008-10-08 11:06:27 +02:00
|
|
|
if (!ret) {
|
2008-10-08 11:05:34 +02:00
|
|
|
song_free(song);
|
2008-10-08 11:05:25 +02:00
|
|
|
return NULL;
|
2004-03-10 03:38:31 +01:00
|
|
|
}
|
|
|
|
|
2004-02-24 00:41:20 +01:00
|
|
|
return song;
|
|
|
|
}
|
|
|
|
|
2008-10-08 10:49:11 +02:00
|
|
|
void
|
2008-10-08 11:05:34 +02:00
|
|
|
song_free(struct song *song)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
|
|
|
if (song->tag)
|
2008-08-29 09:38:21 +02:00
|
|
|
tag_free(song->tag);
|
2004-04-10 19:56:01 +02:00
|
|
|
free(song);
|
|
|
|
}
|
|
|
|
|
2008-10-08 11:06:26 +02:00
|
|
|
bool
|
2008-10-08 11:05:34 +02:00
|
|
|
song_file_update(struct song *song)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2008-10-31 16:47:21 +01:00
|
|
|
char buffer[MPD_PATH_MAX];
|
|
|
|
const char *path_fs;
|
2008-11-01 14:51:41 +01:00
|
|
|
const struct decoder_plugin *plugin;
|
2008-10-08 11:05:38 +02:00
|
|
|
unsigned int next = 0;
|
2008-10-13 16:56:00 +02:00
|
|
|
struct stat st;
|
2008-01-01 11:10:08 +01:00
|
|
|
|
2008-10-08 11:05:38 +02:00
|
|
|
assert(song_is_file(song));
|
2004-05-31 03:21:17 +02:00
|
|
|
|
2008-10-31 16:47:21 +01:00
|
|
|
path_fs = map_song_fs(song, buffer);
|
|
|
|
if (path_fs == NULL)
|
|
|
|
return false;
|
2004-03-10 10:55:54 +01:00
|
|
|
|
2008-10-08 11:05:38 +02:00
|
|
|
if (song->tag != NULL) {
|
|
|
|
tag_free(song->tag);
|
2004-05-13 20:16:03 +02:00
|
|
|
song->tag = NULL;
|
|
|
|
}
|
2004-03-10 10:55:54 +01:00
|
|
|
|
2008-10-31 16:47:21 +01:00
|
|
|
if (stat(path_fs, &st) < 0 || !S_ISREG(st.st_mode))
|
2008-10-13 16:56:00 +02:00
|
|
|
return false;
|
|
|
|
|
|
|
|
song->mtime = st.st_mtime;
|
|
|
|
|
2008-10-08 11:05:38 +02:00
|
|
|
while (song->tag == NULL &&
|
2008-10-31 16:47:21 +01:00
|
|
|
(plugin = hasMusicSuffix(path_fs, next++)))
|
|
|
|
song->tag = plugin->tag_dup(path_fs);
|
2008-10-08 11:05:38 +02:00
|
|
|
|
2008-10-13 16:56:00 +02:00
|
|
|
return song->tag != NULL;
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
|
|
|
|
2008-12-16 21:42:42 +01:00
|
|
|
bool
|
|
|
|
song_file_update_inarchive(struct song *song)
|
|
|
|
{
|
|
|
|
char buffer[MPD_PATH_MAX];
|
|
|
|
const char *path_fs;
|
|
|
|
const struct decoder_plugin *plugin;
|
|
|
|
|
|
|
|
assert(song_is_file(song));
|
|
|
|
|
|
|
|
path_fs = map_song_fs(song, buffer);
|
|
|
|
if (path_fs == NULL)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (song->tag != NULL) {
|
|
|
|
tag_free(song->tag);
|
|
|
|
song->tag = NULL;
|
|
|
|
}
|
|
|
|
//accept every file that has music suffix
|
|
|
|
//because we dont support tag reading throught
|
|
|
|
//input streams
|
|
|
|
plugin = hasMusicSuffix(path_fs, 0);
|
|
|
|
if (plugin) {
|
|
|
|
song->tag = tag_new();
|
|
|
|
//tag_add_item(tag, TAG_ITEM_TITLE, f->title);
|
|
|
|
}
|
|
|
|
return song->tag != NULL;
|
|
|
|
}
|
|
|
|
|
2008-10-08 10:49:11 +02:00
|
|
|
char *
|
2008-10-14 11:10:44 +02:00
|
|
|
song_get_url(const struct song *song, char *path_max_tmp)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2008-10-08 11:06:37 +02:00
|
|
|
assert(song != NULL);
|
2008-10-06 18:46:52 +02:00
|
|
|
assert(*song->url);
|
2008-08-26 08:27:11 +02:00
|
|
|
|
2008-10-13 09:39:02 +02:00
|
|
|
if (!song->parent || isRootDirectory(song->parent->path))
|
2007-12-28 03:56:25 +01:00
|
|
|
strcpy(path_max_tmp, song->url);
|
|
|
|
else
|
|
|
|
pfx_dir(path_max_tmp, song->url, strlen(song->url),
|
2008-10-08 11:07:58 +02:00
|
|
|
directory_get_path(song->parent),
|
|
|
|
strlen(directory_get_path(song->parent)));
|
2007-12-28 03:56:25 +01:00
|
|
|
return path_max_tmp;
|
2004-11-11 03:36:25 +01:00
|
|
|
}
|