2014-02-09 08:24:16 +01:00
|
|
|
/*
|
2016-02-26 17:54:05 +01:00
|
|
|
* Copyright 2003-2016 The Music Player Daemon Project
|
2014-02-09 08:24:16 +01:00
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2014-02-28 19:02:23 +01:00
|
|
|
#define __STDC_FORMAT_MACROS /* for PRIu64 */
|
|
|
|
|
2014-02-09 08:24:16 +01:00
|
|
|
#include "config.h"
|
|
|
|
#include "StorageCommands.hxx"
|
2015-08-11 22:11:28 +02:00
|
|
|
#include "Request.hxx"
|
2014-02-09 08:24:16 +01:00
|
|
|
#include "CommandError.hxx"
|
2014-02-12 21:46:32 +01:00
|
|
|
#include "util/UriUtil.hxx"
|
2014-02-09 08:24:16 +01:00
|
|
|
#include "util/Error.hxx"
|
2014-12-06 00:08:08 +01:00
|
|
|
#include "util/ConstBuffer.hxx"
|
2014-02-12 21:46:32 +01:00
|
|
|
#include "fs/Traits.hxx"
|
2014-02-09 08:24:16 +01:00
|
|
|
#include "client/Client.hxx"
|
2015-08-06 22:10:25 +02:00
|
|
|
#include "client/Response.hxx"
|
2014-02-09 08:24:16 +01:00
|
|
|
#include "Partition.hxx"
|
|
|
|
#include "Instance.hxx"
|
|
|
|
#include "storage/Registry.hxx"
|
|
|
|
#include "storage/CompositeStorage.hxx"
|
2014-02-28 19:02:23 +01:00
|
|
|
#include "storage/FileInfo.hxx"
|
2014-02-26 08:39:44 +01:00
|
|
|
#include "db/plugins/simple/SimpleDatabasePlugin.hxx"
|
|
|
|
#include "db/update/Service.hxx"
|
2014-02-28 19:02:23 +01:00
|
|
|
#include "TimePrint.hxx"
|
2014-10-07 19:45:40 +02:00
|
|
|
#include "IOThread.hxx"
|
2014-02-12 21:46:16 +01:00
|
|
|
#include "Idle.hxx"
|
2014-02-09 08:24:16 +01:00
|
|
|
|
2014-02-28 19:02:23 +01:00
|
|
|
#include <inttypes.h> /* for PRIu64 */
|
|
|
|
|
|
|
|
gcc_pure
|
|
|
|
static bool
|
|
|
|
skip_path(const char *name_utf8)
|
|
|
|
{
|
|
|
|
return strchr(name_utf8, '\n') != nullptr;
|
|
|
|
}
|
|
|
|
|
2014-03-14 08:58:43 +01:00
|
|
|
#if defined(WIN32) && GCC_CHECK_VERSION(4,6)
|
|
|
|
/* PRIu64 causes bogus compiler warning */
|
|
|
|
#pragma GCC diagnostic push
|
|
|
|
#pragma GCC diagnostic ignored "-Wformat"
|
|
|
|
#pragma GCC diagnostic ignored "-Wformat-extra-args"
|
|
|
|
#endif
|
|
|
|
|
2014-02-28 19:02:23 +01:00
|
|
|
static bool
|
2015-08-06 22:10:25 +02:00
|
|
|
handle_listfiles_storage(Response &r, StorageDirectoryReader &reader,
|
2014-02-28 19:02:23 +01:00
|
|
|
Error &error)
|
|
|
|
{
|
|
|
|
const char *name_utf8;
|
|
|
|
while ((name_utf8 = reader.Read()) != nullptr) {
|
|
|
|
if (skip_path(name_utf8))
|
|
|
|
continue;
|
|
|
|
|
2015-02-28 20:50:15 +01:00
|
|
|
StorageFileInfo info;
|
2014-02-28 19:02:23 +01:00
|
|
|
if (!reader.GetInfo(false, info, error))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
switch (info.type) {
|
2015-02-28 20:50:15 +01:00
|
|
|
case StorageFileInfo::Type::OTHER:
|
2014-02-28 19:02:23 +01:00
|
|
|
/* ignore */
|
|
|
|
continue;
|
|
|
|
|
2015-02-28 20:50:15 +01:00
|
|
|
case StorageFileInfo::Type::REGULAR:
|
2015-08-06 22:10:25 +02:00
|
|
|
r.Format("file: %s\n"
|
|
|
|
"size: %" PRIu64 "\n",
|
|
|
|
name_utf8,
|
|
|
|
info.size);
|
2014-02-28 19:02:23 +01:00
|
|
|
break;
|
|
|
|
|
2015-02-28 20:50:15 +01:00
|
|
|
case StorageFileInfo::Type::DIRECTORY:
|
2015-08-06 22:10:25 +02:00
|
|
|
r.Format("directory: %s\n", name_utf8);
|
2014-02-28 19:02:23 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (info.mtime != 0)
|
2015-08-06 22:10:25 +02:00
|
|
|
time_print(r, "Last-Modified", info.mtime);
|
2014-02-28 19:02:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-03-14 08:58:43 +01:00
|
|
|
#if defined(WIN32) && GCC_CHECK_VERSION(4,6)
|
|
|
|
#pragma GCC diagnostic pop
|
|
|
|
#endif
|
|
|
|
|
2014-02-28 19:02:23 +01:00
|
|
|
static bool
|
2015-08-06 22:10:25 +02:00
|
|
|
handle_listfiles_storage(Response &r, Storage &storage, const char *uri,
|
2014-02-28 19:02:23 +01:00
|
|
|
Error &error)
|
|
|
|
{
|
|
|
|
auto reader = storage.OpenDirectory(uri, error);
|
|
|
|
if (reader == nullptr)
|
|
|
|
return false;
|
|
|
|
|
2015-08-06 22:10:25 +02:00
|
|
|
bool success = handle_listfiles_storage(r, *reader, error);
|
2014-02-28 19:02:23 +01:00
|
|
|
delete reader;
|
|
|
|
return success;
|
|
|
|
}
|
|
|
|
|
|
|
|
CommandResult
|
2015-08-06 22:10:25 +02:00
|
|
|
handle_listfiles_storage(Response &r, Storage &storage, const char *uri)
|
2014-02-28 19:02:23 +01:00
|
|
|
{
|
|
|
|
Error error;
|
2015-08-06 22:10:25 +02:00
|
|
|
if (!handle_listfiles_storage(r, storage, uri, error))
|
|
|
|
return print_error(r, error);
|
2014-02-28 19:02:23 +01:00
|
|
|
|
|
|
|
return CommandResult::OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
CommandResult
|
2015-08-06 22:10:25 +02:00
|
|
|
handle_listfiles_storage(Response &r, const char *uri)
|
2014-02-28 19:02:23 +01:00
|
|
|
{
|
|
|
|
Error error;
|
2014-10-07 19:45:40 +02:00
|
|
|
Storage *storage = CreateStorageURI(io_thread_get(), uri, error);
|
2014-02-28 19:02:23 +01:00
|
|
|
if (storage == nullptr) {
|
|
|
|
if (error.IsDefined())
|
2015-08-06 22:10:25 +02:00
|
|
|
return print_error(r, error);
|
2014-02-28 19:02:23 +01:00
|
|
|
|
2015-08-06 22:10:25 +02:00
|
|
|
r.Error(ACK_ERROR_ARG, "Unrecognized storage URI");
|
2014-02-28 19:02:23 +01:00
|
|
|
return CommandResult::ERROR;
|
|
|
|
}
|
|
|
|
|
2015-08-06 22:10:25 +02:00
|
|
|
bool success = handle_listfiles_storage(r, *storage, "", error);
|
2014-02-28 19:02:23 +01:00
|
|
|
delete storage;
|
|
|
|
if (!success)
|
2015-08-06 22:10:25 +02:00
|
|
|
return print_error(r, error);
|
2014-02-28 19:02:23 +01:00
|
|
|
|
|
|
|
return CommandResult::OK;
|
|
|
|
}
|
|
|
|
|
2014-02-12 21:46:32 +01:00
|
|
|
static void
|
2015-08-06 22:10:25 +02:00
|
|
|
print_storage_uri(Client &client, Response &r, const Storage &storage)
|
2014-02-12 21:46:32 +01:00
|
|
|
{
|
|
|
|
std::string uri = storage.MapUTF8("");
|
|
|
|
if (uri.empty())
|
|
|
|
return;
|
|
|
|
|
2015-03-02 21:22:18 +01:00
|
|
|
if (PathTraitsUTF8::IsAbsolute(uri.c_str())) {
|
2014-02-12 21:46:32 +01:00
|
|
|
/* 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);
|
|
|
|
}
|
|
|
|
|
2015-08-06 22:10:25 +02:00
|
|
|
r.Format("storage: %s\n", uri.c_str());
|
2014-02-12 21:46:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
CommandResult
|
2015-08-13 12:48:31 +02:00
|
|
|
handle_listmounts(Client &client, gcc_unused Request args, Response &r)
|
2014-02-12 21:46:32 +01:00
|
|
|
{
|
|
|
|
Storage *_composite = client.partition.instance.storage;
|
|
|
|
if (_composite == nullptr) {
|
2015-08-06 22:10:25 +02:00
|
|
|
r.Error(ACK_ERROR_NO_EXIST, "No database");
|
2014-02-12 21:46:32 +01:00
|
|
|
return CommandResult::ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
CompositeStorage &composite = *(CompositeStorage *)_composite;
|
|
|
|
|
2015-08-06 22:10:25 +02:00
|
|
|
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);
|
2014-02-12 21:46:32 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
composite.VisitMounts(visitor);
|
|
|
|
|
|
|
|
return CommandResult::OK;
|
|
|
|
}
|
|
|
|
|
2014-02-09 08:24:16 +01:00
|
|
|
CommandResult
|
2015-08-13 12:48:31 +02:00
|
|
|
handle_mount(Client &client, Request args, Response &r)
|
2014-02-09 08:24:16 +01:00
|
|
|
{
|
|
|
|
Storage *_composite = client.partition.instance.storage;
|
|
|
|
if (_composite == nullptr) {
|
2015-08-06 22:10:25 +02:00
|
|
|
r.Error(ACK_ERROR_NO_EXIST, "No database");
|
2014-02-09 08:24:16 +01:00
|
|
|
return CommandResult::ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
CompositeStorage &composite = *(CompositeStorage *)_composite;
|
|
|
|
|
2014-12-06 00:08:08 +01:00
|
|
|
const char *const local_uri = args[0];
|
|
|
|
const char *const remote_uri = args[1];
|
2014-02-09 08:24:16 +01:00
|
|
|
|
|
|
|
if (*local_uri == 0) {
|
2015-08-06 22:10:25 +02:00
|
|
|
r.Error(ACK_ERROR_ARG, "Bad mount point");
|
2014-02-09 08:24:16 +01:00
|
|
|
return CommandResult::ERROR;
|
|
|
|
}
|
|
|
|
|
2014-02-26 08:39:44 +01:00
|
|
|
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 */
|
2015-08-06 22:10:25 +02:00
|
|
|
r.Error(ACK_ERROR_ARG, "Bad mount point");
|
2014-02-26 08:39:44 +01:00
|
|
|
return CommandResult::ERROR;
|
|
|
|
}
|
|
|
|
|
2014-02-09 08:24:16 +01:00
|
|
|
Error error;
|
2014-10-07 19:45:40 +02:00
|
|
|
Storage *storage = CreateStorageURI(io_thread_get(), remote_uri,
|
|
|
|
error);
|
2014-02-09 08:24:16 +01:00
|
|
|
if (storage == nullptr) {
|
|
|
|
if (error.IsDefined())
|
2015-08-06 22:10:25 +02:00
|
|
|
return print_error(r, error);
|
2014-02-09 08:24:16 +01:00
|
|
|
|
2015-08-06 22:10:25 +02:00
|
|
|
r.Error(ACK_ERROR_ARG, "Unrecognized storage URI");
|
2014-02-09 08:24:16 +01:00
|
|
|
return CommandResult::ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
composite.Mount(local_uri, storage);
|
2014-02-12 21:46:16 +01:00
|
|
|
idle_add(IDLE_MOUNT);
|
2014-02-26 08:39:44 +01:00
|
|
|
|
|
|
|
#ifdef ENABLE_DATABASE
|
|
|
|
Database *_db = client.partition.instance.database;
|
|
|
|
if (_db != nullptr && _db->IsPlugin(simple_db_plugin)) {
|
|
|
|
SimpleDatabase &db = *(SimpleDatabase *)_db;
|
|
|
|
|
|
|
|
if (!db.Mount(local_uri, remote_uri, error)) {
|
|
|
|
composite.Unmount(local_uri);
|
2015-08-06 22:10:25 +02:00
|
|
|
return print_error(r, error);
|
2014-02-26 08:39:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: call Instance::OnDatabaseModified()?
|
|
|
|
// TODO: trigger database update?
|
|
|
|
idle_add(IDLE_DATABASE);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2014-02-09 08:24:16 +01:00
|
|
|
return CommandResult::OK;
|
|
|
|
}
|
2014-02-12 21:45:04 +01:00
|
|
|
|
|
|
|
CommandResult
|
2015-08-13 12:48:31 +02:00
|
|
|
handle_unmount(Client &client, Request args, Response &r)
|
2014-02-12 21:45:04 +01:00
|
|
|
{
|
|
|
|
Storage *_composite = client.partition.instance.storage;
|
|
|
|
if (_composite == nullptr) {
|
2015-08-06 22:10:25 +02:00
|
|
|
r.Error(ACK_ERROR_NO_EXIST, "No database");
|
2014-02-12 21:45:04 +01:00
|
|
|
return CommandResult::ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
CompositeStorage &composite = *(CompositeStorage *)_composite;
|
|
|
|
|
2014-12-06 00:08:08 +01:00
|
|
|
const char *const local_uri = args.front();
|
2014-02-12 21:45:04 +01:00
|
|
|
|
|
|
|
if (*local_uri == 0) {
|
2015-08-06 22:10:25 +02:00
|
|
|
r.Error(ACK_ERROR_ARG, "Bad mount point");
|
2014-02-12 21:45:04 +01:00
|
|
|
return CommandResult::ERROR;
|
|
|
|
}
|
|
|
|
|
2014-02-26 08:39:44 +01:00
|
|
|
#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()?
|
|
|
|
idle_add(IDLE_DATABASE);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2014-02-12 21:45:04 +01:00
|
|
|
if (!composite.Unmount(local_uri)) {
|
2015-08-06 22:10:25 +02:00
|
|
|
r.Error(ACK_ERROR_ARG, "Not a mount point");
|
2014-02-12 21:45:04 +01:00
|
|
|
return CommandResult::ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
idle_add(IDLE_MOUNT);
|
2014-02-26 08:39:44 +01:00
|
|
|
|
2014-02-12 21:45:04 +01:00
|
|
|
return CommandResult::OK;
|
|
|
|
}
|