stored_playlist.c 9.96 KB
Newer Older
1 2 3
/*
 * Copyright (C) 2003-2009 The Music Player Daemon Project
 * http://www.musicpd.org
4 5 6 7 8 9 10 11 12 13
 *
 * 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.
14 15 16 17
 *
 * 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.
18 19
 */

20
#include "stored_playlist.h"
21
#include "playlist_save.h"
22
#include "song.h"
23
#include "mapper.h"
24
#include "path.h"
25
#include "uri.h"
26
#include "database.h"
27
#include "idle.h"
28
#include "conf.h"
Max Kellermann's avatar
Max Kellermann committed
29 30 31 32 33 34 35

#include <assert.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <dirent.h>
#include <string.h>
36
#include <errno.h>
37

38 39 40 41 42 43 44 45 46 47 48 49 50 51
static unsigned playlist_max_length;
bool playlist_saveAbsolutePaths = DEFAULT_PLAYLIST_SAVE_ABSOLUTE_PATHS;

void
spl_global_init(void)
{
	playlist_max_length = config_get_positive(CONF_MAX_PLAYLIST_LENGTH,
						  DEFAULT_PLAYLIST_MAX_LENGTH);

	playlist_saveAbsolutePaths =
		config_get_bool(CONF_SAVE_ABSOLUTE_PATHS,
				DEFAULT_PLAYLIST_SAVE_ABSOLUTE_PATHS);
}

52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
bool
spl_valid_name(const char *name_utf8)
{
	/*
	 * Not supporting '/' was done out of laziness, and we should
	 * really strive to support it in the future.
	 *
	 * Not supporting '\r' and '\n' is done out of protocol
	 * limitations (and arguably laziness), but bending over head
	 * over heels to modify the protocol (and compatibility with
	 * all clients) to support idiots who put '\r' and '\n' in
	 * filenames isn't going to happen, either.
	 */

	return strchr(name_utf8, '/') == NULL &&
		strchr(name_utf8, '\n') == NULL &&
		strchr(name_utf8, '\r') == NULL;
}

71 72 73 74
static struct stored_playlist_info *
load_playlist_info(const char *parent_path_fs, const char *name_fs)
{
	size_t name_length = strlen(name_fs);
75
	char *path_fs, *name, *name_utf8;
76 77 78 79
	int ret;
	struct stat st;
	struct stored_playlist_info *playlist;

80
	if (name_length < sizeof(PLAYLIST_FILE_SUFFIX) ||
81 82 83
	    memchr(name_fs, '\n', name_length) != NULL)
		return NULL;

84
	if (!g_str_has_suffix(name_fs, PLAYLIST_FILE_SUFFIX))
85 86
		return NULL;

Max Kellermann's avatar
Max Kellermann committed
87 88 89
	path_fs = g_build_filename(parent_path_fs, name_fs, NULL);
	ret = stat(path_fs, &st);
	g_free(path_fs);
90 91 92
	if (ret < 0 || !S_ISREG(st.st_mode))
		return NULL;

93 94
	name = g_strndup(name_fs,
			 name_length + 1 - sizeof(PLAYLIST_FILE_SUFFIX));
95
	name_utf8 = fs_charset_to_utf8(name);
96 97 98 99 100
	g_free(name);
	if (name_utf8 == NULL)
		return NULL;

	playlist = g_new(struct stored_playlist_info, 1);
101
	playlist->name = name_utf8;
102 103 104 105 106 107 108
	playlist->mtime = st.st_mtime;
	return playlist;
}

GPtrArray *
spl_list(void)
{
109
	const char *parent_path_fs = map_spl_path();
110 111 112 113 114
	DIR *dir;
	struct dirent *ent;
	GPtrArray *list;
	struct stored_playlist_info *playlist;

115 116 117
	if (parent_path_fs == NULL)
		return NULL;

118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
	dir = opendir(parent_path_fs);
	if (dir == NULL)
		return NULL;

	list = g_ptr_array_new();

	while ((ent = readdir(dir)) != NULL) {
		playlist = load_playlist_info(parent_path_fs, ent->d_name);
		if (playlist != NULL)
			g_ptr_array_add(list, playlist);
	}

	closedir(dir);
	return list;
}

void
spl_list_free(GPtrArray *list)
{
	for (unsigned i = 0; i < list->len; ++i) {
		struct stored_playlist_info *playlist =
			g_ptr_array_index(list, i);
		g_free(playlist->name);
		g_free(playlist);
	}

	g_ptr_array_free(list, true);
}

