1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2010-2012 Lennart Poettering
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
11 (at your option) any later version.
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
27 #include <sys/statvfs.h>
30 /* When we include libgen.h because we need dirname() we immediately
31 * undefine basename() since libgen.h defines it as a macro to the
32 * POSIX version which is really broken. We prefer GNU basename(). */
36 #include "alloc-util.h"
43 #include "parse-util.h"
44 #include "path-util.h"
45 #include "stat-util.h"
46 #include "string-util.h"
50 bool path_is_absolute(const char *p) {
54 bool is_path(const char *p) {
55 return !!strchr(p, '/');
58 #if 0 /// UNNEEDED by elogind
59 int path_split_and_make_absolute(const char *p, char ***ret) {
66 l = strv_split(p, ":");
70 r = path_strv_make_absolute_cwd(l);
80 char *path_make_absolute(const char *p, const char *prefix) {
83 /* Makes every item in the list an absolute path by prepending
84 * the prefix, if specified and necessary */
86 if (path_is_absolute(p) || !prefix)
89 return strjoin(prefix, "/", p, NULL);
93 int path_make_absolute_cwd(const char *p, char **ret) {
99 /* Similar to path_make_absolute(), but prefixes with the
100 * current working directory. */
102 if (path_is_absolute(p))
105 _cleanup_free_ char *cwd = NULL;
107 cwd = get_current_dir_name();
111 c = strjoin(cwd, "/", p, NULL);
120 #if 0 /// UNNEEDED by elogind
121 int path_make_relative(const char *from_dir, const char *to_path, char **_r) {
129 /* Strips the common part, and adds ".." elements as necessary. */
131 if (!path_is_absolute(from_dir))
134 if (!path_is_absolute(to_path))
137 /* Skip the common part. */
142 from_dir += strspn(from_dir, "/");
143 to_path += strspn(to_path, "/");
147 /* from_dir equals to_path. */
150 /* from_dir is a parent directory of to_path. */
156 path_kill_slashes(r);
165 a = strcspn(from_dir, "/");
166 b = strcspn(to_path, "/");
171 if (memcmp(from_dir, to_path, a) != 0)
178 /* If we're here, then "from_dir" has one or more elements that need to
179 * be replaced with "..". */
181 /* Count the number of necessary ".." elements. */
182 for (n_parents = 0;;) {
183 from_dir += strspn(from_dir, "/");
188 from_dir += strcspn(from_dir, "/");
192 r = malloc(n_parents * 3 + strlen(to_path) + 1);
196 for (p = r; n_parents > 0; n_parents--, p += 3)
200 path_kill_slashes(r);
206 int path_strv_make_absolute_cwd(char **l) {
210 /* Goes through every item in the string list and makes it
211 * absolute. This works in place and won't rollback any
212 * changes on failure. */
217 r = path_make_absolute_cwd(*s, &t);
229 char **path_strv_resolve(char **l, const char *prefix) {
237 /* Goes through every item in the string list and canonicalize
238 * the path. This works in place and won't rollback any
239 * changes on failure. */
243 _cleanup_free_ char *orig = NULL;
245 if (!path_is_absolute(*s)) {
252 t = strappend(prefix, orig);
261 u = canonicalize_file_name(t);
263 if (errno == ENOENT) {
272 if (errno == ENOMEM || errno == 0)
281 x = path_startswith(u, prefix);
283 /* restore the slash if it was lost */
284 if (!startswith(x, "/"))
295 /* canonicalized path goes outside of
296 * prefix, keep the original path instead */
315 char **path_strv_resolve_uniq(char **l, const char *prefix) {
320 if (!path_strv_resolve(l, prefix))
326 char *path_kill_slashes(char *path) {
330 /* Removes redundant inner and trailing slashes. Modifies the
331 * passed string in-place.
333 * ///foo///bar/ becomes /foo/bar
336 for (f = path, t = path; *f; f++) {
351 /* Special rule, if we are talking of the root directory, a
352 trailing slash is good */
354 if (t == path && slash)
361 char* path_startswith(const char *path, const char *prefix) {
365 if ((path[0] == '/') != (prefix[0] == '/'))
371 path += strspn(path, "/");
372 prefix += strspn(prefix, "/");
380 a = strcspn(path, "/");
381 b = strcspn(prefix, "/");
386 if (memcmp(path, prefix, a) != 0)
394 int path_compare(const char *a, const char *b) {
400 /* A relative path and an abolute path must not compare as equal.
401 * Which one is sorted before the other does not really matter.
402 * Here a relative path is ordered before an absolute path. */
403 d = (a[0] == '/') - (b[0] == '/');
413 if (*a == 0 && *b == 0)
416 /* Order prefixes first: "/foo" before "/foo/bar" */
425 /* Alphabetical sort: "/foo/aaa" before "/foo/b" */
426 d = memcmp(a, b, MIN(j, k));
428 return (d > 0) - (d < 0); /* sign of d */
430 /* Sort "/foo/a" before "/foo/aaa" */
431 d = (j > k) - (j < k); /* sign of (j - k) */
440 bool path_equal(const char *a, const char *b) {
441 return path_compare(a, b) == 0;
444 bool path_equal_or_files_same(const char *a, const char *b) {
445 return path_equal(a, b) || files_same(a, b) > 0;
448 #if 0 /// UNNEEDED by elogind
449 char* path_join(const char *root, const char *path, const char *rest) {
453 return strjoin(root, endswith(root, "/") ? "" : "/",
454 path[0] == '/' ? path+1 : path,
455 rest ? (endswith(path, "/") ? "" : "/") : NULL,
456 rest && rest[0] == '/' ? rest+1 : rest,
460 rest ? (endswith(path, "/") ? "" : "/") : NULL,
461 rest && rest[0] == '/' ? rest+1 : rest,
465 int find_binary(const char *name, char **ret) {
472 if (access(name, X_OK) < 0)
476 r = path_make_absolute_cwd(name, ret);
485 * Plain getenv, not secure_getenv, because we want
486 * to actually allow the user to pick the binary.
492 last_error = -ENOENT;
495 _cleanup_free_ char *j = NULL, *element = NULL;
497 r = extract_first_word(&p, &element, ":", EXTRACT_RELAX|EXTRACT_DONT_COALESCE_SEPARATORS);
503 if (!path_is_absolute(element))
506 j = strjoin(element, "/", name, NULL);
510 if (access(j, X_OK) >= 0) {
514 *ret = path_kill_slashes(j);
527 bool paths_check_timestamp(const char* const* paths, usec_t *timestamp, bool update) {
528 bool changed = false;
529 const char* const* i;
536 STRV_FOREACH(i, paths) {
540 if (stat(*i, &stats) < 0)
543 u = timespec_load(&stats.st_mtim);
549 log_debug("timestamp of '%s' changed", *i);
551 /* update timestamp */
562 static int binary_is_good(const char *binary) {
563 _cleanup_free_ char *p = NULL, *d = NULL;
566 r = find_binary(binary, &p);
572 /* An fsck that is linked to /bin/true is a non-existent
575 r = readlink_malloc(p, &d);
576 if (r == -EINVAL) /* not a symlink */
581 return !path_equal(d, "true") &&
582 !path_equal(d, "/bin/true") &&
583 !path_equal(d, "/usr/bin/true") &&
584 !path_equal(d, "/dev/null");
587 int fsck_exists(const char *fstype) {
592 if (streq(fstype, "auto"))
595 checker = strjoina("fsck.", fstype);
596 return binary_is_good(checker);
599 int mkfs_exists(const char *fstype) {
604 if (streq(fstype, "auto"))
607 mkfs = strjoina("mkfs.", fstype);
608 return binary_is_good(mkfs);
611 char *prefix_root(const char *root, const char *path) {
615 /* If root is passed, prefixes path with it. Otherwise returns
620 /* First, drop duplicate prefixing slashes from the path */
621 while (path[0] == '/' && path[1] == '/')
624 if (isempty(root) || path_equal(root, "/"))
627 l = strlen(root) + 1 + strlen(path) + 1;
635 while (p > n && p[-1] == '/')
645 int parse_path_argument_and_warn(const char *path, bool suppress_root, char **arg) {
650 * This function is intended to be used in command line
651 * parsers, to handle paths that are passed in. It makes the
652 * path absolute, and reduces it to NULL if omitted or
653 * root (the latter optionally).
655 * NOTE THAT THIS WILL FREE THE PREVIOUS ARGUMENT POINTER ON
656 * SUCCESS! Hence, do not pass in uninitialized pointers.
664 r = path_make_absolute_cwd(path, &p);
666 return log_error_errno(r, "Failed to parse path \"%s\" and make it absolute: %m", path);
668 path_kill_slashes(p);
669 if (suppress_root && path_equal(p, "/"))
678 char* dirname_malloc(const char *path) {
679 char *d, *dir, *dir2;
699 bool filename_is_valid(const char *p) {
711 e = strchrnul(p, '/');
715 if (e - p > FILENAME_MAX)
721 bool path_is_safe(const char *p) {
726 if (streq(p, "..") || startswith(p, "../") || endswith(p, "/..") || strstr(p, "/../"))
729 if (strlen(p)+1 > PATH_MAX)
732 /* The following two checks are not really dangerous, but hey, they still are confusing */
733 if (streq(p, ".") || startswith(p, "./") || endswith(p, "/.") || strstr(p, "/./"))
742 char *file_in_same_dir(const char *path, const char *filename) {
749 /* This removes the last component of path and appends
750 * filename, unless the latter is absolute anyway or the
753 if (path_is_absolute(filename))
754 return strdup(filename);
756 e = strrchr(path, '/');
758 return strdup(filename);
760 k = strlen(filename);
761 ret = new(char, (e + 1 - path) + k + 1);
765 memcpy(mempcpy(ret, path, e + 1 - path), filename, k + 1);
769 bool hidden_file_allow_backup(const char *filename) {
773 filename[0] == '.' ||
774 streq(filename, "lost+found") ||
775 streq(filename, "aquota.user") ||
776 streq(filename, "aquota.group") ||
777 endswith(filename, ".rpmnew") ||
778 endswith(filename, ".rpmsave") ||
779 endswith(filename, ".rpmorig") ||
780 endswith(filename, ".dpkg-old") ||
781 endswith(filename, ".dpkg-new") ||
782 endswith(filename, ".dpkg-tmp") ||
783 endswith(filename, ".dpkg-dist") ||
784 endswith(filename, ".dpkg-bak") ||
785 endswith(filename, ".dpkg-backup") ||
786 endswith(filename, ".dpkg-remove") ||
787 endswith(filename, ".swp");
790 bool hidden_file(const char *filename) {
793 if (endswith(filename, "~"))
796 return hidden_file_allow_backup(filename);
799 #if 0 /// UNNEEDED by elogind
800 bool is_device_path(const char *path) {
802 /* Returns true on paths that refer to a device, either in
803 * sysfs or in /dev */
806 path_startswith(path, "/dev/") ||
807 path_startswith(path, "/sys/");