stored_playlist.c 10.4 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 "config.h"
21
#include "stored_playlist.h"
22
#include "playlist_save.h"
23
#include "song.h"
24
#include "mapper.h"
25
#include "path.h"
26
#include "uri.h"
27
#include "database.h"
28
#include "idle.h"
29
#include "conf.h"
Max Kellermann's avatar
Max Kellermann committed
30 31 32 33 34 35 36

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

39 40 41 42 43 44 45 46 47 48 49 50 51 52
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);
}

53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
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;
}

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

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

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

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

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

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

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

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

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 147
	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);
}

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

	assert(utf8path != NULL);
155

156 157 158
	if (map_spl_path() == NULL)
		return PLAYLIST_RESULT_DISABLED;

159
	path_fs = map_spl_utf8_to_fs(utf8path);
160
	if (path_fs == NULL)
161
		return PLAYLIST_RESULT_BAD_NAME;
162

163
	while (!(file = fopen(path_fs, "w")) && errno == EINTR);
164
	g_free(path_fs);
165 166
	if (file == NULL)
		return PLAYLIST_RESULT_ERRNO;
167

168 169 170
	for (unsigned i = 0; i < list->len; ++i) {
		const char *uri = g_ptr_array_index(list, i);
		playlist_print_uri(file, uri);
171 172 173
	}

	while (fclose(file) != 0 && errno == EINTR);
174
	return PLAYLIST_RESULT_SUCCESS;
175 176
}

177
GPtrArray *
178
spl_load(const char *utf8path)
179 180
{
	FILE *file;
181
	GPtrArray *list;
182
	char buffer[MPD_PATH_MAX];
183
	char *path_fs;
184

185
	if (!spl_valid_name(utf8path) || map_spl_path() == NULL)
186
		return NULL;
187

188
	path_fs = map_spl_utf8_to_fs(utf8path);
189 190
	if (path_fs == NULL)
		return NULL;
191 192

	while (!(file = fopen(path_fs, "r")) && errno == EINTR);
193
	g_free(path_fs);
194
	if (file == NULL)
195
		return NULL;
196

197
	list = g_ptr_array_new();
198

199
	while (fgets(buffer, sizeof(buffer), file)) {
200 201 202 203
		char *s = buffer;

		if (*s == PLAYLIST_COMMENT)
			continue;
204

205 206
		g_strchomp(buffer);

207
		if (!uri_has_scheme(s)) {
208
			char *path_utf8;
209 210
			struct song *song;

211
			path_utf8 = map_fs_to_utf8(s);
212 213 214 215
			if (path_utf8 == NULL)
				continue;

			song = db_get_song(path_utf8);
216
			g_free(path_utf8);
217 218 219
			if (song == NULL)
				continue;

220 221 222
			s = song_get_uri(song);
		} else
			s = g_strdup(s);
223

224
		g_ptr_array_add(list, s);
225 226

		if (list->len >= playlist_max_length)
227
			break;
228 229 230
	}

	while (fclose(file) && errno == EINTR);
231
	return list;
232 233
}

234 235
void
spl_free(GPtrArray *list)
236
{
237 238 239
	for (unsigned i = 0; i < list->len; ++i) {
		char *uri = g_ptr_array_index(list, i);
		g_free(uri);
240 241
	}

242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266
	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;
267 268
}

269
enum playlist_result
270
spl_move_index(const char *utf8path, unsigned src, unsigned dest)
271
{
272 273
	GPtrArray *list;
	char *uri;
274
	enum playlist_result result;
275

276 277 278 279 280
	if (src == dest)
		/* this doesn't check whether the playlist exists, but
		   what the hell.. */
		return PLAYLIST_RESULT_SUCCESS;

281
	if (!(list = spl_load(utf8path)))
282
		return PLAYLIST_RESULT_NO_SUCH_LIST;
283

284 285
	if (src >= list->len || dest >= list->len) {
		spl_free(list);
286
		return PLAYLIST_RESULT_BAD_RANGE;
287 288
	}

289 290 291
	uri = spl_remove_index_internal(list, src);
	spl_insert_index_internal(list, dest, uri);

292
	result = spl_save(list, utf8path);
293

294
	spl_free(list);
295 296

	idle_add(IDLE_STORED_PLAYLIST);
297
	return result;
298 299
}

300
enum playlist_result
301
spl_clear(const char *utf8path)
302
{
303
	char *path_fs;
304 305
	FILE *file;

306 307 308
	if (map_spl_path() == NULL)
		return PLAYLIST_RESULT_DISABLED;

309
	if (!spl_valid_name(utf8path))
310 311
		return PLAYLIST_RESULT_BAD_NAME;

312
	path_fs = map_spl_utf8_to_fs(utf8path);
313
	if (path_fs == NULL)
314
		return PLAYLIST_RESULT_BAD_NAME;
315

316
	while (!(file = fopen(path_fs, "w")) && errno == EINTR);
317
	g_free(path_fs);
318 319
	if (file == NULL)
		return PLAYLIST_RESULT_ERRNO;
320 321

	while (fclose(file) != 0 && errno == EINTR);
322 323

	idle_add(IDLE_STORED_PLAYLIST);
324
	return PLAYLIST_RESULT_SUCCESS;
325 326
}

