realpath.cpp 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  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/Process.h>
  10. namespace Kernel {
  11. KResultOr<FlatPtr> Process::sys$realpath(Userspace<const Syscall::SC_realpath_params*> user_params)
  12. {
  13. REQUIRE_PROMISE(rpath);
  14. Syscall::SC_realpath_params params;
  15. if (!copy_from_user(&params, user_params))
  16. return EFAULT;
  17. auto path = get_syscall_path_argument(params.path);
  18. if (path.is_error())
  19. return path.error();
  20. auto custody_or_error = VFS::the().resolve_path(path.value()->view(), current_directory());
  21. if (custody_or_error.is_error())
  22. return custody_or_error.error();
  23. auto& custody = custody_or_error.value();
  24. auto absolute_path = custody->absolute_path();
  25. size_t ideal_size = absolute_path.length() + 1;
  26. auto size_to_copy = min(ideal_size, params.buffer.size);
  27. if (!copy_to_user(params.buffer.data, absolute_path.characters(), size_to_copy))
  28. return EFAULT;
  29. // Note: we return the whole size here, not the copied size.
  30. return ideal_size;
  31. };
  32. }