2010-03-17 03:42:27 +00:00
|
|
|
#include <algorithm>
|
2010-03-15 03:05:48 +00:00
|
|
|
#include <cerrno>
|
|
|
|
|
2010-03-17 03:42:27 +00:00
|
|
|
#include <File/File.h>
|
|
|
|
#include <ProFUSE/Exception.h>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
using ProFUSE::Exception;
|
|
|
|
using ProFUSE::POSIXException;
|
|
|
|
|
2010-03-15 03:05:48 +00:00
|
|
|
|
|
|
|
File::File()
|
|
|
|
{
|
|
|
|
_fd = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
File::File(int fd)
|
|
|
|
{
|
|
|
|
_fd = fd;
|
|
|
|
}
|
|
|
|
|
|
|
|
File::File(File& f)
|
|
|
|
{
|
|
|
|
_fd = f._fd;
|
|
|
|
f._fd = -1;
|
|
|
|
}
|
|
|
|
|
2010-05-21 18:51:00 +00:00
|
|
|
File::File(const char *name, int flags, const std::nothrow_t&)
|
|
|
|
{
|
|
|
|
_fd = ::open(name, flags);
|
|
|
|
}
|
|
|
|
|
|
|
|
File::File(const char *name, bool readOnly, const std::nothrow_t&)
|
|
|
|
{
|
|
|
|
_fd = ::open(name, readOnly ? O_RDONLY : O_RDWR);
|
|
|
|
}
|
|
|
|
|
2010-03-15 03:05:48 +00:00
|
|
|
File::File(const char *name, int flags)
|
|
|
|
{
|
2010-03-17 03:42:27 +00:00
|
|
|
#undef __METHOD__
|
|
|
|
#define __METHOD__ "File::File"
|
|
|
|
|
2010-03-15 03:05:48 +00:00
|
|
|
_fd = ::open(name, flags);
|
|
|
|
if (_fd < 0)
|
2010-03-17 03:42:27 +00:00
|
|
|
throw POSIXException( __METHOD__ ": open", errno);
|
2010-03-15 03:05:48 +00:00
|
|
|
}
|
|
|
|
|
2010-05-19 16:06:42 +00:00
|
|
|
|
|
|
|
File::File(const char *name, bool readOnly)
|
|
|
|
{
|
|
|
|
#undef __METHOD__
|
|
|
|
#define __METHOD__ "File::File"
|
|
|
|
|
|
|
|
_fd = ::open(name, readOnly ? O_RDONLY : O_RDWR);
|
|
|
|
if (_fd < 0)
|
|
|
|
throw POSIXException( __METHOD__ ": open", errno);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-03-15 03:05:48 +00:00
|
|
|
File::~File()
|
|
|
|
{
|
|
|
|
close();
|
|
|
|
}
|
|
|
|
|
2010-03-17 03:42:27 +00:00
|
|
|
void File::close()
|
2010-03-15 03:05:48 +00:00
|
|
|
{
|
2010-03-17 03:42:27 +00:00
|
|
|
#undef __METHOD__
|
|
|
|
#define __METHOD__ "File::close"
|
|
|
|
|
|
|
|
if (_fd >= 0)
|
|
|
|
{
|
|
|
|
int fd = _fd;
|
|
|
|
_fd = -1;
|
2010-03-15 03:05:48 +00:00
|
|
|
|
2010-03-17 03:42:27 +00:00
|
|
|
if (::close(fd) != 0)
|
|
|
|
throw POSIXException(__METHOD__ ": close", errno);
|
|
|
|
}
|
2010-03-15 03:05:48 +00:00
|
|
|
}
|
|
|
|
|
2010-03-17 03:42:27 +00:00
|
|
|
|
|
|
|
void File::adopt(File &f)
|
|
|
|
{
|
2010-05-19 23:47:32 +00:00
|
|
|
if (&f == this) return;
|
|
|
|
|
2010-03-17 03:42:27 +00:00
|
|
|
close();
|
|
|
|
_fd = f._fd;
|
|
|
|
f._fd = -1;
|
|
|
|
}
|
|
|
|
|
2010-05-19 23:47:32 +00:00
|
|
|
void File::adopt(int fd)
|
|
|
|
{
|
|
|
|
if (fd == _fd) return;
|
|
|
|
close();
|
|
|
|
_fd = fd;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-03-17 03:42:27 +00:00
|
|
|
void File::swap(File &f)
|
|
|
|
{
|
|
|
|
std::swap(_fd, f._fd);
|
|
|
|
}
|