]> git.ipfire.org Git - thirdparty/snapper.git/commitdiff
- added entries_recursive functions
authorArvin Schnell <aschnell@suse.de>
Wed, 30 Jan 2013 13:34:03 +0000 (14:34 +0100)
committerArvin Schnell <aschnell@suse.de>
Wed, 30 Jan 2013 13:34:03 +0000 (14:34 +0100)
snapper/Compare.cc
snapper/FileUtils.cc
snapper/FileUtils.h

index 25fa54962ddfc557d15947d5d2fa9e3077dbef87..065f93b718ef0e999b1b0a34ed4024e51ba1e922 100644 (file)
@@ -333,11 +333,13 @@ namespace snapper
     {
        boost::this_thread::interruption_point();
 
-       const vector<string> entries1 = dir1.entries();
+       vector<string> entries1 = dir1.entries();
+       sort(entries1.begin(), entries1.end());
        vector<string>::const_iterator first1 = entries1.begin();
        vector<string>::const_iterator last1 = entries1.end();
 
-       const vector<string> entries2 = dir2.entries();
+       vector<string> entries2 = dir2.entries();
+       sort(entries2.begin(), entries2.end());
        vector<string>::const_iterator first2 = entries2.begin();
        vector<string>::const_iterator last2 = entries2.end();
 
index 990fa79e50af836d2c05465d2d89cc901d409484..88cffca4dda2d4324f311bf5201e9a76a547c048 100644 (file)
@@ -52,9 +52,9 @@ namespace snapper
            throw IOErrorException();
        }
 
-       struct stat stat;
-       fstat(dirfd, &stat);
-       if (!S_ISDIR(stat.st_mode))
+       struct stat buf;
+       fstat(dirfd, &buf);
+       if (!S_ISDIR(buf.st_mode))
        {
            y2err("not a directory path:" << base_path);
            throw IOErrorException();
@@ -72,9 +72,9 @@ namespace snapper
            throw IOErrorException();
        }
 
-       struct stat stat;
-       fstat(dirfd, &stat);
-       if (!S_ISDIR(stat.st_mode))
+       struct stat buf;
+       fstat(dirfd, &buf);
+       if (!S_ISDIR(buf.st_mode))
        {
            y2err("not a directory path:" << dir.fullname(name));
            throw IOErrorException();
@@ -147,7 +147,7 @@ namespace snapper
 
 
     vector<string>
-    SDir::entries(std::function<bool(unsigned char, const char* name)> pred) const
+    SDir::entries(entries_pred_t pred) const
     {
        int fd = dup(dirfd);
        if (fd == -1)
@@ -170,6 +170,7 @@ namespace snapper
        struct dirent* ep = (struct dirent*) malloc(len);
        struct dirent* epp;
 
+       rewinddir(dp);
        while (readdir_r(dp, ep, &epp) == 0 && epp != NULL)
        {
            if (strcmp(ep->d_name, ".") != 0 && strcmp(ep->d_name, "..") != 0 &&
@@ -181,7 +182,38 @@ namespace snapper
 
        closedir(dp);
 
-       sort(ret.begin(), ret.end());
+       return ret;
+    }
+
+
+    vector<string>
+    SDir::entries_recursive() const
+    {
+       return entries_recursive(all_entries);
+    }
+
+
+    vector<string>
+    SDir::entries_recursive(entries_pred_t pred) const
+    {
+       vector<string> ret;
+
+       vector<string> a = entries(pred);
+       for (vector<string>::const_iterator it1 = a.begin(); it1 != a.end(); ++it1)
+       {
+           ret.push_back(*it1);
+
+           struct stat buf;
+           stat(*it1, &buf, AT_SYMLINK_NOFOLLOW);
+           if (S_ISDIR(buf.st_mode))
+           {
+               vector<string> b = SDir(*this, *it1).entries_recursive();
+               for (vector<string>::const_iterator it2 = b.begin(); it2 != b.end(); ++it2)
+               {
+                   ret.push_back(*it1 + "/" + *it2);
+               }
+           }
+       }
 
        return ret;
     }
index 4f8b02427e1e12b90a1ea5051ca1163078fec98c..30ac834e9ee912cdb5a78afa4d6d936dec40f582 100644 (file)
@@ -51,8 +51,14 @@ namespace snapper
        string fullname(bool with_base_path = true) const;
        string fullname(const string& name, bool with_base_path = true) const;
 
+       // Type is not supported by all file system types, see readdir(3).
+       typedef std::function<bool(unsigned char type, const char* name)> entries_pred_t;
+
+       // The order of the result of the entries functions is undefined.
        vector<string> entries() const;
-       vector<string> entries(std::function<bool(unsigned char type, const char* name)> pred) const;
+       vector<string> entries(entries_pred_t pred) const;
+       vector<string> entries_recursive() const;
+       vector<string> entries_recursive(entries_pred_t pred) const;
 
        int stat(struct stat* buf) const;