stored_playlist.c 8.74 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/* the Music Player Daemon (MPD)
 * Copyright (C) 2007 by Warren Dukes (warren.dukes@gmail.com)
 * This project's homepage is: 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

19
#include "stored_playlist.h"
20
#include "playlist_save.h"
21
#include "song.h"
22
#include "mapper.h"
23 24
#include "path.h"
#include "utils.h"
25
#include "ls.h"
26
#include "database.h"
27
#include "idle.h"
28
#include "os_compat.h"
29

30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
static struct stored_playlist_info *
load_playlist_info(const char *parent_path_fs, const char *name_fs)
{
	size_t name_length = strlen(name_fs);
	char buffer[MPD_PATH_MAX], *name, *name_utf8;
	int ret;
	struct stat st;
	struct stored_playlist_info *playlist;

	if (name_length < 1 + sizeof(PLAYLIST_FILE_SUFFIX) ||
	    strlen(parent_path_fs) + name_length >= sizeof(buffer) ||
	    memchr(name_fs, '\n', name_length) != NULL)
		return NULL;

	if (name_fs[name_length - sizeof(PLAYLIST_FILE_SUFFIX)] != '.' ||
	    memcmp(name_fs + name_length - sizeof(PLAYLIST_FILE_SUFFIX) + 1,
		   PLAYLIST_FILE_SUFFIX,
		   sizeof(PLAYLIST_FILE_SUFFIX) - 1) != 0)
		return NULL;

	pfx_dir(buffer, name_fs, name_length,
		parent_path_fs, strlen(parent_path_fs));

	ret = stat(buffer, &st);
	if (ret < 0 || !S_ISREG(st.st_mode))
		return NULL;

	name = g_strdup(name_fs);
	name[name_length - sizeof(PLAYLIST_FILE_SUFFIX)] = 0;
	name_utf8 = fs_charset_to_utf8(buffer, name);
	g_free(name);
	if (name_utf8 == NULL)
		return NULL;

	playlist = g_new(struct stored_playlist_info, 1);
	playlist->name = g_strdup(name_utf8);
	playlist->mtime = st.st_mtime;
	return playlist;
}

GPtrArray *
spl_list(void)
{
	char parent_path_fs[MPD_PATH_MAX];
	DIR *dir;
	struct dirent *ent;
	GPtrArray *list;
	struct stored_playlist_info *playlist;

	rpp2app_r(parent_path_fs, "");
	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);
}

109
static enum playlist_result
110
spl_save(GPtrArray *list, const char *utf8path)
111 112
{
	FILE *file;
113 114 115
	char path_max_tmp[MPD_PATH_MAX];

	assert(utf8path != NULL);
116

117
	utf8_to_fs_playlist_path(path_max_tmp, utf8path);
118

119
	while (!(file = fopen(path_max_tmp, "w")) && errno == EINTR);
120 121
	if (file == NULL)
		return PLAYLIST_RESULT_ERRNO;
122

123 124 125
	for (unsigned i = 0; i < list->len; ++i) {
		const char *uri = g_ptr_array_index(list, i);
		playlist_print_uri(file, uri);
126 127 128
	}

	while (fclose(file) != 0 && errno == EINTR);
129
	return PLAYLIST_RESULT_SUCCESS;
130 131
}

132
GPtrArray *
133
spl_load(const char *utf8path)
134 135
{
	FILE *file;
136
	GPtrArray *list;
137
	char buffer[MPD_PATH_MAX];
Eric Wong's avatar
Eric Wong committed
138
	char path_max_tmp[MPD_PATH_MAX];
139

140
	if (!is_valid_playlist_name(utf8path))
141
		return NULL;
142

143 144
	utf8_to_fs_playlist_path(path_max_tmp, utf8path);
	while (!(file = fopen(path_max_tmp, "r")) && errno == EINTR);
145
	if (file == NULL)
146
		return NULL;
147

148
	list = g_ptr_array_new();
149

150 151
	while (myFgets(buffer, sizeof(buffer), file)) {
		char *s = buffer;
152
		const char *path_utf8;
153 154 155

		if (*s == PLAYLIST_COMMENT)
			continue;
156

157
		if (!isValidRemoteUtf8Url(s)) {
158 159 160 161 162 163 164 165 166 167
			struct song *song;

			path_utf8 = map_fs_to_utf8(s, path_max_tmp);
			if (path_utf8 == NULL)
				continue;

			song = db_get_song(path_utf8);
			if (song == NULL)
				continue;

168
			s = song_get_url(song, path_max_tmp);
169
		}
170

171 172 173
		g_ptr_array_add(list, xstrdup(s));

		if (list->len >= playlist_max_length)
174
			break;
175 176 177
	}

	while (fclose(file) && errno == EINTR);
178
	return list;
179 180
}

181 182
void
spl_free(GPtrArray *list)
183
{
184 185 186
	for (unsigned i = 0; i < list->len; ++i) {
		char *uri = g_ptr_array_index(list, i);
		g_free(uri);
187 188
	}

189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213
	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;
214 215
}

216
enum playlist_result
217
spl_move_index(const char *utf8path, unsigned src, unsigned dest)
218
{
219 220
	GPtrArray *list;
	char *uri;
221
	enum playlist_result result;
222

223 224 225 226 227
	if (src == dest)
		/* this doesn't check whether the playlist exists, but
		   what the hell.. */
		return PLAYLIST_RESULT_SUCCESS;

228
	if (!(list = spl_load(utf8path)))
229
		return PLAYLIST_RESULT_NO_SUCH_LIST;
230

231 232
	if (src >= list->len || dest >= list->len) {
		spl_free(list);
233
		return PLAYLIST_RESULT_BAD_RANGE;
234 235
	}

