FileCommands.cxx 6.14 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright (C) 2003-2015 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 "FileCommands.hxx"
24
#include "Request.hxx"
25 26
#include "CommandError.hxx"
#include "protocol/Ack.hxx"
27
#include "client/Client.hxx"
28
#include "client/Response.hxx"
29
#include "util/ConstBuffer.hxx"
30
#include "util/CharUtil.hxx"
31
#include "util/UriUtil.hxx"
32 33
#include "util/Error.hxx"
#include "tag/TagHandler.hxx"
34 35
#include "tag/ApeTag.hxx"
#include "tag/TagId3.hxx"
36
#include "TagStream.hxx"
37
#include "TagFile.hxx"
38
#include "storage/StorageInterface.hxx"
39
#include "fs/AllocatedPath.hxx"
40
#include "fs/FileInfo.hxx"
41
#include "fs/DirectoryReader.hxx"
42
#include "TimePrint.hxx"
43
#include "ls.hxx"
44 45

#include <assert.h>
46 47 48 49 50
#include <sys/stat.h>
#include <inttypes.h> /* for PRIu64 */

gcc_pure
static bool
51
SkipNameFS(PathTraitsFS::const_pointer name_fs)
52 53 54 55 56 57 58 59
{
	return name_fs[0] == '.' &&
		(name_fs[1] == 0 ||
		 (name_fs[1] == '.' && name_fs[2] == 0));
}

gcc_pure
static bool
60
skip_path(Path name_fs)
61
{
62
	return name_fs.HasNewline();
63 64
}

65 66 67 68 69 70 71
#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

72
CommandResult
73 74
handle_listfiles_local(Client &client, Response &r,
		       const char *path_utf8)
75 76 77
{
	const auto path_fs = AllocatedPath::FromUTF8(path_utf8);
	if (path_fs.IsNull()) {
78
		r.Error(ACK_ERROR_NO_EXIST, "unsupported file name");
79 80 81 82 83
		return CommandResult::ERROR;
	}

	Error error;
	if (!client.AllowFile(path_fs, error))
84
		return print_error(r, error);
85 86 87 88

	DirectoryReader reader(path_fs);
	if (reader.HasFailed()) {
		error.FormatErrno("Failed to open '%s'", path_utf8);
89
		return print_error(r, error);
90 91 92 93
	}

	while (reader.ReadEntry()) {
		const Path name_fs = reader.GetEntry();
94
		if (SkipNameFS(name_fs.c_str()) || skip_path(name_fs))
95 96 97 98 99 100 101 102
			continue;

		std::string name_utf8 = name_fs.ToUTF8();
		if (name_utf8.empty())
			continue;

		const AllocatedPath full_fs =
			AllocatedPath::Build(path_fs, name_fs);
103 104
		FileInfo fi;
		if (!GetFileInfo(full_fs, fi, false))
105 106
			continue;

107
		if (fi.IsRegular())
108 109 110 111
			r.Format("file: %s\n"
				 "size: %" PRIu64 "\n",
				 name_utf8.c_str(),
				 fi.GetSize());
112
		else if (fi.IsDirectory())
113
			r.Format("directory: %s\n", name_utf8.c_str());
114 115
		else
			continue;
116

117
		time_print(r, "Last-Modified", fi.GetModificationTime());
118 119 120 121
	}

	return CommandResult::OK;
}
122

123 124 125 126
#if defined(WIN32) && GCC_CHECK_VERSION(4,6)
#pragma GCC diagnostic pop
#endif

127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
gcc_pure
static bool
IsValidName(const char *p)
{
	if (!IsAlphaASCII(*p))
		return false;

	while (*++p) {
		const char ch = *p;
		if (!IsAlphaASCII(ch) && ch != '_' && ch != '-')
			return false;
	}

	return true;
}

