]> git.tdb.fi Git - libs/core.git/blobdiff - source/utils.cpp
Add rename function
[libs/core.git] / source / utils.cpp
index bae91cc50898ad83838749f58987a9c3a182d61a..8f06757bda2227f69aa075acc045f1046dba1ce6 100644 (file)
@@ -1,33 +1,64 @@
-/*
-This file is part of libmsppath
-Copyright © 2006  Mikko Rasa, Mikkosoft Productions
+/* $Id$
+
+This file is part of libmspfs
+Copyright © 2006-2008  Mikko Rasa, Mikkosoft Productions
 Distributed under the LGPL
 */
-#include <dirent.h>
-#include <sys/stat.h>
-#include <errno.h>
+
+#include <cerrno>
+#include <cstdio>
+#include <msp/core/except.h>
 #ifndef WIN32
 #include <fnmatch.h>
+#else
+#include <msp/strings/glob.h>
 #endif
-#include <msp/strutils.h>
+#include <msp/strings/utils.h>
+#include "dir.h"
 #include "path.h"
+#include "stat.h"
 #include "utils.h"
 
 using namespace std;
 
 namespace Msp {
-namespace Path {
+namespace FS {
+
+string basename(const Path &p)
+{
+       return p[-1];
+}
+
+Path dirname(const Path &p)
+{
+       if(p.size()==1)
+       {
+               if(p.is_absolute())
+                       return p;
+               return ".";
+       }
+       return p.subpath(0, p.size()-1);
+}
+
+string basepart(const string &fn)
+{
+       unsigned dot=fn.rfind('.');
+       return fn.substr(0, dot);
+}
+
+string extpart(const string &fn)
+{
+       string::size_type dot=fn.rfind('.');
+       if(dot==string::npos)
+               return string();
+       return fn.substr(dot);
+}
 
-/**
-Fixes the case of the given path to match files / directories on the
-filesystem.  Intended to be used in programs that need to interact with
-emulated Windows programs.
-*/
 Path fix_case(const Path &path)
 {
        bool found=true;
        Path result;
-       for(Path::iterator i=path.begin(); i!=path.end(); ++i)
+       for(Path::Iterator i=path.begin(); i!=path.end(); ++i)
        {
                if(!found || *i=="/")
                        result/=*i;
@@ -55,106 +86,84 @@ Path fix_case(const Path &path)
        return result;
 }
 
-int mkpath(const Path &path, int mode)
+Path readlink(const Path &link)
 {
-       Path p;
-       for(Path::iterator i=path.begin(); i!=path.end(); ++i)
-       {
-               p/=*i;
-#ifdef WIN32
-               if(p.size()==1 && is_windows_drive(*i)) continue;
-#endif
-               struct stat st;
-               int err=stat(p.str().c_str(),&st);
-               if(err==0)
-               {
-                       if(!S_ISDIR(st.st_mode))
-                       {
-                               errno=EEXIST;
-                               return -1;
-                       }
-                       continue;
-               }
-               else if(errno!=ENOENT)
-                       return -1;
-               else
-               {
 #ifdef WIN32
-                       // The win32 version of this function doesn't take the mode argument.  Go figure.
-                       err=mkdir(p.str().c_str());
+       (void)link;
+       throw Exception("No symbolic links on win32");
 #else
-                       err=mkdir(p.str().c_str(),mode);
+       char buf[4096];
+       int len=::readlink(link.str().c_str(), buf, sizeof(buf));
+       if(len==-1)
+               throw SystemError("readlink failed", errno);
+       return string(buf, len);
 #endif
-                       if(err==-1) return -1;
-               }
-       }
-
-       return 0;
 }
 
-int rmdir(const Path &path, bool recursive)
+Path realpath(const Path &path)
 {
-       if(recursive)
+#ifdef WIN32
+       if(path.is_absolute())
+               return path;
+       else
+               return getcwd()/path;
+#else
+       list<string> queue(path.begin(), path.end());
+       if(!path.is_absolute())
        {
-               list<string> files=list_files(path);
-               for(list<string>::iterator i=files.begin(); i!=files.end(); ++i)
-               {
-                       Path p=path/ *i;
-                       struct stat st;
-                       stat(p.str().c_str(),&st);
-                       int err=0;
-                       if(S_ISDIR(st.st_mode))
-                               err=rmdir(p,true);
-                       else
-                               err=unlink(p.str().c_str());
-                       if(err) return err;
-               }
+               Path cwd=getcwd();
+               queue.insert(queue.begin(), cwd.begin(), cwd.end());
        }
-       return rmdir(path.str().c_str());
-}
 
-/**
-Lists all files in a directory except the implied . and .. entries.
-*/
-list<string> list_files(const Path &path)
-{
-       list<string> result;
-       DIR *dir=opendir(path.str().c_str());
-       if(dir)
+       Path real;
+       unsigned n_links=0;
+       while(!queue.empty())
        {
-               while(dirent *de=readdir(dir))
+               Path next=real/queue.front();
+               queue.pop_front();
+
+               struct stat st=lstat(next);
+               if(S_ISLNK(st.st_mode))
                {
-                       const char *fn=de->d_name;
-                       if(fn[0]=='.' && (fn[1]==0 || (fn[1]=='.' && fn[2]==0))) continue;
-                       result.push_back(fn);
+                       if(++n_links>64)
+                               throw Exception("Ludicrous amount of symlinks detected in realpath, giving up");
+                       Path link=readlink(next);
+                       queue.insert(queue.begin(), link.begin(), link.end());
                }
-               closedir(dir);
+               else
+                       real=next;
        }
-       return result;
+
+       return real;
+#endif
 }
 
-bool exists(const Path &path)
+void rename(const Path &from, const Path &to)
 {
-       struct stat st;
-       return !stat(path.str().c_str(),&st);
+       if(::rename(from.str().c_str(), to.str().c_str())==-1)
+               throw SystemError("rename failed", errno);
 }
 
-Filename splitext(const string &fn)
+void unlink(const Path &path)
 {
-       Filename result;
-       unsigned dot=fn.rfind('.');
-       result.base=fn.substr(0,dot);
-       if(dot!=string::npos)
-               result.ext=fn.substr(dot);
-       return result;
+       if(::unlink(path.str().c_str())==-1)
+               throw SystemError("unlink failed", errno);
 }
 
-#ifndef WIN32
-int fnmatch(const string &pat, const Path &fn)
+Path relative(const Path &path, const Path &base)
 {
-       return ::fnmatch(pat.c_str(), fn.str().c_str(), FNM_PATHNAME);
+       Path::Iterator i=path.begin();
+       Path::Iterator j=base.begin();
+       for(; (i!=path.end() && j!=base.end() && *i==*j); ++i, ++j) ;
+
+       Path result;
+       for(; j!=base.end(); ++j)
+               result/="..";
+       for(; i!=path.end(); ++i)
+               result/=*i;
+
+       return result;
 }
-#endif
 
-} // namespace Path
+} // namespace FS
 } // namespace Msp