Update to 6762
[qball-mpd.git] / src / storedPlaylist.c
blob322cb1b5b10f492d0605aa30af5f6ab66bfdba85
1 /* the Music Player Daemon (MPD)
2 * Copyright (C) 2007 by Warren Dukes (warren.dukes@gmail.com)
3 * This project's homepage is: http://www.musicpd.org
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 #include "storedPlaylist.h"
20 #include "log.h"
21 #include "path.h"
22 #include "utils.h"
23 #include "playlist.h"
24 #include "ack.h"
25 #include "command.h"
26 #include "ls.h"
27 #include "directory.h"
29 #include <string.h>
30 #include <errno.h>
32 static char *utf8pathToFsPathInStoredPlaylist(const char *utf8path, int fd)
34 char *file;
35 char *rfile;
36 char *actualFile;
38 if (strstr(utf8path, "/")) {
39 commandError(fd, ACK_ERROR_ARG, "playlist name \"%s\" is "
40 "invalid: playlist names may not contain slashes",
41 utf8path);
42 return NULL;
45 file = utf8ToFsCharset((char *)utf8path);
47 rfile = xmalloc(strlen(file) + strlen(".") +
48 strlen(PLAYLIST_FILE_SUFFIX) + 1);
50 strcpy(rfile, file);
51 strcat(rfile, ".");
52 strcat(rfile, PLAYLIST_FILE_SUFFIX);
54 actualFile = rpp2app(rfile);
56 free(rfile);
58 return actualFile;
61 static unsigned int lengthOfStoredPlaylist(StoredPlaylist *sp)
63 return sp->list->numberOfNodes;
66 static ListNode *nodeOfStoredPlaylist(StoredPlaylist *sp, int index)
68 int forward;
69 ListNode *node;
70 int i;
72 if (index >= lengthOfStoredPlaylist(sp) || index < 0)
73 return NULL;
75 if (index > lengthOfStoredPlaylist(sp)/2) {
76 forward = 0;
77 node = sp->list->lastNode;
78 i = lengthOfStoredPlaylist(sp) - 1;
79 } else {
80 forward = 1;
81 node = sp->list->firstNode;
82 i = 0;
85 while (node != NULL) {
86 if (i == index)
87 return node;
89 if (forward) {
90 i++;
91 node = node->nextNode;
92 } else {
93 i--;
94 node = node->prevNode;
98 return NULL;
101 static void appendSongToStoredPlaylist(StoredPlaylist *sp, Song *song)
103 insertInListWithoutKey(sp->list, xstrdup(getSongUrl(song)));
106 StoredPlaylist *newStoredPlaylist(const char *utf8name, int fd, int ignoreExisting)
108 struct stat buf;
109 char *filename = NULL;
110 StoredPlaylist *sp = calloc(1, sizeof(*sp));
111 if (!sp)
112 return NULL;
114 if (utf8name) {
115 filename = utf8pathToFsPathInStoredPlaylist(utf8name, fd);
117 if (filename && stat(filename, &buf) == 0 &&
118 ignoreExisting == 0) {
119 commandError(fd, ACK_ERROR_EXIST,
120 "a file or directory already exists with "
121 "the name \"%s\"", utf8name);
122 free(sp);
123 return NULL;
127 sp->list = makeList(DEFAULT_FREE_DATA_FUNC, 0);
128 sp->fd = fd;
130 if (filename)
131 sp->fspath = xstrdup(filename);
133 return sp;
136 StoredPlaylist *loadStoredPlaylist(const char *utf8path, int fd)
138 char *filename;
139 StoredPlaylist *sp;
140 FILE *file;
141 char s[MAXPATHLEN + 1];
142 int slength = 0;
143 char *temp = utf8ToFsCharset((char *)utf8path);
144 char *parent = parentPath(temp);
145 int parentlen = strlen(parent);
146 int tempInt;
147 int commentCharFound = 0;
148 Song *song;
150 filename = utf8pathToFsPathInStoredPlaylist(utf8path, fd);
151 if (!filename)
152 return NULL;
154 while (!(file = fopen(filename, "r")) && errno == EINTR);
155 if (file == NULL) {
156 commandError(fd, ACK_ERROR_NO_EXIST, "could not open file "
157 "\"%s\": %s", filename, strerror(errno));
158 return NULL;
161 sp = newStoredPlaylist(utf8path, fd, 1);
162 if (!sp)
163 goto out;
165 while ((tempInt = fgetc(file)) != EOF) {
166 s[slength] = tempInt;
167 if (s[slength] == '\n' || s[slength] == '\0') {
168 commentCharFound = 0;
169 s[slength] = '\0';
170 if (s[0] == PLAYLIST_COMMENT)
171 commentCharFound = 1;
172 if (strncmp(s, musicDir, strlen(musicDir)) == 0) {
173 strcpy(s, &(s[strlen(musicDir)]));
174 } else if (parentlen) {
175 temp = xstrdup(s);
176 memset(s, 0, MAXPATHLEN + 1);
177 strcpy(s, parent);
178 strncat(s, "/", MAXPATHLEN - parentlen);
179 strncat(s, temp, MAXPATHLEN - parentlen - 1);
180 if (strlen(s) >= MAXPATHLEN) {
181 commandError(sp->fd,
182 ACK_ERROR_PLAYLIST_LOAD,
183 "\"%s\" is too long", temp);
184 free(temp);
185 freeStoredPlaylist(sp);
186 sp = NULL;
187 goto out;
189 free(temp);
191 slength = 0;
192 temp = fsCharsetToUtf8(s);
193 if (temp && !commentCharFound) {
194 song = getSongFromDB(temp);
195 if (song) {
196 appendSongToStoredPlaylist(sp, song);
197 continue;
200 if (!isValidRemoteUtf8Url(temp))
201 continue;
203 song = newSong(temp, SONG_TYPE_URL, NULL);
204 if (song) {
205 appendSongToStoredPlaylist(sp, song);
206 freeJustSong(song);
209 } else if (slength == MAXPATHLEN) {
210 s[slength] = '\0';
211 commandError(sp->fd, ACK_ERROR_PLAYLIST_LOAD,
212 "line \"%s\" in playlist \"%s\" "
213 "is too long", s, utf8path);
214 freeStoredPlaylist(sp);
215 sp = NULL;
216 goto out;
217 } else if (s[slength] != '\r') {
218 slength++;
222 out:
223 while (fclose(file) && errno == EINTR);
224 return sp;
227 void freeStoredPlaylist(StoredPlaylist *sp)
229 if (sp->list)
230 freeList(sp->list);
231 if (sp->fspath)
232 free(sp->fspath);
234 free(sp);
237 static int moveSongInStoredPlaylist(int fd, StoredPlaylist *sp, int src, int dest)
239 ListNode *srcNode, *destNode;
241 if (src >= lengthOfStoredPlaylist(sp) || dest >= lengthOfStoredPlaylist(sp) || src < 0 || dest < 0 || src == dest) {
242 commandError(fd, ACK_ERROR_ARG, "argument out of range");
243 return -1;
246 srcNode = nodeOfStoredPlaylist(sp, src);
247 if (!srcNode)
248 return -1;
250 destNode = nodeOfStoredPlaylist(sp, dest);
252 /* remove src */
253 if (srcNode->prevNode)
254 srcNode->prevNode->nextNode = srcNode->nextNode;
255 else
256 sp->list->firstNode = srcNode->nextNode;
258 if (srcNode->nextNode)
259 srcNode->nextNode->prevNode = srcNode->prevNode;
260 else
261 sp->list->lastNode = srcNode->prevNode;
263 /* this is all a bit complicated - but I tried to
264 * maintain the same order stuff is moved as in the
265 * real playlist */
266 if (dest == 0) {
267 sp->list->firstNode->prevNode = srcNode;
268 srcNode->nextNode = sp->list->firstNode;
269 srcNode->prevNode = NULL;
270 sp->list->firstNode = srcNode;
271 } else if ((dest + 1) == lengthOfStoredPlaylist(sp)) {
272 sp->list->lastNode->nextNode = srcNode;
273 srcNode->nextNode = NULL;
274 srcNode->prevNode = sp->list->lastNode;
275 sp->list->lastNode = srcNode;
276 } else {
277 if (destNode == NULL) {
278 /* this shouldn't be happening. */
279 return -1;
282 if (src > dest) {
283 destNode->prevNode->nextNode = srcNode;
284 srcNode->prevNode = destNode->prevNode;
285 srcNode->nextNode = destNode;
286 destNode->prevNode = srcNode;
287 } else {
288 destNode->nextNode->prevNode = srcNode;
289 srcNode->prevNode = destNode;
290 srcNode->nextNode = destNode->nextNode;
291 destNode->nextNode = srcNode;
295 return 0;
298 int moveSongInStoredPlaylistByPath(int fd, const char *utf8path, int src, int dest)
300 StoredPlaylist *sp = loadStoredPlaylist(utf8path, fd);
301 if (!sp) {
302 commandError(fd, ACK_ERROR_UNKNOWN, "could not open playlist");
303 return -1;
306 if (moveSongInStoredPlaylist(fd, sp, src, dest) != 0) {
307 freeStoredPlaylist(sp);
308 return -1;
311 if (writeStoredPlaylist(sp) != 0) {
312 commandError(fd, ACK_ERROR_UNKNOWN, "failed to save playlist");
313 freeStoredPlaylist(sp);
314 return -1;
317 freeStoredPlaylist(sp);
318 return 0;
321 /* Not used currently
322 static void removeAllFromStoredPlaylist(StoredPlaylist *sp)
324 freeList(sp->list);
325 sp->list = makeList(DEFAULT_FREE_DATA_FUNC, 0);
329 int removeAllFromStoredPlaylistByPath(int fd, const char *utf8path)
331 char *filename;
332 FILE *file;
334 filename = utf8pathToFsPathInStoredPlaylist(utf8path, fd);
335 if (!filename)
336 return -1;
338 while (!(file = fopen(filename, "w")) && errno == EINTR);
339 if (file == NULL) {
340 commandError(fd, ACK_ERROR_NO_EXIST, "could not open file "
341 "\"%s\": %s", filename, strerror(errno));
342 return -1;
345 while (fclose(file) != 0 && errno == EINTR);
346 return 0;
349 static int removeOneSongFromStoredPlaylist(int fd, StoredPlaylist *sp, int pos)
351 ListNode *node = nodeOfStoredPlaylist(sp, pos);
352 if (!node) {
353 commandError(fd, ACK_ERROR_ARG,
354 "could not find song at position");
355 return -1;
358 deleteNodeFromList(sp->list, node);
360 return 0;
363 int removeOneSongFromStoredPlaylistByPath(int fd, const char *utf8path, int pos)
365 StoredPlaylist *sp = loadStoredPlaylist(utf8path, fd);
366 if (!sp) {
367 commandError(fd, ACK_ERROR_UNKNOWN, "could not open playlist");
368 return -1;
371 if (removeOneSongFromStoredPlaylist(fd, sp, pos) != 0) {
372 freeStoredPlaylist(sp);
373 return -1;
376 if (writeStoredPlaylist(sp) != 0) {
377 commandError(fd, ACK_ERROR_UNKNOWN, "failed to save playlist");
378 freeStoredPlaylist(sp);
379 return -1;
382 freeStoredPlaylist(sp);
383 return 0;
386 static int writeStoredPlaylistToPath(StoredPlaylist *sp, const char *fspath)
388 ListNode *node;
389 FILE *file;
390 char *s;
392 if (fspath == NULL)
393 return -1;
395 while (!(file = fopen(fspath, "w")) && errno == EINTR);
396 if (file == NULL) {
397 commandError(sp->fd, ACK_ERROR_NO_EXIST, "could not open file "
398 "\"%s\": %s", fspath, strerror(errno));
399 return -1;
402 node = sp->list->firstNode;
403 while (node != NULL) {
404 s = (char *)node->data;
405 if (isValidRemoteUtf8Url(s) || !playlist_saveAbsolutePaths)
406 s = utf8ToFsCharset(s);
407 else
408 s = rmp2amp(utf8ToFsCharset(s));
409 fprintf(file, "%s\n", s);
410 node = node->nextNode;
413 while (fclose(file) != 0 && errno == EINTR);
414 return 0;
417 int writeStoredPlaylist(StoredPlaylist *sp)
419 return writeStoredPlaylistToPath(sp, sp->fspath);
422 int appendSongToStoredPlaylistByPath(int fd, const char *utf8path, Song *song)
424 char *filename;
425 FILE *file;
426 char *s;
428 filename = utf8pathToFsPathInStoredPlaylist(utf8path, fd);
429 if (!filename)
430 return -1;
432 while (!(file = fopen(filename, "a")) && errno == EINTR);
433 if (file == NULL) {
434 commandError(fd, ACK_ERROR_NO_EXIST, "could not open file "
435 "\"%s\": %s", filename, strerror(errno));
436 return -1;
439 if (playlist_saveAbsolutePaths && song->type == SONG_TYPE_FILE)
440 s = rmp2amp(utf8ToFsCharset(getSongUrl(song)));
441 else
442 s = utf8ToFsCharset(getSongUrl(song));
444 fprintf(file, "%s\n", s);
446 while (fclose(file) != 0 && errno == EINTR);
447 return 0;
450 void appendPlaylistToStoredPlaylist(StoredPlaylist *sp, Playlist *playlist)
452 int i;
453 for (i = 0; i < playlist->length; i++)
454 appendSongToStoredPlaylist(sp, playlist->songs[i]);
457 int renameStoredPlaylist(int fd, const char *utf8from, const char *utf8to)
459 struct stat st;
460 char *from;
461 char *to;
462 int ret = 0;
464 from = xstrdup(utf8pathToFsPathInStoredPlaylist(utf8from, fd));
465 if (!from)
466 return -1;
468 to = xstrdup(utf8pathToFsPathInStoredPlaylist(utf8to, fd));
469 if (!to) {
470 free(from);
471 return -1;
474 if (stat(from, &st) != 0) {
475 commandError(fd, ACK_ERROR_NO_EXIST,
476 "no playlist named \"%s\"", utf8from);
477 ret = -1;
478 goto out;
481 if (stat(to, &st) == 0) {
482 commandError(fd, ACK_ERROR_EXIST, "a file or directory "
483 "already exists with the name \"%s\"", utf8to);
484 ret = -1;
485 goto out;
488 if (rename(from, to) < 0) {
489 commandError(fd, ACK_ERROR_UNKNOWN,
490 "could not rename playlist \"%s\" to \"%s\": %s",
491 utf8from, utf8to, strerror(errno));
492 ret = -1;
493 goto out;
496 out:
497 free(from);
498 free(to);
500 return ret;