SongUpdate.cxx 4.04 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
#include "Song.hxx"
Max Kellermann's avatar
Max Kellermann committed
22
#include "util/UriUtil.hxx"
23
#include "util/Error.hxx"
24
#include "Directory.hxx"
Max Kellermann's avatar
Max Kellermann committed
25
#include "Mapper.hxx"
26
#include "fs/Path.hxx"
27
#include "fs/FileSystem.hxx"
28
#include "InputStream.hxx"
29
#include "DecoderPlugin.hxx"
30
#include "DecoderList.hxx"
31 32
#include "tag/Tag.hxx"
#include "tag/TagHandler.hxx"
33 34
#include "tag/TagId3.hxx"
#include "tag/ApeTag.hxx"
35

36 37 38 39 40
#include <glib.h>

#include <assert.h>
#include <sys/types.h>
#include <sys/stat.h>
41
#include <stdio.h>
42

43 44
Song *
Song::LoadFile(const char *path_utf8, Directory *parent)
45
{
46
	Song *song;
47 48
	bool ret;

49 50 51
	assert((parent == NULL) == g_path_is_absolute(path_utf8));
	assert(!uri_has_scheme(path_utf8));
	assert(strchr(path_utf8, '\n') == NULL);
52

53
	song = NewFile(path_utf8, parent);
54 55 56

	//in archive ?
	if (parent != NULL && parent->device == DEVICE_INARCHIVE) {
57
		ret = song->UpdateFileInArchive();
58
	} else {
59
		ret = song->UpdateFile();
60 61
	}
	if (!ret) {
62
		song->Free();
63 64 65 66 67 68 69 70 71
		return NULL;
	}

	return song;
}

/**
 * Attempts to load APE or ID3 tags from the specified file.
 */
72 73 74
static bool
tag_scan_fallback(const char *path,
		  const struct tag_handler *handler, void *handler_ctx)
75
{
76 77
	return tag_ape_scan2(path, handler, handler_ctx) ||
		tag_id3_scan(path, handler, handler_ctx);
78 79 80
}

bool
81
Song::UpdateFile()
82 83 84 85
{
	const char *suffix;
	const struct decoder_plugin *plugin;
	struct stat st;
86
	struct input_stream *is = NULL;
87

88
	assert(IsFile());
89 90 91

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

92
	suffix = uri_get_suffix(uri);
93 94 95
	if (suffix == NULL)
		return false;

96
	plugin = decoder_plugin_from_suffix(suffix, NULL);
97 98 99
	if (plugin == NULL)
		return false;

100
	const Path path_fs = map_song_fs(this);
101
	if (path_fs.IsNull())
102 103
		return false;

Max Kellermann's avatar
Max Kellermann committed
104 105
	delete tag;
	tag = nullptr;
106

107
	if (!StatFile(path_fs, st) || !S_ISREG(st.st_mode)) {
108 109 110
		return false;
	}

111
	mtime = st.st_mtime;
112

113 114
	Mutex mutex;
	Cond cond;
115

116
	do {
117
		/* load file tag */
Max Kellermann's avatar
Max Kellermann committed
118
		tag = new Tag();
119
		if (decoder_plugin_scan_file(plugin, path_fs.c_str(),
120
					     &full_tag_handler, tag))
121 122
			break;

Max Kellermann's avatar
Max Kellermann committed
123
		delete tag;
124
		tag = nullptr;
125

126
		/* fall back to stream tag */
127
		if (plugin->scan_stream != NULL) {
128 129
			/* open the input_stream (if not already
			   open) */
130 131 132 133
			if (is == NULL)
				is = input_stream::Open(path_fs.c_str(),
							mutex, cond,
							IgnoreError());
134 135

			/* now try the stream_tag() method */
136
			if (is != NULL) {
Max Kellermann's avatar
Max Kellermann committed
137
				tag = new Tag();
138
				if (decoder_plugin_scan_stream(plugin, is,
139
							       &full_tag_handler,
140
							       tag))
141 142
					break;

Max Kellermann's avatar
Max Kellermann committed
143
				delete tag;
144
				tag = nullptr;
145

146
				is->LockSeek(0, SEEK_SET, IgnoreError());
147 148 149
			}
		}

150
		plugin = decoder_plugin_from_suffix(suffix, plugin);
151 152
	} while (plugin != NULL);

153
	if (is != NULL)
154
		is->Close();
155

Max Kellermann's avatar
Max Kellermann committed
156
	if (tag != nullptr && tag->IsEmpty())
157
		tag_scan_fallback(path_fs.c_str(), &full_tag_handler, tag);
158

159
	return tag != nullptr;
160 161 162
}

bool
163
Song::UpdateFileInArchive()
164 165 166 167
{
	const char *suffix;
	const struct decoder_plugin *plugin;

168
	assert(IsFile());
169 170 171

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

172
	suffix = uri_get_suffix(uri);
173 174 175
	if (suffix == NULL)
		return false;

176
	plugin = decoder_plugin_from_suffix(suffix, NULL);
177 178 179
	if (plugin == NULL)
		return false;

Max Kellermann's avatar
Max Kellermann committed
180
	delete tag;
181 182

	//accept every file that has music suffix
183
	//because we don't support tag reading through
184
	//input streams
Max Kellermann's avatar
Max Kellermann committed
185
	tag = new Tag();
186 187 188

	return true;
}