mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 03:27:44 +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 {
|
||||
|
||||
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)
|
||||
: IODevice(parent)
|
||||
, m_filename(filename)
|
||||
|
@ -54,6 +62,11 @@ bool File::open(int fd, IODevice::OpenMode mode, ShouldCloseFileDescription shou
|
|||
}
|
||||
|
||||
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());
|
||||
int flags = 0;
|
||||
|
@ -73,7 +86,7 @@ bool File::open(IODevice::OpenMode mode)
|
|||
flags |= O_TRUNC;
|
||||
if (mode & IODevice::MustBeNew)
|
||||
flags |= O_EXCL;
|
||||
int fd = ::open(m_filename.characters(), flags, 0666);
|
||||
int fd = ::open(m_filename.characters(), flags, permissions);
|
||||
if (fd < 0) {
|
||||
set_error(errno);
|
||||
return false;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue