SongUpdate.cxx 4.47 KB
Newer Older
1
/*
2
 * Copyright (C) 2003-2013 The Music Player Daemon Project
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
 * 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.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

20
#include "config.h" /* must be first for large file support */
21 22

extern "C" {
23 24
#include "song.h"
#include "uri.h"
25 26
}

27
#include "Directory.hxx"
Max Kellermann's avatar
Max Kellermann committed
28
#include "Mapper.hxx"
29 30

extern "C" {
31 32 33 34 35
#include "decoder_list.h"
#include "decoder_plugin.h"
#include "tag_ape.h"
#include "tag_id3.h"
#include "tag.h"
36
#include "tag_handler.h"
37
#include "input_stream.h"
38
}
39 40 41 42 43 44

#include <glib.h>

#include <assert.h>
#include <sys/types.h>
#include <sys/stat.h>
45
#include <stdio.h>
46 47

struct song *
48
song_file_load(const char *path, Directory *parent)
49 50 51 52
{
	struct song *song;
	bool ret;

53
	assert((parent == NULL) == g_path_is_absolute(path));
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
	assert(!uri_has_scheme(path));
	assert(strchr(path, '\n') == NULL);

	song = song_file_new(path, parent);

	//in archive ?
	if (parent != NULL && parent->device == DEVICE_INARCHIVE) {
		ret = song_file_update_inarchive(song);
	} else {
		ret = song_file_update(song);
	}
	if (!ret) {
		song_free(song);
		return NULL;
	}

	return song;
}

/**
 * Attempts to load APE or ID3 tags from the specified file.
 */
76 77 78
static bool
tag_scan_fallback(const char *path,
		  const struct tag_handler *handler, void *handler_ctx)
79
{
80 81
	return tag_ape_scan2(path, handler, handler_ctx) ||
		tag_id3_scan(path, handler, handler_ctx);
82 83 84 85 86 87 88 89 90
}

bool
song_file_update(struct song *song)
{
	const char *suffix;
	char *path_fs;
	const struct decoder_plugin *plugin;
	struct stat st;
91
	struct input_stream *is = NULL;
92 93 94 95 96

	assert(song_is_file(song));

	/* check if there's a suffix and a plugin */

97
	suffix = uri_get_suffix(song->uri);
98 99 100
	if (suffix == NULL)
		return false;

101
	plugin = decoder_plugin_from_suffix(suffix, NULL);
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
	if (plugin == NULL)
		return false;

	path_fs = map_song_fs(song);
	if (path_fs == NULL)
		return false;

	if (song->tag != NULL) {
		tag_free(song->tag);
		song->tag = NULL;
	}

	if (stat(path_fs, &st) < 0 || !S_ISREG(st.st_mode)) {
		g_free(path_fs);
		return false;
	}

	song->mtime = st.st_mtime;

121 122
	GMutex *mutex = NULL;
	GCond *cond;
123 124 125 126 127
#if !GCC_CHECK_VERSION(4, 2)
	/* work around "may be used uninitialized in this function"
	   false positive */
	cond = NULL;
#endif
128

129
	do {
130
		/* load file tag */
131 132
		song->tag = tag_new();
		if (decoder_plugin_scan_file(plugin, path_fs,
133
					     &full_tag_handler, song->tag))
134 135
			break;

136 137 138
		tag_free(song->tag);
		song->tag = NULL;

139
		/* fall back to stream tag */
140
		if (plugin->scan_stream != NULL) {
141 142
			/* open the input_stream (if not already
			   open) */
143 144 145 146 147 148
			if (is == NULL) {
				mutex = g_mutex_new();
				cond = g_cond_new();
				is = input_stream_open(path_fs, mutex, cond,
						       NULL);
			}
149 150

			/* now try the stream_tag() method */
151
			if (is != NULL) {
152 153
				song->tag = tag_new();
				if (decoder_plugin_scan_stream(plugin, is,
154
							       &full_tag_handler,
155
							       song->tag))
156 157
					break;

158 159 160
				tag_free(song->tag);
				song->tag = NULL;

161
				input_stream_lock_seek(is, 0, SEEK_SET, NULL);
162 163 164
			}
		}

165
		plugin = decoder_plugin_from_suffix(suffix, plugin);
166 167
	} while (plugin != NULL);

168 169
	if (is != NULL)
		input_stream_close(is);
170

171 172 173 174 175
	if (mutex != NULL) {
		g_cond_free(cond);
		g_mutex_free(mutex);
	}

176
	if (song->tag != NULL && tag_is_empty(song->tag))
177
		tag_scan_fallback(path_fs, &full_tag_handler, song->tag);
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192

	g_free(path_fs);
	return song->tag != NULL;
}

bool
song_file_update_inarchive(struct song *song)
{
	const char *suffix;
	const struct decoder_plugin *plugin;

	assert(song_is_file(song));

	/* check if there's a suffix and a plugin */

193
	suffix = uri_get_suffix(song->uri);
194 195 196
	if (suffix == NULL)
		return false;

197
	plugin = decoder_plugin_from_suffix(suffix, nullptr);
198 199 200 201 202 203 204
	if (plugin == NULL)
		return false;

	if (song->tag != NULL)
		tag_free(song->tag);

	//accept every file that has music suffix
205
	//because we don't support tag reading through
206 207 208 209 210
	//input streams
	song->tag = tag_new();

	return true;
}