From 4b33b9b826d88c76dd562bebade1eacfa4a7d4a6 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20H=C3=A4rdeman?= Date: Sat, 19 May 2007 18:09:09 +0200 Subject: [PATCH] Improve some variable names and add comments to the last few functions --- metaentry.c | 87 ++++++++++++++++++++++++++------------------- metaentry.h | 24 ++++++------- metastore.c | 115 ++++++++++++++++++++++++++++++------------------------------ 3 files changed, 121 insertions(+), 105 deletions(-) diff --git a/metaentry.c b/metaentry.c index 28046f2..257f86d 100644 --- a/metaentry.c +++ b/metaentry.c @@ -40,6 +40,7 @@ #include "utils.h" #if 0 +/* Free's a metaentry and all its parameters */ static void mentry_free(struct metaentry *m) { @@ -65,6 +66,7 @@ mentry_free(struct metaentry *m) } #endif +/* Allocates an empty metaentry */ static struct metaentry * mentry_alloc() { @@ -74,25 +76,26 @@ mentry_alloc() return mentry; } +/* Inserts a metaentry into a metaentry list */ static void -mentry_insert(struct metaentry *mentry, struct metaentry **mhead) +mentry_insert(struct metaentry *mentry, struct metaentry **mlist) { struct metaentry *prev; struct metaentry *curr; int comp; - if (!(*mhead)) { - *mhead = mentry; + if (!(*mlist)) { + *mlist = mentry; return; } - if (strcmp(mentry->path, (*mhead)->path) < 0) { - mentry->next = *mhead; - *mhead = mentry; + if (strcmp(mentry->path, (*mlist)->path) < 0) { + mentry->next = *mlist; + *mlist = mentry; return; } - prev = *mhead; + prev = *mlist; for (curr = prev->next; curr; curr = curr->next) { comp = strcmp(mentry->path, curr->path); if (!comp) @@ -109,6 +112,7 @@ mentry_insert(struct metaentry *mentry, struct metaentry **mhead) } #ifdef DEBUG +/* Prints a metaentry */ static void mentry_print(const struct metaentry *mentry) { @@ -139,13 +143,14 @@ mentry_print(const struct metaentry *mentry) msg(MSG_DEBUG, "===========================\n\n"); } +/* Prints all metaentries in a metaentry list */ static void -mentries_print(const struct metaentry *mhead) +mentries_print(const struct metaentry *mlist) { const struct metaentry *mentry; int i; - for (mentry = mhead; mentry; mentry = mentry->next) { + for (mentry = mlist; mentry; mentry = mentry->next) { i++; mentry_print(mentry); } @@ -154,6 +159,7 @@ mentries_print(const struct metaentry *mhead) } #endif +/* Creates a metaentry for the file/dir/etc at path */ static struct metaentry * mentry_create(const char *path) { @@ -255,6 +261,7 @@ mentry_create(const char *path) return mentry; } +/* Cleans up a path and makes it relative to current working dir unless it is absolute */ static char * normalize_path(const char *orig) { @@ -279,8 +286,9 @@ normalize_path(const char *orig) return result; } +/* Internal function for the recursive path walk */ static void -mentries_recurse(const char *path, struct metaentry **mhead) +mentries_recurse(const char *path, struct metaentry **mlist) { struct stat sbuf; struct metaentry *mentry; @@ -301,7 +309,7 @@ mentries_recurse(const char *path, struct metaentry **mhead) if (!mentry) return; - mentry_insert(mentry, mhead); + mentry_insert(mentry, mlist); if (S_ISDIR(sbuf.st_mode)) { dir = opendir(path); @@ -318,23 +326,25 @@ mentries_recurse(const char *path, struct metaentry **mhead) continue; snprintf(tpath, PATH_MAX, "%s/%s", path, dent->d_name); tpath[PATH_MAX - 1] = '\0'; - mentries_recurse(tpath, mhead); + mentries_recurse(tpath, mlist); } closedir(dir); } } +/* Recurses opath and adds metadata entries to the metaentry list */ void -mentries_recurse_path(const char *opath, struct metaentry **mhead) +mentries_recurse_path(const char *opath, struct metaentry **mlist) { char *path = normalize_path(opath); - mentries_recurse(path, mhead); + mentries_recurse(path, mlist); free(path); } +/* Stores a metaentry list to a file */ void -mentries_tofile(const struct metaentry *mhead, const char *path) +mentries_tofile(const struct metaentry *mlist, const char *path) { FILE *to; const struct metaentry *mentry; @@ -349,7 +359,7 @@ mentries_tofile(const struct metaentry *mhead, const char *path) write_binary_string(SIGNATURE, SIGNATURELEN, to); write_binary_string(VERSION, VERSIONLEN, to); - for (mentry = mhead; mentry; mentry = mentry->next) { + for (mentry = mlist; mentry; mentry = mentry->next) { write_string(mentry->path, to); write_string(mentry->owner, to); write_string(mentry->group, to); @@ -368,8 +378,9 @@ mentries_tofile(const struct metaentry *mhead, const char *path) fclose(to); } +/* Creates a metaentry list from a file */ void -mentries_fromfile(struct metaentry **mhead, const char *path) +mentries_fromfile(struct metaentry **mlist, const char *path) { struct metaentry *mentry; char *mmapstart; @@ -434,7 +445,7 @@ mentries_fromfile(struct metaentry **mhead, const char *path) mentry->xattrs = (unsigned int)read_int(&ptr, 4, max); if (!mentry->xattrs) { - mentry_insert(mentry, mhead); + mentry_insert(mentry, mlist); continue; } @@ -454,7 +465,7 @@ mentries_fromfile(struct metaentry **mhead, const char *path) mentry->xattr_lvalues[i], max); } - mentry_insert(mentry, mhead); + mentry_insert(mentry, mlist); } out: @@ -462,20 +473,21 @@ out: close(fd); } +/* Finds a metaentry matching path */ static struct metaentry * -mentry_find(const char *path, struct metaentry *mhead) +mentry_find(const char *path, struct metaentry *mlist) { struct metaentry *m; /* FIXME - We can do a bisect search here instead */ - for (m = mhead; m; m = m->next) { + for (m = mlist; m; m = m->next) { if (!strcmp(path, m->path)) return m; } return NULL; } -/* Returns xattr index in haystack which corresponds to xattr n in needle */ +/* Searches haystack for an xattr matching xattr number n in needle */ int mentry_find_xattr(struct metaentry *haystack, struct metaentry *needle, int n) { @@ -514,6 +526,7 @@ mentry_compare_xattr(struct metaentry *left, struct metaentry *right) return 0; } +/* Compares two metaentries and returns an int with a bitmask of differences */ static int mentry_compare(struct metaentry *left, struct metaentry *right, int do_mtime) { @@ -552,33 +565,35 @@ mentry_compare(struct metaentry *left, struct metaentry *right, int do_mtime) return retval; } +/* Compares lists of real and stored metadata and calls pfunc for each */ void -mentries_compare(struct metaentry *mheadleft, - struct metaentry *mheadright, - void (*pfunc)(struct metaentry *, struct metaentry *, int), +mentries_compare(struct metaentry *mlistreal, + struct metaentry *mliststored, + void (*pfunc) + (struct metaentry *real, struct metaentry *stored, int do_mtime), int do_mtime) { - struct metaentry *left, *right; + struct metaentry *real, *stored; int cmp; - if (!mheadleft || !mheadright) { + if (!mlistreal || !mliststored) { msg(MSG_ERROR, "%s called with empty list\n", __FUNCTION__); return; } - for (left = mheadleft; left; left = left->next) { - right = mentry_find(left->path, mheadright); - if (!right) + for (real = mlistreal; real; real = real->next) { + stored = mentry_find(real->path, mliststored); + if (!stored) cmp = DIFF_ADDED; else - cmp = mentry_compare(left, right, do_mtime); - pfunc(left, right, cmp); + cmp = mentry_compare(real, stored, do_mtime); + pfunc(real, stored, cmp); } - for (right = mheadright; right; right = right->next) { - left = mentry_find(right->path, mheadleft); - if (!left) - pfunc(left, right, DIFF_DELE); + for (stored = mliststored; stored; stored = stored->next) { + real = mentry_find(stored->path, mlistreal); + if (!real) + pfunc(real, stored, DIFF_DELE); } } diff --git a/metaentry.h b/metaentry.h index 5e126ce..7ec9465 100644 --- a/metaentry.h +++ b/metaentry.h @@ -18,16 +18,16 @@ * */ -/* Recurses opath and adds metadata entries to the mhead list */ -void mentries_recurse_path(const char *opath, struct metaentry **mhead); +/* Recurses opath and adds metadata entries to the metaentry list */ +void mentries_recurse_path(const char *opath, struct metaentry **mlist); -/* Stores a metadata list to a file */ -void mentries_tofile(const struct metaentry *mhead, const char *path); +/* Stores a metaentry list to a file */ +void mentries_tofile(const struct metaentry *mlist, const char *path); -/* Creates a metadata list from a file */ -void mentries_fromfile(struct metaentry **mhead, const char *path); +/* Creates a metaentry list from a file */ +void mentries_fromfile(struct metaentry **mlist, const char *path); -/* Searches haystack for an xattr matching xattr n in needle */ +/* Searches haystack for an xattr matching xattr number n in needle */ int mentry_find_xattr(struct metaentry *haystack, struct metaentry *needle, int n); @@ -42,11 +42,11 @@ int mentry_find_xattr(struct metaentry *haystack, #define DIFF_ADDED 0x40 #define DIFF_DELE 0x80 -/* Compares two lists of metaentries and calls pfunc for each entry */ -void mentries_compare(struct metaentry *mheadleft, - struct metaentry *mheadright, - void (*pfunc)(struct metaentry *right, - struct metaentry *left, +/* Compares lists of real and stored metadata and calls pfunc for each */ +void mentries_compare(struct metaentry *mlistreal, + struct metaentry *mliststored, + void (*pfunc)(struct metaentry *real, + struct metaentry *stored, int cmp), int do_mtime); diff --git a/metastore.c b/metastore.c index d831aa7..008a90b 100644 --- a/metastore.c +++ b/metastore.c @@ -37,28 +37,28 @@ static int do_mtime = 0; /* - * Prints differences between stored and actual metadata + * Prints differences between real and stored actual metadata * - for use in mentries_compare */ static void -compare_print(struct metaentry *left, struct metaentry *right, int cmp) +compare_print(struct metaentry *real, struct metaentry *stored, int cmp) { - if (!left) { - msg(MSG_QUIET, "%s:\tremoved\n", right->path); + if (!real) { + msg(MSG_QUIET, "%s:\tremoved\n", stored->path); return; } - if (!right) { - msg(MSG_QUIET, "%s:\tadded\n", left->path); + if (!stored) { + msg(MSG_QUIET, "%s:\tadded\n", real->path); return; } if (cmp == DIFF_NONE) { - msg(MSG_DEBUG, "%s:\tno difference\n", left->path); + msg(MSG_DEBUG, "%s:\tno difference\n", real->path); return; } - msg(MSG_QUIET, "%s:\t", left->path); + msg(MSG_QUIET, "%s:\t", real->path); if (cmp & DIFF_OWNER) msg(MSG_QUIET, "owner "); if (cmp & DIFF_GROUP) @@ -79,7 +79,7 @@ compare_print(struct metaentry *left, struct metaentry *right, int cmp) * - for use in mentries_compare */ static void -compare_fix(struct metaentry *left, struct metaentry *right, int cmp) +compare_fix(struct metaentry *real, struct metaentry *stored, int cmp) { struct group *group; struct passwd *owner; @@ -88,40 +88,40 @@ compare_fix(struct metaentry *left, struct metaentry *right, int cmp) struct utimbuf tbuf; int i; - if (!left && !right) { + if (!real && !stored) { msg(MSG_ERROR, "%s called with incorrect arguments\n", __FUNCTION__); return; } - if (!left) { - msg(MSG_NORMAL, "%s:\tremoved\n", right->path); + if (!real) { + msg(MSG_NORMAL, "%s:\tremoved\n", stored->path); return; } - if (!right) { - msg(MSG_NORMAL, "%s:\tadded\n", left->path); + if (!stored) { + msg(MSG_NORMAL, "%s:\tadded\n", real->path); return; } if (cmp == DIFF_NONE) { - msg(MSG_DEBUG, "%s:\tno difference\n", left->path); + msg(MSG_DEBUG, "%s:\tno difference\n", real->path); return; } if (cmp & DIFF_TYPE) { msg(MSG_NORMAL, "%s:\tnew type, will not change metadata\n", - left->path); + real->path); return; } - msg(MSG_QUIET, "%s:\tchanging metadata\n", left->path); + msg(MSG_QUIET, "%s:\tchanging metadata\n", real->path); while (cmp & (DIFF_OWNER | DIFF_GROUP)) { if (cmp & DIFF_OWNER) { msg(MSG_NORMAL, "\tchanging owner from %s to %s\n", - left->path, left->group, right->group); - owner = getpwnam(right->owner); + real->path, real->group, stored->group); + owner = getpwnam(stored->owner); if (!owner) { msg(MSG_DEBUG, "\tgetpwnam failed: %s\n", strerror(errno)); @@ -132,8 +132,8 @@ compare_fix(struct metaentry *left, struct metaentry *right, int cmp) if (cmp & DIFF_GROUP) { msg(MSG_NORMAL, "\tchanging group from %s to %s\n", - left->path, left->group, right->group); - group = getgrnam(right->group); + real->path, real->group, stored->group); + group = getgrnam(stored->group); if (!group) { msg(MSG_DEBUG, "\tgetgrnam failed: %s\n", strerror(errno)); @@ -142,7 +142,7 @@ compare_fix(struct metaentry *left, struct metaentry *right, int cmp) gid = group->gr_gid; } - if (lchown(left->path, uid, gid)) { + if (lchown(real->path, uid, gid)) { msg(MSG_DEBUG, "\tlchown failed: %s\n", strerror(errno)); break; @@ -152,46 +152,46 @@ compare_fix(struct metaentry *left, struct metaentry *right, int cmp) if (cmp & DIFF_MODE) { 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)) + real->path, real->mode, stored->mode); + if (chmod(real->path, real->mode)) msg(MSG_DEBUG, "\tchmod failed: %s\n", strerror(errno)); } if (cmp & DIFF_MTIME) { msg(MSG_NORMAL, "%s:\tchanging mtime from %ld to %ld\n", - left->path, left->mtime, right->mtime); + real->path, real->mtime, stored->mtime); /* FIXME: Use utimensat here */ - tbuf.actime = right->mtime; - tbuf.modtime = right->mtime; - if (utime(left->path, &tbuf)) { + tbuf.actime = stored->mtime; + tbuf.modtime = stored->mtime; + if (utime(real->path, &tbuf)) { msg(MSG_DEBUG, "\tutime failed: %s\n", strerror(errno)); return; } } if (cmp & DIFF_XATTR) { - for (i = 0; i < left->xattrs; i++) { + for (i = 0; i < real->xattrs; i++) { /* Any attrs to remove? */ - if (mentry_find_xattr(right, left, i) >= 0) + if (mentry_find_xattr(stored, real, i) >= 0) continue; msg(MSG_NORMAL, "%s:\tremoving xattr %s\n", - left->path, left->xattr_names[i]); - if (lremovexattr(left->path, left->xattr_names[i])) + real->path, real->xattr_names[i]); + if (lremovexattr(real->path, real->xattr_names[i])) msg(MSG_DEBUG, "\tlremovexattr failed: %s\n", strerror(errno)); } - for (i = 0; i < right->xattrs; i++) { + for (i = 0; i < stored->xattrs; i++) { /* Any xattrs to add? (on change they are removed above) */ - if (mentry_find_xattr(left, right, i) >= 0) + if (mentry_find_xattr(real, stored, i) >= 0) continue; 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)) + stored->path, stored->xattr_names[i]); + if (lsetxattr(stored->path, stored->xattr_names[i], + stored->xattr_values[i], + stored->xattr_lvalues[i], XATTR_CREATE)) msg(MSG_DEBUG, "\tlsetxattr failed: %s\n", strerror(errno)); } @@ -234,8 +234,8 @@ int main(int argc, char **argv, char **envp) { int i, c; - struct metaentry *mhead = NULL; - struct metaentry *mfhead = NULL; + struct metaentry *lreal = NULL; + struct metaentry *lstored = NULL; int action = 0; /* Parse options */ @@ -248,7 +248,8 @@ main(int argc, char **argv, char **envp) break; switch (c) { case 0: - if (!strcmp("verbose", long_options[option_index].name)) { + if (!strcmp("verbose", + long_options[option_index].name)) { adjust_verbosity(1); } else if (!strcmp("quiet", long_options[option_index].name)) { @@ -298,8 +299,8 @@ main(int argc, char **argv, char **envp) /* Perform action */ switch (action) { case ACTION_DIFF: - mentries_fromfile(&mfhead, METAFILE); - if (!mfhead) { + mentries_fromfile(&lstored, METAFILE); + if (!lstored) { msg(MSG_CRITICAL, "Failed to load metadata from %s\n", METAFILE); exit(EXIT_FAILURE); @@ -307,40 +308,40 @@ main(int argc, char **argv, char **envp) if (optind < argc) { while (optind < argc) - mentries_recurse_path(argv[optind++], &mhead); + mentries_recurse_path(argv[optind++], &lreal); } else { - mentries_recurse_path(".", &mhead); + mentries_recurse_path(".", &lreal); } - if (!mhead) { + if (!lreal) { msg(MSG_CRITICAL, "Failed to load metadata from file system\n"); exit(EXIT_FAILURE); } - mentries_compare(mhead, mfhead, compare_print, do_mtime); + mentries_compare(lreal, lstored, compare_print, do_mtime); break; case ACTION_SAVE: if (optind < argc) { while (optind < argc) - mentries_recurse_path(argv[optind++], &mhead); + mentries_recurse_path(argv[optind++], &lreal); } else { - mentries_recurse_path(".", &mhead); + mentries_recurse_path(".", &lreal); } - if (!mhead) { + if (!lreal) { msg(MSG_CRITICAL, "Failed to load metadata from file system\n"); exit(EXIT_FAILURE); } - mentries_tofile(mhead, METAFILE); + mentries_tofile(lreal, METAFILE); break; case ACTION_APPLY: - mentries_fromfile(&mfhead, METAFILE); - if (!mfhead) { + mentries_fromfile(&lstored, METAFILE); + if (!lstored) { msg(MSG_CRITICAL, "Failed to load metadata from %s\n", METAFILE); exit(EXIT_FAILURE); @@ -348,18 +349,18 @@ main(int argc, char **argv, char **envp) if (optind < argc) { while (optind < argc) - mentries_recurse_path(argv[optind++], &mhead); + mentries_recurse_path(argv[optind++], &lreal); } else { - mentries_recurse_path(".", &mhead); + mentries_recurse_path(".", &lreal); } - if (!mhead) { + if (!lreal) { msg(MSG_CRITICAL, "Failed to load metadata from file system\n"); exit(EXIT_FAILURE); } - mentries_compare(mhead, mfhead, compare_fix, do_mtime); + mentries_compare(lreal, lstored, compare_fix, do_mtime); break; case ACTION_HELP: -- 2.11.4.GIT