StorageCommands.cxx 6.95 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
 * 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 21
#define __STDC_FORMAT_MACROS /* for PRIu64 */

22 23
#include "config.h"
#include "StorageCommands.hxx"
24
#include "Request.hxx"
25
#include "CommandError.hxx"
26
#include "util/UriUtil.hxx"
27
#include "util/ConstBuffer.hxx"
28
#include "fs/Traits.hxx"
29
#include "client/Client.hxx"
30
#include "client/Response.hxx"
31 32 33 34
#include "Partition.hxx"
#include "Instance.hxx"
#include "storage/Registry.hxx"
#include "storage/CompositeStorage.hxx"
35
#include "storage/FileInfo.hxx"
Max Kellermann's avatar
Max Kellermann committed
36 37
#include "db/plugins/simple/SimpleDatabasePlugin.hxx"
#include "db/update/Service.hxx"
38
#include "TimePrint.hxx"
39
#include "IOThread.hxx"
40
#include "Idle.hxx"
41

42 43
#include <memory>

44 45 46 47
#include <inttypes.h> /* for PRIu64 */

gcc_pure
static bool
48
skip_path(const char *name_utf8) noexcept
49 50 51 52
{
	return strchr(name_utf8, '\n') != nullptr;
}

53
#if defined(_WIN32) && GCC_CHECK_VERSION(4,6)
54 55 56 57 58 59
/* PRIu64 causes bogus compiler warning */
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wformat"
#pragma GCC diagnostic ignored "-Wformat-extra-args"
#endif

60 61
static void
handle_listfiles_storage(Response &r, StorageDirectoryReader &reader)
62 63 64 65 66 67
{
	const char *name_utf8;
	while ((name_utf8 = reader.Read()) != nullptr) {
		if (skip_path(name_utf8))
			continue;

68
		StorageFileInfo info;
69 70 71
		try {
			info = reader.GetInfo(false);
		} catch (const std::runtime_error &) {
72
			continue;
73
		}
74 75

		switch (info.type) {
76
		case StorageFileInfo::Type::OTHER:
77 78 79
			/* ignore */
			continue;

80
		case StorageFileInfo::Type::REGULAR:
81 82 83 84
			r.Format("file: %s\n"
				 "size: %" PRIu64 "\n",
				 name_utf8,
				 info.size);
85 86
			break;

87
		case StorageFileInfo::Type::DIRECTORY:
88
			r.Format("directory: %s\n", name_utf8);
89 90 91 92
			break;
		}

		if (info.mtime != 0)
93
			time_print(r, "Last-Modified", info.mtime);
94 95 96
	}
}

97
#if defined(_WIN32) && GCC_CHECK_VERSION(4,6)
98 99 100
#pragma GCC diagnostic pop
#endif

101
CommandResult
102
handle_listfiles_storage(Response &r, Storage &storage, const char *uri)
103
{
104 105
	std::unique_ptr<StorageDirectoryReader> reader(storage.OpenDirectory(uri));
	handle_listfiles_storage(r, *reader);
106 107 108 109
	return CommandResult::OK;
}

CommandResult
110
handle_listfiles_storage(Response &r, const char *uri)
111
{
112
	std::unique_ptr<Storage> storage(CreateStorageURI(io_thread_get(), uri));
113
	if (storage == nullptr) {
114
		r.Error(ACK_ERROR_ARG, "Unrecognized storage URI");
115 116 117
		return CommandResult::ERROR;
	}

118
	return handle_listfiles_storage(r, *storage, "");
119 120
}

121
static void
122
print_storage_uri(Client &client, Response &r, const Storage &storage)
123 124 125 126 127
{
	std::string uri = storage.MapUTF8("");
	if (uri.empty())
		return;

128
	if (PathTraitsUTF8::IsAbsolute(uri.c_str())) {
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
		/* storage points to local directory */

		if (!client.IsLocal())
			/* only "local" clients may see local paths
			   (same policy as with the "config"
			   command) */
			return;
	} else {
		/* hide username/passwords from client */

		std::string allocated = uri_remove_auth(uri.c_str());
		if (!allocated.empty())
			uri = std::move(allocated);
	}

144
	r.Format("storage: %s\n", uri.c_str());
145 146 147
}

CommandResult
148
handle_listmounts(Client &client, gcc_unused Request args, Response &r)
149 150 151
{
	Storage *_composite = client.partition.instance.storage;
	if (_composite == nullptr) {
152
		r.Error(ACK_ERROR_NO_EXIST, "No database");
153 154 155 156 157
		return CommandResult::ERROR;
	}

	CompositeStorage &composite = *(CompositeStorage *)_composite;

158 159 160 161
	const auto visitor = [&client, &r](const char *mount_uri,
					   const Storage &storage){
		r.Format("mount: %s\n", mount_uri);
		print_storage_uri(client, r, storage);
162 163 164 165 166 167 168
	};

	composite.VisitMounts(visitor);

	return CommandResult::OK;
}

