Archive.cxx 5.29 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2017 The Music Player Daemon Project
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
 * 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.
 */

#include "config.h" /* must be first for large file support */
21
#include "Walk.hxx"
22
#include "UpdateDomain.hxx"
Max Kellermann's avatar
Max Kellermann committed
23
#include "db/DatabaseLock.hxx"
24 25
#include "db/plugins/simple/Directory.hxx"
#include "db/plugins/simple/Song.hxx"
26
#include "storage/StorageInterface.hxx"
27
#include "fs/AllocatedPath.hxx"
28
#include "storage/FileInfo.hxx"
29 30 31 32
#include "archive/ArchiveList.hxx"
#include "archive/ArchivePlugin.hxx"
#include "archive/ArchiveFile.hxx"
#include "archive/ArchiveVisitor.hxx"
33
#include "util/StringCompare.hxx"
34
#include "Log.hxx"
35

36
#include <string>
37
#include <stdexcept>
38 39 40

#include <string.h>

41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
static Directory *
LockFindChild(Directory &directory, const char *name)
{
	const ScopeDatabaseLock protect;
	return directory.FindChild(name);
}

static Directory *
LockMakeChild(Directory &directory, const char *name)
{
	const ScopeDatabaseLock protect;
	return directory.MakeChild(name);
}

static Song *
LockFindSong(Directory &directory, const char *name)
{
	const ScopeDatabaseLock protect;
	return directory.FindSong(name);
}

62
void
63 64
UpdateWalk::UpdateArchiveTree(ArchiveFile &archive, Directory &directory,
			      const char *name)
65
{
66
	const char *tmp = strchr(name, '/');
67
	if (tmp) {
68
		const std::string child_name(name, tmp);
69
		//add dir is not there already
70 71
		Directory *subdir = LockMakeChild(directory,
						  child_name.c_str());
72
		subdir->device = DEVICE_INARCHIVE;
73

74
		//create directories first
75
		UpdateArchiveTree(archive, *subdir, tmp + 1);
76
	} else {
77
		if (StringIsEmpty(name)) {
78 79
			LogWarning(update_domain,
				   "archive returned directory only");
80 81 82 83
			return;
		}

		//add file
84
		Song *song = LockFindSong(directory, name);
85
		if (song == nullptr) {
86
			song = Song::LoadFromArchive(archive, name, directory);
87
			if (song != nullptr) {
88 89 90 91
				{
					const ScopeDatabaseLock protect;
					directory.AddSong(song);
				}
92 93

				modified = true;
94 95
				FormatDefault(update_domain, "added %s/%s",
					      directory.GetPath(), name);
96
			}
97 98 99 100 101 102 103
		} else {
			if (!song->UpdateFileInArchive(archive)) {
				FormatDebug(update_domain,
					    "deleting unrecognized file %s/%s",
					    directory.GetPath(), name);
				editor.LockDeleteSong(directory, song);
			}
104 105 106 107
		}
	}
}

108 109
class UpdateArchiveVisitor final : public ArchiveVisitor {
	UpdateWalk &walk;
110
	ArchiveFile &archive;
111 112 113
	Directory *directory;

 public:
114 115 116
	UpdateArchiveVisitor(UpdateWalk &_walk, ArchiveFile &_archive,
			     Directory *_directory)
		:walk(_walk), archive(_archive), directory(_directory) {}
117 118 119 120

	virtual void VisitArchiveEntry(const char *path_utf8) override {
		FormatDebug(update_domain,
			    "adding archive file: %s", path_utf8);
121
		walk.UpdateArchiveTree(archive, *directory, path_utf8);
122 123 124
	}
};

125 126 127 128 129 130 131 132
/**
 * 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
 */
133 134
void
UpdateWalk::UpdateArchiveFile(Directory &parent, const char *name,
135
			      const StorageFileInfo &info,
136
			      const ArchivePlugin &plugin)
137
{
138
	Directory *directory = LockFindChild(parent, name);
139

140
	if (directory != nullptr && directory->mtime == info.mtime &&
141 142 143 144 145
	    !walk_discard)
		/* MPD has already scanned the archive, and it hasn't
		   changed since - don't consider updating it */
		return;

146 147 148 149 150
	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;
151 152

	/* open archive */
153 154 155 156 157
	ArchiveFile *file;
	try {
		file = archive_file_open(&plugin, path_fs);
	} catch (const std::runtime_error &e) {
		LogError(e);
158 159
		if (directory != nullptr)
			editor.LockDeleteDirectory(directory);
160 161 162
		return;
	}

163
	FormatDebug(update_domain, "archive %s opened", path_fs.c_str());
164

165
	if (directory == nullptr) {
166 167
		FormatDebug(update_domain,
			    "creating archive directory: %s", name);
168 169

		const ScopeDatabaseLock protect;
170
		directory = parent.CreateChild(name);
171 172 173 174 175
		/* mark this directory as archive (we use device for
		   this) */
		directory->device = DEVICE_INARCHIVE;
	}

176
	directory->mtime = info.mtime;
177

178
	UpdateArchiveVisitor visitor(*this, *file, directory);
179 180
	file->Visit(visitor);
	file->Close();
181 182 183
}

bool
184 185
UpdateWalk::UpdateArchiveFile(Directory &directory,
			      const char *name, const char *suffix,
186
			      const StorageFileInfo &info)
187
{
188
	const ArchivePlugin *plugin = archive_plugin_from_suffix(suffix);
189
	if (plugin == nullptr)
190 191
		return false;

192
	UpdateArchiveFile(directory, name, info, *plugin);
193 194
	return true;
}