Revert r7111 and begin to properly fix storedPlaylist code

git-svn-id: https://svn.musicpd.org/mpd/trunk@7112 09075e82-0dd4-0310-85a5-a0d7c8717e4f
This commit is contained in:
Eric Wong 2008-01-01 03:01:24 +00:00
parent c75d33752a
commit 6fbbc642dd
2 changed files with 73 additions and 90 deletions

View File

@ -29,33 +29,40 @@
#include <string.h> #include <string.h>
#include <errno.h> #include <errno.h>
static char *utf8pathToFsPathInStoredPlaylist(const char *utf8path, int fd) /*
* Not supporting '/' was done out of laziness, and we should really
* strive to support it in the future.
*
* Not supporting '\r' and '\n' is done out of protocol limitations (and
* arguably laziness), but bending over head over heels to modify the
* protocol (and compatibility with all clients) to support idiots who
* put '\r' and '\n' in filenames isn't going to happen, either.
*/
static int valid_playlist_name(int err_fd, const char *utf8path)
{ {
char *file; if (strchr(utf8path, '/') ||
char *rfile; strchr(utf8path, '\n') ||
char *actualFile; strchr(utf8path, '\r')) {
static char path_max_tmp[MPD_PATH_MAX]; /* should be MT-safe */ commandError(err_fd, ACK_ERROR_ARG, "playlist name \"%s\" is "
"invalid: playlist names may not contain slashes,"
if (strstr(utf8path, "/")) { " newlines or carriage returns",
commandError(fd, ACK_ERROR_ARG, "playlist name \"%s\" is "
"invalid: playlist names may not contain slashes",
utf8path); utf8path);
return NULL; return 0;
} }
return 1;
}
file = utf8_to_fs_charset(path_max_tmp, (char *)utf8path); /*
rfile = xmalloc(strlen(file) + strlen(".") + * converts a path passed from a client into an absolute, FS path
strlen(PLAYLIST_FILE_SUFFIX) + 1); * paths passed by clients do NOT have file suffixes in them
*/
static void utf8_to_fs_playlist_path(char *path_max_tmp, const char *utf8path)
{
utf8_to_fs_charset(path_max_tmp, (char *)utf8path);
rpp2app_r(path_max_tmp, path_max_tmp);
strncat(path_max_tmp, "." PLAYLIST_FILE_SUFFIX, MPD_PATH_MAX - 1);
strcpy(rfile, file); return path_max_tmp;
strcat(rfile, ".");
strcat(rfile, PLAYLIST_FILE_SUFFIX);
actualFile = rpp2app_r(path_max_tmp, rfile);
free(rfile);
return actualFile;
} }
static unsigned int lengthOfStoredPlaylist(StoredPlaylist *sp) static unsigned int lengthOfStoredPlaylist(StoredPlaylist *sp)
@ -109,36 +116,33 @@ static void appendSongToStoredPlaylist(StoredPlaylist *sp, Song *song)
StoredPlaylist *newStoredPlaylist(const char *utf8name, int fd, int ignoreExisting) StoredPlaylist *newStoredPlaylist(const char *utf8name, int fd, int ignoreExisting)
{ {
struct stat buf; struct stat buf;
char *filename = NULL; char filename[MPD_PATH_MAX];
StoredPlaylist *sp = calloc(1, sizeof(*sp)); StoredPlaylist *sp;
if (!sp)
if (!valid_playlist_name(fd, utf8name))
return NULL; return NULL;
if (utf8name) { utf8_to_fs_playlist_path(filename, utf8name);
filename = utf8pathToFsPathInStoredPlaylist(utf8name, fd);
if (filename && stat(filename, &buf) == 0 && if (stat(filename, &buf) == 0 && ! ignoreExisting) {
ignoreExisting == 0) { commandError(fd, ACK_ERROR_EXIST,
commandError(fd, ACK_ERROR_EXIST, "a file or directory already exists with "
"a file or directory already exists with " "the name \"%s\"", utf8name);
"the name \"%s\"", utf8name); return NULL;
free(sp);
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);
if (filename)
sp->fspath = xstrdup(filename);
return sp; return sp;
} }
/* FIXME - this function is gross */
StoredPlaylist *loadStoredPlaylist(const char *utf8path, int fd) StoredPlaylist *loadStoredPlaylist(const char *utf8path, int fd)
{ {
char *filename;
StoredPlaylist *sp; StoredPlaylist *sp;
FILE *file; FILE *file;
char s[MPD_PATH_MAX]; char s[MPD_PATH_MAX];
@ -146,21 +150,22 @@ StoredPlaylist *loadStoredPlaylist(const char *utf8path, int fd)
char path_max_tmp2[MPD_PATH_MAX]; /* TODO: cleanup */ char path_max_tmp2[MPD_PATH_MAX]; /* TODO: cleanup */
char path_max_tmp3[MPD_PATH_MAX]; /* TODO: cleanup */ char path_max_tmp3[MPD_PATH_MAX]; /* TODO: cleanup */
int slength = 0; int slength = 0;
char *temp = utf8_to_fs_charset(path_max_tmp2, (char *)utf8path); char *temp;
char *parent = parent_path(path_max_tmp3, temp); char *parent;
int parentlen = strlen(parent); int parentlen;
int tempInt; int tempInt;
int commentCharFound = 0; int commentCharFound = 0;
Song *song; Song *song;
filename = utf8pathToFsPathInStoredPlaylist(utf8path, fd); if (!valid_playlist_name(fd, utf8path))
if (!filename)
return NULL; return NULL;
while (!(file = fopen(filename, "r")) && errno == EINTR); utf8_to_fs_playlist_path(path_max_tmp, utf8path);
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", filename, strerror(errno)); "\"%s\": %s", path_max_tmp, strerror(errno));
return NULL; return NULL;
} }
@ -168,6 +173,10 @@ StoredPlaylist *loadStoredPlaylist(const char *utf8path, int fd)
if (!sp) if (!sp)
goto out; goto out;
temp = utf8_to_fs_charset(path_max_tmp2, (char *)utf8path);
parent = parent_path(path_max_tmp3, temp);
parentlen = strlen(parent);
while ((tempInt = fgetc(file)) != EOF) { while ((tempInt = fgetc(file)) != EOF) {
s[slength] = tempInt; s[slength] = tempInt;
if (s[slength] == '\n' || s[slength] == '\0') { if (s[slength] == '\n' || s[slength] == '\0') {
@ -179,10 +188,8 @@ StoredPlaylist *loadStoredPlaylist(const char *utf8path, int fd)
s[strlen(musicDir)] == '/') { s[strlen(musicDir)] == '/') {
memmove(s, &(s[strlen(musicDir) + 1]), memmove(s, &(s[strlen(musicDir) + 1]),
strlen(&(s[strlen(musicDir) + 1])) + 1); strlen(&(s[strlen(musicDir) + 1])) + 1);
printf("s: <%s>\n", s);
} else if (parentlen) { } else if (parentlen) {
temp = xstrdup(s); temp = xstrdup(s);
memset(s, 0, MPD_PATH_MAX);
strcpy(s, parent); strcpy(s, parent);
strncat(s, "/", MPD_PATH_MAX - parentlen); strncat(s, "/", MPD_PATH_MAX - parentlen);
strncat(s, temp, MPD_PATH_MAX - parentlen - 1); strncat(s, temp, MPD_PATH_MAX - parentlen - 1);
@ -327,22 +334,14 @@ int moveSongInStoredPlaylistByPath(int fd, const char *utf8path, int src, int de
return 0; return 0;
} }
/* Not used currently
static void removeAllFromStoredPlaylist(StoredPlaylist *sp)
{
freeList(sp->list);
sp->list = makeList(DEFAULT_FREE_DATA_FUNC, 0);
}
*/
int removeAllFromStoredPlaylistByPath(int fd, const char *utf8path) int removeAllFromStoredPlaylistByPath(int fd, const char *utf8path)
{ {
char *filename; char filename[MPD_PATH_MAX];
FILE *file; FILE *file;
filename = utf8pathToFsPathInStoredPlaylist(utf8path, fd); if (!valid_playlist_name(fd, utf8path))
if (!filename)
return -1; return -1;
utf8_to_fs_playlist_path(filename, utf8path);
while (!(file = fopen(filename, "w")) && errno == EINTR); while (!(file = fopen(filename, "w")) && errno == EINTR);
if (file == NULL) { if (file == NULL) {
@ -430,20 +429,19 @@ int writeStoredPlaylist(StoredPlaylist *sp)
int appendSongToStoredPlaylistByPath(int fd, const char *utf8path, Song *song) int appendSongToStoredPlaylistByPath(int fd, const char *utf8path, Song *song)
{ {
char *filename;
FILE *file; FILE *file;
char *s; char *s;
char path_max_tmp[MPD_PATH_MAX]; char path_max_tmp[MPD_PATH_MAX];
char path_max_tmp2[MPD_PATH_MAX]; char path_max_tmp2[MPD_PATH_MAX];
filename = utf8pathToFsPathInStoredPlaylist(utf8path, fd); if (!valid_playlist_name(fd, utf8path))
if (!filename)
return -1; return -1;
utf8_to_fs_playlist_path(path_max_tmp, utf8path);
while (!(file = fopen(filename, "a")) && errno == EINTR); while (!(file = fopen(path_max_tmp, "a")) && 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", filename, strerror(errno)); "\"%s\": %s", path_max_tmp, strerror(errno));
return -1; return -1;
} }
@ -468,45 +466,34 @@ 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)
{ {
struct stat st; struct stat st;
char *from; char from[MPD_PATH_MAX];
char *to; char to[MPD_PATH_MAX];
int ret = 0;
from = xstrdup(utf8pathToFsPathInStoredPlaylist(utf8from, fd)); if (!valid_playlist_name(fd, utf8from) ||
if (!from) !valid_playlist_name(fd, utf8to))
return -1; return -1;
to = xstrdup(utf8pathToFsPathInStoredPlaylist(utf8to, fd)); utf8_to_fs_playlist_path(from, utf8from);
if (!to) { utf8_to_fs_playlist_path(to, utf8to);
free(from);
return -1;
}
if (stat(from, &st) != 0) { if (stat(from, &st) != 0) {
commandError(fd, ACK_ERROR_NO_EXIST, commandError(fd, ACK_ERROR_NO_EXIST,
"no playlist named \"%s\"", utf8from); "no playlist named \"%s\"", utf8from);
ret = -1; return -1;
goto out;
} }
if (stat(to, &st) == 0) { if (stat(to, &st) == 0) {
commandError(fd, ACK_ERROR_EXIST, "a file or directory " commandError(fd, ACK_ERROR_EXIST, "a file or directory "
"already exists with the name \"%s\"", utf8to); "already exists with the name \"%s\"", utf8to);
ret = -1; return -1;
goto out;
} }
if (rename(from, to) < 0) { if (rename(from, to) < 0) {
commandError(fd, ACK_ERROR_UNKNOWN, commandError(fd, ACK_ERROR_UNKNOWN,
"could not rename playlist \"%s\" to \"%s\": %s", "could not rename playlist \"%s\" to \"%s\": %s",
utf8from, utf8to, strerror(errno)); utf8from, utf8to, strerror(errno));
ret = -1; return -1;
goto out;
} }
out: return 0;
free(from);
free(to);
return ret;
} }

View File

@ -121,11 +121,7 @@ unsigned long readLEuint32(const unsigned char *p)
mpd_malloc char *xstrdup(const char *s) mpd_malloc char *xstrdup(const char *s)
{ {
char *ret; char *ret = strdup(s);
/* Return NULL, if s is NULL */
if(!s)
return NULL;
ret = strdup(s);
if (mpd_unlikely(!ret)) if (mpd_unlikely(!ret))
FATAL("OOM: strdup\n"); FATAL("OOM: strdup\n");
return ret; return ret;