summaryrefslogtreecommitdiff
path: root/gentoobrowse-api/service/fileUtils.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'gentoobrowse-api/service/fileUtils.cpp')
-rw-r--r--gentoobrowse-api/service/fileUtils.cpp45
1 files changed, 45 insertions, 0 deletions
diff --git a/gentoobrowse-api/service/fileUtils.cpp b/gentoobrowse-api/service/fileUtils.cpp
new file mode 100644
index 0000000..cfa02bc
--- /dev/null
+++ b/gentoobrowse-api/service/fileUtils.cpp
@@ -0,0 +1,45 @@
+#include "fileUtils.h"
+#include <fcntl.h>
+#include <unistd.h>
+#include <sys/mman.h>
+
+namespace Gentoo {
+ namespace Utils {
+ FileHandle::FileHandle(const std::string & path) :
+ fh(open(path.c_str(), O_RDONLY))
+ {
+ if (fh < 0) {
+ throw std::runtime_error("Failed to open " + path);
+ }
+ }
+
+ FileHandle::~FileHandle()
+ {
+ close(fh);
+ }
+
+ FileHandleStat::FileHandleStat(const std::string & path) :
+ FileHandle(path)
+ {
+ if (fstat(fh, &st)) {
+ throw std::runtime_error("Failed to stat " + path);
+ }
+ }
+
+ MemMap::MemMap(const std::string & path) :
+ FileHandleStat(path),
+ data(mmap(0, st.st_size, PROT_READ, MAP_SHARED, fh, 0))
+ {
+ if (data == (void*)-1) {
+ throw std::runtime_error("Failed to mmap " + path);
+ }
+ }
+
+ MemMap::~MemMap()
+ {
+ munmap(data, st.st_size);
+ }
+
+ }
+}
+