summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibCore/FilePermissionsMask.h
diff options
context:
space:
mode:
authorXavier Defrang <xavier.defrang@gmail.com>2021-12-24 15:34:30 +0100
committerBrian Gianforcaro <b.gianfo@gmail.com>2022-01-01 17:33:43 +0000
commit8b95423b5027e3363a3d7fc5e796bc4bf8a0ea15 (patch)
tree0d6b95182dcebd9216c7d98b5a23768211e37dc0 /Userland/Libraries/LibCore/FilePermissionsMask.h
parent0f729cebf4ff08b8d625c300a00be32241dbe27a (diff)
downloadserenity-8b95423b5027e3363a3d7fc5e796bc4bf8a0ea15.zip
LibCore: Add FilePermissionsMask
This class parses UNIX file permissions definitions in numeric (octal) or symbolic (ugoa+rwx) format and can apply them on a given file mode.
Diffstat (limited to 'Userland/Libraries/LibCore/FilePermissionsMask.h')
-rw-r--r--Userland/Libraries/LibCore/FilePermissionsMask.h40
1 files changed, 40 insertions, 0 deletions
diff --git a/Userland/Libraries/LibCore/FilePermissionsMask.h b/Userland/Libraries/LibCore/FilePermissionsMask.h
new file mode 100644
index 0000000000..c238344d91
--- /dev/null
+++ b/Userland/Libraries/LibCore/FilePermissionsMask.h
@@ -0,0 +1,40 @@
+/*
+ * Copyright (c) 2021, Xavier Defrang <xavier.defrang@gmail.com>
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+#pragma once
+
+#include <AK/Error.h>
+#include <AK/String.h>
+#include <sys/stat.h>
+
+namespace Core {
+
+class FilePermissionsMask {
+public:
+ static ErrorOr<FilePermissionsMask> parse(StringView string);
+ static ErrorOr<FilePermissionsMask> from_numeric_notation(StringView string);
+ static ErrorOr<FilePermissionsMask> from_symbolic_notation(StringView string);
+
+ FilePermissionsMask()
+ : m_clear_mask(0)
+ , m_write_mask(0)
+ {
+ }
+
+ FilePermissionsMask& assign_permissions(mode_t mode);
+ FilePermissionsMask& add_permissions(mode_t mode);
+ FilePermissionsMask& remove_permissions(mode_t mode);
+
+ mode_t apply(mode_t mode) const { return m_write_mask | (mode & ~m_clear_mask); }
+ mode_t clear_mask() const { return m_clear_mask; }
+ mode_t write_mask() const { return m_write_mask; }
+
+private:
+ mode_t m_clear_mask; // the bits that will be cleared
+ mode_t m_write_mask; // the bits that will be set
+};
+
+}