gcc_pure
static bool
IsValidValue(const char *p)
{
	while (*p) {
		const char ch = *p++;

150
		if ((unsigned char)ch < 0x20)
151 152 153 154 155 156 157 158 159
			return false;
	}

	return true;
}

static void
print_pair(const char *key, const char *value, void *ctx)
{
160
	auto &r = *(Response *)ctx;
161 162

	if (IsValidName(key) && IsValidValue(value))
163
		r.Format("%s: %s\n", key, value);
164 165 166 167 168 169 170 171
}

static constexpr tag_handler print_comment_handler = {
	nullptr,
	nullptr,
	print_pair,
};

172
static CommandResult
173
read_stream_comments(Response &r, const char *uri)
174 175
{
	if (!uri_supported_scheme(uri)) {
176
		r.Error(ACK_ERROR_NO_EXIST, "unsupported URI scheme");
177 178 179
		return CommandResult::ERROR;
	}

180 181
	if (!tag_stream_scan(uri, print_comment_handler, &r)) {
		r.Error(ACK_ERROR_NO_EXIST, "Failed to load file");
182 183 184 185 186 187 188
		return CommandResult::ERROR;
	}

	return CommandResult::OK;

}

189
static CommandResult
190
read_file_comments(Response &r, const Path path_fs)
191
{
192 193
	if (!tag_file_scan(path_fs, print_comment_handler, &r)) {
		r.Error(ACK_ERROR_NO_EXIST, "Failed to load file");
194 195 196
		return CommandResult::ERROR;
	}

197 198
	tag_ape_scan2(path_fs, &print_comment_handler, &r);
	tag_id3_scan(path_fs, &print_comment_handler, &r);
199 200 201 202 203

	return CommandResult::OK;

}

204 205 206 207 208 209 210 211 212 213 214
static const char *
translate_uri(const char *uri)
{
	if (memcmp(uri, "file:///", 8) == 0)
		/* drop the "file://", leave only an absolute path
		   (starting with a slash) */
		return uri + 7;

	return uri;
}

215
CommandResult
216
handle_read_comments(Client &client, Request args, Response &r)
217
{
218
	assert(args.size == 1);
219
	const char *const uri = translate_uri(args.front());
220 221 222 223

	if (memcmp(uri, "file:///", 8) == 0) {
		/* read comments from arbitrary local file */
		const char *path_utf8 = uri + 7;
224
		AllocatedPath path_fs = AllocatedPath::FromUTF8(path_utf8);
225
		if (path_fs.IsNull()) {
226
			r.Error(ACK_ERROR_NO_EXIST, "unsupported file name");
227 228 229 230
			return CommandResult::ERROR;
		}

		Error error;
231
		if (!client.AllowFile(path_fs, error))
232
			return print_error(r, error);
233

234
		return read_file_comments(r, path_fs);
235
	} else if (uri_has_scheme(uri)) {
236
		return read_stream_comments(r, uri);
237
	} else if (!PathTraitsUTF8::IsAbsolute(uri)) {
238
#ifdef ENABLE_DATABASE
239 240 241
		const Storage *storage = client.GetStorage();
		if (storage == nullptr) {
#endif
242
			r.Error(ACK_ERROR_NO_EXIST, "No database");
243 244 245 246
			return CommandResult::ERROR;
#ifdef ENABLE_DATABASE
		}

247 248 249
		{
			AllocatedPath path_fs = storage->MapFS(uri);
			if (!path_fs.IsNull())
250
				return read_file_comments(r, path_fs);
251
		}
252

253 254 255
		{
			const std::string uri2 = storage->MapUTF8(uri);
			if (uri_has_scheme(uri2.c_str()))
256
				return read_stream_comments(r, uri2.c_str());
257 258
		}

259
		r.Error(ACK_ERROR_NO_EXIST, "No such file");
260
		return CommandResult::ERROR;
261
#endif
262
	} else {
263
		r.Error(ACK_ERROR_NO_EXIST, "No such file");
264 265 266
		return CommandResult::ERROR;
	}
}