SongUpdate.cxx 3.88 KB
Newer Older
1
/*
2
 * Copyright 2003-2016 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 "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 "util/Error.hxx"
28
#include "fs/AllocatedPath.hxx"
29
#include "fs/FileInfo.hxx"
30
#include "tag/TagBuilder.hxx"
31
#include "TagFile.hxx"
32
#include "TagStream.hxx"
33

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

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

41 42
#ifdef ENABLE_DATABASE

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

49
	Song *song = NewFile(path_utf8, parent);
50
	if (!song->UpdateFile(storage)) {
51
		song->Free();
52
		return nullptr;
53 54 55 56 57
	}

	return song;
}

58 59 60 61
#endif

#ifdef ENABLE_DATABASE

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

67
	StorageFileInfo info;
68
	if (!storage.GetInfo(relative_uri.c_str(), true, info, IgnoreError()))
69
		return false;
70

71
	if (!info.IsRegular())
72 73
		return false;

74
	TagBuilder tag_builder;
75

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

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

92 93 94 95
#endif

#ifdef ENABLE_ARCHIVE

96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
Song *
Song::LoadFromArchive(ArchiveFile &archive, const char *name_utf8,
		      Directory &parent)
{
	assert(!uri_has_scheme(name_utf8));
	assert(strchr(name_utf8, '\n') == nullptr);

	Song *song = NewFile(name_utf8, parent);

	if (!song->UpdateFileInArchive(archive)) {
		song->Free();
		return nullptr;
	}

	return song;
}

bool
Song::UpdateFileInArchive(ArchiveFile &archive)
{
	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;
}

137 138
#endif

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

	TagBuilder tag_builder;
147
	if (!tag_file_scan(path, tag_builder))
148 149 150 151 152 153 154
		return false;

	mtime = fi.GetModificationTime();
	tag_builder.Commit(tag);
	return true;
}

155 156 157 158 159
bool
DetachedSong::Update()
{
	if (IsAbsoluteFile()) {
		const AllocatedPath path_fs =
160
			AllocatedPath::FromUTF8(GetRealURI());
161 162
		if (path_fs.IsNull())
			return false;
163

164
		return LoadFile(path_fs);
165 166
	} else if (IsRemote()) {
		TagBuilder tag_builder;
167
		if (!tag_stream_scan(uri.c_str(), tag_builder))
168 169 170 171 172 173 174 175 176
			return false;

		mtime = 0;
		tag_builder.Commit(tag);
		return true;
	} else
		// TODO: implement
		return false;
}