]> git.ipfire.org Git - thirdparty/snapper.git/commitdiff
- added call to diff for single files
authorArvin Schnell <aschnell@suse.de>
Wed, 23 Feb 2011 10:38:42 +0000 (11:38 +0100)
committerArvin Schnell <aschnell@suse.de>
Wed, 23 Feb 2011 10:38:42 +0000 (11:38 +0100)
snapper.spec.in
snapper/File.cc
snapper/File.h
snapper/SnapperDefines.h
tools/snapper.cc

index 87d51a0aa5b616596edc9735ab98c3ce6baf53d3..ff793eb19f9c22848f0bd326105906b961a30409 100644 (file)
@@ -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
 
index d58bebc28da82ac78cbbe9846702ca13f7af41f5..35805932bedd85957392ec3c6387b6f6bbe73604 100644 (file)
@@ -324,6 +324,17 @@ namespace snapper
     }
 
 
+    vector<string>
+    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()
     {
index 4aad0318197446d1ecdf3a9aa75f497e4292b3c5..82103f63c60c4a601afd1e45b60e4faa66b47ef0 100644 (file)
@@ -87,6 +87,8 @@ namespace snapper
 
        string getAbsolutePath(Location loc) const;
 
+       vector<string> getDiff(const string& options) const;
+
        bool getRollback() const { return rollback; }
        void setRollback(bool value) { rollback = value; }
 
index fd6ded3c658f5f6e20ae3753f296ee12e7d151a8..52c3de1854d425cb69103eb819c0889bc4191c29 100644 (file)
@@ -35,5 +35,7 @@
 
 #define CPBIN "/bin/cp"
 
+#define DIFFBIN "/usr/bin/diff"
+
 
 #endif
index 6f83937200960c9440692cea781ae4eabed956d0..bb191d7b1f83486eeff776f8a44a382384e395ac 100644 (file)
@@ -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 <file>\t\tSave diff to file.") << endl
+        << _("\t--file, -f <file>\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<string> lines = tmp->getDiff("-u");
+       for (vector<string>::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);