ftruncate.cpp 727 B

1234567891011121314151617181920212223242526272829
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <Kernel/FileSystem/FileDescription.h>
  7. #include <Kernel/Process.h>
  8. namespace Kernel {
  9. KResultOr<FlatPtr> Process::sys$ftruncate(int fd, Userspace<off_t*> userspace_length)
  10. {
  11. VERIFY_PROCESS_BIG_LOCK_ACQUIRED(this)
  12. REQUIRE_PROMISE(stdio);
  13. off_t length;
  14. if (!copy_from_user(&length, userspace_length))
  15. return EFAULT;
  16. if (length < 0)
  17. return EINVAL;
  18. auto description = fds().file_description(fd);
  19. if (!description)
  20. return EBADF;
  21. if (!description->is_writable())
  22. return EBADF;
  23. return description->truncate(static_cast<u64>(length));
  24. }
  25. }