From d8e877e3355d0858c78a6d9a7060a6683024dd30 Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Fri, 31 Oct 2008 16:47:14 +0100 Subject: path: moved playlist_dir to mapper.c Added the function map_spl_utf8_to_fs() which replaces utf8_to_fs_playlist_path(). --- src/mapper.c | 38 ++++++++++++++++++++++++++++++++++++++ src/mapper.h | 15 +++++++++++++++ src/path.c | 28 ---------------------------- src/path.h | 9 --------- src/playlist.c | 7 ++++--- src/playlist.h | 1 - src/stored_playlist.c | 41 ++++++++++++++++++++++++----------------- 7 files changed, 81 insertions(+), 58 deletions(-) (limited to 'src') diff --git a/src/mapper.c b/src/mapper.c index f0a546eb2..a21b42d92 100644 --- a/src/mapper.c +++ b/src/mapper.c @@ -34,9 +34,13 @@ static char *music_dir; static size_t music_dir_length; +static char *playlist_dir; +static size_t playlist_dir_length; + void mapper_init(void) { ConfigParam *music_dir_param = parseConfigFilePath(CONF_MUSIC_DIR, 1); + ConfigParam *playlist_dir_param = parseConfigFilePath(CONF_PLAYLIST_DIR, 1); int ret; struct stat st; @@ -51,11 +55,24 @@ void mapper_init(void) else if (!S_ISDIR(st.st_mode)) g_warning("music directory is not a directory: \"%s\" (config line %i)\n", music_dir_param->value, music_dir_param->line); + + playlist_dir = g_strdup(playlist_dir_param->value); + playlist_dir_length = strlen(playlist_dir); + + ret = stat(playlist_dir, &st); + if (ret < 0) + g_warning("failed to stat playlist directory \"%s\" (config line %i): %s\n", + playlist_dir_param->value, playlist_dir_param->line, + strerror(errno)); + else if (!S_ISDIR(st.st_mode)) + g_warning("playlist directory is not a directory: \"%s\" (config line %i)\n", + playlist_dir_param->value, playlist_dir_param->line); } void mapper_finish(void) { g_free(music_dir); + g_free(playlist_dir); } static char * @@ -117,3 +134,24 @@ map_fs_to_utf8(const char *path_fs, char *buffer) return fs_charset_to_utf8(buffer, path_fs); } + +static char *rpp2app_r(char *dst, const char *rel_path) +{ + pfx_dir(dst, rel_path, strlen(rel_path), + playlist_dir, playlist_dir_length); + return dst; +} + +const char * +map_spl_path(void) +{ + return playlist_dir; +} + +const char * +map_spl_utf8_to_fs(const char *name, char *buffer) +{ + rpp2app_r(buffer, utf8_to_fs_charset(buffer, name)); + g_strlcat(buffer, "." PLAYLIST_FILE_SUFFIX, MPD_PATH_MAX); + return buffer; +} diff --git a/src/mapper.h b/src/mapper.h index 03835a6eb..e8249b27d 100644 --- a/src/mapper.h +++ b/src/mapper.h @@ -23,6 +23,8 @@ #ifndef MPD_MAPPER_H #define MPD_MAPPER_H +#define PLAYLIST_FILE_SUFFIX "m3u" + struct directory; struct song; @@ -75,4 +77,17 @@ map_song_fs(const struct song *song, char *buffer); const char * map_fs_to_utf8(const char *path_fs, char *buffer); +/** + * Returns the playlist directory. + */ +const char * +map_spl_path(void); + +/** + * Maps a playlist name (without the ".m3u" suffix) to a file system + * path. + */ +const char * +map_spl_utf8_to_fs(const char *name, char *buffer); + #endif diff --git a/src/path.c b/src/path.c index 0494a0985..62a2362b5 100644 --- a/src/path.c +++ b/src/path.c @@ -32,8 +32,6 @@ #include -static const char *playlistDir; -static size_t playlist_dir_len; static char *fsCharset; char *fs_charset_to_utf8(char *dst, const char *str) @@ -99,22 +97,10 @@ const char *getFsCharset(void) void initPaths(void) { - ConfigParam *playlistParam = parseConfigFilePath(CONF_PLAYLIST_DIR, 1); ConfigParam *fsCharsetParam = getConfigParam(CONF_FS_CHARSET); char *charset = NULL; char *originalLocale; - DIR *dir; - - playlistDir = xstrdup(playlistParam->value); - playlist_dir_len = strlen(playlistDir); - - if ((dir = opendir(playlistDir)) == NULL) { - ERROR("cannot open %s \"%s\" (config line %i): %s\n", - CONF_PLAYLIST_DIR, playlistParam->value, - playlistParam->line, strerror(errno)); - } else - closedir(dir); if (fsCharsetParam) { charset = xstrdup(fsCharsetParam->value); @@ -183,13 +169,6 @@ char *pfx_dir(char *dst, return (dst + pfx_len + 1); } -char *rpp2app_r(char *dst, const char *rel_path) -{ - pfx_dir(dst, rel_path, strlen(rel_path), - (const char *)playlistDir, playlist_dir_len); - return dst; -} - char *sanitizePathDup(const char *path) { int len = strlen(path) + 1; @@ -229,10 +208,3 @@ char *sanitizePathDup(const char *path) return xrealloc(ret, len + 1); } - -void utf8_to_fs_playlist_path(char *path_max_tmp, const char *utf8path) -{ - utf8_to_fs_charset(path_max_tmp, utf8path); - rpp2app_r(path_max_tmp, path_max_tmp); - strncat(path_max_tmp, "." PLAYLIST_FILE_SUFFIX, MPD_PATH_MAX - 1); -} diff --git a/src/path.h b/src/path.h index 013da1373..e341169b0 100644 --- a/src/path.h +++ b/src/path.h @@ -55,16 +55,7 @@ char *pfx_dir(char *dst, const char *path, const size_t path_len, const char *pfx, const size_t pfx_len); -/* relative playlist path to absolute playlist path */ -char *rpp2app_r(char *dst, const char *rel_path); - /* strips extra "///" and leading "/" and trailing "/" */ char *sanitizePathDup(const char *path); -/* - * converts a path passed from a client into an absolute FS path. - * paths passed by clients do NOT have file suffixes in them - */ -void utf8_to_fs_playlist_path(char *path_max_tmp, const char *utf8path); - #endif diff --git a/src/playlist.c b/src/playlist.c index 58db673aa..61a0bb56b 100644 --- a/src/playlist.c +++ b/src/playlist.c @@ -1190,15 +1190,16 @@ enum playlist_result savePlaylist(const char *utf8file) FILE *fp; struct stat sb; char path_max_tmp[MPD_PATH_MAX]; + const char *path; if (!is_valid_playlist_name(utf8file)) return PLAYLIST_RESULT_BAD_NAME; - utf8_to_fs_playlist_path(path_max_tmp, utf8file); - if (!stat(path_max_tmp, &sb)) + path = map_spl_utf8_to_fs(utf8file, path_max_tmp); + if (!stat(path, &sb)) return PLAYLIST_RESULT_LIST_EXISTS; - while (!(fp = fopen(path_max_tmp, "w")) && errno == EINTR); + while (!(fp = fopen(path, "w")) && errno == EINTR); if (fp == NULL) return PLAYLIST_RESULT_ERRNO; diff --git a/src/playlist.h b/src/playlist.h index 419b169ca..34da9f9af 100644 --- a/src/playlist.h +++ b/src/playlist.h @@ -24,7 +24,6 @@ #include #include -#define PLAYLIST_FILE_SUFFIX "m3u" #define PLAYLIST_COMMENT '#' struct client; diff --git a/src/stored_playlist.c b/src/stored_playlist.c index 28975c3a8..cadd2ccdf 100644 --- a/src/stored_playlist.c +++ b/src/stored_playlist.c @@ -70,13 +70,12 @@ load_playlist_info(const char *parent_path_fs, const char *name_fs) GPtrArray * spl_list(void) { - char parent_path_fs[MPD_PATH_MAX]; + const char *parent_path_fs = map_spl_path(); DIR *dir; struct dirent *ent; GPtrArray *list; struct stored_playlist_info *playlist; - rpp2app_r(parent_path_fs, ""); dir = opendir(parent_path_fs); if (dir == NULL) return NULL; @@ -111,12 +110,13 @@ spl_save(GPtrArray *list, const char *utf8path) { FILE *file; char path_max_tmp[MPD_PATH_MAX]; + const char *path_fs; assert(utf8path != NULL); - utf8_to_fs_playlist_path(path_max_tmp, utf8path); + path_fs = map_spl_utf8_to_fs(utf8path, path_max_tmp); - while (!(file = fopen(path_max_tmp, "w")) && errno == EINTR); + while (!(file = fopen(path_fs, "w")) && errno == EINTR); if (file == NULL) return PLAYLIST_RESULT_ERRNO; @@ -136,12 +136,14 @@ spl_load(const char *utf8path) GPtrArray *list; char buffer[MPD_PATH_MAX]; char path_max_tmp[MPD_PATH_MAX]; + const char *path_fs; if (!is_valid_playlist_name(utf8path)) return NULL; - utf8_to_fs_playlist_path(path_max_tmp, utf8path); - while (!(file = fopen(path_max_tmp, "r")) && errno == EINTR); + path_fs = map_spl_utf8_to_fs(utf8path, path_max_tmp); + + while (!(file = fopen(path_fs, "r")) && errno == EINTR); if (file == NULL) return NULL; @@ -248,14 +250,15 @@ enum playlist_result spl_clear(const char *utf8path) { char filename[MPD_PATH_MAX]; + const char *path_fs; FILE *file; if (!is_valid_playlist_name(utf8path)) return PLAYLIST_RESULT_BAD_NAME; - utf8_to_fs_playlist_path(filename, utf8path); + path_fs = map_spl_utf8_to_fs(utf8path, filename); - while (!(file = fopen(filename, "w")) && errno == EINTR); + while (!(file = fopen(path_fs, "w")) && errno == EINTR); if (file == NULL) return PLAYLIST_RESULT_ERRNO; @@ -269,10 +272,11 @@ enum playlist_result spl_delete(const char *name_utf8) { char filename[MPD_PATH_MAX]; + const char *path_fs; - utf8_to_fs_playlist_path(filename, name_utf8); + path_fs = map_spl_utf8_to_fs(name_utf8, filename); - if (unlink(filename) < 0) + if (unlink(path_fs) < 0) return errno == ENOENT ? PLAYLIST_RESULT_NO_SUCH_LIST : PLAYLIST_RESULT_ERRNO; @@ -312,12 +316,14 @@ spl_append_song(const char *utf8path, struct song *song) FILE *file; struct stat st; char path_max_tmp[MPD_PATH_MAX]; + const char *path_fs; if (!is_valid_playlist_name(utf8path)) return PLAYLIST_RESULT_BAD_NAME; - utf8_to_fs_playlist_path(path_max_tmp, utf8path); - while (!(file = fopen(path_max_tmp, "a")) && errno == EINTR); + path_fs = map_spl_utf8_to_fs(utf8path, path_max_tmp); + + while (!(file = fopen(path_fs, "a")) && errno == EINTR); if (file == NULL) { int save_errno = errno; while (fclose(file) != 0 && errno == EINTR); @@ -373,21 +379,22 @@ spl_rename(const char *utf8from, const char *utf8to) struct stat st; char from[MPD_PATH_MAX]; char to[MPD_PATH_MAX]; + const char *from_path_fs, *to_path_fs; if (!is_valid_playlist_name(utf8from) || !is_valid_playlist_name(utf8to)) return PLAYLIST_RESULT_BAD_NAME; - utf8_to_fs_playlist_path(from, utf8from); - utf8_to_fs_playlist_path(to, utf8to); + from_path_fs = map_spl_utf8_to_fs(utf8from, from); + to_path_fs = map_spl_utf8_to_fs(utf8to, to); - if (stat(from, &st) != 0) + if (stat(from_path_fs, &st) != 0) return PLAYLIST_RESULT_NO_SUCH_LIST; - if (stat(to, &st) == 0) + if (stat(to_path_fs, &st) == 0) return PLAYLIST_RESULT_LIST_EXISTS; - if (rename(from, to) < 0) + if (rename(from_path_fs, to_path_fs) < 0) return PLAYLIST_RESULT_ERRNO; idle_add(IDLE_STORED_PLAYLIST); -- cgit v1.2.3