link.cpp 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  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/VirtualFileSystem.h>
  8. #include <Kernel/Process.h>
  9. namespace Kernel {
  10. KResultOr<FlatPtr> Process::sys$link(Userspace<const Syscall::SC_link_params*> user_params)
  11. {
  12. VERIFY_PROCESS_BIG_LOCK_ACQUIRED(this)
  13. REQUIRE_PROMISE(cpath);
  14. Syscall::SC_link_params params;
  15. if (!copy_from_user(&params, user_params))
  16. return EFAULT;
  17. auto old_path_or_error = try_copy_kstring_from_user(params.old_path);
  18. if (old_path_or_error.is_error())
  19. return old_path_or_error.error();
  20. auto new_path_or_error = try_copy_kstring_from_user(params.new_path);
  21. if (new_path_or_error.is_error())
  22. return new_path_or_error.error();
  23. return VirtualFileSystem::the().link(old_path_or_error.value()->view(), new_path_or_error.value()->view(), current_directory());
  24. }
  25. KResultOr<FlatPtr> Process::sys$symlink(Userspace<const Syscall::SC_symlink_params*> user_params)
  26. {
  27. VERIFY_PROCESS_BIG_LOCK_ACQUIRED(this)
  28. REQUIRE_PROMISE(cpath);
  29. Syscall::SC_symlink_params params;
  30. if (!copy_from_user(&params, user_params))
  31. return EFAULT;
  32. auto target = get_syscall_path_argument(params.target);
  33. if (target.is_error())
  34. return target.error();
  35. auto linkpath = get_syscall_path_argument(params.linkpath);
  36. if (linkpath.is_error())
  37. return linkpath.error();
  38. return VirtualFileSystem::the().symlink(target.value()->view(), linkpath.value()->view(), current_directory());
  39. }
  40. }