From: Arvin Schnell Date: Tue, 28 Aug 2012 09:18:50 +0000 (+0200) Subject: - work on dbus interface X-Git-Tag: v0.1.3~140 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=a860e6a2ef6e9dac4be31ff6cfdbbba2daa61197;p=thirdparty%2Fsnapper.git - work on dbus interface --- diff --git a/snapper/FileUtils.cc b/snapper/FileUtils.cc index 93f4a815..e40528b1 100644 --- a/snapper/FileUtils.cc +++ b/snapper/FileUtils.cc @@ -49,6 +49,14 @@ namespace snapper y2err("open failed path:" << base_path << " error:" << stringerror(errno)); throw IOErrorException(); } + + struct stat stat; + fstat(dirfd, &stat); + if (!S_ISDIR(stat.st_mode)) + { + y2err("not a directory path:" << base_path); + throw IOErrorException(); + } } @@ -61,6 +69,14 @@ namespace snapper y2err("open failed path:" << dir.fullname(name) << " (" << stringerror(errno) << ")"); throw IOErrorException(); } + + struct stat stat; + fstat(dirfd, &stat); + if (!S_ISDIR(stat.st_mode)) + { + y2err("not a directory path:" << dir.fullname(name)); + throw IOErrorException(); + } } @@ -114,8 +130,22 @@ namespace snapper } + static bool + all_entries(unsigned char type, const char* name) + { + return true; + } + + vector SDir::entries() const + { + return entries(all_entries); + } + + + vector + SDir::entries(std::function pred) const { int fd = dup(dirfd); if (fd == -1) @@ -140,7 +170,8 @@ namespace snapper while (readdir_r(dp, ep, &epp) == 0 && epp != NULL) { - if (strcmp(ep->d_name, ".") != 0 && strcmp(ep->d_name, "..") != 0) + if (strcmp(ep->d_name, ".") != 0 && strcmp(ep->d_name, "..") != 0 && + pred(ep->d_type, ep->d_name)) ret.push_back(ep->d_name); } @@ -168,6 +199,13 @@ namespace snapper } + int + SDir::open(const string& name, int flags, mode_t mode) const + { + return ::openat(dirfd, name.c_str(), flags, mode); + } + + int SDir::readlink(const string& name, string& buf) const { @@ -214,6 +252,42 @@ namespace snapper } + int + SDir::mktemp(string& name) const + { + static const char letters[] = "abcdefghijklmnopqrstuvwxyz" "ABCDEFGHIJKLMNOPQRSTUVWXYZ" + "0123456789"; + + static uint64_t value; + + struct timeval tv; + gettimeofday(&tv, NULL); + value += ((uint64_t) tv.tv_usec << 16) ^ tv.tv_sec; + + unsigned int attempts = 62 * 62 * 62; + + string::size_type length = name.size(); + + for (unsigned int count = 0; count < attempts; value += 7777, ++count) + { + uint64_t v = value; + for (string::size_type i = length - 6; i < length; ++i) + { + name[i] = letters[v % 62]; + v /= 62; + } + + int fd = open(name, O_RDWR | O_CREAT | O_EXCL); + if (fd >= 0) + return fd; + else if (errno != EEXIST) + return -1; + } + + return -1; + } + + SFile::SFile(const SDir& dir, const string& name) : dir(dir), name(name) { diff --git a/snapper/FileUtils.h b/snapper/FileUtils.h index 4877b841..ab1e3837 100644 --- a/snapper/FileUtils.h +++ b/snapper/FileUtils.h @@ -26,6 +26,7 @@ #include #include +#include namespace snapper @@ -49,9 +50,11 @@ namespace snapper string fullname(const string& name, bool with_base_path = true) const; vector entries() const; + vector entries(std::function pred) const; int stat(const string& name, struct stat* buf, int flags) const; int open(const string& name, int flags) const; + int open(const string& name, int flags, mode_t mode) const; int readlink(const string& name, string& buf) const; int mkdir(const string& name, mode_t mode) const; int unlink(const string& name, int flags) const; @@ -59,6 +62,8 @@ namespace snapper int chown(const string& name, uid_t owner, gid_t group, int flags) const; int rename(const string& oldname, const string& newname) const; + int mktemp(string& name) const; + private: const string base_path;