2004-02-24 00:41:20 +01:00
|
|
|
/* the Music Player Daemon (MPD)
|
|
|
|
* (c)2003-2004 by Warren Dukes (shank@mercury.chem.pitt.edu)
|
|
|
|
* 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 <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include <dirent.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
char * dupAndStripPlaylistSuffix(char * file) {
|
2004-02-25 22:10:56 +01:00
|
|
|
size_t size = strlen(file)-strlen(PLAYLIST_FILE_SUFFIX)-1;
|
2004-02-24 00:41:20 +01:00
|
|
|
char * ret = malloc(size+1);
|
|
|
|
|
|
|
|
strncpy(ret,file,size);
|
|
|
|
ret[size] = '\0';
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2004-06-02 03:26:15 +02:00
|
|
|
static char * remoteUrlPrefixes[] =
|
|
|
|
{
|
|
|
|
"http://",
|
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
2004-06-03 01:22:37 +02:00
|
|
|
int printRemoteUrlHandlers(FILE * fp) {
|
|
|
|
char ** prefixes = remoteUrlPrefixes;
|
|
|
|
|
|
|
|
while (*prefixes) {
|
|
|
|
myfprintf(fp,"handler: %s\n", *prefixes);
|
|
|
|
prefixes++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2004-06-02 03:26:15 +02:00
|
|
|
int isValidRemoteUtf8Url(char * utf8url) {
|
|
|
|
int ret = 0;
|
|
|
|
char * temp;
|
|
|
|
|
2004-06-03 02:24:33 +02:00
|
|
|
switch(isRemoteUrl(utf8url)) {
|
2004-06-02 03:26:15 +02:00
|
|
|
case 1:
|
|
|
|
ret = 1;
|
2004-06-03 02:24:33 +02:00
|
|
|
temp = utf8url;
|
2004-06-02 03:26:15 +02:00
|
|
|
while(*temp) {
|
|
|
|
if((*temp >= 'a' && *temp <= 'z') ||
|
2004-06-13 22:58:22 +02:00
|
|
|
(*temp >= 'A' && *temp <= 'Z') ||
|
2004-06-02 03:26:15 +02:00
|
|
|
(*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;
|
|
|
|
}
|
2004-05-13 20:46:38 +02:00
|
|
|
|
2004-06-02 03:26:15 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
int isRemoteUrl(char * url) {
|
|
|
|
int count = 0;
|
|
|
|
char ** urlPrefixes = remoteUrlPrefixes;
|
2004-05-14 23:35:20 +02:00
|
|
|
|
2004-05-13 20:46:38 +02:00
|
|
|
while(*urlPrefixes) {
|
2004-06-02 03:26:15 +02:00
|
|
|
count++;
|
2004-05-13 20:46:38 +02:00
|
|
|
if(strncmp(*urlPrefixes,url,strlen(*urlPrefixes)) == 0) {
|
2004-06-02 03:26:15 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2004-02-24 00:41:20 +01:00
|
|
|
int lsPlaylists(FILE * fp, char * utf8path) {
|
|
|
|
DIR * dir;
|
|
|
|
struct stat st;
|
|
|
|
struct dirent * ent;
|
|
|
|
char * dup;
|
2004-04-13 06:59:57 +02:00
|
|
|
char * utf8;
|
2004-02-24 00:41:20 +01:00
|
|
|
char s[MAXPATHLEN+1];
|
|
|
|
List * list = NULL;
|
|
|
|
ListNode * node = NULL;
|
|
|
|
char * path = strdup(utf8ToFsCharset(utf8path));
|
|
|
|
char * actualPath = rpp2app(path);
|
|
|
|
int actlen = strlen(actualPath)+1;
|
|
|
|
int maxlen = MAXPATHLEN-actlen;
|
|
|
|
int suflen = strlen(PLAYLIST_FILE_SUFFIX)+1;
|
|
|
|
int suff;
|
|
|
|
|
|
|
|
if(actlen>MAXPATHLEN-1 || (dir = opendir(actualPath))==NULL) {
|
|
|
|
free(path);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
s[MAXPATHLEN] = '\0';
|
2004-04-16 17:01:06 +02:00
|
|
|
/* this is safe, notice actlen > MAXPATHLEN-1 above */
|
2004-02-24 00:41:20 +01:00
|
|
|
strcpy(s,actualPath);
|
|
|
|
strcat(s,"/");
|
|
|
|
|
|
|
|
while((ent = readdir(dir))) {
|
|
|
|
dup = ent->d_name;
|
|
|
|
if(dup[0]!='.' &&
|
|
|
|
(suff=strlen(dup)-suflen)>0 &&
|
|
|
|
dup[suff]=='.' &&
|
|
|
|
strcmp(dup+suff+1,PLAYLIST_FILE_SUFFIX)==0)
|
|
|
|
{
|
|
|
|
strncpy(s+actlen,ent->d_name,maxlen);
|
|
|
|
if(stat(s,&st)==0) {
|
|
|
|
if(S_ISREG(st.st_mode)) {
|
|
|
|
if(list==NULL) list = makeList(NULL);
|
|
|
|
dup = strdup(ent->d_name);
|
|
|
|
dup[suff] = '\0';
|
2004-04-13 06:59:57 +02:00
|
|
|
if((utf8 = fsCharsetToUtf8(dup))) {
|
|
|
|
insertInList(list,utf8,NULL);
|
|
|
|
}
|
2004-02-24 00:41:20 +01:00
|
|
|
free(dup);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
closedir(dir);
|
|
|
|
free(path);
|
|
|
|
|
|
|
|
if(list) {
|
|
|
|
sortList(list);
|
|
|
|
|
|
|
|
dup = malloc(strlen(utf8path)+2);
|
|
|
|
strcpy(dup,utf8path);
|
|
|
|
while(dup[strlen(dup)-1]=='/') dup[strlen(dup)-1] = '\0';
|
|
|
|
if(strlen(dup)) strcat(dup,"/");
|
|
|
|
|
|
|
|
node = list->firstNode;
|
|
|
|
while(node!=NULL) {
|
2004-06-02 04:07:07 +02:00
|
|
|
if(!strchr(node->key, '\n')) {
|
|
|
|
myfprintf(fp,"playlist: %s%s\n",dup,node->key);
|
|
|
|
}
|
2004-02-24 00:41:20 +01:00
|
|
|
node = node->nextNode;
|
|
|
|
}
|
|
|
|
|
|
|
|
freeList(list);
|
|
|
|
free(dup);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2004-06-21 06:11:23 +02:00
|
|
|
int myStat(char * utf8file, struct stat * st) {
|
2004-02-24 00:41:20 +01:00
|
|
|
char * file = utf8ToFsCharset(utf8file);
|
|
|
|
char * actualFile = file;
|
|
|
|
|
2004-03-21 04:45:58 +01:00
|
|
|
if(actualFile[0]!='/') actualFile = rmp2amp(file);
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2004-06-21 06:11:23 +02:00
|
|
|
return stat(actualFile,st);
|
|
|
|
}
|
|
|
|
|
|
|
|
int isFile(char * utf8file, time_t * mtime) {
|
|
|
|
struct stat st;
|
|
|
|
|
2004-06-21 16:32:13 +02:00
|
|
|
if(myStat(utf8file,&st)==0) {
|
2004-02-24 00:41:20 +01:00
|
|
|
if(S_ISREG(st.st_mode)) {
|
2004-03-21 04:45:58 +01:00
|
|
|
if(mtime) *mtime = st.st_mtime;
|
|
|
|
return 1;
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
2004-03-21 04:45:58 +01:00
|
|
|
else return 0;
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2004-05-30 02:05:23 +02:00
|
|
|
/* suffixes should be ascii only characters */
|
|
|
|
char * getSuffix(char * utf8file) {
|
|
|
|
char * ret = NULL;
|
|
|
|
|
|
|
|
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
|
|
|
|
2004-05-30 02:05:23 +02:00
|
|
|
int hasSuffix(char * utf8file, char * suffix) {
|
|
|
|
char * s = getSuffix(utf8file);
|
|
|
|
if(s && 0==strcmp(s,suffix)) return 1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2004-03-21 04:45:58 +01:00
|
|
|
int isPlaylist(char * utf8file) {
|
|
|
|
if(isFile(utf8file,NULL)) {
|
|
|
|
return hasSuffix(utf8file,PLAYLIST_FILE_SUFFIX);
|
|
|
|
}
|
2004-02-24 00:41:20 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2004-04-16 02:41:56 +02:00
|
|
|
int isDir(char * utf8name) {
|
2004-02-24 00:41:20 +01:00
|
|
|
struct stat st;
|
|
|
|
|
2004-06-21 06:11:23 +02:00
|
|
|
if(myStat(utf8name,&st)==0) {
|
2004-03-10 03:58:27 +01:00
|
|
|
if(S_ISDIR(st.st_mode)) {
|
|
|
|
return 1;
|
|
|
|
}
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2004-03-21 04:45:58 +01:00
|
|
|
|
2004-06-21 06:11:23 +02:00
|
|
|
InputPlugin * hasMusicSuffix(char * utf8file) {
|
2004-05-31 03:21:17 +02:00
|
|
|
InputPlugin * ret = NULL;
|
|
|
|
|
2004-06-21 06:11:23 +02:00
|
|
|
char * s = getSuffix(utf8file);
|
|
|
|
if(s) ret = getInputPluginFromSuffix(s);
|
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
|
|
|
}
|
|
|
|
|
2004-06-21 06:11:23 +02:00
|
|
|
InputPlugin * isMusic(char * utf8file, time_t * mtime) {
|
|
|
|
if(isFile(utf8file,mtime)) {
|
|
|
|
return hasMusicSuffix(utf8file);
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|