Serenity Operating System
1/*
2 * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice, this
9 * list of conditions and the following disclaimer.
10 *
11 * 2. Redistributions in binary form must reproduce the above copyright notice,
12 * this list of conditions and the following disclaimer in the documentation
13 * and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
22 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
23 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27#pragma once
28
29#include <AK/Badge.h>
30#include <AK/ByteBuffer.h>
31#include <AK/RefCounted.h>
32#include <Kernel/FileSystem/FIFO.h>
33#include <Kernel/FileSystem/Inode.h>
34#include <Kernel/FileSystem/InodeMetadata.h>
35#include <Kernel/FileSystem/VirtualFileSystem.h>
36#include <Kernel/KBuffer.h>
37#include <LibBareMetal/Memory/VirtualAddress.h>
38
39namespace Kernel {
40
41class CharacterDevice;
42class File;
43class MasterPTY;
44class Process;
45class Region;
46class Socket;
47class TTY;
48
49class FileDescription : public RefCounted<FileDescription> {
50 MAKE_SLAB_ALLOCATED(FileDescription)
51public:
52 static NonnullRefPtr<FileDescription> create(Custody&);
53 static NonnullRefPtr<FileDescription> create(File&);
54 ~FileDescription();
55
56 bool is_readable() const { return m_readable; }
57 bool is_writable() const { return m_writable; }
58
59 void set_readable(bool b) { m_readable = b; }
60 void set_writable(bool b) { m_writable = b; }
61
62 void set_rw_mode(int options)
63 {
64 set_readable(options & O_RDONLY);
65 set_writable(options & O_WRONLY);
66 }
67
68 int close();
69
70 off_t seek(off_t, int whence);
71 ssize_t read(u8*, ssize_t);
72 ssize_t write(const u8* data, ssize_t);
73 KResult fstat(stat&);
74
75 KResult chmod(mode_t);
76
77 bool can_read() const;
78 bool can_write() const;
79
80 ssize_t get_dir_entries(u8* buffer, ssize_t);
81
82 ByteBuffer read_entire_file();
83
84 String absolute_path() const;
85
86 bool is_direct() const { return m_direct; }
87
88 bool is_directory() const { return m_is_directory; }
89
90 File& file() { return *m_file; }
91 const File& file() const { return *m_file; }
92
93 bool is_device() const;
94 const Device* device() const;
95 Device* device();
96
97 bool is_tty() const;
98 const TTY* tty() const;
99 TTY* tty();
100
101 bool is_master_pty() const;
102 const MasterPTY* master_pty() const;
103 MasterPTY* master_pty();
104
105 InodeMetadata metadata() const;
106 Inode* inode() { return m_inode.ptr(); }
107 const Inode* inode() const { return m_inode.ptr(); }
108
109 Custody* custody() { return m_custody.ptr(); }
110 const Custody* custody() const { return m_custody.ptr(); }
111
112 KResultOr<Region*> mmap(Process&, VirtualAddress, size_t offset, size_t, int prot, bool shared);
113
114 bool is_blocking() const { return m_is_blocking; }
115 void set_blocking(bool b) { m_is_blocking = b; }
116 bool should_append() const { return m_should_append; }
117 void set_should_append(bool s) { m_should_append = s; }
118
119 u32 file_flags() const { return m_file_flags; }
120 void set_file_flags(u32);
121
122 bool is_socket() const;
123 Socket* socket();
124 const Socket* socket() const;
125
126 bool is_fifo() const;
127 FIFO* fifo();
128 FIFO::Direction fifo_direction() { return m_fifo_direction; }
129 void set_fifo_direction(Badge<FIFO>, FIFO::Direction direction) { m_fifo_direction = direction; }
130
131 Optional<KBuffer>& generator_cache() { return m_generator_cache; }
132
133 void set_original_inode(Badge<VFS>, NonnullRefPtr<Inode>&& inode) { m_inode = move(inode); }
134
135 KResult truncate(u64);
136
137 off_t offset() const { return m_current_offset; }
138
139 KResult chown(uid_t, gid_t);
140
141private:
142 friend class VFS;
143 explicit FileDescription(File&);
144 FileDescription(FIFO&, FIFO::Direction);
145
146 RefPtr<Custody> m_custody;
147 RefPtr<Inode> m_inode;
148 NonnullRefPtr<File> m_file;
149
150 off_t m_current_offset { 0 };
151
152 Optional<KBuffer> m_generator_cache;
153
154 u32 m_file_flags { 0 };
155
156 bool m_readable { false };
157 bool m_writable { false };
158 bool m_is_blocking { true };
159 bool m_is_directory { false };
160 bool m_should_append { false };
161 bool m_direct { false };
162 FIFO::Direction m_fifo_direction { FIFO::Direction::Neither };
163
164 Lock m_lock { "FileDescription" };
165};
166
167}