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 "path.h"
|
|
|
|
#include "log.h"
|
|
|
|
#include "charConv.h"
|
|
|
|
#include "conf.h"
|
2004-04-14 07:26:32 +02:00
|
|
|
#include "utf8.h"
|
2006-08-26 08:25:57 +02:00
|
|
|
#include "utils.h"
|
2004-02-24 00:41:20 +01:00
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2004-04-15 05:26:15 +02:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <unistd.h>
|
2004-07-20 19:43:01 +02:00
|
|
|
#include <errno.h>
|
2005-03-06 20:00:58 +01:00
|
|
|
#include <dirent.h>
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2007-02-18 01:42:22 +01:00
|
|
|
#ifdef HAVE_LOCALE
|
|
|
|
#ifdef HAVE_LANGINFO_CODESET
|
|
|
|
#include <locale.h>
|
|
|
|
#include <langinfo.h>
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
2006-07-30 12:31:41 +02:00
|
|
|
const char *musicDir;
|
|
|
|
static const char *playlistDir;
|
2007-01-14 04:07:53 +01:00
|
|
|
static char *fsCharset;
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2007-02-18 01:42:22 +01:00
|
|
|
static char *pathConvCharset(char *to, char *from, char *str, char *ret)
|
|
|
|
{
|
|
|
|
if (ret)
|
|
|
|
free(ret);
|
|
|
|
return setCharSetConversion(to, from) ? NULL : convStrDup(str);
|
|
|
|
}
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
char *fsCharsetToUtf8(char *str)
|
|
|
|
{
|
2007-01-14 04:07:53 +01:00
|
|
|
static char *ret;
|
2006-08-14 15:46:51 +02:00
|
|
|
|
2007-02-18 01:42:22 +01:00
|
|
|
ret = pathConvCharset("UTF-8", fsCharset, str, ret);
|
2004-04-13 06:59:57 +02:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if (ret && !validUtf8String(ret)) {
|
2004-04-13 07:19:45 +02:00
|
|
|
free(ret);
|
|
|
|
ret = NULL;
|
|
|
|
}
|
2004-04-13 06:59:57 +02:00
|
|
|
|
|
|
|
return ret;
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
char *utf8ToFsCharset(char *str)
|
|
|
|
{
|
2007-01-14 04:07:53 +01:00
|
|
|
static char *ret;
|
2006-08-14 15:46:51 +02:00
|
|
|
|
2007-02-18 01:42:22 +01:00
|
|
|
ret = pathConvCharset(fsCharset, "UTF-8", str, ret);
|
2004-04-13 06:59:57 +02:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if (!ret)
|
2006-08-26 08:25:57 +02:00
|
|
|
ret = xstrdup(str);
|
2004-04-13 06:59:57 +02:00
|
|
|
|
|
|
|
return ret;
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
void setFsCharset(char *charset)
|
|
|
|
{
|
2004-04-13 06:59:57 +02:00
|
|
|
int error = 0;
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if (fsCharset)
|
|
|
|
free(fsCharset);
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2006-08-26 08:25:57 +02:00
|
|
|
fsCharset = xstrdup(charset);
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
DEBUG("setFsCharset: fs charset is: %s\n", fsCharset);
|
|
|
|
|
|
|
|
if (setCharSetConversion("UTF-8", fsCharset) != 0) {
|
2004-06-12 04:06:16 +02:00
|
|
|
WARNING("fs charset conversion problem: "
|
2004-02-24 00:41:20 +01:00
|
|
|
"not able to convert from \"%s\" to \"%s\"\n",
|
2006-07-20 18:02:40 +02:00
|
|
|
fsCharset, "UTF-8");
|
2004-04-13 06:59:57 +02:00
|
|
|
error = 1;
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
2006-07-20 18:02:40 +02:00
|
|
|
if (setCharSetConversion(fsCharset, "UTF-8") != 0) {
|
2004-06-12 04:06:16 +02:00
|
|
|
WARNING("fs charset conversion problem: "
|
2004-02-24 00:41:20 +01:00
|
|
|
"not able to convert from \"%s\" to \"%s\"\n",
|
2006-07-20 18:02:40 +02:00
|
|
|
"UTF-8", fsCharset);
|
2004-04-13 06:59:57 +02:00
|
|
|
error = 1;
|
|
|
|
}
|
2006-07-20 18:02:40 +02:00
|
|
|
|
|
|
|
if (error) {
|
2004-04-13 06:59:57 +02:00
|
|
|
free(fsCharset);
|
2004-06-12 04:06:16 +02:00
|
|
|
WARNING("setting fs charset to ISO-8859-1!\n");
|
2006-08-26 08:25:57 +02:00
|
|
|
fsCharset = xstrdup("ISO-8859-1");
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
char *getFsCharset(void)
|
|
|
|
{
|
2004-02-24 00:41:20 +01:00
|
|
|
return fsCharset;
|
|
|
|
}
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
static char *appendSlash(char **path)
|
|
|
|
{
|
|
|
|
char *temp = *path;
|
2005-03-06 20:00:58 +01:00
|
|
|
int len = strlen(temp);
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if (temp[len - 1] != '/') {
|
2006-08-26 08:25:57 +02:00
|
|
|
temp = xmalloc(len + 2);
|
2006-07-20 18:02:40 +02:00
|
|
|
memset(temp, 0, len + 2);
|
2005-03-06 21:04:50 +01:00
|
|
|
memcpy(temp, *path, len);
|
|
|
|
temp[len] = '/';
|
2005-03-06 20:00:58 +01:00
|
|
|
free(*path);
|
2005-03-06 21:04:50 +01:00
|
|
|
*path = temp;
|
2005-03-06 20:00:58 +01:00
|
|
|
}
|
|
|
|
|
2005-03-06 21:04:50 +01:00
|
|
|
return temp;
|
2005-03-06 20:00:58 +01:00
|
|
|
}
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
void initPaths(void)
|
|
|
|
{
|
|
|
|
ConfigParam *musicParam = parseConfigFilePath(CONF_MUSIC_DIR, 1);
|
|
|
|
ConfigParam *playlistParam = parseConfigFilePath(CONF_PLAYLIST_DIR, 1);
|
|
|
|
ConfigParam *fsCharsetParam = getConfigParam(CONF_FS_CHARSET);
|
2005-03-06 20:00:58 +01:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
char *charset = NULL;
|
2007-02-18 01:42:22 +01:00
|
|
|
char *originalLocale;
|
2006-07-20 18:02:40 +02:00
|
|
|
DIR *dir;
|
2004-04-15 05:26:15 +02:00
|
|
|
|
2005-03-06 21:04:50 +01:00
|
|
|
musicDir = appendSlash(&(musicParam->value));
|
|
|
|
playlistDir = appendSlash(&(playlistParam->value));
|
2004-04-15 05:26:15 +02:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if ((dir = opendir(playlistDir)) == NULL) {
|
2007-05-26 20:15:54 +02:00
|
|
|
FATAL("cannot open %s \"%s\" (config line %i): %s\n",
|
2006-07-20 18:02:40 +02:00
|
|
|
CONF_PLAYLIST_DIR, playlistParam->value,
|
|
|
|
playlistParam->line, strerror(errno));
|
|
|
|
}
|
2005-03-06 20:00:58 +01:00
|
|
|
closedir(dir);
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if ((dir = opendir(musicDir)) == NULL) {
|
2007-05-26 20:15:54 +02:00
|
|
|
FATAL("cannot open %s \"%s\" (config line %i): %s\n",
|
2006-07-20 18:02:40 +02:00
|
|
|
CONF_MUSIC_DIR, musicParam->value,
|
|
|
|
musicParam->line, strerror(errno));
|
|
|
|
}
|
2005-03-06 20:00:58 +01:00
|
|
|
closedir(dir);
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2007-02-18 01:42:22 +01:00
|
|
|
if (fsCharsetParam) {
|
2006-08-26 08:25:57 +02:00
|
|
|
charset = xstrdup(fsCharsetParam->value);
|
2007-02-18 01:42:22 +01:00
|
|
|
}
|
|
|
|
#ifdef HAVE_LOCALE
|
|
|
|
#ifdef HAVE_LANGINFO_CODESET
|
|
|
|
else if ((originalLocale = setlocale(LC_CTYPE, NULL))) {
|
|
|
|
char *temp;
|
|
|
|
char *currentLocale;
|
|
|
|
originalLocale = xstrdup(originalLocale);
|
|
|
|
|
|
|
|
if (!(currentLocale = setlocale(LC_CTYPE, ""))) {
|
|
|
|
WARNING("problems setting current locale with "
|
|
|
|
"setlocale()\n");
|
|
|
|
} else {
|
|
|
|
if (strcmp(currentLocale, "C") == 0 ||
|
|
|
|
strcmp(currentLocale, "POSIX") == 0) {
|
|
|
|
WARNING("current locale is \"%s\"\n",
|
|
|
|
currentLocale);
|
|
|
|
} else if ((temp = nl_langinfo(CODESET))) {
|
|
|
|
charset = xstrdup(temp);
|
|
|
|
} else
|
|
|
|
WARNING
|
|
|
|
("problems getting charset for locale\n");
|
|
|
|
if (!setlocale(LC_CTYPE, originalLocale)) {
|
|
|
|
WARNING
|
|
|
|
("problems resetting locale with setlocale()\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
free(originalLocale);
|
|
|
|
} else
|
|
|
|
WARNING("problems getting locale with setlocale()\n");
|
|
|
|
#endif
|
|
|
|
#endif
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if (charset) {
|
2004-02-24 00:41:20 +01:00
|
|
|
setFsCharset(charset);
|
|
|
|
free(charset);
|
2006-07-20 18:02:40 +02:00
|
|
|
} else {
|
2004-06-12 04:06:16 +02:00
|
|
|
WARNING("setting filesystem charset to ISO-8859-1\n");
|
2004-03-21 22:32:23 +01:00
|
|
|
setFsCharset("ISO-8859-1");
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
void finishPaths(void)
|
|
|
|
{
|
2004-02-24 00:41:20 +01:00
|
|
|
free(fsCharset);
|
|
|
|
fsCharset = NULL;
|
|
|
|
}
|
|
|
|
|
2006-07-30 12:31:41 +02:00
|
|
|
static char *pfx_path(const char *path, const char *pfx, const size_t pfx_len)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2006-07-30 12:31:41 +02:00
|
|
|
static char ret[MAXPATHLEN+1];
|
|
|
|
size_t rp_len = strlen(path);
|
|
|
|
|
|
|
|
/* check for the likely condition first: */
|
|
|
|
if (mpd_likely((pfx_len + rp_len) < MAXPATHLEN)) {
|
|
|
|
memcpy(ret, pfx, pfx_len);
|
|
|
|
memcpy(ret + pfx_len, path, rp_len + 1);
|
|
|
|
return ret;
|
|
|
|
}
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2006-07-30 12:31:41 +02:00
|
|
|
/* unlikely, return an empty string because truncating would
|
|
|
|
* also be wrong... break early and break loudly (the system
|
|
|
|
* headers are likely screwed, not mpd) */
|
2007-08-27 23:03:22 +02:00
|
|
|
ERROR("Cannot prefix '%s' to '%s', max: %d\n", pfx, path, MAXPATHLEN);
|
2006-07-30 12:31:41 +02:00
|
|
|
ret[0] = '\0';
|
|
|
|
return ret;
|
|
|
|
}
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2006-07-30 12:31:41 +02:00
|
|
|
char *rmp2amp(char *relativePath)
|
|
|
|
{
|
|
|
|
size_t pfx_len = strlen(musicDir);
|
|
|
|
return pfx_path(relativePath, musicDir, pfx_len);
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
char *rpp2app(char *relativePath)
|
|
|
|
{
|
2006-07-30 12:31:41 +02:00
|
|
|
size_t pfx_len = strlen(playlistDir);
|
|
|
|
return pfx_path(relativePath, playlistDir, pfx_len);
|
|
|
|
}
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2006-07-30 12:31:41 +02:00
|
|
|
/* this is actually like strlcpy (OpenBSD), but we don't actually want to
|
|
|
|
* blindly use it everywhere, only for paths that are OK to truncate (for
|
|
|
|
* error reporting and such */
|
|
|
|
void pathcpy_trunc(char *dest, const char *src)
|
|
|
|
{
|
|
|
|
size_t len = strlen(src);
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2006-07-30 12:31:41 +02:00
|
|
|
if (mpd_unlikely(len > MAXPATHLEN))
|
|
|
|
len = MAXPATHLEN;
|
|
|
|
memcpy(dest, src, len);
|
|
|
|
dest[len] = '\0';
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
char *parentPath(char *path)
|
|
|
|
{
|
2006-07-30 12:31:41 +02:00
|
|
|
static char parentPath[MAXPATHLEN+1];
|
2006-07-20 18:02:40 +02:00
|
|
|
char *c;
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2006-07-30 12:31:41 +02:00
|
|
|
pathcpy_trunc(parentPath, path);
|
|
|
|
c = strrchr(parentPath,'/');
|
2006-07-20 18:02:40 +02:00
|
|
|
|
2005-03-28 14:21:12 +02:00
|
|
|
if (c == NULL)
|
|
|
|
parentPath[0] = '\0';
|
|
|
|
else {
|
2006-07-20 18:02:40 +02:00
|
|
|
while ((parentPath <= c) && *(--c) == '/') /* nothing */
|
|
|
|
;
|
2005-03-28 14:21:12 +02:00
|
|
|
c[1] = '\0';
|
2006-07-20 18:02:40 +02:00
|
|
|
}
|
2004-02-24 00:41:20 +01:00
|
|
|
|
|
|
|
return parentPath;
|
|
|
|
}
|
2004-04-14 07:26:32 +02:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
char *sanitizePathDup(char *path)
|
|
|
|
{
|
|
|
|
int len = strlen(path) + 1;
|
2006-08-26 08:25:57 +02:00
|
|
|
char *ret = xmalloc(len);
|
2006-07-20 18:02:40 +02:00
|
|
|
char *cp = ret;
|
2004-04-14 07:26:32 +02:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
memset(ret, 0, len);
|
2004-04-14 07:26:32 +02:00
|
|
|
|
|
|
|
len = 0;
|
|
|
|
|
2006-10-14 10:50:23 +02:00
|
|
|
/* eliminate more than one '/' in a row, like "///" */
|
2006-07-20 18:02:40 +02:00
|
|
|
while (*path) {
|
|
|
|
while (*path == '/')
|
|
|
|
path++;
|
|
|
|
if (*path == '.') {
|
2006-10-14 10:50:23 +02:00
|
|
|
/* we don't want to have hidden directories, or '.' or
|
2004-04-14 07:26:32 +02:00
|
|
|
".." in our path */
|
|
|
|
free(ret);
|
|
|
|
return NULL;
|
|
|
|
}
|
2006-07-20 18:02:40 +02:00
|
|
|
while (*path && *path != '/') {
|
2004-04-14 07:26:32 +02:00
|
|
|
*(cp++) = *(path++);
|
|
|
|
len++;
|
|
|
|
}
|
2006-07-20 18:02:40 +02:00
|
|
|
if (*path == '/') {
|
2004-04-14 07:26:32 +02:00
|
|
|
*(cp++) = *(path++);
|
|
|
|
len++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if (len && ret[len - 1] == '/') {
|
2004-04-14 07:26:32 +02:00
|
|
|
len--;
|
|
|
|
ret[len] = '\0';
|
|
|
|
}
|
|
|
|
|
2004-07-12 17:19:26 +02:00
|
|
|
DEBUG("sanitized: %s\n", ret);
|
2004-04-14 07:26:32 +02:00
|
|
|
|
2006-08-26 08:25:57 +02:00
|
|
|
return xrealloc(ret, len + 1);
|
2004-04-14 07:26:32 +02:00
|
|
|
}
|