Mapper: improve usage of Path class

This commit is contained in:
Denis Krjuchkov 2013-01-24 00:38:09 +06:00 committed by Max Kellermann
parent a9b62a2ece
commit a3ee26da64
6 changed files with 44 additions and 46 deletions

View File

@ -267,7 +267,7 @@ mpd_inotify_callback(int wd, unsigned mask,
(mask & IN_ISDIR) != 0) {
/* a sub directory was changed: register those in
inotify */
const char *root = mapper_get_music_directory_fs();
const char *root = mapper_get_music_directory_fs().c_str();
const char *path_fs;
char *allocated = NULL;
@ -309,8 +309,8 @@ mpd_inotify_init(unsigned max_depth)
g_debug("initializing inotify");
const char *path = mapper_get_music_directory_fs();
if (path == NULL) {
const Path &path = mapper_get_music_directory_fs();
if (path.IsNull()) {
g_debug("no music directory configured");
return;
}
@ -326,8 +326,9 @@ mpd_inotify_init(unsigned max_depth)
inotify_max_depth = max_depth;
inotify_root.name = g_strdup(path);
inotify_root.descriptor = inotify_source->Add(path, IN_MASK, &error);
inotify_root.name = g_strdup(path.c_str());
inotify_root.descriptor = inotify_source->Add(path.c_str(),
IN_MASK, &error);
if (inotify_root.descriptor < 0) {
g_warning("%s", error->message);
g_error_free(error);
@ -339,7 +340,7 @@ mpd_inotify_init(unsigned max_depth)
inotify_directories = g_tree_new(compare);
tree_add_watch_directory(&inotify_root);
recursive_watch_subdirectories(&inotify_root, path, 0);
recursive_watch_subdirectories(&inotify_root, path.c_str(), 0);
inotify_queue = new InotifyQueue(*main_loop);

View File

@ -26,6 +26,8 @@
#include "Directory.hxx"
#include "song.h"
#include "fs/Path.hxx"
#include "fs/FileSystem.hxx"
#include "fs/DirectoryReader.hxx"
#include <glib.h>
@ -46,14 +48,14 @@ static size_t music_dir_utf8_length;
* The absolute path of the music directory encoded in the filesystem
* character set.
*/
static char *music_dir_fs;
static Path music_dir_fs = Path::Null();
static size_t music_dir_fs_length;
/**
* The absolute path of the playlist directory encoded in the
* filesystem character set.
*/
static char *playlist_dir_fs;
static Path playlist_dir_fs = Path::Null();
/**
* Duplicate a string, chop all trailing slashes.
@ -70,33 +72,30 @@ strdup_chop_slash(const char *path_fs)
}
static void
check_directory(const char *path)
check_directory(const char *path_utf8, const Path &path_fs)
{
struct stat st;
if (stat(path, &st) < 0) {
if (!StatFile(path_fs, st)) {
g_warning("Failed to stat directory \"%s\": %s",
path, g_strerror(errno));
path_utf8, g_strerror(errno));
return;
}
if (!S_ISDIR(st.st_mode)) {
g_warning("Not a directory: %s", path);
g_warning("Not a directory: %s", path_utf8);
return;
}
#ifndef WIN32
char *x = g_build_filename(path, ".", NULL);
if (stat(x, &st) < 0 && errno == EACCES)
const Path x = Path::Build(path_fs, ".");
if (!StatFile(x, st) && errno == EACCES)
g_warning("No permission to traverse (\"execute\") directory: %s",
path);
g_free(x);
path_utf8);
#endif
DIR *dir = opendir(path);
if (dir != NULL)
closedir(dir);
else if (errno == EACCES)
g_warning("No permission to read directory: %s", path);
const DirectoryReader reader(path_fs);
if (reader.Failed() && errno == EACCES)
g_warning("No permission to read directory: %s", path_utf8);
}
static void
@ -105,16 +104,16 @@ mapper_set_music_dir(const char *path_utf8)
music_dir_utf8 = strdup_chop_slash(path_utf8);
music_dir_utf8_length = strlen(music_dir_utf8);
music_dir_fs = utf8_to_fs_charset(music_dir_utf8);
check_directory(music_dir_fs);
music_dir_fs_length = strlen(music_dir_fs);
music_dir_fs = Path::FromUTF8(path_utf8);
check_directory(path_utf8, music_dir_fs);
music_dir_fs_length = music_dir_fs.length();
}
static void
mapper_set_playlist_dir(const char *path_utf8)
{
playlist_dir_fs = utf8_to_fs_charset(path_utf8);
check_directory(playlist_dir_fs);
playlist_dir_fs = Path::FromUTF8(path_utf8);
check_directory(path_utf8, playlist_dir_fs);
}
void mapper_init(const char *_music_dir, const char *_playlist_dir)
@ -129,8 +128,6 @@ void mapper_init(const char *_music_dir, const char *_playlist_dir)
void mapper_finish(void)
{
g_free(music_dir_utf8);
g_free(music_dir_fs);
g_free(playlist_dir_fs);
}
const char *
@ -139,7 +136,7 @@ mapper_get_music_directory_utf8(void)
return music_dir_utf8;
}
const char *
const Path &
mapper_get_music_directory_fs(void)
{
return music_dir_fs;
@ -162,7 +159,7 @@ map_uri_fs(const char *uri)
assert(uri != NULL);
assert(*uri != '/');
if (music_dir_fs == NULL)
if (music_dir_fs.IsNull())
return Path::Null();
const Path uri_fs = Path::FromUTF8(uri);
@ -176,10 +173,10 @@ Path
map_directory_fs(const Directory *directory)
{
assert(music_dir_utf8 != NULL);
assert(music_dir_fs != NULL);
assert(!music_dir_fs.IsNull());
if (directory->IsRoot())
return Path::FromFS(music_dir_fs);
return music_dir_fs;
return map_uri_fs(directory->GetPath());
}
@ -188,7 +185,7 @@ Path
map_directory_child_fs(const Directory *directory, const char *name)
{
assert(music_dir_utf8 != NULL);
assert(music_dir_fs != NULL);
assert(!music_dir_fs.IsNull());
/* check for invalid or unauthorized base names */
if (*name == 0 || strchr(name, '/') != NULL ||
@ -237,8 +234,8 @@ map_song_fs(const struct song *song)
char *
map_fs_to_utf8(const char *path_fs)
{
if (music_dir_fs != NULL &&
strncmp(path_fs, music_dir_fs, music_dir_fs_length) == 0 &&
if (!music_dir_fs.IsNull() &&
strncmp(path_fs, music_dir_fs.c_str(), music_dir_fs_length) == 0 &&
G_IS_DIR_SEPARATOR(path_fs[music_dir_fs_length]))
/* remove musicDir prefix */
path_fs += music_dir_fs_length + 1;
@ -252,7 +249,7 @@ map_fs_to_utf8(const char *path_fs)
return fs_charset_to_utf8(path_fs);
}
const char *
const Path &
map_spl_path(void)
{
return playlist_dir_fs;
@ -261,7 +258,7 @@ map_spl_path(void)
Path
map_spl_utf8_to_fs(const char *name)
{
if (playlist_dir_fs == NULL)
if (playlist_dir_fs.IsNull())
return Path::Null();
char *filename_utf8 = g_strconcat(name, PLAYLIST_FILE_SUFFIX, NULL);

View File

@ -49,7 +49,7 @@ mapper_get_music_directory_utf8(void);
* filesystem character set.
*/
gcc_const
const char *
const Path &
mapper_get_music_directory_fs(void);
/**
@ -128,7 +128,7 @@ map_fs_to_utf8(const char *path_fs);
* Returns the playlist directory.
*/
gcc_const
const char *
const Path &
map_spl_path(void);
/**

View File

@ -84,13 +84,13 @@ spl_valid_name(const char *name_utf8)
static const char *
spl_map(GError **error_r)
{
const char *path_fs = map_spl_path();
if (path_fs == NULL)
const Path &path_fs = map_spl_path();
if (path_fs.IsNull())
g_set_error_literal(error_r, playlist_quark(),
PLAYLIST_RESULT_DISABLED,
"Stored playlists are disabled");
return path_fs;
return path_fs.c_str();
}
static bool

View File

@ -56,11 +56,11 @@ playlist_open_in_playlist_dir(const char *uri, GMutex *mutex, GCond *cond,
assert(spl_valid_name(uri));
const char *playlist_directory_fs = map_spl_path();
if (playlist_directory_fs == NULL)
const Path &playlist_directory_fs = map_spl_path();
if (playlist_directory_fs.IsNull())
return NULL;
path_fs = g_build_filename(playlist_directory_fs, uri, NULL);
path_fs = g_build_filename(playlist_directory_fs.c_str(), uri, NULL);
struct playlist_provider *playlist =
playlist_open_path(path_fs, mutex, cond, is_r);

View File

@ -65,7 +65,7 @@ playlist_print_uri(FILE *file, const char *uri)
enum playlist_result
spl_save_queue(const char *name_utf8, const struct queue *queue)
{
if (map_spl_path() == NULL)
if (map_spl_path().IsNull())
return PLAYLIST_RESULT_DISABLED;
if (!spl_valid_name(name_utf8))