aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAvatar Ciaran McCreesh <ciaran.mccreesh@googlemail.com> 2006-04-30 08:24:07 +0000
committerAvatar Ciaran McCreesh <ciaran.mccreesh@googlemail.com> 2006-04-30 08:24:07 +0000
commitab8a4ea58b93cfc4193e5d9d338918e190cc02c6 (patch)
treef8f9dc6e8cb7bf821b18f62d509ef126fcdcd9c5
parent5a6bd1604798c5bbce32bc4a86666bb2a40aee14 (diff)
downloadpaludis-ab8a4ea58b93cfc4193e5d9d338918e190cc02c6.tar.gz
paludis-ab8a4ea58b93cfc4193e5d9d338918e190cc02c6.tar.xz
Add a mkdir method to FSEntry.
-rw-r--r--paludis/util/fs_entry.cc18
-rw-r--r--paludis/util/fs_entry.hh11
-rw-r--r--paludis/util/fs_entry_TEST.cc12
3 files changed, 41 insertions, 0 deletions
diff --git a/paludis/util/fs_entry.cc b/paludis/util/fs_entry.cc
index 7f481ab..3f83163 100644
--- a/paludis/util/fs_entry.cc
+++ b/paludis/util/fs_entry.cc
@@ -23,6 +23,7 @@
#include <paludis/util/stringify.hh>
#include <sys/stat.h>
+#include <sys/types.h>
#include <unistd.h>
#include <errno.h>
#include <limits.h>
@@ -336,3 +337,20 @@ FSEntry::file_size() const
return _stat_info->st_size;
}
+bool
+FSEntry::mkdir()
+{
+ if (0 == ::mkdir(_path.c_str(), 0755))
+ return true;
+
+ int e(errno);
+ if (e == EEXIST)
+ {
+ if (is_directory())
+ return false;
+ throw FSError("mkdir '" + _path + "' failed: target exists and is not a directory");
+ }
+ else
+ throw FSError("mkdir '" + _path + "' failed: " + ::strerror(e));
+}
+
diff --git a/paludis/util/fs_entry.hh b/paludis/util/fs_entry.hh
index 6b3e6bb..b0e55bd 100644
--- a/paludis/util/fs_entry.hh
+++ b/paludis/util/fs_entry.hh
@@ -216,6 +216,17 @@ namespace paludis
off_t file_size() const;
/**
+ * Try to make a directory.
+ *
+ * \return True, if we succeeded, and false if the directory
+ * already exists and is a directory.
+ *
+ * \exception FSError If an error other than the directory already
+ * existing occurs.
+ */
+ bool mkdir();
+
+ /**
* Return the current working directory
*/
static FSEntry cwd();
diff --git a/paludis/util/fs_entry_TEST.cc b/paludis/util/fs_entry_TEST.cc
index 5a26709..f3dc2bf 100644
--- a/paludis/util/fs_entry_TEST.cc
+++ b/paludis/util/fs_entry_TEST.cc
@@ -74,6 +74,11 @@ namespace test_cases
{
FSEntryRealpathTest() : TestCase("behavior") { }
+ bool repeatable() const
+ {
+ return false;
+ }
+
void run()
{
FSEntry d("fs_entry_TEST_dir");
@@ -109,6 +114,13 @@ namespace test_cases
FSEntry h("fs_entry_TEST_dir/symlink_to_file_in_a");
TEST_CHECK(h.is_symbolic_link());
TEST_CHECK(! h.is_regular_file());
+
+ FSEntry i("fs_entry_TEST_dir/dir_to_make");
+ TEST_CHECK(i.mkdir());
+ TEST_CHECK(! i.mkdir());
+
+ FSEntry j("fs_entry_TEST_dir/dir_a/file_in_a");
+ TEST_CHECK_THROWS(j.mkdir(), FSError);
}
} test_fs_entry_behaviour;