2004-02-24 00:41:20 +01:00
|
|
|
/* the Music Player Daemon (MPD)
|
2006-07-14 21:37:45 +02:00
|
|
|
* (c)2003-2006 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"
|
2004-02-24 00:41:20 +01:00
|
|
|
|
|
|
|
#include <dirent.h>
|
|
|
|
#include <stdio.h>
|
2005-09-08 23:08:02 +02:00
|
|
|
#include <errno.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;
|
|
|
|
char *dup;
|
|
|
|
char *utf8;
|
|
|
|
char s[MAXPATHLEN + 1];
|
|
|
|
List *list = NULL;
|
|
|
|
ListNode *node = NULL;
|
|
|
|
char *path = utf8ToFsCharset(utf8path);
|
|
|
|
char *actualPath = rpp2app(path);
|
|
|
|
int actlen = strlen(actualPath) + 1;
|
|
|
|
int maxlen = MAXPATHLEN - actlen;
|
|
|
|
int suflen = strlen(PLAYLIST_FILE_SUFFIX) + 1;
|
2004-02-24 00:41:20 +01:00
|
|
|
int suff;
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if (actlen > MAXPATHLEN - 1 || (dir = opendir(actualPath)) == NULL) {
|
2004-02-24 00:41:20 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
s[MAXPATHLEN] = '\0';
|
2004-04-16 17:01:06 +02:00
|
|
|
/* this is safe, notice actlen > MAXPATHLEN-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;
|
2004-02-24 00:41:20 +01:00
|
|
|
dup = ent->d_name;
|
2006-07-30 12:31:41 +02:00
|
|
|
if (mpd_likely(len <= maxlen) &&
|
|
|
|
dup[0] != '.' &&
|
2006-07-20 18:02:40 +02:00
|
|
|
(suff = strlen(dup) - suflen) > 0 &&
|
|
|
|
dup[suff] == '.' &&
|
|
|
|
strcmp(dup + 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);
|
2004-02-24 00:41:20 +01:00
|
|
|
dup[suff] = '\0';
|
2006-07-20 18:02:40 +02:00
|
|
|
if ((utf8 = fsCharsetToUtf8(dup))) {
|
|
|
|
insertInList(list, utf8, NULL);
|
2004-04-13 06:59:57 +02:00
|
|
|
}
|
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);
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
dup = malloc(strlen(utf8path) + 2);
|
|
|
|
strcpy(dup, utf8path);
|
|
|
|
for (i = strlen(dup) - 1; i >= 0 && dup[i] == '/'; i--) {
|
2004-06-26 19:33:42 +02:00
|
|
|
dup[i] = '\0';
|
|
|
|
}
|
2006-07-20 18:02:40 +02:00
|
|
|
if (strlen(dup))
|
|
|
|
strcat(dup, "/");
|
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')) {
|
2006-07-30 05:43:38 +02:00
|
|
|
fdprintf(fd, "playlist: %s%s\n", dup,
|
2006-07-20 18:02:40 +02:00
|
|
|
node->key);
|
|
|
|
}
|
2004-02-24 00:41:20 +01:00
|
|
|
node = node->nextNode;
|
|
|
|
}
|
|
|
|
|
|
|
|
freeList(list);
|
|
|
|
free(dup);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
int myStat(char *utf8file, struct stat *st)
|
|
|
|
{
|
|
|
|
char *file = utf8ToFsCharset(utf8file);
|
|
|
|
char *actualFile = file;
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if (actualFile[0] != '/')
|
|
|
|
actualFile = rmp2amp(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
|
|
|
}
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
static int isFile(char *utf8file, time_t * mtime)
|
|
|
|
{
|
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-07-20 18:02:40 +02:00
|
|
|
int hasSuffix(char *utf8file, char *suffix)
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|