stored_playlist.c 10 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright (C) 2003-2011 The Music Player Daemon Project
3
 * 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 "text_file.h"
24
#include "song.h"
25
#include "mapper.h"
26
#include "path.h"
27
#include "uri.h"
28
#include "database.h"
29
#include "idle.h"
30
#include "conf.h"
Max Kellermann's avatar
Max Kellermann committed
31 32 33 34 35 36 37

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

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

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

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

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

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

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

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

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

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

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

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

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

	assert(utf8path != NULL);
156

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

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

164
	file = fopen(path_fs, "w");
165
	g_free(path_fs);
166 167
	if (file == NULL)
		return PLAYLIST_RESULT_ERRNO;
168

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

174
	fclose(file);
175
	return PLAYLIST_RESULT_SUCCESS;
176 177
}

178
GPtrArray *
179
spl_load(const char *utf8path)
180 181
{
	FILE *file;
182
	GPtrArray *list;
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
	file = fopen(path_fs, "r");
193
	g_free(path_fs);
194
	if (file == NULL)
195
		return NULL;
196

197
	list = g_ptr_array_new();
198

199 200 201
	GString *buffer = g_string_sized_new(1024);
	char *s;
	while ((s = read_text_line(file, buffer)) != NULL) {
202
		if (*s == 0 || *s == PLAYLIST_COMMENT)
203
			continue;
204

205
		if (!uri_has_scheme(s)) {
206
			char *path_utf8;
207

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

212
			s = path_utf8;
213 214
		} else
			s = g_strdup(s);
215

216
		g_ptr_array_add(list, s);
217 218

		if (list->len >= playlist_max_length)
219
			break;
220 221
	}

222
	fclose(file);
223
	return list;
224 225
}

226 227
void
spl_free(GPtrArray *list)
228
{
229 230 231
	for (unsigned i = 0; i < list->len; ++i) {
		char *uri = g_ptr_array_index(list, i);
		g_free(uri);
232 233
	}

234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258
	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;
259 260
}

261
enum playlist_result
262
spl_move_index(const char *utf8path, unsigned src, unsigned dest)
263
{
264 265
	GPtrArray *list;
	char *uri;
266
	enum playlist_result result;
267

268 269 270 271 272
	if (src == dest)
		/* this doesn't check whether the playlist exists, but
		   what the hell.. */
		return PLAYLIST_RESULT_SUCCESS;

273
	if (!(list = spl_load(utf8path)))
274
		return PLAYLIST_RESULT_NO_SUCH_LIST;
275

276 277
	if (src >= list->len || dest >= list->len) {
		spl_free(list);
278
		return PLAYLIST_RESULT_BAD_RANGE;
279 280
	}

281 282 283
	uri = spl_remove_index_internal(list, src);
	spl_insert_index_internal(list, dest, uri);

284
	result = spl_save(list, utf8path);
285

286
	spl_free(list);
287 288

	idle_add(IDLE_STORED_PLAYLIST);
289
	return result;
290 291
}

292
enum playlist_result
293
spl_clear(const char *utf8path)
294
{
295
	char *path_fs;
296 297
	FILE *file;

298 299 300
	if (map_spl_path() == NULL)
		return PLAYLIST_RESULT_DISABLED;

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

304
	path_fs = map_spl_utf8_to_fs(utf8path);
305
	if (path_fs == NULL)
306
		return PLAYLIST_RESULT_BAD_NAME;
307

308
	file = fopen(path_fs, "w");
309
	g_free(path_fs);
310 311
	if (file == NULL)
		return PLAYLIST_RESULT_ERRNO;
312

313
	fclose(file);
314 315

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

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

325 326 327
	if (map_spl_path() == NULL)
		return PLAYLIST_RESULT_DISABLED;

328 329 330
	if (!spl_valid_name(name_utf8))
		return PLAYLIST_RESULT_BAD_NAME;

331
	path_fs = map_spl_utf8_to_fs(name_utf8);
332
	if (path_fs == NULL)
333
		return PLAYLIST_RESULT_BAD_NAME;
334

335 336 337
	ret = unlink(path_fs);
	g_free(path_fs);
	if (ret < 0)
338 339 340 341
		return errno == ENOENT
			? PLAYLIST_RESULT_NO_SUCH_LIST
			: PLAYLIST_RESULT_ERRNO;

342
	idle_add(IDLE_STORED_PLAYLIST);
343 344 345
	return PLAYLIST_RESULT_SUCCESS;
}

