From ed02819deb98c8eafe94c9eedc3e9f00e07ea9cc Mon Sep 17 00:00:00 2001 From: Przemyslaw Pawelczyk Date: Fri, 3 May 2013 22:10:48 +0200 Subject: [PATCH] Introduce settings structure. No more passing particular options (like git or mtime) to functions. The structure is meant to be immutable after filling during startup. --- metaentry.c | 12 +++++------ metaentry.h | 8 +++++--- metastore.c | 67 +++++++++++++++++++++++++++++-------------------------------- metastore.h | 4 ---- settings.h | 34 +++++++++++++++++++++++++++++++ 5 files changed, 77 insertions(+), 48 deletions(-) create mode 100644 settings.h diff --git a/metaentry.c b/metaentry.c index 79db236..d2a29e3 100644 --- a/metaentry.c +++ b/metaentry.c @@ -407,13 +407,13 @@ mentries_recurse_wo_git(const char *path, struct metahash *mhash) /* Recurses opath and adds metadata entries to the metaentry list */ void -mentries_recurse_path(const char *opath, struct metahash **mhash, bool git) +mentries_recurse_path(const char *opath, struct metahash **mhash, msettings *st) { char *path = normalize_path(opath); if (!(*mhash)) *mhash = mhash_alloc(); - if (git) + if (st->do_git) mentries_recurse(path, *mhash); else mentries_recurse_wo_git(path, *mhash); @@ -601,7 +601,7 @@ mentry_compare_xattr(struct metaentry *left, struct metaentry *right) /* Compares two metaentries and returns an int with a bitmask of differences */ int -mentry_compare(struct metaentry *left, struct metaentry *right, bool do_mtime) +mentry_compare(struct metaentry *left, struct metaentry *right, msettings *st) { int retval = DIFF_NONE; @@ -625,7 +625,7 @@ mentry_compare(struct metaentry *left, struct metaentry *right, bool do_mtime) if ((left->mode & S_IFMT) != (right->mode & S_IFMT)) retval |= DIFF_TYPE; - if (do_mtime && strcmp(left->path, metafile) && + if (st->do_mtime && strcmp(left->path, st->metafile) && (left->mtime != right->mtime || left->mtimensec != right->mtimensec)) retval |= DIFF_MTIME; @@ -644,7 +644,7 @@ mentries_compare(struct metahash *mhashreal, struct metahash *mhashstored, void (*pfunc) (struct metaentry *real, struct metaentry *stored, int cmp), - bool do_mtime) + msettings *st) { struct metaentry *real, *stored; int key; @@ -661,7 +661,7 @@ mentries_compare(struct metahash *mhashreal, if (!stored) pfunc(real, NULL, DIFF_ADDED); else - pfunc(real, stored, mentry_compare(real, stored, do_mtime)); + pfunc(real, stored, mentry_compare(real, stored, st)); } for (stored = mhashstored->bucket[key]; stored; stored = stored->next) { diff --git a/metaentry.h b/metaentry.h index 3b2f2a1..bd80bc4 100644 --- a/metaentry.h +++ b/metaentry.h @@ -20,6 +20,8 @@ #include +#include "settings.h" + /* Data structure to hold all metadata for a file/dir */ struct metaentry { struct metaentry *next; /* For the metahash chains */ @@ -52,7 +54,7 @@ struct metahash { struct metaentry *mentry_create(const char *path); /* Recurses opath and adds metadata entries to the metaentry list */ -void mentries_recurse_path(const char *opath, struct metahash **mhash, bool git); +void mentries_recurse_path(const char *opath, struct metahash **mhash, msettings *st); /* Stores a metaentry list to a file */ void mentries_tofile(const struct metahash *mhash, const char *path); @@ -78,7 +80,7 @@ int mentry_find_xattr(struct metaentry *haystack, /* Compares two metaentries and returns an int with a bitmask of differences */ int mentry_compare(struct metaentry *left, struct metaentry *right, - bool do_mtime); + msettings *st); /* Compares lists of real and stored metadata and calls pfunc for each */ void mentries_compare(struct metahash *mhashreal, @@ -86,5 +88,5 @@ void mentries_compare(struct metahash *mhashreal, void (*pfunc)(struct metaentry *real, struct metaentry *stored, int cmp), - bool do_mtime); + msettings *st); diff --git a/metastore.c b/metastore.c index d3864f4..de1bf07 100644 --- a/metastore.c +++ b/metastore.c @@ -29,20 +29,17 @@ #include #include "metastore.h" +#include "settings.h" #include "utils.h" #include "metaentry.h" -/* Used to store the path to the file containing the metadata */ -char *metafile = METAFILE; - -/* Used to indicate whether mtimes should be corrected */ -static bool do_mtime = false; - -/* Used to indicate whether empty dirs should be recreated */ -static bool do_emptydirs = false; - -/* Used to indicate whether .git dirs should be processed */ -static bool do_git = false; +/* metastore settings */ +static struct metasettings settings = { + .metafile = METAFILE, + .do_mtime = false, + .do_emptydirs = false, + .do_git = false, +}; /* Used to create lists of dirs / other files which are missing in the fs */ static struct metaentry *missingdirs = NULL; @@ -325,7 +322,7 @@ fixup_emptydirs(struct metahash *real, struct metahash *stored) continue; } - compare_fix(new, cur, mentry_compare(new, cur, do_mtime)); + compare_fix(new, cur, mentry_compare(new, cur, &settings)); } } @@ -395,16 +392,16 @@ main(int argc, char **argv, char **envp) adjust_verbosity(-1); } else if (!strcmp("mtime", long_options[option_index].name)) { - do_mtime = true; + settings.do_mtime = true; } else if (!strcmp("empty-dirs", long_options[option_index].name)) { - do_emptydirs = true; + settings.do_emptydirs = true; } else if (!strcmp("git", long_options[option_index].name)) { - do_git = true; + settings.do_git = true; } else if (!strcmp("file", long_options[option_index].name)) { - metafile = optarg; + settings.metafile = optarg; } else { action |= (1 << option_index); i++; @@ -433,16 +430,16 @@ main(int argc, char **argv, char **envp) adjust_verbosity(-1); break; case 'm': - do_mtime = true; + settings.do_mtime = true; break; case 'e': - do_emptydirs = true; + settings.do_emptydirs = true; break; case 'g': - do_git = true; + settings.do_git = true; break; case 'f': - metafile = optarg; + settings.metafile = optarg; break; default: usage(argv[0], "unknown option"); @@ -454,24 +451,24 @@ main(int argc, char **argv, char **envp) usage(argv[0], "incorrect option(s)"); /* Make sure --empty-dirs is only used with apply */ - if (do_emptydirs && action != ACTION_APPLY) + if (settings.do_emptydirs && action != ACTION_APPLY) usage(argv[0], "--empty-dirs is only valid with --apply"); /* Perform action */ switch (action) { case ACTION_DIFF: - mentries_fromfile(&stored, metafile); + mentries_fromfile(&stored, settings.metafile); if (!stored) { msg(MSG_CRITICAL, "Failed to load metadata from %s\n", - metafile); + settings.metafile); exit(EXIT_FAILURE); } if (optind < argc) { while (optind < argc) - mentries_recurse_path(argv[optind++], &real, do_git); + mentries_recurse_path(argv[optind++], &real, &settings); } else { - mentries_recurse_path(".", &real, do_git); + mentries_recurse_path(".", &real, &settings); } if (!real) { @@ -480,15 +477,15 @@ main(int argc, char **argv, char **envp) exit(EXIT_FAILURE); } - mentries_compare(real, stored, compare_print, do_mtime); + mentries_compare(real, stored, compare_print, &settings); break; case ACTION_SAVE: if (optind < argc) { while (optind < argc) - mentries_recurse_path(argv[optind++], &real, do_git); + mentries_recurse_path(argv[optind++], &real, &settings); } else { - mentries_recurse_path(".", &real, do_git); + mentries_recurse_path(".", &real, &settings); } if (!real) { @@ -497,22 +494,22 @@ main(int argc, char **argv, char **envp) exit(EXIT_FAILURE); } - mentries_tofile(real, metafile); + mentries_tofile(real, settings.metafile); break; case ACTION_APPLY: - mentries_fromfile(&stored, metafile); + mentries_fromfile(&stored, settings.metafile); if (!stored) { msg(MSG_CRITICAL, "Failed to load metadata from %s\n", - metafile); + settings.metafile); exit(EXIT_FAILURE); } if (optind < argc) { while (optind < argc) - mentries_recurse_path(argv[optind++], &real, do_git); + mentries_recurse_path(argv[optind++], &real, &settings); } else { - mentries_recurse_path(".", &real, do_git); + mentries_recurse_path(".", &real, &settings); } if (!real) { @@ -521,9 +518,9 @@ main(int argc, char **argv, char **envp) exit(EXIT_FAILURE); } - mentries_compare(real, stored, compare_fix, do_mtime); + mentries_compare(real, stored, compare_fix, &settings); - if (do_emptydirs) + if (settings.do_emptydirs) fixup_emptydirs(real, stored); break; diff --git a/metastore.h b/metastore.h index 74debb8..7bc7868 100644 --- a/metastore.h +++ b/metastore.h @@ -32,7 +32,3 @@ #define ACTION_SAVE 0x02 #define ACTION_APPLY 0x04 #define ACTION_HELP 0x08 - -/* Used to store the name of the file containing the metadata */ -extern char *metafile; - diff --git a/settings.h b/settings.h new file mode 100644 index 0000000..75489b7 --- /dev/null +++ b/settings.h @@ -0,0 +1,34 @@ +/* + * Copyright (C) 2013 Przemyslaw Pawelczyk + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation version 2 of the License. + * + * This program 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 + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +#ifndef SETTINGS_H +#define SETTINGS_H + +#include + +/* Data structure to hold metastore settings */ +struct metasettings { + char *metafile; /* path to the file containing the metadata */ + bool do_mtime; /* should mtimes be corrected? */ + bool do_emptydirs; /* should empty dirs be recreated? */ + bool do_git; /* should .git dirs be processed? */ +}; + +/* Convenient typedef for immutable settings */ +typedef const struct metasettings msettings; + +#endif /* SETTINGS_H */ -- 2.11.4.GIT