Commit ba96920a authored by Max Kellermann's avatar Max Kellermann

path: replaced mpd_basename() with g_path_get_basename()

GLib's g_path_get_basename() is much more reliable than mpd_basename(). The latter could be tricked into an assertion failure.
parent 99e82a2e
...@@ -22,6 +22,7 @@ ...@@ -22,6 +22,7 @@
#include <assert.h> #include <assert.h>
#include <string.h> #include <string.h>
#include <glib.h>
struct directory * struct directory *
directory_new(const char *path, struct directory *parent) directory_new(const char *path, struct directory *parent)
...@@ -53,7 +54,7 @@ directory_free(struct directory *directory) ...@@ -53,7 +54,7 @@ directory_free(struct directory *directory)
const char * const char *
directory_get_name(const struct directory *directory) directory_get_name(const struct directory *directory)
{ {
return mpd_basename(directory->path); return g_basename(directory->path);
} }
void void
......
...@@ -24,6 +24,8 @@ ...@@ -24,6 +24,8 @@
#include "utils.h" #include "utils.h"
#include "song_save.h" #include "song_save.h"
#include <glib.h>
/* TODO error checking */ /* TODO error checking */
int int
directory_save(FILE *fp, struct directory *directory) directory_save(FILE *fp, struct directory *directory)
...@@ -41,9 +43,10 @@ directory_save(FILE *fp, struct directory *directory) ...@@ -41,9 +43,10 @@ directory_save(FILE *fp, struct directory *directory)
for (i = 0; i < children->nr; ++i) { for (i = 0; i < children->nr; ++i) {
struct directory *cur = children->base[i]; struct directory *cur = children->base[i];
const char *base = mpd_basename(cur->path); char *base = g_path_get_basename(cur->path);
retv = fprintf(fp, DIRECTORY_DIR "%s\n", base); retv = fprintf(fp, DIRECTORY_DIR "%s\n", base);
g_free(base);
if (retv < 0) if (retv < 0)
return -1; return -1;
if (directory_save(fp, cur) < 0) if (directory_save(fp, cur) < 0)
......
...@@ -4,6 +4,7 @@ ...@@ -4,6 +4,7 @@
#include "path.h" #include "path.h"
#include <string.h> #include <string.h>
#include <glib.h>
static size_t dv_size(struct dirvec *dv) static size_t dv_size(struct dirvec *dv)
{ {
...@@ -25,13 +26,18 @@ void dirvec_sort(struct dirvec *dv) ...@@ -25,13 +26,18 @@ void dirvec_sort(struct dirvec *dv)
struct directory *dirvec_find(const struct dirvec *dv, const char *path) struct directory *dirvec_find(const struct dirvec *dv, const char *path)
{ {
char *basename;
int i; int i;
path = mpd_basename(path); basename = g_path_get_basename(path);
for (i = dv->nr; --i >= 0; ) for (i = dv->nr; --i >= 0; )
if (!strcmp(directory_get_name(dv->base[i]), path)) if (!strcmp(directory_get_name(dv->base[i]), basename)) {
g_free(basename);
return dv->base[i]; return dv->base[i];
}
g_free(basename);
return NULL; return NULL;
} }
......
...@@ -276,15 +276,3 @@ void utf8_to_fs_playlist_path(char *path_max_tmp, const char *utf8path) ...@@ -276,15 +276,3 @@ void utf8_to_fs_playlist_path(char *path_max_tmp, const char *utf8path)
rpp2app_r(path_max_tmp, path_max_tmp); rpp2app_r(path_max_tmp, path_max_tmp);
strncat(path_max_tmp, "." PLAYLIST_FILE_SUFFIX, MPD_PATH_MAX - 1); strncat(path_max_tmp, "." PLAYLIST_FILE_SUFFIX, MPD_PATH_MAX - 1);
} }
/* Only takes sanitized paths w/o trailing slashes */
const char *mpd_basename(const char *path)
{
const char *ret = strrchr(path, '/');
if (!ret)
return path;
++ret;
assert(*ret != '\0');
return ret;
}
...@@ -83,12 +83,4 @@ void pathcpy_trunc(char *dest, const char *src); ...@@ -83,12 +83,4 @@ void pathcpy_trunc(char *dest, const char *src);
*/ */
void utf8_to_fs_playlist_path(char *path_max_tmp, const char *utf8path); void utf8_to_fs_playlist_path(char *path_max_tmp, const char *utf8path);
/*
* Like basename(3) but with predictable semantics independent
* of C library or build options used. This is also much more strict
* and does not account for trailing slashes (mpd should never deal with
* trailing slashes on internal functions).
*/
const char *mpd_basename(const char *path);
#endif #endif
...@@ -32,6 +32,8 @@ ...@@ -32,6 +32,8 @@
#include "update.h" #include "update.h"
#include "idle.h" #include "idle.h"
#include <glib.h>
static enum update_progress { static enum update_progress {
UPDATE_PROGRESS_IDLE = 0, UPDATE_PROGRESS_IDLE = 0,
UPDATE_PROGRESS_RUNNING = 1, UPDATE_PROGRESS_RUNNING = 1,
...@@ -407,6 +409,7 @@ static struct directory * ...@@ -407,6 +409,7 @@ static struct directory *
directory_make_child_checked(struct directory *parent, const char *path) directory_make_child_checked(struct directory *parent, const char *path)
{ {
struct directory *directory; struct directory *directory;
char *basename;
struct stat st; struct stat st;
struct song *conflicting; struct song *conflicting;
...@@ -414,16 +417,22 @@ directory_make_child_checked(struct directory *parent, const char *path) ...@@ -414,16 +417,22 @@ directory_make_child_checked(struct directory *parent, const char *path)
if (directory != NULL) if (directory != NULL)
return directory; return directory;
if (stat_directory_child(parent, mpd_basename(path), &st) < 0 || basename = g_path_get_basename(path);
inodeFoundInParent(parent, st.st_ino, st.st_dev))
if (stat_directory_child(parent, basename, &st) < 0 ||
inodeFoundInParent(parent, st.st_ino, st.st_dev)) {
g_free(basename);
return NULL; return NULL;
}
/* if we're adding directory paths, make sure to delete filenames /* if we're adding directory paths, make sure to delete filenames
with potentially the same name */ with potentially the same name */
conflicting = songvec_find(&parent->songs, mpd_basename(path)); conflicting = songvec_find(&parent->songs, basename);
if (conflicting) if (conflicting)
delete_song(parent, conflicting); delete_song(parent, conflicting);
g_free(basename);
directory = directory_new_child(parent, path); directory = directory_new_child(parent, path);
directory_set_stat(directory, &st); directory_set_stat(directory, &st);
return directory; return directory;
...@@ -455,19 +464,21 @@ static void ...@@ -455,19 +464,21 @@ static void
updatePath(const char *path) updatePath(const char *path)
{ {
struct directory *parent; struct directory *parent;
const char *name; char *name;
struct stat st; struct stat st;
parent = addParentPathToDB(path); parent = addParentPathToDB(path);
if (parent == NULL) if (parent == NULL)
return; return;
name = mpd_basename(path); name = g_path_get_basename(path);
if (stat_directory_child(parent, name, &st) == 0) if (stat_directory_child(parent, name, &st) == 0)
updateInDirectory(parent, name, &st); updateInDirectory(parent, name, &st);
else else
delete_name_in(parent, name); delete_name_in(parent, name);
g_free(name);
} }
static void * update_task(void *_path) static void * update_task(void *_path)
......
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