tree-wide: use path_join() instead of prefix_roota() in various cases
prefix_roota() is something we should stop using. It is bad for three reasons: 1. As it names suggests it's supposed to be used when working relative to some root directory, but given it doesn't follow symlinks (and instead just stupidly joins paths) it is not a good choice for that. 2. More often than not it is currently used with inputs under control of the user, and that is icky given it typically allocates memory on the stack. 3. It's a redundant interface, where chase_symlinks() and path_join() already exist as better, safer interfaces. Hence, let's start moving things from prefix_roota() to path_join() for the cases where that's appropriate.
This commit is contained in:
committed by
Luca Boccassi
parent
8ef6106de4
commit
8e7e4a730b
@@ -2128,8 +2128,8 @@ int device_get_cached_sysattr_value(sd_device *device, const char *key, const ch
|
||||
/* We cache all sysattr lookups. If an attribute does not exist, it is stored
|
||||
* with a NULL value in the cache, otherwise the returned string is stored */
|
||||
_public_ int sd_device_get_sysattr_value(sd_device *device, const char *sysattr, const char **ret_value) {
|
||||
_cleanup_free_ char *value = NULL;
|
||||
const char *path, *syspath;
|
||||
_cleanup_free_ char *value = NULL, *path = NULL;
|
||||
const char *syspath;
|
||||
struct stat statbuf;
|
||||
int r;
|
||||
|
||||
@@ -2145,7 +2145,10 @@ _public_ int sd_device_get_sysattr_value(sd_device *device, const char *sysattr,
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
path = prefix_roota(syspath, sysattr);
|
||||
path = path_join(syspath, sysattr);
|
||||
if (!path)
|
||||
return -ENOMEM;
|
||||
|
||||
if (lstat(path, &statbuf) < 0) {
|
||||
int k;
|
||||
|
||||
@@ -2227,8 +2230,8 @@ static void device_remove_cached_sysattr_value(sd_device *device, const char *_k
|
||||
}
|
||||
|
||||
_public_ int sd_device_set_sysattr_value(sd_device *device, const char *sysattr, const char *_value) {
|
||||
_cleanup_free_ char *value = NULL;
|
||||
const char *syspath, *path;
|
||||
_cleanup_free_ char *value = NULL, *path = NULL;
|
||||
const char *syspath;
|
||||
size_t len;
|
||||
int r;
|
||||
|
||||
@@ -2247,7 +2250,9 @@ _public_ int sd_device_set_sysattr_value(sd_device *device, const char *sysattr,
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
path = prefix_roota(syspath, sysattr);
|
||||
path = path_join(syspath, sysattr);
|
||||
if (!path)
|
||||
return -ENOMEM;
|
||||
|
||||
len = strlen(_value);
|
||||
|
||||
|
||||
@@ -1375,7 +1375,7 @@ static int add_file_by_name(
|
||||
const char *prefix,
|
||||
const char *filename) {
|
||||
|
||||
const char *path;
|
||||
_cleanup_free_ char *path = NULL;
|
||||
|
||||
assert(j);
|
||||
assert(prefix);
|
||||
@@ -1387,28 +1387,35 @@ static int add_file_by_name(
|
||||
if (!file_type_wanted(j->flags, filename))
|
||||
return 0;
|
||||
|
||||
path = prefix_roota(prefix, filename);
|
||||
path = path_join(prefix, filename);
|
||||
if (!path)
|
||||
return -ENOMEM;
|
||||
|
||||
return add_any_file(j, -1, path);
|
||||
}
|
||||
|
||||
static void remove_file_by_name(
|
||||
static int remove_file_by_name(
|
||||
sd_journal *j,
|
||||
const char *prefix,
|
||||
const char *filename) {
|
||||
|
||||
const char *path;
|
||||
_cleanup_free_ char *path = NULL;
|
||||
JournalFile *f;
|
||||
|
||||
assert(j);
|
||||
assert(prefix);
|
||||
assert(filename);
|
||||
|
||||
path = prefix_roota(prefix, filename);
|
||||
path = path_join(prefix, filename);
|
||||
if (!path)
|
||||
return -ENOMEM;
|
||||
|
||||
f = ordered_hashmap_get(j->files, path);
|
||||
if (!f)
|
||||
return;
|
||||
return 0;
|
||||
|
||||
remove_file_real(j, f);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static void remove_file_real(sd_journal *j, JournalFile *f) {
|
||||
@@ -2620,7 +2627,7 @@ static void process_inotify_event(sd_journal *j, const struct inotify_event *e)
|
||||
if (e->mask & (IN_CREATE|IN_MOVED_TO|IN_MODIFY|IN_ATTRIB))
|
||||
(void) add_file_by_name(j, d->path, e->name);
|
||||
else if (e->mask & (IN_DELETE|IN_MOVED_FROM|IN_UNMOUNT))
|
||||
remove_file_by_name(j, d->path, e->name);
|
||||
(void) remove_file_by_name(j, d->path, e->name);
|
||||
|
||||
} else if (!d->is_root && e->len == 0) {
|
||||
|
||||
|
||||
Reference in New Issue
Block a user