path-util.c revision 93cc7779e0c121b75183920173f37cd1ee9d59cf
4a4de23cbdc0936dc74a01248113fd80730ce457Jens Elkner/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4a4de23cbdc0936dc74a01248113fd80730ce457Jens Elkner This file is part of systemd.
along with systemd; If not, see <http://www.gnu.org/licenses/>.
#include <errno.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
/* When we include libgen.h because we need dirname() we immediately
* undefine basename() since libgen.h defines it as a macro to the
#include <libgen.h>
#include "alloc-util.h"
#include "extract-word.h"
#include "fs-util.h"
#include "log.h"
#include "macro.h"
#include "missing.h"
#include "path-util.h"
#include "stat-util.h"
#include "string-util.h"
#include "strv.h"
#include "time-util.h"
bool path_is_absolute(const char *p) {
bool is_path(const char *p) {
assert(p);
return -ENOMEM;
r = path_strv_make_absolute_cwd(l);
strv_free(l);
*ret = l;
assert(p);
return strdup(p);
assert(p);
if (path_is_absolute(p))
c = strdup(p);
if (!cwd)
return -errno;
return -ENOMEM;
*ret = c;
unsigned n_parents;
return -EINVAL;
return -EINVAL;
size_t a;
size_t b;
if (!*from_dir) {
if (!*to_path)
return -ENOMEM;
*_r = r;
if (!*to_path)
from_dir += a;
to_path += b;
for (n_parents = 0;;) {
if (!*from_dir)
n_parents++;
return -ENOMEM;
*_r = r;
int path_strv_make_absolute_cwd(char **l) {
STRV_FOREACH(s, l) {
r = path_make_absolute_cwd(*s, &t);
free(*s);
bool enomem = false;
if (strv_isempty(l))
STRV_FOREACH(s, l) {
if (!path_is_absolute(*s)) {
free(*s);
if (prefix) {
orig = *s;
enomem = true;
errno = 0;
u = canonicalize_file_name(t);
if (prefix) {
u = orig;
free(t);
free(t);
enomem = true;
} else if (prefix) {
free(t);
t = strdup(x);
free(u);
enomem = true;
free(u);
u = orig;
free(t);
l[k] = NULL;
if (enomem)
return NULL;
if (strv_isempty(l))
return NULL;
return strv_uniq(l);
bool slash = false;
slash = true;
if (slash) {
slash = false;
return path;
return NULL;
size_t a, b;
if (*prefix == 0)
return (char*) path;
if (*path == 0)
return NULL;
return NULL;
return NULL;
path += a;
prefix += b;
int path_compare(const char *a, const char *b) {
assert(a);
assert(b);
size_t j, k;
bool path_equal(const char *a, const char *b) {
return path_compare(a, b) == 0;
bool path_equal_or_files_same(const char *a, const char *b) {
NULL);
NULL);
int last_error, r;
return -errno;
if (ret) {
p = DEFAULT_PATH;
return -ENOMEM;
if (ret) {
j = NULL;
return last_error;
bool changed = false;
usec_t u;
if (*timestamp >= u)
if (update) {
*timestamp = u;
changed = true;
return changed;
if (r == -ENOENT)
r = readlink_malloc(p, &d);
const char *checker;
return -EINVAL;
const char *mkfs;
return -EINVAL;
size_t l;
path++;
n = new(char, l);
return NULL;
p = mfree(p);
*arg = p;
return NULL;
if (dir == d)
free(d);
return dir2;
bool filename_is_valid(const char *p) {
if (isempty(p))
if (e - p > FILENAME_MAX)
bool path_is_safe(const char *p) {
if (isempty(p))
char *e, *ret;
size_t k;
if (!ret)
return NULL;
return ret;