summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPrzemyslaw Pawelczyk <przemoc@gmail.com>2015-09-06 01:19:11 +0200
committerPrzemyslaw Pawelczyk <przemoc@gmail.com>2015-09-06 01:19:11 +0200
commit8bea7c6955d3c6b39cb28403be38e6f278113606 (patch)
tree91643a119da65146945687b228b4f4b5869eb773
parent9ff6ae1499b2744b70ca95fdfe0523f695c9f6f9 (diff)
Simplify code performing actions. Remove duplicated code.
-rw-r--r--metastore.c70
-rw-r--r--metastore.h12
2 files changed, 26 insertions, 56 deletions
diff --git a/metastore.c b/metastore.c
index c4ab66d..0ec29cb 100644
--- a/metastore.c
+++ b/metastore.c
@@ -485,80 +485,46 @@ main(int argc, char **argv)
if (settings.do_removeemptydirs && action != ACTION_APPLY)
usage(argv[0], "--remove-empty-dirs is only valid with --apply");
+ if (action == ACTION_HELP)
+ usage(argv[0], NULL);
+
/* Perform action */
- switch (action) {
- case ACTION_DIFF:
+ if (action & ACTIONS_READING) {
mentries_fromfile(&stored, settings.metafile);
if (!stored) {
msg(MSG_CRITICAL, "Failed to load metadata from %s\n",
settings.metafile);
exit(EXIT_FAILURE);
}
+ }
- if (optind < argc) {
- while (optind < argc)
- mentries_recurse_path(argv[optind++], &real, &settings);
- } else {
- mentries_recurse_path(".", &real, &settings);
- }
+ if (optind < argc) {
+ while (optind < argc)
+ mentries_recurse_path(argv[optind++], &real, &settings);
+ } else {
+ mentries_recurse_path(".", &real, &settings);
+ }
- if (!real) {
- msg(MSG_CRITICAL,
- "Failed to load metadata from file system\n");
- exit(EXIT_FAILURE);
- }
+ if (!real) {
+ msg(MSG_CRITICAL,
+ "Failed to load metadata from file system\n");
+ exit(EXIT_FAILURE);
+ }
+ switch (action) {
+ case ACTION_DIFF:
mentries_compare(real, stored, compare_print, &settings);
break;
-
case ACTION_SAVE:
- if (optind < argc) {
- while (optind < argc)
- mentries_recurse_path(argv[optind++], &real, &settings);
- } else {
- mentries_recurse_path(".", &real, &settings);
- }
-
- if (!real) {
- msg(MSG_CRITICAL,
- "Failed to load metadata from file system\n");
- exit(EXIT_FAILURE);
- }
-
mentries_tofile(real, settings.metafile);
break;
-
case ACTION_APPLY:
- mentries_fromfile(&stored, settings.metafile);
- if (!stored) {
- msg(MSG_CRITICAL, "Failed to load metadata from %s\n",
- settings.metafile);
- exit(EXIT_FAILURE);
- }
-
- if (optind < argc) {
- while (optind < argc)
- mentries_recurse_path(argv[optind++], &real, &settings);
- } else {
- mentries_recurse_path(".", &real, &settings);
- }
-
- if (!real) {
- msg(MSG_CRITICAL,
- "Failed to load metadata from file system\n");
- exit(EXIT_FAILURE);
- }
-
mentries_compare(real, stored, compare_fix, &settings);
-
if (settings.do_emptydirs)
fixup_emptydirs();
if (settings.do_removeemptydirs)
fixup_newemptydirs();
break;
-
- case ACTION_HELP:
- usage(argv[0], NULL);
}
exit(EXIT_SUCCESS);
diff --git a/metastore.h b/metastore.h
index d3bd55f..9dbd19e 100644
--- a/metastore.h
+++ b/metastore.h
@@ -30,9 +30,13 @@
#define METAFILE "./.metadata"
/* Utility defines for the action to take */
-#define ACTION_DIFF 0x01
-#define ACTION_SAVE 0x02
-#define ACTION_APPLY 0x04
-#define ACTION_HELP 0x08
+#define ACTION_APPLY 0x01
+#define ACTION_DIFF 0x02
+#define ACTION_SAVE 0x10
+#define ACTION_HELP 0x80
+
+/* Action masks */
+#define ACTIONS_READING 0x07
+#define ACTIONS_WRITING 0x70
#endif /* METASTORE_H */