aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAvatar Ciaran McCreesh <ciaran.mccreesh@googlemail.com> 2006-02-16 13:05:32 +0000
committerAvatar Ciaran McCreesh <ciaran.mccreesh@googlemail.com> 2006-02-16 13:05:32 +0000
commit80111275ca47dd8153705764416954e1f9dfd829 (patch)
tree9e339ae8deb7f41cc57fc935473e194dd32c470a
parent3fe70cd0ba5e9d26acd171abce113f44c3091d48 (diff)
downloadpaludis-80111275ca47dd8153705764416954e1f9dfd829.tar.gz
paludis-80111275ca47dd8153705764416954e1f9dfd829.tar.xz
Add FSEntry->file_size()
-rw-r--r--paludis/fs_entry.cc15
-rw-r--r--paludis/fs_entry.hh7
-rw-r--r--paludis/fs_entry_TEST.cc19
-rwxr-xr-xpaludis/fs_entry_TEST_setup.sh2
4 files changed, 43 insertions, 0 deletions
diff --git a/paludis/fs_entry.cc b/paludis/fs_entry.cc
index 86df8ff..7f9b03d 100644
--- a/paludis/fs_entry.cc
+++ b/paludis/fs_entry.cc
@@ -326,3 +326,18 @@ FSEntry::mtime() const
return (*_stat_info).st_mtime;
}
+
+off_t
+FSEntry::file_size() const
+{
+ _stat();
+
+ if (! _exists)
+ throw FSError("Filesystem entry does not exist");
+
+ if (! is_regular_file())
+ throw FSError("file_size called on a non-regular file");
+
+ return _stat_info->st_size;
+}
+
diff --git a/paludis/fs_entry.hh b/paludis/fs_entry.hh
index d576b5a..8000180 100644
--- a/paludis/fs_entry.hh
+++ b/paludis/fs_entry.hh
@@ -208,6 +208,13 @@ namespace paludis
time_t mtime() const;
/**
+ * Return the size of our file, in bytes.
+ *
+ * \exception FSError if we don't have a size.
+ */
+ off_t file_size() const;
+
+ /**
* Return the current working directory
*/
static FSEntry cwd();
diff --git a/paludis/fs_entry_TEST.cc b/paludis/fs_entry_TEST.cc
index 4f0d0da..68c7f61 100644
--- a/paludis/fs_entry_TEST.cc
+++ b/paludis/fs_entry_TEST.cc
@@ -165,6 +165,25 @@ namespace test_cases
} test_fs_entry_time;
/**
+ * \test Test FSEntry file_size
+ */
+ struct FSEntryFileSize : TestCase
+ {
+ FSEntryFileSize() : TestCase("file size") {}
+
+ void run()
+ {
+ FSEntry f("fs_entry_TEST_dir/ten_bytes");
+ FSEntry d("fs_entry_TEST_dir/dir_a");
+ FSEntry e("fs_entry_TEST_dir/no_such_file");
+
+ TEST_CHECK_EQUAL(f.file_size(), 10);
+ TEST_CHECK_THROWS(d.file_size(), FSError);
+ TEST_CHECK_THROWS(e.file_size(), FSError);
+ }
+ } test_fs_entry_size;
+
+ /**
* \test Test FSEntry basename and dirname methods
*/
struct FSEntryBaseDirName : TestCase
diff --git a/paludis/fs_entry_TEST_setup.sh b/paludis/fs_entry_TEST_setup.sh
index a3d3789..6945a78 100755
--- a/paludis/fs_entry_TEST_setup.sh
+++ b/paludis/fs_entry_TEST_setup.sh
@@ -10,3 +10,5 @@ touch all_perms || exit 7
chmod 777 all_perms || exit 8
touch no_perms || exit 9
chmod 000 no_perms || exit 10
+echo -n '0123456789' > ten_bytes || exit 11
+