Commit e98e2a0b authored by Denis Krjuchkov's avatar Denis Krjuchkov

Path::FromUTF8() returns nulled instance on error, add error handling where required

parent 943064bb
...@@ -103,6 +103,11 @@ Partition *global_partition; ...@@ -103,6 +103,11 @@ Partition *global_partition;
static StateFile *state_file; static StateFile *state_file;
static inline GQuark main_quark()
{
return g_quark_from_static_string ("main");
}
static bool static bool
glue_daemonize_init(const struct options *options, GError **error_r) glue_daemonize_init(const struct options *options, GError **error_r)
{ {
...@@ -144,7 +149,10 @@ glue_mapper_init(GError **error_r) ...@@ -144,7 +149,10 @@ glue_mapper_init(GError **error_r)
if (music_dir == NULL) if (music_dir == NULL)
music_dir = g_strdup(g_get_user_special_dir(G_USER_DIRECTORY_MUSIC)); music_dir = g_strdup(g_get_user_special_dir(G_USER_DIRECTORY_MUSIC));
mapper_init(music_dir, playlist_dir); if (!mapper_init(music_dir, playlist_dir, &error)) {
g_propagate_error(error_r, error);
return false;
}
g_free(music_dir); g_free(music_dir);
g_free(playlist_dir); g_free(playlist_dir);
...@@ -236,9 +244,18 @@ glue_state_file_init(GError **error_r) ...@@ -236,9 +244,18 @@ glue_state_file_init(GError **error_r)
return true; return true;
} }
state_file = new StateFile(Path::FromUTF8(path), Path path_fs = Path::FromUTF8(path);
*global_partition, *main_loop);
g_free(path); g_free(path);
if (path_fs.IsNull()) {
g_set_error(error_r, main_quark(), 0,
"Failed to convert state file path to FS encoding");
return false;
}
state_file = new StateFile(std::move(path_fs),
*global_partition, *main_loop);
state_file->Read(); state_file->Read();
return true; return true;
} }
......
...@@ -57,6 +57,12 @@ static size_t music_dir_fs_length; ...@@ -57,6 +57,12 @@ static size_t music_dir_fs_length;
*/ */
static Path playlist_dir_fs = Path::Null(); static Path playlist_dir_fs = Path::Null();
static inline GQuark
mapper_quark()
{
return g_quark_from_static_string ("mapper");
}
/** /**
* Duplicate a string, chop all trailing slashes. * Duplicate a string, chop all trailing slashes.
*/ */
...@@ -98,31 +104,52 @@ check_directory(const char *path_utf8, const Path &path_fs) ...@@ -98,31 +104,52 @@ check_directory(const char *path_utf8, const Path &path_fs)
g_warning("No permission to read directory: %s", path_utf8); g_warning("No permission to read directory: %s", path_utf8);
} }
static void static bool
mapper_set_music_dir(const char *path_utf8) mapper_set_music_dir(const char *path_utf8, GError **error_r)
{ {
music_dir_fs = Path::FromUTF8(path_utf8);
if (music_dir_fs.IsNull()) {
g_set_error(error_r, mapper_quark(), 0,
"Failed to convert music path to FS encoding");
return false;
}
music_dir_fs_length = music_dir_fs.length();
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 = Path::FromUTF8(path_utf8);
check_directory(path_utf8, music_dir_fs); check_directory(path_utf8, music_dir_fs);
music_dir_fs_length = music_dir_fs.length();
return true;
} }
static void static bool
mapper_set_playlist_dir(const char *path_utf8) mapper_set_playlist_dir(const char *path_utf8, GError **error_r)
{ {
playlist_dir_fs = Path::FromUTF8(path_utf8); playlist_dir_fs = Path::FromUTF8(path_utf8);
if (playlist_dir_fs.IsNull()) {
g_set_error(error_r, mapper_quark(), 0,
"Failed to convert playlist path to FS encoding");
return false;
}
check_directory(path_utf8, playlist_dir_fs); check_directory(path_utf8, playlist_dir_fs);
return true;
} }
void mapper_init(const char *_music_dir, const char *_playlist_dir) bool mapper_init(const char *_music_dir, const char *_playlist_dir,
GError **error_r)
{ {
if (_music_dir != NULL) if (_music_dir != NULL)
mapper_set_music_dir(_music_dir); if (!mapper_set_music_dir(_music_dir, error_r))
return false;
if (_playlist_dir != NULL) if (_playlist_dir != NULL)
mapper_set_playlist_dir(_playlist_dir); if (!mapper_set_playlist_dir(_playlist_dir, error_r))
return false;
return true;
} }
void mapper_finish(void) void mapper_finish(void)
......
...@@ -33,7 +33,8 @@ class Path; ...@@ -33,7 +33,8 @@ class Path;
struct Directory; struct Directory;
struct song; struct song;
void mapper_init(const char *_music_dir, const char *_playlist_dir); bool mapper_init(const char *_music_dir, const char *_playlist_dir,
GError **error_r);
void mapper_finish(void); void mapper_finish(void);
......
...@@ -46,7 +46,8 @@ playlist_print_song(FILE *file, const struct song *song) ...@@ -46,7 +46,8 @@ playlist_print_song(FILE *file, const struct song *song)
const Path uri_fs = Path::FromUTF8(uri); const Path uri_fs = Path::FromUTF8(uri);
g_free(uri); g_free(uri);
fprintf(file, "%s\n", uri_fs.c_str()); if (!uri_fs.IsNull())
fprintf(file, "%s\n", uri_fs.c_str());
} }
} }
......
...@@ -104,7 +104,7 @@ remove_excluded_from_directory(Directory *directory, ...@@ -104,7 +104,7 @@ remove_excluded_from_directory(Directory *directory,
directory_for_each_child_safe(child, n, directory) { directory_for_each_child_safe(child, n, directory) {
const Path name_fs = Path::FromUTF8(child->GetName()); const Path name_fs = Path::FromUTF8(child->GetName());
if (exclude_list.Check(name_fs.c_str())) { if (name_fs.IsNull() || exclude_list.Check(name_fs.c_str())) {
delete_directory(child); delete_directory(child);
modified = true; modified = true;
} }
...@@ -115,7 +115,7 @@ remove_excluded_from_directory(Directory *directory, ...@@ -115,7 +115,7 @@ remove_excluded_from_directory(Directory *directory,
assert(song->parent == directory); assert(song->parent == directory);
const Path name_fs = Path::FromUTF8(song->uri); const Path name_fs = Path::FromUTF8(song->uri);
if (exclude_list.Check(name_fs.c_str())) { if (name_fs.IsNull() || exclude_list.Check(name_fs.c_str())) {
delete_song(directory, song); delete_song(directory, song);
modified = true; modified = true;
} }
......
...@@ -71,6 +71,12 @@ SimpleDatabase::Configure(const struct config_param *param, GError **error_r) ...@@ -71,6 +71,12 @@ SimpleDatabase::Configure(const struct config_param *param, GError **error_r)
path = Path::FromUTF8(_path); path = Path::FromUTF8(_path);
free(_path); free(_path);
if (path.IsNull()) {
g_set_error(error_r, simple_db_quark(), 0,
"Failed to convert database path to FS encoding");
return false;
}
return true; return true;
} }
......
...@@ -82,9 +82,6 @@ Path Path::FromUTF8(const char *path_utf8) ...@@ -82,9 +82,6 @@ Path Path::FromUTF8(const char *path_utf8)
p = g_convert(path_utf8, -1, p = g_convert(path_utf8, -1,
fs_charset.c_str(), "utf-8", fs_charset.c_str(), "utf-8",
NULL, NULL, NULL); NULL, NULL, NULL);
if (p == NULL)
/* fall back to UTF-8 */
p = g_strdup(path_utf8);
return Path(Donate(), p); return Path(Donate(), p);
} }
......
...@@ -144,10 +144,7 @@ public: ...@@ -144,10 +144,7 @@ public:
/** /**
* Convert a UTF-8 C string to a #Path instance. * Convert a UTF-8 C string to a #Path instance.
* Returns a duplicate of the UTF-8 string on failure. * Returns return a "nulled" instance on error.
*
* TODO: return a "nulled" instance on error and add checks to
* all callers
*/ */
gcc_pure gcc_pure
static Path FromUTF8(const char *path_utf8); static Path FromUTF8(const char *path_utf8);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment