Serenity Operating System
1/*
2 * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
3 *
4 * SPDX-License-Identifier: BSD-2-Clause
5 */
6
7#include <Kernel/FileSystem/OpenFileDescription.h>
8#include <Kernel/Process.h>
9
10namespace Kernel {
11
12// NOTE: The length is passed by pointer because off_t is 64bit,
13// hence it can't be passed by register on 32bit platforms.
14ErrorOr<FlatPtr> Process::sys$ftruncate(int fd, Userspace<off_t const*> userspace_length)
15{
16 VERIFY_NO_PROCESS_BIG_LOCK(this);
17 TRY(require_promise(Pledge::stdio));
18 auto length = TRY(copy_typed_from_user(userspace_length));
19 if (length < 0)
20 return EINVAL;
21 auto description = TRY(open_file_description(fd));
22 if (!description->is_writable())
23 return EBADF;
24 TRY(description->truncate(static_cast<u64>(length)));
25 return 0;
26}
27
28}