StorageCommands.cxx 6.84 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2020 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 "time/ChronoUtil.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
#include "Instance.hxx"
#include "storage/Registry.hxx"
#include "storage/CompositeStorage.hxx"
34
#include "storage/FileInfo.hxx"
Max Kellermann's avatar
Max Kellermann committed
35 36
#include "db/plugins/simple/SimpleDatabasePlugin.hxx"
#include "db/update/Service.hxx"
37
#include "TimePrint.hxx"
38
#include "IdleFlags.hxx"
39

40 41
#include <memory>

42 43 44 45
#include <inttypes.h> /* for PRIu64 */

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

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

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

66
		StorageFileInfo info;
67 68
		try {
			info = reader.GetInfo(false);
69
		} catch (...) {
70
			continue;
71
		}
72 73

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

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

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

90
		if (!IsNegative(info.mtime))
91
			time_print(r, "Last-Modified", info.mtime);
92 93 94
	}
}

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

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

CommandResult
108
handle_listfiles_storage(Client &client, Response &r, const char *uri)
109
{
110
	auto &event_loop = client.GetInstance().io_thread.GetEventLoop();
111
	std::unique_ptr<Storage> storage(CreateStorageURI(event_loop, uri));
112
	if (storage == nullptr) {
113
		r.Error(ACK_ERROR_ARG, "Unrecognized storage URI");
114 115 116
		return CommandResult::ERROR;
	}

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

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

127
	if (PathTraitsUTF8::IsAbsolute(uri.c_str())) {
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
		/* 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);
	}

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

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

	CompositeStorage &composite = *(CompositeStorage *)_composite;

157 158 159 160
	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);
161 162 163 164 165 166 167
	};

	composite.VisitMounts(visitor);

	return CommandResult::OK;
}

168
CommandResult
169
handle_mount(Client &client, Request args, Response &r)
170
{
171 172 173
	auto &instance = client.GetInstance();

	Storage *_composite = instance.storage;
174
	if (_composite == nullptr) {
175
		r.Error(ACK_ERROR_NO_EXIST, "No database");
176 177 178 179 180
		return CommandResult::ERROR;
	}

	CompositeStorage &composite = *(CompositeStorage *)_composite;

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

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

Max Kellermann's avatar
Max Kellermann committed
189 190 191 192 193 194
	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 */
195
		r.Error(ACK_ERROR_ARG, "Bad mount point");
Max Kellermann's avatar
Max Kellermann committed
196 197 198
		return CommandResult::ERROR;
	}

199
	auto &event_loop = instance.io_thread.GetEventLoop();
200
	auto storage = CreateStorageURI(event_loop, remote_uri);
201
	if (storage == nullptr) {
202
		r.Error(ACK_ERROR_ARG, "Unrecognized storage URI");
203 204 205
		return CommandResult::ERROR;
	}

206
	composite.Mount(local_uri, std::move(storage));
207
	instance.EmitIdle(IDLE_MOUNT);
Max Kellermann's avatar
Max Kellermann committed
208 209

#ifdef ENABLE_DATABASE
210
	if (auto *db = dynamic_cast<SimpleDatabase *>(instance.GetDatabase())) {
211
		try {
212
			db->Mount(local_uri, remote_uri);
213
		} catch (...) {
Max Kellermann's avatar
Max Kellermann committed
214
			composite.Unmount(local_uri);
215
			throw;
Max Kellermann's avatar
Max Kellermann committed
216 217 218 219
		}

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

224 225
	return CommandResult::OK;
}
226 227

CommandResult
228
handle_unmount(Client &client, Request args, Response &r)
229
{
230 231 232
	auto &instance = client.GetInstance();

	Storage *_composite = instance.storage;
233
	if (_composite == nullptr) {
234
		r.Error(ACK_ERROR_NO_EXIST, "No database");
235 236 237 238 239
		return CommandResult::ERROR;
	}

	CompositeStorage &composite = *(CompositeStorage *)_composite;

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

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

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

254
	if (auto *db = dynamic_cast<SimpleDatabase *>(instance.GetDatabase())) {
255
		if (db->Unmount(local_uri))
Max Kellermann's avatar
Max Kellermann committed
256
			// TODO: call Instance::OnDatabaseModified()?
257
			instance.EmitIdle(IDLE_DATABASE);
Max Kellermann's avatar
Max Kellermann committed
258 259 260
	}
#endif

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

266
	instance.EmitIdle(IDLE_MOUNT);
Max Kellermann's avatar
Max Kellermann committed
267

268 269
	return CommandResult::OK;
}