Commit 09936358 authored by Eric Wong's avatar Eric Wong

storedPlaylist: cleanup API and avoid needless heap allocations + error checks

git-svn-id: https://svn.musicpd.org/mpd/trunk@7119 09075e82-0dd4-0310-85a5-a0d7c8717e4f
parent 390ed297
...@@ -1516,11 +1516,12 @@ int getPlaylistSongId(int song) ...@@ -1516,11 +1516,12 @@ int getPlaylistSongId(int song)
int PlaylistInfo(int fd, char *utf8file, int detail) int PlaylistInfo(int fd, char *utf8file, int detail)
{ {
ListNode *node; ListNode *node;
StoredPlaylist *sp = loadStoredPlaylist(utf8file, fd); StoredPlaylist sp;
if (sp == NULL)
if (loadStoredPlaylist(fd, &sp, utf8file) < 0)
return -1; return -1;
node = sp->list->firstNode; node = sp.list->firstNode;
while (node != NULL) { while (node != NULL) {
char *temp = node->data; char *temp = node->data;
int wrote = 0; int wrote = 0;
...@@ -1540,18 +1541,19 @@ int PlaylistInfo(int fd, char *utf8file, int detail) ...@@ -1540,18 +1541,19 @@ int PlaylistInfo(int fd, char *utf8file, int detail)
node = node->nextNode; node = node->nextNode;
} }
freeStoredPlaylist(sp); freeStoredPlaylist(&sp);
return 0; return 0;
} }
int loadPlaylist(int fd, char *utf8file) int loadPlaylist(int fd, char *utf8file)
{ {
ListNode *node; ListNode *node;
StoredPlaylist *sp = loadStoredPlaylist(utf8file, fd); StoredPlaylist sp;
if (sp == NULL)
if (loadStoredPlaylist(fd, &sp, utf8file) < 0)
return -1; return -1;
node = sp->list->firstNode; node = sp.list->firstNode;
while (node != NULL) { while (node != NULL) {
char *temp = node->data; char *temp = node->data;
if ((addToPlaylist(STDERR_FILENO, temp, 0)) < 0) { if ((addToPlaylist(STDERR_FILENO, temp, 0)) < 0) {
...@@ -1573,7 +1575,7 @@ int loadPlaylist(int fd, char *utf8file) ...@@ -1573,7 +1575,7 @@ int loadPlaylist(int fd, char *utf8file)
node = node->nextNode; node = node->nextNode;
} }
freeStoredPlaylist(sp); freeStoredPlaylist(&sp);
return 0; return 0;
} }
......
...@@ -69,6 +69,37 @@ static ListNode *nodeOfStoredPlaylist(StoredPlaylist *sp, int index) ...@@ -69,6 +69,37 @@ static ListNode *nodeOfStoredPlaylist(StoredPlaylist *sp, int index)
return NULL; return NULL;
} }
static int writeStoredPlaylistToPath(StoredPlaylist *sp, const char *fspath)
{
ListNode *node;
FILE *file;
char *s;
if (fspath == NULL)
return -1;
while (!(file = fopen(fspath, "w")) && errno == EINTR);
if (file == NULL) {
commandError(sp->fd, ACK_ERROR_NO_EXIST, "could not open file "
"\"%s\": %s", fspath, strerror(errno));
return -1;
}
node = sp->list->firstNode;
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;
}
static void appendSongToStoredPlaylist(StoredPlaylist *sp, Song *song) static void appendSongToStoredPlaylist(StoredPlaylist *sp, Song *song)
{ {
char path_max_tmp[MPD_PATH_MAX]; char path_max_tmp[MPD_PATH_MAX];
...@@ -77,55 +108,32 @@ static void appendSongToStoredPlaylist(StoredPlaylist *sp, Song *song) ...@@ -77,55 +108,32 @@ static void appendSongToStoredPlaylist(StoredPlaylist *sp, Song *song)
insertInListWithoutKey(sp->list, xstrdup(path_max_tmp)); insertInListWithoutKey(sp->list, xstrdup(path_max_tmp));
} }
StoredPlaylist *newStoredPlaylist(const char *utf8name, int fd, int ignoreExisting) static void initStoredPlaylist(int fd, StoredPlaylist *sp, const char *utf8name)
{ {
struct stat buf; utf8_to_fs_playlist_path(sp->fs_path, utf8name);
char filename[MPD_PATH_MAX];
StoredPlaylist *sp;
if (!valid_playlist_name(fd, utf8name))
return NULL;
utf8_to_fs_playlist_path(filename, utf8name);
if (stat(filename, &buf) == 0 && ! ignoreExisting) {
commandError(fd, ACK_ERROR_EXIST,
"a file or directory already exists with "
"the name \"%s\"", utf8name);
return NULL;
}
if (!(sp = malloc(sizeof(*sp))))
return NULL;
sp->list = makeList(DEFAULT_FREE_DATA_FUNC, 0); sp->list = makeList(DEFAULT_FREE_DATA_FUNC, 0);
sp->fd = fd; sp->fd = fd;
sp->fspath = xstrdup(filename);
return sp;
} }
StoredPlaylist *loadStoredPlaylist(const char *utf8path, int fd) int loadStoredPlaylist(int fd, StoredPlaylist *sp, const char *utf8path)
{ {
StoredPlaylist *sp;
FILE *file; FILE *file;
char buffer[MPD_PATH_MAX]; char buffer[MPD_PATH_MAX];
char path_max_tmp[MPD_PATH_MAX]; char path_max_tmp[MPD_PATH_MAX];
const size_t musicDir_len = strlen(musicDir); const size_t musicDir_len = strlen(musicDir);
if (!valid_playlist_name(fd, utf8path)) if (!valid_playlist_name(fd, utf8path))
return NULL; return -1;
utf8_to_fs_playlist_path(path_max_tmp, utf8path); utf8_to_fs_playlist_path(path_max_tmp, utf8path);
while (!(file = fopen(path_max_tmp, "r")) && errno == EINTR); while (!(file = fopen(path_max_tmp, "r")) && errno == EINTR);
if (file == NULL) { if (file == NULL) {
commandError(fd, ACK_ERROR_NO_EXIST, "could not open file " commandError(fd, ACK_ERROR_NO_EXIST, "could not open file "
"\"%s\": %s", path_max_tmp, strerror(errno)); "\"%s\": %s", path_max_tmp, strerror(errno));
return NULL; return -1;
} }
sp = newStoredPlaylist(utf8path, fd, 1); initStoredPlaylist(fd, sp, utf8path);
if (!sp)
goto out;
while (myFgets(buffer, sizeof(buffer), file)) { while (myFgets(buffer, sizeof(buffer), file)) {
char *s = buffer; char *s = buffer;
...@@ -143,19 +151,14 @@ StoredPlaylist *loadStoredPlaylist(const char *utf8path, int fd) ...@@ -143,19 +151,14 @@ StoredPlaylist *loadStoredPlaylist(const char *utf8path, int fd)
insertInListWithoutKey(sp->list, xstrdup(s)); insertInListWithoutKey(sp->list, xstrdup(s));
} }
out:
while (fclose(file) && errno == EINTR); while (fclose(file) && errno == EINTR);
return sp; return 0;
} }
void freeStoredPlaylist(StoredPlaylist *sp) void freeStoredPlaylist(StoredPlaylist *sp)
{ {
if (sp->list) if (sp->list)
freeList(sp->list); freeList(sp->list);
if (sp->fspath)
free(sp->fspath);
free(sp);
} }
static int moveSongInStoredPlaylist(int fd, StoredPlaylist *sp, int src, int dest) static int moveSongInStoredPlaylist(int fd, StoredPlaylist *sp, int src, int dest)
...@@ -219,26 +222,28 @@ static int moveSongInStoredPlaylist(int fd, StoredPlaylist *sp, int src, int des ...@@ -219,26 +222,28 @@ static int moveSongInStoredPlaylist(int fd, StoredPlaylist *sp, int src, int des
return 0; return 0;
} }
int moveSongInStoredPlaylistByPath(int fd, const char *utf8path, int src, int dest) int moveSongInStoredPlaylistByPath(int fd, const char *utf8path,
int src, int dest)
{ {
StoredPlaylist *sp = loadStoredPlaylist(utf8path, fd); StoredPlaylist sp;
if (!sp) {
if (loadStoredPlaylist(fd, &sp, utf8path) < 0) {
commandError(fd, ACK_ERROR_UNKNOWN, "could not open playlist"); commandError(fd, ACK_ERROR_UNKNOWN, "could not open playlist");
return -1; return -1;
} }
if (moveSongInStoredPlaylist(fd, sp, src, dest) != 0) { if (moveSongInStoredPlaylist(fd, &sp, src, dest) != 0) {
freeStoredPlaylist(sp); freeStoredPlaylist(&sp);
return -1; return -1;
} }
if (writeStoredPlaylist(sp) != 0) { if (writeStoredPlaylistToPath(&sp, utf8path) != 0) {
commandError(fd, ACK_ERROR_UNKNOWN, "failed to save playlist"); commandError(fd, ACK_ERROR_UNKNOWN, "failed to save playlist");
freeStoredPlaylist(sp); freeStoredPlaylist(&sp);
return -1; return -1;
} }
freeStoredPlaylist(sp); freeStoredPlaylist(&sp);
return 0; return 0;
} }
...@@ -278,63 +283,28 @@ static int removeOneSongFromStoredPlaylist(int fd, StoredPlaylist *sp, int pos) ...@@ -278,63 +283,28 @@ static int removeOneSongFromStoredPlaylist(int fd, StoredPlaylist *sp, int pos)
int removeOneSongFromStoredPlaylistByPath(int fd, const char *utf8path, int pos) int removeOneSongFromStoredPlaylistByPath(int fd, const char *utf8path, int pos)
{ {
StoredPlaylist *sp = loadStoredPlaylist(utf8path, fd); StoredPlaylist sp;
if (!sp) {
if (loadStoredPlaylist(fd, &sp, utf8path) < 0) {
commandError(fd, ACK_ERROR_UNKNOWN, "could not open playlist"); commandError(fd, ACK_ERROR_UNKNOWN, "could not open playlist");
return -1; return -1;
} }
if (removeOneSongFromStoredPlaylist(fd, sp, pos) != 0) { if (removeOneSongFromStoredPlaylist(fd, &sp, pos) != 0) {
freeStoredPlaylist(sp); freeStoredPlaylist(&sp);
return -1; return -1;
} }
if (writeStoredPlaylist(sp) != 0) { if (writeStoredPlaylistToPath(&sp, utf8path) != 0) {
commandError(fd, ACK_ERROR_UNKNOWN, "failed to save playlist"); commandError(fd, ACK_ERROR_UNKNOWN, "failed to save playlist");
freeStoredPlaylist(sp); freeStoredPlaylist(&sp);
return -1; return -1;
} }
freeStoredPlaylist(sp); freeStoredPlaylist(&sp);
return 0; return 0;
} }
static int writeStoredPlaylistToPath(StoredPlaylist *sp, const char *fspath)
{
ListNode *node;
FILE *file;
char *s;
if (fspath == NULL)
return -1;
while (!(file = fopen(fspath, "w")) && errno == EINTR);
if (file == NULL) {
commandError(sp->fd, ACK_ERROR_NO_EXIST, "could not open file "
"\"%s\": %s", fspath, strerror(errno));
return -1;
}
node = sp->list->firstNode;
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;
}
int writeStoredPlaylist(StoredPlaylist *sp)
{
return writeStoredPlaylistToPath(sp, sp->fspath);
}
int appendSongToStoredPlaylistByPath(int fd, const char *utf8path, Song *song) int appendSongToStoredPlaylistByPath(int fd, const char *utf8path, Song *song)
{ {
FILE *file; FILE *file;
...@@ -364,13 +334,6 @@ int appendSongToStoredPlaylistByPath(int fd, const char *utf8path, Song *song) ...@@ -364,13 +334,6 @@ int appendSongToStoredPlaylistByPath(int fd, const char *utf8path, Song *song)
return 0; return 0;
} }
void appendPlaylistToStoredPlaylist(StoredPlaylist *sp, Playlist *playlist)
{
int i;
for (i = 0; i < playlist->length; i++)
appendSongToStoredPlaylist(sp, playlist->songs[i]);
}
int renameStoredPlaylist(int fd, const char *utf8from, const char *utf8to) int renameStoredPlaylist(int fd, const char *utf8from, const char *utf8to)
{ {
struct stat st; struct stat st;
......
...@@ -22,26 +22,22 @@ ...@@ -22,26 +22,22 @@
#include "song.h" #include "song.h"
#include "list.h" #include "list.h"
#include "playlist.h" #include "playlist.h"
#include "path.h"
typedef struct _storedPlaylist { typedef struct _storedPlaylist {
List *list; List *list;
unsigned int length; char fs_path[MPD_PATH_MAX];
char *fspath;
int fd; int fd;
} StoredPlaylist; } StoredPlaylist;
StoredPlaylist *newStoredPlaylist(const char *filename, int fd, int ignoreExisting); int loadStoredPlaylist(int fd, StoredPlaylist *sp, const char *utf8path);
StoredPlaylist *loadStoredPlaylist(const char *utf8path, int fd);
void freeStoredPlaylist(StoredPlaylist *sp); void freeStoredPlaylist(StoredPlaylist *sp);
int moveSongInStoredPlaylistByPath(int fd, const char *utf8path, int src, int dest); int moveSongInStoredPlaylistByPath(int fd, const char *utf8path, int src, int dest);
int removeAllFromStoredPlaylistByPath(int fd, const char *utf8path); int removeAllFromStoredPlaylistByPath(int fd, const char *utf8path);
int removeOneSongFromStoredPlaylistByPath(int fd, const char *utf8path, int pos); int removeOneSongFromStoredPlaylistByPath(int fd, const char *utf8path, int pos);
int writeStoredPlaylist(StoredPlaylist *sp);
int appendSongToStoredPlaylistByPath(int fd, const char *utf8path, Song *song); int appendSongToStoredPlaylistByPath(int fd, const char *utf8path, Song *song);
void appendPlaylistToStoredPlaylist(StoredPlaylist *sp, Playlist *playlist);
int renameStoredPlaylist(int fd, const char *utf8from, const char *utf8to); int renameStoredPlaylist(int fd, const char *utf8from, const char *utf8to);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment