delta.c revision 6b01f1d3911bd7c7eadbb8a3b4375bd3ac05c98f
/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
/***
This file is part of systemd.
Copyright 2012 Lennart Poettering
Copyright 2013 Zbigniew Jędrzejewski-Szmek
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 <assert.h>
#include <string.h>
#include <unistd.h>
#include <getopt.h>
#include "hashmap.h"
#include "util.h"
#include "path-util.h"
#include "log.h"
#include "pager.h"
#include "build.h"
#include "strv.h"
static const char prefixes[] =
"/etc\0"
"/run\0"
#ifdef HAVE_SPLIT_USR
"/lib\0"
#endif
;
static const char suffixes[] =
"sysctl.d\0"
"tmpfiles.d\0"
"modules-load.d\0"
"binfmt.d\0"
"systemd/system-preset\0"
"systemd/user-preset\0"
"modprobe.d\0";
static const char have_dropins[] =
static bool arg_no_pager = false;
static int arg_diff = -1;
static enum {
SHOW_MASKED = 1 << 0,
} arg_flags = 0;
static void pager_open_if_enabled(void) {
if (arg_no_pager)
return;
pager_open(false);
}
static int equivalent(const char *a, const char *b) {
x = canonicalize_file_name(a);
if (!x)
return -errno;
y = canonicalize_file_name(b);
if (!y)
return -errno;
return path_equal(x, y);
}
if (!(arg_flags & SHOW_MASKED))
return 0;
printf("%s%s%s %s %s %s\n",
return 1;
}
if (!(arg_flags & SHOW_EQUIVALENT))
return 0;
printf("%s%s%s %s %s %s\n",
return 1;
}
if (!(arg_flags & SHOW_REDIRECTED))
return 0;
printf("%s%s%s %s %s %s\n",
return 1;
}
if (!(arg_flags & SHOW_OVERRIDDEN))
return 0;
printf("%s%s%s %s %s %s\n",
return 1;
}
if (!(arg_flags & SHOW_EXTENDED))
return 0;
printf("%s%s%s %s %s %s\n",
return 1;
}
static int notify_override_unchanged(const char *f) {
if (!(arg_flags & SHOW_UNCHANGED))
return 0;
printf("[UNCHANGED] %s\n", f);
return 1;
}
int k;
if (null_or_empty_path(top) > 0)
if (k >= 0) {
else
}
if (!arg_diff)
return k;
putchar('\n');
if (pid < 0) {
log_error("Failed to fork off diff: %m");
return -errno;
} else if (pid == 0) {
log_error("Failed to execute diff: %m");
_exit(1);
}
putchar('\n');
return k;
}
static int enumerate_dir_d(Hashmap *top, Hashmap *bottom, Hashmap *drops, const char *toppath, const char *drop) {
char **file;
char *c;
int r;
if (!path)
return -ENOMEM;
if (!unit)
return -ENOMEM;
if (!c)
return -EINVAL;
*c = 0;
if (r < 0){
return r;
}
Hashmap *h;
int k;
char *p;
char *d;
continue;
if (!p)
return -ENOMEM;
k = hashmap_put(top, d, p);
if (k >= 0) {
p = strdup(p);
if (!p)
return -ENOMEM;
} else if (k != -EEXIST) {
free(p);
return k;
}
k = hashmap_put(bottom, d, p);
if (k < 0) {
free(p);
return k;
}
if (!h) {
if (!h)
return -ENOMEM;
if (!unit)
return -ENOMEM;
}
p = strdup(p);
if (!p)
return -ENOMEM;
log_debug("Adding to drops: %s %s %s %s %s",
k = hashmap_put(h, basename(p), p);
if (k < 0) {
free(p);
if (k != -EEXIST)
return k;
}
}
return 0;
}
static int enumerate_dir(Hashmap *top, Hashmap *bottom, Hashmap *drops, const char *path, bool dropins) {
if (!d) {
return 0;
return -errno;
}
for (;;) {
int k;
char *p;
errno = 0;
if (!de)
return -errno;
dirent_ensure_type(d, de);
if (!dirent_is_file(de))
continue;
if (!p)
return -ENOMEM;
if (k >= 0) {
p = strdup(p);
if (!p)
return -ENOMEM;
} else if (k != -EEXIST) {
free(p);
return k;
}
if (k < 0) {
free(p);
return k;
}
}
}
const char *p;
char *f;
Hashmap *h;
char *key;
int r = 0, k;
Iterator i, j;
int n_found = 0;
bool dropins;
r = -ENOMEM;
goto finish;
}
NULSTR_FOREACH(p, prefixes) {
_cleanup_free_ char *t = NULL;
if (!t) {
r = -ENOMEM;
goto finish;
}
if (r == 0)
r = k;
}
char *o;
assert(o);
if (path_equal(o, f)) {
} else {
k = found_override(f, o);
if (k < 0)
r = k;
else
n_found += k;
}
}
if (h)
HASHMAP_FOREACH(o, h, j)
n_found += notify_override_extended(f, o);
}
if (top)
if (bottom)
if (drops) {
}
}
return r < 0 ? r : n_found;
}
static int process_suffixes(const char *onlyprefix) {
const char *n;
int n_found = 0, r;
NULSTR_FOREACH(n, suffixes) {
r = process_suffix(n, onlyprefix);
if (r < 0)
return r;
else
n_found += r;
}
return n_found;
}
static int process_suffix_chop(const char *arg) {
const char *p;
if (!path_is_absolute(arg))
/* Strip prefix from the suffix */
NULSTR_FOREACH(p, prefixes) {
if (suffix) {
if (*suffix)
else
return process_suffixes(arg);
}
}
return -EINVAL;
}
static int help(void) {
printf("%s [OPTIONS...] [SUFFIX...]\n\n"
"Find overridden configuration files.\n\n"
" -h --help Show this help\n"
" --version Show package version\n"
" --no-pager Do not pipe output into a pager\n"
" --diff[=1|0] Show a diff when overridden files differ\n"
" -t --type=LIST... Only display a selected set of override types\n",
return 0;
}
char *w, *state;
size_t l;
if (strneq("masked", w, l))
flags |= SHOW_MASKED;
else if (strneq ("equivalent", w, l))
flags |= SHOW_EQUIVALENT;
else if (strneq("redirected", w, l))
flags |= SHOW_REDIRECTED;
else if (strneq("overridden", w, l))
flags |= SHOW_OVERRIDDEN;
else if (strneq("unchanged", w, l))
flags |= SHOW_UNCHANGED;
else if (strneq("extended", w, l))
flags |= SHOW_EXTENDED;
else if (strneq("default", w, l))
flags |= SHOW_DEFAULTS;
else
return -EINVAL;
}
return flags;
}
enum {
ARG_NO_PAGER = 0x100,
};
{}
};
int c;
switch (c) {
case 'h':
help();
return 0;
case ARG_VERSION:
return 0;
case ARG_NO_PAGER:
arg_no_pager = true;
break;
case 't': {
int f;
if (f < 0) {
log_error("Failed to parse flags field.");
return -EINVAL;
}
arg_flags = f;
break;
}
case ARG_DIFF:
if (!optarg)
arg_diff = 1;
else {
int b;
b = parse_boolean(optarg);
if (b < 0) {
log_error("Failed to parse diff boolean.");
return -EINVAL;
} else if (b)
arg_diff = 1;
else
arg_diff = 0;
}
break;
case '?':
return -EINVAL;
default:
assert_not_reached("Unhandled option");
}
}
return 1;
}
int r = 0, k;
int n_found = 0;
log_open();
if (r <= 0)
goto finish;
if (arg_flags == 0)
if (arg_diff < 0)
else if (arg_diff)
int i;
path_kill_slashes(argv[i]);
k = process_suffix_chop(argv[i]);
if (k < 0)
r = k;
else
n_found += k;
}
} else {
k = process_suffixes(NULL);
if (k < 0)
r = k;
else
n_found += k;
}
if (r >= 0)
printf("%s%i overridden configuration files found.\n",
pager_close();
return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
}