2007-05-16 14:02:10 +02:00
|
|
|
/* 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"
|
|
|
|
#include "log.h"
|
|
|
|
#include "path.h"
|
|
|
|
#include "utils.h"
|
|
|
|
#include "playlist.h"
|
|
|
|
#include "ack.h"
|
|
|
|
#include "command.h"
|
2007-05-26 16:09:09 +02:00
|
|
|
#include "ls.h"
|
|
|
|
#include "directory.h"
|
2008-01-03 08:29:49 +01:00
|
|
|
#include "os_compat.h"
|
2007-05-16 14:02:10 +02:00
|
|
|
|
2008-01-26 13:46:21 +01:00
|
|
|
static ListNode *nodeOfStoredPlaylist(List *list, int idx)
|
2007-05-16 14:02:10 +02:00
|
|
|
{
|
|
|
|
int forward;
|
|
|
|
ListNode *node;
|
|
|
|
int i;
|
|
|
|
|
2008-01-26 13:46:21 +01:00
|
|
|
if (idx >= list->numberOfNodes || idx < 0)
|
2007-05-16 14:02:10 +02:00
|
|
|
return NULL;
|
|
|
|
|
2008-01-26 13:46:21 +01:00
|
|
|
if (idx > (list->numberOfNodes/2)) {
|
2007-05-16 14:02:10 +02:00
|
|
|
forward = 0;
|
2008-01-01 11:09:36 +01:00
|
|
|
node = list->lastNode;
|
|
|
|
i = list->numberOfNodes - 1;
|
2007-05-16 14:02:10 +02:00
|
|
|
} else {
|
|
|
|
forward = 1;
|
2008-01-01 11:09:36 +01:00
|
|
|
node = list->firstNode;
|
2007-05-16 14:02:10 +02:00
|
|
|
i = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (node != NULL) {
|
2008-01-26 13:46:21 +01:00
|
|
|
if (i == idx)
|
2007-05-16 14:02:10 +02:00
|
|
|
return node;
|
|
|
|
|
|
|
|
if (forward) {
|
|
|
|
i++;
|
|
|
|
node = node->nextNode;
|
|
|
|
} else {
|
|
|
|
i--;
|
|
|
|
node = node->prevNode;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2008-01-01 11:09:36 +01:00
|
|
|
static int writeStoredPlaylistToPath(int fd, List *list, const char *fspath)
|
2008-01-01 11:09:31 +01:00
|
|
|
{
|
|
|
|
ListNode *node;
|
|
|
|
FILE *file;
|
|
|
|
char *s;
|
|
|
|
|
|
|
|
if (fspath == NULL)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
while (!(file = fopen(fspath, "w")) && errno == EINTR);
|
|
|
|
if (file == NULL) {
|
2008-01-01 11:09:36 +01:00
|
|
|
commandError(fd, ACK_ERROR_NO_EXIST, "could not open file "
|
2008-01-01 11:09:31 +01:00
|
|
|
"\"%s\": %s", fspath, strerror(errno));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2008-01-01 11:09:36 +01:00
|
|
|
node = list->firstNode;
|
2008-01-01 11:09:31 +01:00
|
|
|
while (node != NULL) {
|
|
|
|
char path_max_tmp[MPD_PATH_MAX];
|
|
|
|
|
|
|
|
s = utf8_to_fs_charset(path_max_tmp, (char *)node->data);
|
|
|
|
if (playlist_saveAbsolutePaths && !isValidRemoteUtf8Url(s))
|
|
|
|
s = rmp2amp_r(path_max_tmp, s);
|
|
|
|
fprintf(file, "%s\n", s);
|
|
|
|
node = node->nextNode;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (fclose(file) != 0 && errno == EINTR);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-01-01 11:09:36 +01:00
|
|
|
List *loadStoredPlaylist(int fd, const char *utf8path)
|
2007-05-16 14:02:10 +02:00
|
|
|
{
|
2008-01-01 11:09:36 +01:00
|
|
|
List *list;
|
2007-05-16 14:02:10 +02:00
|
|
|
FILE *file;
|
2008-01-01 11:09:26 +01:00
|
|
|
char buffer[MPD_PATH_MAX];
|
2007-12-28 03:56:25 +01:00
|
|
|
char path_max_tmp[MPD_PATH_MAX];
|
2008-01-01 11:09:26 +01:00
|
|
|
const size_t musicDir_len = strlen(musicDir);
|
2007-05-16 14:02:10 +02:00
|
|
|
|
2008-01-01 04:01:24 +01:00
|
|
|
if (!valid_playlist_name(fd, utf8path))
|
2008-01-01 11:09:36 +01:00
|
|
|
return NULL;
|
2007-05-16 14:02:10 +02:00
|
|
|
|
2008-01-01 04:01:24 +01:00
|
|
|
utf8_to_fs_playlist_path(path_max_tmp, utf8path);
|
|
|
|
while (!(file = fopen(path_max_tmp, "r")) && errno == EINTR);
|
2007-05-16 14:02:10 +02:00
|
|
|
if (file == NULL) {
|
2007-05-26 02:48:53 +02:00
|
|
|
commandError(fd, ACK_ERROR_NO_EXIST, "could not open file "
|
2008-01-01 04:01:24 +01:00
|
|
|
"\"%s\": %s", path_max_tmp, strerror(errno));
|
2008-01-01 11:09:36 +01:00
|
|
|
return NULL;
|
2007-05-16 14:02:10 +02:00
|
|
|
}
|
|
|
|
|
2008-01-01 11:09:36 +01:00
|
|
|
list = makeList(DEFAULT_FREE_DATA_FUNC, 0);
|
2007-05-16 14:02:10 +02:00
|
|
|
|
2008-01-01 11:09:26 +01:00
|
|
|
while (myFgets(buffer, sizeof(buffer), file)) {
|
|
|
|
char *s = buffer;
|
|
|
|
Song *song;
|
|
|
|
|
|
|
|
if (*s == PLAYLIST_COMMENT)
|
|
|
|
continue;
|
|
|
|
if (s[musicDir_len] == '/' &&
|
|
|
|
!strncmp(s, musicDir, musicDir_len))
|
|
|
|
memmove(s, s + musicDir_len + 1,
|
|
|
|
strlen(s + musicDir_len + 1) + 1);
|
2008-01-01 11:09:36 +01:00
|
|
|
if ((song = getSongFromDB(s))) {
|
|
|
|
get_song_url(path_max_tmp, song);
|
|
|
|
insertInListWithoutKey(list, xstrdup(path_max_tmp));
|
|
|
|
} else if (isValidRemoteUtf8Url(s))
|
|
|
|
insertInListWithoutKey(list, xstrdup(s));
|
2007-05-16 14:02:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
while (fclose(file) && errno == EINTR);
|
2008-01-01 11:09:36 +01:00
|
|
|
return list;
|
2007-05-16 14:02:10 +02:00
|
|
|
}
|
|
|
|
|
2008-01-01 11:09:36 +01:00
|
|
|
static int moveSongInStoredPlaylist(int fd, List *list, int src, int dest)
|
2007-05-16 14:02:10 +02:00
|
|
|
{
|
|
|
|
ListNode *srcNode, *destNode;
|
|
|
|
|
2008-01-01 11:09:36 +01:00
|
|
|
if (src >= list->numberOfNodes || dest >= list->numberOfNodes ||
|
|
|
|
src < 0 || dest < 0 || src == dest) {
|
2007-05-24 19:25:21 +02:00
|
|
|
commandError(fd, ACK_ERROR_ARG, "argument out of range");
|
2007-05-16 14:02:10 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2008-01-01 11:09:36 +01:00
|
|
|
srcNode = nodeOfStoredPlaylist(list, src);
|
2007-05-16 14:02:10 +02:00
|
|
|
if (!srcNode)
|
|
|
|
return -1;
|
|
|
|
|
2008-01-01 11:09:36 +01:00
|
|
|
destNode = nodeOfStoredPlaylist(list, dest);
|
2007-05-16 14:02:10 +02:00
|
|
|
|
|
|
|
/* remove src */
|
|
|
|
if (srcNode->prevNode)
|
|
|
|
srcNode->prevNode->nextNode = srcNode->nextNode;
|
|
|
|
else
|
2008-01-01 11:09:36 +01:00
|
|
|
list->firstNode = srcNode->nextNode;
|
2007-05-16 14:02:10 +02:00
|
|
|
|
|
|
|
if (srcNode->nextNode)
|
|
|
|
srcNode->nextNode->prevNode = srcNode->prevNode;
|
|
|
|
else
|
2008-01-01 11:09:36 +01:00
|
|
|
list->lastNode = srcNode->prevNode;
|
2007-05-16 14:02:10 +02:00
|
|
|
|
|
|
|
/* 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) {
|
2008-01-01 11:09:36 +01:00
|
|
|
list->firstNode->prevNode = srcNode;
|
|
|
|
srcNode->nextNode = list->firstNode;
|
2007-05-16 14:02:10 +02:00
|
|
|
srcNode->prevNode = NULL;
|
2008-01-01 11:09:36 +01:00
|
|
|
list->firstNode = srcNode;
|
|
|
|
} else if ((dest + 1) == list->numberOfNodes) {
|
|
|
|
list->lastNode->nextNode = srcNode;
|
2007-05-16 14:02:10 +02:00
|
|
|
srcNode->nextNode = NULL;
|
2008-01-01 11:09:36 +01:00
|
|
|
srcNode->prevNode = list->lastNode;
|
|
|
|
list->lastNode = srcNode;
|
2007-05-16 14:02:10 +02:00
|
|
|
} 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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-01-01 11:09:31 +01:00
|
|
|
int moveSongInStoredPlaylistByPath(int fd, const char *utf8path,
|
|
|
|
int src, int dest)
|
2007-05-16 14:02:10 +02:00
|
|
|
{
|
2008-01-01 11:09:36 +01:00
|
|
|
List *list;
|
2008-01-01 11:09:31 +01:00
|
|
|
|
2008-01-01 11:09:36 +01:00
|
|
|
if (!(list = loadStoredPlaylist(fd, utf8path))) {
|
2007-05-24 19:25:21 +02:00
|
|
|
commandError(fd, ACK_ERROR_UNKNOWN, "could not open playlist");
|
2007-05-16 14:02:10 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2008-01-01 11:09:36 +01:00
|
|
|
if (moveSongInStoredPlaylist(fd, list, src, dest) != 0) {
|
|
|
|
freeList(list);
|
2007-05-16 14:02:10 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2008-01-01 11:09:36 +01:00
|
|
|
if (writeStoredPlaylistToPath(fd, list, utf8path) != 0) {
|
2007-05-24 19:25:21 +02:00
|
|
|
commandError(fd, ACK_ERROR_UNKNOWN, "failed to save playlist");
|
2008-01-01 11:09:36 +01:00
|
|
|
freeList(list);
|
2007-05-16 14:02:10 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2008-01-01 11:09:36 +01:00
|
|
|
freeList(list);
|
2007-05-16 14:02:10 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int removeAllFromStoredPlaylistByPath(int fd, const char *utf8path)
|
|
|
|
{
|
2008-01-01 04:01:24 +01:00
|
|
|
char filename[MPD_PATH_MAX];
|
2007-05-16 14:02:10 +02:00
|
|
|
FILE *file;
|
|
|
|
|
2008-01-01 04:01:24 +01:00
|
|
|
if (!valid_playlist_name(fd, utf8path))
|
2007-05-16 14:02:10 +02:00
|
|
|
return -1;
|
2008-01-01 04:01:24 +01:00
|
|
|
utf8_to_fs_playlist_path(filename, utf8path);
|
2007-05-16 14:02:10 +02:00
|
|
|
|
|
|
|
while (!(file = fopen(filename, "w")) && errno == EINTR);
|
|
|
|
if (file == NULL) {
|
2007-05-25 22:25:11 +02:00
|
|
|
commandError(fd, ACK_ERROR_NO_EXIST, "could not open file "
|
|
|
|
"\"%s\": %s", filename, strerror(errno));
|
2007-05-16 14:02:10 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (fclose(file) != 0 && errno == EINTR);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-01-01 11:09:36 +01:00
|
|
|
static int removeOneSongFromStoredPlaylist(int fd, List *list, int pos)
|
2007-05-16 14:02:10 +02:00
|
|
|
{
|
2008-01-01 11:09:36 +01:00
|
|
|
ListNode *node = nodeOfStoredPlaylist(list, pos);
|
2007-05-16 14:02:10 +02:00
|
|
|
if (!node) {
|
2007-05-25 22:25:11 +02:00
|
|
|
commandError(fd, ACK_ERROR_ARG,
|
|
|
|
"could not find song at position");
|
2007-05-16 14:02:10 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2008-01-01 11:09:36 +01:00
|
|
|
deleteNodeFromList(list, node);
|
2007-05-16 14:02:10 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int removeOneSongFromStoredPlaylistByPath(int fd, const char *utf8path, int pos)
|
|
|
|
{
|
2008-01-01 11:09:36 +01:00
|
|
|
List *list;
|
2008-01-01 11:09:31 +01:00
|
|
|
|
2008-01-01 11:09:36 +01:00
|
|
|
if (!(list = loadStoredPlaylist(fd, utf8path))) {
|
2007-05-24 19:25:21 +02:00
|
|
|
commandError(fd, ACK_ERROR_UNKNOWN, "could not open playlist");
|
2007-05-16 14:02:10 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2008-01-01 11:09:36 +01:00
|
|
|
if (removeOneSongFromStoredPlaylist(fd, list, pos) != 0) {
|
|
|
|
freeList(list);
|
2007-05-16 14:02:10 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2008-01-01 11:09:36 +01:00
|
|
|
if (writeStoredPlaylistToPath(fd, list, utf8path) != 0) {
|
2007-05-24 19:25:21 +02:00
|
|
|
commandError(fd, ACK_ERROR_UNKNOWN, "failed to save playlist");
|
2008-01-01 11:09:36 +01:00
|
|
|
freeList(list);
|
2007-05-16 14:02:10 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2008-01-01 11:09:36 +01:00
|
|
|
freeList(list);
|
2007-05-16 14:02:10 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int appendSongToStoredPlaylistByPath(int fd, const char *utf8path, Song *song)
|
|
|
|
{
|
|
|
|
FILE *file;
|
2007-05-26 16:09:09 +02:00
|
|
|
char *s;
|
2007-12-28 03:56:25 +01:00
|
|
|
char path_max_tmp[MPD_PATH_MAX];
|
|
|
|
char path_max_tmp2[MPD_PATH_MAX];
|
2007-05-16 14:02:10 +02:00
|
|
|
|
2008-01-01 04:01:24 +01:00
|
|
|
if (!valid_playlist_name(fd, utf8path))
|
2007-05-16 14:02:10 +02:00
|
|
|
return -1;
|
2008-01-01 04:01:24 +01:00
|
|
|
utf8_to_fs_playlist_path(path_max_tmp, utf8path);
|
2007-05-16 14:02:10 +02:00
|
|
|
|
2008-01-01 04:01:24 +01:00
|
|
|
while (!(file = fopen(path_max_tmp, "a")) && errno == EINTR);
|
2007-05-16 14:02:10 +02:00
|
|
|
if (file == NULL) {
|
2007-05-25 22:25:11 +02:00
|
|
|
commandError(fd, ACK_ERROR_NO_EXIST, "could not open file "
|
2008-01-01 04:01:24 +01:00
|
|
|
"\"%s\": %s", path_max_tmp, strerror(errno));
|
2007-05-16 14:02:10 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2007-12-28 03:56:25 +01:00
|
|
|
s = utf8_to_fs_charset(path_max_tmp2, get_song_url(path_max_tmp, song));
|
|
|
|
|
2007-05-25 22:25:11 +02:00
|
|
|
if (playlist_saveAbsolutePaths && song->type == SONG_TYPE_FILE)
|
2007-12-28 03:56:25 +01:00
|
|
|
s = rmp2amp_r(path_max_tmp, s);
|
2007-05-26 16:09:09 +02:00
|
|
|
|
|
|
|
fprintf(file, "%s\n", s);
|
2007-05-16 14:02:10 +02:00
|
|
|
|
|
|
|
while (fclose(file) != 0 && errno == EINTR);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-05-24 20:07:19 +02:00
|
|
|
int renameStoredPlaylist(int fd, const char *utf8from, const char *utf8to)
|
|
|
|
{
|
|
|
|
struct stat st;
|
2008-01-01 04:01:24 +01:00
|
|
|
char from[MPD_PATH_MAX];
|
|
|
|
char to[MPD_PATH_MAX];
|
2007-05-24 20:07:19 +02:00
|
|
|
|
2008-01-01 04:01:24 +01:00
|
|
|
if (!valid_playlist_name(fd, utf8from) ||
|
|
|
|
!valid_playlist_name(fd, utf8to))
|
2007-05-24 20:07:19 +02:00
|
|
|
return -1;
|
|
|
|
|
2008-01-01 04:01:24 +01:00
|
|
|
utf8_to_fs_playlist_path(from, utf8from);
|
|
|
|
utf8_to_fs_playlist_path(to, utf8to);
|
2007-05-24 20:07:19 +02:00
|
|
|
|
|
|
|
if (stat(from, &st) != 0) {
|
2007-05-25 22:25:11 +02:00
|
|
|
commandError(fd, ACK_ERROR_NO_EXIST,
|
|
|
|
"no playlist named \"%s\"", utf8from);
|
2008-01-01 04:01:24 +01:00
|
|
|
return -1;
|
2007-05-24 20:07:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (stat(to, &st) == 0) {
|
2007-05-25 22:25:11 +02:00
|
|
|
commandError(fd, ACK_ERROR_EXIST, "a file or directory "
|
|
|
|
"already exists with the name \"%s\"", utf8to);
|
2008-01-01 04:01:24 +01:00
|
|
|
return -1;
|
2007-05-24 20:07:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (rename(from, to) < 0) {
|
2007-05-25 22:25:11 +02:00
|
|
|
commandError(fd, ACK_ERROR_UNKNOWN,
|
|
|
|
"could not rename playlist \"%s\" to \"%s\": %s",
|
|
|
|
utf8from, utf8to, strerror(errno));
|
2008-01-01 04:01:24 +01:00
|
|
|
return -1;
|
2007-05-24 20:07:19 +02:00
|
|
|
}
|
|
|
|
|
2008-01-01 04:01:24 +01:00
|
|
|
return 0;
|
2007-05-24 20:07:19 +02:00
|
|
|
}
|