/***
This file is part of systemd.
Copyright 2010-2012 Lennart Poettering
under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
systemd is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
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
* POSIX version which is really broken. We prefer GNU basename(). */
#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) {
return p[0] == '/';
}
bool is_path(const char *p) {
return !!strchr(p, '/');
}
char **l;
int r;
assert(p);
l = strv_split(p, ":");
if (!l)
return -ENOMEM;
r = path_strv_make_absolute_cwd(l);
if (r < 0) {
strv_free(l);
return r;
}
*ret = l;
return r;
}
assert(p);
/* Makes every item in the list an absolute path by prepending
* the prefix, if specified and necessary */
if (path_is_absolute(p) || !prefix)
return strdup(p);
}
char *c;
assert(p);
/* Similar to path_make_absolute(), but prefixes with the
* current working directory. */
if (path_is_absolute(p))
c = strdup(p);
else {
cwd = get_current_dir_name();
if (!cwd)
return negative_errno();
}
if (!c)
return -ENOMEM;
*ret = c;
return 0;
}
char *r, *p;
unsigned n_parents;
/* Strips the common part, and adds ".." elements as necessary. */
if (!path_is_absolute(from_dir))
return -EINVAL;
if (!path_is_absolute(to_path))
return -EINVAL;
/* Skip the common part. */
for (;;) {
size_t a;
size_t b;
if (!*from_dir) {
if (!*to_path)
/* from_dir equals to_path. */
r = strdup(".");
else
/* from_dir is a parent directory of to_path. */
if (!r)
return -ENOMEM;
*_r = r;
return 0;
}
if (!*to_path)
break;
if (a != b)
break;
break;
from_dir += a;
to_path += b;
}
/* If we're here, then "from_dir" has one or more elements that need to
* be replaced with "..". */
/* Count the number of necessary ".." elements. */
for (n_parents = 0;;) {
if (!*from_dir)
break;
n_parents++;
}
if (!r)
return -ENOMEM;
*_r = r;
return 0;
}
int path_strv_make_absolute_cwd(char **l) {
char **s;
int r;
/* Goes through every item in the string list and makes it
* absolute. This works in place and won't rollback any
* changes on failure. */
STRV_FOREACH(s, l) {
char *t;
r = path_make_absolute_cwd(*s, &t);
if (r < 0)
return r;
free(*s);
*s = t;
}
return 0;
}
char **s;
unsigned k = 0;
bool enomem = false;
if (strv_isempty(l))
return l;
/* Goes through every item in the string list and canonicalize
* the path. This works in place and won't rollback any
* changes on failure. */
STRV_FOREACH(s, l) {
char *t, *u;
if (!path_is_absolute(*s)) {
free(*s);
continue;
}
if (prefix) {
orig = *s;
if (!t) {
enomem = true;
continue;
}
} else
t = *s;
errno = 0;
u = canonicalize_file_name(t);
if (!u) {
if (prefix) {
u = orig;
free(t);
} else
u = t;
} else {
free(t);
enomem = true;
continue;
}
} else if (prefix) {
char *x;
free(t);
x = path_startswith(u, prefix);
if (x) {
/* restore the slash if it was lost */
if (!startswith(x, "/"))
*(--x) = '/';
t = strdup(x);
free(u);
if (!t) {
enomem = true;
continue;
}
u = t;
} else {
/* canonicalized path goes outside of
* prefix, keep the original path instead */
free(u);
u = orig;
}
} else
free(t);
l[k++] = u;
}
l[k] = NULL;
if (enomem)
return NULL;
return l;
}
if (strv_isempty(l))
return l;
if (!path_strv_resolve(l, prefix))
return NULL;
return strv_uniq(l);
}
char *f, *t;
bool slash = false;
/* Removes redundant inner and trailing slashes. Modifies the
* passed string in-place.
*
*/
if (*f == '/') {
slash = true;
continue;
}
if (slash) {
slash = false;
*(t++) = '/';
}
*(t++) = *f;
}
/* Special rule, if we are talking of the root directory, a
trailing slash is good */
*(t++) = '/';
*t = 0;
return path;
}
return NULL;
for (;;) {
size_t a, b;
if (*prefix == 0)
return (char*) path;
if (*path == 0)
return NULL;
if (a != b)
return NULL;
return NULL;
path += a;
prefix += b;
}
}
int path_compare(const char *a, const char *b) {
int d;
assert(a);
assert(b);
/* A relative path and an abolute path must not compare as equal.
* Which one is sorted before the other does not really matter.
* Here a relative path is ordered before an absolute path. */
d = (a[0] == '/') - (b[0] == '/');
if (d != 0)
return d;
for (;;) {
size_t j, k;
a += strspn(a, "/");
b += strspn(b, "/");
if (*a == 0 && *b == 0)
return 0;
if (*a == 0)
return -1;
if (*b == 0)
return 1;
j = strcspn(a, "/");
k = strcspn(b, "/");
if (d != 0)
return (d > 0) - (d < 0); /* sign of d */
d = (j > k) - (j < k); /* sign of (j - k) */
if (d != 0)
return d;
a += j;
b += 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) {
return path_equal(a, b) || files_same(a, b) > 0;
}
NULL);
else
NULL);
}
int last_error, r;
const char *p;
return -errno;
if (ret) {
if (r < 0)
return r;
}
return 0;
}
/**
* Plain getenv, not secure_getenv, because we want
* to actually allow the user to pick the binary.
*/
p = getenv("PATH");
if (!p)
p = DEFAULT_PATH;
last_error = -ENOENT;
for (;;) {
if (r < 0)
return r;
if (r == 0)
break;
if (!path_is_absolute(element))
continue;
if (!j)
return -ENOMEM;
/* Found it! */
if (ret) {
*ret = path_kill_slashes(j);
j = NULL;
}
return 0;
}
last_error = -errno;
}
return last_error;
}
bool changed = false;
const char* const* i;
return false;
usec_t u;
continue;
/* first check */
if (*timestamp >= u)
continue;
log_debug("timestamp of '%s' changed", *i);
/* update timestamp */
if (update) {
*timestamp = u;
changed = true;
} else
return true;
}
return changed;
}
int r;
r = find_binary(binary, &p);
if (r == -ENOENT)
return 0;
if (r < 0)
return r;
* fsck */
r = readlink_malloc(p, &d);
if (r == -EINVAL) /* not a symlink */
return 1;
if (r < 0)
return r;
return !path_equal(d, "true") &&
!path_equal(d, "/bin/true") &&
!path_equal(d, "/usr/bin/true") &&
!path_equal(d, "/dev/null");
}
const char *checker;
return -EINVAL;
return binary_is_good(checker);
}
const char *mkfs;
return -EINVAL;
return binary_is_good(mkfs);
}
char *n, *p;
size_t l;
/* If root is passed, prefixes path with it. Otherwise returns
* it as is. */
/* First, drop duplicate prefixing slashes from the path */
path++;
n = new(char, l);
if (!n)
return NULL;
while (p > n && p[-1] == '/')
p--;
if (path[0] != '/')
*(p++) = '/';
return n;
}
char *p;
int r;
/*
* This function is intended to be used in command line
* parsers, to handle paths that are passed in. It makes the
* path absolute, and reduces it to NULL if omitted or
* root (the latter optionally).
*
* NOTE THAT THIS WILL FREE THE PREVIOUS ARGUMENT POINTER ON
* SUCCESS! Hence, do not pass in uninitialized pointers.
*/
return 0;
}
r = path_make_absolute_cwd(path, &p);
if (r < 0)
p = mfree(p);
*arg = p;
return 0;
}
if (!d)
return NULL;
if (dir == d)
return d;
free(d);
return dir2;
}
bool filename_is_valid(const char *p) {
const char *e;
if (isempty(p))
return false;
if (streq(p, "."))
return false;
if (streq(p, ".."))
return false;
e = strchrnul(p, '/');
if (*e != 0)
return false;
if (e - p > FILENAME_MAX)
return false;
return true;
}
bool path_is_safe(const char *p) {
if (isempty(p))
return false;
return false;
return false;
/* The following two checks are not really dangerous, but hey, they still are confusing */
return false;
if (strstr(p, "//"))
return false;
return true;
}
char *e, *ret;
size_t k;
/* This removes the last component of path and appends
* filename, unless the latter is absolute anyway or the
* former isn't */
if (path_is_absolute(filename))
if (!e)
if (!ret)
return NULL;
return ret;
}
return
filename[0] == '.' ||
}
return true;
return hidden_file_allow_backup(filename);
}
/* Returns true on paths that refer to a device, either in
* sysfs or in /dev */
return
}