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

View File

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

View File

@ -84,13 +84,13 @@ spl_valid_name(const char *name_utf8)
static const char * static const char *
spl_map(GError **error_r) spl_map(GError **error_r)
{ {
const char *path_fs = map_spl_path(); const Path &path_fs = map_spl_path();
if (path_fs == NULL) if (path_fs.IsNull())
g_set_error_literal(error_r, playlist_quark(), g_set_error_literal(error_r, playlist_quark(),
PLAYLIST_RESULT_DISABLED, PLAYLIST_RESULT_DISABLED,
"Stored playlists are disabled"); "Stored playlists are disabled");
return path_fs; return path_fs.c_str();
} }
static bool 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)); assert(spl_valid_name(uri));
const char *playlist_directory_fs = map_spl_path(); const Path &playlist_directory_fs = map_spl_path();
if (playlist_directory_fs == NULL) if (playlist_directory_fs.IsNull())
return NULL; 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 = struct playlist_provider *playlist =
playlist_open_path(path_fs, mutex, cond, is_r); 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 enum playlist_result
spl_save_queue(const char *name_utf8, const struct queue *queue) 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; return PLAYLIST_RESULT_DISABLED;
if (!spl_valid_name(name_utf8)) if (!spl_valid_name(name_utf8))