236 237 238
	uri = spl_remove_index_internal(list, src);
	spl_insert_index_internal(list, dest, uri);

239
	result = spl_save(list, utf8path);
240

241
	spl_free(list);
242 243

	idle_add(IDLE_STORED_PLAYLIST);
244
	return result;
245 246
}

247
enum playlist_result
248
spl_clear(const char *utf8path)
249
{
250
	char filename[MPD_PATH_MAX];
251 252
	FILE *file;

253 254 255
	if (!is_valid_playlist_name(utf8path))
		return PLAYLIST_RESULT_BAD_NAME;

256
	utf8_to_fs_playlist_path(filename, utf8path);
257 258

	while (!(file = fopen(filename, "w")) && errno == EINTR);
259 260
	if (file == NULL)
		return PLAYLIST_RESULT_ERRNO;
261 262

	while (fclose(file) != 0 && errno == EINTR);
263 264

	idle_add(IDLE_STORED_PLAYLIST);
265
	return PLAYLIST_RESULT_SUCCESS;
266 267
}

268 269 270 271 272 273 274 275 276 277 278 279
enum playlist_result
spl_delete(const char *name_utf8)
{
	char filename[MPD_PATH_MAX];

	utf8_to_fs_playlist_path(filename, name_utf8);

	if (unlink(filename) < 0)
		return errno == ENOENT
			? PLAYLIST_RESULT_NO_SUCH_LIST
			: PLAYLIST_RESULT_ERRNO;

280
	idle_add(IDLE_STORED_PLAYLIST);
281 282 283
	return PLAYLIST_RESULT_SUCCESS;
}

284
enum playlist_result
285
spl_remove_index(const char *utf8path, unsigned pos)
286
{
287 288
	GPtrArray *list;
	char *uri;
289
	enum playlist_result result;
290

291
	if (!(list = spl_load(utf8path)))
292
		return PLAYLIST_RESULT_NO_SUCH_LIST;
293

294 295
	if (pos >= list->len) {
		spl_free(list);
296
		return PLAYLIST_RESULT_BAD_RANGE;
297 298
	}

299 300
	uri = spl_remove_index_internal(list, pos);
	g_free(uri);
301
	result = spl_save(list, utf8path);
302

303
	spl_free(list);
304 305

	idle_add(IDLE_STORED_PLAYLIST);
306
	return result;
307 308
}

309
enum playlist_result
310
spl_append_song(const char *utf8path, struct song *song)
311 312
{
	FILE *file;
313
	struct stat st;
Eric Wong's avatar
Eric Wong committed
314
	char path_max_tmp[MPD_PATH_MAX];
315

316 317
	if (!is_valid_playlist_name(utf8path))
		return PLAYLIST_RESULT_BAD_NAME;
318
	utf8_to_fs_playlist_path(path_max_tmp, utf8path);
319

320
	while (!(file = fopen(path_max_tmp, "a")) && errno == EINTR);
321
	if (file == NULL) {
322 323 324 325
		int save_errno = errno;
		while (fclose(file) != 0 && errno == EINTR);
		errno = save_errno;
		return PLAYLIST_RESULT_ERRNO;
326
	}
327

328
	if (fstat(fileno(file), &st) < 0) {
329
		int save_errno = errno;
330
		while (fclose(file) != 0 && errno == EINTR);
331 332
		errno = save_errno;
		return PLAYLIST_RESULT_ERRNO;
333
	}
334

335
	if (st.st_size >= ((MPD_PATH_MAX+1) * (off_t)playlist_max_length)) {
336
		while (fclose(file) != 0 && errno == EINTR);
337
		return PLAYLIST_RESULT_TOO_LARGE;
338
	}
339

340
	playlist_print_song(file, song);
341 342

	while (fclose(file) != 0 && errno == EINTR);
343 344

	idle_add(IDLE_STORED_PLAYLIST);
345
	return PLAYLIST_RESULT_SUCCESS;
346 347
}

348
enum playlist_result
349
spl_append_uri(const char *url, const char *utf8file)
350 351 352 353 354 355 356 357
{
	struct song *song;

	song = db_get_song(url);
	if (song)
		return spl_append_song(utf8file, song);

	if (!isValidRemoteUtf8Url(url))
358
		return PLAYLIST_RESULT_NO_SUCH_SONG;
359 360 361

	song = song_remote_new(url);
	if (song) {
362
		enum playlist_result ret = spl_append_song(utf8file, song);
363 364 365 366
		song_free(song);
		return ret;
	}

367
	return PLAYLIST_RESULT_NO_SUCH_SONG;
368 369
}

370
enum playlist_result
371
spl_rename(const char *utf8from, const char *utf8to)
372 373
{
	struct stat st;
374 375
	char from[MPD_PATH_MAX];
	char to[MPD_PATH_MAX];
376

377 378 379
	if (!is_valid_playlist_name(utf8from) ||
	    !is_valid_playlist_name(utf8to))
		return PLAYLIST_RESULT_BAD_NAME;
380

381 382
	utf8_to_fs_playlist_path(from, utf8from);
	utf8_to_fs_playlist_path(to, utf8to);
383

384 385
	if (stat(from, &st) != 0)
		return PLAYLIST_RESULT_NO_SUCH_LIST;
386

387 388
	if (stat(to, &st) == 0)
		return PLAYLIST_RESULT_LIST_EXISTS;
389

390 391
	if (rename(from, to) < 0)
		return PLAYLIST_RESULT_ERRNO;
392

393
	idle_add(IDLE_STORED_PLAYLIST);
394
	return PLAYLIST_RESULT_SUCCESS;
395
}