summaryrefslogtreecommitdiff
path: root/Userland
diff options
context:
space:
mode:
authorJunior Rantila <junior.rantila@gmail.com>2021-12-29 21:47:38 +0100
committerAndreas Kling <kling@serenityos.org>2022-01-06 18:20:07 +0100
commit9a38d1de07aa19af171d3b9143d0560d5e92cf47 (patch)
treeda2b809a5461108f6e79ce798bcedf3e933070fc /Userland
parent080c3164c7df77e622eea86541ae62d52fd4e471 (diff)
downloadserenity-9a38d1de07aa19af171d3b9143d0560d5e92cf47.zip
LibCore+ImageViewer: Add unlink() wrapper, use it
Diffstat (limited to 'Userland')
-rw-r--r--Userland/Applications/ImageViewer/main.cpp6
-rw-r--r--Userland/Libraries/LibCore/System.cpp16
-rw-r--r--Userland/Libraries/LibCore/System.h1
3 files changed, 20 insertions, 3 deletions
diff --git a/Userland/Applications/ImageViewer/main.cpp b/Userland/Applications/ImageViewer/main.cpp
index a3027f8908..60dd32162c 100644
--- a/Userland/Applications/ImageViewer/main.cpp
+++ b/Userland/Applications/ImageViewer/main.cpp
@@ -131,10 +131,10 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
if (msgbox_result == GUI::MessageBox::ExecCancel)
return;
- if (unlink(widget.path().characters()) < 0) {
- int saved_errno = errno;
+ auto unlinked_or_error = Core::System::unlink(widget.path());
+ if (unlinked_or_error.is_error()) {
GUI::MessageBox::show(window,
- String::formatted("unlink({}) failed: {}", path, strerror(saved_errno)),
+ String::formatted("unlink({}) failed: {}", path, unlinked_or_error.error()),
"Delete failed",
GUI::MessageBox::Type::Error);
diff --git a/Userland/Libraries/LibCore/System.cpp b/Userland/Libraries/LibCore/System.cpp
index 99b5b1cacc..46d21b2b13 100644
--- a/Userland/Libraries/LibCore/System.cpp
+++ b/Userland/Libraries/LibCore/System.cpp
@@ -644,6 +644,22 @@ ErrorOr<void> rename(StringView old_path, StringView new_path)
#endif
}
+ErrorOr<void> unlink(StringView path)
+{
+ if (path.is_null())
+ return Error::from_errno(EFAULT);
+
+#ifdef __serenity__
+ int rc = syscall(SC_unlink, path.characters_without_null_termination(), path.length());
+ HANDLE_SYSCALL_RETURN_VALUE("unlink"sv, rc, {});
+#else
+ String path_string = path;
+ if (::unlink(path_string.characters()) < 0)
+ return Error::from_syscall("unlink"sv, -errno);
+ return {};
+#endif
+}
+
ErrorOr<void> utime(StringView path, Optional<struct utimbuf> maybe_buf)
{
if (path.is_null())
diff --git a/Userland/Libraries/LibCore/System.h b/Userland/Libraries/LibCore/System.h
index f39f120bbe..97e60ad6b7 100644
--- a/Userland/Libraries/LibCore/System.h
+++ b/Userland/Libraries/LibCore/System.h
@@ -98,6 +98,7 @@ 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);
+ErrorOr<void> unlink(StringView path);
ErrorOr<void> utime(StringView path, Optional<struct utimbuf>);
ErrorOr<struct utsname> uname();
ErrorOr<Array<int, 2>> pipe2(int flags);