mapper: allocate the result of map_directory_child_fs(), map_song_fs()

Don't use fixed stack buffers.
This commit is contained in:
Max Kellermann 2009-01-02 10:48:55 +01:00
parent 72255d580e
commit daf7c3db5a
6 changed files with 72 additions and 53 deletions

View File

@ -74,27 +74,14 @@ decoder_file_decode(const struct decoder_plugin *plugin,
return dc.state != DECODE_STATE_START;
}
static void decoder_run(void)
static void decoder_run_song(const struct song *song, const char *uri)
{
struct song *song = dc.next_song;
char buffer[MPD_PATH_MAX];
const char *uri;
struct decoder decoder;
int ret;
bool close_instream = true;
struct input_stream input_stream;
const struct decoder_plugin *plugin;
if (song_is_file(song))
uri = map_song_fs(song, buffer);
else
uri = song_get_url(song, buffer);
if (uri == NULL) {
dc.state = DECODE_STATE_ERROR;
return;
}
dc.current_song = dc.next_song; /* NEED LOCK */
if (!input_stream_open(&input_stream, uri)) {
dc.state = DECODE_STATE_ERROR;
return;
@ -202,6 +189,30 @@ static void decoder_run(void)
dc.state = ret ? DECODE_STATE_STOP : DECODE_STATE_ERROR;
}
static void decoder_run(void)
{
struct song *song = dc.next_song;
char *uri;
if (song_is_file(song))
uri = map_song_fs(song);
else {
char buffer[MPD_PATH_MAX];
uri = g_strdup(song_get_url(song, buffer));
}
if (uri == NULL) {
dc.state = DECODE_STATE_ERROR;
return;
}
dc.current_song = dc.next_song; /* NEED LOCK */
decoder_run_song(song, uri);
g_free(uri);
}
static gpointer decoder_task(G_GNUC_UNUSED gpointer arg)
{
do {

View File

@ -108,11 +108,11 @@ map_directory_fs(const struct directory *directory)
return map_uri_fs(dirname);
}
const char *
map_directory_child_fs(const struct directory *directory, const char *name,
char *buffer)
char *
map_directory_child_fs(const struct directory *directory, const char *name)
{
char *parent_fs;
char buffer[MPD_PATH_MAX];
char *parent_fs, *path;
/* check for invalid or unauthorized base names */
if (*name == 0 || strchr(name, '/') != NULL ||
@ -124,21 +124,22 @@ map_directory_child_fs(const struct directory *directory, const char *name,
return NULL;
name = utf8_to_fs_charset(buffer, name);
pfx_dir(buffer, name, strlen(name),
parent_fs, strlen(parent_fs));
path = g_build_filename(parent_fs, name, NULL);
g_free(parent_fs);
return buffer;
return path;
}
const char *
map_song_fs(const struct song *song, char *buffer)
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, buffer);
return map_directory_child_fs(song->parent, song->url);
else
return utf8_to_fs_charset(buffer, song->url);
return g_strdup(utf8_to_fs_charset(buffer, song->url));
}
const char *

View File

@ -59,9 +59,8 @@ map_directory_fs(const struct directory *directory);
* @param a buffer which is MPD_PATH_MAX bytes long
* @return the path in file system encoding, or NULL if mapping failed
*/
const char *
map_directory_child_fs(const struct directory *directory, const char *name,
char *buffer);
char *
map_directory_child_fs(const struct directory *directory, const char *name);
/**
* Determines the file system path of a song. This must not be a
@ -71,8 +70,8 @@ map_directory_child_fs(const struct directory *directory, const char *name,
* @param a buffer which is MPD_PATH_MAX bytes long
* @return the path in file system encoding, or NULL if mapping failed
*/
const char *
map_song_fs(const struct song *song, char *buffer);
char *
map_song_fs(const struct song *song);
/**
* Maps a file system path (relative to the music directory or

View File

@ -32,9 +32,11 @@ playlist_print_song(FILE *file, const struct song *song)
char tmp1[MPD_PATH_MAX], tmp2[MPD_PATH_MAX];
if (playlist_saveAbsolutePaths && song_in_database(song)) {
const char *path = map_song_fs(song, tmp1);
if (path != NULL)
char *path = map_song_fs(song);
if (path != NULL) {
fprintf(file, "%s\n", path);
g_free(path);
}
} else {
song_get_url(song, tmp1);
utf8_to_fs_charset(tmp2, tmp1);

View File

@ -104,15 +104,14 @@ song_free(struct song *song)
bool
song_file_update(struct song *song)
{
char buffer[MPD_PATH_MAX];
const char *path_fs;
char *path_fs;
const struct decoder_plugin *plugin;
unsigned int next = 0;
struct stat st;
assert(song_is_file(song));
path_fs = map_song_fs(song, buffer);
path_fs = map_song_fs(song);
if (path_fs == NULL)
return false;
@ -121,8 +120,10 @@ song_file_update(struct song *song)
song->tag = NULL;
}
if (stat(path_fs, &st) < 0 || !S_ISREG(st.st_mode))
if (stat(path_fs, &st) < 0 || !S_ISREG(st.st_mode)) {
g_free(path_fs);
return false;
}
song->mtime = st.st_mtime;
@ -130,19 +131,19 @@ song_file_update(struct song *song)
(plugin = hasMusicSuffix(path_fs, next++)))
song->tag = plugin->tag_dup(path_fs);
g_free(path_fs);
return song->tag != NULL;
}
bool
song_file_update_inarchive(struct song *song)
{
char buffer[MPD_PATH_MAX];
const char *path_fs;
char *path_fs;
const struct decoder_plugin *plugin;
assert(song_is_file(song));
path_fs = map_song_fs(song, buffer);
path_fs = map_song_fs(song);
if (path_fs == NULL)
return false;
@ -154,6 +155,7 @@ song_file_update_inarchive(struct song *song)
//because we dont support tag reading throught
//input streams
plugin = hasMusicSuffix(path_fs, 0);
g_free(path_fs);
if (plugin) {
song->tag = tag_new();
//tag_add_item(tag, TAG_ITEM_TITLE, f->title);

View File

@ -163,7 +163,6 @@ delete_name_in(struct directory *parent, const char *name)
}
struct delete_data {
char *tmp;
struct directory *dir;
};
@ -172,19 +171,21 @@ static int
delete_song_if_removed(struct song *song, void *_data)
{
struct delete_data *data = _data;
const char *path;
char *path;
struct stat st;
if ((path = map_song_fs(song, data->tmp)) == NULL ||
stat(data->tmp, &st) < 0 || !S_ISREG(st.st_mode)) {
if ((path = map_song_fs(song)) == NULL ||
stat(path, &st) < 0 || !S_ISREG(st.st_mode)) {
delete_song(data->dir, song);
modified = true;
}
g_free(path);
return 0;
}
static void
removeDeletedFromDirectory(char *path_max_tmp, struct directory *directory)
removeDeletedFromDirectory(struct directory *directory)
{
int i;
struct dirvec *dv = &directory->children;
@ -208,7 +209,6 @@ removeDeletedFromDirectory(char *path_max_tmp, struct directory *directory)
}
data.dir = directory;
data.tmp = path_max_tmp;
songvec_for_each(&directory->songs, delete_song_if_removed, &data);
}
@ -230,14 +230,16 @@ static int
stat_directory_child(const struct directory *parent, const char *name,
struct stat *st)
{
char buffer[MPD_PATH_MAX];
const char *path_fs;
char *path_fs;
int ret;
path_fs = map_directory_child_fs(parent, name, buffer);
path_fs = map_directory_child_fs(parent, name);
if (path_fs == NULL)
return -1;
return stat(path_fs, st);
ret = stat(path_fs, st);
g_free(path_fs);
return ret;
}
static int
@ -424,14 +426,16 @@ static bool
skip_symlink(const struct directory *directory, const char *utf8_name)
{
char buffer[MPD_PATH_MAX];
char *path_fs;
const char *p;
ssize_t ret;
p = map_directory_child_fs(directory, utf8_name, buffer);
if (p == NULL)
path_fs = map_directory_child_fs(directory, utf8_name);
if (path_fs == NULL)
return true;
ret = readlink(p, buffer, sizeof(buffer));
ret = readlink(path_fs, buffer, sizeof(buffer));
g_free(path_fs);
if (ret < 0)
/* don't skip if this is not a symlink */
return errno != EINVAL;
@ -493,7 +497,7 @@ updateDirectory(struct directory *directory, const struct stat *st)
if (!dir)
return false;
removeDeletedFromDirectory(path_max_tmp, directory);
removeDeletedFromDirectory(directory);
while ((ent = readdir(dir))) {
char *utf8;