From 80fa9183e4d30643b1e089d9b96f36a4b266276e Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Thu, 1 Jan 2009 19:17:44 +0100 Subject: mapper: allocate playlist path from heap Don't pass a static buffer to map_spl_utf8_to_fs(). --- src/mapper.c | 21 +++++++--------- src/mapper.h | 7 +++--- src/playlist.c | 10 +++++--- src/stored_playlist.c | 69 ++++++++++++++++++++++++++++++--------------------- 4 files changed, 60 insertions(+), 47 deletions(-) (limited to 'src') diff --git a/src/mapper.c b/src/mapper.c index 291816e6e..3777b7c62 100644 --- a/src/mapper.c +++ b/src/mapper.c @@ -164,23 +164,20 @@ map_fs_to_utf8(const char *path_fs, char *buffer) return fs_charset_to_utf8(buffer, path_fs); } -static char *rpp2app_r(char *dst, const char *rel_path) -{ - pfx_dir(dst, rel_path, strlen(rel_path), - playlist_dir, playlist_dir_length); - return dst; -} - const char * map_spl_path(void) { return playlist_dir; } -const char * -map_spl_utf8_to_fs(const char *name, char *buffer) +char * +map_spl_utf8_to_fs(const char *name) { - rpp2app_r(buffer, utf8_to_fs_charset(buffer, name)); - g_strlcat(buffer, "." PLAYLIST_FILE_SUFFIX, MPD_PATH_MAX); - return buffer; + char *filename = g_strconcat(name, "." PLAYLIST_FILE_SUFFIX, NULL); + char *path; + + path = g_build_filename(playlist_dir, filename, NULL); + g_free(filename); + + return path; } diff --git a/src/mapper.h b/src/mapper.h index 2aecd9a52..2f4ab353d 100644 --- a/src/mapper.h +++ b/src/mapper.h @@ -93,9 +93,10 @@ map_spl_path(void); /** * Maps a playlist name (without the ".m3u" suffix) to a file system - * path. + * path. The return value is allocated on the heap and must be freed + * with g_free(). */ -const char * -map_spl_utf8_to_fs(const char *name, char *buffer); +char * +map_spl_utf8_to_fs(const char *name); #endif diff --git a/src/playlist.c b/src/playlist.c index 59a2a0dee..0c93f92b0 100644 --- a/src/playlist.c +++ b/src/playlist.c @@ -1203,17 +1203,19 @@ enum playlist_result savePlaylist(const char *utf8file) { FILE *fp; struct stat sb; - char path_max_tmp[MPD_PATH_MAX]; - const char *path; + char *path; if (!is_valid_playlist_name(utf8file)) return PLAYLIST_RESULT_BAD_NAME; - path = map_spl_utf8_to_fs(utf8file, path_max_tmp); - if (!stat(path, &sb)) + path = map_spl_utf8_to_fs(utf8file); + if (!stat(path, &sb)) { + g_free(path); return PLAYLIST_RESULT_LIST_EXISTS; + } while (!(fp = fopen(path, "w")) && errno == EINTR); + g_free(path); if (fp == NULL) return PLAYLIST_RESULT_ERRNO; diff --git a/src/stored_playlist.c b/src/stored_playlist.c index 545ecc158..534b40d2a 100644 --- a/src/stored_playlist.c +++ b/src/stored_playlist.c @@ -115,14 +115,14 @@ static enum playlist_result spl_save(GPtrArray *list, const char *utf8path) { FILE *file; - char path_max_tmp[MPD_PATH_MAX]; - const char *path_fs; + char *path_fs; assert(utf8path != NULL); - path_fs = map_spl_utf8_to_fs(utf8path, path_max_tmp); + path_fs = map_spl_utf8_to_fs(utf8path); while (!(file = fopen(path_fs, "w")) && errno == EINTR); + g_free(path_fs); if (file == NULL) return PLAYLIST_RESULT_ERRNO; @@ -142,14 +142,15 @@ spl_load(const char *utf8path) GPtrArray *list; char buffer[MPD_PATH_MAX]; char path_max_tmp[MPD_PATH_MAX]; - const char *path_fs; + char *path_fs; if (!is_valid_playlist_name(utf8path)) return NULL; - path_fs = map_spl_utf8_to_fs(utf8path, path_max_tmp); + path_fs = map_spl_utf8_to_fs(utf8path); while (!(file = fopen(path_fs, "r")) && errno == EINTR); + g_free(path_fs); if (file == NULL) return NULL; @@ -257,16 +258,16 @@ spl_move_index(const char *utf8path, unsigned src, unsigned dest) enum playlist_result spl_clear(const char *utf8path) { - char filename[MPD_PATH_MAX]; - const char *path_fs; + char *path_fs; FILE *file; if (!is_valid_playlist_name(utf8path)) return PLAYLIST_RESULT_BAD_NAME; - path_fs = map_spl_utf8_to_fs(utf8path, filename); + path_fs = map_spl_utf8_to_fs(utf8path); while (!(file = fopen(path_fs, "w")) && errno == EINTR); + g_free(path_fs); if (file == NULL) return PLAYLIST_RESULT_ERRNO; @@ -279,12 +280,13 @@ spl_clear(const char *utf8path) enum playlist_result spl_delete(const char *name_utf8) { - char filename[MPD_PATH_MAX]; - const char *path_fs; - - path_fs = map_spl_utf8_to_fs(name_utf8, filename); + char *path_fs; + int ret; - if (unlink(path_fs) < 0) + path_fs = map_spl_utf8_to_fs(name_utf8); + ret = unlink(path_fs); + g_free(path_fs); + if (ret < 0) return errno == ENOENT ? PLAYLIST_RESULT_NO_SUCH_LIST : PLAYLIST_RESULT_ERRNO; @@ -323,15 +325,15 @@ spl_append_song(const char *utf8path, struct song *song) { FILE *file; struct stat st; - char path_max_tmp[MPD_PATH_MAX]; - const char *path_fs; + char *path_fs; if (!is_valid_playlist_name(utf8path)) return PLAYLIST_RESULT_BAD_NAME; - path_fs = map_spl_utf8_to_fs(utf8path, path_max_tmp); + path_fs = map_spl_utf8_to_fs(utf8path); while (!(file = fopen(path_fs, "a")) && errno == EINTR); + g_free(path_fs); if (file == NULL) { int save_errno = errno; while (fclose(file) != 0 && errno == EINTR); @@ -381,20 +383,10 @@ spl_append_uri(const char *url, const char *utf8file) return PLAYLIST_RESULT_NO_SUCH_SONG; } -enum playlist_result -spl_rename(const char *utf8from, const char *utf8to) +static enum playlist_result +spl_rename_internal(const char *from_path_fs, const char *to_path_fs) { struct stat st; - char from[MPD_PATH_MAX]; - char to[MPD_PATH_MAX]; - const char *from_path_fs, *to_path_fs; - - if (!is_valid_playlist_name(utf8from) || - !is_valid_playlist_name(utf8to)) - return PLAYLIST_RESULT_BAD_NAME; - - from_path_fs = map_spl_utf8_to_fs(utf8from, from); - to_path_fs = map_spl_utf8_to_fs(utf8to, to); if (stat(from_path_fs, &st) != 0) return PLAYLIST_RESULT_NO_SUCH_LIST; @@ -408,3 +400,24 @@ spl_rename(const char *utf8from, const char *utf8to) idle_add(IDLE_STORED_PLAYLIST); return PLAYLIST_RESULT_SUCCESS; } + +enum playlist_result +spl_rename(const char *utf8from, const char *utf8to) +{ + char *from_path_fs, *to_path_fs; + static enum playlist_result ret; + + if (!is_valid_playlist_name(utf8from) || + !is_valid_playlist_name(utf8to)) + return PLAYLIST_RESULT_BAD_NAME; + + from_path_fs = map_spl_utf8_to_fs(utf8from); + to_path_fs = map_spl_utf8_to_fs(utf8to); + + ret = spl_rename_internal(from_path_fs, to_path_fs); + + g_free(from_path_fs); + g_free(to_path_fs); + + return ret; +} -- cgit v1.2.3