summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndreas Kling <kling@serenityos.org>2021-12-17 11:22:27 +0100
committerAndreas Kling <kling@serenityos.org>2021-12-18 11:30:10 +0100
commit0ae870269226f7eee92a2aed119f46d408da9400 (patch)
treefc334d9ae3cd5ce746b88998b831cd619437f3c1
parent1f2d0d0ad4ddb1ca0c6f47f2a8323f42af0f0673 (diff)
downloadserenity-0ae870269226f7eee92a2aed119f46d408da9400.zip
Kernel: Make File::stat() & friends return Error<struct stat>
Instead of making the caller provide a stat buffer, let's just return one as a value.
-rw-r--r--Kernel/FileSystem/FIFO.cpp6
-rw-r--r--Kernel/FileSystem/FIFO.h2
-rw-r--r--Kernel/FileSystem/File.h2
-rw-r--r--Kernel/FileSystem/InodeFile.h2
-rw-r--r--Kernel/FileSystem/InodeMetadata.h5
-rw-r--r--Kernel/FileSystem/OpenFileDescription.cpp6
-rw-r--r--Kernel/FileSystem/OpenFileDescription.h2
-rw-r--r--Kernel/Net/Socket.cpp6
-rw-r--r--Kernel/Net/Socket.h2
-rw-r--r--Kernel/Syscalls/stat.cpp6
10 files changed, 19 insertions, 20 deletions
diff --git a/Kernel/FileSystem/FIFO.cpp b/Kernel/FileSystem/FIFO.cpp
index 6692a753fb..6ca8a1f3f8 100644
--- a/Kernel/FileSystem/FIFO.cpp
+++ b/Kernel/FileSystem/FIFO.cpp
@@ -137,11 +137,11 @@ ErrorOr<NonnullOwnPtr<KString>> FIFO::pseudo_path(const OpenFileDescription&) co
return KString::formatted("fifo:{}", m_fifo_id);
}
-ErrorOr<void> FIFO::stat(::stat& st) const
+ErrorOr<struct stat> FIFO::stat() const
{
- memset(&st, 0, sizeof(st));
+ struct stat st = {};
st.st_mode = S_IFIFO;
- return {};
+ return st;
}
}
diff --git a/Kernel/FileSystem/FIFO.h b/Kernel/FileSystem/FIFO.h
index 9c20a81452..082930f6be 100644
--- a/Kernel/FileSystem/FIFO.h
+++ b/Kernel/FileSystem/FIFO.h
@@ -42,7 +42,7 @@ private:
// ^File
virtual ErrorOr<size_t> write(OpenFileDescription&, u64, const UserOrKernelBuffer&, size_t) override;
virtual ErrorOr<size_t> read(OpenFileDescription&, u64, UserOrKernelBuffer&, size_t) override;
- virtual ErrorOr<void> stat(::stat&) const override;
+ virtual ErrorOr<struct stat> stat() const override;
virtual bool can_read(const OpenFileDescription&, size_t) const override;
virtual bool can_write(const OpenFileDescription&, size_t) const override;
virtual ErrorOr<NonnullOwnPtr<KString>> pseudo_path(const OpenFileDescription&) const override;
diff --git a/Kernel/FileSystem/File.h b/Kernel/FileSystem/File.h
index 5602e7d40a..891eae69fe 100644
--- a/Kernel/FileSystem/File.h
+++ b/Kernel/FileSystem/File.h
@@ -91,7 +91,7 @@ public:
virtual ErrorOr<size_t> write(OpenFileDescription&, u64, const UserOrKernelBuffer&, size_t) = 0;
virtual ErrorOr<void> ioctl(OpenFileDescription&, unsigned request, Userspace<void*> arg);
virtual ErrorOr<Memory::Region*> mmap(Process&, OpenFileDescription&, Memory::VirtualRange const&, u64 offset, int prot, bool shared);
- virtual ErrorOr<void> stat(::stat&) const { return EBADF; }
+ virtual ErrorOr<struct stat> stat() const { return EBADF; }
// Although this might be better described "name" or "description", these terms already have other meanings.
virtual ErrorOr<NonnullOwnPtr<KString>> pseudo_path(const OpenFileDescription&) const = 0;
diff --git a/Kernel/FileSystem/InodeFile.h b/Kernel/FileSystem/InodeFile.h
index 1701d41c06..e22dc165f5 100644
--- a/Kernel/FileSystem/InodeFile.h
+++ b/Kernel/FileSystem/InodeFile.h
@@ -34,7 +34,7 @@ public:
virtual ErrorOr<size_t> write(OpenFileDescription&, u64, const UserOrKernelBuffer&, size_t) override;
virtual ErrorOr<void> ioctl(OpenFileDescription&, unsigned request, Userspace<void*> arg) override;
virtual ErrorOr<Memory::Region*> mmap(Process&, OpenFileDescription&, Memory::VirtualRange const&, u64 offset, int prot, bool shared) override;
- virtual ErrorOr<void> stat(::stat& buffer) const override { return inode().metadata().stat(buffer); }
+ virtual ErrorOr<struct stat> stat() const override { return inode().metadata().stat(); }
virtual ErrorOr<NonnullOwnPtr<KString>> pseudo_path(const OpenFileDescription&) const override;
diff --git a/Kernel/FileSystem/InodeMetadata.h b/Kernel/FileSystem/InodeMetadata.h
index 6b3b509cb6..95bfba8367 100644
--- a/Kernel/FileSystem/InodeMetadata.h
+++ b/Kernel/FileSystem/InodeMetadata.h
@@ -86,10 +86,11 @@ struct InodeMetadata {
bool is_setuid() const { return Kernel::is_setuid(mode); }
bool is_setgid() const { return Kernel::is_setgid(mode); }
- ErrorOr<void> stat(stat& buffer) const
+ ErrorOr<struct stat> stat() const
{
if (!is_valid())
return EIO;
+ struct stat buffer = {};
buffer.st_rdev = encoded_device(major_device, minor_device);
buffer.st_ino = inode.index().value();
buffer.st_mode = mode;
@@ -106,7 +107,7 @@ struct InodeMetadata {
buffer.st_mtim.tv_nsec = 0;
buffer.st_ctim.tv_sec = ctime;
buffer.st_ctim.tv_nsec = 0;
- return {};
+ return buffer;
}
InodeIdentifier inode;
diff --git a/Kernel/FileSystem/OpenFileDescription.cpp b/Kernel/FileSystem/OpenFileDescription.cpp
index 1143459f7d..5b7af1e4cb 100644
--- a/Kernel/FileSystem/OpenFileDescription.cpp
+++ b/Kernel/FileSystem/OpenFileDescription.cpp
@@ -97,13 +97,13 @@ Thread::FileBlocker::BlockFlags OpenFileDescription::should_unblock(Thread::File
return unblock_flags;
}
-ErrorOr<void> OpenFileDescription::stat(::stat& buffer)
+ErrorOr<struct stat> OpenFileDescription::stat()
{
MutexLocker locker(m_lock);
// FIXME: This is due to the Device class not overriding File::stat().
if (m_inode)
- return m_inode->metadata().stat(buffer);
- return m_file->stat(buffer);
+ return m_inode->metadata().stat();
+ return m_file->stat();
}
ErrorOr<off_t> OpenFileDescription::seek(off_t offset, int whence)
diff --git a/Kernel/FileSystem/OpenFileDescription.h b/Kernel/FileSystem/OpenFileDescription.h
index ae84e410af..f075be6776 100644
--- a/Kernel/FileSystem/OpenFileDescription.h
+++ b/Kernel/FileSystem/OpenFileDescription.h
@@ -49,7 +49,7 @@ public:
ErrorOr<off_t> seek(off_t, int whence);
ErrorOr<size_t> read(UserOrKernelBuffer&, size_t);
ErrorOr<size_t> write(const UserOrKernelBuffer& data, size_t);
- ErrorOr<void> stat(::stat&);
+ ErrorOr<struct stat> stat();
// NOTE: These ignore the current offset of this file description.
ErrorOr<size_t> read(UserOrKernelBuffer&, u64 offset, size_t);
diff --git a/Kernel/Net/Socket.cpp b/Kernel/Net/Socket.cpp
index ba0b75abf1..8655b913cb 100644
--- a/Kernel/Net/Socket.cpp
+++ b/Kernel/Net/Socket.cpp
@@ -263,11 +263,11 @@ ErrorOr<void> Socket::shutdown(int how)
return {};
}
-ErrorOr<void> Socket::stat(::stat& st) const
+ErrorOr<struct stat> Socket::stat() const
{
- memset(&st, 0, sizeof(st));
+ struct stat st = {};
st.st_mode = S_IFSOCK;
- return {};
+ return st;
}
void Socket::set_connected(bool connected)
diff --git a/Kernel/Net/Socket.h b/Kernel/Net/Socket.h
index e3cff41cb4..3c1d6c261c 100644
--- a/Kernel/Net/Socket.h
+++ b/Kernel/Net/Socket.h
@@ -104,7 +104,7 @@ public:
// ^File
virtual ErrorOr<size_t> read(OpenFileDescription&, u64, UserOrKernelBuffer&, size_t) override final;
virtual ErrorOr<size_t> write(OpenFileDescription&, u64, const UserOrKernelBuffer&, size_t) override final;
- virtual ErrorOr<void> stat(::stat&) const override;
+ virtual ErrorOr<struct stat> stat() const override;
virtual ErrorOr<NonnullOwnPtr<KString>> pseudo_path(const OpenFileDescription&) const override = 0;
bool has_receive_timeout() const { return m_receive_timeout != Time::zero(); }
diff --git a/Kernel/Syscalls/stat.cpp b/Kernel/Syscalls/stat.cpp
index 692eb30f7e..a646d5a505 100644
--- a/Kernel/Syscalls/stat.cpp
+++ b/Kernel/Syscalls/stat.cpp
@@ -16,8 +16,7 @@ ErrorOr<FlatPtr> Process::sys$fstat(int fd, Userspace<stat*> user_statbuf)
VERIFY_PROCESS_BIG_LOCK_ACQUIRED(this)
REQUIRE_PROMISE(stdio);
auto description = TRY(fds().open_file_description(fd));
- stat buffer = {};
- TRY(description->stat(buffer));
+ auto buffer = TRY(description->stat());
TRY(copy_to_user(user_statbuf, &buffer));
return 0;
}
@@ -42,8 +41,7 @@ ErrorOr<FlatPtr> Process::sys$stat(Userspace<const Syscall::SC_stat_params*> use
base = base_description->custody();
}
auto metadata = TRY(VirtualFileSystem::the().lookup_metadata(path->view(), *base, params.follow_symlinks ? 0 : O_NOFOLLOW_NOERROR));
- stat statbuf = {};
- TRY(metadata.stat(statbuf));
+ auto statbuf = TRY(metadata.stat());
TRY(copy_to_user(params.statbuf, &statbuf));
return 0;
}