147
static enum playlist_result
148
spl_save(GPtrArray *list, const char *utf8path)
149 150
{
	FILE *file;
151
	char *path_fs;
152 153

	assert(utf8path != NULL);
154

155
	path_fs = map_spl_utf8_to_fs(utf8path);
156 157
	if (path_fs == NULL)
		return PLAYLIST_RESULT_DISABLED;
158

159
	while (!(file = fopen(path_fs, "w")) && errno == EINTR);
160
	g_free(path_fs);
161 162
	if (file == NULL)
		return PLAYLIST_RESULT_ERRNO;
163

164 165 166
	for (unsigned i = 0; i < list->len; ++i) {
		const char *uri = g_ptr_array_index(list, i);
		playlist_print_uri(file, uri);
167 168 169
	}

	while (fclose(file) != 0 && errno == EINTR);
170
	return PLAYLIST_RESULT_SUCCESS;
171 172
}

173
GPtrArray *
174
spl_load(const char *utf8path)
175 176
{
	FILE *file;
177
	GPtrArray *list;
178
	char buffer[MPD_PATH_MAX];
179
	char *path_fs;
180

181
	if (!spl_valid_name(utf8path))
182
		return NULL;
183

184
	path_fs = map_spl_utf8_to_fs(utf8path);
185 186
	if (path_fs == NULL)
		return NULL;
187 188

	while (!(file = fopen(path_fs, "r")) && errno == EINTR);
189
	g_free(path_fs);
190
	if (file == NULL)
191
		return NULL;
192

193
	list = g_ptr_array_new();
194

195
	while (fgets(buffer, sizeof(buffer), file)) {
196 197 198 199
		char *s = buffer;

		if (*s == PLAYLIST_COMMENT)
			continue;
200

201 202
		g_strchomp(buffer);

203
		if (!uri_has_scheme(s)) {
204
			char *path_utf8;
205 206
			struct song *song;

207
			path_utf8 = map_fs_to_utf8(s);
208 209 210 211
			if (path_utf8 == NULL)
				continue;

			song = db_get_song(path_utf8);
212
			g_free(path_utf8);
213 214 215
			if (song == NULL)
				continue;

216 217 218
			s = song_get_uri(song);
		} else
			s = g_strdup(s);
219

220
		g_ptr_array_add(list, s);
221 222

		if (list->len >= playlist_max_length)
223
			break;
224 225 226
	}

	while (fclose(file) && errno == EINTR);
227
	return list;
228 229
}

230 231
void
spl_free(GPtrArray *list)
232
{
233 234 235
	for (unsigned i = 0; i < list->len; ++i) {
		char *uri = g_ptr_array_index(list, i);
		g_free(uri);
236 237
	}

238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262
	g_ptr_array_free(list, true);
}

static char *
spl_remove_index_internal(GPtrArray *list, unsigned idx)
{
	char *uri;

	assert(idx < list->len);

	uri = g_ptr_array_remove_index(list, idx);
	assert(uri != NULL);
	return uri;
}

static void
spl_insert_index_internal(GPtrArray *list, unsigned idx, char *uri)
{
	assert(idx <= list->len);

	g_ptr_array_add(list, uri);

	memmove(list->pdata + idx + 1, list->pdata + idx,
		(list->len - idx - 1) * sizeof(list->pdata[0]));
	g_ptr_array_index(list, idx) = uri;
263 264
}

265
enum playlist_result
266
spl_move_index(const char *utf8path, unsigned src, unsigned dest)
267
{
268 269
	GPtrArray *list;
	char *uri;
270
	enum playlist_result result;
271

272 273 274 275 276
	if (src == dest)
		/* this doesn't check whether the playlist exists, but
		   what the hell.. */
		return PLAYLIST_RESULT_SUCCESS;

277
	if (!(list = spl_load(utf8path)))
278
		return PLAYLIST_RESULT_NO_SUCH_LIST;
279

280 281
	if (src >= list->len || dest >= list->len) {
		spl_free(list);
282
		return PLAYLIST_RESULT_BAD_RANGE;
283 284
	}

285 286 287
	uri = spl_remove_index_internal(list, src);
	spl_insert_index_internal(list, dest, uri);

288
	result = spl_save(list, utf8path);
289

290
	spl_free(list);
291 292

	idle_add(IDLE_STORED_PLAYLIST);
293
	return result;
294 295
}

296
enum playlist_result
297
spl_clear(const char *utf8path)
298
{
299
	char *path_fs;
300 301
	FILE *file;

302
	if (!spl_valid_name(utf8path))
303 304
		return PLAYLIST_RESULT_BAD_NAME;

305
	path_fs = map_spl_utf8_to_fs(utf8path);
306 307
	if (path_fs == NULL)
		return PLAYLIST_RESULT_DISABLED;
308

309
	while (!(file = fopen(path_fs, "w")) && errno == EINTR);
310
	g_free(path_fs);
311 312
	if (file == NULL)
		return PLAYLIST_RESULT_ERRNO;
313 314

	while (fclose(file) != 0 && errno == EINTR);
315 316

	idle_add(IDLE_STORED_PLAYLIST);
317
	return PLAYLIST_RESULT_SUCCESS;
318 319
}

