SongUpdate.cxx 4.24 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2020 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 "fs/AllocatedPath.hxx"
27
#include "fs/FileInfo.hxx"
28
#include "tag/Builder.hxx"
29
#include "TagFile.hxx"
30
#include "TagStream.hxx"
Max Kellermann's avatar
Max Kellermann committed
31
#include "util/UriExtract.hxx"
32

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

37 38
#include <cassert>

39
#include <string.h>
40

41 42
#ifdef ENABLE_DATABASE

43
SongPtr
44
Song::LoadFile(Storage &storage, const char *path_utf8, Directory &parent)
45
{
46
	assert(!uri_has_scheme(path_utf8));
Rosen Penev's avatar
Rosen Penev committed
47
	assert(std::strchr(path_utf8, '\n') == nullptr);
48

49
	auto song = std::make_unique<Song>(path_utf8, parent);
50
	if (!song->UpdateFile(storage))
51
		return nullptr;
52 53 54 55

	return song;
}

56 57 58 59
#endif

#ifdef ENABLE_DATABASE

60
bool
61
Song::UpdateFile(Storage &storage)
62
{
63
	const auto &relative_uri = GetURI();
64

65
	const auto info = storage.GetInfo(relative_uri.c_str(), true);
66
	if (!info.IsRegular())
67 68
		return false;

69
	TagBuilder tag_builder;
70
	auto new_audio_format = AudioFormat::Undefined();
71

72 73 74 75 76 77
	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,
78
					     &new_audio_format))
79 80 81 82 83 84 85 86 87
				return false;
		} else {
			if (!ScanFileTagsWithGeneric(path_fs, tag_builder,
						     &new_audio_format))
				return false;
		}
	} catch (...) {
		// TODO: log or propagate I/O errors?
		return false;
88
	}
89

90
	mtime = info.mtime;
91
	audio_format = new_audio_format;
92
	tag_builder.Commit(tag);
93
	return true;
94 95
}

96 97
#ifdef ENABLE_ARCHIVE

98
SongPtr
99
Song::LoadFromArchive(ArchiveFile &archive, const char *name_utf8,
100
		      Directory &parent) noexcept
101 102
{
	assert(!uri_has_scheme(name_utf8));
Rosen Penev's avatar
Rosen Penev committed
103
	assert(std::strchr(name_utf8, '\n') == nullptr);
104

105
	auto song = std::make_unique<Song>(name_utf8, parent);
106
	if (!song->UpdateFileInArchive(archive))
107 108 109 110 111 112
		return nullptr;

	return song;
}

bool
113
Song::UpdateFileInArchive(ArchiveFile &archive) noexcept
114
{
115
	assert(parent.device == DEVICE_INARCHIVE);
116

117
	std::string path_utf8(filename);
118

119
	for (const Directory *directory = &parent;
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
	     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;
}

135 136
#endif

137 138
#endif /* ENABLE_DATABASE */

139
bool
140
DetachedSong::LoadFile(Path path)
141
{
142 143
	const FileInfo fi(path);
	if (!fi.IsRegular())
144 145 146
		return false;

	TagBuilder tag_builder;
147 148 149 150 151 152

	try {
		if (!ScanFileTagsWithGeneric(path, tag_builder))
			return false;
	} catch (...) {
		// TODO: log or propagate I/O errors?
153
		return false;
154
	}
155

156
	mtime = fi.GetModificationTime();
157 158 159 160
	tag_builder.Commit(tag);
	return true;
}

161
bool
162
DetachedSong::Update()
163 164 165
{
	if (IsAbsoluteFile()) {
		const AllocatedPath path_fs =
166
			AllocatedPath::FromUTF8Throw(GetRealURI());
167

168
		return LoadFile(path_fs);
169 170
	} else if (IsRemote()) {
		TagBuilder tag_builder;
171 172 173 174 175 176

		try {
			if (!tag_stream_scan(uri.c_str(), tag_builder))
				return false;
		} catch (...) {
			// TODO: log or propagate I/O errors?
177
			return false;
178
		}
179

180
		mtime = std::chrono::system_clock::time_point::min();
181 182 183 184 185 186
		tag_builder.Commit(tag);
		return true;
	} else
		// TODO: implement
		return false;
}