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 "conf.h"
|
2008-01-01 11:07:34 +01:00
|
|
|
#include "playlist.h"
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2008-10-15 19:36:30 +02:00
|
|
|
#include <glib.h>
|
|
|
|
|
2008-12-29 17:28:32 +01:00
|
|
|
#include <string.h>
|
|
|
|
|
2008-12-29 17:32:53 +01:00
|
|
|
#undef G_LOG_DOMAIN
|
|
|
|
#define G_LOG_DOMAIN "path"
|
|
|
|
|
2008-11-05 18:21:52 +01:00
|
|
|
static char *fs_charset;
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2008-02-05 11:17:33 +01:00
|
|
|
char *fs_charset_to_utf8(char *dst, const char *str)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2008-10-15 19:36:30 +02:00
|
|
|
gchar *p;
|
|
|
|
GError *error = NULL;
|
|
|
|
|
|
|
|
p = g_convert(str, -1,
|
2008-11-20 17:48:11 +01:00
|
|
|
"utf-8", fs_charset,
|
2008-10-15 19:36:30 +02:00
|
|
|
NULL, NULL, &error);
|
2008-10-31 12:20:48 +01:00
|
|
|
if (p == NULL) {
|
2008-10-15 19:36:30 +02:00
|
|
|
/* no fallback */
|
2008-10-31 12:20:48 +01:00
|
|
|
g_error_free(error);
|
2008-10-15 19:36:30 +02:00
|
|
|
return NULL;
|
2008-10-31 12:20:48 +01:00
|
|
|
}
|
2008-10-15 19:36:30 +02:00
|
|
|
|
|
|
|
g_strlcpy(dst, p, MPD_PATH_MAX);
|
|
|
|
g_free(p);
|
|
|
|
return dst;
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
|
|
|
|
2008-02-05 11:17:33 +01:00
|
|
|
char *utf8_to_fs_charset(char *dst, const char *str)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2008-10-15 19:36:30 +02:00
|
|
|
gchar *p;
|
|
|
|
GError *error = NULL;
|
|
|
|
|
|
|
|
p = g_convert(str, -1,
|
2008-11-20 17:48:11 +01:00
|
|
|
fs_charset, "utf-8",
|
2008-10-15 19:36:30 +02:00
|
|
|
NULL, NULL, &error);
|
|
|
|
if (p == NULL) {
|
|
|
|
/* fall back to UTF-8 */
|
|
|
|
g_error_free(error);
|
|
|
|
return strcpy(dst, str);
|
|
|
|
}
|
|
|
|
|
|
|
|
g_strlcpy(dst, p, MPD_PATH_MAX);
|
|
|
|
g_free(p);
|
|
|
|
return dst;
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
|
|
|
|
2008-11-05 18:21:52 +01:00
|
|
|
void path_set_fs_charset(const char *charset)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2008-11-05 18:21:52 +01:00
|
|
|
g_free(fs_charset);
|
|
|
|
fs_charset = g_strdup(charset);
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2008-12-29 17:32:53 +01:00
|
|
|
g_debug("path_set_fs_charset: fs charset is: %s", fs_charset);
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
|
|
|
|
2008-11-05 18:21:52 +01:00
|
|
|
const char *path_get_fs_charset(void)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2008-11-05 18:21:52 +01:00
|
|
|
return fs_charset;
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
|
|
|
|
2008-11-05 18:21:52 +01:00
|
|
|
void path_global_init(void)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2008-11-05 18:21:52 +01:00
|
|
|
ConfigParam *fs_charset_param = getConfigParam(CONF_FS_CHARSET);
|
2008-11-05 18:26:45 +01:00
|
|
|
const char *charset = NULL;
|
2005-03-06 20:00:58 +01:00
|
|
|
|
2008-11-05 18:21:52 +01:00
|
|
|
if (fs_charset_param) {
|
2008-11-05 18:26:45 +01:00
|
|
|
charset = fs_charset_param->value;
|
2008-11-05 18:25:57 +01:00
|
|
|
} else {
|
|
|
|
const gchar **encodings;
|
|
|
|
g_get_filename_charsets(&encodings);
|
|
|
|
|
|
|
|
if (encodings[0] != NULL && *encodings[0] != '\0')
|
2008-11-05 18:26:45 +01:00
|
|
|
charset = encodings[0];
|
2007-02-18 01:42:22 +01:00
|
|
|
}
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if (charset) {
|
2008-11-05 18:21:52 +01:00
|
|
|
path_set_fs_charset(charset);
|
2006-07-20 18:02:40 +02:00
|
|
|
} else {
|
2008-12-29 17:32:53 +01:00
|
|
|
g_message("setting filesystem charset to ISO-8859-1");
|
2008-11-05 18:21:52 +01:00
|
|
|
path_set_fs_charset("ISO-8859-1");
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-11-05 18:21:52 +01:00
|
|
|
void path_global_finish(void)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2008-11-05 18:21:52 +01:00
|
|
|
g_free(fs_charset);
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
|
|
|
|
2007-12-28 03:56:25 +01:00
|
|
|
char *pfx_dir(char *dst,
|
|
|
|
const char *path, const size_t path_len,
|
|
|
|
const char *pfx, const size_t pfx_len)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2008-12-02 02:22:43 +01:00
|
|
|
if (G_UNLIKELY((pfx_len + path_len + 1) >= MPD_PATH_MAX))
|
2008-12-29 17:32:53 +01:00
|
|
|
g_error("Cannot prefix '%s' to '%s', PATH_MAX: %d",
|
|
|
|
pfx, path, MPD_PATH_MAX);
|
2007-12-28 03:56:25 +01:00
|
|
|
|
|
|
|
/* memmove allows dst == path */
|
|
|
|
memmove(dst + pfx_len + 1, path, path_len + 1);
|
|
|
|
memcpy(dst, pfx, pfx_len);
|
|
|
|
dst[pfx_len] = '/';
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2007-12-28 03:56:25 +01:00
|
|
|
/* this is weird, but directory.c can use it more safely/efficiently */
|
|
|
|
return (dst + pfx_len + 1);
|
2006-07-30 12:31:41 +02:00
|
|
|
}
|