git-svn-id: https://profuse.googlecode.com/svn/branches/v2@202 aa027e90-d47c-11dd-86d7-074df07e0730

This commit is contained in:
ksherlock
2010-03-17 03:42:27 +00:00
parent b83d2df14b
commit 12385a19dc
4 changed files with 92 additions and 35 deletions

View File

@@ -1,11 +1,14 @@
#include "MappedFile.h"
#include <algorithm>
#include <cerrno>
#include <sys/stat.h>
using namespace File;
#include <File/MappedFile.h>
#include <ProFUSE/Exception.h>
using ProFUSE::POSIXException;
MappedFile::MappedFile()
{
_length = -1;
@@ -21,20 +24,23 @@ MappedFile::MappedFile(MappedFile &mf)
mf._length = -1;
}
MappedFile::MappedFile(File f, int flags)
MappedFile::MappedFile(File f, bool readOnly)
{
#undef __METHOD__
#define __METHOD__ "MappedFile::MappedFile"
struct stat st;
// close enough
if (f.fd() < 0)
throw ProFUSE::PosixException(EBADF);
throw POSIXException( __METHOD__, EBADF);
if (::fstat(f.fd(), st) != 0)
throw ProFUSE::PosixException(errno);
if (::fstat(f.fd(), &st) != 0)
throw POSIXException(__METHOD__ ": fstat", errno);
if (!S_ISREG(st.st_mode))
throw ProFUSE::PosixException(ENODEV);
throw POSIXException(__METHOD__, ENODEV);
_length = st.st_size;
_address = ::mmap(0, _length,
@@ -42,7 +48,7 @@ MappedFile::MappedFile(File f, int flags)
MAP_FILE | MAP_SHARED, f.fd(), 0);
if (_address == MAP_FAILED)
throw ProFUSE::PosixException(errno);
throw POSIXException(__METHOD__ ": mmap", errno);
}
MappedFile::~MappedFile()
@@ -52,8 +58,11 @@ MappedFile::~MappedFile()
MappedFile::close()
void MappedFile::close()
{
#undef __METHOD__
#define __METHOD__ "MappedFile::close"
if (_address != MAP_FAILED)
{
void *address = _address;
@@ -63,15 +72,34 @@ MappedFile::close()
_length = -1;
if (::munmap(address, length) != 0)
throw ProFUSE::PosixException(errno);
throw POSIXException(__METHOD__ ": munmap", errno);
}
}
MappedFile::sync()
void MappedFile::sync()
{
#undef __METHOD__
#define __METHOD__ "MappedFile::sync"
if (_address != MAP_FAILED)
{
if (::msync(_address, _length, MS_SYNC) != 0)
throw ProFUSE::PosixException(errno);
throw POSIXException(__METHOD__ ": msync", errno);
}
}
void MappedFile::adopt(MappedFile &mf)
{
close();
_address = mf._address;
_length = mf._length;
mf._address = MAP_FAILED;
mf._length = -1;
}
void MappedFile::swap(MappedFile &mf)
{
std::swap(_address, mf._address);
std::swap(_length, mf._length);
}