2020-01-18 05:38:21 -03:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
|
* All rights reserved.
|
|
|
|
|
*
|
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
|
* modification, are permitted provided that the following conditions are met:
|
|
|
|
|
*
|
|
|
|
|
* 1. Redistributions of source code must retain the above copyright notice, this
|
|
|
|
|
* list of conditions and the following disclaimer.
|
|
|
|
|
*
|
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
|
* this list of conditions and the following disclaimer in the documentation
|
|
|
|
|
* and/or other materials provided with the distribution.
|
|
|
|
|
*
|
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
|
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
|
|
|
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
|
|
|
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
|
|
|
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
*/
|
|
|
|
|
|
2018-10-10 06:53:07 -03:00
|
|
|
#pragma once
|
|
|
|
|
|
2019-05-28 06:53:16 -03:00
|
|
|
#include <AK/Badge.h>
|
2018-10-10 06:53:07 -03:00
|
|
|
#include <AK/ByteBuffer.h>
|
2019-06-21 13:58:45 -03:00
|
|
|
#include <AK/RefCounted.h>
|
2019-05-28 06:53:16 -03:00
|
|
|
#include <Kernel/FileSystem/FIFO.h>
|
|
|
|
|
#include <Kernel/FileSystem/Inode.h>
|
|
|
|
|
#include <Kernel/FileSystem/InodeMetadata.h>
|
|
|
|
|
#include <Kernel/FileSystem/VirtualFileSystem.h>
|
2019-08-05 06:35:49 -03:00
|
|
|
#include <Kernel/KBuffer.h>
|
2020-02-09 13:15:58 -03:00
|
|
|
#include <LibBareMetal/Memory/VirtualAddress.h>
|
2018-10-10 06:53:07 -03:00
|
|
|
|
2020-02-15 21:27:42 -03:00
|
|
|
namespace Kernel {
|
|
|
|
|
|
|
|
|
|
class CharacterDevice;
|
2019-04-28 10:02:55 -03:00
|
|
|
class File;
|
2019-01-15 03:30:19 -02:00
|
|
|
class MasterPTY;
|
2019-01-14 11:21:51 -02:00
|
|
|
class Process;
|
2019-02-16 06:57:42 -02:00
|
|
|
class Region;
|
2020-02-15 21:27:42 -03:00
|
|
|
class Socket;
|
|
|
|
|
class TTY;
|
2019-02-14 12:40:04 -02:00
|
|
|
|
2019-06-21 10:29:31 -03:00
|
|
|
class FileDescription : public RefCounted<FileDescription> {
|
2020-02-22 10:37:58 -03:00
|
|
|
MAKE_SLAB_ALLOCATED(FileDescription)
|
2018-10-10 06:53:07 -03:00
|
|
|
public:
|
2019-08-11 04:32:21 -03:00
|
|
|
static NonnullRefPtr<FileDescription> create(Custody&);
|
2019-08-11 10:38:20 -03:00
|
|
|
static NonnullRefPtr<FileDescription> create(File&);
|
2019-06-07 04:36:51 -03:00
|
|
|
~FileDescription();
|
2018-10-10 06:53:07 -03:00
|
|
|
|
2020-01-02 23:29:59 -03:00
|
|
|
bool is_readable() const { return m_readable; }
|
|
|
|
|
bool is_writable() const { return m_writable; }
|
|
|
|
|
|
|
|
|
|
void set_readable(bool b) { m_readable = b; }
|
|
|
|
|
void set_writable(bool b) { m_writable = b; }
|
|
|
|
|
|
|
|
|
|
void set_rw_mode(int options)
|
|
|
|
|
{
|
2020-01-21 09:14:26 -03:00
|
|
|
set_readable(options & O_RDONLY);
|
|
|
|
|
set_writable(options & O_WRONLY);
|
2020-01-02 23:29:59 -03:00
|
|
|
}
|
|
|
|
|
|
2018-11-01 10:00:28 -03:00
|
|
|
int close();
|
|
|
|
|
|
2019-01-23 03:53:01 -02:00
|
|
|
off_t seek(off_t, int whence);
|
2019-07-03 16:17:35 -03:00
|
|
|
ssize_t read(u8*, ssize_t);
|
|
|
|
|
ssize_t write(const u8* data, ssize_t);
|
2019-03-01 20:11:08 -03:00
|
|
|
KResult fstat(stat&);
|
2018-10-14 16:19:27 -03:00
|
|
|
|
2020-01-03 16:14:56 -03:00
|
|
|
KResult chmod(mode_t);
|
2019-03-01 06:39:19 -03:00
|
|
|
|
2019-07-19 08:19:47 -03:00
|
|
|
bool can_read() const;
|
|
|
|
|
bool can_write() const;
|
2018-10-25 08:07:59 -03:00
|
|
|
|
2019-07-03 16:17:35 -03:00
|
|
|
ssize_t get_dir_entries(u8* buffer, ssize_t);
|
2018-10-24 07:43:52 -03:00
|
|
|
|
2019-04-29 08:58:40 -03:00
|
|
|
ByteBuffer read_entire_file();
|
2018-10-10 06:53:07 -03:00
|
|
|
|
2019-06-01 13:56:56 -03:00
|
|
|
String absolute_path() const;
|
2018-10-24 09:28:22 -03:00
|
|
|
|
2019-11-05 15:35:12 -03:00
|
|
|
bool is_direct() const { return m_direct; }
|
|
|
|
|
|
2019-10-25 04:22:22 -03:00
|
|
|
bool is_directory() const { return m_is_directory; }
|
2018-10-26 09:24:11 -03:00
|
|
|
|
2019-05-30 08:39:17 -03:00
|
|
|
File& file() { return *m_file; }
|
|
|
|
|
const File& file() const { return *m_file; }
|
2019-02-16 06:57:42 -02:00
|
|
|
|
2019-04-28 10:02:55 -03:00
|
|
|
bool is_device() const;
|
2020-01-12 12:28:23 -03:00
|
|
|
const Device* device() const;
|
|
|
|
|
Device* device();
|
2018-11-16 10:11:21 -02:00
|
|
|
|
2018-12-02 21:39:25 -02:00
|
|
|
bool is_tty() const;
|
2018-10-30 18:03:02 -03:00
|
|
|
const TTY* tty() const;
|
2018-11-02 09:14:25 -03:00
|
|
|
TTY* tty();
|
2019-01-15 03:30:19 -02:00
|
|
|
|
|
|
|
|
bool is_master_pty() const;
|
|
|
|
|
const MasterPTY* master_pty() const;
|
|
|
|
|
MasterPTY* master_pty();
|
2018-10-30 18:03:02 -03:00
|
|
|
|
2019-01-16 09:57:07 -02:00
|
|
|
InodeMetadata metadata() const;
|
|
|
|
|
Inode* inode() { return m_inode.ptr(); }
|
|
|
|
|
const Inode* inode() const { return m_inode.ptr(); }
|
2018-10-27 11:43:03 -03:00
|
|
|
|
2019-05-30 13:58:59 -03:00
|
|
|
Custody* custody() { return m_custody.ptr(); }
|
|
|
|
|
const Custody* custody() const { return m_custody.ptr(); }
|
|
|
|
|
|
2020-02-28 16:47:27 -03:00
|
|
|
KResultOr<Region*> mmap(Process&, VirtualAddress, size_t offset, size_t, int prot, bool shared);
|
2018-10-26 09:24:11 -03:00
|
|
|
|
2018-12-02 21:39:25 -02:00
|
|
|
bool is_blocking() const { return m_is_blocking; }
|
|
|
|
|
void set_blocking(bool b) { m_is_blocking = b; }
|
2019-05-25 20:18:04 -03:00
|
|
|
bool should_append() const { return m_should_append; }
|
|
|
|
|
void set_should_append(bool s) { m_should_append = s; }
|
2018-11-11 12:36:40 -02:00
|
|
|
|
2019-07-03 16:17:35 -03:00
|
|
|
u32 file_flags() const { return m_file_flags; }
|
|
|
|
|
void set_file_flags(u32);
|
2018-11-11 22:28:46 -02:00
|
|
|
|
2019-05-03 15:42:43 -03:00
|
|
|
bool is_socket() const;
|
|
|
|
|
Socket* socket();
|
|
|
|
|
const Socket* socket() const;
|
2019-02-14 11:17:38 -02:00
|
|
|
|
2019-04-28 23:55:54 -03:00
|
|
|
bool is_fifo() const;
|
|
|
|
|
FIFO* fifo();
|
2018-11-11 22:28:46 -02:00
|
|
|
FIFO::Direction fifo_direction() { return m_fifo_direction; }
|
2019-04-28 23:55:54 -03:00
|
|
|
void set_fifo_direction(Badge<FIFO>, FIFO::Direction direction) { m_fifo_direction = direction; }
|
2018-10-18 05:27:07 -03:00
|
|
|
|
2019-08-05 06:35:49 -03:00
|
|
|
Optional<KBuffer>& generator_cache() { return m_generator_cache; }
|
2018-10-26 19:14:24 -03:00
|
|
|
|
2019-06-21 13:37:47 -03:00
|
|
|
void set_original_inode(Badge<VFS>, NonnullRefPtr<Inode>&& inode) { m_inode = move(inode); }
|
2019-01-31 02:05:57 -02:00
|
|
|
|
2020-02-08 08:07:04 -03:00
|
|
|
KResult truncate(u64);
|
2019-04-08 20:10:00 -03:00
|
|
|
|
2019-05-30 08:39:17 -03:00
|
|
|
off_t offset() const { return m_current_offset; }
|
|
|
|
|
|
2019-06-01 15:31:36 -03:00
|
|
|
KResult chown(uid_t, gid_t);
|
|
|
|
|
|
2018-10-10 06:53:07 -03:00
|
|
|
private:
|
2018-11-15 11:43:10 -02:00
|
|
|
friend class VFS;
|
2019-08-11 10:38:20 -03:00
|
|
|
explicit FileDescription(File&);
|
2019-06-07 04:36:51 -03:00
|
|
|
FileDescription(FIFO&, FIFO::Direction);
|
2018-10-10 06:53:07 -03:00
|
|
|
|
2019-06-21 13:37:47 -03:00
|
|
|
RefPtr<Custody> m_custody;
|
|
|
|
|
RefPtr<Inode> m_inode;
|
2019-08-11 04:32:21 -03:00
|
|
|
NonnullRefPtr<File> m_file;
|
2018-10-14 16:19:27 -03:00
|
|
|
|
2019-01-23 03:53:01 -02:00
|
|
|
off_t m_current_offset { 0 };
|
2018-10-18 05:27:07 -03:00
|
|
|
|
2019-08-05 06:35:49 -03:00
|
|
|
Optional<KBuffer> m_generator_cache;
|
2018-10-26 19:14:24 -03:00
|
|
|
|
2019-07-03 16:17:35 -03:00
|
|
|
u32 m_file_flags { 0 };
|
2018-11-11 22:28:46 -02:00
|
|
|
|
2020-04-18 06:23:47 -03:00
|
|
|
bool m_readable : 1 { false };
|
|
|
|
|
bool m_writable : 1 { false };
|
|
|
|
|
bool m_is_blocking : 1 { true };
|
|
|
|
|
bool m_is_directory : 1 { false };
|
|
|
|
|
bool m_should_append : 1 { false };
|
|
|
|
|
bool m_direct : 1 { false };
|
2019-05-17 23:14:22 -03:00
|
|
|
FIFO::Direction m_fifo_direction { FIFO::Direction::Neither };
|
2020-01-12 16:09:44 -03:00
|
|
|
|
|
|
|
|
Lock m_lock { "FileDescription" };
|
2018-10-10 06:53:07 -03:00
|
|
|
};
|
2020-02-15 21:27:42 -03:00
|
|
|
|
|
|
|
|
}
|