storedPlaylist.c 7.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/* 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
 */

#include "storedPlaylist.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

Max Kellermann's avatar
Max Kellermann committed
30
static ListNode *nodeOfStoredPlaylist(List *list, int idx)
31 32 33 34 35
{
	int forward;
	ListNode *node;
	int i;

Max Kellermann's avatar
Max Kellermann committed
36
	if (idx >= list->numberOfNodes || idx < 0)
37 38
		return NULL;

Max Kellermann's avatar
Max Kellermann committed
39
	if (idx > (list->numberOfNodes/2)) {
40
		forward = 0;
41 42
		node = list->lastNode;
		i = list->numberOfNodes - 1;
43 44
	} else {
		forward = 1;
45
		node = list->firstNode;
46 47 48 49
		i = 0;
	}

	while (node != NULL) {
Max Kellermann's avatar
Max Kellermann committed
50
		if (i == idx)
51 52 53 54 55 56 57 58 59 60 61 62 63 64
			return node;

		if (forward) {
			i++;
			node = node->nextNode;
		} else {
			i--;
			node = node->prevNode;
		}
	}

	return NULL;
}

65
static enum playlist_result
66
writeStoredPlaylistToPath(List *list, const char *utf8path)
67 68 69
{
	ListNode *node;
	FILE *file;
70 71 72
	char path_max_tmp[MPD_PATH_MAX];

	assert(utf8path != NULL);
73

74
	utf8_to_fs_playlist_path(path_max_tmp, utf8path);
75

76
	while (!(file = fopen(path_max_tmp, "w")) && errno == EINTR);
77 78
	if (file == NULL)
		return PLAYLIST_RESULT_ERRNO;
79

80
	node = list->firstNode;
81
	while (node != NULL) {
82
		playlist_print_uri(file, (const char *)node->data);
83 84 85 86
		node = node->nextNode;
	}

	while (fclose(file) != 0 && errno == EINTR);
87
	return PLAYLIST_RESULT_SUCCESS;
88 89
}

90
List *loadStoredPlaylist(const char *utf8path)
91
{
92
	List *list;
93
	FILE *file;
94
	char buffer[MPD_PATH_MAX];
Eric Wong's avatar
Eric Wong committed
95
	char path_max_tmp[MPD_PATH_MAX];
96

97
	if (!is_valid_playlist_name(utf8path))
98
		return NULL;
99

100 101
	utf8_to_fs_playlist_path(path_max_tmp, utf8path);
	while (!(file = fopen(path_max_tmp, "r")) && errno == EINTR);
102
	if (file == NULL)
103
		return NULL;
104

105
	list = makeList(DEFAULT_FREE_DATA_FUNC, 0);
106

107 108
	while (myFgets(buffer, sizeof(buffer), file)) {
		char *s = buffer;
109
		const char *path_utf8;
110 111 112

		if (*s == PLAYLIST_COMMENT)
			continue;
113 114 115 116 117 118 119 120 121 122 123 124 125 126

		if (isValidRemoteUtf8Url(s))
			insertInListWithoutKey(list, xstrdup(s));
		else {
			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;

Max Kellermann's avatar
Max Kellermann committed
127
			song_get_url(song, path_max_tmp);
128
			insertInListWithoutKey(list, xstrdup(path_max_tmp));
129
		}
130 131 132

		if (list->numberOfNodes >= playlist_max_length)
			break;
133 134 135
	}

	while (fclose(file) && errno == EINTR);
136
	return list;
137 138
}

139
static int moveSongInStoredPlaylist(List *list, int src, int dest)
140 141 142
{
	ListNode *srcNode, *destNode;

143
	if (src >= list->numberOfNodes || dest >= list->numberOfNodes ||
144
	    src < 0 || dest < 0 || src == dest)
145 146
		return -1;

147
	srcNode = nodeOfStoredPlaylist(list, src);
148 149 150
	if (!srcNode)
		return -1;

151
	destNode = nodeOfStoredPlaylist(list, dest);
152 153 154 155 156

	/* remove src */
	if (srcNode->prevNode)
		srcNode->prevNode->nextNode = srcNode->nextNode;
	else
157
		list->firstNode = srcNode->nextNode;
158 159 160 161

	if (srcNode->nextNode)
		srcNode->nextNode->prevNode = srcNode->prevNode;
	else
162
		list->lastNode = srcNode->prevNode;
163 164 165 166 167

	/* this is all a bit complicated - but I tried to
	 * maintain the same order stuff is moved as in the
	 * real playlist */
	if (dest == 0) {
168 169
		list->firstNode->prevNode = srcNode;
		srcNode->nextNode = list->firstNode;
170
		srcNode->prevNode = NULL;
171 172 173
		list->firstNode = srcNode;
	} else if ((dest + 1) == list->numberOfNodes) {
		list->lastNode->nextNode = srcNode;
174
		srcNode->nextNode = NULL;
175 176
		srcNode->prevNode = list->lastNode;
		list->lastNode = srcNode;
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
	} else {
		if (destNode == NULL) {
			/* this shouldn't be happening. */
			return -1;
		}

		if (src > dest) {
			destNode->prevNode->nextNode = srcNode;
			srcNode->prevNode = destNode->prevNode;
			srcNode->nextNode = destNode;
			destNode->prevNode = srcNode;
		} else {
			destNode->nextNode->prevNode = srcNode;
			srcNode->prevNode = destNode;
			srcNode->nextNode = destNode->nextNode;
			destNode->nextNode = srcNode;
		}
	}

196
	idle_add(IDLE_STORED_PLAYLIST);
197 198 199
	return 0;
}

