SongUpdate.cxx 3.96 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
#include <assert.h>
38
#include <string.h>
39

40 41
#ifdef ENABLE_DATABASE

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

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

	return song;
}

55 56 57 58
#endif

#ifdef ENABLE_DATABASE

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

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

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

71 72 73 74
	const auto path_fs = storage.MapFS(relative_uri.c_str());
	if (path_fs.IsNull()) {
		const auto absolute_uri =
			storage.MapUTF8(relative_uri.c_str());
75 76
		if (!tag_stream_scan(absolute_uri.c_str(), tag_builder,
				     &new_audio_format))
77 78
			return false;
	} else {
79 80
		if (!ScanFileTagsWithGeneric(path_fs, tag_builder,
					     &new_audio_format))
81 82
			return false;
	}
83

84
	mtime = info.mtime;
85
	audio_format = new_audio_format;
86
	tag_builder.Commit(tag);
87
	return true;
88 89
}

90 91
#ifdef ENABLE_ARCHIVE

92
SongPtr
93
Song::LoadFromArchive(ArchiveFile &archive, const char *name_utf8,
94
		      Directory &parent) noexcept
95 96 97 98
{
	assert(!uri_has_scheme(name_utf8));
	assert(strchr(name_utf8, '\n') == nullptr);

99
	auto song = std::make_unique<Song>(name_utf8, parent);
100
	if (!song->UpdateFileInArchive(archive))
101 102 103 104 105 106
		return nullptr;

	return song;
}

bool
107
Song::UpdateFileInArchive(ArchiveFile &archive) noexcept
108
{
109
	assert(parent.device == DEVICE_INARCHIVE);
110

111
	std::string path_utf8(filename);
112

113
	for (const Directory *directory = &parent;
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
	     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;
}

129 130
#endif

131 132
#endif /* ENABLE_DATABASE */

133
bool
134
DetachedSong::LoadFile(Path path)
135
{
136 137
	const FileInfo fi(path);
	if (!fi.IsRegular())
138 139 140
		return false;

	TagBuilder tag_builder;
141
	if (!ScanFileTagsWithGeneric(path, tag_builder))
142 143
		return false;

144
	mtime = fi.GetModificationTime();
145 146 147 148
	tag_builder.Commit(tag);
	return true;
}

149
bool
150
DetachedSong::Update()
151 152 153
{
	if (IsAbsoluteFile()) {
		const AllocatedPath path_fs =
154
			AllocatedPath::FromUTF8Throw(GetRealURI());
155

156
		return LoadFile(path_fs);
157 158
	} else if (IsRemote()) {
		TagBuilder tag_builder;
159
		if (!tag_stream_scan(uri.c_str(), tag_builder))
160 161
			return false;

162
		mtime = std::chrono::system_clock::time_point::min();
163 164 165 166 167 168
		tag_builder.Commit(tag);
		return true;
	} else
		// TODO: implement
		return false;
}