]> git.ipfire.org Git - thirdparty/snapper.git/commitdiff
- work on dbus interface
authorArvin Schnell <aschnell@suse.de>
Tue, 28 Aug 2012 09:18:50 +0000 (11:18 +0200)
committerArvin Schnell <aschnell@suse.de>
Tue, 28 Aug 2012 09:18:50 +0000 (11:18 +0200)
snapper/FileUtils.cc
snapper/FileUtils.h

index 93f4a815e8b81ac1adf035afd7f7e9d9d2571640..e40528b19045e929ad4c3aa2d97008985892cca9 100644 (file)
@@ -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<string>
     SDir::entries() const
+    {
+       return entries(all_entries);
+    }
+
+
+    vector<string>
+    SDir::entries(std::function<bool(unsigned char, const char* name)> 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)
     {
index 4877b841087e647c9c2171d68c69771513601e7d..ab1e38377742b06016feebea79716d2c6edad50c 100644 (file)
@@ -26,6 +26,7 @@
 
 #include <string>
 #include <vector>
+#include <algorithm>
 
 
 namespace snapper
@@ -49,9 +50,11 @@ namespace snapper
        string fullname(const string& name, bool with_base_path = true) const;
 
        vector<string> entries() const;
+       vector<string> entries(std::function<bool(unsigned char type, const char* name)> 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;