2013-10-25 19:35:40 +02:00
|
|
|
/*
|
2017-01-03 20:48:59 +01:00
|
|
|
* Copyright 2003-2017 The Music Player Daemon Project
|
2013-10-25 19:35:40 +02: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 */
|
|
|
|
|
2013-10-25 19:35:40 +02:00
|
|
|
#include "config.h"
|
|
|
|
#include "FileCommands.hxx"
|
2015-08-11 22:11:28 +02:00
|
|
|
#include "Request.hxx"
|
2013-10-25 19:35:40 +02:00
|
|
|
#include "CommandError.hxx"
|
|
|
|
#include "protocol/Ack.hxx"
|
2014-01-24 00:26:53 +01:00
|
|
|
#include "client/Client.hxx"
|
2015-08-06 22:10:25 +02:00
|
|
|
#include "client/Response.hxx"
|
2013-10-25 19:35:40 +02:00
|
|
|
#include "util/CharUtil.hxx"
|
2013-12-29 14:40:24 +01:00
|
|
|
#include "util/UriUtil.hxx"
|
2017-02-08 08:26:58 +01:00
|
|
|
#include "tag/Handler.hxx"
|
2016-02-22 18:00:49 +01:00
|
|
|
#include "tag/Generic.hxx"
|
2013-12-29 14:40:24 +01:00
|
|
|
#include "TagStream.hxx"
|
2013-10-25 19:35:40 +02:00
|
|
|
#include "TagFile.hxx"
|
2014-02-07 18:38:10 +01:00
|
|
|
#include "storage/StorageInterface.hxx"
|
2013-10-25 19:35:40 +02:00
|
|
|
#include "fs/AllocatedPath.hxx"
|
2015-02-28 20:42:50 +01:00
|
|
|
#include "fs/FileInfo.hxx"
|
2014-10-01 23:38:17 +02:00
|
|
|
#include "fs/DirectoryReader.hxx"
|
2017-07-21 11:49:28 +02:00
|
|
|
#include "input/InputStream.hxx"
|
2015-10-22 09:29:02 +02:00
|
|
|
#include "LocateUri.hxx"
|
2014-02-28 19:02:23 +01:00
|
|
|
#include "TimePrint.hxx"
|
2017-07-21 11:49:28 +02:00
|
|
|
#include "thread/Mutex.hxx"
|
|
|
|
#include "thread/Cond.hxx"
|
2013-10-25 19:35:40 +02:00
|
|
|
|
|
|
|
#include <assert.h>
|
2014-02-28 19:02:23 +01:00
|
|
|
#include <inttypes.h> /* for PRIu64 */
|
|
|
|
|
|
|
|
gcc_pure
|
|
|
|
static bool
|
2017-05-08 14:44:49 +02:00
|
|
|
SkipNameFS(PathTraitsFS::const_pointer_type name_fs) noexcept
|
2014-02-28 19:02:23 +01:00
|
|
|
{
|
|
|
|
return name_fs[0] == '.' &&
|
|
|
|
(name_fs[1] == 0 ||
|
|
|
|
(name_fs[1] == '.' && name_fs[2] == 0));
|
|
|
|
}
|
|
|
|
|
|
|
|
gcc_pure
|
|
|
|
static bool
|
2017-05-08 14:44:49 +02:00
|
|
|
skip_path(Path name_fs) noexcept
|
2014-02-28 19:02:23 +01:00
|
|
|
{
|
2015-03-03 12:42:27 +01:00
|
|
|
return name_fs.HasNewline();
|
2014-02-28 19:02:23 +01:00
|
|
|
}
|
|
|
|
|
2017-12-12 10:22:20 +01:00
|
|
|
#if defined(_WIN32) && GCC_CHECK_VERSION(4,6)
|
2014-03-14 08:58:43 +01:00
|
|
|
/* 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
|
|
|
CommandResult
|
2015-12-29 12:41:45 +01:00
|
|
|
handle_listfiles_local(Response &r, Path path_fs)
|
2014-02-28 19:02:23 +01:00
|
|
|
{
|
|
|
|
DirectoryReader reader(path_fs);
|
|
|
|
|
|
|
|
while (reader.ReadEntry()) {
|
|
|
|
const Path name_fs = reader.GetEntry();
|
2015-03-03 12:42:27 +01:00
|
|
|
if (SkipNameFS(name_fs.c_str()) || skip_path(name_fs))
|
2014-02-28 19:02:23 +01:00
|
|
|
continue;
|
|
|
|
|
|
|
|
std::string name_utf8 = name_fs.ToUTF8();
|
|
|
|
if (name_utf8.empty())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
const AllocatedPath full_fs =
|
|
|
|
AllocatedPath::Build(path_fs, name_fs);
|
2015-02-28 20:42:50 +01:00
|
|
|
FileInfo fi;
|
|
|
|
if (!GetFileInfo(full_fs, fi, false))
|
2014-02-28 19:02:23 +01:00
|
|
|
continue;
|
|
|
|
|
2015-02-28 20:42:50 +01:00
|
|
|
if (fi.IsRegular())
|
2015-08-06 22:10:25 +02:00
|
|
|
r.Format("file: %s\n"
|
|
|
|
"size: %" PRIu64 "\n",
|
|
|
|
name_utf8.c_str(),
|
|
|
|
fi.GetSize());
|
2015-02-28 20:42:50 +01:00
|
|
|
else if (fi.IsDirectory())
|
2015-08-06 22:10:25 +02:00
|
|
|
r.Format("directory: %s\n", name_utf8.c_str());
|
2015-02-28 20:58:48 +01:00
|
|
|
else
|
|
|
|
continue;
|
2014-02-28 19:02:23 +01:00
|
|
|
|
2015-08-06 22:10:25 +02:00
|
|
|
time_print(r, "Last-Modified", fi.GetModificationTime());
|
2014-02-28 19:02:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return CommandResult::OK;
|
|
|
|
}
|
2013-10-25 19:35:40 +02:00
|
|
|
|
2017-12-12 10:22:20 +01:00
|
|
|
#if defined(_WIN32) && GCC_CHECK_VERSION(4,6)
|
2014-03-14 08:58:43 +01:00
|
|
|
#pragma GCC diagnostic pop
|
|
|
|
#endif
|
|
|
|
|
2013-10-25 19:35:40 +02:00
|
|
|
gcc_pure
|
|
|
|
static bool
|
2017-05-08 14:44:49 +02:00
|
|
|
IsValidName(const char *p) noexcept
|
2013-10-25 19:35:40 +02:00
|
|
|
{
|
|
|
|
if (!IsAlphaASCII(*p))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
while (*++p) {
|
|
|
|
const char ch = *p;
|
|
|
|
if (!IsAlphaASCII(ch) && ch != '_' && ch != '-')
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
gcc_pure
|
|
|
|
static bool
|
2017-05-08 14:44:49 +02:00
|
|
|
IsValidValue(const char *p) noexcept
|
2013-10-25 19:35:40 +02:00
|
|
|
{
|
|
|
|
while (*p) {
|
|
|
|
const char ch = *p++;
|
|
|
|
|
2013-10-26 16:03:53 +02:00
|
|
|
if ((unsigned char)ch < 0x20)
|
2013-10-25 19:35:40 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
print_pair(const char *key, const char *value, void *ctx)
|
|
|
|
{
|
2015-08-06 22:10:25 +02:00
|
|
|
auto &r = *(Response *)ctx;
|
2013-10-25 19:35:40 +02:00
|
|
|
|
|
|
|
if (IsValidName(key) && IsValidValue(value))
|
2015-08-06 22:10:25 +02:00
|
|
|
r.Format("%s: %s\n", key, value);
|
2013-10-25 19:35:40 +02:00
|
|
|
}
|
|
|
|
|
2016-02-22 17:38:06 +01:00
|
|
|
static constexpr TagHandler print_comment_handler = {
|
2013-10-25 19:35:40 +02:00
|
|
|
nullptr,
|
|
|
|
nullptr,
|
|
|
|
print_pair,
|
|
|
|
};
|
|
|
|
|
2013-12-29 14:40:24 +01:00
|
|
|
static CommandResult
|
2015-08-06 22:10:25 +02:00
|
|
|
read_stream_comments(Response &r, const char *uri)
|
2013-12-29 14:40:24 +01:00
|
|
|
{
|
2015-08-06 22:10:25 +02:00
|
|
|
if (!tag_stream_scan(uri, print_comment_handler, &r)) {
|
|
|
|
r.Error(ACK_ERROR_NO_EXIST, "Failed to load file");
|
2013-12-29 14:40:24 +01:00
|
|
|
return CommandResult::ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
return CommandResult::OK;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2014-02-07 18:33:43 +01:00
|
|
|
static CommandResult
|
2015-08-06 22:10:25 +02:00
|
|
|
read_file_comments(Response &r, const Path path_fs)
|
2014-02-07 18:33:43 +01:00
|
|
|
{
|
2015-08-06 22:10:25 +02:00
|
|
|
if (!tag_file_scan(path_fs, print_comment_handler, &r)) {
|
|
|
|
r.Error(ACK_ERROR_NO_EXIST, "Failed to load file");
|
2014-02-07 18:33:43 +01:00
|
|
|
return CommandResult::ERROR;
|
|
|
|
}
|
|
|
|
|
2016-02-22 18:00:49 +01:00
|
|
|
ScanGenericTags(path_fs, print_comment_handler, &r);
|
2014-02-07 18:33:43 +01:00
|
|
|
|
|
|
|
return CommandResult::OK;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-10-22 09:29:02 +02:00
|
|
|
static CommandResult
|
|
|
|
read_db_comments(Client &client, Response &r, const char *uri)
|
2015-02-09 07:55:06 +01:00
|
|
|
{
|
2015-10-22 09:29:02 +02:00
|
|
|
#ifdef ENABLE_DATABASE
|
|
|
|
const Storage *storage = client.GetStorage();
|
|
|
|
if (storage == nullptr) {
|
|
|
|
#else
|
|
|
|
(void)client;
|
|
|
|
(void)uri;
|
|
|
|
#endif
|
|
|
|
r.Error(ACK_ERROR_NO_EXIST, "No database");
|
|
|
|
return CommandResult::ERROR;
|
|
|
|
#ifdef ENABLE_DATABASE
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
AllocatedPath path_fs = storage->MapFS(uri);
|
|
|
|
if (!path_fs.IsNull())
|
|
|
|
return read_file_comments(r, path_fs);
|
|
|
|
}
|
2015-02-09 07:55:06 +01:00
|
|
|
|
2015-10-22 09:29:02 +02:00
|
|
|
{
|
|
|
|
const std::string uri2 = storage->MapUTF8(uri);
|
|
|
|
if (uri_has_scheme(uri2.c_str()))
|
|
|
|
return read_stream_comments(r, uri2.c_str());
|
|
|
|
}
|
|
|
|
|
|
|
|
r.Error(ACK_ERROR_NO_EXIST, "No such file");
|
|
|
|
return CommandResult::ERROR;
|
|
|
|
#endif
|
2015-02-09 07:55:06 +01:00
|
|
|
}
|
|
|
|
|
2013-10-25 19:35:40 +02:00
|
|
|
CommandResult
|
2015-08-13 12:48:31 +02:00
|
|
|
handle_read_comments(Client &client, Request args, Response &r)
|
2013-10-25 19:35:40 +02:00
|
|
|
{
|
2014-12-06 00:08:08 +01:00
|
|
|
assert(args.size == 1);
|
2013-10-25 19:35:40 +02:00
|
|
|
|
2015-10-22 09:29:02 +02:00
|
|
|
const char *const uri = args.front();
|
2014-02-07 18:33:43 +01:00
|
|
|
|
2016-10-27 21:59:17 +02:00
|
|
|
const auto located_uri = LocateUri(uri, &client
|
2014-01-30 20:29:48 +01:00
|
|
|
#ifdef ENABLE_DATABASE
|
2016-10-27 21:59:17 +02:00
|
|
|
, nullptr
|
2014-02-07 18:38:10 +01:00
|
|
|
#endif
|
2016-10-27 21:59:17 +02:00
|
|
|
);
|
2015-10-22 09:29:02 +02:00
|
|
|
switch (located_uri.type) {
|
|
|
|
case LocatedUri::Type::ABSOLUTE:
|
|
|
|
return read_stream_comments(r, located_uri.canonical_uri);
|
2014-02-07 18:33:43 +01:00
|
|
|
|
2015-10-22 09:29:02 +02:00
|
|
|
case LocatedUri::Type::RELATIVE:
|
|
|
|
return read_db_comments(client, r, located_uri.canonical_uri);
|
2014-02-07 18:41:42 +01:00
|
|
|
|
2015-10-22 09:29:02 +02:00
|
|
|
case LocatedUri::Type::PATH:
|
|
|
|
return read_file_comments(r, located_uri.path);
|
2013-10-25 19:35:40 +02:00
|
|
|
}
|
2015-10-22 09:29:02 +02:00
|
|
|
|
|
|
|
gcc_unreachable();
|
2013-10-25 19:35:40 +02:00
|
|
|
}
|
2017-07-21 11:49:28 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Searches for the files listed in #artnames in the UTF8 folder
|
|
|
|
* URI #directory. This can be a local path or protocol-based
|
|
|
|
* URI that #InputStream supports. Returns the first successfully
|
|
|
|
* opened file or #nullptr on failure.
|
|
|
|
*/
|
|
|
|
static InputStreamPtr
|
|
|
|
find_stream_art(const char *directory, Mutex &mutex, Cond &cond)
|
|
|
|
{
|
|
|
|
static constexpr char const * art_names[] = {
|
|
|
|
"cover.png",
|
|
|
|
"cover.jpg",
|
|
|
|
"cover.tiff",
|
|
|
|
"cover.bmp"
|
|
|
|
};
|
|
|
|
|
|
|
|
for(const auto name: art_names) {
|
|
|
|
std::string art_file = PathTraitsUTF8::Build(directory, name);
|
|
|
|
|
|
|
|
try {
|
|
|
|
return InputStream::OpenReady(art_file.c_str(), mutex, cond);
|
|
|
|
} catch (const std::exception &e) {}
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
static CommandResult
|
|
|
|
read_stream_art(Response &r, const char *uri, size_t offset)
|
|
|
|
{
|
2017-08-17 12:57:59 +02:00
|
|
|
std::string art_directory = PathTraitsUTF8::GetParent(uri);
|
2017-07-21 11:49:28 +02:00
|
|
|
|
|
|
|
Mutex mutex;
|
|
|
|
Cond cond;
|
|
|
|
|
2017-08-17 12:57:59 +02:00
|
|
|
InputStreamPtr is = find_stream_art(art_directory.c_str(), mutex, cond);
|
2017-07-21 11:49:28 +02:00
|
|
|
|
|
|
|
if (is == nullptr) {
|
|
|
|
r.Error(ACK_ERROR_NO_EXIST, "No file exists");
|
|
|
|
return CommandResult::ERROR;
|
|
|
|
}
|
|
|
|
if (!is->KnownSize()) {
|
|
|
|
r.Error(ACK_ERROR_NO_EXIST, "Cannot get size for stream");
|
|
|
|
return CommandResult::ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
const size_t art_file_size = is->GetSize();
|
|
|
|
|
|
|
|
constexpr size_t CHUNK_SIZE = 8192;
|
|
|
|
uint8_t buffer[CHUNK_SIZE];
|
|
|
|
size_t read_size;
|
|
|
|
|
|
|
|
is->Seek(offset);
|
|
|
|
read_size = is->Read(&buffer, CHUNK_SIZE);
|
|
|
|
|
|
|
|
r.Format("size: %" PRIu64 "\n"
|
|
|
|
"binary: %u\n",
|
|
|
|
art_file_size,
|
|
|
|
read_size
|
|
|
|
);
|
|
|
|
|
|
|
|
r.Write(buffer, read_size);
|
|
|
|
r.Write("\n");
|
|
|
|
|
|
|
|
return CommandResult::OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef ENABLE_DATABASE
|
|
|
|
static CommandResult
|
|
|
|
read_db_art(Client &client, Response &r, const char *uri, const uint64_t offset)
|
|
|
|
{
|
|
|
|
const Storage *storage = client.GetStorage();
|
|
|
|
if (storage == nullptr) {
|
|
|
|
r.Error(ACK_ERROR_NO_EXIST, "No database");
|
|
|
|
return CommandResult::ERROR;
|
|
|
|
}
|
|
|
|
std::string uri2 = storage->MapUTF8(uri);
|
|
|
|
return read_stream_art(r, uri2.c_str(), offset);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
CommandResult
|
|
|
|
handle_album_art(Client &client, Request args, Response &r)
|
|
|
|
{
|
|
|
|
assert(args.size == 2);
|
|
|
|
|
|
|
|
const char *uri = args.front();
|
|
|
|
size_t offset = args.ParseUnsigned(1);
|
|
|
|
|
|
|
|
const auto located_uri = LocateUri(uri, &client
|
|
|
|
#ifdef ENABLE_DATABASE
|
|
|
|
, nullptr
|
|
|
|
#endif
|
|
|
|
);
|
|
|
|
|
|
|
|
switch (located_uri.type) {
|
|
|
|
case LocatedUri::Type::ABSOLUTE:
|
|
|
|
case LocatedUri::Type::PATH:
|
|
|
|
return read_stream_art(r, located_uri.canonical_uri, offset);
|
|
|
|
case LocatedUri::Type::RELATIVE:
|
|
|
|
#ifdef ENABLE_DATABASE
|
|
|
|
return read_db_art(client, r, located_uri.canonical_uri, offset);
|
|
|
|
#else
|
|
|
|
r.Error(ACK_ERROR_NO_EXIST, "Database disabled");
|
|
|
|
return CommandResult::ERROR;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
r.Error(ACK_ERROR_NO_EXIST, "No art file exists");
|
|
|
|
return CommandResult::ERROR;
|
|
|
|
}
|
|
|
|
|