320 321 322
enum playlist_result
spl_delete(const char *name_utf8)
{
323 324
	char *path_fs;
	int ret;
325

326
	path_fs = map_spl_utf8_to_fs(name_utf8);
327 328 329
	if (path_fs == NULL)
		return PLAYLIST_RESULT_DISABLED;

330 331 332
	ret = unlink(path_fs);
	g_free(path_fs);
	if (ret < 0)
333 334 335 336
		return errno == ENOENT
			? PLAYLIST_RESULT_NO_SUCH_LIST
			: PLAYLIST_RESULT_ERRNO;

337
	idle_add(IDLE_STORED_PLAYLIST);
338 339 340
	return PLAYLIST_RESULT_SUCCESS;
}

341
enum playlist_result
342
spl_remove_index(const char *utf8path, unsigned pos)
343
{
344 345
	GPtrArray *list;
	char *uri;
346
	enum playlist_result result;
347

348
	if (!(list = spl_load(utf8path)))
349
		return PLAYLIST_RESULT_NO_SUCH_LIST;
350

351 352
	if (pos >= list->len) {
		spl_free(list);
353
		return PLAYLIST_RESULT_BAD_RANGE;
354 355
	}

356 357
	uri = spl_remove_index_internal(list, pos);
	g_free(uri);
358
	result = spl_save(list, utf8path);
359

360
	spl_free(list);
361 362

	idle_add(IDLE_STORED_PLAYLIST);
363
	return result;
364 365
}

366
enum playlist_result
367
spl_append_song(const char *utf8path, struct song *song)
368 369
{
	FILE *file;
370
	struct stat st;
371
	char *path_fs;
372

373
	if (!spl_valid_name(utf8path))
374
		return PLAYLIST_RESULT_BAD_NAME;
375

376
	path_fs = map_spl_utf8_to_fs(utf8path);
377 378
	if (path_fs == NULL)
		return PLAYLIST_RESULT_DISABLED;
379 380

	while (!(file = fopen(path_fs, "a")) && errno == EINTR);
381
	g_free(path_fs);
382
	if (file == NULL)
383 384
		return PLAYLIST_RESULT_ERRNO;

385
	if (fstat(fileno(file), &st) < 0) {
386
		int save_errno = errno;
387
		while (fclose(file) != 0 && errno == EINTR);
388 389
		errno = save_errno;
		return PLAYLIST_RESULT_ERRNO;
390
	}
391

392
	if (st.st_size / (MPD_PATH_MAX + 1) >= (off_t)playlist_max_length) {
393
		while (fclose(file) != 0 && errno == EINTR);
394
		return PLAYLIST_RESULT_TOO_LARGE;
395
	}
396

397
	playlist_print_song(file, song);
398 399

	while (fclose(file) != 0 && errno == EINTR);
400 401

	idle_add(IDLE_STORED_PLAYLIST);
402
	return PLAYLIST_RESULT_SUCCESS;
403 404
}

405
enum playlist_result
406
spl_append_uri(const char *url, const char *utf8file)
407 408 409
{
	struct song *song;

410 411
	if (uri_has_scheme(url)) {
		enum playlist_result ret;
412

413 414
		song = song_remote_new(url);
		ret = spl_append_song(utf8file, song);
415 416
		song_free(song);
		return ret;
417 418 419 420
	} else {
		song = db_get_song(url);
		if (song == NULL)
			return PLAYLIST_RESULT_NO_SUCH_SONG;
421

422 423
		return spl_append_song(utf8file, song);
	}
424 425
}

426 427
static enum playlist_result
spl_rename_internal(const char *from_path_fs, const char *to_path_fs)
428
{
429
	if (!g_file_test(from_path_fs, G_FILE_TEST_IS_REGULAR))
430
		return PLAYLIST_RESULT_NO_SUCH_LIST;
431

432
	if (g_file_test(to_path_fs, G_FILE_TEST_EXISTS))
433
		return PLAYLIST_RESULT_LIST_EXISTS;
434

435
	if (rename(from_path_fs, to_path_fs) < 0)
436
		return PLAYLIST_RESULT_ERRNO;
437

438
	idle_add(IDLE_STORED_PLAYLIST);
439
	return PLAYLIST_RESULT_SUCCESS;
440
}
441 442 443 444 445 446 447

enum playlist_result
spl_rename(const char *utf8from, const char *utf8to)
{
	char *from_path_fs, *to_path_fs;
	static enum playlist_result ret;

448
	if (!spl_valid_name(utf8from) || !spl_valid_name(utf8to))
449 450 451 452 453
		return PLAYLIST_RESULT_BAD_NAME;

	from_path_fs = map_spl_utf8_to_fs(utf8from);
	to_path_fs = map_spl_utf8_to_fs(utf8to);

454 455 456 457
	if (from_path_fs != NULL && to_path_fs != NULL)
		ret = spl_rename_internal(from_path_fs, to_path_fs);
	else
		ret = PLAYLIST_RESULT_DISABLED;
458 459 460 461 462 463

	g_free(from_path_fs);
	g_free(to_path_fs);

	return ret;
}