2019-04-10 18:22:23 +00:00
|
|
|
#include <LibCore/CFile.h>
|
2019-06-22 19:21:57 +00:00
|
|
|
#include <errno.h>
|
2019-03-17 14:54:43 +00:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include <stdio.h>
|
2019-05-28 09:53:16 +00:00
|
|
|
#include <unistd.h>
|
2019-03-17 14:54:43 +00:00
|
|
|
|
2019-06-02 10:26:28 +00:00
|
|
|
CFile::CFile(const StringView& filename)
|
2019-03-17 14:54:43 +00:00
|
|
|
: m_filename(filename)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-04-10 18:22:23 +00:00
|
|
|
CFile::~CFile()
|
2019-03-17 14:54:43 +00:00
|
|
|
{
|
2019-06-07 07:36:51 +00:00
|
|
|
if (m_should_close_file_descriptor == ShouldCloseFileDescription::Yes && mode() != NotOpen)
|
2019-03-17 14:54:43 +00:00
|
|
|
close();
|
|
|
|
}
|
|
|
|
|
2019-06-07 07:36:51 +00:00
|
|
|
bool CFile::open(int fd, CIODevice::OpenMode mode, ShouldCloseFileDescription should_close)
|
2019-04-26 00:22:21 +00:00
|
|
|
{
|
|
|
|
set_fd(fd);
|
|
|
|
set_mode(mode);
|
|
|
|
m_should_close_file_descriptor = should_close;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-04-10 18:22:23 +00:00
|
|
|
bool CFile::open(CIODevice::OpenMode mode)
|
2019-03-17 14:54:43 +00:00
|
|
|
{
|
|
|
|
int flags = 0;
|
2019-04-10 18:22:23 +00:00
|
|
|
if ((mode & CIODevice::ReadWrite) == CIODevice::ReadWrite) {
|
2019-03-17 14:54:43 +00:00
|
|
|
flags |= O_RDWR | O_CREAT;
|
2019-04-10 18:22:23 +00:00
|
|
|
} else if (mode & CIODevice::ReadOnly) {
|
2019-03-17 14:54:43 +00:00
|
|
|
flags |= O_RDONLY;
|
2019-04-10 18:22:23 +00:00
|
|
|
} else if (mode & CIODevice::WriteOnly) {
|
2019-03-17 14:54:43 +00:00
|
|
|
flags |= O_WRONLY | O_CREAT;
|
|
|
|
}
|
2019-04-10 18:22:23 +00:00
|
|
|
if (mode & CIODevice::Append)
|
2019-03-17 14:54:43 +00:00
|
|
|
flags |= O_APPEND;
|
2019-04-10 18:22:23 +00:00
|
|
|
if (mode & CIODevice::Truncate)
|
2019-03-17 14:54:43 +00:00
|
|
|
flags |= O_TRUNC;
|
2019-04-10 18:22:23 +00:00
|
|
|
if (mode & CIODevice::MustBeNew)
|
2019-03-17 14:54:43 +00:00
|
|
|
flags |= O_EXCL;
|
|
|
|
int fd = ::open(m_filename.characters(), flags, 0666);
|
|
|
|
if (fd < 0) {
|
|
|
|
set_error(errno);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
set_fd(fd);
|
|
|
|
set_mode(mode);
|
|
|
|
return true;
|
|
|
|
}
|