stored_playlist.c 8.92 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
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)
{
73
	const char *parent_path_fs = map_spl_path();
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
	DIR *dir;
	struct dirent *ent;
	GPtrArray *list;
	struct stored_playlist_info *playlist;

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

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

	assert(utf8path != NULL);
116

117
	path_fs = map_spl_utf8_to_fs(utf8path, path_max_tmp);
118

119
	while (!(file = fopen(path_fs, "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
	const char *path_fs;
140

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

144 145 146
	path_fs = map_spl_utf8_to_fs(utf8path, path_max_tmp);

	while (!(file = fopen(path_fs, "r")) && errno == EINTR);
147
	if (file == NULL)
148
		return NULL;
149

150
	list = g_ptr_array_new();
151

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

		if (*s == PLAYLIST_COMMENT)
			continue;
158

159
		if (!isValidRemoteUtf8Url(s)) {
160 161 162 163 164 165 166 167 168 169
			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;

170
			s = song_get_url(song, path_max_tmp);
171
		}
172

173 174 175
		g_ptr_array_add(list, xstrdup(s));

		if (list->len >= playlist_max_length)
176
			break;
177 178 179
	}

	while (fclose(file) && errno == EINTR);
180
	return list;
181 182
}

183 184
void
spl_free(GPtrArray *list)
185
{
186 187 188
	for (unsigned i = 0; i < list->len; ++i) {
		char *uri = g_ptr_array_index(list, i);
		g_free(uri);
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 214 215
	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;
216 217
}

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

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

230
	if (!(list = spl_load(utf8path)))
231
		return PLAYLIST_RESULT_NO_SUCH_LIST;
232

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

238 239 240
	uri = spl_remove_index_internal(list, src);
	spl_insert_index_internal(list, dest, uri);

241
	result = spl_save(list, utf8path);
242

243
	spl_free(list);
244 245

	idle_add(IDLE_STORED_PLAYLIST);
246
	return result;
247 248
}

249
enum playlist_result
250
spl_clear(const char *utf8path)
251
{
252
	char filename[MPD_PATH_MAX];
253
	const char *path_fs;
254 255
	FILE *file;

256 257 258
	if (!is_valid_playlist_name(utf8path))
		return PLAYLIST_RESULT_BAD_NAME;

259
	path_fs = map_spl_utf8_to_fs(utf8path, filename);
260

261
	while (!(file = fopen(path_fs, "w")) && errno == EINTR);
262 263
	if (file == NULL)
		return PLAYLIST_RESULT_ERRNO;
264 265

	while (fclose(file) != 0 && errno == EINTR);
266 267

	idle_add(IDLE_STORED_PLAYLIST);
268
	return PLAYLIST_RESULT_SUCCESS;
269 270
}

271 272 273 274
enum playlist_result
spl_delete(const char *name_utf8)
{
	char filename[MPD_PATH_MAX];
275
	const char *path_fs;
276

277
	path_fs = map_spl_utf8_to_fs(name_utf8, filename);
278

279
	if (unlink(path_fs) < 0)
280 281 282 283
		return errno == ENOENT
			? PLAYLIST_RESULT_NO_SUCH_LIST
			: PLAYLIST_RESULT_ERRNO;

284
	idle_add(IDLE_STORED_PLAYLIST);
285 286 287
	return PLAYLIST_RESULT_SUCCESS;
}

288
enum playlist_result
289
spl_remove_index(const char *utf8path, unsigned pos)
290
{
291 292
	GPtrArray *list;
	char *uri;
293
	enum playlist_result result;
294

295
	if (!(list = spl_load(utf8path)))
296
		return PLAYLIST_RESULT_NO_SUCH_LIST;
297

298 299
	if (pos >= list->len) {
		spl_free(list);
300
		return PLAYLIST_RESULT_BAD_RANGE;
301 302
	}

303 304
	uri = spl_remove_index_internal(list, pos);
	g_free(uri);
305
	result = spl_save(list, utf8path);
306

307
	spl_free(list);
308 309

	idle_add(IDLE_STORED_PLAYLIST);
310
	return result;
311 312
}

313
enum playlist_result
314
spl_append_song(const char *utf8path, struct song *song)
315 316
{
	FILE *file;
317
	struct stat st;
Eric Wong's avatar
Eric Wong committed
318
	char path_max_tmp[MPD_PATH_MAX];
319
	const char *path_fs;
320

321 322
	if (!is_valid_playlist_name(utf8path))
		return PLAYLIST_RESULT_BAD_NAME;
323

324 325 326
	path_fs = map_spl_utf8_to_fs(utf8path, path_max_tmp);

	while (!(file = fopen(path_fs, "a")) && errno == EINTR);
327
	if (file == NULL) {
328 329 330 331
		int save_errno = errno;
		while (fclose(file) != 0 && errno == EINTR);
		errno = save_errno;
		return PLAYLIST_RESULT_ERRNO;
332
	}
333

334
	if (fstat(fileno(file), &st) < 0) {
335
		int save_errno = errno;
336
		while (fclose(file) != 0 && errno == EINTR);
337 338
		errno = save_errno;
		return PLAYLIST_RESULT_ERRNO;
339
	}
340

341
	if (st.st_size >= ((MPD_PATH_MAX+1) * (off_t)playlist_max_length)) {
342
		while (fclose(file) != 0 && errno == EINTR);
343
		return PLAYLIST_RESULT_TOO_LARGE;
344
	}
345

346
	playlist_print_song(file, song);
347 348

	while (fclose(file) != 0 && errno == EINTR);
349 350

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

354
enum playlist_result
355
spl_append_uri(const char *url, const char *utf8file)
356 357 358 359 360 361 362 363
{
	struct song *song;

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

	if (!isValidRemoteUtf8Url(url))
364
		return PLAYLIST_RESULT_NO_SUCH_SONG;
365 366 367

	song = song_remote_new(url);
	if (song) {
368
		enum playlist_result ret = spl_append_song(utf8file, song);
369 370 371 372
		song_free(song);
		return ret;
	}

373
	return PLAYLIST_RESULT_NO_SUCH_SONG;
374 375
}

376
enum playlist_result
377
spl_rename(const char *utf8from, const char *utf8to)
378 379
{
	struct stat st;
380 381
	char from[MPD_PATH_MAX];
	char to[MPD_PATH_MAX];
382
	const char *from_path_fs, *to_path_fs;
383

384 385 386
	if (!is_valid_playlist_name(utf8from) ||
	    !is_valid_playlist_name(utf8to))
		return PLAYLIST_RESULT_BAD_NAME;
387

388 389
	from_path_fs = map_spl_utf8_to_fs(utf8from, from);
	to_path_fs = map_spl_utf8_to_fs(utf8to, to);
390

391
	if (stat(from_path_fs, &st) != 0)
392
		return PLAYLIST_RESULT_NO_SUCH_LIST;
393

394
	if (stat(to_path_fs, &st) == 0)
395
		return PLAYLIST_RESULT_LIST_EXISTS;
396

397
	if (rename(from_path_fs, to_path_fs) < 0)
398
		return PLAYLIST_RESULT_ERRNO;
399

400
	idle_add(IDLE_STORED_PLAYLIST);
401
	return PLAYLIST_RESULT_SUCCESS;
402
}