#include <map>
#include <string>
#include <fstream>
+#include <cassert>
extern "C" {
#include <fcntl.h>
// Make sure a directory exists.
int
-create_dir(const char *dir)
+create_dir(const char *dir, int mode)
{
struct stat st;
if (stat(dir, &st) == 0)
return 1;
}
- if (mkdir(dir, 0777) != 0 && errno != EEXIST)
- return 1;
+ // Create the directory. We must create each component
+ // of the path ourselves.
+ vector<string> components;
+ tokenize (dir, components, "/");
+ string path;
+ if (*dir == '/')
+ {
+ // Absolute path
+ path = "/";
+ }
+ unsigned limit = components.size ();
+ assert (limit != 0);
+ for (unsigned ix = 0; ix < limit; ++ix)
+ {
+ path += components[ix] + '/';
+ if (mkdir(path.c_str (), mode) != 0 && errno != EEXIST)
+ return 1;
+ }
return 0;
}
bool file_exists (const std::string &path);
bool copy_file(const std::string& src, const std::string& dest,
bool verbose=false);
-int create_dir(const char *dir);
+int create_dir(const char *dir, int mode = 0777);
int remove_file_or_dir(const char *dir);
bool in_group_id (gid_t target_gid);
std::string getmemusage ();