]> git.tdb.fi Git - libs/core.git/blobdiff - source/fs/utils.cpp
Add common_ancestor function
[libs/core.git] / source / fs / utils.cpp
index 0c29a5306cf591d7f10abf053c65651804f6a942..1da18591b1c87119c2fccd0012898af78c5e3412 100644 (file)
@@ -1,11 +1,8 @@
-#include <cerrno>
 #include <cstdio>
-#include <msp/core/except.h>
 #ifndef WIN32
-#include <fnmatch.h>
-#else
-#include <msp/strings/glob.h>
+#include <unistd.h>
 #endif
+#include <msp/core/systemerror.h>
 #include <msp/strings/utils.h>
 #include "dir.h"
 #include "path.h"
@@ -83,12 +80,12 @@ Path readlink(const Path &link)
 {
 #ifdef WIN32
        (void)link;
-       throw Exception("No symbolic links on win32");
+       throw logic_error("no symbolic links on win32");
 #else
        char buf[4096];
        int len = ::readlink(link.str().c_str(), buf, sizeof(buf));
        if(len==-1)
-               throw SystemError("readlink failed", errno);
+               throw system_error("readlink");
        return string(buf, len);
 #endif
 }
@@ -115,11 +112,10 @@ Path realpath(const Path &path)
                Path next = real/queue.front();
                queue.pop_front();
 
-               struct stat st = lstat(next);
-               if(S_ISLNK(st.st_mode))
+               if(is_link(next))
                {
                        if(++n_links>64)
-                               throw Exception("Ludicrous amount of symlinks detected in realpath, giving up");
+                               throw runtime_error("too many symbolic links");
                        Path link = readlink(next);
                        queue.insert(queue.begin(), link.begin(), link.end());
                }
@@ -134,13 +130,13 @@ Path realpath(const Path &path)
 void rename(const Path &from, const Path &to)
 {
        if(::rename(from.str().c_str(), to.str().c_str())==-1)
-               throw SystemError("rename failed", errno);
+               throw system_error("rename");
 }
 
 void unlink(const Path &path)
 {
        if(::unlink(path.str().c_str())==-1)
-               throw SystemError("unlink failed", errno);
+               throw system_error("unlink");
 }
 
 Path relative(const Path &path, const Path &base)
@@ -158,6 +154,16 @@ Path relative(const Path &path, const Path &base)
        return result;
 }
 
+Path common_ancestor(const Path &path1, const Path &path2)
+{
+       Path::Iterator i = path1.begin();
+       Path::Iterator j = path2.begin();
+       Path result;
+       for(; (i!=path1.end() && j!=path2.end() && *i==*j); ++i, ++j)
+               result /= *i;
+       return result;
+}
+
 int descendant_depth(const Path &path, const Path &parent)
 {
        Path::Iterator i = path.begin();