169
CommandResult
170
handle_mount(Client &client, Request args, Response &r)
171 172 173
{
	Storage *_composite = client.partition.instance.storage;
	if (_composite == nullptr) {
174
		r.Error(ACK_ERROR_NO_EXIST, "No database");
175 176 177 178 179
		return CommandResult::ERROR;
	}

	CompositeStorage &composite = *(CompositeStorage *)_composite;

180 181
	const char *const local_uri = args[0];
	const char *const remote_uri = args[1];
182 183

	if (*local_uri == 0) {
184
		r.Error(ACK_ERROR_ARG, "Bad mount point");
185 186 187
		return CommandResult::ERROR;
	}

Max Kellermann's avatar
Max Kellermann committed
188 189 190 191 192 193
	if (strchr(local_uri, '/') != nullptr) {
		/* allow only top-level mounts for now */
		/* TODO: eliminate this limitation after ensuring that
		   UpdateQueue::Erase() really gets called for every
		   unmount, and no Directory disappears recursively
		   during database update */
194
		r.Error(ACK_ERROR_ARG, "Bad mount point");
Max Kellermann's avatar
Max Kellermann committed
195 196 197
		return CommandResult::ERROR;
	}

198
	Storage *storage = CreateStorageURI(io_thread_get(), remote_uri);
199
	if (storage == nullptr) {
200
		r.Error(ACK_ERROR_ARG, "Unrecognized storage URI");
201 202 203 204
		return CommandResult::ERROR;
	}

	composite.Mount(local_uri, storage);
205
	client.partition.EmitIdle(IDLE_MOUNT);
Max Kellermann's avatar
Max Kellermann committed
206 207 208 209 210 211

#ifdef ENABLE_DATABASE
	Database *_db = client.partition.instance.database;
	if (_db != nullptr && _db->IsPlugin(simple_db_plugin)) {
		SimpleDatabase &db = *(SimpleDatabase *)_db;

212
		try {
213
			db.Mount(local_uri, remote_uri);
214
		} catch (...) {
Max Kellermann's avatar
Max Kellermann committed
215
			composite.Unmount(local_uri);
216
			throw;
Max Kellermann's avatar
Max Kellermann committed
217 218 219 220
		}

		// TODO: call Instance::OnDatabaseModified()?
		// TODO: trigger database update?
221
		client.partition.EmitIdle(IDLE_DATABASE);
Max Kellermann's avatar
Max Kellermann committed
222 223 224
	}
#endif

225 226
	return CommandResult::OK;
}
227 228

CommandResult
229
handle_unmount(Client &client, Request args, Response &r)
230 231 232
{
	Storage *_composite = client.partition.instance.storage;
	if (_composite == nullptr) {
233
		r.Error(ACK_ERROR_NO_EXIST, "No database");
234 235 236 237 238
		return CommandResult::ERROR;
	}

	CompositeStorage &composite = *(CompositeStorage *)_composite;

239
	const char *const local_uri = args.front();
240 241

	if (*local_uri == 0) {
242
		r.Error(ACK_ERROR_ARG, "Bad mount point");
243 244 245
		return CommandResult::ERROR;
	}

Max Kellermann's avatar
Max Kellermann committed
246 247 248 249 250 251 252 253 254 255 256 257 258
#ifdef ENABLE_DATABASE
	if (client.partition.instance.update != nullptr)
		/* ensure that no database update will attempt to work
		   with the database/storage instances we're about to
		   destroy here */
		client.partition.instance.update->CancelMount(local_uri);

	Database *_db = client.partition.instance.database;
	if (_db != nullptr && _db->IsPlugin(simple_db_plugin)) {
		SimpleDatabase &db = *(SimpleDatabase *)_db;

		if (db.Unmount(local_uri))
			// TODO: call Instance::OnDatabaseModified()?
259
			client.partition.EmitIdle(IDLE_DATABASE);
Max Kellermann's avatar
Max Kellermann committed
260 261 262
	}
#endif

263
	if (!composite.Unmount(local_uri)) {
264
		r.Error(ACK_ERROR_ARG, "Not a mount point");
265 266 267
		return CommandResult::ERROR;
	}

268
	client.partition.EmitIdle(IDLE_MOUNT);
Max Kellermann's avatar
Max Kellermann committed
269

270 271
	return CommandResult::OK;
}