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

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

37
#include <exception>
38

39
#include <assert.h>
40
#include <string.h>
41

42 43
#ifdef ENABLE_DATABASE

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

50 51
	auto song = NewFile(path_utf8, parent);
	if (!song->UpdateFile(storage))
52
		return nullptr;
53 54 55 56

	return song;
}

57 58 59 60
#endif

#ifdef ENABLE_DATABASE

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

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

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

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

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

92 93 94 95
#endif

#ifdef ENABLE_ARCHIVE

96
SongPtr
97
Song::LoadFromArchive(ArchiveFile &archive, const char *name_utf8,
98
		      Directory &parent) noexcept
99 100 101 102
{
	assert(!uri_has_scheme(name_utf8));
	assert(strchr(name_utf8, '\n') == nullptr);

103
	auto song = NewFile(name_utf8, parent);
104

105
	if (!song->UpdateFileInArchive(archive))
106 107 108 109 110 111
		return nullptr;

	return song;
}

bool
112
Song::UpdateFileInArchive(ArchiveFile &archive) noexcept
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
{
	assert(parent != nullptr);
	assert(parent->device == DEVICE_INARCHIVE);

	std::string path_utf8(uri);

	for (const Directory *directory = parent;
	     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
bool
138
DetachedSong::LoadFile(Path path)
139
{
140 141
	const FileInfo fi(path);
	if (!fi.IsRegular())
142 143 144
		return false;

	TagBuilder tag_builder;
145
	if (!ScanFileTagsWithGeneric(path, tag_builder))
146 147
		return false;

148
	mtime = fi.GetModificationTime();
149 150 151 152
	tag_builder.Commit(tag);
	return true;
}

153
bool
154
DetachedSong::Update()
155 156 157
{
	if (IsAbsoluteFile()) {
		const AllocatedPath path_fs =
158
			AllocatedPath::FromUTF8Throw(GetRealURI());
159

160
		return LoadFile(path_fs);
161 162
	} else if (IsRemote()) {
		TagBuilder tag_builder;
163
		if (!tag_stream_scan(uri.c_str(), tag_builder))
164 165
			return false;

166
		mtime = std::chrono::system_clock::time_point::min();
167 168 169 170 171 172
		tag_builder.Commit(tag);
		return true;
	} else
		// TODO: implement
		return false;
}