diff options
Diffstat (limited to 'src/syrep.c')
-rw-r--r-- | src/syrep.c | 37 |
1 files changed, 32 insertions, 5 deletions
diff --git a/src/syrep.c b/src/syrep.c index 07bbe37..aaf98f4 100644 --- a/src/syrep.c +++ b/src/syrep.c @@ -26,6 +26,7 @@ #include "info.h" #include "history.h" #include "dump.h" +#include "extract.h" struct gengetopt_args_info args; @@ -137,7 +138,7 @@ finish: return r; } -int do_foreach(int (*func) (struct syrep_db_context *c)) { +int do_foreach(int (*func) (struct syrep_db_context *c), int m) { char *path = NULL; int r = 1, i; struct syrep_db_context *c = NULL; @@ -146,6 +147,7 @@ int do_foreach(int (*func) (struct syrep_db_context *c)) { fprintf(stderr, "WARNING: No repository or snapshot to specified!\n"); for (i = 0; i < args.inputs_num; i++) { + static char saved_cwd[PATH_MAX]; if (!(path = strdup(get_attached_filename(args.inputs[i], SYREP_SNAPSHOTFILENAME)))) goto finish; @@ -155,10 +157,33 @@ int do_foreach(int (*func) (struct syrep_db_context *c)) { if (args.inputs_num > 1) fprintf(stderr, "*** %s ***\n", path); + + if (m && args.output_directory_given) { + if (getcwd(saved_cwd, sizeof(saved_cwd)) < 0) { + fprintf(stderr, "getcwd(): %s\n", strerror(errno)); + return -1; + } + + mkdir(args.output_directory_arg, 0777); + + if (chdir(args.output_directory_arg) < 0) { + fprintf(stderr, "Failed to chdir() to directory %s: %s\n", args.output_directory_arg, strerror(errno)); + return -1; + } + } + if (func(c) < 0) goto finish; + if (m && args.output_directory_given) { + if (chdir(saved_cwd) < 0) { + fprintf(stderr, "Failed to chdir() back to directory %s: %s\n", saved_cwd, strerror(errno)); + return -1; + } + } + + if (args.inputs_num > 1 && i < args.inputs_num-1) fprintf(stderr, "\n"); @@ -281,11 +306,11 @@ int main(int argc, char *argv[]) { cmdline_parser(argc, argv, &args); if (args.list_flag) - return do_foreach(list); + return do_foreach(list, 0); else if (args.info_flag) - return do_foreach(info); + return do_foreach(info, 0); else if (args.history_flag) - return do_foreach(history); + return do_foreach(history, 0); else if (args.diff_flag) return do_diff(); else if (args.update_flag) @@ -293,7 +318,9 @@ int main(int argc, char *argv[]) { else if (args.merge_flag) return do_merge(); else if (args.dump_flag) - return do_foreach(dump); + return do_foreach(dump, 0); + else if (args.extract_flag) + return do_foreach(extract, 1); cmdline_parser_print_help(); |