realpath.cpp 1.0 KB

12345678910111213141516171819202122232425262728293031
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/StringView.h>
  7. #include <Kernel/FileSystem/Custody.h>
  8. #include <Kernel/FileSystem/VirtualFileSystem.h>
  9. #include <Kernel/Tasks/Process.h>
  10. namespace Kernel {
  11. ErrorOr<FlatPtr> Process::sys$realpath(Userspace<Syscall::SC_realpath_params const*> user_params)
  12. {
  13. VERIFY_NO_PROCESS_BIG_LOCK(this);
  14. TRY(require_promise(Pledge::rpath));
  15. auto params = TRY(copy_typed_from_user(user_params));
  16. auto path = TRY(get_syscall_path_argument(params.path));
  17. auto custody = TRY(VirtualFileSystem::the().resolve_path(credentials(), path->view(), current_directory()));
  18. auto absolute_path = TRY(custody->try_serialize_absolute_path());
  19. size_t ideal_size = absolute_path->length() + 1;
  20. auto size_to_copy = min(ideal_size, params.buffer.size);
  21. TRY(copy_to_user(params.buffer.data, absolute_path->characters(), size_to_copy));
  22. // Note: we return the whole size here, not the copied size.
  23. return ideal_size;
  24. }
  25. }