43 lines
1.1 KiB
C
43 lines
1.1 KiB
C
|
#pragma once
|
||
|
#include <bits/open-flags.h>
|
||
|
#include <luna/Buffer.h>
|
||
|
#include <luna/Result.h>
|
||
|
#include <luna/SharedPtr.h>
|
||
|
#include <luna/StringView.h>
|
||
|
#include <sys/types.h>
|
||
|
|
||
|
namespace os
|
||
|
{
|
||
|
class File
|
||
|
{
|
||
|
public:
|
||
|
enum OpenMode
|
||
|
{
|
||
|
ReadOnly = O_RDONLY,
|
||
|
WriteOnly = O_WRONLY | O_TRUNC,
|
||
|
ReadWrite = O_RDWR | O_TRUNC,
|
||
|
Append = O_WRONLY | O_APPEND,
|
||
|
ReadAppend = O_RDWR | O_APPEND,
|
||
|
};
|
||
|
|
||
|
static Result<SharedPtr<File>> open(StringView path, OpenMode flags);
|
||
|
static Result<SharedPtr<File>> open_or_create(StringView path, OpenMode flags, mode_t mode = 0644);
|
||
|
static Result<SharedPtr<File>> create(StringView path, OpenMode flags, mode_t mode = 0644);
|
||
|
|
||
|
void set_close_on_exec();
|
||
|
|
||
|
Result<void> write(StringView str);
|
||
|
|
||
|
File(Badge<File>);
|
||
|
~File();
|
||
|
|
||
|
private:
|
||
|
static Result<SharedPtr<File>> construct(StringView path, int flags, mode_t mode);
|
||
|
|
||
|
Result<usize> raw_read(u8* buf, usize length);
|
||
|
Result<usize> raw_write(const u8* buf, usize length);
|
||
|
|
||
|
int m_fd { -1 };
|
||
|
};
|
||
|
}
|