diff options
-rw-r--r-- | src/main.c | 3 | ||||
-rw-r--r-- | src/mapper.c | 51 | ||||
-rw-r--r-- | src/mapper.h | 4 | ||||
-rw-r--r-- | src/path.c | 20 | ||||
-rw-r--r-- | src/path.h | 5 |
5 files changed, 52 insertions, 31 deletions
diff --git a/src/main.c b/src/main.c index 1a7e73254..21efb871f 100644 --- a/src/main.c +++ b/src/main.c @@ -26,6 +26,7 @@ #include "listen.h" #include "conf.h" #include "path.h" +#include "mapper.h" #include "playerData.h" #include "outputBuffer.h" #include "decoder_thread.h" @@ -410,6 +411,7 @@ int main(int argc, char *argv[]) open_log_files(options.stdOutput); initPaths(); + mapper_init(); initPermissions(); initPlaylist(); decoder_plugin_init_all(); @@ -477,6 +479,7 @@ int main(int argc, char *argv[]) finishAudioDriver(); finishAudioConfig(); finishVolume(); + mapper_finish(); finishPaths(); finishPermissions(); dc_deinit(); diff --git a/src/mapper.c b/src/mapper.c index 392211214..b1ac6f86c 100644 --- a/src/mapper.c +++ b/src/mapper.c @@ -24,13 +24,54 @@ #include "directory.h" #include "song.h" #include "path.h" +#include "conf.h" + +#include <sys/types.h> +#include <sys/stat.h> +#include <unistd.h> +#include <glib.h> + +static char *music_dir; +static size_t music_dir_length; + +void mapper_init(void) +{ + ConfigParam *music_dir_param = parseConfigFilePath(CONF_MUSIC_DIR, 1); + int ret; + struct stat st; + + music_dir = g_strdup(music_dir_param->value); + music_dir_length = strlen(music_dir); + + ret = stat(music_dir, &st); + if (ret < 0) + g_error("failed to stat music directory \"%s\" (config line %i): %s\n", + music_dir_param->value, music_dir_param->line, + strerror(errno)); + else if (!S_ISDIR(st.st_mode)) + g_error("music directory is not a directory: \"%s\" (config line %i)\n", + music_dir_param->value, music_dir_param->line); +} + +void mapper_finish(void) +{ + g_free(music_dir); +} + +static char * +rmp2amp_r(char *dst, const char *rel_path) +{ + pfx_dir(dst, rel_path, strlen(rel_path), + (const char *)music_dir, music_dir_length); + return dst; +} const char * map_directory_fs(const struct directory *directory, char *buffer) { const char *dirname = directory_get_path(directory); if (isRootDirectory(dirname)) - return musicDir; + return music_dir; return rmp2amp_r(buffer, utf8_to_fs_charset(buffer, dirname)); } @@ -63,12 +104,10 @@ map_song_fs(const struct song *song, char *buffer) const char * map_fs_to_utf8(const char *path_fs, char *buffer) { - size_t music_path_length = strlen(musicDir); - - if (strncmp(path_fs, musicDir, music_path_length) == 0 && - path_fs[music_path_length] == '/') + if (strncmp(path_fs, music_dir, music_dir_length) == 0 && + path_fs[music_dir_length] == '/') /* remove musicDir prefix */ - path_fs += music_path_length; + path_fs += music_dir_length; else if (path_fs[0] == '/') /* not within musicDir */ return NULL; diff --git a/src/mapper.h b/src/mapper.h index 2c21a68e8..ff6603d1e 100644 --- a/src/mapper.h +++ b/src/mapper.h @@ -26,6 +26,10 @@ struct directory; struct song; +void mapper_init(void); + +void mapper_finish(void); + /** * Determines the file system path of a directory object. * diff --git a/src/path.c b/src/path.c index 2a78870c0..159d923da 100644 --- a/src/path.c +++ b/src/path.c @@ -33,9 +33,7 @@ #include <glib.h> -const char *musicDir; static const char *playlistDir; -static size_t music_dir_len; static size_t playlist_dir_len; static char *fsCharset; @@ -100,7 +98,6 @@ const char *getFsCharset(void) void initPaths(void) { - ConfigParam *musicParam = parseConfigFilePath(CONF_MUSIC_DIR, 1); ConfigParam *playlistParam = parseConfigFilePath(CONF_PLAYLIST_DIR, 1); ConfigParam *fsCharsetParam = getConfigParam(CONF_FS_CHARSET); @@ -108,10 +105,7 @@ void initPaths(void) char *originalLocale; DIR *dir; - musicDir = xstrdup(musicParam->value); playlistDir = xstrdup(playlistParam->value); - - music_dir_len = strlen(musicDir); playlist_dir_len = strlen(playlistDir); if ((dir = opendir(playlistDir)) == NULL) { @@ -121,13 +115,6 @@ void initPaths(void) } else closedir(dir); - if ((dir = opendir(musicDir)) == NULL) { - ERROR("cannot open %s \"%s\" (config line %i): %s\n", - CONF_MUSIC_DIR, musicParam->value, - musicParam->line, strerror(errno)); - } else - closedir(dir); - if (fsCharsetParam) { charset = xstrdup(fsCharsetParam->value); } @@ -195,13 +182,6 @@ char *pfx_dir(char *dst, return (dst + pfx_len + 1); } -char *rmp2amp_r(char *dst, const char *rel_path) -{ - pfx_dir(dst, rel_path, strlen(rel_path), - (const char *)musicDir, music_dir_len); - return dst; -} - char *rpp2app_r(char *dst, const char *rel_path) { pfx_dir(dst, rel_path, strlen(rel_path), diff --git a/src/path.h b/src/path.h index 2c5801528..5230ce37c 100644 --- a/src/path.h +++ b/src/path.h @@ -31,8 +31,6 @@ # endif #endif -extern const char *musicDir; - void initPaths(void); void finishPaths(void); @@ -57,9 +55,6 @@ char *pfx_dir(char *dst, const char *path, const size_t path_len, const char *pfx, const size_t pfx_len); -/* relative music path to absolute music path */ -char *rmp2amp_r(char *dst, const char *rel_path); - /* relative playlist path to absolute playlist path */ char *rpp2app_r(char *dst, const char *rel_path); |