2004-02-24 00:41:20 +01:00
|
|
|
/* the Music Player Daemon (MPD)
|
2007-04-05 05:22:33 +02:00
|
|
|
* Copyright (C) 2003-2007 by Warren Dukes (warren.dukes@gmail.com)
|
2004-02-24 00:41:20 +01:00
|
|
|
* 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 "ls.h"
|
|
|
|
#include "playlist.h"
|
|
|
|
#include "path.h"
|
|
|
|
#include "myfprintf.h"
|
|
|
|
#include "log.h"
|
2004-06-02 03:26:15 +02:00
|
|
|
#include "utf8.h"
|
2006-08-26 08:25:57 +02:00
|
|
|
#include "utils.h"
|
2008-01-03 08:29:49 +01:00
|
|
|
#include "os_compat.h"
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
static char *remoteUrlPrefixes[] = {
|
|
|
|
"http://",
|
2004-06-02 03:26:15 +02:00
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
2006-07-30 05:43:38 +02:00
|
|
|
int printRemoteUrlHandlers(int fd)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
|
|
|
char **prefixes = remoteUrlPrefixes;
|
2004-06-03 01:22:37 +02:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
while (*prefixes) {
|
2006-07-30 05:43:38 +02:00
|
|
|
fdprintf(fd, "handler: %s\n", *prefixes);
|
2006-07-20 18:02:40 +02:00
|
|
|
prefixes++;
|
|
|
|
}
|
2004-06-03 01:22:37 +02:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
return 0;
|
2004-06-03 01:22:37 +02:00
|
|
|
}
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
int isValidRemoteUtf8Url(char *utf8url)
|
|
|
|
{
|
|
|
|
int ret = 0;
|
|
|
|
char *temp;
|
|
|
|
|
|
|
|
switch (isRemoteUrl(utf8url)) {
|
|
|
|
case 1:
|
|
|
|
ret = 1;
|
|
|
|
temp = utf8url;
|
|
|
|
while (*temp) {
|
|
|
|
if ((*temp >= 'a' && *temp <= 'z') ||
|
|
|
|
(*temp >= 'A' && *temp <= 'Z') ||
|
|
|
|
(*temp >= '0' && *temp <= '9') ||
|
|
|
|
*temp == '$' ||
|
|
|
|
*temp == '-' ||
|
|
|
|
*temp == '.' ||
|
|
|
|
*temp == '+' ||
|
|
|
|
*temp == '!' ||
|
|
|
|
*temp == '*' ||
|
|
|
|
*temp == '\'' ||
|
|
|
|
*temp == '(' ||
|
|
|
|
*temp == ')' ||
|
|
|
|
*temp == ',' ||
|
|
|
|
*temp == '%' ||
|
|
|
|
*temp == '/' ||
|
|
|
|
*temp == ':' ||
|
|
|
|
*temp == '?' ||
|
|
|
|
*temp == ';' || *temp == '&' || *temp == '=') {
|
|
|
|
} else {
|
|
|
|
ret = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
temp++;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
2004-06-02 03:26:15 +02:00
|
|
|
}
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
int isRemoteUrl(char *url)
|
|
|
|
{
|
|
|
|
int count = 0;
|
|
|
|
char **urlPrefixes = remoteUrlPrefixes;
|
2004-05-14 23:35:20 +02:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
while (*urlPrefixes) {
|
|
|
|
count++;
|
|
|
|
if (strncmp(*urlPrefixes, url, strlen(*urlPrefixes)) == 0) {
|
|
|
|
return count;
|
2004-05-13 20:46:38 +02:00
|
|
|
}
|
2004-05-14 23:35:20 +02:00
|
|
|
urlPrefixes++;
|
2004-05-13 20:46:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-07-30 05:43:38 +02:00
|
|
|
int lsPlaylists(int fd, char *utf8path)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
|
|
|
DIR *dir;
|
2004-02-24 00:41:20 +01:00
|
|
|
struct stat st;
|
2006-07-20 18:02:40 +02:00
|
|
|
struct dirent *ent;
|
2008-01-26 13:46:21 +01:00
|
|
|
char *duplicated;
|
2006-07-20 18:02:40 +02:00
|
|
|
char *utf8;
|
2007-12-28 03:56:25 +01:00
|
|
|
char s[MPD_PATH_MAX];
|
|
|
|
char path_max_tmp[MPD_PATH_MAX];
|
2006-07-20 18:02:40 +02:00
|
|
|
List *list = NULL;
|
|
|
|
ListNode *node = NULL;
|
2007-12-28 03:56:25 +01:00
|
|
|
char *actualPath = rpp2app_r(path_max_tmp,
|
|
|
|
utf8_to_fs_charset(path_max_tmp,
|
|
|
|
utf8path));
|
2006-07-20 18:02:40 +02:00
|
|
|
int actlen = strlen(actualPath) + 1;
|
2007-12-28 03:56:25 +01:00
|
|
|
int maxlen = MPD_PATH_MAX - actlen;
|
2006-07-20 18:02:40 +02:00
|
|
|
int suflen = strlen(PLAYLIST_FILE_SUFFIX) + 1;
|
2004-02-24 00:41:20 +01:00
|
|
|
int suff;
|
|
|
|
|
2007-12-28 03:56:25 +01:00
|
|
|
if (actlen > MPD_PATH_MAX - 1 || (dir = opendir(actualPath)) == NULL) {
|
2004-02-24 00:41:20 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-12-28 03:56:25 +01:00
|
|
|
s[MPD_PATH_MAX - 1] = '\0';
|
|
|
|
/* this is safe, notice actlen > MPD_PATH_MAX-1 above */
|
2006-07-20 18:02:40 +02:00
|
|
|
strcpy(s, actualPath);
|
|
|
|
strcat(s, "/");
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
while ((ent = readdir(dir))) {
|
2006-07-30 12:31:41 +02:00
|
|
|
size_t len = strlen(ent->d_name) + 1;
|
2008-01-26 13:46:21 +01:00
|
|
|
duplicated = ent->d_name;
|
2006-07-30 12:31:41 +02:00
|
|
|
if (mpd_likely(len <= maxlen) &&
|
2008-01-26 13:46:21 +01:00
|
|
|
duplicated[0] != '.' &&
|
|
|
|
(suff = strlen(duplicated) - suflen) > 0 &&
|
|
|
|
duplicated[suff] == '.' &&
|
|
|
|
strcmp(duplicated + suff + 1, PLAYLIST_FILE_SUFFIX) == 0) {
|
2006-07-30 12:31:41 +02:00
|
|
|
memcpy(s + actlen, ent->d_name, len);
|
2006-07-20 18:02:40 +02:00
|
|
|
if (stat(s, &st) == 0) {
|
|
|
|
if (S_ISREG(st.st_mode)) {
|
|
|
|
if (list == NULL)
|
|
|
|
list = makeList(NULL, 1);
|
2008-01-26 13:46:21 +01:00
|
|
|
duplicated[suff] = '\0';
|
2007-12-28 03:56:25 +01:00
|
|
|
utf8 = fs_charset_to_utf8(path_max_tmp,
|
2008-01-26 13:46:21 +01:00
|
|
|
duplicated);
|
2007-12-28 03:56:25 +01:00
|
|
|
if (utf8)
|
2006-07-20 18:02:40 +02:00
|
|
|
insertInList(list, utf8, NULL);
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2006-07-20 18:02:40 +02:00
|
|
|
|
2004-02-24 00:41:20 +01:00
|
|
|
closedir(dir);
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if (list) {
|
2004-06-26 19:33:42 +02:00
|
|
|
int i;
|
2004-02-24 00:41:20 +01:00
|
|
|
sortList(list);
|
|
|
|
|
2008-01-26 13:46:21 +01:00
|
|
|
duplicated = xmalloc(strlen(utf8path) + 2);
|
|
|
|
strcpy(duplicated, utf8path);
|
|
|
|
for (i = strlen(duplicated) - 1;
|
|
|
|
i >= 0 && duplicated[i] == '/';
|
|
|
|
i--) {
|
|
|
|
duplicated[i] = '\0';
|
2004-06-26 19:33:42 +02:00
|
|
|
}
|
2008-01-26 13:46:21 +01:00
|
|
|
if (strlen(duplicated))
|
|
|
|
strcat(duplicated, "/");
|
2004-02-24 00:41:20 +01:00
|
|
|
|
|
|
|
node = list->firstNode;
|
2006-07-20 18:02:40 +02:00
|
|
|
while (node != NULL) {
|
|
|
|
if (!strchr(node->key, '\n')) {
|
2008-01-26 13:46:21 +01:00
|
|
|
fdprintf(fd, "playlist: %s%s\n", duplicated,
|
2006-07-20 18:02:40 +02:00
|
|
|
node->key);
|
|
|
|
}
|
2004-02-24 00:41:20 +01:00
|
|
|
node = node->nextNode;
|
|
|
|
}
|
|
|
|
|
|
|
|
freeList(list);
|
2008-01-26 13:46:21 +01:00
|
|
|
free(duplicated);
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
int myStat(char *utf8file, struct stat *st)
|
|
|
|
{
|
2007-12-28 03:56:25 +01:00
|
|
|
char path_max_tmp[MPD_PATH_MAX];
|
|
|
|
char *file = utf8_to_fs_charset(path_max_tmp, utf8file);
|
2006-07-20 18:02:40 +02:00
|
|
|
char *actualFile = file;
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if (actualFile[0] != '/')
|
2007-12-28 03:56:25 +01:00
|
|
|
actualFile = rmp2amp_r(path_max_tmp, file);
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2006-08-14 15:46:51 +02:00
|
|
|
return stat(actualFile, st);
|
2004-06-21 06:11:23 +02:00
|
|
|
}
|
|
|
|
|
2007-12-28 03:56:25 +01:00
|
|
|
int isFile(char *utf8file, time_t * mtime)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2004-06-21 06:11:23 +02:00
|
|
|
struct stat st;
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if (myStat(utf8file, &st) == 0) {
|
|
|
|
if (S_ISREG(st.st_mode)) {
|
|
|
|
if (mtime)
|
|
|
|
*mtime = st.st_mtime;
|
2004-03-21 04:45:58 +01:00
|
|
|
return 1;
|
2006-07-20 18:02:40 +02:00
|
|
|
} else {
|
|
|
|
DEBUG("isFile: %s is not a regular file\n", utf8file);
|
|
|
|
return 0;
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
2006-07-20 18:02:40 +02:00
|
|
|
} else {
|
|
|
|
DEBUG("isFile: failed to stat: %s: %s\n", utf8file,
|
|
|
|
strerror(errno));
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2004-05-30 02:05:23 +02:00
|
|
|
/* suffixes should be ascii only characters */
|
2006-07-20 18:02:40 +02:00
|
|
|
char *getSuffix(char *utf8file)
|
|
|
|
{
|
|
|
|
char *ret = NULL;
|
2006-07-14 21:02:48 +02:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
while (*utf8file) {
|
|
|
|
if (*utf8file == '.')
|
|
|
|
ret = utf8file + 1;
|
|
|
|
utf8file++;
|
|
|
|
}
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2004-03-21 04:45:58 +01:00
|
|
|
return ret;
|
|
|
|
}
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2006-08-20 02:50:44 +02:00
|
|
|
static int hasSuffix(char *utf8file, char *suffix)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
|
|
|
char *s = getSuffix(utf8file);
|
|
|
|
if (s && 0 == strcmp(s, suffix))
|
|
|
|
return 1;
|
|
|
|
return 0;
|
2004-05-30 02:05:23 +02:00
|
|
|
}
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
int isPlaylist(char *utf8file)
|
|
|
|
{
|
|
|
|
if (isFile(utf8file, NULL)) {
|
|
|
|
return hasSuffix(utf8file, PLAYLIST_FILE_SUFFIX);
|
2004-03-21 04:45:58 +01:00
|
|
|
}
|
2004-02-24 00:41:20 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
int isDir(char *utf8name)
|
|
|
|
{
|
2004-02-24 00:41:20 +01:00
|
|
|
struct stat st;
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if (myStat(utf8name, &st) == 0) {
|
|
|
|
if (S_ISDIR(st.st_mode)) {
|
2004-03-10 03:58:27 +01:00
|
|
|
return 1;
|
|
|
|
}
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2004-03-21 04:45:58 +01:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
InputPlugin *hasMusicSuffix(char *utf8file, unsigned int next)
|
|
|
|
{
|
|
|
|
InputPlugin *ret = NULL;
|
|
|
|
|
|
|
|
char *s = getSuffix(utf8file);
|
|
|
|
if (s) {
|
|
|
|
ret = getInputPluginFromSuffix(s, next);
|
|
|
|
} else {
|
|
|
|
DEBUG("hasMusicSuffix: The file: %s has no valid suffix\n",
|
|
|
|
utf8file);
|
2005-09-08 23:08:02 +02:00
|
|
|
}
|
2004-03-21 04:45:58 +01:00
|
|
|
|
2004-05-31 03:21:17 +02:00
|
|
|
return ret;
|
2004-03-21 04:45:58 +01:00
|
|
|
}
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
InputPlugin *isMusic(char *utf8file, time_t * mtime, unsigned int next)
|
|
|
|
{
|
|
|
|
if (isFile(utf8file, mtime)) {
|
|
|
|
InputPlugin *plugin = hasMusicSuffix(utf8file, next);
|
2005-10-25 22:35:16 +02:00
|
|
|
if (plugin != NULL)
|
|
|
|
return plugin;
|
2004-06-21 06:11:23 +02:00
|
|
|
}
|
2006-07-20 18:02:40 +02:00
|
|
|
DEBUG("isMusic: %s is not a valid file\n", utf8file);
|
2004-06-21 06:11:23 +02:00
|
|
|
return NULL;
|
|
|
|
}
|