200 201
enum playlist_result
moveSongInStoredPlaylistByPath(const char *utf8path, int src, int dest)
202
{
203
	List *list;
204
	enum playlist_result result;
205

206 207
	if (!(list = loadStoredPlaylist(utf8path)))
		return PLAYLIST_RESULT_NO_SUCH_LIST;
208

209
	if (moveSongInStoredPlaylist(list, src, dest) != 0) {
210
		freeList(list);
211
		return PLAYLIST_RESULT_BAD_RANGE;
212 213
	}

214
	result = writeStoredPlaylistToPath(list, utf8path);
215

216
	freeList(list);
217 218

	idle_add(IDLE_STORED_PLAYLIST);
219
	return result;
220 221
}

222 223
enum playlist_result
removeAllFromStoredPlaylistByPath(const char *utf8path)
224
{
225
	char filename[MPD_PATH_MAX];
226 227
	FILE *file;

228 229 230
	if (!is_valid_playlist_name(utf8path))
		return PLAYLIST_RESULT_BAD_NAME;

231
	utf8_to_fs_playlist_path(filename, utf8path);
232 233

	while (!(file = fopen(filename, "w")) && errno == EINTR);
234 235
	if (file == NULL)
		return PLAYLIST_RESULT_ERRNO;
236 237

	while (fclose(file) != 0 && errno == EINTR);
238 239

	idle_add(IDLE_STORED_PLAYLIST);
240
	return PLAYLIST_RESULT_SUCCESS;
241 242
}

243
static int removeOneSongFromStoredPlaylist(List *list, int pos)
244
{
245
	ListNode *node = nodeOfStoredPlaylist(list, pos);
246
	if (!node)
247 248
		return -1;

249
	deleteNodeFromList(list, node);
250 251 252 253

	return 0;
}

254 255
enum playlist_result
removeOneSongFromStoredPlaylistByPath(const char *utf8path, int pos)
256
{
257
	List *list;
258
	enum playlist_result result;
259

260 261
	if (!(list = loadStoredPlaylist(utf8path)))
		return PLAYLIST_RESULT_NO_SUCH_LIST;
262

263
	if (removeOneSongFromStoredPlaylist(list, pos) != 0) {
264
		freeList(list);
265
		return PLAYLIST_RESULT_BAD_RANGE;
266 267
	}

268
	result = writeStoredPlaylistToPath(list, utf8path);
269

270
	freeList(list);
271 272

	idle_add(IDLE_STORED_PLAYLIST);
273
	return result;
274 275
}

276
enum playlist_result
277
appendSongToStoredPlaylistByPath(const char *utf8path, struct song *song)
278 279
{
	FILE *file;
280
	struct stat st;
Eric Wong's avatar
Eric Wong committed
281
	char path_max_tmp[MPD_PATH_MAX];
282

283 284
	if (!is_valid_playlist_name(utf8path))
		return PLAYLIST_RESULT_BAD_NAME;
285
	utf8_to_fs_playlist_path(path_max_tmp, utf8path);
286

287
	while (!(file = fopen(path_max_tmp, "a")) && errno == EINTR);
288
	if (file == NULL) {
289 290 291 292
		int save_errno = errno;
		while (fclose(file) != 0 && errno == EINTR);
		errno = save_errno;
		return PLAYLIST_RESULT_ERRNO;
293
	}
294

295
	if (fstat(fileno(file), &st) < 0) {
296
		int save_errno = errno;
297
		while (fclose(file) != 0 && errno == EINTR);
298 299
		errno = save_errno;
		return PLAYLIST_RESULT_ERRNO;
300
	}
301

302
	if (st.st_size >= ((MPD_PATH_MAX+1) * playlist_max_length)) {
303
		while (fclose(file) != 0 && errno == EINTR);
304
		return PLAYLIST_RESULT_TOO_LARGE;
305
	}
306

307
	playlist_print_song(file, song);
308 309

	while (fclose(file) != 0 && errno == EINTR);
310 311

	idle_add(IDLE_STORED_PLAYLIST);
312
	return PLAYLIST_RESULT_SUCCESS;
313 314
}

315 316
enum playlist_result
renameStoredPlaylist(const char *utf8from, const char *utf8to)
317 318
{
	struct stat st;
319 320
	char from[MPD_PATH_MAX];
	char to[MPD_PATH_MAX];
321

322 323 324
	if (!is_valid_playlist_name(utf8from) ||
	    !is_valid_playlist_name(utf8to))
		return PLAYLIST_RESULT_BAD_NAME;
325

326 327
	utf8_to_fs_playlist_path(from, utf8from);
	utf8_to_fs_playlist_path(to, utf8to);
328

329 330
	if (stat(from, &st) != 0)
		return PLAYLIST_RESULT_NO_SUCH_LIST;
331

332 333
	if (stat(to, &st) == 0)
		return PLAYLIST_RESULT_LIST_EXISTS;
334

335 336
	if (rename(from, to) < 0)
		return PLAYLIST_RESULT_ERRNO;
337

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