From: Arvin Schnell Date: Wed, 30 Jan 2013 13:34:03 +0000 (+0100) Subject: - added entries_recursive functions X-Git-Tag: v0.1.3~48 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=7e1353cd2f51d45c07d3e6abba97bf29f34ef615;p=thirdparty%2Fsnapper.git - added entries_recursive functions --- diff --git a/snapper/Compare.cc b/snapper/Compare.cc index 25fa5496..065f93b7 100644 --- a/snapper/Compare.cc +++ b/snapper/Compare.cc @@ -333,11 +333,13 @@ namespace snapper { boost::this_thread::interruption_point(); - const vector entries1 = dir1.entries(); + vector entries1 = dir1.entries(); + sort(entries1.begin(), entries1.end()); vector::const_iterator first1 = entries1.begin(); vector::const_iterator last1 = entries1.end(); - const vector entries2 = dir2.entries(); + vector entries2 = dir2.entries(); + sort(entries2.begin(), entries2.end()); vector::const_iterator first2 = entries2.begin(); vector::const_iterator last2 = entries2.end(); diff --git a/snapper/FileUtils.cc b/snapper/FileUtils.cc index 990fa79e..88cffca4 100644 --- a/snapper/FileUtils.cc +++ b/snapper/FileUtils.cc @@ -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 - SDir::entries(std::function 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 + SDir::entries_recursive() const + { + return entries_recursive(all_entries); + } + + + vector + SDir::entries_recursive(entries_pred_t pred) const + { + vector ret; + + vector a = entries(pred); + for (vector::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 b = SDir(*this, *it1).entries_recursive(); + for (vector::const_iterator it2 = b.begin(); it2 != b.end(); ++it2) + { + ret.push_back(*it1 + "/" + *it2); + } + } + } return ret; } diff --git a/snapper/FileUtils.h b/snapper/FileUtils.h index 4f8b0242..30ac834e 100644 --- a/snapper/FileUtils.h +++ b/snapper/FileUtils.h @@ -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 entries_pred_t; + + // The order of the result of the entries functions is undefined. vector entries() const; - vector entries(std::function pred) const; + vector entries(entries_pred_t pred) const; + vector entries_recursive() const; + vector entries_recursive(entries_pred_t pred) const; int stat(struct stat* buf) const;