Archive.cxx 4.73 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 "Walk.hxx"
21
#include "UpdateDomain.hxx"
Max Kellermann's avatar
Max Kellermann committed
22
#include "db/DatabaseLock.hxx"
23 24
#include "db/plugins/simple/Directory.hxx"
#include "db/plugins/simple/Song.hxx"
25
#include "storage/StorageInterface.hxx"
26
#include "fs/AllocatedPath.hxx"
27
#include "storage/FileInfo.hxx"
28 29 30 31
#include "archive/ArchiveList.hxx"
#include "archive/ArchivePlugin.hxx"
#include "archive/ArchiveFile.hxx"
#include "archive/ArchiveVisitor.hxx"
32
#include "util/StringCompare.hxx"
33
#include "Log.hxx"
34

35
#include <string>
36
#include <exception>
37 38 39

#include <string.h>

40
static Directory *
41
LockMakeChild(Directory &directory, std::string_view name) noexcept
42 43 44 45 46 47
{
	const ScopeDatabaseLock protect;
	return directory.MakeChild(name);
}

static Song *
48
LockFindSong(Directory &directory, std::string_view name) noexcept
49 50 51 52 53
{
	const ScopeDatabaseLock protect;
	return directory.FindSong(name);
}

54
void
55
UpdateWalk::UpdateArchiveTree(ArchiveFile &archive, Directory &directory,
56
			      const char *name) noexcept
57
{
Rosen Penev's avatar
Rosen Penev committed
58
	const char *tmp = std::strchr(name, '/');
59
	if (tmp) {
60
		const std::string_view child_name(name, tmp - name);
61
		//add dir is not there already
62
		Directory *subdir = LockMakeChild(directory, child_name);
63
		subdir->device = DEVICE_INARCHIVE;
64

65
		//create directories first
66
		UpdateArchiveTree(archive, *subdir, tmp + 1);
67
	} else {
68
		if (StringIsEmpty(name)) {
69 70
			LogWarning(update_domain,
				   "archive returned directory only");
71 72 73 74
			return;
		}

		//add file
75
		Song *song = LockFindSong(directory, name);
76
		if (song == nullptr) {
77
			auto new_song = Song::LoadFromArchive(archive, name, directory);
78
			if (new_song) {
79 80
				{
					const ScopeDatabaseLock protect;
81
					directory.AddSong(std::move(new_song));
82
				}
83 84

				modified = true;
85 86
				FormatNotice(update_domain, "added %s/%s",
					     directory.GetPath(), name);
87
			}
88 89 90 91 92 93 94
		} else {
			if (!song->UpdateFileInArchive(archive)) {
				FormatDebug(update_domain,
					    "deleting unrecognized file %s/%s",
					    directory.GetPath(), name);
				editor.LockDeleteSong(directory, song);
			}
95 96 97 98
		}
	}
}

99 100
class UpdateArchiveVisitor final : public ArchiveVisitor {
	UpdateWalk &walk;
101
	ArchiveFile &archive;
102
	Directory &directory;
103 104

 public:
105
	UpdateArchiveVisitor(UpdateWalk &_walk, ArchiveFile &_archive,
106
			     Directory &_directory) noexcept
107
		:walk(_walk), archive(_archive), directory(_directory) {}
108

109
	void VisitArchiveEntry(const char *path_utf8) override {
110 111
		FormatDebug(update_domain,
			    "adding archive file: %s", path_utf8);
112
		walk.UpdateArchiveTree(archive, directory, path_utf8);
113 114 115
	}
};

116 117 118 119 120 121 122 123
/**
 * Updates the file listing from an archive file.
 *
 * @param parent the parent directory the archive file resides in
 * @param name the UTF-8 encoded base name of the archive file
 * @param st stat() information on the archive file
 * @param plugin the archive plugin which fits this archive type
 */
124
void
125
UpdateWalk::UpdateArchiveFile(Directory &parent, std::string_view name,
126
			      const StorageFileInfo &info,
127
			      const ArchivePlugin &plugin) noexcept
128
{
129 130 131 132 133
	const auto path_fs = storage.MapChildFS(parent.GetPath(), name);
	if (path_fs.IsNull())
		/* not a local file: skip, because the archive API
		   supports only local files */
		return;
134

135 136 137 138 139 140 141
	Directory *directory =
		LockMakeVirtualDirectoryIfModified(parent, name, info,
						   DEVICE_INARCHIVE);
	if (directory == nullptr)
		/* not modified */
		return;

142
	/* open archive */
143
	std::unique_ptr<ArchiveFile> file;
144 145
	try {
		file = archive_file_open(&plugin, path_fs);
146 147
	} catch (...) {
		LogError(std::current_exception());
148
		editor.LockDeleteDirectory(directory);
149 150 151
		return;
	}

152
	FormatDebug(update_domain, "archive %s opened", path_fs.c_str());
153

154
	UpdateArchiveVisitor visitor(*this, *file, *directory);
155
	file->Visit(visitor);
156 157 158
}

bool
159
UpdateWalk::UpdateArchiveFile(Directory &directory,
160
			      std::string_view name, const char *suffix,
161
			      const StorageFileInfo &info) noexcept
162
{
163
	const ArchivePlugin *plugin = archive_plugin_from_suffix(suffix);
164
	if (plugin == nullptr)
165 166
		return false;

167
	UpdateArchiveFile(directory, name, info, *plugin);
168 169
	return true;
}