327 328 329
enum playlist_result
spl_delete(const char *name_utf8)
{
330 331
	char *path_fs;
	int ret;
332

333 334 335
	if (map_spl_path() == NULL)
		return PLAYLIST_RESULT_DISABLED;

336 337 338
	if (!spl_valid_name(name_utf8))
		return PLAYLIST_RESULT_BAD_NAME;

339
	path_fs = map_spl_utf8_to_fs(name_utf8);
340
	if (path_fs == NULL)
341
		return PLAYLIST_RESULT_BAD_NAME;
342

343 344 345
	ret = unlink(path_fs);
	g_free(path_fs);
	if (ret < 0)
346 347 348 349
		return errno == ENOENT
			? PLAYLIST_RESULT_NO_SUCH_LIST
			: PLAYLIST_RESULT_ERRNO;

350
	idle_add(IDLE_STORED_PLAYLIST);
351 352 353
	return PLAYLIST_RESULT_SUCCESS;
}

354
enum playlist_result
355
spl_remove_index(const char *utf8path, unsigned pos)
356
{
357 358
	GPtrArray *list;
	char *uri;
359
	enum playlist_result result;
360

361
	if (!(list = spl_load(utf8path)))
362
		return PLAYLIST_RESULT_NO_SUCH_LIST;
363

364 365
	if (pos >= list->len) {
		spl_free(list);
366
		return PLAYLIST_RESULT_BAD_RANGE;
367 368
	}

369 370
	uri = spl_remove_index_internal(list, pos);
	g_free(uri);
371
	result = spl_save(list, utf8path);
372

373
	spl_free(list);
374 375

	idle_add(IDLE_STORED_PLAYLIST);
376
	return result;
377 378
}

379
enum playlist_result
380
spl_append_song(const char *utf8path, struct song *song)
381 382
{
	FILE *file;
383
	struct stat st;
384
	char *path_fs;
385

386 387 388
	if (map_spl_path() == NULL)
		return PLAYLIST_RESULT_DISABLED;

389
	if (!spl_valid_name(utf8path))
390
		return PLAYLIST_RESULT_BAD_NAME;
391

392
	path_fs = map_spl_utf8_to_fs(utf8path);
393
	if (path_fs == NULL)
394
		return PLAYLIST_RESULT_BAD_NAME;
395 396

	while (!(file = fopen(path_fs, "a")) && errno == EINTR);
397
	g_free(path_fs);
398
	if (file == NULL)
399 400
		return PLAYLIST_RESULT_ERRNO;

401
	if (fstat(fileno(file), &st) < 0) {
402
		int save_errno = errno;
403
		while (fclose(file) != 0 && errno == EINTR);
404 405
		errno = save_errno;
		return PLAYLIST_RESULT_ERRNO;
406
	}
407

408
	if (st.st_size / (MPD_PATH_MAX + 1) >= (off_t)playlist_max_length) {
409
		while (fclose(file) != 0 && errno == EINTR);
410
		return PLAYLIST_RESULT_TOO_LARGE;
411
	}
412

413
	playlist_print_song(file, song);
414 415

	while (fclose(file) != 0 && errno == EINTR);
416 417

	idle_add(IDLE_STORED_PLAYLIST);
418
	return PLAYLIST_RESULT_SUCCESS;
419 420
}

421
enum playlist_result
422
spl_append_uri(const char *url, const char *utf8file)
423 424 425
{
	struct song *song;

426 427
	if (uri_has_scheme(url)) {
		enum playlist_result ret;
428

429 430
		song = song_remote_new(url);
		ret = spl_append_song(utf8file, song);
431 432
		song_free(song);
		return ret;
433 434 435 436
	} else {
		song = db_get_song(url);
		if (song == NULL)
			return PLAYLIST_RESULT_NO_SUCH_SONG;
437

438 439
		return spl_append_song(utf8file, song);
	}
440 441
}

442 443
static enum playlist_result
spl_rename_internal(const char *from_path_fs, const char *to_path_fs)
444
{
445
	if (!g_file_test(from_path_fs, G_FILE_TEST_IS_REGULAR))
446
		return PLAYLIST_RESULT_NO_SUCH_LIST;
447

448
	if (g_file_test(to_path_fs, G_FILE_TEST_EXISTS))
449
		return PLAYLIST_RESULT_LIST_EXISTS;
450

451
	if (rename(from_path_fs, to_path_fs) < 0)
452
		return PLAYLIST_RESULT_ERRNO;
453

454
	idle_add(IDLE_STORED_PLAYLIST);
455
	return PLAYLIST_RESULT_SUCCESS;
456
}
457 458 459 460 461 462 463

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

464 465 466
	if (map_spl_path() == NULL)
		return PLAYLIST_RESULT_DISABLED;

467
	if (!spl_valid_name(utf8from) || !spl_valid_name(utf8to))
468 469 470 471 472
		return PLAYLIST_RESULT_BAD_NAME;

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

473 474 475
	if (from_path_fs != NULL && to_path_fs != NULL)
		ret = spl_rename_internal(from_path_fs, to_path_fs);
	else
476
		ret = PLAYLIST_RESULT_BAD_NAME;
477 478 479 480 481 482

	g_free(from_path_fs);
	g_free(to_path_fs);

	return ret;
}