path-util.c revision e26d6ce517a49c246141ed20528614823c2f5799
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek/***
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek This file is part of systemd.
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek Copyright 2010-2012 Lennart Poettering
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek systemd is free software; you can redistribute it and/or modify it
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek under the terms of the GNU Lesser General Public License as published by
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek the Free Software Foundation; either version 2.1 of the License, or
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek (at your option) any later version.
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek systemd is distributed in the hope that it will be useful, but
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek WITHOUT ANY WARRANTY; without even the implied warranty of
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek Lesser General Public License for more details.
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek You should have received a copy of the GNU Lesser General Public License
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek along with systemd; If not, see <http://www.gnu.org/licenses/>.
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek***/
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek#include <string.h>
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek#include <unistd.h>
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek#include <errno.h>
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek#include <stdlib.h>
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek#include <stdio.h>
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek#include <fcntl.h>
b5efdb8af40ea759a1ea584c1bc44ecc81dd00ceLennart Poettering#include <sys/statvfs.h>
cc3773810855956bad92337cee8fa193584ab62eLennart Poettering
3ffd4af22052963e7a29431721ee204e634bea75Lennart Poettering#include "macro.h"
3ffd4af22052963e7a29431721ee204e634bea75Lennart Poettering#include "util.h"
3ffd4af22052963e7a29431721ee204e634bea75Lennart Poettering#include "log.h"
cc3773810855956bad92337cee8fa193584ab62eLennart Poettering#include "strv.h"
3ffd4af22052963e7a29431721ee204e634bea75Lennart Poettering#include "path-util.h"
288a74cce597f81d3ba01d8a5ca7d2ba5b654b7eRonny Chevalier#include "missing.h"
3ffd4af22052963e7a29431721ee204e634bea75Lennart Poettering#include "fileio.h"
b1d4f8e154bf61b5de1b27461ef8e9c8c5e838a1Lennart Poettering
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmekbool path_is_absolute(const char *p) {
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek return p[0] == '/';
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek}
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmekbool is_path(const char *p) {
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek return !!strchr(p, '/');
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek}
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek
61376f96a98291b5421bfd79a15ca4db50c2a3feZbigniew Jędrzejewski-Szmekint path_get_parent(const char *path, char **_r) {
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek const char *e, *a = NULL, *b = NULL, *p;
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek char *r;
61376f96a98291b5421bfd79a15ca4db50c2a3feZbigniew Jędrzejewski-Szmek bool slash = false;
61376f96a98291b5421bfd79a15ca4db50c2a3feZbigniew Jędrzejewski-Szmek
61376f96a98291b5421bfd79a15ca4db50c2a3feZbigniew Jędrzejewski-Szmek assert(path);
61376f96a98291b5421bfd79a15ca4db50c2a3feZbigniew Jędrzejewski-Szmek assert(_r);
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek if (!*path)
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek return -EINVAL;
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek for (e = path; *e; e++) {
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek if (!slash && *e == '/') {
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek a = b;
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek b = e;
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek slash = true;
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek } else if (slash && *e != '/')
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek slash = false;
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek }
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek if (*(e-1) == '/')
61376f96a98291b5421bfd79a15ca4db50c2a3feZbigniew Jędrzejewski-Szmek p = a;
61376f96a98291b5421bfd79a15ca4db50c2a3feZbigniew Jędrzejewski-Szmek else
61376f96a98291b5421bfd79a15ca4db50c2a3feZbigniew Jędrzejewski-Szmek p = b;
61376f96a98291b5421bfd79a15ca4db50c2a3feZbigniew Jędrzejewski-Szmek
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek if (!p)
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek return -EINVAL;
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek if (p == path)
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek r = strdup("/");
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek else
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek r = strndup(path, p-path);
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek if (!r)
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek return -ENOMEM;
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek *_r = r;
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek return 0;
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek}
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek
61376f96a98291b5421bfd79a15ca4db50c2a3feZbigniew Jędrzejewski-Szmekchar **path_split_and_make_absolute(const char *p) {
61376f96a98291b5421bfd79a15ca4db50c2a3feZbigniew Jędrzejewski-Szmek char **l;
61376f96a98291b5421bfd79a15ca4db50c2a3feZbigniew Jędrzejewski-Szmek assert(p);
61376f96a98291b5421bfd79a15ca4db50c2a3feZbigniew Jędrzejewski-Szmek
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek l = strv_split(p, ":");
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek if (!l)
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek return NULL;
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek if (!path_strv_make_absolute_cwd(l)) {
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek strv_free(l);
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek return NULL;
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek }
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek return l;
157f50577fbee094eb8ca18f3f0af4e82af8558fDavid Herrmann}
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmekchar *path_make_absolute(const char *p, const char *prefix) {
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek assert(p);
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek
8cb4ab0058e51f1fba93683d145ef95f97c2fa86Lennart Poettering /* Makes every item in the list an absolute path by prepending
61376f96a98291b5421bfd79a15ca4db50c2a3feZbigniew Jędrzejewski-Szmek * the prefix, if specified and necessary */
157f50577fbee094eb8ca18f3f0af4e82af8558fDavid Herrmann
157f50577fbee094eb8ca18f3f0af4e82af8558fDavid Herrmann if (path_is_absolute(p) || !prefix)
157f50577fbee094eb8ca18f3f0af4e82af8558fDavid Herrmann return strdup(p);
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek return strjoin(prefix, "/", p, NULL);
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek}
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmekchar *path_make_absolute_cwd(const char *p) {
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek _cleanup_free_ char *cwd = NULL;
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek assert(p);
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek /* Similar to path_make_absolute(), but prefixes with the
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek * current working directory. */
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek if (path_is_absolute(p))
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek return strdup(p);
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek cwd = get_current_dir_name();
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek if (!cwd)
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek return NULL;
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek return strjoin(cwd, "/", p, NULL);
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek}
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmekint path_make_relative(const char *from_dir, const char *to_path, char **_r) {
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek char *r, *p;
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek unsigned n_parents;
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek assert(from_dir);
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek assert(to_path);
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek assert(_r);
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek /* Strips the common part, and adds ".." elements as necessary. */
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek if (!path_is_absolute(from_dir))
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek return -EINVAL;
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek if (!path_is_absolute(to_path))
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek return -EINVAL;
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek /* Skip the common part. */
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek for (;;) {
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek size_t a;
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek size_t b;
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek from_dir += strspn(from_dir, "/");
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek to_path += strspn(to_path, "/");
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek if (!*from_dir) {
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek if (!*to_path)
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek /* from_dir equals to_path. */
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek r = strdup(".");
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek else
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek /* from_dir is a parent directory of to_path. */
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek r = strdup(to_path);
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek if (!r)
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek return -ENOMEM;
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek path_kill_slashes(r);
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek *_r = r;
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek return 0;
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek }
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek if (!*to_path)
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek break;
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek a = strcspn(from_dir, "/");
61376f96a98291b5421bfd79a15ca4db50c2a3feZbigniew Jędrzejewski-Szmek b = strcspn(to_path, "/");
61376f96a98291b5421bfd79a15ca4db50c2a3feZbigniew Jędrzejewski-Szmek
61376f96a98291b5421bfd79a15ca4db50c2a3feZbigniew Jędrzejewski-Szmek if (a != b)
61376f96a98291b5421bfd79a15ca4db50c2a3feZbigniew Jędrzejewski-Szmek break;
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek if (memcmp(from_dir, to_path, a) != 0)
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek break;
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek from_dir += a;
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek to_path += b;
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek }
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek /* If we're here, then "from_dir" has one or more elements that need to
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek * be replaced with "..". */
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek /* Count the number of necessary ".." elements. */
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek for (n_parents = 0;;) {
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek from_dir += strspn(from_dir, "/");
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek if (!*from_dir)
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek break;
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek from_dir += strcspn(from_dir, "/");
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek n_parents++;
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek }
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek r = malloc(n_parents * 3 + strlen(to_path) + 1);
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek if (!r)
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek return -ENOMEM;
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek for (p = r; n_parents > 0; n_parents--, p += 3)
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek memcpy(p, "../", 3);
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek strcpy(p, to_path);
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek path_kill_slashes(r);
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek *_r = r;
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek return 0;
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek}
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmekchar **path_strv_make_absolute_cwd(char **l) {
6a79c58603ea816a1b4fa1520397b4e138bc1ca0Lennart Poettering char **s;
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek
6a79c58603ea816a1b4fa1520397b4e138bc1ca0Lennart Poettering /* Goes through every item in the string list and makes it
6a79c58603ea816a1b4fa1520397b4e138bc1ca0Lennart Poettering * absolute. This works in place and won't rollback any
6a79c58603ea816a1b4fa1520397b4e138bc1ca0Lennart Poettering * changes on failure. */
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek STRV_FOREACH(s, l) {
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek char *t;
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek t = path_make_absolute_cwd(*s);
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek if (!t)
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek return NULL;
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek free(*s);
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek *s = t;
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek }
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek return l;
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek}
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmekchar **path_strv_resolve(char **l, const char *prefix) {
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek char **s;
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek unsigned k = 0;
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek bool enomem = false;
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek if (strv_isempty(l))
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek return l;
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek /* Goes through every item in the string list and canonicalize
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek * the path. This works in place and won't rollback any
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek * changes on failure. */
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek STRV_FOREACH(s, l) {
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek char *t, *u;
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek _cleanup_free_ char *orig = NULL;
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek if (!path_is_absolute(*s)) {
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek free(*s);
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek continue;
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek }
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek if (prefix) {
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek orig = *s;
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek t = strappend(prefix, orig);
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek if (!t) {
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek enomem = true;
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek continue;
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek }
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek } else
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek t = *s;
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek errno = 0;
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek u = canonicalize_file_name(t);
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek if (!u) {
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek if (errno == ENOENT) {
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek if (prefix) {
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek u = orig;
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek orig = NULL;
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek free(t);
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek } else
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek u = t;
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek } else {
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek free(t);
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek if (errno == ENOMEM || errno == 0)
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek enomem = true;
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek continue;
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek }
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek } else if (prefix) {
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek char *x;
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek free(t);
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek x = path_startswith(u, prefix);
d3e84ddb885e9d5f0ae9930eb905910e3a81f157Lennart Poettering if (x) {
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek /* restore the slash if it was lost */
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek if (!startswith(x, "/"))
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek *(--x) = '/';
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek t = strdup(x);
b80120c4cba7d134b5437a58437a23fdf7ab2084David Herrmann free(u);
b80120c4cba7d134b5437a58437a23fdf7ab2084David Herrmann if (!t) {
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek enomem = true;
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek continue;
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek }
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek u = t;
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek } else {
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek /* canonicalized path goes outside of
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek * prefix, keep the original path instead */
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek free(u);
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek u = orig;
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek orig = NULL;
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek }
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek } else
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek free(t);
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek l[k++] = u;
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek }
d3e84ddb885e9d5f0ae9930eb905910e3a81f157Lennart Poettering
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek l[k] = NULL;
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek if (enomem)
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek return NULL;
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek return l;
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek}
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmekchar **path_strv_resolve_uniq(char **l, const char *prefix) {
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek if (strv_isempty(l))
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek return l;
5cb14b3742038b28551b161635a0cba3559404b2Lennart Poettering
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek if (!path_strv_resolve(l, prefix))
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek return NULL;
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek return strv_uniq(l);
85a428c69465b047731b6abb5005f01824f1444eLennart Poettering}
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmekchar *path_kill_slashes(char *path) {
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek char *f, *t;
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek bool slash = false;
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek /* Removes redundant inner and trailing slashes. Modifies the
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek * passed string in-place.
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek *
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek * ///foo///bar/ becomes /foo/bar
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek */
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek for (f = path, t = path; *f; f++) {
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek if (*f == '/') {
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek slash = true;
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek continue;
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek }
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek if (slash) {
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek slash = false;
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek *(t++) = '/';
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek }
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek *(t++) = *f;
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek }
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek /* Special rule, if we are talking of the root directory, a
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek trailing slash is good */
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek if (t == path && slash)
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek *(t++) = '/';
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek *t = 0;
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek return path;
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek}
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmekchar* path_startswith(const char *path, const char *prefix) {
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek assert(path);
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek assert(prefix);
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek if ((path[0] == '/') != (prefix[0] == '/'))
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek return NULL;
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek for (;;) {
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek size_t a, b;
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek
92bd5ff3a062c3f9475b9d9d39b9335bfeb7705eDavid Herrmann path += strspn(path, "/");
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek prefix += strspn(prefix, "/");
61376f96a98291b5421bfd79a15ca4db50c2a3feZbigniew Jędrzejewski-Szmek
61376f96a98291b5421bfd79a15ca4db50c2a3feZbigniew Jędrzejewski-Szmek if (*prefix == 0)
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek return (char*) path;
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek if (*path == 0)
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek return NULL;
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek a = strcspn(path, "/");
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek b = strcspn(prefix, "/");
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek if (a != b)
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek return NULL;
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek if (memcmp(path, prefix, a) != 0)
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek return NULL;
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek path += a;
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek prefix += b;
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek }
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek}
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmekint path_compare(const char *a, const char *b) {
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek int d;
92bd5ff3a062c3f9475b9d9d39b9335bfeb7705eDavid Herrmann
cc3773810855956bad92337cee8fa193584ab62eLennart Poettering assert(a);
61376f96a98291b5421bfd79a15ca4db50c2a3feZbigniew Jędrzejewski-Szmek assert(b);
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek /* A relative path and an abolute path must not compare as equal.
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek * Which one is sorted before the other does not really matter.
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek * Here a relative path is ordered before an absolute path. */
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek d = (a[0] == '/') - (b[0] == '/');
92bd5ff3a062c3f9475b9d9d39b9335bfeb7705eDavid Herrmann if (d)
92bd5ff3a062c3f9475b9d9d39b9335bfeb7705eDavid Herrmann return d;
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek for (;;) {
92bd5ff3a062c3f9475b9d9d39b9335bfeb7705eDavid Herrmann size_t j, k;
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek a += strspn(a, "/");
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek b += strspn(b, "/");
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek if (*a == 0 && *b == 0)
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek return 0;
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek /* Order prefixes first: "/foo" before "/foo/bar" */
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek if (*a == 0)
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek return -1;
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek if (*b == 0)
61376f96a98291b5421bfd79a15ca4db50c2a3feZbigniew Jędrzejewski-Szmek return 1;
cc3773810855956bad92337cee8fa193584ab62eLennart Poettering
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek j = strcspn(a, "/");
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek k = strcspn(b, "/");
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek /* Alphabetical sort: "/foo/aaa" before "/foo/b" */
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek d = memcmp(a, b, MIN(j, k));
cc3773810855956bad92337cee8fa193584ab62eLennart Poettering if (d)
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek return (d > 0) - (d < 0); /* sign of d */
cc3773810855956bad92337cee8fa193584ab62eLennart Poettering
cc3773810855956bad92337cee8fa193584ab62eLennart Poettering /* Sort "/foo/a" before "/foo/aaa" */
cc3773810855956bad92337cee8fa193584ab62eLennart Poettering d = (j > k) - (j < k); /* sign of (j - k) */
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek if (d)
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek return d;
2b3ab29de466ae6bd7c3243a5a48c7291cc2af0aZbigniew Jędrzejewski-Szmek
2d62c530d2b4c2730abff715b7342f1402114513Lennart Poettering a += j;
602a41c22ac2df33b4b5e5083719c1cfaf58acf9Lennart Poettering b += k;
2d62c530d2b4c2730abff715b7342f1402114513Lennart Poettering }
2d62c530d2b4c2730abff715b7342f1402114513Lennart Poettering}
2d62c530d2b4c2730abff715b7342f1402114513Lennart Poettering
2d62c530d2b4c2730abff715b7342f1402114513Lennart Poetteringbool path_equal(const char *a, const char *b) {
2d62c530d2b4c2730abff715b7342f1402114513Lennart Poettering return path_compare(a, b) == 0;
2d62c530d2b4c2730abff715b7342f1402114513Lennart Poettering}
2d62c530d2b4c2730abff715b7342f1402114513Lennart Poettering
2d62c530d2b4c2730abff715b7342f1402114513Lennart Poetteringbool path_equal_or_files_same(const char *a, const char *b) {
2d62c530d2b4c2730abff715b7342f1402114513Lennart Poettering return path_equal(a, b) || files_same(a, b) > 0;
6a79c58603ea816a1b4fa1520397b4e138bc1ca0Lennart Poettering}
602a41c22ac2df33b4b5e5083719c1cfaf58acf9Lennart Poettering
6a79c58603ea816a1b4fa1520397b4e138bc1ca0Lennart Poetteringchar* path_join(const char *root, const char *path, const char *rest) {
6a79c58603ea816a1b4fa1520397b4e138bc1ca0Lennart Poettering assert(path);
6a79c58603ea816a1b4fa1520397b4e138bc1ca0Lennart Poettering
6a79c58603ea816a1b4fa1520397b4e138bc1ca0Lennart Poettering if (!isempty(root))
6a79c58603ea816a1b4fa1520397b4e138bc1ca0Lennart Poettering return strjoin(root, endswith(root, "/") ? "" : "/",
6a79c58603ea816a1b4fa1520397b4e138bc1ca0Lennart Poettering path[0] == '/' ? path+1 : path,
6a79c58603ea816a1b4fa1520397b4e138bc1ca0Lennart Poettering rest ? (endswith(path, "/") ? "" : "/") : NULL,
6a79c58603ea816a1b4fa1520397b4e138bc1ca0Lennart Poettering rest && rest[0] == '/' ? rest+1 : rest,
6a79c58603ea816a1b4fa1520397b4e138bc1ca0Lennart Poettering NULL);
6a79c58603ea816a1b4fa1520397b4e138bc1ca0Lennart Poettering else
6a79c58603ea816a1b4fa1520397b4e138bc1ca0Lennart Poettering return strjoin(path,
6a79c58603ea816a1b4fa1520397b4e138bc1ca0Lennart Poettering rest ? (endswith(path, "/") ? "" : "/") : NULL,
6a79c58603ea816a1b4fa1520397b4e138bc1ca0Lennart Poettering rest && rest[0] == '/' ? rest+1 : rest,
6a79c58603ea816a1b4fa1520397b4e138bc1ca0Lennart Poettering NULL);
6a79c58603ea816a1b4fa1520397b4e138bc1ca0Lennart Poettering}
6a79c58603ea816a1b4fa1520397b4e138bc1ca0Lennart Poettering
6a79c58603ea816a1b4fa1520397b4e138bc1ca0Lennart Poetteringstatic int fd_fdinfo_mnt_id(int fd, const char *filename, int flags, int *mnt_id) {
6a79c58603ea816a1b4fa1520397b4e138bc1ca0Lennart Poettering char path[strlen("/proc/self/fdinfo/") + DECIMAL_STR_MAX(int)];
6a79c58603ea816a1b4fa1520397b4e138bc1ca0Lennart Poettering _cleanup_free_ char *fdinfo = NULL;
6a79c58603ea816a1b4fa1520397b4e138bc1ca0Lennart Poettering _cleanup_close_ int subfd = -1;
6a79c58603ea816a1b4fa1520397b4e138bc1ca0Lennart Poettering char *p;
602a41c22ac2df33b4b5e5083719c1cfaf58acf9Lennart Poettering int r;
602a41c22ac2df33b4b5e5083719c1cfaf58acf9Lennart Poettering
6a79c58603ea816a1b4fa1520397b4e138bc1ca0Lennart Poettering if ((flags & AT_EMPTY_PATH) && isempty(filename))
6a79c58603ea816a1b4fa1520397b4e138bc1ca0Lennart Poettering xsprintf(path, "/proc/self/fdinfo/%i", fd);
6a79c58603ea816a1b4fa1520397b4e138bc1ca0Lennart Poettering else {
6a79c58603ea816a1b4fa1520397b4e138bc1ca0Lennart Poettering subfd = openat(fd, filename, O_RDONLY|O_CLOEXEC|O_NOCTTY|O_PATH);
6a79c58603ea816a1b4fa1520397b4e138bc1ca0Lennart Poettering if (subfd < 0)
6a79c58603ea816a1b4fa1520397b4e138bc1ca0Lennart Poettering return -errno;
6a79c58603ea816a1b4fa1520397b4e138bc1ca0Lennart Poettering
94036de887ad5b0dc805abe38b5c1c58b57d9465Mantas Mikulėnas xsprintf(path, "/proc/self/fdinfo/%i", subfd);
6a79c58603ea816a1b4fa1520397b4e138bc1ca0Lennart Poettering }
6a79c58603ea816a1b4fa1520397b4e138bc1ca0Lennart Poettering
6a79c58603ea816a1b4fa1520397b4e138bc1ca0Lennart Poettering r = read_full_file(path, &fdinfo, NULL);
6a79c58603ea816a1b4fa1520397b4e138bc1ca0Lennart Poettering if (r == -ENOENT) /* The fdinfo directory is a relatively new addition */
6a79c58603ea816a1b4fa1520397b4e138bc1ca0Lennart Poettering return -EOPNOTSUPP;
6a79c58603ea816a1b4fa1520397b4e138bc1ca0Lennart Poettering if (r < 0)
6a79c58603ea816a1b4fa1520397b4e138bc1ca0Lennart Poettering return -errno;
602a41c22ac2df33b4b5e5083719c1cfaf58acf9Lennart Poettering
602a41c22ac2df33b4b5e5083719c1cfaf58acf9Lennart Poettering p = startswith(fdinfo, "mnt_id:");
602a41c22ac2df33b4b5e5083719c1cfaf58acf9Lennart Poettering if (!p) {
602a41c22ac2df33b4b5e5083719c1cfaf58acf9Lennart Poettering p = strstr(fdinfo, "\nmnt_id:");
602a41c22ac2df33b4b5e5083719c1cfaf58acf9Lennart Poettering if (!p) /* The mnt_id field is a relatively new addition */
602a41c22ac2df33b4b5e5083719c1cfaf58acf9Lennart Poettering return -EOPNOTSUPP;
602a41c22ac2df33b4b5e5083719c1cfaf58acf9Lennart Poettering
602a41c22ac2df33b4b5e5083719c1cfaf58acf9Lennart Poettering p += 8;
602a41c22ac2df33b4b5e5083719c1cfaf58acf9Lennart Poettering }
602a41c22ac2df33b4b5e5083719c1cfaf58acf9Lennart Poettering
602a41c22ac2df33b4b5e5083719c1cfaf58acf9Lennart Poettering p += strspn(p, WHITESPACE);
602a41c22ac2df33b4b5e5083719c1cfaf58acf9Lennart Poettering p[strcspn(p, WHITESPACE)] = 0;
602a41c22ac2df33b4b5e5083719c1cfaf58acf9Lennart Poettering
602a41c22ac2df33b4b5e5083719c1cfaf58acf9Lennart Poettering return safe_atoi(p, mnt_id);
602a41c22ac2df33b4b5e5083719c1cfaf58acf9Lennart Poettering}
602a41c22ac2df33b4b5e5083719c1cfaf58acf9Lennart Poettering
602a41c22ac2df33b4b5e5083719c1cfaf58acf9Lennart Poetteringint fd_is_mount_point(int fd, const char *filename, int flags) {
602a41c22ac2df33b4b5e5083719c1cfaf58acf9Lennart Poettering union file_handle_union h = FILE_HANDLE_INIT, h_parent = FILE_HANDLE_INIT;
602a41c22ac2df33b4b5e5083719c1cfaf58acf9Lennart Poettering int mount_id = -1, mount_id_parent = -1;
602a41c22ac2df33b4b5e5083719c1cfaf58acf9Lennart Poettering bool nosupp = false, check_st_dev = true;
602a41c22ac2df33b4b5e5083719c1cfaf58acf9Lennart Poettering struct stat a, b;
602a41c22ac2df33b4b5e5083719c1cfaf58acf9Lennart Poettering int r;
602a41c22ac2df33b4b5e5083719c1cfaf58acf9Lennart Poettering
602a41c22ac2df33b4b5e5083719c1cfaf58acf9Lennart Poettering assert(fd >= 0);
602a41c22ac2df33b4b5e5083719c1cfaf58acf9Lennart Poettering assert(filename);
602a41c22ac2df33b4b5e5083719c1cfaf58acf9Lennart Poettering
602a41c22ac2df33b4b5e5083719c1cfaf58acf9Lennart Poettering /* First we will try the name_to_handle_at() syscall, which
602a41c22ac2df33b4b5e5083719c1cfaf58acf9Lennart Poettering * tells us the mount id and an opaque file "handle". It is
602a41c22ac2df33b4b5e5083719c1cfaf58acf9Lennart Poettering * not supported everywhere though (kernel compile-time
602a41c22ac2df33b4b5e5083719c1cfaf58acf9Lennart Poettering * option, not all file systems are hooked up). If it works
602a41c22ac2df33b4b5e5083719c1cfaf58acf9Lennart Poettering * the mount id is usually good enough to tell us whether
602a41c22ac2df33b4b5e5083719c1cfaf58acf9Lennart Poettering * something is a mount point.
602a41c22ac2df33b4b5e5083719c1cfaf58acf9Lennart Poettering *
602a41c22ac2df33b4b5e5083719c1cfaf58acf9Lennart Poettering * If that didn't work we will try to read the mount id from
6a79c58603ea816a1b4fa1520397b4e138bc1ca0Lennart Poettering * /proc/self/fdinfo/<fd>. This is almost as good as
6a79c58603ea816a1b4fa1520397b4e138bc1ca0Lennart Poettering * name_to_handle_at(), however, does not return the the
6a79c58603ea816a1b4fa1520397b4e138bc1ca0Lennart Poettering * opaque file handle. The opaque file handle is pretty useful
6a79c58603ea816a1b4fa1520397b4e138bc1ca0Lennart Poettering * to detect the root directory, which we should always
6a79c58603ea816a1b4fa1520397b4e138bc1ca0Lennart Poettering * consider a mount point. Hence we use this only as
6a79c58603ea816a1b4fa1520397b4e138bc1ca0Lennart Poettering * fallback. Exporting the mnt_id in fdinfo is a pretty recent
6a79c58603ea816a1b4fa1520397b4e138bc1ca0Lennart Poettering * kernel addition.
6a79c58603ea816a1b4fa1520397b4e138bc1ca0Lennart Poettering *
6a79c58603ea816a1b4fa1520397b4e138bc1ca0Lennart Poettering * As last fallback we do traditional fstat() based st_dev
3c56cab44150ad47323970cfadfb0257c6305a74Ben Wolsieffer * comparisons. This is how things were traditionally done,
602a41c22ac2df33b4b5e5083719c1cfaf58acf9Lennart Poettering * but unionfs breaks breaks this since it exposes file
3c56cab44150ad47323970cfadfb0257c6305a74Ben Wolsieffer * systems with a variety of st_dev reported. Also, btrfs
3c56cab44150ad47323970cfadfb0257c6305a74Ben Wolsieffer * subvolumes have different st_dev, even though they aren't
3c56cab44150ad47323970cfadfb0257c6305a74Ben Wolsieffer * real mounts of their own. */
3c56cab44150ad47323970cfadfb0257c6305a74Ben Wolsieffer
3c56cab44150ad47323970cfadfb0257c6305a74Ben Wolsieffer r = name_to_handle_at(fd, filename, &h.handle, &mount_id, flags);
3c56cab44150ad47323970cfadfb0257c6305a74Ben Wolsieffer if (r < 0) {
3c56cab44150ad47323970cfadfb0257c6305a74Ben Wolsieffer if (errno == ENOSYS)
3c56cab44150ad47323970cfadfb0257c6305a74Ben Wolsieffer /* This kernel does not support name_to_handle_at()
3c56cab44150ad47323970cfadfb0257c6305a74Ben Wolsieffer * fall back to simpler logic. */
3c56cab44150ad47323970cfadfb0257c6305a74Ben Wolsieffer goto fallback_fdinfo;
602a41c22ac2df33b4b5e5083719c1cfaf58acf9Lennart Poettering else if (errno == EOPNOTSUPP)
3c56cab44150ad47323970cfadfb0257c6305a74Ben Wolsieffer /* This kernel or file system does not support
da927ba997d68401563b927f92e6e40e021a8e5cMichal Schmidt * name_to_handle_at(), hence let's see if the
602a41c22ac2df33b4b5e5083719c1cfaf58acf9Lennart Poettering * upper fs supports it (in which case it is a
602a41c22ac2df33b4b5e5083719c1cfaf58acf9Lennart Poettering * mount point), otherwise fallback to the
3c56cab44150ad47323970cfadfb0257c6305a74Ben Wolsieffer * traditional stat() logic */
3c56cab44150ad47323970cfadfb0257c6305a74Ben Wolsieffer nosupp = true;
3c56cab44150ad47323970cfadfb0257c6305a74Ben Wolsieffer else
3c56cab44150ad47323970cfadfb0257c6305a74Ben Wolsieffer return -errno;
3c56cab44150ad47323970cfadfb0257c6305a74Ben Wolsieffer }
r = name_to_handle_at(fd, "", &h_parent.handle, &mount_id_parent, AT_EMPTY_PATH);
if (r < 0) {
if (errno == EOPNOTSUPP) {
if (nosupp)
/* Neither parent nor child do name_to_handle_at()?
We have no choice but to fall back. */
goto fallback_fdinfo;
else
/* The parent can't do name_to_handle_at() but the
* directory we are interested in can?
* If so, it must be a mount point. */
return 1;
} else
return -errno;
}
/* The parent can do name_to_handle_at() but the
* directory we are interested in can't? If so, it
* must be a mount point. */
if (nosupp)
return 1;
/* If the file handle for the directory we are
* interested in and its parent are identical, we
* assume this is the root directory, which is a mount
* point. */
if (h.handle.handle_bytes == h_parent.handle.handle_bytes &&
h.handle.handle_type == h_parent.handle.handle_type &&
memcmp(h.handle.f_handle, h_parent.handle.f_handle, h.handle.handle_bytes) == 0)
return 1;
return mount_id != mount_id_parent;
fallback_fdinfo:
r = fd_fdinfo_mnt_id(fd, filename, flags, &mount_id);
if (r == -EOPNOTSUPP)
goto fallback_fstat;
if (r < 0)
return r;
r = fd_fdinfo_mnt_id(fd, "", AT_EMPTY_PATH, &mount_id_parent);
if (r < 0)
return r;
if (mount_id != mount_id_parent)
return 1;
/* Hmm, so, the mount ids are the same. This leaves one
* special case though for the root file system. For that,
* let's see if the parent directory has the same inode as we
* are interested in. Hence, let's also do fstat() checks now,
* too, but avoid the st_dev comparisons, since they aren't
* that useful on unionfs mounts. */
check_st_dev = false;
fallback_fstat:
/* yay for fstatat() taking a different set of flags than the other
* _at() above */
if (flags & AT_SYMLINK_FOLLOW)
flags &= ~AT_SYMLINK_FOLLOW;
else
flags |= AT_SYMLINK_NOFOLLOW;
if (fstatat(fd, filename, &a, flags) < 0)
return -errno;
if (fstatat(fd, "", &b, AT_EMPTY_PATH) < 0)
return -errno;
/* A directory with same device and inode as its parent? Must
* be the root directory */
if (a.st_dev == b.st_dev &&
a.st_ino == b.st_ino)
return 1;
return check_st_dev && (a.st_dev != b.st_dev);
}
/* flags can be AT_SYMLINK_FOLLOW or 0 */
int path_is_mount_point(const char *t, int flags) {
_cleanup_close_ int fd = -1;
_cleanup_free_ char *parent = NULL;
int r;
assert(t);
if (path_equal(t, "/"))
return 1;
r = path_get_parent(t, &parent);
if (r < 0)
return r;
fd = openat(AT_FDCWD, parent, O_RDONLY|O_NONBLOCK|O_DIRECTORY|O_CLOEXEC|O_PATH);
if (fd < 0)
return -errno;
return fd_is_mount_point(fd, basename(t), flags);
}
int path_is_read_only_fs(const char *path) {
struct statvfs st;
assert(path);
if (statvfs(path, &st) < 0)
return -errno;
if (st.f_flag & ST_RDONLY)
return true;
/* On NFS, statvfs() might not reflect whether we can actually
* write to the remote share. Let's try again with
* access(W_OK) which is more reliable, at least sometimes. */
if (access(path, W_OK) < 0 && errno == EROFS)
return true;
return false;
}
int path_is_os_tree(const char *path) {
char *p;
int r;
/* We use /usr/lib/os-release as flag file if something is an OS */
p = strjoina(path, "/usr/lib/os-release");
r = access(p, F_OK);
if (r >= 0)
return 1;
/* Also check for the old location in /etc, just in case. */
p = strjoina(path, "/etc/os-release");
r = access(p, F_OK);
return r >= 0;
}
int find_binary(const char *name, bool local, char **filename) {
assert(name);
if (is_path(name)) {
if (local && access(name, X_OK) < 0)
return -errno;
if (filename) {
char *p;
p = path_make_absolute_cwd(name);
if (!p)
return -ENOMEM;
*filename = p;
}
return 0;
} else {
const char *path;
const char *word, *state;
size_t l;
/**
* Plain getenv, not secure_getenv, because we want
* to actually allow the user to pick the binary.
*/
path = getenv("PATH");
if (!path)
path = DEFAULT_PATH;
FOREACH_WORD_SEPARATOR(word, l, path, ":", state) {
_cleanup_free_ char *p = NULL;
if (asprintf(&p, "%.*s/%s", (int) l, word, name) < 0)
return -ENOMEM;
if (access(p, X_OK) < 0)
continue;
if (filename) {
*filename = path_kill_slashes(p);
p = NULL;
}
return 0;
}
return -ENOENT;
}
}
bool paths_check_timestamp(const char* const* paths, usec_t *timestamp, bool update) {
bool changed = false;
const char* const* i;
assert(timestamp);
if (paths == NULL)
return false;
STRV_FOREACH(i, paths) {
struct stat stats;
usec_t u;
if (stat(*i, &stats) < 0)
continue;
u = timespec_load(&stats.st_mtim);
/* 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 fsck_exists(const char *fstype) {
_cleanup_free_ char *p = NULL, *d = NULL;
const char *checker;
int r;
checker = strjoina("fsck.", fstype);
r = find_binary(checker, true, &p);
if (r < 0)
return r;
/* An fsck that is linked to /bin/true is a non-existent
* fsck */
r = readlink_malloc(p, &d);
if (r >= 0 &&
(path_equal(d, "/bin/true") ||
path_equal(d, "/usr/bin/true") ||
path_equal(d, "/dev/null")))
return -ENOENT;
return 0;
}
char *prefix_root(const char *root, const char *path) {
char *n, *p;
size_t l;
/* If root is passed, prefixes path with it. Otherwise returns
* it as is. */
assert(path);
/* First, drop duplicate prefixing slashes from the path */
while (path[0] == '/' && path[1] == '/')
path++;
if (isempty(root) || path_equal(root, "/"))
return strdup(path);
l = strlen(root) + 1 + strlen(path) + 1;
n = new(char, l);
if (!n)
return NULL;
p = stpcpy(n, root);
while (p > n && p[-1] == '/')
p--;
if (path[0] != '/')
*(p++) = '/';
strcpy(p, path);
return n;
}