You need to sign in or sign up before continuing.
SongUpdate.cxx 4.65 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2021 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/DetachedSong.hxx"
22 23
#include "db/plugins/simple/Song.hxx"
#include "db/plugins/simple/Directory.hxx"
24 25
#include "storage/StorageInterface.hxx"
#include "storage/FileInfo.hxx"
26
#include "decoder/DecoderList.hxx"
27
#include "fs/AllocatedPath.hxx"
28
#include "fs/FileInfo.hxx"
29
#include "tag/Builder.hxx"
30
#include "TagFile.hxx"
31
#include "TagStream.hxx"
Max Kellermann's avatar
Max Kellermann committed
32
#include "util/UriExtract.hxx"
33

34 35 36 37
#ifdef ENABLE_ARCHIVE
#include "TagArchive.hxx"
#endif

38 39
#include <cassert>

40
#include <string.h>
41

42 43
#ifdef ENABLE_DATABASE

44 45 46 47 48 49 50 51
bool
Song::IsPluginAvailable() const noexcept
{
	const char *suffix = GetFilenameSuffix();
	return suffix != nullptr &&
		decoder_plugins_supports_suffix(suffix);
}

52
SongPtr
53
Song::LoadFile(Storage &storage, const char *path_utf8, Directory &parent)
54
{
55
	assert(!uri_has_scheme(path_utf8));
Rosen Penev's avatar
Rosen Penev committed
56
	assert(std::strchr(path_utf8, '\n') == nullptr);
57

58
	auto song = std::make_unique<Song>(path_utf8, parent);
59
	if (!song->UpdateFile(storage))
60
		return nullptr;
61 62 63 64

	return song;
}

65 66 67 68
#endif

#ifdef ENABLE_DATABASE

69
bool
70
Song::UpdateFile(Storage &storage)
71
{
72
	const auto &relative_uri = GetURI();
73

74
	const auto info = storage.GetInfo(relative_uri.c_str(), true);
75
	if (!info.IsRegular())
76 77
		return false;

78
	TagBuilder tag_builder;
79
	auto new_audio_format = AudioFormat::Undefined();
80

81 82 83 84 85 86
	try {
		const auto path_fs = storage.MapFS(relative_uri.c_str());
		if (path_fs.IsNull()) {
			const auto absolute_uri =
				storage.MapUTF8(relative_uri.c_str());
			if (!tag_stream_scan(absolute_uri.c_str(), tag_builder,
87
					     &new_audio_format))
88 89 90 91 92 93 94 95 96
				return false;
		} else {
			if (!ScanFileTagsWithGeneric(path_fs, tag_builder,
						     &new_audio_format))
				return false;
		}
	} catch (...) {
		// TODO: log or propagate I/O errors?
		return false;
97
	}
98

99
	mtime = info.mtime;
100
	audio_format = new_audio_format;
101
	tag_builder.Commit(tag);
102
	return true;
103 104
}

105 106
#ifdef ENABLE_ARCHIVE

107
SongPtr
108
Song::LoadFromArchive(ArchiveFile &archive, const char *name_utf8,
109
		      Directory &parent) noexcept
110 111
{
	assert(!uri_has_scheme(name_utf8));
Rosen Penev's avatar
Rosen Penev committed
112
	assert(std::strchr(name_utf8, '\n') == nullptr);
113

114
	auto song = std::make_unique<Song>(name_utf8, parent);
115
	if (!song->UpdateFileInArchive(archive))
116 117 118 119 120 121
		return nullptr;

	return song;
}

bool
122
Song::UpdateFileInArchive(ArchiveFile &archive) noexcept
123
{
124
	assert(parent.device == DEVICE_INARCHIVE);
125

126
	std::string path_utf8(filename);
127

128
	for (const Directory *directory = &parent;
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
	     directory->parent != nullptr &&
		     directory->parent->device == DEVICE_INARCHIVE;
	     directory = directory->parent) {
		path_utf8.insert(path_utf8.begin(), '/');
		path_utf8.insert(0, directory->GetName());
	}

	TagBuilder tag_builder;
	if (!tag_archive_scan(archive, path_utf8.c_str(), tag_builder))
		return false;

	tag_builder.Commit(tag);
	return true;
}

144 145
#endif

146 147
#endif /* ENABLE_DATABASE */

148
bool
149
DetachedSong::LoadFile(Path path)
150
{
151 152
	const FileInfo fi(path);
	if (!fi.IsRegular())
153 154 155
		return false;

	TagBuilder tag_builder;
156
	auto new_audio_format = AudioFormat::Undefined();
157 158

	try {
159
		if (!ScanFileTagsWithGeneric(path, tag_builder, &new_audio_format))
160 161 162
			return false;
	} catch (...) {
		// TODO: log or propagate I/O errors?
163
		return false;
164
	}
165

166
	mtime = fi.GetModificationTime();
167
	audio_format = new_audio_format;
168 169 170 171
	tag_builder.Commit(tag);
	return true;
}

172
bool
173
DetachedSong::Update()
174 175 176
{
	if (IsAbsoluteFile()) {
		const AllocatedPath path_fs =
177
			AllocatedPath::FromUTF8Throw(GetRealURI());
178

179
		return LoadFile(path_fs);
180 181
	} else if (IsRemote()) {
		TagBuilder tag_builder;
182
		auto new_audio_format = AudioFormat::Undefined();
183 184

		try {
185 186
			if (!tag_stream_scan(uri.c_str(), tag_builder,
					     &new_audio_format))
187 188 189
				return false;
		} catch (...) {
			// TODO: log or propagate I/O errors?
190
			return false;
191
		}
192

193
		mtime = std::chrono::system_clock::time_point::min();
194
		audio_format = new_audio_format;
195 196 197 198 199 200
		tag_builder.Commit(tag);
		return true;
	} else
		// TODO: implement
		return false;
}