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,8 +1,14 @@
#include "File.h"
#include <algorithm>
#include <cerrno>
using namespace File;
#include <File/File.h>
#include <ProFUSE/Exception.h>
using ProFUSE::Exception;
using ProFUSE::POSIXException;
File::File()
{
@ -22,9 +28,12 @@ File::File(File& f)
File::File(const char *name, int flags)
{
#undef __METHOD__
#define __METHOD__ "File::File"
_fd = ::open(name, flags);
if (_fd < 0)
throw ProFUSE::PosixException(errno);
throw POSIXException( __METHOD__ ": open", errno);
}
File::~File()
@ -32,12 +41,30 @@ File::~File()
close();
}
File::close()
void File::close()
{
int fd = _fd;
_fd = -1;
#undef __METHOD__
#define __METHOD__ "File::close"
if (_fd >= 0)
{
int fd = _fd;
_fd = -1;
if (fd >= 0 && ::close(fd) != 0)
throw ProFUSE::PosixException(errno);
if (::close(fd) != 0)
throw POSIXException(__METHOD__ ": close", errno);
}
}
void File::adopt(File &f)
{
close();
_fd = f._fd;
f._fd = -1;
}
void File::swap(File &f)
{
std::swap(_fd, f._fd);
}

View File

@ -1,17 +1,11 @@
#ifndef __FILE_H__
#define __FILE_H__
#include <sys/types.h>
#include <unistd.h>
#include <fcntl.h>
namespace File {
enum Flags {
ReadOnly = O_RDONLY,
WriteOnly = O_WRONLY,
ReadWrite = O_RDWR
};
class File {
public:
@ -24,10 +18,15 @@ class File {
int fd() const { return _fd; }
void close();
void adopt(File &f);
void swap(File &f);
private:
// could call dup() or something.
File& operator=(const File &f);
int _fd;
};
}
#endif

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);
}

View File

@ -3,15 +3,14 @@
#include <sys/mman.h>
#include "File.h"
#include <File/File.h>
namespace File {
class MappedFile {
public:
MappedFile();
MappedFile(File f, int flags);
MappedFile(File f, bool readOnly);
MappedFile(MappedFile&);
~MappedFile();
@ -22,12 +21,16 @@ class MappedFile {
void *address() const { return _address; }
size_t length() const { return _length; }
void swap(MappedFile &);
void adopt(MappedFile &);
private:
MappedFile& operator=(MappedFile &);
void *_address;
size_t _length;
};
}
#endif