]> git.ipfire.org Git - thirdparty/snapper.git/commitdiff
- robuster file reading
authorArvin Schnell <aschnell@suse.de>
Wed, 6 Apr 2011 07:38:28 +0000 (09:38 +0200)
committerArvin Schnell <aschnell@suse.de>
Wed, 6 Apr 2011 07:38:28 +0000 (09:38 +0200)
snapper/AsciiFile.cc
snapper/AsciiFile.h
snapper/File.cc
snapper/Snapper.cc
tools/snapper.cc

index c9bc3406d63ffb030c40e310ff8924a858350dd0..4dbb88b3892d93b5aabe35a142f3f51063f81053 100644 (file)
@@ -34,6 +34,17 @@ namespace snapper
     using namespace std;
 
 
+    AsciiFileReader::AsciiFileReader(FILE* file)
+       : file(file), buffer(NULL), len(0)
+    {
+       if (file == NULL)
+       {
+           y2err("file is NULL");
+           throw exception();  // TODO
+       }
+    }
+
+
     AsciiFileReader::AsciiFileReader(const string& filename)
        : file(NULL), buffer(NULL), len(0)
     {
index 78894397b8482d9db18e11e4250dabae1abbec67..4961e6d88b4a1c30f805392a6734c39ba6e2b4eb 100644 (file)
@@ -39,6 +39,7 @@ namespace snapper
     {
     public:
 
+       AsciiFileReader(FILE* file);
        AsciiFileReader(const string& filename);
        ~AsciiFileReader();
 
index 2eb0a4bd424e309c5c5fb80f4320dd7af8797656..95bbda46aa6897282ca7b377c0c49bf91e6949bb 100644 (file)
@@ -157,16 +157,17 @@ namespace snapper
 
        try
        {
-           AsciiFileReader file(input);
+           AsciiFileReader asciifile(input);
 
            string line;
-           while (file.getline(line))
+           while (asciifile.getline(line))
            {
-               // TODO: more robust splitting
+               string::size_type pos = line.find(" ");
+               if (pos == string::npos)
+                   continue;
 
-               string name = string(line, 5);
-
-               unsigned int status = stringToStatus(string(line, 0, 4));
+               unsigned int status = stringToStatus(string(line, 0, pos));
+               string name = string(line, pos + 1);
 
                if (invert)
                    status = invertStatus(status);
index 12b54334280d8e91c93be3f961e6d2f97c0d6e78..2a636681dccbd8bd929feb3cd367b3fdb96c71e9 100644 (file)
@@ -82,10 +82,10 @@ namespace snapper
        {
            try
            {
-               AsciiFileReader file(*it);
+               AsciiFileReader asciifile(*it);
 
                string line;
-               while (file.getline(line))
+               while (asciifile.getline(line))
                    ignore_patterns.push_back(line);
            }
            catch (...)         // TODO
index 8a2b8a58790ea33eee3a91855347359427251526..44a5d0930584a6f30c34fd73d4fdfd92fa032a2a 100644 (file)
@@ -12,6 +12,7 @@
 #include <snapper/SnapperTmpl.h>
 #include <snapper/Compare.h>
 #include <snapper/Enum.h>
+#include <snapper/AsciiFile.h>
 
 #include "utils/Table.h"
 #include "utils/GetOpts.h"
@@ -375,14 +376,25 @@ command_rollback()
 
     if (file)
     {
-       char* line = NULL;
-       size_t len = 0;
+       AsciiFileReader asciifile(file);
 
-       while (getline(&line, &len, file) != -1)
+       string line;
+       while (asciifile.getline(line))
        {
-           // TODO: more robust splitting, make status optional
+           if (line.empty())
+               continue;
 
-           string name = string(line, 5, strlen(line) - 6);
+           string name = line;
+
+           // strip optional status
+           if (name[0] != '/')
+           {
+               string::size_type pos = name.find(" ");
+               if (pos == string::npos)
+                   continue;
+
+               name.erase(0, pos + 1);
+           }
 
            Files::iterator it = files.find(name);
            if (it == files.end())
@@ -393,10 +405,6 @@ command_rollback()
 
            it->setRollback(true);
        }
-
-       free(line);
-
-       fclose(file);
     }
     else
     {