aboutsummaryrefslogtreecommitdiffstats
path: root/src/path.c
diff options
context:
space:
mode:
authorEric Wong <normalperson@yhbt.net>2007-12-28 02:56:25 +0000
committerEric Wong <normalperson@yhbt.net>2007-12-28 02:56:25 +0000
commitb79f6b882a70526ec2e9fc231e0baeebdfef1e52 (patch)
tree719912135d059476070834c3c62ae940955e9e20 /src/path.c
parent0d26248a0d8cb2e5cf6be5a438a18b16793bbb63 (diff)
downloadmpd-b79f6b882a70526ec2e9fc231e0baeebdfef1e52.tar.gz
mpd-b79f6b882a70526ec2e9fc231e0baeebdfef1e52.tar.xz
mpd-b79f6b882a70526ec2e9fc231e0baeebdfef1e52.zip
Merge branches/ew r7104
thread-safety work in preparation for rewrite to use pthreads Expect no regressions against trunk (r7078), possibly minor performance improvements in update (due to fewer heap allocations), but increased stack usage. Applied the following patches: * maxpath_str for reentrancy (temporary fix, reverted) * path: start working on thread-safe variants of these methods * Re-entrancy work on path/character-set conversions * directory.c: exploreDirectory() use reentrant functions here * directory/update: more use of reentrant functions + cleanups * string_toupper: a strdup-less version of strDupToUpper * get_song_url: a static-variable-free version of getSongUrl() * Use reentrant/thread-safe get_song_url everywhere * replace rmp2amp with the reentrant version, rmp2amp_r * Get rid of the non-reentrant/non-thread-safe rpp2app, too. * buffer2array: assert strdup() returns a usable value in unit tests * replace utf8ToFsCharset and fsCharsetToUtf8 with thread-safe variants * fix storing playlists w/o absolute paths * parent_path(), a reentrant version of parentPath() * parentPath => parent_path for reentrancy and thread-safety * allow "make test" to automatically run embedded unit tests * remove convStrDup() and maxpath_str() * use MPD_PATH_MAX everywhere instead of MAXPATHLEN * path: get rid of appendSlash, pfx_path and just use pfx_dir * get_song_url: fix the ability to play songs in the top-level music_directory git-svn-id: https://svn.musicpd.org/mpd/trunk@7106 09075e82-0dd4-0310-85a5-a0d7c8717e4f
Diffstat (limited to '')
-rw-r--r--src/path.c118
1 files changed, 44 insertions, 74 deletions
diff --git a/src/path.c b/src/path.c
index 134a0701c..c02103331 100644
--- a/src/path.c
+++ b/src/path.c
@@ -40,39 +40,25 @@
const char *musicDir;
static const char *playlistDir;
+static size_t music_dir_len;
+static size_t playlist_dir_len;
static char *fsCharset;
-static char *pathConvCharset(char *to, char *from, char *str, char *ret)
+static char *path_conv_charset(char *dest, char *to, char *from, char *str)
{
- if (ret)
- free(ret);
- return setCharSetConversion(to, from) ? NULL : convStrDup(str);
+ return setCharSetConversion(to, from) ? NULL : char_conv_str(dest, str);
}
-char *fsCharsetToUtf8(char *str)
+char *fs_charset_to_utf8(char *dst, char *str)
{
- static char *ret;
-
- ret = pathConvCharset("UTF-8", fsCharset, str, ret);
-
- if (ret && !validUtf8String(ret)) {
- free(ret);
- ret = NULL;
- }
-
- return ret;
+ char *ret = path_conv_charset(dst, "UTF-8", fsCharset, str);
+ return (ret && !validUtf8String(ret)) ? NULL : ret;
}
-char *utf8ToFsCharset(char *str)
+char *utf8_to_fs_charset(char *dst, char *str)
{
- static char *ret;
-
- ret = pathConvCharset(fsCharset, "UTF-8", str, ret);
-
- if (!ret)
- ret = xstrdup(str);
-
- return ret;
+ char *ret = path_conv_charset(dst, fsCharset, "UTF-8", str);
+ return ret ? ret : strcpy(dst, str);
}
void setFsCharset(char *charset)
@@ -111,23 +97,6 @@ char *getFsCharset(void)
return fsCharset;
}
-static char *appendSlash(char **path)
-{
- char *temp = *path;
- int len = strlen(temp);
-
- if (temp[len - 1] != '/') {
- temp = xmalloc(len + 2);
- memset(temp, 0, len + 2);
- memcpy(temp, *path, len);
- temp[len] = '/';
- free(*path);
- *path = temp;
- }
-
- return temp;
-}
-
void initPaths(void)
{
ConfigParam *musicParam = parseConfigFilePath(CONF_MUSIC_DIR, 1);
@@ -138,8 +107,11 @@ void initPaths(void)
char *originalLocale;
DIR *dir;
- musicDir = appendSlash(&(musicParam->value));
- playlistDir = appendSlash(&(playlistParam->value));
+ musicDir = xstrdup(musicParam->value);
+ playlistDir = xstrdup(playlistParam->value);
+
+ music_dir_len = strlen(musicDir);
+ playlist_dir_len = strlen(playlistDir);
if ((dir = opendir(playlistDir)) == NULL) {
FATAL("cannot open %s \"%s\" (config line %i): %s\n",
@@ -205,36 +177,35 @@ void finishPaths(void)
fsCharset = NULL;
}
-static char *pfx_path(const char *path, const char *pfx, const size_t pfx_len)
+char *pfx_dir(char *dst,
+ const char *path, const size_t path_len,
+ const char *pfx, const size_t pfx_len)
{
- 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;
- }
+ if (mpd_unlikely((pfx_len + path_len + 1) >= MPD_PATH_MAX))
+ FATAL("Cannot prefix '%s' to '%s', PATH_MAX: %d\n",
+ pfx, path, MPD_PATH_MAX);
+
+ /* memmove allows dst == path */
+ memmove(dst + pfx_len + 1, path, path_len + 1);
+ memcpy(dst, pfx, pfx_len);
+ dst[pfx_len] = '/';
- /* unlikely, return an empty string because truncating would
- * also be wrong... break early and break loudly (the system
- * headers are likely screwed, not mpd) */
- ERROR("Cannot prefix '%s' to '%s', max: %d\n", pfx, path, MAXPATHLEN);
- ret[0] = '\0';
- return ret;
+ /* this is weird, but directory.c can use it more safely/efficiently */
+ return (dst + pfx_len + 1);
}
-char *rmp2amp(char *relativePath)
+char *rmp2amp_r(char *dst, const char *rel_path)
{
- size_t pfx_len = strlen(musicDir);
- return pfx_path(relativePath, musicDir, pfx_len);
+ pfx_dir(dst, rel_path, strlen(rel_path),
+ (const char *)musicDir, music_dir_len);
+ return dst;
}
-char *rpp2app(char *relativePath)
+char *rpp2app_r(char *dst, const char *rel_path)
{
- size_t pfx_len = strlen(playlistDir);
- return pfx_path(relativePath, playlistDir, pfx_len);
+ pfx_dir(dst, rel_path, strlen(rel_path),
+ (const char *)playlistDir, playlist_dir_len);
+ return dst;
}
/* this is actually like strlcpy (OpenBSD), but we don't actually want to
@@ -244,29 +215,28 @@ void pathcpy_trunc(char *dest, const char *src)
{
size_t len = strlen(src);
- if (mpd_unlikely(len > MAXPATHLEN))
- len = MAXPATHLEN;
+ if (mpd_unlikely(len >= MPD_PATH_MAX))
+ len = MPD_PATH_MAX - 1;
memcpy(dest, src, len);
dest[len] = '\0';
}
-char *parentPath(char *path)
+char *parent_path(char *path_max_tmp, const char *path)
{
- static char parentPath[MAXPATHLEN+1];
char *c;
- pathcpy_trunc(parentPath, path);
- c = strrchr(parentPath,'/');
+ pathcpy_trunc(path_max_tmp, path);
+ c = strrchr(path_max_tmp,'/');
if (c == NULL)
- parentPath[0] = '\0';
+ path_max_tmp[0] = '\0';
else {
- while ((parentPath <= c) && *(--c) == '/') /* nothing */
+ while ((path_max_tmp <= c) && *(--c) == '/') /* nothing */
;
c[1] = '\0';
}
- return parentPath;
+ return path_max_tmp;
}
char *sanitizePathDup(char *path)