diff options
author | Max Kellermann <max@duempel.org> | 2009-01-08 21:20:46 +0100 |
---|---|---|
committer | Max Kellermann <max@duempel.org> | 2009-01-08 21:20:46 +0100 |
commit | 5ed5aa99acb6b8360df75bb1ce13d4b97ea50538 (patch) | |
tree | 518a01dabc3c9652fdbb090eb4e8fd97ed725729 /src | |
parent | f0980283bcce8ad7819f71bc49b973e28e54b22d (diff) | |
download | mpd-5ed5aa99acb6b8360df75bb1ce13d4b97ea50538.tar.gz mpd-5ed5aa99acb6b8360df75bb1ce13d4b97ea50538.tar.xz mpd-5ed5aa99acb6b8360df75bb1ce13d4b97ea50538.zip |
path: allocate buffer in fs_charset conversion functions
Don't use fixed static buffers. GLib allocates a new string for us
anyway, let's just return this one instead of copying it.
Diffstat (limited to '')
-rw-r--r-- | src/mapper.c | 33 | ||||
-rw-r--r-- | src/path.c | 29 | ||||
-rw-r--r-- | src/path.h | 14 | ||||
-rw-r--r-- | src/playlist_save.c | 11 | ||||
-rw-r--r-- | src/stored_playlist.c | 6 | ||||
-rw-r--r-- | src/update.c | 8 |
6 files changed, 55 insertions, 46 deletions
diff --git a/src/mapper.c b/src/mapper.c index bbcc220e3..6583e1c54 100644 --- a/src/mapper.c +++ b/src/mapper.c @@ -91,16 +91,19 @@ void mapper_finish(void) char * map_uri_fs(const char *uri) { - char buffer[MPD_PATH_MAX]; + char *uri_fs, *path_fs; assert(uri != NULL); assert(*uri != '/'); - uri = utf8_to_fs_charset(buffer, uri); - if (uri == NULL) + uri_fs = utf8_to_fs_charset(uri); + if (uri_fs == NULL) return NULL; - return g_build_filename(music_dir, uri, NULL); + path_fs = g_build_filename(music_dir, uri_fs, NULL); + g_free(uri_fs); + + return path_fs; } char * @@ -116,8 +119,7 @@ map_directory_fs(const struct directory *directory) char * map_directory_child_fs(const struct directory *directory, const char *name) { - char buffer[MPD_PATH_MAX]; - char *parent_fs, *path; + char *name_fs, *parent_fs, *path; /* check for invalid or unauthorized base names */ if (*name == 0 || strchr(name, '/') != NULL || @@ -128,30 +130,33 @@ map_directory_child_fs(const struct directory *directory, const char *name) if (parent_fs == NULL) return NULL; - name = utf8_to_fs_charset(buffer, name); - path = g_build_filename(parent_fs, name, NULL); + name_fs = utf8_to_fs_charset(name); + if (name_fs == NULL) { + g_free(parent_fs); + return NULL; + } + + path = g_build_filename(parent_fs, name_fs, NULL); g_free(parent_fs); + g_free(name_fs); + return path; } char * map_song_fs(const struct song *song) { - char buffer[MPD_PATH_MAX]; - assert(song_is_file(song)); if (song_in_database(song)) return map_directory_child_fs(song->parent, song->url); else - return g_strdup(utf8_to_fs_charset(buffer, song->url)); + return utf8_to_fs_charset(song->url); } char * map_fs_to_utf8(const char *path_fs) { - char buffer[MPD_PATH_MAX]; - if (strncmp(path_fs, music_dir, music_dir_length) == 0 && path_fs[music_dir_length] == '/') /* remove musicDir prefix */ @@ -160,7 +165,7 @@ map_fs_to_utf8(const char *path_fs) /* not within musicDir */ return NULL; - return g_strdup(fs_charset_to_utf8(buffer, path_fs)); + return fs_charset_to_utf8(path_fs); } const char * diff --git a/src/path.c b/src/path.c index 88ac9382e..fe602a15f 100644 --- a/src/path.c +++ b/src/path.c @@ -29,36 +29,27 @@ static char *fs_charset; -char *fs_charset_to_utf8(char *dst, const char *str) +char * +fs_charset_to_utf8(const char *path_fs) { - gchar *p; - - p = g_convert(str, -1, - "utf-8", fs_charset, - NULL, NULL, NULL); - if (p == NULL) - /* no fallback */ - return NULL; - - g_strlcpy(dst, p, MPD_PATH_MAX); - g_free(p); - return dst; + return g_convert(path_fs, -1, + "utf-8", fs_charset, + NULL, NULL, NULL); } -char *utf8_to_fs_charset(char *dst, const char *str) +char * +utf8_to_fs_charset(const char *path_utf8) { gchar *p; - p = g_convert(str, -1, + p = g_convert(path_utf8, -1, fs_charset, "utf-8", NULL, NULL, NULL); if (p == NULL) /* fall back to UTF-8 */ - return strcpy(dst, str); + p = g_strdup(path_utf8); - g_strlcpy(dst, p, MPD_PATH_MAX); - g_free(p); - return dst; + return p; } void path_set_fs_charset(const char *charset) diff --git a/src/path.h b/src/path.h index 3bb088b5f..1dbdd7820 100644 --- a/src/path.h +++ b/src/path.h @@ -36,9 +36,19 @@ void path_global_init(void); void path_global_finish(void); -char *fs_charset_to_utf8(char *dst, const char *str); +/** + * Converts a file name in the filesystem charset to UTF-8. Returns + * NULL on failure. + */ +char * +fs_charset_to_utf8(const char *path_fs); -char *utf8_to_fs_charset(char *dst, const char *str); +/** + * Converts a file name in UTF-8 to the filesystem charset. Returns a + * duplicate of the UTF-8 string on failure. + */ +char * +utf8_to_fs_charset(const char *path_utf8); void path_set_fs_charset(const char *charset); diff --git a/src/playlist_save.c b/src/playlist_save.c index 054ba73f4..0913fcd09 100644 --- a/src/playlist_save.c +++ b/src/playlist_save.c @@ -36,27 +36,26 @@ playlist_print_song(FILE *file, const struct song *song) g_free(path); } } else { - char *uri = song_get_uri(song); - char tmp2[MPD_PATH_MAX]; + char *uri = song_get_uri(song), *uri_fs; - utf8_to_fs_charset(tmp2, uri); + uri_fs = utf8_to_fs_charset(uri); g_free(uri); - fprintf(file, "%s\n", uri); + fprintf(file, "%s\n", uri_fs); + g_free(uri_fs); } } void playlist_print_uri(FILE *file, const char *uri) { - char tmp[MPD_PATH_MAX]; char *s; if (playlist_saveAbsolutePaths && !uri_has_scheme(uri) && uri[0] != '/') s = map_uri_fs(uri); else - s = g_strdup(utf8_to_fs_charset(tmp, uri)); + s = utf8_to_fs_charset(uri); if (s != NULL) { fprintf(file, "%s\n", s); diff --git a/src/stored_playlist.c b/src/stored_playlist.c index a503c2841..e6fdf944a 100644 --- a/src/stored_playlist.c +++ b/src/stored_playlist.c @@ -37,7 +37,7 @@ static struct stored_playlist_info * load_playlist_info(const char *parent_path_fs, const char *name_fs) { size_t name_length = strlen(name_fs); - char buffer[MPD_PATH_MAX], *path_fs, *name, *name_utf8; + char *path_fs, *name, *name_utf8; int ret; struct stat st; struct stored_playlist_info *playlist; @@ -60,13 +60,13 @@ load_playlist_info(const char *parent_path_fs, const char *name_fs) name = g_strdup(name_fs); name[name_length - sizeof(PLAYLIST_FILE_SUFFIX)] = 0; - name_utf8 = fs_charset_to_utf8(buffer, name); + name_utf8 = fs_charset_to_utf8(name); g_free(name); if (name_utf8 == NULL) return NULL; playlist = g_new(struct stored_playlist_info, 1); - playlist->name = g_strdup(name_utf8); + playlist->name = name_utf8; playlist->mtime = st.st_mtime; return playlist; } diff --git a/src/update.c b/src/update.c index 691f533b5..c2a6296cd 100644 --- a/src/update.c +++ b/src/update.c @@ -545,15 +545,19 @@ updateDirectory(struct directory *directory, const struct stat *st) if (skip_path(ent->d_name)) continue; - utf8 = fs_charset_to_utf8(path_max_tmp, ent->d_name); - if (utf8 == NULL || skip_symlink(directory, utf8)) + utf8 = fs_charset_to_utf8(ent->d_name); + if (utf8 == NULL || skip_symlink(directory, utf8)) { + g_free(utf8); continue; + } if (stat_directory_child(directory, utf8, &st2) == 0) updateInDirectory(directory, path_max_tmp, &st2); else delete_name_in(directory, path_max_tmp); + + g_free(utf8); } closedir(dir); |