2019-04-28 16:02:55 +03:00
|
|
|
#pragma once
|
|
|
|
|
2019-09-06 16:34:26 +03:00
|
|
|
#include <AK/String.h>
|
2019-06-21 19:58:45 +03:00
|
|
|
#include <AK/RefCounted.h>
|
2019-06-21 19:45:35 +03:00
|
|
|
#include <AK/NonnullRefPtr.h>
|
2019-04-28 15:53:50 +03:00
|
|
|
#include <AK/Types.h>
|
2019-04-29 05:55:54 +03:00
|
|
|
#include <Kernel/KResult.h>
|
2019-05-30 14:39:17 +03:00
|
|
|
#include <Kernel/UnixTypes.h>
|
2019-07-09 15:54:26 +03:00
|
|
|
#include <Kernel/VM/VirtualAddress.h>
|
2019-04-28 15:53:50 +03:00
|
|
|
|
2019-06-07 10:36:51 +03:00
|
|
|
class FileDescription;
|
2019-04-28 15:53:50 +03:00
|
|
|
class Process;
|
2019-04-29 05:55:54 +03:00
|
|
|
class Region;
|
2019-04-28 15:53:50 +03:00
|
|
|
|
2019-06-07 10:36:51 +03:00
|
|
|
// File is the base class for anything that can be referenced by a FileDescription.
|
2019-06-02 10:23:37 +03:00
|
|
|
//
|
|
|
|
// The most important functions in File are:
|
|
|
|
//
|
|
|
|
// read() and write()
|
|
|
|
// - Implement reading and writing.
|
|
|
|
// - Return the number of bytes read/written, OR a negative error code.
|
|
|
|
//
|
|
|
|
// can_read() and can_write()
|
|
|
|
//
|
|
|
|
// - Used to implement blocking I/O, and the select() and poll() syscalls.
|
|
|
|
// - Return true if read() or write() would succeed, respectively.
|
|
|
|
// - Note that can_read() should return true in EOF conditions,
|
|
|
|
// and a subsequent call to read() should return 0.
|
|
|
|
//
|
|
|
|
// ioctl()
|
|
|
|
//
|
|
|
|
// - Optional. If unimplemented, ioctl() on this File will fail with -ENOTTY.
|
|
|
|
// - Can be overridden in subclasses to implement arbitrary functionality.
|
|
|
|
// - Subclasses should take care to validate incoming addresses before dereferencing.
|
|
|
|
//
|
|
|
|
// mmap()
|
|
|
|
//
|
|
|
|
// - Optional. If unimplemented, mmap() on this File will fail with -ENODEV.
|
|
|
|
// - Called by mmap() when userspace wants to memory-map this File somewhere.
|
|
|
|
// - Should create a Region in the Process and return it if successful.
|
|
|
|
|
2019-06-21 16:29:31 +03:00
|
|
|
class File : public RefCounted<File> {
|
2019-04-28 15:53:50 +03:00
|
|
|
public:
|
|
|
|
virtual ~File();
|
|
|
|
|
2019-06-21 19:37:47 +03:00
|
|
|
virtual KResultOr<NonnullRefPtr<FileDescription>> open(int options);
|
2019-04-28 15:53:50 +03:00
|
|
|
virtual void close();
|
|
|
|
|
2019-11-04 16:03:14 +03:00
|
|
|
virtual bool can_read(const FileDescription&) const = 0;
|
|
|
|
virtual bool can_write(const FileDescription&) const = 0;
|
2019-04-28 15:53:50 +03:00
|
|
|
|
2019-07-03 22:17:35 +03:00
|
|
|
virtual ssize_t read(FileDescription&, u8*, ssize_t) = 0;
|
|
|
|
virtual ssize_t write(FileDescription&, const u8*, ssize_t) = 0;
|
2019-06-07 10:36:51 +03:00
|
|
|
virtual int ioctl(FileDescription&, unsigned request, unsigned arg);
|
2019-06-07 13:56:50 +03:00
|
|
|
virtual KResultOr<Region*> mmap(Process&, FileDescription&, VirtualAddress preferred_vaddr, size_t offset, size_t size, int prot);
|
2019-04-28 15:53:50 +03:00
|
|
|
|
2019-06-07 10:36:51 +03:00
|
|
|
virtual String absolute_path(const FileDescription&) const = 0;
|
2019-04-28 15:53:50 +03:00
|
|
|
|
2019-05-30 14:39:17 +03:00
|
|
|
virtual KResult truncate(off_t) { return KResult(-EINVAL); }
|
2020-01-03 22:14:56 +03:00
|
|
|
virtual KResult chown(uid_t, gid_t) { return KResult(-EBADF); }
|
|
|
|
virtual KResult chmod(mode_t) { return KResult(-EBADF); }
|
2019-05-30 14:39:17 +03:00
|
|
|
|
2019-04-28 15:53:50 +03:00
|
|
|
virtual const char* class_name() const = 0;
|
|
|
|
|
|
|
|
virtual bool is_seekable() const { return false; }
|
|
|
|
|
2019-05-30 14:39:17 +03:00
|
|
|
virtual bool is_inode() const { return false; }
|
2019-04-29 05:55:54 +03:00
|
|
|
virtual bool is_fifo() const { return false; }
|
2019-04-28 15:53:50 +03:00
|
|
|
virtual bool is_device() const { return false; }
|
|
|
|
virtual bool is_tty() const { return false; }
|
|
|
|
virtual bool is_master_pty() const { return false; }
|
|
|
|
virtual bool is_block_device() const { return false; }
|
|
|
|
virtual bool is_character_device() const { return false; }
|
2019-05-03 21:42:43 +03:00
|
|
|
virtual bool is_socket() const { return false; }
|
2019-04-28 15:53:50 +03:00
|
|
|
|
|
|
|
protected:
|
|
|
|
File();
|
|
|
|
};
|