realpath.cpp 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  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. VERIFY_PROCESS_BIG_LOCK_ACQUIRED(this)
  14. REQUIRE_PROMISE(rpath);
  15. Syscall::SC_realpath_params params;
  16. if (!copy_from_user(&params, user_params))
  17. return EFAULT;
  18. auto path = get_syscall_path_argument(params.path);
  19. if (path.is_error())
  20. return path.error();
  21. auto custody_or_error = VirtualFileSystem::the().resolve_path(path.value()->view(), current_directory());
  22. if (custody_or_error.is_error())
  23. return custody_or_error.error();
  24. auto& custody = custody_or_error.value();
  25. auto absolute_path = custody->try_create_absolute_path();
  26. if (!absolute_path)
  27. return ENOMEM;
  28. size_t ideal_size = absolute_path->length() + 1;
  29. auto size_to_copy = min(ideal_size, params.buffer.size);
  30. if (!copy_to_user(params.buffer.data, absolute_path->characters(), size_to_copy))
  31. return EFAULT;
  32. // Note: we return the whole size here, not the copied size.
  33. return ideal_size;
  34. };
  35. }