From 528c5711a3bf8ea8effa2a8c6ab7e0b3413ca7ef Mon Sep 17 00:00:00 2001 From: Andre Noll Date: Sun, 16 Mar 2008 16:38:40 +0100 Subject: [PATCH] Switch to gengetopt's group options and add more documentation. --- dss.c | 53 +++++++++++++++++---------------------- dss.ggo | 78 ++++++++++++++++++++++++++++++++++++--------------------- 2 files changed, 73 insertions(+), 58 deletions(-) diff --git a/dss.c b/dss.c index 0913301..f33ce8d 100644 --- a/dss.c +++ b/dss.c @@ -29,29 +29,24 @@ char *dss_error_txt = NULL; DEFINE_DSS_ERRLIST; -/** Defines one dss command. */ -struct server_command { - /** The name of the command. */ - const char *name; - /** Pointer to the function that handles the command. */ - int (*handler)(int, char * const * const); -}; /* a litte cpp magic helps to DRY */ -#define SERVER_COMMANDS \ - SERVER_COMMAND(ls) \ - SERVER_COMMAND(create) \ - SERVER_COMMAND(prune) \ - SERVER_COMMAND(daemon) -#define SERVER_COMMAND(x) int com_ ##x(int, char * const * const); -SERVER_COMMANDS -#undef SERVER_COMMAND -#define SERVER_COMMAND(x) {.name = #x, .handler = com_ ## x}, -static struct server_command command_list[] = { - SERVER_COMMANDS - {.name = NULL, .handler = NULL} -}; -#undef SERVER_COMMAND +#define COMMANDS \ + COMMAND(ls) \ + COMMAND(create) \ + COMMAND(prune) \ + COMMAND(run) +#define COMMAND(x) int com_ ##x(int, char * const * const); +COMMANDS +#undef COMMAND +#define COMMAND(x) if (conf.x ##_given) return com_ ##x(argc, argv); +int call_command_handler(int argc, char * const * const argv) +{ + COMMANDS + return -E_INVALID_COMMAND; +} +#undef COMMAND +#undef COMMANDS /* * complete, not being deleted: 1204565370-1204565371.Sun_Mar_02_2008_14_33-Sun_Mar_02_2008_14_43 @@ -413,6 +408,11 @@ int wait_for_rm_process(pid_t pid) return 1; } +int com_run(int argc, char * const * argv) +{ + return 42; +} + int com_prune(int argc, char * const * argv) { int ret, dry_run = 0; @@ -688,7 +688,7 @@ int check_config(void) int main(int argc, char **argv) { - int i, ret; + int ret; cmdline_parser(argc, argv, &conf); /* aborts on errors */ if (!conf.inputs_num) { @@ -705,14 +705,7 @@ int main(int argc, char **argv) ret = dss_chdir(conf.dest_dir_arg); if (ret < 0) goto out; - for (i = 0; command_list[i].name; i++) { - if (strcmp(command_list[i].name, conf.inputs[0])) - continue; - ret = command_list[i].handler(conf.inputs_num, conf.inputs); - goto out; - } - ret = -E_INVALID_COMMAND; - make_err_msg("%s", conf.inputs[0]); + ret = call_command_handler(conf.inputs_num, conf.inputs); out: if (ret < 0) log_err_msg(EMERG, -ret); diff --git a/dss.ggo b/dss.ggo index fe44aad..88459cd 100644 --- a/dss.ggo +++ b/dss.ggo @@ -1,3 +1,7 @@ +# +package "dss" +version "0.0.1" +purpose "the dyadic snapshot scheduler" text " dss snapshot aging is implemented in terms of intervals. There are @@ -26,9 +30,6 @@ num_intervals snapshots each interval for this to work out. " -package "dss" -version "0.0.1" - option "config_file" c #~~~~~~~~~~~~~~~~~~~~~ "(default='~/.dssrc')" @@ -48,8 +49,53 @@ option "loglevel" l default="4" optional -option "logfile" L +defgroup "command" +groupdesc=" + dss supports a couple of commands each of which corresponds to a different + command line option. Exactly one of these options must be given. +" +required + +groupoption "create" C +#~~~~~~~~~~~~~~~~~~~~~ +"create a new snapshot" +group="command" +details=" + Execute the rsync command to create a new snapshot.Mote that this + command does not care about free disk space. +" +groupoption "prune" P +#~~~~~~~~~~~~~~~~~~~~ +"remove a redundant snapshot" +group="command" +details=" + A snapshot is considered redundant if it ether belongs to + an interval greater than the maximum nuber of intervals, + or if it belongs to an interval that already contains more + than the desired number of snapshots. +" + +groupoption "ls" L +#~~~~~~~~~~~~~~~~~ +"print a list of all snapshots" +group="command" +details=" + The list will contain all snapshots not matter of their state, + i.e. incomplete snapshots and snapshots being deleted will + also be listed. +" + +groupoption "run" R #~~~~~~~~~~~~~~~~~~ +"start creating and pruning snapshots" +group="command" +details=" + This is the main mode of operation. Snapshots will be created + as needed and pruned automatically. +" + +option "logfile" - +#~~~~~~~~~~~~~~~~~ "logfile for the dss daemon process" @@ -196,27 +242,3 @@ details=" dss will start to remove snapshots (starting from the oldest snapshot) until the free disk space exeecds this value. " - - -text " -subcommands: - -ls: - - Print list of existing snapshots. - - Usage: ls - -free: - - Remove old snapshots in order to free space. - - Usage: free [size] - - Without size parameter, print the amount of free space on the file system - in human-readable format. - - Otherwise, remove snapshots (starting from the oldest one) until the number of - free space exceeds the given number of gigabytes. - Use with caution! -" -- 2.39.2