fileops.cc 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. #include <errno.h>
  2. #include <types/path.hpp>
  3. #include <kernel/process.hpp>
  4. #include <kernel/syscall.hpp>
  5. #include <kernel/vfs.hpp>
  6. long _syscall_symlink(interrupt_stack_normal* data)
  7. {
  8. SYSCALL_ARG1(const char __user*, target);
  9. SYSCALL_ARG2(const char __user*, linkpath);
  10. // TODO: use copy_from_user
  11. auto path = current_process->pwd + linkpath;
  12. auto* dent = fs::vfs_open(*current_process->root, path);
  13. if (dent)
  14. return -EEXIST;
  15. auto linkname = path.last_name();
  16. path.remove_last();
  17. dent = fs::vfs_open(*current_process->root, path);
  18. if (!dent)
  19. return -ENOENT;
  20. return dent->ind->fs->symlink(dent, linkname.c_str(), target);
  21. }
  22. long _syscall_readlink(interrupt_stack_normal* data)
  23. {
  24. SYSCALL_ARG1(const char __user*, pathname);
  25. SYSCALL_ARG2(char __user*, buf);
  26. SYSCALL_ARG3(size_t, buf_size);
  27. // TODO: use copy_from_user
  28. auto path = current_process->pwd + pathname;
  29. auto* dent = fs::vfs_open(*current_process->root, path, false);
  30. if (!dent)
  31. return -ENOENT;
  32. if (buf_size <= 0 || !S_ISLNK(dent->ind->mode))
  33. return -EINVAL;
  34. // TODO: use copy_to_user
  35. return dent->ind->fs->readlink(dent->ind, buf, buf_size);
  36. }