summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid Härdeman <david@hardeman.nu>2007-05-19 12:25:05 +0200
committerDavid Härdeman <david@hardeman.nu>2007-05-19 12:25:05 +0200
commit22fd68b59d5f792ff033120ad260581ac7fa72ee (patch)
treeda8efee0169c33d8ae532fd31e5a83862688fc52
parent3cb9d64b9b2ace1463f274c8554bffbfa4979e0d (diff)
Change all printf() calls to msg() calls and clean up output
-rw-r--r--metaentry.c68
-rw-r--r--metastore.c139
-rw-r--r--utils.c19
-rw-r--r--utils.h4
4 files changed, 125 insertions, 105 deletions
diff --git a/metaentry.c b/metaentry.c
index 3af4a6c..83f84fa 100644
--- a/metaentry.c
+++ b/metaentry.c
@@ -19,6 +19,7 @@
#include "metaentry.h"
#include "utils.h"
+#if 0
static void
mentry_free(struct metaentry *m)
{
@@ -42,6 +43,7 @@ mentry_free(struct metaentry *m)
free(m);
}
+#endif
static struct metaentry *
mentry_alloc()
@@ -86,33 +88,34 @@ mentry_insert(struct metaentry *mentry, struct metaentry **mhead)
prev->next = mentry;
}
+#ifdef DEBUG
static void
mentry_print(const struct metaentry *mentry)
{
int i;
if (!mentry || !mentry->path) {
- fprintf(stderr, "Incorrect meta entry passed to printmetaentry\n");
+ msg(MSG_DEBUG, "Incorrect meta entry passed to printmetaentry\n");
return;
}
- printf("===========================\n");
- printf("Dump of metaentry %p\n", mentry);
- printf("===========================\n");
+ msg(MSG_DEBUG, "===========================\n");
+ msg(MSG_DEBUG, "Dump of metaentry %p\n", mentry);
+ msg(MSG_DEBUG, "===========================\n");
- printf("path\t\t: %s\n", mentry->path);
- printf("owner\t\t: %s\n", mentry->owner);
- printf("group\t\t: %s\n", mentry->group);
- printf("mtime\t\t: %ld\n", (unsigned long)mentry->mtime);
- printf("mtimensec\t: %ld\n", (unsigned long)mentry->mtimensec);
- printf("mode\t\t: %ld\n", (unsigned long)mentry->mode);
+ msg(MSG_DEBUG, "path\t\t: %s\n", mentry->path);
+ msg(MSG_DEBUG, "owner\t\t: %s\n", mentry->owner);
+ msg(MSG_DEBUG, "group\t\t: %s\n", mentry->group);
+ msg(MSG_DEBUG, "mtime\t\t: %ld\n", (unsigned long)mentry->mtime);
+ msg(MSG_DEBUG, "mtimensec\t: %ld\n", (unsigned long)mentry->mtimensec);
+ msg(MSG_DEBUG, "mode\t\t: %ld\n", (unsigned long)mentry->mode);
for (i = 0; i < mentry->xattrs; i++) {
- printf("xattr[%i]\t: %s=\"", i, mentry->xattr_names[i]);
+ msg(MSG_DEBUG, "xattr[%i]\t: %s=\"", i, mentry->xattr_names[i]);
binary_print(mentry->xattr_values[i], mentry->xattr_lvalues[i]);
- printf("\"\n");
+ msg(MSG_DEBUG, "\"\n");
}
- printf("===========================\n\n");
+ msg(MSG_DEBUG, "===========================\n\n");
}
static void
@@ -126,8 +129,9 @@ mentries_print(const struct metaentry *mhead)
mentry_print(mentry);
}
- printf("%i entries in total\n", i);
+ msg(MSG_DEBUG, "%i entries in total\n", i);
}
+#endif
static struct metaentry *
mentry_create(const char *path)
@@ -141,19 +145,19 @@ mentry_create(const char *path)
struct metaentry *mentry;
if (lstat(path, &sbuf)) {
- perror("lstat");
+ msg(MSG_ERROR, "lstat failed for %s: %s\n", path, strerror(errno));
return NULL;
}
pbuf = getpwuid(sbuf.st_uid);
if (!pbuf) {
- perror("getpwuid");
+ msg(MSG_ERROR, "getpwuid failed for %i: %s\n", (int)sbuf.st_uid, strerror(errno));
return NULL;
}
gbuf = getgrgid(sbuf.st_gid);
if (!gbuf) {
- perror("getgrgid");
+ msg(MSG_ERROR, "getgrgid failed for %i: %s\n", (int)sbuf.st_gid, strerror(errno));
return NULL;
}
@@ -171,14 +175,14 @@ mentry_create(const char *path)
lsize = listxattr(path, NULL, 0);
if (lsize < 0) {
- perror("listxattr");
+ msg(MSG_ERROR, "listxattr failed for %s: %s\n", path, strerror(errno));
return NULL;
}
list = xmalloc(lsize);
lsize = listxattr(path, list, lsize);
if (lsize < 0) {
- perror("listxattr");
+ msg(MSG_ERROR, "listxattr failed for %s: %s\n", path, strerror(errno));
return NULL;
}
@@ -205,7 +209,7 @@ mentry_create(const char *path)
mentry->xattr_names[i] = xstrdup(attr);
vsize = getxattr(path, attr, NULL, 0);
if (vsize < 0) {
- perror("getxattr");
+ msg(MSG_ERROR, "getxattr failed for %s: %s\n", path, strerror(errno));
return NULL;
}
@@ -214,7 +218,7 @@ mentry_create(const char *path)
vsize = getxattr(path, attr, mentry->xattr_values[i], vsize);
if (vsize < 0) {
- perror("getxattr");
+ msg(MSG_ERROR, "getxattr failed for %s: %s\n", path, strerror(errno));
return NULL;
}
i++;
@@ -260,22 +264,20 @@ mentries_recurse(const char *path, struct metaentry **mhead)
return;
if (lstat(path, &sbuf)) {
- printf("Failed to stat %s\n", path);
+ msg(MSG_ERROR, "lstat failed for %s: %s\n", path, strerror(errno));
return;
}
mentry = mentry_create(path);
- if (!mentry) {
- printf("Failed to get metadata for %s\n", path);
+ if (!mentry)
return;
- }
mentry_insert(mentry, mhead);
if (S_ISDIR(sbuf.st_mode)) {
dir = opendir(path);
if (!dir) {
- printf("Failed to open dir %s\n", path);
+ msg(MSG_ERROR, "opendir failed for %s: %s\n", path, strerror(errno));
return;
}
@@ -356,33 +358,33 @@ mentries_fromfile(struct metaentry **mhead, const char *path)
}
if (sbuf.st_size < (SIGNATURELEN + VERSIONLEN)) {
- fprintf(stderr, "Invalid size for file %s\n", path);
+ msg(MSG_CRITICAL, "File %s has an invalid size\n", path);
exit(EXIT_FAILURE);
}
mmapstart = mmap(NULL, (size_t)sbuf.st_size, PROT_READ, MAP_SHARED, fd, 0);
if (mmapstart == MAP_FAILED) {
- perror("mmap");
+ msg(MSG_CRITICAL, "Unable to mmap %s: %s\n", path, strerror(errno));
exit(EXIT_FAILURE);
}
ptr = mmapstart;
max = mmapstart + sbuf.st_size;
if (strncmp(ptr, SIGNATURE, SIGNATURELEN)) {
- printf("Invalid signature for file %s\n", path);
+ msg(MSG_CRITICAL, "Invalid signature for file %s\n", path);
goto out;
}
ptr += SIGNATURELEN;
if (strncmp(ptr, VERSION, VERSIONLEN)) {
- printf("Invalid version for file %s\n", path);
+ msg(MSG_CRITICAL, "Invalid version of file %s\n", path);
goto out;
}
ptr += VERSIONLEN;
while (ptr < mmapstart + sbuf.st_size) {
if (*ptr == '\0') {
- fprintf(stderr, "Invalid characters in file %s\n", path);
+ msg(MSG_CRITICAL, "Invalid characters in file %s\n", path);
goto out;
}
@@ -471,7 +473,7 @@ mentry_compare(struct metaentry *left, struct metaentry *right)
int retval = DIFF_NONE;
if (!left || !right) {
- fprintf(stderr, "mentry_compare called with empty arguments\n");
+ msg(MSG_ERROR, "%s called with empty list\n", __FUNCTION__);
return -1;
}
@@ -511,7 +513,7 @@ mentries_compare(struct metaentry *mheadleft,
int cmp;
if (!mheadleft || !mheadright) {
- fprintf(stderr, "mentries_compare called with empty list\n");
+ msg(MSG_ERROR, "%s called with empty list\n", __FUNCTION__);
return;
}
diff --git a/metastore.c b/metastore.c
index ec28c38..7a2ac7a 100644
--- a/metastore.c
+++ b/metastore.c
@@ -1,4 +1,3 @@
-#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <pwd.h>
@@ -20,34 +19,34 @@ static void
compare_print(struct metaentry *left, struct metaentry *right, int cmp)
{
if (!left) {
- printf("Path %s: removed\n", right->path);
+ msg(MSG_QUIET, "%s:\tremoved\n", right->path);
return;
}
if (!right) {
- printf("Path %s: added\n", left->path);
+ msg(MSG_QUIET, "%s:\tadded\n", left->path);
return;
}
if (cmp == DIFF_NONE) {
- msg(MSG_DEBUG, "Path %s: no difference\n", left->path);
+ msg(MSG_DEBUG, "%s:\tno difference\n", left->path);
return;
}
- printf("Path %s: ", left->path);
+ msg(MSG_QUIET, "%s:\t", left->path);
if (cmp & DIFF_OWNER)
- printf("owner ");
+ msg(MSG_QUIET, "owner ");
if (cmp & DIFF_GROUP)
- printf("group ");
+ msg(MSG_QUIET, "group ");
if (cmp & DIFF_MODE)
- printf("mode ");
+ msg(MSG_QUIET, "mode ");
if (cmp & DIFF_TYPE)
- printf("type ");
+ msg(MSG_QUIET, "type ");
if (cmp & DIFF_MTIME)
- printf("mtime ");
+ msg(MSG_QUIET, "mtime ");
if (cmp & DIFF_XATTR)
- printf("xattr ");
- printf("\n");
+ msg(MSG_QUIET, "xattr ");
+ msg(MSG_QUIET, "\n");
}
static void
@@ -61,73 +60,73 @@ compare_fix(struct metaentry *left, struct metaentry *right, int cmp)
int i;
if (!left && !right) {
- printf("%s called with incorrect arguments\n", __FUNCTION__);
+ msg(MSG_ERROR, "%s called with incorrect arguments\n", __FUNCTION__);
return;
}
if (!left) {
- printf("Path %s: removed\n", right->path);
+ msg(MSG_NORMAL, "%s:\tremoved\n", right->path);
return;
}
if (!right) {
- printf("Path %s: added\n", left->path);
+ msg(MSG_NORMAL, "%s:\tadded\n", left->path);
return;
}
if (cmp == DIFF_NONE) {
- msg(MSG_DEBUG, "Path %s: no difference\n", left->path);
+ msg(MSG_DEBUG, "%s:\tno difference\n", left->path);
return;
}
if (cmp & DIFF_TYPE) {
- printf("Path %s: new type, will not change metadata\n", left->path);
+ msg(MSG_NORMAL, "%s:\tnew type, will not change metadata\n", left->path);
return;
}
- if (cmp & (DIFF_OWNER | DIFF_GROUP)) {
+ msg(MSG_QUIET, "%s:\tchanging metadata\n", left->path);
+
+ while (cmp & (DIFF_OWNER | DIFF_GROUP)) {
if (cmp & DIFF_OWNER) {
- printf("Path %s: fixing owner from %s to %s\n", left->path, left->group, right->group);
+ msg(MSG_NORMAL, "\tchanging owner from %s to %s\n", left->path, left->group, right->group);
owner = getpwnam(right->owner);
if (!owner) {
- perror("getpwnam");
- return;
+ msg(MSG_DEBUG, "\tgetpwnam failed: %s\n", strerror(errno));
+ break;
}
uid = owner->pw_uid;
}
if (cmp & DIFF_GROUP) {
- printf("Path %s: fixing group from %s to %s\n", left->path, left->group, right->group);
+ msg(MSG_NORMAL, "\tchanging group from %s to %s\n", left->path, left->group, right->group);
group = getgrnam(right->group);
if (!group) {
- perror("getgrnam");
- return;
+ msg(MSG_DEBUG, "\tgetgrnam failed: %s\n", strerror(errno));
+ break;
}
gid = group->gr_gid;
}
if (lchown(left->path, uid, gid)) {
- perror("lchown");
- return;
+ msg(MSG_DEBUG, "\tlchown failed: %s\n", strerror(errno));
+ break;
}
- printf("Success\n");
+ break;
}
if (cmp & DIFF_MODE) {
- printf("Path %s: fixing mode from 0%o to 0%o\n", left->path, left->mode, right->mode);
- if (chmod(left->path, left->mode)) {
- perror("chmod");
- return;
- }
+ msg(MSG_NORMAL, "%s:\tchanging mode from 0%o to 0%o\n", left->path, left->mode, right->mode);
+ if (chmod(left->path, left->mode))
+ msg(MSG_DEBUG, "\tchmod failed: %s\n", strerror(errno));
}
if (cmp & DIFF_MTIME) {
- printf("Path %s: fixing mtime %ld to %ld\n", left->path, left->mtime, right->mtime);
+ msg(MSG_NORMAL, "%s:\tchanging mtime from %ld to %ld\n", left->path, left->mtime, right->mtime);
/* FIXME: Use utimensat here */
tbuf.actime = right->mtime;
tbuf.modtime = right->mtime;
if (utime(left->path, &tbuf)) {
- perror("utime");
+ msg(MSG_DEBUG, "\tutime failed: %s\n", strerror(errno));
return;
}
}
@@ -138,10 +137,10 @@ compare_fix(struct metaentry *left, struct metaentry *right, int cmp)
if (mentry_find_xattr(right, left, i) >= 0)
continue;
- msg(MSG_NORMAL, "Path %s: removing xattr %s\n",
+ msg(MSG_NORMAL, "%s:\tremoving xattr %s\n",
left->path, left->xattr_names[i]);
if (lremovexattr(left->path, left->xattr_names[i]))
- perror("lremovexattr");
+ msg(MSG_DEBUG, "\tlremovexattr failed: %s\n", strerror(errno));
}
for (i = 0; i < right->xattrs; i++) {
@@ -149,31 +148,31 @@ compare_fix(struct metaentry *left, struct metaentry *right, int cmp)
if (mentry_find_xattr(left, right, i) >= 0)
continue;
- msg(MSG_NORMAL, "Path %s: adding xattr %s\n",
+ msg(MSG_NORMAL, "%s:\tadding xattr %s\n",
right->path, right->xattr_names[i]);
if (lsetxattr(right->path, right->xattr_names[i],
- right->xattr_values[i], right->xattr_lvalues[i], XATTR_CREATE))
- perror("lsetxattr");
+ right->xattr_values[i], right->xattr_lvalues[i], XATTR_CREATE))
+ msg(MSG_DEBUG, "\tlsetxattr failed: %s\n", strerror(errno));
}
}
}
static void
-usage(const char *arg0, const char *msg)
+usage(const char *arg0, const char *message)
{
- if (msg)
- fprintf(stderr, "%s: %s\n\n", arg0, msg);
- fprintf(stderr, "Usage: %s ACTION [OPTIONS] [PATH]...\n\n", arg0);
- fprintf(stderr, "Where ACTION is one of:\n"
- " -d, --diff\tShow differences between stored and actual metadata\n"
- " -s, --save\tSave current metadata\n"
- " -a, --apply\tApply stored metadata\n"
- " -h, --help\tHelp message (this text)\n\n"
- "Valid OPTIONS are (can be given more than once):\n"
- " -v, --verbose\tPrint more verbose messages\n"
- " -q, --quiet\tPrint less verbose messages\n");
-
- exit(msg ? EXIT_FAILURE : EXIT_SUCCESS);
+ if (message)
+ msg(MSG_CRITICAL, "%s: %s\n\n", arg0, msg);
+ msg(MSG_CRITICAL, "Usage: %s ACTION [OPTIONS] [PATH]...\n\n", arg0);
+ msg(MSG_CRITICAL, "Where ACTION is one of:\n"
+ " -d, --diff\tShow differences between stored and actual metadata\n"
+ " -s, --save\tSave current metadata\n"
+ " -a, --apply\tApply stored metadata\n"
+ " -h, --help\tHelp message (this text)\n\n"
+ "Valid OPTIONS are (can be given more than once):\n"
+ " -v, --verbose\tPrint more verbose messages\n"
+ " -q, --quiet\tPrint less verbose messages\n");
+
+ exit(message ? EXIT_FAILURE : EXIT_SUCCESS);
}
static struct option long_options[] = {
@@ -251,51 +250,63 @@ main(int argc, char **argv, char **envp)
case ACTION_DIFF:
mentries_fromfile(&mfhead, METAFILE);
if (!mfhead) {
- fprintf(stderr, "Failed to load metadata from file\n");
+ msg(MSG_CRITICAL, "Failed to load metadata from file\n");
exit(EXIT_FAILURE);
}
- if (optind < argc)
+ if (optind < argc) {
while (optind < argc)
mentries_recurse_path(argv[optind++], &mhead);
- else
+ } else {
mentries_recurse_path(".", &mhead);
+ }
+
if (!mhead) {
- fprintf(stderr, "Failed to load metadata from fs\n");
+ msg(MSG_CRITICAL, "Failed to load metadata from file system\n");
exit(EXIT_FAILURE);
}
+
mentries_compare(mhead, mfhead, compare_print);
break;
+
case ACTION_SAVE:
- if (optind < argc)
+ if (optind < argc) {
while (optind < argc)
mentries_recurse_path(argv[optind++], &mhead);
- else
+ } else {
mentries_recurse_path(".", &mhead);
+ }
+
if (!mhead) {
- fprintf(stderr, "Failed to load metadata from fs\n");
+ msg(MSG_CRITICAL, "Failed to load metadata from file system\n");
exit(EXIT_FAILURE);
}
+
mentries_tofile(mhead, METAFILE);
break;
+
case ACTION_APPLY:
mentries_fromfile(&mfhead, METAFILE);
if (!mfhead) {
- fprintf(stderr, "Failed to load metadata from file\n");
+ msg(MSG_CRITICAL, "Failed to load metadata from file\n");
exit(EXIT_FAILURE);
}
- if (optind < argc)
+ if (optind < argc) {
while (optind < argc)
mentries_recurse_path(argv[optind++], &mhead);
- else
+ } else {
mentries_recurse_path(".", &mhead);
+ }
+
if (!mhead) {
- fprintf(stderr, "Failed to load metadata from fs\n");
+ msg(MSG_CRITICAL, "Failed to load metadata from file system\n");
exit(EXIT_FAILURE);
}
+
mentries_compare(mhead, mfhead, compare_fix);
break;
+
case ACTION_HELP:
usage(argv[0], NULL);
}
diff --git a/utils.c b/utils.c
index 91cb1ed..dc053ea 100644
--- a/utils.c
+++ b/utils.c
@@ -19,7 +19,12 @@ msg(int level, const char *fmt, ...)
return 0;
va_start(ap, fmt);
- ret = vfprintf(stderr, fmt, ap);
+
+ if (level < MSG_QUIET)
+ ret = vfprintf(stderr, fmt, ap);
+ else
+ ret = vfprintf(stdout, fmt, ap);
+
va_end(ap);
return ret;
}
@@ -29,7 +34,7 @@ xmalloc(size_t size)
{
void *result = malloc(size);
if (!result) {
- fprintf(stderr, "Failed to malloc %zi bytes\n", size);
+ msg(MSG_CRITICAL, "Failed to malloc %zi bytes\n", size);
exit(EXIT_FAILURE);
}
return result;
@@ -40,7 +45,7 @@ xstrdup(const char *s)
{
char *result = strdup(s);
if (!result) {
- fprintf(stderr, "Failed to strdup %zi bytes\n", strlen(s));
+ msg(MSG_CRITICAL, "Failed to strdup %zi bytes\n", strlen(s));
exit(EXIT_FAILURE);
}
return result;
@@ -53,9 +58,9 @@ binary_print(const char *s, ssize_t len)
for (i = 0; i < len; i++) {
if (isprint(s[i]))
- printf("%c", s[i]);
+ msg(MSG_DEBUG, "%c", s[i]);
else
- printf("0x%02X", (int)s[i]);
+ msg(MSG_DEBUG, "0x%02X", (int)s[i]);
}
}
@@ -98,7 +103,7 @@ read_int(char **from, size_t len, const char *max)
int i;
if (*from + len > max) {
- fprintf(stderr, "Attempt to read beyond end of file, corrupt file?\n");
+ msg(MSG_CRITICAL, "Attempt to read beyond end of file, corrupt file?\n");
exit(EXIT_FAILURE);
}
@@ -114,7 +119,7 @@ read_binary_string(char **from, size_t len, const char *max)
char *result;
if (*from + len > max) {
- fprintf(stderr, "Attempt to read beyond end of file, corrupt file?\n");
+ msg(MSG_CRITICAL, "Attempt to read beyond end of file, corrupt file?\n");
exit(EXIT_FAILURE);
}
diff --git a/utils.h b/utils.h
index d9989a4..dac3bc4 100644
--- a/utils.h
+++ b/utils.h
@@ -4,7 +4,9 @@ extern int verbosity;
#define MSG_NORMAL 0
#define MSG_DEBUG 1
#define MSG_QUIET -1
-#define MSG_CRITICAL -2
+#define MSG_ERROR -2
+#define MSG_CRITICAL -3
+
int msg(int level, const char *fmt, ...);
void *xmalloc(size_t size);