3 This file is part of libmspfs
4 Copyright © 2006-2008 Mikko Rasa, Mikkosoft Productions
5 Distributed under the LGPL
12 #include <msp/core/except.h>
23 void mkdir(const Path &path, int mode)
27 // The win32 version of this function doesn't take the mode argument. Go figure.
29 err=::mkdir(path.str().c_str());
31 err=::mkdir(path.str().c_str(), mode);
35 throw SystemError("mkdir failed", errno);
39 Creates a directory and any parent directories if needed.
41 @param path The path to create
42 @param mode Access mode for new directories
44 @return 0 on success, -1 on error
46 void mkpath(const Path &path, int mode)
49 for(Path::Iterator i=path.begin(); i!=path.end(); ++i)
53 if(p.size()==1 && is_windows_drive(*i))
60 if(!S_ISDIR(st.st_mode))
61 throw Exception("A component exists and is not a directory");
64 else if(errno!=ENOENT)
65 throw SystemError("stat failed", errno);
71 void rmdir(const Path &path)
73 if(::rmdir(path.str().c_str())==-1)
74 throw SystemError("rmdir failed", errno);
77 void rmdirs(const Path &path)
79 list<string> files=list_files(path);
80 for(list<string>::iterator i=files.begin(); i!=files.end(); ++i)
83 struct stat st=stat(p.str().c_str());
84 if(S_ISDIR(st.st_mode))
93 list<string> list_files(const Path &path)
96 DIR *dir=opendir(path.str().c_str());
99 while(dirent *de=readdir(dir))
101 const char *fn=de->d_name;
102 if(fn[0]=='.' && (fn[1]==0 || (fn[1]=='.' && fn[2]==0)))
104 result.push_back(fn);
115 return ::getcwd(buf, sizeof(buf));
121 const char *home=getenv("HOME");
130 void chdir(const Path &path)
132 if(::chdir(path.str().c_str())==-1)
133 throw SystemError("chdir failed", errno);