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>
|
2011-03-14 23:08:19 +00:00
|
|
|
#include <Common/Exception.h>
|
|
|
|
#include <POSIX/Exception.h>
|
2010-03-17 03:42:27 +00:00
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2010-05-26 02:03:29 +00:00
|
|
|
|
|
|
|
File::File(const char *name, int flags, mode_t mode, const std::nothrow_t&)
|
|
|
|
{
|
|
|
|
_fd = ::open(name, flags, mode);
|
|
|
|
}
|
|
|
|
|
2010-05-29 19:29:59 +00:00
|
|
|
File::File(const char *name, FileFlags flags, const std::nothrow_t&)
|
2010-05-21 18:51:00 +00:00
|
|
|
{
|
2010-05-29 19:29:59 +00:00
|
|
|
_fd = ::open(name, flags == ReadOnly ? O_RDONLY : O_RDWR);
|
2010-05-21 18:51:00 +00:00
|
|
|
}
|
|
|
|
|
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)
|
2011-03-14 23:08:19 +00:00
|
|
|
throw POSIX::Exception( __METHOD__ ": open", errno);
|
2010-03-15 03:05:48 +00:00
|
|
|
}
|
|
|
|
|
2010-05-26 02:03:29 +00:00
|
|
|
File::File(const char *name, int flags, mode_t mode)
|
|
|
|
{
|
|
|
|
#undef __METHOD__
|
|
|
|
#define __METHOD__ "File::File"
|
|
|
|
|
|
|
|
_fd = ::open(name, flags, mode);
|
|
|
|
if (_fd < 0)
|
2011-03-14 23:08:19 +00:00
|
|
|
throw POSIX::Exception( __METHOD__ ": open", errno);
|
2010-05-26 02:03:29 +00:00
|
|
|
}
|
|
|
|
|
2010-05-19 16:06:42 +00:00
|
|
|
|
2010-05-29 19:29:59 +00:00
|
|
|
File::File(const char *name, FileFlags flags)
|
2010-05-19 16:06:42 +00:00
|
|
|
{
|
|
|
|
#undef __METHOD__
|
|
|
|
#define __METHOD__ "File::File"
|
|
|
|
|
2010-05-29 19:29:59 +00:00
|
|
|
_fd = ::open(name, flags == ReadOnly ? O_RDONLY : O_RDWR);
|
2010-05-19 16:06:42 +00:00
|
|
|
if (_fd < 0)
|
2011-03-14 23:08:19 +00:00
|
|
|
throw POSIX::Exception( __METHOD__ ": open", errno);
|
2010-05-19 16:06:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-03-15 03:05:48 +00:00
|
|
|
File::~File()
|
|
|
|
{
|
|
|
|
close();
|
|
|
|
}
|
|
|
|
|
2010-05-31 00:07:02 +00:00
|
|
|
int File::release()
|
|
|
|
{
|
|
|
|
int tmp = _fd;
|
|
|
|
_fd = -1;
|
|
|
|
return tmp;
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
{
|
2010-05-29 19:29:59 +00:00
|
|
|
::close(_fd);
|
2010-03-17 03:42:27 +00:00
|
|
|
_fd = -1;
|
2010-03-15 03:05:48 +00:00
|
|
|
|
2010-05-29 19:29:59 +00:00
|
|
|
// destructor shouldn't throw.
|
|
|
|
/*
|
|
|
|
if (::close(fd) != 0)
|
2011-03-14 23:08:19 +00:00
|
|
|
throw POSIX::Exception(__METHOD__ ": close", errno);
|
2010-05-29 19:29:59 +00:00
|
|
|
*/
|
2010-03-17 03:42:27 +00:00
|
|
|
}
|
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);
|
|
|
|
}
|