From: Arvin Schnell Date: Wed, 23 Feb 2011 10:38:42 +0000 (+0100) Subject: - added call to diff for single files X-Git-Tag: v0.1.3~461 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=53f491c6958ec6ec85b2e935d56cc1719d0385d2;p=thirdparty%2Fsnapper.git - added call to diff for single files --- diff --git a/snapper.spec.in b/snapper.spec.in index 87d51a0a..ff793eb1 100644 --- a/snapper.spec.in +++ b/snapper.spec.in @@ -13,7 +13,8 @@ Source: snapper-%{version}.tar.bz2 Source1: snapper-rpmlintrc Prefix: /usr BuildRequires: gcc-c++ boost-devel blocxx-devel doxygen dejagnu libxml2-devel -Requires: libsnapper = %version cron +Requires: libsnapper = %version +Recommends: cron Summary: Tool for filesystem snapshot management %description @@ -57,7 +58,7 @@ rm -rf "$RPM_BUILD_ROOT" %package -n libsnapper Summary: Library for filesystem snapshot management Group: System/Libraries -Requires: btrfsprogs +Requires: btrfsprogs diffutils Recommends: logrotate PreReq: %fillup_prereq diff --git a/snapper/File.cc b/snapper/File.cc index d58bebc2..35805932 100644 --- a/snapper/File.cc +++ b/snapper/File.cc @@ -324,6 +324,17 @@ namespace snapper } + vector + File::getDiff(const string& options) const + { + // TODO check for created/deleted + + SystemCmd cmd(DIFFBIN " " + options + " " + quote(getAbsolutePath(LOC_PRE)) + " " + + quote(getAbsolutePath(LOC_POST))); + return cmd.stdout(); + } + + bool File::doRollback() { diff --git a/snapper/File.h b/snapper/File.h index 4aad0318..82103f63 100644 --- a/snapper/File.h +++ b/snapper/File.h @@ -87,6 +87,8 @@ namespace snapper string getAbsolutePath(Location loc) const; + vector getDiff(const string& options) const; + bool getRollback() const { return rollback; } void setRollback(bool value) { rollback = value; } diff --git a/snapper/SnapperDefines.h b/snapper/SnapperDefines.h index fd6ded3c..52c3de18 100644 --- a/snapper/SnapperDefines.h +++ b/snapper/SnapperDefines.h @@ -35,5 +35,7 @@ #define CPBIN "/bin/cp" +#define DIFFBIN "/usr/bin/diff" + #endif diff --git a/tools/snapper.cc b/tools/snapper.cc index 6f839372..bb191d7b 100644 --- a/tools/snapper.cc +++ b/tools/snapper.cc @@ -138,22 +138,22 @@ command_create() bool print_number = false; string cleanup; - GetOpts::parsed_opts::const_iterator it; + GetOpts::parsed_opts::const_iterator opt; - if ((it = opts.find("type")) != opts.end()) - toValue(it->second, type, SINGLE); + if ((opt = opts.find("type")) != opts.end()) + toValue(opt->second, type, SINGLE); - if ((it = opts.find("pre-number")) != opts.end()) - snap1 = readNum(it->second); + if ((opt = opts.find("pre-number")) != opts.end()) + snap1 = readNum(opt->second); - if ((it = opts.find("description")) != opts.end()) - description = it->second; + if ((opt = opts.find("description")) != opts.end()) + description = opt->second; - if ((it = opts.find("print-number")) != opts.end()) + if ((opt = opts.find("print-number")) != opts.end()) print_number = true; - if ((it = opts.find("cleanup")) != opts.end()) - cleanup = it->second; + if ((opt = opts.find("cleanup")) != opts.end()) + cleanup = opt->second; switch (type) { @@ -211,10 +211,10 @@ command_modify() Snapshots::iterator snapshot = readNum(getopts.popArg()); - GetOpts::parsed_opts::const_iterator it; + GetOpts::parsed_opts::const_iterator opt; - if ((it = opts.find("description")) != opts.end()) - snapshot->setDescription(it->second); + if ((opt = opts.find("description")) != opts.end()) + snapshot->setDescription(opt->second); } @@ -253,6 +253,7 @@ help_diff() << endl << _(" Options for 'diff' command:") << endl << _("\t--output, -o \t\tSave diff to file.") << endl + << _("\t--file, -f \t\tRun diff for file.") << endl << endl; } @@ -262,6 +263,7 @@ command_diff() { const struct option options[] = { { "output", required_argument, 0, 'o' }, + { "file", required_argument, 0, 'f' }, { 0, 0, 0, 0 } }; @@ -272,28 +274,50 @@ command_diff() exit(EXIT_FAILURE); } - FILE* file = stdout; + GetOpts::parsed_opts::const_iterator opt; - GetOpts::parsed_opts::const_iterator it; + Snapshots::const_iterator snap1 = readNum(getopts.popArg()); + Snapshots::const_iterator snap2 = readNum(getopts.popArg()); - if ((it = opts.find("output")) != opts.end()) + sh->setComparison(snap1, snap2); + + const Files& files = sh->getFiles(); + + Files::const_iterator tmp = files.end(); + + if ((opt = opts.find("file")) != opts.end()) { - file = fopen(it->second.c_str(), "w"); - if (!file) + tmp = files.find(opt->second); + if (tmp == files.end()) { - cerr << sformat(_("Opening file '%s' failed."), it->second.c_str()) << endl; + cerr << sformat(_("File '%s' not included in diff."), opt->second.c_str()) << endl; exit(EXIT_FAILURE); } } - Snapshots::const_iterator snap1 = readNum(getopts.popArg()); - Snapshots::const_iterator snap2 = readNum(getopts.popArg()); + FILE* file = stdout; - sh->setComparison(snap1, snap2); + if ((opt = opts.find("output")) != opts.end()) + { + file = fopen(opt->second.c_str(), "w"); + if (!file) + { + cerr << sformat(_("Opening file '%s' failed."), opt->second.c_str()) << endl; + exit(EXIT_FAILURE); + } + } - const Files& files = sh->getFiles(); - for (Files::const_iterator it = files.begin(); it != files.end(); ++it) - fprintf(file, "%s %s\n", statusToString(it->getPreToPostStatus()).c_str(), it->getName().c_str()); + if (tmp == files.end()) + { + for (Files::const_iterator it = files.begin(); it != files.end(); ++it) + fprintf(file, "%s %s\n", statusToString(it->getPreToPostStatus()).c_str(), it->getName().c_str()); + } + else + { + vector lines = tmp->getDiff("-u"); + for (vector::const_iterator it = lines.begin(); it != lines.end(); ++it) + fprintf(file, "%s\n", it->c_str()); + } if (file != stdout) fclose(file); @@ -332,14 +356,14 @@ command_rollback() FILE* file = NULL; - GetOpts::parsed_opts::const_iterator it; + GetOpts::parsed_opts::const_iterator opt; - if ((it = opts.find("file")) != opts.end()) + if ((opt = opts.find("file")) != opts.end()) { - file = fopen(it->second.c_str(), "r"); + file = fopen(opt->second.c_str(), "r"); if (!file) { - cerr << sformat(_("Opening file '%s' failed."), it->second.c_str()) << endl; + cerr << sformat(_("Opening file '%s' failed."), opt->second.c_str()) << endl; exit(EXIT_FAILURE); } } @@ -515,15 +539,15 @@ main(int argc, char** argv) exit(EXIT_FAILURE); } - GetOpts::parsed_opts::const_iterator it; + GetOpts::parsed_opts::const_iterator opt; - if ((it = opts.find("quiet")) != opts.end()) + if ((opt = opts.find("quiet")) != opts.end()) quiet = true; - if ((it = opts.find("table-style")) != opts.end()) + if ((opt = opts.find("table-style")) != opts.end()) { unsigned int s; - it->second >> s; + opt->second >> s; if (s >= _End) { cerr << sformat(_("Invalid table style %d."), s) << " " @@ -533,8 +557,8 @@ main(int argc, char** argv) Table::defaultStyle = (TableLineStyle) s; } - if ((it = opts.find("root")) != opts.end()) - root = it->second; + if ((opt = opts.find("root")) != opts.end()) + root = opt->second; sh = createSnapper(root);