delta.c revision 1b12a7b5896f94bdf33b3a6661ebabd761ea6adc
/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
/***
This file is part of systemd.
Copyright 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 <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"
static bool arg_no_pager = false;
static int arg_diff = -1;
static enum {
SHOW_MASKED = 1 << 0,
} arg_flags = 0;
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;
return 1;
}
if (!(arg_flags & SHOW_EQUIVALENT))
return 0;
return 1;
}
if (!(arg_flags & SHOW_REDIRECTED))
return 0;
return 1;
}
if (!(arg_flags & SHOW_OVERRIDDEN))
return 0;
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) {
return 0;
}
if (k >= 0) {
else
return 0;
}
if (!arg_diff)
return 0;
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 0;
}
if (!d) {
return 0;
return -errno;
}
for (;;) {
union dirent_storage buf;
int k;
char *p;
if (k != 0)
return -k;
if (!de)
break;
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;
}
}
return 0;
}
const char *p;
char *f;
int r = 0, k;
Iterator i;
int n_found = 0;
if (!top) {
r = -ENOMEM;
goto finish;
}
if (!bottom) {
r = -ENOMEM;
goto finish;
}
NULSTR_FOREACH(p, prefixes) {
_cleanup_free_ char *t = NULL;
if (!t) {
r = -ENOMEM;
goto finish;
}
if (k < 0)
r = k;
log_debug("Looking at %s", t);
}
HASHMAP_FOREACH(f, top, i) {
char *o;
assert(o);
if (path_equal(o, f)) {
continue;
}
k = found_override(f, o);
if (k < 0)
r = k;
n_found ++;
}
if (top)
if (bottom)
return r < 0 ? r : n_found;
}
const char *p;
if (!path_is_absolute(suffix))
/* Strip prefix from the suffix */
NULSTR_FOREACH(p, prefixes) {
if (startswith(suffix, p)) {
}
}
return -EINVAL;
}
static void 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",
}
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("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 '?':
return -EINVAL;
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;
default:
log_error("Unknown option code %c", c);
return -EINVAL;
}
}
return 1;
}
const char prefixes[] =
"/etc\0"
"/run\0"
#ifdef HAVE_SPLIT_USR
"/lib\0"
#endif
;
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";
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)
if (!arg_no_pager)
pager_open(false);
int i;
if (k < 0)
r = k;
else
n_found += k;
}
} else {
const char *n;
NULSTR_FOREACH(n, suffixes) {
k = process_suffix(prefixes, n);
if (k < 0)
r = k;
else
n_found += k;
}
}
if (r >= 0)
pager_close();
return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
}