diff options
author | Andreas Kling <kling@serenityos.org> | 2021-12-16 21:40:42 +0100 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2021-12-16 22:48:17 +0100 |
commit | ead9c36c92c4d0dcb973a4f0e8e35ee5ba360bbd (patch) | |
tree | 2e13a481d6f4b15f733645dfc435b494e7a39b85 /Userland/Libraries | |
parent | fb4ffe22c81098b8552b5cc531ab0ba30bfb2bff (diff) | |
download | serenity-ead9c36c92c4d0dcb973a4f0e8e35ee5ba360bbd.zip |
LibCore: Add syscall wrapper for rename()
Diffstat (limited to 'Userland/Libraries')
-rw-r--r-- | Userland/Libraries/LibCore/System.cpp | 21 | ||||
-rw-r--r-- | Userland/Libraries/LibCore/System.h | 1 |
2 files changed, 22 insertions, 0 deletions
diff --git a/Userland/Libraries/LibCore/System.cpp b/Userland/Libraries/LibCore/System.cpp index 8defd74759..18d11244bf 100644 --- a/Userland/Libraries/LibCore/System.cpp +++ b/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, ¶ms); + 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 +} + } diff --git a/Userland/Libraries/LibCore/System.h b/Userland/Libraries/LibCore/System.h index 46e3dfb302..6bffc771c3 100644 --- a/Userland/Libraries/LibCore/System.h +++ b/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); } |