瀏覽代碼

LibCore: Add syscall wrapper for rename()

Andreas Kling 3 年之前
父節點
當前提交
ead9c36c92
共有 2 個文件被更改,包括 22 次插入0 次删除
  1. 21 0
      Userland/Libraries/LibCore/System.cpp
  2. 1 0
      Userland/Libraries/LibCore/System.h

+ 21 - 0
Userland/Libraries/LibCore/System.cpp

@@ -503,4 +503,25 @@ ErrorOr<int> mkstemp(Span<char> pattern)
     return fd;
 }
 
+ErrorOr<void> rename(StringView old_path, StringView new_path)
+{
+    if (old_path.is_null() || new_path.is_null())
+        return Error::from_errno(EFAULT);
+
+#ifdef __serenity__
+    Syscall::SC_rename_params params {
+        .old_path = { old_path.characters_without_null_termination(), old_path.length() },
+        .new_path = { new_path.characters_without_null_termination(), new_path.length() },
+    };
+    int rc = syscall(SC_rename, &params);
+    HANDLE_SYSCALL_RETURN_VALUE("rename"sv, rc, {});
+#else
+    String old_path_string = old_path;
+    String new_path_string = new_path;
+    if (::rename(old_path_string.characters(), new_path_string.characters()) < 0)
+        return Error::from_syscall("rename"sv, -errno);
+    return {};
+#endif
+}
+
 }

+ 1 - 0
Userland/Libraries/LibCore/System.h

@@ -67,5 +67,6 @@ ErrorOr<void> mkdir(StringView path, mode_t);
 ErrorOr<pid_t> fork();
 ErrorOr<int> mkstemp(Span<char> pattern);
 ErrorOr<void> fchmod(int fd, mode_t mode);
+ErrorOr<void> rename(StringView old_path, StringView new_path);
 
 }