mirror of
https://github.com/RGBCube/serenity
synced 2025-07-26 02:47:34 +00:00
LibCore: Add a static Core::File::open() convenience function
This helper opens a file with a given name, mode and permissions and returns it in a RefPtr<File>. I think this will be a bit nicer to use than having to go through Core::File::construct() every time.
This commit is contained in:
parent
ec91d2eb9f
commit
0df15823b5
2 changed files with 18 additions and 1 deletions
|
@ -33,6 +33,14 @@
|
||||||
|
|
||||||
namespace Core {
|
namespace Core {
|
||||||
|
|
||||||
|
RefPtr<File> File::open(const String& filename, IODevice::OpenMode mode, mode_t permissions)
|
||||||
|
{
|
||||||
|
auto file = File::construct(filename);
|
||||||
|
if (!file->open_impl(mode, permissions))
|
||||||
|
return nullptr;
|
||||||
|
return file;
|
||||||
|
}
|
||||||
|
|
||||||
File::File(const StringView& filename, Object* parent)
|
File::File(const StringView& filename, Object* parent)
|
||||||
: IODevice(parent)
|
: IODevice(parent)
|
||||||
, m_filename(filename)
|
, m_filename(filename)
|
||||||
|
@ -54,6 +62,11 @@ bool File::open(int fd, IODevice::OpenMode mode, ShouldCloseFileDescription shou
|
||||||
}
|
}
|
||||||
|
|
||||||
bool File::open(IODevice::OpenMode mode)
|
bool File::open(IODevice::OpenMode mode)
|
||||||
|
{
|
||||||
|
return open_impl(mode, 0666);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool File::open_impl(IODevice::OpenMode mode, mode_t permissions)
|
||||||
{
|
{
|
||||||
ASSERT(!m_filename.is_null());
|
ASSERT(!m_filename.is_null());
|
||||||
int flags = 0;
|
int flags = 0;
|
||||||
|
@ -73,7 +86,7 @@ bool File::open(IODevice::OpenMode mode)
|
||||||
flags |= O_TRUNC;
|
flags |= O_TRUNC;
|
||||||
if (mode & IODevice::MustBeNew)
|
if (mode & IODevice::MustBeNew)
|
||||||
flags |= O_EXCL;
|
flags |= O_EXCL;
|
||||||
int fd = ::open(m_filename.characters(), flags, 0666);
|
int fd = ::open(m_filename.characters(), flags, permissions);
|
||||||
if (fd < 0) {
|
if (fd < 0) {
|
||||||
set_error(errno);
|
set_error(errno);
|
||||||
return false;
|
return false;
|
||||||
|
|
|
@ -36,6 +36,8 @@ class File final : public IODevice {
|
||||||
public:
|
public:
|
||||||
virtual ~File() override;
|
virtual ~File() override;
|
||||||
|
|
||||||
|
static RefPtr<File> open(const String& filename, IODevice::OpenMode, mode_t = 0644);
|
||||||
|
|
||||||
String filename() const { return m_filename; }
|
String filename() const { return m_filename; }
|
||||||
void set_filename(const StringView& filename) { m_filename = filename; }
|
void set_filename(const StringView& filename) { m_filename = filename; }
|
||||||
|
|
||||||
|
@ -59,6 +61,8 @@ private:
|
||||||
}
|
}
|
||||||
explicit File(const StringView&, Object* parent = nullptr);
|
explicit File(const StringView&, Object* parent = nullptr);
|
||||||
|
|
||||||
|
bool open_impl(IODevice::OpenMode, mode_t);
|
||||||
|
|
||||||
String m_filename;
|
String m_filename;
|
||||||
ShouldCloseFileDescription m_should_close_file_descriptor { ShouldCloseFileDescription::Yes };
|
ShouldCloseFileDescription m_should_close_file_descriptor { ShouldCloseFileDescription::Yes };
|
||||||
};
|
};
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue