Serenity Operating System
at master 64 lines 2.1 kB view raw
1/* 2 * Copyright (c) 2022, Leon Albrecht <leon.a@serenityos.org> 3 * 4 * SPDX-License-Identifier: BSD-2-Clause 5 */ 6 7#include <AK/Checked.h> 8#include <Kernel/FileSystem/Inode.h> 9#include <Kernel/FileSystem/InodeFile.h> 10#include <Kernel/FileSystem/OpenFileDescription.h> 11#include <Kernel/Process.h> 12 13namespace Kernel { 14 15// https://pubs.opengroup.org/onlinepubs/9699919799/functions/posix_fallocate.html 16ErrorOr<FlatPtr> Process::sys$posix_fallocate(int fd, Userspace<off_t const*> userspace_offset, Userspace<off_t const*> userspace_length) 17{ 18 VERIFY_NO_PROCESS_BIG_LOCK(this); 19 TRY(require_promise(Pledge::stdio)); 20 21 // [EINVAL] The len argument is less than zero, or the offset argument is less than zero, or the underlying file system does not support this operation. 22 auto offset = TRY(copy_typed_from_user(userspace_offset)); 23 if (offset < 0) 24 return EINVAL; 25 auto length = TRY(copy_typed_from_user(userspace_length)); 26 if (length <= 0) 27 return EINVAL; 28 29 Checked<size_t> checked_size { length }; 30 checked_size += offset; 31 // FIXME: Return EFBIG if offset+length > FileSizeMax 32 if (checked_size.has_overflow()) 33 return EFBIG; 34 35 auto description = TRY(open_file_description(fd)); 36 37 // [EBADF] The fd argument references a file that was opened without write permission. 38 if (!description->is_writable()) 39 return EBADF; 40 41 // [ESPIPE] The fd argument is associated with a pipe or FIFO. 42 if (description->is_fifo()) 43 return ESPIPE; 44 45 // [ENODEV] The fd argument does not refer to a regular file. 46 if (!description->file().is_regular_file()) 47 return ENODEV; 48 49 VERIFY(description->file().is_inode()); 50 51 auto& file = static_cast<InodeFile&>(description->file()); 52 if (file.inode().size() >= checked_size.value()) 53 return 0; 54 55 // Note: truncate essentially calls resize in the inodes implementation 56 // while resize is not a standard member of an inode, so we just call 57 // truncate instead 58 TRY(file.inode().truncate(checked_size.value())); 59 60 // FIXME: EINTR: A signal was caught during execution. 61 return 0; 62} 63 64}