346
enum playlist_result
347
spl_remove_index(const char *utf8path, unsigned pos)
348
{
349 350
	GPtrArray *list;
	char *uri;
351
	enum playlist_result result;
352

353
	if (!(list = spl_load(utf8path)))
354
		return PLAYLIST_RESULT_NO_SUCH_LIST;
355

356 357
	if (pos >= list->len) {
		spl_free(list);
358
		return PLAYLIST_RESULT_BAD_RANGE;
359 360
	}

361 362
	uri = spl_remove_index_internal(list, pos);
	g_free(uri);
363
	result = spl_save(list, utf8path);
364

365
	spl_free(list);
366 367

	idle_add(IDLE_STORED_PLAYLIST);
368
	return result;
369 370
}

371
enum playlist_result
372
spl_append_song(const char *utf8path, struct song *song)
373 374
{
	FILE *file;
375
	struct stat st;
376
	char *path_fs;
377

378 379 380
	if (map_spl_path() == NULL)
		return PLAYLIST_RESULT_DISABLED;

381
	if (!spl_valid_name(utf8path))
382
		return PLAYLIST_RESULT_BAD_NAME;
383

384
	path_fs = map_spl_utf8_to_fs(utf8path);
385
	if (path_fs == NULL)
386
		return PLAYLIST_RESULT_BAD_NAME;
387

388
	file = fopen(path_fs, "a");
389
	g_free(path_fs);
390
	if (file == NULL)
391 392
		return PLAYLIST_RESULT_ERRNO;

393
	if (fstat(fileno(file), &st) < 0) {
394
		int save_errno = errno;
395
		fclose(file);
396 397
		errno = save_errno;
		return PLAYLIST_RESULT_ERRNO;
398
	}
399

400
	if (st.st_size / (MPD_PATH_MAX + 1) >= (off_t)playlist_max_length) {
401
		fclose(file);
402
		return PLAYLIST_RESULT_TOO_LARGE;
403
	}
404

405
	playlist_print_song(file, song);
406

407
	fclose(file);
408 409

	idle_add(IDLE_STORED_PLAYLIST);
410
	return PLAYLIST_RESULT_SUCCESS;
411 412
}

413
enum playlist_result
414
spl_append_uri(const char *url, const char *utf8file)
415 416 417
{
	struct song *song;

418 419
	if (uri_has_scheme(url)) {
		enum playlist_result ret;
420

421 422
		song = song_remote_new(url);
		ret = spl_append_song(utf8file, song);
423 424
		song_free(song);
		return ret;
425 426 427 428
	} else {
		song = db_get_song(url);
		if (song == NULL)
			return PLAYLIST_RESULT_NO_SUCH_SONG;
429

430 431
		return spl_append_song(utf8file, song);
	}
432 433
}

434 435
static enum playlist_result
spl_rename_internal(const char *from_path_fs, const char *to_path_fs)
436
{
437
	if (!g_file_test(from_path_fs, G_FILE_TEST_IS_REGULAR))
438
		return PLAYLIST_RESULT_NO_SUCH_LIST;
439

440
	if (g_file_test(to_path_fs, G_FILE_TEST_EXISTS))
441
		return PLAYLIST_RESULT_LIST_EXISTS;
442

443
	if (rename(from_path_fs, to_path_fs) < 0)
444
		return PLAYLIST_RESULT_ERRNO;
445

446
	idle_add(IDLE_STORED_PLAYLIST);
447
	return PLAYLIST_RESULT_SUCCESS;
448
}
449 450 451 452 453 454 455

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

456 457 458
	if (map_spl_path() == NULL)
		return PLAYLIST_RESULT_DISABLED;

459
	if (!spl_valid_name(utf8from) || !spl_valid_name(utf8to))
460 461 462 463 464
		return PLAYLIST_RESULT_BAD_NAME;

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

465 466 467
	if (from_path_fs != NULL && to_path_fs != NULL)
		ret = spl_rename_internal(from_path_fs, to_path_fs);
	else
468
		ret = PLAYLIST_RESULT_BAD_NAME;
469 470 471 472 473 474

	g_free(from_path_fs);
	g_free(to_path_fs);

	return ret;
}